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