Set CMS EnvelopedData version correctly.
[openssl.git] / crypto / cms / cms_lcl.h
1 /* crypto/cms/cms_lcl.h */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3  * project.
4  */
5 /* ====================================================================
6  * Copyright (c) 2008 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer. 
14  *
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
18  *    distribution.
19  *
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/)"
24  *
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.
29  *
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.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
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  * ====================================================================
52  */
53
54 #ifndef HEADER_CMS_LCL_H
55 #define HEADER_CMS_LCL_H
56
57 #ifdef __cplusplus
58 extern "C" {
59 #endif
60
61 #include <openssl/x509.h>
62
63 /* Cryptographic message syntax (CMS) structures: taken
64  * from RFC3852
65  */
66
67 /* Forward references */
68
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_OtherKeyAttribute_st CMS_OtherKeyAttribute;
87 typedef struct CMS_RecipientKeyIdentifier_st CMS_RecipientKeyIdentifier;
88 typedef struct CMS_KeyAgreeRecipientIdentifier_st CMS_KeyAgreeRecipientIdentifier;
89 typedef struct CMS_RecipientEncryptedKey_st CMS_RecipientEncryptedKey;
90 typedef struct CMS_KEKIdentifier_st CMS_KEKIdentifier;
91 typedef struct CMS_KEKRecipientInfo_st CMS_KEKRecipientInfo;
92 typedef struct CMS_PasswordRecipientInfo_st CMS_PasswordRecipientInfo;
93 typedef struct CMS_OtherRecipientInfo_st CMS_OtherRecipientInfo;
94 typedef struct CMS_ReceiptsFrom_st CMS_ReceiptsFrom;
95
96 struct CMS_ContentInfo_st
97         {
98         ASN1_OBJECT *contentType;
99         union   {
100                 ASN1_OCTET_STRING *data;
101                 CMS_SignedData *signedData;
102                 CMS_EnvelopedData *envelopedData;
103                 CMS_DigestedData *digestedData;
104                 CMS_EncryptedData *encryptedData;
105                 CMS_AuthenticatedData *authenticatedData;
106                 CMS_CompressedData *compressedData;
107                 ASN1_TYPE *other;
108                 /* Other types ... */
109                 void *otherData;
110                 } d;
111         };
112
113 struct CMS_SignedData_st
114         {
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  
123 struct CMS_EncapsulatedContentInfo_st
124         {
125         ASN1_OBJECT *eContentType;
126         ASN1_OCTET_STRING *eContent;
127         /* Set to 1 if incomplete structure only part set up */
128         int partial;
129         };
130
131 struct CMS_SignerInfo_st
132         {
133         long version;
134         CMS_SignerIdentifier *sid;
135         X509_ALGOR *digestAlgorithm;
136         STACK_OF(X509_ATTRIBUTE) *signedAttrs;
137         X509_ALGOR *signatureAlgorithm;
138         ASN1_OCTET_STRING *signature;
139         STACK_OF(X509_ATTRIBUTE) *unsignedAttrs;
140         /* Signing certificate and key */
141         X509 *signer;
142         EVP_PKEY *pkey;
143         /* Digest and public key context for alternative parameters */
144         EVP_MD_CTX mctx;
145         EVP_PKEY_CTX *pctx;
146         };
147
148 struct CMS_SignerIdentifier_st
149         {
150         int type;
151         union   {
152                 CMS_IssuerAndSerialNumber *issuerAndSerialNumber;
153                 ASN1_OCTET_STRING *subjectKeyIdentifier;
154                 } d;
155         };
156
157 struct CMS_EnvelopedData_st
158         {
159         long version;
160         CMS_OriginatorInfo *originatorInfo;
161         STACK_OF(CMS_RecipientInfo) *recipientInfos;
162         CMS_EncryptedContentInfo *encryptedContentInfo;
163         STACK_OF(X509_ATTRIBUTE) *unprotectedAttrs;
164         };
165
166 struct CMS_OriginatorInfo_st
167         {
168         STACK_OF(CMS_CertificateChoices) *certificates;
169         STACK_OF(CMS_RevocationInfoChoice) *crls;
170         };
171
172 struct CMS_EncryptedContentInfo_st
173         {
174         ASN1_OBJECT *contentType;
175         X509_ALGOR *contentEncryptionAlgorithm;
176         ASN1_OCTET_STRING *encryptedContent;
177         /* Content encryption algorithm and key */
178         const EVP_CIPHER *cipher;
179         unsigned char *key;
180         size_t keylen;
181         /* Set to 1 if we are debugging decrypt and don't fake keys for MMA */
182         int debug;
183         };
184
185 struct CMS_RecipientInfo_st
186         {
187         int type;
188         union   {
189                 CMS_KeyTransRecipientInfo *ktri;
190                 CMS_KeyAgreeRecipientInfo *kari;
191                 CMS_KEKRecipientInfo *kekri;
192                 CMS_PasswordRecipientInfo *pwri;
193                 CMS_OtherRecipientInfo *ori;
194                 } d;
195         };
196
197 typedef CMS_SignerIdentifier CMS_RecipientIdentifier;
198
199 struct CMS_KeyTransRecipientInfo_st
200         {
201         long version;
202         CMS_RecipientIdentifier *rid;
203         X509_ALGOR *keyEncryptionAlgorithm;
204         ASN1_OCTET_STRING *encryptedKey;
205         /* Recipient Key and cert */
206         X509 *recip;
207         EVP_PKEY *pkey;
208         /* Public key context for this operation */
209         EVP_PKEY_CTX *pctx;
210         };
211
212 struct CMS_KeyAgreeRecipientInfo_st
213         {
214         long version;
215         CMS_OriginatorIdentifierOrKey *originator;
216         ASN1_OCTET_STRING *ukm;
217         X509_ALGOR *keyEncryptionAlgorithm;
218         STACK_OF(CMS_RecipientEncryptedKey) *recipientEncryptedKeys;
219         };
220
221 struct CMS_OriginatorIdentifierOrKey_st
222         {
223         int type;
224         union   {
225                 CMS_IssuerAndSerialNumber *issuerAndSerialNumber;
226                 ASN1_OCTET_STRING *subjectKeyIdentifier;
227                 CMS_OriginatorPublicKey *originatorKey;
228                 } d;
229         };
230
231 struct CMS_OriginatorPublicKey_st
232         {
233         X509_ALGOR *algorithm;
234         ASN1_BIT_STRING *publicKey;
235         };
236
237 struct CMS_RecipientEncryptedKey_st
238         {
239         CMS_KeyAgreeRecipientIdentifier *rid;
240         ASN1_OCTET_STRING *encryptedKey;
241         };
242
243 struct CMS_KeyAgreeRecipientIdentifier_st
244         {
245         int type;
246         union   {
247                 CMS_IssuerAndSerialNumber *issuerAndSerialNumber;
248                 CMS_RecipientKeyIdentifier *rKeyId;
249                 } d;
250         };
251
252 struct CMS_RecipientKeyIdentifier_st
253         {
254         ASN1_OCTET_STRING *subjectKeyIdentifier;
255         ASN1_GENERALIZEDTIME *date;
256         CMS_OtherKeyAttribute *other;
257         };
258
259 struct CMS_KEKRecipientInfo_st
260         {
261         long version;
262         CMS_KEKIdentifier *kekid;
263         X509_ALGOR *keyEncryptionAlgorithm;
264         ASN1_OCTET_STRING *encryptedKey;
265         /* Extra info: symmetric key to use */
266         unsigned char *key;
267         size_t keylen;
268         };
269
270 struct CMS_KEKIdentifier_st
271         {
272         ASN1_OCTET_STRING *keyIdentifier;
273         ASN1_GENERALIZEDTIME *date;
274         CMS_OtherKeyAttribute *other;
275         };
276
277 struct CMS_PasswordRecipientInfo_st
278         {
279         long version;
280         X509_ALGOR *keyDerivationAlgorithm;
281         X509_ALGOR *keyEncryptionAlgorithm;
282         ASN1_OCTET_STRING *encryptedKey;
283         /* Extra info: password to use */
284         unsigned char *pass;
285         size_t passlen;
286         };
287
288 struct CMS_OtherRecipientInfo_st
289         {
290         ASN1_OBJECT *oriType;
291         ASN1_TYPE *oriValue;
292         };
293
294 struct CMS_DigestedData_st
295         {
296         long version;
297         X509_ALGOR *digestAlgorithm;
298         CMS_EncapsulatedContentInfo *encapContentInfo;
299         ASN1_OCTET_STRING *digest;
300         };
301
302 struct CMS_EncryptedData_st
303         {
304         long version;
305         CMS_EncryptedContentInfo *encryptedContentInfo;
306         STACK_OF(X509_ATTRIBUTE) *unprotectedAttrs;
307         };
308
309 struct CMS_AuthenticatedData_st
310         {
311         long version;
312         CMS_OriginatorInfo *originatorInfo;
313         STACK_OF(CMS_RecipientInfo) *recipientInfos;
314         X509_ALGOR *macAlgorithm;
315         X509_ALGOR *digestAlgorithm;
316         CMS_EncapsulatedContentInfo *encapContentInfo;
317         STACK_OF(X509_ATTRIBUTE) *authAttrs;
318         ASN1_OCTET_STRING *mac;
319         STACK_OF(X509_ATTRIBUTE) *unauthAttrs;
320         };
321
322 struct CMS_CompressedData_st
323         {
324         long version;
325         X509_ALGOR *compressionAlgorithm;
326         STACK_OF(CMS_RecipientInfo) *recipientInfos;
327         CMS_EncapsulatedContentInfo *encapContentInfo;
328         };
329
330 struct CMS_RevocationInfoChoice_st
331         {
332         int type;
333         union   {
334                 X509_CRL *crl;
335                 CMS_OtherRevocationInfoFormat *other;
336                 } d;
337         };
338
339 #define CMS_REVCHOICE_CRL               0
340 #define CMS_REVCHOICE_OTHER             1
341
342 struct CMS_OtherRevocationInfoFormat_st
343         {
344         ASN1_OBJECT *otherRevInfoFormat;
345         ASN1_TYPE *otherRevInfo;
346         };
347
348 struct CMS_CertificateChoices
349         {
350         int type;
351                 union {
352                 X509 *certificate;
353                 ASN1_STRING *extendedCertificate;       /* Obsolete */
354                 ASN1_STRING *v1AttrCert;        /* Left encoded for now */
355                 ASN1_STRING *v2AttrCert;        /* Left encoded for now */
356                 CMS_OtherCertificateFormat *other;
357                 } d;
358         };
359
360 #define CMS_CERTCHOICE_CERT             0
361 #define CMS_CERTCHOICE_EXCERT           1
362 #define CMS_CERTCHOICE_V1ACERT          2
363 #define CMS_CERTCHOICE_V2ACERT          3
364 #define CMS_CERTCHOICE_OTHER            4
365
366 struct CMS_OtherCertificateFormat_st
367         {
368         ASN1_OBJECT *otherCertFormat;
369         ASN1_TYPE *otherCert;
370         };
371
372 /* This is also defined in pkcs7.h but we duplicate it
373  * to allow the CMS code to be independent of PKCS#7
374  */
375
376 struct CMS_IssuerAndSerialNumber_st
377         {
378         X509_NAME *issuer;
379         ASN1_INTEGER *serialNumber;
380         };
381
382 struct CMS_OtherKeyAttribute_st
383         {
384         ASN1_OBJECT *keyAttrId;
385         ASN1_TYPE *keyAttr;
386         };
387
388 /* ESS structures */
389
390 #ifdef HEADER_X509V3_H
391
392 struct CMS_ReceiptRequest_st
393         {
394         ASN1_OCTET_STRING *signedContentIdentifier;
395         CMS_ReceiptsFrom *receiptsFrom;
396         STACK_OF(GENERAL_NAMES) *receiptsTo;
397         };
398
399
400 struct CMS_ReceiptsFrom_st
401         {
402         int type;
403         union
404                 {
405                 long allOrFirstTier;
406                 STACK_OF(GENERAL_NAMES) *receiptList;
407                 } d;
408         };
409 #endif
410
411 struct CMS_Receipt_st
412         {
413         long version;
414         ASN1_OBJECT *contentType;
415         ASN1_OCTET_STRING *signedContentIdentifier;
416         ASN1_OCTET_STRING *originatorSignatureValue;
417         };
418
419 DECLARE_ASN1_FUNCTIONS(CMS_ContentInfo)
420 DECLARE_ASN1_ITEM(CMS_SignerInfo)
421 DECLARE_ASN1_ITEM(CMS_IssuerAndSerialNumber)
422 DECLARE_ASN1_ITEM(CMS_Attributes_Sign)
423 DECLARE_ASN1_ITEM(CMS_Attributes_Verify)
424 DECLARE_ASN1_ITEM(CMS_RecipientInfo)
425 DECLARE_ASN1_ITEM(CMS_PasswordRecipientInfo)
426 DECLARE_ASN1_ALLOC_FUNCTIONS(CMS_IssuerAndSerialNumber)
427
428 #define CMS_SIGNERINFO_ISSUER_SERIAL    0
429 #define CMS_SIGNERINFO_KEYIDENTIFIER    1
430
431 #define CMS_RECIPINFO_ISSUER_SERIAL     0
432 #define CMS_RECIPINFO_KEYIDENTIFIER     1
433
434 BIO *cms_content_bio(CMS_ContentInfo *cms);
435
436 CMS_ContentInfo *cms_Data_create(void);
437
438 CMS_ContentInfo *cms_DigestedData_create(const EVP_MD *md);
439 BIO *cms_DigestedData_init_bio(CMS_ContentInfo *cms);
440 int cms_DigestedData_do_final(CMS_ContentInfo *cms, BIO *chain, int verify);
441
442 BIO *cms_SignedData_init_bio(CMS_ContentInfo *cms);
443 int cms_SignedData_final(CMS_ContentInfo *cms, BIO *chain);
444 int cms_set1_SignerIdentifier(CMS_SignerIdentifier *sid, X509 *cert, int type);
445 int cms_SignerIdentifier_get0_signer_id(CMS_SignerIdentifier *sid,
446                                         ASN1_OCTET_STRING **keyid,
447                                         X509_NAME **issuer, ASN1_INTEGER **sno);
448 int cms_SignerIdentifier_cert_cmp(CMS_SignerIdentifier *sid, X509 *cert);
449
450 CMS_ContentInfo *cms_CompressedData_create(int comp_nid);
451 BIO *cms_CompressedData_init_bio(CMS_ContentInfo *cms);
452
453 BIO *cms_DigestAlgorithm_init_bio(X509_ALGOR *digestAlgorithm);
454 int cms_DigestAlgorithm_find_ctx(EVP_MD_CTX *mctx, BIO *chain,
455                                         X509_ALGOR *mdalg);
456
457 BIO *cms_EncryptedContent_init_bio(CMS_EncryptedContentInfo *ec);
458 BIO *cms_EncryptedData_init_bio(CMS_ContentInfo *cms);
459 int cms_EncryptedContent_init(CMS_EncryptedContentInfo *ec, 
460                                 const EVP_CIPHER *cipher,
461                                 const unsigned char *key, size_t keylen);
462
463 int cms_Receipt_verify(CMS_ContentInfo *cms, CMS_ContentInfo *req_cms);
464 int cms_msgSigDigest_add1(CMS_SignerInfo *dest, CMS_SignerInfo *src);
465 ASN1_OCTET_STRING *cms_encode_Receipt(CMS_SignerInfo *si);
466
467 BIO *cms_EnvelopedData_init_bio(CMS_ContentInfo *cms);
468 CMS_EnvelopedData *cms_get0_enveloped(CMS_ContentInfo *cms);
469
470 /* PWRI routines */
471 int cms_RecipientInfo_pwri_crypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri,
472                                                         int en_de);
473         
474 #ifdef  __cplusplus
475 }
476 #endif
477 #endif