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