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