2 * Copyright 2008-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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 HEADER_CMS_LCL_H
11 # define HEADER_CMS_LCL_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;
47 struct CMS_ContentInfo_st {
48 ASN1_OBJECT *contentType;
50 ASN1_OCTET_STRING *data;
51 CMS_SignedData *signedData;
52 CMS_EnvelopedData *envelopedData;
53 CMS_DigestedData *digestedData;
54 CMS_EncryptedData *encryptedData;
55 CMS_AuthenticatedData *authenticatedData;
56 CMS_CompressedData *compressedData;
63 DEFINE_STACK_OF(CMS_CertificateChoices)
65 struct CMS_SignedData_st {
67 STACK_OF(X509_ALGOR) *digestAlgorithms;
68 CMS_EncapsulatedContentInfo *encapContentInfo;
69 STACK_OF(CMS_CertificateChoices) *certificates;
70 STACK_OF(CMS_RevocationInfoChoice) *crls;
71 STACK_OF(CMS_SignerInfo) *signerInfos;
74 struct CMS_EncapsulatedContentInfo_st {
75 ASN1_OBJECT *eContentType;
76 ASN1_OCTET_STRING *eContent;
77 /* Set to 1 if incomplete structure only part set up */
81 struct CMS_SignerInfo_st {
83 CMS_SignerIdentifier *sid;
84 X509_ALGOR *digestAlgorithm;
85 STACK_OF(X509_ATTRIBUTE) *signedAttrs;
86 X509_ALGOR *signatureAlgorithm;
87 ASN1_OCTET_STRING *signature;
88 STACK_OF(X509_ATTRIBUTE) *unsignedAttrs;
89 /* Signing certificate and key */
92 /* Digest and public key context for alternative parameters */
97 struct CMS_SignerIdentifier_st {
100 CMS_IssuerAndSerialNumber *issuerAndSerialNumber;
101 ASN1_OCTET_STRING *subjectKeyIdentifier;
105 struct CMS_EnvelopedData_st {
107 CMS_OriginatorInfo *originatorInfo;
108 STACK_OF(CMS_RecipientInfo) *recipientInfos;
109 CMS_EncryptedContentInfo *encryptedContentInfo;
110 STACK_OF(X509_ATTRIBUTE) *unprotectedAttrs;
113 struct CMS_OriginatorInfo_st {
114 STACK_OF(CMS_CertificateChoices) *certificates;
115 STACK_OF(CMS_RevocationInfoChoice) *crls;
118 struct CMS_EncryptedContentInfo_st {
119 ASN1_OBJECT *contentType;
120 X509_ALGOR *contentEncryptionAlgorithm;
121 ASN1_OCTET_STRING *encryptedContent;
122 /* Content encryption algorithm and key */
123 const EVP_CIPHER *cipher;
126 /* Set to 1 if we are debugging decrypt and don't fake keys for MMA */
130 struct CMS_RecipientInfo_st {
133 CMS_KeyTransRecipientInfo *ktri;
134 CMS_KeyAgreeRecipientInfo *kari;
135 CMS_KEKRecipientInfo *kekri;
136 CMS_PasswordRecipientInfo *pwri;
137 CMS_OtherRecipientInfo *ori;
141 typedef CMS_SignerIdentifier CMS_RecipientIdentifier;
143 struct CMS_KeyTransRecipientInfo_st {
145 CMS_RecipientIdentifier *rid;
146 X509_ALGOR *keyEncryptionAlgorithm;
147 ASN1_OCTET_STRING *encryptedKey;
148 /* Recipient Key and cert */
151 /* Public key context for this operation */
155 struct CMS_KeyAgreeRecipientInfo_st {
157 CMS_OriginatorIdentifierOrKey *originator;
158 ASN1_OCTET_STRING *ukm;
159 X509_ALGOR *keyEncryptionAlgorithm;
160 STACK_OF(CMS_RecipientEncryptedKey) *recipientEncryptedKeys;
161 /* Public key context associated with current operation */
163 /* Cipher context for CEK wrapping */
167 struct CMS_OriginatorIdentifierOrKey_st {
170 CMS_IssuerAndSerialNumber *issuerAndSerialNumber;
171 ASN1_OCTET_STRING *subjectKeyIdentifier;
172 CMS_OriginatorPublicKey *originatorKey;
176 struct CMS_OriginatorPublicKey_st {
177 X509_ALGOR *algorithm;
178 ASN1_BIT_STRING *publicKey;
181 struct CMS_RecipientEncryptedKey_st {
182 CMS_KeyAgreeRecipientIdentifier *rid;
183 ASN1_OCTET_STRING *encryptedKey;
184 /* Public key associated with this recipient */
188 struct CMS_KeyAgreeRecipientIdentifier_st {
191 CMS_IssuerAndSerialNumber *issuerAndSerialNumber;
192 CMS_RecipientKeyIdentifier *rKeyId;
196 struct CMS_RecipientKeyIdentifier_st {
197 ASN1_OCTET_STRING *subjectKeyIdentifier;
198 ASN1_GENERALIZEDTIME *date;
199 CMS_OtherKeyAttribute *other;
202 struct CMS_KEKRecipientInfo_st {
204 CMS_KEKIdentifier *kekid;
205 X509_ALGOR *keyEncryptionAlgorithm;
206 ASN1_OCTET_STRING *encryptedKey;
207 /* Extra info: symmetric key to use */
212 struct CMS_KEKIdentifier_st {
213 ASN1_OCTET_STRING *keyIdentifier;
214 ASN1_GENERALIZEDTIME *date;
215 CMS_OtherKeyAttribute *other;
218 struct CMS_PasswordRecipientInfo_st {
220 X509_ALGOR *keyDerivationAlgorithm;
221 X509_ALGOR *keyEncryptionAlgorithm;
222 ASN1_OCTET_STRING *encryptedKey;
223 /* Extra info: password to use */
228 struct CMS_OtherRecipientInfo_st {
229 ASN1_OBJECT *oriType;
233 struct CMS_DigestedData_st {
235 X509_ALGOR *digestAlgorithm;
236 CMS_EncapsulatedContentInfo *encapContentInfo;
237 ASN1_OCTET_STRING *digest;
240 struct CMS_EncryptedData_st {
242 CMS_EncryptedContentInfo *encryptedContentInfo;
243 STACK_OF(X509_ATTRIBUTE) *unprotectedAttrs;
246 struct CMS_AuthenticatedData_st {
248 CMS_OriginatorInfo *originatorInfo;
249 STACK_OF(CMS_RecipientInfo) *recipientInfos;
250 X509_ALGOR *macAlgorithm;
251 X509_ALGOR *digestAlgorithm;
252 CMS_EncapsulatedContentInfo *encapContentInfo;
253 STACK_OF(X509_ATTRIBUTE) *authAttrs;
254 ASN1_OCTET_STRING *mac;
255 STACK_OF(X509_ATTRIBUTE) *unauthAttrs;
258 struct CMS_CompressedData_st {
260 X509_ALGOR *compressionAlgorithm;
261 STACK_OF(CMS_RecipientInfo) *recipientInfos;
262 CMS_EncapsulatedContentInfo *encapContentInfo;
265 struct CMS_RevocationInfoChoice_st {
269 CMS_OtherRevocationInfoFormat *other;
273 # define CMS_REVCHOICE_CRL 0
274 # define CMS_REVCHOICE_OTHER 1
276 struct CMS_OtherRevocationInfoFormat_st {
277 ASN1_OBJECT *otherRevInfoFormat;
278 ASN1_TYPE *otherRevInfo;
281 struct CMS_CertificateChoices {
285 ASN1_STRING *extendedCertificate; /* Obsolete */
286 ASN1_STRING *v1AttrCert; /* Left encoded for now */
287 ASN1_STRING *v2AttrCert; /* Left encoded for now */
288 CMS_OtherCertificateFormat *other;
292 # define CMS_CERTCHOICE_CERT 0
293 # define CMS_CERTCHOICE_EXCERT 1
294 # define CMS_CERTCHOICE_V1ACERT 2
295 # define CMS_CERTCHOICE_V2ACERT 3
296 # define CMS_CERTCHOICE_OTHER 4
298 struct CMS_OtherCertificateFormat_st {
299 ASN1_OBJECT *otherCertFormat;
300 ASN1_TYPE *otherCert;
304 * This is also defined in pkcs7.h but we duplicate it to allow the CMS code
305 * to be independent of PKCS#7
308 struct CMS_IssuerAndSerialNumber_st {
310 ASN1_INTEGER *serialNumber;
313 struct CMS_OtherKeyAttribute_st {
314 ASN1_OBJECT *keyAttrId;
320 # ifdef HEADER_X509V3_H
322 struct CMS_ReceiptRequest_st {
323 ASN1_OCTET_STRING *signedContentIdentifier;
324 CMS_ReceiptsFrom *receiptsFrom;
325 STACK_OF(GENERAL_NAMES) *receiptsTo;
328 struct CMS_ReceiptsFrom_st {
331 int32_t allOrFirstTier;
332 STACK_OF(GENERAL_NAMES) *receiptList;
337 struct CMS_Receipt_st {
339 ASN1_OBJECT *contentType;
340 ASN1_OCTET_STRING *signedContentIdentifier;
341 ASN1_OCTET_STRING *originatorSignatureValue;
344 DECLARE_ASN1_FUNCTIONS(CMS_ContentInfo)
345 DECLARE_ASN1_ITEM(CMS_SignerInfo)
346 DECLARE_ASN1_ITEM(CMS_IssuerAndSerialNumber)
347 DECLARE_ASN1_ITEM(CMS_Attributes_Sign)
348 DECLARE_ASN1_ITEM(CMS_Attributes_Verify)
349 DECLARE_ASN1_ITEM(CMS_RecipientInfo)
350 DECLARE_ASN1_ITEM(CMS_PasswordRecipientInfo)
351 DECLARE_ASN1_ALLOC_FUNCTIONS(CMS_IssuerAndSerialNumber)
353 # define CMS_SIGNERINFO_ISSUER_SERIAL 0
354 # define CMS_SIGNERINFO_KEYIDENTIFIER 1
356 # define CMS_RECIPINFO_ISSUER_SERIAL 0
357 # define CMS_RECIPINFO_KEYIDENTIFIER 1
359 # define CMS_REK_ISSUER_SERIAL 0
360 # define CMS_REK_KEYIDENTIFIER 1
362 # define CMS_OIK_ISSUER_SERIAL 0
363 # define CMS_OIK_KEYIDENTIFIER 1
364 # define CMS_OIK_PUBKEY 2
366 BIO *cms_content_bio(CMS_ContentInfo *cms);
368 CMS_ContentInfo *cms_Data_create(void);
370 CMS_ContentInfo *cms_DigestedData_create(const EVP_MD *md);
371 BIO *cms_DigestedData_init_bio(CMS_ContentInfo *cms);
372 int cms_DigestedData_do_final(CMS_ContentInfo *cms, BIO *chain, int verify);
374 BIO *cms_SignedData_init_bio(CMS_ContentInfo *cms);
375 int cms_SignedData_final(CMS_ContentInfo *cms, BIO *chain);
376 int cms_set1_SignerIdentifier(CMS_SignerIdentifier *sid, X509 *cert,
378 int cms_SignerIdentifier_get0_signer_id(CMS_SignerIdentifier *sid,
379 ASN1_OCTET_STRING **keyid,
382 int cms_SignerIdentifier_cert_cmp(CMS_SignerIdentifier *sid, X509 *cert);
384 CMS_ContentInfo *cms_CompressedData_create(int comp_nid);
385 BIO *cms_CompressedData_init_bio(CMS_ContentInfo *cms);
387 BIO *cms_DigestAlgorithm_init_bio(X509_ALGOR *digestAlgorithm);
388 int cms_DigestAlgorithm_find_ctx(EVP_MD_CTX *mctx, BIO *chain,
391 int cms_ias_cert_cmp(CMS_IssuerAndSerialNumber *ias, X509 *cert);
392 int cms_keyid_cert_cmp(ASN1_OCTET_STRING *keyid, X509 *cert);
393 int cms_set1_ias(CMS_IssuerAndSerialNumber **pias, X509 *cert);
394 int cms_set1_keyid(ASN1_OCTET_STRING **pkeyid, X509 *cert);
396 BIO *cms_EncryptedContent_init_bio(CMS_EncryptedContentInfo *ec);
397 BIO *cms_EncryptedData_init_bio(CMS_ContentInfo *cms);
398 int cms_EncryptedContent_init(CMS_EncryptedContentInfo *ec,
399 const EVP_CIPHER *cipher,
400 const unsigned char *key, size_t keylen);
402 int cms_Receipt_verify(CMS_ContentInfo *cms, CMS_ContentInfo *req_cms);
403 int cms_msgSigDigest_add1(CMS_SignerInfo *dest, CMS_SignerInfo *src);
404 ASN1_OCTET_STRING *cms_encode_Receipt(CMS_SignerInfo *si);
406 BIO *cms_EnvelopedData_init_bio(CMS_ContentInfo *cms);
407 CMS_EnvelopedData *cms_get0_enveloped(CMS_ContentInfo *cms);
408 int cms_env_asn1_ctrl(CMS_RecipientInfo *ri, int cmd);
409 int cms_pkey_get_ri_type(EVP_PKEY *pk);
411 int cms_RecipientInfo_kari_init(CMS_RecipientInfo *ri, X509 *recip,
412 EVP_PKEY *pk, unsigned int flags);
413 int cms_RecipientInfo_kari_encrypt(CMS_ContentInfo *cms,
414 CMS_RecipientInfo *ri);
417 int cms_RecipientInfo_pwri_crypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri,
419 /* SignerInfo routines */
420 int CMS_si_check_attributes(const CMS_SignerInfo *si);
422 DECLARE_ASN1_ITEM(CMS_CertificateChoices)
423 DECLARE_ASN1_ITEM(CMS_DigestedData)
424 DECLARE_ASN1_ITEM(CMS_EncryptedData)
425 DECLARE_ASN1_ITEM(CMS_EnvelopedData)
426 DECLARE_ASN1_ITEM(CMS_KEKRecipientInfo)
427 DECLARE_ASN1_ITEM(CMS_KeyAgreeRecipientInfo)
428 DECLARE_ASN1_ITEM(CMS_KeyTransRecipientInfo)
429 DECLARE_ASN1_ITEM(CMS_OriginatorPublicKey)
430 DECLARE_ASN1_ITEM(CMS_OtherKeyAttribute)
431 DECLARE_ASN1_ITEM(CMS_Receipt)
432 DECLARE_ASN1_ITEM(CMS_ReceiptRequest)
433 DECLARE_ASN1_ITEM(CMS_RecipientEncryptedKey)
434 DECLARE_ASN1_ITEM(CMS_RecipientKeyIdentifier)
435 DECLARE_ASN1_ITEM(CMS_RevocationInfoChoice)
436 DECLARE_ASN1_ITEM(CMS_SignedData)
437 DECLARE_ASN1_ITEM(CMS_CompressedData)