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