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_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_DigestedData_st CMS_DigestedData;
29 typedef struct CMS_EncryptedData_st CMS_EncryptedData;
30 typedef struct CMS_AuthenticatedData_st CMS_AuthenticatedData;
31 typedef struct CMS_AuthEnvelopedData_st CMS_AuthEnvelopedData;
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_AuthEnvelopedData *authEnvelopedData;
62 CMS_AuthenticatedData *authenticatedData;
63 CMS_CompressedData *compressedData;
71 DEFINE_STACK_OF(CMS_CertificateChoices)
73 struct CMS_SignedData_st {
75 STACK_OF(X509_ALGOR) *digestAlgorithms;
76 CMS_EncapsulatedContentInfo *encapContentInfo;
77 STACK_OF(CMS_CertificateChoices) *certificates;
78 STACK_OF(CMS_RevocationInfoChoice) *crls;
79 STACK_OF(CMS_SignerInfo) *signerInfos;
82 struct CMS_EncapsulatedContentInfo_st {
83 ASN1_OBJECT *eContentType;
84 ASN1_OCTET_STRING *eContent;
85 /* Set to 1 if incomplete structure only part set up */
89 struct CMS_SignerInfo_st {
91 CMS_SignerIdentifier *sid;
92 X509_ALGOR *digestAlgorithm;
93 STACK_OF(X509_ATTRIBUTE) *signedAttrs;
94 X509_ALGOR *signatureAlgorithm;
95 ASN1_OCTET_STRING *signature;
96 STACK_OF(X509_ATTRIBUTE) *unsignedAttrs;
97 /* Signing certificate and key */
100 /* Digest and public key context for alternative parameters */
103 const CMS_CTX *cms_ctx;
106 struct CMS_SignerIdentifier_st {
109 CMS_IssuerAndSerialNumber *issuerAndSerialNumber;
110 ASN1_OCTET_STRING *subjectKeyIdentifier;
114 struct CMS_EnvelopedData_st {
116 CMS_OriginatorInfo *originatorInfo;
117 STACK_OF(CMS_RecipientInfo) *recipientInfos;
118 CMS_EncryptedContentInfo *encryptedContentInfo;
119 STACK_OF(X509_ATTRIBUTE) *unprotectedAttrs;
122 struct CMS_OriginatorInfo_st {
123 STACK_OF(CMS_CertificateChoices) *certificates;
124 STACK_OF(CMS_RevocationInfoChoice) *crls;
127 struct CMS_EncryptedContentInfo_st {
128 ASN1_OBJECT *contentType;
129 X509_ALGOR *contentEncryptionAlgorithm;
130 ASN1_OCTET_STRING *encryptedContent;
131 /* Content encryption algorithm, key and tag */
132 const EVP_CIPHER *cipher;
137 /* Set to 1 if we are debugging decrypt and don't fake keys for MMA */
139 /* Set to 1 if we have no cert and need extra safety measures for MMA */
143 struct CMS_RecipientInfo_st {
146 CMS_KeyTransRecipientInfo *ktri;
147 CMS_KeyAgreeRecipientInfo *kari;
148 CMS_KEKRecipientInfo *kekri;
149 CMS_PasswordRecipientInfo *pwri;
150 CMS_OtherRecipientInfo *ori;
154 typedef CMS_SignerIdentifier CMS_RecipientIdentifier;
156 struct CMS_KeyTransRecipientInfo_st {
158 CMS_RecipientIdentifier *rid;
159 X509_ALGOR *keyEncryptionAlgorithm;
160 ASN1_OCTET_STRING *encryptedKey;
161 /* Recipient Key and cert */
164 /* Public key context for this operation */
166 const CMS_CTX *cms_ctx;
169 struct CMS_KeyAgreeRecipientInfo_st {
171 CMS_OriginatorIdentifierOrKey *originator;
172 ASN1_OCTET_STRING *ukm;
173 X509_ALGOR *keyEncryptionAlgorithm;
174 STACK_OF(CMS_RecipientEncryptedKey) *recipientEncryptedKeys;
175 /* Public key context associated with current operation */
177 /* Cipher context for CEK wrapping */
179 const CMS_CTX *cms_ctx;
182 struct CMS_OriginatorIdentifierOrKey_st {
185 CMS_IssuerAndSerialNumber *issuerAndSerialNumber;
186 ASN1_OCTET_STRING *subjectKeyIdentifier;
187 CMS_OriginatorPublicKey *originatorKey;
191 struct CMS_OriginatorPublicKey_st {
192 X509_ALGOR *algorithm;
193 ASN1_BIT_STRING *publicKey;
196 struct CMS_RecipientEncryptedKey_st {
197 CMS_KeyAgreeRecipientIdentifier *rid;
198 ASN1_OCTET_STRING *encryptedKey;
199 /* Public key associated with this recipient */
203 struct CMS_KeyAgreeRecipientIdentifier_st {
206 CMS_IssuerAndSerialNumber *issuerAndSerialNumber;
207 CMS_RecipientKeyIdentifier *rKeyId;
211 struct CMS_RecipientKeyIdentifier_st {
212 ASN1_OCTET_STRING *subjectKeyIdentifier;
213 ASN1_GENERALIZEDTIME *date;
214 CMS_OtherKeyAttribute *other;
217 struct CMS_KEKRecipientInfo_st {
219 CMS_KEKIdentifier *kekid;
220 X509_ALGOR *keyEncryptionAlgorithm;
221 ASN1_OCTET_STRING *encryptedKey;
222 /* Extra info: symmetric key to use */
225 const CMS_CTX *cms_ctx;
228 struct CMS_KEKIdentifier_st {
229 ASN1_OCTET_STRING *keyIdentifier;
230 ASN1_GENERALIZEDTIME *date;
231 CMS_OtherKeyAttribute *other;
234 struct CMS_PasswordRecipientInfo_st {
236 X509_ALGOR *keyDerivationAlgorithm;
237 X509_ALGOR *keyEncryptionAlgorithm;
238 ASN1_OCTET_STRING *encryptedKey;
239 /* Extra info: password to use */
242 const CMS_CTX *cms_ctx;
245 struct CMS_OtherRecipientInfo_st {
246 ASN1_OBJECT *oriType;
250 struct CMS_DigestedData_st {
252 X509_ALGOR *digestAlgorithm;
253 CMS_EncapsulatedContentInfo *encapContentInfo;
254 ASN1_OCTET_STRING *digest;
257 struct CMS_EncryptedData_st {
259 CMS_EncryptedContentInfo *encryptedContentInfo;
260 STACK_OF(X509_ATTRIBUTE) *unprotectedAttrs;
263 struct CMS_AuthenticatedData_st {
265 CMS_OriginatorInfo *originatorInfo;
266 STACK_OF(CMS_RecipientInfo) *recipientInfos;
267 X509_ALGOR *macAlgorithm;
268 X509_ALGOR *digestAlgorithm;
269 CMS_EncapsulatedContentInfo *encapContentInfo;
270 STACK_OF(X509_ATTRIBUTE) *authAttrs;
271 ASN1_OCTET_STRING *mac;
272 STACK_OF(X509_ATTRIBUTE) *unauthAttrs;
275 struct CMS_AuthEnvelopedData_st {
277 CMS_OriginatorInfo *originatorInfo;
278 STACK_OF(CMS_RecipientInfo) *recipientInfos;
279 CMS_EncryptedContentInfo *authEncryptedContentInfo;
280 STACK_OF(X509_ATTRIBUTE) *authAttrs;
281 ASN1_OCTET_STRING *mac;
282 STACK_OF(X509_ATTRIBUTE) *unauthAttrs;
285 struct CMS_CompressedData_st {
287 X509_ALGOR *compressionAlgorithm;
288 STACK_OF(CMS_RecipientInfo) *recipientInfos;
289 CMS_EncapsulatedContentInfo *encapContentInfo;
292 struct CMS_RevocationInfoChoice_st {
296 CMS_OtherRevocationInfoFormat *other;
300 # define CMS_REVCHOICE_CRL 0
301 # define CMS_REVCHOICE_OTHER 1
303 struct CMS_OtherRevocationInfoFormat_st {
304 ASN1_OBJECT *otherRevInfoFormat;
305 ASN1_TYPE *otherRevInfo;
308 struct CMS_CertificateChoices {
312 ASN1_STRING *extendedCertificate; /* Obsolete */
313 ASN1_STRING *v1AttrCert; /* Left encoded for now */
314 ASN1_STRING *v2AttrCert; /* Left encoded for now */
315 CMS_OtherCertificateFormat *other;
319 # define CMS_CERTCHOICE_CERT 0
320 # define CMS_CERTCHOICE_EXCERT 1
321 # define CMS_CERTCHOICE_V1ACERT 2
322 # define CMS_CERTCHOICE_V2ACERT 3
323 # define CMS_CERTCHOICE_OTHER 4
325 struct CMS_OtherCertificateFormat_st {
326 ASN1_OBJECT *otherCertFormat;
327 ASN1_TYPE *otherCert;
331 * This is also defined in pkcs7.h but we duplicate it to allow the CMS code
332 * to be independent of PKCS#7
335 struct CMS_IssuerAndSerialNumber_st {
337 ASN1_INTEGER *serialNumber;
340 struct CMS_OtherKeyAttribute_st {
341 ASN1_OBJECT *keyAttrId;
347 struct CMS_ReceiptRequest_st {
348 ASN1_OCTET_STRING *signedContentIdentifier;
349 CMS_ReceiptsFrom *receiptsFrom;
350 STACK_OF(GENERAL_NAMES) *receiptsTo;
353 struct CMS_ReceiptsFrom_st {
356 int32_t allOrFirstTier;
357 STACK_OF(GENERAL_NAMES) *receiptList;
361 struct CMS_Receipt_st {
363 ASN1_OBJECT *contentType;
364 ASN1_OCTET_STRING *signedContentIdentifier;
365 ASN1_OCTET_STRING *originatorSignatureValue;
368 DECLARE_ASN1_FUNCTIONS(CMS_ContentInfo)
369 DECLARE_ASN1_ITEM(CMS_SignerInfo)
370 DECLARE_ASN1_ITEM(CMS_IssuerAndSerialNumber)
371 DECLARE_ASN1_ITEM(CMS_Attributes_Sign)
372 DECLARE_ASN1_ITEM(CMS_Attributes_Verify)
373 DECLARE_ASN1_ITEM(CMS_RecipientInfo)
374 DECLARE_ASN1_ITEM(CMS_PasswordRecipientInfo)
375 DECLARE_ASN1_ALLOC_FUNCTIONS(CMS_IssuerAndSerialNumber)
377 # define CMS_SIGNERINFO_ISSUER_SERIAL 0
378 # define CMS_SIGNERINFO_KEYIDENTIFIER 1
380 # define CMS_RECIPINFO_ISSUER_SERIAL 0
381 # define CMS_RECIPINFO_KEYIDENTIFIER 1
383 # define CMS_REK_ISSUER_SERIAL 0
384 # define CMS_REK_KEYIDENTIFIER 1
386 # define CMS_OIK_ISSUER_SERIAL 0
387 # define CMS_OIK_KEYIDENTIFIER 1
388 # define CMS_OIK_PUBKEY 2
390 BIO *ossl_cms_content_bio(CMS_ContentInfo *cms);
391 const CMS_CTX *ossl_cms_get0_cmsctx(const CMS_ContentInfo *cms);
392 OSSL_LIB_CTX *ossl_cms_ctx_get0_libctx(const CMS_CTX *ctx);
393 const char *ossl_cms_ctx_get0_propq(const CMS_CTX *ctx);
394 void ossl_cms_resolve_libctx(CMS_ContentInfo *ci);
396 CMS_ContentInfo *ossl_cms_Data_create(OSSL_LIB_CTX *ctx, const char *propq);
397 int ossl_cms_DataFinal(CMS_ContentInfo *cms, BIO *cmsbio,
398 const unsigned char *precomp_md,
399 unsigned int precomp_mdlen);
401 CMS_ContentInfo *ossl_cms_DigestedData_create(const EVP_MD *md,
402 OSSL_LIB_CTX *libctx,
404 BIO *ossl_cms_DigestedData_init_bio(const CMS_ContentInfo *cms);
405 int ossl_cms_DigestedData_do_final(const CMS_ContentInfo *cms,
406 BIO *chain, int verify);
408 BIO *ossl_cms_SignedData_init_bio(CMS_ContentInfo *cms);
409 int ossl_cms_SignedData_final(CMS_ContentInfo *cms, BIO *chain,
410 const unsigned char *precomp_md,
411 unsigned int precomp_mdlen);
412 int ossl_cms_set1_SignerIdentifier(CMS_SignerIdentifier *sid, X509 *cert,
413 int type, const CMS_CTX *ctx);
414 int ossl_cms_SignerIdentifier_get0_signer_id(CMS_SignerIdentifier *sid,
415 ASN1_OCTET_STRING **keyid,
418 int ossl_cms_SignerIdentifier_cert_cmp(CMS_SignerIdentifier *sid, X509 *cert);
420 CMS_ContentInfo *ossl_cms_CompressedData_create(int comp_nid,
421 OSSL_LIB_CTX *libctx,
423 BIO *ossl_cms_CompressedData_init_bio(const CMS_ContentInfo *cms);
425 BIO *ossl_cms_DigestAlgorithm_init_bio(X509_ALGOR *digestAlgorithm,
427 int ossl_cms_DigestAlgorithm_find_ctx(EVP_MD_CTX *mctx, BIO *chain,
430 int ossl_cms_ias_cert_cmp(CMS_IssuerAndSerialNumber *ias, X509 *cert);
431 int ossl_cms_keyid_cert_cmp(ASN1_OCTET_STRING *keyid, X509 *cert);
432 int ossl_cms_set1_ias(CMS_IssuerAndSerialNumber **pias, X509 *cert);
433 int ossl_cms_set1_keyid(ASN1_OCTET_STRING **pkeyid, X509 *cert);
435 BIO *ossl_cms_EncryptedContent_init_bio(CMS_EncryptedContentInfo *ec,
437 BIO *ossl_cms_EncryptedData_init_bio(const CMS_ContentInfo *cms);
438 int ossl_cms_EncryptedContent_init(CMS_EncryptedContentInfo *ec,
439 const EVP_CIPHER *cipher,
440 const unsigned char *key, size_t keylen,
443 int ossl_cms_Receipt_verify(CMS_ContentInfo *cms, CMS_ContentInfo *req_cms);
444 int ossl_cms_msgSigDigest_add1(CMS_SignerInfo *dest, CMS_SignerInfo *src);
445 ASN1_OCTET_STRING *ossl_cms_encode_Receipt(CMS_SignerInfo *si);
447 BIO *ossl_cms_EnvelopedData_init_bio(CMS_ContentInfo *cms);
448 int ossl_cms_EnvelopedData_final(CMS_ContentInfo *cms, BIO *chain);
449 BIO *ossl_cms_AuthEnvelopedData_init_bio(CMS_ContentInfo *cms);
450 int ossl_cms_AuthEnvelopedData_final(CMS_ContentInfo *cms, BIO *cmsbio);
451 CMS_EnvelopedData *ossl_cms_get0_enveloped(CMS_ContentInfo *cms);
452 CMS_AuthEnvelopedData *ossl_cms_get0_auth_enveloped(CMS_ContentInfo *cms);
453 CMS_EncryptedContentInfo *ossl_cms_get0_env_enc_content(const CMS_ContentInfo *cms);
455 /* RecipientInfo routines */
456 int ossl_cms_env_asn1_ctrl(CMS_RecipientInfo *ri, int cmd);
457 int ossl_cms_pkey_get_ri_type(EVP_PKEY *pk);
458 int ossl_cms_pkey_is_ri_type_supported(EVP_PKEY *pk, int ri_type);
460 void ossl_cms_RecipientInfos_set_cmsctx(CMS_ContentInfo *cms);
463 int ossl_cms_RecipientInfo_kari_init(CMS_RecipientInfo *ri, X509 *recip,
464 EVP_PKEY *recipPubKey, X509 *originator,
465 EVP_PKEY *originatorPrivKey,
468 int ossl_cms_RecipientInfo_kari_encrypt(const CMS_ContentInfo *cms,
469 CMS_RecipientInfo *ri);
472 int ossl_cms_RecipientInfo_pwri_crypt(const CMS_ContentInfo *cms,
473 CMS_RecipientInfo *ri, int en_de);
474 /* SignerInfo routines */
475 int ossl_cms_si_check_attributes(const CMS_SignerInfo *si);
476 void ossl_cms_SignerInfos_set_cmsctx(CMS_ContentInfo *cms);
480 int ossl_cms_check_signing_certs(const CMS_SignerInfo *si,
481 const STACK_OF(X509) *chain);
483 int ossl_cms_dh_envelope(CMS_RecipientInfo *ri, int decrypt);
484 int ossl_cms_ecdh_envelope(CMS_RecipientInfo *ri, int decrypt);
485 int ossl_cms_rsa_envelope(CMS_RecipientInfo *ri, int decrypt);
486 int ossl_cms_rsa_sign(CMS_SignerInfo *si, int verify);
488 DECLARE_ASN1_ITEM(CMS_CertificateChoices)
489 DECLARE_ASN1_ITEM(CMS_DigestedData)
490 DECLARE_ASN1_ITEM(CMS_EncryptedData)
491 DECLARE_ASN1_ITEM(CMS_EnvelopedData)
492 DECLARE_ASN1_ITEM(CMS_AuthEnvelopedData)
493 DECLARE_ASN1_ITEM(CMS_KEKRecipientInfo)
494 DECLARE_ASN1_ITEM(CMS_KeyAgreeRecipientInfo)
495 DECLARE_ASN1_ITEM(CMS_KeyTransRecipientInfo)
496 DECLARE_ASN1_ITEM(CMS_OriginatorPublicKey)
497 DECLARE_ASN1_ITEM(CMS_OtherKeyAttribute)
498 DECLARE_ASN1_ITEM(CMS_Receipt)
499 DECLARE_ASN1_ITEM(CMS_ReceiptRequest)
500 DECLARE_ASN1_ITEM(CMS_RecipientEncryptedKey)
501 DECLARE_ASN1_ITEM(CMS_RecipientKeyIdentifier)
502 DECLARE_ASN1_ITEM(CMS_RevocationInfoChoice)
503 DECLARE_ASN1_ITEM(CMS_SignedData)
504 DECLARE_ASN1_ITEM(CMS_CompressedData)