]>
Commit | Line | Data |
---|---|---|
62867571 | 1 | /* |
b0edda11 | 2 | * Copyright 1995-2018 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; | |
a8d8e06b DSH |
357 | return ASN1_INTEGER_cmp(X509_get_serialNumber(pcert), |
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 MC |
363 | { |
364 | int i, j; | |
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 */ | |
527 | tkeylen = EVP_CIPHER_CTX_key_length(evp_ctx); | |
528 | tkey = OPENSSL_malloc(tkeylen); | |
90945fa3 | 529 | if (tkey == NULL) |
0f113f3e MC |
530 | goto err; |
531 | if (EVP_CIPHER_CTX_rand_key(evp_ctx, tkey) <= 0) | |
532 | goto err; | |
533 | if (ek == NULL) { | |
534 | ek = tkey; | |
535 | eklen = tkeylen; | |
536 | tkey = NULL; | |
537 | } | |
538 | ||
539 | if (eklen != EVP_CIPHER_CTX_key_length(evp_ctx)) { | |
540 | /* | |
541 | * Some S/MIME clients don't use the same key and effective key | |
542 | * length. The key length is determined by the size of the | |
543 | * decrypted RSA key. | |
544 | */ | |
545 | if (!EVP_CIPHER_CTX_set_key_length(evp_ctx, eklen)) { | |
546 | /* Use random key as MMA defence */ | |
4b45c6e5 | 547 | OPENSSL_clear_free(ek, eklen); |
0f113f3e MC |
548 | ek = tkey; |
549 | eklen = tkeylen; | |
550 | tkey = NULL; | |
551 | } | |
552 | } | |
553 | /* Clear errors so we don't leak information useful in MMA */ | |
554 | ERR_clear_error(); | |
555 | if (EVP_CipherInit_ex(evp_ctx, NULL, NULL, ek, NULL, 0) <= 0) | |
556 | goto err; | |
557 | ||
4b45c6e5 RS |
558 | OPENSSL_clear_free(ek, eklen); |
559 | ek = NULL; | |
560 | OPENSSL_clear_free(tkey, tkeylen); | |
561 | tkey = NULL; | |
0f113f3e MC |
562 | |
563 | if (out == NULL) | |
564 | out = etmp; | |
565 | else | |
566 | BIO_push(out, etmp); | |
567 | etmp = NULL; | |
568 | } | |
59302b60 | 569 | if (in_bio != NULL) { |
0f113f3e MC |
570 | bio = in_bio; |
571 | } else { | |
0f113f3e MC |
572 | if (data_body->length > 0) |
573 | bio = BIO_new_mem_buf(data_body->data, data_body->length); | |
574 | else { | |
575 | bio = BIO_new(BIO_s_mem()); | |
90945fa3 MC |
576 | if (bio == NULL) |
577 | goto err; | |
0f113f3e MC |
578 | BIO_set_mem_eof_return(bio, 0); |
579 | } | |
580 | if (bio == NULL) | |
581 | goto err; | |
0f113f3e MC |
582 | } |
583 | BIO_push(out, bio); | |
584 | bio = NULL; | |
4b45c6e5 RS |
585 | return out; |
586 | ||
0f113f3e | 587 | err: |
4b45c6e5 RS |
588 | OPENSSL_clear_free(ek, eklen); |
589 | OPENSSL_clear_free(tkey, tkeylen); | |
590 | BIO_free_all(out); | |
591 | BIO_free_all(btmp); | |
592 | BIO_free_all(etmp); | |
593 | BIO_free_all(bio); | |
02e112a8 | 594 | return NULL; |
0f113f3e | 595 | } |
d02b48c6 | 596 | |
c5a55463 | 597 | static BIO *PKCS7_find_digest(EVP_MD_CTX **pmd, BIO *bio, int nid) |
0f113f3e MC |
598 | { |
599 | for (;;) { | |
600 | bio = BIO_find_type(bio, BIO_TYPE_MD); | |
601 | if (bio == NULL) { | |
602 | PKCS7err(PKCS7_F_PKCS7_FIND_DIGEST, | |
603 | PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST); | |
604 | return NULL; | |
605 | } | |
606 | BIO_get_md_ctx(bio, pmd); | |
607 | if (*pmd == NULL) { | |
608 | PKCS7err(PKCS7_F_PKCS7_FIND_DIGEST, ERR_R_INTERNAL_ERROR); | |
609 | return NULL; | |
610 | } | |
611 | if (EVP_MD_CTX_type(*pmd) == nid) | |
612 | return bio; | |
613 | bio = BIO_next(bio); | |
614 | } | |
615 | return NULL; | |
616 | } | |
c5a55463 | 617 | |
76fa8f18 | 618 | static int do_pkcs7_signed_attrib(PKCS7_SIGNER_INFO *si, EVP_MD_CTX *mctx) |
0f113f3e MC |
619 | { |
620 | unsigned char md_data[EVP_MAX_MD_SIZE]; | |
621 | unsigned int md_len; | |
622 | ||
623 | /* Add signing time if not already present */ | |
624 | if (!PKCS7_get_signed_attribute(si, NID_pkcs9_signingTime)) { | |
625 | if (!PKCS7_add0_attrib_signing_time(si, NULL)) { | |
626 | PKCS7err(PKCS7_F_DO_PKCS7_SIGNED_ATTRIB, ERR_R_MALLOC_FAILURE); | |
627 | return 0; | |
628 | } | |
629 | } | |
630 | ||
631 | /* Add digest */ | |
632 | if (!EVP_DigestFinal_ex(mctx, md_data, &md_len)) { | |
633 | PKCS7err(PKCS7_F_DO_PKCS7_SIGNED_ATTRIB, ERR_R_EVP_LIB); | |
634 | return 0; | |
635 | } | |
636 | if (!PKCS7_add1_attrib_digest(si, md_data, md_len)) { | |
637 | PKCS7err(PKCS7_F_DO_PKCS7_SIGNED_ATTRIB, ERR_R_MALLOC_FAILURE); | |
638 | return 0; | |
639 | } | |
640 | ||
641 | /* Now sign the attributes */ | |
642 | if (!PKCS7_SIGNER_INFO_sign(si)) | |
643 | return 0; | |
644 | ||
645 | return 1; | |
646 | } | |
647 | ||
6b691a5c | 648 | int PKCS7_dataFinal(PKCS7 *p7, BIO *bio) |
0f113f3e MC |
649 | { |
650 | int ret = 0; | |
651 | int i, j; | |
652 | BIO *btmp; | |
653 | PKCS7_SIGNER_INFO *si; | |
6e59a892 | 654 | EVP_MD_CTX *mdc, *ctx_tmp; |
0f113f3e MC |
655 | STACK_OF(X509_ATTRIBUTE) *sk; |
656 | STACK_OF(PKCS7_SIGNER_INFO) *si_sk = NULL; | |
657 | ASN1_OCTET_STRING *os = NULL; | |
658 | ||
c225c3cf EK |
659 | if (p7 == NULL) { |
660 | PKCS7err(PKCS7_F_PKCS7_DATAFINAL, PKCS7_R_INVALID_NULL_POINTER); | |
661 | return 0; | |
662 | } | |
663 | ||
664 | if (p7->d.ptr == NULL) { | |
665 | PKCS7err(PKCS7_F_PKCS7_DATAFINAL, PKCS7_R_NO_CONTENT); | |
666 | return 0; | |
667 | } | |
668 | ||
bfb0641f | 669 | ctx_tmp = EVP_MD_CTX_new(); |
6e59a892 RL |
670 | if (ctx_tmp == NULL) { |
671 | PKCS7err(PKCS7_F_PKCS7_DATAFINAL, ERR_R_MALLOC_FAILURE); | |
672 | return 0; | |
673 | } | |
674 | ||
0f113f3e MC |
675 | i = OBJ_obj2nid(p7->type); |
676 | p7->state = PKCS7_S_HEADER; | |
677 | ||
678 | switch (i) { | |
679 | case NID_pkcs7_data: | |
680 | os = p7->d.data; | |
681 | break; | |
682 | case NID_pkcs7_signedAndEnveloped: | |
683 | /* XXXXXXXXXXXXXXXX */ | |
684 | si_sk = p7->d.signed_and_enveloped->signer_info; | |
685 | os = p7->d.signed_and_enveloped->enc_data->enc_data; | |
90945fa3 | 686 | if (os == NULL) { |
f422a514 | 687 | os = ASN1_OCTET_STRING_new(); |
90945fa3 | 688 | if (os == NULL) { |
0f113f3e MC |
689 | PKCS7err(PKCS7_F_PKCS7_DATAFINAL, ERR_R_MALLOC_FAILURE); |
690 | goto err; | |
691 | } | |
692 | p7->d.signed_and_enveloped->enc_data->enc_data = os; | |
693 | } | |
694 | break; | |
695 | case NID_pkcs7_enveloped: | |
696 | /* XXXXXXXXXXXXXXXX */ | |
697 | os = p7->d.enveloped->enc_data->enc_data; | |
90945fa3 | 698 | if (os == NULL) { |
f422a514 | 699 | os = ASN1_OCTET_STRING_new(); |
90945fa3 | 700 | if (os == NULL) { |
0f113f3e MC |
701 | PKCS7err(PKCS7_F_PKCS7_DATAFINAL, ERR_R_MALLOC_FAILURE); |
702 | goto err; | |
703 | } | |
704 | p7->d.enveloped->enc_data->enc_data = os; | |
705 | } | |
706 | break; | |
707 | case NID_pkcs7_signed: | |
708 | si_sk = p7->d.sign->signer_info; | |
709 | os = PKCS7_get_octet_string(p7->d.sign->contents); | |
710 | /* If detached data then the content is excluded */ | |
711 | if (PKCS7_type_is_data(p7->d.sign->contents) && p7->detached) { | |
f422a514 | 712 | ASN1_OCTET_STRING_free(os); |
c225c3cf | 713 | os = NULL; |
0f113f3e MC |
714 | p7->d.sign->contents->d.data = NULL; |
715 | } | |
716 | break; | |
717 | ||
718 | case NID_pkcs7_digest: | |
719 | os = PKCS7_get_octet_string(p7->d.digest->contents); | |
720 | /* If detached data then the content is excluded */ | |
721 | if (PKCS7_type_is_data(p7->d.digest->contents) && p7->detached) { | |
f422a514 | 722 | ASN1_OCTET_STRING_free(os); |
c225c3cf | 723 | os = NULL; |
0f113f3e MC |
724 | p7->d.digest->contents->d.data = NULL; |
725 | } | |
726 | break; | |
727 | ||
728 | default: | |
729 | PKCS7err(PKCS7_F_PKCS7_DATAFINAL, PKCS7_R_UNSUPPORTED_CONTENT_TYPE); | |
730 | goto err; | |
731 | } | |
732 | ||
733 | if (si_sk != NULL) { | |
734 | for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(si_sk); i++) { | |
735 | si = sk_PKCS7_SIGNER_INFO_value(si_sk, i); | |
736 | if (si->pkey == NULL) | |
737 | continue; | |
738 | ||
739 | j = OBJ_obj2nid(si->digest_alg->algorithm); | |
740 | ||
741 | btmp = bio; | |
742 | ||
743 | btmp = PKCS7_find_digest(&mdc, btmp, j); | |
744 | ||
745 | if (btmp == NULL) | |
746 | goto err; | |
747 | ||
748 | /* | |
749 | * We now have the EVP_MD_CTX, lets do the signing. | |
750 | */ | |
6e59a892 | 751 | if (!EVP_MD_CTX_copy_ex(ctx_tmp, mdc)) |
0f113f3e MC |
752 | goto err; |
753 | ||
754 | sk = si->auth_attr; | |
755 | ||
756 | /* | |
757 | * If there are attributes, we add the digest attribute and only | |
758 | * sign the attributes | |
759 | */ | |
760 | if (sk_X509_ATTRIBUTE_num(sk) > 0) { | |
6e59a892 | 761 | if (!do_pkcs7_signed_attrib(si, ctx_tmp)) |
0f113f3e MC |
762 | goto err; |
763 | } else { | |
764 | unsigned char *abuf = NULL; | |
765 | unsigned int abuflen; | |
766 | abuflen = EVP_PKEY_size(si->pkey); | |
767 | abuf = OPENSSL_malloc(abuflen); | |
90945fa3 | 768 | if (abuf == NULL) |
0f113f3e MC |
769 | goto err; |
770 | ||
6e59a892 | 771 | if (!EVP_SignFinal(ctx_tmp, abuf, &abuflen, si->pkey)) { |
d54ac5c4 | 772 | OPENSSL_free(abuf); |
0f113f3e MC |
773 | PKCS7err(PKCS7_F_PKCS7_DATAFINAL, ERR_R_EVP_LIB); |
774 | goto err; | |
775 | } | |
776 | ASN1_STRING_set0(si->enc_digest, abuf, abuflen); | |
777 | } | |
778 | } | |
779 | } else if (i == NID_pkcs7_digest) { | |
780 | unsigned char md_data[EVP_MAX_MD_SIZE]; | |
781 | unsigned int md_len; | |
782 | if (!PKCS7_find_digest(&mdc, bio, | |
783 | OBJ_obj2nid(p7->d.digest->md->algorithm))) | |
784 | goto err; | |
785 | if (!EVP_DigestFinal_ex(mdc, md_data, &md_len)) | |
786 | goto err; | |
d356dc56 MC |
787 | if (!ASN1_OCTET_STRING_set(p7->d.digest->digest, md_data, md_len)) |
788 | goto err; | |
0f113f3e MC |
789 | } |
790 | ||
c225c3cf | 791 | if (!PKCS7_is_detached(p7)) { |
0f113f3e | 792 | /* |
c225c3cf EK |
793 | * NOTE(emilia): I think we only reach os == NULL here because detached |
794 | * digested data support is broken. | |
0f113f3e | 795 | */ |
c225c3cf EK |
796 | if (os == NULL) |
797 | goto err; | |
798 | if (!(os->flags & ASN1_STRING_FLAG_NDEF)) { | |
799 | char *cont; | |
800 | long contlen; | |
801 | btmp = BIO_find_type(bio, BIO_TYPE_MEM); | |
802 | if (btmp == NULL) { | |
803 | PKCS7err(PKCS7_F_PKCS7_DATAFINAL, PKCS7_R_UNABLE_TO_FIND_MEM_BIO); | |
804 | goto err; | |
805 | } | |
806 | contlen = BIO_get_mem_data(btmp, &cont); | |
807 | /* | |
808 | * Mark the BIO read only then we can use its copy of the data | |
809 | * instead of making an extra copy. | |
810 | */ | |
811 | BIO_set_flags(btmp, BIO_FLAGS_MEM_RDONLY); | |
812 | BIO_set_mem_eof_return(btmp, 0); | |
813 | ASN1_STRING_set0(os, (unsigned char *)cont, contlen); | |
814 | } | |
0f113f3e MC |
815 | } |
816 | ret = 1; | |
817 | err: | |
bfb0641f | 818 | EVP_MD_CTX_free(ctx_tmp); |
26a7d938 | 819 | return ret; |
0f113f3e | 820 | } |
d02b48c6 | 821 | |
76fa8f18 | 822 | int PKCS7_SIGNER_INFO_sign(PKCS7_SIGNER_INFO *si) |
0f113f3e | 823 | { |
6e59a892 | 824 | EVP_MD_CTX *mctx; |
e6803117 | 825 | EVP_PKEY_CTX *pctx = NULL; |
0f113f3e MC |
826 | unsigned char *abuf = NULL; |
827 | int alen; | |
828 | size_t siglen; | |
829 | const EVP_MD *md = NULL; | |
830 | ||
831 | md = EVP_get_digestbyobj(si->digest_alg->algorithm); | |
832 | if (md == NULL) | |
833 | return 0; | |
834 | ||
bfb0641f | 835 | mctx = EVP_MD_CTX_new(); |
6e59a892 RL |
836 | if (mctx == NULL) { |
837 | PKCS7err(PKCS7_F_PKCS7_SIGNER_INFO_SIGN, ERR_R_MALLOC_FAILURE); | |
838 | goto err; | |
839 | } | |
840 | ||
841 | if (EVP_DigestSignInit(mctx, &pctx, md, NULL, si->pkey) <= 0) | |
0f113f3e MC |
842 | goto err; |
843 | ||
dfcb5d29 MC |
844 | /* |
845 | * TODO(3.0): This causes problems when providers are in use, so disabled | |
846 | * for now. Can we get rid of this completely? AFAICT this ctrl has never | |
847 | * been used since it was first put in. All internal implementations just | |
848 | * return 1 and ignore this ctrl and have always done so by the looks of | |
849 | * things. To fix this we could convert this ctrl into a param, which would | |
850 | * require us to send all the signer info data as a set of params...but that | |
851 | * is non-trivial and since this isn't used by anything it may be better | |
852 | * just to remove it. The original commit that added it had this | |
853 | * justification in CHANGES: | |
854 | * | |
855 | * "During PKCS7 signing pass the PKCS7 SignerInfo structure to the | |
856 | * EVP_PKEY_METHOD before and after signing via the | |
857 | * EVP_PKEY_CTRL_PKCS7_SIGN ctrl. It can then customise the structure | |
858 | * before and/or after signing if necessary." | |
859 | */ | |
860 | #if 0 | |
0f113f3e MC |
861 | if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN, |
862 | EVP_PKEY_CTRL_PKCS7_SIGN, 0, si) <= 0) { | |
863 | PKCS7err(PKCS7_F_PKCS7_SIGNER_INFO_SIGN, PKCS7_R_CTRL_ERROR); | |
864 | goto err; | |
865 | } | |
dfcb5d29 | 866 | #endif |
0f113f3e MC |
867 | |
868 | alen = ASN1_item_i2d((ASN1_VALUE *)si->auth_attr, &abuf, | |
869 | ASN1_ITEM_rptr(PKCS7_ATTR_SIGN)); | |
870 | if (!abuf) | |
871 | goto err; | |
6e59a892 | 872 | if (EVP_DigestSignUpdate(mctx, abuf, alen) <= 0) |
0f113f3e MC |
873 | goto err; |
874 | OPENSSL_free(abuf); | |
875 | abuf = NULL; | |
6e59a892 | 876 | if (EVP_DigestSignFinal(mctx, NULL, &siglen) <= 0) |
0f113f3e MC |
877 | goto err; |
878 | abuf = OPENSSL_malloc(siglen); | |
90945fa3 | 879 | if (abuf == NULL) |
0f113f3e | 880 | goto err; |
6e59a892 | 881 | if (EVP_DigestSignFinal(mctx, abuf, &siglen) <= 0) |
0f113f3e MC |
882 | goto err; |
883 | ||
dfcb5d29 MC |
884 | /* |
885 | * TODO(3.0): This causes problems when providers are in use, so disabled | |
886 | * for now. Can we get rid of this completely? AFAICT this ctrl has never | |
887 | * been used since it was first put in. All internal implementations just | |
888 | * return 1 and ignore this ctrl and have always done so by the looks of | |
889 | * things. To fix this we could convert this ctrl into a param, which would | |
890 | * require us to send all the signer info data as a set of params...but that | |
891 | * is non-trivial and since this isn't used by anything it may be better | |
892 | * just to remove it. The original commit that added it had this | |
893 | * justification in CHANGES: | |
894 | * | |
895 | * "During PKCS7 signing pass the PKCS7 SignerInfo structure to the | |
896 | * EVP_PKEY_METHOD before and after signing via the | |
897 | * EVP_PKEY_CTRL_PKCS7_SIGN ctrl. It can then customise the structure | |
898 | * before and/or after signing if necessary." | |
899 | */ | |
900 | #if 0 | |
0f113f3e MC |
901 | if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN, |
902 | EVP_PKEY_CTRL_PKCS7_SIGN, 1, si) <= 0) { | |
903 | PKCS7err(PKCS7_F_PKCS7_SIGNER_INFO_SIGN, PKCS7_R_CTRL_ERROR); | |
904 | goto err; | |
905 | } | |
dfcb5d29 | 906 | #endif |
0f113f3e | 907 | |
bfb0641f | 908 | EVP_MD_CTX_free(mctx); |
0f113f3e MC |
909 | |
910 | ASN1_STRING_set0(si->enc_digest, abuf, siglen); | |
911 | ||
912 | return 1; | |
913 | ||
914 | err: | |
b548a1f1 | 915 | OPENSSL_free(abuf); |
bfb0641f | 916 | EVP_MD_CTX_free(mctx); |
0f113f3e MC |
917 | return 0; |
918 | ||
919 | } | |
76fa8f18 | 920 | |
6b691a5c | 921 | int PKCS7_dataVerify(X509_STORE *cert_store, X509_STORE_CTX *ctx, BIO *bio, |
0f113f3e MC |
922 | PKCS7 *p7, PKCS7_SIGNER_INFO *si) |
923 | { | |
924 | PKCS7_ISSUER_AND_SERIAL *ias; | |
925 | int ret = 0, i; | |
926 | STACK_OF(X509) *cert; | |
927 | X509 *x509; | |
928 | ||
c225c3cf EK |
929 | if (p7 == NULL) { |
930 | PKCS7err(PKCS7_F_PKCS7_DATAVERIFY, PKCS7_R_INVALID_NULL_POINTER); | |
931 | return 0; | |
932 | } | |
933 | ||
934 | if (p7->d.ptr == NULL) { | |
935 | PKCS7err(PKCS7_F_PKCS7_DATAVERIFY, PKCS7_R_NO_CONTENT); | |
936 | return 0; | |
937 | } | |
938 | ||
0f113f3e MC |
939 | if (PKCS7_type_is_signed(p7)) { |
940 | cert = p7->d.sign->cert; | |
941 | } else if (PKCS7_type_is_signedAndEnveloped(p7)) { | |
942 | cert = p7->d.signed_and_enveloped->cert; | |
943 | } else { | |
944 | PKCS7err(PKCS7_F_PKCS7_DATAVERIFY, PKCS7_R_WRONG_PKCS7_TYPE); | |
945 | goto err; | |
946 | } | |
947 | /* XXXXXXXXXXXXXXXXXXXXXXX */ | |
948 | ias = si->issuer_and_serial; | |
949 | ||
950 | x509 = X509_find_by_issuer_and_serial(cert, ias->issuer, ias->serial); | |
951 | ||
952 | /* were we able to find the cert in passed to us */ | |
953 | if (x509 == NULL) { | |
954 | PKCS7err(PKCS7_F_PKCS7_DATAVERIFY, | |
955 | PKCS7_R_UNABLE_TO_FIND_CERTIFICATE); | |
956 | goto err; | |
957 | } | |
958 | ||
959 | /* Lets verify */ | |
960 | if (!X509_STORE_CTX_init(ctx, cert_store, x509, cert)) { | |
961 | PKCS7err(PKCS7_F_PKCS7_DATAVERIFY, ERR_R_X509_LIB); | |
962 | goto err; | |
963 | } | |
964 | X509_STORE_CTX_set_purpose(ctx, X509_PURPOSE_SMIME_SIGN); | |
965 | i = X509_verify_cert(ctx); | |
966 | if (i <= 0) { | |
967 | PKCS7err(PKCS7_F_PKCS7_DATAVERIFY, ERR_R_X509_LIB); | |
968 | X509_STORE_CTX_cleanup(ctx); | |
969 | goto err; | |
970 | } | |
971 | X509_STORE_CTX_cleanup(ctx); | |
972 | ||
973 | return PKCS7_signatureVerify(bio, p7, si, x509); | |
974 | err: | |
975 | return ret; | |
976 | } | |
170afce5 DSH |
977 | |
978 | int PKCS7_signatureVerify(BIO *bio, PKCS7 *p7, PKCS7_SIGNER_INFO *si, | |
0f113f3e | 979 | X509 *x509) |
dfeab068 | 980 | { |
0f113f3e | 981 | ASN1_OCTET_STRING *os; |
6e59a892 | 982 | EVP_MD_CTX *mdc_tmp, *mdc; |
0f113f3e MC |
983 | int ret = 0, i; |
984 | int md_type; | |
985 | STACK_OF(X509_ATTRIBUTE) *sk; | |
986 | BIO *btmp; | |
987 | EVP_PKEY *pkey; | |
988 | ||
bfb0641f | 989 | mdc_tmp = EVP_MD_CTX_new(); |
6e59a892 RL |
990 | if (mdc_tmp == NULL) { |
991 | PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, ERR_R_MALLOC_FAILURE); | |
992 | goto err; | |
993 | } | |
0f113f3e MC |
994 | |
995 | if (!PKCS7_type_is_signed(p7) && !PKCS7_type_is_signedAndEnveloped(p7)) { | |
996 | PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, PKCS7_R_WRONG_PKCS7_TYPE); | |
997 | goto err; | |
998 | } | |
999 | ||
1000 | md_type = OBJ_obj2nid(si->digest_alg->algorithm); | |
1001 | ||
1002 | btmp = bio; | |
1003 | for (;;) { | |
1004 | if ((btmp == NULL) || | |
1005 | ((btmp = BIO_find_type(btmp, BIO_TYPE_MD)) == NULL)) { | |
1006 | PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, | |
1007 | PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST); | |
1008 | goto err; | |
1009 | } | |
1010 | BIO_get_md_ctx(btmp, &mdc); | |
1011 | if (mdc == NULL) { | |
1012 | PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, ERR_R_INTERNAL_ERROR); | |
1013 | goto err; | |
1014 | } | |
1015 | if (EVP_MD_CTX_type(mdc) == md_type) | |
1016 | break; | |
1017 | /* | |
1018 | * Workaround for some broken clients that put the signature OID | |
1019 | * instead of the digest OID in digest_alg->algorithm | |
1020 | */ | |
1021 | if (EVP_MD_pkey_type(EVP_MD_CTX_md(mdc)) == md_type) | |
1022 | break; | |
1023 | btmp = BIO_next(btmp); | |
1024 | } | |
1025 | ||
1026 | /* | |
1027 | * mdc is the digest ctx that we want, unless there are attributes, in | |
1028 | * which case the digest is the signed attributes | |
1029 | */ | |
6e59a892 | 1030 | if (!EVP_MD_CTX_copy_ex(mdc_tmp, mdc)) |
0f113f3e MC |
1031 | goto err; |
1032 | ||
1033 | sk = si->auth_attr; | |
1034 | if ((sk != NULL) && (sk_X509_ATTRIBUTE_num(sk) != 0)) { | |
1035 | unsigned char md_dat[EVP_MAX_MD_SIZE], *abuf = NULL; | |
1036 | unsigned int md_len; | |
1037 | int alen; | |
1038 | ASN1_OCTET_STRING *message_digest; | |
1039 | ||
6e59a892 | 1040 | if (!EVP_DigestFinal_ex(mdc_tmp, md_dat, &md_len)) |
0f113f3e MC |
1041 | goto err; |
1042 | message_digest = PKCS7_digest_from_attributes(sk); | |
1043 | if (!message_digest) { | |
1044 | PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, | |
1045 | PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST); | |
1046 | goto err; | |
1047 | } | |
1048 | if ((message_digest->length != (int)md_len) || | |
1049 | (memcmp(message_digest->data, md_dat, md_len))) { | |
0f113f3e MC |
1050 | PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, PKCS7_R_DIGEST_FAILURE); |
1051 | ret = -1; | |
1052 | goto err; | |
1053 | } | |
1054 | ||
6e59a892 | 1055 | if (!EVP_VerifyInit_ex(mdc_tmp, EVP_get_digestbynid(md_type), NULL)) |
0f113f3e MC |
1056 | goto err; |
1057 | ||
1058 | alen = ASN1_item_i2d((ASN1_VALUE *)sk, &abuf, | |
1059 | ASN1_ITEM_rptr(PKCS7_ATTR_VERIFY)); | |
1060 | if (alen <= 0) { | |
1061 | PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, ERR_R_ASN1_LIB); | |
1062 | ret = -1; | |
1063 | goto err; | |
1064 | } | |
6e59a892 | 1065 | if (!EVP_VerifyUpdate(mdc_tmp, abuf, alen)) |
0f113f3e MC |
1066 | goto err; |
1067 | ||
1068 | OPENSSL_free(abuf); | |
1069 | } | |
1070 | ||
1071 | os = si->enc_digest; | |
8382fd3a | 1072 | pkey = X509_get0_pubkey(x509); |
12a765a5 | 1073 | if (pkey == NULL) { |
0f113f3e MC |
1074 | ret = -1; |
1075 | goto err; | |
1076 | } | |
1077 | ||
6e59a892 | 1078 | i = EVP_VerifyFinal(mdc_tmp, os->data, os->length, pkey); |
0f113f3e MC |
1079 | if (i <= 0) { |
1080 | PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, PKCS7_R_SIGNATURE_FAILURE); | |
1081 | ret = -1; | |
1082 | goto err; | |
c5ba2d99 RS |
1083 | } |
1084 | ret = 1; | |
0f113f3e | 1085 | err: |
bfb0641f | 1086 | EVP_MD_CTX_free(mdc_tmp); |
26a7d938 | 1087 | return ret; |
0f113f3e | 1088 | } |
d02b48c6 | 1089 | |
6b691a5c | 1090 | PKCS7_ISSUER_AND_SERIAL *PKCS7_get_issuer_and_serial(PKCS7 *p7, int idx) |
0f113f3e MC |
1091 | { |
1092 | STACK_OF(PKCS7_RECIP_INFO) *rsk; | |
1093 | PKCS7_RECIP_INFO *ri; | |
1094 | int i; | |
1095 | ||
1096 | i = OBJ_obj2nid(p7->type); | |
1097 | if (i != NID_pkcs7_signedAndEnveloped) | |
1098 | return NULL; | |
1099 | if (p7->d.signed_and_enveloped == NULL) | |
1100 | return NULL; | |
1101 | rsk = p7->d.signed_and_enveloped->recipientinfo; | |
1102 | if (rsk == NULL) | |
1103 | return NULL; | |
0f113f3e | 1104 | if (sk_PKCS7_RECIP_INFO_num(rsk) <= idx) |
26a7d938 | 1105 | return NULL; |
0f113f3e | 1106 | ri = sk_PKCS7_RECIP_INFO_value(rsk, idx); |
26a7d938 | 1107 | return ri->issuer_and_serial; |
0f113f3e | 1108 | } |
dfeab068 | 1109 | |
6b691a5c | 1110 | ASN1_TYPE *PKCS7_get_signed_attribute(PKCS7_SIGNER_INFO *si, int nid) |
0f113f3e | 1111 | { |
26a7d938 | 1112 | return get_attribute(si->auth_attr, nid); |
0f113f3e | 1113 | } |
dfeab068 | 1114 | |
6b691a5c | 1115 | ASN1_TYPE *PKCS7_get_attribute(PKCS7_SIGNER_INFO *si, int nid) |
0f113f3e | 1116 | { |
26a7d938 | 1117 | return get_attribute(si->unauth_attr, nid); |
0f113f3e | 1118 | } |
dfeab068 | 1119 | |
b6436ff2 | 1120 | static ASN1_TYPE *get_attribute(STACK_OF(X509_ATTRIBUTE) *sk, int nid) |
0f113f3e | 1121 | { |
9b0a4531 | 1122 | int idx; |
0f113f3e | 1123 | X509_ATTRIBUTE *xa; |
9b0a4531 DSH |
1124 | idx = X509at_get_attr_by_NID(sk, nid, -1); |
1125 | xa = X509at_get_attr(sk, idx); | |
1126 | return X509_ATTRIBUTE_get0_type(xa, 0); | |
0f113f3e | 1127 | } |
dfeab068 | 1128 | |
b6436ff2 | 1129 | ASN1_OCTET_STRING *PKCS7_digest_from_attributes(STACK_OF(X509_ATTRIBUTE) *sk) |
10243d97 | 1130 | { |
0f113f3e | 1131 | ASN1_TYPE *astype; |
75ebbd9a | 1132 | if ((astype = get_attribute(sk, NID_pkcs9_messageDigest)) == NULL) |
0f113f3e MC |
1133 | return NULL; |
1134 | return astype->value.octet_string; | |
10243d97 | 1135 | } |
dfeab068 | 1136 | |
b6436ff2 | 1137 | int PKCS7_set_signed_attributes(PKCS7_SIGNER_INFO *p7si, |
0f113f3e MC |
1138 | STACK_OF(X509_ATTRIBUTE) *sk) |
1139 | { | |
1140 | int i; | |
1141 | ||
222561fe | 1142 | sk_X509_ATTRIBUTE_pop_free(p7si->auth_attr, X509_ATTRIBUTE_free); |
0f113f3e MC |
1143 | p7si->auth_attr = sk_X509_ATTRIBUTE_dup(sk); |
1144 | if (p7si->auth_attr == NULL) | |
1145 | return 0; | |
1146 | for (i = 0; i < sk_X509_ATTRIBUTE_num(sk); i++) { | |
1147 | if ((sk_X509_ATTRIBUTE_set(p7si->auth_attr, i, | |
1148 | X509_ATTRIBUTE_dup(sk_X509_ATTRIBUTE_value | |
1149 | (sk, i)))) | |
1150 | == NULL) | |
26a7d938 | 1151 | return 0; |
0f113f3e | 1152 | } |
208fb891 | 1153 | return 1; |
0f113f3e MC |
1154 | } |
1155 | ||
1156 | int PKCS7_set_attributes(PKCS7_SIGNER_INFO *p7si, | |
1157 | STACK_OF(X509_ATTRIBUTE) *sk) | |
1158 | { | |
1159 | int i; | |
1160 | ||
222561fe | 1161 | sk_X509_ATTRIBUTE_pop_free(p7si->unauth_attr, X509_ATTRIBUTE_free); |
0f113f3e MC |
1162 | p7si->unauth_attr = sk_X509_ATTRIBUTE_dup(sk); |
1163 | if (p7si->unauth_attr == NULL) | |
1164 | return 0; | |
1165 | for (i = 0; i < sk_X509_ATTRIBUTE_num(sk); i++) { | |
1166 | if ((sk_X509_ATTRIBUTE_set(p7si->unauth_attr, i, | |
1167 | X509_ATTRIBUTE_dup(sk_X509_ATTRIBUTE_value | |
1168 | (sk, i)))) | |
1169 | == NULL) | |
26a7d938 | 1170 | return 0; |
0f113f3e | 1171 | } |
208fb891 | 1172 | return 1; |
0f113f3e | 1173 | } |
dfeab068 | 1174 | |
6b691a5c | 1175 | int PKCS7_add_signed_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int atrtype, |
0f113f3e MC |
1176 | void *value) |
1177 | { | |
26a7d938 | 1178 | return add_attribute(&(p7si->auth_attr), nid, atrtype, value); |
0f113f3e | 1179 | } |
dfeab068 | 1180 | |
6b691a5c | 1181 | int PKCS7_add_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int atrtype, |
0f113f3e MC |
1182 | void *value) |
1183 | { | |
26a7d938 | 1184 | return add_attribute(&(p7si->unauth_attr), nid, atrtype, value); |
0f113f3e | 1185 | } |
dfeab068 | 1186 | |
b6436ff2 | 1187 | static int add_attribute(STACK_OF(X509_ATTRIBUTE) **sk, int nid, int atrtype, |
0f113f3e MC |
1188 | void *value) |
1189 | { | |
1190 | X509_ATTRIBUTE *attr = NULL; | |
1191 | ||
1192 | if (*sk == NULL) { | |
75ebbd9a | 1193 | if ((*sk = sk_X509_ATTRIBUTE_new_null()) == NULL) |
0f113f3e MC |
1194 | return 0; |
1195 | new_attrib: | |
75ebbd9a | 1196 | if ((attr = X509_ATTRIBUTE_create(nid, atrtype, value)) == NULL) |
0f113f3e MC |
1197 | return 0; |
1198 | if (!sk_X509_ATTRIBUTE_push(*sk, attr)) { | |
1199 | X509_ATTRIBUTE_free(attr); | |
1200 | return 0; | |
1201 | } | |
1202 | } else { | |
1203 | int i; | |
1204 | ||
1205 | for (i = 0; i < sk_X509_ATTRIBUTE_num(*sk); i++) { | |
1206 | attr = sk_X509_ATTRIBUTE_value(*sk, i); | |
9b0a4531 | 1207 | if (OBJ_obj2nid(X509_ATTRIBUTE_get0_object(attr)) == nid) { |
0f113f3e MC |
1208 | X509_ATTRIBUTE_free(attr); |
1209 | attr = X509_ATTRIBUTE_create(nid, atrtype, value); | |
1210 | if (attr == NULL) | |
1211 | return 0; | |
1212 | if (!sk_X509_ATTRIBUTE_set(*sk, i, attr)) { | |
1213 | X509_ATTRIBUTE_free(attr); | |
1214 | return 0; | |
1215 | } | |
1216 | goto end; | |
1217 | } | |
1218 | } | |
1219 | goto new_attrib; | |
1220 | } | |
1221 | end: | |
208fb891 | 1222 | return 1; |
0f113f3e | 1223 | } |