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.

5284 lines
166 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1995 - 1999
  6. //
  7. // File: certhlpr.cpp
  8. //
  9. // Contents: Certificate and CRL Helper APIs
  10. //
  11. // Functions: CertHelperDllMain
  12. // I_CryptGetDefaultCryptProv
  13. // I_CryptGetDefaultCryptProvForEncrypt
  14. // CertCompareIntegerBlob
  15. // CertCompareCertificate
  16. // CertCompareCertificateName
  17. // CertIsRDNAttrsInCertificateName
  18. // CertComparePublicKeyInfo
  19. // CryptVerifyCertificateSignature
  20. // CryptHashCertificate
  21. // CryptHashToBeSigned
  22. // CryptSignCertificate
  23. // CryptSignAndEncodeCertificate
  24. // CertVerifyTimeValidity
  25. // CertVerifyCRLTimeValidity
  26. // CertVerifyValidityNesting
  27. // CertVerifyCRLRevocation
  28. // CertAlgIdToOID
  29. // CertOIDToAlgId
  30. // CertFindExtension
  31. // CertFindAttribute
  32. // CertFindRDNAttr
  33. // CertGetIntendedKeyUsage
  34. // CertGetPublicKeyLength
  35. // CryptHashPublicKeyInfo
  36. //
  37. // I_CertCompareCertAndProviderPublicKey
  38. // CryptFindCertificateKeyProvInfo
  39. //
  40. // CryptCreatePublicKeyInfo
  41. // CryptConvertPublicKeyInfo
  42. // CryptExportPublicKeyInfo
  43. // CryptExportPublicKeyInfoEx
  44. // CryptImportPublicKeyInfo
  45. // CryptImportPublicKeyInfoEx
  46. // CryptCreateKeyIdentifierFromCSP
  47. //
  48. // CryptInstallDefaultContext
  49. // CryptUninstallDefaultContext
  50. //
  51. // History: 23-Feb-96 philh created
  52. //--------------------------------------------------------------------------
  53. #include "global.hxx"
  54. #include <dbgdef.h>
  55. // All the *pvInfo extra stuff needs to be aligned
  56. #define INFO_LEN_ALIGN(Len) ((Len + 7) & ~7)
  57. #define NULL_ASN_TAG 0x05
  58. //+=========================================================================
  59. // CryptCreatePublicKeyInfo, EncodePublicKeyAndParameters
  60. // and CryptConvertPublicKeyInfo functions
  61. //-=========================================================================
  62. // The following should be moved to wincrypt.x
  63. // If CRYPT_ALLOC_FLAG is set, *pvPubKeyInfo is updated with a LocalAlloc'ed
  64. // pointer to a CERT_PUBLIC_KEY_INFO data structure which must be freed by
  65. // calling LocalFree. Otherwise, pvPubKeyInfo points to a user allocated
  66. // CERT_PUBLIC_KEY_INFO data structure which is updated.
  67. WINCRYPT32API
  68. BOOL
  69. WINAPI
  70. CryptCreatePublicKeyInfo(
  71. IN DWORD dwCertEncodingType,
  72. IN OPTIONAL LPCSTR pszPubKeyOID,
  73. IN const PUBLICKEYSTRUC *pPubKeyStruc,
  74. IN DWORD cbPubKeyStruc,
  75. IN DWORD dwFlags,
  76. IN OPTIONAL void *pvReserved,
  77. OUT void *pvPubKeyInfo,
  78. IN OUT DWORD *pcbPubKeyInfo
  79. );
  80. #define CRYPT_ALLOC_FLAG 0x8000
  81. #define CRYPT_OID_ENCODE_PUBLIC_KEY_AND_PARAMETERS_FUNC \
  82. "CryptDllEncodePublicKeyAndParameters"
  83. // The returned encoded public keys and parameters are LocalAlloc'ed.
  84. typedef BOOL (WINAPI *PFN_CRYPT_ENCODE_PUBLIC_KEY_AND_PARAMETERS)(
  85. IN DWORD dwCertEncodingType,
  86. IN OPTIONAL LPCSTR pszPubKeyOID,
  87. IN const PUBLICKEYSTRUC *pPubKeyStruc,
  88. IN DWORD cbPubKeyStruc,
  89. IN DWORD dwFlags,
  90. IN OPTIONAL void *pvReserved,
  91. OUT BYTE **ppbEncodedPubKey,
  92. OUT DWORD *pcbEncodedPubKey,
  93. OUT BYTE **ppbEncodedParameters,
  94. OUT DWORD *pcbEncodedParameters
  95. );
  96. // If CRYPT_ALLOC_FLAG is set, *pvPubKeyStruc is updated with a LocalAlloc'ed
  97. // pointer to a PUBLICKEYSTRUC data structure which must be freed by calling
  98. // LocalFree. Otherwise, pvPubKeyStruc points to a user allocated
  99. // PUBLICKEYSTRUC data structure which is updated.
  100. WINCRYPT32API
  101. BOOL
  102. WINAPI
  103. CryptConvertPublicKeyInfo(
  104. IN DWORD dwCertEncodingType,
  105. IN PCERT_PUBLIC_KEY_INFO pPubKeyInfo,
  106. IN DWORD dwFlags,
  107. IN OPTIONAL void *pvReserved,
  108. OUT void *pvPubKeyStruc,
  109. IN OUT DWORD *pcbPubKeyStruc
  110. );
  111. #define CRYPT_OID_CONVERT_PUBLIC_KEY_INFO_FUNC "CryptDllConvertPublicKeyInfo"
  112. typedef BOOL (WINAPI *PFN_CRYPT_CONVERT_PUBLIC_KEY_INFO)(
  113. IN DWORD dwCertEncodingType,
  114. IN PCERT_PUBLIC_KEY_INFO pPubKeyInfo,
  115. IN DWORD dwFlags,
  116. IN OPTIONAL void *pvReserved,
  117. OUT void *pvPubKeyStruc,
  118. IN OUT DWORD *pcbPubKeyStruc
  119. );
  120. // End of what should be moved to wincrypt.x
  121. static HCRYPTOIDFUNCSET hEncodePubKeyFuncSet;
  122. static HCRYPTOIDFUNCSET hConvertPubKeyFuncSet;
  123. //+-------------------------------------------------------------------------
  124. // Encode the RSA public key and parameters
  125. //--------------------------------------------------------------------------
  126. static BOOL WINAPI EncodeRSAPublicKeyAndParameters(
  127. IN DWORD dwCertEncodingType,
  128. IN OPTIONAL LPCSTR pszPubKeyOID,
  129. IN const PUBLICKEYSTRUC *pPubKeyStruc,
  130. IN DWORD cbPubKeyStruc,
  131. IN DWORD dwFlags,
  132. IN OPTIONAL void *pvReserved,
  133. OUT BYTE **ppbEncodedPubKey,
  134. OUT DWORD *pcbEncodedPubKey,
  135. OUT BYTE **ppbEncodedParameters,
  136. OUT DWORD *pcbEncodedParameters
  137. );
  138. //+-------------------------------------------------------------------------
  139. // Convert as an RSA public key
  140. //--------------------------------------------------------------------------
  141. static BOOL WINAPI ConvertRSAPublicKeyInfo(
  142. IN DWORD dwCertEncodingType,
  143. IN PCERT_PUBLIC_KEY_INFO pPubKeyInfo,
  144. IN DWORD dwFlags,
  145. IN OPTIONAL void *pvReserved,
  146. OUT void *pvPubKeyStruc,
  147. IN OUT DWORD *pcbPubKeyStruc
  148. );
  149. //+-------------------------------------------------------------------------
  150. // Encode the DSS public key and parameters
  151. //--------------------------------------------------------------------------
  152. static BOOL WINAPI EncodeDSSPublicKeyAndParameters(
  153. IN DWORD dwCertEncodingType,
  154. IN OPTIONAL LPCSTR pszPubKeyOID,
  155. IN const PUBLICKEYSTRUC *pPubKeyStruc,
  156. IN DWORD cbPubKeyStruc,
  157. IN DWORD dwFlags,
  158. IN OPTIONAL void *pvReserved,
  159. OUT BYTE **ppbEncodedPubKey,
  160. OUT DWORD *pcbEncodedPubKey,
  161. OUT BYTE **ppbEncodedParameters,
  162. OUT DWORD *pcbEncodedParameters
  163. );
  164. //+-------------------------------------------------------------------------
  165. // Convert as an DSS public key
  166. //--------------------------------------------------------------------------
  167. static BOOL WINAPI ConvertDSSPublicKeyInfo(
  168. IN DWORD dwCertEncodingType,
  169. IN PCERT_PUBLIC_KEY_INFO pPubKeyInfo,
  170. IN DWORD dwFlags,
  171. IN OPTIONAL void *pvReserved,
  172. OUT void *pvPubKeyStruc,
  173. IN OUT DWORD *pcbPubKeyStruc
  174. );
  175. //+-------------------------------------------------------------------------
  176. // Encode the RSA DH public key and parameters
  177. //--------------------------------------------------------------------------
  178. static BOOL WINAPI EncodeRSADHPublicKeyAndParameters(
  179. IN DWORD dwCertEncodingType,
  180. IN OPTIONAL LPCSTR pszPubKeyOID,
  181. IN const PUBLICKEYSTRUC *pPubKeyStruc,
  182. IN DWORD cbPubKeyStruc,
  183. IN DWORD dwFlags,
  184. IN OPTIONAL void *pvReserved,
  185. OUT BYTE **ppbEncodedPubKey,
  186. OUT DWORD *pcbEncodedPubKey,
  187. OUT BYTE **ppbEncodedParameters,
  188. OUT DWORD *pcbEncodedParameters
  189. );
  190. //+-------------------------------------------------------------------------
  191. // Encode the X942 DH public key and parameters
  192. //--------------------------------------------------------------------------
  193. static BOOL WINAPI EncodeX942DHPublicKeyAndParameters(
  194. IN DWORD dwCertEncodingType,
  195. IN OPTIONAL LPCSTR pszPubKeyOID,
  196. IN const PUBLICKEYSTRUC *pPubKeyStruc,
  197. IN DWORD cbPubKeyStruc,
  198. IN DWORD dwFlags,
  199. IN OPTIONAL void *pvReserved,
  200. OUT BYTE **ppbEncodedPubKey,
  201. OUT DWORD *pcbEncodedPubKey,
  202. OUT BYTE **ppbEncodedParameters,
  203. OUT DWORD *pcbEncodedParameters
  204. );
  205. static const CRYPT_OID_FUNC_ENTRY EncodePubKeyFuncTable[] = {
  206. szOID_RSA_RSA, EncodeRSAPublicKeyAndParameters,
  207. szOID_OIWSEC_rsaXchg, EncodeRSAPublicKeyAndParameters,
  208. szOID_OIWSEC_dsa, EncodeDSSPublicKeyAndParameters,
  209. szOID_X957_DSA, EncodeDSSPublicKeyAndParameters,
  210. szOID_ANSI_X942_DH, EncodeX942DHPublicKeyAndParameters,
  211. szOID_RSA_DH, EncodeRSADHPublicKeyAndParameters,
  212. };
  213. #define ENCODE_PUB_KEY_FUNC_COUNT (sizeof(EncodePubKeyFuncTable) / \
  214. sizeof(EncodePubKeyFuncTable[0]))
  215. static const CRYPT_OID_FUNC_ENTRY ConvertPubKeyFuncTable[] = {
  216. szOID_RSA_RSA, ConvertRSAPublicKeyInfo,
  217. szOID_OIWSEC_rsaXchg, ConvertRSAPublicKeyInfo,
  218. szOID_OIWSEC_dsa, ConvertDSSPublicKeyInfo,
  219. szOID_X957_DSA, ConvertDSSPublicKeyInfo,
  220. };
  221. #define CONVERT_PUB_KEY_FUNC_COUNT (sizeof(ConvertPubKeyFuncTable) / \
  222. sizeof(ConvertPubKeyFuncTable[0]))
  223. //+=========================================================================
  224. // CryptExportPublicKeyInfoEx and CryptImportPublicKeyInfoEx OID
  225. // installable functions.
  226. //-=========================================================================
  227. typedef BOOL (WINAPI *PFN_EXPORT_PUB_KEY_FUNC) (
  228. IN HCRYPTPROV hCryptProv,
  229. IN DWORD dwKeySpec,
  230. IN DWORD dwCertEncodingType,
  231. IN LPSTR pszPublicKeyObjId,
  232. IN DWORD dwFlags,
  233. IN OPTIONAL void *pvAuxInfo,
  234. OUT PCERT_PUBLIC_KEY_INFO pInfo,
  235. IN OUT DWORD *pcbInfo
  236. );
  237. typedef BOOL (WINAPI *PFN_IMPORT_PUB_KEY_FUNC) (
  238. IN HCRYPTPROV hCryptProv,
  239. IN DWORD dwCertEncodingType,
  240. IN PCERT_PUBLIC_KEY_INFO pInfo,
  241. IN ALG_ID aiKeyAlg,
  242. IN DWORD dwFlags,
  243. IN OPTIONAL void *pvAuxInfo,
  244. OUT HCRYPTKEY *phKey
  245. );
  246. static HCRYPTOIDFUNCSET hExportPubKeyFuncSet;
  247. static HCRYPTOIDFUNCSET hImportPubKeyFuncSet;
  248. //+-------------------------------------------------------------------------
  249. // Default CryptProvs. Once acquired, not released until ProcessDetach.
  250. //--------------------------------------------------------------------------
  251. #define DEFAULT_RSA_CRYPT_PROV 0
  252. #define DEFAULT_DSS_CRYPT_PROV 1
  253. #define DEFAULT_ENCRYPT_BASE_RSA_CRYPT_PROV 2
  254. #define DEFAULT_ENCRYPT_ENH_RSA_CRYPT_PROV 3
  255. #define DEFAULT_ENCRYPT_DH_CRYPT_PROV 4
  256. #define DEFAULT_CRYPT_PROV_CNT 5
  257. static HCRYPTPROV rghDefaultCryptProv[DEFAULT_CRYPT_PROV_CNT];
  258. static CRITICAL_SECTION DefaultCryptProvCriticalSection;
  259. typedef struct _ENCRYPT_ALG_INFO ENCRYPT_ALG_INFO, *PENCRYPT_ALG_INFO;
  260. struct _ENCRYPT_ALG_INFO {
  261. ALG_ID aiAlgid;
  262. DWORD dwMinLen;
  263. DWORD dwMaxLen;
  264. PENCRYPT_ALG_INFO pNext;
  265. };
  266. static BOOL fLoadedRSAEncryptAlgInfo = FALSE;
  267. static PENCRYPT_ALG_INFO pRSAEncryptAlgInfoHead = NULL;
  268. //+=========================================================================
  269. // DefaultContext Function Forward References and Data Structures
  270. //-=========================================================================
  271. //
  272. // dwDefaultTypes:
  273. // CRYPT_DEFAULT_CONTEXT_CERT_SIGN_OID (pvDefaultPara :== pszOID)
  274. BOOL
  275. WINAPI
  276. I_CryptGetDefaultContext(
  277. IN DWORD dwDefaultType,
  278. IN const void *pvDefaultPara,
  279. OUT HCRYPTPROV *phCryptProv,
  280. OUT HCRYPTDEFAULTCONTEXT *phDefaultContext
  281. );
  282. // hDefaultContext is only NON-null for Process default context
  283. void
  284. WINAPI
  285. I_CryptFreeDefaultContext(
  286. HCRYPTDEFAULTCONTEXT hDefaultContext
  287. );
  288. typedef struct _DEFAULT_CONTEXT DEFAULT_CONTEXT, *PDEFAULT_CONTEXT;
  289. struct _DEFAULT_CONTEXT {
  290. HCRYPTPROV hCryptProv;
  291. DWORD dwDefaultType;
  292. union {
  293. // CRYPT_DEFAULT_CONTEXT_CERT_SIGN_OID (note, converted to MULTI_)
  294. // CRYPT_DEFAULT_CONTEXT_MULTI_CERT_SIGN_OID
  295. PCRYPT_DEFAULT_CONTEXT_MULTI_OID_PARA pOIDDefaultPara;
  296. };
  297. DWORD dwFlags;
  298. PDEFAULT_CONTEXT pNext;
  299. PDEFAULT_CONTEXT pPrev;
  300. // Following applicable to Process DefaultContext
  301. LONG lRefCnt;
  302. HANDLE hWait;
  303. };
  304. static BOOL fHasThreadDefaultContext;
  305. static HCRYPTTLS hTlsDefaultContext;
  306. static BOOL fHasProcessDefaultContext;
  307. static CRITICAL_SECTION DefaultContextCriticalSection;
  308. static PDEFAULT_CONTEXT pProcessDefaultContextHead;
  309. //+-------------------------------------------------------------------------
  310. // Default CryptProv: initialization and free
  311. //--------------------------------------------------------------------------
  312. static BOOL InitDefaultCryptProv()
  313. {
  314. return Pki_InitializeCriticalSection(&DefaultCryptProvCriticalSection);
  315. }
  316. static void FreeDefaultCryptProv()
  317. {
  318. PENCRYPT_ALG_INFO pAlgInfo;
  319. DWORD cProv = DEFAULT_CRYPT_PROV_CNT;
  320. while (cProv--) {
  321. HCRYPTPROV hProv = rghDefaultCryptProv[cProv];
  322. if (hProv)
  323. CryptReleaseContext(hProv, 0);
  324. }
  325. pAlgInfo = pRSAEncryptAlgInfoHead;
  326. while (pAlgInfo) {
  327. PENCRYPT_ALG_INFO pDeleteAlgInfo = pAlgInfo;
  328. pAlgInfo = pAlgInfo->pNext;
  329. PkiFree(pDeleteAlgInfo);
  330. }
  331. DeleteCriticalSection(&DefaultCryptProvCriticalSection);
  332. }
  333. static
  334. VOID
  335. WINAPI
  336. DetachDefaultContext(
  337. IN LPVOID pv
  338. )
  339. {
  340. PDEFAULT_CONTEXT pDefaultContext = (PDEFAULT_CONTEXT) pv;
  341. while (pDefaultContext) {
  342. PDEFAULT_CONTEXT pFree = pDefaultContext;
  343. pDefaultContext = pDefaultContext->pNext;
  344. if (pFree->dwFlags & CRYPT_DEFAULT_CONTEXT_AUTO_RELEASE_FLAG)
  345. CryptReleaseContext(pFree->hCryptProv, 0);
  346. PkiFree(pFree);
  347. }
  348. }
  349. //+-------------------------------------------------------------------------
  350. // Dll initialization
  351. //--------------------------------------------------------------------------
  352. BOOL
  353. WINAPI
  354. CertHelperDllMain(
  355. HMODULE hInst,
  356. ULONG ulReason,
  357. LPVOID lpReserved)
  358. {
  359. BOOL fRet;
  360. switch (ulReason) {
  361. case DLL_PROCESS_ATTACH:
  362. // Public key function setup
  363. if (NULL == (hExportPubKeyFuncSet = CryptInitOIDFunctionSet(
  364. CRYPT_OID_EXPORT_PUBLIC_KEY_INFO_FUNC,
  365. 0)))
  366. goto CryptInitOIDFunctionSetError;
  367. if (NULL == (hImportPubKeyFuncSet = CryptInitOIDFunctionSet(
  368. CRYPT_OID_IMPORT_PUBLIC_KEY_INFO_FUNC,
  369. 0)))
  370. goto CryptInitOIDFunctionSetError;
  371. if (NULL == (hEncodePubKeyFuncSet = CryptInitOIDFunctionSet(
  372. CRYPT_OID_ENCODE_PUBLIC_KEY_AND_PARAMETERS_FUNC,
  373. 0)))
  374. goto CryptInitOIDFunctionSetError;
  375. if (NULL == (hConvertPubKeyFuncSet = CryptInitOIDFunctionSet(
  376. CRYPT_OID_CONVERT_PUBLIC_KEY_INFO_FUNC,
  377. 0)))
  378. goto CryptInitOIDFunctionSetError;
  379. if (!CryptInstallOIDFunctionAddress(
  380. NULL, // hModule
  381. X509_ASN_ENCODING,
  382. CRYPT_OID_ENCODE_PUBLIC_KEY_AND_PARAMETERS_FUNC,
  383. ENCODE_PUB_KEY_FUNC_COUNT,
  384. EncodePubKeyFuncTable,
  385. 0)) // dwFlags
  386. goto CryptInstallOIDFunctionAddressError;
  387. if (!CryptInstallOIDFunctionAddress(
  388. NULL, // hModule
  389. X509_ASN_ENCODING,
  390. CRYPT_OID_CONVERT_PUBLIC_KEY_INFO_FUNC,
  391. CONVERT_PUB_KEY_FUNC_COUNT,
  392. ConvertPubKeyFuncTable,
  393. 0)) // dwFlags
  394. goto CryptInstallOIDFunctionAddressError;
  395. if (!InitDefaultCryptProv())
  396. goto InitDefaultCryptProvError;
  397. if (!Pki_InitializeCriticalSection(&DefaultContextCriticalSection))
  398. goto InitCritSectionError;
  399. if (NULL == (hTlsDefaultContext = I_CryptAllocTls()))
  400. goto CryptAllocTlsError;
  401. break;
  402. case DLL_PROCESS_DETACH:
  403. FreeDefaultCryptProv();
  404. while (pProcessDefaultContextHead) {
  405. PDEFAULT_CONTEXT pFree = pProcessDefaultContextHead;
  406. pProcessDefaultContextHead = pProcessDefaultContextHead->pNext;
  407. if (pFree->dwFlags & CRYPT_DEFAULT_CONTEXT_AUTO_RELEASE_FLAG)
  408. CryptReleaseContext(pFree->hCryptProv, 0);
  409. PkiFree(pFree);
  410. }
  411. DeleteCriticalSection(&DefaultContextCriticalSection);
  412. I_CryptFreeTls(hTlsDefaultContext, DetachDefaultContext);
  413. break;
  414. case DLL_THREAD_DETACH:
  415. DetachDefaultContext(I_CryptDetachTls(hTlsDefaultContext));
  416. break;
  417. default:
  418. break;
  419. }
  420. fRet = TRUE;
  421. CommonReturn:
  422. return fRet;
  423. CryptAllocTlsError:
  424. DeleteCriticalSection(&DefaultContextCriticalSection);
  425. InitCritSectionError:
  426. FreeDefaultCryptProv();
  427. ErrorReturn:
  428. fRet = FALSE;
  429. goto CommonReturn;
  430. TRACE_ERROR(InitDefaultCryptProvError)
  431. TRACE_ERROR(CryptInitOIDFunctionSetError)
  432. TRACE_ERROR(CryptInstallOIDFunctionAddressError)
  433. }
  434. //+-------------------------------------------------------------------------
  435. // Acquire default CryptProv according to the public key algorithm supported
  436. // by the provider type. The provider is acquired with only
  437. // CRYPT_VERIFYCONTEXT.
  438. //
  439. // Setting aiPubKey to 0, gets the default provider for RSA_FULL.
  440. //
  441. // Note, the returned CryptProv must not be released. Once acquired, the
  442. // CryptProv isn't released until ProcessDetach. This allows the returned
  443. // CryptProvs to be shared.
  444. //--------------------------------------------------------------------------
  445. HCRYPTPROV
  446. WINAPI
  447. I_CryptGetDefaultCryptProv(
  448. IN ALG_ID aiPubKey
  449. )
  450. {
  451. HCRYPTPROV hProv;
  452. DWORD dwProvType;
  453. DWORD dwDefaultProvIndex;
  454. switch (aiPubKey) {
  455. case 0:
  456. case CALG_RSA_SIGN:
  457. case CALG_RSA_KEYX:
  458. case CALG_NO_SIGN:
  459. dwProvType = PROV_RSA_FULL;
  460. dwDefaultProvIndex = DEFAULT_RSA_CRYPT_PROV;
  461. break;
  462. case CALG_DSS_SIGN:
  463. dwProvType = PROV_DSS_DH;
  464. dwDefaultProvIndex = DEFAULT_DSS_CRYPT_PROV;
  465. break;
  466. default:
  467. SetLastError((DWORD) E_INVALIDARG);
  468. return 0;
  469. }
  470. hProv = rghDefaultCryptProv[dwDefaultProvIndex];
  471. if (0 == hProv) {
  472. EnterCriticalSection(&DefaultCryptProvCriticalSection);
  473. hProv = rghDefaultCryptProv[dwDefaultProvIndex];
  474. if (0 == hProv) {
  475. if (!CryptAcquireContext(
  476. &hProv,
  477. NULL, // pszContainer
  478. NULL, // pszProvider,
  479. dwProvType,
  480. CRYPT_VERIFYCONTEXT // dwFlags
  481. )) {
  482. hProv = 0; // CAPI bug, sets hCryptProv to nonzero
  483. if (DEFAULT_DSS_CRYPT_PROV == dwDefaultProvIndex) {
  484. if (!CryptAcquireContext(
  485. &hProv,
  486. NULL, // pszContainer
  487. NULL, // pszProvider,
  488. PROV_DSS,
  489. CRYPT_VERIFYCONTEXT // dwFlags
  490. ))
  491. hProv = 0; // CAPI bug, sets hCryptProv to nonzero
  492. }
  493. }
  494. rghDefaultCryptProv[dwDefaultProvIndex] = hProv;
  495. }
  496. LeaveCriticalSection(&DefaultCryptProvCriticalSection);
  497. }
  498. return hProv;
  499. }
  500. // Note, PP_ENUMALGS_EX returns the bit range. However, this parameter type
  501. // may not be supported by all CSPs. If this fails, try PP_ENUMALGS which only
  502. // returns a single, default bit length.
  503. static void LoadRSAEncryptAlgInfo()
  504. {
  505. EnterCriticalSection(&DefaultCryptProvCriticalSection);
  506. if (!fLoadedRSAEncryptAlgInfo) {
  507. HCRYPTPROV hProv;
  508. if (hProv = I_CryptGetDefaultCryptProv(CALG_RSA_KEYX)) {
  509. DWORD dwFlags = CRYPT_FIRST;
  510. BOOL fEx = TRUE;
  511. while (TRUE) {
  512. ENCRYPT_ALG_INFO AlgInfo;
  513. PENCRYPT_ALG_INFO pAllocAlgInfo;
  514. if (fEx) {
  515. PROV_ENUMALGS_EX Data;
  516. DWORD cbData = sizeof(Data);
  517. if (!CryptGetProvParam(
  518. hProv,
  519. PP_ENUMALGS_EX,
  520. (BYTE *) &Data,
  521. &cbData,
  522. dwFlags
  523. )) {
  524. if (0 != dwFlags) {
  525. // Try PP_ENUMALGS
  526. fEx = FALSE;
  527. continue;
  528. } else
  529. break;
  530. }
  531. AlgInfo.aiAlgid = Data.aiAlgid;
  532. AlgInfo.dwMinLen = Data.dwMinLen;
  533. AlgInfo.dwMaxLen = Data.dwMaxLen;
  534. } else {
  535. PROV_ENUMALGS Data;
  536. DWORD cbData = sizeof(Data);
  537. if (!CryptGetProvParam(
  538. hProv,
  539. PP_ENUMALGS,
  540. (BYTE *) &Data,
  541. &cbData,
  542. dwFlags
  543. ))
  544. break;
  545. // Only know about a single length
  546. AlgInfo.aiAlgid = Data.aiAlgid;
  547. AlgInfo.dwMinLen = Data.dwBitLen;
  548. AlgInfo.dwMaxLen = Data.dwBitLen;
  549. }
  550. dwFlags = 0; // CRYPT_NEXT
  551. // Only interested in encrypt algorithms
  552. if (ALG_CLASS_DATA_ENCRYPT != GET_ALG_CLASS(AlgInfo.aiAlgid))
  553. continue;
  554. if (NULL == (pAllocAlgInfo = (PENCRYPT_ALG_INFO)
  555. PkiNonzeroAlloc(sizeof(ENCRYPT_ALG_INFO))))
  556. break;
  557. AlgInfo.pNext = pRSAEncryptAlgInfoHead;
  558. memcpy(pAllocAlgInfo, &AlgInfo, sizeof(*pAllocAlgInfo));
  559. pRSAEncryptAlgInfoHead = pAllocAlgInfo;
  560. }
  561. }
  562. fLoadedRSAEncryptAlgInfo = TRUE;
  563. }
  564. LeaveCriticalSection(&DefaultCryptProvCriticalSection);
  565. }
  566. static BOOL IsDefaultRSACryptProvForEncrypt(
  567. IN ALG_ID aiEncrypt,
  568. IN DWORD dwBitLen
  569. )
  570. {
  571. PENCRYPT_ALG_INFO pInfo;
  572. if (!fLoadedRSAEncryptAlgInfo)
  573. LoadRSAEncryptAlgInfo();
  574. if (0 == dwBitLen && (CALG_RC2 == aiEncrypt || CALG_RC4 == aiEncrypt))
  575. dwBitLen = 40;
  576. for (pInfo = pRSAEncryptAlgInfoHead; pInfo; pInfo = pInfo->pNext) {
  577. if (aiEncrypt == pInfo->aiAlgid) {
  578. if (0 == dwBitLen || (pInfo->dwMinLen <= dwBitLen &&
  579. dwBitLen <= pInfo->dwMaxLen))
  580. return TRUE;
  581. }
  582. }
  583. return FALSE;
  584. }
  585. //+-------------------------------------------------------------------------
  586. // Acquire default CryptProv according to the public key algorithm, encrypt
  587. // key algorithm and encrypt key length supported by the provider type.
  588. //
  589. // dwBitLen = 0, assumes the aiEncrypt's default bit length. For example,
  590. // CALG_RC2 has a default bit length of 40.
  591. //
  592. // Note, the returned CryptProv must not be released. Once acquired, the
  593. // CryptProv isn't released until ProcessDetach. This allows the returned
  594. // CryptProvs to be shared.
  595. //--------------------------------------------------------------------------
  596. HCRYPTPROV
  597. WINAPI
  598. I_CryptGetDefaultCryptProvForEncrypt(
  599. IN ALG_ID aiPubKey,
  600. IN ALG_ID aiEncrypt,
  601. IN DWORD dwBitLen
  602. )
  603. {
  604. HCRYPTPROV hProv;
  605. DWORD dwProvType;
  606. DWORD dwDefaultProvIndex;
  607. LPCSTR pszProvider;
  608. if (CALG_DH_SF == aiPubKey || CALG_DH_EPHEM == aiPubKey) {
  609. dwProvType = PROV_DSS_DH;
  610. dwDefaultProvIndex = DEFAULT_ENCRYPT_DH_CRYPT_PROV;
  611. pszProvider = NULL;
  612. } else {
  613. dwProvType = PROV_RSA_FULL;
  614. if (IsDefaultRSACryptProvForEncrypt(
  615. aiEncrypt,
  616. dwBitLen
  617. ))
  618. // Set to fall through to the default case
  619. aiEncrypt = 0;
  620. switch (aiEncrypt) {
  621. case CALG_DES:
  622. case CALG_3DES:
  623. case CALG_3DES_112:
  624. dwDefaultProvIndex = DEFAULT_ENCRYPT_ENH_RSA_CRYPT_PROV;
  625. pszProvider = MS_ENHANCED_PROV_A;
  626. break;
  627. case CALG_RC2:
  628. case CALG_RC4:
  629. if (40 >= dwBitLen) {
  630. dwDefaultProvIndex = DEFAULT_ENCRYPT_BASE_RSA_CRYPT_PROV;
  631. pszProvider = MS_DEF_PROV_A;
  632. } else {
  633. dwDefaultProvIndex = DEFAULT_ENCRYPT_ENH_RSA_CRYPT_PROV;
  634. pszProvider = MS_ENHANCED_PROV_A;
  635. }
  636. break;
  637. case 0:
  638. default:
  639. dwDefaultProvIndex = DEFAULT_RSA_CRYPT_PROV;
  640. pszProvider = NULL;
  641. break;
  642. }
  643. }
  644. hProv = rghDefaultCryptProv[dwDefaultProvIndex];
  645. if (0 == hProv) {
  646. EnterCriticalSection(&DefaultCryptProvCriticalSection);
  647. hProv = rghDefaultCryptProv[dwDefaultProvIndex];
  648. if (0 == hProv) {
  649. if (!CryptAcquireContext(
  650. &hProv,
  651. NULL, // pszContainer
  652. pszProvider,
  653. dwProvType,
  654. CRYPT_VERIFYCONTEXT // dwFlags
  655. ))
  656. hProv = 0; // CAPI bug, sets hCryptProv to nonzero
  657. else
  658. rghDefaultCryptProv[dwDefaultProvIndex] = hProv;
  659. }
  660. LeaveCriticalSection(&DefaultCryptProvCriticalSection);
  661. }
  662. return hProv;
  663. }
  664. //+-------------------------------------------------------------------------
  665. // Cert helper allocation and free functions
  666. //--------------------------------------------------------------------------
  667. static void *AllocAndDecodeObject(
  668. IN DWORD dwCertEncodingType,
  669. IN LPCSTR lpszStructType,
  670. IN const BYTE *pbEncoded,
  671. IN DWORD cbEncoded,
  672. OUT OPTIONAL DWORD *pcbStructInfo = NULL
  673. )
  674. {
  675. DWORD cbStructInfo;
  676. void *pvStructInfo;
  677. if (!CryptDecodeObjectEx(
  678. dwCertEncodingType,
  679. lpszStructType,
  680. pbEncoded,
  681. cbEncoded,
  682. CRYPT_DECODE_NOCOPY_FLAG | CRYPT_DECODE_ALLOC_FLAG,
  683. &PkiDecodePara,
  684. (void *) &pvStructInfo,
  685. &cbStructInfo
  686. ))
  687. goto ErrorReturn;
  688. CommonReturn:
  689. if (pcbStructInfo)
  690. *pcbStructInfo = cbStructInfo;
  691. return pvStructInfo;
  692. ErrorReturn:
  693. pvStructInfo = NULL;
  694. goto CommonReturn;
  695. }
  696. static BOOL AllocAndEncodeObject(
  697. IN DWORD dwCertEncodingType,
  698. IN LPCSTR lpszStructType,
  699. IN const void *pvStructInfo,
  700. OUT BYTE **ppbEncoded,
  701. OUT DWORD *pcbEncoded
  702. )
  703. {
  704. return CryptEncodeObjectEx(
  705. dwCertEncodingType,
  706. lpszStructType,
  707. pvStructInfo,
  708. CRYPT_ENCODE_ALLOC_FLAG,
  709. &PkiEncodePara,
  710. (void *) ppbEncoded,
  711. pcbEncoded
  712. );
  713. }
  714. #if 0
  715. //+-------------------------------------------------------------------------
  716. // For an authority key identifier extension, compare the extension's optional
  717. // fields with the specified issuer certificate.
  718. //
  719. // Returns TRUE for no authority key identifier extension or an issuer
  720. // certificate match.
  721. //--------------------------------------------------------------------------
  722. static BOOL CompareAuthorityKeyIdentifier(
  723. IN DWORD dwCertEncodingType,
  724. IN DWORD cExtensions,
  725. IN CERT_EXTENSION rgExtensions[],
  726. IN PCERT_INFO pIssuerInfo
  727. )
  728. {
  729. BOOL fResult;
  730. PCERT_EXTENSION pExt;
  731. PCERT_AUTHORITY_KEY_ID_INFO pKeyIdInfo = NULL;
  732. pExt = CertFindExtension(
  733. szOID_AUTHORITY_KEY_IDENTIFIER,
  734. cExtensions,
  735. rgExtensions
  736. );
  737. if (pExt == NULL)
  738. return TRUE;
  739. if (NULL == (pKeyIdInfo =
  740. (PCERT_AUTHORITY_KEY_ID_INFO) AllocAndDecodeObject(
  741. dwCertEncodingType,
  742. X509_AUTHORITY_KEY_ID,
  743. pExt->Value.pbData,
  744. pExt->Value.cbData
  745. ))) goto DecodeError;
  746. if (pKeyIdInfo->CertIssuer.cbData) {
  747. // Issuer certificate's issuer name must match
  748. if (!CertCompareCertificateName(
  749. dwCertEncodingType,
  750. &pKeyIdInfo->CertIssuer,
  751. &pIssuerInfo->Issuer
  752. )) goto ErrorReturn;
  753. }
  754. if (pKeyIdInfo->CertSerialNumber.cbData) {
  755. // Issuer certificate's serial number must match
  756. if (!CertCompareIntegerBlob(
  757. &pKeyIdInfo->CertSerialNumber,
  758. &pIssuerInfo->SerialNumber))
  759. goto ErrorReturn;
  760. }
  761. fResult = TRUE;
  762. goto CommonReturn;
  763. DecodeError:
  764. fResult = TRUE;
  765. goto CommonReturn;
  766. ErrorReturn:
  767. fResult = FALSE;
  768. CommonReturn:
  769. PkiFree(pKeyIdInfo);
  770. return fResult;
  771. }
  772. #endif
  773. //+-------------------------------------------------------------------------
  774. // Compare two multiple byte integer blobs to see if they are identical.
  775. //
  776. // Before doing the comparison, leading zero bytes are removed from a
  777. // positive number and leading 0xFF bytes are removed from a negative
  778. // number.
  779. //
  780. // The multiple byte integers are treated as Little Endian. pbData[0] is the
  781. // least significant byte and pbData[cbData - 1] is the most significant
  782. // byte.
  783. //
  784. // Returns TRUE if the integer blobs are identical after removing leading
  785. // 0 or 0xFF bytes.
  786. //--------------------------------------------------------------------------
  787. BOOL
  788. WINAPI
  789. CertCompareIntegerBlob(
  790. IN PCRYPT_INTEGER_BLOB pInt1,
  791. IN PCRYPT_INTEGER_BLOB pInt2
  792. )
  793. {
  794. BYTE *pb1 = pInt1->pbData;
  795. DWORD cb1 = pInt1->cbData;
  796. BYTE *pb2 = pInt2->pbData;
  797. DWORD cb2 = pInt2->cbData;
  798. // Assumption: normally don't have leading 0 or 0xFF bytes.
  799. while (cb1 > 1) {
  800. BYTE bEnd = pb1[cb1 - 1];
  801. BYTE bPrev = pb1[cb1 - 2];
  802. if ((0 == bEnd && 0 == (bPrev & 0x80)) ||
  803. (0xFF == bEnd && 0 != (bPrev & 0x80)))
  804. cb1--;
  805. else
  806. break;
  807. }
  808. while (cb2 > 1) {
  809. BYTE bEnd = pb2[cb2 - 1];
  810. BYTE bPrev = pb2[cb2 - 2];
  811. if ((0 == bEnd && 0 == (bPrev & 0x80)) ||
  812. (0xFF == bEnd && 0 != (bPrev & 0x80)))
  813. cb2--;
  814. else
  815. break;
  816. }
  817. if (cb1 == cb2 && (0 == cb1 || 0 == memcmp(pb1, pb2, cb1)))
  818. return TRUE;
  819. else
  820. return FALSE;
  821. }
  822. //+-------------------------------------------------------------------------
  823. // Compare two certificates to see if they are identical.
  824. //
  825. // Since a certificate is uniquely identified by its Issuer and SerialNumber,
  826. // these are the only fields needing to be compared.
  827. //
  828. // Returns TRUE if the certificates are identical.
  829. //--------------------------------------------------------------------------
  830. BOOL
  831. WINAPI
  832. CertCompareCertificate(
  833. IN DWORD dwCertEncodingType,
  834. IN PCERT_INFO pCertId1,
  835. IN PCERT_INFO pCertId2
  836. )
  837. {
  838. if (CertCompareIntegerBlob(&pCertId1->SerialNumber,
  839. &pCertId2->SerialNumber) &&
  840. pCertId1->Issuer.cbData == pCertId2->Issuer.cbData &&
  841. memcmp(pCertId1->Issuer.pbData, pCertId2->Issuer.pbData,
  842. pCertId1->Issuer.cbData) == 0)
  843. return TRUE;
  844. else
  845. return FALSE;
  846. }
  847. //+-------------------------------------------------------------------------
  848. // Compare two certificate names to see if they are identical.
  849. //
  850. // Returns TRUE if the names are identical.
  851. //--------------------------------------------------------------------------
  852. BOOL
  853. WINAPI
  854. CertCompareCertificateName(
  855. IN DWORD dwCertEncodingType,
  856. IN PCERT_NAME_BLOB pCertName1,
  857. IN PCERT_NAME_BLOB pCertName2
  858. )
  859. {
  860. if (pCertName1->cbData == pCertName2->cbData &&
  861. memcmp(pCertName1->pbData, pCertName2->pbData,
  862. pCertName1->cbData) == 0)
  863. return TRUE;
  864. else
  865. return FALSE;
  866. }
  867. //+-------------------------------------------------------------------------
  868. // Compare the attributes in the certificate name with the specified
  869. // Relative Distinguished Name's (CERT_RDN) array of attributes.
  870. // The comparison iterates through the CERT_RDN attributes and looks for an
  871. // attribute match in any of the certificate's RDNs. Returns TRUE if all the
  872. // attributes are found and match.
  873. //
  874. // The CERT_RDN_ATTR fields can have the following special values:
  875. // pszObjId == NULL - ignore the attribute object identifier
  876. // dwValueType == CERT_RDN_ANY_TYPE - ignore the value type
  877. // Value.pbData == NULL - match any value
  878. //
  879. // CERT_CASE_INSENSITIVE_IS_RDN_ATTRS_FLAG should be set to do
  880. // a case insensitive match. Otherwise, defaults to an exact, case sensitive
  881. // match.
  882. //
  883. // CERT_UNICODE_IS_RDN_ATTRS_FLAG should be set if the pRDN was initialized
  884. // with unicode strings as for CryptEncodeObject(X509_UNICODE_NAME).
  885. //--------------------------------------------------------------------------
  886. BOOL
  887. WINAPI
  888. CertIsRDNAttrsInCertificateName(
  889. IN DWORD dwCertEncodingType,
  890. IN DWORD dwFlags,
  891. IN PCERT_NAME_BLOB pCertName,
  892. IN PCERT_RDN pRDN
  893. )
  894. {
  895. BOOL fResult;
  896. PCERT_NAME_INFO pNameInfo = NULL;
  897. DWORD cCmpAttr;
  898. PCERT_RDN_ATTR pCmpAttr;
  899. BOOL fMatch;
  900. if (NULL == (pNameInfo =
  901. (PCERT_NAME_INFO) AllocAndDecodeObject(
  902. dwCertEncodingType,
  903. CERT_UNICODE_IS_RDN_ATTRS_FLAG & dwFlags ? X509_UNICODE_NAME :
  904. X509_NAME,
  905. pCertName->pbData,
  906. pCertName->cbData
  907. ))) goto ErrorReturn;
  908. cCmpAttr = pRDN->cRDNAttr;
  909. pCmpAttr = pRDN->rgRDNAttr;
  910. fMatch = TRUE;
  911. // Iterate through list of attributes to be compared against
  912. for ( ; cCmpAttr > 0; cCmpAttr--, pCmpAttr++) {
  913. fMatch = FALSE;
  914. DWORD cNameRDN = pNameInfo->cRDN;
  915. PCERT_RDN pNameRDN = pNameInfo->rgRDN;
  916. // Iterate through name's list of RDNs
  917. for ( ; cNameRDN > 0; cNameRDN--, pNameRDN++) {
  918. DWORD cNameAttr = pNameRDN->cRDNAttr;
  919. PCERT_RDN_ATTR pNameAttr = pNameRDN->rgRDNAttr;
  920. // Iterate through name's CERT_RDN's list of attributes
  921. for ( ; cNameAttr > 0; cNameAttr--, pNameAttr++) {
  922. if (pCmpAttr->pszObjId &&
  923. (pNameAttr->pszObjId == NULL ||
  924. strcmp(pCmpAttr->pszObjId, pNameAttr->pszObjId) != 0))
  925. continue;
  926. if (pCmpAttr->dwValueType != CERT_RDN_ANY_TYPE &&
  927. pCmpAttr->dwValueType != pNameAttr->dwValueType)
  928. continue;
  929. if (pCmpAttr->Value.pbData == NULL) {
  930. fMatch = TRUE;
  931. break;
  932. }
  933. if (CERT_CASE_INSENSITIVE_IS_RDN_ATTRS_FLAG & dwFlags) {
  934. DWORD cch;
  935. if (CERT_UNICODE_IS_RDN_ATTRS_FLAG & dwFlags) {
  936. if (0 == pCmpAttr->Value.cbData)
  937. cch = wcslen((LPWSTR) pCmpAttr->Value.pbData);
  938. else
  939. cch = pCmpAttr->Value.cbData / sizeof(WCHAR);
  940. if (cch == (pNameAttr->Value.cbData / sizeof(WCHAR))
  941. &&
  942. CSTR_EQUAL == CompareStringU(
  943. LOCALE_USER_DEFAULT,
  944. NORM_IGNORECASE,
  945. (LPWSTR) pCmpAttr->Value.pbData,
  946. cch,
  947. (LPWSTR) pNameAttr->Value.pbData,
  948. cch)) {
  949. fMatch = TRUE;
  950. break;
  951. }
  952. } else {
  953. cch = pCmpAttr->Value.cbData;
  954. if (cch == (pNameAttr->Value.cbData)
  955. &&
  956. CSTR_EQUAL == CompareStringA(
  957. LOCALE_USER_DEFAULT,
  958. NORM_IGNORECASE,
  959. (LPSTR) pCmpAttr->Value.pbData,
  960. cch,
  961. (LPSTR) pNameAttr->Value.pbData,
  962. cch)) {
  963. fMatch = TRUE;
  964. break;
  965. }
  966. }
  967. } else {
  968. DWORD cbCmpData = pCmpAttr->Value.cbData;
  969. if ((CERT_UNICODE_IS_RDN_ATTRS_FLAG & dwFlags) &&
  970. 0 == cbCmpData)
  971. cbCmpData = wcslen((LPWSTR) pCmpAttr->Value.pbData) *
  972. sizeof(WCHAR);
  973. if (cbCmpData == pNameAttr->Value.cbData &&
  974. (cbCmpData == 0 ||
  975. memcmp(pCmpAttr->Value.pbData,
  976. pNameAttr->Value.pbData,
  977. cbCmpData) == 0)) {
  978. fMatch = TRUE;
  979. break;
  980. }
  981. }
  982. }
  983. if (fMatch) break;
  984. }
  985. if (!fMatch) break;
  986. }
  987. if (!fMatch) {
  988. SetLastError((DWORD) CRYPT_E_NO_MATCH);
  989. goto ErrorReturn;
  990. }
  991. fResult = TRUE;
  992. goto CommonReturn;
  993. ErrorReturn:
  994. fResult = FALSE;
  995. CommonReturn:
  996. PkiFree(pNameInfo);
  997. return fResult;
  998. }
  999. #if 0
  1000. #ifndef RSA1
  1001. #define RSA1 ((DWORD)'R'+((DWORD)'S'<<8)+((DWORD)'A'<<16)+((DWORD)'1'<<24))
  1002. #endif
  1003. //+-------------------------------------------------------------------------
  1004. // Compare two public keys to see if they are identical.
  1005. //
  1006. // Returns TRUE if the keys are identical.
  1007. //
  1008. // Note: ignores CAPI's reserved and aiKeyAlg fields in the comparison.
  1009. //--------------------------------------------------------------------------
  1010. BOOL
  1011. WINAPI
  1012. CertComparePublicKeyBitBlob(
  1013. IN DWORD dwCertEncodingType,
  1014. IN PCRYPT_BIT_BLOB pPublicKey1,
  1015. IN PCRYPT_BIT_BLOB pPublicKey2
  1016. )
  1017. {
  1018. BYTE *pb1, *pb2;
  1019. PUBLICKEYSTRUC *pPubKeyStruc1, *pPubKeyStruc2;
  1020. RSAPUBKEY *pRsaPubKey1, *pRsaPubKey2;
  1021. BYTE *pbModulus1, *pbModulus2;
  1022. DWORD cbModulus1, cbModulus2;
  1023. // The CAPI public key representation consists of the following sequence:
  1024. // - PUBLICKEYSTRUC
  1025. // - RSAPUBKEY
  1026. // - rgbModulus[]
  1027. pb1 = pPublicKey1->pbData;
  1028. pPubKeyStruc1 = (PUBLICKEYSTRUC *) pb1;
  1029. pRsaPubKey1 = (RSAPUBKEY *) (pb1 + sizeof(PUBLICKEYSTRUC));
  1030. pbModulus1 = pb1 + sizeof(PUBLICKEYSTRUC) + sizeof(RSAPUBKEY);
  1031. cbModulus1 = pRsaPubKey1->bitlen / 8;
  1032. assert(cbModulus1 > 0);
  1033. assert(sizeof(PUBLICKEYSTRUC) + sizeof(RSAPUBKEY) + cbModulus1 <=
  1034. pPublicKey1->cbData);
  1035. assert(pPubKeyStruc1->bType == PUBLICKEYBLOB);
  1036. assert(pPubKeyStruc1->bVersion == CUR_BLOB_VERSION);
  1037. assert(pPubKeyStruc1->aiKeyAlg == CALG_RSA_SIGN ||
  1038. pPubKeyStruc1->aiKeyAlg == CALG_RSA_KEYX);
  1039. assert(pRsaPubKey1->magic == RSA1);
  1040. assert(pRsaPubKey1->bitlen % 8 == 0);
  1041. pb2 = pPublicKey2->pbData;
  1042. pPubKeyStruc2 = (PUBLICKEYSTRUC *) pb2;
  1043. pRsaPubKey2 = (RSAPUBKEY *) (pb2 + sizeof(PUBLICKEYSTRUC));
  1044. pbModulus2 = pb2 + sizeof(PUBLICKEYSTRUC) + sizeof(RSAPUBKEY);
  1045. cbModulus2 = pRsaPubKey2->bitlen / 8;
  1046. assert(cbModulus2 > 0);
  1047. assert(sizeof(PUBLICKEYSTRUC) + sizeof(RSAPUBKEY) + cbModulus2 <=
  1048. pPublicKey2->cbData);
  1049. assert(pPubKeyStruc2->bType == PUBLICKEYBLOB);
  1050. assert(pPubKeyStruc2->bVersion == CUR_BLOB_VERSION);
  1051. assert(pPubKeyStruc2->aiKeyAlg == CALG_RSA_SIGN ||
  1052. pPubKeyStruc2->aiKeyAlg == CALG_RSA_KEYX);
  1053. assert(pRsaPubKey2->magic == RSA1);
  1054. assert(pRsaPubKey2->bitlen % 8 == 0);
  1055. if (pRsaPubKey1->pubexp == pRsaPubKey2->pubexp &&
  1056. cbModulus1 == cbModulus2 &&
  1057. memcmp(pbModulus1, pbModulus2, cbModulus1) == 0)
  1058. return TRUE;
  1059. else
  1060. return FALSE;
  1061. }
  1062. #endif
  1063. //+-------------------------------------------------------------------------
  1064. // Compare two public keys to see if they are identical.
  1065. //
  1066. // Returns TRUE if the keys are identical.
  1067. //--------------------------------------------------------------------------
  1068. BOOL
  1069. WINAPI
  1070. CertComparePublicKeyInfo(
  1071. IN DWORD dwCertEncodingType,
  1072. IN PCERT_PUBLIC_KEY_INFO pPublicKey1,
  1073. IN PCERT_PUBLIC_KEY_INFO pPublicKey2
  1074. )
  1075. {
  1076. DWORD cbData;
  1077. DWORD cb1;
  1078. BYTE * pb1;
  1079. DWORD cb2;
  1080. BYTE * pb2;
  1081. BOOL fResult = FALSE;
  1082. PUBLICKEYSTRUC * pBlob1 = NULL;
  1083. PUBLICKEYSTRUC * pBlob2 = NULL;
  1084. if (!((cbData = pPublicKey1->PublicKey.cbData) ==
  1085. pPublicKey2->PublicKey.cbData
  1086. &&
  1087. (cbData == 0 || memcmp(pPublicKey1->PublicKey.pbData,
  1088. pPublicKey2->PublicKey.pbData, cbData) == 0)))
  1089. {
  1090. // DSIE: Bug 402662.
  1091. // Encoded compare failed, try decoded compare.
  1092. if (NULL == (pBlob1 = (PUBLICKEYSTRUC *) AllocAndDecodeObject(
  1093. dwCertEncodingType,
  1094. RSA_CSP_PUBLICKEYBLOB,
  1095. pPublicKey1->PublicKey.pbData,
  1096. pPublicKey1->PublicKey.cbData,
  1097. &cb1)))
  1098. {
  1099. goto CLEANUP;
  1100. }
  1101. if (NULL == (pBlob2 = (PUBLICKEYSTRUC *) AllocAndDecodeObject(
  1102. dwCertEncodingType,
  1103. RSA_CSP_PUBLICKEYBLOB,
  1104. pPublicKey2->PublicKey.pbData,
  1105. pPublicKey2->PublicKey.cbData,
  1106. &cb2)))
  1107. {
  1108. goto CLEANUP;
  1109. }
  1110. if (!((cb1 == cb2) &&
  1111. (cb1 == 0 || memcmp(pBlob1, pBlob2, cb1) == 0)))
  1112. {
  1113. goto CLEANUP;
  1114. }
  1115. }
  1116. // Compare algorithm parameters
  1117. cb1 = pPublicKey1->Algorithm.Parameters.cbData;
  1118. pb1 = pPublicKey1->Algorithm.Parameters.pbData;
  1119. cb2 = pPublicKey2->Algorithm.Parameters.cbData;
  1120. pb2 = pPublicKey2->Algorithm.Parameters.pbData;
  1121. if (X509_ASN_ENCODING == GET_CERT_ENCODING_TYPE(dwCertEncodingType))
  1122. {
  1123. // Check if either has NO or NULL parameters
  1124. if (0 == cb1 || *pb1 == NULL_ASN_TAG ||
  1125. 0 == cb2 || *pb2 == NULL_ASN_TAG)
  1126. {
  1127. fResult = TRUE;
  1128. goto CLEANUP;
  1129. }
  1130. }
  1131. if (cb1 == cb2)
  1132. {
  1133. if (0 == cb1 || 0 == memcmp(pb1, pb2, cb1))
  1134. {
  1135. fResult = TRUE;
  1136. }
  1137. }
  1138. CLEANUP:
  1139. if (pBlob1)
  1140. PkiFree(pBlob1);
  1141. if (pBlob2)
  1142. PkiFree(pBlob2);
  1143. return fResult;
  1144. }
  1145. static BOOL GetSignOIDInfo(
  1146. IN LPCSTR pszObjId,
  1147. OUT ALG_ID *paiHash,
  1148. OUT ALG_ID *paiPubKey,
  1149. OUT DWORD *pdwFlags,
  1150. OUT DWORD *pdwProvType = NULL
  1151. )
  1152. {
  1153. BOOL fResult;
  1154. PCCRYPT_OID_INFO pInfo;
  1155. *paiPubKey = 0;
  1156. *pdwFlags = 0;
  1157. if (pdwProvType)
  1158. *pdwProvType = 0;
  1159. if (pInfo = CryptFindOIDInfo(
  1160. CRYPT_OID_INFO_OID_KEY,
  1161. (void *) pszObjId,
  1162. CRYPT_SIGN_ALG_OID_GROUP_ID
  1163. )) {
  1164. DWORD cExtra = pInfo->ExtraInfo.cbData / sizeof(DWORD);
  1165. DWORD *pdwExtra = (DWORD *) pInfo->ExtraInfo.pbData;
  1166. *paiHash = pInfo->Algid;
  1167. if (1 <= cExtra) {
  1168. *paiPubKey = pdwExtra[0];
  1169. if (2 <= cExtra) {
  1170. *pdwFlags = pdwExtra[1];
  1171. if (3 <= cExtra && pdwProvType) {
  1172. *pdwProvType = pdwExtra[2];
  1173. }
  1174. }
  1175. }
  1176. fResult = TRUE;
  1177. } else if (pInfo = CryptFindOIDInfo(
  1178. CRYPT_OID_INFO_OID_KEY,
  1179. (void *) pszObjId,
  1180. CRYPT_HASH_ALG_OID_GROUP_ID
  1181. )) {
  1182. *paiHash = pInfo->Algid;
  1183. *paiPubKey = CALG_NO_SIGN;
  1184. fResult = TRUE;
  1185. } else {
  1186. *paiHash = 0;
  1187. fResult = FALSE;
  1188. SetLastError((DWORD) NTE_BAD_ALGID);
  1189. }
  1190. return fResult;
  1191. }
  1192. #ifndef CMS_PKCS7
  1193. //+-------------------------------------------------------------------------
  1194. // Verify the signature of a subject certificate or a CRL using the
  1195. // specified public key.
  1196. //
  1197. // Returns TRUE for a valid signature.
  1198. //
  1199. // hCryptProv specifies the crypto provider to use to verify the signature.
  1200. // It doesn't need to use a private key.
  1201. //--------------------------------------------------------------------------
  1202. BOOL
  1203. WINAPI
  1204. CryptVerifyCertificateSignature(
  1205. IN HCRYPTPROV hCryptProv,
  1206. IN DWORD dwCertEncodingType,
  1207. IN const BYTE * pbEncoded,
  1208. IN DWORD cbEncoded,
  1209. IN PCERT_PUBLIC_KEY_INFO pPublicKey
  1210. )
  1211. {
  1212. BOOL fResult;
  1213. PCERT_SIGNED_CONTENT_INFO pSignedInfo = NULL;
  1214. HCRYPTDEFAULTCONTEXT hDefaultContext = NULL;
  1215. HCRYPTKEY hSignKey = 0;
  1216. HCRYPTHASH hHash = 0;
  1217. BYTE *pbSignature; // not allocated
  1218. DWORD cbSignature;
  1219. BYTE rgbDssSignature[CERT_DSS_SIGNATURE_LEN];
  1220. ALG_ID aiHash;
  1221. ALG_ID aiPubKey;
  1222. DWORD dwSignFlags;
  1223. DWORD dwErr;
  1224. if (NULL == (pSignedInfo =
  1225. (PCERT_SIGNED_CONTENT_INFO) AllocAndDecodeObject(
  1226. dwCertEncodingType,
  1227. X509_CERT,
  1228. pbEncoded,
  1229. cbEncoded
  1230. ))) goto ErrorReturn;
  1231. if (!GetSignOIDInfo(pSignedInfo->SignatureAlgorithm.pszObjId,
  1232. &aiHash, &aiPubKey, &dwSignFlags))
  1233. goto ErrorReturn;
  1234. if (0 == hCryptProv) {
  1235. if (!I_CryptGetDefaultContext(
  1236. CRYPT_DEFAULT_CONTEXT_CERT_SIGN_OID,
  1237. (const void *) pSignedInfo->SignatureAlgorithm.pszObjId,
  1238. &hCryptProv,
  1239. &hDefaultContext
  1240. )) {
  1241. if (0 == (hCryptProv = I_CryptGetDefaultCryptProv(aiPubKey)))
  1242. goto ErrorReturn;
  1243. }
  1244. }
  1245. #if 0
  1246. // Slow down the signature verify while holding the default context
  1247. // reference count
  1248. if (hDefaultContext)
  1249. Sleep(5000);
  1250. #endif
  1251. if (!CryptImportPublicKeyInfo(
  1252. hCryptProv,
  1253. dwCertEncodingType,
  1254. pPublicKey,
  1255. &hSignKey
  1256. )) goto ErrorReturn;
  1257. if (!CryptCreateHash(
  1258. hCryptProv,
  1259. aiHash,
  1260. NULL, // hKey - optional for MAC
  1261. 0, // dwFlags
  1262. &hHash
  1263. )) goto ErrorReturn;
  1264. if (!CryptHashData(
  1265. hHash,
  1266. pSignedInfo->ToBeSigned.pbData,
  1267. pSignedInfo->ToBeSigned.cbData,
  1268. 0 // dwFlags
  1269. )) goto ErrorReturn;
  1270. pbSignature = pSignedInfo->Signature.pbData;
  1271. cbSignature = pSignedInfo->Signature.cbData;
  1272. if (CALG_DSS_SIGN == aiPubKey &&
  1273. 0 == (dwSignFlags & CRYPT_OID_INHIBIT_SIGNATURE_FORMAT_FLAG)) {
  1274. DWORD cbData;
  1275. // Undo the reversal done by CryptDecodeObject(X509_CERT)
  1276. PkiAsn1ReverseBytes(pbSignature, cbSignature);
  1277. // Convert from ASN.1 sequence of two integers to the CSP signature
  1278. // format.
  1279. cbData = sizeof(rgbDssSignature);
  1280. if (!CryptDecodeObject(
  1281. dwCertEncodingType,
  1282. X509_DSS_SIGNATURE,
  1283. pbSignature,
  1284. cbSignature,
  1285. 0, // dwFlags
  1286. rgbDssSignature,
  1287. &cbData
  1288. ))
  1289. goto ErrorReturn;
  1290. pbSignature = rgbDssSignature;
  1291. assert(cbData == sizeof(rgbDssSignature));
  1292. cbSignature = sizeof(rgbDssSignature);
  1293. }
  1294. if (!CryptVerifySignature(
  1295. hHash,
  1296. pbSignature,
  1297. cbSignature,
  1298. hSignKey,
  1299. NULL, // sDescription
  1300. 0 // dwFlags
  1301. )) goto ErrorReturn;
  1302. fResult = TRUE;
  1303. goto CommonReturn;
  1304. ErrorReturn:
  1305. fResult = FALSE;
  1306. CommonReturn:
  1307. dwErr = GetLastError();
  1308. if (hSignKey)
  1309. CryptDestroyKey(hSignKey);
  1310. if (hHash)
  1311. CryptDestroyHash(hHash);
  1312. I_CryptFreeDefaultContext(hDefaultContext);
  1313. PkiFree(pSignedInfo);
  1314. SetLastError(dwErr);
  1315. return fResult;
  1316. }
  1317. #endif // CMS_PKCS7
  1318. BOOL
  1319. WINAPI
  1320. DefaultHashCertificate(
  1321. IN ALG_ID Algid,
  1322. IN const BYTE *pbEncoded,
  1323. IN DWORD cbEncoded,
  1324. OUT BYTE *pbHash,
  1325. IN OUT DWORD *pcbHash
  1326. )
  1327. {
  1328. DWORD cbInHash;
  1329. DWORD cbOutHash;
  1330. if (NULL == pbHash)
  1331. cbInHash = 0;
  1332. else
  1333. cbInHash = *pcbHash;
  1334. switch (Algid) {
  1335. case CALG_MD5:
  1336. cbOutHash = MD5DIGESTLEN;
  1337. if (MD5DIGESTLEN <= cbInHash) {
  1338. MD5_CTX md5ctx;
  1339. MD5Init(&md5ctx);
  1340. if (cbEncoded)
  1341. MD5Update(&md5ctx, pbEncoded, cbEncoded);
  1342. MD5Final(&md5ctx);
  1343. memcpy(pbHash, md5ctx.digest, MD5DIGESTLEN);
  1344. }
  1345. break;
  1346. case CALG_SHA1:
  1347. default:
  1348. assert(CALG_SHA1 == Algid);
  1349. assert(CALG_SHA == Algid);
  1350. cbOutHash = A_SHA_DIGEST_LEN;
  1351. if (A_SHA_DIGEST_LEN <= cbInHash) {
  1352. A_SHA_CTX shactx;
  1353. A_SHAInit(&shactx);
  1354. if (cbEncoded)
  1355. A_SHAUpdate(&shactx, (BYTE *) pbEncoded, cbEncoded);
  1356. A_SHAFinal(&shactx, pbHash);
  1357. }
  1358. break;
  1359. }
  1360. *pcbHash = cbOutHash;
  1361. if (cbInHash < cbOutHash && pbHash) {
  1362. SetLastError((DWORD) ERROR_MORE_DATA);
  1363. return FALSE;
  1364. } else
  1365. return TRUE;
  1366. }
  1367. //+-------------------------------------------------------------------------
  1368. // Hash the encoded content.
  1369. //
  1370. // hCryptProv specifies the crypto provider to use to compute the hash.
  1371. // It doesn't need to use a private key.
  1372. //
  1373. // Algid specifies the CAPI hash algorithm to use. If Algid is 0, then, the
  1374. // default hash algorithm (currently SHA1) is used.
  1375. //--------------------------------------------------------------------------
  1376. BOOL
  1377. WINAPI
  1378. CryptHashCertificate(
  1379. IN HCRYPTPROV hCryptProv,
  1380. IN ALG_ID Algid,
  1381. IN DWORD dwFlags,
  1382. IN const BYTE *pbEncoded,
  1383. IN DWORD cbEncoded,
  1384. OUT BYTE *pbComputedHash,
  1385. IN OUT DWORD *pcbComputedHash
  1386. )
  1387. {
  1388. BOOL fResult;
  1389. HCRYPTHASH hHash = 0;
  1390. DWORD dwErr;
  1391. if (Algid == 0) {
  1392. Algid = CALG_SHA;
  1393. dwFlags = 0;
  1394. }
  1395. if (0 == hCryptProv) {
  1396. if (CALG_SHA1 == Algid || CALG_MD5 == Algid)
  1397. return DefaultHashCertificate(
  1398. Algid,
  1399. pbEncoded,
  1400. cbEncoded,
  1401. pbComputedHash,
  1402. pcbComputedHash
  1403. );
  1404. if (0 == (hCryptProv = I_CryptGetDefaultCryptProv(0)))
  1405. goto ErrorReturn;
  1406. }
  1407. if (!CryptCreateHash(
  1408. hCryptProv,
  1409. Algid,
  1410. NULL, // hKey - optional for MAC
  1411. dwFlags,
  1412. &hHash
  1413. ))
  1414. goto ErrorReturn;
  1415. if (!CryptHashData(
  1416. hHash,
  1417. pbEncoded,
  1418. cbEncoded,
  1419. 0 // dwFlags
  1420. ))
  1421. goto ErrorReturn;
  1422. fResult = CryptGetHashParam(
  1423. hHash,
  1424. HP_HASHVAL,
  1425. pbComputedHash,
  1426. pcbComputedHash,
  1427. 0 // dwFlags
  1428. );
  1429. goto CommonReturn;
  1430. ErrorReturn:
  1431. fResult = FALSE;
  1432. *pcbComputedHash = 0;
  1433. CommonReturn:
  1434. dwErr = GetLastError();
  1435. if (hHash)
  1436. CryptDestroyHash(hHash);
  1437. SetLastError(dwErr);
  1438. return fResult;
  1439. }
  1440. //+-------------------------------------------------------------------------
  1441. // Compute the hash of the "to be signed" information in the encoded
  1442. // signed content.
  1443. //
  1444. // hCryptProv specifies the crypto provider to use to compute the hash.
  1445. // It doesn't need to use a private key.
  1446. //--------------------------------------------------------------------------
  1447. BOOL
  1448. WINAPI
  1449. CryptHashToBeSigned(
  1450. IN HCRYPTPROV hCryptProv,
  1451. IN DWORD dwCertEncodingType,
  1452. IN const BYTE *pbEncoded,
  1453. IN DWORD cbEncoded,
  1454. OUT BYTE *pbComputedHash,
  1455. IN OUT DWORD *pcbComputedHash
  1456. )
  1457. {
  1458. BOOL fResult;
  1459. PCERT_SIGNED_CONTENT_INFO pSignedInfo = NULL;
  1460. HCRYPTHASH hHash = 0;
  1461. DWORD dwErr;
  1462. ALG_ID aiHash;
  1463. ALG_ID aiPubKey;
  1464. DWORD dwSignFlags;
  1465. if (NULL == (pSignedInfo =
  1466. (PCERT_SIGNED_CONTENT_INFO) AllocAndDecodeObject(
  1467. dwCertEncodingType,
  1468. X509_CERT,
  1469. pbEncoded,
  1470. cbEncoded
  1471. ))) goto ErrorReturn;
  1472. if (!GetSignOIDInfo(pSignedInfo->SignatureAlgorithm.pszObjId,
  1473. &aiHash, &aiPubKey, &dwSignFlags))
  1474. goto ErrorReturn;
  1475. if (0 == hCryptProv) {
  1476. if (CALG_SHA1 == aiHash || CALG_MD5 == aiHash) {
  1477. fResult = DefaultHashCertificate(
  1478. aiHash,
  1479. pSignedInfo->ToBeSigned.pbData,
  1480. pSignedInfo->ToBeSigned.cbData,
  1481. pbComputedHash,
  1482. pcbComputedHash
  1483. );
  1484. goto CommonReturn;
  1485. }
  1486. if (0 == (hCryptProv = I_CryptGetDefaultCryptProv(0)))
  1487. goto ErrorReturn;
  1488. }
  1489. if (!CryptCreateHash(
  1490. hCryptProv,
  1491. aiHash,
  1492. NULL, // hKey - optional for MAC
  1493. 0, // dwFlags
  1494. &hHash
  1495. ))
  1496. goto ErrorReturn;
  1497. if (!CryptHashData(
  1498. hHash,
  1499. pSignedInfo->ToBeSigned.pbData,
  1500. pSignedInfo->ToBeSigned.cbData,
  1501. 0 // dwFlags
  1502. ))
  1503. goto ErrorReturn;
  1504. fResult = CryptGetHashParam(
  1505. hHash,
  1506. HP_HASHVAL,
  1507. pbComputedHash,
  1508. pcbComputedHash,
  1509. 0 // dwFlags
  1510. );
  1511. goto CommonReturn;
  1512. ErrorReturn:
  1513. fResult = FALSE;
  1514. *pcbComputedHash = 0;
  1515. CommonReturn:
  1516. dwErr = GetLastError();
  1517. if (hHash)
  1518. CryptDestroyHash(hHash);
  1519. PkiFree(pSignedInfo);
  1520. SetLastError(dwErr);
  1521. return fResult;
  1522. }
  1523. //+-------------------------------------------------------------------------
  1524. // Sign the "to be signed" information in the encoded signed content.
  1525. //
  1526. // hCryptProv specifies the crypto provider to use to do the signature.
  1527. // It needs to use the provider's signature private key.
  1528. //--------------------------------------------------------------------------
  1529. BOOL
  1530. WINAPI
  1531. CryptSignCertificate(
  1532. IN HCRYPTPROV hCryptProv,
  1533. IN DWORD dwKeySpec,
  1534. IN DWORD dwCertEncodingType,
  1535. IN const BYTE *pbEncodedToBeSigned,
  1536. IN DWORD cbEncodedToBeSigned,
  1537. IN PCRYPT_ALGORITHM_IDENTIFIER pSignatureAlgorithm,
  1538. IN OPTIONAL const void *pvHashAuxInfo,
  1539. OUT BYTE *pbSignature,
  1540. IN OUT DWORD *pcbSignature
  1541. )
  1542. {
  1543. BOOL fResult;
  1544. ALG_ID aiHash;
  1545. ALG_ID aiPubKey;
  1546. DWORD dwSignFlags;
  1547. HCRYPTHASH hHash = 0;
  1548. DWORD dwErr;
  1549. if (!GetSignOIDInfo(pSignatureAlgorithm->pszObjId,
  1550. &aiHash, &aiPubKey, &dwSignFlags))
  1551. goto ErrorReturn;
  1552. if (CALG_NO_SIGN == aiPubKey) {
  1553. fResult = CryptHashCertificate(
  1554. hCryptProv,
  1555. aiHash,
  1556. 0, // dwFlags
  1557. pbEncodedToBeSigned,
  1558. cbEncodedToBeSigned,
  1559. pbSignature,
  1560. pcbSignature
  1561. );
  1562. if (fResult && pbSignature)
  1563. // A subsequent CryptEncodeObject(X509_CERT) will reverse
  1564. // the signature bytes
  1565. PkiAsn1ReverseBytes(pbSignature, *pcbSignature);
  1566. return fResult;
  1567. }
  1568. if (CALG_DSS_SIGN == aiPubKey &&
  1569. 0 == (dwSignFlags & CRYPT_OID_INHIBIT_SIGNATURE_FORMAT_FLAG)) {
  1570. if (NULL == pbSignature) {
  1571. *pcbSignature = CERT_MAX_ASN_ENCODED_DSS_SIGNATURE_LEN;
  1572. return TRUE;
  1573. }
  1574. }
  1575. if (!CryptCreateHash(
  1576. hCryptProv,
  1577. aiHash,
  1578. NULL, // hKey - optional for MAC
  1579. 0, // dwFlags,
  1580. &hHash
  1581. ))
  1582. goto ErrorReturn;
  1583. if (!CryptHashData(
  1584. hHash,
  1585. pbEncodedToBeSigned,
  1586. cbEncodedToBeSigned,
  1587. 0 // dwFlags
  1588. ))
  1589. goto ErrorReturn;
  1590. if (CALG_DSS_SIGN == aiPubKey &&
  1591. 0 == (dwSignFlags & CRYPT_OID_INHIBIT_SIGNATURE_FORMAT_FLAG)) {
  1592. DWORD cbData;
  1593. BYTE rgbDssSignature[CERT_DSS_SIGNATURE_LEN];
  1594. cbData = sizeof(rgbDssSignature);
  1595. if (!CryptSignHash(
  1596. hHash,
  1597. dwKeySpec,
  1598. NULL, // sDescription
  1599. 0, // dwFlags
  1600. rgbDssSignature,
  1601. &cbData
  1602. )) goto ErrorReturn;
  1603. assert(cbData == sizeof(rgbDssSignature));
  1604. // Convert from the CSP signature format to an ASN.1 sequence of
  1605. // two integers
  1606. fResult = CryptEncodeObject(
  1607. dwCertEncodingType,
  1608. X509_DSS_SIGNATURE,
  1609. rgbDssSignature,
  1610. pbSignature,
  1611. pcbSignature
  1612. );
  1613. if (fResult)
  1614. // A subsequent CryptEncodeObject(X509_CERT) will reverse
  1615. // the signature bytes
  1616. PkiAsn1ReverseBytes(pbSignature, *pcbSignature);
  1617. else if (0 != *pcbSignature)
  1618. // Since a random number is used in each CryptSignHash invocation,
  1619. // the generated signature will be different. In particular
  1620. // different signatures may have different leading 0x00's or
  1621. // 0xFF's which get removed when converted to the ASN.1 sequence
  1622. // of integers.
  1623. *pcbSignature = CERT_MAX_ASN_ENCODED_DSS_SIGNATURE_LEN;
  1624. } else
  1625. fResult = CryptSignHash(
  1626. hHash,
  1627. dwKeySpec,
  1628. NULL, // sDescription
  1629. 0, // dwFlags
  1630. pbSignature, // pbData
  1631. pcbSignature
  1632. );
  1633. goto CommonReturn;
  1634. ErrorReturn:
  1635. fResult = FALSE;
  1636. *pcbSignature = 0;
  1637. CommonReturn:
  1638. dwErr = GetLastError();
  1639. if (hHash)
  1640. CryptDestroyHash(hHash);
  1641. SetLastError(dwErr);
  1642. return fResult;
  1643. }
  1644. static DWORD AdjustForMaximumEncodedSignatureLength(
  1645. IN PCRYPT_ALGORITHM_IDENTIFIER pSignatureAlgorithm,
  1646. IN DWORD cbOrig
  1647. )
  1648. {
  1649. DWORD cbAdjust;
  1650. ALG_ID aiHash;
  1651. ALG_ID aiPubKey;
  1652. DWORD dwSignFlags;
  1653. cbAdjust = 0;
  1654. if (GetSignOIDInfo(pSignatureAlgorithm->pszObjId,
  1655. &aiHash, &aiPubKey, &dwSignFlags)) {
  1656. if (CALG_DSS_SIGN == aiPubKey &&
  1657. 0 == (dwSignFlags & CRYPT_OID_INHIBIT_SIGNATURE_FORMAT_FLAG)) {
  1658. assert(CERT_MAX_ASN_ENCODED_DSS_SIGNATURE_LEN >= cbOrig);
  1659. if (CERT_MAX_ASN_ENCODED_DSS_SIGNATURE_LEN > cbOrig)
  1660. // the +1 is for adjusting the number of length octets in
  1661. // the outer SEQUENCE. Note, the number of length octets in
  1662. // the signature's BITSTRING will always be 1, ie,
  1663. // CERT_MAX_ASN_ENCODED_DSS_SIGNATURE_LEN <= 0x7F.
  1664. cbAdjust =
  1665. (CERT_MAX_ASN_ENCODED_DSS_SIGNATURE_LEN - cbOrig) + 1;
  1666. }
  1667. }
  1668. return cbAdjust;
  1669. }
  1670. //+-------------------------------------------------------------------------
  1671. // Encode the "to be signed" information. Sign the encoded "to be signed".
  1672. // Encode the "to be signed" and the signature.
  1673. //
  1674. // hCryptProv specifies the crypto provider to use to do the signature.
  1675. // It uses the specified private key.
  1676. //--------------------------------------------------------------------------
  1677. BOOL
  1678. WINAPI
  1679. CryptSignAndEncodeCertificate(
  1680. IN HCRYPTPROV hCryptProv,
  1681. IN DWORD dwKeySpec,
  1682. IN DWORD dwCertEncodingType,
  1683. IN LPCSTR lpszStructType,
  1684. IN const void *pvStructInfo,
  1685. IN PCRYPT_ALGORITHM_IDENTIFIER pSignatureAlgorithm,
  1686. IN OPTIONAL const void *pvHashAuxInfo,
  1687. OUT BYTE *pbEncoded,
  1688. IN OUT DWORD *pcbEncoded
  1689. )
  1690. {
  1691. BOOL fResult;
  1692. CERT_SIGNED_CONTENT_INFO SignedInfo;
  1693. memset(&SignedInfo, 0, sizeof(SignedInfo));
  1694. SignedInfo.SignatureAlgorithm = *pSignatureAlgorithm;
  1695. if (!AllocAndEncodeObject(
  1696. dwCertEncodingType,
  1697. lpszStructType,
  1698. pvStructInfo,
  1699. &SignedInfo.ToBeSigned.pbData,
  1700. &SignedInfo.ToBeSigned.cbData
  1701. )) goto ErrorReturn;
  1702. CryptSignCertificate(
  1703. hCryptProv,
  1704. dwKeySpec,
  1705. dwCertEncodingType,
  1706. SignedInfo.ToBeSigned.pbData,
  1707. SignedInfo.ToBeSigned.cbData,
  1708. &SignedInfo.SignatureAlgorithm,
  1709. pvHashAuxInfo,
  1710. NULL, // pbSignature
  1711. &SignedInfo.Signature.cbData
  1712. );
  1713. if (SignedInfo.Signature.cbData == 0) goto ErrorReturn;
  1714. SignedInfo.Signature.pbData =
  1715. (BYTE *) PkiNonzeroAlloc(SignedInfo.Signature.cbData);
  1716. if (SignedInfo.Signature.pbData == NULL) goto ErrorReturn;
  1717. if (pbEncoded) {
  1718. if (!CryptSignCertificate(
  1719. hCryptProv,
  1720. dwKeySpec,
  1721. dwCertEncodingType,
  1722. SignedInfo.ToBeSigned.pbData,
  1723. SignedInfo.ToBeSigned.cbData,
  1724. &SignedInfo.SignatureAlgorithm,
  1725. pvHashAuxInfo,
  1726. SignedInfo.Signature.pbData,
  1727. &SignedInfo.Signature.cbData
  1728. )) goto ErrorReturn;
  1729. }
  1730. fResult = CryptEncodeObject(
  1731. dwCertEncodingType,
  1732. X509_CERT,
  1733. &SignedInfo,
  1734. pbEncoded,
  1735. pcbEncoded
  1736. );
  1737. if (!fResult && *pcbEncoded) {
  1738. *pcbEncoded += AdjustForMaximumEncodedSignatureLength(
  1739. &SignedInfo.SignatureAlgorithm,
  1740. SignedInfo.Signature.cbData
  1741. );
  1742. }
  1743. CommonReturn:
  1744. PkiFree(SignedInfo.ToBeSigned.pbData);
  1745. PkiFree(SignedInfo.Signature.pbData);
  1746. return fResult;
  1747. ErrorReturn:
  1748. fResult = FALSE;
  1749. *pcbEncoded = 0;
  1750. goto CommonReturn;
  1751. }
  1752. //+-------------------------------------------------------------------------
  1753. // Verify the time validity of a certificate.
  1754. //
  1755. // Returns -1 if before NotBefore, +1 if after NotAfter and otherwise 0 for
  1756. // a valid certificate
  1757. //
  1758. // If pTimeToVerify is NULL, uses the current time.
  1759. //--------------------------------------------------------------------------
  1760. LONG
  1761. WINAPI
  1762. CertVerifyTimeValidity(
  1763. IN LPFILETIME pTimeToVerify,
  1764. IN PCERT_INFO pCertInfo
  1765. )
  1766. {
  1767. SYSTEMTIME SystemTime;
  1768. FILETIME FileTime;
  1769. LPFILETIME pFileTime;
  1770. if (pTimeToVerify)
  1771. pFileTime = pTimeToVerify;
  1772. else {
  1773. GetSystemTime(&SystemTime);
  1774. SystemTimeToFileTime(&SystemTime, &FileTime);
  1775. pFileTime = &FileTime;
  1776. }
  1777. if (CompareFileTime(pFileTime, &pCertInfo->NotBefore) < 0)
  1778. return -1;
  1779. else if (CompareFileTime(pFileTime, &pCertInfo->NotAfter) > 0)
  1780. return 1;
  1781. else
  1782. return 0;
  1783. }
  1784. //+-------------------------------------------------------------------------
  1785. // Verify the time validity of a CRL.
  1786. //
  1787. // Returns -1 if before ThisUpdate, +1 if after NextUpdate and otherwise 0 for
  1788. // a valid CRL
  1789. //
  1790. // If pTimeToVerify is NULL, uses the current time.
  1791. //--------------------------------------------------------------------------
  1792. LONG
  1793. WINAPI
  1794. CertVerifyCRLTimeValidity(
  1795. IN LPFILETIME pTimeToVerify,
  1796. IN PCRL_INFO pCrlInfo
  1797. )
  1798. {
  1799. SYSTEMTIME SystemTime;
  1800. FILETIME FileTime;
  1801. LPFILETIME pFileTime;
  1802. if (pTimeToVerify)
  1803. pFileTime = pTimeToVerify;
  1804. else {
  1805. GetSystemTime(&SystemTime);
  1806. SystemTimeToFileTime(&SystemTime, &FileTime);
  1807. pFileTime = &FileTime;
  1808. }
  1809. // Note, NextUpdate is optional. When not present, set to 0
  1810. if (CompareFileTime(pFileTime, &pCrlInfo->ThisUpdate) < 0)
  1811. return -1;
  1812. else if ((pCrlInfo->NextUpdate.dwLowDateTime ||
  1813. pCrlInfo->NextUpdate.dwHighDateTime) &&
  1814. CompareFileTime(pFileTime, &pCrlInfo->NextUpdate) > 0)
  1815. return 1;
  1816. else
  1817. return 0;
  1818. }
  1819. //+-------------------------------------------------------------------------
  1820. // Verify that the subject's time validity nests within the issuer's time
  1821. // validity.
  1822. //
  1823. // Returns TRUE if it nests. Otherwise, returns FALSE.
  1824. //--------------------------------------------------------------------------
  1825. BOOL
  1826. WINAPI
  1827. CertVerifyValidityNesting(
  1828. IN PCERT_INFO pSubjectInfo,
  1829. IN PCERT_INFO pIssuerInfo
  1830. )
  1831. {
  1832. if ((CompareFileTime(&pSubjectInfo->NotBefore,
  1833. &pIssuerInfo->NotBefore) >= 0) &&
  1834. (CompareFileTime(&pSubjectInfo->NotAfter,
  1835. &pIssuerInfo->NotAfter) <= 0))
  1836. return TRUE;
  1837. else
  1838. return FALSE;
  1839. }
  1840. //+-------------------------------------------------------------------------
  1841. // Verify that the subject certificate isn't on its issuer CRL.
  1842. //
  1843. // Returns true if the certificate isn't on the CRL.
  1844. //--------------------------------------------------------------------------
  1845. BOOL
  1846. WINAPI
  1847. CertVerifyCRLRevocation(
  1848. IN DWORD dwCertEncodingType,
  1849. IN PCERT_INFO pCertId, // Only the Issuer and SerialNumber
  1850. // fields are used
  1851. IN DWORD cCrlInfo,
  1852. IN PCRL_INFO rgpCrlInfo[]
  1853. )
  1854. {
  1855. DWORD InfoIdx;
  1856. for (InfoIdx = 0; InfoIdx < cCrlInfo; InfoIdx++) {
  1857. DWORD cEntry = rgpCrlInfo[InfoIdx]->cCRLEntry;
  1858. PCRL_ENTRY rgEntry = rgpCrlInfo[InfoIdx]->rgCRLEntry;
  1859. DWORD EntryIdx;
  1860. for (EntryIdx = 0; EntryIdx < cEntry; EntryIdx++) {
  1861. if (CertCompareIntegerBlob(&rgEntry[EntryIdx].SerialNumber,
  1862. &pCertId->SerialNumber))
  1863. // It has been revoked!!!
  1864. return FALSE;
  1865. }
  1866. }
  1867. return TRUE;
  1868. }
  1869. //+-------------------------------------------------------------------------
  1870. // Convert the CAPI AlgId to the ASN.1 Object Identifier string
  1871. //
  1872. // Returns NULL if there isn't an ObjId corresponding to the AlgId.
  1873. //--------------------------------------------------------------------------
  1874. LPCSTR
  1875. WINAPI
  1876. CertAlgIdToOID(
  1877. IN DWORD dwAlgId
  1878. )
  1879. {
  1880. DWORD dwGroupId;
  1881. for (dwGroupId = CRYPT_FIRST_ALG_OID_GROUP_ID;
  1882. dwGroupId <= CRYPT_LAST_ALG_OID_GROUP_ID; dwGroupId++) {
  1883. PCCRYPT_OID_INFO pInfo;
  1884. if (pInfo = CryptFindOIDInfo(
  1885. CRYPT_OID_INFO_ALGID_KEY,
  1886. &dwAlgId,
  1887. dwGroupId
  1888. ))
  1889. return pInfo->pszOID;
  1890. }
  1891. return NULL;
  1892. }
  1893. //+-------------------------------------------------------------------------
  1894. // Convert the ASN.1 Object Identifier string to the CAPI AlgId.
  1895. //
  1896. // Returns 0 if there isn't an AlgId corresponding to the ObjId.
  1897. //--------------------------------------------------------------------------
  1898. DWORD
  1899. WINAPI
  1900. CertOIDToAlgId(
  1901. IN LPCSTR pszObjId
  1902. )
  1903. {
  1904. DWORD dwGroupId;
  1905. for (dwGroupId = CRYPT_FIRST_ALG_OID_GROUP_ID;
  1906. dwGroupId <= CRYPT_LAST_ALG_OID_GROUP_ID; dwGroupId++) {
  1907. PCCRYPT_OID_INFO pInfo;
  1908. if (pInfo = CryptFindOIDInfo(
  1909. CRYPT_OID_INFO_OID_KEY,
  1910. (void *) pszObjId,
  1911. dwGroupId
  1912. ))
  1913. return pInfo->Algid;
  1914. }
  1915. return 0;
  1916. }
  1917. //+-------------------------------------------------------------------------
  1918. // Find an extension identified by its Object Identifier.
  1919. //
  1920. // If found, returns pointer to the extension. Otherwise, returns NULL.
  1921. //--------------------------------------------------------------------------
  1922. PCERT_EXTENSION
  1923. WINAPI
  1924. CertFindExtension(
  1925. IN LPCSTR pszObjId,
  1926. IN DWORD cExtensions,
  1927. IN CERT_EXTENSION rgExtensions[]
  1928. )
  1929. {
  1930. for (; cExtensions > 0; cExtensions--, rgExtensions++) {
  1931. if (strcmp(pszObjId, rgExtensions->pszObjId) == 0)
  1932. return rgExtensions;
  1933. }
  1934. return NULL;
  1935. }
  1936. //+-------------------------------------------------------------------------
  1937. // Find the first attribute identified by its Object Identifier.
  1938. //
  1939. // If found, returns pointer to the attribute. Otherwise, returns NULL.
  1940. //--------------------------------------------------------------------------
  1941. PCRYPT_ATTRIBUTE
  1942. WINAPI
  1943. CertFindAttribute(
  1944. IN LPCSTR pszObjId,
  1945. IN DWORD cAttr,
  1946. IN CRYPT_ATTRIBUTE rgAttr[]
  1947. )
  1948. {
  1949. for (; cAttr > 0; cAttr--, rgAttr++) {
  1950. if (strcmp(pszObjId, rgAttr->pszObjId) == 0)
  1951. return rgAttr;
  1952. }
  1953. return NULL;
  1954. }
  1955. //+-------------------------------------------------------------------------
  1956. // Find the first CERT_RDN attribute identified by its Object Identifier in
  1957. // the name's list of Relative Distinguished Names.
  1958. //
  1959. // If found, returns pointer to the attribute. Otherwise, returns NULL.
  1960. //--------------------------------------------------------------------------
  1961. PCERT_RDN_ATTR
  1962. WINAPI
  1963. CertFindRDNAttr(
  1964. IN LPCSTR pszObjId,
  1965. IN PCERT_NAME_INFO pName
  1966. )
  1967. {
  1968. DWORD cRDN = pName->cRDN;
  1969. PCERT_RDN pRDN = pName->rgRDN;
  1970. for ( ; cRDN > 0; cRDN--, pRDN++) {
  1971. DWORD cRDNAttr = pRDN->cRDNAttr;
  1972. PCERT_RDN_ATTR pRDNAttr = pRDN->rgRDNAttr;
  1973. for (; cRDNAttr > 0; cRDNAttr--, pRDNAttr++) {
  1974. if (strcmp(pszObjId, pRDNAttr->pszObjId) == 0)
  1975. return pRDNAttr;
  1976. }
  1977. }
  1978. return NULL;
  1979. }
  1980. //+-------------------------------------------------------------------------
  1981. // Get the intended key usage bytes from the certificate.
  1982. //
  1983. // If the certificate doesn't have any intended key usage bytes, returns FALSE
  1984. // and *pbKeyUsage is zeroed. Otherwise, returns TRUE and up through
  1985. // cbKeyUsage bytes are copied into *pbKeyUsage. Any remaining uncopied
  1986. // bytes are zeroed.
  1987. //--------------------------------------------------------------------------
  1988. BOOL
  1989. WINAPI
  1990. CertGetIntendedKeyUsage(
  1991. IN DWORD dwCertEncodingType,
  1992. IN PCERT_INFO pCertInfo,
  1993. OUT BYTE *pbKeyUsage,
  1994. IN DWORD cbKeyUsage
  1995. )
  1996. {
  1997. BOOL fResult;
  1998. DWORD cbData;
  1999. PCERT_EXTENSION pExt;
  2000. PCERT_KEY_ATTRIBUTES_INFO pKeyAttrInfo = NULL;
  2001. PCRYPT_BIT_BLOB pAllocKeyUsage = NULL;
  2002. PCRYPT_BIT_BLOB pKeyUsage = NULL; // not allocated
  2003. // First see if the certificate has the simple Key Usage Extension
  2004. if (NULL != (pExt = CertFindExtension(
  2005. szOID_KEY_USAGE,
  2006. pCertInfo->cExtension,
  2007. pCertInfo->rgExtension
  2008. )) &&
  2009. NULL != (pAllocKeyUsage =
  2010. (PCRYPT_BIT_BLOB) AllocAndDecodeObject(
  2011. dwCertEncodingType,
  2012. X509_KEY_USAGE,
  2013. pExt->Value.pbData,
  2014. pExt->Value.cbData
  2015. )))
  2016. pKeyUsage = pAllocKeyUsage;
  2017. else {
  2018. pExt = CertFindExtension(
  2019. szOID_KEY_ATTRIBUTES,
  2020. pCertInfo->cExtension,
  2021. pCertInfo->rgExtension
  2022. );
  2023. if (pExt == NULL) goto GetError;
  2024. if (NULL == (pKeyAttrInfo =
  2025. (PCERT_KEY_ATTRIBUTES_INFO) AllocAndDecodeObject(
  2026. dwCertEncodingType,
  2027. X509_KEY_ATTRIBUTES,
  2028. pExt->Value.pbData,
  2029. pExt->Value.cbData
  2030. ))) goto ErrorReturn;
  2031. pKeyUsage = &pKeyAttrInfo->IntendedKeyUsage;
  2032. }
  2033. if (pKeyUsage->cbData == 0 || cbKeyUsage == 0)
  2034. goto GetError;
  2035. cbData = min(pKeyUsage->cbData, cbKeyUsage);
  2036. memcpy(pbKeyUsage, pKeyUsage->pbData, cbData);
  2037. fResult = TRUE;
  2038. goto CommonReturn;
  2039. GetError:
  2040. SetLastError(0);
  2041. ErrorReturn:
  2042. fResult = FALSE;
  2043. cbData = 0;
  2044. CommonReturn:
  2045. PkiFree(pAllocKeyUsage);
  2046. PkiFree(pKeyAttrInfo);
  2047. if (cbData < cbKeyUsage)
  2048. memset(pbKeyUsage + cbData, 0, cbKeyUsage - cbData);
  2049. return fResult;
  2050. }
  2051. static DWORD GetYPublicKeyLength(
  2052. IN DWORD dwCertEncodingType,
  2053. IN PCERT_PUBLIC_KEY_INFO pPublicKeyInfo
  2054. )
  2055. {
  2056. PCRYPT_UINT_BLOB pY = NULL;
  2057. DWORD dwBitLen;
  2058. if (NULL == (pY = (PCRYPT_UINT_BLOB) AllocAndDecodeObject(
  2059. dwCertEncodingType,
  2060. X509_MULTI_BYTE_UINT,
  2061. pPublicKeyInfo->PublicKey.pbData,
  2062. pPublicKeyInfo->PublicKey.cbData
  2063. ))) goto DecodePubKeyError;
  2064. dwBitLen = pY->cbData * 8;
  2065. CommonReturn:
  2066. PkiFree(pY);
  2067. return dwBitLen;
  2068. ErrorReturn:
  2069. dwBitLen = 0;
  2070. goto CommonReturn;
  2071. TRACE_ERROR(DecodePubKeyError)
  2072. }
  2073. // If there are parameters, use the length of the 'P' parameter. Otherwise,
  2074. // use the length of Y. Note, P's MSB must be set. Y's MSB may not be set.
  2075. static DWORD GetDHPublicKeyLength(
  2076. IN DWORD dwCertEncodingType,
  2077. IN PCERT_PUBLIC_KEY_INFO pPublicKey
  2078. )
  2079. {
  2080. PCERT_X942_DH_PARAMETERS pDhParameters = NULL;
  2081. DWORD dwBitLen;
  2082. if (0 == pPublicKey->Algorithm.Parameters.cbData)
  2083. goto NoDhParametersError;
  2084. if (NULL == (pDhParameters =
  2085. (PCERT_X942_DH_PARAMETERS) AllocAndDecodeObject(
  2086. dwCertEncodingType,
  2087. X942_DH_PARAMETERS,
  2088. pPublicKey->Algorithm.Parameters.pbData,
  2089. pPublicKey->Algorithm.Parameters.cbData
  2090. ))) goto DecodeParametersError;
  2091. dwBitLen = pDhParameters->p.cbData * 8;
  2092. CommonReturn:
  2093. PkiFree(pDhParameters);
  2094. return dwBitLen;
  2095. ErrorReturn:
  2096. dwBitLen = GetYPublicKeyLength(dwCertEncodingType, pPublicKey);
  2097. goto CommonReturn;
  2098. TRACE_ERROR(NoDhParametersError)
  2099. TRACE_ERROR(DecodeParametersError)
  2100. }
  2101. // If there are parameters, use the length of the 'P' parameter. Otherwise,
  2102. // use the length of Y. Note, P's MSB must be set. Y's MSB may not be set.
  2103. static DWORD GetDSSPublicKeyLength(
  2104. IN DWORD dwCertEncodingType,
  2105. IN PCERT_PUBLIC_KEY_INFO pPublicKey
  2106. )
  2107. {
  2108. PCERT_DSS_PARAMETERS pDssParameters = NULL;
  2109. DWORD dwBitLen;
  2110. if (0 == pPublicKey->Algorithm.Parameters.cbData)
  2111. goto NoDssParametersError;
  2112. if (NULL == (pDssParameters = (PCERT_DSS_PARAMETERS) AllocAndDecodeObject(
  2113. dwCertEncodingType,
  2114. X509_DSS_PARAMETERS,
  2115. pPublicKey->Algorithm.Parameters.pbData,
  2116. pPublicKey->Algorithm.Parameters.cbData
  2117. ))) goto DecodeParametersError;
  2118. dwBitLen = pDssParameters->p.cbData * 8;
  2119. CommonReturn:
  2120. PkiFree(pDssParameters);
  2121. return dwBitLen;
  2122. ErrorReturn:
  2123. dwBitLen = GetYPublicKeyLength(dwCertEncodingType, pPublicKey);
  2124. goto CommonReturn;
  2125. TRACE_ERROR(NoDssParametersError)
  2126. TRACE_ERROR(DecodeParametersError)
  2127. }
  2128. //+-------------------------------------------------------------------------
  2129. // Get the public/private key's bit length.
  2130. //
  2131. // Returns 0 if unable to determine the key's length.
  2132. //--------------------------------------------------------------------------
  2133. DWORD
  2134. WINAPI
  2135. CertGetPublicKeyLength(
  2136. IN DWORD dwCertEncodingType,
  2137. IN PCERT_PUBLIC_KEY_INFO pPublicKey
  2138. )
  2139. {
  2140. DWORD dwErr = 0;
  2141. DWORD dwBitLen;
  2142. ALG_ID aiPubKey;
  2143. PCCRYPT_OID_INFO pOIDInfo;
  2144. HCRYPTPROV hCryptProv; // don't need to release
  2145. HCRYPTKEY hPubKey = 0;
  2146. DWORD cbData;
  2147. if (pOIDInfo = CryptFindOIDInfo(
  2148. CRYPT_OID_INFO_OID_KEY,
  2149. pPublicKey->Algorithm.pszObjId,
  2150. CRYPT_PUBKEY_ALG_OID_GROUP_ID))
  2151. aiPubKey = pOIDInfo->Algid;
  2152. else
  2153. aiPubKey = 0;
  2154. if (aiPubKey == CALG_DH_SF || aiPubKey == CALG_DH_EPHEM)
  2155. return GetDHPublicKeyLength(
  2156. dwCertEncodingType,
  2157. pPublicKey
  2158. );
  2159. if (aiPubKey == CALG_DSS_SIGN)
  2160. return GetDSSPublicKeyLength(
  2161. dwCertEncodingType,
  2162. pPublicKey
  2163. );
  2164. if (0 == (hCryptProv = I_CryptGetDefaultCryptProv(aiPubKey)))
  2165. goto GetDefaultCryptProvError;
  2166. if (!CryptImportPublicKeyInfo(
  2167. hCryptProv,
  2168. dwCertEncodingType,
  2169. pPublicKey,
  2170. &hPubKey
  2171. )) goto ImportPublicKeyError;
  2172. cbData = sizeof(dwBitLen);
  2173. if (CryptGetKeyParam(
  2174. hPubKey,
  2175. KP_KEYLEN,
  2176. (BYTE *) &dwBitLen,
  2177. &cbData,
  2178. 0)) // dwFlags
  2179. goto CommonReturn;
  2180. cbData = sizeof(dwBitLen);
  2181. if (CryptGetKeyParam(
  2182. hPubKey,
  2183. KP_BLOCKLEN,
  2184. (BYTE *) &dwBitLen,
  2185. &cbData,
  2186. 0)) // dwFlags
  2187. goto CommonReturn;
  2188. {
  2189. // The CSP should have supported one of the above
  2190. // Export the public key and look at the bitlen field.
  2191. // The CAPI public key representation consists of the following
  2192. // sequence:
  2193. // - PUBLICKEYSTRUC
  2194. // - DSSPUBKEY | RSAPUBKEY (DSSPUBKEY is subset of RSAPUBKEY)
  2195. // ...
  2196. BYTE *pbPubKey = NULL;
  2197. DWORD cbPubKey;
  2198. dwBitLen = 0;
  2199. dwErr = GetLastError();
  2200. cbPubKey = 0;
  2201. if (CryptExportKey(
  2202. hPubKey,
  2203. 0, // hPubKey
  2204. PUBLICKEYBLOB,
  2205. 0, // dwFlags
  2206. NULL, // pbData
  2207. &cbPubKey
  2208. ) &&
  2209. cbPubKey >= (sizeof(PUBLICKEYSTRUC) + sizeof(DSSPUBKEY)) &&
  2210. NULL != (pbPubKey = (BYTE *) PkiNonzeroAlloc(cbPubKey))) {
  2211. if (CryptExportKey(
  2212. hPubKey,
  2213. 0, // hPubKey
  2214. PUBLICKEYBLOB,
  2215. 0, // dwFlags
  2216. pbPubKey,
  2217. &cbPubKey
  2218. )) {
  2219. DSSPUBKEY *pPubKey =
  2220. (DSSPUBKEY *) (pbPubKey + sizeof(PUBLICKEYSTRUC));
  2221. dwBitLen = pPubKey->bitlen;
  2222. }
  2223. PkiFree(pbPubKey);
  2224. }
  2225. if (0 != dwBitLen)
  2226. goto CommonReturn;
  2227. SetLastError(dwErr);
  2228. goto GetKeyParamError;
  2229. }
  2230. CommonReturn:
  2231. if (hPubKey)
  2232. CryptDestroyKey(hPubKey);
  2233. SetLastError(dwErr);
  2234. return dwBitLen;
  2235. ErrorReturn:
  2236. dwBitLen = 0;
  2237. dwErr = GetLastError();
  2238. goto CommonReturn;
  2239. TRACE_ERROR(GetDefaultCryptProvError)
  2240. TRACE_ERROR(ImportPublicKeyError)
  2241. TRACE_ERROR(GetKeyParamError)
  2242. }
  2243. //+-------------------------------------------------------------------------
  2244. // Compute the hash of the encoded public key info.
  2245. //
  2246. // The public key info is encoded and then hashed.
  2247. //--------------------------------------------------------------------------
  2248. BOOL
  2249. WINAPI
  2250. CryptHashPublicKeyInfo(
  2251. IN HCRYPTPROV hCryptProv,
  2252. IN ALG_ID Algid,
  2253. IN DWORD dwFlags,
  2254. IN DWORD dwCertEncodingType,
  2255. IN PCERT_PUBLIC_KEY_INFO pInfo,
  2256. OUT BYTE *pbComputedHash,
  2257. IN OUT DWORD *pcbComputedHash
  2258. )
  2259. {
  2260. BOOL fResult;
  2261. BYTE *pbEncoded = NULL;
  2262. DWORD cbEncoded;
  2263. if (!AllocAndEncodeObject(
  2264. dwCertEncodingType,
  2265. X509_PUBLIC_KEY_INFO,
  2266. pInfo,
  2267. &pbEncoded,
  2268. &cbEncoded
  2269. ))
  2270. goto ErrorReturn;
  2271. fResult = CryptHashCertificate(
  2272. hCryptProv,
  2273. Algid ? Algid : CALG_MD5,
  2274. dwFlags,
  2275. pbEncoded,
  2276. cbEncoded,
  2277. pbComputedHash,
  2278. pcbComputedHash
  2279. );
  2280. goto CommonReturn;
  2281. ErrorReturn:
  2282. fResult = FALSE;
  2283. *pcbComputedHash = 0;
  2284. CommonReturn:
  2285. PkiFree(pbEncoded);
  2286. return fResult;
  2287. }
  2288. //+-------------------------------------------------------------------------
  2289. // Compares the certificate's public key with the provider's public key
  2290. // to see if they are identical.
  2291. //
  2292. // Returns TRUE if the keys are identical.
  2293. //--------------------------------------------------------------------------
  2294. BOOL
  2295. WINAPI
  2296. I_CertCompareCertAndProviderPublicKey(
  2297. IN PCCERT_CONTEXT pCert,
  2298. IN HCRYPTPROV hProv,
  2299. IN DWORD dwKeySpec
  2300. )
  2301. {
  2302. BOOL fResult;
  2303. PCERT_PUBLIC_KEY_INFO pProvPubKeyInfo = NULL;
  2304. DWORD cbProvPubKeyInfo;
  2305. DWORD dwCertEncodingType = pCert->dwCertEncodingType;
  2306. // Get provider's public key
  2307. if (!CryptExportPublicKeyInfo(
  2308. hProv,
  2309. dwKeySpec,
  2310. dwCertEncodingType,
  2311. NULL, // pProvPubKeyInfo
  2312. &cbProvPubKeyInfo
  2313. ))
  2314. goto ExportPublicKeyInfoError;
  2315. assert(cbProvPubKeyInfo);
  2316. if (NULL == (pProvPubKeyInfo = (PCERT_PUBLIC_KEY_INFO) PkiNonzeroAlloc(
  2317. cbProvPubKeyInfo)))
  2318. goto OutOfMemory;
  2319. if (!CryptExportPublicKeyInfo(
  2320. hProv,
  2321. dwKeySpec,
  2322. dwCertEncodingType,
  2323. pProvPubKeyInfo,
  2324. &cbProvPubKeyInfo
  2325. ))
  2326. goto ExportPublicKeyInfoError;
  2327. if (!CertComparePublicKeyInfo(
  2328. dwCertEncodingType,
  2329. &pCert->pCertInfo->SubjectPublicKeyInfo,
  2330. pProvPubKeyInfo
  2331. ))
  2332. goto ComparePublicKeyError;
  2333. fResult = TRUE;
  2334. CommonReturn:
  2335. PkiFree(pProvPubKeyInfo);
  2336. return fResult;
  2337. ErrorReturn:
  2338. fResult = FALSE;
  2339. goto CommonReturn;
  2340. TRACE_ERROR(ExportPublicKeyInfoError)
  2341. TRACE_ERROR(OutOfMemory)
  2342. SET_ERROR(ComparePublicKeyError, NTE_BAD_PUBLIC_KEY)
  2343. }
  2344. //+=========================================================================
  2345. // CryptFindCertificateKeyProvInfo Support Functions
  2346. //-=========================================================================
  2347. static BOOL HasValidKeyProvInfo(
  2348. IN PCCERT_CONTEXT pCert,
  2349. IN DWORD dwFindKeySetFlags
  2350. )
  2351. {
  2352. BOOL fResult;
  2353. PCRYPT_KEY_PROV_INFO pKeyProvInfo = NULL;
  2354. HCRYPTPROV hProv = 0;
  2355. DWORD cbData;
  2356. DWORD dwAcquireFlags;
  2357. if (!CertGetCertificateContextProperty(
  2358. pCert,
  2359. CERT_KEY_PROV_INFO_PROP_ID,
  2360. NULL, // pvData
  2361. &cbData
  2362. ))
  2363. return FALSE;
  2364. if (NULL == (pKeyProvInfo = (PCRYPT_KEY_PROV_INFO) PkiNonzeroAlloc(
  2365. cbData)))
  2366. goto OutOfMemory;
  2367. if (!CertGetCertificateContextProperty(
  2368. pCert,
  2369. CERT_KEY_PROV_INFO_PROP_ID,
  2370. pKeyProvInfo,
  2371. &cbData
  2372. ))
  2373. goto GetKeyProvInfoPropertyError;
  2374. if (pKeyProvInfo->dwFlags & CRYPT_MACHINE_KEYSET) {
  2375. if (0 == (dwFindKeySetFlags & CRYPT_FIND_MACHINE_KEYSET_FLAG))
  2376. goto NotUserContainer;
  2377. } else {
  2378. if (0 == (dwFindKeySetFlags & CRYPT_FIND_USER_KEYSET_FLAG))
  2379. goto NotMachineContainer;
  2380. }
  2381. dwAcquireFlags = CRYPT_ACQUIRE_COMPARE_KEY_FLAG;
  2382. if (dwFindKeySetFlags & CRYPT_FIND_SILENT_KEYSET_FLAG)
  2383. dwAcquireFlags |= CRYPT_ACQUIRE_SILENT_FLAG;
  2384. if (!CryptAcquireCertificatePrivateKey(
  2385. pCert,
  2386. dwAcquireFlags,
  2387. NULL, // pvReserved
  2388. &hProv,
  2389. NULL, // pdwKeySpec
  2390. NULL // pfCallerFreeProv
  2391. ))
  2392. goto AcquireCertPrivateKeyError;
  2393. fResult = TRUE;
  2394. CommonReturn:
  2395. PkiFree(pKeyProvInfo);
  2396. if (hProv) {
  2397. DWORD dwErr = GetLastError();
  2398. CryptReleaseContext(hProv, 0);
  2399. SetLastError(dwErr);
  2400. }
  2401. return fResult;
  2402. ErrorReturn:
  2403. fResult = FALSE;
  2404. goto CommonReturn;
  2405. TRACE_ERROR(OutOfMemory)
  2406. TRACE_ERROR(GetKeyProvInfoPropertyError)
  2407. SET_ERROR(NotUserContainer, NTE_NOT_FOUND)
  2408. SET_ERROR(NotMachineContainer, NTE_NOT_FOUND)
  2409. TRACE_ERROR(AcquireCertPrivateKeyError)
  2410. }
  2411. // Default to Algid being supported. Only return FALSE if successfully
  2412. // enumerated all the provider algorithms and didn't find a match.
  2413. static BOOL IsPublicKeyAlgidSupported(
  2414. IN PCCERT_CONTEXT pCert,
  2415. IN HCRYPTPROV hProv,
  2416. IN ALG_ID aiPubKey
  2417. )
  2418. {
  2419. BOOL fResult;
  2420. DWORD dwErr;
  2421. BYTE *pbData = NULL;
  2422. DWORD cbMaxData;
  2423. DWORD cbData;
  2424. DWORD dwFlags;
  2425. if (0 == aiPubKey)
  2426. return TRUE;
  2427. // Get maximum length of provider algorithm parameter data
  2428. cbMaxData = 0;
  2429. if (!CryptGetProvParam(
  2430. hProv,
  2431. PP_ENUMALGS,
  2432. NULL, // pbData
  2433. &cbMaxData,
  2434. CRYPT_FIRST // dwFlags
  2435. )) {
  2436. dwErr = GetLastError();
  2437. if (ERROR_MORE_DATA != dwErr)
  2438. goto GetProvAlgParamError;
  2439. }
  2440. if (0 == cbMaxData)
  2441. goto NoProvAlgParamError;
  2442. if (NULL == (pbData = (BYTE *) PkiNonzeroAlloc(cbMaxData)))
  2443. goto OutOfMemory;
  2444. dwFlags = CRYPT_FIRST;
  2445. while (TRUE) {
  2446. ALG_ID aiProv;
  2447. cbData = cbMaxData;
  2448. if (!CryptGetProvParam(
  2449. hProv,
  2450. PP_ENUMALGS,
  2451. pbData,
  2452. &cbData,
  2453. dwFlags
  2454. )) {
  2455. dwErr = GetLastError();
  2456. if (ERROR_NO_MORE_ITEMS == dwErr) {
  2457. fResult = FALSE;
  2458. goto PublicKeyAlgidNotSupported;
  2459. } else
  2460. goto GetProvAlgParamError;
  2461. }
  2462. assert(cbData >= sizeof(ALG_ID));
  2463. aiProv = *(ALG_ID *) pbData;
  2464. // Don't distinguish between exchange or signature
  2465. if (GET_ALG_TYPE(aiPubKey) == GET_ALG_TYPE(aiProv))
  2466. break;
  2467. dwFlags = 0; // CRYPT_NEXT
  2468. }
  2469. fResult = TRUE;
  2470. PublicKeyAlgidNotSupported:
  2471. CommonReturn:
  2472. PkiFree(pbData);
  2473. return fResult;
  2474. ErrorReturn:
  2475. // For an error, assume the public key algorithm is supported.
  2476. fResult = TRUE;
  2477. goto CommonReturn;
  2478. SET_ERROR_VAR(GetProvAlgParamError, dwErr)
  2479. SET_ERROR(NoProvAlgParamError, NTE_NOT_FOUND)
  2480. TRACE_ERROR(OutOfMemory)
  2481. }
  2482. // For success, updates the certificate's KEY_PROV_INFO property
  2483. //
  2484. // If container isn't found, LastError is set to ERROR_NO_MORE_ITEMS.
  2485. //
  2486. static BOOL FindContainerAndSetKeyProvInfo(
  2487. IN PCCERT_CONTEXT pCert,
  2488. IN HCRYPTPROV hProv,
  2489. IN LPWSTR pwszProvName,
  2490. IN DWORD dwProvType,
  2491. IN DWORD dwProvFlags // CRYPT_MACHINE_KEYSET and/or CRYPT_SILENT
  2492. )
  2493. {
  2494. BOOL fResult;
  2495. DWORD dwEnumFlags;
  2496. DWORD dwEnumErr = 0;
  2497. DWORD dwAcquireErr = 0;
  2498. LPSTR pszContainerName = NULL;
  2499. DWORD cchContainerName;
  2500. DWORD cchMaxContainerName;
  2501. LPWSTR pwszContainerName = NULL;
  2502. // Get maximum container name length
  2503. cchMaxContainerName = 0;
  2504. if (!CryptGetProvParam(
  2505. hProv,
  2506. PP_ENUMCONTAINERS,
  2507. NULL, // pbData
  2508. &cchMaxContainerName,
  2509. CRYPT_FIRST
  2510. )) {
  2511. dwEnumErr = GetLastError();
  2512. if (ERROR_FILE_NOT_FOUND == dwEnumErr ||
  2513. ERROR_INVALID_PARAMETER == dwEnumErr)
  2514. goto PublicKeyContainerNotFound;
  2515. else if (ERROR_MORE_DATA != dwEnumErr)
  2516. goto EnumContainersError;
  2517. }
  2518. if (0 == cchMaxContainerName)
  2519. goto PublicKeyContainerNotFound;
  2520. if (NULL == (pszContainerName = (LPSTR) PkiNonzeroAlloc(
  2521. cchMaxContainerName + 1)))
  2522. goto OutOfMemory;
  2523. dwEnumFlags = CRYPT_FIRST;
  2524. while (TRUE) {
  2525. HCRYPTPROV hContainerProv = 0;
  2526. LPWSTR pwszAcquireProvName = pwszProvName;
  2527. cchContainerName = cchMaxContainerName;
  2528. if (!CryptGetProvParam(
  2529. hProv,
  2530. PP_ENUMCONTAINERS,
  2531. (BYTE *) pszContainerName,
  2532. &cchContainerName,
  2533. dwEnumFlags
  2534. )) {
  2535. dwEnumErr = GetLastError();
  2536. if (ERROR_NO_MORE_ITEMS == dwEnumErr ||
  2537. ERROR_FILE_NOT_FOUND == dwEnumErr) {
  2538. if (0 != dwAcquireErr)
  2539. goto CryptAcquireContextError;
  2540. else
  2541. goto PublicKeyContainerNotFound;
  2542. } else
  2543. goto EnumContainersError;
  2544. }
  2545. dwEnumFlags = 0; // CRYPT_NEXT
  2546. if (NULL == (pwszContainerName = MkWStr(pszContainerName)))
  2547. goto OutOfMemory;
  2548. // First try using enhanced providers for the base guys
  2549. if (PROV_RSA_FULL == dwProvType &&
  2550. 0 == _wcsicmp(pwszProvName, MS_DEF_PROV_W)) {
  2551. fResult = CryptAcquireContextU(
  2552. &hContainerProv,
  2553. pwszContainerName,
  2554. MS_ENHANCED_PROV_W,
  2555. PROV_RSA_FULL,
  2556. dwProvFlags
  2557. );
  2558. if (fResult)
  2559. pwszAcquireProvName = MS_ENHANCED_PROV_W;
  2560. } else if (PROV_DSS_DH == dwProvType &&
  2561. 0 == _wcsicmp(pwszProvName, MS_DEF_DSS_DH_PROV_W)) {
  2562. fResult = CryptAcquireContextU(
  2563. &hContainerProv,
  2564. pwszContainerName,
  2565. MS_ENH_DSS_DH_PROV_W,
  2566. PROV_DSS_DH,
  2567. dwProvFlags
  2568. );
  2569. if (fResult)
  2570. pwszAcquireProvName = MS_ENH_DSS_DH_PROV_W;
  2571. } else
  2572. fResult = FALSE;
  2573. if (!fResult)
  2574. fResult = CryptAcquireContextU(
  2575. &hContainerProv,
  2576. pwszContainerName,
  2577. pwszAcquireProvName,
  2578. dwProvType,
  2579. dwProvFlags
  2580. );
  2581. if (!fResult)
  2582. dwAcquireErr = GetLastError();
  2583. else {
  2584. DWORD dwKeySpec;
  2585. dwKeySpec = AT_KEYEXCHANGE;
  2586. fResult = FALSE;
  2587. while (TRUE) {
  2588. if (I_CertCompareCertAndProviderPublicKey(
  2589. pCert,
  2590. hContainerProv,
  2591. dwKeySpec
  2592. )) {
  2593. fResult = TRUE;
  2594. break;
  2595. } else if (AT_SIGNATURE == dwKeySpec)
  2596. break;
  2597. else
  2598. dwKeySpec = AT_SIGNATURE;
  2599. }
  2600. CryptReleaseContext(hContainerProv, 0);
  2601. if (fResult) {
  2602. CRYPT_KEY_PROV_INFO KeyProvInfo;
  2603. memset(&KeyProvInfo, 0, sizeof(KeyProvInfo));
  2604. KeyProvInfo.pwszContainerName = pwszContainerName;
  2605. KeyProvInfo.pwszProvName = pwszAcquireProvName;
  2606. KeyProvInfo.dwProvType = dwProvType;
  2607. KeyProvInfo.dwFlags = dwProvFlags & ~CRYPT_SILENT;
  2608. KeyProvInfo.dwKeySpec = dwKeySpec;
  2609. if (!CertSetCertificateContextProperty(
  2610. pCert,
  2611. CERT_KEY_PROV_INFO_PROP_ID,
  2612. 0, // dwFlags
  2613. &KeyProvInfo
  2614. ))
  2615. goto SetKeyProvInfoPropertyError;
  2616. else
  2617. goto SuccessReturn;
  2618. }
  2619. }
  2620. FreeWStr(pwszContainerName);
  2621. pwszContainerName = NULL;
  2622. }
  2623. goto UnexpectedError;
  2624. SuccessReturn:
  2625. fResult = TRUE;
  2626. CommonReturn:
  2627. PkiFree(pszContainerName);
  2628. FreeWStr(pwszContainerName);
  2629. return fResult;
  2630. ErrorReturn:
  2631. fResult = FALSE;
  2632. goto CommonReturn;
  2633. SET_ERROR_VAR(EnumContainersError, dwEnumErr)
  2634. TRACE_ERROR(OutOfMemory)
  2635. SET_ERROR_VAR(CryptAcquireContextError, dwAcquireErr)
  2636. SET_ERROR(PublicKeyContainerNotFound, ERROR_NO_MORE_ITEMS)
  2637. TRACE_ERROR(SetKeyProvInfoPropertyError)
  2638. SET_ERROR(UnexpectedError, E_UNEXPECTED)
  2639. }
  2640. //+-------------------------------------------------------------------------
  2641. // Enumerates the cryptographic providers and their containers to find the
  2642. // private key corresponding to the certificate's public key. For a match,
  2643. // the certificate's CERT_KEY_PROV_INFO_PROP_ID property is updated.
  2644. //
  2645. // If the CERT_KEY_PROV_INFO_PROP_ID is already set, then, its checked to
  2646. // see if it matches the provider's public key. For a match, the above
  2647. // enumeration is skipped.
  2648. //
  2649. // By default both the user and machine key containers are searched.
  2650. // The CRYPT_FIND_USER_KEYSET_FLAG or CRYPT_FIND_MACHINE_KEYSET_FLAG
  2651. // can be set in dwFlags to restrict the search to either of the containers.
  2652. //
  2653. // The CRYPT_FIND_SILENT_KEYSET_FLAG can be set to suppress any UI by the CSP.
  2654. // See CryptAcquireContext's CRYPT_SILENT flag for more details.
  2655. //
  2656. // If a container isn't found, returns FALSE with LastError set to
  2657. // NTE_NO_KEY.
  2658. //--------------------------------------------------------------------------
  2659. BOOL
  2660. WINAPI
  2661. CryptFindCertificateKeyProvInfo(
  2662. IN PCCERT_CONTEXT pCert,
  2663. IN DWORD dwFlags,
  2664. IN void *pvReserved
  2665. )
  2666. {
  2667. BOOL fResult;
  2668. DWORD dwFindContainerErr = ERROR_NO_MORE_ITEMS;
  2669. DWORD dwAcquireErr = 0;
  2670. DWORD dwProvIndex;
  2671. PCCRYPT_OID_INFO pOIDInfo;
  2672. ALG_ID aiPubKey;
  2673. if (0 == (dwFlags &
  2674. (CRYPT_FIND_USER_KEYSET_FLAG | CRYPT_FIND_MACHINE_KEYSET_FLAG)))
  2675. dwFlags |=
  2676. CRYPT_FIND_USER_KEYSET_FLAG | CRYPT_FIND_MACHINE_KEYSET_FLAG;
  2677. if (HasValidKeyProvInfo(pCert, dwFlags))
  2678. return TRUE;
  2679. if (pOIDInfo = CryptFindOIDInfo(
  2680. CRYPT_OID_INFO_OID_KEY,
  2681. pCert->pCertInfo->SubjectPublicKeyInfo.Algorithm.pszObjId,
  2682. CRYPT_PUBKEY_ALG_OID_GROUP_ID
  2683. ))
  2684. aiPubKey = pOIDInfo->Algid;
  2685. else
  2686. aiPubKey = 0;
  2687. for (dwProvIndex = 0; TRUE; dwProvIndex++) {
  2688. LPWSTR pwszProvName;
  2689. DWORD cbProvName;
  2690. HCRYPTPROV hProv;
  2691. DWORD dwProvType;
  2692. cbProvName = 0;
  2693. dwProvType = 0;
  2694. if (!CryptEnumProvidersU(
  2695. dwProvIndex,
  2696. NULL, // pdwReserved
  2697. 0, // dwFlags
  2698. &dwProvType,
  2699. NULL, // pwszProvName,
  2700. &cbProvName
  2701. ) || 0 == cbProvName) {
  2702. if (0 == dwProvIndex)
  2703. goto EnumProvidersError;
  2704. else if (ERROR_NO_MORE_ITEMS != dwFindContainerErr)
  2705. goto FindContainerError;
  2706. else if (0 != dwAcquireErr)
  2707. goto CryptAcquireContextError;
  2708. else
  2709. goto KeyContainerNotFound;
  2710. }
  2711. if (NULL == (pwszProvName = (LPWSTR) PkiNonzeroAlloc(
  2712. (cbProvName + 1) * sizeof(WCHAR))))
  2713. goto OutOfMemory;
  2714. if (!CryptEnumProvidersU(
  2715. dwProvIndex,
  2716. NULL, // pdwReserved
  2717. 0, // dwFlags
  2718. &dwProvType,
  2719. pwszProvName,
  2720. &cbProvName
  2721. )) {
  2722. PkiFree(pwszProvName);
  2723. goto EnumProvidersError;
  2724. }
  2725. fResult = FALSE;
  2726. if (!CryptAcquireContextU(
  2727. &hProv,
  2728. NULL, // pwszContainerName,
  2729. pwszProvName,
  2730. dwProvType,
  2731. CRYPT_VERIFYCONTEXT // dwFlags
  2732. )) {
  2733. dwAcquireErr = GetLastError();
  2734. hProv = 0; // CAPI bug, sets hCryptProv to nonzero
  2735. } else if (IsPublicKeyAlgidSupported(
  2736. pCert,
  2737. hProv,
  2738. aiPubKey
  2739. )) {
  2740. DWORD dwSetProvFlags;
  2741. if (dwFlags & CRYPT_FIND_SILENT_KEYSET_FLAG)
  2742. dwSetProvFlags = CRYPT_SILENT;
  2743. else
  2744. dwSetProvFlags = 0;
  2745. if (dwFlags & CRYPT_FIND_USER_KEYSET_FLAG) {
  2746. if (FindContainerAndSetKeyProvInfo(
  2747. pCert,
  2748. hProv,
  2749. pwszProvName,
  2750. dwProvType,
  2751. dwSetProvFlags
  2752. ))
  2753. fResult = TRUE;
  2754. else if (ERROR_NO_MORE_ITEMS == dwFindContainerErr)
  2755. dwFindContainerErr = GetLastError();
  2756. }
  2757. if (!fResult && (dwFlags & CRYPT_FIND_MACHINE_KEYSET_FLAG)) {
  2758. CryptReleaseContext(hProv, 0);
  2759. if (!CryptAcquireContextU(
  2760. &hProv,
  2761. NULL, // pwszContainerName,
  2762. pwszProvName,
  2763. dwProvType,
  2764. CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET // dwFlags
  2765. )) {
  2766. dwAcquireErr = GetLastError();
  2767. hProv = 0; // CAPI bug, sets hCryptProv to nonzero
  2768. } else {
  2769. if (FindContainerAndSetKeyProvInfo(
  2770. pCert,
  2771. hProv,
  2772. pwszProvName,
  2773. dwProvType,
  2774. dwSetProvFlags | CRYPT_MACHINE_KEYSET
  2775. ))
  2776. fResult = TRUE;
  2777. else if (ERROR_NO_MORE_ITEMS == dwFindContainerErr)
  2778. dwFindContainerErr = GetLastError();
  2779. }
  2780. }
  2781. }
  2782. if (hProv)
  2783. CryptReleaseContext(hProv, 0);
  2784. PkiFree(pwszProvName);
  2785. if (fResult)
  2786. goto CommonReturn;
  2787. }
  2788. goto UnexpectedError;
  2789. CommonReturn:
  2790. return fResult;
  2791. ErrorReturn:
  2792. fResult = FALSE;
  2793. goto CommonReturn;
  2794. TRACE_ERROR(EnumProvidersError)
  2795. SET_ERROR(KeyContainerNotFound, NTE_NO_KEY)
  2796. SET_ERROR_VAR(FindContainerError, dwFindContainerErr)
  2797. SET_ERROR_VAR(CryptAcquireContextError, dwAcquireErr)
  2798. TRACE_ERROR(OutOfMemory)
  2799. SET_ERROR(UnexpectedError, E_UNEXPECTED)
  2800. }
  2801. //+=========================================================================
  2802. // CryptCreatePublicKeyInfo, EncodePublicKeyAndParameters
  2803. // and CryptConvertPublicKeyInfo functions
  2804. //-=========================================================================
  2805. static BOOL EncodePublicKeyInfo(
  2806. IN LPCSTR pszPubKeyOID,
  2807. IN BYTE *pbEncodedPubKey,
  2808. IN DWORD cbEncodedPubKey,
  2809. IN BYTE *pbEncodedParameters,
  2810. IN DWORD cbEncodedParameters,
  2811. OUT PCERT_PUBLIC_KEY_INFO pInfo,
  2812. IN OUT DWORD *pcbInfo
  2813. )
  2814. {
  2815. BOOL fResult;
  2816. BYTE *pbExtra;
  2817. LONG lRemainExtra;
  2818. DWORD cbOID;
  2819. if (pInfo == NULL)
  2820. *pcbInfo = 0;
  2821. // for lRemainExtra < 0, LENGTH_ONLY calculation
  2822. lRemainExtra = (LONG) *pcbInfo - sizeof(CERT_PUBLIC_KEY_INFO);
  2823. if (lRemainExtra < 0)
  2824. pbExtra = NULL;
  2825. else
  2826. pbExtra = (BYTE *) pInfo + sizeof(CERT_PUBLIC_KEY_INFO);
  2827. cbOID = strlen(pszPubKeyOID) + 1;
  2828. lRemainExtra -= INFO_LEN_ALIGN(cbOID) +
  2829. INFO_LEN_ALIGN(cbEncodedParameters) + cbEncodedPubKey;
  2830. if (lRemainExtra >= 0) {
  2831. memset(pInfo, 0, sizeof(CERT_PUBLIC_KEY_INFO));
  2832. pInfo->Algorithm.pszObjId = (LPSTR) pbExtra;
  2833. memcpy(pbExtra, pszPubKeyOID, cbOID);
  2834. pbExtra += INFO_LEN_ALIGN(cbOID);
  2835. if (cbEncodedParameters) {
  2836. pInfo->Algorithm.Parameters.cbData = cbEncodedParameters;
  2837. pInfo->Algorithm.Parameters.pbData = pbExtra;
  2838. memcpy(pbExtra, pbEncodedParameters, cbEncodedParameters);
  2839. pbExtra += INFO_LEN_ALIGN(cbEncodedParameters);
  2840. }
  2841. pInfo->PublicKey.pbData = pbExtra;
  2842. pInfo->PublicKey.cbData = cbEncodedPubKey;
  2843. memcpy(pbExtra, pbEncodedPubKey, cbEncodedPubKey);
  2844. *pcbInfo = *pcbInfo - (DWORD) lRemainExtra;
  2845. } else {
  2846. *pcbInfo = *pcbInfo + (DWORD) -lRemainExtra;
  2847. if (pInfo) goto LengthError;
  2848. }
  2849. fResult = TRUE;
  2850. CommonReturn:
  2851. return fResult;
  2852. LengthError:
  2853. SetLastError((DWORD) ERROR_MORE_DATA);
  2854. fResult = FALSE;
  2855. goto CommonReturn;
  2856. }
  2857. // By default, the pPubKeyStruc->aiKeyAlg is used to find the appropriate
  2858. // public key Object Identifier. pszPubKeyOID can be set to override
  2859. // the default OID obtained from the aiKeyAlg.
  2860. BOOL
  2861. WINAPI
  2862. CryptCreatePublicKeyInfo(
  2863. IN DWORD dwCertEncodingType,
  2864. IN OPTIONAL LPCSTR pszPubKeyOID,
  2865. IN const PUBLICKEYSTRUC *pPubKeyStruc,
  2866. IN DWORD cbPubKeyStruc,
  2867. IN DWORD dwFlags,
  2868. IN OPTIONAL void *pvReserved,
  2869. OUT void *pvPubKeyInfo,
  2870. IN OUT DWORD *pcbPubKeyInfo
  2871. )
  2872. {
  2873. BOOL fResult;
  2874. void *pvFuncAddr;
  2875. HCRYPTOIDFUNCADDR hFuncAddr;
  2876. LPCSTR pszEncodePubKeyOID;
  2877. BYTE *pbEncodedPubKey = NULL;
  2878. DWORD cbEncodedPubKey = 0;
  2879. BYTE *pbEncodedParameters = NULL;
  2880. DWORD cbEncodedParameters = 0;
  2881. PCERT_PUBLIC_KEY_INFO pPubKeyInfo = NULL;
  2882. DWORD cbPubKeyInfo;
  2883. if (NULL == pszPubKeyOID) {
  2884. PCCRYPT_OID_INFO pInfo;
  2885. if (NULL == (pInfo = CryptFindOIDInfo(
  2886. CRYPT_OID_INFO_ALGID_KEY,
  2887. (void *) &pPubKeyStruc->aiKeyAlg,
  2888. CRYPT_PUBKEY_ALG_OID_GROUP_ID
  2889. )))
  2890. goto NoPubKeyOIDInfo;
  2891. pszEncodePubKeyOID = pInfo->pszOID;
  2892. } else
  2893. pszEncodePubKeyOID = pszPubKeyOID;
  2894. if (!CryptGetOIDFunctionAddress(
  2895. hEncodePubKeyFuncSet,
  2896. dwCertEncodingType,
  2897. pszEncodePubKeyOID,
  2898. 0, // dwFlags
  2899. &pvFuncAddr,
  2900. &hFuncAddr)) {
  2901. PCCRYPT_OID_INFO pInfo;
  2902. if (NULL == pszPubKeyOID)
  2903. goto NoEncodePubKeyFunction;
  2904. if (NULL == (pInfo = CryptFindOIDInfo(
  2905. CRYPT_OID_INFO_ALGID_KEY,
  2906. (void *) &pPubKeyStruc->aiKeyAlg,
  2907. CRYPT_PUBKEY_ALG_OID_GROUP_ID
  2908. )))
  2909. goto NoPubKeyOIDInfo;
  2910. pszEncodePubKeyOID = pInfo->pszOID;
  2911. if (!CryptGetOIDFunctionAddress(
  2912. hEncodePubKeyFuncSet,
  2913. dwCertEncodingType,
  2914. pszEncodePubKeyOID,
  2915. 0, // dwFlags
  2916. &pvFuncAddr,
  2917. &hFuncAddr))
  2918. goto NoEncodePubKeyFunction;
  2919. }
  2920. if (NULL == pszPubKeyOID)
  2921. pszPubKeyOID = pszEncodePubKeyOID;
  2922. fResult = ((PFN_CRYPT_ENCODE_PUBLIC_KEY_AND_PARAMETERS) pvFuncAddr)(
  2923. dwCertEncodingType,
  2924. pszPubKeyOID,
  2925. pPubKeyStruc,
  2926. cbPubKeyStruc,
  2927. dwFlags,
  2928. pvReserved,
  2929. &pbEncodedPubKey,
  2930. &cbEncodedPubKey,
  2931. &pbEncodedParameters,
  2932. &cbEncodedParameters
  2933. );
  2934. CryptFreeOIDFunctionAddress(hFuncAddr, 0);
  2935. if (!fResult)
  2936. goto EncodePubKeyAndParametersError;
  2937. if (dwFlags & CRYPT_ALLOC_FLAG) {
  2938. if (!EncodePublicKeyInfo(
  2939. pszPubKeyOID,
  2940. pbEncodedPubKey,
  2941. cbEncodedPubKey,
  2942. pbEncodedParameters,
  2943. cbEncodedParameters,
  2944. NULL, // pPubKeyInfo
  2945. &cbPubKeyInfo
  2946. ))
  2947. goto EncodePublicKeyInfoError;
  2948. if (NULL == (pPubKeyInfo =
  2949. (PCERT_PUBLIC_KEY_INFO) PkiDefaultCryptAlloc(cbPubKeyInfo)))
  2950. goto OutOfMemory;
  2951. *((PCERT_PUBLIC_KEY_INFO *) pvPubKeyInfo) = pPubKeyInfo;
  2952. } else {
  2953. pPubKeyInfo = (PCERT_PUBLIC_KEY_INFO) pvPubKeyInfo;
  2954. cbPubKeyInfo = *pcbPubKeyInfo;
  2955. }
  2956. fResult = EncodePublicKeyInfo(
  2957. pszPubKeyOID,
  2958. pbEncodedPubKey,
  2959. cbEncodedPubKey,
  2960. pbEncodedParameters,
  2961. cbEncodedParameters,
  2962. pPubKeyInfo,
  2963. &cbPubKeyInfo
  2964. );
  2965. if (!fResult && (dwFlags & CRYPT_ALLOC_FLAG))
  2966. goto ErrorReturn;
  2967. CommonReturn:
  2968. PkiDefaultCryptFree(pbEncodedPubKey);
  2969. PkiDefaultCryptFree(pbEncodedParameters);
  2970. *pcbPubKeyInfo = cbPubKeyInfo;
  2971. return fResult;
  2972. ErrorReturn:
  2973. if (dwFlags & CRYPT_ALLOC_FLAG) {
  2974. PkiDefaultCryptFree(pPubKeyInfo);
  2975. *((void **) pvPubKeyInfo) = NULL;
  2976. }
  2977. cbPubKeyInfo = 0;
  2978. fResult = FALSE;
  2979. goto CommonReturn;
  2980. SET_ERROR(NoPubKeyOIDInfo, ERROR_FILE_NOT_FOUND)
  2981. TRACE_ERROR(NoEncodePubKeyFunction)
  2982. TRACE_ERROR(EncodePubKeyAndParametersError)
  2983. TRACE_ERROR(EncodePublicKeyInfoError)
  2984. TRACE_ERROR(OutOfMemory)
  2985. }
  2986. BOOL
  2987. WINAPI
  2988. CryptConvertPublicKeyInfo(
  2989. IN DWORD dwCertEncodingType,
  2990. IN PCERT_PUBLIC_KEY_INFO pPubKeyInfo,
  2991. IN DWORD dwFlags,
  2992. IN OPTIONAL void *pvReserved,
  2993. OUT void *pvPubKeyStruc,
  2994. IN OUT DWORD *pcbPubKeyStruc
  2995. )
  2996. {
  2997. BOOL fResult;
  2998. void *pvFuncAddr;
  2999. HCRYPTOIDFUNCADDR hFuncAddr;
  3000. if (CryptGetOIDFunctionAddress(
  3001. hConvertPubKeyFuncSet,
  3002. dwCertEncodingType,
  3003. pPubKeyInfo->Algorithm.pszObjId,
  3004. 0, // dwFlags
  3005. &pvFuncAddr,
  3006. &hFuncAddr)) {
  3007. fResult = ((PFN_CRYPT_CONVERT_PUBLIC_KEY_INFO) pvFuncAddr)(
  3008. dwCertEncodingType,
  3009. pPubKeyInfo,
  3010. dwFlags,
  3011. pvReserved,
  3012. pvPubKeyStruc,
  3013. pcbPubKeyStruc
  3014. );
  3015. CryptFreeOIDFunctionAddress(hFuncAddr, 0);
  3016. } else {
  3017. ALG_ID aiPubKey;
  3018. PCCRYPT_OID_INFO pOIDInfo;
  3019. if (pOIDInfo = CryptFindOIDInfo(
  3020. CRYPT_OID_INFO_OID_KEY,
  3021. pPubKeyInfo->Algorithm.pszObjId,
  3022. CRYPT_PUBKEY_ALG_OID_GROUP_ID
  3023. ))
  3024. aiPubKey = pOIDInfo->Algid;
  3025. else
  3026. aiPubKey = 0;
  3027. switch (aiPubKey) {
  3028. case CALG_DSS_SIGN:
  3029. fResult = ConvertDSSPublicKeyInfo(
  3030. dwCertEncodingType,
  3031. pPubKeyInfo,
  3032. dwFlags,
  3033. pvReserved,
  3034. pvPubKeyStruc,
  3035. pcbPubKeyStruc
  3036. );
  3037. break;
  3038. default:
  3039. // Attempt to decode as a PKCS #1 RSA public key
  3040. fResult = ConvertRSAPublicKeyInfo(
  3041. dwCertEncodingType,
  3042. pPubKeyInfo,
  3043. dwFlags,
  3044. pvReserved,
  3045. pvPubKeyStruc,
  3046. pcbPubKeyStruc
  3047. );
  3048. break;
  3049. }
  3050. }
  3051. return fResult;
  3052. }
  3053. //+-------------------------------------------------------------------------
  3054. // Encode the RSA public key and parameters
  3055. //--------------------------------------------------------------------------
  3056. static BOOL WINAPI EncodeRSAPublicKeyAndParameters(
  3057. IN DWORD dwCertEncodingType,
  3058. IN OPTIONAL LPCSTR pszPubKeyOID,
  3059. IN const PUBLICKEYSTRUC *pPubKeyStruc,
  3060. IN DWORD cbPubKeyStruc,
  3061. IN DWORD dwFlags,
  3062. IN OPTIONAL void *pvReserved,
  3063. OUT BYTE **ppbEncodedPubKey,
  3064. OUT DWORD *pcbEncodedPubKey,
  3065. OUT BYTE **ppbEncodedParameters,
  3066. OUT DWORD *pcbEncodedParameters
  3067. )
  3068. {
  3069. *ppbEncodedParameters = NULL;
  3070. *pcbEncodedParameters = 0;
  3071. return CryptEncodeObjectEx(
  3072. dwCertEncodingType,
  3073. RSA_CSP_PUBLICKEYBLOB,
  3074. pPubKeyStruc,
  3075. CRYPT_ENCODE_ALLOC_FLAG,
  3076. NULL, // pEncodePara
  3077. (void *) ppbEncodedPubKey,
  3078. pcbEncodedPubKey
  3079. );
  3080. }
  3081. //+-------------------------------------------------------------------------
  3082. // Convert as an RSA public key
  3083. //--------------------------------------------------------------------------
  3084. static BOOL WINAPI ConvertRSAPublicKeyInfo(
  3085. IN DWORD dwCertEncodingType,
  3086. IN PCERT_PUBLIC_KEY_INFO pPubKeyInfo,
  3087. IN DWORD dwFlags,
  3088. IN OPTIONAL void *pvReserved,
  3089. OUT void *pvPubKeyStruc,
  3090. IN OUT DWORD *pcbPubKeyStruc
  3091. )
  3092. {
  3093. return CryptDecodeObjectEx(
  3094. dwCertEncodingType,
  3095. RSA_CSP_PUBLICKEYBLOB,
  3096. pPubKeyInfo->PublicKey.pbData,
  3097. pPubKeyInfo->PublicKey.cbData,
  3098. (dwFlags & CRYPT_ALLOC_FLAG) ? CRYPT_DECODE_ALLOC_FLAG : 0,
  3099. NULL, // pDecodePara,
  3100. pvPubKeyStruc,
  3101. pcbPubKeyStruc
  3102. );
  3103. }
  3104. #ifndef DSS1
  3105. #define DSS1 ((DWORD)'D'+((DWORD)'S'<<8)+((DWORD)'S'<<16)+((DWORD)'1'<<24))
  3106. #endif
  3107. #define DSS_Q_LEN 20
  3108. //+-------------------------------------------------------------------------
  3109. // Encode the DSS public key and parameters
  3110. //--------------------------------------------------------------------------
  3111. static BOOL WINAPI EncodeDSSPublicKeyAndParameters(
  3112. IN DWORD dwCertEncodingType,
  3113. IN OPTIONAL LPCSTR pszPubKeyOID,
  3114. IN const PUBLICKEYSTRUC *pPubKeyStruc,
  3115. IN DWORD cbPubKeyStruc,
  3116. IN DWORD dwFlags,
  3117. IN OPTIONAL void *pvReserved,
  3118. OUT BYTE **ppbEncodedPubKey,
  3119. OUT DWORD *pcbEncodedPubKey,
  3120. OUT BYTE **ppbEncodedParameters,
  3121. OUT DWORD *pcbEncodedParameters
  3122. )
  3123. {
  3124. BOOL fResult;
  3125. BYTE *pbKeyBlob;
  3126. DSSPUBKEY *pCspPubKey;
  3127. DWORD cbKey;
  3128. BYTE *pbKey;
  3129. CERT_DSS_PARAMETERS DssParameters;
  3130. CRYPT_UINT_BLOB DssPubKey;
  3131. *ppbEncodedPubKey = NULL;
  3132. *ppbEncodedParameters = NULL;
  3133. // The CAPI public key representation consists of the following sequence:
  3134. // - PUBLICKEYSTRUC
  3135. // - DSSPUBKEY
  3136. // - rgbP[cbKey]
  3137. // - rgbQ[20]
  3138. // - rgbG[cbKey]
  3139. // - rgbY[cbKey]
  3140. // - DSSSEED
  3141. pbKeyBlob = (BYTE *) pPubKeyStruc;
  3142. pCspPubKey = (DSSPUBKEY *) (pbKeyBlob + sizeof(PUBLICKEYSTRUC));
  3143. pbKey = pbKeyBlob + sizeof(PUBLICKEYSTRUC) + sizeof(DSSPUBKEY);
  3144. cbKey = pCspPubKey->bitlen / 8;
  3145. assert(cbKey > 0);
  3146. assert(cbPubKeyStruc >= sizeof(PUBLICKEYSTRUC) + sizeof(DSSPUBKEY) +
  3147. cbKey + DSS_Q_LEN + cbKey + cbKey + sizeof(DSSSEED));
  3148. assert(pPubKeyStruc->bType == PUBLICKEYBLOB);
  3149. assert(pPubKeyStruc->bVersion == CUR_BLOB_VERSION);
  3150. assert(pPubKeyStruc->aiKeyAlg == CALG_DSS_SIGN);
  3151. assert(pCspPubKey->magic == DSS1);
  3152. assert(pCspPubKey->bitlen % 8 == 0);
  3153. if (pPubKeyStruc->bType != PUBLICKEYBLOB)
  3154. goto InvalidArg;
  3155. // Initialize DSS parameters from CSP data structure
  3156. DssParameters.p.cbData = cbKey;
  3157. DssParameters.p.pbData = pbKey;
  3158. pbKey += cbKey;
  3159. DssParameters.q.cbData = DSS_Q_LEN;
  3160. DssParameters.q.pbData = pbKey;
  3161. pbKey += DSS_Q_LEN;
  3162. DssParameters.g.cbData = cbKey;
  3163. DssParameters.g.pbData = pbKey;
  3164. pbKey += cbKey;
  3165. // Initialize DSS public key from CSP data structure
  3166. DssPubKey.cbData = cbKey;
  3167. DssPubKey.pbData = pbKey;
  3168. // Encode the parameters and public key
  3169. if (!CryptEncodeObjectEx(
  3170. dwCertEncodingType,
  3171. X509_DSS_PARAMETERS,
  3172. &DssParameters,
  3173. CRYPT_ENCODE_ALLOC_FLAG,
  3174. NULL, // pEncodePara
  3175. (void *) ppbEncodedParameters,
  3176. pcbEncodedParameters
  3177. )) goto ErrorReturn;
  3178. if (!CryptEncodeObjectEx(
  3179. dwCertEncodingType,
  3180. X509_DSS_PUBLICKEY,
  3181. &DssPubKey,
  3182. CRYPT_ENCODE_ALLOC_FLAG,
  3183. NULL, // pEncodePara
  3184. (void *) ppbEncodedPubKey,
  3185. pcbEncodedPubKey
  3186. )) goto ErrorReturn;
  3187. fResult = TRUE;
  3188. CommonReturn:
  3189. return fResult;
  3190. ErrorReturn:
  3191. PkiDefaultCryptFree(*ppbEncodedParameters);
  3192. PkiDefaultCryptFree(*ppbEncodedPubKey);
  3193. *ppbEncodedParameters = NULL;
  3194. *ppbEncodedPubKey = NULL;
  3195. *pcbEncodedParameters = 0;
  3196. *pcbEncodedPubKey = 0;
  3197. fResult = FALSE;
  3198. goto CommonReturn;
  3199. SET_ERROR(InvalidArg, E_INVALIDARG)
  3200. }
  3201. //+-------------------------------------------------------------------------
  3202. // Convert as an DSS public key
  3203. //--------------------------------------------------------------------------
  3204. static BOOL WINAPI ConvertDSSPublicKeyInfo(
  3205. IN DWORD dwCertEncodingType,
  3206. IN PCERT_PUBLIC_KEY_INFO pPubKeyInfo,
  3207. IN DWORD dwFlags,
  3208. IN OPTIONAL void *pvReserved,
  3209. OUT void *pvPubKeyStruc,
  3210. IN OUT DWORD *pcbPubKeyStruc
  3211. )
  3212. {
  3213. BOOL fResult;
  3214. PCERT_DSS_PARAMETERS pDssParameters = NULL;
  3215. PCRYPT_UINT_BLOB pDssPubKey = NULL;
  3216. PUBLICKEYSTRUC *pPubKeyStruc = NULL;
  3217. DWORD cbPubKeyStruc;
  3218. BYTE *pbKeyBlob;
  3219. DSSPUBKEY *pCspPubKey;
  3220. DSSSEED *pCspSeed;
  3221. DWORD cbKey;
  3222. BYTE *pbKey;
  3223. DWORD cb;
  3224. if (0 == pPubKeyInfo->Algorithm.Parameters.cbData ||
  3225. NULL_ASN_TAG == *pPubKeyInfo->Algorithm.Parameters.pbData)
  3226. goto NoDssParametersError;
  3227. if (NULL == (pDssParameters = (PCERT_DSS_PARAMETERS) AllocAndDecodeObject(
  3228. dwCertEncodingType,
  3229. X509_DSS_PARAMETERS,
  3230. pPubKeyInfo->Algorithm.Parameters.pbData,
  3231. pPubKeyInfo->Algorithm.Parameters.cbData
  3232. ))) goto DecodeParametersError;
  3233. if (NULL == (pDssPubKey = (PCRYPT_UINT_BLOB) AllocAndDecodeObject(
  3234. dwCertEncodingType,
  3235. X509_DSS_PUBLICKEY,
  3236. pPubKeyInfo->PublicKey.pbData,
  3237. pPubKeyInfo->PublicKey.cbData
  3238. ))) goto DecodePubKeyError;
  3239. // The CAPI public key representation consists of the following sequence:
  3240. // - PUBLICKEYSTRUC
  3241. // - DSSPUBKEY
  3242. // - rgbP[cbKey]
  3243. // - rgbQ[20]
  3244. // - rgbG[cbKey]
  3245. // - rgbY[cbKey]
  3246. // - DSSSEED
  3247. cbKey = pDssParameters->p.cbData;
  3248. if (0 == cbKey)
  3249. goto InvalidDssParametersError;
  3250. cbPubKeyStruc = sizeof(PUBLICKEYSTRUC) + sizeof(DSSPUBKEY) +
  3251. cbKey + DSS_Q_LEN + cbKey + cbKey + sizeof(DSSSEED);
  3252. if (dwFlags & CRYPT_ALLOC_FLAG) {
  3253. if (NULL == (pPubKeyStruc =
  3254. (PUBLICKEYSTRUC *) PkiDefaultCryptAlloc(cbPubKeyStruc)))
  3255. goto OutOfMemory;
  3256. *((PUBLICKEYSTRUC **) pvPubKeyStruc) = pPubKeyStruc;
  3257. } else
  3258. pPubKeyStruc = (PUBLICKEYSTRUC *) pvPubKeyStruc;
  3259. fResult = TRUE;
  3260. if (pPubKeyStruc) {
  3261. if (0 == (dwFlags & CRYPT_ALLOC_FLAG) &&
  3262. *pcbPubKeyStruc < cbPubKeyStruc) {
  3263. SetLastError((DWORD) ERROR_MORE_DATA);
  3264. fResult = FALSE;
  3265. } else {
  3266. pbKeyBlob = (BYTE *) pPubKeyStruc;
  3267. pCspPubKey = (DSSPUBKEY *) (pbKeyBlob + sizeof(PUBLICKEYSTRUC));
  3268. pbKey = pbKeyBlob + sizeof(PUBLICKEYSTRUC) + sizeof(DSSPUBKEY);
  3269. // NOTE, the length of G and Y can be less than the length of P.
  3270. // The CSP requires G and Y to be padded out with 0x00 bytes if it
  3271. // is less and in little endian form
  3272. // PUBLICKEYSTRUC
  3273. pPubKeyStruc->bType = PUBLICKEYBLOB;
  3274. pPubKeyStruc->bVersion = CUR_BLOB_VERSION;
  3275. pPubKeyStruc->reserved = 0;
  3276. pPubKeyStruc->aiKeyAlg = CALG_DSS_SIGN;
  3277. // DSSPUBKEY
  3278. pCspPubKey->magic = DSS1;
  3279. pCspPubKey->bitlen = cbKey * 8;
  3280. // rgbP[cbKey]
  3281. memcpy(pbKey, pDssParameters->p.pbData, cbKey);
  3282. pbKey += cbKey;
  3283. // rgbQ[20]
  3284. cb = pDssParameters->q.cbData;
  3285. if (0 == cb || cb > DSS_Q_LEN)
  3286. goto InvalidDssParametersError;
  3287. memcpy(pbKey, pDssParameters->q.pbData, cb);
  3288. if (DSS_Q_LEN > cb)
  3289. memset(pbKey + cb, 0, DSS_Q_LEN - cb);
  3290. pbKey += DSS_Q_LEN;
  3291. // rgbG[cbKey]
  3292. cb = pDssParameters->g.cbData;
  3293. if (0 == cb || cb > cbKey)
  3294. goto InvalidDssParametersError;
  3295. memcpy(pbKey, pDssParameters->g.pbData, cb);
  3296. if (cbKey > cb)
  3297. memset(pbKey + cb, 0, cbKey - cb);
  3298. pbKey += cbKey;
  3299. // rgbY[cbKey]
  3300. cb = pDssPubKey->cbData;
  3301. if (0 == cb || cb > cbKey)
  3302. goto InvalidDssPubKeyError;
  3303. memcpy(pbKey, pDssPubKey->pbData, cb);
  3304. if (cbKey > cb)
  3305. memset(pbKey + cb, 0, cbKey - cb);
  3306. pbKey += cbKey;
  3307. // DSSSEED: set counter to 0xFFFFFFFF to indicate not available
  3308. pCspSeed = (DSSSEED *) pbKey;
  3309. memset(&pCspSeed->counter, 0xFF, sizeof(pCspSeed->counter));
  3310. }
  3311. }
  3312. CommonReturn:
  3313. *pcbPubKeyStruc = cbPubKeyStruc;
  3314. PkiFree(pDssParameters);
  3315. PkiFree(pDssPubKey);
  3316. return fResult;
  3317. ErrorReturn:
  3318. if (dwFlags & CRYPT_ALLOC_FLAG) {
  3319. PkiDefaultCryptFree(pPubKeyStruc);
  3320. *((PUBLICKEYSTRUC **) pvPubKeyStruc) = NULL;
  3321. }
  3322. cbPubKeyStruc = 0;
  3323. fResult = FALSE;
  3324. goto CommonReturn;
  3325. TRACE_ERROR(OutOfMemory)
  3326. TRACE_ERROR(DecodeParametersError)
  3327. TRACE_ERROR(DecodePubKeyError)
  3328. #ifdef CMS_PKCS7
  3329. SET_ERROR(NoDssParametersError, CRYPT_E_MISSING_PUBKEY_PARA)
  3330. #else
  3331. SET_ERROR(NoDssParametersError, E_INVALIDARG)
  3332. #endif // CMS_PKCS7
  3333. SET_ERROR(InvalidDssParametersError, E_INVALIDARG)
  3334. SET_ERROR(InvalidDssPubKeyError, E_INVALIDARG)
  3335. }
  3336. #ifndef DH3
  3337. #define DH3 (((DWORD)'D'<<8)+((DWORD)'H'<<16)+((DWORD)'3'<<24))
  3338. #endif
  3339. //+-------------------------------------------------------------------------
  3340. // Encode the RSA DH public key and parameters
  3341. //--------------------------------------------------------------------------
  3342. static BOOL WINAPI EncodeRSADHPublicKeyAndParameters(
  3343. IN DWORD dwCertEncodingType,
  3344. IN OPTIONAL LPCSTR pszPubKeyOID,
  3345. IN const PUBLICKEYSTRUC *pPubKeyStruc,
  3346. IN DWORD cbPubKeyStruc,
  3347. IN DWORD dwFlags,
  3348. IN OPTIONAL void *pvReserved,
  3349. OUT BYTE **ppbEncodedPubKey,
  3350. OUT DWORD *pcbEncodedPubKey,
  3351. OUT BYTE **ppbEncodedParameters,
  3352. OUT DWORD *pcbEncodedParameters
  3353. )
  3354. {
  3355. BOOL fResult;
  3356. BYTE *pbKeyBlob;
  3357. DHPUBKEY_VER3 *pCspPubKey;
  3358. DWORD cbP;
  3359. DWORD cbQ;
  3360. DWORD cbJ;
  3361. BYTE *pbKey;
  3362. CERT_DH_PARAMETERS DhParameters;
  3363. CRYPT_UINT_BLOB DhPubKey;
  3364. *ppbEncodedPubKey = NULL;
  3365. *ppbEncodedParameters = NULL;
  3366. // The CAPI public key representation consists of the following sequence:
  3367. // - PUBLICKEYSTRUC
  3368. // - DHPUBKEY_VER3
  3369. // - rgbP[cbP]
  3370. // - rgbQ[cbQ] -- not used in RSA_DH
  3371. // - rgbG[cbP]
  3372. // - rgbJ[cbJ] -- not used in RSA_DH
  3373. // - rgbY[cbP]
  3374. pbKeyBlob = (BYTE *) pPubKeyStruc;
  3375. pCspPubKey = (DHPUBKEY_VER3 *) (pbKeyBlob + sizeof(PUBLICKEYSTRUC));
  3376. pbKey = pbKeyBlob + sizeof(PUBLICKEYSTRUC) + sizeof(DHPUBKEY_VER3);
  3377. cbP = pCspPubKey->bitlenP / 8;
  3378. cbQ = pCspPubKey->bitlenQ / 8;
  3379. cbJ = pCspPubKey->bitlenJ / 8;
  3380. if (cbPubKeyStruc < sizeof(PUBLICKEYSTRUC) + sizeof(DHPUBKEY_VER3) +
  3381. cbP * 3 + cbQ + cbJ)
  3382. goto InvalidArg;
  3383. if (pPubKeyStruc->bType != PUBLICKEYBLOB)
  3384. goto InvalidArg;
  3385. if (pCspPubKey->magic != DH3)
  3386. goto InvalidArg;
  3387. assert(cbP > 0);
  3388. assert(cbPubKeyStruc >= sizeof(PUBLICKEYSTRUC) + sizeof(DHPUBKEY_VER3) +
  3389. cbP * 3 + cbQ + cbJ);
  3390. assert(pPubKeyStruc->bType == PUBLICKEYBLOB);
  3391. //assert(pPubKeyStruc->bVersion == 3);
  3392. assert(pPubKeyStruc->aiKeyAlg == CALG_DH_SF ||
  3393. pPubKeyStruc->aiKeyAlg == CALG_DH_EPHEM);
  3394. assert(pCspPubKey->magic == DH3);
  3395. assert(pCspPubKey->bitlenP % 8 == 0);
  3396. assert(pCspPubKey->bitlenQ % 8 == 0);
  3397. assert(pCspPubKey->bitlenJ % 8 == 0);
  3398. // Initialize the RSA DH Parameters from CSP data structure
  3399. DhParameters.p.pbData = pbKey;
  3400. DhParameters.p.cbData = cbP;
  3401. pbKey += cbP;
  3402. // No RSA DH Q parameter
  3403. pbKey += cbQ;
  3404. DhParameters.g.pbData = pbKey;
  3405. DhParameters.g.cbData = cbP;
  3406. pbKey += cbP;
  3407. // No RSA DH J parameter
  3408. pbKey += cbJ;
  3409. // Initialize DH public key from CSP data structure
  3410. DhPubKey.cbData = cbP;
  3411. DhPubKey.pbData = pbKey;
  3412. // Encode the parameters and public key
  3413. if (!CryptEncodeObjectEx(
  3414. dwCertEncodingType,
  3415. X509_DH_PARAMETERS,
  3416. &DhParameters,
  3417. CRYPT_ENCODE_ALLOC_FLAG,
  3418. NULL, // pEncodePara
  3419. (void *) ppbEncodedParameters,
  3420. pcbEncodedParameters
  3421. )) goto ErrorReturn;
  3422. if (!CryptEncodeObjectEx(
  3423. dwCertEncodingType,
  3424. X509_DH_PUBLICKEY,
  3425. &DhPubKey,
  3426. CRYPT_ENCODE_ALLOC_FLAG,
  3427. NULL, // pEncodePara
  3428. (void *) ppbEncodedPubKey,
  3429. pcbEncodedPubKey
  3430. )) goto ErrorReturn;
  3431. fResult = TRUE;
  3432. CommonReturn:
  3433. return fResult;
  3434. ErrorReturn:
  3435. PkiDefaultCryptFree(*ppbEncodedParameters);
  3436. PkiDefaultCryptFree(*ppbEncodedPubKey);
  3437. *ppbEncodedParameters = NULL;
  3438. *ppbEncodedPubKey = NULL;
  3439. *pcbEncodedParameters = 0;
  3440. *pcbEncodedPubKey = 0;
  3441. fResult = FALSE;
  3442. goto CommonReturn;
  3443. SET_ERROR(InvalidArg, E_INVALIDARG)
  3444. }
  3445. //+-------------------------------------------------------------------------
  3446. // Encode the X942 DH public key and parameters
  3447. //--------------------------------------------------------------------------
  3448. static BOOL WINAPI EncodeX942DHPublicKeyAndParameters(
  3449. IN DWORD dwCertEncodingType,
  3450. IN OPTIONAL LPCSTR pszPubKeyOID,
  3451. IN const PUBLICKEYSTRUC *pPubKeyStruc,
  3452. IN DWORD cbPubKeyStruc,
  3453. IN DWORD dwFlags,
  3454. IN OPTIONAL void *pvReserved,
  3455. OUT BYTE **ppbEncodedPubKey,
  3456. OUT DWORD *pcbEncodedPubKey,
  3457. OUT BYTE **ppbEncodedParameters,
  3458. OUT DWORD *pcbEncodedParameters
  3459. )
  3460. {
  3461. BOOL fResult;
  3462. BYTE *pbKeyBlob;
  3463. DHPUBKEY_VER3 *pCspPubKey;
  3464. DWORD cbP;
  3465. DWORD cbQ;
  3466. DWORD cbJ;
  3467. BYTE *pbKey;
  3468. CERT_X942_DH_PARAMETERS DhParameters;
  3469. CERT_X942_DH_VALIDATION_PARAMS DhValidationParams;
  3470. CRYPT_UINT_BLOB DhPubKey;
  3471. *ppbEncodedPubKey = NULL;
  3472. *ppbEncodedParameters = NULL;
  3473. // The CAPI public key representation consists of the following sequence:
  3474. // - PUBLICKEYSTRUC
  3475. // - DHPUBKEY_VER3
  3476. // - rgbP[cbP]
  3477. // - rgbQ[cbQ]
  3478. // - rgbG[cbP]
  3479. // - rgbJ[cbJ]
  3480. // - rgbY[cbP]
  3481. pbKeyBlob = (BYTE *) pPubKeyStruc;
  3482. pCspPubKey = (DHPUBKEY_VER3 *) (pbKeyBlob + sizeof(PUBLICKEYSTRUC));
  3483. pbKey = pbKeyBlob + sizeof(PUBLICKEYSTRUC) + sizeof(DHPUBKEY_VER3);
  3484. cbP = pCspPubKey->bitlenP / 8;
  3485. cbQ = pCspPubKey->bitlenQ / 8;
  3486. cbJ = pCspPubKey->bitlenJ / 8;
  3487. if (0 == cbQ)
  3488. return EncodeRSADHPublicKeyAndParameters(
  3489. dwCertEncodingType,
  3490. pszPubKeyOID,
  3491. pPubKeyStruc,
  3492. cbPubKeyStruc,
  3493. dwFlags,
  3494. pvReserved,
  3495. ppbEncodedPubKey,
  3496. pcbEncodedPubKey,
  3497. ppbEncodedParameters,
  3498. pcbEncodedParameters
  3499. );
  3500. if (cbPubKeyStruc < sizeof(PUBLICKEYSTRUC) + sizeof(DHPUBKEY_VER3) +
  3501. cbP * 3 + cbQ + cbJ)
  3502. goto InvalidArg;
  3503. if (pPubKeyStruc->bType != PUBLICKEYBLOB)
  3504. goto InvalidArg;
  3505. if (pCspPubKey->magic != DH3)
  3506. goto InvalidArg;
  3507. assert(cbP > 0);
  3508. assert(cbPubKeyStruc >= sizeof(PUBLICKEYSTRUC) + sizeof(DHPUBKEY_VER3) +
  3509. cbP * 3 + cbQ + cbJ);
  3510. assert(pPubKeyStruc->bType == PUBLICKEYBLOB);
  3511. //assert(pPubKeyStruc->bVersion == 3);
  3512. assert(pPubKeyStruc->aiKeyAlg == CALG_DH_SF ||
  3513. pPubKeyStruc->aiKeyAlg == CALG_DH_EPHEM);
  3514. assert(pCspPubKey->magic == DH3);
  3515. assert(pCspPubKey->bitlenP % 8 == 0);
  3516. assert(pCspPubKey->bitlenQ % 8 == 0);
  3517. assert(pCspPubKey->bitlenJ % 8 == 0);
  3518. // Initialize the X942 DH Parameters from CSP data structure
  3519. DhParameters.p.pbData = pbKey;
  3520. DhParameters.p.cbData = cbP;
  3521. pbKey += cbP;
  3522. DhParameters.q.pbData = pbKey;
  3523. DhParameters.q.cbData = cbQ;
  3524. pbKey += cbQ;
  3525. DhParameters.g.pbData = pbKey;
  3526. DhParameters.g.cbData = cbP;
  3527. pbKey += cbP;
  3528. DhParameters.j.pbData = pbKey;
  3529. DhParameters.j.cbData = cbJ;
  3530. pbKey += cbJ;
  3531. if (0xFFFFFFFF == pCspPubKey->DSSSeed.counter ||
  3532. 0 == pCspPubKey->DSSSeed.counter)
  3533. DhParameters.pValidationParams = NULL;
  3534. else {
  3535. DhParameters.pValidationParams = &DhValidationParams;
  3536. DhValidationParams.pgenCounter = pCspPubKey->DSSSeed.counter;
  3537. DhValidationParams.seed.pbData = pCspPubKey->DSSSeed.seed;
  3538. DhValidationParams.seed.cbData = sizeof(pCspPubKey->DSSSeed.seed);
  3539. DhValidationParams.seed.cUnusedBits = 0;
  3540. }
  3541. // Initialize DH public key from CSP data structure
  3542. DhPubKey.cbData = cbP;
  3543. DhPubKey.pbData = pbKey;
  3544. // Encode the parameters and public key
  3545. if (!CryptEncodeObjectEx(
  3546. dwCertEncodingType,
  3547. X942_DH_PARAMETERS,
  3548. &DhParameters,
  3549. CRYPT_ENCODE_ALLOC_FLAG,
  3550. NULL, // pEncodePara
  3551. (void *) ppbEncodedParameters,
  3552. pcbEncodedParameters
  3553. )) goto ErrorReturn;
  3554. if (!CryptEncodeObjectEx(
  3555. dwCertEncodingType,
  3556. X509_DH_PUBLICKEY,
  3557. &DhPubKey,
  3558. CRYPT_ENCODE_ALLOC_FLAG,
  3559. NULL, // pEncodePara
  3560. (void *) ppbEncodedPubKey,
  3561. pcbEncodedPubKey
  3562. )) goto ErrorReturn;
  3563. fResult = TRUE;
  3564. CommonReturn:
  3565. return fResult;
  3566. ErrorReturn:
  3567. PkiDefaultCryptFree(*ppbEncodedParameters);
  3568. PkiDefaultCryptFree(*ppbEncodedPubKey);
  3569. *ppbEncodedParameters = NULL;
  3570. *ppbEncodedPubKey = NULL;
  3571. *pcbEncodedParameters = 0;
  3572. *pcbEncodedPubKey = 0;
  3573. fResult = FALSE;
  3574. goto CommonReturn;
  3575. SET_ERROR(InvalidArg, E_INVALIDARG)
  3576. }
  3577. #ifndef DH1
  3578. #define DH1 (((DWORD)'D'<<8)+((DWORD)'H'<<16)+((DWORD)'1'<<24))
  3579. #endif
  3580. // Convert a DH1 PublicKey Struc, to a DH3 PublicKey Struc by getting
  3581. // the P and G parameters from the hPubKey.
  3582. static BOOL ConvertDh1ToDh3PublicKeyStruc(
  3583. IN HCRYPTKEY hPubKey,
  3584. IN OUT PUBLICKEYSTRUC **ppPubKeyStruc,
  3585. IN OUT DWORD *pcbPubKeyStruc
  3586. )
  3587. {
  3588. BOOL fResult;
  3589. PUBLICKEYSTRUC *pDh1PubKeyStruc = *ppPubKeyStruc;
  3590. BYTE *pbDh1KeyBlob;
  3591. DHPUBKEY *pDh1CspPubKey;
  3592. BYTE *pbDh1Key;
  3593. PUBLICKEYSTRUC *pDh3PubKeyStruc = NULL;
  3594. DWORD cbDh3PubKeyStruc;
  3595. BYTE *pbDh3KeyBlob;
  3596. DHPUBKEY_VER3 *pDh3CspPubKey;
  3597. BYTE *pbDh3Key;
  3598. DWORD cbP;
  3599. DWORD cbData;
  3600. // The DH1 CAPI public key representation consists of the following
  3601. // sequence:
  3602. // - PUBLICKEYSTRUC
  3603. // - DHPUBKEY
  3604. // - rgbY[cbP]
  3605. pbDh1KeyBlob = (BYTE *) pDh1PubKeyStruc;
  3606. pDh1CspPubKey = (DHPUBKEY *) (pbDh1KeyBlob + sizeof(PUBLICKEYSTRUC));
  3607. pbDh1Key = pbDh1KeyBlob + sizeof(PUBLICKEYSTRUC) + sizeof(DHPUBKEY);
  3608. if (pDh1CspPubKey->magic != DH1)
  3609. return TRUE;
  3610. cbP = pDh1CspPubKey->bitlen / 8;
  3611. if (*pcbPubKeyStruc < sizeof(PUBLICKEYSTRUC) + sizeof(DHPUBKEY) + cbP)
  3612. goto InvalidArg;
  3613. // The DH3 CAPI public key representation consists of the following
  3614. // sequence:
  3615. // - PUBLICKEYSTRUC
  3616. // - DHPUBKEY_VER3
  3617. // - rgbP[cbP]
  3618. // - rgbQ[cbQ] -- will be omitted here
  3619. // - rgbG[cbP]
  3620. // - rgbJ[cbJ] -- will be omitted here
  3621. // - rgbY[cbP]
  3622. cbDh3PubKeyStruc = sizeof(PUBLICKEYSTRUC) + sizeof(DHPUBKEY_VER3) +
  3623. cbP * 3;
  3624. if (NULL == (pDh3PubKeyStruc = (PUBLICKEYSTRUC *) PkiZeroAlloc(
  3625. cbDh3PubKeyStruc)))
  3626. goto OutOfMemory;
  3627. pbDh3KeyBlob = (BYTE *) pDh3PubKeyStruc;
  3628. pDh3CspPubKey = (DHPUBKEY_VER3 *) (pbDh3KeyBlob + sizeof(PUBLICKEYSTRUC));
  3629. pbDh3Key = pbDh3KeyBlob + sizeof(PUBLICKEYSTRUC) + sizeof(DHPUBKEY_VER3);
  3630. pDh3PubKeyStruc->bType = PUBLICKEYBLOB;
  3631. pDh3PubKeyStruc->bVersion = 3;
  3632. pDh3PubKeyStruc->aiKeyAlg = CALG_DH_SF;
  3633. pDh3CspPubKey->magic = DH3;
  3634. pDh3CspPubKey->bitlenP = cbP * 8;
  3635. //pDh3CspPubKey->bitlenQ = 0;
  3636. //pDh3CspPubKey->bitlenJ = 0;
  3637. // Get the P parameter from the public key
  3638. cbData = cbP;
  3639. if (!CryptGetKeyParam(
  3640. hPubKey,
  3641. KP_P,
  3642. pbDh3Key,
  3643. &cbData,
  3644. 0 // dwFlags
  3645. ) || cbData != cbP)
  3646. goto GetPError;
  3647. pbDh3Key += cbP;
  3648. // No Q parameter
  3649. // Get G parameter from the public key
  3650. cbData = cbP;
  3651. if (!CryptGetKeyParam(
  3652. hPubKey,
  3653. KP_G,
  3654. pbDh3Key,
  3655. &cbData,
  3656. 0 // dwFlags
  3657. ) || cbData != cbP)
  3658. goto GetGError;
  3659. pbDh3Key += cbP;
  3660. // No J parameter
  3661. // Y
  3662. memcpy(pbDh3Key, pbDh1Key, cbP);
  3663. assert(pbDh3Key - pbDh3KeyBlob + cbP == cbDh3PubKeyStruc);
  3664. PkiFree(pDh1PubKeyStruc);
  3665. *ppPubKeyStruc = pDh3PubKeyStruc;
  3666. *pcbPubKeyStruc = cbDh3PubKeyStruc;
  3667. fResult = TRUE;
  3668. CommonReturn:
  3669. return fResult;
  3670. ErrorReturn:
  3671. fResult = FALSE;
  3672. PkiFree(pDh3PubKeyStruc);
  3673. goto CommonReturn;
  3674. SET_ERROR(InvalidArg, E_INVALIDARG)
  3675. TRACE_ERROR(OutOfMemory)
  3676. TRACE_ERROR(GetPError)
  3677. TRACE_ERROR(GetGError)
  3678. }
  3679. //+=========================================================================
  3680. // CryptExportPublicKeyInfo functions
  3681. //-=========================================================================
  3682. //+-------------------------------------------------------------------------
  3683. // Use the aiKeyAlg in the public key structure exported by the CSP to
  3684. // determine how to encode the public key.
  3685. //
  3686. // The dwFlags and pvAuxInfo aren't used.
  3687. //--------------------------------------------------------------------------
  3688. static BOOL WINAPI ExportCspPublicKeyInfoEx(
  3689. IN HCRYPTPROV hCryptProv,
  3690. IN DWORD dwKeySpec,
  3691. IN DWORD dwCertEncodingType,
  3692. IN OPTIONAL LPSTR pszPublicKeyObjId,
  3693. IN DWORD dwFlags,
  3694. IN OPTIONAL void *pvAuxInfo,
  3695. OUT PCERT_PUBLIC_KEY_INFO pInfo,
  3696. IN OUT DWORD *pcbInfo
  3697. )
  3698. {
  3699. BOOL fResult;
  3700. DWORD dwErr;
  3701. HCRYPTKEY hPubKey = 0;
  3702. PUBLICKEYSTRUC *pPubKeyStruc = NULL;
  3703. DWORD cbPubKeyStruc;
  3704. if (!CryptGetUserKey(
  3705. hCryptProv,
  3706. dwKeySpec,
  3707. &hPubKey
  3708. )) {
  3709. hPubKey = 0;
  3710. goto GetUserKeyError;
  3711. }
  3712. cbPubKeyStruc = 0;
  3713. if (!CryptExportKey(
  3714. hPubKey,
  3715. 0, // hPubKey
  3716. PUBLICKEYBLOB,
  3717. 0, // dwFlags
  3718. NULL, // pbData
  3719. &cbPubKeyStruc
  3720. ) || (cbPubKeyStruc == 0))
  3721. goto ExportPublicKeyBlobError;
  3722. if (NULL == (pPubKeyStruc = (PUBLICKEYSTRUC *) PkiNonzeroAlloc(
  3723. cbPubKeyStruc)))
  3724. goto OutOfMemory;
  3725. if (!CryptExportKey(
  3726. hPubKey,
  3727. 0, // hPubKey
  3728. PUBLICKEYBLOB,
  3729. 0, // dwFlags
  3730. (BYTE *) pPubKeyStruc,
  3731. &cbPubKeyStruc
  3732. ))
  3733. goto ExportPublicKeyBlobError;
  3734. if (CALG_DH_SF == pPubKeyStruc->aiKeyAlg ||
  3735. CALG_DH_EPHEM == pPubKeyStruc->aiKeyAlg) {
  3736. DWORD cbDh3PubKeyStruc;
  3737. PUBLICKEYSTRUC *pDh3PubKeyStruc;
  3738. // Check if the CSP supports DH3
  3739. cbDh3PubKeyStruc = 0;
  3740. if (!CryptExportKey(
  3741. hPubKey,
  3742. 0, // hPubKey
  3743. PUBLICKEYBLOB,
  3744. CRYPT_BLOB_VER3,
  3745. NULL, // pbData
  3746. &cbDh3PubKeyStruc
  3747. ) || (cbDh3PubKeyStruc == 0)) {
  3748. // Convert DH1 to DH3 by getting and adding the P and G
  3749. // parameters
  3750. if (!ConvertDh1ToDh3PublicKeyStruc(
  3751. hPubKey,
  3752. &pPubKeyStruc,
  3753. &cbPubKeyStruc
  3754. ))
  3755. goto ConvertDh1ToDh3PublicKeyStrucError;
  3756. } else {
  3757. if (NULL == (pDh3PubKeyStruc = (PUBLICKEYSTRUC *) PkiNonzeroAlloc(
  3758. cbDh3PubKeyStruc)))
  3759. goto OutOfMemory;
  3760. if (!CryptExportKey(
  3761. hPubKey,
  3762. 0, // hPubKey
  3763. PUBLICKEYBLOB,
  3764. CRYPT_BLOB_VER3,
  3765. (BYTE *) pDh3PubKeyStruc,
  3766. &cbDh3PubKeyStruc
  3767. )) {
  3768. PkiFree(pDh3PubKeyStruc);
  3769. goto ExportPublicKeyBlobError;
  3770. }
  3771. PkiFree(pPubKeyStruc);
  3772. pPubKeyStruc = pDh3PubKeyStruc;
  3773. cbPubKeyStruc = cbDh3PubKeyStruc;
  3774. }
  3775. if (NULL == pszPublicKeyObjId) {
  3776. DHPUBKEY_VER3 *pDh3CspPubKey;
  3777. // The CAPI public key representation consists of the
  3778. // following sequence:
  3779. // - PUBLICKEYSTRUC
  3780. // - DHPUBKEY_VER3
  3781. // - rgbP[cbP]
  3782. // - rgbQ[cbQ] -- not used in szOID_RSA_DH
  3783. // - rgbG[cbP]
  3784. // - rgbJ[cbJ] -- not used in szOID_RSA_DH
  3785. // - rgbY[cbP]
  3786. pDh3CspPubKey = (DHPUBKEY_VER3 *)
  3787. ((BYTE*) pPubKeyStruc + sizeof(PUBLICKEYSTRUC));
  3788. if (DH3 == pDh3CspPubKey->magic && 0 == pDh3CspPubKey->bitlenQ)
  3789. // szOID_RSA_DH indicates no Q parameter
  3790. pszPublicKeyObjId = szOID_RSA_DH;
  3791. }
  3792. }
  3793. fResult = CryptCreatePublicKeyInfo(
  3794. dwCertEncodingType,
  3795. pszPublicKeyObjId,
  3796. pPubKeyStruc,
  3797. cbPubKeyStruc,
  3798. 0, // dwFlags
  3799. NULL, // pvAuxInfo
  3800. pInfo,
  3801. pcbInfo
  3802. );
  3803. CommonReturn:
  3804. dwErr = GetLastError();
  3805. if (hPubKey)
  3806. CryptDestroyKey(hPubKey);
  3807. PkiFree(pPubKeyStruc);
  3808. SetLastError(dwErr);
  3809. return fResult;
  3810. ErrorReturn:
  3811. *pcbInfo = 0;
  3812. fResult = FALSE;
  3813. goto CommonReturn;
  3814. TRACE_ERROR(GetUserKeyError)
  3815. TRACE_ERROR(ExportPublicKeyBlobError)
  3816. TRACE_ERROR(OutOfMemory)
  3817. TRACE_ERROR(ConvertDh1ToDh3PublicKeyStrucError)
  3818. }
  3819. //+-------------------------------------------------------------------------
  3820. // Export the public key info associated with the provider's corresponding
  3821. // private key.
  3822. //
  3823. // Uses the dwCertEncodingType and pszPublicKeyObjId to call the
  3824. // installable CRYPT_OID_EXPORT_PUBLIC_KEY_INFO_FUNC. The called function
  3825. // has the same signature as CryptExportPublicKeyInfoEx.
  3826. //
  3827. // If unable to find an installable OID function for the pszPublicKeyObjId,
  3828. // attempts to export via the default export function.
  3829. //--------------------------------------------------------------------------
  3830. BOOL
  3831. WINAPI
  3832. CryptExportPublicKeyInfoEx(
  3833. IN HCRYPTPROV hCryptProv,
  3834. IN DWORD dwKeySpec,
  3835. IN DWORD dwCertEncodingType,
  3836. IN OPTIONAL LPSTR pszPublicKeyObjId,
  3837. IN DWORD dwFlags,
  3838. IN OPTIONAL void *pvAuxInfo,
  3839. OUT PCERT_PUBLIC_KEY_INFO pInfo,
  3840. IN OUT DWORD *pcbInfo
  3841. )
  3842. {
  3843. BOOL fResult;
  3844. void *pvFuncAddr;
  3845. HCRYPTOIDFUNCADDR hFuncAddr;
  3846. if (pszPublicKeyObjId && CryptGetOIDFunctionAddress(
  3847. hExportPubKeyFuncSet,
  3848. dwCertEncodingType,
  3849. pszPublicKeyObjId,
  3850. 0, // dwFlags
  3851. &pvFuncAddr,
  3852. &hFuncAddr)) {
  3853. fResult = ((PFN_EXPORT_PUB_KEY_FUNC) pvFuncAddr)(
  3854. hCryptProv,
  3855. dwKeySpec,
  3856. dwCertEncodingType,
  3857. pszPublicKeyObjId,
  3858. dwFlags,
  3859. pvAuxInfo,
  3860. pInfo,
  3861. pcbInfo
  3862. );
  3863. CryptFreeOIDFunctionAddress(hFuncAddr, 0);
  3864. } else
  3865. // Attempt to export via the default function that looks at the
  3866. // public key algorithm in the public key struc exported by the CSP.
  3867. fResult = ExportCspPublicKeyInfoEx(
  3868. hCryptProv,
  3869. dwKeySpec,
  3870. dwCertEncodingType,
  3871. pszPublicKeyObjId,
  3872. dwFlags,
  3873. pvAuxInfo,
  3874. pInfo,
  3875. pcbInfo
  3876. );
  3877. return fResult;
  3878. }
  3879. //+-------------------------------------------------------------------------
  3880. // Export the public key info associated with the provider's corresponding
  3881. // private key.
  3882. //
  3883. // Calls CryptExportPublicKeyInfoEx with pszPublicKeyObjId = NULL,
  3884. // dwFlags = 0 and pvAuxInfo = NULL.
  3885. //--------------------------------------------------------------------------
  3886. BOOL
  3887. WINAPI
  3888. CryptExportPublicKeyInfo(
  3889. IN HCRYPTPROV hCryptProv,
  3890. IN DWORD dwKeySpec,
  3891. IN DWORD dwCertEncodingType,
  3892. OUT PCERT_PUBLIC_KEY_INFO pInfo,
  3893. IN OUT DWORD *pcbInfo
  3894. )
  3895. {
  3896. return CryptExportPublicKeyInfoEx(
  3897. hCryptProv,
  3898. dwKeySpec,
  3899. dwCertEncodingType,
  3900. NULL, // pszPublicKeyObjId
  3901. 0, // dwFlags
  3902. NULL, // pvAuxInfo
  3903. pInfo,
  3904. pcbInfo
  3905. );
  3906. }
  3907. //+=========================================================================
  3908. // CryptImportPublicKeyInfo functions
  3909. //-=========================================================================
  3910. //+-------------------------------------------------------------------------
  3911. // Convert and import the public key info into the provider and return a
  3912. // handle to the public key.
  3913. //
  3914. // Uses the dwCertEncodingType and pInfo->Algorithm.pszObjId to call the
  3915. // installable CRYPT_OID_IMPORT_PUBLIC_KEY_INFO_FUNC. The called function
  3916. // has the same signature as CryptImportPublicKeyInfoEx.
  3917. //
  3918. // If unable to find an installable OID function for the pszObjId,
  3919. // decodes the PublicKeyInfo into a CSP PublicKey Blob and imports.
  3920. //--------------------------------------------------------------------------
  3921. BOOL
  3922. WINAPI
  3923. CryptImportPublicKeyInfoEx(
  3924. IN HCRYPTPROV hCryptProv,
  3925. IN DWORD dwCertEncodingType,
  3926. IN PCERT_PUBLIC_KEY_INFO pInfo,
  3927. IN ALG_ID aiKeyAlg,
  3928. IN DWORD dwFlags,
  3929. IN OPTIONAL void *pvAuxInfo,
  3930. OUT HCRYPTKEY *phKey
  3931. )
  3932. {
  3933. BOOL fResult;
  3934. void *pvFuncAddr;
  3935. HCRYPTOIDFUNCADDR hFuncAddr;
  3936. PUBLICKEYSTRUC *pPubKeyStruc = NULL;
  3937. DWORD cbPubKeyStruc;
  3938. if (CryptGetOIDFunctionAddress(
  3939. hImportPubKeyFuncSet,
  3940. dwCertEncodingType,
  3941. pInfo->Algorithm.pszObjId,
  3942. 0, // dwFlags
  3943. &pvFuncAddr,
  3944. &hFuncAddr)) {
  3945. fResult = ((PFN_IMPORT_PUB_KEY_FUNC) pvFuncAddr)(
  3946. hCryptProv,
  3947. dwCertEncodingType,
  3948. pInfo,
  3949. aiKeyAlg,
  3950. dwFlags,
  3951. pvAuxInfo,
  3952. phKey
  3953. );
  3954. CryptFreeOIDFunctionAddress(hFuncAddr, 0);
  3955. } else {
  3956. if (!CryptConvertPublicKeyInfo(
  3957. dwCertEncodingType,
  3958. pInfo,
  3959. CRYPT_ALLOC_FLAG,
  3960. NULL, // pvReserved
  3961. (void *) &pPubKeyStruc,
  3962. &cbPubKeyStruc
  3963. ))
  3964. goto ConvertPublicKeyInfoError;
  3965. if (aiKeyAlg)
  3966. pPubKeyStruc->aiKeyAlg = aiKeyAlg;
  3967. if (!CryptImportKey(
  3968. hCryptProv,
  3969. (BYTE *) pPubKeyStruc,
  3970. cbPubKeyStruc,
  3971. NULL, // hImpKey
  3972. 0, // dwFlags
  3973. phKey
  3974. ))
  3975. goto ImportKeyError;
  3976. fResult = TRUE;
  3977. }
  3978. CommonReturn:
  3979. PkiDefaultCryptFree(pPubKeyStruc);
  3980. return fResult;
  3981. ErrorReturn:
  3982. *phKey = NULL;
  3983. fResult = FALSE;
  3984. goto CommonReturn;
  3985. TRACE_ERROR(ConvertPublicKeyInfoError)
  3986. TRACE_ERROR(ImportKeyError)
  3987. }
  3988. //+-------------------------------------------------------------------------
  3989. // Convert and import the public key info into the provider and return a
  3990. // handle to the public key.
  3991. //
  3992. // Calls CryptImportPublicKeyInfoEx with aiKeyAlg = 0, dwFlags = 0 and
  3993. // pvAuxInfo = NULL.
  3994. //--------------------------------------------------------------------------
  3995. BOOL
  3996. WINAPI
  3997. CryptImportPublicKeyInfo(
  3998. IN HCRYPTPROV hCryptProv,
  3999. IN DWORD dwCertEncodingType,
  4000. IN PCERT_PUBLIC_KEY_INFO pInfo,
  4001. OUT HCRYPTKEY *phKey
  4002. )
  4003. {
  4004. return CryptImportPublicKeyInfoEx(
  4005. hCryptProv,
  4006. dwCertEncodingType,
  4007. pInfo,
  4008. 0, // aiKeyAlg
  4009. 0, // dwFlags
  4010. NULL, // pvAuxInfo
  4011. phKey
  4012. );
  4013. }
  4014. //+-------------------------------------------------------------------------
  4015. // Create a KeyIdentifier from the CSP Public Key Blob.
  4016. //
  4017. // Converts the CSP PUBLICKEYSTRUC into a X.509 CERT_PUBLIC_KEY_INFO and
  4018. // encodes. The encoded CERT_PUBLIC_KEY_INFO is SHA1 hashed to obtain
  4019. // the Key Identifier.
  4020. //
  4021. // By default, the pPubKeyStruc->aiKeyAlg is used to find the appropriate
  4022. // public key Object Identifier. pszPubKeyOID can be set to override
  4023. // the default OID obtained from the aiKeyAlg.
  4024. //--------------------------------------------------------------------------
  4025. BOOL
  4026. WINAPI
  4027. CryptCreateKeyIdentifierFromCSP(
  4028. IN DWORD dwCertEncodingType,
  4029. IN OPTIONAL LPCSTR pszPubKeyOID,
  4030. IN const PUBLICKEYSTRUC *pPubKeyStruc,
  4031. IN DWORD cbPubKeyStruc,
  4032. IN DWORD dwFlags,
  4033. IN OPTIONAL void *pvReserved,
  4034. OUT BYTE *pbHash,
  4035. IN OUT DWORD *pcbHash
  4036. )
  4037. {
  4038. BOOL fResult;
  4039. PCERT_PUBLIC_KEY_INFO pInfo = NULL;
  4040. DWORD cbInfo;
  4041. if (!CryptCreatePublicKeyInfo(
  4042. dwCertEncodingType,
  4043. pszPubKeyOID,
  4044. pPubKeyStruc,
  4045. cbPubKeyStruc,
  4046. CRYPT_ALLOC_FLAG,
  4047. NULL, // pvReserved
  4048. (void *) &pInfo,
  4049. &cbInfo
  4050. ))
  4051. goto CreatePublicKeyInfoError;
  4052. fResult = CryptHashPublicKeyInfo(
  4053. NULL, // hCryptProv
  4054. CALG_SHA1,
  4055. 0, // dwFlags
  4056. dwCertEncodingType,
  4057. pInfo,
  4058. pbHash,
  4059. pcbHash
  4060. );
  4061. CommonReturn:
  4062. PkiDefaultCryptFree(pInfo);
  4063. return fResult;
  4064. ErrorReturn:
  4065. *pcbHash = 0;
  4066. fResult = FALSE;
  4067. goto CommonReturn;
  4068. TRACE_ERROR(CreatePublicKeyInfoError)
  4069. }
  4070. //+=========================================================================
  4071. // DefaultContext APIs and Data Structures
  4072. //-=========================================================================
  4073. static BOOL InstallThreadDefaultContext(
  4074. IN PDEFAULT_CONTEXT pDefaultContext
  4075. )
  4076. {
  4077. PDEFAULT_CONTEXT pNext;
  4078. pNext = (PDEFAULT_CONTEXT) I_CryptGetTls(hTlsDefaultContext);
  4079. if (pNext) {
  4080. pDefaultContext->pNext = pNext;
  4081. pNext->pPrev = pDefaultContext;
  4082. }
  4083. fHasThreadDefaultContext = TRUE;
  4084. return I_CryptSetTls(hTlsDefaultContext, pDefaultContext);
  4085. }
  4086. static BOOL InstallProcessDefaultContext(
  4087. IN PDEFAULT_CONTEXT pDefaultContext
  4088. )
  4089. {
  4090. EnterCriticalSection(&DefaultContextCriticalSection);
  4091. if (pProcessDefaultContextHead) {
  4092. pDefaultContext->pNext = pProcessDefaultContextHead;
  4093. pProcessDefaultContextHead->pPrev = pDefaultContext;
  4094. }
  4095. pProcessDefaultContextHead = pDefaultContext;
  4096. fHasProcessDefaultContext = TRUE;
  4097. LeaveCriticalSection(&DefaultContextCriticalSection);
  4098. return TRUE;
  4099. }
  4100. //+-------------------------------------------------------------------------
  4101. // Install a previously CryptAcquiredContext'ed HCRYPTPROV to be used as
  4102. // a default context.
  4103. //
  4104. // dwDefaultType and pvDefaultPara specify where the default context is used.
  4105. // For example, install the HCRYPTPROV to be used to verify certificate's
  4106. // having szOID_OIWSEC_md5RSA signatures.
  4107. //
  4108. // By default, the installed HCRYPTPROV is only applicable to the current
  4109. // thread. Set CRYPT_DEFAULT_CONTEXT_PROCESS_FLAG to allow the HCRYPTPROV
  4110. // to be used by all threads in the current process.
  4111. //
  4112. // For a successful install, TRUE is returned and *phDefaultContext is
  4113. // updated with the HANDLE to be passed to CryptUninstallDefaultContext.
  4114. //
  4115. // The installed HCRYPTPROVs are stack ordered (the last installed
  4116. // HCRYPTPROV is checked first). All thread installed HCRYPTPROVs are
  4117. // checked before any process HCRYPTPROVs.
  4118. //
  4119. // The installed HCRYPTPROV remains available for default usage until
  4120. // CryptUninstallDefaultContext is called or the thread or process exits.
  4121. //
  4122. // If CRYPT_DEFAULT_CONTEXT_AUTO_RELEASE_FLAG is set, then, the HCRYPTPROV
  4123. // is CryptReleaseContext'ed at thread or process exit. However,
  4124. // not CryptReleaseContext'ed if CryptUninstallDefaultContext is
  4125. // called.
  4126. //--------------------------------------------------------------------------
  4127. BOOL
  4128. WINAPI
  4129. CryptInstallDefaultContext(
  4130. IN HCRYPTPROV hCryptProv,
  4131. IN DWORD dwDefaultType,
  4132. IN const void *pvDefaultPara,
  4133. IN DWORD dwFlags,
  4134. IN void *pvReserved,
  4135. OUT HCRYPTDEFAULTCONTEXT *phDefaultContext
  4136. )
  4137. {
  4138. BOOL fResult;
  4139. CRYPT_DEFAULT_CONTEXT_MULTI_OID_PARA MultiOIDPara;
  4140. LPSTR rgpszOID[1];
  4141. PCRYPT_DEFAULT_CONTEXT_MULTI_OID_PARA pMultiOIDPara;
  4142. PDEFAULT_CONTEXT pDefaultContext = NULL;
  4143. DWORD cbDefaultContext;
  4144. BYTE *pbExtra;
  4145. DWORD cbExtra;
  4146. if (CRYPT_DEFAULT_CONTEXT_CERT_SIGN_OID == dwDefaultType) {
  4147. dwDefaultType = CRYPT_DEFAULT_CONTEXT_MULTI_CERT_SIGN_OID;
  4148. if (pvDefaultPara) {
  4149. rgpszOID[0] = (LPSTR) pvDefaultPara;
  4150. MultiOIDPara.cOID = 1;
  4151. MultiOIDPara.rgpszOID = rgpszOID;
  4152. pvDefaultPara = (const void *) &MultiOIDPara;
  4153. }
  4154. }
  4155. if (CRYPT_DEFAULT_CONTEXT_MULTI_CERT_SIGN_OID != dwDefaultType)
  4156. goto InvalidArg;
  4157. pMultiOIDPara = (PCRYPT_DEFAULT_CONTEXT_MULTI_OID_PARA) pvDefaultPara;
  4158. if (pMultiOIDPara) {
  4159. DWORD cOID = pMultiOIDPara->cOID;
  4160. LPSTR *ppszOID = pMultiOIDPara->rgpszOID;
  4161. if (0 == cOID)
  4162. goto InvalidArg;
  4163. cbExtra = INFO_LEN_ALIGN(sizeof(CRYPT_DEFAULT_CONTEXT_MULTI_OID_PARA)) +
  4164. cOID * sizeof(LPSTR);
  4165. for ( ; cOID; cOID--, ppszOID++)
  4166. cbExtra += strlen(*ppszOID) + 1;
  4167. } else {
  4168. if (dwFlags & CRYPT_DEFAULT_CONTEXT_PROCESS_FLAG)
  4169. goto InvalidArg;
  4170. cbExtra = 0;
  4171. }
  4172. cbDefaultContext = INFO_LEN_ALIGN(sizeof(DEFAULT_CONTEXT)) + cbExtra;
  4173. if (NULL == (pDefaultContext = (PDEFAULT_CONTEXT) PkiZeroAlloc(
  4174. cbDefaultContext)))
  4175. goto OutOfMemory;
  4176. pDefaultContext->hCryptProv = hCryptProv;
  4177. pDefaultContext->dwDefaultType = dwDefaultType;
  4178. pDefaultContext->dwFlags = dwFlags;
  4179. pbExtra = ((BYTE *) pDefaultContext) +
  4180. INFO_LEN_ALIGN(sizeof(DEFAULT_CONTEXT));
  4181. if (cbExtra) {
  4182. DWORD cOID = pMultiOIDPara->cOID;
  4183. LPSTR *ppszOID = pMultiOIDPara->rgpszOID;
  4184. PCRYPT_DEFAULT_CONTEXT_MULTI_OID_PARA pOIDDefaultPara;
  4185. LPSTR *ppszOIDDefault;
  4186. assert(cOID);
  4187. pOIDDefaultPara = (PCRYPT_DEFAULT_CONTEXT_MULTI_OID_PARA) pbExtra;
  4188. pDefaultContext->pOIDDefaultPara = pOIDDefaultPara;
  4189. pbExtra += INFO_LEN_ALIGN(sizeof(CRYPT_DEFAULT_CONTEXT_MULTI_OID_PARA));
  4190. ppszOIDDefault = (LPSTR *) pbExtra;
  4191. pbExtra += cOID * sizeof(LPSTR);
  4192. pOIDDefaultPara->cOID = cOID;
  4193. pOIDDefaultPara->rgpszOID = ppszOIDDefault;
  4194. for ( ; cOID; cOID--, ppszOID++, ppszOIDDefault++) {
  4195. DWORD cch = strlen(*ppszOID) + 1;
  4196. memcpy(pbExtra, *ppszOID, cch);
  4197. *ppszOIDDefault = (LPSTR) pbExtra;
  4198. pbExtra += cch;
  4199. }
  4200. }
  4201. assert(pbExtra == ((BYTE *) pDefaultContext) + cbDefaultContext);
  4202. if (dwFlags & CRYPT_DEFAULT_CONTEXT_PROCESS_FLAG)
  4203. fResult = InstallProcessDefaultContext(pDefaultContext);
  4204. else
  4205. fResult = InstallThreadDefaultContext(pDefaultContext);
  4206. if (!fResult)
  4207. goto ErrorReturn;
  4208. CommonReturn:
  4209. *phDefaultContext = (HCRYPTDEFAULTCONTEXT) pDefaultContext;
  4210. return fResult;
  4211. ErrorReturn:
  4212. PkiFree(pDefaultContext);
  4213. fResult = FALSE;
  4214. goto CommonReturn;
  4215. SET_ERROR(InvalidArg, E_INVALIDARG)
  4216. TRACE_ERROR(OutOfMemory)
  4217. }
  4218. //+-------------------------------------------------------------------------
  4219. // Uninstall a default context previously installed by
  4220. // CryptInstallDefaultContext.
  4221. //
  4222. // For a default context installed with CRYPT_DEFAULT_CONTEXT_PROCESS_FLAG
  4223. // set, if any other threads are currently using this context,
  4224. // this function will block until they finish.
  4225. //--------------------------------------------------------------------------
  4226. BOOL
  4227. WINAPI
  4228. CryptUninstallDefaultContext(
  4229. HCRYPTDEFAULTCONTEXT hDefaultContext,
  4230. IN DWORD dwFlags,
  4231. IN void *pvReserved
  4232. )
  4233. {
  4234. BOOL fResult;
  4235. PDEFAULT_CONTEXT pDefaultContext = (PDEFAULT_CONTEXT) hDefaultContext;
  4236. PDEFAULT_CONTEXT pDefaultContextHead;
  4237. BOOL fProcess;
  4238. if (NULL == pDefaultContext)
  4239. return TRUE;
  4240. fProcess = (pDefaultContext->dwFlags & CRYPT_DEFAULT_CONTEXT_PROCESS_FLAG);
  4241. if (fProcess) {
  4242. EnterCriticalSection(&DefaultContextCriticalSection);
  4243. pDefaultContextHead = pProcessDefaultContextHead;
  4244. } else {
  4245. pDefaultContextHead = (PDEFAULT_CONTEXT) I_CryptGetTls(
  4246. hTlsDefaultContext);
  4247. }
  4248. if (NULL == pDefaultContextHead)
  4249. goto InvalidArg;
  4250. // Remove context from the list
  4251. if (pDefaultContext->pNext)
  4252. pDefaultContext->pNext->pPrev = pDefaultContext->pPrev;
  4253. if (pDefaultContext->pPrev)
  4254. pDefaultContext->pPrev->pNext = pDefaultContext->pNext;
  4255. else if (pDefaultContext == pDefaultContextHead) {
  4256. pDefaultContextHead = pDefaultContext->pNext;
  4257. if (fProcess)
  4258. pProcessDefaultContextHead = pDefaultContextHead;
  4259. else
  4260. I_CryptSetTls(hTlsDefaultContext, pDefaultContextHead);
  4261. } else
  4262. goto InvalidArg;
  4263. if (fProcess) {
  4264. if (pDefaultContext->lRefCnt) {
  4265. // Wait for all uses of the hCryptProv handle to finish
  4266. if (NULL == (pDefaultContext->hWait = CreateEvent(
  4267. NULL, // lpsa
  4268. FALSE, // fManualReset
  4269. FALSE, // fInitialState
  4270. NULL))) { // lpszEventName
  4271. assert(pDefaultContext->hWait);
  4272. goto UnexpectedError;
  4273. }
  4274. while (pDefaultContext->lRefCnt) {
  4275. LeaveCriticalSection(&DefaultContextCriticalSection);
  4276. WaitForSingleObject(pDefaultContext->hWait, INFINITE);
  4277. EnterCriticalSection(&DefaultContextCriticalSection);
  4278. }
  4279. CloseHandle(pDefaultContext->hWait);
  4280. pDefaultContext->hWait = NULL;
  4281. }
  4282. }
  4283. PkiFree(pDefaultContext);
  4284. fResult = TRUE;
  4285. CommonReturn:
  4286. if (fProcess)
  4287. LeaveCriticalSection(&DefaultContextCriticalSection);
  4288. return fResult;
  4289. ErrorReturn:
  4290. fResult = FALSE;
  4291. goto CommonReturn;
  4292. SET_ERROR(InvalidArg, E_INVALIDARG)
  4293. SET_ERROR(UnexpectedError, E_UNEXPECTED)
  4294. }
  4295. static PDEFAULT_CONTEXT FindDefaultContext(
  4296. IN DWORD dwDefaultType,
  4297. IN const void *pvDefaultPara,
  4298. IN PDEFAULT_CONTEXT pDefaultContext
  4299. )
  4300. {
  4301. for ( ; pDefaultContext; pDefaultContext = pDefaultContext->pNext) {
  4302. switch (dwDefaultType) {
  4303. case CRYPT_DEFAULT_CONTEXT_CERT_SIGN_OID:
  4304. if (CRYPT_DEFAULT_CONTEXT_MULTI_CERT_SIGN_OID ==
  4305. pDefaultContext->dwDefaultType) {
  4306. PCRYPT_DEFAULT_CONTEXT_MULTI_OID_PARA pOIDDefaultPara =
  4307. pDefaultContext->pOIDDefaultPara;
  4308. DWORD cOID;
  4309. LPSTR *ppszOID;
  4310. if (NULL == pOIDDefaultPara)
  4311. return pDefaultContext;
  4312. cOID = pOIDDefaultPara->cOID;
  4313. ppszOID = pOIDDefaultPara->rgpszOID;
  4314. for ( ; cOID; cOID--, ppszOID++) {
  4315. if (0 == strcmp(*ppszOID, (LPSTR) pvDefaultPara))
  4316. return pDefaultContext;
  4317. }
  4318. }
  4319. break;
  4320. default:
  4321. return NULL;
  4322. }
  4323. }
  4324. return NULL;
  4325. }
  4326. //
  4327. // dwDefaultTypes:
  4328. // CRYPT_DEFAULT_CONTEXT_CERT_SIGN_OID (pvDefaultPara :== pszOID)
  4329. BOOL
  4330. WINAPI
  4331. I_CryptGetDefaultContext(
  4332. IN DWORD dwDefaultType,
  4333. IN const void *pvDefaultPara,
  4334. OUT HCRYPTPROV *phCryptProv,
  4335. OUT HCRYPTDEFAULTCONTEXT *phDefaultContext
  4336. )
  4337. {
  4338. if (fHasThreadDefaultContext) {
  4339. PDEFAULT_CONTEXT pDefaultContext;
  4340. pDefaultContext = (PDEFAULT_CONTEXT) I_CryptGetTls(hTlsDefaultContext);
  4341. if (pDefaultContext = FindDefaultContext(
  4342. dwDefaultType,
  4343. pvDefaultPara,
  4344. pDefaultContext
  4345. )) {
  4346. *phCryptProv = pDefaultContext->hCryptProv;
  4347. *phDefaultContext = NULL;
  4348. return TRUE;
  4349. }
  4350. }
  4351. if (fHasProcessDefaultContext) {
  4352. PDEFAULT_CONTEXT pDefaultContext;
  4353. EnterCriticalSection(&DefaultContextCriticalSection);
  4354. if (pDefaultContext = FindDefaultContext(
  4355. dwDefaultType,
  4356. pvDefaultPara,
  4357. pProcessDefaultContextHead
  4358. ))
  4359. pDefaultContext->lRefCnt++;
  4360. LeaveCriticalSection(&DefaultContextCriticalSection);
  4361. if (pDefaultContext) {
  4362. *phCryptProv = pDefaultContext->hCryptProv;
  4363. *phDefaultContext = (HCRYPTDEFAULTCONTEXT) pDefaultContext;
  4364. return TRUE;
  4365. }
  4366. }
  4367. *phCryptProv = NULL;
  4368. *phDefaultContext = NULL;
  4369. return FALSE;
  4370. }
  4371. // hDefaultContext is only NON-null for Process default context
  4372. void
  4373. WINAPI
  4374. I_CryptFreeDefaultContext(
  4375. HCRYPTDEFAULTCONTEXT hDefaultContext
  4376. )
  4377. {
  4378. PDEFAULT_CONTEXT pDefaultContext = (PDEFAULT_CONTEXT) hDefaultContext;
  4379. if (NULL == pDefaultContext)
  4380. return;
  4381. assert(pDefaultContext->dwFlags & CRYPT_DEFAULT_CONTEXT_PROCESS_FLAG);
  4382. assert(0 < pDefaultContext->lRefCnt);
  4383. EnterCriticalSection(&DefaultContextCriticalSection);
  4384. if (0 == --pDefaultContext->lRefCnt && pDefaultContext->hWait)
  4385. SetEvent(pDefaultContext->hWait);
  4386. LeaveCriticalSection(&DefaultContextCriticalSection);
  4387. }
  4388. #ifdef CMS_PKCS7
  4389. WINCRYPT32API
  4390. BOOL
  4391. WINAPI
  4392. CryptVerifyCertificateSignatureEx(
  4393. IN OPTIONAL HCRYPTPROV hCryptProv,
  4394. IN DWORD dwCertEncodingType,
  4395. IN DWORD dwSubjectType,
  4396. IN void *pvSubject,
  4397. IN DWORD dwIssuerType,
  4398. IN void *pvIssuer,
  4399. IN DWORD dwFlags,
  4400. IN OPTIONAL void *pvReserved
  4401. )
  4402. {
  4403. BOOL fResult;
  4404. PCERT_SIGNED_CONTENT_INFO pSignedInfo = NULL;
  4405. DWORD cbSignedInfo;
  4406. HCRYPTDEFAULTCONTEXT hDefaultContext = NULL;
  4407. HCRYPTKEY hSignKey = 0;
  4408. HCRYPTHASH hHash = 0;
  4409. BYTE *pbSignature; // not allocated
  4410. DWORD cbSignature;
  4411. BYTE rgbDssSignature[CERT_DSS_SIGNATURE_LEN];
  4412. ALG_ID aiHash;
  4413. ALG_ID aiPubKey;
  4414. DWORD dwProvType;
  4415. HCRYPTPROV hAcquiredCryptProv = 0;
  4416. DWORD dwSignFlags;
  4417. DWORD dwErr;
  4418. const BYTE *pbEncoded; // not allocated
  4419. DWORD cbEncoded;
  4420. PCERT_PUBLIC_KEY_INFO pIssuerPubKeyInfo;
  4421. CERT_PUBLIC_KEY_INFO IssuerPubKeyInfo;
  4422. PCRYPT_OBJID_BLOB pIssuerPara;
  4423. BYTE *pbAllocIssuerPara = NULL;
  4424. switch (dwSubjectType) {
  4425. case CRYPT_VERIFY_CERT_SIGN_SUBJECT_BLOB:
  4426. {
  4427. PCRYPT_DATA_BLOB pBlob = (PCRYPT_DATA_BLOB) pvSubject;
  4428. pbEncoded = pBlob->pbData;
  4429. cbEncoded = pBlob->cbData;
  4430. }
  4431. break;
  4432. case CRYPT_VERIFY_CERT_SIGN_SUBJECT_CERT:
  4433. {
  4434. PCCERT_CONTEXT pSubject = (PCCERT_CONTEXT) pvSubject;
  4435. pbEncoded = pSubject->pbCertEncoded;
  4436. cbEncoded = pSubject->cbCertEncoded;
  4437. }
  4438. break;
  4439. case CRYPT_VERIFY_CERT_SIGN_SUBJECT_CRL:
  4440. {
  4441. PCCRL_CONTEXT pSubject = (PCCRL_CONTEXT) pvSubject;
  4442. pbEncoded = pSubject->pbCrlEncoded;
  4443. cbEncoded = pSubject->cbCrlEncoded;
  4444. }
  4445. break;
  4446. default:
  4447. goto InvalidSubjectType;
  4448. }
  4449. if (!CryptDecodeObjectEx(
  4450. dwCertEncodingType,
  4451. X509_CERT,
  4452. pbEncoded,
  4453. cbEncoded,
  4454. CRYPT_DECODE_NOCOPY_FLAG | CRYPT_DECODE_ALLOC_FLAG |
  4455. CRYPT_DECODE_NO_SIGNATURE_BYTE_REVERSAL_FLAG,
  4456. &PkiDecodePara,
  4457. (void *) &pSignedInfo,
  4458. &cbSignedInfo
  4459. )) goto DecodeCertError;
  4460. if (!GetSignOIDInfo(pSignedInfo->SignatureAlgorithm.pszObjId,
  4461. &aiHash, &aiPubKey, &dwSignFlags, &dwProvType))
  4462. goto GetSignOIDInfoError;
  4463. if (0 == hCryptProv) {
  4464. if (!I_CryptGetDefaultContext(
  4465. CRYPT_DEFAULT_CONTEXT_CERT_SIGN_OID,
  4466. (const void *) pSignedInfo->SignatureAlgorithm.pszObjId,
  4467. &hCryptProv,
  4468. &hDefaultContext
  4469. )) {
  4470. if (dwProvType && CryptAcquireContext(
  4471. &hCryptProv,
  4472. NULL, // pszContainer
  4473. NULL, // pszProvider,
  4474. dwProvType,
  4475. CRYPT_VERIFYCONTEXT // dwFlags
  4476. ))
  4477. hAcquiredCryptProv = hCryptProv;
  4478. else if (0 == (hCryptProv = I_CryptGetDefaultCryptProv(aiPubKey)))
  4479. goto GetDefaultCryptProvError;
  4480. }
  4481. }
  4482. #if 0
  4483. // Slow down the signature verify while holding the default context
  4484. // reference count
  4485. if (hDefaultContext)
  4486. Sleep(5000);
  4487. #endif
  4488. switch (dwIssuerType) {
  4489. case CRYPT_VERIFY_CERT_SIGN_ISSUER_PUBKEY:
  4490. pIssuerPubKeyInfo = (PCERT_PUBLIC_KEY_INFO) pvIssuer;
  4491. break;
  4492. case CRYPT_VERIFY_CERT_SIGN_ISSUER_CHAIN:
  4493. {
  4494. PCCERT_CHAIN_CONTEXT pChain = (PCCERT_CHAIN_CONTEXT) pvIssuer;
  4495. // All chains have at least the leaf certificate context
  4496. assert(pChain->cChain && pChain->rgpChain[0]->cElement);
  4497. pvIssuer =
  4498. (void *) pChain->rgpChain[0]->rgpElement[0]->pCertContext;
  4499. dwIssuerType = CRYPT_VERIFY_CERT_SIGN_ISSUER_CERT;
  4500. }
  4501. // fall through
  4502. case CRYPT_VERIFY_CERT_SIGN_ISSUER_CERT:
  4503. {
  4504. PCCERT_CONTEXT pIssuer = (PCCERT_CONTEXT) pvIssuer;
  4505. pIssuerPubKeyInfo = &pIssuer->pCertInfo->SubjectPublicKeyInfo;
  4506. // Check if the public key parameters were omitted
  4507. // from the encoded certificate. If omitted, try
  4508. // to use the certificate's CERT_PUBKEY_ALG_PARA_PROP_ID
  4509. // property.
  4510. pIssuerPara = &pIssuerPubKeyInfo->Algorithm.Parameters;
  4511. if (0 == pIssuerPara->cbData ||
  4512. NULL_ASN_TAG == *pIssuerPara->pbData) {
  4513. DWORD cbData;
  4514. if (CertGetCertificateContextProperty(
  4515. pIssuer,
  4516. CERT_PUBKEY_ALG_PARA_PROP_ID,
  4517. NULL, // pvData
  4518. &cbData) && 0 < cbData
  4519. &&
  4520. (pbAllocIssuerPara = (BYTE *) PkiNonzeroAlloc(
  4521. cbData))
  4522. &&
  4523. CertGetCertificateContextProperty(
  4524. pIssuer,
  4525. CERT_PUBKEY_ALG_PARA_PROP_ID,
  4526. pbAllocIssuerPara,
  4527. &cbData)) {
  4528. IssuerPubKeyInfo = *pIssuerPubKeyInfo;
  4529. IssuerPubKeyInfo.Algorithm.Parameters.pbData =
  4530. pbAllocIssuerPara;
  4531. IssuerPubKeyInfo.Algorithm.Parameters.cbData = cbData;
  4532. pIssuerPubKeyInfo = &IssuerPubKeyInfo;
  4533. }
  4534. }
  4535. }
  4536. break;
  4537. case CRYPT_VERIFY_CERT_SIGN_ISSUER_NULL:
  4538. if (CALG_NO_SIGN != aiPubKey)
  4539. goto InvalidIssuerType;
  4540. pIssuerPubKeyInfo = NULL;
  4541. break;
  4542. default:
  4543. goto InvalidIssuerType;
  4544. }
  4545. if (CALG_NO_SIGN == aiPubKey) {
  4546. if (dwIssuerType != CRYPT_VERIFY_CERT_SIGN_ISSUER_NULL)
  4547. goto InvalidIssuerType;
  4548. } else {
  4549. if (!CryptImportPublicKeyInfo(
  4550. hCryptProv,
  4551. dwCertEncodingType,
  4552. pIssuerPubKeyInfo,
  4553. &hSignKey
  4554. )) goto ImportPublicKeyInfoError;
  4555. }
  4556. if (!CryptCreateHash(
  4557. hCryptProv,
  4558. aiHash,
  4559. NULL, // hKey - optional for MAC
  4560. 0, // dwFlags
  4561. &hHash
  4562. )) goto CreateHashError;
  4563. if (!CryptHashData(
  4564. hHash,
  4565. pSignedInfo->ToBeSigned.pbData,
  4566. pSignedInfo->ToBeSigned.cbData,
  4567. 0 // dwFlags
  4568. )) goto HashDataError;
  4569. pbSignature = pSignedInfo->Signature.pbData;
  4570. cbSignature = pSignedInfo->Signature.cbData;
  4571. if (0 == cbSignature)
  4572. goto NoSignatureError;
  4573. if (CALG_NO_SIGN == aiPubKey) {
  4574. BYTE rgbHash[MAX_HASH_LEN];
  4575. DWORD cbHash = sizeof(rgbHash);
  4576. if (!CryptGetHashParam(
  4577. hHash,
  4578. HP_HASHVAL,
  4579. rgbHash,
  4580. &cbHash,
  4581. 0 // dwFlags
  4582. ))
  4583. goto GetHashValueError;
  4584. if (cbHash != cbSignature || 0 != memcmp(rgbHash, pbSignature, cbHash))
  4585. goto NoSignHashCompareError;
  4586. goto SuccessReturn;
  4587. }
  4588. if (CALG_DSS_SIGN == aiPubKey &&
  4589. 0 == (dwSignFlags & CRYPT_OID_INHIBIT_SIGNATURE_FORMAT_FLAG)) {
  4590. DWORD cbData;
  4591. // Convert from ASN.1 sequence of two integers to the CSP signature
  4592. // format.
  4593. cbData = sizeof(rgbDssSignature);
  4594. if (!CryptDecodeObject(
  4595. dwCertEncodingType,
  4596. X509_DSS_SIGNATURE,
  4597. pbSignature,
  4598. cbSignature,
  4599. 0, // dwFlags
  4600. rgbDssSignature,
  4601. &cbData
  4602. ))
  4603. goto DecodeDssSignatureError;
  4604. pbSignature = rgbDssSignature;
  4605. assert(cbData == sizeof(rgbDssSignature));
  4606. cbSignature = sizeof(rgbDssSignature);
  4607. } else
  4608. PkiAsn1ReverseBytes(pbSignature, cbSignature);
  4609. if (!CryptVerifySignature(
  4610. hHash,
  4611. pbSignature,
  4612. cbSignature,
  4613. hSignKey,
  4614. NULL, // sDescription
  4615. 0 // dwFlags
  4616. )) goto VerifySignatureError;
  4617. // For a certificate context certificate, check if the issuer has public
  4618. // key parameters that can be inherited
  4619. pIssuerPara = &pIssuerPubKeyInfo->Algorithm.Parameters;
  4620. if (CRYPT_VERIFY_CERT_SIGN_SUBJECT_CERT == dwSubjectType &&
  4621. pIssuerPara->cbData && NULL_ASN_TAG != *pIssuerPara->pbData) {
  4622. // If a subject is missing its public key parameters and has
  4623. // the same public key algorithm as its issuer, then, set
  4624. // its CERT_PUBKEY_ALG_PARA_PROP_ID property.
  4625. PCCERT_CONTEXT pSubject = (PCCERT_CONTEXT) pvSubject;
  4626. PCERT_PUBLIC_KEY_INFO pSubjectPubKeyInfo =
  4627. &pSubject->pCertInfo->SubjectPublicKeyInfo;
  4628. PCCRYPT_OID_INFO pOIDInfo;
  4629. PCRYPT_OBJID_BLOB pSubjectPara;
  4630. DWORD cbData;
  4631. pSubjectPara = &pSubjectPubKeyInfo->Algorithm.Parameters;
  4632. if (pSubjectPara->cbData && NULL_ASN_TAG != *pSubjectPara->pbData)
  4633. // Subject public key has parameters
  4634. goto SuccessReturn;
  4635. if (CertGetCertificateContextProperty(
  4636. pSubject,
  4637. CERT_PUBKEY_ALG_PARA_PROP_ID,
  4638. NULL, // pvData
  4639. &cbData) && 0 < cbData)
  4640. // Subject already has public key parameters property
  4641. goto SuccessReturn;
  4642. pOIDInfo = CryptFindOIDInfo(
  4643. CRYPT_OID_INFO_OID_KEY,
  4644. pSubjectPubKeyInfo->Algorithm.pszObjId,
  4645. CRYPT_PUBKEY_ALG_OID_GROUP_ID);
  4646. if (NULL == pOIDInfo || aiPubKey != pOIDInfo->Algid)
  4647. // Subject and issuer don't have the same public key algorithms
  4648. goto SuccessReturn;
  4649. CertSetCertificateContextProperty(
  4650. pSubject,
  4651. CERT_PUBKEY_ALG_PARA_PROP_ID,
  4652. CERT_SET_PROPERTY_IGNORE_PERSIST_ERROR_FLAG,
  4653. pIssuerPara
  4654. );
  4655. }
  4656. SuccessReturn:
  4657. fResult = TRUE;
  4658. CommonReturn:
  4659. dwErr = GetLastError();
  4660. if (hSignKey)
  4661. CryptDestroyKey(hSignKey);
  4662. if (hHash)
  4663. CryptDestroyHash(hHash);
  4664. I_CryptFreeDefaultContext(hDefaultContext);
  4665. if (hAcquiredCryptProv)
  4666. CryptReleaseContext(hAcquiredCryptProv, 0);
  4667. PkiFree(pSignedInfo);
  4668. PkiFree(pbAllocIssuerPara);
  4669. SetLastError(dwErr);
  4670. return fResult;
  4671. ErrorReturn:
  4672. fResult = FALSE;
  4673. goto CommonReturn;
  4674. SET_ERROR(InvalidSubjectType, E_INVALIDARG)
  4675. TRACE_ERROR(DecodeCertError)
  4676. TRACE_ERROR(GetSignOIDInfoError)
  4677. TRACE_ERROR(GetDefaultCryptProvError)
  4678. SET_ERROR(InvalidIssuerType, E_INVALIDARG)
  4679. TRACE_ERROR(ImportPublicKeyInfoError)
  4680. TRACE_ERROR(CreateHashError)
  4681. TRACE_ERROR(HashDataError)
  4682. SET_ERROR(NoSignatureError, TRUST_E_NOSIGNATURE)
  4683. TRACE_ERROR(GetHashValueError)
  4684. SET_ERROR(NoSignHashCompareError, NTE_BAD_SIGNATURE)
  4685. TRACE_ERROR(DecodeDssSignatureError)
  4686. TRACE_ERROR(VerifySignatureError)
  4687. }
  4688. //+-------------------------------------------------------------------------
  4689. // Verify the signature of a subject certificate or a CRL using the
  4690. // specified public key.
  4691. //
  4692. // Returns TRUE for a valid signature.
  4693. //
  4694. // hCryptProv specifies the crypto provider to use to verify the signature.
  4695. // It doesn't need to use a private key.
  4696. //--------------------------------------------------------------------------
  4697. BOOL
  4698. WINAPI
  4699. CryptVerifyCertificateSignature(
  4700. IN HCRYPTPROV hCryptProv,
  4701. IN DWORD dwCertEncodingType,
  4702. IN const BYTE * pbEncoded,
  4703. IN DWORD cbEncoded,
  4704. IN PCERT_PUBLIC_KEY_INFO pPublicKey
  4705. )
  4706. {
  4707. CRYPT_DATA_BLOB Subject;
  4708. Subject.cbData = cbEncoded;
  4709. Subject.pbData = (BYTE *) pbEncoded;
  4710. return CryptVerifyCertificateSignatureEx(
  4711. hCryptProv,
  4712. dwCertEncodingType,
  4713. CRYPT_VERIFY_CERT_SIGN_SUBJECT_BLOB,
  4714. (void *) &Subject,
  4715. CRYPT_VERIFY_CERT_SIGN_ISSUER_PUBKEY,
  4716. (void *) pPublicKey,
  4717. 0, // dwFlags
  4718. NULL // pvReserved
  4719. );
  4720. }
  4721. #endif // CMS_PKCS7