Leaked source code of windows server 2003
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.

327 lines
7.3 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1995 - 1999
  6. //
  7. // File: Cert2Spc.cpp
  8. //
  9. // Contents: Copy certs and/or CRLs to a SPC file.
  10. //
  11. // A SPC file is an ASN.1 encoded PKCS #7 SignedData message
  12. // containing certificates and/or CRLs.
  13. //
  14. // See Usage() for list of options.
  15. //
  16. //
  17. // Functions: main
  18. //
  19. // History: 05-May-96 philh created
  20. // History: 08-August-97 xiaohs input can be a spc, serialized store
  21. //
  22. //--------------------------------------------------------------------------
  23. #include <windows.h>
  24. #include <assert.h>
  25. #include "wincrypt.h"
  26. #include "resource.h"
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29. #include <stdarg.h>
  30. #include <string.h>
  31. #include <memory.h>
  32. #include <time.h>
  33. #include <dbgdef.h>
  34. #include <unicode.h>
  35. #include <wchar.h>
  36. #include "toolutl.h"
  37. //--------------------------------------------------------------------------
  38. //
  39. // Global Data
  40. //
  41. //----------------------------------------------------------------------------
  42. HMODULE hModule=NULL;
  43. #define ITEM_CERT 0x00000001
  44. #define ITEM_CTL 0x00000002
  45. #define ITEM_CRL 0x00000004
  46. //---------------------------------------------------------------------------
  47. // Get the hModule hanlder and init
  48. //---------------------------------------------------------------------------
  49. BOOL InitModule()
  50. {
  51. if(!(hModule=GetModuleHandle(NULL)))
  52. return FALSE;
  53. return TRUE;
  54. }
  55. //---------------------------------------------------------------------------
  56. // Get the hModule hanlder and init
  57. //---------------------------------------------------------------------------
  58. static void Usage(void)
  59. {
  60. IDSwprintf(hModule, IDS_SYNTAX);
  61. }
  62. BOOL MoveItem(HCERTSTORE hSrcStore,
  63. HCERTSTORE hDesStore,
  64. DWORD dwItem);
  65. //---------------------------------------------------------------------------
  66. // wmain
  67. //---------------------------------------------------------------------------
  68. extern "C" int __cdecl
  69. wmain(int argc, WCHAR *wargv[])
  70. {
  71. int ReturnStatus=-1;
  72. HCERTSTORE hStore = NULL;
  73. HCERTSTORE hFileStore=NULL;
  74. HANDLE hFile = INVALID_HANDLE_VALUE;
  75. LPWSTR pwszFilename=NULL;
  76. BYTE *pbEncoded = NULL;
  77. DWORD cbEncoded =0;
  78. if (argc < 3)
  79. {
  80. Usage();
  81. return -1;
  82. }
  83. if(!InitModule())
  84. return -1;
  85. // Open temp store to contain the certs and/or CRLs to be written
  86. // to the spc file
  87. if (NULL == (hStore = CertOpenStore(
  88. CERT_STORE_PROV_MEMORY,
  89. 0, // dwCertEncodingType
  90. 0, // hCryptProv,
  91. 0, // dwFlags
  92. NULL // pvPara
  93. )))
  94. {
  95. IDSwprintf(hModule,IDS_CAN_NOT_OPEN_STORE);
  96. goto ErrorReturn;
  97. }
  98. //If there is any .crt or .crl file left
  99. while (--argc > 1)
  100. {
  101. pwszFilename = *(++wargv);
  102. if (S_OK != RetrieveBLOBFromFile(pwszFilename, &cbEncoded, &pbEncoded))
  103. {
  104. IDSwprintf(hModule, IDS_CAN_NOT_LOAD, pwszFilename);
  105. goto ErrorReturn;
  106. }
  107. //deal with .crl file
  108. if (!CertAddEncodedCRLToStore(
  109. hStore,
  110. X509_ASN_ENCODING,
  111. pbEncoded,
  112. cbEncoded,
  113. CERT_STORE_ADD_USE_EXISTING,
  114. NULL // ppCrlContext
  115. ))
  116. {
  117. //open a certificate store
  118. hFileStore=CertOpenStore(CERT_STORE_PROV_FILENAME_W,
  119. X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
  120. NULL,
  121. 0,
  122. pwszFilename);
  123. if(!hFileStore)
  124. {
  125. IDSwprintf(hModule, IDS_CAN_NOT_LOAD, pwszFilename);
  126. goto ErrorReturn;
  127. }
  128. //copy all the certs and CRLs from hFileStore to hStore
  129. if(!MoveItem(hFileStore, hStore, ITEM_CERT|ITEM_CRL))
  130. {
  131. IDSwprintf(hModule, IDS_CAN_NOT_LOAD, pwszFilename);
  132. goto ErrorReturn;
  133. }
  134. //close store
  135. CertCloseStore(hFileStore, 0);
  136. hFileStore=NULL;
  137. }
  138. UnmapViewOfFile(pbEncoded);
  139. pbEncoded = NULL;
  140. cbEncoded=0;
  141. }
  142. pwszFilename = *(++wargv);
  143. hFile = CreateFileU(
  144. pwszFilename,
  145. GENERIC_READ | GENERIC_WRITE,
  146. FILE_SHARE_READ,
  147. NULL, // lpsa
  148. CREATE_ALWAYS,
  149. FILE_ATTRIBUTE_NORMAL,
  150. NULL // hTemplateFile
  151. );
  152. if (hFile == INVALID_HANDLE_VALUE)
  153. {
  154. IDSwprintf(hModule, IDS_CAN_NOT_OPEN_FILE, pwszFilename);
  155. goto ErrorReturn;
  156. }
  157. if (!CertSaveStore(hStore,
  158. X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
  159. CERT_STORE_SAVE_AS_PKCS7,
  160. CERT_STORE_SAVE_TO_FILE,
  161. (void *)hFile,
  162. 0 //dwFlags
  163. ))
  164. {
  165. DWORD dwErr = GetLastError();
  166. IDSwprintf(hModule, IDS_ERROR_OUTPUT, dwErr, dwErr);
  167. goto ErrorReturn;
  168. }
  169. ReturnStatus = 0;
  170. IDSwprintf(hModule, IDS_SUCCEEDED);
  171. goto CommonReturn;
  172. ErrorReturn:
  173. ReturnStatus = -1;
  174. //print out an error msg
  175. IDSwprintf(hModule, IDS_FAILED);
  176. CommonReturn:
  177. if (pbEncoded)
  178. UnmapViewOfFile(pbEncoded);
  179. if (hFileStore)
  180. CertCloseStore(hFileStore, 0);
  181. if (hStore)
  182. CertCloseStore(hStore, 0);
  183. if (hFile != INVALID_HANDLE_VALUE)
  184. CloseHandle(hFile);
  185. return ReturnStatus;
  186. }
  187. //-------------------------------------------------------------------------
  188. //
  189. // Move Certs/CRls/CTLs from the source store to the destination
  190. //
  191. //-------------------------------------------------------------------------
  192. BOOL MoveItem(HCERTSTORE hSrcStore,
  193. HCERTSTORE hDesStore,
  194. DWORD dwItem)
  195. {
  196. BOOL fResult=FALSE;
  197. DWORD dwCRLFlag=0;
  198. PCCERT_CONTEXT pCertContext=NULL;
  199. PCCERT_CONTEXT pCertPre=NULL;
  200. PCCRL_CONTEXT pCRLContext=NULL;
  201. PCCRL_CONTEXT pCRLPre=NULL;
  202. PCCTL_CONTEXT pCTLContext=NULL;
  203. PCCTL_CONTEXT pCTLPre=NULL;
  204. //add the certs
  205. if(dwItem & ITEM_CERT)
  206. {
  207. while(pCertContext=CertEnumCertificatesInStore(hSrcStore, pCertPre))
  208. {
  209. if(!CertAddCertificateContextToStore(hDesStore,
  210. pCertContext,
  211. CERT_STORE_ADD_REPLACE_EXISTING,
  212. NULL))
  213. goto CLEANUP;
  214. pCertPre=pCertContext;
  215. }
  216. }
  217. //add the CTLs
  218. if(dwItem & ITEM_CTL)
  219. {
  220. while(pCTLContext=CertEnumCTLsInStore(hSrcStore, pCTLPre))
  221. {
  222. if(!CertAddCTLContextToStore(hDesStore,
  223. pCTLContext,
  224. CERT_STORE_ADD_REPLACE_EXISTING,
  225. NULL))
  226. goto CLEANUP;
  227. pCTLPre=pCTLContext;
  228. }
  229. }
  230. //add the CRLs
  231. if(dwItem & ITEM_CRL)
  232. {
  233. while(pCRLContext=CertGetCRLFromStore(hSrcStore,
  234. NULL,
  235. pCRLPre,
  236. &dwCRLFlag))
  237. {
  238. if(!CertAddCRLContextToStore(hDesStore,
  239. pCRLContext,
  240. CERT_STORE_ADD_REPLACE_EXISTING,
  241. NULL))
  242. goto CLEANUP;
  243. pCRLPre=pCRLContext;
  244. }
  245. }
  246. fResult=TRUE;
  247. CLEANUP:
  248. if(pCertContext)
  249. CertFreeCertificateContext(pCertContext);
  250. if(pCTLContext)
  251. CertFreeCTLContext(pCTLContext);
  252. if(pCRLContext)
  253. CertFreeCRLContext(pCRLContext);
  254. return fResult;
  255. }