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.

616 lines
17 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1996 - 1999
  6. //
  7. // File: crmsgp.h
  8. //
  9. // Contents: Private Header for Cryptographic Message APIs
  10. //
  11. // History: 12-Dec-96 kevinr created
  12. //
  13. //--------------------------------------------------------------------------
  14. #ifndef __WINCRMSG_P_INCLUDED__
  15. #define __WINCRMSG_P_INCLUDED__
  16. #if DBG
  17. #define STATIC
  18. #else
  19. #define STATIC static
  20. #endif
  21. #ifndef OSS_CRYPT_ASN1
  22. #define parameters_present AlgorithmIdentifier_parameters_present
  23. #define unauthAttributes_present SignerInfo_unauthAttributes_present
  24. #define certificates_present SignedData_certificates_present
  25. #define crls_present SignedData_crls_present
  26. #define date_present RecipientKeyIdentifier_date_present
  27. #define other_present RecipientKeyIdentifier_other_present
  28. #define content_present ContentInfo_content_present
  29. #define authAttributes_present SignerInfoWithBlobs_authAttributes_present
  30. #define issuerAndSerialNumber_chosen CertIdentifier_issuerAndSerialNumber_chosen
  31. #define subjectKeyIdentifier_chosen CertIdentifier_subjectKeyIdentifier_chosen
  32. #endif // OSS_CRYPT_ASN1
  33. #define INFO_LEN_ALIGN(Len) ((Len + 7) & ~7)
  34. #define STRUCT_CBSIZE(StructName, FieldName) \
  35. (offsetof(StructName, FieldName) + sizeof(((StructName *) 0)->FieldName))
  36. #define ICM_TAG_NULL 0x00
  37. #define ICM_TAG_CONSTRUCTED 0x20
  38. #define ICM_TAG_SEQ 0x30
  39. #define ICM_TAG_SET 0x31
  40. #define ICM_TAG_OCTETSTRING 0x04
  41. #define ICM_TAG_NULL_OCTETS 0x05
  42. #define ICM_TAG_CONTEXT_0 0x80
  43. #define ICM_TAG_CONTEXT_1 0x81
  44. #define ICM_TAG_CONSTRUCTED_CONTEXT_0 (ICM_TAG_CONSTRUCTED | ICM_TAG_CONTEXT_0)
  45. #define ICM_TAG_CONSTRUCTED_CONTEXT_1 (ICM_TAG_CONSTRUCTED | ICM_TAG_CONTEXT_1)
  46. #define ICM_LENGTH_INDEFINITE 0x80
  47. // Streaming flags
  48. #define ICMS_QUEUE_OUTPUT_FLAG 0x80000000
  49. typedef struct _SIMPLEBLOBHEADER {
  50. ALG_ID aiEncAlg;
  51. } SIMPLEBLOBHEADER, *PSIMPLEBLOBHEADER;
  52. #define SIZE_OSS_OID 16
  53. extern ObjectID aoidMessages[];
  54. extern const CRYPT_OBJID_TABLE aotSign[];
  55. extern const DWORD COUNTOF_aotSign;
  56. extern const LPSTR apszObjIdPKCS7[];
  57. extern const DWORD COUNTOF_apszObjIdPKCS7;
  58. extern const CRYPT_OBJID_TABLE aotContentEncryption[];
  59. extern const DWORD COUNTOF_aotContentEncryption;
  60. extern const LPSTR pszObjIdDataType;
  61. extern const LPSTR pszObjIdContentType;
  62. extern const LPSTR pszObjIdMessageDigest;
  63. #define INVALID_ENCODING_SIZE (0xFFFFFFFE)
  64. #define ZEROSTRUCT(arg) (memset( &arg, 0, sizeof(arg)))
  65. typedef struct _ICM_BUFFER {
  66. PBYTE pbData;
  67. DWORD cbSize;
  68. DWORD cbUsed;
  69. DWORD cbDead;
  70. } ICM_BUFFER, *PICM_BUFFER;
  71. #define ICM_BUFFER_SIZE_INCR 1024
  72. void *
  73. WINAPI
  74. ICM_Alloc(
  75. IN size_t cbBytes);
  76. void *
  77. WINAPI
  78. ICM_AllocZero(
  79. IN size_t cb);
  80. void *
  81. WINAPI
  82. ICM_ReAlloc(
  83. IN void *pvOrg,
  84. IN size_t cb);
  85. void
  86. WINAPI
  87. ICM_Free(
  88. IN void *pv);
  89. BOOL
  90. WINAPI
  91. ICM_AppendBlob(
  92. PCRYPT_DATA_BLOB pblob,
  93. const BYTE *pbIn,
  94. DWORD cbIn);
  95. BOOL
  96. WINAPI
  97. ICM_CopyOssObjectIdentifier(
  98. OUT ObjectID *poiDst,
  99. IN ObjectID *poiSrc);
  100. typedef struct _CONTENT_INFO {
  101. LPSTR pszContentType;
  102. CRYPT_DATA_BLOB content;
  103. } CONTENT_INFO, *PCONTENT_INFO;
  104. class CHashList;
  105. class CBlobList;
  106. class CSignerList;
  107. class COssDecodeInfoList;
  108. #ifdef CMS_PKCS7
  109. class CHashNode;
  110. typedef struct _SIGNER_ENCODE_DATA_INFO {
  111. HCRYPTPROV hCryptProv;
  112. DWORD dwKeySpec;
  113. CHashNode *pHashNode;
  114. } SIGNER_ENCODE_DATA_INFO, *PSIGNER_ENCODE_DATA_INFO;
  115. #endif // CMS_PKCS7
  116. typedef struct _SIGNED_DATA_INFO {
  117. int version;
  118. CBlobList *pAlgidList;
  119. PCONTENT_INFO pci;
  120. CBlobList *pCertificateList;
  121. CBlobList *pCrlList;
  122. CSignerList *pSignerList;
  123. } SIGNED_DATA_INFO, *PSIGNED_DATA_INFO;
  124. typedef struct _CRYPT_MSG_INFO {
  125. CRITICAL_SECTION CriticalSection;
  126. BOOL fInitializedCriticalSection;
  127. LONG lRefCnt;
  128. HCRYPTPROV hCryptProv; // decode
  129. BOOL fDefaultCryptProv; // decode
  130. DWORD dwKeySpec; // key to use in CryptSignHash
  131. DWORD dwEncodingType; // encode
  132. DWORD dwMsgType;
  133. DWORD dwFlags;
  134. VOID *pvMsg;
  135. BOOL fEncoding;
  136. DWORD dwPhase;
  137. LPSTR pszInnerContentObjID;
  138. // signed or signed-and-enveloped
  139. PSIGNED_DATA_INFO psdi;
  140. // signed, digested or signed-and-enveloped
  141. BOOL fDetached;
  142. CHashList *pHashList;
  143. #ifdef CMS_PKCS7
  144. // encode signed
  145. DWORD cSignerEncodeDataInfo;
  146. PSIGNER_ENCODE_DATA_INFO rgSignerEncodeDataInfo;
  147. #endif // CMS_PKCS7
  148. // enveloped or signed-and-enveloped
  149. HCRYPTKEY hkeyContentCrypt;
  150. HCRYPTPROV hCryptProvContentCrypt;
  151. CRYPT_DATA_BLOB Plaintext;
  152. DWORD dwDecryptedRecipientIndex;
  153. #ifdef CMS_PKCS7
  154. DWORD dwDecryptedRecipientEncryptedKeyIndex;
  155. #endif // CMS_PKCS7
  156. #ifndef CMS_PKCS7
  157. PBYTE pbEncryptParameters; // encoded and allocated
  158. #endif // CMS_PKCS7
  159. // streaming
  160. PCMSG_STREAM_INFO pStreamInfo;
  161. DWORD aflStream;
  162. DWORD aflDecode;
  163. BOOL fStreamCallbackOutput;
  164. BOOL fStreamContentExtracted;
  165. ICM_BUFFER bufDecode;
  166. ICM_BUFFER bufEncode;
  167. ICM_BUFFER bufOutput;
  168. ICM_BUFFER bufCrypt;
  169. ICM_BUFFER bufPendingCrypt;
  170. DWORD cbBlockSize;
  171. BOOL fBlockCipher;
  172. DWORD cEndNullPairs; // decode
  173. DWORD cInnerNullPairs; // decode
  174. DWORD cLevelIndefiniteInner; // decode
  175. DWORD cbDefiniteRemain; // decode
  176. DWORD cbContentInfo; // decode
  177. ObjectIdentifierType *pooid; // decode
  178. DWORD aflOuter; // decode
  179. DWORD aflInner; // decode
  180. COssDecodeInfoList *plDecodeInfo; // decode
  181. #ifdef CMS_PKCS7
  182. // Optional OriginatorInfo for decoded CMS EnvelopedData
  183. CBlobList *pCertificateList;
  184. CBlobList *pCrlList;
  185. // Misc allocations, such as, Algorithm parameters
  186. CBlobList *pFreeList;
  187. #endif // CMS_PKCS7
  188. } CRYPT_MSG_INFO, *PCRYPT_MSG_INFO;
  189. //--------------------------------------------------------------------------
  190. // Decoding progress flags
  191. #define ICMS_DECODED_PREFIX 0x00000001
  192. #define ICMS_DECODED_CONTENT_START 0x00000002
  193. #define ICMS_DECODED_CONTENT 0x00000004
  194. #define ICMS_DECODED_SUFFIX 0x00000008
  195. #define ICMS_DECODED_CONTENTINFO_SEQ 0x00000010
  196. #define ICMS_DECODED_CONTENTINFO_CONTENT 0x00000020
  197. //#define ICMS_DECODED_ 0x00000040
  198. //#define ICMS_DECODED_ 0x00000080
  199. // Signed
  200. #define ICMS_DECODED_SIGNED_SEQ 0x00000100
  201. #define ICMS_DECODED_SIGNED_VERSION 0x00000200
  202. #define ICMS_DECODED_SIGNED_DIGESTALGOS 0x00000400
  203. #define ICMS_DECODED_SIGNED_CONTENTINFO 0x00000800
  204. #define ICMS_DECODED_SIGNED_CERTIFICATES 0x00001000
  205. #define ICMS_DECODED_SIGNED_CRLS 0x00002000
  206. #define ICMS_DECODED_SIGNED_SIGNERINFOS 0x00004000
  207. //#define ICMS_DECODED_ 0x00008000
  208. // Enveloped
  209. #define ICMS_DECODED_ENVELOPED_SEQ 0x00000100
  210. #define ICMS_DECODED_ENVELOPED_VERSION 0x00000200
  211. #define ICMS_DECODED_ENVELOPED_RECIPINFOS 0x00000400
  212. #define ICMS_DECODED_ENVELOPED_ECISEQ 0x00000800
  213. #define ICMS_DECODED_ENVELOPED_ECITYPE 0x00001000
  214. #define ICMS_DECODED_ENVELOPED_ECIALGID 0x00002000
  215. #define ICMS_DECODED_ENVELOPED_ECICONTENT 0x00004000
  216. #ifdef CMS_PKCS7
  217. #define ICMS_DECODED_ENVELOPED_ORIGINATOR 0x00008000
  218. #define ICMS_DECODED_ENVELOPED_ATTR 0x00010000
  219. #endif // CMS_PKCS7
  220. //#define ICMS_DECODED_ 0x00020000
  221. // Misc flags
  222. #define ICMS_INNER_OCTETSTRING 0x00100000
  223. #define ICMS_FINAL 0x00200000
  224. #define ICMS_NONBARE 0x00400000
  225. #define ICMS_RAW_DATA 0x00800000
  226. #define ICMS_PROCESS_CONTENT_BEGUN 0x01000000
  227. #define ICMS_PROCESS_CONTENT_DONE 0x02000000
  228. // Used in streaming decode parsing
  229. #define ICMS_TOKEN_INDEFINITE 1
  230. #define ICMS_TOKEN_NULLPAIR 2
  231. #define ICMS_TOKEN_DEFINITE 3
  232. #define ICMS_TOKEN_INCOMPLETE 4
  233. #if DBG
  234. #define CMSGP_STREAM_CRYPT_BLOCK_COUNT 1
  235. #else
  236. #define CMSGP_STREAM_CRYPT_BLOCK_COUNT 128
  237. #endif
  238. #define CMSGP_STREAM_MAX_ENCRYPT_BUFFER 1024
  239. typedef struct _OSS_DECODE_INFO {
  240. int iPDU;
  241. PVOID pvPDU;
  242. } OSS_DECODE_INFO, *POSS_DECODE_INFO;
  243. DEFINE_LIST_AND_NODE_CLASS( COssDecodeInfoList, COssDecodeInfoNode, OSS_DECODE_INFO);
  244. DEFINE_LIST_AND_NODE_CLASS( CBlobList, CBlobNode, CRYPT_DATA_BLOB);
  245. typedef struct _SIGNER_DATA_INFO {
  246. CRYPT_DATA_BLOB blob;
  247. CBlobList *pUnauthAttrList;
  248. } SIGNER_DATA_INFO, *PSIGNER_DATA_INFO;
  249. DEFINE_LIST_AND_NODE_CLASS( CSignerList, CSignerNode, SIGNER_DATA_INFO);
  250. typedef struct _ICM_HASH_INFO {
  251. #ifndef CMS_PKCS7
  252. HCRYPTPROV hCryptProv;
  253. #endif // CMS_PKCS7
  254. DWORD dwAlgoCAPI;
  255. HCRYPTHASH hHash;
  256. CRYPT_HASH_BLOB HashBlob;
  257. } ICM_HASH_INFO, *PICM_HASH_INFO;
  258. DEFINE_LIST_AND_NODE_CLASS( CHashList, CHashNode, ICM_HASH_INFO);
  259. extern HCRYPTASN1MODULE ICM_hAsn1Module;
  260. __inline ASN1encoding_t ICM_GetEncoder(void)
  261. {
  262. return I_CryptGetAsn1Encoder(ICM_hAsn1Module);
  263. }
  264. __inline ASN1decoding_t ICM_GetDecoder(void)
  265. {
  266. return I_CryptGetAsn1Decoder(ICM_hAsn1Module);
  267. }
  268. BOOL
  269. WINAPI
  270. ICM_GetLengthOctets(
  271. IN DWORD cbContent,
  272. OUT OPTIONAL PBYTE pbOut,
  273. IN OUT PDWORD pcbOut);
  274. VOID
  275. WINAPI
  276. ICM_SetLastError(
  277. IN DWORD dwError);
  278. BOOL
  279. WINAPI
  280. ICM_GetCAPI(
  281. IN DWORD dwGroupId,
  282. IN PCRYPT_ALGORITHM_IDENTIFIER pai,
  283. OUT PDWORD pdwAlgId);
  284. LONG
  285. WINAPI
  286. ICM_ObjIdToIndex(
  287. IN ObjectID *poi);
  288. BOOL
  289. WINAPI
  290. ICM_EqualObjectIDs(
  291. IN ObjectID *poid1,
  292. IN ObjectID *poid2);
  293. BOOL
  294. WINAPI
  295. ICM_IsData(
  296. IN LPSTR pszContentType);
  297. BOOL
  298. WINAPI
  299. ICM_InsertTailBlob(
  300. IN OUT CBlobList *pBlobList,
  301. IN Any *pAny);
  302. BOOL
  303. WINAPI
  304. ICM_InsertTailSigner(
  305. IN OUT CSignerList *pSignerList,
  306. IN Any *pAny);
  307. BOOL
  308. WINAPI
  309. ICM_CreateHashList(
  310. IN HCRYPTPROV hCryptProv,
  311. IN OUT CHashList **ppHashList,
  312. IN CBlobList *pAlgidList);
  313. BOOL
  314. WINAPI
  315. ICM_UpdateListDigest(
  316. IN CHashList *pHashList,
  317. IN const BYTE *pbData,
  318. IN DWORD cbData);
  319. #ifdef CMS_PKCS7
  320. BOOL
  321. WINAPI
  322. ICM_IsDuplicateSignerEncodeHashAlgorithm(
  323. IN PCMSG_SIGNER_ENCODE_INFO rgSigners,
  324. IN PCMSG_SIGNER_ENCODE_INFO pNewSigner,
  325. OUT OPTIONAL DWORD *pdwPrevIndex = NULL
  326. );
  327. BOOL
  328. WINAPI
  329. ICM_FillSignerEncodeEncryptedDigests(
  330. IN PCRYPT_MSG_INFO pcmi,
  331. IN BOOL fMaxLength);
  332. #else
  333. BOOL
  334. WINAPI
  335. ICM_FillSignerEncryptedDigest(
  336. IN SignerInfo *psi,
  337. IN LPSTR pszInnerContentObjID,
  338. IN CHashNode *pnHash,
  339. IN DWORD dwKeySpec,
  340. IN BOOL fMaxLength);
  341. #endif // CMS_PKCS7
  342. BOOL
  343. WINAPI
  344. ICM_GetKeyBlockSize(
  345. IN HCRYPTKEY hkey,
  346. OUT PDWORD pcbBlock,
  347. OUT OPTIONAL PBOOL pfBlockCipher);
  348. #ifdef CMS_PKCS7
  349. BOOL
  350. WINAPI
  351. ICM_InitializeContentEncryptInfo(
  352. IN PCMSG_ENVELOPED_ENCODE_INFO pEnvelopedEncodeInfo,
  353. OUT PCMSG_CONTENT_ENCRYPT_INFO pContentEncryptInfo
  354. );
  355. void
  356. WINAPI
  357. ICM_FreeContentEncryptInfo(
  358. IN PCMSG_ENVELOPED_ENCODE_INFO pEnvelopedEncodeInfo,
  359. IN OUT PCMSG_CONTENT_ENCRYPT_INFO pContentEncryptInfo
  360. );
  361. BOOL
  362. WINAPI
  363. ICM_FillOssCmsRecipientInfos(
  364. IN PCMSG_CONTENT_ENCRYPT_INFO pContentEncryptInfo,
  365. IN OUT CmsRecipientInfos *poris,
  366. #ifdef OSS_CRYPT_ASN1
  367. IN OUT int *pEnvelopedDataVersion
  368. #else
  369. IN OUT ASN1int32_t *pEnvelopedDataVersion
  370. #endif // OSS_CRYPT_ASN1
  371. );
  372. void
  373. WINAPI
  374. ICM_FreeOssCmsRecipientInfos(
  375. IN OUT CmsRecipientInfos *poris
  376. );
  377. #else
  378. // rgcbEncryptParameters[1] is the dwEncryptFlags passed to
  379. // ICM_ExportEncryptKey
  380. BOOL
  381. WINAPI
  382. ICM_GenEncryptKey(
  383. IN OUT HCRYPTPROV *phCryptProv,
  384. IN PCRYPT_ALGORITHM_IDENTIFIER paiEncrypt,
  385. IN PVOID pvEncryptAuxInfo,
  386. IN PCERT_PUBLIC_KEY_INFO pPublicKeyInfo,
  387. IN PFN_CMSG_ALLOC pfnAlloc,
  388. OUT HCRYPTKEY *phEncryptKey,
  389. OUT PBYTE *ppbEncryptParameters,
  390. OUT DWORD rgcbEncryptParameters[2]);
  391. // rgcbData[1] is the dwEncryptFlags passed from ICM_GenEncryptKey
  392. BOOL
  393. WINAPI
  394. ICM_ExportEncryptKey(
  395. IN HCRYPTPROV hCryptProv,
  396. IN HCRYPTKEY hEncryptKey,
  397. IN PCERT_PUBLIC_KEY_INFO pPublicKeyInfo,
  398. OUT PBYTE pbData,
  399. IN OUT DWORD rgcbData[2]);
  400. BOOL
  401. WINAPI
  402. ICM_ImportEncryptKey(
  403. IN HCRYPTPROV hCryptProv,
  404. IN DWORD dwKeySpec,
  405. IN PCRYPT_ALGORITHM_IDENTIFIER paiEncrypt,
  406. IN PCRYPT_ALGORITHM_IDENTIFIER paiPubKey,
  407. IN PBYTE pbEncodedKey,
  408. IN DWORD cbEncodedKey,
  409. OUT HCRYPTKEY *phEncryptKey);
  410. #endif // CMS_PKCS7
  411. BOOL
  412. WINAPI
  413. ICM_EncryptBuffer(
  414. IN HCRYPTKEY hkeyEncrypt,
  415. HCRYPTHASH hHash,
  416. IN const BYTE *pbPlain,
  417. IN DWORD cbPlain,
  418. OUT PBYTE *ppbCipher,
  419. OUT PDWORD pcbCipher);
  420. BOOL
  421. WINAPI
  422. ICMS_SetDecryptKey(
  423. IN PCRYPT_MSG_INFO pcmi,
  424. IN HCRYPTKEY hkeyDecrypt);
  425. BOOL
  426. WINAPI
  427. ICMS_ExtractContent(
  428. IN PCRYPT_MSG_INFO pcmi,
  429. IN const BYTE *pbDER,
  430. IN DWORD cbDER,
  431. OUT PDWORD pcbContent,
  432. OUT const BYTE **ppbContent);
  433. BOOL
  434. WINAPI
  435. ICMS_OpenToEncodeData(
  436. IN PCRYPT_MSG_INFO pcmi);
  437. BOOL
  438. WINAPI
  439. ICMS_UpdateEncodingData(
  440. IN PCRYPT_MSG_INFO pcmi,
  441. IN PBYTE pbData,
  442. IN DWORD cbData,
  443. IN BOOL fFinal);
  444. BOOL
  445. WINAPI
  446. ICMS_UpdateDecodingInner(
  447. IN PCRYPT_MSG_INFO pcmi,
  448. IN BOOL fFinal);
  449. BOOL
  450. WINAPI
  451. ICMS_UpdateDecoding(
  452. IN PCRYPT_MSG_INFO pcmi,
  453. IN const BYTE *pbData,
  454. IN DWORD cbData,
  455. IN BOOL fFinal);
  456. BOOL
  457. WINAPI
  458. ICMS_OpenToEncodeSignedData(
  459. IN PCRYPT_MSG_INFO pcmi,
  460. IN PCMSG_SIGNED_ENCODE_INFO psmei);
  461. BOOL
  462. WINAPI
  463. ICMS_UpdateEncodingSignedData(
  464. IN PCRYPT_MSG_INFO pcmi,
  465. IN PBYTE pbData,
  466. IN DWORD cbData,
  467. IN BOOL fFinal);
  468. BOOL
  469. WINAPI
  470. ICMS_OpenToEncodeEnvelopedData(
  471. IN PCRYPT_MSG_INFO pcmi,
  472. IN PCMSG_ENVELOPED_ENCODE_INFO pemei);
  473. BOOL
  474. WINAPI
  475. ICMS_UpdateEncodingEnvelopedData(
  476. IN PCRYPT_MSG_INFO pcmi,
  477. IN const BYTE *pbPlain,
  478. IN DWORD cbPlain,
  479. IN BOOL fFinal);
  480. DWORD
  481. WINAPI
  482. ICM_LengthSigned(
  483. IN PCMSG_SIGNED_ENCODE_INFO psmei,
  484. IN DWORD dwFlags,
  485. IN LPSTR pszInnerContentObjID,
  486. IN DWORD cbData,
  487. OUT OPTIONAL PDWORD pcbContent);
  488. DWORD
  489. WINAPI
  490. ICM_LengthEnveloped(
  491. IN PCMSG_ENVELOPED_ENCODE_INFO pemei,
  492. IN DWORD dwFlags,
  493. IN OPTIONAL LPSTR pszInnerContentObjID,
  494. IN DWORD cbData,
  495. OUT OPTIONAL PDWORD pcbContent);
  496. // OCTET STRING callback, used for streaming
  497. typedef BOOL (WINAPI *POSTRCALLBACK)(
  498. IN const void *pvArg,
  499. IN OUT PICM_BUFFER pbuf,
  500. IN OUT PDWORD pcbPending,
  501. IN BOOL fFinal);
  502. BOOL
  503. WINAPI
  504. ICMS_HashContent(
  505. IN PCRYPT_MSG_INFO pcmi,
  506. IN PBYTE pbData,
  507. IN DWORD cbData);
  508. BOOL
  509. WINAPI
  510. ICM_GetSignerIdFromSignerEncodeInfo(
  511. IN PCMSG_SIGNER_ENCODE_INFO psei,
  512. OUT PCERT_ID pSignerId);
  513. #endif // __WINCRMSG_P_INCLUDED__