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