157590d84515b98696fd7eee9cc22f7e872c07f0
[openssl.git] / crypto / cms / cms_lib.c
1 /* crypto/cms/cms_lib.c */
2 /*
3  * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
4  * project.
5  */
6 /* ====================================================================
7  * Copyright (c) 2008 The OpenSSL Project.  All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in
18  *    the documentation and/or other materials provided with the
19  *    distribution.
20  *
21  * 3. All advertising materials mentioning features or use of this
22  *    software must display the following acknowledgment:
23  *    "This product includes software developed by the OpenSSL Project
24  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25  *
26  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27  *    endorse or promote products derived from this software without
28  *    prior written permission. For written permission, please contact
29  *    licensing@OpenSSL.org.
30  *
31  * 5. Products derived from this software may not be called "OpenSSL"
32  *    nor may "OpenSSL" appear in their names without prior written
33  *    permission of the OpenSSL Project.
34  *
35  * 6. Redistributions of any form whatsoever must retain the following
36  *    acknowledgment:
37  *    "This product includes software developed by the OpenSSL Project
38  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39  *
40  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51  * OF THE POSSIBILITY OF SUCH DAMAGE.
52  * ====================================================================
53  */
54
55 #include <openssl/asn1t.h>
56 #include <openssl/x509v3.h>
57 #include <openssl/err.h>
58 #include <openssl/pem.h>
59 #include <openssl/bio.h>
60 #include <openssl/asn1.h>
61 #include <openssl/cms.h>
62 #include "cms_lcl.h"
63
64 IMPLEMENT_ASN1_FUNCTIONS(CMS_ContentInfo)
65 IMPLEMENT_ASN1_PRINT_FUNCTION(CMS_ContentInfo)
66
67 DECLARE_STACK_OF(CMS_CertificateChoices)
68 DECLARE_STACK_OF(CMS_RevocationInfoChoice)
69
70 const ASN1_OBJECT *CMS_get0_type(CMS_ContentInfo *cms)
71 {
72     return cms->contentType;
73 }
74
75 CMS_ContentInfo *cms_Data_create(void)
76 {
77     CMS_ContentInfo *cms;
78     cms = CMS_ContentInfo_new();
79     if (cms) {
80         cms->contentType = OBJ_nid2obj(NID_pkcs7_data);
81         /* Never detached */
82         CMS_set_detached(cms, 0);
83     }
84     return cms;
85 }
86
87 BIO *cms_content_bio(CMS_ContentInfo *cms)
88 {
89     ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
90     if (!pos)
91         return NULL;
92     /* If content detached data goes nowhere: create NULL BIO */
93     if (!*pos)
94         return BIO_new(BIO_s_null());
95     /*
96      * If content not detached and created return memory BIO
97      */
98     if (!*pos || ((*pos)->flags == ASN1_STRING_FLAG_CONT))
99         return BIO_new(BIO_s_mem());
100     /* Else content was read in: return read only BIO for it */
101     return BIO_new_mem_buf((*pos)->data, (*pos)->length);
102 }
103
104 BIO *CMS_dataInit(CMS_ContentInfo *cms, BIO *icont)
105 {
106     BIO *cmsbio, *cont;
107     if (icont)
108         cont = icont;
109     else
110         cont = cms_content_bio(cms);
111     if (!cont) {
112         CMSerr(CMS_F_CMS_DATAINIT, CMS_R_NO_CONTENT);
113         return NULL;
114     }
115     switch (OBJ_obj2nid(cms->contentType)) {
116
117     case NID_pkcs7_data:
118         return cont;
119
120     case NID_pkcs7_signed:
121         cmsbio = cms_SignedData_init_bio(cms);
122         break;
123
124     case NID_pkcs7_digest:
125         cmsbio = cms_DigestedData_init_bio(cms);
126         break;
127 #ifdef ZLIB
128     case NID_id_smime_ct_compressedData:
129         cmsbio = cms_CompressedData_init_bio(cms);
130         break;
131 #endif
132
133     case NID_pkcs7_encrypted:
134         cmsbio = cms_EncryptedData_init_bio(cms);
135         break;
136
137     case NID_pkcs7_enveloped:
138         cmsbio = cms_EnvelopedData_init_bio(cms);
139         break;
140
141     default:
142         CMSerr(CMS_F_CMS_DATAINIT, CMS_R_UNSUPPORTED_TYPE);
143         return NULL;
144     }
145
146     if (cmsbio)
147         return BIO_push(cmsbio, cont);
148
149     if (!icont)
150         BIO_free(cont);
151     return NULL;
152
153 }
154
155 int CMS_dataFinal(CMS_ContentInfo *cms, BIO *cmsbio)
156 {
157     ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
158     if (!pos)
159         return 0;
160     /* If ebmedded content find memory BIO and set content */
161     if (*pos && ((*pos)->flags & ASN1_STRING_FLAG_CONT)) {
162         BIO *mbio;
163         unsigned char *cont;
164         long contlen;
165         mbio = BIO_find_type(cmsbio, BIO_TYPE_MEM);
166         if (!mbio) {
167             CMSerr(CMS_F_CMS_DATAFINAL, CMS_R_CONTENT_NOT_FOUND);
168             return 0;
169         }
170         contlen = BIO_get_mem_data(mbio, &cont);
171         /* Set bio as read only so its content can't be clobbered */
172         BIO_set_flags(mbio, BIO_FLAGS_MEM_RDONLY);
173         BIO_set_mem_eof_return(mbio, 0);
174         ASN1_STRING_set0(*pos, cont, contlen);
175         (*pos)->flags &= ~ASN1_STRING_FLAG_CONT;
176     }
177
178     switch (OBJ_obj2nid(cms->contentType)) {
179
180     case NID_pkcs7_data:
181     case NID_pkcs7_enveloped:
182     case NID_pkcs7_encrypted:
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 /*
200  * Return an OCTET STRING pointer to content. This allows it to be accessed
201  * or set later.
202  */
203
204 ASN1_OCTET_STRING **CMS_get0_content(CMS_ContentInfo *cms)
205 {
206     switch (OBJ_obj2nid(cms->contentType)) {
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 /*
239  * Return an ASN1_OBJECT pointer to content type. This allows it to be
240  * 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     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, CMS_R_UNSUPPORTED_CONTENT_TYPE);
267         return NULL;
268
269     }
270 }
271
272 const ASN1_OBJECT *CMS_get0_eContentType(CMS_ContentInfo *cms)
273 {
274     ASN1_OBJECT **petype;
275     petype = cms_get0_econtent_type(cms);
276     if (petype)
277         return *petype;
278     return NULL;
279 }
280
281 int CMS_set1_eContentType(CMS_ContentInfo *cms, const ASN1_OBJECT *oid)
282 {
283     ASN1_OBJECT **petype, *etype;
284     petype = cms_get0_econtent_type(cms);
285     if (!petype)
286         return 0;
287     if (!oid)
288         return 1;
289     etype = OBJ_dup(oid);
290     if (!etype)
291         return 0;
292     ASN1_OBJECT_free(*petype);
293     *petype = etype;
294     return 1;
295 }
296
297 int CMS_is_detached(CMS_ContentInfo *cms)
298 {
299     ASN1_OCTET_STRING **pos;
300     pos = CMS_get0_content(cms);
301     if (!pos)
302         return -1;
303     if (*pos)
304         return 0;
305     return 1;
306 }
307
308 int CMS_set_detached(CMS_ContentInfo *cms, int detached)
309 {
310     ASN1_OCTET_STRING **pos;
311     pos = CMS_get0_content(cms);
312     if (!pos)
313         return 0;
314     if (detached) {
315         ASN1_OCTET_STRING_free(*pos);
316         *pos = NULL;
317         return 1;
318     }
319     if (!*pos)
320         *pos = ASN1_OCTET_STRING_new();
321     if (*pos) {
322         /*
323          * NB: special flag to show content is created and not read in.
324          */
325         (*pos)->flags |= ASN1_STRING_FLAG_CONT;
326         return 1;
327     }
328     CMSerr(CMS_F_CMS_SET_DETACHED, ERR_R_MALLOC_FAILURE);
329     return 0;
330 }
331
332 /* Create a digest BIO from an X509_ALGOR structure */
333
334 BIO *cms_DigestAlgorithm_init_bio(X509_ALGOR *digestAlgorithm)
335 {
336     BIO *mdbio = NULL;
337     ASN1_OBJECT *digestoid;
338     const EVP_MD *digest;
339     X509_ALGOR_get0(&digestoid, NULL, NULL, digestAlgorithm);
340     digest = EVP_get_digestbyobj(digestoid);
341     if (!digest) {
342         CMSerr(CMS_F_CMS_DIGESTALGORITHM_INIT_BIO,
343                CMS_R_UNKNOWN_DIGEST_ALGORIHM);
344         goto err;
345     }
346     mdbio = BIO_new(BIO_f_md());
347     if (!mdbio || !BIO_set_md(mdbio, digest)) {
348         CMSerr(CMS_F_CMS_DIGESTALGORITHM_INIT_BIO, CMS_R_MD_BIO_INIT_ERROR);
349         goto err;
350     }
351     return mdbio;
352  err:
353     BIO_free(mdbio);
354     return NULL;
355 }
356
357 /* Locate a message digest content from a BIO chain based on SignerInfo */
358
359 int cms_DigestAlgorithm_find_ctx(EVP_MD_CTX *mctx, BIO *chain,
360                                  X509_ALGOR *mdalg)
361 {
362     int nid;
363     ASN1_OBJECT *mdoid;
364     X509_ALGOR_get0(&mdoid, NULL, NULL, mdalg);
365     nid = OBJ_obj2nid(mdoid);
366     /* Look for digest type to match signature */
367     for (;;) {
368         EVP_MD_CTX *mtmp;
369         chain = BIO_find_type(chain, BIO_TYPE_MD);
370         if (chain == NULL) {
371             CMSerr(CMS_F_CMS_DIGESTALGORITHM_FIND_CTX,
372                    CMS_R_NO_MATCHING_DIGEST);
373             return 0;
374         }
375         BIO_get_md_ctx(chain, &mtmp);
376         if (EVP_MD_CTX_type(mtmp) == nid
377             /*
378              * Workaround for broken implementations that use signature
379              * algorithm OID instead of digest.
380              */
381             || EVP_MD_pkey_type(EVP_MD_CTX_md(mtmp)) == nid)
382             return EVP_MD_CTX_copy_ex(mctx, mtmp);
383         chain = BIO_next(chain);
384     }
385 }
386
387 static STACK_OF(CMS_CertificateChoices)
388 **cms_get0_certificate_choices(CMS_ContentInfo *cms)
389 {
390     switch (OBJ_obj2nid(cms->contentType)) {
391
392     case NID_pkcs7_signed:
393         return &cms->d.signedData->certificates;
394
395     case NID_pkcs7_enveloped:
396         return &cms->d.envelopedData->originatorInfo->certificates;
397
398     default:
399         CMSerr(CMS_F_CMS_GET0_CERTIFICATE_CHOICES,
400                CMS_R_UNSUPPORTED_CONTENT_TYPE);
401         return NULL;
402
403     }
404 }
405
406 CMS_CertificateChoices *CMS_add0_CertificateChoices(CMS_ContentInfo *cms)
407 {
408     STACK_OF(CMS_CertificateChoices) **pcerts;
409     CMS_CertificateChoices *cch;
410     pcerts = cms_get0_certificate_choices(cms);
411     if (!pcerts)
412         return NULL;
413     if (!*pcerts)
414         *pcerts = sk_CMS_CertificateChoices_new_null();
415     if (!*pcerts)
416         return NULL;
417     cch = M_ASN1_new_of(CMS_CertificateChoices);
418     if (!cch)
419         return NULL;
420     if (!sk_CMS_CertificateChoices_push(*pcerts, cch)) {
421         M_ASN1_free_of(cch, CMS_CertificateChoices);
422         return NULL;
423     }
424     return cch;
425 }
426
427 int CMS_add0_cert(CMS_ContentInfo *cms, X509 *cert)
428 {
429     CMS_CertificateChoices *cch;
430     STACK_OF(CMS_CertificateChoices) **pcerts;
431     int i;
432     pcerts = cms_get0_certificate_choices(cms);
433     if (!pcerts)
434         return 0;
435     for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++) {
436         cch = sk_CMS_CertificateChoices_value(*pcerts, i);
437         if (cch->type == CMS_CERTCHOICE_CERT) {
438             if (!X509_cmp(cch->d.certificate, cert)) {
439                 CMSerr(CMS_F_CMS_ADD0_CERT,
440                        CMS_R_CERTIFICATE_ALREADY_PRESENT);
441                 return 0;
442             }
443         }
444     }
445     cch = CMS_add0_CertificateChoices(cms);
446     if (!cch)
447         return 0;
448     cch->type = CMS_CERTCHOICE_CERT;
449     cch->d.certificate = cert;
450     return 1;
451 }
452
453 int CMS_add1_cert(CMS_ContentInfo *cms, X509 *cert)
454 {
455     int r;
456     r = CMS_add0_cert(cms, cert);
457     if (r > 0)
458         X509_up_ref(cert);
459     return r;
460 }
461
462 static STACK_OF(CMS_RevocationInfoChoice)
463 **cms_get0_revocation_choices(CMS_ContentInfo *cms)
464 {
465     switch (OBJ_obj2nid(cms->contentType)) {
466
467     case NID_pkcs7_signed:
468         return &cms->d.signedData->crls;
469
470     case NID_pkcs7_enveloped:
471         return &cms->d.envelopedData->originatorInfo->crls;
472
473     default:
474         CMSerr(CMS_F_CMS_GET0_REVOCATION_CHOICES,
475                CMS_R_UNSUPPORTED_CONTENT_TYPE);
476         return NULL;
477
478     }
479 }
480
481 CMS_RevocationInfoChoice *CMS_add0_RevocationInfoChoice(CMS_ContentInfo *cms)
482 {
483     STACK_OF(CMS_RevocationInfoChoice) **pcrls;
484     CMS_RevocationInfoChoice *rch;
485     pcrls = cms_get0_revocation_choices(cms);
486     if (!pcrls)
487         return NULL;
488     if (!*pcrls)
489         *pcrls = sk_CMS_RevocationInfoChoice_new_null();
490     if (!*pcrls)
491         return NULL;
492     rch = M_ASN1_new_of(CMS_RevocationInfoChoice);
493     if (!rch)
494         return NULL;
495     if (!sk_CMS_RevocationInfoChoice_push(*pcrls, rch)) {
496         M_ASN1_free_of(rch, CMS_RevocationInfoChoice);
497         return NULL;
498     }
499     return rch;
500 }
501
502 int CMS_add0_crl(CMS_ContentInfo *cms, X509_CRL *crl)
503 {
504     CMS_RevocationInfoChoice *rch;
505     rch = CMS_add0_RevocationInfoChoice(cms);
506     if (!rch)
507         return 0;
508     rch->type = CMS_REVCHOICE_CRL;
509     rch->d.crl = crl;
510     return 1;
511 }
512
513 int CMS_add1_crl(CMS_ContentInfo *cms, X509_CRL *crl)
514 {
515     int r;
516     r = CMS_add0_crl(cms, crl);
517     if (r > 0)
518         X509_CRL_up_ref(crl);
519     return r;
520 }
521
522 STACK_OF(X509) *CMS_get1_certs(CMS_ContentInfo *cms)
523 {
524     STACK_OF(X509) *certs = NULL;
525     CMS_CertificateChoices *cch;
526     STACK_OF(CMS_CertificateChoices) **pcerts;
527     int i;
528     pcerts = cms_get0_certificate_choices(cms);
529     if (!pcerts)
530         return NULL;
531     for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++) {
532         cch = sk_CMS_CertificateChoices_value(*pcerts, i);
533         if (cch->type == 0) {
534             if (!certs) {
535                 certs = sk_X509_new_null();
536                 if (!certs)
537                     return NULL;
538             }
539             if (!sk_X509_push(certs, cch->d.certificate)) {
540                 sk_X509_pop_free(certs, X509_free);
541                 return NULL;
542             }
543             X509_up_ref(cch->d.certificate);
544         }
545     }
546     return certs;
547
548 }
549
550 STACK_OF(X509_CRL) *CMS_get1_crls(CMS_ContentInfo *cms)
551 {
552     STACK_OF(X509_CRL) *crls = NULL;
553     STACK_OF(CMS_RevocationInfoChoice) **pcrls;
554     CMS_RevocationInfoChoice *rch;
555     int i;
556     pcrls = cms_get0_revocation_choices(cms);
557     if (!pcrls)
558         return NULL;
559     for (i = 0; i < sk_CMS_RevocationInfoChoice_num(*pcrls); i++) {
560         rch = sk_CMS_RevocationInfoChoice_value(*pcrls, i);
561         if (rch->type == 0) {
562             if (!crls) {
563                 crls = sk_X509_CRL_new_null();
564                 if (!crls)
565                     return NULL;
566             }
567             if (!sk_X509_CRL_push(crls, rch->d.crl)) {
568                 sk_X509_CRL_pop_free(crls, X509_CRL_free);
569                 return NULL;
570             }
571             X509_CRL_up_ref(rch->d.crl);
572         }
573     }
574     return crls;
575 }
576
577 int cms_ias_cert_cmp(CMS_IssuerAndSerialNumber *ias, X509 *cert)
578 {
579     int ret;
580     ret = X509_NAME_cmp(ias->issuer, X509_get_issuer_name(cert));
581     if (ret)
582         return ret;
583     return ASN1_INTEGER_cmp(ias->serialNumber, X509_get_serialNumber(cert));
584 }
585
586 int cms_keyid_cert_cmp(ASN1_OCTET_STRING *keyid, X509 *cert)
587 {
588     const ASN1_OCTET_STRING *cert_keyid = X509_get0_subject_key_id(cert);
589
590     if (cert_keyid == NULL)
591         return -1;
592     return ASN1_OCTET_STRING_cmp(keyid, cert_keyid);
593 }
594
595 int cms_set1_ias(CMS_IssuerAndSerialNumber **pias, X509 *cert)
596 {
597     CMS_IssuerAndSerialNumber *ias;
598     ias = M_ASN1_new_of(CMS_IssuerAndSerialNumber);
599     if (!ias)
600         goto err;
601     if (!X509_NAME_set(&ias->issuer, X509_get_issuer_name(cert)))
602         goto err;
603     if (!ASN1_STRING_copy(ias->serialNumber, X509_get_serialNumber(cert)))
604         goto err;
605     M_ASN1_free_of(*pias, CMS_IssuerAndSerialNumber);
606     *pias = ias;
607     return 1;
608  err:
609     M_ASN1_free_of(ias, CMS_IssuerAndSerialNumber);
610     CMSerr(CMS_F_CMS_SET1_IAS, ERR_R_MALLOC_FAILURE);
611     return 0;
612 }
613
614 int cms_set1_keyid(ASN1_OCTET_STRING **pkeyid, X509 *cert)
615 {
616     ASN1_OCTET_STRING *keyid = NULL;
617     const ASN1_OCTET_STRING *cert_keyid;
618     cert_keyid = X509_get0_subject_key_id(cert);
619     if (cert_keyid == NULL) {
620         CMSerr(CMS_F_CMS_SET1_KEYID, CMS_R_CERTIFICATE_HAS_NO_KEYID);
621         return 0;
622     }
623     keyid = ASN1_STRING_dup(cert_keyid);
624     if (!keyid) {
625         CMSerr(CMS_F_CMS_SET1_KEYID, ERR_R_MALLOC_FAILURE);
626         return 0;
627     }
628     ASN1_OCTET_STRING_free(*pkeyid);
629     *pkeyid = keyid;
630     return 1;
631 }