2 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 2008 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
54 #ifndef HEADER_CMS_LCL_H
55 # define HEADER_CMS_LCL_H
61 # include <openssl/x509.h>
64 * Cryptographic message syntax (CMS) structures: taken from RFC3852
67 /* Forward references */
69 typedef struct CMS_IssuerAndSerialNumber_st CMS_IssuerAndSerialNumber;
70 typedef struct CMS_EncapsulatedContentInfo_st CMS_EncapsulatedContentInfo;
71 typedef struct CMS_SignerIdentifier_st CMS_SignerIdentifier;
72 typedef struct CMS_SignedData_st CMS_SignedData;
73 typedef struct CMS_OtherRevocationInfoFormat_st CMS_OtherRevocationInfoFormat;
74 typedef struct CMS_OriginatorInfo_st CMS_OriginatorInfo;
75 typedef struct CMS_EncryptedContentInfo_st CMS_EncryptedContentInfo;
76 typedef struct CMS_EnvelopedData_st CMS_EnvelopedData;
77 typedef struct CMS_DigestedData_st CMS_DigestedData;
78 typedef struct CMS_EncryptedData_st CMS_EncryptedData;
79 typedef struct CMS_AuthenticatedData_st CMS_AuthenticatedData;
80 typedef struct CMS_CompressedData_st CMS_CompressedData;
81 typedef struct CMS_OtherCertificateFormat_st CMS_OtherCertificateFormat;
82 typedef struct CMS_KeyTransRecipientInfo_st CMS_KeyTransRecipientInfo;
83 typedef struct CMS_OriginatorPublicKey_st CMS_OriginatorPublicKey;
84 typedef struct CMS_OriginatorIdentifierOrKey_st CMS_OriginatorIdentifierOrKey;
85 typedef struct CMS_KeyAgreeRecipientInfo_st CMS_KeyAgreeRecipientInfo;
86 typedef struct CMS_RecipientKeyIdentifier_st CMS_RecipientKeyIdentifier;
87 typedef struct CMS_KeyAgreeRecipientIdentifier_st
88 CMS_KeyAgreeRecipientIdentifier;
89 typedef struct CMS_KEKIdentifier_st CMS_KEKIdentifier;
90 typedef struct CMS_KEKRecipientInfo_st CMS_KEKRecipientInfo;
91 typedef struct CMS_PasswordRecipientInfo_st CMS_PasswordRecipientInfo;
92 typedef struct CMS_OtherRecipientInfo_st CMS_OtherRecipientInfo;
93 typedef struct CMS_ReceiptsFrom_st CMS_ReceiptsFrom;
95 struct CMS_ContentInfo_st {
96 ASN1_OBJECT *contentType;
98 ASN1_OCTET_STRING *data;
99 CMS_SignedData *signedData;
100 CMS_EnvelopedData *envelopedData;
101 CMS_DigestedData *digestedData;
102 CMS_EncryptedData *encryptedData;
103 CMS_AuthenticatedData *authenticatedData;
104 CMS_CompressedData *compressedData;
106 /* Other types ... */
111 DEFINE_STACK_OF(CMS_CertificateChoices)
113 struct CMS_SignedData_st {
115 STACK_OF(X509_ALGOR) *digestAlgorithms;
116 CMS_EncapsulatedContentInfo *encapContentInfo;
117 STACK_OF(CMS_CertificateChoices) *certificates;
118 STACK_OF(CMS_RevocationInfoChoice) *crls;
119 STACK_OF(CMS_SignerInfo) *signerInfos;
122 struct CMS_EncapsulatedContentInfo_st {
123 ASN1_OBJECT *eContentType;
124 ASN1_OCTET_STRING *eContent;
125 /* Set to 1 if incomplete structure only part set up */
129 struct CMS_SignerInfo_st {
131 CMS_SignerIdentifier *sid;
132 X509_ALGOR *digestAlgorithm;
133 STACK_OF(X509_ATTRIBUTE) *signedAttrs;
134 X509_ALGOR *signatureAlgorithm;
135 ASN1_OCTET_STRING *signature;
136 STACK_OF(X509_ATTRIBUTE) *unsignedAttrs;
137 /* Signing certificate and key */
140 /* Digest and public key context for alternative parameters */
145 struct CMS_SignerIdentifier_st {
148 CMS_IssuerAndSerialNumber *issuerAndSerialNumber;
149 ASN1_OCTET_STRING *subjectKeyIdentifier;
153 struct CMS_EnvelopedData_st {
155 CMS_OriginatorInfo *originatorInfo;
156 STACK_OF(CMS_RecipientInfo) *recipientInfos;
157 CMS_EncryptedContentInfo *encryptedContentInfo;
158 STACK_OF(X509_ATTRIBUTE) *unprotectedAttrs;
161 struct CMS_OriginatorInfo_st {
162 STACK_OF(CMS_CertificateChoices) *certificates;
163 STACK_OF(CMS_RevocationInfoChoice) *crls;
166 struct CMS_EncryptedContentInfo_st {
167 ASN1_OBJECT *contentType;
168 X509_ALGOR *contentEncryptionAlgorithm;
169 ASN1_OCTET_STRING *encryptedContent;
170 /* Content encryption algorithm and key */
171 const EVP_CIPHER *cipher;
174 /* Set to 1 if we are debugging decrypt and don't fake keys for MMA */
178 struct CMS_RecipientInfo_st {
181 CMS_KeyTransRecipientInfo *ktri;
182 CMS_KeyAgreeRecipientInfo *kari;
183 CMS_KEKRecipientInfo *kekri;
184 CMS_PasswordRecipientInfo *pwri;
185 CMS_OtherRecipientInfo *ori;
189 typedef CMS_SignerIdentifier CMS_RecipientIdentifier;
191 struct CMS_KeyTransRecipientInfo_st {
193 CMS_RecipientIdentifier *rid;
194 X509_ALGOR *keyEncryptionAlgorithm;
195 ASN1_OCTET_STRING *encryptedKey;
196 /* Recipient Key and cert */
199 /* Public key context for this operation */
203 struct CMS_KeyAgreeRecipientInfo_st {
205 CMS_OriginatorIdentifierOrKey *originator;
206 ASN1_OCTET_STRING *ukm;
207 X509_ALGOR *keyEncryptionAlgorithm;
208 STACK_OF(CMS_RecipientEncryptedKey) *recipientEncryptedKeys;
209 /* Public key context associated with current operation */
211 /* Cipher context for CEK wrapping */
215 struct CMS_OriginatorIdentifierOrKey_st {
218 CMS_IssuerAndSerialNumber *issuerAndSerialNumber;
219 ASN1_OCTET_STRING *subjectKeyIdentifier;
220 CMS_OriginatorPublicKey *originatorKey;
224 struct CMS_OriginatorPublicKey_st {
225 X509_ALGOR *algorithm;
226 ASN1_BIT_STRING *publicKey;
229 struct CMS_RecipientEncryptedKey_st {
230 CMS_KeyAgreeRecipientIdentifier *rid;
231 ASN1_OCTET_STRING *encryptedKey;
232 /* Public key associated with this recipient */
236 struct CMS_KeyAgreeRecipientIdentifier_st {
239 CMS_IssuerAndSerialNumber *issuerAndSerialNumber;
240 CMS_RecipientKeyIdentifier *rKeyId;
244 struct CMS_RecipientKeyIdentifier_st {
245 ASN1_OCTET_STRING *subjectKeyIdentifier;
246 ASN1_GENERALIZEDTIME *date;
247 CMS_OtherKeyAttribute *other;
250 struct CMS_KEKRecipientInfo_st {
252 CMS_KEKIdentifier *kekid;
253 X509_ALGOR *keyEncryptionAlgorithm;
254 ASN1_OCTET_STRING *encryptedKey;
255 /* Extra info: symmetric key to use */
260 struct CMS_KEKIdentifier_st {
261 ASN1_OCTET_STRING *keyIdentifier;
262 ASN1_GENERALIZEDTIME *date;
263 CMS_OtherKeyAttribute *other;
266 struct CMS_PasswordRecipientInfo_st {
268 X509_ALGOR *keyDerivationAlgorithm;
269 X509_ALGOR *keyEncryptionAlgorithm;
270 ASN1_OCTET_STRING *encryptedKey;
271 /* Extra info: password to use */
276 struct CMS_OtherRecipientInfo_st {
277 ASN1_OBJECT *oriType;
281 struct CMS_DigestedData_st {
283 X509_ALGOR *digestAlgorithm;
284 CMS_EncapsulatedContentInfo *encapContentInfo;
285 ASN1_OCTET_STRING *digest;
288 struct CMS_EncryptedData_st {
290 CMS_EncryptedContentInfo *encryptedContentInfo;
291 STACK_OF(X509_ATTRIBUTE) *unprotectedAttrs;
294 struct CMS_AuthenticatedData_st {
296 CMS_OriginatorInfo *originatorInfo;
297 STACK_OF(CMS_RecipientInfo) *recipientInfos;
298 X509_ALGOR *macAlgorithm;
299 X509_ALGOR *digestAlgorithm;
300 CMS_EncapsulatedContentInfo *encapContentInfo;
301 STACK_OF(X509_ATTRIBUTE) *authAttrs;
302 ASN1_OCTET_STRING *mac;
303 STACK_OF(X509_ATTRIBUTE) *unauthAttrs;
306 struct CMS_CompressedData_st {
308 X509_ALGOR *compressionAlgorithm;
309 STACK_OF(CMS_RecipientInfo) *recipientInfos;
310 CMS_EncapsulatedContentInfo *encapContentInfo;
313 struct CMS_RevocationInfoChoice_st {
317 CMS_OtherRevocationInfoFormat *other;
321 # define CMS_REVCHOICE_CRL 0
322 # define CMS_REVCHOICE_OTHER 1
324 struct CMS_OtherRevocationInfoFormat_st {
325 ASN1_OBJECT *otherRevInfoFormat;
326 ASN1_TYPE *otherRevInfo;
329 struct CMS_CertificateChoices {
333 ASN1_STRING *extendedCertificate; /* Obsolete */
334 ASN1_STRING *v1AttrCert; /* Left encoded for now */
335 ASN1_STRING *v2AttrCert; /* Left encoded for now */
336 CMS_OtherCertificateFormat *other;
340 # define CMS_CERTCHOICE_CERT 0
341 # define CMS_CERTCHOICE_EXCERT 1
342 # define CMS_CERTCHOICE_V1ACERT 2
343 # define CMS_CERTCHOICE_V2ACERT 3
344 # define CMS_CERTCHOICE_OTHER 4
346 struct CMS_OtherCertificateFormat_st {
347 ASN1_OBJECT *otherCertFormat;
348 ASN1_TYPE *otherCert;
352 * This is also defined in pkcs7.h but we duplicate it to allow the CMS code
353 * to be independent of PKCS#7
356 struct CMS_IssuerAndSerialNumber_st {
358 ASN1_INTEGER *serialNumber;
361 struct CMS_OtherKeyAttribute_st {
362 ASN1_OBJECT *keyAttrId;
368 # ifdef HEADER_X509V3_H
370 struct CMS_ReceiptRequest_st {
371 ASN1_OCTET_STRING *signedContentIdentifier;
372 CMS_ReceiptsFrom *receiptsFrom;
373 STACK_OF(GENERAL_NAMES) *receiptsTo;
376 struct CMS_ReceiptsFrom_st {
380 STACK_OF(GENERAL_NAMES) *receiptList;
385 struct CMS_Receipt_st {
387 ASN1_OBJECT *contentType;
388 ASN1_OCTET_STRING *signedContentIdentifier;
389 ASN1_OCTET_STRING *originatorSignatureValue;
392 DECLARE_ASN1_FUNCTIONS(CMS_ContentInfo)
393 DECLARE_ASN1_ITEM(CMS_SignerInfo)
394 DECLARE_ASN1_ITEM(CMS_IssuerAndSerialNumber)
395 DECLARE_ASN1_ITEM(CMS_Attributes_Sign)
396 DECLARE_ASN1_ITEM(CMS_Attributes_Verify)
397 DECLARE_ASN1_ITEM(CMS_RecipientInfo)
398 DECLARE_ASN1_ITEM(CMS_PasswordRecipientInfo)
399 DECLARE_ASN1_ALLOC_FUNCTIONS(CMS_IssuerAndSerialNumber)
401 # define CMS_SIGNERINFO_ISSUER_SERIAL 0
402 # define CMS_SIGNERINFO_KEYIDENTIFIER 1
404 # define CMS_RECIPINFO_ISSUER_SERIAL 0
405 # define CMS_RECIPINFO_KEYIDENTIFIER 1
407 # define CMS_REK_ISSUER_SERIAL 0
408 # define CMS_REK_KEYIDENTIFIER 1
410 # define CMS_OIK_ISSUER_SERIAL 0
411 # define CMS_OIK_KEYIDENTIFIER 1
412 # define CMS_OIK_PUBKEY 2
414 BIO *cms_content_bio(CMS_ContentInfo *cms);
416 CMS_ContentInfo *cms_Data_create(void);
418 CMS_ContentInfo *cms_DigestedData_create(const EVP_MD *md);
419 BIO *cms_DigestedData_init_bio(CMS_ContentInfo *cms);
420 int cms_DigestedData_do_final(CMS_ContentInfo *cms, BIO *chain, int verify);
422 BIO *cms_SignedData_init_bio(CMS_ContentInfo *cms);
423 int cms_SignedData_final(CMS_ContentInfo *cms, BIO *chain);
424 int cms_set1_SignerIdentifier(CMS_SignerIdentifier *sid, X509 *cert,
426 int cms_SignerIdentifier_get0_signer_id(CMS_SignerIdentifier *sid,
427 ASN1_OCTET_STRING **keyid,
430 int cms_SignerIdentifier_cert_cmp(CMS_SignerIdentifier *sid, X509 *cert);
432 CMS_ContentInfo *cms_CompressedData_create(int comp_nid);
433 BIO *cms_CompressedData_init_bio(CMS_ContentInfo *cms);
435 BIO *cms_DigestAlgorithm_init_bio(X509_ALGOR *digestAlgorithm);
436 int cms_DigestAlgorithm_find_ctx(EVP_MD_CTX *mctx, BIO *chain,
439 int cms_ias_cert_cmp(CMS_IssuerAndSerialNumber *ias, X509 *cert);
440 int cms_keyid_cert_cmp(ASN1_OCTET_STRING *keyid, X509 *cert);
441 int cms_set1_ias(CMS_IssuerAndSerialNumber **pias, X509 *cert);
442 int cms_set1_keyid(ASN1_OCTET_STRING **pkeyid, X509 *cert);
444 BIO *cms_EncryptedContent_init_bio(CMS_EncryptedContentInfo *ec);
445 BIO *cms_EncryptedData_init_bio(CMS_ContentInfo *cms);
446 int cms_EncryptedContent_init(CMS_EncryptedContentInfo *ec,
447 const EVP_CIPHER *cipher,
448 const unsigned char *key, size_t keylen);
450 int cms_Receipt_verify(CMS_ContentInfo *cms, CMS_ContentInfo *req_cms);
451 int cms_msgSigDigest_add1(CMS_SignerInfo *dest, CMS_SignerInfo *src);
452 ASN1_OCTET_STRING *cms_encode_Receipt(CMS_SignerInfo *si);
454 BIO *cms_EnvelopedData_init_bio(CMS_ContentInfo *cms);
455 CMS_EnvelopedData *cms_get0_enveloped(CMS_ContentInfo *cms);
456 int cms_env_asn1_ctrl(CMS_RecipientInfo *ri, int cmd);
457 int cms_pkey_get_ri_type(EVP_PKEY *pk);
459 int cms_RecipientInfo_kari_init(CMS_RecipientInfo *ri, X509 *recip,
460 EVP_PKEY *pk, unsigned int flags);
461 int cms_RecipientInfo_kari_encrypt(CMS_ContentInfo *cms,
462 CMS_RecipientInfo *ri);
465 int cms_RecipientInfo_pwri_crypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri,
468 DECLARE_ASN1_ITEM(CMS_CertificateChoices)
469 DECLARE_ASN1_ITEM(CMS_DigestedData)
470 DECLARE_ASN1_ITEM(CMS_EncryptedData)
471 DECLARE_ASN1_ITEM(CMS_EnvelopedData)
472 DECLARE_ASN1_ITEM(CMS_KEKRecipientInfo)
473 DECLARE_ASN1_ITEM(CMS_KeyAgreeRecipientInfo)
474 DECLARE_ASN1_ITEM(CMS_KeyTransRecipientInfo)
475 DECLARE_ASN1_ITEM(CMS_OriginatorPublicKey)
476 DECLARE_ASN1_ITEM(CMS_OtherKeyAttribute)
477 DECLARE_ASN1_ITEM(CMS_Receipt)
478 DECLARE_ASN1_ITEM(CMS_ReceiptRequest)
479 DECLARE_ASN1_ITEM(CMS_RecipientEncryptedKey)
480 DECLARE_ASN1_ITEM(CMS_RecipientKeyIdentifier)
481 DECLARE_ASN1_ITEM(CMS_RevocationInfoChoice)
482 DECLARE_ASN1_ITEM(CMS_SignedData)
483 DECLARE_ASN1_ITEM(CMS_CompressedData)