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