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.

473 lines
12 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows NT Security
  4. // Copyright (C) Microsoft Corporation, 1997 - 1998
  5. //
  6. // File: teku.cpp
  7. //
  8. // Contents: Cert Enhanced Key Usage tests
  9. //
  10. // History: 27-May-97 kirtd Created
  11. //
  12. //----------------------------------------------------------------------------
  13. #include <windows.h>
  14. #include <assert.h>
  15. #include "wincrypt.h"
  16. #include "certtest.h"
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <memory.h>
  21. #include <time.h>
  22. #define szOID_STUFF1 "2.2.2.4"
  23. #define szOID_STUFF2 "2.2.2.5"
  24. //+---------------------------------------------------------------------------
  25. //
  26. // Function: Usage
  27. //
  28. // Synopsis: prints the usage statement
  29. //
  30. //----------------------------------------------------------------------------
  31. static void Usage(void)
  32. {
  33. printf("Usage: teku [options]\n");
  34. printf("Options are:\n");
  35. printf(" -h - This message\n");
  36. printf(" -f<filename> - Cert file (.CER)\n");
  37. printf(" -s<store name> - Place cert in store\n");
  38. printf("\n");
  39. }
  40. //+---------------------------------------------------------------------------
  41. //
  42. // Function: GetAndDisplayEKU
  43. //
  44. // Synopsis: gets and displays enhanced key usage
  45. //
  46. //----------------------------------------------------------------------------
  47. static void GetAndDisplayEKU (PCCERT_CONTEXT pCertContext, DWORD dwFlags)
  48. {
  49. DWORD cbUsage;
  50. DWORD cCount;
  51. PCERT_ENHKEY_USAGE pUsage;
  52. //
  53. // Get the usage
  54. //
  55. if ( CertGetEnhancedKeyUsage(
  56. pCertContext,
  57. dwFlags,
  58. NULL,
  59. &cbUsage
  60. ) == FALSE )
  61. {
  62. if ( GetLastError() == CRYPT_E_NOT_FOUND )
  63. {
  64. printf("No enhanced key usage present\n\n");
  65. }
  66. else
  67. {
  68. printf(
  69. "Error: Could not get enhanced key usage %x\n\n",
  70. GetLastError()
  71. );
  72. }
  73. return;
  74. }
  75. pUsage = (PCERT_ENHKEY_USAGE)new BYTE [cbUsage];
  76. if ( pUsage != NULL )
  77. {
  78. if ( CertGetEnhancedKeyUsage(
  79. pCertContext,
  80. dwFlags,
  81. pUsage,
  82. &cbUsage
  83. ) == FALSE )
  84. {
  85. if ( GetLastError() == CRYPT_E_NOT_FOUND )
  86. {
  87. printf("No enhanced key usage present\n\n");
  88. }
  89. else
  90. {
  91. printf(
  92. "Error: Could not get enhanced key usage %x\n\n",
  93. GetLastError()
  94. );
  95. }
  96. delete pUsage;
  97. return;
  98. }
  99. }
  100. else
  101. {
  102. printf("Out of Memory!\n\n");
  103. return;
  104. }
  105. //
  106. // Display the usage
  107. //
  108. printf(
  109. "%d enhanced key usage OID(s) present:\n",
  110. pUsage->cUsageIdentifier
  111. );
  112. for ( cCount = 0; cCount < pUsage->cUsageIdentifier; cCount++ )
  113. {
  114. printf("\t%s\n", pUsage->rgpszUsageIdentifier[cCount]);
  115. }
  116. printf("\n");
  117. //
  118. // Cleanup
  119. //
  120. delete pUsage;
  121. }
  122. //+---------------------------------------------------------------------------
  123. //
  124. // Function: GetAndDisplayAllEKUForms
  125. //
  126. // Synopsis: displays all EKU forms
  127. //
  128. //----------------------------------------------------------------------------
  129. static void GetAndDisplayAllEKUForms (PCCERT_CONTEXT pCertContext)
  130. {
  131. //
  132. // Get and display EKU extension
  133. //
  134. printf("Certificate EKU extension\n\n");
  135. GetAndDisplayEKU(pCertContext, CERT_FIND_EXT_ONLY_ENHKEY_USAGE_FLAG);
  136. //
  137. // Get and display EKU property
  138. //
  139. printf("Certificate EKU property\n\n");
  140. GetAndDisplayEKU(pCertContext, CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG);
  141. //
  142. // Get and display both EKU extension and property
  143. //
  144. printf("Certificate EKU extension and property\n\n");
  145. GetAndDisplayEKU(
  146. pCertContext,
  147. CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG |
  148. CERT_FIND_EXT_ONLY_ENHKEY_USAGE_FLAG
  149. );
  150. }
  151. //+---------------------------------------------------------------------------
  152. //
  153. // Function: main
  154. //
  155. // Synopsis: main program entry point
  156. //
  157. //----------------------------------------------------------------------------
  158. int _cdecl main(int argc, char * argv[])
  159. {
  160. LPSTR pszCertFile = NULL;
  161. DWORD cbEncoded;
  162. LPBYTE pbEncoded;
  163. PCCERT_CONTEXT pCertContext;
  164. PCCERT_CONTEXT pContextToUse;
  165. LPSTR pszStore = NULL;
  166. HCERTSTORE hStore;
  167. while ( --argc > 0 )
  168. {
  169. if ( **++argv == '-' )
  170. {
  171. switch( argv[0][1] )
  172. {
  173. case 'f':
  174. case 'F':
  175. pszCertFile = argv[0]+2;
  176. if ( *pszCertFile == '\0' )
  177. {
  178. printf("Need to specify filename\n");
  179. Usage();
  180. return( -1 );
  181. }
  182. break;
  183. case 's':
  184. case 'S':
  185. pszStore = argv[0]+2;
  186. if ( *pszStore == '\0' )
  187. {
  188. printf("Need to specify store name\n");
  189. Usage();
  190. return( -1 );
  191. }
  192. break;
  193. default:
  194. Usage();
  195. return -1;
  196. }
  197. }
  198. }
  199. printf("command line: %s\n", GetCommandLineA());
  200. //
  201. // For now we must have a certificate file to process
  202. //
  203. if ( pszCertFile == NULL )
  204. {
  205. printf("Must specify a certificate file to process\n");
  206. Usage();
  207. return( -1 );
  208. }
  209. //
  210. // Use our input to get a certificate context to play with
  211. //
  212. if ( ReadDERFromFile(pszCertFile, &pbEncoded, &cbEncoded) == FALSE )
  213. {
  214. printf("Error reading CERT!\n");
  215. return(-1);
  216. }
  217. pCertContext = CertCreateCertificateContext(
  218. X509_ASN_ENCODING,
  219. pbEncoded,
  220. cbEncoded
  221. );
  222. TestFree(pbEncoded);
  223. if ( pCertContext == NULL )
  224. {
  225. printf( "Error create certificate context\n" );
  226. return( -1 );
  227. }
  228. //
  229. // If store action is requested ...
  230. //
  231. if ( pszStore != NULL )
  232. {
  233. hStore = CertOpenStore(
  234. CERT_STORE_PROV_SYSTEM_A,
  235. 0,
  236. NULL,
  237. CERT_SYSTEM_STORE_CURRENT_USER,
  238. pszStore
  239. );
  240. if ( hStore == NULL )
  241. {
  242. CertFreeCertificateContext( pCertContext );
  243. printf( "Error creating system store %lx\n", GetLastError() );
  244. return( -1 );
  245. }
  246. if ( CertEnumCertificatesInStore( hStore, NULL ) != NULL )
  247. {
  248. CertFreeCertificateContext( pCertContext );
  249. CertCloseStore( hStore, 0 );
  250. printf( "Must be a new or empty store\n" );
  251. return( -1 );
  252. }
  253. if ( CertAddCertificateContextToStore(
  254. hStore,
  255. pCertContext,
  256. CERT_STORE_ADD_NEW,
  257. NULL
  258. ) == FALSE )
  259. {
  260. CertFreeCertificateContext( pCertContext );
  261. CertCloseStore( hStore, 0 );
  262. printf( "Error creating system store %lx\n", GetLastError() );
  263. return( -1 );
  264. }
  265. CertFreeCertificateContext( pCertContext );
  266. pCertContext = CertEnumCertificatesInStore( hStore, NULL );
  267. if ( pCertContext == NULL )
  268. {
  269. CertCloseStore( hStore, 0 );
  270. printf( "Error finding certificate from store\n" );
  271. return( -1 );
  272. }
  273. printf( "hStore = %p\n", hStore );
  274. printf( "pCertContext->hCertStore = %p\n", pCertContext->hCertStore );
  275. pContextToUse = CertDuplicateCertificateContext( pCertContext );
  276. printf( "pContextToUse->hCertStore = %p\n", pContextToUse->hCertStore );
  277. CertCloseStore( hStore, 0 );
  278. printf( "pContextToUse->hCertStore = %p\n", pContextToUse->hCertStore );
  279. }
  280. else
  281. {
  282. pContextToUse = CertDuplicateCertificateContext( pCertContext );
  283. CertFreeCertificateContext( pCertContext );
  284. }
  285. //
  286. // Get and display all EKU forms
  287. //
  288. GetAndDisplayAllEKUForms(pContextToUse);
  289. //
  290. // Add a identifier property
  291. //
  292. printf("Adding %s enhanced key usage OID to the cert\n", szOID_STUFF1);
  293. if ( CertAddEnhancedKeyUsageIdentifier(
  294. pContextToUse,
  295. szOID_STUFF1
  296. ) == FALSE )
  297. {
  298. printf("Error adding key usage identifier %x\n", GetLastError());
  299. CertFreeCertificateContext(pContextToUse);
  300. return( -1 );
  301. }
  302. //
  303. // Get and display all forms
  304. //
  305. GetAndDisplayAllEKUForms(pContextToUse);
  306. //
  307. // Add another identifier property
  308. //
  309. printf("Adding %s enhanced key usage OID to the cert\n", szOID_STUFF2);
  310. if ( CertAddEnhancedKeyUsageIdentifier(
  311. pContextToUse,
  312. szOID_STUFF2
  313. ) == FALSE )
  314. {
  315. printf("Error adding key usage identifier %x\n", GetLastError());
  316. CertFreeCertificateContext(pContextToUse);
  317. return( -1 );
  318. }
  319. //
  320. // Get and display all forms
  321. //
  322. GetAndDisplayAllEKUForms(pContextToUse);
  323. if ( pszStore != NULL )
  324. {
  325. CertFreeCertificateContext( pContextToUse );
  326. hStore = CertOpenStore(
  327. CERT_STORE_PROV_SYSTEM_A,
  328. 0,
  329. NULL,
  330. CERT_SYSTEM_STORE_CURRENT_USER,
  331. pszStore
  332. );
  333. if ( hStore == NULL )
  334. {
  335. printf( "Error creating system store %lx\n", GetLastError() );
  336. return( -1 );
  337. }
  338. pContextToUse = CertEnumCertificatesInStore( hStore, NULL );
  339. if ( pContextToUse != NULL )
  340. {
  341. pContextToUse = CertDuplicateCertificateContext( pContextToUse );
  342. }
  343. else
  344. {
  345. printf( "Error enumerating certificate in store\n" );
  346. CertCloseStore( hStore, 0 );
  347. return( -1 );
  348. }
  349. CertCloseStore( hStore, 0 );
  350. printf( "Check EKUs after playing with store\n" );
  351. GetAndDisplayAllEKUForms(pContextToUse);
  352. }
  353. //
  354. // Remove an OID
  355. //
  356. printf("Removing %s enhanced key usage OID from the cert\n", szOID_STUFF2);
  357. if ( CertRemoveEnhancedKeyUsageIdentifier(
  358. pContextToUse,
  359. szOID_STUFF2
  360. ) == FALSE )
  361. {
  362. printf("Error removing key usage identifier %x\n", GetLastError());
  363. CertFreeCertificateContext(pContextToUse);
  364. return( -1 );
  365. }
  366. //
  367. // Get and display all forms
  368. //
  369. GetAndDisplayAllEKUForms(pContextToUse);
  370. //
  371. // Remove an OID
  372. //
  373. printf("Removing %s enhanced key usage OID from the cert\n", szOID_STUFF1);
  374. if ( CertRemoveEnhancedKeyUsageIdentifier(
  375. pContextToUse,
  376. szOID_STUFF1
  377. ) == FALSE )
  378. {
  379. printf("Error removing key usage identifier %x\n", GetLastError());
  380. CertFreeCertificateContext(pContextToUse);
  381. return( -1 );
  382. }
  383. //
  384. // Get and display all forms
  385. //
  386. GetAndDisplayAllEKUForms(pContextToUse);
  387. //
  388. // Free the certificate context
  389. //
  390. if ( pszStore != NULL )
  391. {
  392. CertDeleteCertificateFromStore( pContextToUse );
  393. }
  394. CertFreeCertificateContext(pContextToUse);
  395. printf("Test succeeded\n");
  396. return 0;
  397. }
  398.