update cms code to use X509_ALGOR_set_md instead of internal function
[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         };
144
145 struct CMS_SignerIdentifier_st
146         {
147         int type;
148         union   {
149                 CMS_IssuerAndSerialNumber *issuerAndSerialNumber;
150                 ASN1_OCTET_STRING *subjectKeyIdentifier;
151                 } d;
152         };
153
154 struct CMS_EnvelopedData_st
155         {
156         long version;
157         CMS_OriginatorInfo *originatorInfo;
158         STACK_OF(CMS_RecipientInfo) *recipientInfos;
159         CMS_EncryptedContentInfo *encryptedContentInfo;
160         STACK_OF(X509_ATTRIBUTE) *unprotectedAttrs;
161         };
162
163 struct CMS_OriginatorInfo_st
164         {
165         STACK_OF(CMS_CertificateChoices) *certificates;
166         STACK_OF(CMS_RevocationInfoChoice) *crls;
167         };
168
169 struct CMS_EncryptedContentInfo_st
170         {
171         ASN1_OBJECT *contentType;
172         X509_ALGOR *contentEncryptionAlgorithm;
173         ASN1_OCTET_STRING *encryptedContent;
174         /* Content encryption algorithm and key */
175         const EVP_CIPHER *cipher;
176         unsigned char *key;
177         size_t keylen;
178         };
179
180 struct CMS_RecipientInfo_st
181         {
182         int type;
183         union   {
184                 CMS_KeyTransRecipientInfo *ktri;
185                 CMS_KeyAgreeRecipientInfo *kari;
186                 CMS_KEKRecipientInfo *kekri;
187                 CMS_PasswordRecipientInfo *pwri;
188                 CMS_OtherRecipientInfo *ori;
189                 } d;
190         };
191
192 typedef CMS_SignerIdentifier CMS_RecipientIdentifier;
193
194 struct CMS_KeyTransRecipientInfo_st
195         {
196         long version;
197         CMS_RecipientIdentifier *rid;
198         X509_ALGOR *keyEncryptionAlgorithm;
199         ASN1_OCTET_STRING *encryptedKey;
200         /* Recipient Key and cert */
201         X509 *recip;
202         EVP_PKEY *pkey;
203         };
204
205 struct CMS_KeyAgreeRecipientInfo_st
206         {
207         long version;
208         CMS_OriginatorIdentifierOrKey *originator;
209         ASN1_OCTET_STRING *ukm;
210         X509_ALGOR *keyEncryptionAlgorithm;
211         STACK_OF(CMS_RecipientEncryptedKey) *recipientEncryptedKeys;
212         };
213
214 struct CMS_OriginatorIdentifierOrKey_st
215         {
216         int type;
217         union   {
218                 CMS_IssuerAndSerialNumber *issuerAndSerialNumber;
219                 ASN1_OCTET_STRING *subjectKeyIdentifier;
220                 CMS_OriginatorPublicKey *originatorKey;
221                 } d;
222         };
223
224 struct CMS_OriginatorPublicKey_st
225         {
226         X509_ALGOR *algorithm;
227         ASN1_BIT_STRING *publicKey;
228         };
229
230 struct CMS_RecipientEncryptedKey_st
231         {
232         CMS_KeyAgreeRecipientIdentifier *rid;
233         ASN1_OCTET_STRING *encryptedKey;
234         };
235
236 struct CMS_KeyAgreeRecipientIdentifier_st
237         {
238         int type;
239         union   {
240                 CMS_IssuerAndSerialNumber *issuerAndSerialNumber;
241                 CMS_RecipientKeyIdentifier *rKeyId;
242                 } d;
243         };
244
245 struct CMS_RecipientKeyIdentifier_st
246         {
247         ASN1_OCTET_STRING *subjectKeyIdentifier;
248         ASN1_GENERALIZEDTIME *date;
249         CMS_OtherKeyAttribute *other;
250         };
251
252 struct CMS_KEKRecipientInfo_st
253         {
254         long version;
255         CMS_KEKIdentifier *kekid;
256         X509_ALGOR *keyEncryptionAlgorithm;
257         ASN1_OCTET_STRING *encryptedKey;
258         /* Extra info: symmetric key to use */
259         unsigned char *key;
260         size_t keylen;
261         };
262
263 struct CMS_KEKIdentifier_st
264         {
265         ASN1_OCTET_STRING *keyIdentifier;
266         ASN1_GENERALIZEDTIME *date;
267         CMS_OtherKeyAttribute *other;
268         };
269
270 struct CMS_PasswordRecipientInfo_st
271         {
272         long version;
273         X509_ALGOR *keyDerivationAlgorithm;
274         X509_ALGOR *keyEncryptionAlgorithm;
275         ASN1_OCTET_STRING *encryptedKey;
276         /* Extra info: password to use */
277         unsigned char *pass;
278         size_t passlen;
279         };
280
281 struct CMS_OtherRecipientInfo_st
282         {
283         ASN1_OBJECT *oriType;
284         ASN1_TYPE *oriValue;
285         };
286
287 struct CMS_DigestedData_st
288         {
289         long version;
290         X509_ALGOR *digestAlgorithm;
291         CMS_EncapsulatedContentInfo *encapContentInfo;
292         ASN1_OCTET_STRING *digest;
293         };
294
295 struct CMS_EncryptedData_st
296         {
297         long version;
298         CMS_EncryptedContentInfo *encryptedContentInfo;
299         STACK_OF(X509_ATTRIBUTE) *unprotectedAttrs;
300         };
301
302 struct CMS_AuthenticatedData_st
303         {
304         long version;
305         CMS_OriginatorInfo *originatorInfo;
306         STACK_OF(CMS_RecipientInfo) *recipientInfos;
307         X509_ALGOR *macAlgorithm;
308         X509_ALGOR *digestAlgorithm;
309         CMS_EncapsulatedContentInfo *encapContentInfo;
310         STACK_OF(X509_ATTRIBUTE) *authAttrs;
311         ASN1_OCTET_STRING *mac;
312         STACK_OF(X509_ATTRIBUTE) *unauthAttrs;
313         };
314
315 struct CMS_CompressedData_st
316         {
317         long version;
318         X509_ALGOR *compressionAlgorithm;
319         STACK_OF(CMS_RecipientInfo) *recipientInfos;
320         CMS_EncapsulatedContentInfo *encapContentInfo;
321         };
322
323 struct CMS_RevocationInfoChoice_st
324         {
325         int type;
326         union   {
327                 X509_CRL *crl;
328                 CMS_OtherRevocationInfoFormat *other;
329                 } d;
330         };
331
332 #define CMS_REVCHOICE_CRL               0
333 #define CMS_REVCHOICE_OTHER             1
334
335 struct CMS_OtherRevocationInfoFormat_st
336         {
337         ASN1_OBJECT *otherRevInfoFormat;
338         ASN1_TYPE *otherRevInfo;
339         };
340
341 struct CMS_CertificateChoices
342         {
343         int type;
344                 union {
345                 X509 *certificate;
346                 ASN1_STRING *extendedCertificate;       /* Obsolete */
347                 ASN1_STRING *v1AttrCert;        /* Left encoded for now */
348                 ASN1_STRING *v2AttrCert;        /* Left encoded for now */
349                 CMS_OtherCertificateFormat *other;
350                 } d;
351         };
352
353 #define CMS_CERTCHOICE_CERT             0
354 #define CMS_CERTCHOICE_EXCERT           1
355 #define CMS_CERTCHOICE_V1ACERT          2
356 #define CMS_CERTCHOICE_V2ACERT          3
357 #define CMS_CERTCHOICE_OTHER            4
358
359 struct CMS_OtherCertificateFormat_st
360         {
361         ASN1_OBJECT *otherCertFormat;
362         ASN1_TYPE *otherCert;
363         };
364
365 /* This is also defined in pkcs7.h but we duplicate it
366  * to allow the CMS code to be independent of PKCS#7
367  */
368
369 struct CMS_IssuerAndSerialNumber_st
370         {
371         X509_NAME *issuer;
372         ASN1_INTEGER *serialNumber;
373         };
374
375 struct CMS_OtherKeyAttribute_st
376         {
377         ASN1_OBJECT *keyAttrId;
378         ASN1_TYPE *keyAttr;
379         };
380
381 /* ESS structures */
382
383 #ifdef HEADER_X509V3_H
384
385 struct CMS_ReceiptRequest_st
386         {
387         ASN1_OCTET_STRING *signedContentIdentifier;
388         CMS_ReceiptsFrom *receiptsFrom;
389         STACK_OF(GENERAL_NAMES) *receiptsTo;
390         };
391
392
393 struct CMS_ReceiptsFrom_st
394         {
395         int type;
396         union
397                 {
398                 long allOrFirstTier;
399                 STACK_OF(GENERAL_NAMES) *receiptList;
400                 } d;
401         };
402 #endif
403
404 struct CMS_Receipt_st
405         {
406         long version;
407         ASN1_OBJECT *contentType;
408         ASN1_OCTET_STRING *signedContentIdentifier;
409         ASN1_OCTET_STRING *originatorSignatureValue;
410         };
411
412 DECLARE_ASN1_FUNCTIONS(CMS_ContentInfo)
413 DECLARE_ASN1_ITEM(CMS_SignerInfo)
414 DECLARE_ASN1_ITEM(CMS_IssuerAndSerialNumber)
415 DECLARE_ASN1_ITEM(CMS_Attributes_Sign)
416 DECLARE_ASN1_ITEM(CMS_Attributes_Verify)
417 DECLARE_ASN1_ITEM(CMS_RecipientInfo)
418 DECLARE_ASN1_ITEM(CMS_PasswordRecipientInfo)
419 DECLARE_ASN1_ALLOC_FUNCTIONS(CMS_IssuerAndSerialNumber)
420
421 #define CMS_SIGNERINFO_ISSUER_SERIAL    0
422 #define CMS_SIGNERINFO_KEYIDENTIFIER    1
423
424 #define CMS_RECIPINFO_ISSUER_SERIAL     0
425 #define CMS_RECIPINFO_KEYIDENTIFIER     1
426
427 BIO *cms_content_bio(CMS_ContentInfo *cms);
428
429 CMS_ContentInfo *cms_Data_create(void);
430
431 CMS_ContentInfo *cms_DigestedData_create(const EVP_MD *md);
432 BIO *cms_DigestedData_init_bio(CMS_ContentInfo *cms);
433 int cms_DigestedData_do_final(CMS_ContentInfo *cms, BIO *chain, int verify);
434
435 BIO *cms_SignedData_init_bio(CMS_ContentInfo *cms);
436 int cms_SignedData_final(CMS_ContentInfo *cms, BIO *chain);
437 int cms_set1_SignerIdentifier(CMS_SignerIdentifier *sid, X509 *cert, int type);
438 int cms_SignerIdentifier_get0_signer_id(CMS_SignerIdentifier *sid,
439                                         ASN1_OCTET_STRING **keyid,
440                                         X509_NAME **issuer, ASN1_INTEGER **sno);
441 int cms_SignerIdentifier_cert_cmp(CMS_SignerIdentifier *sid, X509 *cert);
442
443 CMS_ContentInfo *cms_CompressedData_create(int comp_nid);
444 BIO *cms_CompressedData_init_bio(CMS_ContentInfo *cms);
445
446 BIO *cms_DigestAlgorithm_init_bio(X509_ALGOR *digestAlgorithm);
447 int cms_DigestAlgorithm_find_ctx(EVP_MD_CTX *mctx, BIO *chain,
448                                         X509_ALGOR *mdalg);
449
450 BIO *cms_EncryptedContent_init_bio(CMS_EncryptedContentInfo *ec);
451 BIO *cms_EncryptedData_init_bio(CMS_ContentInfo *cms);
452 int cms_EncryptedContent_init(CMS_EncryptedContentInfo *ec, 
453                                 const EVP_CIPHER *cipher,
454                                 const unsigned char *key, size_t keylen);
455
456 int cms_Receipt_verify(CMS_ContentInfo *cms, CMS_ContentInfo *req_cms);
457 int cms_msgSigDigest_add1(CMS_SignerInfo *dest, CMS_SignerInfo *src);
458 ASN1_OCTET_STRING *cms_encode_Receipt(CMS_SignerInfo *si);
459
460 BIO *cms_EnvelopedData_init_bio(CMS_ContentInfo *cms);
461 CMS_EnvelopedData *cms_get0_enveloped(CMS_ContentInfo *cms);
462
463 /* PWRI routines */
464 int cms_RecipientInfo_pwri_crypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri,
465                                                         int en_de);
466         
467 #ifdef  __cplusplus
468 }
469 #endif
470 #endif