2 * Copyright 2008-2022 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_OtherRevocationInfoFormat_st CMS_OtherRevocationInfoFormat;
25 typedef struct CMS_OriginatorInfo_st CMS_OriginatorInfo;
26 typedef struct CMS_EncryptedContentInfo_st CMS_EncryptedContentInfo;
27 typedef struct CMS_DigestedData_st CMS_DigestedData;
28 typedef struct CMS_EncryptedData_st CMS_EncryptedData;
29 typedef struct CMS_AuthenticatedData_st CMS_AuthenticatedData;
30 typedef struct CMS_AuthEnvelopedData_st CMS_AuthEnvelopedData;
31 typedef struct CMS_CompressedData_st CMS_CompressedData;
32 typedef struct CMS_OtherCertificateFormat_st CMS_OtherCertificateFormat;
33 typedef struct CMS_KeyTransRecipientInfo_st CMS_KeyTransRecipientInfo;
34 typedef struct CMS_OriginatorPublicKey_st CMS_OriginatorPublicKey;
35 typedef struct CMS_OriginatorIdentifierOrKey_st CMS_OriginatorIdentifierOrKey;
36 typedef struct CMS_KeyAgreeRecipientInfo_st CMS_KeyAgreeRecipientInfo;
37 typedef struct CMS_RecipientKeyIdentifier_st CMS_RecipientKeyIdentifier;
38 typedef struct CMS_KeyAgreeRecipientIdentifier_st
39 CMS_KeyAgreeRecipientIdentifier;
40 typedef struct CMS_KEKIdentifier_st CMS_KEKIdentifier;
41 typedef struct CMS_KEKRecipientInfo_st CMS_KEKRecipientInfo;
42 typedef struct CMS_PasswordRecipientInfo_st CMS_PasswordRecipientInfo;
43 typedef struct CMS_OtherRecipientInfo_st CMS_OtherRecipientInfo;
44 typedef struct CMS_ReceiptsFrom_st CMS_ReceiptsFrom;
45 typedef struct CMS_CTX_st CMS_CTX;
52 struct CMS_ContentInfo_st {
53 ASN1_OBJECT *contentType;
55 ASN1_OCTET_STRING *data;
56 CMS_SignedData *signedData;
57 CMS_EnvelopedData *envelopedData;
58 CMS_DigestedData *digestedData;
59 CMS_EncryptedData *encryptedData;
60 CMS_AuthEnvelopedData *authEnvelopedData;
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, key and tag */
131 const EVP_CIPHER *cipher;
136 /* Set to 1 if we are debugging decrypt and don't fake keys for MMA */
138 /* Set to 1 if we have no cert and need extra safety measures for MMA */
142 struct CMS_RecipientInfo_st {
145 CMS_KeyTransRecipientInfo *ktri;
146 CMS_KeyAgreeRecipientInfo *kari;
147 CMS_KEKRecipientInfo *kekri;
148 CMS_PasswordRecipientInfo *pwri;
149 CMS_OtherRecipientInfo *ori;
153 typedef CMS_SignerIdentifier CMS_RecipientIdentifier;
155 struct CMS_KeyTransRecipientInfo_st {
157 CMS_RecipientIdentifier *rid;
158 X509_ALGOR *keyEncryptionAlgorithm;
159 ASN1_OCTET_STRING *encryptedKey;
160 /* Recipient Key and cert */
163 /* Public key context for this operation */
165 const CMS_CTX *cms_ctx;
168 struct CMS_KeyAgreeRecipientInfo_st {
170 CMS_OriginatorIdentifierOrKey *originator;
171 ASN1_OCTET_STRING *ukm;
172 X509_ALGOR *keyEncryptionAlgorithm;
173 STACK_OF(CMS_RecipientEncryptedKey) *recipientEncryptedKeys;
174 /* Public key context associated with current operation */
176 /* Cipher context for CEK wrapping */
178 const CMS_CTX *cms_ctx;
181 struct CMS_OriginatorIdentifierOrKey_st {
184 CMS_IssuerAndSerialNumber *issuerAndSerialNumber;
185 ASN1_OCTET_STRING *subjectKeyIdentifier;
186 CMS_OriginatorPublicKey *originatorKey;
190 struct CMS_OriginatorPublicKey_st {
191 X509_ALGOR *algorithm;
192 ASN1_BIT_STRING *publicKey;
195 struct CMS_RecipientEncryptedKey_st {
196 CMS_KeyAgreeRecipientIdentifier *rid;
197 ASN1_OCTET_STRING *encryptedKey;
198 /* Public key associated with this recipient */
202 struct CMS_KeyAgreeRecipientIdentifier_st {
205 CMS_IssuerAndSerialNumber *issuerAndSerialNumber;
206 CMS_RecipientKeyIdentifier *rKeyId;
210 struct CMS_RecipientKeyIdentifier_st {
211 ASN1_OCTET_STRING *subjectKeyIdentifier;
212 ASN1_GENERALIZEDTIME *date;
213 CMS_OtherKeyAttribute *other;
216 struct CMS_KEKRecipientInfo_st {
218 CMS_KEKIdentifier *kekid;
219 X509_ALGOR *keyEncryptionAlgorithm;
220 ASN1_OCTET_STRING *encryptedKey;
221 /* Extra info: symmetric key to use */
224 const CMS_CTX *cms_ctx;
227 struct CMS_KEKIdentifier_st {
228 ASN1_OCTET_STRING *keyIdentifier;
229 ASN1_GENERALIZEDTIME *date;
230 CMS_OtherKeyAttribute *other;
233 struct CMS_PasswordRecipientInfo_st {
235 X509_ALGOR *keyDerivationAlgorithm;
236 X509_ALGOR *keyEncryptionAlgorithm;
237 ASN1_OCTET_STRING *encryptedKey;
238 /* Extra info: password to use */
241 const CMS_CTX *cms_ctx;
244 struct CMS_OtherRecipientInfo_st {
245 ASN1_OBJECT *oriType;
249 struct CMS_DigestedData_st {
251 X509_ALGOR *digestAlgorithm;
252 CMS_EncapsulatedContentInfo *encapContentInfo;
253 ASN1_OCTET_STRING *digest;
256 struct CMS_EncryptedData_st {
258 CMS_EncryptedContentInfo *encryptedContentInfo;
259 STACK_OF(X509_ATTRIBUTE) *unprotectedAttrs;
262 struct CMS_AuthenticatedData_st {
264 CMS_OriginatorInfo *originatorInfo;
265 STACK_OF(CMS_RecipientInfo) *recipientInfos;
266 X509_ALGOR *macAlgorithm;
267 X509_ALGOR *digestAlgorithm;
268 CMS_EncapsulatedContentInfo *encapContentInfo;
269 STACK_OF(X509_ATTRIBUTE) *authAttrs;
270 ASN1_OCTET_STRING *mac;
271 STACK_OF(X509_ATTRIBUTE) *unauthAttrs;
274 struct CMS_AuthEnvelopedData_st {
276 CMS_OriginatorInfo *originatorInfo;
277 STACK_OF(CMS_RecipientInfo) *recipientInfos;
278 CMS_EncryptedContentInfo *authEncryptedContentInfo;
279 STACK_OF(X509_ATTRIBUTE) *authAttrs;
280 ASN1_OCTET_STRING *mac;
281 STACK_OF(X509_ATTRIBUTE) *unauthAttrs;
284 struct CMS_CompressedData_st {
286 X509_ALGOR *compressionAlgorithm;
287 STACK_OF(CMS_RecipientInfo) *recipientInfos;
288 CMS_EncapsulatedContentInfo *encapContentInfo;
291 struct CMS_RevocationInfoChoice_st {
295 CMS_OtherRevocationInfoFormat *other;
299 # define CMS_REVCHOICE_CRL 0
300 # define CMS_REVCHOICE_OTHER 1
302 struct CMS_OtherRevocationInfoFormat_st {
303 ASN1_OBJECT *otherRevInfoFormat;
304 ASN1_TYPE *otherRevInfo;
307 struct CMS_CertificateChoices {
311 ASN1_STRING *extendedCertificate; /* Obsolete */
312 ASN1_STRING *v1AttrCert; /* Left encoded for now */
313 ASN1_STRING *v2AttrCert; /* Left encoded for now */
314 CMS_OtherCertificateFormat *other;
318 # define CMS_CERTCHOICE_CERT 0
319 # define CMS_CERTCHOICE_EXCERT 1
320 # define CMS_CERTCHOICE_V1ACERT 2
321 # define CMS_CERTCHOICE_V2ACERT 3
322 # define CMS_CERTCHOICE_OTHER 4
324 struct CMS_OtherCertificateFormat_st {
325 ASN1_OBJECT *otherCertFormat;
326 ASN1_TYPE *otherCert;
330 * This is also defined in pkcs7.h but we duplicate it to allow the CMS code
331 * to be independent of PKCS#7
334 struct CMS_IssuerAndSerialNumber_st {
336 ASN1_INTEGER *serialNumber;
339 struct CMS_OtherKeyAttribute_st {
340 ASN1_OBJECT *keyAttrId;
346 struct CMS_ReceiptRequest_st {
347 ASN1_OCTET_STRING *signedContentIdentifier;
348 CMS_ReceiptsFrom *receiptsFrom;
349 STACK_OF(GENERAL_NAMES) *receiptsTo;
352 struct CMS_ReceiptsFrom_st {
355 int32_t allOrFirstTier;
356 STACK_OF(GENERAL_NAMES) *receiptList;
360 struct CMS_Receipt_st {
362 ASN1_OBJECT *contentType;
363 ASN1_OCTET_STRING *signedContentIdentifier;
364 ASN1_OCTET_STRING *originatorSignatureValue;
367 DECLARE_ASN1_FUNCTIONS(CMS_ContentInfo)
368 DECLARE_ASN1_ITEM(CMS_SignerInfo)
369 DECLARE_ASN1_ITEM(CMS_IssuerAndSerialNumber)
370 DECLARE_ASN1_ITEM(CMS_Attributes_Sign)
371 DECLARE_ASN1_ITEM(CMS_Attributes_Verify)
372 DECLARE_ASN1_ITEM(CMS_RecipientInfo)
373 DECLARE_ASN1_ITEM(CMS_PasswordRecipientInfo)
374 DECLARE_ASN1_ALLOC_FUNCTIONS(CMS_IssuerAndSerialNumber)
376 # define CMS_SIGNERINFO_ISSUER_SERIAL 0
377 # define CMS_SIGNERINFO_KEYIDENTIFIER 1
379 # define CMS_RECIPINFO_ISSUER_SERIAL 0
380 # define CMS_RECIPINFO_KEYIDENTIFIER 1
382 # define CMS_REK_ISSUER_SERIAL 0
383 # define CMS_REK_KEYIDENTIFIER 1
385 # define CMS_OIK_ISSUER_SERIAL 0
386 # define CMS_OIK_KEYIDENTIFIER 1
387 # define CMS_OIK_PUBKEY 2
389 BIO *ossl_cms_content_bio(CMS_ContentInfo *cms);
390 const CMS_CTX *ossl_cms_get0_cmsctx(const CMS_ContentInfo *cms);
391 OSSL_LIB_CTX *ossl_cms_ctx_get0_libctx(const CMS_CTX *ctx);
392 const char *ossl_cms_ctx_get0_propq(const CMS_CTX *ctx);
393 void ossl_cms_resolve_libctx(CMS_ContentInfo *ci);
395 CMS_ContentInfo *ossl_cms_Data_create(OSSL_LIB_CTX *ctx, const char *propq);
396 int ossl_cms_DataFinal(CMS_ContentInfo *cms, BIO *cmsbio,
397 const unsigned char *precomp_md,
398 unsigned int precomp_mdlen);
400 CMS_ContentInfo *ossl_cms_DigestedData_create(const EVP_MD *md,
401 OSSL_LIB_CTX *libctx,
403 BIO *ossl_cms_DigestedData_init_bio(const CMS_ContentInfo *cms);
404 int ossl_cms_DigestedData_do_final(const CMS_ContentInfo *cms,
405 BIO *chain, int verify);
407 BIO *ossl_cms_SignedData_init_bio(CMS_ContentInfo *cms);
408 int ossl_cms_SignedData_final(CMS_ContentInfo *cms, BIO *chain,
409 const unsigned char *precomp_md,
410 unsigned int precomp_mdlen);
411 int ossl_cms_set1_SignerIdentifier(CMS_SignerIdentifier *sid, X509 *cert,
412 int type, const CMS_CTX *ctx);
413 int ossl_cms_SignerIdentifier_get0_signer_id(CMS_SignerIdentifier *sid,
414 ASN1_OCTET_STRING **keyid,
417 int ossl_cms_SignerIdentifier_cert_cmp(CMS_SignerIdentifier *sid, X509 *cert);
419 CMS_ContentInfo *ossl_cms_CompressedData_create(int comp_nid,
420 OSSL_LIB_CTX *libctx,
422 BIO *ossl_cms_CompressedData_init_bio(const CMS_ContentInfo *cms);
424 BIO *ossl_cms_DigestAlgorithm_init_bio(X509_ALGOR *digestAlgorithm,
426 int ossl_cms_DigestAlgorithm_find_ctx(EVP_MD_CTX *mctx, BIO *chain,
429 int ossl_cms_ias_cert_cmp(CMS_IssuerAndSerialNumber *ias, X509 *cert);
430 int ossl_cms_keyid_cert_cmp(ASN1_OCTET_STRING *keyid, X509 *cert);
431 int ossl_cms_set1_ias(CMS_IssuerAndSerialNumber **pias, X509 *cert);
432 int ossl_cms_set1_keyid(ASN1_OCTET_STRING **pkeyid, X509 *cert);
434 BIO *ossl_cms_EncryptedContent_init_bio(CMS_EncryptedContentInfo *ec,
436 BIO *ossl_cms_EncryptedData_init_bio(const CMS_ContentInfo *cms);
437 int ossl_cms_EncryptedContent_init(CMS_EncryptedContentInfo *ec,
438 const EVP_CIPHER *cipher,
439 const unsigned char *key, size_t keylen,
442 int ossl_cms_Receipt_verify(CMS_ContentInfo *cms, CMS_ContentInfo *req_cms);
443 int ossl_cms_msgSigDigest_add1(CMS_SignerInfo *dest, CMS_SignerInfo *src);
444 ASN1_OCTET_STRING *ossl_cms_encode_Receipt(CMS_SignerInfo *si);
446 BIO *ossl_cms_EnvelopedData_init_bio(CMS_ContentInfo *cms);
447 int ossl_cms_EnvelopedData_final(CMS_ContentInfo *cms, BIO *chain);
448 BIO *ossl_cms_AuthEnvelopedData_init_bio(CMS_ContentInfo *cms);
449 int ossl_cms_AuthEnvelopedData_final(CMS_ContentInfo *cms, BIO *cmsbio);
450 CMS_EnvelopedData *ossl_cms_get0_enveloped(CMS_ContentInfo *cms);
451 CMS_AuthEnvelopedData *ossl_cms_get0_auth_enveloped(CMS_ContentInfo *cms);
452 CMS_EncryptedContentInfo *ossl_cms_get0_env_enc_content(const CMS_ContentInfo *cms);
454 /* RecipientInfo routines */
455 int ossl_cms_env_asn1_ctrl(CMS_RecipientInfo *ri, int cmd);
456 int ossl_cms_pkey_get_ri_type(EVP_PKEY *pk);
457 int ossl_cms_pkey_is_ri_type_supported(EVP_PKEY *pk, int ri_type);
459 void ossl_cms_RecipientInfos_set_cmsctx(CMS_ContentInfo *cms);
462 int ossl_cms_RecipientInfo_kari_init(CMS_RecipientInfo *ri, X509 *recip,
463 EVP_PKEY *recipPubKey, X509 *originator,
464 EVP_PKEY *originatorPrivKey,
467 int ossl_cms_RecipientInfo_kari_encrypt(const CMS_ContentInfo *cms,
468 CMS_RecipientInfo *ri);
471 int ossl_cms_RecipientInfo_pwri_crypt(const CMS_ContentInfo *cms,
472 CMS_RecipientInfo *ri, int en_de);
473 /* SignerInfo routines */
474 int ossl_cms_si_check_attributes(const CMS_SignerInfo *si);
475 void ossl_cms_SignerInfos_set_cmsctx(CMS_ContentInfo *cms);
479 int ossl_cms_check_signing_certs(const CMS_SignerInfo *si,
480 const STACK_OF(X509) *chain);
482 int ossl_cms_dh_envelope(CMS_RecipientInfo *ri, int decrypt);
483 int ossl_cms_ecdh_envelope(CMS_RecipientInfo *ri, int decrypt);
484 int ossl_cms_rsa_envelope(CMS_RecipientInfo *ri, int decrypt);
485 int ossl_cms_rsa_sign(CMS_SignerInfo *si, int verify);
487 DECLARE_ASN1_ITEM(CMS_CertificateChoices)
488 DECLARE_ASN1_ITEM(CMS_DigestedData)
489 DECLARE_ASN1_ITEM(CMS_EncryptedData)
490 DECLARE_ASN1_ITEM(CMS_EnvelopedData)
491 DECLARE_ASN1_ITEM(CMS_AuthEnvelopedData)
492 DECLARE_ASN1_ITEM(CMS_KEKRecipientInfo)
493 DECLARE_ASN1_ITEM(CMS_KeyAgreeRecipientInfo)
494 DECLARE_ASN1_ITEM(CMS_KeyTransRecipientInfo)
495 DECLARE_ASN1_ITEM(CMS_OriginatorPublicKey)
496 DECLARE_ASN1_ITEM(CMS_OtherKeyAttribute)
497 DECLARE_ASN1_ITEM(CMS_Receipt)
498 DECLARE_ASN1_ITEM(CMS_ReceiptRequest)
499 DECLARE_ASN1_ITEM(CMS_RecipientEncryptedKey)
500 DECLARE_ASN1_ITEM(CMS_RecipientKeyIdentifier)
501 DECLARE_ASN1_ITEM(CMS_RevocationInfoChoice)
502 DECLARE_ASN1_ITEM(CMS_SignedData)
503 DECLARE_ASN1_ITEM(CMS_CompressedData)