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