Various tidies/fixes:
[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
95 struct CMS_ContentInfo_st
96         {
97         ASN1_OBJECT *contentType;
98         union   {
99                 ASN1_OCTET_STRING *data;
100                 CMS_SignedData *signedData;
101                 CMS_EnvelopedData *envelopedData;
102                 CMS_DigestedData *digestedData;
103                 CMS_EncryptedData *encryptedData;
104                 CMS_AuthenticatedData *authenticatedData;
105                 CMS_CompressedData *compressedData;
106                 ASN1_TYPE *other;
107                 /* Other types ... */
108                 void *otherData;
109                 } d;
110         };
111
112 struct CMS_SignedData_st
113         {
114         long version;
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;
120         };
121  
122 struct CMS_EncapsulatedContentInfo_st
123         {
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
130 struct CMS_SignerInfo_st
131         {
132         long version;
133         CMS_SignerIdentifier *sid;
134         X509_ALGOR *digestAlgorithm;
135         STACK_OF(X509_ATTRIBUTE) *signedAttrs;
136         X509_ALGOR *signatureAlgorithm;
137         ASN1_OCTET_STRING *signature;
138         STACK_OF(X509_ATTRIBUTE) *unsignedAttrs;
139         /* Signing certificate and key */
140         X509 *signer;
141         EVP_PKEY *pkey;
142         };
143
144 struct CMS_SignerIdentifier_st
145         {
146         int type;
147         union   {
148                 CMS_IssuerAndSerialNumber *issuerAndSerialNumber;
149                 ASN1_OCTET_STRING *subjectKeyIdentifier;
150                 } d;
151         };
152
153 struct CMS_EnvelopedData_st
154         {
155         long version;
156         CMS_OriginatorInfo *originatorInfo;
157         STACK_OF(CMS_RecipientInfo) *recipientInfos;
158         CMS_EncryptedContentInfo *encryptedContentInfo;
159         STACK_OF(X509_ATTRIBUTE) *unprotectedAttrs;
160         };
161
162 struct CMS_OriginatorInfo_st
163         {
164         STACK_OF(CMS_CertificateChoices) *certificates;
165         STACK_OF(CMS_RevocationInfoChoice) *crls;
166         };
167
168 struct CMS_EncryptedContentInfo_st
169         {
170         ASN1_OBJECT *contentType;
171         X509_ALGOR *contentEncryptionAlgorithm;
172         ASN1_OCTET_STRING *encryptedContent;
173         /* Content encryption algorithm and key */
174         const EVP_CIPHER *cipher;
175         unsigned char *key;
176         size_t keylen;
177         };
178
179 struct CMS_RecipientInfo_st
180         {
181         int type;
182         union   {
183                 CMS_KeyTransRecipientInfo *ktri;
184                 CMS_KeyAgreeRecipientInfo *kari;
185                 CMS_KEKRecipientInfo *kekri;
186                 CMS_PasswordRecipientInfo *pwri;
187                 CMS_OtherRecipientInfo *ori;
188                 } d;
189         };
190
191 typedef CMS_SignerIdentifier CMS_RecipientIdentifier;
192
193 struct CMS_KeyTransRecipientInfo_st
194         {
195         long version;
196         CMS_RecipientIdentifier *rid;
197         X509_ALGOR *keyEncryptionAlgorithm;
198         ASN1_OCTET_STRING *encryptedKey;
199         /* Recipient Key and cert */
200         X509 *recip;
201         EVP_PKEY *pkey;
202         };
203
204 struct CMS_KeyAgreeRecipientInfo_st
205         {
206         long version;
207         CMS_OriginatorIdentifierOrKey *originator;
208         ASN1_OCTET_STRING *ukm;
209         X509_ALGOR *keyEncryptionAlgorithm;
210         STACK_OF(CMS_RecipientEncryptedKey) *recipientEncryptedKeys;
211         };
212
213 struct CMS_OriginatorIdentifierOrKey_st
214         {
215         int type;
216         union   {
217                 CMS_IssuerAndSerialNumber *issuerAndSerialNumber;
218                 ASN1_OCTET_STRING *subjectKeyIdentifier;
219                 CMS_OriginatorPublicKey *originatorKey;
220                 } d;
221         };
222
223 struct CMS_OriginatorPublicKey_st
224         {
225         X509_ALGOR *algorithm;
226         ASN1_BIT_STRING *publicKey;
227         };
228
229 struct CMS_RecipientEncryptedKey_st
230         {
231         CMS_KeyAgreeRecipientIdentifier *rid;
232         ASN1_OCTET_STRING *encryptedKey;
233         };
234
235 struct CMS_KeyAgreeRecipientIdentifier_st
236         {
237         int type;
238         union   {
239                 CMS_IssuerAndSerialNumber *issuerAndSerialNumber;
240                 CMS_RecipientKeyIdentifier *rKeyId;
241                 } d;
242         };
243
244 struct CMS_RecipientKeyIdentifier_st
245         {
246         ASN1_OCTET_STRING *subjectKeyIdentifier;
247         ASN1_GENERALIZEDTIME *date;
248         CMS_OtherKeyAttribute *other;
249         };
250
251 struct CMS_KEKRecipientInfo_st
252         {
253         long version;
254         CMS_KEKIdentifier *kekid;
255         X509_ALGOR *keyEncryptionAlgorithm;
256         ASN1_OCTET_STRING *encryptedKey;
257         /* Extra info: symmetric key to use */
258         unsigned char *key;
259         size_t keylen;
260         };
261
262 struct CMS_KEKIdentifier_st
263         {
264         ASN1_OCTET_STRING *keyIdentifier;
265         ASN1_GENERALIZEDTIME *date;
266         CMS_OtherKeyAttribute *other;
267         };
268
269 struct CMS_PasswordRecipientInfo_st
270         {
271         long version;
272         X509_ALGOR *keyDerivationAlgorithm;
273         X509_ALGOR *keyEncryptionAlgorithm;
274         ASN1_OCTET_STRING *encryptedKey;
275         };
276
277 struct CMS_OtherRecipientInfo_st
278         {
279         ASN1_OBJECT *oriType;
280         ASN1_TYPE *oriValue;
281         };
282
283 struct CMS_DigestedData_st
284         {
285         long version;
286         X509_ALGOR *digestAlgorithm;
287         CMS_EncapsulatedContentInfo *encapContentInfo;
288         ASN1_OCTET_STRING *digest;
289         };
290
291 struct CMS_EncryptedData_st
292         {
293         long version;
294         CMS_EncryptedContentInfo *encryptedContentInfo;
295         STACK_OF(X509_ATTRIBUTE) *unprotectedAttrs;
296         };
297
298 struct CMS_AuthenticatedData_st
299         {
300         long version;
301         CMS_OriginatorInfo *originatorInfo;
302         STACK_OF(CMS_RecipientInfo) *recipientInfos;
303         X509_ALGOR *macAlgorithm;
304         X509_ALGOR *digestAlgorithm;
305         CMS_EncapsulatedContentInfo *encapContentInfo;
306         STACK_OF(X509_ATTRIBUTE) *authAttrs;
307         ASN1_OCTET_STRING *mac;
308         STACK_OF(X509_ATTRIBUTE) *unauthAttrs;
309         };
310
311 struct CMS_CompressedData_st
312         {
313         long version;
314         X509_ALGOR *compressionAlgorithm;
315         STACK_OF(CMS_RecipientInfo) *recipientInfos;
316         CMS_EncapsulatedContentInfo *encapContentInfo;
317         };
318
319 struct CMS_RevocationInfoChoice_st
320         {
321         int type;
322         union   {
323                 X509_CRL *crl;
324                 CMS_OtherRevocationInfoFormat *other;
325                 } d;
326         };
327
328 #define CMS_REVCHOICE_CRL               0
329 #define CMS_REVCHOICE_OTHER             1
330
331 struct CMS_OtherRevocationInfoFormat_st
332         {
333         ASN1_OBJECT *otherRevInfoFormat;
334         ASN1_TYPE *otherRevInfo;
335         };
336
337 struct CMS_CertificateChoices
338         {
339         int type;
340                 union {
341                 X509 *certificate;
342                 ASN1_STRING *extendedCertificate;       /* Obsolete */
343                 ASN1_STRING *v1AttrCert;        /* Left encoded for now */
344                 ASN1_STRING *v2AttrCert;        /* Left encoded for now */
345                 CMS_OtherCertificateFormat *other;
346                 } d;
347         };
348
349 #define CMS_CERTCHOICE_CERT             0
350 #define CMS_CERTCHOICE_EXCERT           1
351 #define CMS_CERTCHOICE_V1ACERT          2
352 #define CMS_CERTCHOICE_V2ACERT          3
353 #define CMS_CERTCHOICE_OTHER            4
354
355 struct CMS_OtherCertificateFormat_st
356         {
357         ASN1_OBJECT *otherCertFormat;
358         ASN1_TYPE *otherCert;
359         };
360
361 /* This is also defined in pkcs7.h but we duplicate it
362  * to allow the CMS code to be independent of PKCS#7
363  */
364
365 struct CMS_IssuerAndSerialNumber_st
366         {
367         X509_NAME *issuer;
368         ASN1_INTEGER *serialNumber;
369         };
370
371 struct CMS_OtherKeyAttribute_st
372         {
373         ASN1_OBJECT *keyAttrId;
374         ASN1_TYPE *keyAttr;
375         };
376
377 DECLARE_ASN1_FUNCTIONS(CMS_ContentInfo)
378 DECLARE_ASN1_ITEM(CMS_SignerInfo)
379 DECLARE_ASN1_ITEM(CMS_IssuerAndSerialNumber)
380 DECLARE_ASN1_ITEM(CMS_Attributes_Sign)
381 DECLARE_ASN1_ITEM(CMS_Attributes_Verify)
382 DECLARE_ASN1_ALLOC_FUNCTIONS(CMS_IssuerAndSerialNumber)
383
384 #define CMS_SIGNERINFO_ISSUER_SERIAL    0
385 #define CMS_SIGNERINFO_KEYIDENTIFIER    1
386
387 #define CMS_RECIPINFO_ISSUER_SERIAL     0
388 #define CMS_RECIPINFO_KEYIDENTIFIER     1
389
390 BIO *cms_content_bio(CMS_ContentInfo *cms);
391
392 CMS_ContentInfo *cms_Data_create(void);
393
394 CMS_ContentInfo *cms_DigestedData_create(const EVP_MD *md);
395 BIO *cms_DigestedData_init_bio(CMS_ContentInfo *cms);
396 int cms_DigestedData_do_final(CMS_ContentInfo *cms, BIO *chain, int verify);
397
398 BIO *cms_SignedData_init_bio(CMS_ContentInfo *cms);
399 int cms_SignedData_final(CMS_ContentInfo *cms, BIO *chain);
400 int cms_set1_SignerIdentifier(CMS_SignerIdentifier *sid, X509 *cert, int type);
401 int cms_SignerIdentifier_get0_signer_id(CMS_SignerIdentifier *sid,
402                                         ASN1_OCTET_STRING **keyid,
403                                         X509_NAME **issuer, ASN1_INTEGER **sno);
404 int cms_SignerIdentifier_cert_cmp(CMS_SignerIdentifier *sid, X509 *cert);
405
406 CMS_ContentInfo *cms_CompressedData_create(int comp_nid);
407 BIO *cms_CompressedData_init_bio(CMS_ContentInfo *cms);
408
409 void cms_DigestAlgorithm_set(X509_ALGOR *alg, const EVP_MD *md);
410 BIO *cms_DigestAlgorithm_init_bio(X509_ALGOR *digestAlgorithm);
411 int cms_DigestAlgorithm_find_ctx(EVP_MD_CTX *mctx, BIO *chain,
412                                         X509_ALGOR *mdalg);
413
414 BIO *cms_EncryptedContent_init_bio(CMS_EncryptedContentInfo *ec);
415 BIO *cms_EncryptedData_init_bio(CMS_ContentInfo *cms);
416 int cms_EncryptedContent_init(CMS_EncryptedContentInfo *ec, 
417                                 const EVP_CIPHER *cipher,
418                                 const unsigned char *key, size_t keylen);
419
420 BIO *cms_EnvelopedData_init_bio(CMS_ContentInfo *cms);
421         
422 #ifdef  __cplusplus
423 }
424 #endif
425 #endif