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.

2706 lines
82 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1995 - 1996
  6. //
  7. // File: tcert.cpp
  8. //
  9. // Contents: Certificate and CRL Encode/Decode API Tests
  10. //
  11. // See Usage() for list of test options.
  12. //
  13. //
  14. // Functions: main
  15. //
  16. // History: 04-Mar-96 philh created
  17. // 07-Jun-96 HelleS Added printing the command line
  18. // and Failed or Passed at the end.
  19. // 20-Aug-96 jeffspel name changes
  20. //
  21. //--------------------------------------------------------------------------
  22. #include <windows.h>
  23. #include <regstr.h>
  24. #include <assert.h>
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <memory.h>
  29. #include <time.h>
  30. #include <stddef.h>
  31. #include <wincrypt.h>
  32. #include <signcde.h>
  33. //#include <crypt32l.h>
  34. // Note: the SubjectPublicKey is really the PKCS #1 ASN encoding of the
  35. // following information. However, since the SubjectPublicKeyInfo.PublicKey
  36. // is a CRYPT_BIT_BLOB that following is OK for testing purposes.
  37. #ifndef RSA1
  38. #define RSA1 ((DWORD)'R'+((DWORD)'S'<<8)+((DWORD)'A'<<16)+((DWORD)'1'<<24))
  39. #endif
  40. // Build my own CAPI public key
  41. typedef struct _CAPI_PUB_KEY {
  42. PUBLICKEYSTRUC PubKeyStruc;
  43. RSAPUBKEY RsaPubKey;
  44. BYTE rgbModulus[10];
  45. } CAPI_PUB_KEY;
  46. static const CAPI_PUB_KEY SubjectPublicKey = {
  47. {PUBLICKEYBLOB, CUR_BLOB_VERSION, 0, CALG_RSA_SIGN}, // PUBLICKEYSTRUC
  48. {RSA1, 10*8, 4}, // RSAPUBKEY
  49. {0,1,2,3,4,5,6,7,8,9} // rgbModulus
  50. };
  51. static LPSTR pszReadFilename = NULL;
  52. static LPSTR pszPublicKeyFilename = NULL;
  53. static BOOL fWritePublicKeyInfo = FALSE;
  54. static LPSTR pszForwardCertFilename = NULL;
  55. static LPSTR pszReverseCertFilename = NULL;
  56. //+-------------------------------------------------------------------------
  57. // Parameters, data used to encode the messages.
  58. //--------------------------------------------------------------------------
  59. static DWORD dwCertEncodingType = X509_ASN_ENCODING;
  60. static DWORD dwDecodeObjectFlags = 0;
  61. static BOOL fFormatNameStrings = FALSE;
  62. static BOOL fFormatAllNameStrings = FALSE;
  63. static DWORD dwExtLen = 0;
  64. static LPCSTR pszOIDNoSignHash = szOID_OIWSEC_sha1;
  65. //+-------------------------------------------------------------------------
  66. // Error output routines
  67. //--------------------------------------------------------------------------
  68. static void PrintError(LPCSTR pszMsg)
  69. {
  70. printf("%s\n", pszMsg);
  71. }
  72. static void PrintLastError(LPCSTR pszMsg)
  73. {
  74. DWORD dwErr = GetLastError();
  75. printf("%s failed => 0x%x (%d) \n", pszMsg, dwErr, dwErr);
  76. }
  77. void PrintNoError(LPCSTR pszMsg)
  78. {
  79. printf("%s failed => expected error\n", pszMsg);
  80. }
  81. //+-------------------------------------------------------------------------
  82. // Test allocation and free routines
  83. //--------------------------------------------------------------------------
  84. static void *TestAlloc(
  85. IN size_t cbBytes
  86. )
  87. {
  88. void *pv;
  89. pv = malloc(cbBytes);
  90. if (pv == NULL) {
  91. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  92. PrintLastError("TestAlloc");
  93. }
  94. return pv;
  95. }
  96. static void TestFree(
  97. IN void *pv
  98. )
  99. {
  100. if (pv)
  101. free(pv);
  102. }
  103. static CRYPT_DECODE_PARA TestDecodePara = {
  104. offsetof(CRYPT_DECODE_PARA, pfnFree) + sizeof(TestDecodePara.pfnFree),
  105. TestAlloc,
  106. TestFree
  107. };
  108. static void *TestDecodeObject(
  109. IN LPCSTR lpszStructType,
  110. IN const BYTE *pbEncoded,
  111. IN DWORD cbEncoded,
  112. OUT OPTIONAL DWORD *pcbStructInfo = NULL
  113. )
  114. {
  115. DWORD cbStructInfo;
  116. void *pvStructInfo;
  117. if (!CryptDecodeObjectEx(
  118. dwCertEncodingType,
  119. lpszStructType,
  120. pbEncoded,
  121. cbEncoded,
  122. dwDecodeObjectFlags | CRYPT_DECODE_ALLOC_FLAG,
  123. &TestDecodePara,
  124. (void *) &pvStructInfo,
  125. &cbStructInfo
  126. ))
  127. goto ErrorReturn;
  128. CommonReturn:
  129. if (pcbStructInfo)
  130. *pcbStructInfo = cbStructInfo;
  131. return pvStructInfo;
  132. ErrorReturn:
  133. if ((DWORD_PTR) lpszStructType <= 0xFFFF)
  134. printf("CryptDecodeObject(StructType: %d)",
  135. (DWORD)(DWORD_PTR) lpszStructType);
  136. else
  137. printf("CryptDecodeObject(StructType: %s)",
  138. lpszStructType);
  139. PrintLastError("");
  140. pvStructInfo = NULL;
  141. goto CommonReturn;
  142. }
  143. //+-------------------------------------------------------------------------
  144. // Allocate and read an encoded DER blob from a file
  145. //--------------------------------------------------------------------------
  146. BOOL
  147. ReadDERFromFile(
  148. LPCSTR pszFileName,
  149. PBYTE *ppbDER,
  150. PDWORD pcbDER
  151. )
  152. {
  153. BOOL fRet;
  154. HANDLE hFile = 0;
  155. PBYTE pbDER = NULL;
  156. DWORD cbDER;
  157. DWORD cbRead;
  158. if( INVALID_HANDLE_VALUE == (hFile = CreateFile( pszFileName, GENERIC_READ,
  159. FILE_SHARE_READ | FILE_SHARE_WRITE,
  160. NULL, OPEN_EXISTING, 0, NULL))) {
  161. printf( "can't open %s\n", pszFileName);
  162. goto ErrorReturn;
  163. }
  164. cbDER = GetFileSize( hFile, NULL);
  165. if (cbDER == 0) {
  166. printf( "empty file %s\n", pszFileName);
  167. goto ErrorReturn;
  168. }
  169. if (NULL == (pbDER = (PBYTE)TestAlloc(cbDER))) {
  170. printf( "can't alloc %d bytes\n", cbDER);
  171. goto ErrorReturn;
  172. }
  173. if (!ReadFile( hFile, pbDER, cbDER, &cbRead, NULL) ||
  174. (cbRead != cbDER)) {
  175. printf( "can't read %s\n", pszFileName);
  176. goto ErrorReturn;
  177. }
  178. *ppbDER = pbDER;
  179. *pcbDER = cbDER;
  180. fRet = TRUE;
  181. CommonReturn:
  182. if (hFile)
  183. CloseHandle(hFile);
  184. return fRet;
  185. ErrorReturn:
  186. if (pbDER)
  187. TestFree(pbDER);
  188. *ppbDER = NULL;
  189. *pcbDER = 0;
  190. fRet = FALSE;
  191. goto CommonReturn;
  192. }
  193. //+-------------------------------------------------------------------------
  194. // Write an encoded DER blob to a file
  195. //--------------------------------------------------------------------------
  196. BOOL
  197. WriteDERToFile(
  198. LPCSTR pszFileName,
  199. PBYTE pbDER,
  200. DWORD cbDER
  201. )
  202. {
  203. BOOL fResult;
  204. // Write the Encoded Blob to the file
  205. HANDLE hFile;
  206. hFile = CreateFile(pszFileName,
  207. GENERIC_WRITE,
  208. 0, // fdwShareMode
  209. NULL, // lpsa
  210. CREATE_ALWAYS,
  211. 0, // fdwAttrsAndFlags
  212. 0); // TemplateFile
  213. if (INVALID_HANDLE_VALUE == hFile) {
  214. fResult = FALSE;
  215. PrintLastError("WriteDERToFile::CreateFile");
  216. } else {
  217. DWORD dwBytesWritten;
  218. if (!(fResult = WriteFile(
  219. hFile,
  220. pbDER,
  221. cbDER,
  222. &dwBytesWritten,
  223. NULL // lpOverlapped
  224. )))
  225. PrintLastError("WriteDERToFile::WriteFile");
  226. CloseHandle(hFile);
  227. }
  228. return fResult;
  229. }
  230. typedef BOOL (*PFN_ENCODE)(BYTE **ppbEncoded, DWORD *pcbEncoded);
  231. typedef BOOL (*PFN_DECODE)(BYTE *pbEncoded, DWORD cbEncoded);
  232. typedef struct _TEST {
  233. LPCSTR pszName;
  234. PFN_ENCODE pfnEncode;
  235. PFN_DECODE pfnDecode;
  236. } TEST, *PTEST;
  237. static BOOL EncodeCert(BYTE **ppbEncoded, DWORD *pcbEncoded);
  238. static BOOL DecodeCert(BYTE *pbEncoded, DWORD cbEncoded);
  239. static BOOL EncodeCrl(BYTE **ppbEncoded, DWORD *pcbEncoded);
  240. static BOOL DecodeCrl(BYTE *pbEncoded, DWORD cbEncoded);
  241. static BOOL EncodeCertReq(BYTE **ppbEncoded, DWORD *pcbEncoded);
  242. static BOOL DecodeCertReq(BYTE *pbEncoded, DWORD cbEncoded);
  243. static BOOL EncodeKeygenReq(BYTE **ppbEncoded, DWORD *pcbEncoded);
  244. static BOOL DecodeKeygenReq(BYTE *pbEncoded, DWORD cbEncoded);
  245. static BOOL EncodeContentInfo(BYTE **ppbEncoded, DWORD *pcbEncoded);
  246. static BOOL DecodeContentInfo(BYTE *pbEncoded, DWORD cbEncoded);
  247. static BOOL EncodeCertPair(BYTE **ppbEncoded, DWORD *pcbEncoded);
  248. static BOOL DecodeCertPair(BYTE *pbEncoded, DWORD cbEncoded);
  249. TEST Tests[] = {
  250. "cert", EncodeCert, DecodeCert,
  251. "crl", EncodeCrl, DecodeCrl,
  252. "certReq", EncodeCertReq, DecodeCertReq,
  253. "keygenReq", EncodeKeygenReq, DecodeKeygenReq,
  254. "ContentInfo", EncodeContentInfo, DecodeContentInfo,
  255. "CertPair", EncodeCertPair, DecodeCertPair
  256. };
  257. #define NTESTS (sizeof(Tests)/sizeof(Tests[0]))
  258. static void Usage(void)
  259. {
  260. int i;
  261. printf("Usage: tcert [options] [<ContentType>]\n");
  262. printf("Options are:\n");
  263. printf(" -h - This message\n");
  264. printf(" -r<filename> - Read encoded content from file\n");
  265. printf(" -w<filename> - Write encoded content to file\n");
  266. printf(" -p<filename> - Write public key to file\n");
  267. printf(" -P<filename> - Write Name, PublicKeyInfo to file\n");
  268. printf(" -f - Enable name string formatting\n");
  269. printf(" -fAll - Name string formatting (All types)\n");
  270. printf(" -N - Enable NOCOPY decode\n");
  271. printf(" -o<OID> - NoSignHash OID (SHA1 is default)\n");
  272. printf(" -X<number> - eXtension byte length\n");
  273. printf(" -F<CertFilename> - CertPair Forward certificate\n");
  274. printf(" -R<CertFilename> - CertPair Reverse certificate\n");
  275. printf("\n");
  276. printf("ContentTypes (case insensitive):\n");
  277. for (i = 0; i < NTESTS; i++)
  278. printf(" %s\n", Tests[i].pszName);
  279. printf("\n");
  280. printf("Default: %s\n", Tests[0].pszName);
  281. }
  282. int _cdecl main(int argc, char * argv[])
  283. {
  284. int ReturnStatus;
  285. BOOL fResult;
  286. LPCSTR pszName = Tests[0].pszName;
  287. LPSTR pszWriteFilename = NULL;
  288. int c;
  289. PTEST pTest;
  290. BYTE *pbEncoded = NULL;
  291. DWORD cbEncoded;
  292. /*
  293. if (!Crypt32DllMain( NULL, DLL_PROCESS_ATTACH, NULL)) {
  294. printf("Crypt32DllMain attach failed, aborting\n");
  295. ReturnStatus = -1;
  296. goto CommonReturn;
  297. }
  298. */
  299. while (--argc>0)
  300. {
  301. if (**++argv == '-')
  302. {
  303. switch(argv[0][1])
  304. {
  305. case 'r':
  306. pszReadFilename = argv[0]+2;
  307. if (*pszReadFilename == '\0') {
  308. printf("Need to specify filename\n");
  309. goto BadUsage;
  310. }
  311. break;
  312. case 'w':
  313. pszWriteFilename = argv[0]+2;
  314. if (*pszWriteFilename == '\0') {
  315. printf("Need to specify filename\n");
  316. goto BadUsage;
  317. }
  318. break;
  319. case 'P':
  320. fWritePublicKeyInfo = TRUE;
  321. case 'p':
  322. pszPublicKeyFilename = argv[0]+2;
  323. if (*pszPublicKeyFilename == '\0') {
  324. printf("Need to specify filename\n");
  325. goto BadUsage;
  326. }
  327. break;
  328. case 'F':
  329. pszForwardCertFilename = argv[0]+2;
  330. if (*pszForwardCertFilename == '\0') {
  331. printf("Need to specify Forward filename\n");
  332. goto BadUsage;
  333. }
  334. break;
  335. case 'R':
  336. pszReverseCertFilename = argv[0]+2;
  337. if (*pszReverseCertFilename == '\0') {
  338. printf("Need to specify Reverse filename\n");
  339. goto BadUsage;
  340. }
  341. break;
  342. case 'N':
  343. dwDecodeObjectFlags |= CRYPT_DECODE_NOCOPY_FLAG;
  344. break;
  345. case 'X':
  346. dwExtLen = (DWORD) strtoul(argv[0]+2, NULL, 0);
  347. break;
  348. case 'f':
  349. if (argv[0][2]) {
  350. if (0 != _stricmp(argv[0]+2, "ALL")) {
  351. printf("Need to specify -fALL\n");
  352. goto BadUsage;
  353. }
  354. fFormatAllNameStrings = TRUE;
  355. } else
  356. fFormatNameStrings = TRUE;
  357. break;
  358. case 'o':
  359. pszOIDNoSignHash = (LPCSTR) argv[0]+2;
  360. break;
  361. case 'h':
  362. default:
  363. goto BadUsage;
  364. }
  365. } else
  366. pszName = argv[0];
  367. }
  368. for (c = NTESTS, pTest = Tests; c > 0; c--, pTest++) {
  369. if (_stricmp(pszName, pTest->pszName) == 0)
  370. break;
  371. }
  372. if (c == 0) {
  373. printf("Bad ContentType: %s\n", pszName);
  374. goto BadUsage;
  375. }
  376. printf("command line: %s\n", GetCommandLine());
  377. if (pszReadFilename) printf("Reading from: %s ", pszReadFilename);
  378. if (pszWriteFilename) printf("Writing to: %s ", pszWriteFilename);
  379. if (pszPublicKeyFilename) {
  380. if (fWritePublicKeyInfo)
  381. printf("PublicKeyInfo to: %s ", pszPublicKeyFilename);
  382. else
  383. printf("Public Key to: %s ", pszPublicKeyFilename);
  384. }
  385. if (pszForwardCertFilename)
  386. printf("ForwardCert: %s ", pszForwardCertFilename);
  387. if (pszReverseCertFilename)
  388. printf("ReverseCert: %s ", pszReverseCertFilename);
  389. printf("\n");
  390. if (pszReadFilename)
  391. fResult = ReadDERFromFile(pszReadFilename, &pbEncoded, &cbEncoded);
  392. else
  393. fResult = pTest->pfnEncode(&pbEncoded, &cbEncoded);
  394. if (fResult) {
  395. if (pszWriteFilename)
  396. WriteDERToFile(pszWriteFilename, pbEncoded, cbEncoded);
  397. pTest->pfnDecode(pbEncoded, cbEncoded);
  398. TestFree(pbEncoded);
  399. }
  400. ReturnStatus = 0;
  401. goto CommonReturn;
  402. BadUsage:
  403. Usage();
  404. ReturnStatus = -1;
  405. CommonReturn:
  406. if (!ReturnStatus)
  407. printf("Passed\n");
  408. else
  409. printf("Failed\n");
  410. /*
  411. if (!Crypt32DllMain( NULL, DLL_PROCESS_DETACH, NULL)) {
  412. printf("Crypt32DllMain detach failed, aborting\n");
  413. ReturnStatus = -1;
  414. }
  415. */
  416. return ReturnStatus;
  417. }
  418. static BOOL EncodeSignedContent(
  419. BYTE *pbToBeSigned,
  420. DWORD cbToBeSigned,
  421. BYTE **ppbEncoded,
  422. DWORD *pcbEncoded)
  423. {
  424. BOOL fResult;
  425. BYTE *pbEncoded = NULL;
  426. DWORD cbEncoded;
  427. BYTE *pbSignature = NULL;
  428. DWORD cbSignature;
  429. CERT_SIGNED_CONTENT_INFO CertEncoding;
  430. CRYPT_ALGORITHM_IDENTIFIER SignatureAlgorithm = {
  431. (LPSTR) pszOIDNoSignHash, 0, 0
  432. };
  433. CRYPT_DATA_BLOB EncodedBlob;
  434. cbSignature = 0;
  435. if (!CryptSignCertificate(
  436. NULL, // hCryptProv
  437. 0, // dwKeySpec
  438. dwCertEncodingType,
  439. pbToBeSigned,
  440. cbToBeSigned,
  441. &SignatureAlgorithm,
  442. NULL, // pvHashAuxInfo
  443. NULL, // pbSignature
  444. &cbSignature
  445. )) {
  446. PrintLastError("EncodeSignedContent::CryptSignCertificate(cbEncoded == 0)");
  447. goto ErrorReturn;
  448. }
  449. pbSignature = (BYTE *) TestAlloc(cbSignature);
  450. if (pbSignature == NULL) goto ErrorReturn;
  451. if (!CryptSignCertificate(
  452. NULL, // hCryptProv
  453. 0, // dwKeySpec
  454. dwCertEncodingType,
  455. pbToBeSigned,
  456. cbToBeSigned,
  457. &SignatureAlgorithm,
  458. NULL, // pvHashAuxInfo
  459. pbSignature,
  460. &cbSignature
  461. )) {
  462. PrintLastError("EncodeSignedContent::CryptSignCertificate");
  463. goto ErrorReturn;
  464. }
  465. ZeroMemory(&CertEncoding, sizeof(CertEncoding));
  466. CertEncoding.ToBeSigned.pbData = pbToBeSigned;
  467. CertEncoding.ToBeSigned.cbData = cbToBeSigned;
  468. CertEncoding.SignatureAlgorithm = SignatureAlgorithm;
  469. CertEncoding.Signature.pbData = pbSignature;
  470. CertEncoding.Signature.cbData = cbSignature;
  471. cbEncoded = 0;
  472. CryptEncodeObject(
  473. dwCertEncodingType,
  474. X509_CERT,
  475. &CertEncoding,
  476. NULL, // pbEncoded
  477. &cbEncoded
  478. );
  479. if (cbEncoded == 0) {
  480. PrintLastError("EncodeSignedContent::CryptEncodeObject(cbEncoded == 0)");
  481. goto ErrorReturn;
  482. }
  483. pbEncoded = (BYTE *) TestAlloc(cbEncoded);
  484. if (pbEncoded == NULL) goto ErrorReturn;
  485. if (!CryptEncodeObject(
  486. dwCertEncodingType,
  487. X509_CERT,
  488. &CertEncoding,
  489. pbEncoded,
  490. &cbEncoded
  491. )) {
  492. PrintLastError("EncodeSignedContent::CryptEncodeObject");
  493. goto ErrorReturn;
  494. }
  495. EncodedBlob.cbData = cbEncoded;
  496. EncodedBlob.pbData = pbEncoded;
  497. if (!CryptVerifyCertificateSignatureEx(
  498. NULL, // hCryptProv
  499. dwCertEncodingType,
  500. CRYPT_VERIFY_CERT_SIGN_SUBJECT_BLOB,
  501. (void *) &EncodedBlob,
  502. CRYPT_VERIFY_CERT_SIGN_ISSUER_NULL,
  503. NULL, // pvIssuer
  504. 0, // dwFlags
  505. NULL // pvReserved
  506. )) {
  507. PrintLastError("EncodeSignedContent::CryptVerifyCertificateSignatureEx");
  508. }
  509. fResult = TRUE;
  510. goto CommonReturn;
  511. ErrorReturn:
  512. if (pbEncoded) {
  513. TestFree(pbEncoded);
  514. pbEncoded = NULL;
  515. }
  516. cbEncoded = 0;
  517. fResult = FALSE;
  518. CommonReturn:
  519. if (pbSignature)
  520. TestFree(pbSignature);
  521. *ppbEncoded = pbEncoded;
  522. *pcbEncoded = cbEncoded;
  523. return fResult;
  524. }
  525. static void PrintBadUnicodeEncode(LPCSTR pszMsg, DWORD dwExpectedErr,
  526. DWORD cbEncoded)
  527. {
  528. DWORD dwErr = GetLastError();
  529. if (dwErr != dwExpectedErr)
  530. printf("%s failed => expected : 0x%x (%d), LastError: 0x%x (%d)\n",
  531. pszMsg, dwExpectedErr, dwExpectedErr, dwErr, dwErr);
  532. printf("%s bad unicode encode => LastError: 0x%x (%d) ",
  533. pszMsg, dwErr, dwErr);
  534. printf("cbEncoded: 0x%x RDN: %d Attr: %d Value: %d\n",
  535. cbEncoded,
  536. GET_CERT_UNICODE_RDN_ERR_INDEX(cbEncoded),
  537. GET_CERT_UNICODE_ATTR_ERR_INDEX(cbEncoded),
  538. GET_CERT_UNICODE_VALUE_ERR_INDEX(cbEncoded));
  539. }
  540. static void DoBadEncodeIssuer()
  541. {
  542. DWORD cbIssuerEncoded;
  543. CERT_RDN_ATTR rgBadPrintableAttr[] = {
  544. // 0 - rgdwPrintableOrT61ValueType,
  545. szOID_COMMON_NAME, 0, 0,
  546. (BYTE *) L"CN: printable or t61",
  547. // 1 - rgdwPrintableOrT61ValueType,
  548. szOID_LOCALITY_NAME, 0, 0,
  549. (BYTE *) L"L: printable or t61 \"###\"",
  550. // 2 - BAD rgdwPrintableValueType,
  551. szOID_COUNTRY_NAME, 0, 0,
  552. (BYTE *) L"C: printable ### az AZ 09 \'()+,-./:=? "
  553. };
  554. CERT_RDN rgBadPrintableRDN[] = {
  555. 1, &rgBadPrintableAttr[0],
  556. 3, &rgBadPrintableAttr[0]
  557. };
  558. CERT_NAME_INFO BadPrintableName = {2, rgBadPrintableRDN};
  559. CERT_RDN_ATTR rgBadNumericAttr[] = {
  560. // 0 - rgdwPrintableOrT61ValueType,
  561. szOID_COMMON_NAME, 0, 0,
  562. (BYTE *) L"CN: printable or t61",
  563. // 1 - rgdwPrintableValueType,
  564. szOID_COUNTRY_NAME, 0, 0,
  565. (BYTE *) L"C: printable az AZ 09 \'()+,-./:=? ",
  566. // 2 - rgdwPrintableOrT61ValueType,
  567. szOID_LOCALITY_NAME, 0, 0,
  568. (BYTE *) L"L: printable or t61 \"###\"",
  569. // 3 - BAD rgdwNumericValueType,
  570. szOID_X21_ADDRESS, 0, 0,
  571. (BYTE *) L"0123456789a ",
  572. // 4 - none, use default
  573. szOID_REGISTERED_ADDRESS, 0, 0,
  574. (BYTE *) L"Default"
  575. };
  576. CERT_RDN rgBadNumericRDN[] = {
  577. 1, &rgBadNumericAttr[0],
  578. 1, &rgBadNumericAttr[1],
  579. 1, &rgBadNumericAttr[2],
  580. 1, &rgBadNumericAttr[3],
  581. 1, &rgBadNumericAttr[4]
  582. };
  583. CERT_NAME_INFO BadNumericName = {5, rgBadNumericRDN};
  584. // This one has non-zero dwValueTypes
  585. CERT_RDN_ATTR rgBadNumericAttr2[] = {
  586. // 0 - rgdwPrintableOrT61ValueType,
  587. szOID_COMMON_NAME, CERT_RDN_PRINTABLE_STRING, 0,
  588. (BYTE *) L"CN: printable or t61",
  589. // 1 - rgdwPrintableValueType,
  590. szOID_COUNTRY_NAME, CERT_RDN_PRINTABLE_STRING, 0,
  591. (BYTE *) L"C: printable az AZ 09 \'()+,-./:=? ",
  592. // 2 - rgdwPrintableOrT61ValueType,
  593. szOID_LOCALITY_NAME, CERT_RDN_T61_STRING, 0,
  594. (BYTE *) L"L: printable or t61 \"###\"",
  595. // 3 - BAD rgdwNumericValueType,
  596. szOID_X21_ADDRESS, CERT_RDN_NUMERIC_STRING, 0,
  597. (BYTE *) L"0123456789a ",
  598. // 4 - none, use default
  599. szOID_REGISTERED_ADDRESS, CERT_RDN_IA5_STRING, 0,
  600. (BYTE *) L"Default"
  601. };
  602. CERT_RDN rgBadNumericRDN2[] = {
  603. 1, &rgBadNumericAttr2[0],
  604. 1, &rgBadNumericAttr2[1],
  605. 1, &rgBadNumericAttr2[2],
  606. 4, &rgBadNumericAttr2[1],
  607. 1, &rgBadNumericAttr2[4]
  608. };
  609. CERT_NAME_INFO BadNumericName2 = {5, rgBadNumericRDN2};
  610. BYTE rgbBadIA5[] = {0x80, 0x00, 0x00, 0x00};
  611. CERT_RDN_ATTR rgBadIA5Attr[] = {
  612. // 0 - BAD rgdwIA5ValueType
  613. szOID_RSA_emailAddr, 0, 0,
  614. rgbBadIA5,
  615. };
  616. CERT_RDN rgBadIA5RDN[] = {
  617. 1, &rgBadIA5Attr[0]
  618. };
  619. CERT_NAME_INFO BadIA5Name = {1, rgBadIA5RDN};
  620. cbIssuerEncoded = 0;
  621. if (CryptEncodeObject(
  622. dwCertEncodingType,
  623. X509_UNICODE_NAME,
  624. &BadPrintableName,
  625. NULL, // pbEncoded
  626. &cbIssuerEncoded
  627. ))
  628. PrintNoError("X509_UNICODE_NAME:: BadPrintableName");
  629. else
  630. PrintBadUnicodeEncode("PrintableString",
  631. (DWORD) CRYPT_E_INVALID_PRINTABLE_STRING, cbIssuerEncoded);
  632. cbIssuerEncoded = 0;
  633. rgBadPrintableAttr[2].dwValueType = CERT_RDN_PRINTABLE_STRING;
  634. if (CryptEncodeObject(
  635. dwCertEncodingType,
  636. X509_UNICODE_NAME,
  637. &BadPrintableName,
  638. NULL, // pbEncoded
  639. &cbIssuerEncoded
  640. ))
  641. PrintNoError("X509_UNICODE_NAME:: BadPrintableName(set dwValueType)");
  642. else
  643. PrintBadUnicodeEncode("PrintableString(set dwValueType)",
  644. (DWORD) CRYPT_E_INVALID_PRINTABLE_STRING, cbIssuerEncoded);
  645. cbIssuerEncoded = 0;
  646. if (!CryptEncodeObjectEx(
  647. dwCertEncodingType,
  648. X509_UNICODE_NAME,
  649. &BadPrintableName,
  650. CRYPT_UNICODE_NAME_ENCODE_DISABLE_CHECK_TYPE_FLAG,
  651. NULL, // pEncodePara
  652. NULL, // pbEncoded
  653. &cbIssuerEncoded
  654. ) || 0 == cbIssuerEncoded)
  655. PrintLastError("X509_UNICODE_NAME:: DISABLE_CHECK dwFlags");
  656. rgBadPrintableAttr[2].dwValueType =
  657. CERT_RDN_DISABLE_CHECK_TYPE_FLAG | CERT_RDN_PRINTABLE_STRING;
  658. cbIssuerEncoded = 0;
  659. if (!CryptEncodeObjectEx(
  660. dwCertEncodingType,
  661. X509_UNICODE_NAME,
  662. &BadPrintableName,
  663. 0,
  664. NULL, // pEncodePara
  665. NULL, // pbEncoded
  666. &cbIssuerEncoded
  667. ) || 0 == cbIssuerEncoded)
  668. PrintLastError("X509_UNICODE_NAME:: DISABLE_CHECK dwValueType");
  669. cbIssuerEncoded = 0;
  670. if (!CryptEncodeObjectEx(
  671. dwCertEncodingType,
  672. X509_UNICODE_NAME,
  673. &BadPrintableName,
  674. CRYPT_UNICODE_NAME_ENCODE_ENABLE_T61_UNICODE_FLAG,
  675. NULL, // pEncodePara
  676. NULL, // pbEncoded
  677. &cbIssuerEncoded
  678. ) || 0 == cbIssuerEncoded)
  679. PrintLastError("X509_UNICODE_NAME:: ENABLE_T61 dwFlags");
  680. rgBadPrintableAttr[1].dwValueType =
  681. CERT_RDN_ENABLE_T61_UNICODE_FLAG;
  682. cbIssuerEncoded = 0;
  683. if (!CryptEncodeObjectEx(
  684. dwCertEncodingType,
  685. X509_UNICODE_NAME,
  686. &BadPrintableName,
  687. 0,
  688. NULL, // pEncodePara
  689. NULL, // pbEncoded
  690. &cbIssuerEncoded
  691. ) || 0 == cbIssuerEncoded)
  692. PrintLastError("X509_UNICODE_NAME:: ENABLE_T61 dwValueType");
  693. cbIssuerEncoded = 0;
  694. if (!CryptEncodeObjectEx(
  695. dwCertEncodingType,
  696. X509_UNICODE_NAME,
  697. &BadPrintableName,
  698. CRYPT_UNICODE_NAME_ENCODE_ENABLE_UTF8_UNICODE_FLAG,
  699. NULL, // pEncodePara
  700. NULL, // pbEncoded
  701. &cbIssuerEncoded
  702. ) || 0 == cbIssuerEncoded)
  703. PrintLastError("X509_UNICODE_NAME:: ENABLE_UTF8 dwFlags");
  704. cbIssuerEncoded = 0;
  705. if (CryptEncodeObject(
  706. dwCertEncodingType,
  707. X509_UNICODE_NAME,
  708. &BadNumericName,
  709. NULL, // pbEncoded
  710. &cbIssuerEncoded
  711. ))
  712. PrintNoError("X509_UNICODE_NAME:: BadNumericName");
  713. else
  714. PrintBadUnicodeEncode("NumericString",
  715. (DWORD) CRYPT_E_INVALID_NUMERIC_STRING, cbIssuerEncoded);
  716. cbIssuerEncoded = 0;
  717. if (CryptEncodeObject(
  718. dwCertEncodingType,
  719. X509_UNICODE_NAME,
  720. &BadNumericName2,
  721. NULL, // pbEncoded
  722. &cbIssuerEncoded
  723. ))
  724. PrintNoError("X509_UNICODE_NAME:: BadNumericName2");
  725. else
  726. PrintBadUnicodeEncode("NumericString2",
  727. (DWORD) CRYPT_E_INVALID_NUMERIC_STRING, cbIssuerEncoded);
  728. cbIssuerEncoded = 0;
  729. if (CryptEncodeObject(
  730. dwCertEncodingType,
  731. X509_UNICODE_NAME,
  732. &BadIA5Name,
  733. NULL, // pbEncoded
  734. &cbIssuerEncoded
  735. ))
  736. PrintNoError("X509_UNICODE_NAME:: BadIA5Name");
  737. else
  738. PrintBadUnicodeEncode("IA5String",
  739. (DWORD) CRYPT_E_INVALID_IA5_STRING, cbIssuerEncoded);
  740. }
  741. static BYTE *EncodeIssuer(DWORD *pcbIssuerEncoded)
  742. {
  743. BYTE *pbIssuerEncoded = NULL;
  744. DWORD cbIssuerEncoded;
  745. BYTE rgbOctet[] = {1, 0xFF, 0x7F};
  746. BYTE rgbEncodedBlob[] = {0x05, 00};
  747. CERT_RDN_ATTR rgAttr[] = {
  748. // 0 - rgdwPrintableOrT61ValueType
  749. szOID_COMMON_NAME, 0, 0,
  750. (BYTE *) L"CN: printable or t61",
  751. // 1 - rgdwPrintableValueType
  752. szOID_COUNTRY_NAME, 0, 0,
  753. (BYTE *) L"C: printable az AZ 09 \'()+,-./:=? ",
  754. // 2 - rgdwPrintableOrT61ValueType
  755. szOID_LOCALITY_NAME, 0, 0,
  756. (BYTE *) L"L: printable or t61 \"###\"",
  757. // 3 - rgdwNumericValueType
  758. szOID_X21_ADDRESS, 0, 0,
  759. (BYTE *) L" 0123456789 ",
  760. // 4 - none, use default
  761. szOID_REGISTERED_ADDRESS, 0, 0,
  762. (BYTE *) L"Default",
  763. // 5 - rgdwIA5ValueType
  764. szOID_RSA_emailAddr, 0, 0,
  765. (BYTE *) L"Email, IA5 !@#$%^&*()_+{|}",
  766. // 6 - Unicode
  767. "1.2.2.5", CERT_RDN_BMP_STRING, 0,
  768. (BYTE *) L"Null terminated UNICODE",
  769. // 7 - Unicode
  770. "1.2.2.5.1", CERT_RDN_BMP_STRING, 10 *2,
  771. (BYTE *) L"Length UNICODE",
  772. // 8 - Universal
  773. "1.2.2.5.2", CERT_RDN_UNIVERSAL_STRING, 0,
  774. (BYTE *) L"Universal ~!@#$%^&*()_+{}:\"<>?",
  775. // 9 - Octet
  776. "1.2.2.5.3", CERT_RDN_OCTET_STRING, sizeof(rgbOctet),
  777. rgbOctet,
  778. // 10 - EncodedBlob
  779. "1.2.2.5.4", CERT_RDN_ENCODED_BLOB, sizeof(rgbEncodedBlob),
  780. rgbEncodedBlob,
  781. // 11 - Empty rgdwPrintableOrT61ValueType
  782. szOID_LOCALITY_NAME, 0, 0, NULL,
  783. // 12 - Empty rgdwNumericValueType
  784. szOID_X21_ADDRESS, 0, 0, NULL,
  785. // 13 - DC (IA5)
  786. szOID_DOMAIN_COMPONENT, 0, 0,
  787. (BYTE *) L"microsoft",
  788. // 14 - DC (IA5)
  789. szOID_DOMAIN_COMPONENT, 0, 0,
  790. (BYTE *) L"com",
  791. // 15 - UTF8
  792. "1.2.8.5", CERT_RDN_UTF8_STRING, 0,
  793. (BYTE *) L"Null terminated UTF8",
  794. // 16 - UTF8
  795. "1.2.8.5.1", CERT_RDN_UTF8_STRING, 11 *2,
  796. (BYTE *) L"Length UTF8",
  797. // Note, FFFE and FFFF are excluded from the UTF8 standard
  798. // 17 - UTF8
  799. "1.2.8.5.2", CERT_RDN_UTF8_STRING, 0,
  800. (BYTE *) L"SPECIAL UTF8: "
  801. L"\x0001 \x0002 \x007e \x007f "
  802. L"\x0080 \x0081 \x07fe \x07ff "
  803. L"\x0800 \x0801 \xfffc \xfffd",
  804. // 18 - UNICODE
  805. "1.2.8.5.3", CERT_RDN_UNICODE_STRING, 0,
  806. (BYTE *) L"SPECIAL UNICODE: "
  807. L"\x0001 \x0002 \x007e \x007f "
  808. L"\x0080 \x0081 \x07fe \x07ff "
  809. L"\x0800 \x0801 \xfffe \xffff",
  810. // 19 - DC (UTF8)
  811. szOID_DOMAIN_COMPONENT, 0, 0,
  812. (BYTE *) L"Unicode DC: "
  813. L"\x0001 \x0002 \x007e \x007f "
  814. L"\x0080 \x0081 \x07fe \x07ff "
  815. L"\x0800 \x0801 \xfffe \xffff",
  816. // 20 - Universal
  817. "1.2.2.5.2.1.1.1", CERT_RDN_UNIVERSAL_STRING, 0,
  818. (BYTE *) L"SPECIAL UNIVERSAL with Surrogate Pairs: "
  819. L"\xd800\xdc00\xdbff\xdfff"
  820. L"\xdbfe\xdc03\xd801\xdfcf"
  821. L"\xd801\x0081\xdc01\xdc02"
  822. L"\xd805\xd806\xd807\xdc04"
  823. L"\xd802\xd803\xfffe\xd804",
  824. };
  825. CERT_RDN rgRDN[] = {
  826. 1, &rgAttr[0],
  827. 1, &rgAttr[1],
  828. 1, &rgAttr[2],
  829. 1, &rgAttr[3],
  830. 1, &rgAttr[4],
  831. 1, &rgAttr[5],
  832. 3, &rgAttr[5],
  833. 13, &rgAttr[8]
  834. };
  835. CERT_NAME_INFO Name = {8, rgRDN};
  836. DoBadEncodeIssuer();
  837. cbIssuerEncoded = 0;
  838. CryptEncodeObject(
  839. dwCertEncodingType,
  840. X509_UNICODE_NAME,
  841. &Name,
  842. NULL, // pbEncoded
  843. &cbIssuerEncoded
  844. );
  845. if (cbIssuerEncoded == 0) {
  846. PrintLastError("EncodeIssuer::CryptEncodeObject(cbEncoded == 0)");
  847. goto ErrorReturn;
  848. }
  849. pbIssuerEncoded = (BYTE *) TestAlloc(cbIssuerEncoded);
  850. if (pbIssuerEncoded == NULL) goto ErrorReturn;
  851. if (!CryptEncodeObject(
  852. dwCertEncodingType,
  853. X509_UNICODE_NAME,
  854. &Name,
  855. pbIssuerEncoded,
  856. &cbIssuerEncoded
  857. )) {
  858. PrintLastError("EncodeIssuer::CryptEncodeObject");
  859. goto ErrorReturn;
  860. }
  861. goto CommonReturn;
  862. ErrorReturn:
  863. if (pbIssuerEncoded)
  864. TestFree(pbIssuerEncoded);
  865. pbIssuerEncoded = NULL;
  866. cbIssuerEncoded = 0;
  867. CommonReturn:
  868. *pcbIssuerEncoded = cbIssuerEncoded;
  869. return pbIssuerEncoded;
  870. }
  871. static BOOL EncodeCert(BYTE **ppbEncoded, DWORD *pcbEncoded)
  872. {
  873. BOOL fResult;
  874. BYTE *pbIssuerEncoded = NULL;
  875. DWORD cbIssuerEncoded;
  876. BYTE *pbNameEncoded = NULL;
  877. DWORD cbNameEncoded;
  878. BYTE *pbCertEncoded = NULL;
  879. DWORD cbCertEncoded;
  880. DWORD SerialNumber[2] = {0x12345678, 0x33445566};
  881. SYSTEMTIME SystemTime;
  882. #define ISSUER_UNIQUE_ID "IssuerUniqueId"
  883. #define ATTR_0_0 "attr 0_0 printable"
  884. #define ATTR_0_1 "attr 0_1 IA5"
  885. #define ATTR_1_0 "attr 1_0 numeric"
  886. #define ATTR_1_1 "attr 1_1 octet"
  887. #define ATTR_2_0 "attr 2_0 teletex"
  888. #define ATTR_2_1 "attr 2_1 videotex"
  889. #define ATTR_2_2 "attr 2_2 graphic"
  890. #define ATTR_2_3 "attr 2_3 visible"
  891. #define ATTR_2_4 "attr 2_4 general"
  892. #define ATTR_2_5 L"attr 2_5 BMP:: Unicode"
  893. #define ATTR_2_6 L"attr 2_6 UTF8:: Unicode"
  894. ULONG Universal[] = {0x12345678, 0, 0xFFFF1111, 0x87654321,
  895. 0x00FFFF,
  896. 0x010000,
  897. 0x010001,
  898. 0x10FFFE,
  899. 0x10FFFF,
  900. 0x110000,
  901. 0x10F803,
  902. 0x0107CF,
  903. 0x011C04,
  904. };
  905. BYTE NullDer[] = {0x05, 0x00};
  906. BYTE IntegerDer[] = {0x02, 0x01, 0x35};
  907. CERT_RDN_ATTR rgAttr0[] = {
  908. "1.2.0.0", CERT_RDN_PRINTABLE_STRING,
  909. strlen(ATTR_0_0), (BYTE *) ATTR_0_0,
  910. "1.2.0.1", CERT_RDN_IA5_STRING,
  911. strlen(ATTR_0_1), (BYTE *) ATTR_0_1
  912. };
  913. CERT_RDN_ATTR rgAttr1[] = {
  914. "1.2.1.0", CERT_RDN_NUMERIC_STRING,
  915. strlen(ATTR_1_0), (BYTE *) ATTR_1_0,
  916. "1.2.1.1", CERT_RDN_OCTET_STRING,
  917. strlen(ATTR_1_1), (BYTE *) ATTR_1_1,
  918. "1.2.1.2", CERT_RDN_PRINTABLE_STRING,
  919. 0, NULL,
  920. "1.2.1.3", CERT_RDN_ENCODED_BLOB,
  921. sizeof(NullDer), NullDer,
  922. "1.2.1.4", CERT_RDN_ENCODED_BLOB,
  923. sizeof(IntegerDer), IntegerDer
  924. };
  925. CERT_RDN_ATTR rgAttr2[] = {
  926. "1.2.2.0", CERT_RDN_TELETEX_STRING,
  927. strlen(ATTR_2_0), (BYTE *) ATTR_2_0,
  928. "1.2.2.1", CERT_RDN_VIDEOTEX_STRING,
  929. strlen(ATTR_2_1), (BYTE *) ATTR_2_1,
  930. "1.2.2.2", CERT_RDN_GRAPHIC_STRING,
  931. strlen(ATTR_2_2), (BYTE *) ATTR_2_2,
  932. "1.2.2.3", CERT_RDN_VISIBLE_STRING,
  933. strlen(ATTR_2_3), (BYTE *) ATTR_2_3,
  934. "1.2.2.4", CERT_RDN_GENERAL_STRING,
  935. strlen(ATTR_2_4), (BYTE *) ATTR_2_4,
  936. "1.2.2.5", CERT_RDN_BMP_STRING,
  937. wcslen(ATTR_2_5) * 2, (BYTE *) ATTR_2_5,
  938. "1.2.2.6", CERT_RDN_UTF8_STRING,
  939. wcslen(ATTR_2_6) * 2, (BYTE *) ATTR_2_6,
  940. "1.2.2.7", CERT_RDN_UNIVERSAL_STRING,
  941. sizeof(Universal), (BYTE *) Universal
  942. };
  943. CERT_RDN_ATTR rgAttr3[] = {
  944. "1.2.2.2", CERT_RDN_OCTET_STRING,
  945. 0, NULL,
  946. "1.2.2.3", CERT_RDN_NUMERIC_STRING,
  947. 0, NULL,
  948. "1.2.2.4", CERT_RDN_PRINTABLE_STRING,
  949. 0, NULL,
  950. "1.2.2.5", CERT_RDN_TELETEX_STRING,
  951. 0, NULL,
  952. "1.2.2.6", CERT_RDN_VIDEOTEX_STRING,
  953. 0, NULL,
  954. "1.2.2.7", CERT_RDN_IA5_STRING,
  955. 0, NULL,
  956. "1.2.2.8", CERT_RDN_GRAPHIC_STRING,
  957. 0, NULL,
  958. "1.2.2.9", CERT_RDN_VISIBLE_STRING,
  959. 0, NULL,
  960. "1.2.2.10", CERT_RDN_GENERAL_STRING,
  961. 0, NULL,
  962. "1.2.2.11", CERT_RDN_UNIVERSAL_STRING,
  963. 0, NULL,
  964. "1.2.2.12", CERT_RDN_BMP_STRING,
  965. 0, NULL,
  966. "1.2.2.13", CERT_RDN_UTF8_STRING,
  967. 0, NULL
  968. };
  969. CERT_RDN rgRDN[] = {
  970. 2, rgAttr0,
  971. 5, rgAttr1,
  972. 8, rgAttr2,
  973. 12, rgAttr3
  974. };
  975. CERT_NAME_INFO Name = {4, rgRDN};
  976. #define EXT_0 "extension 0 ."
  977. #define EXT_1 "extension 1 .."
  978. #define EXT_2 "extension 2 ..."
  979. #define EXT_3 "extension 3 ...."
  980. CERT_EXTENSION rgExt[] = {
  981. "1.14.0", TRUE, strlen(EXT_0), (BYTE *) EXT_0,
  982. "1.14.1.35.45", FALSE, strlen(EXT_1), (BYTE *) EXT_1,
  983. "1.14.2", TRUE, strlen(EXT_2), (BYTE *) EXT_2,
  984. "1.14.4", FALSE, strlen(EXT_3), (BYTE *) EXT_3
  985. };
  986. CERT_INFO Cert;
  987. BYTE *pbExt = NULL;
  988. if (dwExtLen) {
  989. DWORD i;
  990. if (NULL == (pbExt = (BYTE *) TestAlloc(dwExtLen)))
  991. goto ErrorReturn;
  992. for (i = 0; i < dwExtLen; i++)
  993. pbExt[i] = (BYTE) i;
  994. rgExt[3].Value.cbData = dwExtLen;
  995. rgExt[3].Value.pbData = pbExt;
  996. }
  997. cbNameEncoded = 0;
  998. CryptEncodeObject(
  999. dwCertEncodingType,
  1000. X509_NAME,
  1001. &Name,
  1002. NULL, // pbEncoded
  1003. &cbNameEncoded
  1004. );
  1005. if (cbNameEncoded == 0) {
  1006. PrintLastError("EncodeCert::CryptEncodeObject(cbEncoded == 0)");
  1007. goto ErrorReturn;
  1008. }
  1009. pbNameEncoded = (BYTE *) TestAlloc(cbNameEncoded);
  1010. if (pbNameEncoded == NULL) goto ErrorReturn;
  1011. if (!CryptEncodeObject(
  1012. dwCertEncodingType,
  1013. X509_NAME,
  1014. &Name,
  1015. pbNameEncoded,
  1016. &cbNameEncoded
  1017. )) {
  1018. PrintLastError("EncodeCert::CryptEncodeObject");
  1019. goto ErrorReturn;
  1020. }
  1021. pbIssuerEncoded = EncodeIssuer(&cbIssuerEncoded);
  1022. if (NULL == pbIssuerEncoded)
  1023. goto ErrorReturn;
  1024. memset(&Cert, 0, sizeof(Cert));
  1025. Cert.dwVersion = CERT_V3;
  1026. Cert.SerialNumber.pbData = (BYTE *) &SerialNumber;
  1027. Cert.SerialNumber.cbData = sizeof(SerialNumber);
  1028. Cert.SignatureAlgorithm.pszObjId = "1.2.4.5.898";
  1029. Cert.Issuer.pbData = pbIssuerEncoded;
  1030. Cert.Issuer.cbData = cbIssuerEncoded;
  1031. GetSystemTime(&SystemTime);
  1032. SystemTimeToFileTime(&SystemTime, &Cert.NotBefore);
  1033. SystemTime.wYear++;
  1034. SystemTimeToFileTime(&SystemTime, &Cert.NotAfter);
  1035. Cert.Subject.pbData = pbNameEncoded;
  1036. Cert.Subject.cbData = cbNameEncoded;
  1037. Cert.SubjectPublicKeyInfo.Algorithm.pszObjId = "1.3.4.5.911";
  1038. Cert.SubjectPublicKeyInfo.PublicKey.pbData = (BYTE *) &SubjectPublicKey;
  1039. Cert.SubjectPublicKeyInfo.PublicKey.cbData = sizeof(SubjectPublicKey);
  1040. Cert.IssuerUniqueId.pbData = (BYTE *) ISSUER_UNIQUE_ID;
  1041. Cert.IssuerUniqueId.cbData = strlen(ISSUER_UNIQUE_ID);
  1042. Cert.IssuerUniqueId.cUnusedBits = 5;
  1043. // Cert.SubjectUniqueId = 0
  1044. Cert.cExtension = sizeof(rgExt) / sizeof(rgExt[0]);
  1045. Cert.rgExtension = rgExt;
  1046. cbCertEncoded = 0;
  1047. CryptEncodeObject(
  1048. dwCertEncodingType,
  1049. X509_CERT_TO_BE_SIGNED,
  1050. &Cert,
  1051. NULL, // pbEncoded
  1052. &cbCertEncoded
  1053. );
  1054. if (cbCertEncoded == 0) {
  1055. PrintLastError("EncodeCert::CryptEncodeObject(cbEncoded == 0)");
  1056. goto ErrorReturn;
  1057. }
  1058. pbCertEncoded = (BYTE *) TestAlloc(cbCertEncoded);
  1059. if (pbCertEncoded == NULL) goto ErrorReturn;
  1060. if (!CryptEncodeObject(
  1061. dwCertEncodingType,
  1062. X509_CERT_TO_BE_SIGNED,
  1063. &Cert,
  1064. pbCertEncoded,
  1065. &cbCertEncoded
  1066. )) {
  1067. PrintLastError("EncodeCert::CryptEncodeObject");
  1068. goto ErrorReturn;
  1069. }
  1070. if (!EncodeSignedContent(
  1071. pbCertEncoded,
  1072. cbCertEncoded,
  1073. ppbEncoded,
  1074. pcbEncoded))
  1075. goto ErrorReturn;
  1076. fResult = TRUE;
  1077. goto CommonReturn;
  1078. ErrorReturn:
  1079. *ppbEncoded = NULL;
  1080. *pcbEncoded = 0;
  1081. fResult = FALSE;
  1082. CommonReturn:
  1083. if (pbNameEncoded)
  1084. TestFree(pbNameEncoded);
  1085. if (pbIssuerEncoded)
  1086. TestFree(pbIssuerEncoded);
  1087. if (pbCertEncoded)
  1088. TestFree(pbCertEncoded);
  1089. if (pbExt)
  1090. TestFree(pbExt);
  1091. return fResult;
  1092. }
  1093. static BOOL EncodeCertReq(BYTE **ppbEncoded, DWORD *pcbEncoded)
  1094. {
  1095. BOOL fResult;
  1096. BYTE *pbNameEncoded = NULL;
  1097. DWORD cbNameEncoded;
  1098. BYTE *pbCertReqEncoded = NULL;
  1099. DWORD cbCertReqEncoded;
  1100. BYTE *pbExtEncoded = NULL;
  1101. DWORD cbExtEncoded;
  1102. #define CERT_REQ_0 "Cert Request subject 0"
  1103. #define CERT_REQ_1 "Cert Request subject 1 ...."
  1104. #define CERT_REQ_2 "Cert Request subject 2 ......."
  1105. CERT_RDN_ATTR rgNameAttr[] = {
  1106. "1.2.1.0", CERT_RDN_PRINTABLE_STRING,
  1107. strlen(CERT_REQ_0), (BYTE *) CERT_REQ_0,
  1108. "1.2.1.1", CERT_RDN_PRINTABLE_STRING,
  1109. strlen(CERT_REQ_1), (BYTE *) CERT_REQ_1,
  1110. "1.2.1.2", CERT_RDN_PRINTABLE_STRING,
  1111. strlen(CERT_REQ_2), (BYTE *) CERT_REQ_2
  1112. };
  1113. CERT_RDN rgRDN[] = {
  1114. 1, &rgNameAttr[0],
  1115. 1, &rgNameAttr[1],
  1116. 1, &rgNameAttr[2]
  1117. };
  1118. CERT_NAME_INFO Name = {3, rgRDN};
  1119. BYTE NullDer[] = {0x05, 0x00};
  1120. BYTE IntegerDer[] = {0x02, 0x01, 0x35};
  1121. CRYPT_ATTR_BLOB rgAttrBlob[2] = {
  1122. 2, (BYTE *) NullDer,
  1123. 3, (BYTE *) IntegerDer
  1124. };
  1125. CRYPT_ATTR_BLOB ExtAttrBlob;
  1126. CRYPT_ATTRIBUTE rgAttr[] = {
  1127. szOID_RSA_certExtensions,
  1128. 1, &ExtAttrBlob,
  1129. "1.2.3.4.5.0",
  1130. 1, rgAttrBlob,
  1131. "1.2.1.1.1.1.1.1",
  1132. 2, rgAttrBlob
  1133. };
  1134. #define REQ_EXT_0 "request extension 0 -"
  1135. #define REQ_EXT_1 "request extension 1 --"
  1136. #define REQ_EXT_2 "request extension 2 ---"
  1137. #define REQ_EXT_3 "request extension 3 ----"
  1138. CERT_EXTENSION rgExt[4] = {
  1139. "2.50.0", FALSE, strlen(REQ_EXT_0), (BYTE *) REQ_EXT_0,
  1140. "2.51.1", TRUE, strlen(REQ_EXT_1), (BYTE *) REQ_EXT_1,
  1141. "2.52.2", FALSE, strlen(REQ_EXT_2), (BYTE *) REQ_EXT_2,
  1142. "2.53.3", FALSE, strlen(REQ_EXT_3), (BYTE *) REQ_EXT_3
  1143. };
  1144. CERT_EXTENSIONS Extensions = {4, &rgExt[0]};
  1145. CERT_REQUEST_INFO CertReq;
  1146. cbNameEncoded = 0;
  1147. CryptEncodeObject(
  1148. dwCertEncodingType,
  1149. X509_NAME,
  1150. &Name,
  1151. NULL, // pbEncoded
  1152. &cbNameEncoded
  1153. );
  1154. if (cbNameEncoded == 0) {
  1155. PrintLastError("EncodeCertReq::CryptEncodeObject(cbEncoded == 0)");
  1156. goto ErrorReturn;
  1157. }
  1158. pbNameEncoded = (BYTE *) TestAlloc(cbNameEncoded);
  1159. if (pbNameEncoded == NULL) goto ErrorReturn;
  1160. if (!CryptEncodeObject(
  1161. dwCertEncodingType,
  1162. X509_NAME,
  1163. &Name,
  1164. pbNameEncoded,
  1165. &cbNameEncoded
  1166. )) {
  1167. PrintLastError("EncodeCertReq::CryptEncodeObject");
  1168. goto ErrorReturn;
  1169. }
  1170. cbExtEncoded = 0;
  1171. CryptEncodeObject(
  1172. dwCertEncodingType,
  1173. X509_EXTENSIONS,
  1174. &Extensions,
  1175. NULL, // pbEncoded
  1176. &cbExtEncoded
  1177. );
  1178. if (cbExtEncoded == 0) {
  1179. PrintLastError("EncodeCertReq::CryptEncodeObject(cbEncoded == 0)");
  1180. goto ErrorReturn;
  1181. }
  1182. pbExtEncoded = (BYTE *) TestAlloc(cbExtEncoded);
  1183. if (pbExtEncoded == NULL) goto ErrorReturn;
  1184. if (!CryptEncodeObject(
  1185. dwCertEncodingType,
  1186. X509_EXTENSIONS,
  1187. &Extensions,
  1188. pbExtEncoded,
  1189. &cbExtEncoded
  1190. )) {
  1191. PrintLastError("EncodeCertReq::CryptEncodeObject");
  1192. goto ErrorReturn;
  1193. }
  1194. ExtAttrBlob.pbData = pbExtEncoded;
  1195. ExtAttrBlob.cbData = cbExtEncoded;
  1196. memset(&CertReq, 0, sizeof(CertReq));
  1197. CertReq.dwVersion = 2;
  1198. CertReq.Subject.pbData = pbNameEncoded;
  1199. CertReq.Subject.cbData = cbNameEncoded;
  1200. CertReq.SubjectPublicKeyInfo.Algorithm.pszObjId = "1.3.4.5.911";
  1201. CertReq.SubjectPublicKeyInfo.PublicKey.pbData = (BYTE *) &SubjectPublicKey;
  1202. CertReq.SubjectPublicKeyInfo.PublicKey.cbData = sizeof(SubjectPublicKey);
  1203. CertReq.cAttribute = sizeof(rgAttr) / sizeof(rgAttr[0]);
  1204. CertReq.rgAttribute = rgAttr;
  1205. cbCertReqEncoded = 0;
  1206. CryptEncodeObject(
  1207. dwCertEncodingType,
  1208. X509_CERT_REQUEST_TO_BE_SIGNED,
  1209. &CertReq,
  1210. NULL, // pbEncoded
  1211. &cbCertReqEncoded
  1212. );
  1213. if (cbCertReqEncoded == 0) {
  1214. PrintLastError("EncodeCertReq::CryptEncodeObject(cbEncoded == 0)");
  1215. goto ErrorReturn;
  1216. }
  1217. pbCertReqEncoded = (BYTE *) TestAlloc(cbCertReqEncoded);
  1218. if (pbCertReqEncoded == NULL) goto ErrorReturn;
  1219. if (!CryptEncodeObject(
  1220. dwCertEncodingType,
  1221. X509_CERT_REQUEST_TO_BE_SIGNED,
  1222. &CertReq,
  1223. pbCertReqEncoded,
  1224. &cbCertReqEncoded
  1225. )) {
  1226. PrintLastError("EncodeCertReq::CryptEncodeObject");
  1227. goto ErrorReturn;
  1228. }
  1229. if (!EncodeSignedContent(
  1230. pbCertReqEncoded,
  1231. cbCertReqEncoded,
  1232. ppbEncoded,
  1233. pcbEncoded))
  1234. goto ErrorReturn;
  1235. fResult = TRUE;
  1236. goto CommonReturn;
  1237. ErrorReturn:
  1238. *ppbEncoded = NULL;
  1239. *pcbEncoded = 0;
  1240. fResult = FALSE;
  1241. CommonReturn:
  1242. if (pbExtEncoded)
  1243. TestFree(pbExtEncoded);
  1244. if (pbNameEncoded)
  1245. TestFree(pbNameEncoded);
  1246. if (pbCertReqEncoded)
  1247. TestFree(pbCertReqEncoded);
  1248. return fResult;
  1249. }
  1250. static BOOL EncodeKeygenReq(BYTE **ppbEncoded, DWORD *pcbEncoded)
  1251. {
  1252. BOOL fResult;
  1253. BYTE *pbKeygenReqEncoded = NULL;
  1254. DWORD cbKeygenReqEncoded;
  1255. CERT_KEYGEN_REQUEST_INFO KeygenReq;
  1256. memset(&KeygenReq, 0, sizeof(KeygenReq));
  1257. KeygenReq.dwVersion = CERT_KEYGEN_REQUEST_V1;
  1258. KeygenReq.SubjectPublicKeyInfo.Algorithm.pszObjId = "1.3.4.5.911";
  1259. KeygenReq.SubjectPublicKeyInfo.PublicKey.pbData = (BYTE *) &SubjectPublicKey;
  1260. KeygenReq.SubjectPublicKeyInfo.PublicKey.cbData = sizeof(SubjectPublicKey);
  1261. KeygenReq.pwszChallengeString = L"Keygen Challenge String";
  1262. cbKeygenReqEncoded = 0;
  1263. CryptEncodeObject(
  1264. dwCertEncodingType,
  1265. X509_KEYGEN_REQUEST_TO_BE_SIGNED,
  1266. &KeygenReq,
  1267. NULL, // pbEncoded
  1268. &cbKeygenReqEncoded
  1269. );
  1270. if (cbKeygenReqEncoded == 0) {
  1271. PrintLastError("EncodeKeygenReq::CryptEncodeObject(cbEncoded == 0)");
  1272. goto ErrorReturn;
  1273. }
  1274. pbKeygenReqEncoded = (BYTE *) TestAlloc(cbKeygenReqEncoded);
  1275. if (pbKeygenReqEncoded == NULL) goto ErrorReturn;
  1276. if (!CryptEncodeObject(
  1277. dwCertEncodingType,
  1278. X509_KEYGEN_REQUEST_TO_BE_SIGNED,
  1279. &KeygenReq,
  1280. pbKeygenReqEncoded,
  1281. &cbKeygenReqEncoded
  1282. )) {
  1283. PrintLastError("EncodeKeygenReq::CryptEncodeObject");
  1284. goto ErrorReturn;
  1285. }
  1286. if (!EncodeSignedContent(
  1287. pbKeygenReqEncoded,
  1288. cbKeygenReqEncoded,
  1289. ppbEncoded,
  1290. pcbEncoded))
  1291. goto ErrorReturn;
  1292. fResult = TRUE;
  1293. goto CommonReturn;
  1294. ErrorReturn:
  1295. *ppbEncoded = NULL;
  1296. *pcbEncoded = 0;
  1297. fResult = FALSE;
  1298. CommonReturn:
  1299. if (pbKeygenReqEncoded)
  1300. TestFree(pbKeygenReqEncoded);
  1301. return fResult;
  1302. }
  1303. static BOOL EncodeContentInfo(BYTE **ppbEncoded, DWORD *pcbEncoded)
  1304. {
  1305. BOOL fResult;
  1306. BYTE *pbEncoded = NULL;
  1307. DWORD cbEncoded;
  1308. CRYPT_CONTENT_INFO ContentInfo;
  1309. BYTE rgb0[] = {0x4, 0x5, 0x11, 0x22, 0x33, 0x44, 0x55}; // OCTET STRING
  1310. CRYPT_DER_BLOB Content = {
  1311. sizeof(rgb0), rgb0
  1312. };
  1313. memset(&ContentInfo, 0, sizeof(ContentInfo));
  1314. ContentInfo.pszObjId = "1.2.3.4.5.6.7.8.9.10";
  1315. ContentInfo.Content = Content;
  1316. cbEncoded = 0;
  1317. CryptEncodeObject(
  1318. dwCertEncodingType,
  1319. PKCS_CONTENT_INFO,
  1320. &ContentInfo,
  1321. NULL, // pbEncoded
  1322. &cbEncoded
  1323. );
  1324. if (cbEncoded == 0) {
  1325. PrintLastError("EncodeContentInfo::CryptEncodeObject(cbEncoded == 0)");
  1326. goto ErrorReturn;
  1327. }
  1328. pbEncoded = (BYTE *) TestAlloc(cbEncoded);
  1329. if (pbEncoded == NULL) goto ErrorReturn;
  1330. if (!CryptEncodeObject(
  1331. dwCertEncodingType,
  1332. PKCS_CONTENT_INFO,
  1333. &ContentInfo,
  1334. pbEncoded,
  1335. &cbEncoded
  1336. )) {
  1337. PrintLastError("EncodeContent::CryptEncodeObject");
  1338. goto ErrorReturn;
  1339. }
  1340. fResult = TRUE;
  1341. goto CommonReturn;
  1342. ErrorReturn:
  1343. if (pbEncoded) {
  1344. TestFree(pbEncoded);
  1345. pbEncoded = NULL;
  1346. }
  1347. cbEncoded = 0;
  1348. fResult = FALSE;
  1349. CommonReturn:
  1350. *ppbEncoded = pbEncoded;
  1351. *pcbEncoded = cbEncoded;
  1352. return fResult;
  1353. }
  1354. static LPCSTR FileTimeText(FILETIME *pft)
  1355. {
  1356. static char buf[80];
  1357. FILETIME ftLocal;
  1358. struct tm ctm;
  1359. SYSTEMTIME st;
  1360. FileTimeToLocalFileTime(pft, &ftLocal);
  1361. if (FileTimeToSystemTime(&ftLocal, &st))
  1362. {
  1363. ctm.tm_sec = st.wSecond;
  1364. ctm.tm_min = st.wMinute;
  1365. ctm.tm_hour = st.wHour;
  1366. ctm.tm_mday = st.wDay;
  1367. ctm.tm_mon = st.wMonth-1;
  1368. ctm.tm_year = st.wYear-1900;
  1369. ctm.tm_wday = st.wDayOfWeek;
  1370. ctm.tm_yday = 0;
  1371. ctm.tm_isdst = 0;
  1372. strcpy(buf, asctime(&ctm));
  1373. buf[strlen(buf)-1] = 0;
  1374. }
  1375. else
  1376. sprintf(buf, "<FILETIME %08lX:%08lX>", pft->dwHighDateTime,
  1377. pft->dwLowDateTime);
  1378. return buf;
  1379. }
  1380. #define CROW 16
  1381. static void PrintBytes(LPCSTR pszHdr, BYTE *pb, DWORD cbSize)
  1382. {
  1383. ULONG cb, i;
  1384. while (cbSize > 0)
  1385. {
  1386. printf("%s", pszHdr);
  1387. cb = min(CROW, cbSize);
  1388. cbSize -= cb;
  1389. for (i = 0; i<cb; i++)
  1390. printf(" %02X", pb[i]);
  1391. for (i = cb; i<CROW; i++)
  1392. printf(" ");
  1393. printf(" '");
  1394. for (i = 0; i<cb; i++)
  1395. if (pb[i] >= 0x20 && pb[i] <= 0x7f)
  1396. printf("%c", pb[i]);
  1397. else
  1398. printf(".");
  1399. pb += cb;
  1400. printf("'\n");
  1401. }
  1402. }
  1403. static BOOL DecodeName(BYTE *pbEncoded, DWORD cbEncoded)
  1404. {
  1405. BOOL fResult;
  1406. PCERT_NAME_INFO pInfo = NULL;
  1407. DWORD i,j;
  1408. PCERT_RDN pRDN;
  1409. PCERT_RDN_ATTR pAttr;
  1410. if (NULL == (pInfo = (PCERT_NAME_INFO) TestDecodeObject(
  1411. X509_NAME,
  1412. pbEncoded,
  1413. cbEncoded
  1414. ))) goto ErrorReturn;
  1415. for (i = 0, pRDN = pInfo->rgRDN; i < pInfo->cRDN; i++, pRDN++) {
  1416. for (j = 0, pAttr = pRDN->rgRDNAttr; j < pRDN->cRDNAttr; j++, pAttr++) {
  1417. LPSTR pszObjId = pAttr->pszObjId;
  1418. if (pszObjId == NULL)
  1419. pszObjId = "<NULL OBJID>";
  1420. printf(" [%d,%d] %s ValueType: %d\n",
  1421. i, j, pszObjId, pAttr->dwValueType);
  1422. if (pAttr->Value.cbData)
  1423. PrintBytes(" ", pAttr->Value.pbData, pAttr->Value.cbData);
  1424. else
  1425. printf(" NO Value Bytes\n");
  1426. }
  1427. }
  1428. if (fFormatAllNameStrings) {
  1429. CERT_NAME_BLOB Name;
  1430. DWORD cwsz;
  1431. LPWSTR pwsz;
  1432. DWORD csz;
  1433. LPSTR psz;
  1434. Name.pbData = pbEncoded;
  1435. Name.cbData = cbEncoded;
  1436. #define DELTA_DECRMENT 7
  1437. DWORD dwDelta;
  1438. DWORD rgdwStrType[] = {
  1439. CERT_SIMPLE_NAME_STR,
  1440. CERT_OID_NAME_STR,
  1441. CERT_X500_NAME_STR,
  1442. CERT_SIMPLE_NAME_STR | CERT_NAME_STR_SEMICOLON_FLAG |
  1443. CERT_NAME_STR_NO_PLUS_FLAG | CERT_NAME_STR_NO_QUOTING_FLAG,
  1444. CERT_X500_NAME_STR | CERT_NAME_STR_NO_PLUS_FLAG |
  1445. CERT_NAME_STR_NO_QUOTING_FLAG,
  1446. CERT_X500_NAME_STR | CERT_NAME_STR_CRLF_FLAG,
  1447. 0
  1448. };
  1449. DWORD *pdwStrType;
  1450. for (pdwStrType = rgdwStrType, dwDelta = DELTA_DECRMENT; *pdwStrType;
  1451. pdwStrType++, dwDelta += DELTA_DECRMENT) {
  1452. printf("\nCertNameToStrW(dwStrType == 0x%x)\n", *pdwStrType);
  1453. cwsz = CertNameToStrW(
  1454. dwCertEncodingType,
  1455. &Name,
  1456. *pdwStrType,
  1457. NULL, // pwsz
  1458. 0); // cwsz
  1459. if (pwsz = (LPWSTR) TestAlloc(cwsz * sizeof(WCHAR))) {
  1460. CertNameToStrW(
  1461. dwCertEncodingType,
  1462. &Name,
  1463. *pdwStrType,
  1464. pwsz,
  1465. cwsz);
  1466. printf(" %S\n", pwsz);
  1467. if (cwsz > dwDelta) {
  1468. CertNameToStrW(
  1469. dwCertEncodingType,
  1470. &Name,
  1471. *pdwStrType,
  1472. pwsz,
  1473. cwsz - dwDelta);
  1474. printf("Delta[-%d]\n", dwDelta);
  1475. printf(" %S\n", pwsz);
  1476. }
  1477. TestFree(pwsz);
  1478. }
  1479. }
  1480. for (pdwStrType = rgdwStrType, dwDelta = DELTA_DECRMENT; *pdwStrType;
  1481. pdwStrType++, dwDelta += DELTA_DECRMENT) {
  1482. printf("\nCertNameToStrA(dwStrType == 0x%x)\n", *pdwStrType);
  1483. csz = CertNameToStrA(
  1484. dwCertEncodingType,
  1485. &Name,
  1486. *pdwStrType,
  1487. NULL, // psz
  1488. 0); // csz
  1489. if (psz = (LPSTR) TestAlloc(csz)) {
  1490. CertNameToStrA(
  1491. dwCertEncodingType,
  1492. &Name,
  1493. *pdwStrType,
  1494. psz,
  1495. csz);
  1496. printf(" %s\n", psz);
  1497. if (csz > dwDelta) {
  1498. csz = CertNameToStrA(
  1499. dwCertEncodingType,
  1500. &Name,
  1501. *pdwStrType,
  1502. psz,
  1503. csz - dwDelta);
  1504. printf("Delta[-%d]\n", dwDelta);
  1505. if (1 >= csz) {
  1506. DWORD dwErr = GetLastError();
  1507. printf(" No CertNameToStrA string, LastError: 0x%x (%d) \n",
  1508. dwErr, dwErr);
  1509. } else
  1510. printf(" %s\n", psz);
  1511. }
  1512. TestFree(psz);
  1513. }
  1514. }
  1515. } else if (fFormatNameStrings) {
  1516. CERT_NAME_BLOB Name;
  1517. DWORD cwsz;
  1518. LPWSTR pwsz;
  1519. Name.pbData = pbEncoded;
  1520. Name.cbData = cbEncoded;
  1521. cwsz = CertNameToStrW(
  1522. dwCertEncodingType,
  1523. &Name,
  1524. CERT_X500_NAME_STR,
  1525. NULL, // pwsz
  1526. 0); // cwsz
  1527. if (pwsz = (LPWSTR) TestAlloc(cwsz * sizeof(WCHAR))) {
  1528. CertNameToStrW(
  1529. dwCertEncodingType,
  1530. &Name,
  1531. CERT_X500_NAME_STR,
  1532. pwsz,
  1533. cwsz);
  1534. printf(" %S\n", pwsz);
  1535. TestFree(pwsz);
  1536. }
  1537. }
  1538. fResult = TRUE;
  1539. goto CommonReturn;
  1540. ErrorReturn:
  1541. fResult = FALSE;
  1542. CommonReturn:
  1543. if (pInfo)
  1544. TestFree(pInfo);
  1545. return fResult;
  1546. }
  1547. static void DecodeSignedContent(
  1548. BYTE *pbEncoded,
  1549. DWORD cbEncoded,
  1550. LPCSTR lpszToBeSignedStructType,
  1551. DWORD cbToBeSignedStruct
  1552. )
  1553. {
  1554. BOOL fResult;
  1555. PCERT_INFO pInfo = NULL;
  1556. LPSTR pszObjId;
  1557. DWORD cbInfo;
  1558. PCERT_SIGNED_CONTENT_INFO pCertEncoding;
  1559. if (NULL == (pCertEncoding = (PCERT_SIGNED_CONTENT_INFO) TestDecodeObject(
  1560. X509_CERT,
  1561. pbEncoded,
  1562. cbEncoded
  1563. ))) goto ErrorReturn;
  1564. // Decode the ToBeSigned
  1565. cbInfo = 0x12345678;
  1566. if (!CryptDecodeObject(
  1567. dwCertEncodingType,
  1568. lpszToBeSignedStructType,
  1569. pCertEncoding->ToBeSigned.pbData,
  1570. pCertEncoding->ToBeSigned.cbData,
  1571. dwDecodeObjectFlags | CRYPT_DECODE_TO_BE_SIGNED_FLAG,
  1572. NULL, // pvInfo
  1573. &cbInfo))
  1574. PrintLastError("CryptDecodeObject(TO_BE_SIGNED_FLAG)");
  1575. else if (cbInfo != cbToBeSignedStruct)
  1576. printf("failed => CryptDecodeObject(TO_BE_SIGNED_FLAG) returned cbInfo = %d, not %d\n",
  1577. cbInfo, cbToBeSignedStruct);
  1578. cbInfo = 0x12345678;
  1579. if (!CryptDecodeObject(
  1580. dwCertEncodingType,
  1581. lpszToBeSignedStructType,
  1582. pCertEncoding->ToBeSigned.pbData,
  1583. pCertEncoding->ToBeSigned.cbData,
  1584. dwDecodeObjectFlags,
  1585. NULL, // pvInfo
  1586. &cbInfo))
  1587. PrintLastError("CryptDecodeObject(ToBeSigned, without flag)");
  1588. else if (cbInfo != cbToBeSignedStruct)
  1589. printf("failed => CryptDecodeObject(ToBeSigned without flag) returned cbInfo = %d, not %d\n",
  1590. cbInfo, cbToBeSignedStruct);
  1591. pszObjId = pCertEncoding->SignatureAlgorithm.pszObjId;
  1592. if (pszObjId == NULL)
  1593. pszObjId = "<NULL OBJID>";
  1594. printf("Content SignatureAlgorithm:: %s\n", pszObjId);
  1595. if (pCertEncoding->SignatureAlgorithm.Parameters.cbData) {
  1596. printf("Content SignatureAlgorithm.Parameters::\n");
  1597. PrintBytes(" ", pCertEncoding->SignatureAlgorithm.Parameters.pbData,
  1598. pCertEncoding->SignatureAlgorithm.Parameters.cbData);
  1599. }
  1600. if (pCertEncoding->Signature.cbData) {
  1601. printf("Content Signature::\n");
  1602. PrintBytes(" ", pCertEncoding->Signature.pbData,
  1603. pCertEncoding->Signature.cbData);
  1604. } else
  1605. printf("Content Signature:: NONE\n");
  1606. printf("Content Length:: %d\n", pCertEncoding->ToBeSigned.cbData);
  1607. fResult = TRUE;
  1608. goto CommonReturn;
  1609. ErrorReturn:
  1610. fResult = FALSE;
  1611. CommonReturn:
  1612. if (pCertEncoding)
  1613. TestFree(pCertEncoding);
  1614. }
  1615. static void PrintExtensions(DWORD cExt, PCERT_EXTENSION pExt)
  1616. {
  1617. DWORD i;
  1618. for (i = 0; i < cExt; i++, pExt++) {
  1619. LPSTR pszObjId = pExt->pszObjId;
  1620. if (pszObjId == NULL)
  1621. pszObjId = "<NULL OBJID>";
  1622. LPSTR pszCritical = pExt->fCritical ? "TRUE" : "FALSE";
  1623. printf(" [%d] %s Critical: %s\n", i, pszObjId, pszCritical);
  1624. if (pExt->Value.cbData)
  1625. PrintBytes(" ", pExt->Value.pbData, pExt->Value.cbData);
  1626. else
  1627. printf(" NO Value Bytes\n");
  1628. }
  1629. }
  1630. static void DecodeExtensions(BYTE *pbEncoded, DWORD cbEncoded)
  1631. {
  1632. PCERT_EXTENSIONS pInfo;
  1633. if (NULL == (pInfo = (PCERT_EXTENSIONS) TestDecodeObject(
  1634. X509_EXTENSIONS,
  1635. pbEncoded,
  1636. cbEncoded
  1637. ))) goto ErrorReturn;
  1638. PrintExtensions(pInfo->cExtension, pInfo->rgExtension);
  1639. goto CommonReturn;
  1640. ErrorReturn:
  1641. CommonReturn:
  1642. if (pInfo)
  1643. TestFree(pInfo);
  1644. }
  1645. static void PrintAttributes(DWORD cAttr, PCRYPT_ATTRIBUTE pAttr)
  1646. {
  1647. DWORD i;
  1648. DWORD j;
  1649. for (i = 0; i < cAttr; i++, pAttr++) {
  1650. DWORD cValue = pAttr->cValue;
  1651. PCRYPT_ATTR_BLOB pValue = pAttr->rgValue;
  1652. LPSTR pszObjId = pAttr->pszObjId;
  1653. if (pszObjId == NULL)
  1654. pszObjId = "<NULL OBJID>";
  1655. if (cValue) {
  1656. for (j = 0; j < cValue; j++, pValue++) {
  1657. printf(" [%d,%d] %s\n", i, j, pszObjId);
  1658. if (pValue->cbData) {
  1659. PrintBytes(" ", pValue->pbData, pValue->cbData);
  1660. if (strcmp(pszObjId, szOID_RSA_certExtensions) == 0 ||
  1661. strcmp(pszObjId, SPC_CERT_EXTENSIONS_OBJID) == 0) {
  1662. printf(" Extensions::\n");
  1663. DecodeExtensions(pValue->pbData, pValue->cbData);
  1664. }
  1665. } else
  1666. printf(" NO Value Bytes\n");
  1667. }
  1668. } else
  1669. printf(" [%d] %s :: No Values\n", i, pszObjId);
  1670. }
  1671. }
  1672. //+-------------------------------------------------------------------------
  1673. // Write the public key to the file
  1674. //--------------------------------------------------------------------------
  1675. BOOL WritePublicKeyToFile(
  1676. LPCSTR pszFileName,
  1677. PBYTE pbPub,
  1678. DWORD cbPub
  1679. )
  1680. {
  1681. FILE *stream;
  1682. DWORD i;
  1683. if (NULL == (stream = fopen(pszFileName, "w"))) {
  1684. printf("Failed to open %s for writing public key\n", pszFileName);
  1685. return FALSE;
  1686. }
  1687. for (i = 0; i < cbPub; i++) {
  1688. fprintf(stream, "0x%02X", pbPub[i]);
  1689. if (i != (cbPub - 1))
  1690. fprintf(stream, ",");
  1691. if ((i + 1) % 16 == 0)
  1692. fprintf(stream, "\n");
  1693. }
  1694. fprintf(stream, "\n");
  1695. fclose(stream);
  1696. return TRUE;
  1697. }
  1698. void WriteBytesToFile(
  1699. IN FILE *stream,
  1700. IN const BYTE *pb,
  1701. IN DWORD cb
  1702. )
  1703. {
  1704. DWORD i;
  1705. fprintf(stream, "= {\n");
  1706. for (i = 0; i < cb; i++) {
  1707. if ((i % 8) == 0)
  1708. fprintf(stream, " ");
  1709. fprintf(stream, "0x%02X", pb[i]);
  1710. if (i == (cb - 1))
  1711. fprintf(stream, "\n");
  1712. else {
  1713. fprintf(stream, ",");
  1714. if ((i + 1) % 8 == 0)
  1715. fprintf(stream, "\n");
  1716. else
  1717. fprintf(stream, " ");
  1718. }
  1719. }
  1720. fprintf(stream, "};\n\n");
  1721. }
  1722. //+-------------------------------------------------------------------------
  1723. // Write the Name and PublicKeyInfo to the file
  1724. //--------------------------------------------------------------------------
  1725. BOOL WritePublicKeyInfoToFile(
  1726. LPCSTR pszFileName,
  1727. PCERT_INFO pCertInfo
  1728. )
  1729. {
  1730. BOOL fResult;
  1731. FILE *stream;
  1732. LPWSTR pwszName = NULL;
  1733. DWORD cchName;
  1734. BYTE *pbEncoded = NULL;
  1735. DWORD cbEncoded;
  1736. if (NULL == (stream = fopen(pszFileName, "w"))) {
  1737. printf("Failed to open %s for writing PublicKeyInfo\n", pszFileName);
  1738. return FALSE;
  1739. }
  1740. // Output the Subject X500 name string as a comment
  1741. cchName = CertNameToStrW(
  1742. X509_ASN_ENCODING,
  1743. &pCertInfo->Subject,
  1744. CERT_X500_NAME_STR,
  1745. NULL, // pwsz
  1746. 0 // cch
  1747. );
  1748. if (NULL == (pwszName = (LPWSTR) TestAlloc(cchName * sizeof(WCHAR))))
  1749. goto ErrorReturn;
  1750. cchName = CertNameToStrW(
  1751. X509_ASN_ENCODING,
  1752. &pCertInfo->Subject,
  1753. CERT_NAME_STR_REVERSE_FLAG |
  1754. CERT_X500_NAME_STR,
  1755. pwszName,
  1756. cchName
  1757. );
  1758. fprintf(stream, "// Name:: <%S>\n", pwszName);
  1759. // Write the encoded Subject Name bytes
  1760. WriteBytesToFile(stream, pCertInfo->Subject.pbData,
  1761. pCertInfo->Subject.cbData);
  1762. fprintf(stream, "// PublicKeyInfo\n");
  1763. // Encode and write the PublicKeyInfo bytes
  1764. if (!CryptEncodeObject(
  1765. X509_ASN_ENCODING,
  1766. X509_PUBLIC_KEY_INFO,
  1767. &pCertInfo->SubjectPublicKeyInfo,
  1768. NULL, // pbEncoded
  1769. &cbEncoded
  1770. )) {
  1771. PrintLastError("CryptEncodeObject(X509_PUBLIC_KEY_INFO)");
  1772. goto ErrorReturn;
  1773. }
  1774. pbEncoded = (BYTE *) TestAlloc(cbEncoded);
  1775. if (pbEncoded == NULL) goto ErrorReturn;
  1776. if (!CryptEncodeObject(
  1777. X509_ASN_ENCODING,
  1778. X509_PUBLIC_KEY_INFO,
  1779. &pCertInfo->SubjectPublicKeyInfo,
  1780. pbEncoded,
  1781. &cbEncoded
  1782. )) {
  1783. PrintLastError("CryptEncodeObject(X509_PUBLIC_KEY_INFO)");
  1784. goto ErrorReturn;
  1785. }
  1786. WriteBytesToFile(stream, pbEncoded, cbEncoded);
  1787. fprintf(stream, "\n");
  1788. fResult = TRUE;
  1789. CommonReturn:
  1790. fclose(stream);
  1791. TestFree(pwszName);
  1792. TestFree(pbEncoded);
  1793. return fResult;
  1794. ErrorReturn:
  1795. fResult = FALSE;
  1796. goto CommonReturn;
  1797. }
  1798. static BOOL DecodeCert(BYTE *pbEncoded, DWORD cbEncoded)
  1799. {
  1800. BOOL fResult;
  1801. PCERT_INFO pInfo = NULL;
  1802. DWORD cbInfo;
  1803. LPSTR pszObjId;
  1804. if (NULL == (pInfo = (PCERT_INFO) TestDecodeObject(
  1805. X509_CERT_TO_BE_SIGNED,
  1806. pbEncoded,
  1807. cbEncoded,
  1808. &cbInfo
  1809. ))) goto ErrorReturn;
  1810. printf("Version:: %d\n", pInfo->dwVersion);
  1811. {
  1812. DWORD cb;
  1813. BYTE *pb;
  1814. printf("SerialNumber::");
  1815. for (cb = pInfo->SerialNumber.cbData,
  1816. pb = pInfo->SerialNumber.pbData + (cb - 1);
  1817. cb > 0; cb--, pb--) {
  1818. printf(" %02X", *pb);
  1819. }
  1820. printf("\n");
  1821. }
  1822. pszObjId = pInfo->SignatureAlgorithm.pszObjId;
  1823. if (pszObjId == NULL)
  1824. pszObjId = "<NULL OBJID>";
  1825. printf("SignatureAlgorithm:: %s\n", pszObjId);
  1826. if (pInfo->SignatureAlgorithm.Parameters.cbData) {
  1827. printf("SignatureAlgorithm.Parameters::\n");
  1828. PrintBytes(" ", pInfo->SignatureAlgorithm.Parameters.pbData,
  1829. pInfo->SignatureAlgorithm.Parameters.cbData);
  1830. }
  1831. printf("Issuer::\n");
  1832. DecodeName(pInfo->Issuer.pbData, pInfo->Issuer.cbData);
  1833. printf("NotBefore:: %s\n", FileTimeText(&pInfo->NotBefore));
  1834. printf("NotAfter:: %s\n", FileTimeText(&pInfo->NotAfter));
  1835. printf("Subject::\n");
  1836. DecodeName(pInfo->Subject.pbData, pInfo->Subject.cbData);
  1837. pszObjId = pInfo->SubjectPublicKeyInfo.Algorithm.pszObjId;
  1838. if (pszObjId == NULL)
  1839. pszObjId = "<NULL OBJID>";
  1840. printf("SubjectPublicKeyInfo.Algorithm:: %s\n", pszObjId);
  1841. if (pInfo->SubjectPublicKeyInfo.Algorithm.Parameters.cbData) {
  1842. printf("SubjectPublicKeyInfo.Algorithm.Parameters::\n");
  1843. PrintBytes(" ",
  1844. pInfo->SubjectPublicKeyInfo.Algorithm.Parameters.pbData,
  1845. pInfo->SubjectPublicKeyInfo.Algorithm.Parameters.cbData);
  1846. }
  1847. printf("SubjectPublicKeyInfo.PublicKey");
  1848. if (pInfo->SubjectPublicKeyInfo.PublicKey.cUnusedBits)
  1849. printf(" (UnusedBits: %d)",
  1850. pInfo->SubjectPublicKeyInfo.PublicKey.cUnusedBits);
  1851. printf("::\n");
  1852. if (pInfo->SubjectPublicKeyInfo.PublicKey.cbData) {
  1853. PrintBytes(" ", pInfo->SubjectPublicKeyInfo.PublicKey.pbData,
  1854. pInfo->SubjectPublicKeyInfo.PublicKey.cbData);
  1855. } else
  1856. printf(" No public key\n");
  1857. if (pszPublicKeyFilename) {
  1858. if (fWritePublicKeyInfo)
  1859. WritePublicKeyInfoToFile(pszPublicKeyFilename, pInfo);
  1860. else
  1861. WritePublicKeyToFile(pszPublicKeyFilename,
  1862. pInfo->SubjectPublicKeyInfo.PublicKey.pbData,
  1863. pInfo->SubjectPublicKeyInfo.PublicKey.cbData
  1864. );
  1865. }
  1866. if (pszReadFilename == NULL) {
  1867. // Verify that the public key was properly encoded/decoded
  1868. CERT_PUBLIC_KEY_INFO PublicKeyInfo;
  1869. memset(&PublicKeyInfo, 0, sizeof(PublicKeyInfo));
  1870. PublicKeyInfo.Algorithm.pszObjId = "1.3.4.5.911";
  1871. PublicKeyInfo.PublicKey.pbData = (BYTE *) &SubjectPublicKey;
  1872. PublicKeyInfo.PublicKey.cbData = sizeof(SubjectPublicKey);
  1873. if (!CertComparePublicKeyInfo(
  1874. dwCertEncodingType,
  1875. &PublicKeyInfo,
  1876. &pInfo->SubjectPublicKeyInfo))
  1877. PrintLastError("CertComparePublicKeyInfo");
  1878. }
  1879. if (pInfo->IssuerUniqueId.cbData) {
  1880. printf("IssuerUniqueId");
  1881. if (pInfo->IssuerUniqueId.cUnusedBits)
  1882. printf(" (UnusedBits: %d)", pInfo->IssuerUniqueId.cUnusedBits);
  1883. printf("::\n");
  1884. PrintBytes(" ", pInfo->IssuerUniqueId.pbData,
  1885. pInfo->IssuerUniqueId.cbData);
  1886. }
  1887. if (pInfo->SubjectUniqueId.cbData) {
  1888. printf("SubjectUniqueId");
  1889. if (pInfo->SubjectUniqueId.cUnusedBits)
  1890. printf(" (UnusedBits: %d)", pInfo->SubjectUniqueId.cUnusedBits);
  1891. printf("::\n");
  1892. PrintBytes(" ", pInfo->SubjectUniqueId.pbData,
  1893. pInfo->SubjectUniqueId.cbData);
  1894. }
  1895. if (pInfo->cExtension == 0)
  1896. printf("Extensions:: NONE\n");
  1897. else {
  1898. printf("Extensions::\n");
  1899. PrintExtensions(pInfo->cExtension, pInfo->rgExtension);
  1900. }
  1901. DecodeSignedContent(pbEncoded, cbEncoded, X509_CERT_TO_BE_SIGNED,
  1902. cbInfo);
  1903. fResult = TRUE;
  1904. goto CommonReturn;
  1905. ErrorReturn:
  1906. fResult = FALSE;
  1907. CommonReturn:
  1908. if (pInfo)
  1909. TestFree(pInfo);
  1910. return fResult;
  1911. }
  1912. static BOOL DecodeCertReq(BYTE *pbEncoded, DWORD cbEncoded)
  1913. {
  1914. BOOL fResult;
  1915. PCERT_REQUEST_INFO pInfo = NULL;
  1916. DWORD cbInfo;
  1917. LPSTR pszObjId;
  1918. if (NULL == (pInfo = (PCERT_REQUEST_INFO) TestDecodeObject(
  1919. X509_CERT_REQUEST_TO_BE_SIGNED,
  1920. pbEncoded,
  1921. cbEncoded,
  1922. &cbInfo
  1923. ))) goto ErrorReturn;
  1924. printf("Version:: %d\n", pInfo->dwVersion);
  1925. printf("Subject::\n");
  1926. DecodeName(pInfo->Subject.pbData, pInfo->Subject.cbData);
  1927. pszObjId = pInfo->SubjectPublicKeyInfo.Algorithm.pszObjId;
  1928. if (pszObjId == NULL)
  1929. pszObjId = "<NULL OBJID>";
  1930. printf("SubjectPublicKeyInfo.Algorithm:: %s\n", pszObjId);
  1931. if (pInfo->SubjectPublicKeyInfo.Algorithm.Parameters.cbData) {
  1932. printf("SubjectPublicKeyInfo.Algorithm.Parameters::\n");
  1933. PrintBytes(" ",
  1934. pInfo->SubjectPublicKeyInfo.Algorithm.Parameters.pbData,
  1935. pInfo->SubjectPublicKeyInfo.Algorithm.Parameters.cbData);
  1936. }
  1937. printf("SubjectPublicKeyInfo.PublicKey");
  1938. if (pInfo->SubjectPublicKeyInfo.PublicKey.cUnusedBits)
  1939. printf(" (UnusedBits: %d)",
  1940. pInfo->SubjectPublicKeyInfo.PublicKey.cUnusedBits);
  1941. printf("::\n");
  1942. if (pInfo->SubjectPublicKeyInfo.PublicKey.cbData) {
  1943. PrintBytes(" ", pInfo->SubjectPublicKeyInfo.PublicKey.pbData,
  1944. pInfo->SubjectPublicKeyInfo.PublicKey.cbData);
  1945. } else
  1946. printf(" No public key\n");
  1947. if (pInfo->cAttribute == 0)
  1948. printf("Attributes:: NONE\n");
  1949. else {
  1950. printf("Attributes::\n");
  1951. PrintAttributes(pInfo->cAttribute, pInfo->rgAttribute);
  1952. }
  1953. DecodeSignedContent(pbEncoded, cbEncoded, X509_CERT_REQUEST_TO_BE_SIGNED,
  1954. cbInfo);
  1955. fResult = TRUE;
  1956. goto CommonReturn;
  1957. ErrorReturn:
  1958. fResult = FALSE;
  1959. CommonReturn:
  1960. if (pInfo)
  1961. TestFree(pInfo);
  1962. return fResult;
  1963. }
  1964. static BOOL DecodeKeygenReq(BYTE *pbEncoded, DWORD cbEncoded)
  1965. {
  1966. BOOL fResult;
  1967. PCERT_KEYGEN_REQUEST_INFO pInfo = NULL;
  1968. DWORD cbInfo;
  1969. LPSTR pszObjId;
  1970. if (NULL == (pInfo = (PCERT_KEYGEN_REQUEST_INFO) TestDecodeObject(
  1971. X509_KEYGEN_REQUEST_TO_BE_SIGNED,
  1972. pbEncoded,
  1973. cbEncoded,
  1974. &cbInfo
  1975. ))) goto ErrorReturn;
  1976. printf("Version:: %d\n", pInfo->dwVersion);
  1977. pszObjId = pInfo->SubjectPublicKeyInfo.Algorithm.pszObjId;
  1978. if (pszObjId == NULL)
  1979. pszObjId = "<NULL OBJID>";
  1980. printf("SubjectPublicKeyInfo.Algorithm:: %s\n", pszObjId);
  1981. if (pInfo->SubjectPublicKeyInfo.Algorithm.Parameters.cbData) {
  1982. printf("SubjectPublicKeyInfo.Algorithm.Parameters::\n");
  1983. PrintBytes(" ",
  1984. pInfo->SubjectPublicKeyInfo.Algorithm.Parameters.pbData,
  1985. pInfo->SubjectPublicKeyInfo.Algorithm.Parameters.cbData);
  1986. }
  1987. printf("SubjectPublicKeyInfo.PublicKey");
  1988. if (pInfo->SubjectPublicKeyInfo.PublicKey.cUnusedBits)
  1989. printf(" (UnusedBits: %d)",
  1990. pInfo->SubjectPublicKeyInfo.PublicKey.cUnusedBits);
  1991. printf("::\n");
  1992. if (pInfo->SubjectPublicKeyInfo.PublicKey.cbData) {
  1993. PrintBytes(" ", pInfo->SubjectPublicKeyInfo.PublicKey.pbData,
  1994. pInfo->SubjectPublicKeyInfo.PublicKey.cbData);
  1995. } else
  1996. printf(" No public key\n");
  1997. printf("ChallengeString:: %S\n", pInfo->pwszChallengeString);
  1998. DecodeSignedContent(pbEncoded, cbEncoded, X509_KEYGEN_REQUEST_TO_BE_SIGNED,
  1999. cbInfo);
  2000. fResult = TRUE;
  2001. goto CommonReturn;
  2002. ErrorReturn:
  2003. fResult = FALSE;
  2004. CommonReturn:
  2005. if (pInfo)
  2006. TestFree(pInfo);
  2007. return fResult;
  2008. }
  2009. static BOOL DecodeContentInfo(BYTE *pbEncoded, DWORD cbEncoded)
  2010. {
  2011. BOOL fResult;
  2012. PCRYPT_CONTENT_INFO pInfo = NULL;
  2013. LPSTR pszObjId;
  2014. if (NULL == (pInfo = (PCRYPT_CONTENT_INFO) TestDecodeObject(
  2015. PKCS_CONTENT_INFO,
  2016. pbEncoded,
  2017. cbEncoded
  2018. ))) goto ErrorReturn;
  2019. pszObjId = pInfo->pszObjId;
  2020. if (pszObjId == NULL)
  2021. pszObjId = "<NULL OBJID>";
  2022. printf("ContentType:: %s\n", pszObjId);
  2023. if (pInfo->Content.cbData) {
  2024. printf("Content::\n");
  2025. PrintBytes(" ", pInfo->Content.pbData, pInfo->Content.cbData);
  2026. } else
  2027. printf("NO Content\n");
  2028. fResult = TRUE;
  2029. goto CommonReturn;
  2030. ErrorReturn:
  2031. fResult = FALSE;
  2032. CommonReturn:
  2033. if (pInfo)
  2034. TestFree(pInfo);
  2035. return fResult;
  2036. }
  2037. static void TestCompareIntegerBlob()
  2038. {
  2039. BYTE bZero = 0;
  2040. BYTE bFF = 0xFF;
  2041. BYTE rgbLeadZero1[] = {0x12, 0x34, 0x56, 0x87, 0, 0};
  2042. BYTE rgbLeadZero2[] = {0x12, 0x34, 0x56, 0x87, 0, 0, 0, 0};
  2043. BYTE rgbLeadFF1[] = {0x12, 0x34, 0x56, 0x87, 0xFF, 0xFF};
  2044. BYTE rgbLeadFF2[] = {0x12, 0x34, 0x56, 0x87};
  2045. CRYPT_INTEGER_BLOB Int1;
  2046. CRYPT_INTEGER_BLOB Int2;
  2047. Int1.pbData = &bZero;
  2048. Int1.cbData = sizeof(bZero);
  2049. Int2 = Int1;
  2050. if (!CertCompareIntegerBlob(&Int1, &Int2))
  2051. printf("Failed => Compare of Zero == Zero\n");
  2052. Int1.pbData = &bFF;
  2053. Int1.cbData = sizeof(bFF);
  2054. Int2 = Int1;
  2055. if (!CertCompareIntegerBlob(&Int1, &Int2))
  2056. printf("Failed => Compare of FF == FF\n");
  2057. Int1.pbData = &bZero;
  2058. Int1.cbData = sizeof(bZero);
  2059. if (CertCompareIntegerBlob(&Int1, &Int2))
  2060. printf("Failed => Compare of Zero != FF\n");
  2061. Int1.pbData = rgbLeadZero1;
  2062. Int1.cbData = sizeof(rgbLeadZero1);
  2063. Int2.pbData = rgbLeadZero2;
  2064. Int2.cbData = sizeof(rgbLeadZero2);
  2065. if (!CertCompareIntegerBlob(&Int1, &Int2))
  2066. printf("Failed => Compare of Leading Zeroes\n");
  2067. Int1.pbData = rgbLeadFF1;
  2068. Int1.cbData = sizeof(rgbLeadFF1);
  2069. Int2.pbData = rgbLeadFF2;
  2070. Int2.cbData = sizeof(rgbLeadFF2);
  2071. if (!CertCompareIntegerBlob(&Int1, &Int2))
  2072. printf("Failed => Compare of Leading FFs\n");
  2073. Int1.pbData = rgbLeadZero1;
  2074. Int1.cbData = sizeof(rgbLeadZero1);
  2075. if (CertCompareIntegerBlob(&Int1, &Int2))
  2076. printf("Failed => Compare of Leading Zeroes != Leading FFs\n");
  2077. }
  2078. static BOOL EncodeCrl(BYTE **ppbEncoded, DWORD *pcbEncoded)
  2079. {
  2080. BOOL fResult;
  2081. BYTE *pbNameEncoded = NULL;
  2082. DWORD cbNameEncoded;
  2083. BYTE *pbCrlEncoded = NULL;
  2084. DWORD cbCrlEncoded;
  2085. DWORD SerialNumber0[2] = {0x12345678, 0x33445566};
  2086. DWORD SerialNumber1[1] = {0x12345678};
  2087. SYSTEMTIME SystemTime;
  2088. #define CRL_ATTR_0_0 "attr 0_0 printable"
  2089. #define CRL_ATTR_0_1 "attr 0_1 IA5"
  2090. #define CRL_ATTR_1_0 "attr 1_0 numeric"
  2091. #define CRL_ATTR_1_1 "attr 1_1 octet"
  2092. CERT_RDN_ATTR rgAttr0[] = {
  2093. "1.2.3.4.5.0.0.0", CERT_RDN_PRINTABLE_STRING,
  2094. strlen(CRL_ATTR_0_0), (BYTE *) CRL_ATTR_0_0,
  2095. "1.2.3.4.5.0.1.1.1", CERT_RDN_IA5_STRING,
  2096. strlen(CRL_ATTR_0_1), (BYTE *) CRL_ATTR_0_1
  2097. };
  2098. CERT_RDN_ATTR rgAttr1[] = {
  2099. "1.2.3.4.5.1.0", CERT_RDN_NUMERIC_STRING,
  2100. strlen(CRL_ATTR_1_0), (BYTE *) CRL_ATTR_1_0,
  2101. "1.2.3.4.5.1.1", CERT_RDN_OCTET_STRING,
  2102. strlen(CRL_ATTR_1_1), (BYTE *) CRL_ATTR_1_1,
  2103. "1.2.3.4.5.2", CERT_RDN_PRINTABLE_STRING,
  2104. 0, NULL
  2105. };
  2106. CERT_RDN rgRDN[] = {
  2107. 2, rgAttr0,
  2108. 3, rgAttr1,
  2109. };
  2110. CERT_NAME_INFO Name = {2, rgRDN};
  2111. #define CRL_EXT_0 "extension 0 ."
  2112. #define CRL_EXT_1 "extension 1 .."
  2113. #define CRL_EXT_2 "extension 2 ..."
  2114. #define CRL_EXT_3 "extension 3 ...."
  2115. CERT_EXTENSION rgExt[] = {
  2116. "1.14.89990", FALSE, strlen(CRL_EXT_0), (BYTE *) CRL_EXT_0,
  2117. "1.14.89991", TRUE, strlen(CRL_EXT_1), (BYTE *) CRL_EXT_1,
  2118. "1.14.89992", FALSE, strlen(CRL_EXT_2), (BYTE *) CRL_EXT_2,
  2119. "1.14.89993", FALSE, strlen(CRL_EXT_3), (BYTE *) CRL_EXT_3
  2120. };
  2121. CRL_INFO Crl;
  2122. BYTE *pbExt = NULL;
  2123. if (dwExtLen) {
  2124. DWORD i;
  2125. if (NULL == (pbExt = (BYTE *) TestAlloc(dwExtLen)))
  2126. goto ErrorReturn;
  2127. for (i = 0; i < dwExtLen; i++)
  2128. pbExt[i] = (BYTE) i;
  2129. rgExt[3].Value.cbData = dwExtLen;
  2130. rgExt[3].Value.pbData = pbExt;
  2131. }
  2132. CRL_ENTRY rgCrlEntry[2];
  2133. TestCompareIntegerBlob();
  2134. cbNameEncoded = 0;
  2135. CryptEncodeObject(
  2136. dwCertEncodingType,
  2137. X509_NAME,
  2138. &Name,
  2139. NULL, // pbEncoded
  2140. &cbNameEncoded
  2141. );
  2142. if (cbNameEncoded == 0) {
  2143. PrintLastError("EncodeCrl::CryptEncodeObject(cbEncoded == 0)");
  2144. goto ErrorReturn;
  2145. }
  2146. pbNameEncoded = (BYTE *) TestAlloc(cbNameEncoded);
  2147. if (pbNameEncoded == NULL) goto ErrorReturn;
  2148. if (!CryptEncodeObject(
  2149. dwCertEncodingType,
  2150. X509_NAME,
  2151. &Name,
  2152. pbNameEncoded,
  2153. &cbNameEncoded
  2154. )) {
  2155. PrintLastError("EncodeCrl::CryptEncodeObject");
  2156. goto ErrorReturn;
  2157. }
  2158. GetSystemTime(&SystemTime);
  2159. memset(rgCrlEntry, 0, sizeof(rgCrlEntry));
  2160. rgCrlEntry[0].SerialNumber.pbData = (BYTE *) &SerialNumber0[0];
  2161. rgCrlEntry[0].SerialNumber.cbData = sizeof(SerialNumber0);
  2162. SystemTime.wYear--;
  2163. SystemTimeToFileTime(&SystemTime, &rgCrlEntry[0].RevocationDate);
  2164. rgCrlEntry[0].cExtension = 0;
  2165. rgCrlEntry[0].rgExtension = NULL;
  2166. rgCrlEntry[1].SerialNumber.pbData = (BYTE *) &SerialNumber1[0];
  2167. rgCrlEntry[1].SerialNumber.cbData = sizeof(SerialNumber1);
  2168. SystemTime.wYear--;
  2169. SystemTimeToFileTime(&SystemTime, &rgCrlEntry[1].RevocationDate);
  2170. rgCrlEntry[1].cExtension = 2;
  2171. rgCrlEntry[1].rgExtension = &rgExt[1];
  2172. memset(&Crl, 0, sizeof(Crl));
  2173. Crl.dwVersion = CRL_V2;
  2174. Crl.SignatureAlgorithm.pszObjId = "1.2.4.5.898";
  2175. Crl.Issuer.pbData = pbNameEncoded;
  2176. Crl.Issuer.cbData = cbNameEncoded;
  2177. GetSystemTime(&SystemTime);
  2178. SystemTimeToFileTime(&SystemTime, &Crl.ThisUpdate);
  2179. SystemTime.wYear++;
  2180. SystemTimeToFileTime(&SystemTime, &Crl.NextUpdate);
  2181. Crl.cCRLEntry = sizeof(rgCrlEntry) / sizeof(rgCrlEntry[0]);
  2182. Crl.rgCRLEntry = rgCrlEntry;
  2183. Crl.cExtension = sizeof(rgExt) / sizeof(rgExt[0]);
  2184. Crl.rgExtension = rgExt;
  2185. cbCrlEncoded = 0;
  2186. CryptEncodeObject(
  2187. dwCertEncodingType,
  2188. X509_CERT_CRL_TO_BE_SIGNED,
  2189. &Crl,
  2190. NULL, // pbEncoded
  2191. &cbCrlEncoded
  2192. );
  2193. if (cbCrlEncoded == 0) {
  2194. PrintLastError("EncodeCrl::CryptEncodeObject(cbEncoded == 0)");
  2195. goto ErrorReturn;
  2196. }
  2197. pbCrlEncoded = (BYTE *) TestAlloc(cbCrlEncoded);
  2198. if (pbCrlEncoded == NULL) goto ErrorReturn;
  2199. if (!CryptEncodeObject(
  2200. dwCertEncodingType,
  2201. X509_CERT_CRL_TO_BE_SIGNED,
  2202. &Crl,
  2203. pbCrlEncoded,
  2204. &cbCrlEncoded
  2205. )) {
  2206. PrintLastError("EncodeCrl::CryptEncodeObject");
  2207. goto ErrorReturn;
  2208. }
  2209. if (!EncodeSignedContent(
  2210. pbCrlEncoded,
  2211. cbCrlEncoded,
  2212. ppbEncoded,
  2213. pcbEncoded))
  2214. goto ErrorReturn;
  2215. fResult = TRUE;
  2216. goto CommonReturn;
  2217. ErrorReturn:
  2218. *ppbEncoded = NULL;
  2219. *pcbEncoded = 0;
  2220. fResult = FALSE;
  2221. CommonReturn:
  2222. if (pbNameEncoded)
  2223. TestFree(pbNameEncoded);
  2224. if (pbExt)
  2225. TestFree(pbExt);
  2226. if (pbCrlEncoded)
  2227. TestFree(pbCrlEncoded);
  2228. return fResult;
  2229. }
  2230. static void PrintCrlEntries(DWORD cEntry, PCRL_ENTRY pEntry)
  2231. {
  2232. DWORD i;
  2233. for (i = 0; i < cEntry; i++, pEntry++) {
  2234. {
  2235. DWORD cb;
  2236. BYTE *pb;
  2237. printf(" [%d] SerialNumber::", i);
  2238. for (cb = pEntry->SerialNumber.cbData,
  2239. pb = pEntry->SerialNumber.pbData + (cb - 1);
  2240. cb > 0; cb--, pb--) {
  2241. printf(" %02X", *pb);
  2242. }
  2243. printf("\n");
  2244. }
  2245. printf(" [%d] RevocationDate:: %s\n", i,
  2246. FileTimeText(&pEntry->RevocationDate));
  2247. if (pEntry->cExtension == 0)
  2248. printf(" [%d] Extensions:: NONE\n", i);
  2249. else {
  2250. printf(" [%d] Extensions::\n", i);
  2251. PrintExtensions(pEntry->cExtension, pEntry->rgExtension);
  2252. }
  2253. }
  2254. }
  2255. static BOOL DecodeCrl(BYTE *pbEncoded, DWORD cbEncoded)
  2256. {
  2257. BOOL fResult;
  2258. PCRL_INFO pInfo = NULL;
  2259. DWORD cbInfo;
  2260. LPSTR pszObjId;
  2261. if (NULL == (pInfo = (PCRL_INFO) TestDecodeObject(
  2262. X509_CERT_CRL_TO_BE_SIGNED,
  2263. pbEncoded,
  2264. cbEncoded,
  2265. &cbInfo
  2266. ))) goto ErrorReturn;
  2267. printf("Version:: %d\n", pInfo->dwVersion);
  2268. pszObjId = pInfo->SignatureAlgorithm.pszObjId;
  2269. if (pszObjId == NULL)
  2270. pszObjId = "<NULL OBJID>";
  2271. printf("SignatureAlgorithm:: %s\n", pszObjId);
  2272. if (pInfo->SignatureAlgorithm.Parameters.cbData) {
  2273. printf("SignatureAlgorithm.Parameters::\n");
  2274. PrintBytes(" ", pInfo->SignatureAlgorithm.Parameters.pbData,
  2275. pInfo->SignatureAlgorithm.Parameters.cbData);
  2276. }
  2277. printf("Issuer::\n");
  2278. DecodeName(pInfo->Issuer.pbData, pInfo->Issuer.cbData);
  2279. printf("ThisUpdate:: %s\n", FileTimeText(&pInfo->ThisUpdate));
  2280. printf("NextUpdate:: %s\n", FileTimeText(&pInfo->NextUpdate));
  2281. if (pInfo->cExtension == 0)
  2282. printf("Extensions:: NONE\n");
  2283. else {
  2284. printf("Extensions::\n");
  2285. PrintExtensions(pInfo->cExtension, pInfo->rgExtension);
  2286. }
  2287. if (pInfo->cCRLEntry == 0)
  2288. printf("Entries:: NONE\n");
  2289. else {
  2290. printf("Entries::\n");
  2291. PrintCrlEntries(pInfo->cCRLEntry, pInfo->rgCRLEntry);
  2292. }
  2293. DecodeSignedContent(pbEncoded, cbEncoded, X509_CERT_CRL_TO_BE_SIGNED,
  2294. cbInfo);
  2295. fResult = TRUE;
  2296. goto CommonReturn;
  2297. ErrorReturn:
  2298. fResult = FALSE;
  2299. CommonReturn:
  2300. if (pInfo)
  2301. TestFree(pInfo);
  2302. return fResult;
  2303. }
  2304. static BOOL EncodeCertPair(BYTE **ppbEncoded, DWORD *pcbEncoded)
  2305. {
  2306. BOOL fResult;
  2307. BYTE *pbEncoded = NULL;
  2308. DWORD cbEncoded;
  2309. CERT_PAIR CertPair;
  2310. memset(&CertPair, 0, sizeof(CertPair));
  2311. if (pszForwardCertFilename)
  2312. ReadDERFromFile(pszForwardCertFilename,
  2313. &CertPair.Forward.pbData, &CertPair.Forward.cbData);
  2314. if (pszReverseCertFilename)
  2315. ReadDERFromFile(pszReverseCertFilename,
  2316. &CertPair.Reverse.pbData, &CertPair.Reverse.cbData);
  2317. cbEncoded = 0;
  2318. CryptEncodeObject(
  2319. dwCertEncodingType,
  2320. X509_CERT_PAIR,
  2321. &CertPair,
  2322. NULL, // pbEncoded
  2323. &cbEncoded
  2324. );
  2325. if (cbEncoded == 0) {
  2326. PrintLastError("EncodeCertPair::CryptEncodeObject(cbEncoded == 0)");
  2327. goto ErrorReturn;
  2328. }
  2329. pbEncoded = (BYTE *) TestAlloc(cbEncoded);
  2330. if (pbEncoded == NULL) goto ErrorReturn;
  2331. if (!CryptEncodeObject(
  2332. dwCertEncodingType,
  2333. X509_CERT_PAIR,
  2334. &CertPair,
  2335. pbEncoded,
  2336. &cbEncoded
  2337. )) {
  2338. PrintLastError("EncodeCertPair::CryptEncodeObject");
  2339. goto ErrorReturn;
  2340. }
  2341. fResult = TRUE;
  2342. goto CommonReturn;
  2343. ErrorReturn:
  2344. if (pbEncoded) {
  2345. TestFree(pbEncoded);
  2346. pbEncoded = NULL;
  2347. }
  2348. cbEncoded = 0;
  2349. fResult = FALSE;
  2350. CommonReturn:
  2351. TestFree(CertPair.Forward.pbData);
  2352. TestFree(CertPair.Reverse.pbData);
  2353. *ppbEncoded = pbEncoded;
  2354. *pcbEncoded = cbEncoded;
  2355. return fResult;
  2356. }
  2357. static BOOL DecodeCertPair(BYTE *pbEncoded, DWORD cbEncoded)
  2358. {
  2359. BOOL fResult;
  2360. PCERT_PAIR pInfo = NULL;
  2361. if (NULL == (pInfo = (PCERT_PAIR) TestDecodeObject(
  2362. X509_CERT_PAIR,
  2363. pbEncoded,
  2364. cbEncoded
  2365. ))) goto ErrorReturn;
  2366. if (pInfo->Forward.cbData) {
  2367. printf("Forward Certificate::\n");
  2368. PrintBytes(" ", pInfo->Forward.pbData, pInfo->Forward.cbData);
  2369. } else
  2370. printf("NO Forward Certificate\n");
  2371. if (pInfo->Reverse.cbData) {
  2372. printf("Reverse Certificate::\n");
  2373. PrintBytes(" ", pInfo->Reverse.pbData, pInfo->Reverse.cbData);
  2374. } else
  2375. printf("NO Reverse Certificate\n");
  2376. fResult = TRUE;
  2377. goto CommonReturn;
  2378. ErrorReturn:
  2379. fResult = FALSE;
  2380. CommonReturn:
  2381. TestFree(pInfo);
  2382. return fResult;
  2383. }