Copyright consolidation 09/10
[openssl.git] / crypto / cms / cms_lcl.h
1 /*
2  * Copyright 2008-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #ifndef HEADER_CMS_LCL_H
11 # define HEADER_CMS_LCL_H
12
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16
17 # include <openssl/x509.h>
18
19 /*
20  * Cryptographic message syntax (CMS) structures: taken from RFC3852
21  */
22
23 /* Forward references */
24
25 typedef struct CMS_IssuerAndSerialNumber_st CMS_IssuerAndSerialNumber;
26 typedef struct CMS_EncapsulatedContentInfo_st CMS_EncapsulatedContentInfo;
27 typedef struct CMS_SignerIdentifier_st CMS_SignerIdentifier;
28 typedef struct CMS_SignedData_st CMS_SignedData;
29 typedef struct CMS_OtherRevocationInfoFormat_st CMS_OtherRevocationInfoFormat;
30 typedef struct CMS_OriginatorInfo_st CMS_OriginatorInfo;
31 typedef struct CMS_EncryptedContentInfo_st CMS_EncryptedContentInfo;
32 typedef struct CMS_EnvelopedData_st CMS_EnvelopedData;
33 typedef struct CMS_DigestedData_st CMS_DigestedData;
34 typedef struct CMS_EncryptedData_st CMS_EncryptedData;
35 typedef struct CMS_AuthenticatedData_st CMS_AuthenticatedData;
36 typedef struct CMS_CompressedData_st CMS_CompressedData;
37 typedef struct CMS_OtherCertificateFormat_st CMS_OtherCertificateFormat;
38 typedef struct CMS_KeyTransRecipientInfo_st CMS_KeyTransRecipientInfo;
39 typedef struct CMS_OriginatorPublicKey_st CMS_OriginatorPublicKey;
40 typedef struct CMS_OriginatorIdentifierOrKey_st CMS_OriginatorIdentifierOrKey;
41 typedef struct CMS_KeyAgreeRecipientInfo_st CMS_KeyAgreeRecipientInfo;
42 typedef struct CMS_RecipientKeyIdentifier_st CMS_RecipientKeyIdentifier;
43 typedef struct CMS_KeyAgreeRecipientIdentifier_st
44     CMS_KeyAgreeRecipientIdentifier;
45 typedef struct CMS_KEKIdentifier_st CMS_KEKIdentifier;
46 typedef struct CMS_KEKRecipientInfo_st CMS_KEKRecipientInfo;
47 typedef struct CMS_PasswordRecipientInfo_st CMS_PasswordRecipientInfo;
48 typedef struct CMS_OtherRecipientInfo_st CMS_OtherRecipientInfo;
49 typedef struct CMS_ReceiptsFrom_st CMS_ReceiptsFrom;
50
51 struct CMS_ContentInfo_st {
52     ASN1_OBJECT *contentType;
53     union {
54         ASN1_OCTET_STRING *data;
55         CMS_SignedData *signedData;
56         CMS_EnvelopedData *envelopedData;
57         CMS_DigestedData *digestedData;
58         CMS_EncryptedData *encryptedData;
59         CMS_AuthenticatedData *authenticatedData;
60         CMS_CompressedData *compressedData;
61         ASN1_TYPE *other;
62         /* Other types ... */
63         void *otherData;
64     } d;
65 };
66
67 DEFINE_STACK_OF(CMS_CertificateChoices)
68
69 struct CMS_SignedData_st {
70     long version;
71     STACK_OF(X509_ALGOR) *digestAlgorithms;
72     CMS_EncapsulatedContentInfo *encapContentInfo;
73     STACK_OF(CMS_CertificateChoices) *certificates;
74     STACK_OF(CMS_RevocationInfoChoice) *crls;
75     STACK_OF(CMS_SignerInfo) *signerInfos;
76 };
77
78 struct CMS_EncapsulatedContentInfo_st {
79     ASN1_OBJECT *eContentType;
80     ASN1_OCTET_STRING *eContent;
81     /* Set to 1 if incomplete structure only part set up */
82     int partial;
83 };
84
85 struct CMS_SignerInfo_st {
86     long version;
87     CMS_SignerIdentifier *sid;
88     X509_ALGOR *digestAlgorithm;
89     STACK_OF(X509_ATTRIBUTE) *signedAttrs;
90     X509_ALGOR *signatureAlgorithm;
91     ASN1_OCTET_STRING *signature;
92     STACK_OF(X509_ATTRIBUTE) *unsignedAttrs;
93     /* Signing certificate and key */
94     X509 *signer;
95     EVP_PKEY *pkey;
96     /* Digest and public key context for alternative parameters */
97     EVP_MD_CTX *mctx;
98     EVP_PKEY_CTX *pctx;
99 };
100
101 struct CMS_SignerIdentifier_st {
102     int type;
103     union {
104         CMS_IssuerAndSerialNumber *issuerAndSerialNumber;
105         ASN1_OCTET_STRING *subjectKeyIdentifier;
106     } d;
107 };
108
109 struct CMS_EnvelopedData_st {
110     long version;
111     CMS_OriginatorInfo *originatorInfo;
112     STACK_OF(CMS_RecipientInfo) *recipientInfos;
113     CMS_EncryptedContentInfo *encryptedContentInfo;
114     STACK_OF(X509_ATTRIBUTE) *unprotectedAttrs;
115 };
116
117 struct CMS_OriginatorInfo_st {
118     STACK_OF(CMS_CertificateChoices) *certificates;
119     STACK_OF(CMS_RevocationInfoChoice) *crls;
120 };
121
122 struct CMS_EncryptedContentInfo_st {
123     ASN1_OBJECT *contentType;
124     X509_ALGOR *contentEncryptionAlgorithm;
125     ASN1_OCTET_STRING *encryptedContent;
126     /* Content encryption algorithm and key */
127     const EVP_CIPHER *cipher;
128     unsigned char *key;
129     size_t keylen;
130     /* Set to 1 if we are debugging decrypt and don't fake keys for MMA */
131     int debug;
132 };
133
134 struct CMS_RecipientInfo_st {
135     int type;
136     union {
137         CMS_KeyTransRecipientInfo *ktri;
138         CMS_KeyAgreeRecipientInfo *kari;
139         CMS_KEKRecipientInfo *kekri;
140         CMS_PasswordRecipientInfo *pwri;
141         CMS_OtherRecipientInfo *ori;
142     } d;
143 };
144
145 typedef CMS_SignerIdentifier CMS_RecipientIdentifier;
146
147 struct CMS_KeyTransRecipientInfo_st {
148     long version;
149     CMS_RecipientIdentifier *rid;
150     X509_ALGOR *keyEncryptionAlgorithm;
151     ASN1_OCTET_STRING *encryptedKey;
152     /* Recipient Key and cert */
153     X509 *recip;
154     EVP_PKEY *pkey;
155     /* Public key context for this operation */
156     EVP_PKEY_CTX *pctx;
157 };
158
159 struct CMS_KeyAgreeRecipientInfo_st {
160     long version;
161     CMS_OriginatorIdentifierOrKey *originator;
162     ASN1_OCTET_STRING *ukm;
163     X509_ALGOR *keyEncryptionAlgorithm;
164     STACK_OF(CMS_RecipientEncryptedKey) *recipientEncryptedKeys;
165     /* Public key context associated with current operation */
166     EVP_PKEY_CTX *pctx;
167     /* Cipher context for CEK wrapping */
168     EVP_CIPHER_CTX *ctx;
169 };
170
171 struct CMS_OriginatorIdentifierOrKey_st {
172     int type;
173     union {
174         CMS_IssuerAndSerialNumber *issuerAndSerialNumber;
175         ASN1_OCTET_STRING *subjectKeyIdentifier;
176         CMS_OriginatorPublicKey *originatorKey;
177     } d;
178 };
179
180 struct CMS_OriginatorPublicKey_st {
181     X509_ALGOR *algorithm;
182     ASN1_BIT_STRING *publicKey;
183 };
184
185 struct CMS_RecipientEncryptedKey_st {
186     CMS_KeyAgreeRecipientIdentifier *rid;
187     ASN1_OCTET_STRING *encryptedKey;
188     /* Public key associated with this recipient */
189     EVP_PKEY *pkey;
190 };
191
192 struct CMS_KeyAgreeRecipientIdentifier_st {
193     int type;
194     union {
195         CMS_IssuerAndSerialNumber *issuerAndSerialNumber;
196         CMS_RecipientKeyIdentifier *rKeyId;
197     } d;
198 };
199
200 struct CMS_RecipientKeyIdentifier_st {
201     ASN1_OCTET_STRING *subjectKeyIdentifier;
202     ASN1_GENERALIZEDTIME *date;
203     CMS_OtherKeyAttribute *other;
204 };
205
206 struct CMS_KEKRecipientInfo_st {
207     long version;
208     CMS_KEKIdentifier *kekid;
209     X509_ALGOR *keyEncryptionAlgorithm;
210     ASN1_OCTET_STRING *encryptedKey;
211     /* Extra info: symmetric key to use */
212     unsigned char *key;
213     size_t keylen;
214 };
215
216 struct CMS_KEKIdentifier_st {
217     ASN1_OCTET_STRING *keyIdentifier;
218     ASN1_GENERALIZEDTIME *date;
219     CMS_OtherKeyAttribute *other;
220 };
221
222 struct CMS_PasswordRecipientInfo_st {
223     long version;
224     X509_ALGOR *keyDerivationAlgorithm;
225     X509_ALGOR *keyEncryptionAlgorithm;
226     ASN1_OCTET_STRING *encryptedKey;
227     /* Extra info: password to use */
228     unsigned char *pass;
229     size_t passlen;
230 };
231
232 struct CMS_OtherRecipientInfo_st {
233     ASN1_OBJECT *oriType;
234     ASN1_TYPE *oriValue;
235 };
236
237 struct CMS_DigestedData_st {
238     long version;
239     X509_ALGOR *digestAlgorithm;
240     CMS_EncapsulatedContentInfo *encapContentInfo;
241     ASN1_OCTET_STRING *digest;
242 };
243
244 struct CMS_EncryptedData_st {
245     long version;
246     CMS_EncryptedContentInfo *encryptedContentInfo;
247     STACK_OF(X509_ATTRIBUTE) *unprotectedAttrs;
248 };
249
250 struct CMS_AuthenticatedData_st {
251     long version;
252     CMS_OriginatorInfo *originatorInfo;
253     STACK_OF(CMS_RecipientInfo) *recipientInfos;
254     X509_ALGOR *macAlgorithm;
255     X509_ALGOR *digestAlgorithm;
256     CMS_EncapsulatedContentInfo *encapContentInfo;
257     STACK_OF(X509_ATTRIBUTE) *authAttrs;
258     ASN1_OCTET_STRING *mac;
259     STACK_OF(X509_ATTRIBUTE) *unauthAttrs;
260 };
261
262 struct CMS_CompressedData_st {
263     long version;
264     X509_ALGOR *compressionAlgorithm;
265     STACK_OF(CMS_RecipientInfo) *recipientInfos;
266     CMS_EncapsulatedContentInfo *encapContentInfo;
267 };
268
269 struct CMS_RevocationInfoChoice_st {
270     int type;
271     union {
272         X509_CRL *crl;
273         CMS_OtherRevocationInfoFormat *other;
274     } d;
275 };
276
277 # define CMS_REVCHOICE_CRL               0
278 # define CMS_REVCHOICE_OTHER             1
279
280 struct CMS_OtherRevocationInfoFormat_st {
281     ASN1_OBJECT *otherRevInfoFormat;
282     ASN1_TYPE *otherRevInfo;
283 };
284
285 struct CMS_CertificateChoices {
286     int type;
287     union {
288         X509 *certificate;
289         ASN1_STRING *extendedCertificate; /* Obsolete */
290         ASN1_STRING *v1AttrCert; /* Left encoded for now */
291         ASN1_STRING *v2AttrCert; /* Left encoded for now */
292         CMS_OtherCertificateFormat *other;
293     } d;
294 };
295
296 # define CMS_CERTCHOICE_CERT             0
297 # define CMS_CERTCHOICE_EXCERT           1
298 # define CMS_CERTCHOICE_V1ACERT          2
299 # define CMS_CERTCHOICE_V2ACERT          3
300 # define CMS_CERTCHOICE_OTHER            4
301
302 struct CMS_OtherCertificateFormat_st {
303     ASN1_OBJECT *otherCertFormat;
304     ASN1_TYPE *otherCert;
305 };
306
307 /*
308  * This is also defined in pkcs7.h but we duplicate it to allow the CMS code
309  * to be independent of PKCS#7
310  */
311
312 struct CMS_IssuerAndSerialNumber_st {
313     X509_NAME *issuer;
314     ASN1_INTEGER *serialNumber;
315 };
316
317 struct CMS_OtherKeyAttribute_st {
318     ASN1_OBJECT *keyAttrId;
319     ASN1_TYPE *keyAttr;
320 };
321
322 /* ESS structures */
323
324 # ifdef HEADER_X509V3_H
325
326 struct CMS_ReceiptRequest_st {
327     ASN1_OCTET_STRING *signedContentIdentifier;
328     CMS_ReceiptsFrom *receiptsFrom;
329     STACK_OF(GENERAL_NAMES) *receiptsTo;
330 };
331
332 struct CMS_ReceiptsFrom_st {
333     int type;
334     union {
335         long allOrFirstTier;
336         STACK_OF(GENERAL_NAMES) *receiptList;
337     } d;
338 };
339 # endif
340
341 struct CMS_Receipt_st {
342     long version;
343     ASN1_OBJECT *contentType;
344     ASN1_OCTET_STRING *signedContentIdentifier;
345     ASN1_OCTET_STRING *originatorSignatureValue;
346 };
347
348 DECLARE_ASN1_FUNCTIONS(CMS_ContentInfo)
349 DECLARE_ASN1_ITEM(CMS_SignerInfo)
350 DECLARE_ASN1_ITEM(CMS_IssuerAndSerialNumber)
351 DECLARE_ASN1_ITEM(CMS_Attributes_Sign)
352 DECLARE_ASN1_ITEM(CMS_Attributes_Verify)
353 DECLARE_ASN1_ITEM(CMS_RecipientInfo)
354 DECLARE_ASN1_ITEM(CMS_PasswordRecipientInfo)
355 DECLARE_ASN1_ALLOC_FUNCTIONS(CMS_IssuerAndSerialNumber)
356
357 # define CMS_SIGNERINFO_ISSUER_SERIAL    0
358 # define CMS_SIGNERINFO_KEYIDENTIFIER    1
359
360 # define CMS_RECIPINFO_ISSUER_SERIAL     0
361 # define CMS_RECIPINFO_KEYIDENTIFIER     1
362
363 # define CMS_REK_ISSUER_SERIAL           0
364 # define CMS_REK_KEYIDENTIFIER           1
365
366 # define CMS_OIK_ISSUER_SERIAL           0
367 # define CMS_OIK_KEYIDENTIFIER           1
368 # define CMS_OIK_PUBKEY                  2
369
370 BIO *cms_content_bio(CMS_ContentInfo *cms);
371
372 CMS_ContentInfo *cms_Data_create(void);
373
374 CMS_ContentInfo *cms_DigestedData_create(const EVP_MD *md);
375 BIO *cms_DigestedData_init_bio(CMS_ContentInfo *cms);
376 int cms_DigestedData_do_final(CMS_ContentInfo *cms, BIO *chain, int verify);
377
378 BIO *cms_SignedData_init_bio(CMS_ContentInfo *cms);
379 int cms_SignedData_final(CMS_ContentInfo *cms, BIO *chain);
380 int cms_set1_SignerIdentifier(CMS_SignerIdentifier *sid, X509 *cert,
381                               int type);
382 int cms_SignerIdentifier_get0_signer_id(CMS_SignerIdentifier *sid,
383                                         ASN1_OCTET_STRING **keyid,
384                                         X509_NAME **issuer,
385                                         ASN1_INTEGER **sno);
386 int cms_SignerIdentifier_cert_cmp(CMS_SignerIdentifier *sid, X509 *cert);
387
388 CMS_ContentInfo *cms_CompressedData_create(int comp_nid);
389 BIO *cms_CompressedData_init_bio(CMS_ContentInfo *cms);
390
391 BIO *cms_DigestAlgorithm_init_bio(X509_ALGOR *digestAlgorithm);
392 int cms_DigestAlgorithm_find_ctx(EVP_MD_CTX *mctx, BIO *chain,
393                                  X509_ALGOR *mdalg);
394
395 int cms_ias_cert_cmp(CMS_IssuerAndSerialNumber *ias, X509 *cert);
396 int cms_keyid_cert_cmp(ASN1_OCTET_STRING *keyid, X509 *cert);
397 int cms_set1_ias(CMS_IssuerAndSerialNumber **pias, X509 *cert);
398 int cms_set1_keyid(ASN1_OCTET_STRING **pkeyid, X509 *cert);
399
400 BIO *cms_EncryptedContent_init_bio(CMS_EncryptedContentInfo *ec);
401 BIO *cms_EncryptedData_init_bio(CMS_ContentInfo *cms);
402 int cms_EncryptedContent_init(CMS_EncryptedContentInfo *ec,
403                               const EVP_CIPHER *cipher,
404                               const unsigned char *key, size_t keylen);
405
406 int cms_Receipt_verify(CMS_ContentInfo *cms, CMS_ContentInfo *req_cms);
407 int cms_msgSigDigest_add1(CMS_SignerInfo *dest, CMS_SignerInfo *src);
408 ASN1_OCTET_STRING *cms_encode_Receipt(CMS_SignerInfo *si);
409
410 BIO *cms_EnvelopedData_init_bio(CMS_ContentInfo *cms);
411 CMS_EnvelopedData *cms_get0_enveloped(CMS_ContentInfo *cms);
412 int cms_env_asn1_ctrl(CMS_RecipientInfo *ri, int cmd);
413 int cms_pkey_get_ri_type(EVP_PKEY *pk);
414 /* KARI routines */
415 int cms_RecipientInfo_kari_init(CMS_RecipientInfo *ri, X509 *recip,
416                                 EVP_PKEY *pk, unsigned int flags);
417 int cms_RecipientInfo_kari_encrypt(CMS_ContentInfo *cms,
418                                    CMS_RecipientInfo *ri);
419
420 /* PWRI routines */
421 int cms_RecipientInfo_pwri_crypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri,
422                                  int en_de);
423
424 DECLARE_ASN1_ITEM(CMS_CertificateChoices)
425 DECLARE_ASN1_ITEM(CMS_DigestedData)
426 DECLARE_ASN1_ITEM(CMS_EncryptedData)
427 DECLARE_ASN1_ITEM(CMS_EnvelopedData)
428 DECLARE_ASN1_ITEM(CMS_KEKRecipientInfo)
429 DECLARE_ASN1_ITEM(CMS_KeyAgreeRecipientInfo)
430 DECLARE_ASN1_ITEM(CMS_KeyTransRecipientInfo)
431 DECLARE_ASN1_ITEM(CMS_OriginatorPublicKey)
432 DECLARE_ASN1_ITEM(CMS_OtherKeyAttribute)
433 DECLARE_ASN1_ITEM(CMS_Receipt)
434 DECLARE_ASN1_ITEM(CMS_ReceiptRequest)
435 DECLARE_ASN1_ITEM(CMS_RecipientEncryptedKey)
436 DECLARE_ASN1_ITEM(CMS_RecipientKeyIdentifier)
437 DECLARE_ASN1_ITEM(CMS_RevocationInfoChoice)
438 DECLARE_ASN1_ITEM(CMS_SignedData)
439 DECLARE_ASN1_ITEM(CMS_CompressedData)
440
441 #ifdef  __cplusplus
442 }
443 #endif
444 #endif