]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/pkcs7/pk7_doit.c
c9e4d719aa5099128b624f2a9e8656835f859af2
[thirdparty/openssl.git] / crypto / pkcs7 / pk7_doit.c
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 "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 static int add_attribute(STACK_OF(X509_ATTRIBUTE) **sk, int nid, int atrtype,
20 void *value);
21 static ASN1_TYPE *get_attribute(STACK_OF(X509_ATTRIBUTE) *sk, int nid);
22
23 int PKCS7_type_is_other(PKCS7 *p7)
24 {
25 int isOther = 1;
26
27 int nid = OBJ_obj2nid(p7->type);
28
29 switch (nid) {
30 case NID_pkcs7_data:
31 case NID_pkcs7_signed:
32 case NID_pkcs7_enveloped:
33 case NID_pkcs7_signedAndEnveloped:
34 case NID_pkcs7_digest:
35 case NID_pkcs7_encrypted:
36 isOther = 0;
37 break;
38 default:
39 isOther = 1;
40 }
41
42 return isOther;
43
44 }
45
46 ASN1_OCTET_STRING *PKCS7_get_octet_string(PKCS7 *p7)
47 {
48 if (PKCS7_type_is_data(p7))
49 return p7->d.data;
50 if (PKCS7_type_is_other(p7) && p7->d.other
51 && (p7->d.other->type == V_ASN1_OCTET_STRING))
52 return p7->d.other->value.octet_string;
53 return NULL;
54 }
55
56 static int pkcs7_bio_add_digest(BIO **pbio, X509_ALGOR *alg,
57 const PKCS7_CTX *ctx)
58 {
59 BIO *btmp;
60 const char *name;
61 EVP_MD *fetched = NULL;
62 const EVP_MD *md;
63
64 if ((btmp = BIO_new(BIO_f_md())) == NULL) {
65 ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB);
66 goto err;
67 }
68
69 name = OBJ_nid2sn(OBJ_obj2nid(alg->algorithm));
70
71 (void)ERR_set_mark();
72 fetched = EVP_MD_fetch(ossl_pkcs7_ctx_get0_libctx(ctx), name,
73 ossl_pkcs7_ctx_get0_propq(ctx));
74 if (fetched != NULL)
75 md = fetched;
76 else
77 md = EVP_get_digestbyname(name);
78
79 if (md == NULL) {
80 (void)ERR_clear_last_mark();
81 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNKNOWN_DIGEST_TYPE);
82 goto err;
83 }
84 (void)ERR_pop_to_mark();
85
86 BIO_set_md(btmp, md);
87 EVP_MD_free(fetched);
88 if (*pbio == NULL)
89 *pbio = btmp;
90 else if (!BIO_push(*pbio, btmp)) {
91 ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB);
92 goto err;
93 }
94 btmp = NULL;
95
96 return 1;
97
98 err:
99 BIO_free(btmp);
100 return 0;
101 }
102
103 static int pkcs7_encode_rinfo(PKCS7_RECIP_INFO *ri,
104 unsigned char *key, int keylen)
105 {
106 EVP_PKEY_CTX *pctx = NULL;
107 EVP_PKEY *pkey = NULL;
108 unsigned char *ek = NULL;
109 int ret = 0;
110 size_t eklen;
111 const PKCS7_CTX *ctx = ri->ctx;
112
113 pkey = X509_get0_pubkey(ri->cert);
114 if (pkey == NULL)
115 return 0;
116
117 pctx = EVP_PKEY_CTX_new_from_pkey(ossl_pkcs7_ctx_get0_libctx(ctx), pkey,
118 ossl_pkcs7_ctx_get0_propq(ctx));
119 if (pctx == NULL)
120 return 0;
121
122 if (EVP_PKEY_encrypt_init(pctx) <= 0)
123 goto err;
124
125 if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_ENCRYPT,
126 EVP_PKEY_CTRL_PKCS7_ENCRYPT, 0, ri) <= 0) {
127 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_CTRL_ERROR);
128 goto err;
129 }
130
131 if (EVP_PKEY_encrypt(pctx, NULL, &eklen, key, keylen) <= 0)
132 goto err;
133
134 ek = OPENSSL_malloc(eklen);
135
136 if (ek == NULL) {
137 ERR_raise(ERR_LIB_PKCS7, ERR_R_MALLOC_FAILURE);
138 goto err;
139 }
140
141 if (EVP_PKEY_encrypt(pctx, ek, &eklen, key, keylen) <= 0)
142 goto err;
143
144 ASN1_STRING_set0(ri->enc_key, ek, eklen);
145 ek = NULL;
146
147 ret = 1;
148
149 err:
150 EVP_PKEY_CTX_free(pctx);
151 OPENSSL_free(ek);
152 return ret;
153
154 }
155
156 static int pkcs7_decrypt_rinfo(unsigned char **pek, int *peklen,
157 PKCS7_RECIP_INFO *ri, EVP_PKEY *pkey,
158 size_t fixlen)
159 {
160 EVP_PKEY_CTX *pctx = NULL;
161 unsigned char *ek = NULL;
162 size_t eklen;
163 int ret = -1;
164 const PKCS7_CTX *ctx = ri->ctx;
165
166 pctx = EVP_PKEY_CTX_new_from_pkey(ossl_pkcs7_ctx_get0_libctx(ctx), pkey,
167 ossl_pkcs7_ctx_get0_propq(ctx));
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 ERR_raise(ERR_LIB_PKCS7, 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 ERR_raise(ERR_LIB_PKCS7, 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 ERR_raise(ERR_LIB_PKCS7, 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 OSSL_LIB_CTX *libctx;
229 const char *propq;
230
231 if (p7 == NULL) {
232 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_INVALID_NULL_POINTER);
233 return NULL;
234 }
235 p7_ctx = ossl_pkcs7_get0_ctx(p7);
236 libctx = ossl_pkcs7_ctx_get0_libctx(p7_ctx);
237 propq = ossl_pkcs7_ctx_get0_propq(p7_ctx);
238
239 /*
240 * The content field in the PKCS7 ContentInfo is optional, but that really
241 * only applies to inner content (precisely, detached signatures).
242 *
243 * When reading content, missing outer content is therefore treated as an
244 * error.
245 *
246 * When creating content, PKCS7_content_new() must be called before
247 * calling this method, so a NULL p7->d is always an error.
248 */
249 if (p7->d.ptr == NULL) {
250 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_CONTENT);
251 return NULL;
252 }
253
254 i = OBJ_obj2nid(p7->type);
255 p7->state = PKCS7_S_HEADER;
256
257 switch (i) {
258 case NID_pkcs7_signed:
259 md_sk = p7->d.sign->md_algs;
260 os = PKCS7_get_octet_string(p7->d.sign->contents);
261 break;
262 case NID_pkcs7_signedAndEnveloped:
263 rsk = p7->d.signed_and_enveloped->recipientinfo;
264 md_sk = p7->d.signed_and_enveloped->md_algs;
265 xalg = p7->d.signed_and_enveloped->enc_data->algorithm;
266 evp_cipher = p7->d.signed_and_enveloped->enc_data->cipher;
267 if (evp_cipher == NULL) {
268 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_CIPHER_NOT_INITIALIZED);
269 goto err;
270 }
271 break;
272 case NID_pkcs7_enveloped:
273 rsk = p7->d.enveloped->recipientinfo;
274 xalg = p7->d.enveloped->enc_data->algorithm;
275 evp_cipher = p7->d.enveloped->enc_data->cipher;
276 if (evp_cipher == NULL) {
277 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_CIPHER_NOT_INITIALIZED);
278 goto err;
279 }
280 break;
281 case NID_pkcs7_digest:
282 xa = p7->d.digest->md;
283 os = PKCS7_get_octet_string(p7->d.digest->contents);
284 break;
285 case NID_pkcs7_data:
286 break;
287 default:
288 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
289 goto err;
290 }
291
292 for (i = 0; i < sk_X509_ALGOR_num(md_sk); i++)
293 if (!pkcs7_bio_add_digest(&out, sk_X509_ALGOR_value(md_sk, i), p7_ctx))
294 goto err;
295
296 if (xa && !pkcs7_bio_add_digest(&out, xa, p7_ctx))
297 goto err;
298
299 if (evp_cipher != NULL) {
300 unsigned char key[EVP_MAX_KEY_LENGTH];
301 unsigned char iv[EVP_MAX_IV_LENGTH];
302 int keylen, ivlen;
303 EVP_CIPHER_CTX *ctx;
304
305 if ((btmp = BIO_new(BIO_f_cipher())) == NULL) {
306 ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB);
307 goto err;
308 }
309 BIO_get_cipher_ctx(btmp, &ctx);
310 keylen = EVP_CIPHER_key_length(evp_cipher);
311 ivlen = EVP_CIPHER_iv_length(evp_cipher);
312 xalg->algorithm = OBJ_nid2obj(EVP_CIPHER_type(evp_cipher));
313 if (ivlen > 0)
314 if (RAND_bytes_ex(libctx, iv, ivlen) <= 0)
315 goto err;
316
317 (void)ERR_set_mark();
318 fetched_cipher = EVP_CIPHER_fetch(libctx,
319 EVP_CIPHER_name(evp_cipher),
320 propq);
321 (void)ERR_pop_to_mark();
322 if (fetched_cipher != NULL)
323 cipher = fetched_cipher;
324 else
325 cipher = evp_cipher;
326
327 if (EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, 1) <= 0)
328 goto err;
329
330 EVP_CIPHER_free(fetched_cipher);
331 fetched_cipher = NULL;
332
333 if (EVP_CIPHER_CTX_rand_key(ctx, key) <= 0)
334 goto err;
335 if (EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, 1) <= 0)
336 goto err;
337
338 if (ivlen > 0) {
339 if (xalg->parameter == NULL) {
340 xalg->parameter = ASN1_TYPE_new();
341 if (xalg->parameter == NULL)
342 goto err;
343 }
344 if (EVP_CIPHER_param_to_asn1(ctx, xalg->parameter) < 0)
345 goto err;
346 }
347
348 /* Lets do the pub key stuff :-) */
349 for (i = 0; i < sk_PKCS7_RECIP_INFO_num(rsk); i++) {
350 ri = sk_PKCS7_RECIP_INFO_value(rsk, i);
351 if (pkcs7_encode_rinfo(ri, key, keylen) <= 0)
352 goto err;
353 }
354 OPENSSL_cleanse(key, keylen);
355
356 if (out == NULL)
357 out = btmp;
358 else
359 BIO_push(out, btmp);
360 btmp = NULL;
361 }
362
363 if (bio == NULL) {
364 if (PKCS7_is_detached(p7)) {
365 bio = BIO_new(BIO_s_null());
366 } else if (os && os->length > 0) {
367 bio = BIO_new_mem_buf(os->data, os->length);
368 } else {
369 bio = BIO_new(BIO_s_mem());
370 if (bio == NULL)
371 goto err;
372 BIO_set_mem_eof_return(bio, 0);
373 }
374 if (bio == NULL)
375 goto err;
376 }
377 if (out)
378 BIO_push(out, bio);
379 else
380 out = bio;
381 return out;
382
383 err:
384 EVP_CIPHER_free(fetched_cipher);
385 BIO_free_all(out);
386 BIO_free_all(btmp);
387 return NULL;
388 }
389
390 static int pkcs7_cmp_ri(PKCS7_RECIP_INFO *ri, X509 *pcert)
391 {
392 int ret;
393 ret = X509_NAME_cmp(ri->issuer_and_serial->issuer,
394 X509_get_issuer_name(pcert));
395 if (ret)
396 return ret;
397 return ASN1_INTEGER_cmp(X509_get0_serialNumber(pcert),
398 ri->issuer_and_serial->serial);
399 }
400
401 /* int */
402 BIO *PKCS7_dataDecode(PKCS7 *p7, EVP_PKEY *pkey, BIO *in_bio, X509 *pcert)
403 {
404 int i, len;
405 BIO *out = NULL, *btmp = NULL, *etmp = NULL, *bio = NULL;
406 X509_ALGOR *xa;
407 ASN1_OCTET_STRING *data_body = NULL;
408 EVP_MD *evp_md = NULL;
409 const EVP_MD *md;
410 EVP_CIPHER *evp_cipher = NULL;
411 const EVP_CIPHER *cipher = NULL;
412 EVP_CIPHER_CTX *evp_ctx = NULL;
413 X509_ALGOR *enc_alg = NULL;
414 STACK_OF(X509_ALGOR) *md_sk = NULL;
415 STACK_OF(PKCS7_RECIP_INFO) *rsk = NULL;
416 PKCS7_RECIP_INFO *ri = NULL;
417 unsigned char *ek = NULL, *tkey = NULL;
418 int eklen = 0, tkeylen = 0;
419 const char *name;
420 const PKCS7_CTX *p7_ctx;
421 OSSL_LIB_CTX *libctx;
422 const char *propq;
423
424 if (p7 == NULL) {
425 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_INVALID_NULL_POINTER);
426 return NULL;
427 }
428
429 p7_ctx = ossl_pkcs7_get0_ctx(p7);
430 libctx = ossl_pkcs7_ctx_get0_libctx(p7_ctx);
431 propq = ossl_pkcs7_ctx_get0_propq(p7_ctx);
432
433 if (p7->d.ptr == NULL) {
434 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_CONTENT);
435 return NULL;
436 }
437
438 i = OBJ_obj2nid(p7->type);
439 p7->state = PKCS7_S_HEADER;
440
441 switch (i) {
442 case NID_pkcs7_signed:
443 /*
444 * p7->d.sign->contents is a PKCS7 structure consisting of a contentType
445 * field and optional content.
446 * data_body is NULL if that structure has no (=detached) content
447 * or if the contentType is wrong (i.e., not "data").
448 */
449 data_body = PKCS7_get_octet_string(p7->d.sign->contents);
450 if (!PKCS7_is_detached(p7) && data_body == NULL) {
451 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_INVALID_SIGNED_DATA_TYPE);
452 goto err;
453 }
454 md_sk = p7->d.sign->md_algs;
455 break;
456 case NID_pkcs7_signedAndEnveloped:
457 rsk = p7->d.signed_and_enveloped->recipientinfo;
458 md_sk = p7->d.signed_and_enveloped->md_algs;
459 /* data_body is NULL if the optional EncryptedContent is missing. */
460 data_body = p7->d.signed_and_enveloped->enc_data->enc_data;
461 enc_alg = p7->d.signed_and_enveloped->enc_data->algorithm;
462
463 name = OBJ_nid2sn(OBJ_obj2nid(enc_alg->algorithm));
464
465 (void)ERR_set_mark();
466 evp_cipher = EVP_CIPHER_fetch(libctx, name, propq);
467 if (evp_cipher != NULL)
468 cipher = evp_cipher;
469 else
470 cipher = EVP_get_cipherbyname(name);
471
472 if (cipher == NULL) {
473 (void)ERR_clear_last_mark();
474 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNSUPPORTED_CIPHER_TYPE);
475 goto err;
476 }
477 (void)ERR_pop_to_mark();
478 break;
479 case NID_pkcs7_enveloped:
480 rsk = p7->d.enveloped->recipientinfo;
481 enc_alg = p7->d.enveloped->enc_data->algorithm;
482 /* data_body is NULL if the optional EncryptedContent is missing. */
483 data_body = p7->d.enveloped->enc_data->enc_data;
484 name = OBJ_nid2sn(OBJ_obj2nid(enc_alg->algorithm));
485
486 (void)ERR_set_mark();
487 evp_cipher = EVP_CIPHER_fetch(libctx, name, propq);
488 if (evp_cipher != NULL)
489 cipher = evp_cipher;
490 else
491 cipher = EVP_get_cipherbyname(name);
492
493 if (cipher == NULL) {
494 (void)ERR_clear_last_mark();
495 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNSUPPORTED_CIPHER_TYPE);
496 goto err;
497 }
498 (void)ERR_pop_to_mark();
499 break;
500 default:
501 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
502 goto err;
503 }
504
505 /* Detached content must be supplied via in_bio instead. */
506 if (data_body == NULL && in_bio == NULL) {
507 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_CONTENT);
508 goto err;
509 }
510
511 /* We will be checking the signature */
512 if (md_sk != NULL) {
513 for (i = 0; i < sk_X509_ALGOR_num(md_sk); i++) {
514 xa = sk_X509_ALGOR_value(md_sk, i);
515 if ((btmp = BIO_new(BIO_f_md())) == NULL) {
516 ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB);
517 goto err;
518 }
519
520 name = OBJ_nid2sn(OBJ_obj2nid(xa->algorithm));
521
522 (void)ERR_set_mark();
523 evp_md = EVP_MD_fetch(libctx, name, propq);
524 if (evp_md != NULL)
525 md = evp_md;
526 else
527 md = EVP_get_digestbyname(name);
528
529 if (md == NULL) {
530 (void)ERR_clear_last_mark();
531 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNKNOWN_DIGEST_TYPE);
532 goto err;
533 }
534 (void)ERR_pop_to_mark();
535
536 BIO_set_md(btmp, md);
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_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_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_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)) {
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
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_type(*pmd) == nid)
694 return bio;
695 bio = BIO_next(bio);
696 }
697 return NULL;
698 }
699
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
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_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
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_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 /*
936 * TODO(3.0): This causes problems when providers are in use, so disabled
937 * for now. Can we get rid of this completely? AFAICT this ctrl has never
938 * been used since it was first put in. All internal implementations just
939 * return 1 and ignore this ctrl and have always done so by the looks of
940 * things. To fix this we could convert this ctrl into a param, which would
941 * require us to send all the signer info data as a set of params...but that
942 * is non-trivial and since this isn't used by anything it may be better
943 * just to remove it. The original commit that added it had this
944 * justification in CHANGES:
945 *
946 * "During PKCS7 signing pass the PKCS7 SignerInfo structure to the
947 * EVP_PKEY_METHOD before and after signing via the
948 * EVP_PKEY_CTRL_PKCS7_SIGN ctrl. It can then customise the structure
949 * before and/or after signing if necessary."
950 */
951 #if 0
952 if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN,
953 EVP_PKEY_CTRL_PKCS7_SIGN, 0, si) <= 0) {
954 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_CTRL_ERROR);
955 goto err;
956 }
957 #endif
958
959 alen = ASN1_item_i2d((ASN1_VALUE *)si->auth_attr, &abuf,
960 ASN1_ITEM_rptr(PKCS7_ATTR_SIGN));
961 if (!abuf)
962 goto err;
963 if (EVP_DigestSignUpdate(mctx, abuf, alen) <= 0)
964 goto err;
965 OPENSSL_free(abuf);
966 abuf = NULL;
967 if (EVP_DigestSignFinal(mctx, NULL, &siglen) <= 0)
968 goto err;
969 abuf = OPENSSL_malloc(siglen);
970 if (abuf == NULL)
971 goto err;
972 if (EVP_DigestSignFinal(mctx, abuf, &siglen) <= 0)
973 goto err;
974
975 /*
976 * TODO(3.0): This causes problems when providers are in use, so disabled
977 * for now. Can we get rid of this completely? AFAICT this ctrl has never
978 * been used since it was first put in. All internal implementations just
979 * return 1 and ignore this ctrl and have always done so by the looks of
980 * things. To fix this we could convert this ctrl into a param, which would
981 * require us to send all the signer info data as a set of params...but that
982 * is non-trivial and since this isn't used by anything it may be better
983 * just to remove it. The original commit that added it had this
984 * justification in CHANGES:
985 *
986 * "During PKCS7 signing pass the PKCS7 SignerInfo structure to the
987 * EVP_PKEY_METHOD before and after signing via the
988 * EVP_PKEY_CTRL_PKCS7_SIGN ctrl. It can then customise the structure
989 * before and/or after signing if necessary."
990 */
991 #if 0
992 if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN,
993 EVP_PKEY_CTRL_PKCS7_SIGN, 1, si) <= 0) {
994 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_CTRL_ERROR);
995 goto err;
996 }
997 #endif
998
999 EVP_MD_CTX_free(mctx);
1000
1001 ASN1_STRING_set0(si->enc_digest, abuf, siglen);
1002
1003 return 1;
1004
1005 err:
1006 OPENSSL_free(abuf);
1007 EVP_MD_CTX_free(mctx);
1008 return 0;
1009 }
1010
1011 int PKCS7_dataVerify(X509_STORE *cert_store, X509_STORE_CTX *ctx, BIO *bio,
1012 PKCS7 *p7, PKCS7_SIGNER_INFO *si)
1013 {
1014 PKCS7_ISSUER_AND_SERIAL *ias;
1015 int ret = 0, i;
1016 STACK_OF(X509) *cert;
1017 X509 *x509;
1018
1019 if (p7 == NULL) {
1020 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_INVALID_NULL_POINTER);
1021 return 0;
1022 }
1023
1024 if (p7->d.ptr == NULL) {
1025 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_CONTENT);
1026 return 0;
1027 }
1028
1029 if (PKCS7_type_is_signed(p7)) {
1030 cert = p7->d.sign->cert;
1031 } else if (PKCS7_type_is_signedAndEnveloped(p7)) {
1032 cert = p7->d.signed_and_enveloped->cert;
1033 } else {
1034 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_WRONG_PKCS7_TYPE);
1035 goto err;
1036 }
1037 /* XXXXXXXXXXXXXXXXXXXXXXX */
1038 ias = si->issuer_and_serial;
1039
1040 x509 = X509_find_by_issuer_and_serial(cert, ias->issuer, ias->serial);
1041
1042 /* were we able to find the cert in passed to us */
1043 if (x509 == NULL) {
1044 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNABLE_TO_FIND_CERTIFICATE);
1045 goto err;
1046 }
1047
1048 /* Lets verify */
1049 if (!X509_STORE_CTX_init(ctx, cert_store, x509, cert)) {
1050 ERR_raise(ERR_LIB_PKCS7, ERR_R_X509_LIB);
1051 goto err;
1052 }
1053 X509_STORE_CTX_set_purpose(ctx, X509_PURPOSE_SMIME_SIGN);
1054 i = X509_verify_cert(ctx);
1055 if (i <= 0) {
1056 ERR_raise(ERR_LIB_PKCS7, ERR_R_X509_LIB);
1057 goto err;
1058 }
1059
1060 return PKCS7_signatureVerify(bio, p7, si, x509);
1061 err:
1062 return ret;
1063 }
1064
1065 int PKCS7_signatureVerify(BIO *bio, PKCS7 *p7, PKCS7_SIGNER_INFO *si,
1066 X509 *x509)
1067 {
1068 ASN1_OCTET_STRING *os;
1069 EVP_MD_CTX *mdc_tmp, *mdc;
1070 const EVP_MD *md;
1071 EVP_MD *fetched_md = NULL;
1072 int ret = 0, i;
1073 int md_type;
1074 STACK_OF(X509_ATTRIBUTE) *sk;
1075 BIO *btmp;
1076 EVP_PKEY *pkey;
1077 const PKCS7_CTX *ctx = ossl_pkcs7_get0_ctx(p7);
1078 OSSL_LIB_CTX *libctx = ossl_pkcs7_ctx_get0_libctx(ctx);
1079 const char *propq = ossl_pkcs7_ctx_get0_propq(ctx);
1080
1081 mdc_tmp = EVP_MD_CTX_new();
1082 if (mdc_tmp == NULL) {
1083 ERR_raise(ERR_LIB_PKCS7, ERR_R_MALLOC_FAILURE);
1084 goto err;
1085 }
1086
1087 if (!PKCS7_type_is_signed(p7) && !PKCS7_type_is_signedAndEnveloped(p7)) {
1088 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_WRONG_PKCS7_TYPE);
1089 goto err;
1090 }
1091
1092 md_type = OBJ_obj2nid(si->digest_alg->algorithm);
1093
1094 btmp = bio;
1095 for (;;) {
1096 if ((btmp == NULL) ||
1097 ((btmp = BIO_find_type(btmp, BIO_TYPE_MD)) == NULL)) {
1098 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);
1099 goto err;
1100 }
1101 BIO_get_md_ctx(btmp, &mdc);
1102 if (mdc == NULL) {
1103 ERR_raise(ERR_LIB_PKCS7, ERR_R_INTERNAL_ERROR);
1104 goto err;
1105 }
1106 if (EVP_MD_CTX_type(mdc) == md_type)
1107 break;
1108 /*
1109 * Workaround for some broken clients that put the signature OID
1110 * instead of the digest OID in digest_alg->algorithm
1111 */
1112 if (EVP_MD_pkey_type(EVP_MD_CTX_md(mdc)) == md_type)
1113 break;
1114 btmp = BIO_next(btmp);
1115 }
1116
1117 /*
1118 * mdc is the digest ctx that we want, unless there are attributes, in
1119 * which case the digest is the signed attributes
1120 */
1121 if (!EVP_MD_CTX_copy_ex(mdc_tmp, mdc))
1122 goto err;
1123
1124 sk = si->auth_attr;
1125 if ((sk != NULL) && (sk_X509_ATTRIBUTE_num(sk) != 0)) {
1126 unsigned char md_dat[EVP_MAX_MD_SIZE], *abuf = NULL;
1127 unsigned int md_len;
1128 int alen;
1129 ASN1_OCTET_STRING *message_digest;
1130
1131 if (!EVP_DigestFinal_ex(mdc_tmp, md_dat, &md_len))
1132 goto err;
1133 message_digest = PKCS7_digest_from_attributes(sk);
1134 if (!message_digest) {
1135 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);
1136 goto err;
1137 }
1138 if ((message_digest->length != (int)md_len) ||
1139 (memcmp(message_digest->data, md_dat, md_len))) {
1140 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_DIGEST_FAILURE);
1141 ret = -1;
1142 goto err;
1143 }
1144
1145 (void)ERR_set_mark();
1146 fetched_md = EVP_MD_fetch(libctx, OBJ_nid2sn(md_type), propq);
1147
1148 if (fetched_md != NULL)
1149 md = fetched_md;
1150 else
1151 md = EVP_get_digestbynid(md_type);
1152
1153 if (md == NULL || !EVP_VerifyInit_ex(mdc_tmp, md, NULL)) {
1154 (void)ERR_clear_last_mark();
1155 goto err;
1156 }
1157 (void)ERR_pop_to_mark();
1158
1159 alen = ASN1_item_i2d((ASN1_VALUE *)sk, &abuf,
1160 ASN1_ITEM_rptr(PKCS7_ATTR_VERIFY));
1161 if (alen <= 0) {
1162 ERR_raise(ERR_LIB_PKCS7, ERR_R_ASN1_LIB);
1163 ret = -1;
1164 goto err;
1165 }
1166 if (!EVP_VerifyUpdate(mdc_tmp, abuf, alen))
1167 goto err;
1168
1169 OPENSSL_free(abuf);
1170 }
1171
1172 os = si->enc_digest;
1173 pkey = X509_get0_pubkey(x509);
1174 if (pkey == NULL) {
1175 ret = -1;
1176 goto err;
1177 }
1178
1179 i = EVP_VerifyFinal_ex(mdc_tmp, os->data, os->length, pkey, libctx, propq);
1180 if (i <= 0) {
1181 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_SIGNATURE_FAILURE);
1182 ret = -1;
1183 goto err;
1184 }
1185 ret = 1;
1186 err:
1187 EVP_MD_CTX_free(mdc_tmp);
1188 EVP_MD_free(fetched_md);
1189 return ret;
1190 }
1191
1192 PKCS7_ISSUER_AND_SERIAL *PKCS7_get_issuer_and_serial(PKCS7 *p7, int idx)
1193 {
1194 STACK_OF(PKCS7_RECIP_INFO) *rsk;
1195 PKCS7_RECIP_INFO *ri;
1196 int i;
1197
1198 i = OBJ_obj2nid(p7->type);
1199 if (i != NID_pkcs7_signedAndEnveloped)
1200 return NULL;
1201 if (p7->d.signed_and_enveloped == NULL)
1202 return NULL;
1203 rsk = p7->d.signed_and_enveloped->recipientinfo;
1204 if (rsk == NULL)
1205 return NULL;
1206 if (sk_PKCS7_RECIP_INFO_num(rsk) <= idx)
1207 return NULL;
1208 ri = sk_PKCS7_RECIP_INFO_value(rsk, idx);
1209 return ri->issuer_and_serial;
1210 }
1211
1212 ASN1_TYPE *PKCS7_get_signed_attribute(PKCS7_SIGNER_INFO *si, int nid)
1213 {
1214 return get_attribute(si->auth_attr, nid);
1215 }
1216
1217 ASN1_TYPE *PKCS7_get_attribute(PKCS7_SIGNER_INFO *si, int nid)
1218 {
1219 return get_attribute(si->unauth_attr, nid);
1220 }
1221
1222 static ASN1_TYPE *get_attribute(STACK_OF(X509_ATTRIBUTE) *sk, int nid)
1223 {
1224 int idx;
1225 X509_ATTRIBUTE *xa;
1226 idx = X509at_get_attr_by_NID(sk, nid, -1);
1227 xa = X509at_get_attr(sk, idx);
1228 return X509_ATTRIBUTE_get0_type(xa, 0);
1229 }
1230
1231 ASN1_OCTET_STRING *PKCS7_digest_from_attributes(STACK_OF(X509_ATTRIBUTE) *sk)
1232 {
1233 ASN1_TYPE *astype;
1234 if ((astype = get_attribute(sk, NID_pkcs9_messageDigest)) == NULL)
1235 return NULL;
1236 return astype->value.octet_string;
1237 }
1238
1239 int PKCS7_set_signed_attributes(PKCS7_SIGNER_INFO *p7si,
1240 STACK_OF(X509_ATTRIBUTE) *sk)
1241 {
1242 int i;
1243
1244 sk_X509_ATTRIBUTE_pop_free(p7si->auth_attr, X509_ATTRIBUTE_free);
1245 p7si->auth_attr = sk_X509_ATTRIBUTE_dup(sk);
1246 if (p7si->auth_attr == NULL)
1247 return 0;
1248 for (i = 0; i < sk_X509_ATTRIBUTE_num(sk); i++) {
1249 if ((sk_X509_ATTRIBUTE_set(p7si->auth_attr, i,
1250 X509_ATTRIBUTE_dup(sk_X509_ATTRIBUTE_value
1251 (sk, i))))
1252 == NULL)
1253 return 0;
1254 }
1255 return 1;
1256 }
1257
1258 int PKCS7_set_attributes(PKCS7_SIGNER_INFO *p7si,
1259 STACK_OF(X509_ATTRIBUTE) *sk)
1260 {
1261 int i;
1262
1263 sk_X509_ATTRIBUTE_pop_free(p7si->unauth_attr, X509_ATTRIBUTE_free);
1264 p7si->unauth_attr = sk_X509_ATTRIBUTE_dup(sk);
1265 if (p7si->unauth_attr == NULL)
1266 return 0;
1267 for (i = 0; i < sk_X509_ATTRIBUTE_num(sk); i++) {
1268 if ((sk_X509_ATTRIBUTE_set(p7si->unauth_attr, i,
1269 X509_ATTRIBUTE_dup(sk_X509_ATTRIBUTE_value
1270 (sk, i))))
1271 == NULL)
1272 return 0;
1273 }
1274 return 1;
1275 }
1276
1277 int PKCS7_add_signed_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int atrtype,
1278 void *value)
1279 {
1280 return add_attribute(&(p7si->auth_attr), nid, atrtype, value);
1281 }
1282
1283 int PKCS7_add_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int atrtype,
1284 void *value)
1285 {
1286 return add_attribute(&(p7si->unauth_attr), nid, atrtype, value);
1287 }
1288
1289 static int add_attribute(STACK_OF(X509_ATTRIBUTE) **sk, int nid, int atrtype,
1290 void *value)
1291 {
1292 X509_ATTRIBUTE *attr = NULL;
1293
1294 if (*sk == NULL) {
1295 if ((*sk = sk_X509_ATTRIBUTE_new_null()) == NULL)
1296 return 0;
1297 new_attrib:
1298 if ((attr = X509_ATTRIBUTE_create(nid, atrtype, value)) == NULL)
1299 return 0;
1300 if (!sk_X509_ATTRIBUTE_push(*sk, attr)) {
1301 X509_ATTRIBUTE_free(attr);
1302 return 0;
1303 }
1304 } else {
1305 int i;
1306
1307 for (i = 0; i < sk_X509_ATTRIBUTE_num(*sk); i++) {
1308 attr = sk_X509_ATTRIBUTE_value(*sk, i);
1309 if (OBJ_obj2nid(X509_ATTRIBUTE_get0_object(attr)) == nid) {
1310 X509_ATTRIBUTE_free(attr);
1311 attr = X509_ATTRIBUTE_create(nid, atrtype, value);
1312 if (attr == NULL)
1313 return 0;
1314 if (!sk_X509_ATTRIBUTE_set(*sk, i, attr)) {
1315 X509_ATTRIBUTE_free(attr);
1316 return 0;
1317 }
1318 goto end;
1319 }
1320 }
1321 goto new_attrib;
1322 }
1323 end:
1324 return 1;
1325 }