2 * Copyright 2008-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #ifndef OSSL_CRYPTO_CMS_LOCAL_H
11 # define OSSL_CRYPTO_CMS_LOCAL_H
13 # include <openssl/x509.h>
16 * Cryptographic message syntax (CMS) structures: taken from RFC3852
19 /* Forward references */
21 typedef struct CMS_IssuerAndSerialNumber_st CMS_IssuerAndSerialNumber;
22 typedef struct CMS_EncapsulatedContentInfo_st CMS_EncapsulatedContentInfo;
23 typedef struct CMS_SignerIdentifier_st CMS_SignerIdentifier;
24 typedef struct CMS_SignedData_st CMS_SignedData;
25 typedef struct CMS_OtherRevocationInfoFormat_st CMS_OtherRevocationInfoFormat;
26 typedef struct CMS_OriginatorInfo_st CMS_OriginatorInfo;
27 typedef struct CMS_EncryptedContentInfo_st CMS_EncryptedContentInfo;
28 typedef struct CMS_EnvelopedData_st CMS_EnvelopedData;
29 typedef struct CMS_DigestedData_st CMS_DigestedData;
30 typedef struct CMS_EncryptedData_st CMS_EncryptedData;
31 typedef struct CMS_AuthenticatedData_st CMS_AuthenticatedData;
32 typedef struct CMS_CompressedData_st CMS_CompressedData;
33 typedef struct CMS_OtherCertificateFormat_st CMS_OtherCertificateFormat;
34 typedef struct CMS_KeyTransRecipientInfo_st CMS_KeyTransRecipientInfo;
35 typedef struct CMS_OriginatorPublicKey_st CMS_OriginatorPublicKey;
36 typedef struct CMS_OriginatorIdentifierOrKey_st CMS_OriginatorIdentifierOrKey;
37 typedef struct CMS_KeyAgreeRecipientInfo_st CMS_KeyAgreeRecipientInfo;
38 typedef struct CMS_RecipientKeyIdentifier_st CMS_RecipientKeyIdentifier;
39 typedef struct CMS_KeyAgreeRecipientIdentifier_st
40 CMS_KeyAgreeRecipientIdentifier;
41 typedef struct CMS_KEKIdentifier_st CMS_KEKIdentifier;
42 typedef struct CMS_KEKRecipientInfo_st CMS_KEKRecipientInfo;
43 typedef struct CMS_PasswordRecipientInfo_st CMS_PasswordRecipientInfo;
44 typedef struct CMS_OtherRecipientInfo_st CMS_OtherRecipientInfo;
45 typedef struct CMS_ReceiptsFrom_st CMS_ReceiptsFrom;
46 typedef struct CMS_CTX_st CMS_CTX;
53 struct CMS_ContentInfo_st {
54 ASN1_OBJECT *contentType;
56 ASN1_OCTET_STRING *data;
57 CMS_SignedData *signedData;
58 CMS_EnvelopedData *envelopedData;
59 CMS_DigestedData *digestedData;
60 CMS_EncryptedData *encryptedData;
61 CMS_AuthenticatedData *authenticatedData;
62 CMS_CompressedData *compressedData;
70 DEFINE_STACK_OF(CMS_CertificateChoices)
72 struct CMS_SignedData_st {
74 STACK_OF(X509_ALGOR) *digestAlgorithms;
75 CMS_EncapsulatedContentInfo *encapContentInfo;
76 STACK_OF(CMS_CertificateChoices) *certificates;
77 STACK_OF(CMS_RevocationInfoChoice) *crls;
78 STACK_OF(CMS_SignerInfo) *signerInfos;
81 struct CMS_EncapsulatedContentInfo_st {
82 ASN1_OBJECT *eContentType;
83 ASN1_OCTET_STRING *eContent;
84 /* Set to 1 if incomplete structure only part set up */
88 struct CMS_SignerInfo_st {
90 CMS_SignerIdentifier *sid;
91 X509_ALGOR *digestAlgorithm;
92 STACK_OF(X509_ATTRIBUTE) *signedAttrs;
93 X509_ALGOR *signatureAlgorithm;
94 ASN1_OCTET_STRING *signature;
95 STACK_OF(X509_ATTRIBUTE) *unsignedAttrs;
96 /* Signing certificate and key */
99 /* Digest and public key context for alternative parameters */
102 const CMS_CTX *cms_ctx;
105 struct CMS_SignerIdentifier_st {
108 CMS_IssuerAndSerialNumber *issuerAndSerialNumber;
109 ASN1_OCTET_STRING *subjectKeyIdentifier;
113 struct CMS_EnvelopedData_st {
115 CMS_OriginatorInfo *originatorInfo;
116 STACK_OF(CMS_RecipientInfo) *recipientInfos;
117 CMS_EncryptedContentInfo *encryptedContentInfo;
118 STACK_OF(X509_ATTRIBUTE) *unprotectedAttrs;
121 struct CMS_OriginatorInfo_st {
122 STACK_OF(CMS_CertificateChoices) *certificates;
123 STACK_OF(CMS_RevocationInfoChoice) *crls;
126 struct CMS_EncryptedContentInfo_st {
127 ASN1_OBJECT *contentType;
128 X509_ALGOR *contentEncryptionAlgorithm;
129 ASN1_OCTET_STRING *encryptedContent;
130 /* Content encryption algorithm and key */
131 const EVP_CIPHER *cipher;
134 /* Set to 1 if we are debugging decrypt and don't fake keys for MMA */
136 /* Set to 1 if we have no cert and need extra safety measures for MMA */
140 struct CMS_RecipientInfo_st {
143 CMS_KeyTransRecipientInfo *ktri;
144 CMS_KeyAgreeRecipientInfo *kari;
145 CMS_KEKRecipientInfo *kekri;
146 CMS_PasswordRecipientInfo *pwri;
147 CMS_OtherRecipientInfo *ori;
151 typedef CMS_SignerIdentifier CMS_RecipientIdentifier;
153 struct CMS_KeyTransRecipientInfo_st {
155 CMS_RecipientIdentifier *rid;
156 X509_ALGOR *keyEncryptionAlgorithm;
157 ASN1_OCTET_STRING *encryptedKey;
158 /* Recipient Key and cert */
161 /* Public key context for this operation */
163 const CMS_CTX *cms_ctx;
166 struct CMS_KeyAgreeRecipientInfo_st {
168 CMS_OriginatorIdentifierOrKey *originator;
169 ASN1_OCTET_STRING *ukm;
170 X509_ALGOR *keyEncryptionAlgorithm;
171 STACK_OF(CMS_RecipientEncryptedKey) *recipientEncryptedKeys;
172 /* Public key context associated with current operation */
174 /* Cipher context for CEK wrapping */
176 const CMS_CTX *cms_ctx;
179 struct CMS_OriginatorIdentifierOrKey_st {
182 CMS_IssuerAndSerialNumber *issuerAndSerialNumber;
183 ASN1_OCTET_STRING *subjectKeyIdentifier;
184 CMS_OriginatorPublicKey *originatorKey;
188 struct CMS_OriginatorPublicKey_st {
189 X509_ALGOR *algorithm;
190 ASN1_BIT_STRING *publicKey;
193 struct CMS_RecipientEncryptedKey_st {
194 CMS_KeyAgreeRecipientIdentifier *rid;
195 ASN1_OCTET_STRING *encryptedKey;
196 /* Public key associated with this recipient */
200 struct CMS_KeyAgreeRecipientIdentifier_st {
203 CMS_IssuerAndSerialNumber *issuerAndSerialNumber;
204 CMS_RecipientKeyIdentifier *rKeyId;
208 struct CMS_RecipientKeyIdentifier_st {
209 ASN1_OCTET_STRING *subjectKeyIdentifier;
210 ASN1_GENERALIZEDTIME *date;
211 CMS_OtherKeyAttribute *other;
214 struct CMS_KEKRecipientInfo_st {
216 CMS_KEKIdentifier *kekid;
217 X509_ALGOR *keyEncryptionAlgorithm;
218 ASN1_OCTET_STRING *encryptedKey;
219 /* Extra info: symmetric key to use */
222 const CMS_CTX *cms_ctx;
225 struct CMS_KEKIdentifier_st {
226 ASN1_OCTET_STRING *keyIdentifier;
227 ASN1_GENERALIZEDTIME *date;
228 CMS_OtherKeyAttribute *other;
231 struct CMS_PasswordRecipientInfo_st {
233 X509_ALGOR *keyDerivationAlgorithm;
234 X509_ALGOR *keyEncryptionAlgorithm;
235 ASN1_OCTET_STRING *encryptedKey;
236 /* Extra info: password to use */
239 const CMS_CTX *cms_ctx;
242 struct CMS_OtherRecipientInfo_st {
243 ASN1_OBJECT *oriType;
247 struct CMS_DigestedData_st {
249 X509_ALGOR *digestAlgorithm;
250 CMS_EncapsulatedContentInfo *encapContentInfo;
251 ASN1_OCTET_STRING *digest;
254 struct CMS_EncryptedData_st {
256 CMS_EncryptedContentInfo *encryptedContentInfo;
257 STACK_OF(X509_ATTRIBUTE) *unprotectedAttrs;
260 struct CMS_AuthenticatedData_st {
262 CMS_OriginatorInfo *originatorInfo;
263 STACK_OF(CMS_RecipientInfo) *recipientInfos;
264 X509_ALGOR *macAlgorithm;
265 X509_ALGOR *digestAlgorithm;
266 CMS_EncapsulatedContentInfo *encapContentInfo;
267 STACK_OF(X509_ATTRIBUTE) *authAttrs;
268 ASN1_OCTET_STRING *mac;
269 STACK_OF(X509_ATTRIBUTE) *unauthAttrs;
272 struct CMS_CompressedData_st {
274 X509_ALGOR *compressionAlgorithm;
275 STACK_OF(CMS_RecipientInfo) *recipientInfos;
276 CMS_EncapsulatedContentInfo *encapContentInfo;
279 struct CMS_RevocationInfoChoice_st {
283 CMS_OtherRevocationInfoFormat *other;
287 # define CMS_REVCHOICE_CRL 0
288 # define CMS_REVCHOICE_OTHER 1
290 struct CMS_OtherRevocationInfoFormat_st {
291 ASN1_OBJECT *otherRevInfoFormat;
292 ASN1_TYPE *otherRevInfo;
295 struct CMS_CertificateChoices {
299 ASN1_STRING *extendedCertificate; /* Obsolete */
300 ASN1_STRING *v1AttrCert; /* Left encoded for now */
301 ASN1_STRING *v2AttrCert; /* Left encoded for now */
302 CMS_OtherCertificateFormat *other;
306 # define CMS_CERTCHOICE_CERT 0
307 # define CMS_CERTCHOICE_EXCERT 1
308 # define CMS_CERTCHOICE_V1ACERT 2
309 # define CMS_CERTCHOICE_V2ACERT 3
310 # define CMS_CERTCHOICE_OTHER 4
312 struct CMS_OtherCertificateFormat_st {
313 ASN1_OBJECT *otherCertFormat;
314 ASN1_TYPE *otherCert;
318 * This is also defined in pkcs7.h but we duplicate it to allow the CMS code
319 * to be independent of PKCS#7
322 struct CMS_IssuerAndSerialNumber_st {
324 ASN1_INTEGER *serialNumber;
327 struct CMS_OtherKeyAttribute_st {
328 ASN1_OBJECT *keyAttrId;
334 struct CMS_ReceiptRequest_st {
335 ASN1_OCTET_STRING *signedContentIdentifier;
336 CMS_ReceiptsFrom *receiptsFrom;
337 STACK_OF(GENERAL_NAMES) *receiptsTo;
340 struct CMS_ReceiptsFrom_st {
343 int32_t allOrFirstTier;
344 STACK_OF(GENERAL_NAMES) *receiptList;
348 struct CMS_Receipt_st {
350 ASN1_OBJECT *contentType;
351 ASN1_OCTET_STRING *signedContentIdentifier;
352 ASN1_OCTET_STRING *originatorSignatureValue;
355 DECLARE_ASN1_FUNCTIONS(CMS_ContentInfo)
356 DECLARE_ASN1_ITEM(CMS_SignerInfo)
357 DECLARE_ASN1_ITEM(CMS_IssuerAndSerialNumber)
358 DECLARE_ASN1_ITEM(CMS_Attributes_Sign)
359 DECLARE_ASN1_ITEM(CMS_Attributes_Verify)
360 DECLARE_ASN1_ITEM(CMS_RecipientInfo)
361 DECLARE_ASN1_ITEM(CMS_PasswordRecipientInfo)
362 DECLARE_ASN1_ALLOC_FUNCTIONS(CMS_IssuerAndSerialNumber)
364 # define CMS_SIGNERINFO_ISSUER_SERIAL 0
365 # define CMS_SIGNERINFO_KEYIDENTIFIER 1
367 # define CMS_RECIPINFO_ISSUER_SERIAL 0
368 # define CMS_RECIPINFO_KEYIDENTIFIER 1
370 # define CMS_REK_ISSUER_SERIAL 0
371 # define CMS_REK_KEYIDENTIFIER 1
373 # define CMS_OIK_ISSUER_SERIAL 0
374 # define CMS_OIK_KEYIDENTIFIER 1
375 # define CMS_OIK_PUBKEY 2
377 BIO *cms_content_bio(CMS_ContentInfo *cms);
378 const CMS_CTX *cms_get0_cmsctx(const CMS_ContentInfo *cms);
379 OPENSSL_CTX *cms_ctx_get0_libctx(const CMS_CTX *ctx);
380 const char *cms_ctx_get0_propq(const CMS_CTX *ctx);
381 void cms_resolve_libctx(CMS_ContentInfo *ci);
383 CMS_ContentInfo *cms_Data_create(OPENSSL_CTX *ctx, const char *propq);
385 CMS_ContentInfo *cms_DigestedData_create(const EVP_MD *md,
386 OPENSSL_CTX *libctx, const char *propq);
387 BIO *cms_DigestedData_init_bio(const CMS_ContentInfo *cms);
388 int cms_DigestedData_do_final(const CMS_ContentInfo *cms, BIO *chain, int verify);
390 BIO *cms_SignedData_init_bio(CMS_ContentInfo *cms);
391 int cms_SignedData_final(CMS_ContentInfo *cms, BIO *chain);
392 int cms_set1_SignerIdentifier(CMS_SignerIdentifier *sid, X509 *cert,
393 int type, const CMS_CTX *ctx);
394 int cms_SignerIdentifier_get0_signer_id(CMS_SignerIdentifier *sid,
395 ASN1_OCTET_STRING **keyid,
398 int cms_SignerIdentifier_cert_cmp(CMS_SignerIdentifier *sid, X509 *cert);
400 CMS_ContentInfo *cms_CompressedData_create(int comp_nid, OPENSSL_CTX *libctx,
402 BIO *cms_CompressedData_init_bio(const CMS_ContentInfo *cms);
404 BIO *cms_DigestAlgorithm_init_bio(X509_ALGOR *digestAlgorithm,
406 int cms_DigestAlgorithm_find_ctx(EVP_MD_CTX *mctx, BIO *chain,
409 int cms_ias_cert_cmp(CMS_IssuerAndSerialNumber *ias, X509 *cert);
410 int cms_keyid_cert_cmp(ASN1_OCTET_STRING *keyid, X509 *cert);
411 int cms_set1_ias(CMS_IssuerAndSerialNumber **pias, X509 *cert);
412 int cms_set1_keyid(ASN1_OCTET_STRING **pkeyid, X509 *cert);
414 BIO *cms_EncryptedContent_init_bio(CMS_EncryptedContentInfo *ec,
416 BIO *cms_EncryptedData_init_bio(const CMS_ContentInfo *cms);
417 int cms_EncryptedContent_init(CMS_EncryptedContentInfo *ec,
418 const EVP_CIPHER *cipher,
419 const unsigned char *key, size_t keylen,
422 int cms_Receipt_verify(CMS_ContentInfo *cms, CMS_ContentInfo *req_cms);
423 int cms_msgSigDigest_add1(CMS_SignerInfo *dest, CMS_SignerInfo *src);
424 ASN1_OCTET_STRING *cms_encode_Receipt(CMS_SignerInfo *si);
426 BIO *cms_EnvelopedData_init_bio(CMS_ContentInfo *cms);
427 int cms_EnvelopedData_final(CMS_ContentInfo *cms, BIO *chain);
428 CMS_EnvelopedData *cms_get0_enveloped(CMS_ContentInfo *cms);
430 /* RecipientInfo routines */
431 int cms_env_asn1_ctrl(CMS_RecipientInfo *ri, int cmd);
432 int cms_pkey_get_ri_type(EVP_PKEY *pk);
433 int cms_pkey_is_ri_type_supported(EVP_PKEY *pk, int ri_type);
435 void cms_RecipientInfos_set_cmsctx(CMS_ContentInfo *cms);
438 int cms_RecipientInfo_kari_init(CMS_RecipientInfo *ri, X509 *recip,
439 EVP_PKEY *recipPubKey, X509 *originator,
440 EVP_PKEY *originatorPrivKey, unsigned int flags,
442 int cms_RecipientInfo_kari_encrypt(const CMS_ContentInfo *cms,
443 CMS_RecipientInfo *ri);
446 int cms_RecipientInfo_pwri_crypt(const CMS_ContentInfo *cms,
447 CMS_RecipientInfo *ri, int en_de);
448 /* SignerInfo routines */
449 int CMS_si_check_attributes(const CMS_SignerInfo *si);
450 void cms_SignerInfos_set_cmsctx(CMS_ContentInfo *cms);
454 int ess_check_signing_certs(CMS_SignerInfo *si, STACK_OF(X509) *chain);
456 DECLARE_ASN1_ITEM(CMS_CertificateChoices)
457 DECLARE_ASN1_ITEM(CMS_DigestedData)
458 DECLARE_ASN1_ITEM(CMS_EncryptedData)
459 DECLARE_ASN1_ITEM(CMS_EnvelopedData)
460 DECLARE_ASN1_ITEM(CMS_KEKRecipientInfo)
461 DECLARE_ASN1_ITEM(CMS_KeyAgreeRecipientInfo)
462 DECLARE_ASN1_ITEM(CMS_KeyTransRecipientInfo)
463 DECLARE_ASN1_ITEM(CMS_OriginatorPublicKey)
464 DECLARE_ASN1_ITEM(CMS_OtherKeyAttribute)
465 DECLARE_ASN1_ITEM(CMS_Receipt)
466 DECLARE_ASN1_ITEM(CMS_ReceiptRequest)
467 DECLARE_ASN1_ITEM(CMS_RecipientEncryptedKey)
468 DECLARE_ASN1_ITEM(CMS_RecipientKeyIdentifier)
469 DECLARE_ASN1_ITEM(CMS_RevocationInfoChoice)
470 DECLARE_ASN1_ITEM(CMS_SignedData)
471 DECLARE_ASN1_ITEM(CMS_CompressedData)