2 * Copyright 2008-2016 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
17 # include <openssl/x509.h>
20 * Cryptographic message syntax (CMS) structures: taken from RFC3852
23 /* Forward references */
25 typedef struct CMS_IssuerAndSerialNumber_st CMS_IssuerAndSerialNumber;
26 typedef struct CMS_EncapsulatedContentInfo_st CMS_EncapsulatedContentInfo;
27 typedef struct CMS_SignerIdentifier_st CMS_SignerIdentifier;
28 typedef struct CMS_SignedData_st CMS_SignedData;
29 typedef struct CMS_OtherRevocationInfoFormat_st CMS_OtherRevocationInfoFormat;
30 typedef struct CMS_OriginatorInfo_st CMS_OriginatorInfo;
31 typedef struct CMS_EncryptedContentInfo_st CMS_EncryptedContentInfo;
32 typedef struct CMS_EnvelopedData_st CMS_EnvelopedData;
33 typedef struct CMS_DigestedData_st CMS_DigestedData;
34 typedef struct CMS_EncryptedData_st CMS_EncryptedData;
35 typedef struct CMS_AuthenticatedData_st CMS_AuthenticatedData;
36 typedef struct CMS_CompressedData_st CMS_CompressedData;
37 typedef struct CMS_OtherCertificateFormat_st CMS_OtherCertificateFormat;
38 typedef struct CMS_KeyTransRecipientInfo_st CMS_KeyTransRecipientInfo;
39 typedef struct CMS_OriginatorPublicKey_st CMS_OriginatorPublicKey;
40 typedef struct CMS_OriginatorIdentifierOrKey_st CMS_OriginatorIdentifierOrKey;
41 typedef struct CMS_KeyAgreeRecipientInfo_st CMS_KeyAgreeRecipientInfo;
42 typedef struct CMS_RecipientKeyIdentifier_st CMS_RecipientKeyIdentifier;
43 typedef struct CMS_KeyAgreeRecipientIdentifier_st
44 CMS_KeyAgreeRecipientIdentifier;
45 typedef struct CMS_KEKIdentifier_st CMS_KEKIdentifier;
46 typedef struct CMS_KEKRecipientInfo_st CMS_KEKRecipientInfo;
47 typedef struct CMS_PasswordRecipientInfo_st CMS_PasswordRecipientInfo;
48 typedef struct CMS_OtherRecipientInfo_st CMS_OtherRecipientInfo;
49 typedef struct CMS_ReceiptsFrom_st CMS_ReceiptsFrom;
51 struct CMS_ContentInfo_st {
52 ASN1_OBJECT *contentType;
54 ASN1_OCTET_STRING *data;
55 CMS_SignedData *signedData;
56 CMS_EnvelopedData *envelopedData;
57 CMS_DigestedData *digestedData;
58 CMS_EncryptedData *encryptedData;
59 CMS_AuthenticatedData *authenticatedData;
60 CMS_CompressedData *compressedData;
67 DEFINE_STACK_OF(CMS_CertificateChoices)
69 struct CMS_SignedData_st {
71 STACK_OF(X509_ALGOR) *digestAlgorithms;
72 CMS_EncapsulatedContentInfo *encapContentInfo;
73 STACK_OF(CMS_CertificateChoices) *certificates;
74 STACK_OF(CMS_RevocationInfoChoice) *crls;
75 STACK_OF(CMS_SignerInfo) *signerInfos;
78 struct CMS_EncapsulatedContentInfo_st {
79 ASN1_OBJECT *eContentType;
80 ASN1_OCTET_STRING *eContent;
81 /* Set to 1 if incomplete structure only part set up */
85 struct CMS_SignerInfo_st {
87 CMS_SignerIdentifier *sid;
88 X509_ALGOR *digestAlgorithm;
89 STACK_OF(X509_ATTRIBUTE) *signedAttrs;
90 X509_ALGOR *signatureAlgorithm;
91 ASN1_OCTET_STRING *signature;
92 STACK_OF(X509_ATTRIBUTE) *unsignedAttrs;
93 /* Signing certificate and key */
96 /* Digest and public key context for alternative parameters */
101 struct CMS_SignerIdentifier_st {
104 CMS_IssuerAndSerialNumber *issuerAndSerialNumber;
105 ASN1_OCTET_STRING *subjectKeyIdentifier;
109 struct CMS_EnvelopedData_st {
111 CMS_OriginatorInfo *originatorInfo;
112 STACK_OF(CMS_RecipientInfo) *recipientInfos;
113 CMS_EncryptedContentInfo *encryptedContentInfo;
114 STACK_OF(X509_ATTRIBUTE) *unprotectedAttrs;
117 struct CMS_OriginatorInfo_st {
118 STACK_OF(CMS_CertificateChoices) *certificates;
119 STACK_OF(CMS_RevocationInfoChoice) *crls;
122 struct CMS_EncryptedContentInfo_st {
123 ASN1_OBJECT *contentType;
124 X509_ALGOR *contentEncryptionAlgorithm;
125 ASN1_OCTET_STRING *encryptedContent;
126 /* Content encryption algorithm and key */
127 const EVP_CIPHER *cipher;
130 /* Set to 1 if we are debugging decrypt and don't fake keys for MMA */
134 struct CMS_RecipientInfo_st {
137 CMS_KeyTransRecipientInfo *ktri;
138 CMS_KeyAgreeRecipientInfo *kari;
139 CMS_KEKRecipientInfo *kekri;
140 CMS_PasswordRecipientInfo *pwri;
141 CMS_OtherRecipientInfo *ori;
145 typedef CMS_SignerIdentifier CMS_RecipientIdentifier;
147 struct CMS_KeyTransRecipientInfo_st {
149 CMS_RecipientIdentifier *rid;
150 X509_ALGOR *keyEncryptionAlgorithm;
151 ASN1_OCTET_STRING *encryptedKey;
152 /* Recipient Key and cert */
155 /* Public key context for this operation */
159 struct CMS_KeyAgreeRecipientInfo_st {
161 CMS_OriginatorIdentifierOrKey *originator;
162 ASN1_OCTET_STRING *ukm;
163 X509_ALGOR *keyEncryptionAlgorithm;
164 STACK_OF(CMS_RecipientEncryptedKey) *recipientEncryptedKeys;
165 /* Public key context associated with current operation */
167 /* Cipher context for CEK wrapping */
171 struct CMS_OriginatorIdentifierOrKey_st {
174 CMS_IssuerAndSerialNumber *issuerAndSerialNumber;
175 ASN1_OCTET_STRING *subjectKeyIdentifier;
176 CMS_OriginatorPublicKey *originatorKey;
180 struct CMS_OriginatorPublicKey_st {
181 X509_ALGOR *algorithm;
182 ASN1_BIT_STRING *publicKey;
185 struct CMS_RecipientEncryptedKey_st {
186 CMS_KeyAgreeRecipientIdentifier *rid;
187 ASN1_OCTET_STRING *encryptedKey;
188 /* Public key associated with this recipient */
192 struct CMS_KeyAgreeRecipientIdentifier_st {
195 CMS_IssuerAndSerialNumber *issuerAndSerialNumber;
196 CMS_RecipientKeyIdentifier *rKeyId;
200 struct CMS_RecipientKeyIdentifier_st {
201 ASN1_OCTET_STRING *subjectKeyIdentifier;
202 ASN1_GENERALIZEDTIME *date;
203 CMS_OtherKeyAttribute *other;
206 struct CMS_KEKRecipientInfo_st {
208 CMS_KEKIdentifier *kekid;
209 X509_ALGOR *keyEncryptionAlgorithm;
210 ASN1_OCTET_STRING *encryptedKey;
211 /* Extra info: symmetric key to use */
216 struct CMS_KEKIdentifier_st {
217 ASN1_OCTET_STRING *keyIdentifier;
218 ASN1_GENERALIZEDTIME *date;
219 CMS_OtherKeyAttribute *other;
222 struct CMS_PasswordRecipientInfo_st {
224 X509_ALGOR *keyDerivationAlgorithm;
225 X509_ALGOR *keyEncryptionAlgorithm;
226 ASN1_OCTET_STRING *encryptedKey;
227 /* Extra info: password to use */
232 struct CMS_OtherRecipientInfo_st {
233 ASN1_OBJECT *oriType;
237 struct CMS_DigestedData_st {
239 X509_ALGOR *digestAlgorithm;
240 CMS_EncapsulatedContentInfo *encapContentInfo;
241 ASN1_OCTET_STRING *digest;
244 struct CMS_EncryptedData_st {
246 CMS_EncryptedContentInfo *encryptedContentInfo;
247 STACK_OF(X509_ATTRIBUTE) *unprotectedAttrs;
250 struct CMS_AuthenticatedData_st {
252 CMS_OriginatorInfo *originatorInfo;
253 STACK_OF(CMS_RecipientInfo) *recipientInfos;
254 X509_ALGOR *macAlgorithm;
255 X509_ALGOR *digestAlgorithm;
256 CMS_EncapsulatedContentInfo *encapContentInfo;
257 STACK_OF(X509_ATTRIBUTE) *authAttrs;
258 ASN1_OCTET_STRING *mac;
259 STACK_OF(X509_ATTRIBUTE) *unauthAttrs;
262 struct CMS_CompressedData_st {
264 X509_ALGOR *compressionAlgorithm;
265 STACK_OF(CMS_RecipientInfo) *recipientInfos;
266 CMS_EncapsulatedContentInfo *encapContentInfo;
269 struct CMS_RevocationInfoChoice_st {
273 CMS_OtherRevocationInfoFormat *other;
277 # define CMS_REVCHOICE_CRL 0
278 # define CMS_REVCHOICE_OTHER 1
280 struct CMS_OtherRevocationInfoFormat_st {
281 ASN1_OBJECT *otherRevInfoFormat;
282 ASN1_TYPE *otherRevInfo;
285 struct CMS_CertificateChoices {
289 ASN1_STRING *extendedCertificate; /* Obsolete */
290 ASN1_STRING *v1AttrCert; /* Left encoded for now */
291 ASN1_STRING *v2AttrCert; /* Left encoded for now */
292 CMS_OtherCertificateFormat *other;
296 # define CMS_CERTCHOICE_CERT 0
297 # define CMS_CERTCHOICE_EXCERT 1
298 # define CMS_CERTCHOICE_V1ACERT 2
299 # define CMS_CERTCHOICE_V2ACERT 3
300 # define CMS_CERTCHOICE_OTHER 4
302 struct CMS_OtherCertificateFormat_st {
303 ASN1_OBJECT *otherCertFormat;
304 ASN1_TYPE *otherCert;
308 * This is also defined in pkcs7.h but we duplicate it to allow the CMS code
309 * to be independent of PKCS#7
312 struct CMS_IssuerAndSerialNumber_st {
314 ASN1_INTEGER *serialNumber;
317 struct CMS_OtherKeyAttribute_st {
318 ASN1_OBJECT *keyAttrId;
324 # ifdef HEADER_X509V3_H
326 struct CMS_ReceiptRequest_st {
327 ASN1_OCTET_STRING *signedContentIdentifier;
328 CMS_ReceiptsFrom *receiptsFrom;
329 STACK_OF(GENERAL_NAMES) *receiptsTo;
332 struct CMS_ReceiptsFrom_st {
336 STACK_OF(GENERAL_NAMES) *receiptList;
341 struct CMS_Receipt_st {
343 ASN1_OBJECT *contentType;
344 ASN1_OCTET_STRING *signedContentIdentifier;
345 ASN1_OCTET_STRING *originatorSignatureValue;
348 DECLARE_ASN1_FUNCTIONS(CMS_ContentInfo)
349 DECLARE_ASN1_ITEM(CMS_SignerInfo)
350 DECLARE_ASN1_ITEM(CMS_IssuerAndSerialNumber)
351 DECLARE_ASN1_ITEM(CMS_Attributes_Sign)
352 DECLARE_ASN1_ITEM(CMS_Attributes_Verify)
353 DECLARE_ASN1_ITEM(CMS_RecipientInfo)
354 DECLARE_ASN1_ITEM(CMS_PasswordRecipientInfo)
355 DECLARE_ASN1_ALLOC_FUNCTIONS(CMS_IssuerAndSerialNumber)
357 # define CMS_SIGNERINFO_ISSUER_SERIAL 0
358 # define CMS_SIGNERINFO_KEYIDENTIFIER 1
360 # define CMS_RECIPINFO_ISSUER_SERIAL 0
361 # define CMS_RECIPINFO_KEYIDENTIFIER 1
363 # define CMS_REK_ISSUER_SERIAL 0
364 # define CMS_REK_KEYIDENTIFIER 1
366 # define CMS_OIK_ISSUER_SERIAL 0
367 # define CMS_OIK_KEYIDENTIFIER 1
368 # define CMS_OIK_PUBKEY 2
370 BIO *cms_content_bio(CMS_ContentInfo *cms);
372 CMS_ContentInfo *cms_Data_create(void);
374 CMS_ContentInfo *cms_DigestedData_create(const EVP_MD *md);
375 BIO *cms_DigestedData_init_bio(CMS_ContentInfo *cms);
376 int cms_DigestedData_do_final(CMS_ContentInfo *cms, BIO *chain, int verify);
378 BIO *cms_SignedData_init_bio(CMS_ContentInfo *cms);
379 int cms_SignedData_final(CMS_ContentInfo *cms, BIO *chain);
380 int cms_set1_SignerIdentifier(CMS_SignerIdentifier *sid, X509 *cert,
382 int cms_SignerIdentifier_get0_signer_id(CMS_SignerIdentifier *sid,
383 ASN1_OCTET_STRING **keyid,
386 int cms_SignerIdentifier_cert_cmp(CMS_SignerIdentifier *sid, X509 *cert);
388 CMS_ContentInfo *cms_CompressedData_create(int comp_nid);
389 BIO *cms_CompressedData_init_bio(CMS_ContentInfo *cms);
391 BIO *cms_DigestAlgorithm_init_bio(X509_ALGOR *digestAlgorithm);
392 int cms_DigestAlgorithm_find_ctx(EVP_MD_CTX *mctx, BIO *chain,
395 int cms_ias_cert_cmp(CMS_IssuerAndSerialNumber *ias, X509 *cert);
396 int cms_keyid_cert_cmp(ASN1_OCTET_STRING *keyid, X509 *cert);
397 int cms_set1_ias(CMS_IssuerAndSerialNumber **pias, X509 *cert);
398 int cms_set1_keyid(ASN1_OCTET_STRING **pkeyid, X509 *cert);
400 BIO *cms_EncryptedContent_init_bio(CMS_EncryptedContentInfo *ec);
401 BIO *cms_EncryptedData_init_bio(CMS_ContentInfo *cms);
402 int cms_EncryptedContent_init(CMS_EncryptedContentInfo *ec,
403 const EVP_CIPHER *cipher,
404 const unsigned char *key, size_t keylen);
406 int cms_Receipt_verify(CMS_ContentInfo *cms, CMS_ContentInfo *req_cms);
407 int cms_msgSigDigest_add1(CMS_SignerInfo *dest, CMS_SignerInfo *src);
408 ASN1_OCTET_STRING *cms_encode_Receipt(CMS_SignerInfo *si);
410 BIO *cms_EnvelopedData_init_bio(CMS_ContentInfo *cms);
411 CMS_EnvelopedData *cms_get0_enveloped(CMS_ContentInfo *cms);
412 int cms_env_asn1_ctrl(CMS_RecipientInfo *ri, int cmd);
413 int cms_pkey_get_ri_type(EVP_PKEY *pk);
415 int cms_RecipientInfo_kari_init(CMS_RecipientInfo *ri, X509 *recip,
416 EVP_PKEY *pk, unsigned int flags);
417 int cms_RecipientInfo_kari_encrypt(CMS_ContentInfo *cms,
418 CMS_RecipientInfo *ri);
421 int cms_RecipientInfo_pwri_crypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri,
424 DECLARE_ASN1_ITEM(CMS_CertificateChoices)
425 DECLARE_ASN1_ITEM(CMS_DigestedData)
426 DECLARE_ASN1_ITEM(CMS_EncryptedData)
427 DECLARE_ASN1_ITEM(CMS_EnvelopedData)
428 DECLARE_ASN1_ITEM(CMS_KEKRecipientInfo)
429 DECLARE_ASN1_ITEM(CMS_KeyAgreeRecipientInfo)
430 DECLARE_ASN1_ITEM(CMS_KeyTransRecipientInfo)
431 DECLARE_ASN1_ITEM(CMS_OriginatorPublicKey)
432 DECLARE_ASN1_ITEM(CMS_OtherKeyAttribute)
433 DECLARE_ASN1_ITEM(CMS_Receipt)
434 DECLARE_ASN1_ITEM(CMS_ReceiptRequest)
435 DECLARE_ASN1_ITEM(CMS_RecipientEncryptedKey)
436 DECLARE_ASN1_ITEM(CMS_RecipientKeyIdentifier)
437 DECLARE_ASN1_ITEM(CMS_RevocationInfoChoice)
438 DECLARE_ASN1_ITEM(CMS_SignedData)
439 DECLARE_ASN1_ITEM(CMS_CompressedData)