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