Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

297 lines
7.5 KiB

  1. #include <stdio.h>
  2. #define INITGUID // must be before guid stuff
  3. #include "iisdebug.h"
  4. #include "certobj.h" // Interface header
  5. #include "certobj_i.c"
  6. BOOL TestCertObj(void);
  7. LPSTR StripWhitespace( LPSTR pszString )
  8. {
  9. LPSTR pszTemp = NULL;
  10. if ( pszString == NULL ) {
  11. return NULL;
  12. }
  13. while ( *pszString == ' ' || *pszString == '\t' ) {
  14. pszString += 1;
  15. }
  16. // Catch case where string consists entirely of whitespace or empty string.
  17. if ( *pszString == '\0' ) {
  18. return pszString;
  19. }
  20. pszTemp = pszString;
  21. pszString += lstrlenA(pszString) - 1;
  22. while ( *pszString == ' ' || *pszString == '\t' ) {
  23. *pszString = '\0';
  24. pszString -= 1;
  25. }
  26. return pszTemp;
  27. }
  28. void
  29. ShowHelp()
  30. {
  31. wprintf(L"tests the CertObj control\n\n");
  32. return;
  33. }
  34. int __cdecl
  35. main(
  36. int argc,
  37. char *argv[]
  38. )
  39. {
  40. BOOL fRet = FALSE;
  41. int argno;
  42. char * pArg = NULL;
  43. char * pCmdStart = NULL;
  44. char szTempString[MAX_PATH];
  45. int iGotParamS = FALSE;
  46. int iGotParamP = FALSE;
  47. int iDoA = FALSE;
  48. WCHAR wszDirPath[MAX_PATH];
  49. WCHAR wszTempString_S[MAX_PATH];
  50. WCHAR wszTempString_P[MAX_PATH];
  51. wszDirPath[0] = '\0';
  52. wszTempString_S[0] = '\0';
  53. wszTempString_P[0] = '\0';
  54. for(argno=1; argno<argc; argno++) {
  55. if ( argv[argno][0] == '-' || argv[argno][0] == '/' ) {
  56. switch (argv[argno][1]) {
  57. case 'a':
  58. case 'A':
  59. iDoA = TRUE;
  60. break;
  61. case 's':
  62. case 'S':
  63. // Get the string for this flag
  64. pArg = CharNextA(argv[argno]);
  65. pArg = CharNextA(pArg);
  66. if (*pArg == ':') {
  67. pArg = CharNextA(pArg);
  68. // Check if it's quoted
  69. if (*pArg == '\"') {
  70. pArg = CharNextA(pArg);
  71. pCmdStart = pArg;
  72. while ((*pArg) && (*pArg != '\"')){pArg = CharNextA(pArg);}
  73. } else {
  74. pCmdStart = pArg;
  75. while (*pArg){pArg = CharNextA(pArg);}
  76. }
  77. *pArg = '\0';
  78. lstrcpyA(szTempString, StripWhitespace(pCmdStart));
  79. // Convert to unicode
  80. MultiByteToWideChar(CP_ACP, 0, (LPCSTR)szTempString, -1, (LPWSTR) wszTempString_S, 50);
  81. iGotParamS = TRUE;
  82. }
  83. break;
  84. case 'p':
  85. case 'P':
  86. // Get the string for this flag
  87. pArg = CharNextA(argv[argno]);
  88. pArg = CharNextA(pArg);
  89. if (*pArg == ':') {
  90. pArg = CharNextA(pArg);
  91. // Check if it's quoted
  92. if (*pArg == '\"') {
  93. pArg = CharNextA(pArg);
  94. pCmdStart = pArg;
  95. while ((*pArg) && (*pArg != '\"')){pArg = CharNextA(pArg);}
  96. } else {
  97. pCmdStart = pArg;
  98. while (*pArg){pArg = CharNextA(pArg);}
  99. }
  100. *pArg = '\0';
  101. lstrcpyA(szTempString, StripWhitespace(pCmdStart));
  102. // Convert to unicode
  103. MultiByteToWideChar(CP_ACP, 0, (LPCSTR)szTempString, -1, (LPWSTR) wszTempString_P, 50);
  104. iGotParamP = TRUE;
  105. }
  106. break;
  107. case '?':
  108. goto main_exit_with_help;
  109. break;
  110. }
  111. } else {
  112. if ( *wszDirPath == '\0' ) {
  113. // if no arguments, then get the filename portion
  114. MultiByteToWideChar(CP_ACP, 0, argv[argno], -1, (LPWSTR) wszDirPath, 50);
  115. }
  116. }
  117. }
  118. fRet = TestCertObj();
  119. goto main_exit_gracefully;
  120. main_exit_gracefully:
  121. exit(fRet);
  122. main_exit_with_help:
  123. ShowHelp();
  124. exit(fRet);
  125. }
  126. HRESULT
  127. HereIsVtArrayGimmieBinary(
  128. VARIANT * lpVarSrcObject,
  129. DWORD * cbBinaryBufferSize,
  130. char **pbBinaryBuffer,
  131. BOOL bReturnBinaryAsVT_VARIANT
  132. )
  133. {
  134. HRESULT hr = S_OK;
  135. LONG dwSLBound = 0;
  136. LONG dwSUBound = 0;
  137. CHAR HUGEP *pArray = NULL;
  138. if (NULL == cbBinaryBufferSize || NULL == pbBinaryBuffer)
  139. {
  140. hr = E_INVALIDARG;
  141. goto HereIsVtArrayGimmieBinary_Exit;
  142. }
  143. if (bReturnBinaryAsVT_VARIANT)
  144. {
  145. hr = VariantChangeType(lpVarSrcObject,lpVarSrcObject,0,VT_ARRAY | VT_VARIANT);
  146. }
  147. else
  148. {
  149. hr = VariantChangeType(lpVarSrcObject,lpVarSrcObject,0,VT_ARRAY | VT_UI1);
  150. }
  151. if (FAILED(hr))
  152. {
  153. if (hr != E_OUTOFMEMORY)
  154. {
  155. IISDebugOutput(_T("OLE_E_CANTCONVERT 1,hr=0x%x\n"),hr);
  156. hr = OLE_E_CANTCONVERT;
  157. }
  158. goto HereIsVtArrayGimmieBinary_Exit;
  159. }
  160. if (bReturnBinaryAsVT_VARIANT)
  161. {
  162. if( lpVarSrcObject->vt != (VT_ARRAY | VT_VARIANT))
  163. {
  164. hr = OLE_E_CANTCONVERT;
  165. goto HereIsVtArrayGimmieBinary_Exit;
  166. }
  167. }
  168. else
  169. {
  170. if( lpVarSrcObject->vt != (VT_ARRAY | VT_UI1))
  171. {
  172. hr = OLE_E_CANTCONVERT;
  173. IISDebugOutput(_T("OLE_E_CANTCONVERT 2\n"));
  174. goto HereIsVtArrayGimmieBinary_Exit;
  175. }
  176. }
  177. hr = SafeArrayGetLBound(V_ARRAY(lpVarSrcObject),1,(long FAR *) &dwSLBound );
  178. if (FAILED(hr))
  179. {goto HereIsVtArrayGimmieBinary_Exit;}
  180. hr = SafeArrayGetUBound(V_ARRAY(lpVarSrcObject),1,(long FAR *) &dwSUBound );
  181. if (FAILED(hr))
  182. {goto HereIsVtArrayGimmieBinary_Exit;}
  183. //*pbBinaryBuffer = (LPBYTE) AllocADsMem(dwSUBound - dwSLBound + 1);
  184. *pbBinaryBuffer = (char *) ::CoTaskMemAlloc(dwSUBound - dwSLBound + 1);
  185. if (*pbBinaryBuffer == NULL)
  186. {
  187. hr = E_OUTOFMEMORY;
  188. goto HereIsVtArrayGimmieBinary_Exit;
  189. }
  190. *cbBinaryBufferSize = dwSUBound - dwSLBound + 1;
  191. hr = SafeArrayAccessData( V_ARRAY(lpVarSrcObject),(void HUGEP * FAR *) &pArray );
  192. if (FAILED(hr))
  193. {goto HereIsVtArrayGimmieBinary_Exit;}
  194. memcpy(*pbBinaryBuffer,pArray,dwSUBound-dwSLBound+1);
  195. SafeArrayUnaccessData( V_ARRAY(lpVarSrcObject) );
  196. HereIsVtArrayGimmieBinary_Exit:
  197. return hr;
  198. }
  199. BOOL
  200. TestCertObj(void)
  201. {
  202. BOOL fRet = FALSE;
  203. HRESULT hr;
  204. IIISCertObj *pTheObject = NULL;
  205. BSTR bstrFileName = SysAllocString(L"c:\\test.pfx");
  206. BSTR bstrFilePassword = SysAllocString(L"www");
  207. VARIANT VtArray;
  208. DWORD cbBinaryBufferSize = 0;
  209. char * pbBinaryBuffer = NULL;
  210. IISDebugOutput(_T("TestCertObj: Start\n"));
  211. if( FAILED (CoInitializeEx( NULL, COINIT_MULTITHREADED )))
  212. {
  213. return FALSE;
  214. }
  215. // Try to instantiante the object on the remote server...
  216. // with the supplied authentication info (pcsiName)
  217. //#define CLSCTX_SERVER (CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER | CLSCTX_REMOTE_SERVER)
  218. //#define CLSCTX_ALL (CLSCTX_INPROC_HANDLER | CLSCTX_SERVER)
  219. // this one seems to work with surrogates..
  220. hr = CoCreateInstance(CLSID_IISCertObj,NULL,CLSCTX_SERVER,IID_IIISCertObj,(void **)&pTheObject);
  221. if (FAILED(hr))
  222. {
  223. IISDebugOutput(_T("CoCreateInstanceEx on CLSID_IISCertObj failed! code=0x%x\n"),hr);
  224. goto TestCertObj_Exit;
  225. }
  226. // at this point we were able to instantiate the com object on the server (local or remote)
  227. hr = pTheObject->ImportToCertStore(bstrFileName, bstrFilePassword,&VtArray);
  228. IISDebugOutput(_T("returned ImportToCertStore, code=0x%x\n"),hr);
  229. // we have a VtArray now.
  230. // change it back to a binary blob
  231. hr = HereIsVtArrayGimmieBinary(&VtArray,&cbBinaryBufferSize,&pbBinaryBuffer,FALSE);
  232. IISDebugOutput(_T("returned HereIsVtArrayGimmieBinary, code=0x%x\n"),hr);
  233. IISDebugOutput(_T("Blob=%d,%p\n"),cbBinaryBufferSize,pbBinaryBuffer);
  234. DebugBreak();
  235. TestCertObj_Exit:
  236. if (pTheObject)
  237. {
  238. pTheObject->Release();
  239. pTheObject = NULL;
  240. }
  241. CoUninitialize();
  242. IISDebugOutput(_T("TestCertObj: End\n"));
  243. return fRet;
  244. }