add OSSL_STACK_OF_X509_free() for commonly used pattern
[openssl.git] / crypto / cms / cms_lib.c
1 /*
2  * Copyright 2008-2021 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     ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
212
213     if (pos == NULL)
214         return 0;
215     /* If embedded content find memory BIO and set content */
216     if (*pos && ((*pos)->flags & ASN1_STRING_FLAG_CONT)) {
217         BIO *mbio;
218         unsigned char *cont;
219         long contlen;
220         mbio = BIO_find_type(cmsbio, BIO_TYPE_MEM);
221         if (!mbio) {
222             ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_NOT_FOUND);
223             return 0;
224         }
225         contlen = BIO_get_mem_data(mbio, &cont);
226         /* Set bio as read only so its content can't be clobbered */
227         BIO_set_flags(mbio, BIO_FLAGS_MEM_RDONLY);
228         BIO_set_mem_eof_return(mbio, 0);
229         ASN1_STRING_set0(*pos, cont, contlen);
230         (*pos)->flags &= ~ASN1_STRING_FLAG_CONT;
231     }
232
233     switch (OBJ_obj2nid(cms->contentType)) {
234
235     case NID_pkcs7_data:
236     case NID_pkcs7_encrypted:
237     case NID_id_smime_ct_compressedData:
238         /* Nothing to do */
239         return 1;
240
241     case NID_pkcs7_enveloped:
242         return ossl_cms_EnvelopedData_final(cms, cmsbio);
243
244     case NID_id_smime_ct_authEnvelopedData:
245         return ossl_cms_AuthEnvelopedData_final(cms, cmsbio);
246
247     case NID_pkcs7_signed:
248         return ossl_cms_SignedData_final(cms, cmsbio);
249
250     case NID_pkcs7_digest:
251         return ossl_cms_DigestedData_do_final(cms, cmsbio, 0);
252
253     default:
254         ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_TYPE);
255         return 0;
256     }
257 }
258
259 /*
260  * Return an OCTET STRING pointer to content. This allows it to be accessed
261  * or set later.
262  */
263
264 ASN1_OCTET_STRING **CMS_get0_content(CMS_ContentInfo *cms)
265 {
266     switch (OBJ_obj2nid(cms->contentType)) {
267
268     case NID_pkcs7_data:
269         return &cms->d.data;
270
271     case NID_pkcs7_signed:
272         return &cms->d.signedData->encapContentInfo->eContent;
273
274     case NID_pkcs7_enveloped:
275         return &cms->d.envelopedData->encryptedContentInfo->encryptedContent;
276
277     case NID_pkcs7_digest:
278         return &cms->d.digestedData->encapContentInfo->eContent;
279
280     case NID_pkcs7_encrypted:
281         return &cms->d.encryptedData->encryptedContentInfo->encryptedContent;
282
283     case NID_id_smime_ct_authEnvelopedData:
284         return &cms->d.authEnvelopedData->authEncryptedContentInfo
285                                         ->encryptedContent;
286
287     case NID_id_smime_ct_authData:
288         return &cms->d.authenticatedData->encapContentInfo->eContent;
289
290     case NID_id_smime_ct_compressedData:
291         return &cms->d.compressedData->encapContentInfo->eContent;
292
293     default:
294         if (cms->d.other->type == V_ASN1_OCTET_STRING)
295             return &cms->d.other->value.octet_string;
296         ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_CONTENT_TYPE);
297         return NULL;
298
299     }
300 }
301
302 /*
303  * Return an ASN1_OBJECT pointer to content type. This allows it to be
304  * accessed or set later.
305  */
306
307 static ASN1_OBJECT **cms_get0_econtent_type(CMS_ContentInfo *cms)
308 {
309     switch (OBJ_obj2nid(cms->contentType)) {
310
311     case NID_pkcs7_signed:
312         return &cms->d.signedData->encapContentInfo->eContentType;
313
314     case NID_pkcs7_enveloped:
315         return &cms->d.envelopedData->encryptedContentInfo->contentType;
316
317     case NID_pkcs7_digest:
318         return &cms->d.digestedData->encapContentInfo->eContentType;
319
320     case NID_pkcs7_encrypted:
321         return &cms->d.encryptedData->encryptedContentInfo->contentType;
322
323     case NID_id_smime_ct_authEnvelopedData:
324         return &cms->d.authEnvelopedData->authEncryptedContentInfo
325                                         ->contentType;
326     case NID_id_smime_ct_authData:
327         return &cms->d.authenticatedData->encapContentInfo->eContentType;
328
329     case NID_id_smime_ct_compressedData:
330         return &cms->d.compressedData->encapContentInfo->eContentType;
331
332     default:
333         ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_CONTENT_TYPE);
334         return NULL;
335
336     }
337 }
338
339 const ASN1_OBJECT *CMS_get0_eContentType(CMS_ContentInfo *cms)
340 {
341     ASN1_OBJECT **petype;
342     petype = cms_get0_econtent_type(cms);
343     if (petype)
344         return *petype;
345     return NULL;
346 }
347
348 int CMS_set1_eContentType(CMS_ContentInfo *cms, const ASN1_OBJECT *oid)
349 {
350     ASN1_OBJECT **petype, *etype;
351
352     petype = cms_get0_econtent_type(cms);
353     if (petype == NULL)
354         return 0;
355     if (oid == NULL)
356         return 1;
357     etype = OBJ_dup(oid);
358     if (etype == NULL)
359         return 0;
360     ASN1_OBJECT_free(*petype);
361     *petype = etype;
362     return 1;
363 }
364
365 int CMS_is_detached(CMS_ContentInfo *cms)
366 {
367     ASN1_OCTET_STRING **pos;
368
369     pos = CMS_get0_content(cms);
370     if (pos == NULL)
371         return -1;
372     if (*pos != NULL)
373         return 0;
374     return 1;
375 }
376
377 int CMS_set_detached(CMS_ContentInfo *cms, int detached)
378 {
379     ASN1_OCTET_STRING **pos;
380
381     pos = CMS_get0_content(cms);
382     if (pos == NULL)
383         return 0;
384     if (detached) {
385         ASN1_OCTET_STRING_free(*pos);
386         *pos = NULL;
387         return 1;
388     }
389     if (*pos == NULL)
390         *pos = ASN1_OCTET_STRING_new();
391     if (*pos != NULL) {
392         /*
393          * NB: special flag to show content is created and not read in.
394          */
395         (*pos)->flags |= ASN1_STRING_FLAG_CONT;
396         return 1;
397     }
398     ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
399     return 0;
400 }
401
402 /* Create a digest BIO from an X509_ALGOR structure */
403
404 BIO *ossl_cms_DigestAlgorithm_init_bio(X509_ALGOR *digestAlgorithm,
405                                        const CMS_CTX *ctx)
406 {
407     BIO *mdbio = NULL;
408     const ASN1_OBJECT *digestoid;
409     const EVP_MD *digest = NULL;
410     EVP_MD *fetched_digest = NULL;
411     char alg[OSSL_MAX_NAME_SIZE];
412
413     X509_ALGOR_get0(&digestoid, NULL, NULL, digestAlgorithm);
414     OBJ_obj2txt(alg, sizeof(alg), digestoid, 0);
415
416     (void)ERR_set_mark();
417     fetched_digest = EVP_MD_fetch(ossl_cms_ctx_get0_libctx(ctx), alg,
418                                   ossl_cms_ctx_get0_propq(ctx));
419
420     if (fetched_digest != NULL)
421         digest = fetched_digest;
422     else
423         digest = EVP_get_digestbyobj(digestoid);
424     if (digest == NULL) {
425         (void)ERR_clear_last_mark();
426         ERR_raise(ERR_LIB_CMS, CMS_R_UNKNOWN_DIGEST_ALGORITHM);
427         goto err;
428     }
429     (void)ERR_pop_to_mark();
430
431     mdbio = BIO_new(BIO_f_md());
432     if (mdbio == NULL || !BIO_set_md(mdbio, digest)) {
433         ERR_raise(ERR_LIB_CMS, CMS_R_MD_BIO_INIT_ERROR);
434         goto err;
435     }
436     EVP_MD_free(fetched_digest);
437     return mdbio;
438  err:
439     EVP_MD_free(fetched_digest);
440     BIO_free(mdbio);
441     return NULL;
442 }
443
444 /* Locate a message digest content from a BIO chain based on SignerInfo */
445
446 int ossl_cms_DigestAlgorithm_find_ctx(EVP_MD_CTX *mctx, BIO *chain,
447                                       X509_ALGOR *mdalg)
448 {
449     int nid;
450     const ASN1_OBJECT *mdoid;
451     X509_ALGOR_get0(&mdoid, NULL, NULL, mdalg);
452     nid = OBJ_obj2nid(mdoid);
453     /* Look for digest type to match signature */
454     for (;;) {
455         EVP_MD_CTX *mtmp;
456         chain = BIO_find_type(chain, BIO_TYPE_MD);
457         if (chain == NULL) {
458             ERR_raise(ERR_LIB_CMS, CMS_R_NO_MATCHING_DIGEST);
459             return 0;
460         }
461         BIO_get_md_ctx(chain, &mtmp);
462         if (EVP_MD_CTX_get_type(mtmp) == nid
463             /*
464              * Workaround for broken implementations that use signature
465              * algorithm OID instead of digest.
466              */
467             || EVP_MD_get_pkey_type(EVP_MD_CTX_get0_md(mtmp)) == nid)
468             return EVP_MD_CTX_copy_ex(mctx, mtmp);
469         chain = BIO_next(chain);
470     }
471 }
472
473 static STACK_OF(CMS_CertificateChoices)
474 **cms_get0_certificate_choices(CMS_ContentInfo *cms)
475 {
476     switch (OBJ_obj2nid(cms->contentType)) {
477
478     case NID_pkcs7_signed:
479         return &cms->d.signedData->certificates;
480
481     case NID_pkcs7_enveloped:
482         if (cms->d.envelopedData->originatorInfo == NULL)
483             return NULL;
484         return &cms->d.envelopedData->originatorInfo->certificates;
485
486     case NID_id_smime_ct_authEnvelopedData:
487         if (cms->d.authEnvelopedData->originatorInfo == NULL)
488             return NULL;
489         return &cms->d.authEnvelopedData->originatorInfo->certificates;
490
491     default:
492         ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_CONTENT_TYPE);
493         return NULL;
494
495     }
496 }
497
498 CMS_CertificateChoices *CMS_add0_CertificateChoices(CMS_ContentInfo *cms)
499 {
500     STACK_OF(CMS_CertificateChoices) **pcerts;
501     CMS_CertificateChoices *cch;
502
503     pcerts = cms_get0_certificate_choices(cms);
504     if (pcerts == NULL)
505         return NULL;
506     if (*pcerts == NULL)
507         *pcerts = sk_CMS_CertificateChoices_new_null();
508     if (*pcerts == NULL)
509         return NULL;
510     cch = M_ASN1_new_of(CMS_CertificateChoices);
511     if (!cch)
512         return NULL;
513     if (!sk_CMS_CertificateChoices_push(*pcerts, cch)) {
514         M_ASN1_free_of(cch, CMS_CertificateChoices);
515         return NULL;
516     }
517     return cch;
518 }
519
520 int CMS_add0_cert(CMS_ContentInfo *cms, X509 *cert)
521 {
522     CMS_CertificateChoices *cch;
523     STACK_OF(CMS_CertificateChoices) **pcerts;
524     int i;
525
526     pcerts = cms_get0_certificate_choices(cms);
527     if (pcerts == NULL)
528         return 0;
529     for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++) {
530         cch = sk_CMS_CertificateChoices_value(*pcerts, i);
531         if (cch->type == CMS_CERTCHOICE_CERT) {
532             if (!X509_cmp(cch->d.certificate, cert)) {
533                 ERR_raise(ERR_LIB_CMS, CMS_R_CERTIFICATE_ALREADY_PRESENT);
534                 return 0;
535             }
536         }
537     }
538     cch = CMS_add0_CertificateChoices(cms);
539     if (!cch)
540         return 0;
541     cch->type = CMS_CERTCHOICE_CERT;
542     cch->d.certificate = cert;
543     return 1;
544 }
545
546 int CMS_add1_cert(CMS_ContentInfo *cms, X509 *cert)
547 {
548     int r;
549     r = CMS_add0_cert(cms, cert);
550     if (r > 0)
551         X509_up_ref(cert);
552     return r;
553 }
554
555 static STACK_OF(CMS_RevocationInfoChoice)
556 **cms_get0_revocation_choices(CMS_ContentInfo *cms)
557 {
558     switch (OBJ_obj2nid(cms->contentType)) {
559
560     case NID_pkcs7_signed:
561         return &cms->d.signedData->crls;
562
563     case NID_pkcs7_enveloped:
564         if (cms->d.envelopedData->originatorInfo == NULL)
565             return NULL;
566         return &cms->d.envelopedData->originatorInfo->crls;
567
568     case NID_id_smime_ct_authEnvelopedData:
569         if (cms->d.authEnvelopedData->originatorInfo == NULL)
570             return NULL;
571         return &cms->d.authEnvelopedData->originatorInfo->crls;
572
573     default:
574         ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_CONTENT_TYPE);
575         return NULL;
576
577     }
578 }
579
580 CMS_RevocationInfoChoice *CMS_add0_RevocationInfoChoice(CMS_ContentInfo *cms)
581 {
582     STACK_OF(CMS_RevocationInfoChoice) **pcrls;
583     CMS_RevocationInfoChoice *rch;
584
585     pcrls = cms_get0_revocation_choices(cms);
586     if (pcrls == NULL)
587         return NULL;
588     if (*pcrls == NULL)
589         *pcrls = sk_CMS_RevocationInfoChoice_new_null();
590     if (*pcrls == NULL)
591         return NULL;
592     rch = M_ASN1_new_of(CMS_RevocationInfoChoice);
593     if (rch == NULL)
594         return NULL;
595     if (!sk_CMS_RevocationInfoChoice_push(*pcrls, rch)) {
596         M_ASN1_free_of(rch, CMS_RevocationInfoChoice);
597         return NULL;
598     }
599     return rch;
600 }
601
602 int CMS_add0_crl(CMS_ContentInfo *cms, X509_CRL *crl)
603 {
604     CMS_RevocationInfoChoice *rch;
605     rch = CMS_add0_RevocationInfoChoice(cms);
606     if (!rch)
607         return 0;
608     rch->type = CMS_REVCHOICE_CRL;
609     rch->d.crl = crl;
610     return 1;
611 }
612
613 int CMS_add1_crl(CMS_ContentInfo *cms, X509_CRL *crl)
614 {
615     int r;
616     r = CMS_add0_crl(cms, crl);
617     if (r > 0)
618         X509_CRL_up_ref(crl);
619     return r;
620 }
621
622 STACK_OF(X509) *CMS_get1_certs(CMS_ContentInfo *cms)
623 {
624     STACK_OF(X509) *certs = NULL;
625     CMS_CertificateChoices *cch;
626     STACK_OF(CMS_CertificateChoices) **pcerts;
627     int i;
628
629     pcerts = cms_get0_certificate_choices(cms);
630     if (pcerts == NULL)
631         return NULL;
632     for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++) {
633         cch = sk_CMS_CertificateChoices_value(*pcerts, i);
634         if (cch->type == 0) {
635             if (!ossl_x509_add_cert_new(&certs, cch->d.certificate,
636                                         X509_ADD_FLAG_UP_REF)) {
637                 OSSL_STACK_OF_X509_free(certs);
638                 return NULL;
639             }
640         }
641     }
642     return certs;
643
644 }
645
646 STACK_OF(X509_CRL) *CMS_get1_crls(CMS_ContentInfo *cms)
647 {
648     STACK_OF(X509_CRL) *crls = NULL;
649     STACK_OF(CMS_RevocationInfoChoice) **pcrls;
650     CMS_RevocationInfoChoice *rch;
651     int i;
652
653     pcrls = cms_get0_revocation_choices(cms);
654     if (pcrls == NULL)
655         return NULL;
656     for (i = 0; i < sk_CMS_RevocationInfoChoice_num(*pcrls); i++) {
657         rch = sk_CMS_RevocationInfoChoice_value(*pcrls, i);
658         if (rch->type == 0) {
659             if (!crls) {
660                 crls = sk_X509_CRL_new_null();
661                 if (!crls)
662                     return NULL;
663             }
664             if (!sk_X509_CRL_push(crls, rch->d.crl)) {
665                 sk_X509_CRL_pop_free(crls, X509_CRL_free);
666                 return NULL;
667             }
668             X509_CRL_up_ref(rch->d.crl);
669         }
670     }
671     return crls;
672 }
673
674 int ossl_cms_ias_cert_cmp(CMS_IssuerAndSerialNumber *ias, X509 *cert)
675 {
676     int ret;
677     ret = X509_NAME_cmp(ias->issuer, X509_get_issuer_name(cert));
678     if (ret)
679         return ret;
680     return ASN1_INTEGER_cmp(ias->serialNumber, X509_get0_serialNumber(cert));
681 }
682
683 int ossl_cms_keyid_cert_cmp(ASN1_OCTET_STRING *keyid, X509 *cert)
684 {
685     const ASN1_OCTET_STRING *cert_keyid = X509_get0_subject_key_id(cert);
686
687     if (cert_keyid == NULL)
688         return -1;
689     return ASN1_OCTET_STRING_cmp(keyid, cert_keyid);
690 }
691
692 int ossl_cms_set1_ias(CMS_IssuerAndSerialNumber **pias, X509 *cert)
693 {
694     CMS_IssuerAndSerialNumber *ias;
695     ias = M_ASN1_new_of(CMS_IssuerAndSerialNumber);
696     if (!ias)
697         goto err;
698     if (!X509_NAME_set(&ias->issuer, X509_get_issuer_name(cert)))
699         goto err;
700     if (!ASN1_STRING_copy(ias->serialNumber, X509_get0_serialNumber(cert)))
701         goto err;
702     M_ASN1_free_of(*pias, CMS_IssuerAndSerialNumber);
703     *pias = ias;
704     return 1;
705  err:
706     M_ASN1_free_of(ias, CMS_IssuerAndSerialNumber);
707     ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
708     return 0;
709 }
710
711 int ossl_cms_set1_keyid(ASN1_OCTET_STRING **pkeyid, X509 *cert)
712 {
713     ASN1_OCTET_STRING *keyid = NULL;
714     const ASN1_OCTET_STRING *cert_keyid;
715     cert_keyid = X509_get0_subject_key_id(cert);
716     if (cert_keyid == NULL) {
717         ERR_raise(ERR_LIB_CMS, CMS_R_CERTIFICATE_HAS_NO_KEYID);
718         return 0;
719     }
720     keyid = ASN1_STRING_dup(cert_keyid);
721     if (!keyid) {
722         ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
723         return 0;
724     }
725     ASN1_OCTET_STRING_free(*pkeyid);
726     *pkeyid = keyid;
727     return 1;
728 }