Run util/openssl-format-source -v -c .
[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_ASN1_ITEM(CMS_CertificateChoices)
68 DECLARE_ASN1_ITEM(CMS_RevocationInfoChoice)
69 DECLARE_STACK_OF(CMS_CertificateChoices)
70 DECLARE_STACK_OF(CMS_RevocationInfoChoice)
71
72 const ASN1_OBJECT *CMS_get0_type(CMS_ContentInfo *cms)
73 {
74     return cms->contentType;
75 }
76
77 CMS_ContentInfo *cms_Data_create(void)
78 {
79     CMS_ContentInfo *cms;
80     cms = CMS_ContentInfo_new();
81     if (cms) {
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     /*
98      * If content not detached and created return memory BIO
99      */
100     if (!*pos || ((*pos)->flags == ASN1_STRING_FLAG_CONT))
101         return BIO_new(BIO_s_mem());
102     /* Else content was read in: return read only BIO for it */
103     return BIO_new_mem_buf((*pos)->data, (*pos)->length);
104 }
105
106 BIO *CMS_dataInit(CMS_ContentInfo *cms, BIO *icont)
107 {
108     BIO *cmsbio, *cont;
109     if (icont)
110         cont = icont;
111     else
112         cont = cms_content_bio(cms);
113     if (!cont) {
114         CMSerr(CMS_F_CMS_DATAINIT, CMS_R_NO_CONTENT);
115         return NULL;
116     }
117     switch (OBJ_obj2nid(cms->contentType)) {
118
119     case NID_pkcs7_data:
120         return cont;
121
122     case NID_pkcs7_signed:
123         cmsbio = cms_SignedData_init_bio(cms);
124         break;
125
126     case NID_pkcs7_digest:
127         cmsbio = cms_DigestedData_init_bio(cms);
128         break;
129 #ifdef ZLIB
130     case NID_id_smime_ct_compressedData:
131         cmsbio = cms_CompressedData_init_bio(cms);
132         break;
133 #endif
134
135     case NID_pkcs7_encrypted:
136         cmsbio = cms_EncryptedData_init_bio(cms);
137         break;
138
139     case NID_pkcs7_enveloped:
140         cmsbio = cms_EnvelopedData_init_bio(cms);
141         break;
142
143     default:
144         CMSerr(CMS_F_CMS_DATAINIT, CMS_R_UNSUPPORTED_TYPE);
145         return NULL;
146     }
147
148     if (cmsbio)
149         return BIO_push(cmsbio, cont);
150
151     if (!icont)
152         BIO_free(cont);
153     return NULL;
154
155 }
156
157 int CMS_dataFinal(CMS_ContentInfo *cms, BIO *cmsbio)
158 {
159     ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
160     if (!pos)
161         return 0;
162     /* If ebmedded content find memory BIO and set content */
163     if (*pos && ((*pos)->flags & ASN1_STRING_FLAG_CONT)) {
164         BIO *mbio;
165         unsigned char *cont;
166         long contlen;
167         mbio = BIO_find_type(cmsbio, BIO_TYPE_MEM);
168         if (!mbio) {
169             CMSerr(CMS_F_CMS_DATAFINAL, CMS_R_CONTENT_NOT_FOUND);
170             return 0;
171         }
172         contlen = BIO_get_mem_data(mbio, &cont);
173         /* Set bio as read only so its content can't be clobbered */
174         BIO_set_flags(mbio, BIO_FLAGS_MEM_RDONLY);
175         BIO_set_mem_eof_return(mbio, 0);
176         ASN1_STRING_set0(*pos, cont, contlen);
177         (*pos)->flags &= ~ASN1_STRING_FLAG_CONT;
178     }
179
180     switch (OBJ_obj2nid(cms->contentType)) {
181
182     case NID_pkcs7_data:
183     case NID_pkcs7_enveloped:
184     case NID_pkcs7_encrypted:
185     case NID_id_smime_ct_compressedData:
186         /* Nothing to do */
187         return 1;
188
189     case NID_pkcs7_signed:
190         return cms_SignedData_final(cms, cmsbio);
191
192     case NID_pkcs7_digest:
193         return cms_DigestedData_do_final(cms, cmsbio, 0);
194
195     default:
196         CMSerr(CMS_F_CMS_DATAFINAL, CMS_R_UNSUPPORTED_TYPE);
197         return 0;
198     }
199 }
200
201 /*
202  * Return an OCTET STRING pointer to content. This allows it to be accessed
203  * or set later.
204  */
205
206 ASN1_OCTET_STRING **CMS_get0_content(CMS_ContentInfo *cms)
207 {
208     switch (OBJ_obj2nid(cms->contentType)) {
209
210     case NID_pkcs7_data:
211         return &cms->d.data;
212
213     case NID_pkcs7_signed:
214         return &cms->d.signedData->encapContentInfo->eContent;
215
216     case NID_pkcs7_enveloped:
217         return &cms->d.envelopedData->encryptedContentInfo->encryptedContent;
218
219     case NID_pkcs7_digest:
220         return &cms->d.digestedData->encapContentInfo->eContent;
221
222     case NID_pkcs7_encrypted:
223         return &cms->d.encryptedData->encryptedContentInfo->encryptedContent;
224
225     case NID_id_smime_ct_authData:
226         return &cms->d.authenticatedData->encapContentInfo->eContent;
227
228     case NID_id_smime_ct_compressedData:
229         return &cms->d.compressedData->encapContentInfo->eContent;
230
231     default:
232         if (cms->d.other->type == V_ASN1_OCTET_STRING)
233             return &cms->d.other->value.octet_string;
234         CMSerr(CMS_F_CMS_GET0_CONTENT, CMS_R_UNSUPPORTED_CONTENT_TYPE);
235         return NULL;
236
237     }
238 }
239
240 /*
241  * Return an ASN1_OBJECT pointer to content type. This allows it to be
242  * accessed or set later.
243  */
244
245 static ASN1_OBJECT **cms_get0_econtent_type(CMS_ContentInfo *cms)
246 {
247     switch (OBJ_obj2nid(cms->contentType)) {
248
249     case NID_pkcs7_signed:
250         return &cms->d.signedData->encapContentInfo->eContentType;
251
252     case NID_pkcs7_enveloped:
253         return &cms->d.envelopedData->encryptedContentInfo->contentType;
254
255     case NID_pkcs7_digest:
256         return &cms->d.digestedData->encapContentInfo->eContentType;
257
258     case NID_pkcs7_encrypted:
259         return &cms->d.encryptedData->encryptedContentInfo->contentType;
260
261     case NID_id_smime_ct_authData:
262         return &cms->d.authenticatedData->encapContentInfo->eContentType;
263
264     case NID_id_smime_ct_compressedData:
265         return &cms->d.compressedData->encapContentInfo->eContentType;
266
267     default:
268         CMSerr(CMS_F_CMS_GET0_ECONTENT_TYPE, 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         if (*pos) {
318             ASN1_OCTET_STRING_free(*pos);
319             *pos = NULL;
320         }
321         return 1;
322     }
323     if (!*pos)
324         *pos = ASN1_OCTET_STRING_new();
325     if (*pos) {
326         /*
327          * NB: special flag to show content is created and not read in.
328          */
329         (*pos)->flags |= ASN1_STRING_FLAG_CONT;
330         return 1;
331     }
332     CMSerr(CMS_F_CMS_SET_DETACHED, ERR_R_MALLOC_FAILURE);
333     return 0;
334 }
335
336 /* Create a digest BIO from an X509_ALGOR structure */
337
338 BIO *cms_DigestAlgorithm_init_bio(X509_ALGOR *digestAlgorithm)
339 {
340     BIO *mdbio = NULL;
341     ASN1_OBJECT *digestoid;
342     const EVP_MD *digest;
343     X509_ALGOR_get0(&digestoid, NULL, NULL, digestAlgorithm);
344     digest = EVP_get_digestbyobj(digestoid);
345     if (!digest) {
346         CMSerr(CMS_F_CMS_DIGESTALGORITHM_INIT_BIO,
347                CMS_R_UNKNOWN_DIGEST_ALGORIHM);
348         goto err;
349     }
350     mdbio = BIO_new(BIO_f_md());
351     if (!mdbio || !BIO_set_md(mdbio, digest)) {
352         CMSerr(CMS_F_CMS_DIGESTALGORITHM_INIT_BIO, CMS_R_MD_BIO_INIT_ERROR);
353         goto err;
354     }
355     return mdbio;
356  err:
357     if (mdbio)
358         BIO_free(mdbio);
359     return NULL;
360 }
361
362 /* Locate a message digest content from a BIO chain based on SignerInfo */
363
364 int cms_DigestAlgorithm_find_ctx(EVP_MD_CTX *mctx, BIO *chain,
365                                  X509_ALGOR *mdalg)
366 {
367     int nid;
368     ASN1_OBJECT *mdoid;
369     X509_ALGOR_get0(&mdoid, NULL, NULL, mdalg);
370     nid = OBJ_obj2nid(mdoid);
371     /* Look for digest type to match signature */
372     for (;;) {
373         EVP_MD_CTX *mtmp;
374         chain = BIO_find_type(chain, BIO_TYPE_MD);
375         if (chain == NULL) {
376             CMSerr(CMS_F_CMS_DIGESTALGORITHM_FIND_CTX,
377                    CMS_R_NO_MATCHING_DIGEST);
378             return 0;
379         }
380         BIO_get_md_ctx(chain, &mtmp);
381         if (EVP_MD_CTX_type(mtmp) == nid
382             /*
383              * Workaround for broken implementations that use signature
384              * algorithm OID instead of digest.
385              */
386             || EVP_MD_pkey_type(EVP_MD_CTX_md(mtmp)) == nid)
387             return EVP_MD_CTX_copy_ex(mctx, mtmp);
388         chain = BIO_next(chain);
389     }
390 }
391
392 static STACK_OF(CMS_CertificateChoices)
393 **cms_get0_certificate_choices(CMS_ContentInfo *cms)
394 {
395     switch (OBJ_obj2nid(cms->contentType)) {
396
397     case NID_pkcs7_signed:
398         return &cms->d.signedData->certificates;
399
400     case NID_pkcs7_enveloped:
401         return &cms->d.envelopedData->originatorInfo->certificates;
402
403     default:
404         CMSerr(CMS_F_CMS_GET0_CERTIFICATE_CHOICES,
405                CMS_R_UNSUPPORTED_CONTENT_TYPE);
406         return NULL;
407
408     }
409 }
410
411 CMS_CertificateChoices *CMS_add0_CertificateChoices(CMS_ContentInfo *cms)
412 {
413     STACK_OF(CMS_CertificateChoices) **pcerts;
414     CMS_CertificateChoices *cch;
415     pcerts = cms_get0_certificate_choices(cms);
416     if (!pcerts)
417         return NULL;
418     if (!*pcerts)
419         *pcerts = sk_CMS_CertificateChoices_new_null();
420     if (!*pcerts)
421         return NULL;
422     cch = M_ASN1_new_of(CMS_CertificateChoices);
423     if (!cch)
424         return NULL;
425     if (!sk_CMS_CertificateChoices_push(*pcerts, cch)) {
426         M_ASN1_free_of(cch, CMS_CertificateChoices);
427         return NULL;
428     }
429     return cch;
430 }
431
432 int CMS_add0_cert(CMS_ContentInfo *cms, X509 *cert)
433 {
434     CMS_CertificateChoices *cch;
435     STACK_OF(CMS_CertificateChoices) **pcerts;
436     int i;
437     pcerts = cms_get0_certificate_choices(cms);
438     if (!pcerts)
439         return 0;
440     for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++) {
441         cch = sk_CMS_CertificateChoices_value(*pcerts, i);
442         if (cch->type == CMS_CERTCHOICE_CERT) {
443             if (!X509_cmp(cch->d.certificate, cert)) {
444                 CMSerr(CMS_F_CMS_ADD0_CERT,
445                        CMS_R_CERTIFICATE_ALREADY_PRESENT);
446                 return 0;
447             }
448         }
449     }
450     cch = CMS_add0_CertificateChoices(cms);
451     if (!cch)
452         return 0;
453     cch->type = CMS_CERTCHOICE_CERT;
454     cch->d.certificate = cert;
455     return 1;
456 }
457
458 int CMS_add1_cert(CMS_ContentInfo *cms, X509 *cert)
459 {
460     int r;
461     r = CMS_add0_cert(cms, cert);
462     if (r > 0)
463         CRYPTO_add(&cert->references, 1, CRYPTO_LOCK_X509);
464     return r;
465 }
466
467 static STACK_OF(CMS_RevocationInfoChoice)
468 **cms_get0_revocation_choices(CMS_ContentInfo *cms)
469 {
470     switch (OBJ_obj2nid(cms->contentType)) {
471
472     case NID_pkcs7_signed:
473         return &cms->d.signedData->crls;
474
475     case NID_pkcs7_enveloped:
476         return &cms->d.envelopedData->originatorInfo->crls;
477
478     default:
479         CMSerr(CMS_F_CMS_GET0_REVOCATION_CHOICES,
480                CMS_R_UNSUPPORTED_CONTENT_TYPE);
481         return NULL;
482
483     }
484 }
485
486 CMS_RevocationInfoChoice *CMS_add0_RevocationInfoChoice(CMS_ContentInfo *cms)
487 {
488     STACK_OF(CMS_RevocationInfoChoice) **pcrls;
489     CMS_RevocationInfoChoice *rch;
490     pcrls = cms_get0_revocation_choices(cms);
491     if (!pcrls)
492         return NULL;
493     if (!*pcrls)
494         *pcrls = sk_CMS_RevocationInfoChoice_new_null();
495     if (!*pcrls)
496         return NULL;
497     rch = M_ASN1_new_of(CMS_RevocationInfoChoice);
498     if (!rch)
499         return NULL;
500     if (!sk_CMS_RevocationInfoChoice_push(*pcrls, rch)) {
501         M_ASN1_free_of(rch, CMS_RevocationInfoChoice);
502         return NULL;
503     }
504     return rch;
505 }
506
507 int CMS_add0_crl(CMS_ContentInfo *cms, X509_CRL *crl)
508 {
509     CMS_RevocationInfoChoice *rch;
510     rch = CMS_add0_RevocationInfoChoice(cms);
511     if (!rch)
512         return 0;
513     rch->type = CMS_REVCHOICE_CRL;
514     rch->d.crl = crl;
515     return 1;
516 }
517
518 int CMS_add1_crl(CMS_ContentInfo *cms, X509_CRL *crl)
519 {
520     int r;
521     r = CMS_add0_crl(cms, crl);
522     if (r > 0)
523         CRYPTO_add(&crl->references, 1, CRYPTO_LOCK_X509_CRL);
524     return r;
525 }
526
527 STACK_OF(X509) *CMS_get1_certs(CMS_ContentInfo *cms)
528 {
529     STACK_OF(X509) *certs = NULL;
530     CMS_CertificateChoices *cch;
531     STACK_OF(CMS_CertificateChoices) **pcerts;
532     int i;
533     pcerts = cms_get0_certificate_choices(cms);
534     if (!pcerts)
535         return NULL;
536     for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++) {
537         cch = sk_CMS_CertificateChoices_value(*pcerts, i);
538         if (cch->type == 0) {
539             if (!certs) {
540                 certs = sk_X509_new_null();
541                 if (!certs)
542                     return NULL;
543             }
544             if (!sk_X509_push(certs, cch->d.certificate)) {
545                 sk_X509_pop_free(certs, X509_free);
546                 return NULL;
547             }
548             CRYPTO_add(&cch->d.certificate->references, 1, CRYPTO_LOCK_X509);
549         }
550     }
551     return certs;
552
553 }
554
555 STACK_OF(X509_CRL) *CMS_get1_crls(CMS_ContentInfo *cms)
556 {
557     STACK_OF(X509_CRL) *crls = NULL;
558     STACK_OF(CMS_RevocationInfoChoice) **pcrls;
559     CMS_RevocationInfoChoice *rch;
560     int i;
561     pcrls = cms_get0_revocation_choices(cms);
562     if (!pcrls)
563         return NULL;
564     for (i = 0; i < sk_CMS_RevocationInfoChoice_num(*pcrls); i++) {
565         rch = sk_CMS_RevocationInfoChoice_value(*pcrls, i);
566         if (rch->type == 0) {
567             if (!crls) {
568                 crls = sk_X509_CRL_new_null();
569                 if (!crls)
570                     return NULL;
571             }
572             if (!sk_X509_CRL_push(crls, rch->d.crl)) {
573                 sk_X509_CRL_pop_free(crls, X509_CRL_free);
574                 return NULL;
575             }
576             CRYPTO_add(&rch->d.crl->references, 1, CRYPTO_LOCK_X509_CRL);
577         }
578     }
579     return crls;
580 }
581
582 int cms_ias_cert_cmp(CMS_IssuerAndSerialNumber *ias, X509 *cert)
583 {
584     int ret;
585     ret = X509_NAME_cmp(ias->issuer, X509_get_issuer_name(cert));
586     if (ret)
587         return ret;
588     return ASN1_INTEGER_cmp(ias->serialNumber, X509_get_serialNumber(cert));
589 }
590
591 int cms_keyid_cert_cmp(ASN1_OCTET_STRING *keyid, X509 *cert)
592 {
593     X509_check_purpose(cert, -1, -1);
594     if (!cert->skid)
595         return -1;
596     return ASN1_OCTET_STRING_cmp(keyid, cert->skid);
597 }
598
599 int cms_set1_ias(CMS_IssuerAndSerialNumber **pias, X509 *cert)
600 {
601     CMS_IssuerAndSerialNumber *ias;
602     ias = M_ASN1_new_of(CMS_IssuerAndSerialNumber);
603     if (!ias)
604         goto err;
605     if (!X509_NAME_set(&ias->issuer, X509_get_issuer_name(cert)))
606         goto err;
607     if (!ASN1_STRING_copy(ias->serialNumber, X509_get_serialNumber(cert)))
608         goto err;
609     if (*pias)
610         M_ASN1_free_of(*pias, CMS_IssuerAndSerialNumber);
611     *pias = ias;
612     return 1;
613  err:
614     if (ias)
615         M_ASN1_free_of(ias, CMS_IssuerAndSerialNumber);
616     CMSerr(CMS_F_CMS_SET1_IAS, ERR_R_MALLOC_FAILURE);
617     return 0;
618 }
619
620 int cms_set1_keyid(ASN1_OCTET_STRING **pkeyid, X509 *cert)
621 {
622     ASN1_OCTET_STRING *keyid = NULL;
623     X509_check_purpose(cert, -1, -1);
624     if (!cert->skid) {
625         CMSerr(CMS_F_CMS_SET1_KEYID, CMS_R_CERTIFICATE_HAS_NO_KEYID);
626         return 0;
627     }
628     keyid = ASN1_STRING_dup(cert->skid);
629     if (!keyid) {
630         CMSerr(CMS_F_CMS_SET1_KEYID, ERR_R_MALLOC_FAILURE);
631         return 0;
632     }
633     if (*pkeyid)
634         ASN1_OCTET_STRING_free(*pkeyid);
635     *pkeyid = keyid;
636     return 1;
637 }