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