2 * Copyright 2008-2023 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_EncryptedContentInfo
)
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
)