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.

646 lines
20 KiB

  1. //*******************************************************************
  2. //
  3. // Copyright(c) Microsoft Corporation, 1996
  4. //
  5. // FILE: CERT.H
  6. //
  7. // PURPOSE: Header file for certificate functions in cert.c.
  8. //
  9. // HISTORY:
  10. // 96/09/23 vikramm Created.
  11. // 96/11/14 markdu BUG 10132 Updated to post-SDR CAPI.
  12. // 96/11/14 markdu BUG 10267 Remove static link to functions in advapi32.dll
  13. //
  14. //*******************************************************************
  15. #ifndef __CERT_H
  16. #define __CERT_H
  17. #ifndef IN
  18. #define IN
  19. #endif
  20. #ifndef OUT
  21. #define OUT
  22. #endif
  23. #ifndef OPTIONAL
  24. #define OPTIONAL
  25. #endif
  26. // Note:
  27. // Some data types are assumed and may need to be changed
  28. //
  29. // other defines
  30. #define MAX_STR 256 // Sting buffer size
  31. #define NUM_CHARS_PER_SN_BYTE 3 // Number of characters needed to display
  32. // each byte of the serial number
  33. // This struct and tags will be published by the exchange group -- this is temporary.
  34. #define NUM_CERT_TAGS 4
  35. #define CERT_TAG_DEFAULT 0x20
  36. #define CERT_TAG_THUMBPRINT 0x22
  37. #define CERT_TAG_BINCERT 0x03
  38. #define CERT_TAG_SYMCAPS 0x02
  39. #define CERT_TAG_SIGNING_TIME 0x0D
  40. #define CERT_TAG_SMIMECERT 0x30
  41. // SIZE_CERTTAGS is the size of the structure excluding the byte array.
  42. #define SIZE_CERTTAGS (2 * sizeof(WORD))
  43. // useless warning, should probably just remove the []
  44. #pragma warning (disable:4200)
  45. typedef struct _CertTag
  46. {
  47. WORD tag;
  48. WORD cbData;
  49. BYTE rgbData[];
  50. } CERTTAGS, FAR * LPCERTTAGS;
  51. #pragma warning (default:4200)
  52. /************************************************************************************/
  53. // Bare minimum info needed for each cert in the details certificate pane
  54. //
  55. typedef struct _CertDisplayInfo
  56. {
  57. LPTSTR lpszDisplayString; // String to display for this certificate
  58. LPTSTR lpszEmailAddress;
  59. DWORD dwTrust; // One of above trust flags
  60. BOOL bIsDefault; // Is this the default cert
  61. BOOL bIsRevoked; // Has this been revoked
  62. BOOL bIsExpired; // Is this expired
  63. BOOL bIsTrusted; // Is this a trusted certificate
  64. PCCERT_CONTEXT pccert; // THis is the actual cert
  65. BLOB blobSymCaps; // Symetric Capabilities
  66. FILETIME ftSigningTime; // Signing Time
  67. struct _CertDisplayInfo * lpNext;
  68. struct _CertDisplayInfo * lpPrev;
  69. } CERT_DISPLAY_INFO, * LPCERT_DISPLAY_INFO;
  70. /************************************************************************************/
  71. /************************************************************************************/
  72. // Details needed to display properties
  73. //
  74. typedef struct _CertDisplayProps
  75. {
  76. BOOL bIsRevoked; // Has this been revoked
  77. BOOL bIsExpired; // Is this expired
  78. DWORD dwTrust; // One of above trust flags
  79. BOOL bIsTrusted; // Whether its trusted or not
  80. LPTSTR lpszSerialNumber; // Serial Number for the cert
  81. LPTSTR lpszValidFromTo; // Valid from XXX to XXX
  82. LPTSTR lpszSubjectName; // Subject's name (same as display name in CERT_DISPLAY_INFO)
  83. LPTSTR lpszIssuerName; // Issuer's name - NULL if no name (self-issued)
  84. CRYPT_DIGEST_BLOB blobIssuerCertThumbPrint; // The actual certificate thumbprint of the issuer cert
  85. int nFieldCount; // Number of fields for which data exists (other that what we already have)
  86. LPTSTR* lppszFieldCount; // LPTSTR array of field names
  87. LPTSTR* lppszDetails; // LPTSTR array of details with one to one correspondence with field names
  88. struct _CertDisplayProps * lpIssuer; // Next cert up in the issuer chain.
  89. struct _CertDisplayProps * lpPrev; // previous cert in the issuer chain.
  90. } CERT_DISPLAY_PROPS, * LPCERT_DISPLAY_PROPS;
  91. /************************************************************************************/
  92. /************************************************************************************/
  93. // This is used by Cert UI elements
  94. typedef struct _CertItem
  95. {
  96. LPCERT_DISPLAY_INFO lpCDI;
  97. PCCERT_CONTEXT pcCert;
  98. TCHAR szDisplayText[MAX_PATH]; //should really be MAX_UI_STR
  99. struct _CertItem * lpNext;
  100. struct _CertItem * lpPrev;
  101. } CERT_ITEM, * LPCERT_ITEM;
  102. /************************************************************************************/
  103. // Function prototypes
  104. //*******************************************************************
  105. //
  106. // FUNCTION: HrGetCertsDisplayInfo
  107. //
  108. // PURPOSE: Takes an input array of certs in a SPropValue structure
  109. // and outputs a list of cert data structures by parsing through
  110. // the array and looking up the cert data in the store.
  111. //
  112. // PARAMETERS: hwndParent - any UI is modal to this
  113. // lpPropValue - PR_USER_X509_CERTIFICATE property array
  114. // lppCDI - recieves an allocated structure containing
  115. // the cert data. Must be freed by calling FreeCertdisplayinfo.
  116. //
  117. // RETURNS: HRESULT.
  118. //
  119. // HISTORY:
  120. // 96/09/24 markdu Created.
  121. //
  122. //*******************************************************************
  123. HRESULT HrGetCertsDisplayInfo(
  124. IN HWND hwndParent,
  125. IN LPSPropValue lpPropValue,
  126. OUT LPCERT_DISPLAY_INFO * lppCDI);
  127. //*******************************************************************
  128. //
  129. // FUNCTION: HrSetCertsFromDisplayInfo
  130. //
  131. // PURPOSE: Takes a linked list of cert data structures and outputs
  132. // an SPropValue array of PR_USER_X509_CERTIFICATE properties.
  133. //
  134. // PARAMETERS: lpCDI - linked list of input structures to convert to
  135. // SPropValue array
  136. // lpulcPropCount - receives the number of SPropValue's returned
  137. // Note that this will always be one.
  138. // lppPropValue - receives a MAPI-allocated SPropValue structure
  139. // containing an X509_USER_CERTIFICATE property
  140. //
  141. // RETURNS: HRESULT.
  142. //
  143. // HISTORY:
  144. // 96/09/24 markdu Created.
  145. //
  146. //*******************************************************************
  147. HRESULT HrSetCertsFromDisplayInfo(
  148. IN LPCERT_ITEM lpCItem,
  149. OUT ULONG * lpulcPropCount,
  150. OUT LPSPropValue * lppPropValue);
  151. //*******************************************************************
  152. //
  153. // FUNCTION: HrGetCertDisplayProps
  154. //
  155. // PURPOSE: Get displayable properties and other data for a certificate.
  156. //
  157. // PARAMETERS: pblobCertThumbPrint - thumb print of certificate to look up
  158. // hcsCertStore - the store that holds the cert. Use NULL to
  159. // open the WAB store.
  160. // hCryptProvider - the provider to use for store access. Use
  161. // zero to get the provider.
  162. // dwTrust - trust flags for this cert.
  163. // bIsTrusted - trusted or not ...
  164. // lppCDP - recieves an allocated structure containing
  165. // the cert data. Must be freed by calling FreeCertdisplayprops.
  166. //
  167. // RETURNS: HRESULT.
  168. //
  169. // HISTORY:
  170. // 96/09/24 markdu Created.
  171. //
  172. //*******************************************************************
  173. HRESULT HrGetCertDisplayProps(
  174. IN PCRYPT_DIGEST_BLOB pblobCertThumbPrint,
  175. IN HCERTSTORE hcsCertStore,
  176. IN HCRYPTPROV hCryptProvider,
  177. IN DWORD dwTrust,
  178. IN BOOL bIsTrusted,
  179. OUT LPCERT_DISPLAY_PROPS * lppCDP);
  180. //*******************************************************************
  181. //
  182. // FUNCTION: HrImportCertFromFile
  183. //
  184. // PURPOSE: Import a cert from a file.
  185. //
  186. // PARAMETERS: lpszFileName - name of file containing the cert.
  187. // lppCDI - recieves an allocated structure containing
  188. // the cert data. Must be freed by calling FreeCertdisplayinfo.
  189. //
  190. // RETURNS: HRESULT.
  191. //
  192. // HISTORY:
  193. // 96/09/24 markdu Created.
  194. //
  195. //*******************************************************************
  196. HRESULT HrImportCertFromFile(
  197. IN LPTSTR lpszFileName,
  198. OUT LPCERT_DISPLAY_INFO * lppCDI);
  199. //*******************************************************************
  200. //
  201. // FUNCTION: HrExportCertToFile
  202. //
  203. // PURPOSE: Export a cert to a file.
  204. //
  205. // PARAMETERS: lpszFileName - name of file in which to store the cert.
  206. // If the file exists, it will be overwritten, so the caller
  207. // must verify that this is OK first if so desired.
  208. // pblobCertThumbPrint - thumb print of certificate to export.
  209. // lpCertDataBuffer - buffer to write cert data to instead of file
  210. // fWriteDataToBuffer - flag indicating where cert data should be written
  211. //
  212. // RETURNS: HRESULT.
  213. //
  214. // HISTORY:
  215. // 96/09/24 markdu Created.
  216. // 98/07/22 t-jstaj updated to take 3 add'l parameters, a data buffer, its length
  217. // and flag which will indicate whether or not to
  218. // write data to buffer or file. The memory allocated to
  219. // to the buffer needs to be freed by caller.
  220. //
  221. //*******************************************************************
  222. HRESULT HrExportCertToFile(
  223. IN LPTSTR lpszFileName,
  224. IN PCCERT_CONTEXT pccert,
  225. OUT LPBYTE *lpCertDataBuffer,
  226. OUT PULONG lpcbBufLen,
  227. IN BOOL fWriteDataToBuffer );
  228. //*******************************************************************
  229. //
  230. // FUNCTION: FreeCertdisplayinfo
  231. //
  232. // PURPOSE: Release memory allocated for a CERT_DISPLAY_INFO structure.
  233. // Assumes all info in the structure was LocalAlloced
  234. //
  235. // PARAMETERS: lpCDI - structure to free.
  236. //
  237. // RETURNS: HRESULT.
  238. //
  239. // HISTORY:
  240. // 96/09/24 markdu Created.
  241. //
  242. //*******************************************************************
  243. void FreeCertdisplayinfo(LPCERT_DISPLAY_INFO lpCDI);
  244. //*******************************************************************
  245. //
  246. // FUNCTION: FreeCertdisplayprops
  247. //
  248. // PURPOSE: Release memory allocated for a CERT_DISPLAY_PROPS structure.
  249. // THIS INCLUDES the entire linked list below this sturcture,
  250. // so an entire list can be free by passing in the head of the list.
  251. // Assumes all info in the structure was LocalAlloced
  252. //
  253. // PARAMETERS: lpCDP - structure (list) to free.
  254. //
  255. // RETURNS: HRESULT.
  256. //
  257. // HISTORY:
  258. // 96/09/24 markdu Created.
  259. //
  260. //*******************************************************************
  261. void FreeCertdisplayprops(LPCERT_DISPLAY_PROPS lpCDP);
  262. //*******************************************************************
  263. //
  264. // FUNCTION: InitCryptoLib
  265. //
  266. // PURPOSE: Load the Crypto API libray and get the proc addrs.
  267. //
  268. // PARAMETERS: None.
  269. //
  270. // RETURNS: TRUE if successful, FALSE otherwise.
  271. //
  272. // HISTORY:
  273. // 96/10/01 markdu Created.
  274. // 96/11/19 markdu No longer keep a ref count, just use the global
  275. // library handles.
  276. //
  277. //*******************************************************************
  278. BOOL InitCryptoLib(void);
  279. //*******************************************************************
  280. //
  281. // FUNCTION: DeinitCryptoLib
  282. //
  283. // PURPOSE: Release the Crypto API libraries.
  284. //
  285. // PARAMETERS: None.
  286. //
  287. // RETURNS: None.
  288. //
  289. // HISTORY:
  290. // 96/10/01 markdu Created.
  291. // 96/11/19 markdu No longer keep a ref count, just call this in
  292. // DLL_PROCESS_DETACH.
  293. //
  294. //*******************************************************************
  295. void DeinitCryptoLib(void);
  296. //*******************************************************************
  297. //
  298. // FUNCTION: HrLDAPCertToMAPICert
  299. //
  300. // PURPOSE: Convert cert(s) returned from LDAP server to MAPI props.
  301. // Two properties are required. The certs are placed in the
  302. // WAB store, and all necessary indexing data is placed in
  303. // PR_USER_X509_CERTIFICATE property. If this certificate
  304. // didn't already exist in the WAB store, it's thumbprint is
  305. // added to PR_WAB_TEMP_CERT_HASH so that these certs can
  306. // be deleted from the store if the user cancels the add.
  307. //
  308. // PARAMETERS: lpPropArray - the prop array where the 2 props are stored
  309. // ulX509Index - the index to the PR_USER_X509_CERTIFICATE prop
  310. // ulTempCertIndex - the index to the PR_WAB_TEMP_CERT_HASH prop
  311. // lpCert, cbCert, - cert from LDAP ppberval struct
  312. // ulcCerts - the number of certs from the LDAP server
  313. //
  314. // RETURNS: HRESULT.
  315. //
  316. // HISTORY:
  317. // 96/12/12 markdu Created.
  318. //
  319. //*******************************************************************
  320. HRESULT HrLDAPCertToMAPICert(
  321. LPSPropValue lpPropArray,
  322. ULONG ulX509Index,
  323. ULONG ulTempCertIndex,
  324. ULONG cbCert,
  325. PBYTE lpCert,
  326. ULONG ulcCerts);
  327. //*******************************************************************
  328. //
  329. // FUNCTION: HrRemoveCertsFromWABStore
  330. //
  331. // PURPOSE: Remove the certs whose thumbprints are in the supplied
  332. // PR_WAB_TEMP_CERT_HASH property.
  333. //
  334. // PARAMETERS: lpPropValue - the PR_WAB_TEMP_CERT_HASH property
  335. //
  336. // RETURNS: HRESULT.
  337. //
  338. // HISTORY:
  339. // 96/12/13 markdu Created.
  340. //
  341. //*******************************************************************
  342. HRESULT HrRemoveCertsFromWABStore(
  343. LPSPropValue lpPropValue);
  344. //*******************************************************************
  345. //
  346. // FUNCTION: DeinitPStore
  347. //
  348. // PURPOSE: Release the protected store.
  349. //
  350. // PARAMETERS: None.
  351. //
  352. // RETURNS: None.
  353. //
  354. // HISTORY:
  355. // 97/02/17 t-erikne Created.
  356. //
  357. //*******************************************************************
  358. void DeinitPStore(void);
  359. //*******************************************************************
  360. //
  361. // FUNCTION: DeleteCertStuff
  362. //
  363. // PURPOSE: Remove trust from the pstore and (later) certs from
  364. // the CAPI store
  365. //
  366. // PARAMETERS:
  367. // LPADRBOOK lpIAB - container to use
  368. // LPENTRYID lpEntryID - eid of item to clean up
  369. // ULONG cbEntryID - cb of above
  370. //
  371. // RETURNS: I promise it does.
  372. //
  373. // HISTORY:
  374. // 97/03/19 t-erikne Created.
  375. //
  376. //*******************************************************************
  377. HRESULT DeleteCertStuff(LPADRBOOK lpIAB,
  378. LPENTRYID lpEntryID,
  379. ULONG cbEntryID);
  380. //*******************************************************************
  381. //
  382. // FUNCTION: WabGetCertFromThumbprint
  383. //
  384. // PURPOSE: Opens the WAB's cert store and tries to find the cert
  385. // the CAPI store
  386. //
  387. // PARAMETERS:
  388. // CRYPT_DIGEST_BLOB thumbprint - the thumbprint to
  389. // search on.
  390. //
  391. // RETURNS: the cert. NULL if not found
  392. //
  393. // HISTORY:
  394. // 97/06/27 t-erikne Created.
  395. //
  396. //*******************************************************************
  397. PCCERT_CONTEXT WabGetCertFromThumbprint(CRYPT_DIGEST_BLOB thumbprint);
  398. //************************************************************************************
  399. // Crypto function typedefs
  400. //
  401. // Updated as of 2/3
  402. // (t-erikne)
  403. //
  404. // CertAddEncodedCertificateToStore
  405. typedef BOOL (WINAPI * LPCERTADDENCODEDCERTIFICATETOSTORE) (
  406. IN HCERTSTORE hCertStore,
  407. IN DWORD dwCertEncodingType,
  408. IN const BYTE *pbCertEncoded,
  409. IN DWORD cbCertEncoded,
  410. IN DWORD dwAddDisposition,
  411. OUT OPTIONAL PCCERT_CONTEXT *ppCertContext
  412. );
  413. // CertCloseStore
  414. typedef BOOL (WINAPI * LPCERTCLOSESTORE) (
  415. IN HCERTSTORE hCertStore,
  416. DWORD dwFlags
  417. );
  418. // CertCreateCertificateContext
  419. typedef PCCERT_CONTEXT (WINAPI * LPCERTCREATECERTIFICATECONTEXT) (
  420. IN DWORD dwCertEncodingType,
  421. IN const BYTE *pbCertEncoded,
  422. IN DWORD cbCertEncoded
  423. );
  424. // CertDeleteCertificateFromStore
  425. typedef BOOL (WINAPI * LPCERTDELETECERTIFICATEFROMSTORE) (
  426. IN PCCERT_CONTEXT pCertContext
  427. );
  428. // CertFindCertificateInStore
  429. typedef PCCERT_CONTEXT (WINAPI * LPCERTFINDCERTIFICATEINSTORE) (
  430. IN HCERTSTORE hCertStore,
  431. IN DWORD dwCertEncodingType,
  432. IN DWORD dwFindFlags,
  433. IN DWORD dwFindType,
  434. IN const void *pvFindPara,
  435. IN PCCERT_CONTEXT pPrevCertContext
  436. );
  437. // CertFreeCertificateContext
  438. typedef BOOL (WINAPI * LPCERTFREECERTIFICATECONTEXT) (
  439. IN PCCERT_CONTEXT pCertContext
  440. );
  441. // CertGetCertificateContextProperty
  442. typedef BOOL (WINAPI * LPCERTGETCERTIFICATECONTEXTPROPERTY) (
  443. IN PCCERT_CONTEXT pCertContext,
  444. IN DWORD dwPropId,
  445. OUT void *pvData,
  446. IN OUT DWORD *pcbData
  447. );
  448. // CertGetIssuerCertificateFromStore
  449. typedef PCCERT_CONTEXT (WINAPI * LPCERTGETISSUERCERTIFICATEFROMSTORE) (
  450. IN HCERTSTORE hCertStore,
  451. IN PCCERT_CONTEXT pSubjectContext,
  452. IN OPTIONAL PCCERT_CONTEXT pPrevIssuerContext,
  453. IN OUT DWORD *pdwFlags
  454. );
  455. // CertOpenSystemStore
  456. typedef HCERTSTORE (WINAPI * LPCERTOPENSYSTEMSTORE) (
  457. HCRYPTPROV hProv,
  458. LPTSTR szSubsystemProtocol
  459. );
  460. // CertOpenStore
  461. typedef HCERTSTORE (WINAPI * LPCERTOPENSTORE) (
  462. IN DWORD dwStoreProvType,
  463. IN DWORD dwCertEncodingType,
  464. IN HCRYPTPROV hCryptProv,
  465. IN DWORD dwFlags,
  466. IN void * pvPara
  467. );
  468. // CertEnumCertificatesInStore
  469. typedef PCCERT_CONTEXT (WINAPI * LPCERTENUMCERTIFICATESINSTORE) (
  470. IN HCERTSTORE hCertStore,
  471. IN PCCERT_CONTEXT pPrevCertContext
  472. );
  473. // CertGetSubjectCertificateFromStore
  474. typedef PCCERT_CONTEXT (WINAPI * LPCERTGETSUBJECTCERTIFICATEFROMSTORE) (
  475. IN HCERTSTORE hCertStore,
  476. IN DWORD dwCertEncodingType,
  477. IN PCERT_INFO pCertId
  478. );
  479. // CertCompareCertificate
  480. typedef BOOL (WINAPI * LPCERTCOMPARECERTIFICATE) (
  481. IN DWORD dwCertEncodingType,
  482. IN PCERT_INFO pCertId1,
  483. IN PCERT_INFO pCertId2
  484. );
  485. // CertDuplicateCertificateContext
  486. typedef PCCERT_CONTEXT (WINAPI * LPCERTDUPLICATECERTIFICATECONTEXT) (
  487. IN PCCERT_CONTEXT pCertContext
  488. );
  489. // CertNameToStrA
  490. //N the right thing to do is use WINCRYPT32API
  491. //N and fixt the import stuff
  492. typedef DWORD (WINAPI * LPCERTNAMETOSTR) (
  493. IN DWORD dwCertEncodingType,
  494. IN PCERT_NAME_BLOB pName,
  495. IN DWORD dwStrType,
  496. OUT OPTIONAL LPTSTR psz,
  497. IN DWORD csz
  498. );
  499. // CryptAcquireContext
  500. typedef BOOL (WINAPI * LPCRYPTACQUIRECONTEXT) (
  501. HCRYPTPROV *phProv,
  502. LPCSTR pszContainer,
  503. LPCSTR pszProvider,
  504. DWORD dwProvType,
  505. DWORD dwFlags);
  506. // CryptDecodeObject
  507. typedef BOOL (WINAPI * LPCRYPTDECODEOBJECT) (
  508. IN DWORD dwCertEncodingType,
  509. IN LPCSTR lpszStructType,
  510. IN const BYTE *pbEncoded,
  511. IN DWORD cbEncoded,
  512. IN DWORD dwFlags,
  513. OUT void *pvStructInfo,
  514. IN OUT DWORD *pcbStructInfo
  515. );
  516. // CryptMsgClose
  517. typedef BOOL (WINAPI * LPCRYPTMSGCLOSE) (
  518. IN HCRYPTMSG hCryptMsg
  519. );
  520. // CryptMsgGetParam
  521. typedef BOOL (WINAPI * LPCRYPTMSGGETPARAM) (
  522. IN HCRYPTMSG hCryptMsg,
  523. IN DWORD dwParamType,
  524. IN DWORD dwIndex,
  525. OUT void *pvData,
  526. IN OUT DWORD *pcbData
  527. );
  528. // CryptMsgOpenToDecode
  529. typedef HCRYPTMSG (WINAPI * LPCRYPTMSGOPENTODECODE) (
  530. IN DWORD dwMsgEncodingType,
  531. IN DWORD dwFlags,
  532. IN DWORD dwMsgType,
  533. IN HCRYPTPROV hCryptProv,
  534. IN OPTIONAL PCERT_INFO pRecipientInfo,
  535. IN OPTIONAL PCMSG_STREAM_INFO pStreamInfo
  536. );
  537. // CryptMsgUpdate
  538. typedef BOOL (WINAPI * LPCRYPTMSGUPDATE) (
  539. IN HCRYPTMSG hCryptMsg,
  540. IN const BYTE *pbData,
  541. IN DWORD cbData,
  542. IN BOOL fFinal
  543. );
  544. // CryptReleaseContext
  545. typedef BOOL (WINAPI * LPCRYPTRELEASECONTEXT) (
  546. HCRYPTPROV hProv,
  547. DWORD dwFlags);
  548. typedef PCERT_RDN_ATTR (WINAPI * LPCERTFINDRDNATTR) (
  549. IN LPCSTR pszObjId,
  550. IN PCERT_NAME_INFO pName
  551. );
  552. // CertRDNValueToStr
  553. typedef DWORD (WINAPI * LPCERTRDNVALUETOSTR) (
  554. IN DWORD dwValueType,
  555. IN PCERT_RDN_VALUE_BLOB pValue,
  556. OUT LPTSTR pszValueString,
  557. IN DWORD cszValueString);
  558. // CertVerifyTimeValidity
  559. typedef LONG (WINAPI * LPCERTVERIFYTIMEVALIDITY) (
  560. IN LPFILETIME pTimeToVerify,
  561. IN PCERT_INFO pCertInfo);
  562. #endif // include once