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