cms_lcl.h revision 296465
1/* crypto/cms/cms_lcl.h */
2/*
3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
4 * project.
5 */
6/* ====================================================================
7 * Copyright (c) 2008 The OpenSSL Project.  All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in
18 *    the documentation and/or other materials provided with the
19 *    distribution.
20 *
21 * 3. All advertising materials mentioning features or use of this
22 *    software must display the following acknowledgment:
23 *    "This product includes software developed by the OpenSSL Project
24 *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 *
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 *    endorse or promote products derived from this software without
28 *    prior written permission. For written permission, please contact
29 *    licensing@OpenSSL.org.
30 *
31 * 5. Products derived from this software may not be called "OpenSSL"
32 *    nor may "OpenSSL" appear in their names without prior written
33 *    permission of the OpenSSL Project.
34 *
35 * 6. Redistributions of any form whatsoever must retain the following
36 *    acknowledgment:
37 *    "This product includes software developed by the OpenSSL Project
38 *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
53 */
54
55#ifndef HEADER_CMS_LCL_H
56# define HEADER_CMS_LCL_H
57
58#ifdef __cplusplus
59extern "C" {
60#endif
61
62# include <openssl/x509.h>
63
64/*
65 * Cryptographic message syntax (CMS) structures: taken from RFC3852
66 */
67
68/* Forward references */
69
70typedef struct CMS_IssuerAndSerialNumber_st CMS_IssuerAndSerialNumber;
71typedef struct CMS_EncapsulatedContentInfo_st CMS_EncapsulatedContentInfo;
72typedef struct CMS_SignerIdentifier_st CMS_SignerIdentifier;
73typedef struct CMS_SignedData_st CMS_SignedData;
74typedef struct CMS_OtherRevocationInfoFormat_st CMS_OtherRevocationInfoFormat;
75typedef struct CMS_OriginatorInfo_st CMS_OriginatorInfo;
76typedef struct CMS_EncryptedContentInfo_st CMS_EncryptedContentInfo;
77typedef struct CMS_EnvelopedData_st CMS_EnvelopedData;
78typedef struct CMS_DigestedData_st CMS_DigestedData;
79typedef struct CMS_EncryptedData_st CMS_EncryptedData;
80typedef struct CMS_AuthenticatedData_st CMS_AuthenticatedData;
81typedef struct CMS_CompressedData_st CMS_CompressedData;
82typedef struct CMS_OtherCertificateFormat_st CMS_OtherCertificateFormat;
83typedef struct CMS_KeyTransRecipientInfo_st CMS_KeyTransRecipientInfo;
84typedef struct CMS_OriginatorPublicKey_st CMS_OriginatorPublicKey;
85typedef struct CMS_OriginatorIdentifierOrKey_st CMS_OriginatorIdentifierOrKey;
86typedef struct CMS_KeyAgreeRecipientInfo_st CMS_KeyAgreeRecipientInfo;
87typedef struct CMS_OtherKeyAttribute_st CMS_OtherKeyAttribute;
88typedef struct CMS_RecipientKeyIdentifier_st CMS_RecipientKeyIdentifier;
89typedef struct CMS_KeyAgreeRecipientIdentifier_st
90    CMS_KeyAgreeRecipientIdentifier;
91typedef struct CMS_RecipientEncryptedKey_st CMS_RecipientEncryptedKey;
92typedef struct CMS_KEKIdentifier_st CMS_KEKIdentifier;
93typedef struct CMS_KEKRecipientInfo_st CMS_KEKRecipientInfo;
94typedef struct CMS_PasswordRecipientInfo_st CMS_PasswordRecipientInfo;
95typedef struct CMS_OtherRecipientInfo_st CMS_OtherRecipientInfo;
96typedef struct CMS_ReceiptsFrom_st CMS_ReceiptsFrom;
97
98struct CMS_ContentInfo_st {
99    ASN1_OBJECT *contentType;
100    union {
101        ASN1_OCTET_STRING *data;
102        CMS_SignedData *signedData;
103        CMS_EnvelopedData *envelopedData;
104        CMS_DigestedData *digestedData;
105        CMS_EncryptedData *encryptedData;
106        CMS_AuthenticatedData *authenticatedData;
107        CMS_CompressedData *compressedData;
108        ASN1_TYPE *other;
109        /* Other types ... */
110        void *otherData;
111    } d;
112};
113
114struct CMS_SignedData_st {
115    long version;
116    STACK_OF(X509_ALGOR) *digestAlgorithms;
117    CMS_EncapsulatedContentInfo *encapContentInfo;
118    STACK_OF(CMS_CertificateChoices) *certificates;
119    STACK_OF(CMS_RevocationInfoChoice) *crls;
120    STACK_OF(CMS_SignerInfo) *signerInfos;
121};
122
123struct CMS_EncapsulatedContentInfo_st {
124    ASN1_OBJECT *eContentType;
125    ASN1_OCTET_STRING *eContent;
126    /* Set to 1 if incomplete structure only part set up */
127    int partial;
128};
129
130struct CMS_SignerInfo_st {
131    long version;
132    CMS_SignerIdentifier *sid;
133    X509_ALGOR *digestAlgorithm;
134    STACK_OF(X509_ATTRIBUTE) *signedAttrs;
135    X509_ALGOR *signatureAlgorithm;
136    ASN1_OCTET_STRING *signature;
137    STACK_OF(X509_ATTRIBUTE) *unsignedAttrs;
138    /* Signing certificate and key */
139    X509 *signer;
140    EVP_PKEY *pkey;
141};
142
143struct CMS_SignerIdentifier_st {
144    int type;
145    union {
146        CMS_IssuerAndSerialNumber *issuerAndSerialNumber;
147        ASN1_OCTET_STRING *subjectKeyIdentifier;
148    } d;
149};
150
151struct CMS_EnvelopedData_st {
152    long version;
153    CMS_OriginatorInfo *originatorInfo;
154    STACK_OF(CMS_RecipientInfo) *recipientInfos;
155    CMS_EncryptedContentInfo *encryptedContentInfo;
156    STACK_OF(X509_ATTRIBUTE) *unprotectedAttrs;
157};
158
159struct CMS_OriginatorInfo_st {
160    STACK_OF(CMS_CertificateChoices) *certificates;
161    STACK_OF(CMS_RevocationInfoChoice) *crls;
162};
163
164struct CMS_EncryptedContentInfo_st {
165    ASN1_OBJECT *contentType;
166    X509_ALGOR *contentEncryptionAlgorithm;
167    ASN1_OCTET_STRING *encryptedContent;
168    /* Content encryption algorithm and key */
169    const EVP_CIPHER *cipher;
170    unsigned char *key;
171    size_t keylen;
172    /* Set to 1 if we are debugging decrypt and don't fake keys for MMA */
173    int debug;
174};
175
176struct CMS_RecipientInfo_st {
177    int type;
178    union {
179        CMS_KeyTransRecipientInfo *ktri;
180        CMS_KeyAgreeRecipientInfo *kari;
181        CMS_KEKRecipientInfo *kekri;
182        CMS_PasswordRecipientInfo *pwri;
183        CMS_OtherRecipientInfo *ori;
184    } d;
185};
186
187typedef CMS_SignerIdentifier CMS_RecipientIdentifier;
188
189struct CMS_KeyTransRecipientInfo_st {
190    long version;
191    CMS_RecipientIdentifier *rid;
192    X509_ALGOR *keyEncryptionAlgorithm;
193    ASN1_OCTET_STRING *encryptedKey;
194    /* Recipient Key and cert */
195    X509 *recip;
196    EVP_PKEY *pkey;
197};
198
199struct CMS_KeyAgreeRecipientInfo_st {
200    long version;
201    CMS_OriginatorIdentifierOrKey *originator;
202    ASN1_OCTET_STRING *ukm;
203    X509_ALGOR *keyEncryptionAlgorithm;
204    STACK_OF(CMS_RecipientEncryptedKey) *recipientEncryptedKeys;
205};
206
207struct CMS_OriginatorIdentifierOrKey_st {
208    int type;
209    union {
210        CMS_IssuerAndSerialNumber *issuerAndSerialNumber;
211        ASN1_OCTET_STRING *subjectKeyIdentifier;
212        CMS_OriginatorPublicKey *originatorKey;
213    } d;
214};
215
216struct CMS_OriginatorPublicKey_st {
217    X509_ALGOR *algorithm;
218    ASN1_BIT_STRING *publicKey;
219};
220
221struct CMS_RecipientEncryptedKey_st {
222    CMS_KeyAgreeRecipientIdentifier *rid;
223    ASN1_OCTET_STRING *encryptedKey;
224};
225
226struct CMS_KeyAgreeRecipientIdentifier_st {
227    int type;
228    union {
229        CMS_IssuerAndSerialNumber *issuerAndSerialNumber;
230        CMS_RecipientKeyIdentifier *rKeyId;
231    } d;
232};
233
234struct CMS_RecipientKeyIdentifier_st {
235    ASN1_OCTET_STRING *subjectKeyIdentifier;
236    ASN1_GENERALIZEDTIME *date;
237    CMS_OtherKeyAttribute *other;
238};
239
240struct CMS_KEKRecipientInfo_st {
241    long version;
242    CMS_KEKIdentifier *kekid;
243    X509_ALGOR *keyEncryptionAlgorithm;
244    ASN1_OCTET_STRING *encryptedKey;
245    /* Extra info: symmetric key to use */
246    unsigned char *key;
247    size_t keylen;
248};
249
250struct CMS_KEKIdentifier_st {
251    ASN1_OCTET_STRING *keyIdentifier;
252    ASN1_GENERALIZEDTIME *date;
253    CMS_OtherKeyAttribute *other;
254};
255
256struct CMS_PasswordRecipientInfo_st {
257    long version;
258    X509_ALGOR *keyDerivationAlgorithm;
259    X509_ALGOR *keyEncryptionAlgorithm;
260    ASN1_OCTET_STRING *encryptedKey;
261};
262
263struct CMS_OtherRecipientInfo_st {
264    ASN1_OBJECT *oriType;
265    ASN1_TYPE *oriValue;
266};
267
268struct CMS_DigestedData_st {
269    long version;
270    X509_ALGOR *digestAlgorithm;
271    CMS_EncapsulatedContentInfo *encapContentInfo;
272    ASN1_OCTET_STRING *digest;
273};
274
275struct CMS_EncryptedData_st {
276    long version;
277    CMS_EncryptedContentInfo *encryptedContentInfo;
278    STACK_OF(X509_ATTRIBUTE) *unprotectedAttrs;
279};
280
281struct CMS_AuthenticatedData_st {
282    long version;
283    CMS_OriginatorInfo *originatorInfo;
284    STACK_OF(CMS_RecipientInfo) *recipientInfos;
285    X509_ALGOR *macAlgorithm;
286    X509_ALGOR *digestAlgorithm;
287    CMS_EncapsulatedContentInfo *encapContentInfo;
288    STACK_OF(X509_ATTRIBUTE) *authAttrs;
289    ASN1_OCTET_STRING *mac;
290    STACK_OF(X509_ATTRIBUTE) *unauthAttrs;
291};
292
293struct CMS_CompressedData_st {
294    long version;
295    X509_ALGOR *compressionAlgorithm;
296    STACK_OF(CMS_RecipientInfo) *recipientInfos;
297    CMS_EncapsulatedContentInfo *encapContentInfo;
298};
299
300struct CMS_RevocationInfoChoice_st {
301    int type;
302    union {
303        X509_CRL *crl;
304        CMS_OtherRevocationInfoFormat *other;
305    } d;
306};
307
308# define CMS_REVCHOICE_CRL               0
309# define CMS_REVCHOICE_OTHER             1
310
311struct CMS_OtherRevocationInfoFormat_st {
312    ASN1_OBJECT *otherRevInfoFormat;
313    ASN1_TYPE *otherRevInfo;
314};
315
316struct CMS_CertificateChoices {
317    int type;
318    union {
319        X509 *certificate;
320        ASN1_STRING *extendedCertificate; /* Obsolete */
321        ASN1_STRING *v1AttrCert; /* Left encoded for now */
322        ASN1_STRING *v2AttrCert; /* Left encoded for now */
323        CMS_OtherCertificateFormat *other;
324    } d;
325};
326
327# define CMS_CERTCHOICE_CERT             0
328# define CMS_CERTCHOICE_EXCERT           1
329# define CMS_CERTCHOICE_V1ACERT          2
330# define CMS_CERTCHOICE_V2ACERT          3
331# define CMS_CERTCHOICE_OTHER            4
332
333struct CMS_OtherCertificateFormat_st {
334    ASN1_OBJECT *otherCertFormat;
335    ASN1_TYPE *otherCert;
336};
337
338/*
339 * This is also defined in pkcs7.h but we duplicate it to allow the CMS code
340 * to be independent of PKCS#7
341 */
342
343struct CMS_IssuerAndSerialNumber_st {
344    X509_NAME *issuer;
345    ASN1_INTEGER *serialNumber;
346};
347
348struct CMS_OtherKeyAttribute_st {
349    ASN1_OBJECT *keyAttrId;
350    ASN1_TYPE *keyAttr;
351};
352
353/* ESS structures */
354
355# ifdef HEADER_X509V3_H
356
357struct CMS_ReceiptRequest_st {
358    ASN1_OCTET_STRING *signedContentIdentifier;
359    CMS_ReceiptsFrom *receiptsFrom;
360    STACK_OF(GENERAL_NAMES) *receiptsTo;
361};
362
363struct CMS_ReceiptsFrom_st {
364    int type;
365    union {
366        long allOrFirstTier;
367        STACK_OF(GENERAL_NAMES) *receiptList;
368    } d;
369};
370# endif
371
372struct CMS_Receipt_st {
373    long version;
374    ASN1_OBJECT *contentType;
375    ASN1_OCTET_STRING *signedContentIdentifier;
376    ASN1_OCTET_STRING *originatorSignatureValue;
377};
378
379DECLARE_ASN1_ITEM(CMS_SignerInfo)
380DECLARE_ASN1_ITEM(CMS_IssuerAndSerialNumber)
381DECLARE_ASN1_ITEM(CMS_Attributes_Sign)
382DECLARE_ASN1_ITEM(CMS_Attributes_Verify)
383DECLARE_ASN1_ALLOC_FUNCTIONS(CMS_IssuerAndSerialNumber)
384
385# define CMS_SIGNERINFO_ISSUER_SERIAL    0
386# define CMS_SIGNERINFO_KEYIDENTIFIER    1
387
388# define CMS_RECIPINFO_ISSUER_SERIAL     0
389# define CMS_RECIPINFO_KEYIDENTIFIER     1
390
391BIO *cms_content_bio(CMS_ContentInfo *cms);
392
393CMS_ContentInfo *cms_Data_create(void);
394
395CMS_ContentInfo *cms_DigestedData_create(const EVP_MD *md);
396BIO *cms_DigestedData_init_bio(CMS_ContentInfo *cms);
397int cms_DigestedData_do_final(CMS_ContentInfo *cms, BIO *chain, int verify);
398
399BIO *cms_SignedData_init_bio(CMS_ContentInfo *cms);
400int cms_SignedData_final(CMS_ContentInfo *cms, BIO *chain);
401int cms_set1_SignerIdentifier(CMS_SignerIdentifier *sid, X509 *cert,
402                              int type);
403int cms_SignerIdentifier_get0_signer_id(CMS_SignerIdentifier *sid,
404                                        ASN1_OCTET_STRING **keyid,
405                                        X509_NAME **issuer,
406                                        ASN1_INTEGER **sno);
407int cms_SignerIdentifier_cert_cmp(CMS_SignerIdentifier *sid, X509 *cert);
408
409CMS_ContentInfo *cms_CompressedData_create(int comp_nid);
410BIO *cms_CompressedData_init_bio(CMS_ContentInfo *cms);
411
412void cms_DigestAlgorithm_set(X509_ALGOR *alg, const EVP_MD *md);
413BIO *cms_DigestAlgorithm_init_bio(X509_ALGOR *digestAlgorithm);
414int cms_DigestAlgorithm_find_ctx(EVP_MD_CTX *mctx, BIO *chain,
415                                 X509_ALGOR *mdalg);
416
417BIO *cms_EncryptedContent_init_bio(CMS_EncryptedContentInfo *ec);
418BIO *cms_EncryptedData_init_bio(CMS_ContentInfo *cms);
419int cms_EncryptedContent_init(CMS_EncryptedContentInfo *ec,
420                              const EVP_CIPHER *cipher,
421                              const unsigned char *key, size_t keylen);
422
423int cms_Receipt_verify(CMS_ContentInfo *cms, CMS_ContentInfo *req_cms);
424int cms_msgSigDigest_add1(CMS_SignerInfo *dest, CMS_SignerInfo *src);
425ASN1_OCTET_STRING *cms_encode_Receipt(CMS_SignerInfo *si);
426
427BIO *cms_EnvelopedData_init_bio(CMS_ContentInfo *cms);
428
429#ifdef  __cplusplus
430}
431#endif
432#endif
433