]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/pkcs7/pk7_doit.c
PKCS7_dataVerify(): fix missing use of CRLs in PKCS#7 message
[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 <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
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
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
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
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
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
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
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 */
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
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
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
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
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
952 /* This partly overlaps with PKCS7_verify(). It does not support flags. */
953 int PKCS7_dataVerify(X509_STORE *cert_store, X509_STORE_CTX *ctx, BIO *bio,
954 PKCS7 *p7, PKCS7_SIGNER_INFO *si)
955 {
956 PKCS7_ISSUER_AND_SERIAL *ias;
957 int ret = 0, i;
958 STACK_OF(X509) *untrusted;
959 STACK_OF(X509_CRL) *crls;
960 X509 *signer;
961
962 if (p7 == NULL) {
963 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_INVALID_NULL_POINTER);
964 return 0;
965 }
966
967 if (p7->d.ptr == NULL) {
968 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_CONTENT);
969 return 0;
970 }
971
972 if (PKCS7_type_is_signed(p7)) {
973 untrusted = p7->d.sign->cert;
974 crls = p7->d.sign->crl;
975 } else if (PKCS7_type_is_signedAndEnveloped(p7)) {
976 untrusted = p7->d.signed_and_enveloped->cert;
977 crls = p7->d.signed_and_enveloped->crl;
978 } else {
979 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_WRONG_PKCS7_TYPE);
980 goto err;
981 }
982 X509_STORE_CTX_set0_crls(ctx, crls);
983
984 /* XXXXXXXXXXXXXXXXXXXXXXX */
985 ias = si->issuer_and_serial;
986
987 signer = X509_find_by_issuer_and_serial(untrusted, ias->issuer, ias->serial);
988
989 /* Were we able to find the signer certificate in passed to us? */
990 if (signer == NULL) {
991 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNABLE_TO_FIND_CERTIFICATE);
992 goto err;
993 }
994
995 /* Lets verify */
996 if (!X509_STORE_CTX_init(ctx, cert_store, signer, untrusted)) {
997 ERR_raise(ERR_LIB_PKCS7, ERR_R_X509_LIB);
998 goto err;
999 }
1000 X509_STORE_CTX_set_purpose(ctx, X509_PURPOSE_SMIME_SIGN);
1001 i = X509_verify_cert(ctx);
1002 if (i <= 0) {
1003 ERR_raise(ERR_LIB_PKCS7, ERR_R_X509_LIB);
1004 goto err;
1005 }
1006
1007 return PKCS7_signatureVerify(bio, p7, si, signer);
1008 err:
1009 return ret;
1010 }
1011
1012 int PKCS7_signatureVerify(BIO *bio, PKCS7 *p7, PKCS7_SIGNER_INFO *si,
1013 X509 *signer)
1014 {
1015 ASN1_OCTET_STRING *os;
1016 EVP_MD_CTX *mdc_tmp, *mdc;
1017 const EVP_MD *md;
1018 EVP_MD *fetched_md = NULL;
1019 int ret = 0, i;
1020 int md_type;
1021 STACK_OF(X509_ATTRIBUTE) *sk;
1022 BIO *btmp;
1023 EVP_PKEY *pkey;
1024 const PKCS7_CTX *ctx = ossl_pkcs7_get0_ctx(p7);
1025 OSSL_LIB_CTX *libctx = ossl_pkcs7_ctx_get0_libctx(ctx);
1026 const char *propq = ossl_pkcs7_ctx_get0_propq(ctx);
1027
1028 mdc_tmp = EVP_MD_CTX_new();
1029 if (mdc_tmp == NULL) {
1030 ERR_raise(ERR_LIB_PKCS7, ERR_R_MALLOC_FAILURE);
1031 goto err;
1032 }
1033
1034 if (!PKCS7_type_is_signed(p7) && !PKCS7_type_is_signedAndEnveloped(p7)) {
1035 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_WRONG_PKCS7_TYPE);
1036 goto err;
1037 }
1038
1039 md_type = OBJ_obj2nid(si->digest_alg->algorithm);
1040
1041 btmp = bio;
1042 for (;;) {
1043 if ((btmp == NULL) ||
1044 ((btmp = BIO_find_type(btmp, BIO_TYPE_MD)) == NULL)) {
1045 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);
1046 goto err;
1047 }
1048 BIO_get_md_ctx(btmp, &mdc);
1049 if (mdc == NULL) {
1050 ERR_raise(ERR_LIB_PKCS7, ERR_R_INTERNAL_ERROR);
1051 goto err;
1052 }
1053 if (EVP_MD_CTX_get_type(mdc) == md_type)
1054 break;
1055 /*
1056 * Workaround for some broken clients that put the signature OID
1057 * instead of the digest OID in digest_alg->algorithm
1058 */
1059 if (EVP_MD_get_pkey_type(EVP_MD_CTX_get0_md(mdc)) == md_type)
1060 break;
1061 btmp = BIO_next(btmp);
1062 }
1063
1064 /*
1065 * mdc is the digest ctx that we want, unless there are attributes, in
1066 * which case the digest is the signed attributes
1067 */
1068 if (!EVP_MD_CTX_copy_ex(mdc_tmp, mdc))
1069 goto err;
1070
1071 sk = si->auth_attr;
1072 if ((sk != NULL) && (sk_X509_ATTRIBUTE_num(sk) != 0)) {
1073 unsigned char md_dat[EVP_MAX_MD_SIZE], *abuf = NULL;
1074 unsigned int md_len;
1075 int alen;
1076 ASN1_OCTET_STRING *message_digest;
1077
1078 if (!EVP_DigestFinal_ex(mdc_tmp, md_dat, &md_len))
1079 goto err;
1080 message_digest = PKCS7_digest_from_attributes(sk);
1081 if (!message_digest) {
1082 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);
1083 goto err;
1084 }
1085 if ((message_digest->length != (int)md_len) ||
1086 (memcmp(message_digest->data, md_dat, md_len))) {
1087 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_DIGEST_FAILURE);
1088 ret = -1;
1089 goto err;
1090 }
1091
1092 (void)ERR_set_mark();
1093 fetched_md = EVP_MD_fetch(libctx, OBJ_nid2sn(md_type), propq);
1094
1095 if (fetched_md != NULL)
1096 md = fetched_md;
1097 else
1098 md = EVP_get_digestbynid(md_type);
1099
1100 if (md == NULL || !EVP_VerifyInit_ex(mdc_tmp, md, NULL)) {
1101 (void)ERR_clear_last_mark();
1102 goto err;
1103 }
1104 (void)ERR_pop_to_mark();
1105
1106 alen = ASN1_item_i2d((ASN1_VALUE *)sk, &abuf,
1107 ASN1_ITEM_rptr(PKCS7_ATTR_VERIFY));
1108 if (alen <= 0) {
1109 ERR_raise(ERR_LIB_PKCS7, ERR_R_ASN1_LIB);
1110 ret = -1;
1111 goto err;
1112 }
1113 if (!EVP_VerifyUpdate(mdc_tmp, abuf, alen))
1114 goto err;
1115
1116 OPENSSL_free(abuf);
1117 }
1118
1119 os = si->enc_digest;
1120 pkey = X509_get0_pubkey(signer);
1121 if (pkey == NULL) {
1122 ret = -1;
1123 goto err;
1124 }
1125
1126 i = EVP_VerifyFinal_ex(mdc_tmp, os->data, os->length, pkey, libctx, propq);
1127 if (i <= 0) {
1128 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_SIGNATURE_FAILURE);
1129 ret = -1;
1130 goto err;
1131 }
1132 ret = 1;
1133 err:
1134 EVP_MD_CTX_free(mdc_tmp);
1135 EVP_MD_free(fetched_md);
1136 return ret;
1137 }
1138
1139 PKCS7_ISSUER_AND_SERIAL *PKCS7_get_issuer_and_serial(PKCS7 *p7, int idx)
1140 {
1141 STACK_OF(PKCS7_RECIP_INFO) *rsk;
1142 PKCS7_RECIP_INFO *ri;
1143 int i;
1144
1145 i = OBJ_obj2nid(p7->type);
1146 if (i != NID_pkcs7_signedAndEnveloped)
1147 return NULL;
1148 if (p7->d.signed_and_enveloped == NULL)
1149 return NULL;
1150 rsk = p7->d.signed_and_enveloped->recipientinfo;
1151 if (rsk == NULL)
1152 return NULL;
1153 if (sk_PKCS7_RECIP_INFO_num(rsk) <= idx)
1154 return NULL;
1155 ri = sk_PKCS7_RECIP_INFO_value(rsk, idx);
1156 return ri->issuer_and_serial;
1157 }
1158
1159 ASN1_TYPE *PKCS7_get_signed_attribute(const PKCS7_SIGNER_INFO *si, int nid)
1160 {
1161 return get_attribute(si->auth_attr, nid);
1162 }
1163
1164 ASN1_TYPE *PKCS7_get_attribute(const PKCS7_SIGNER_INFO *si, int nid)
1165 {
1166 return get_attribute(si->unauth_attr, nid);
1167 }
1168
1169 static ASN1_TYPE *get_attribute(const STACK_OF(X509_ATTRIBUTE) *sk, int nid)
1170 {
1171 int idx = X509at_get_attr_by_NID(sk, nid, -1);
1172
1173 if (idx < 0)
1174 return NULL;
1175 return X509_ATTRIBUTE_get0_type(X509at_get_attr(sk, idx), 0);
1176 }
1177
1178 ASN1_OCTET_STRING *PKCS7_digest_from_attributes(STACK_OF(X509_ATTRIBUTE) *sk)
1179 {
1180 ASN1_TYPE *astype;
1181 if ((astype = get_attribute(sk, NID_pkcs9_messageDigest)) == NULL)
1182 return NULL;
1183 return astype->value.octet_string;
1184 }
1185
1186 int PKCS7_set_signed_attributes(PKCS7_SIGNER_INFO *p7si,
1187 STACK_OF(X509_ATTRIBUTE) *sk)
1188 {
1189 int i;
1190
1191 sk_X509_ATTRIBUTE_pop_free(p7si->auth_attr, X509_ATTRIBUTE_free);
1192 p7si->auth_attr = sk_X509_ATTRIBUTE_dup(sk);
1193 if (p7si->auth_attr == NULL)
1194 return 0;
1195 for (i = 0; i < sk_X509_ATTRIBUTE_num(sk); i++) {
1196 if ((sk_X509_ATTRIBUTE_set(p7si->auth_attr, i,
1197 X509_ATTRIBUTE_dup(sk_X509_ATTRIBUTE_value
1198 (sk, i))))
1199 == NULL)
1200 return 0;
1201 }
1202 return 1;
1203 }
1204
1205 int PKCS7_set_attributes(PKCS7_SIGNER_INFO *p7si,
1206 STACK_OF(X509_ATTRIBUTE) *sk)
1207 {
1208 int i;
1209
1210 sk_X509_ATTRIBUTE_pop_free(p7si->unauth_attr, X509_ATTRIBUTE_free);
1211 p7si->unauth_attr = sk_X509_ATTRIBUTE_dup(sk);
1212 if (p7si->unauth_attr == NULL)
1213 return 0;
1214 for (i = 0; i < sk_X509_ATTRIBUTE_num(sk); i++) {
1215 if ((sk_X509_ATTRIBUTE_set(p7si->unauth_attr, i,
1216 X509_ATTRIBUTE_dup(sk_X509_ATTRIBUTE_value
1217 (sk, i))))
1218 == NULL)
1219 return 0;
1220 }
1221 return 1;
1222 }
1223
1224 int PKCS7_add_signed_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int atrtype,
1225 void *value)
1226 {
1227 return add_attribute(&(p7si->auth_attr), nid, atrtype, value);
1228 }
1229
1230 int PKCS7_add_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int atrtype,
1231 void *value)
1232 {
1233 return add_attribute(&(p7si->unauth_attr), nid, atrtype, value);
1234 }
1235
1236 static int add_attribute(STACK_OF(X509_ATTRIBUTE) **sk, int nid, int atrtype,
1237 void *value)
1238 {
1239 X509_ATTRIBUTE *attr = NULL;
1240
1241 if (*sk == NULL) {
1242 if ((*sk = sk_X509_ATTRIBUTE_new_null()) == NULL)
1243 return 0;
1244 new_attrib:
1245 if ((attr = X509_ATTRIBUTE_create(nid, atrtype, value)) == NULL)
1246 return 0;
1247 if (!sk_X509_ATTRIBUTE_push(*sk, attr)) {
1248 X509_ATTRIBUTE_free(attr);
1249 return 0;
1250 }
1251 } else {
1252 int i;
1253
1254 for (i = 0; i < sk_X509_ATTRIBUTE_num(*sk); i++) {
1255 attr = sk_X509_ATTRIBUTE_value(*sk, i);
1256 if (OBJ_obj2nid(X509_ATTRIBUTE_get0_object(attr)) == nid) {
1257 X509_ATTRIBUTE_free(attr);
1258 attr = X509_ATTRIBUTE_create(nid, atrtype, value);
1259 if (attr == NULL)
1260 return 0;
1261 if (!sk_X509_ATTRIBUTE_set(*sk, i, attr)) {
1262 X509_ATTRIBUTE_free(attr);
1263 return 0;
1264 }
1265 goto end;
1266 }
1267 }
1268 goto new_attrib;
1269 }
1270 end:
1271 return 1;
1272 }