Source code of Windows XP (NT5)
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.

615 lines
16 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. LONG lRefCnt;
  127. HCRYPTPROV hCryptProv; // decode
  128. BOOL fDefaultCryptProv; // decode
  129. DWORD dwKeySpec; // key to use in CryptSignHash
  130. DWORD dwEncodingType; // encode
  131. DWORD dwMsgType;
  132. DWORD dwFlags;
  133. VOID *pvMsg;
  134. BOOL fEncoding;
  135. DWORD dwPhase;
  136. LPSTR pszInnerContentObjID;
  137. // signed or signed-and-enveloped
  138. PSIGNED_DATA_INFO psdi;
  139. // signed, digested or signed-and-enveloped
  140. BOOL fDetached;
  141. CHashList *pHashList;
  142. #ifdef CMS_PKCS7
  143. // encode signed
  144. DWORD cSignerEncodeDataInfo;
  145. PSIGNER_ENCODE_DATA_INFO rgSignerEncodeDataInfo;
  146. #endif // CMS_PKCS7
  147. // enveloped or signed-and-enveloped
  148. HCRYPTKEY hkeyContentCrypt;
  149. HCRYPTPROV hCryptProvContentCrypt;
  150. CRYPT_DATA_BLOB Plaintext;
  151. DWORD dwDecryptedRecipientIndex;
  152. #ifdef CMS_PKCS7
  153. DWORD dwDecryptedRecipientEncryptedKeyIndex;
  154. #endif // CMS_PKCS7
  155. #ifndef CMS_PKCS7
  156. PBYTE pbEncryptParameters; // encoded and allocated
  157. #endif // CMS_PKCS7
  158. // streaming
  159. PCMSG_STREAM_INFO pStreamInfo;
  160. DWORD aflStream;
  161. DWORD aflDecode;
  162. BOOL fStreamCallbackOutput;
  163. BOOL fStreamContentExtracted;
  164. ICM_BUFFER bufDecode;
  165. ICM_BUFFER bufEncode;
  166. ICM_BUFFER bufOutput;
  167. ICM_BUFFER bufCrypt;
  168. ICM_BUFFER bufPendingCrypt;
  169. DWORD cbBlockSize;
  170. BOOL fBlockCipher;
  171. DWORD cEndNullPairs; // decode
  172. DWORD cInnerNullPairs; // decode
  173. DWORD cLevelIndefiniteInner; // decode
  174. DWORD cbDefiniteRemain; // decode
  175. DWORD cbContentInfo; // decode
  176. ObjectIdentifierType *pooid; // decode
  177. DWORD aflOuter; // decode
  178. DWORD aflInner; // decode
  179. COssDecodeInfoList *plDecodeInfo; // decode
  180. #ifdef CMS_PKCS7
  181. // Optional OriginatorInfo for decoded CMS EnvelopedData
  182. CBlobList *pCertificateList;
  183. CBlobList *pCrlList;
  184. // Misc allocations, such as, Algorithm parameters
  185. CBlobList *pFreeList;
  186. #endif // CMS_PKCS7
  187. } CRYPT_MSG_INFO, *PCRYPT_MSG_INFO;
  188. //--------------------------------------------------------------------------
  189. // Decoding progress flags
  190. #define ICMS_DECODED_PREFIX 0x00000001
  191. #define ICMS_DECODED_CONTENT_START 0x00000002
  192. #define ICMS_DECODED_CONTENT 0x00000004
  193. #define ICMS_DECODED_SUFFIX 0x00000008
  194. #define ICMS_DECODED_CONTENTINFO_SEQ 0x00000010
  195. #define ICMS_DECODED_CONTENTINFO_CONTENT 0x00000020
  196. //#define ICMS_DECODED_ 0x00000040
  197. //#define ICMS_DECODED_ 0x00000080
  198. // Signed
  199. #define ICMS_DECODED_SIGNED_SEQ 0x00000100
  200. #define ICMS_DECODED_SIGNED_VERSION 0x00000200
  201. #define ICMS_DECODED_SIGNED_DIGESTALGOS 0x00000400
  202. #define ICMS_DECODED_SIGNED_CONTENTINFO 0x00000800
  203. #define ICMS_DECODED_SIGNED_CERTIFICATES 0x00001000
  204. #define ICMS_DECODED_SIGNED_CRLS 0x00002000
  205. #define ICMS_DECODED_SIGNED_SIGNERINFOS 0x00004000
  206. //#define ICMS_DECODED_ 0x00008000
  207. // Enveloped
  208. #define ICMS_DECODED_ENVELOPED_SEQ 0x00000100
  209. #define ICMS_DECODED_ENVELOPED_VERSION 0x00000200
  210. #define ICMS_DECODED_ENVELOPED_RECIPINFOS 0x00000400
  211. #define ICMS_DECODED_ENVELOPED_ECISEQ 0x00000800
  212. #define ICMS_DECODED_ENVELOPED_ECITYPE 0x00001000
  213. #define ICMS_DECODED_ENVELOPED_ECIALGID 0x00002000
  214. #define ICMS_DECODED_ENVELOPED_ECICONTENT 0x00004000
  215. #ifdef CMS_PKCS7
  216. #define ICMS_DECODED_ENVELOPED_ORIGINATOR 0x00008000
  217. #define ICMS_DECODED_ENVELOPED_ATTR 0x00010000
  218. #endif // CMS_PKCS7
  219. //#define ICMS_DECODED_ 0x00020000
  220. // Misc flags
  221. #define ICMS_INNER_OCTETSTRING 0x00100000
  222. #define ICMS_FINAL 0x00200000
  223. #define ICMS_NONBARE 0x00400000
  224. #define ICMS_RAW_DATA 0x00800000
  225. #define ICMS_PROCESS_CONTENT_BEGUN 0x01000000
  226. #define ICMS_PROCESS_CONTENT_DONE 0x02000000
  227. // Used in streaming decode parsing
  228. #define ICMS_TOKEN_INDEFINITE 1
  229. #define ICMS_TOKEN_NULLPAIR 2
  230. #define ICMS_TOKEN_DEFINITE 3
  231. #define ICMS_TOKEN_INCOMPLETE 4
  232. #if DBG
  233. #define CMSGP_STREAM_CRYPT_BLOCK_COUNT 1
  234. #else
  235. #define CMSGP_STREAM_CRYPT_BLOCK_COUNT 128
  236. #endif
  237. #define CMSGP_STREAM_MAX_ENCRYPT_BUFFER 1024
  238. typedef struct _OSS_DECODE_INFO {
  239. int iPDU;
  240. PVOID pvPDU;
  241. } OSS_DECODE_INFO, *POSS_DECODE_INFO;
  242. DEFINE_LIST_AND_NODE_CLASS( COssDecodeInfoList, COssDecodeInfoNode, OSS_DECODE_INFO);
  243. DEFINE_LIST_AND_NODE_CLASS( CBlobList, CBlobNode, CRYPT_DATA_BLOB);
  244. typedef struct _SIGNER_DATA_INFO {
  245. CRYPT_DATA_BLOB blob;
  246. CBlobList *pUnauthAttrList;
  247. } SIGNER_DATA_INFO, *PSIGNER_DATA_INFO;
  248. DEFINE_LIST_AND_NODE_CLASS( CSignerList, CSignerNode, SIGNER_DATA_INFO);
  249. typedef struct _ICM_HASH_INFO {
  250. #ifndef CMS_PKCS7
  251. HCRYPTPROV hCryptProv;
  252. #endif // CMS_PKCS7
  253. DWORD dwAlgoCAPI;
  254. HCRYPTHASH hHash;
  255. CRYPT_HASH_BLOB HashBlob;
  256. } ICM_HASH_INFO, *PICM_HASH_INFO;
  257. DEFINE_LIST_AND_NODE_CLASS( CHashList, CHashNode, ICM_HASH_INFO);
  258. extern HCRYPTASN1MODULE ICM_hAsn1Module;
  259. __inline ASN1encoding_t ICM_GetEncoder(void)
  260. {
  261. return I_CryptGetAsn1Encoder(ICM_hAsn1Module);
  262. }
  263. __inline ASN1decoding_t ICM_GetDecoder(void)
  264. {
  265. return I_CryptGetAsn1Decoder(ICM_hAsn1Module);
  266. }
  267. BOOL
  268. WINAPI
  269. ICM_GetLengthOctets(
  270. IN DWORD cbContent,
  271. OUT OPTIONAL PBYTE pbOut,
  272. IN OUT PDWORD pcbOut);
  273. VOID
  274. WINAPI
  275. ICM_SetLastError(
  276. IN DWORD dwError);
  277. BOOL
  278. WINAPI
  279. ICM_GetCAPI(
  280. IN DWORD dwGroupId,
  281. IN PCRYPT_ALGORITHM_IDENTIFIER pai,
  282. OUT PDWORD pdwAlgId);
  283. LONG
  284. WINAPI
  285. ICM_ObjIdToIndex(
  286. IN ObjectID *poi);
  287. BOOL
  288. WINAPI
  289. ICM_EqualObjectIDs(
  290. IN ObjectID *poid1,
  291. IN ObjectID *poid2);
  292. BOOL
  293. WINAPI
  294. ICM_IsData(
  295. IN LPSTR pszContentType);
  296. BOOL
  297. WINAPI
  298. ICM_InsertTailBlob(
  299. IN OUT CBlobList *pBlobList,
  300. IN Any *pAny);
  301. BOOL
  302. WINAPI
  303. ICM_InsertTailSigner(
  304. IN OUT CSignerList *pSignerList,
  305. IN Any *pAny);
  306. BOOL
  307. WINAPI
  308. ICM_CreateHashList(
  309. IN HCRYPTPROV hCryptProv,
  310. IN OUT CHashList **ppHashList,
  311. IN CBlobList *pAlgidList);
  312. BOOL
  313. WINAPI
  314. ICM_UpdateListDigest(
  315. IN CHashList *pHashList,
  316. IN const BYTE *pbData,
  317. IN DWORD cbData);
  318. #ifdef CMS_PKCS7
  319. BOOL
  320. WINAPI
  321. ICM_IsDuplicateSignerEncodeHashAlgorithm(
  322. IN PCMSG_SIGNER_ENCODE_INFO rgSigners,
  323. IN PCMSG_SIGNER_ENCODE_INFO pNewSigner,
  324. OUT OPTIONAL DWORD *pdwPrevIndex = NULL
  325. );
  326. BOOL
  327. WINAPI
  328. ICM_FillSignerEncodeEncryptedDigests(
  329. IN PCRYPT_MSG_INFO pcmi,
  330. IN BOOL fMaxLength);
  331. #else
  332. BOOL
  333. WINAPI
  334. ICM_FillSignerEncryptedDigest(
  335. IN SignerInfo *psi,
  336. IN LPSTR pszInnerContentObjID,
  337. IN CHashNode *pnHash,
  338. IN DWORD dwKeySpec,
  339. IN BOOL fMaxLength);
  340. #endif // CMS_PKCS7
  341. BOOL
  342. WINAPI
  343. ICM_GetKeyBlockSize(
  344. IN HCRYPTKEY hkey,
  345. OUT PDWORD pcbBlock,
  346. OUT OPTIONAL PBOOL pfBlockCipher);
  347. #ifdef CMS_PKCS7
  348. BOOL
  349. WINAPI
  350. ICM_InitializeContentEncryptInfo(
  351. IN PCMSG_ENVELOPED_ENCODE_INFO pEnvelopedEncodeInfo,
  352. OUT PCMSG_CONTENT_ENCRYPT_INFO pContentEncryptInfo
  353. );
  354. void
  355. WINAPI
  356. ICM_FreeContentEncryptInfo(
  357. IN PCMSG_ENVELOPED_ENCODE_INFO pEnvelopedEncodeInfo,
  358. IN OUT PCMSG_CONTENT_ENCRYPT_INFO pContentEncryptInfo
  359. );
  360. BOOL
  361. WINAPI
  362. ICM_FillOssCmsRecipientInfos(
  363. IN PCMSG_CONTENT_ENCRYPT_INFO pContentEncryptInfo,
  364. IN OUT CmsRecipientInfos *poris,
  365. #ifdef OSS_CRYPT_ASN1
  366. IN OUT int *pEnvelopedDataVersion
  367. #else
  368. IN OUT ASN1int32_t *pEnvelopedDataVersion
  369. #endif // OSS_CRYPT_ASN1
  370. );
  371. void
  372. WINAPI
  373. ICM_FreeOssCmsRecipientInfos(
  374. IN OUT CmsRecipientInfos *poris
  375. );
  376. #else
  377. // rgcbEncryptParameters[1] is the dwEncryptFlags passed to
  378. // ICM_ExportEncryptKey
  379. BOOL
  380. WINAPI
  381. ICM_GenEncryptKey(
  382. IN OUT HCRYPTPROV *phCryptProv,
  383. IN PCRYPT_ALGORITHM_IDENTIFIER paiEncrypt,
  384. IN PVOID pvEncryptAuxInfo,
  385. IN PCERT_PUBLIC_KEY_INFO pPublicKeyInfo,
  386. IN PFN_CMSG_ALLOC pfnAlloc,
  387. OUT HCRYPTKEY *phEncryptKey,
  388. OUT PBYTE *ppbEncryptParameters,
  389. OUT DWORD rgcbEncryptParameters[2]);
  390. // rgcbData[1] is the dwEncryptFlags passed from ICM_GenEncryptKey
  391. BOOL
  392. WINAPI
  393. ICM_ExportEncryptKey(
  394. IN HCRYPTPROV hCryptProv,
  395. IN HCRYPTKEY hEncryptKey,
  396. IN PCERT_PUBLIC_KEY_INFO pPublicKeyInfo,
  397. OUT PBYTE pbData,
  398. IN OUT DWORD rgcbData[2]);
  399. BOOL
  400. WINAPI
  401. ICM_ImportEncryptKey(
  402. IN HCRYPTPROV hCryptProv,
  403. IN DWORD dwKeySpec,
  404. IN PCRYPT_ALGORITHM_IDENTIFIER paiEncrypt,
  405. IN PCRYPT_ALGORITHM_IDENTIFIER paiPubKey,
  406. IN PBYTE pbEncodedKey,
  407. IN DWORD cbEncodedKey,
  408. OUT HCRYPTKEY *phEncryptKey);
  409. #endif // CMS_PKCS7
  410. BOOL
  411. WINAPI
  412. ICM_EncryptBuffer(
  413. IN HCRYPTKEY hkeyEncrypt,
  414. HCRYPTHASH hHash,
  415. IN const BYTE *pbPlain,
  416. IN DWORD cbPlain,
  417. OUT PBYTE *ppbCipher,
  418. OUT PDWORD pcbCipher);
  419. BOOL
  420. WINAPI
  421. ICMS_SetDecryptKey(
  422. IN PCRYPT_MSG_INFO pcmi,
  423. IN HCRYPTKEY hkeyDecrypt);
  424. BOOL
  425. WINAPI
  426. ICMS_ExtractContent(
  427. IN PCRYPT_MSG_INFO pcmi,
  428. IN const BYTE *pbDER,
  429. IN DWORD cbDER,
  430. OUT PDWORD pcbContent,
  431. OUT const BYTE **ppbContent);
  432. BOOL
  433. WINAPI
  434. ICMS_OpenToEncodeData(
  435. IN PCRYPT_MSG_INFO pcmi);
  436. BOOL
  437. WINAPI
  438. ICMS_UpdateEncodingData(
  439. IN PCRYPT_MSG_INFO pcmi,
  440. IN PBYTE pbData,
  441. IN DWORD cbData,
  442. IN BOOL fFinal);
  443. BOOL
  444. WINAPI
  445. ICMS_UpdateDecodingInner(
  446. IN PCRYPT_MSG_INFO pcmi,
  447. IN BOOL fFinal);
  448. BOOL
  449. WINAPI
  450. ICMS_UpdateDecoding(
  451. IN PCRYPT_MSG_INFO pcmi,
  452. IN const BYTE *pbData,
  453. IN DWORD cbData,
  454. IN BOOL fFinal);
  455. BOOL
  456. WINAPI
  457. ICMS_OpenToEncodeSignedData(
  458. IN PCRYPT_MSG_INFO pcmi,
  459. IN PCMSG_SIGNED_ENCODE_INFO psmei);
  460. BOOL
  461. WINAPI
  462. ICMS_UpdateEncodingSignedData(
  463. IN PCRYPT_MSG_INFO pcmi,
  464. IN PBYTE pbData,
  465. IN DWORD cbData,
  466. IN BOOL fFinal);
  467. BOOL
  468. WINAPI
  469. ICMS_OpenToEncodeEnvelopedData(
  470. IN PCRYPT_MSG_INFO pcmi,
  471. IN PCMSG_ENVELOPED_ENCODE_INFO pemei);
  472. BOOL
  473. WINAPI
  474. ICMS_UpdateEncodingEnvelopedData(
  475. IN PCRYPT_MSG_INFO pcmi,
  476. IN const BYTE *pbPlain,
  477. IN DWORD cbPlain,
  478. IN BOOL fFinal);
  479. DWORD
  480. WINAPI
  481. ICM_LengthSigned(
  482. IN PCMSG_SIGNED_ENCODE_INFO psmei,
  483. IN DWORD dwFlags,
  484. IN LPSTR pszInnerContentObjID,
  485. IN DWORD cbData,
  486. OUT OPTIONAL PDWORD pcbContent);
  487. DWORD
  488. WINAPI
  489. ICM_LengthEnveloped(
  490. IN PCMSG_ENVELOPED_ENCODE_INFO pemei,
  491. IN DWORD dwFlags,
  492. IN OPTIONAL LPSTR pszInnerContentObjID,
  493. IN DWORD cbData,
  494. OUT OPTIONAL PDWORD pcbContent);
  495. // OCTET STRING callback, used for streaming
  496. typedef BOOL (WINAPI *POSTRCALLBACK)(
  497. IN const void *pvArg,
  498. IN OUT PICM_BUFFER pbuf,
  499. IN OUT PDWORD pcbPending,
  500. IN BOOL fFinal);
  501. BOOL
  502. WINAPI
  503. ICMS_HashContent(
  504. IN PCRYPT_MSG_INFO pcmi,
  505. IN PBYTE pbData,
  506. IN DWORD cbData);
  507. BOOL
  508. WINAPI
  509. ICM_GetSignerIdFromSignerEncodeInfo(
  510. IN PCMSG_SIGNER_ENCODE_INFO psei,
  511. OUT PCERT_ID pSignerId);
  512. #endif // __WINCRMSG_P_INCLUDED__