Initial support for Encrypted Data type generation.
[openssl.git] / crypto / cms / cms_lib.c
1 /* crypto/cms/cms_lib.c */
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 #include <openssl/asn1t.h>
55 #include <openssl/x509.h>
56 #include <openssl/err.h>
57 #include <openssl/pem.h>
58 #include <openssl/bio.h>
59 #include <openssl/asn1.h>
60 #include "cms.h"
61 #include "cms_lcl.h"
62
63 IMPLEMENT_ASN1_FUNCTIONS(CMS_ContentInfo)
64 IMPLEMENT_ASN1_PRINT_FUNCTION(CMS_ContentInfo)
65
66 DECLARE_ASN1_ITEM(CMS_CertificateChoices)
67 DECLARE_ASN1_ITEM(CMS_RevocationInfoChoice)
68 DECLARE_STACK_OF(CMS_CertificateChoices)
69 DECLARE_STACK_OF(CMS_RevocationInfoChoice)
70
71 const ASN1_OBJECT *CMS_get0_type(CMS_ContentInfo *cms)
72         {
73         return cms->contentType;
74         }
75
76 CMS_ContentInfo *cms_Data_create(void)
77         {
78         CMS_ContentInfo *cms;
79         cms = CMS_ContentInfo_new();
80         if (cms)
81                 {
82                 cms->contentType = OBJ_nid2obj(NID_pkcs7_data);
83                 /* Never detached */
84                 CMS_set_detached(cms, 0);
85                 }
86         return cms;
87         }
88
89 BIO *cms_content_bio(CMS_ContentInfo *cms)
90         {
91         ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
92         if (!pos)
93                 return NULL;
94         /* If content detached data goes nowhere: create NULL BIO */
95         if (!*pos)
96                 return BIO_new(BIO_s_null());
97         /* If content not detached and created return memory BIO
98          */
99         if (!*pos || ((*pos)->flags == ASN1_STRING_FLAG_CONT))
100                 return BIO_new(BIO_s_mem());
101         /* Else content was read in: return read only BIO for it */
102         return BIO_new_mem_buf((*pos)->data, (*pos)->length);
103         }
104
105 BIO *CMS_dataInit(CMS_ContentInfo *cms, BIO *icont)
106         {
107         BIO *cmsbio, *cont;
108         if (icont)
109                 cont = icont;
110         else
111                 cont = cms_content_bio(cms);
112         if (!cont)
113                 {
114                 CMSerr(CMS_F_CMS_DATAINIT, CMS_R_NO_CONTENT);
115                 return NULL;
116                 }
117         switch (OBJ_obj2nid(cms->contentType))
118                 {
119
120                 case NID_pkcs7_data:
121                 return cont;
122
123                 case NID_pkcs7_signed:
124                 cmsbio = cms_SignedData_init_bio(cms);
125                 break;
126
127                 case NID_pkcs7_digest:
128                 cmsbio = cms_DigestedData_init_bio(cms);
129                 break;
130 #ifdef ZLIB
131                 case NID_id_smime_ct_compressedData:
132                 cmsbio = cms_CompressedData_init_bio(cms);
133                 break;
134 #endif
135
136                 case NID_pkcs7_encrypted:
137                 cmsbio = cms_EncryptedData_init_bio(cms);
138                 break;
139
140                 default:
141                 CMSerr(CMS_F_CMS_DATAINIT, CMS_R_UNSUPPORTED_TYPE);
142                 return NULL;
143                 }
144
145         if (cmsbio)
146                 return BIO_push(cmsbio, cont);
147
148         if (!icont)
149                 BIO_free(cont);
150         return NULL;
151
152         }
153
154 int CMS_dataFinal(CMS_ContentInfo *cms, BIO *cmsbio)
155         {
156         ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
157         if (!pos)
158                 return 0;
159         /* If ebmedded content find memory BIO and set content */
160         if (*pos && ((*pos)->flags & ASN1_STRING_FLAG_CONT))
161                 {
162                 BIO *mbio;
163                 unsigned char *cont;
164                 long contlen;
165                 mbio = BIO_find_type(cmsbio, BIO_TYPE_MEM);
166                 if (!mbio)
167                         {
168                         CMSerr(CMS_F_CMS_DATAFINAL, CMS_R_CONTENT_NOT_FOUND);
169                         return 0;
170                         }
171                 contlen = BIO_get_mem_data(mbio, &cont);
172                 /* Set bio as read only so its content can't be clobbered */
173                 BIO_set_flags(mbio, BIO_FLAGS_MEM_RDONLY);
174                 BIO_set_mem_eof_return(mbio, 0);
175                 ASN1_STRING_set0(*pos, cont, contlen);
176                 (*pos)->flags &= ~ASN1_STRING_FLAG_CONT;
177                 }
178
179         switch (OBJ_obj2nid(cms->contentType))
180                 {
181
182                 case NID_pkcs7_data:
183                 case NID_pkcs7_encrypted:
184                 case NID_id_smime_ct_compressedData:
185                 /* Nothing to do */
186                 return 1;
187
188                 case NID_pkcs7_signed:
189                 return cms_SignedData_final(cms, cmsbio);
190
191                 case NID_pkcs7_digest:
192                 return cms_DigestedData_do_final(cms, cmsbio, 0);
193
194                 default:
195                 CMSerr(CMS_F_CMS_DATAFINAL, CMS_R_UNSUPPORTED_TYPE);
196                 return 0;
197                 }
198         }
199
200 /* Return an OCTET STRING pointer to content. This allows it to
201  * be accessed or set later.
202  */
203
204 ASN1_OCTET_STRING **CMS_get0_content(CMS_ContentInfo *cms)
205         {
206         switch (OBJ_obj2nid(cms->contentType))
207                 {
208
209                 case NID_pkcs7_data:
210                 return &cms->d.data;
211
212                 case NID_pkcs7_signed:
213                 return &cms->d.signedData->encapContentInfo->eContent;
214
215                 case NID_pkcs7_enveloped:
216                 return &cms->d.envelopedData->encryptedContentInfo->encryptedContent;
217
218                 case NID_pkcs7_digest:
219                 return &cms->d.digestedData->encapContentInfo->eContent;
220
221                 case NID_pkcs7_encrypted:
222                 return &cms->d.encryptedData->encryptedContentInfo->encryptedContent;
223
224                 case NID_id_smime_ct_authData:
225                 return &cms->d.authenticatedData->encapContentInfo->eContent;
226
227                 case NID_id_smime_ct_compressedData:
228                 return &cms->d.compressedData->encapContentInfo->eContent;
229
230                 default:
231                 if (cms->d.other->type == V_ASN1_OCTET_STRING)
232                         return &cms->d.other->value.octet_string;
233                 CMSerr(CMS_F_CMS_GET0_CONTENT, CMS_R_UNSUPPORTED_CONTENT_TYPE);
234                 return NULL;
235
236                 }
237         }
238
239 /* Return an ASN1_OBJECT pointer to content type. This allows it to
240  * be accessed or set later.
241  */
242
243 static ASN1_OBJECT **cms_get0_econtent_type(CMS_ContentInfo *cms)
244         {
245         switch (OBJ_obj2nid(cms->contentType))
246                 {
247
248                 case NID_pkcs7_signed:
249                 return &cms->d.signedData->encapContentInfo->eContentType;
250
251                 case NID_pkcs7_enveloped:
252                 return &cms->d.envelopedData->encryptedContentInfo->contentType;
253
254                 case NID_pkcs7_digest:
255                 return &cms->d.digestedData->encapContentInfo->eContentType;
256
257                 case NID_pkcs7_encrypted:
258                 return &cms->d.encryptedData->encryptedContentInfo->contentType;
259
260                 case NID_id_smime_ct_authData:
261                 return &cms->d.authenticatedData->encapContentInfo->eContentType;
262
263                 case NID_id_smime_ct_compressedData:
264                 return &cms->d.compressedData->encapContentInfo->eContentType;
265
266                 default:
267                 CMSerr(CMS_F_CMS_GET0_ECONTENT_TYPE,
268                                         CMS_R_UNSUPPORTED_CONTENT_TYPE);
269                 return NULL;
270
271                 }
272         }
273
274 const ASN1_OBJECT *CMS_get0_eContentType(CMS_ContentInfo *cms)
275         {
276         ASN1_OBJECT **petype;
277         petype = cms_get0_econtent_type(cms);
278         if (petype)
279                 return *petype;
280         return NULL;
281         }
282
283 int CMS_set1_eContentType(CMS_ContentInfo *cms, const ASN1_OBJECT *oid)
284         {
285         ASN1_OBJECT **petype, *etype;
286         petype = cms_get0_econtent_type(cms);
287         if (!petype)
288                 return 0;
289         if (!oid)
290                 return 1;
291         etype = OBJ_dup(oid);
292         if (!etype)
293                 return 0;
294         ASN1_OBJECT_free(*petype);
295         *petype = etype;
296         return 1;
297         }
298
299 int CMS_is_detached(CMS_ContentInfo *cms)
300         {
301         ASN1_OCTET_STRING **pos;
302         pos = CMS_get0_content(cms);
303         if (!pos)
304                 return -1;
305         if (*pos)
306                 return 0;
307         return 1;
308         }
309
310 int CMS_set_detached(CMS_ContentInfo *cms, int detached)
311         {
312         ASN1_OCTET_STRING **pos;
313         pos = CMS_get0_content(cms);
314         if (!pos)
315                 return 0;
316         if (detached)
317                 {
318                 if (*pos)
319                         {
320                         ASN1_OCTET_STRING_free(*pos);
321                         *pos = NULL;
322                         }
323                 return 1;
324                 }
325         if (!*pos)
326                 *pos = ASN1_OCTET_STRING_new();
327         if (*pos)
328                 {
329                 /* NB: special flag to show content is created and not
330                  * read in.
331                  */
332                 (*pos)->flags |= ASN1_STRING_FLAG_CONT;
333                 return 1;
334                 }
335         CMSerr(CMS_F_CMS_SET_DETACHED, ERR_R_MALLOC_FAILURE);
336         return 0;
337         }
338
339 /* Set up an X509_ALGOR DigestAlgorithmIdentifier from an EVP_MD */
340
341 void cms_DigestAlgorithm_set(X509_ALGOR *alg, const EVP_MD *md)
342         {
343         int param_type;
344
345         if (md->flags & EVP_MD_FLAG_DIGALGID_ABSENT)
346                 param_type = V_ASN1_UNDEF;
347         else
348                 param_type = V_ASN1_NULL;
349
350         X509_ALGOR_set0(alg, OBJ_nid2obj(EVP_MD_type(md)), param_type, NULL);
351
352         }
353
354 /* Create a digest BIO from an X509_ALGOR structure */
355
356 BIO *cms_DigestAlgorithm_init_bio(X509_ALGOR *digestAlgorithm)
357         {
358         BIO *mdbio = NULL;
359         ASN1_OBJECT *digestoid;
360         const EVP_MD *digest;
361         X509_ALGOR_get0(&digestoid, NULL, NULL, digestAlgorithm);
362         digest = EVP_get_digestbyobj(digestoid);
363         if (!digest)
364                 {
365                 CMSerr(CMS_F_CMS_DIGESTALGORITHM_INIT_BIO,
366                                 CMS_R_UNKNOWN_DIGEST_ALGORIHM);
367                 goto err;       
368                 }
369         mdbio = BIO_new(BIO_f_md());
370         if (!mdbio || !BIO_set_md(mdbio, digest))
371                 {
372                 CMSerr(CMS_F_CMS_DIGESTALGORITHM_INIT_BIO,
373                                 CMS_R_MD_BIO_INIT_ERROR);
374                 goto err;       
375                 }
376         return mdbio;
377         err:
378         if (mdbio)
379                 BIO_free(mdbio);
380         return NULL;
381         }
382
383 /* Locate a message digest content from a BIO chain based on SignerInfo */
384
385 int cms_DigestAlgorithm_find_ctx(EVP_MD_CTX *mctx, BIO *chain,
386                                         X509_ALGOR *mdalg)
387         {
388         int nid;
389         ASN1_OBJECT *mdoid;
390         X509_ALGOR_get0(&mdoid, NULL, NULL, mdalg);
391         nid = OBJ_obj2nid(mdoid);
392         /* Look for digest type to match signature */
393         for (;;)
394                 {
395                 EVP_MD_CTX *mtmp;
396                 chain = BIO_find_type(chain, BIO_TYPE_MD);
397                 if (chain == NULL)
398                         {
399                         CMSerr(CMS_F_CMS_DIGESTALGORITHM_FIND_CTX,
400                                                 CMS_R_NO_MATCHING_DIGEST);
401                         return 0;
402                         }
403                 BIO_get_md_ctx(chain, &mtmp);
404                 if (EVP_MD_CTX_type(mtmp) == nid)
405                         {
406                         EVP_MD_CTX_copy_ex(mctx, mtmp);
407                         return 1;
408                         }
409                 chain = BIO_next(chain);
410                 }
411         }
412
413 STACK_OF(CMS_CertificateChoices) **cms_get0_certificate_choices(CMS_ContentInfo *cms)
414         {
415         switch (OBJ_obj2nid(cms->contentType))
416                 {
417
418                 case NID_pkcs7_signed:
419                 return &cms->d.signedData->certificates;
420
421                 case NID_pkcs7_enveloped:
422                 return &cms->d.envelopedData->originatorInfo->certificates;
423
424                 default:
425                 CMSerr(CMS_F_CMS_GET0_CERTIFICATE_CHOICES,
426                                         CMS_R_UNSUPPORTED_CONTENT_TYPE);
427                 return NULL;
428
429                 }
430         }
431
432 CMS_CertificateChoices *CMS_add0_CertificateChoices(CMS_ContentInfo *cms)
433         {
434         STACK_OF(CMS_CertificateChoices) **pcerts;
435         CMS_CertificateChoices *cch;
436         pcerts = cms_get0_certificate_choices(cms);
437         if (!pcerts)
438                 return NULL;
439         if (!*pcerts)
440                 *pcerts = sk_CMS_CertificateChoices_new_null();
441         if (!*pcerts)
442                 return NULL;
443         cch = M_ASN1_new_of(CMS_CertificateChoices);
444         if (!cch)
445                 return NULL;
446         if (!sk_CMS_CertificateChoices_push(*pcerts, cch))
447                 {
448                 M_ASN1_free_of(cch, CMS_CertificateChoices);
449                 return NULL;
450                 }
451         return cch;
452         }
453
454 int CMS_add0_cert(CMS_ContentInfo *cms, X509 *cert)
455         {
456         CMS_CertificateChoices *cch;
457         STACK_OF(CMS_CertificateChoices) **pcerts;
458         int i;
459         pcerts = cms_get0_certificate_choices(cms);
460         if (!pcerts)
461                 return 0;
462         if (!pcerts)
463                 return 0;
464         for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++)
465                 {
466                 cch = sk_CMS_CertificateChoices_value(*pcerts, i);
467                 if (cch->type == CMS_CERTCHOICE_CERT)
468                         {
469                         if (!X509_cmp(cch->d.certificate, cert))
470                                 return -1;
471                                 
472                         }
473                 }
474         cch = CMS_add0_CertificateChoices(cms);
475         if (!cch)
476                 return 0;
477         cch->type = CMS_CERTCHOICE_CERT;
478         cch->d.certificate = cert;
479         return 1;
480         }
481
482 int CMS_add1_cert(CMS_ContentInfo *cms, X509 *cert)
483         {
484         int r;
485         r = CMS_add0_cert(cms, cert);
486         if (r > 0)
487                 CRYPTO_add(&cert->references, 1, CRYPTO_LOCK_X509);
488         return r;
489         }
490
491 STACK_OF(CMS_RevocationInfoChoice) **cms_get0_revocation_choices(CMS_ContentInfo *cms)
492         {
493         switch (OBJ_obj2nid(cms->contentType))
494                 {
495
496                 case NID_pkcs7_signed:
497                 return &cms->d.signedData->crls;
498
499                 case NID_pkcs7_enveloped:
500                 return &cms->d.envelopedData->originatorInfo->crls;
501
502                 default:
503                 CMSerr(CMS_F_CMS_GET0_REVOCATION_CHOICES,
504                                         CMS_R_UNSUPPORTED_CONTENT_TYPE);
505                 return NULL;
506
507                 }
508         }
509
510 CMS_RevocationInfoChoice *CMS_add0_RevocationInfoChoice(CMS_ContentInfo *cms)
511         {
512         STACK_OF(CMS_RevocationInfoChoice) **pcrls;
513         CMS_RevocationInfoChoice *rch;
514         pcrls = cms_get0_revocation_choices(cms);
515         if (!pcrls)
516                 return NULL;
517         if (!*pcrls)
518                 *pcrls = sk_CMS_RevocationInfoChoice_new_null();
519         if (!*pcrls)
520                 return NULL;
521         rch = M_ASN1_new_of(CMS_RevocationInfoChoice);
522         if (!rch)
523                 return NULL;
524         if (!sk_CMS_RevocationInfoChoice_push(*pcrls, rch))
525                 {
526                 M_ASN1_free_of(rch, CMS_RevocationInfoChoice);
527                 return NULL;
528                 }
529         return rch;
530         }
531
532 int CMS_add0_crl(CMS_ContentInfo *cms, X509_CRL *crl)
533         {
534         CMS_RevocationInfoChoice *rch;
535         rch = CMS_add0_RevocationInfoChoice(cms);
536         if (!rch)
537                 return 0;
538         rch->type = CMS_REVCHOICE_CRL;
539         rch->d.crl = crl;
540         return 1;
541         }
542
543 STACK_OF(X509) *CMS_get1_certs(CMS_ContentInfo *cms)
544         {
545         STACK_OF(X509) *certs = NULL;
546         CMS_CertificateChoices *cch;
547         STACK_OF(CMS_CertificateChoices) **pcerts;
548         int i;
549         pcerts = cms_get0_certificate_choices(cms);
550         if (!pcerts)
551                 return NULL;
552         for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++)
553                 {
554                 cch = sk_CMS_CertificateChoices_value(*pcerts, i);
555                 if (cch->type == 0)
556                         {
557                         if (!certs)
558                                 {
559                                 certs = sk_X509_new_null();
560                                 if (!certs)
561                                         return NULL;
562                                 }
563                         if (!sk_X509_push(certs, cch->d.certificate))
564                                 {
565                                 sk_X509_pop_free(certs, X509_free);
566                                 return NULL;
567                                 }
568                         CRYPTO_add(&cch->d.certificate->references,
569                                                 1, CRYPTO_LOCK_X509);
570                         }
571                 }
572         return certs;
573
574         }
575
576 STACK_OF(X509_CRL) *CMS_get1_crls(CMS_ContentInfo *cms)
577         {
578         STACK_OF(X509_CRL) *crls = NULL;
579         STACK_OF(CMS_RevocationInfoChoice) **pcrls;
580         CMS_RevocationInfoChoice *rch;
581         int i;
582         pcrls = cms_get0_revocation_choices(cms);
583         if (!pcrls)
584                 return NULL;
585         for (i = 0; i < sk_CMS_RevocationInfoChoice_num(*pcrls); i++)
586                 {
587                 rch = sk_CMS_RevocationInfoChoice_value(*pcrls, i);
588                 if (rch->type == 0)
589                         {
590                         if (!crls)
591                                 {
592                                 crls = sk_X509_CRL_new_null();
593                                 if (!crls)
594                                         return NULL;
595                                 }
596                         if (!sk_X509_CRL_push(crls, rch->d.crl))
597                                 {
598                                 sk_X509_CRL_pop_free(crls, X509_CRL_free);
599                                 return NULL;
600                                 }
601                         CRYPTO_add(&rch->d.crl->references,
602                                                 1, CRYPTO_LOCK_X509_CRL);
603                         }
604                 }
605         return crls;
606         }