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