Update copyright year
[openssl.git] / crypto / cms / cms_lib.c
1 /*
2  * Copyright 2008-2022 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <openssl/asn1t.h>
11 #include <openssl/x509v3.h>
12 #include <openssl/err.h>
13 #include <openssl/pem.h>
14 #include <openssl/bio.h>
15 #include <openssl/asn1.h>
16 #include <openssl/cms.h>
17 #include <openssl/cms.h>
18 #include "internal/sizes.h"
19 #include "crypto/x509.h"
20 #include "cms_local.h"
21
22 static STACK_OF(CMS_CertificateChoices)
23 **cms_get0_certificate_choices(CMS_ContentInfo *cms);
24
25 IMPLEMENT_ASN1_PRINT_FUNCTION(CMS_ContentInfo)
26
27 CMS_ContentInfo *d2i_CMS_ContentInfo(CMS_ContentInfo **a,
28                                      const unsigned char **in, long len)
29 {
30     CMS_ContentInfo *ci;
31     const CMS_CTX *ctx = ossl_cms_get0_cmsctx(a == NULL ? NULL : *a);
32
33     ci = (CMS_ContentInfo *)ASN1_item_d2i_ex((ASN1_VALUE **)a, in, len,
34                                           (CMS_ContentInfo_it()),
35                                           ossl_cms_ctx_get0_libctx(ctx),
36                                           ossl_cms_ctx_get0_propq(ctx));
37     if (ci != NULL)
38         ossl_cms_resolve_libctx(ci);
39     return ci;
40 }
41
42 int i2d_CMS_ContentInfo(const CMS_ContentInfo *a, unsigned char **out)
43 {
44     return ASN1_item_i2d((const ASN1_VALUE *)a, out, (CMS_ContentInfo_it()));
45 }
46
47 CMS_ContentInfo *CMS_ContentInfo_new_ex(OSSL_LIB_CTX *libctx, const char *propq)
48 {
49     CMS_ContentInfo *ci;
50
51     ci = (CMS_ContentInfo *)ASN1_item_new_ex(ASN1_ITEM_rptr(CMS_ContentInfo),
52                                              libctx, propq);
53     if (ci != NULL) {
54         ci->ctx.libctx = libctx;
55         ci->ctx.propq = NULL;
56         if (propq != NULL) {
57             ci->ctx.propq = OPENSSL_strdup(propq);
58             if (ci->ctx.propq == NULL) {
59                 CMS_ContentInfo_free(ci);
60                 ci = NULL;
61                 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
62             }
63         }
64     }
65     return ci;
66 }
67
68 CMS_ContentInfo *CMS_ContentInfo_new(void)
69 {
70     return CMS_ContentInfo_new_ex(NULL, NULL);
71 }
72
73 void CMS_ContentInfo_free(CMS_ContentInfo *cms)
74 {
75     if (cms != NULL) {
76         OPENSSL_free(cms->ctx.propq);
77         ASN1_item_free((ASN1_VALUE *)cms, ASN1_ITEM_rptr(CMS_ContentInfo));
78     }
79 }
80
81 const CMS_CTX *ossl_cms_get0_cmsctx(const CMS_ContentInfo *cms)
82 {
83     return cms != NULL ? &cms->ctx : NULL;
84 }
85
86 OSSL_LIB_CTX *ossl_cms_ctx_get0_libctx(const CMS_CTX *ctx)
87 {
88     return ctx != NULL ? ctx->libctx : NULL;
89 }
90
91 const char *ossl_cms_ctx_get0_propq(const CMS_CTX *ctx)
92 {
93     return ctx != NULL ? ctx->propq : NULL;
94 }
95
96 void ossl_cms_resolve_libctx(CMS_ContentInfo *ci)
97 {
98     int i;
99     CMS_CertificateChoices *cch;
100     STACK_OF(CMS_CertificateChoices) **pcerts;
101     const CMS_CTX *ctx = ossl_cms_get0_cmsctx(ci);
102     OSSL_LIB_CTX *libctx = ossl_cms_ctx_get0_libctx(ctx);
103     const char *propq = ossl_cms_ctx_get0_propq(ctx);
104
105     ossl_cms_SignerInfos_set_cmsctx(ci);
106     ossl_cms_RecipientInfos_set_cmsctx(ci);
107
108     pcerts = cms_get0_certificate_choices(ci);
109     if (pcerts != NULL) {
110         for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++) {
111             cch = sk_CMS_CertificateChoices_value(*pcerts, i);
112             if (cch->type == CMS_CERTCHOICE_CERT)
113                 ossl_x509_set0_libctx(cch->d.certificate, libctx, propq);
114         }
115     }
116 }
117
118 const ASN1_OBJECT *CMS_get0_type(const CMS_ContentInfo *cms)
119 {
120     return cms->contentType;
121 }
122
123 CMS_ContentInfo *ossl_cms_Data_create(OSSL_LIB_CTX *libctx, const char *propq)
124 {
125     CMS_ContentInfo *cms = CMS_ContentInfo_new_ex(libctx, propq);
126
127     if (cms != NULL) {
128         cms->contentType = OBJ_nid2obj(NID_pkcs7_data);
129         /* Never detached */
130         CMS_set_detached(cms, 0);
131     }
132     return cms;
133 }
134
135 BIO *ossl_cms_content_bio(CMS_ContentInfo *cms)
136 {
137     ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
138
139     if (pos == NULL)
140         return NULL;
141     /* If content detached data goes nowhere: create NULL BIO */
142     if (*pos == NULL)
143         return BIO_new(BIO_s_null());
144     /*
145      * If content not detached and created return memory BIO
146      */
147     if (*pos == NULL || ((*pos)->flags == ASN1_STRING_FLAG_CONT))
148         return BIO_new(BIO_s_mem());
149     /* Else content was read in: return read only BIO for it */
150     return BIO_new_mem_buf((*pos)->data, (*pos)->length);
151 }
152
153 BIO *CMS_dataInit(CMS_ContentInfo *cms, BIO *icont)
154 {
155     BIO *cmsbio, *cont;
156     if (icont)
157         cont = icont;
158     else
159         cont = ossl_cms_content_bio(cms);
160     if (!cont) {
161         ERR_raise(ERR_LIB_CMS, CMS_R_NO_CONTENT);
162         return NULL;
163     }
164     switch (OBJ_obj2nid(cms->contentType)) {
165
166     case NID_pkcs7_data:
167         return cont;
168
169     case NID_pkcs7_signed:
170         cmsbio = ossl_cms_SignedData_init_bio(cms);
171         break;
172
173     case NID_pkcs7_digest:
174         cmsbio = ossl_cms_DigestedData_init_bio(cms);
175         break;
176 #ifdef ZLIB
177     case NID_id_smime_ct_compressedData:
178         cmsbio = ossl_cms_CompressedData_init_bio(cms);
179         break;
180 #endif
181
182     case NID_pkcs7_encrypted:
183         cmsbio = ossl_cms_EncryptedData_init_bio(cms);
184         break;
185
186     case NID_pkcs7_enveloped:
187         cmsbio = ossl_cms_EnvelopedData_init_bio(cms);
188         break;
189
190     case NID_id_smime_ct_authEnvelopedData:
191         cmsbio = ossl_cms_AuthEnvelopedData_init_bio(cms);
192         break;
193
194     default:
195         ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_TYPE);
196         goto err;
197     }
198
199     if (cmsbio)
200         return BIO_push(cmsbio, cont);
201 err:
202     if (!icont)
203         BIO_free(cont);
204     return NULL;
205
206 }
207
208 /* unfortunately cannot constify SMIME_write_ASN1() due to this function */
209 int CMS_dataFinal(CMS_ContentInfo *cms, BIO *cmsbio)
210 {
211     return ossl_cms_DataFinal(cms, cmsbio, NULL, 0);
212 }
213
214 int ossl_cms_DataFinal(CMS_ContentInfo *cms, BIO *cmsbio,
215                        const unsigned char *precomp_md,
216                        unsigned int precomp_mdlen)
217 {
218     ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
219
220     if (pos == NULL)
221         return 0;
222     /* If embedded content find memory BIO and set content */
223     if (*pos && ((*pos)->flags & ASN1_STRING_FLAG_CONT)) {
224         BIO *mbio;
225         unsigned char *cont;
226         long contlen;
227         mbio = BIO_find_type(cmsbio, BIO_TYPE_MEM);
228         if (!mbio) {
229             ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_NOT_FOUND);
230             return 0;
231         }
232         contlen = BIO_get_mem_data(mbio, &cont);
233         /* Set bio as read only so its content can't be clobbered */
234         BIO_set_flags(mbio, BIO_FLAGS_MEM_RDONLY);
235         BIO_set_mem_eof_return(mbio, 0);
236         ASN1_STRING_set0(*pos, cont, contlen);
237         (*pos)->flags &= ~ASN1_STRING_FLAG_CONT;
238     }
239
240     switch (OBJ_obj2nid(cms->contentType)) {
241
242     case NID_pkcs7_data:
243     case NID_pkcs7_encrypted:
244     case NID_id_smime_ct_compressedData:
245         /* Nothing to do */
246         return 1;
247
248     case NID_pkcs7_enveloped:
249         return ossl_cms_EnvelopedData_final(cms, cmsbio);
250
251     case NID_id_smime_ct_authEnvelopedData:
252         return ossl_cms_AuthEnvelopedData_final(cms, cmsbio);
253
254     case NID_pkcs7_signed:
255         return ossl_cms_SignedData_final(cms, cmsbio, precomp_md, precomp_mdlen);
256
257     case NID_pkcs7_digest:
258         return ossl_cms_DigestedData_do_final(cms, cmsbio, 0);
259
260     default:
261         ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_TYPE);
262         return 0;
263     }
264 }
265
266 /*
267  * Return an OCTET STRING pointer to content. This allows it to be accessed
268  * or set later.
269  */
270
271 ASN1_OCTET_STRING **CMS_get0_content(CMS_ContentInfo *cms)
272 {
273     switch (OBJ_obj2nid(cms->contentType)) {
274
275     case NID_pkcs7_data:
276         return &cms->d.data;
277
278     case NID_pkcs7_signed:
279         return &cms->d.signedData->encapContentInfo->eContent;
280
281     case NID_pkcs7_enveloped:
282         return &cms->d.envelopedData->encryptedContentInfo->encryptedContent;
283
284     case NID_pkcs7_digest:
285         return &cms->d.digestedData->encapContentInfo->eContent;
286
287     case NID_pkcs7_encrypted:
288         return &cms->d.encryptedData->encryptedContentInfo->encryptedContent;
289
290     case NID_id_smime_ct_authEnvelopedData:
291         return &cms->d.authEnvelopedData->authEncryptedContentInfo
292                                         ->encryptedContent;
293
294     case NID_id_smime_ct_authData:
295         return &cms->d.authenticatedData->encapContentInfo->eContent;
296
297     case NID_id_smime_ct_compressedData:
298         return &cms->d.compressedData->encapContentInfo->eContent;
299
300     default:
301         if (cms->d.other->type == V_ASN1_OCTET_STRING)
302             return &cms->d.other->value.octet_string;
303         ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_CONTENT_TYPE);
304         return NULL;
305
306     }
307 }
308
309 /*
310  * Return an ASN1_OBJECT pointer to content type. This allows it to be
311  * accessed or set later.
312  */
313
314 static ASN1_OBJECT **cms_get0_econtent_type(CMS_ContentInfo *cms)
315 {
316     switch (OBJ_obj2nid(cms->contentType)) {
317
318     case NID_pkcs7_signed:
319         return &cms->d.signedData->encapContentInfo->eContentType;
320
321     case NID_pkcs7_enveloped:
322         return &cms->d.envelopedData->encryptedContentInfo->contentType;
323
324     case NID_pkcs7_digest:
325         return &cms->d.digestedData->encapContentInfo->eContentType;
326
327     case NID_pkcs7_encrypted:
328         return &cms->d.encryptedData->encryptedContentInfo->contentType;
329
330     case NID_id_smime_ct_authEnvelopedData:
331         return &cms->d.authEnvelopedData->authEncryptedContentInfo
332                                         ->contentType;
333     case NID_id_smime_ct_authData:
334         return &cms->d.authenticatedData->encapContentInfo->eContentType;
335
336     case NID_id_smime_ct_compressedData:
337         return &cms->d.compressedData->encapContentInfo->eContentType;
338
339     default:
340         ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_CONTENT_TYPE);
341         return NULL;
342
343     }
344 }
345
346 const ASN1_OBJECT *CMS_get0_eContentType(CMS_ContentInfo *cms)
347 {
348     ASN1_OBJECT **petype;
349     petype = cms_get0_econtent_type(cms);
350     if (petype)
351         return *petype;
352     return NULL;
353 }
354
355 int CMS_set1_eContentType(CMS_ContentInfo *cms, const ASN1_OBJECT *oid)
356 {
357     ASN1_OBJECT **petype, *etype;
358
359     petype = cms_get0_econtent_type(cms);
360     if (petype == NULL)
361         return 0;
362     if (oid == NULL)
363         return 1;
364     etype = OBJ_dup(oid);
365     if (etype == NULL)
366         return 0;
367     ASN1_OBJECT_free(*petype);
368     *petype = etype;
369     return 1;
370 }
371
372 int CMS_is_detached(CMS_ContentInfo *cms)
373 {
374     ASN1_OCTET_STRING **pos;
375
376     pos = CMS_get0_content(cms);
377     if (pos == NULL)
378         return -1;
379     if (*pos != NULL)
380         return 0;
381     return 1;
382 }
383
384 int CMS_set_detached(CMS_ContentInfo *cms, int detached)
385 {
386     ASN1_OCTET_STRING **pos;
387
388     pos = CMS_get0_content(cms);
389     if (pos == NULL)
390         return 0;
391     if (detached) {
392         ASN1_OCTET_STRING_free(*pos);
393         *pos = NULL;
394         return 1;
395     }
396     if (*pos == NULL)
397         *pos = ASN1_OCTET_STRING_new();
398     if (*pos != NULL) {
399         /*
400          * NB: special flag to show content is created and not read in.
401          */
402         (*pos)->flags |= ASN1_STRING_FLAG_CONT;
403         return 1;
404     }
405     ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
406     return 0;
407 }
408
409 /* Create a digest BIO from an X509_ALGOR structure */
410
411 BIO *ossl_cms_DigestAlgorithm_init_bio(X509_ALGOR *digestAlgorithm,
412                                        const CMS_CTX *ctx)
413 {
414     BIO *mdbio = NULL;
415     const ASN1_OBJECT *digestoid;
416     const EVP_MD *digest = NULL;
417     EVP_MD *fetched_digest = NULL;
418     char alg[OSSL_MAX_NAME_SIZE];
419
420     X509_ALGOR_get0(&digestoid, NULL, NULL, digestAlgorithm);
421     OBJ_obj2txt(alg, sizeof(alg), digestoid, 0);
422
423     (void)ERR_set_mark();
424     fetched_digest = EVP_MD_fetch(ossl_cms_ctx_get0_libctx(ctx), alg,
425                                   ossl_cms_ctx_get0_propq(ctx));
426
427     if (fetched_digest != NULL)
428         digest = fetched_digest;
429     else
430         digest = EVP_get_digestbyobj(digestoid);
431     if (digest == NULL) {
432         (void)ERR_clear_last_mark();
433         ERR_raise(ERR_LIB_CMS, CMS_R_UNKNOWN_DIGEST_ALGORITHM);
434         goto err;
435     }
436     (void)ERR_pop_to_mark();
437
438     mdbio = BIO_new(BIO_f_md());
439     if (mdbio == NULL || !BIO_set_md(mdbio, digest)) {
440         ERR_raise(ERR_LIB_CMS, CMS_R_MD_BIO_INIT_ERROR);
441         goto err;
442     }
443     EVP_MD_free(fetched_digest);
444     return mdbio;
445  err:
446     EVP_MD_free(fetched_digest);
447     BIO_free(mdbio);
448     return NULL;
449 }
450
451 /* Locate a message digest content from a BIO chain based on SignerInfo */
452
453 int ossl_cms_DigestAlgorithm_find_ctx(EVP_MD_CTX *mctx, BIO *chain,
454                                       X509_ALGOR *mdalg)
455 {
456     int nid;
457     const ASN1_OBJECT *mdoid;
458     X509_ALGOR_get0(&mdoid, NULL, NULL, mdalg);
459     nid = OBJ_obj2nid(mdoid);
460     /* Look for digest type to match signature */
461     for (;;) {
462         EVP_MD_CTX *mtmp;
463         chain = BIO_find_type(chain, BIO_TYPE_MD);
464         if (chain == NULL) {
465             ERR_raise(ERR_LIB_CMS, CMS_R_NO_MATCHING_DIGEST);
466             return 0;
467         }
468         BIO_get_md_ctx(chain, &mtmp);
469         if (EVP_MD_CTX_get_type(mtmp) == nid
470             /*
471              * Workaround for broken implementations that use signature
472              * algorithm OID instead of digest.
473              */
474             || EVP_MD_get_pkey_type(EVP_MD_CTX_get0_md(mtmp)) == nid)
475             return EVP_MD_CTX_copy_ex(mctx, mtmp);
476         chain = BIO_next(chain);
477     }
478 }
479
480 static STACK_OF(CMS_CertificateChoices)
481 **cms_get0_certificate_choices(CMS_ContentInfo *cms)
482 {
483     switch (OBJ_obj2nid(cms->contentType)) {
484
485     case NID_pkcs7_signed:
486         return &cms->d.signedData->certificates;
487
488     case NID_pkcs7_enveloped:
489         if (cms->d.envelopedData->originatorInfo == NULL)
490             return NULL;
491         return &cms->d.envelopedData->originatorInfo->certificates;
492
493     case NID_id_smime_ct_authEnvelopedData:
494         if (cms->d.authEnvelopedData->originatorInfo == NULL)
495             return NULL;
496         return &cms->d.authEnvelopedData->originatorInfo->certificates;
497
498     default:
499         ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_CONTENT_TYPE);
500         return NULL;
501
502     }
503 }
504
505 CMS_CertificateChoices *CMS_add0_CertificateChoices(CMS_ContentInfo *cms)
506 {
507     STACK_OF(CMS_CertificateChoices) **pcerts;
508     CMS_CertificateChoices *cch;
509
510     pcerts = cms_get0_certificate_choices(cms);
511     if (pcerts == NULL)
512         return NULL;
513     if (*pcerts == NULL)
514         *pcerts = sk_CMS_CertificateChoices_new_null();
515     if (*pcerts == NULL)
516         return NULL;
517     cch = M_ASN1_new_of(CMS_CertificateChoices);
518     if (!cch)
519         return NULL;
520     if (!sk_CMS_CertificateChoices_push(*pcerts, cch)) {
521         M_ASN1_free_of(cch, CMS_CertificateChoices);
522         return NULL;
523     }
524     return cch;
525 }
526
527 int CMS_add0_cert(CMS_ContentInfo *cms, X509 *cert)
528 {
529     CMS_CertificateChoices *cch;
530     STACK_OF(CMS_CertificateChoices) **pcerts;
531     int i;
532
533     pcerts = cms_get0_certificate_choices(cms);
534     if (pcerts == NULL)
535         return 0;
536     for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++) {
537         cch = sk_CMS_CertificateChoices_value(*pcerts, i);
538         if (cch->type == CMS_CERTCHOICE_CERT) {
539             if (!X509_cmp(cch->d.certificate, cert)) {
540                 ERR_raise(ERR_LIB_CMS, CMS_R_CERTIFICATE_ALREADY_PRESENT);
541                 return 0;
542             }
543         }
544     }
545     cch = CMS_add0_CertificateChoices(cms);
546     if (!cch)
547         return 0;
548     cch->type = CMS_CERTCHOICE_CERT;
549     cch->d.certificate = cert;
550     return 1;
551 }
552
553 int CMS_add1_cert(CMS_ContentInfo *cms, X509 *cert)
554 {
555     int r;
556     r = CMS_add0_cert(cms, cert);
557     if (r > 0)
558         X509_up_ref(cert);
559     return r;
560 }
561
562 static STACK_OF(CMS_RevocationInfoChoice)
563 **cms_get0_revocation_choices(CMS_ContentInfo *cms)
564 {
565     switch (OBJ_obj2nid(cms->contentType)) {
566
567     case NID_pkcs7_signed:
568         return &cms->d.signedData->crls;
569
570     case NID_pkcs7_enveloped:
571         if (cms->d.envelopedData->originatorInfo == NULL)
572             return NULL;
573         return &cms->d.envelopedData->originatorInfo->crls;
574
575     case NID_id_smime_ct_authEnvelopedData:
576         if (cms->d.authEnvelopedData->originatorInfo == NULL)
577             return NULL;
578         return &cms->d.authEnvelopedData->originatorInfo->crls;
579
580     default:
581         ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_CONTENT_TYPE);
582         return NULL;
583
584     }
585 }
586
587 CMS_RevocationInfoChoice *CMS_add0_RevocationInfoChoice(CMS_ContentInfo *cms)
588 {
589     STACK_OF(CMS_RevocationInfoChoice) **pcrls;
590     CMS_RevocationInfoChoice *rch;
591
592     pcrls = cms_get0_revocation_choices(cms);
593     if (pcrls == NULL)
594         return NULL;
595     if (*pcrls == NULL)
596         *pcrls = sk_CMS_RevocationInfoChoice_new_null();
597     if (*pcrls == NULL)
598         return NULL;
599     rch = M_ASN1_new_of(CMS_RevocationInfoChoice);
600     if (rch == NULL)
601         return NULL;
602     if (!sk_CMS_RevocationInfoChoice_push(*pcrls, rch)) {
603         M_ASN1_free_of(rch, CMS_RevocationInfoChoice);
604         return NULL;
605     }
606     return rch;
607 }
608
609 int CMS_add0_crl(CMS_ContentInfo *cms, X509_CRL *crl)
610 {
611     CMS_RevocationInfoChoice *rch;
612     rch = CMS_add0_RevocationInfoChoice(cms);
613     if (!rch)
614         return 0;
615     rch->type = CMS_REVCHOICE_CRL;
616     rch->d.crl = crl;
617     return 1;
618 }
619
620 int CMS_add1_crl(CMS_ContentInfo *cms, X509_CRL *crl)
621 {
622     int r;
623     r = CMS_add0_crl(cms, crl);
624     if (r > 0)
625         X509_CRL_up_ref(crl);
626     return r;
627 }
628
629 STACK_OF(X509) *CMS_get1_certs(CMS_ContentInfo *cms)
630 {
631     STACK_OF(X509) *certs = NULL;
632     CMS_CertificateChoices *cch;
633     STACK_OF(CMS_CertificateChoices) **pcerts;
634     int i;
635
636     pcerts = cms_get0_certificate_choices(cms);
637     if (pcerts == NULL)
638         return NULL;
639     for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++) {
640         cch = sk_CMS_CertificateChoices_value(*pcerts, i);
641         if (cch->type == 0) {
642             if (!ossl_x509_add_cert_new(&certs, cch->d.certificate,
643                                         X509_ADD_FLAG_UP_REF)) {
644                 OSSL_STACK_OF_X509_free(certs);
645                 return NULL;
646             }
647         }
648     }
649     return certs;
650
651 }
652
653 STACK_OF(X509_CRL) *CMS_get1_crls(CMS_ContentInfo *cms)
654 {
655     STACK_OF(X509_CRL) *crls = NULL;
656     STACK_OF(CMS_RevocationInfoChoice) **pcrls;
657     CMS_RevocationInfoChoice *rch;
658     int i;
659
660     pcrls = cms_get0_revocation_choices(cms);
661     if (pcrls == NULL)
662         return NULL;
663     for (i = 0; i < sk_CMS_RevocationInfoChoice_num(*pcrls); i++) {
664         rch = sk_CMS_RevocationInfoChoice_value(*pcrls, i);
665         if (rch->type == 0) {
666             if (!crls) {
667                 crls = sk_X509_CRL_new_null();
668                 if (!crls)
669                     return NULL;
670             }
671             if (!sk_X509_CRL_push(crls, rch->d.crl)) {
672                 sk_X509_CRL_pop_free(crls, X509_CRL_free);
673                 return NULL;
674             }
675             X509_CRL_up_ref(rch->d.crl);
676         }
677     }
678     return crls;
679 }
680
681 int ossl_cms_ias_cert_cmp(CMS_IssuerAndSerialNumber *ias, X509 *cert)
682 {
683     int ret;
684     ret = X509_NAME_cmp(ias->issuer, X509_get_issuer_name(cert));
685     if (ret)
686         return ret;
687     return ASN1_INTEGER_cmp(ias->serialNumber, X509_get0_serialNumber(cert));
688 }
689
690 int ossl_cms_keyid_cert_cmp(ASN1_OCTET_STRING *keyid, X509 *cert)
691 {
692     const ASN1_OCTET_STRING *cert_keyid = X509_get0_subject_key_id(cert);
693
694     if (cert_keyid == NULL)
695         return -1;
696     return ASN1_OCTET_STRING_cmp(keyid, cert_keyid);
697 }
698
699 int ossl_cms_set1_ias(CMS_IssuerAndSerialNumber **pias, X509 *cert)
700 {
701     CMS_IssuerAndSerialNumber *ias;
702     ias = M_ASN1_new_of(CMS_IssuerAndSerialNumber);
703     if (!ias)
704         goto err;
705     if (!X509_NAME_set(&ias->issuer, X509_get_issuer_name(cert)))
706         goto err;
707     if (!ASN1_STRING_copy(ias->serialNumber, X509_get0_serialNumber(cert)))
708         goto err;
709     M_ASN1_free_of(*pias, CMS_IssuerAndSerialNumber);
710     *pias = ias;
711     return 1;
712  err:
713     M_ASN1_free_of(ias, CMS_IssuerAndSerialNumber);
714     ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
715     return 0;
716 }
717
718 int ossl_cms_set1_keyid(ASN1_OCTET_STRING **pkeyid, X509 *cert)
719 {
720     ASN1_OCTET_STRING *keyid = NULL;
721     const ASN1_OCTET_STRING *cert_keyid;
722     cert_keyid = X509_get0_subject_key_id(cert);
723     if (cert_keyid == NULL) {
724         ERR_raise(ERR_LIB_CMS, CMS_R_CERTIFICATE_HAS_NO_KEYID);
725         return 0;
726     }
727     keyid = ASN1_STRING_dup(cert_keyid);
728     if (!keyid) {
729         ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
730         return 0;
731     }
732     ASN1_OCTET_STRING_free(*pkeyid);
733     *pkeyid = keyid;
734     return 1;
735 }