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