In OpenSSL builds, declare STACK for datatypes ...
[openssl.git] / crypto / pkcs7 / pk7_doit.c
1 /*
2  * Copyright 1995-2018 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 <stdio.h>
11 #include "internal/cryptlib.h"
12 #include <openssl/rand.h>
13 #include <openssl/objects.h>
14 #include <openssl/x509.h>
15 #include <openssl/x509v3.h>
16 #include <openssl/err.h>
17
18 DEFINE_STACK_OF(X509_ALGOR)
19 DEFINE_STACK_OF(X509_ATTRIBUTE)
20 DEFINE_STACK_OF(PKCS7_RECIP_INFO)
21 DEFINE_STACK_OF(PKCS7_SIGNER_INFO)
22
23 static int add_attribute(STACK_OF(X509_ATTRIBUTE) **sk, int nid, int atrtype,
24                          void *value);
25 static ASN1_TYPE *get_attribute(STACK_OF(X509_ATTRIBUTE) *sk, int nid);
26
27 static int PKCS7_type_is_other(PKCS7 *p7)
28 {
29     int isOther = 1;
30
31     int nid = OBJ_obj2nid(p7->type);
32
33     switch (nid) {
34     case NID_pkcs7_data:
35     case NID_pkcs7_signed:
36     case NID_pkcs7_enveloped:
37     case NID_pkcs7_signedAndEnveloped:
38     case NID_pkcs7_digest:
39     case NID_pkcs7_encrypted:
40         isOther = 0;
41         break;
42     default:
43         isOther = 1;
44     }
45
46     return isOther;
47
48 }
49
50 static ASN1_OCTET_STRING *PKCS7_get_octet_string(PKCS7 *p7)
51 {
52     if (PKCS7_type_is_data(p7))
53         return p7->d.data;
54     if (PKCS7_type_is_other(p7) && p7->d.other
55         && (p7->d.other->type == V_ASN1_OCTET_STRING))
56         return p7->d.other->value.octet_string;
57     return NULL;
58 }
59
60 static int PKCS7_bio_add_digest(BIO **pbio, X509_ALGOR *alg)
61 {
62     BIO *btmp;
63     const EVP_MD *md;
64     if ((btmp = BIO_new(BIO_f_md())) == NULL) {
65         PKCS7err(PKCS7_F_PKCS7_BIO_ADD_DIGEST, ERR_R_BIO_LIB);
66         goto err;
67     }
68
69     md = EVP_get_digestbyobj(alg->algorithm);
70     if (md == NULL) {
71         PKCS7err(PKCS7_F_PKCS7_BIO_ADD_DIGEST, PKCS7_R_UNKNOWN_DIGEST_TYPE);
72         goto err;
73     }
74
75     BIO_set_md(btmp, md);
76     if (*pbio == NULL)
77         *pbio = btmp;
78     else if (!BIO_push(*pbio, btmp)) {
79         PKCS7err(PKCS7_F_PKCS7_BIO_ADD_DIGEST, ERR_R_BIO_LIB);
80         goto err;
81     }
82     btmp = NULL;
83
84     return 1;
85
86  err:
87     BIO_free(btmp);
88     return 0;
89
90 }
91
92 static int pkcs7_encode_rinfo(PKCS7_RECIP_INFO *ri,
93                               unsigned char *key, int keylen)
94 {
95     EVP_PKEY_CTX *pctx = NULL;
96     EVP_PKEY *pkey = NULL;
97     unsigned char *ek = NULL;
98     int ret = 0;
99     size_t eklen;
100
101     pkey = X509_get0_pubkey(ri->cert);
102     if (pkey == NULL)
103         return 0;
104
105     pctx = EVP_PKEY_CTX_new(pkey, NULL);
106     if (pctx == NULL)
107         return 0;
108
109     if (EVP_PKEY_encrypt_init(pctx) <= 0)
110         goto err;
111
112     if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_ENCRYPT,
113                           EVP_PKEY_CTRL_PKCS7_ENCRYPT, 0, ri) <= 0) {
114         PKCS7err(PKCS7_F_PKCS7_ENCODE_RINFO, PKCS7_R_CTRL_ERROR);
115         goto err;
116     }
117
118     if (EVP_PKEY_encrypt(pctx, NULL, &eklen, key, keylen) <= 0)
119         goto err;
120
121     ek = OPENSSL_malloc(eklen);
122
123     if (ek == NULL) {
124         PKCS7err(PKCS7_F_PKCS7_ENCODE_RINFO, ERR_R_MALLOC_FAILURE);
125         goto err;
126     }
127
128     if (EVP_PKEY_encrypt(pctx, ek, &eklen, key, keylen) <= 0)
129         goto err;
130
131     ASN1_STRING_set0(ri->enc_key, ek, eklen);
132     ek = NULL;
133
134     ret = 1;
135
136  err:
137     EVP_PKEY_CTX_free(pctx);
138     OPENSSL_free(ek);
139     return ret;
140
141 }
142
143 static int pkcs7_decrypt_rinfo(unsigned char **pek, int *peklen,
144                                PKCS7_RECIP_INFO *ri, EVP_PKEY *pkey,
145                                size_t fixlen)
146 {
147     EVP_PKEY_CTX *pctx = NULL;
148     unsigned char *ek = NULL;
149     size_t eklen;
150     int ret = -1;
151
152     pctx = EVP_PKEY_CTX_new(pkey, NULL);
153     if (pctx == NULL)
154         return -1;
155
156     if (EVP_PKEY_decrypt_init(pctx) <= 0)
157         goto err;
158
159     if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DECRYPT,
160                           EVP_PKEY_CTRL_PKCS7_DECRYPT, 0, ri) <= 0) {
161         PKCS7err(PKCS7_F_PKCS7_DECRYPT_RINFO, PKCS7_R_CTRL_ERROR);
162         goto err;
163     }
164
165     if (EVP_PKEY_decrypt(pctx, NULL, &eklen,
166                          ri->enc_key->data, ri->enc_key->length) <= 0)
167         goto err;
168
169     ek = OPENSSL_malloc(eklen);
170
171     if (ek == NULL) {
172         PKCS7err(PKCS7_F_PKCS7_DECRYPT_RINFO, ERR_R_MALLOC_FAILURE);
173         goto err;
174     }
175
176     if (EVP_PKEY_decrypt(pctx, ek, &eklen,
177                          ri->enc_key->data, ri->enc_key->length) <= 0
178             || eklen == 0
179             || (fixlen != 0 && eklen != fixlen)) {
180         ret = 0;
181         PKCS7err(PKCS7_F_PKCS7_DECRYPT_RINFO, ERR_R_EVP_LIB);
182         goto err;
183     }
184
185     ret = 1;
186
187     OPENSSL_clear_free(*pek, *peklen);
188     *pek = ek;
189     *peklen = eklen;
190
191  err:
192     EVP_PKEY_CTX_free(pctx);
193     if (!ret)
194         OPENSSL_free(ek);
195
196     return ret;
197 }
198
199 BIO *PKCS7_dataInit(PKCS7 *p7, BIO *bio)
200 {
201     int i;
202     BIO *out = NULL, *btmp = NULL;
203     X509_ALGOR *xa = NULL;
204     const EVP_CIPHER *evp_cipher = NULL;
205     STACK_OF(X509_ALGOR) *md_sk = NULL;
206     STACK_OF(PKCS7_RECIP_INFO) *rsk = NULL;
207     X509_ALGOR *xalg = NULL;
208     PKCS7_RECIP_INFO *ri = NULL;
209     ASN1_OCTET_STRING *os = NULL;
210
211     if (p7 == NULL) {
212         PKCS7err(PKCS7_F_PKCS7_DATAINIT, PKCS7_R_INVALID_NULL_POINTER);
213         return NULL;
214     }
215     /*
216      * The content field in the PKCS7 ContentInfo is optional, but that really
217      * only applies to inner content (precisely, detached signatures).
218      *
219      * When reading content, missing outer content is therefore treated as an
220      * error.
221      *
222      * When creating content, PKCS7_content_new() must be called before
223      * calling this method, so a NULL p7->d is always an error.
224      */
225     if (p7->d.ptr == NULL) {
226         PKCS7err(PKCS7_F_PKCS7_DATAINIT, PKCS7_R_NO_CONTENT);
227         return NULL;
228     }
229
230     i = OBJ_obj2nid(p7->type);
231     p7->state = PKCS7_S_HEADER;
232
233     switch (i) {
234     case NID_pkcs7_signed:
235         md_sk = p7->d.sign->md_algs;
236         os = PKCS7_get_octet_string(p7->d.sign->contents);
237         break;
238     case NID_pkcs7_signedAndEnveloped:
239         rsk = p7->d.signed_and_enveloped->recipientinfo;
240         md_sk = p7->d.signed_and_enveloped->md_algs;
241         xalg = p7->d.signed_and_enveloped->enc_data->algorithm;
242         evp_cipher = p7->d.signed_and_enveloped->enc_data->cipher;
243         if (evp_cipher == NULL) {
244             PKCS7err(PKCS7_F_PKCS7_DATAINIT, PKCS7_R_CIPHER_NOT_INITIALIZED);
245             goto err;
246         }
247         break;
248     case NID_pkcs7_enveloped:
249         rsk = p7->d.enveloped->recipientinfo;
250         xalg = p7->d.enveloped->enc_data->algorithm;
251         evp_cipher = p7->d.enveloped->enc_data->cipher;
252         if (evp_cipher == NULL) {
253             PKCS7err(PKCS7_F_PKCS7_DATAINIT, PKCS7_R_CIPHER_NOT_INITIALIZED);
254             goto err;
255         }
256         break;
257     case NID_pkcs7_digest:
258         xa = p7->d.digest->md;
259         os = PKCS7_get_octet_string(p7->d.digest->contents);
260         break;
261     case NID_pkcs7_data:
262         break;
263     default:
264         PKCS7err(PKCS7_F_PKCS7_DATAINIT, PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
265         goto err;
266     }
267
268     for (i = 0; i < sk_X509_ALGOR_num(md_sk); i++)
269         if (!PKCS7_bio_add_digest(&out, sk_X509_ALGOR_value(md_sk, i)))
270             goto err;
271
272     if (xa && !PKCS7_bio_add_digest(&out, xa))
273         goto err;
274
275     if (evp_cipher != NULL) {
276         unsigned char key[EVP_MAX_KEY_LENGTH];
277         unsigned char iv[EVP_MAX_IV_LENGTH];
278         int keylen, ivlen;
279         EVP_CIPHER_CTX *ctx;
280
281         if ((btmp = BIO_new(BIO_f_cipher())) == NULL) {
282             PKCS7err(PKCS7_F_PKCS7_DATAINIT, ERR_R_BIO_LIB);
283             goto err;
284         }
285         BIO_get_cipher_ctx(btmp, &ctx);
286         keylen = EVP_CIPHER_key_length(evp_cipher);
287         ivlen = EVP_CIPHER_iv_length(evp_cipher);
288         xalg->algorithm = OBJ_nid2obj(EVP_CIPHER_type(evp_cipher));
289         if (ivlen > 0)
290             if (RAND_bytes(iv, ivlen) <= 0)
291                 goto err;
292         if (EVP_CipherInit_ex(ctx, evp_cipher, NULL, NULL, NULL, 1) <= 0)
293             goto err;
294         if (EVP_CIPHER_CTX_rand_key(ctx, key) <= 0)
295             goto err;
296         if (EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, 1) <= 0)
297             goto err;
298
299         if (ivlen > 0) {
300             if (xalg->parameter == NULL) {
301                 xalg->parameter = ASN1_TYPE_new();
302                 if (xalg->parameter == NULL)
303                     goto err;
304             }
305             if (EVP_CIPHER_param_to_asn1(ctx, xalg->parameter) < 0)
306                 goto err;
307         }
308
309         /* Lets do the pub key stuff :-) */
310         for (i = 0; i < sk_PKCS7_RECIP_INFO_num(rsk); i++) {
311             ri = sk_PKCS7_RECIP_INFO_value(rsk, i);
312             if (pkcs7_encode_rinfo(ri, key, keylen) <= 0)
313                 goto err;
314         }
315         OPENSSL_cleanse(key, keylen);
316
317         if (out == NULL)
318             out = btmp;
319         else
320             BIO_push(out, btmp);
321         btmp = NULL;
322     }
323
324     if (bio == NULL) {
325         if (PKCS7_is_detached(p7)) {
326             bio = BIO_new(BIO_s_null());
327         } else if (os && os->length > 0) {
328             bio = BIO_new_mem_buf(os->data, os->length);
329         } else {
330             bio = BIO_new(BIO_s_mem());
331             if (bio == NULL)
332                 goto err;
333             BIO_set_mem_eof_return(bio, 0);
334         }
335         if (bio == NULL)
336             goto err;
337     }
338     if (out)
339         BIO_push(out, bio);
340     else
341         out = bio;
342     return out;
343
344  err:
345     BIO_free_all(out);
346     BIO_free_all(btmp);
347     return NULL;
348 }
349
350 static int pkcs7_cmp_ri(PKCS7_RECIP_INFO *ri, X509 *pcert)
351 {
352     int ret;
353     ret = X509_NAME_cmp(ri->issuer_and_serial->issuer,
354                         X509_get_issuer_name(pcert));
355     if (ret)
356         return ret;
357     return ASN1_INTEGER_cmp(X509_get_serialNumber(pcert),
358                             ri->issuer_and_serial->serial);
359 }
360
361 /* int */
362 BIO *PKCS7_dataDecode(PKCS7 *p7, EVP_PKEY *pkey, BIO *in_bio, X509 *pcert)
363 {
364     int i, j;
365     BIO *out = NULL, *btmp = NULL, *etmp = NULL, *bio = NULL;
366     X509_ALGOR *xa;
367     ASN1_OCTET_STRING *data_body = NULL;
368     const EVP_MD *evp_md;
369     const EVP_CIPHER *evp_cipher = NULL;
370     EVP_CIPHER_CTX *evp_ctx = NULL;
371     X509_ALGOR *enc_alg = NULL;
372     STACK_OF(X509_ALGOR) *md_sk = NULL;
373     STACK_OF(PKCS7_RECIP_INFO) *rsk = NULL;
374     PKCS7_RECIP_INFO *ri = NULL;
375     unsigned char *ek = NULL, *tkey = NULL;
376     int eklen = 0, tkeylen = 0;
377
378     if (p7 == NULL) {
379         PKCS7err(PKCS7_F_PKCS7_DATADECODE, PKCS7_R_INVALID_NULL_POINTER);
380         return NULL;
381     }
382
383     if (p7->d.ptr == NULL) {
384         PKCS7err(PKCS7_F_PKCS7_DATADECODE, PKCS7_R_NO_CONTENT);
385         return NULL;
386     }
387
388     i = OBJ_obj2nid(p7->type);
389     p7->state = PKCS7_S_HEADER;
390
391     switch (i) {
392     case NID_pkcs7_signed:
393         /*
394          * p7->d.sign->contents is a PKCS7 structure consisting of a contentType
395          * field and optional content.
396          * data_body is NULL if that structure has no (=detached) content
397          * or if the contentType is wrong (i.e., not "data").
398          */
399         data_body = PKCS7_get_octet_string(p7->d.sign->contents);
400         if (!PKCS7_is_detached(p7) && data_body == NULL) {
401             PKCS7err(PKCS7_F_PKCS7_DATADECODE,
402                      PKCS7_R_INVALID_SIGNED_DATA_TYPE);
403             goto err;
404         }
405         md_sk = p7->d.sign->md_algs;
406         break;
407     case NID_pkcs7_signedAndEnveloped:
408         rsk = p7->d.signed_and_enveloped->recipientinfo;
409         md_sk = p7->d.signed_and_enveloped->md_algs;
410         /* data_body is NULL if the optional EncryptedContent is missing. */
411         data_body = p7->d.signed_and_enveloped->enc_data->enc_data;
412         enc_alg = p7->d.signed_and_enveloped->enc_data->algorithm;
413         evp_cipher = EVP_get_cipherbyobj(enc_alg->algorithm);
414         if (evp_cipher == NULL) {
415             PKCS7err(PKCS7_F_PKCS7_DATADECODE,
416                      PKCS7_R_UNSUPPORTED_CIPHER_TYPE);
417             goto err;
418         }
419         break;
420     case NID_pkcs7_enveloped:
421         rsk = p7->d.enveloped->recipientinfo;
422         enc_alg = p7->d.enveloped->enc_data->algorithm;
423         /* data_body is NULL if the optional EncryptedContent is missing. */
424         data_body = p7->d.enveloped->enc_data->enc_data;
425         evp_cipher = EVP_get_cipherbyobj(enc_alg->algorithm);
426         if (evp_cipher == NULL) {
427             PKCS7err(PKCS7_F_PKCS7_DATADECODE,
428                      PKCS7_R_UNSUPPORTED_CIPHER_TYPE);
429             goto err;
430         }
431         break;
432     default:
433         PKCS7err(PKCS7_F_PKCS7_DATADECODE, PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
434         goto err;
435     }
436
437     /* Detached content must be supplied via in_bio instead. */
438     if (data_body == NULL && in_bio == NULL) {
439         PKCS7err(PKCS7_F_PKCS7_DATADECODE, PKCS7_R_NO_CONTENT);
440         goto err;
441     }
442
443     /* We will be checking the signature */
444     if (md_sk != NULL) {
445         for (i = 0; i < sk_X509_ALGOR_num(md_sk); i++) {
446             xa = sk_X509_ALGOR_value(md_sk, i);
447             if ((btmp = BIO_new(BIO_f_md())) == NULL) {
448                 PKCS7err(PKCS7_F_PKCS7_DATADECODE, ERR_R_BIO_LIB);
449                 goto err;
450             }
451
452             j = OBJ_obj2nid(xa->algorithm);
453             evp_md = EVP_get_digestbynid(j);
454             if (evp_md == NULL) {
455                 PKCS7err(PKCS7_F_PKCS7_DATADECODE,
456                          PKCS7_R_UNKNOWN_DIGEST_TYPE);
457                 goto err;
458             }
459
460             BIO_set_md(btmp, evp_md);
461             if (out == NULL)
462                 out = btmp;
463             else
464                 BIO_push(out, btmp);
465             btmp = NULL;
466         }
467     }
468
469     if (evp_cipher != NULL) {
470         if ((etmp = BIO_new(BIO_f_cipher())) == NULL) {
471             PKCS7err(PKCS7_F_PKCS7_DATADECODE, ERR_R_BIO_LIB);
472             goto err;
473         }
474
475         /*
476          * It was encrypted, we need to decrypt the secret key with the
477          * private key
478          */
479
480         /*
481          * Find the recipientInfo which matches the passed certificate (if
482          * any)
483          */
484
485         if (pcert) {
486             for (i = 0; i < sk_PKCS7_RECIP_INFO_num(rsk); i++) {
487                 ri = sk_PKCS7_RECIP_INFO_value(rsk, i);
488                 if (!pkcs7_cmp_ri(ri, pcert))
489                     break;
490                 ri = NULL;
491             }
492             if (ri == NULL) {
493                 PKCS7err(PKCS7_F_PKCS7_DATADECODE,
494                          PKCS7_R_NO_RECIPIENT_MATCHES_CERTIFICATE);
495                 goto err;
496             }
497         }
498
499         /* If we haven't got a certificate try each ri in turn */
500         if (pcert == NULL) {
501             /*
502              * Always attempt to decrypt all rinfo even after success as a
503              * defence against MMA timing attacks.
504              */
505             for (i = 0; i < sk_PKCS7_RECIP_INFO_num(rsk); i++) {
506                 ri = sk_PKCS7_RECIP_INFO_value(rsk, i);
507
508                 if (pkcs7_decrypt_rinfo(&ek, &eklen, ri, pkey,
509                         EVP_CIPHER_key_length(evp_cipher)) < 0)
510                     goto err;
511                 ERR_clear_error();
512             }
513         } else {
514             /* Only exit on fatal errors, not decrypt failure */
515             if (pkcs7_decrypt_rinfo(&ek, &eklen, ri, pkey, 0) < 0)
516                 goto err;
517             ERR_clear_error();
518         }
519
520         evp_ctx = NULL;
521         BIO_get_cipher_ctx(etmp, &evp_ctx);
522         if (EVP_CipherInit_ex(evp_ctx, evp_cipher, NULL, NULL, NULL, 0) <= 0)
523             goto err;
524         if (EVP_CIPHER_asn1_to_param(evp_ctx, enc_alg->parameter) < 0)
525             goto err;
526         /* Generate random key as MMA defence */
527         tkeylen = EVP_CIPHER_CTX_key_length(evp_ctx);
528         tkey = OPENSSL_malloc(tkeylen);
529         if (tkey == NULL)
530             goto err;
531         if (EVP_CIPHER_CTX_rand_key(evp_ctx, tkey) <= 0)
532             goto err;
533         if (ek == NULL) {
534             ek = tkey;
535             eklen = tkeylen;
536             tkey = NULL;
537         }
538
539         if (eklen != EVP_CIPHER_CTX_key_length(evp_ctx)) {
540             /*
541              * Some S/MIME clients don't use the same key and effective key
542              * length. The key length is determined by the size of the
543              * decrypted RSA key.
544              */
545             if (!EVP_CIPHER_CTX_set_key_length(evp_ctx, eklen)) {
546                 /* Use random key as MMA defence */
547                 OPENSSL_clear_free(ek, eklen);
548                 ek = tkey;
549                 eklen = tkeylen;
550                 tkey = NULL;
551             }
552         }
553         /* Clear errors so we don't leak information useful in MMA */
554         ERR_clear_error();
555         if (EVP_CipherInit_ex(evp_ctx, NULL, NULL, ek, NULL, 0) <= 0)
556             goto err;
557
558         OPENSSL_clear_free(ek, eklen);
559         ek = NULL;
560         OPENSSL_clear_free(tkey, tkeylen);
561         tkey = NULL;
562
563         if (out == NULL)
564             out = etmp;
565         else
566             BIO_push(out, etmp);
567         etmp = NULL;
568     }
569     if (in_bio != NULL) {
570         bio = in_bio;
571     } else {
572         if (data_body->length > 0)
573             bio = BIO_new_mem_buf(data_body->data, data_body->length);
574         else {
575             bio = BIO_new(BIO_s_mem());
576             if (bio == NULL)
577                 goto err;
578             BIO_set_mem_eof_return(bio, 0);
579         }
580         if (bio == NULL)
581             goto err;
582     }
583     BIO_push(out, bio);
584     bio = NULL;
585     return out;
586
587  err:
588     OPENSSL_clear_free(ek, eklen);
589     OPENSSL_clear_free(tkey, tkeylen);
590     BIO_free_all(out);
591     BIO_free_all(btmp);
592     BIO_free_all(etmp);
593     BIO_free_all(bio);
594     return NULL;
595 }
596
597 static BIO *PKCS7_find_digest(EVP_MD_CTX **pmd, BIO *bio, int nid)
598 {
599     for (;;) {
600         bio = BIO_find_type(bio, BIO_TYPE_MD);
601         if (bio == NULL) {
602             PKCS7err(PKCS7_F_PKCS7_FIND_DIGEST,
603                      PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);
604             return NULL;
605         }
606         BIO_get_md_ctx(bio, pmd);
607         if (*pmd == NULL) {
608             PKCS7err(PKCS7_F_PKCS7_FIND_DIGEST, ERR_R_INTERNAL_ERROR);
609             return NULL;
610         }
611         if (EVP_MD_CTX_type(*pmd) == nid)
612             return bio;
613         bio = BIO_next(bio);
614     }
615     return NULL;
616 }
617
618 static int do_pkcs7_signed_attrib(PKCS7_SIGNER_INFO *si, EVP_MD_CTX *mctx)
619 {
620     unsigned char md_data[EVP_MAX_MD_SIZE];
621     unsigned int md_len;
622
623     /* Add signing time if not already present */
624     if (!PKCS7_get_signed_attribute(si, NID_pkcs9_signingTime)) {
625         if (!PKCS7_add0_attrib_signing_time(si, NULL)) {
626             PKCS7err(PKCS7_F_DO_PKCS7_SIGNED_ATTRIB, ERR_R_MALLOC_FAILURE);
627             return 0;
628         }
629     }
630
631     /* Add digest */
632     if (!EVP_DigestFinal_ex(mctx, md_data, &md_len)) {
633         PKCS7err(PKCS7_F_DO_PKCS7_SIGNED_ATTRIB, ERR_R_EVP_LIB);
634         return 0;
635     }
636     if (!PKCS7_add1_attrib_digest(si, md_data, md_len)) {
637         PKCS7err(PKCS7_F_DO_PKCS7_SIGNED_ATTRIB, ERR_R_MALLOC_FAILURE);
638         return 0;
639     }
640
641     /* Now sign the attributes */
642     if (!PKCS7_SIGNER_INFO_sign(si))
643         return 0;
644
645     return 1;
646 }
647
648 int PKCS7_dataFinal(PKCS7 *p7, BIO *bio)
649 {
650     int ret = 0;
651     int i, j;
652     BIO *btmp;
653     PKCS7_SIGNER_INFO *si;
654     EVP_MD_CTX *mdc, *ctx_tmp;
655     STACK_OF(X509_ATTRIBUTE) *sk;
656     STACK_OF(PKCS7_SIGNER_INFO) *si_sk = NULL;
657     ASN1_OCTET_STRING *os = NULL;
658
659     if (p7 == NULL) {
660         PKCS7err(PKCS7_F_PKCS7_DATAFINAL, PKCS7_R_INVALID_NULL_POINTER);
661         return 0;
662     }
663
664     if (p7->d.ptr == NULL) {
665         PKCS7err(PKCS7_F_PKCS7_DATAFINAL, PKCS7_R_NO_CONTENT);
666         return 0;
667     }
668
669     ctx_tmp = EVP_MD_CTX_new();
670     if (ctx_tmp == NULL) {
671         PKCS7err(PKCS7_F_PKCS7_DATAFINAL, ERR_R_MALLOC_FAILURE);
672         return 0;
673     }
674
675     i = OBJ_obj2nid(p7->type);
676     p7->state = PKCS7_S_HEADER;
677
678     switch (i) {
679     case NID_pkcs7_data:
680         os = p7->d.data;
681         break;
682     case NID_pkcs7_signedAndEnveloped:
683         /* XXXXXXXXXXXXXXXX */
684         si_sk = p7->d.signed_and_enveloped->signer_info;
685         os = p7->d.signed_and_enveloped->enc_data->enc_data;
686         if (os == NULL) {
687             os = ASN1_OCTET_STRING_new();
688             if (os == NULL) {
689                 PKCS7err(PKCS7_F_PKCS7_DATAFINAL, ERR_R_MALLOC_FAILURE);
690                 goto err;
691             }
692             p7->d.signed_and_enveloped->enc_data->enc_data = os;
693         }
694         break;
695     case NID_pkcs7_enveloped:
696         /* XXXXXXXXXXXXXXXX */
697         os = p7->d.enveloped->enc_data->enc_data;
698         if (os == NULL) {
699             os = ASN1_OCTET_STRING_new();
700             if (os == NULL) {
701                 PKCS7err(PKCS7_F_PKCS7_DATAFINAL, ERR_R_MALLOC_FAILURE);
702                 goto err;
703             }
704             p7->d.enveloped->enc_data->enc_data = os;
705         }
706         break;
707     case NID_pkcs7_signed:
708         si_sk = p7->d.sign->signer_info;
709         os = PKCS7_get_octet_string(p7->d.sign->contents);
710         /* If detached data then the content is excluded */
711         if (PKCS7_type_is_data(p7->d.sign->contents) && p7->detached) {
712             ASN1_OCTET_STRING_free(os);
713             os = NULL;
714             p7->d.sign->contents->d.data = NULL;
715         }
716         break;
717
718     case NID_pkcs7_digest:
719         os = PKCS7_get_octet_string(p7->d.digest->contents);
720         /* If detached data then the content is excluded */
721         if (PKCS7_type_is_data(p7->d.digest->contents) && p7->detached) {
722             ASN1_OCTET_STRING_free(os);
723             os = NULL;
724             p7->d.digest->contents->d.data = NULL;
725         }
726         break;
727
728     default:
729         PKCS7err(PKCS7_F_PKCS7_DATAFINAL, PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
730         goto err;
731     }
732
733     if (si_sk != NULL) {
734         for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(si_sk); i++) {
735             si = sk_PKCS7_SIGNER_INFO_value(si_sk, i);
736             if (si->pkey == NULL)
737                 continue;
738
739             j = OBJ_obj2nid(si->digest_alg->algorithm);
740
741             btmp = bio;
742
743             btmp = PKCS7_find_digest(&mdc, btmp, j);
744
745             if (btmp == NULL)
746                 goto err;
747
748             /*
749              * We now have the EVP_MD_CTX, lets do the signing.
750              */
751             if (!EVP_MD_CTX_copy_ex(ctx_tmp, mdc))
752                 goto err;
753
754             sk = si->auth_attr;
755
756             /*
757              * If there are attributes, we add the digest attribute and only
758              * sign the attributes
759              */
760             if (sk_X509_ATTRIBUTE_num(sk) > 0) {
761                 if (!do_pkcs7_signed_attrib(si, ctx_tmp))
762                     goto err;
763             } else {
764                 unsigned char *abuf = NULL;
765                 unsigned int abuflen;
766                 abuflen = EVP_PKEY_size(si->pkey);
767                 abuf = OPENSSL_malloc(abuflen);
768                 if (abuf == NULL)
769                     goto err;
770
771                 if (!EVP_SignFinal(ctx_tmp, abuf, &abuflen, si->pkey)) {
772                     OPENSSL_free(abuf);
773                     PKCS7err(PKCS7_F_PKCS7_DATAFINAL, ERR_R_EVP_LIB);
774                     goto err;
775                 }
776                 ASN1_STRING_set0(si->enc_digest, abuf, abuflen);
777             }
778         }
779     } else if (i == NID_pkcs7_digest) {
780         unsigned char md_data[EVP_MAX_MD_SIZE];
781         unsigned int md_len;
782         if (!PKCS7_find_digest(&mdc, bio,
783                                OBJ_obj2nid(p7->d.digest->md->algorithm)))
784             goto err;
785         if (!EVP_DigestFinal_ex(mdc, md_data, &md_len))
786             goto err;
787         if (!ASN1_OCTET_STRING_set(p7->d.digest->digest, md_data, md_len))
788             goto err;
789     }
790
791     if (!PKCS7_is_detached(p7)) {
792         /*
793          * NOTE(emilia): I think we only reach os == NULL here because detached
794          * digested data support is broken.
795          */
796         if (os == NULL)
797             goto err;
798         if (!(os->flags & ASN1_STRING_FLAG_NDEF)) {
799             char *cont;
800             long contlen;
801             btmp = BIO_find_type(bio, BIO_TYPE_MEM);
802             if (btmp == NULL) {
803                 PKCS7err(PKCS7_F_PKCS7_DATAFINAL, PKCS7_R_UNABLE_TO_FIND_MEM_BIO);
804                 goto err;
805             }
806             contlen = BIO_get_mem_data(btmp, &cont);
807             /*
808              * Mark the BIO read only then we can use its copy of the data
809              * instead of making an extra copy.
810              */
811             BIO_set_flags(btmp, BIO_FLAGS_MEM_RDONLY);
812             BIO_set_mem_eof_return(btmp, 0);
813             ASN1_STRING_set0(os, (unsigned char *)cont, contlen);
814         }
815     }
816     ret = 1;
817  err:
818     EVP_MD_CTX_free(ctx_tmp);
819     return ret;
820 }
821
822 int PKCS7_SIGNER_INFO_sign(PKCS7_SIGNER_INFO *si)
823 {
824     EVP_MD_CTX *mctx;
825     EVP_PKEY_CTX *pctx = NULL;
826     unsigned char *abuf = NULL;
827     int alen;
828     size_t siglen;
829     const EVP_MD *md = NULL;
830
831     md = EVP_get_digestbyobj(si->digest_alg->algorithm);
832     if (md == NULL)
833         return 0;
834
835     mctx = EVP_MD_CTX_new();
836     if (mctx == NULL) {
837         PKCS7err(PKCS7_F_PKCS7_SIGNER_INFO_SIGN, ERR_R_MALLOC_FAILURE);
838         goto err;
839     }
840
841     if (EVP_DigestSignInit(mctx, &pctx, md, NULL, si->pkey) <= 0)
842         goto err;
843
844     /*
845      * TODO(3.0): This causes problems when providers are in use, so disabled
846      * for now. Can we get rid of this completely? AFAICT this ctrl has never
847      * been used since it was first put in. All internal implementations just
848      * return 1 and ignore this ctrl and have always done so by the looks of
849      * things. To fix this we could convert this ctrl into a param, which would
850      * require us to send all the signer info data as a set of params...but that
851      * is non-trivial and since this isn't used by anything it may be better
852      * just to remove it. The original commit that added it had this
853      * justification in CHANGES:
854      *
855      * "During PKCS7 signing pass the PKCS7 SignerInfo structure to the
856      *  EVP_PKEY_METHOD before and after signing via the
857      *  EVP_PKEY_CTRL_PKCS7_SIGN ctrl. It can then customise the structure
858      *  before and/or after signing if necessary."
859      */
860 #if 0
861     if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN,
862                           EVP_PKEY_CTRL_PKCS7_SIGN, 0, si) <= 0) {
863         PKCS7err(PKCS7_F_PKCS7_SIGNER_INFO_SIGN, PKCS7_R_CTRL_ERROR);
864         goto err;
865     }
866 #endif
867
868     alen = ASN1_item_i2d((ASN1_VALUE *)si->auth_attr, &abuf,
869                          ASN1_ITEM_rptr(PKCS7_ATTR_SIGN));
870     if (!abuf)
871         goto err;
872     if (EVP_DigestSignUpdate(mctx, abuf, alen) <= 0)
873         goto err;
874     OPENSSL_free(abuf);
875     abuf = NULL;
876     if (EVP_DigestSignFinal(mctx, NULL, &siglen) <= 0)
877         goto err;
878     abuf = OPENSSL_malloc(siglen);
879     if (abuf == NULL)
880         goto err;
881     if (EVP_DigestSignFinal(mctx, abuf, &siglen) <= 0)
882         goto err;
883
884     /*
885      * TODO(3.0): This causes problems when providers are in use, so disabled
886      * for now. Can we get rid of this completely? AFAICT this ctrl has never
887      * been used since it was first put in. All internal implementations just
888      * return 1 and ignore this ctrl and have always done so by the looks of
889      * things. To fix this we could convert this ctrl into a param, which would
890      * require us to send all the signer info data as a set of params...but that
891      * is non-trivial and since this isn't used by anything it may be better
892      * just to remove it. The original commit that added it had this
893      * justification in CHANGES:
894      *
895      * "During PKCS7 signing pass the PKCS7 SignerInfo structure to the
896      *  EVP_PKEY_METHOD before and after signing via the
897      *  EVP_PKEY_CTRL_PKCS7_SIGN ctrl. It can then customise the structure
898      *  before and/or after signing if necessary."
899      */
900 #if 0
901     if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN,
902                           EVP_PKEY_CTRL_PKCS7_SIGN, 1, si) <= 0) {
903         PKCS7err(PKCS7_F_PKCS7_SIGNER_INFO_SIGN, PKCS7_R_CTRL_ERROR);
904         goto err;
905     }
906 #endif
907
908     EVP_MD_CTX_free(mctx);
909
910     ASN1_STRING_set0(si->enc_digest, abuf, siglen);
911
912     return 1;
913
914  err:
915     OPENSSL_free(abuf);
916     EVP_MD_CTX_free(mctx);
917     return 0;
918
919 }
920
921 int PKCS7_dataVerify(X509_STORE *cert_store, X509_STORE_CTX *ctx, BIO *bio,
922                      PKCS7 *p7, PKCS7_SIGNER_INFO *si)
923 {
924     PKCS7_ISSUER_AND_SERIAL *ias;
925     int ret = 0, i;
926     STACK_OF(X509) *cert;
927     X509 *x509;
928
929     if (p7 == NULL) {
930         PKCS7err(PKCS7_F_PKCS7_DATAVERIFY, PKCS7_R_INVALID_NULL_POINTER);
931         return 0;
932     }
933
934     if (p7->d.ptr == NULL) {
935         PKCS7err(PKCS7_F_PKCS7_DATAVERIFY, PKCS7_R_NO_CONTENT);
936         return 0;
937     }
938
939     if (PKCS7_type_is_signed(p7)) {
940         cert = p7->d.sign->cert;
941     } else if (PKCS7_type_is_signedAndEnveloped(p7)) {
942         cert = p7->d.signed_and_enveloped->cert;
943     } else {
944         PKCS7err(PKCS7_F_PKCS7_DATAVERIFY, PKCS7_R_WRONG_PKCS7_TYPE);
945         goto err;
946     }
947     /* XXXXXXXXXXXXXXXXXXXXXXX */
948     ias = si->issuer_and_serial;
949
950     x509 = X509_find_by_issuer_and_serial(cert, ias->issuer, ias->serial);
951
952     /* were we able to find the cert in passed to us */
953     if (x509 == NULL) {
954         PKCS7err(PKCS7_F_PKCS7_DATAVERIFY,
955                  PKCS7_R_UNABLE_TO_FIND_CERTIFICATE);
956         goto err;
957     }
958
959     /* Lets verify */
960     if (!X509_STORE_CTX_init(ctx, cert_store, x509, cert)) {
961         PKCS7err(PKCS7_F_PKCS7_DATAVERIFY, ERR_R_X509_LIB);
962         goto err;
963     }
964     X509_STORE_CTX_set_purpose(ctx, X509_PURPOSE_SMIME_SIGN);
965     i = X509_verify_cert(ctx);
966     if (i <= 0) {
967         PKCS7err(PKCS7_F_PKCS7_DATAVERIFY, ERR_R_X509_LIB);
968         X509_STORE_CTX_cleanup(ctx);
969         goto err;
970     }
971     X509_STORE_CTX_cleanup(ctx);
972
973     return PKCS7_signatureVerify(bio, p7, si, x509);
974  err:
975     return ret;
976 }
977
978 int PKCS7_signatureVerify(BIO *bio, PKCS7 *p7, PKCS7_SIGNER_INFO *si,
979                           X509 *x509)
980 {
981     ASN1_OCTET_STRING *os;
982     EVP_MD_CTX *mdc_tmp, *mdc;
983     int ret = 0, i;
984     int md_type;
985     STACK_OF(X509_ATTRIBUTE) *sk;
986     BIO *btmp;
987     EVP_PKEY *pkey;
988
989     mdc_tmp = EVP_MD_CTX_new();
990     if (mdc_tmp == NULL) {
991         PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, ERR_R_MALLOC_FAILURE);
992         goto err;
993     }
994
995     if (!PKCS7_type_is_signed(p7) && !PKCS7_type_is_signedAndEnveloped(p7)) {
996         PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, PKCS7_R_WRONG_PKCS7_TYPE);
997         goto err;
998     }
999
1000     md_type = OBJ_obj2nid(si->digest_alg->algorithm);
1001
1002     btmp = bio;
1003     for (;;) {
1004         if ((btmp == NULL) ||
1005             ((btmp = BIO_find_type(btmp, BIO_TYPE_MD)) == NULL)) {
1006             PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY,
1007                      PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);
1008             goto err;
1009         }
1010         BIO_get_md_ctx(btmp, &mdc);
1011         if (mdc == NULL) {
1012             PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, ERR_R_INTERNAL_ERROR);
1013             goto err;
1014         }
1015         if (EVP_MD_CTX_type(mdc) == md_type)
1016             break;
1017         /*
1018          * Workaround for some broken clients that put the signature OID
1019          * instead of the digest OID in digest_alg->algorithm
1020          */
1021         if (EVP_MD_pkey_type(EVP_MD_CTX_md(mdc)) == md_type)
1022             break;
1023         btmp = BIO_next(btmp);
1024     }
1025
1026     /*
1027      * mdc is the digest ctx that we want, unless there are attributes, in
1028      * which case the digest is the signed attributes
1029      */
1030     if (!EVP_MD_CTX_copy_ex(mdc_tmp, mdc))
1031         goto err;
1032
1033     sk = si->auth_attr;
1034     if ((sk != NULL) && (sk_X509_ATTRIBUTE_num(sk) != 0)) {
1035         unsigned char md_dat[EVP_MAX_MD_SIZE], *abuf = NULL;
1036         unsigned int md_len;
1037         int alen;
1038         ASN1_OCTET_STRING *message_digest;
1039
1040         if (!EVP_DigestFinal_ex(mdc_tmp, md_dat, &md_len))
1041             goto err;
1042         message_digest = PKCS7_digest_from_attributes(sk);
1043         if (!message_digest) {
1044             PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY,
1045                      PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);
1046             goto err;
1047         }
1048         if ((message_digest->length != (int)md_len) ||
1049             (memcmp(message_digest->data, md_dat, md_len))) {
1050             PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, PKCS7_R_DIGEST_FAILURE);
1051             ret = -1;
1052             goto err;
1053         }
1054
1055         if (!EVP_VerifyInit_ex(mdc_tmp, EVP_get_digestbynid(md_type), NULL))
1056             goto err;
1057
1058         alen = ASN1_item_i2d((ASN1_VALUE *)sk, &abuf,
1059                              ASN1_ITEM_rptr(PKCS7_ATTR_VERIFY));
1060         if (alen <= 0) {
1061             PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, ERR_R_ASN1_LIB);
1062             ret = -1;
1063             goto err;
1064         }
1065         if (!EVP_VerifyUpdate(mdc_tmp, abuf, alen))
1066             goto err;
1067
1068         OPENSSL_free(abuf);
1069     }
1070
1071     os = si->enc_digest;
1072     pkey = X509_get0_pubkey(x509);
1073     if (pkey == NULL) {
1074         ret = -1;
1075         goto err;
1076     }
1077
1078     i = EVP_VerifyFinal(mdc_tmp, os->data, os->length, pkey);
1079     if (i <= 0) {
1080         PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, PKCS7_R_SIGNATURE_FAILURE);
1081         ret = -1;
1082         goto err;
1083     }
1084     ret = 1;
1085  err:
1086     EVP_MD_CTX_free(mdc_tmp);
1087     return ret;
1088 }
1089
1090 PKCS7_ISSUER_AND_SERIAL *PKCS7_get_issuer_and_serial(PKCS7 *p7, int idx)
1091 {
1092     STACK_OF(PKCS7_RECIP_INFO) *rsk;
1093     PKCS7_RECIP_INFO *ri;
1094     int i;
1095
1096     i = OBJ_obj2nid(p7->type);
1097     if (i != NID_pkcs7_signedAndEnveloped)
1098         return NULL;
1099     if (p7->d.signed_and_enveloped == NULL)
1100         return NULL;
1101     rsk = p7->d.signed_and_enveloped->recipientinfo;
1102     if (rsk == NULL)
1103         return NULL;
1104     if (sk_PKCS7_RECIP_INFO_num(rsk) <= idx)
1105         return NULL;
1106     ri = sk_PKCS7_RECIP_INFO_value(rsk, idx);
1107     return ri->issuer_and_serial;
1108 }
1109
1110 ASN1_TYPE *PKCS7_get_signed_attribute(PKCS7_SIGNER_INFO *si, int nid)
1111 {
1112     return get_attribute(si->auth_attr, nid);
1113 }
1114
1115 ASN1_TYPE *PKCS7_get_attribute(PKCS7_SIGNER_INFO *si, int nid)
1116 {
1117     return get_attribute(si->unauth_attr, nid);
1118 }
1119
1120 static ASN1_TYPE *get_attribute(STACK_OF(X509_ATTRIBUTE) *sk, int nid)
1121 {
1122     int idx;
1123     X509_ATTRIBUTE *xa;
1124     idx = X509at_get_attr_by_NID(sk, nid, -1);
1125     xa = X509at_get_attr(sk, idx);
1126     return X509_ATTRIBUTE_get0_type(xa, 0);
1127 }
1128
1129 ASN1_OCTET_STRING *PKCS7_digest_from_attributes(STACK_OF(X509_ATTRIBUTE) *sk)
1130 {
1131     ASN1_TYPE *astype;
1132     if ((astype = get_attribute(sk, NID_pkcs9_messageDigest)) == NULL)
1133         return NULL;
1134     return astype->value.octet_string;
1135 }
1136
1137 int PKCS7_set_signed_attributes(PKCS7_SIGNER_INFO *p7si,
1138                                 STACK_OF(X509_ATTRIBUTE) *sk)
1139 {
1140     int i;
1141
1142     sk_X509_ATTRIBUTE_pop_free(p7si->auth_attr, X509_ATTRIBUTE_free);
1143     p7si->auth_attr = sk_X509_ATTRIBUTE_dup(sk);
1144     if (p7si->auth_attr == NULL)
1145         return 0;
1146     for (i = 0; i < sk_X509_ATTRIBUTE_num(sk); i++) {
1147         if ((sk_X509_ATTRIBUTE_set(p7si->auth_attr, i,
1148                                    X509_ATTRIBUTE_dup(sk_X509_ATTRIBUTE_value
1149                                                       (sk, i))))
1150             == NULL)
1151             return 0;
1152     }
1153     return 1;
1154 }
1155
1156 int PKCS7_set_attributes(PKCS7_SIGNER_INFO *p7si,
1157                          STACK_OF(X509_ATTRIBUTE) *sk)
1158 {
1159     int i;
1160
1161     sk_X509_ATTRIBUTE_pop_free(p7si->unauth_attr, X509_ATTRIBUTE_free);
1162     p7si->unauth_attr = sk_X509_ATTRIBUTE_dup(sk);
1163     if (p7si->unauth_attr == NULL)
1164         return 0;
1165     for (i = 0; i < sk_X509_ATTRIBUTE_num(sk); i++) {
1166         if ((sk_X509_ATTRIBUTE_set(p7si->unauth_attr, i,
1167                                    X509_ATTRIBUTE_dup(sk_X509_ATTRIBUTE_value
1168                                                       (sk, i))))
1169             == NULL)
1170             return 0;
1171     }
1172     return 1;
1173 }
1174
1175 int PKCS7_add_signed_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int atrtype,
1176                                void *value)
1177 {
1178     return add_attribute(&(p7si->auth_attr), nid, atrtype, value);
1179 }
1180
1181 int PKCS7_add_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int atrtype,
1182                         void *value)
1183 {
1184     return add_attribute(&(p7si->unauth_attr), nid, atrtype, value);
1185 }
1186
1187 static int add_attribute(STACK_OF(X509_ATTRIBUTE) **sk, int nid, int atrtype,
1188                          void *value)
1189 {
1190     X509_ATTRIBUTE *attr = NULL;
1191
1192     if (*sk == NULL) {
1193         if ((*sk = sk_X509_ATTRIBUTE_new_null()) == NULL)
1194             return 0;
1195  new_attrib:
1196         if ((attr = X509_ATTRIBUTE_create(nid, atrtype, value)) == NULL)
1197             return 0;
1198         if (!sk_X509_ATTRIBUTE_push(*sk, attr)) {
1199             X509_ATTRIBUTE_free(attr);
1200             return 0;
1201         }
1202     } else {
1203         int i;
1204
1205         for (i = 0; i < sk_X509_ATTRIBUTE_num(*sk); i++) {
1206             attr = sk_X509_ATTRIBUTE_value(*sk, i);
1207             if (OBJ_obj2nid(X509_ATTRIBUTE_get0_object(attr)) == nid) {
1208                 X509_ATTRIBUTE_free(attr);
1209                 attr = X509_ATTRIBUTE_create(nid, atrtype, value);
1210                 if (attr == NULL)
1211                     return 0;
1212                 if (!sk_X509_ATTRIBUTE_set(*sk, i, attr)) {
1213                     X509_ATTRIBUTE_free(attr);
1214                     return 0;
1215                 }
1216                 goto end;
1217             }
1218         }
1219         goto new_attrib;
1220     }
1221  end:
1222     return 1;
1223 }