/src/openssl41/crypto/cms/cms_smime.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2008-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 "internal/cryptlib.h" |
11 | | #include <openssl/asn1t.h> |
12 | | #include <openssl/x509.h> |
13 | | #include <openssl/x509v3.h> |
14 | | #include <openssl/err.h> |
15 | | #include <openssl/cms.h> |
16 | | #include "cms_local.h" |
17 | | #include "crypto/asn1.h" |
18 | | #include "crypto/x509.h" |
19 | | |
20 | | static BIO *cms_get_text_bio(BIO *out, unsigned int flags) |
21 | 2.08k | { |
22 | 2.08k | BIO *rbio; |
23 | | |
24 | 2.08k | if (out == NULL) |
25 | 0 | rbio = BIO_new(BIO_s_null()); |
26 | 2.08k | else if (flags & CMS_TEXT) { |
27 | 0 | rbio = BIO_new(BIO_s_mem()); |
28 | 0 | BIO_set_mem_eof_return(rbio, 0); |
29 | 0 | } else |
30 | 2.08k | rbio = out; |
31 | 2.08k | return rbio; |
32 | 2.08k | } |
33 | | |
34 | | static int cms_copy_content(BIO *out, BIO *in, unsigned int flags) |
35 | 93 | { |
36 | 93 | unsigned char buf[4096]; |
37 | 93 | int r = 0, i; |
38 | 93 | BIO *tmpout; |
39 | 93 | BIO *aeadbuf = NULL; |
40 | | |
41 | 93 | tmpout = cms_get_text_bio(out, flags); |
42 | | |
43 | 93 | if (tmpout == NULL) { |
44 | 0 | ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB); |
45 | 0 | goto err; |
46 | 0 | } |
47 | | |
48 | | /* |
49 | | * For AEAD content (AuthEnvelopedData) the integrity tag is only verified |
50 | | * once all the ciphertext has been processed, by the |
51 | | * BIO_get_cipher_status() call below. RFC 5083 requires that the plaintext |
52 | | * is not released to the caller until that verification succeeds, so |
53 | | * buffer it in memory and only forward it to the output BIO once the tag |
54 | | * has been checked. When CMS_TEXT is set tmpout is already a memory BIO |
55 | | * that is flushed only on success, so the extra buffering is not needed. |
56 | | */ |
57 | 93 | if (tmpout == out && BIO_method_type(in) == BIO_TYPE_CIPHER) { |
58 | 0 | EVP_CIPHER_CTX *ctx = NULL; |
59 | |
|
60 | 0 | if (BIO_get_cipher_ctx(in, &ctx) > 0 && ctx != NULL |
61 | 0 | && (EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ctx)) |
62 | 0 | & EVP_CIPH_FLAG_AEAD_CIPHER) |
63 | 0 | != 0) { |
64 | 0 | aeadbuf = BIO_new(BIO_s_mem()); |
65 | 0 | if (aeadbuf == NULL) { |
66 | 0 | ERR_raise(ERR_LIB_CMS, ERR_R_BIO_LIB); |
67 | 0 | goto err; |
68 | 0 | } |
69 | | /* Return 0 (EOF) rather than a retryable -1 once drained. */ |
70 | 0 | BIO_set_mem_eof_return(aeadbuf, 0); |
71 | 0 | tmpout = aeadbuf; |
72 | 0 | } |
73 | 0 | } |
74 | | |
75 | | /* Read all content through chain to process digest, decrypt etc */ |
76 | 201 | for (;;) { |
77 | 201 | i = BIO_read(in, buf, sizeof(buf)); |
78 | 201 | if (i <= 0) { |
79 | 93 | if (BIO_method_type(in) == BIO_TYPE_CIPHER) { |
80 | 0 | if (BIO_get_cipher_status(in) <= 0) |
81 | 0 | goto err; |
82 | 0 | } |
83 | 93 | if (i < 0) |
84 | 0 | goto err; |
85 | 93 | break; |
86 | 93 | } |
87 | | |
88 | 108 | if (tmpout != NULL && (BIO_write(tmpout, buf, i) != i)) |
89 | 0 | goto err; |
90 | 108 | } |
91 | | |
92 | 93 | if (flags & CMS_TEXT) { |
93 | 0 | if (!SMIME_text(tmpout, out)) { |
94 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_SMIME_TEXT_ERROR); |
95 | 0 | goto err; |
96 | 0 | } |
97 | 93 | } else if (aeadbuf != NULL) { |
98 | | /* Forward the AEAD BIO to out BIO as the tag has been verified. */ |
99 | 0 | for (;;) { |
100 | 0 | i = BIO_read(aeadbuf, buf, sizeof(buf)); |
101 | 0 | if (i < 0) |
102 | 0 | goto err; |
103 | 0 | if (i == 0) |
104 | 0 | break; |
105 | 0 | if (BIO_write(out, buf, i) != i) |
106 | 0 | goto err; |
107 | 0 | } |
108 | 0 | } |
109 | | |
110 | 93 | r = 1; |
111 | 93 | err: |
112 | 93 | if (tmpout != out) |
113 | 0 | BIO_free(tmpout); |
114 | 93 | return r; |
115 | 93 | } |
116 | | |
117 | | static int check_content(CMS_ContentInfo *cms) |
118 | 692 | { |
119 | 692 | ASN1_OCTET_STRING **pos = CMS_get0_content(cms); |
120 | | |
121 | 692 | if (pos == NULL || *pos == NULL) { |
122 | 105 | ERR_raise(ERR_LIB_CMS, CMS_R_NO_CONTENT); |
123 | 105 | return 0; |
124 | 105 | } |
125 | 587 | return 1; |
126 | 692 | } |
127 | | |
128 | | static void do_free_upto(BIO *f, BIO *upto) |
129 | 2.87k | { |
130 | 2.87k | if (upto != NULL) { |
131 | 1.99k | BIO *tbio; |
132 | | |
133 | 1.99k | do { |
134 | 1.99k | tbio = BIO_pop(f); |
135 | 1.99k | BIO_free(f); |
136 | 1.99k | f = tbio; |
137 | 1.99k | } while (f != NULL && f != upto); |
138 | 1.99k | } else { |
139 | 877 | BIO_free_all(f); |
140 | 877 | } |
141 | 2.87k | } |
142 | | |
143 | | int CMS_data(CMS_ContentInfo *cms, BIO *out, unsigned int flags) |
144 | 0 | { |
145 | 0 | BIO *cont; |
146 | 0 | int r; |
147 | |
|
148 | 0 | if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_data) { |
149 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_TYPE_NOT_DATA); |
150 | 0 | return 0; |
151 | 0 | } |
152 | 0 | cont = CMS_dataInit(cms, NULL); |
153 | 0 | if (cont == NULL) |
154 | 0 | return 0; |
155 | 0 | r = cms_copy_content(out, cont, flags); |
156 | 0 | BIO_free_all(cont); |
157 | 0 | return r; |
158 | 0 | } |
159 | | |
160 | | CMS_ContentInfo *CMS_data_create_ex(BIO *in, unsigned int flags, |
161 | | OSSL_LIB_CTX *libctx, const char *propq) |
162 | 0 | { |
163 | 0 | CMS_ContentInfo *cms = ossl_cms_Data_create(libctx, propq); |
164 | |
|
165 | 0 | if (cms == NULL) |
166 | 0 | return NULL; |
167 | | |
168 | 0 | if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags)) |
169 | 0 | return cms; |
170 | | |
171 | 0 | CMS_ContentInfo_free(cms); |
172 | 0 | return NULL; |
173 | 0 | } |
174 | | |
175 | | CMS_ContentInfo *CMS_data_create(BIO *in, unsigned int flags) |
176 | 0 | { |
177 | 0 | return CMS_data_create_ex(in, flags, NULL, NULL); |
178 | 0 | } |
179 | | |
180 | | int CMS_digest_verify(CMS_ContentInfo *cms, BIO *dcont, BIO *out, |
181 | | unsigned int flags) |
182 | 0 | { |
183 | 0 | BIO *cont; |
184 | 0 | int r; |
185 | |
|
186 | 0 | if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_digest) { |
187 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_TYPE_NOT_DIGESTED_DATA); |
188 | 0 | return 0; |
189 | 0 | } |
190 | | |
191 | 0 | if (dcont == NULL && !check_content(cms)) |
192 | 0 | return 0; |
193 | | |
194 | 0 | cont = CMS_dataInit(cms, dcont); |
195 | 0 | if (cont == NULL) |
196 | 0 | return 0; |
197 | | |
198 | 0 | r = cms_copy_content(out, cont, flags); |
199 | 0 | if (r) |
200 | 0 | r = ossl_cms_DigestedData_do_final(cms, cont, 1); |
201 | 0 | do_free_upto(cont, dcont); |
202 | 0 | return r; |
203 | 0 | } |
204 | | |
205 | | CMS_ContentInfo *CMS_digest_create_ex(BIO *in, const EVP_MD *md, |
206 | | unsigned int flags, OSSL_LIB_CTX *ctx, |
207 | | const char *propq) |
208 | 0 | { |
209 | 0 | CMS_ContentInfo *cms; |
210 | | |
211 | | /* |
212 | | * Because the EVP_MD is cached and can be a legacy algorithm, we |
213 | | * cannot fetch the algorithm if it isn't supplied. |
214 | | */ |
215 | 0 | if (md == NULL) |
216 | 0 | md = EVP_sha1(); |
217 | 0 | cms = ossl_cms_DigestedData_create(md, ctx, propq); |
218 | 0 | if (cms == NULL) |
219 | 0 | return NULL; |
220 | | |
221 | 0 | if (!(flags & CMS_DETACHED)) |
222 | 0 | CMS_set_detached(cms, 0); |
223 | |
|
224 | 0 | if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags)) |
225 | 0 | return cms; |
226 | | |
227 | 0 | CMS_ContentInfo_free(cms); |
228 | 0 | return NULL; |
229 | 0 | } |
230 | | |
231 | | CMS_ContentInfo *CMS_digest_create(BIO *in, const EVP_MD *md, |
232 | | unsigned int flags) |
233 | 0 | { |
234 | 0 | return CMS_digest_create_ex(in, md, flags, NULL, NULL); |
235 | 0 | } |
236 | | |
237 | | int CMS_EncryptedData_decrypt(CMS_ContentInfo *cms, |
238 | | const unsigned char *key, size_t keylen, |
239 | | BIO *dcont, BIO *out, unsigned int flags) |
240 | 0 | { |
241 | 0 | BIO *cont; |
242 | 0 | int r; |
243 | |
|
244 | 0 | if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_encrypted) { |
245 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_TYPE_NOT_ENCRYPTED_DATA); |
246 | 0 | return 0; |
247 | 0 | } |
248 | | |
249 | 0 | if (dcont == NULL && !check_content(cms)) |
250 | 0 | return 0; |
251 | | |
252 | 0 | if (CMS_EncryptedData_set1_key(cms, NULL, key, keylen) <= 0) |
253 | 0 | return 0; |
254 | 0 | cont = CMS_dataInit(cms, dcont); |
255 | 0 | if (cont == NULL) |
256 | 0 | return 0; |
257 | 0 | r = cms_copy_content(out, cont, flags); |
258 | 0 | do_free_upto(cont, dcont); |
259 | 0 | return r; |
260 | 0 | } |
261 | | |
262 | | CMS_ContentInfo *CMS_EncryptedData_encrypt_ex(BIO *in, const EVP_CIPHER *cipher, |
263 | | const unsigned char *key, |
264 | | size_t keylen, unsigned int flags, |
265 | | OSSL_LIB_CTX *libctx, |
266 | | const char *propq) |
267 | 0 | { |
268 | 0 | CMS_ContentInfo *cms; |
269 | |
|
270 | 0 | if (cipher == NULL) { |
271 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_NO_CIPHER); |
272 | 0 | return NULL; |
273 | 0 | } |
274 | 0 | cms = CMS_ContentInfo_new_ex(libctx, propq); |
275 | 0 | if (cms == NULL) |
276 | 0 | return NULL; |
277 | 0 | if (!CMS_EncryptedData_set1_key(cms, cipher, key, keylen)) |
278 | 0 | goto err; |
279 | | |
280 | 0 | if (!(flags & CMS_DETACHED)) |
281 | 0 | CMS_set_detached(cms, 0); |
282 | |
|
283 | 0 | if ((flags & (CMS_STREAM | CMS_PARTIAL)) |
284 | 0 | || CMS_final(cms, in, NULL, flags)) |
285 | 0 | return cms; |
286 | | |
287 | 0 | err: |
288 | 0 | CMS_ContentInfo_free(cms); |
289 | 0 | return NULL; |
290 | 0 | } |
291 | | |
292 | | CMS_ContentInfo *CMS_EncryptedData_encrypt(BIO *in, const EVP_CIPHER *cipher, |
293 | | const unsigned char *key, |
294 | | size_t keylen, unsigned int flags) |
295 | 0 | { |
296 | 0 | return CMS_EncryptedData_encrypt_ex(in, cipher, key, keylen, flags, NULL, |
297 | 0 | NULL); |
298 | 0 | } |
299 | | |
300 | | static int cms_signerinfo_verify_cert(CMS_SignerInfo *si, |
301 | | X509_STORE *store, |
302 | | STACK_OF(X509) *untrusted, |
303 | | STACK_OF(X509_CRL) *crls, |
304 | | STACK_OF(X509) **chain, |
305 | | const CMS_CTX *cms_ctx) |
306 | 0 | { |
307 | 0 | X509_STORE_CTX *ctx; |
308 | 0 | X509 *signer; |
309 | 0 | int i, j, r = 0; |
310 | |
|
311 | 0 | ctx = X509_STORE_CTX_new_ex(ossl_cms_ctx_get0_libctx(cms_ctx), |
312 | 0 | ossl_cms_ctx_get0_propq(cms_ctx)); |
313 | 0 | if (ctx == NULL) { |
314 | 0 | ERR_raise(ERR_LIB_CMS, ERR_R_X509_LIB); |
315 | 0 | goto err; |
316 | 0 | } |
317 | 0 | CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL); |
318 | 0 | if (!X509_STORE_CTX_init(ctx, store, signer, untrusted)) { |
319 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_STORE_INIT_ERROR); |
320 | 0 | goto err; |
321 | 0 | } |
322 | 0 | X509_STORE_CTX_set_default(ctx, "smime_sign"); |
323 | 0 | if (crls != NULL) |
324 | 0 | X509_STORE_CTX_set0_crls(ctx, crls); |
325 | |
|
326 | 0 | i = X509_verify_cert(ctx); |
327 | 0 | if (i <= 0) { |
328 | 0 | j = X509_STORE_CTX_get_error(ctx); |
329 | 0 | ERR_raise_data(ERR_LIB_CMS, CMS_R_CERTIFICATE_VERIFY_ERROR, |
330 | 0 | "Verify error: %s", X509_verify_cert_error_string(j)); |
331 | 0 | goto err; |
332 | 0 | } |
333 | 0 | r = 1; |
334 | | |
335 | | /* also send back the trust chain when required */ |
336 | 0 | if (chain != NULL) |
337 | 0 | *chain = X509_STORE_CTX_get1_chain(ctx); |
338 | 0 | err: |
339 | 0 | X509_STORE_CTX_free(ctx); |
340 | 0 | return r; |
341 | 0 | } |
342 | | |
343 | | /* This strongly overlaps with PKCS7_verify() */ |
344 | | int CMS_verify(CMS_ContentInfo *cms, const STACK_OF(X509) *certs, |
345 | | X509_STORE *store, BIO *dcont, BIO *out, unsigned int flags) |
346 | 3.56k | { |
347 | 3.56k | CMS_SignerInfo *si; |
348 | 3.56k | STACK_OF(CMS_SignerInfo) *sinfos; |
349 | 3.56k | STACK_OF(X509) *untrusted = NULL; |
350 | 3.56k | STACK_OF(X509_CRL) *crls = NULL; |
351 | 3.56k | STACK_OF(X509) **si_chains = NULL; |
352 | 3.56k | X509 *signer; |
353 | 3.56k | int i, n = 0, scount = 0, ret = 0; |
354 | 3.56k | BIO *cmsbio = NULL, *tmpin = NULL, *tmpout = NULL; |
355 | 3.56k | int cadesVerify = (flags & CMS_CADES) != 0; |
356 | 3.56k | int partial = (flags & CMS_VERIFY_PARTIAL) != 0; |
357 | 3.56k | const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms); |
358 | | |
359 | 3.56k | if (dcont == NULL && !check_content(cms)) { |
360 | | /* |
361 | | * A CMS_ContentInfo can be verified more than once (e.g. retried |
362 | | * with a different store). If a previous call left verify_result |
363 | | * == 1 ("so far, fine") or any of the *_verified flags set on a |
364 | | * SignerInfo, this early failure must still be reflected there |
365 | | * instead of silently keeping the stale "verified" state from the |
366 | | * earlier call. |
367 | | */ |
368 | 105 | sinfos = CMS_get0_SignerInfos(cms); |
369 | 191 | for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) { |
370 | 86 | si = sk_CMS_SignerInfo_value(sinfos, i); |
371 | 86 | si->verify_result = 0; |
372 | 86 | si->cert_verified = 0; |
373 | 86 | si->attr_verified = 0; |
374 | 86 | si->content_verified = 0; |
375 | 86 | } |
376 | 105 | return 0; |
377 | 105 | } |
378 | | /* Set a mark so that we can clear any new errors on success. */ |
379 | 3.46k | (void)ERR_set_mark(); |
380 | 3.46k | if (dcont != NULL && !(flags & CMS_BINARY)) { |
381 | 2.87k | const ASN1_OBJECT *coid = CMS_get0_eContentType(cms); |
382 | | |
383 | 2.87k | if (OBJ_obj2nid(coid) == NID_id_ct_asciiTextWithCRLF) |
384 | 61 | flags |= CMS_ASCIICRLF; |
385 | 2.87k | } |
386 | | |
387 | | /* Attempt to find all signer certificates */ |
388 | | |
389 | 3.46k | sinfos = CMS_get0_SignerInfos(cms); |
390 | | |
391 | 3.46k | if (sk_CMS_SignerInfo_num(sinfos) <= 0) { |
392 | 91 | ERR_raise(ERR_LIB_CMS, CMS_R_NO_SIGNERS); |
393 | 91 | goto err; |
394 | 91 | } |
395 | | |
396 | 7.75k | for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) { |
397 | 4.38k | si = sk_CMS_SignerInfo_value(sinfos, i); |
398 | 4.38k | CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL); |
399 | 4.38k | if (signer != NULL) |
400 | 0 | scount++; |
401 | | /* Reset verification results */ |
402 | 4.38k | si->verify_result = 1; /* so far, fine */ |
403 | 4.38k | si->cert_verified = 0; |
404 | 4.38k | si->attr_verified = 0; |
405 | 4.38k | si->content_verified = 0; |
406 | 4.38k | } |
407 | | |
408 | 3.36k | if (scount != sk_CMS_SignerInfo_num(sinfos)) |
409 | 3.36k | scount += CMS_set1_signers_certs(cms, certs, flags); |
410 | | |
411 | 3.36k | if (scount != sk_CMS_SignerInfo_num(sinfos)) { |
412 | 255 | ERR_raise(ERR_LIB_CMS, CMS_R_SIGNER_CERTIFICATE_NOT_FOUND); |
413 | 255 | goto err; |
414 | 255 | } |
415 | | |
416 | | /* Attempt to verify all signers certs */ |
417 | | /* at this point scount == sk_CMS_SignerInfo_num(sinfos) */ |
418 | | |
419 | 3.11k | if ((flags & CMS_NO_SIGNER_CERT_VERIFY) == 0 || cadesVerify) { |
420 | 0 | if (cadesVerify) { |
421 | | /* Certificate trust chain is required to check CAdES signature */ |
422 | 0 | si_chains = OPENSSL_calloc(scount, sizeof(si_chains[0])); |
423 | 0 | if (si_chains == NULL) |
424 | 0 | goto err; |
425 | 0 | } |
426 | 0 | if (!ossl_cms_get1_certs_ex(cms, &untrusted)) |
427 | 0 | goto err; |
428 | 0 | if (sk_X509_num(certs) > 0 |
429 | 0 | && !ossl_x509_add_certs_new(&untrusted, certs, |
430 | 0 | X509_ADD_FLAG_UP_REF | X509_ADD_FLAG_NO_DUP)) |
431 | 0 | goto err; |
432 | | |
433 | 0 | if ((flags & CMS_NOCRL) == 0 |
434 | 0 | && !ossl_cms_get1_crls_ex(cms, &crls)) |
435 | 0 | goto err; |
436 | 0 | for (i = 0; i < scount; i++) { |
437 | 0 | si = sk_CMS_SignerInfo_value(sinfos, i); |
438 | |
|
439 | 0 | if (!cms_signerinfo_verify_cert(si, store, untrusted, crls, |
440 | 0 | si_chains ? &si_chains[i] : NULL, |
441 | 0 | ctx)) { |
442 | 0 | si->verify_result = 0; |
443 | 0 | if (!partial) |
444 | 0 | goto err; |
445 | 0 | continue; |
446 | 0 | } |
447 | 0 | si->cert_verified = 1; |
448 | 0 | } |
449 | 0 | } |
450 | | |
451 | | /* Attempt to verify all SignerInfo signed attribute signatures */ |
452 | | |
453 | 3.11k | if ((flags & CMS_NO_ATTR_VERIFY) == 0 || cadesVerify) { |
454 | 5.30k | for (i = 0; i < scount; i++) { |
455 | 3.20k | si = sk_CMS_SignerInfo_value(sinfos, i); |
456 | 3.20k | if (!si->verify_result) |
457 | 0 | continue; |
458 | 3.20k | if (CMS_signed_get_attr_count(si) < 0) { |
459 | 2.05k | si->attr_verified = 1; |
460 | 2.05k | continue; |
461 | 2.05k | } |
462 | 1.14k | if (CMS_SignerInfo_verify(si) <= 0) { |
463 | 1.00k | si->verify_result = 0; |
464 | 1.00k | if (!partial) |
465 | 1.00k | goto err; |
466 | 0 | continue; |
467 | 1.00k | } |
468 | 139 | if (cadesVerify) { |
469 | 0 | STACK_OF(X509) *si_chain = si_chains ? si_chains[i] : NULL; |
470 | |
|
471 | 0 | if (ossl_cms_check_signing_certs(si, si_chain) <= 0) { |
472 | 0 | si->verify_result = 0; |
473 | 0 | if (!partial) |
474 | 0 | goto err; |
475 | 0 | continue; |
476 | 0 | } |
477 | 0 | } |
478 | 139 | si->attr_verified = 1; |
479 | 139 | } |
480 | 3.11k | } |
481 | | |
482 | | /* |
483 | | * Performance optimization: if the content is a memory BIO then store |
484 | | * its contents in a temporary read only memory BIO. This avoids |
485 | | * potentially large numbers of slow copies of data which will occur when |
486 | | * reading from a read write memory BIO when signatures are calculated. |
487 | | */ |
488 | | |
489 | 2.10k | if (dcont != NULL && (BIO_method_type(dcont) == BIO_TYPE_MEM)) { |
490 | 1.99k | char *ptr; |
491 | 1.99k | long len; |
492 | | |
493 | 1.99k | len = BIO_get_mem_data(dcont, &ptr); |
494 | 1.99k | tmpin = (len == 0) ? dcont : BIO_new_mem_buf(ptr, len); |
495 | 1.99k | if (tmpin == NULL) { |
496 | 0 | ERR_raise(ERR_LIB_CMS, ERR_R_BIO_LIB); |
497 | 0 | goto err; |
498 | 0 | } |
499 | 1.99k | } else { |
500 | 110 | tmpin = dcont; |
501 | 110 | } |
502 | | /* |
503 | | * If not binary mode and detached generate digests by *writing* through |
504 | | * the BIO. That makes it possible to canonicalise the input. |
505 | | */ |
506 | 2.10k | if (!(flags & SMIME_BINARY) && dcont) { |
507 | | /* |
508 | | * Create output BIO so we can either handle text or to ensure |
509 | | * included content doesn't override detached content. |
510 | | */ |
511 | 1.99k | tmpout = cms_get_text_bio(out, flags); |
512 | 1.99k | if (tmpout == NULL) { |
513 | 0 | ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB); |
514 | 0 | goto err; |
515 | 0 | } |
516 | 1.99k | cmsbio = CMS_dataInit(cms, tmpout); |
517 | 1.99k | if (cmsbio == NULL) |
518 | 56 | goto err; |
519 | | /* |
520 | | * Don't use SMIME_TEXT for verify: it adds headers and we want to |
521 | | * remove them. |
522 | | */ |
523 | 1.94k | if (!SMIME_crlf_copy(dcont, cmsbio, flags & ~SMIME_TEXT)) |
524 | 0 | goto err; |
525 | | |
526 | 1.94k | if (flags & CMS_TEXT) { |
527 | 0 | if (!SMIME_text(tmpout, out)) { |
528 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_SMIME_TEXT_ERROR); |
529 | 0 | goto err; |
530 | 0 | } |
531 | 0 | } |
532 | 1.94k | } else { |
533 | 110 | cmsbio = CMS_dataInit(cms, tmpin); |
534 | 110 | if (cmsbio == NULL) |
535 | 17 | goto err; |
536 | | |
537 | 93 | if (!cms_copy_content(out, cmsbio, flags)) |
538 | 0 | goto err; |
539 | 93 | } |
540 | 2.03k | if (!(flags & CMS_NO_CONTENT_VERIFY)) { |
541 | 2.13k | for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) { |
542 | 2.07k | si = sk_CMS_SignerInfo_value(sinfos, i); |
543 | 2.07k | if (!si->verify_result) |
544 | 0 | continue; |
545 | 2.07k | if (CMS_SignerInfo_verify_ex(si, cmsbio, tmpin) <= 0) { |
546 | 1.97k | ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_VERIFY_ERROR); |
547 | 1.97k | si->verify_result = 0; |
548 | 1.97k | if (!partial) |
549 | 1.97k | goto err; |
550 | 0 | continue; |
551 | 1.97k | } |
552 | 101 | si->content_verified = 1; |
553 | 101 | } |
554 | 2.03k | } |
555 | | |
556 | | /* Determine overall result */ |
557 | 140 | for (i = 0; i < scount; i++) { |
558 | 80 | if (sk_CMS_SignerInfo_value(sinfos, i)->verify_result) |
559 | 80 | n++; |
560 | 80 | } |
561 | 60 | if (partial) |
562 | 0 | ret = n > 0; /* One success is enough */ |
563 | 60 | else |
564 | 60 | ret = n == scount; /* All must be successful */ |
565 | 3.46k | err: |
566 | | /* |
567 | | * On overall failure, verify_result must not stand at 1 ("so far, fine") |
568 | | * for any SignerInfo. |
569 | | */ |
570 | 3.46k | if (!ret) { |
571 | 7.70k | for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) |
572 | 4.30k | sk_CMS_SignerInfo_value(sinfos, i)->verify_result = 0; |
573 | 3.40k | } |
574 | 3.46k | if (!(flags & SMIME_BINARY) && dcont) { |
575 | 2.87k | do_free_upto(cmsbio, tmpout); |
576 | 2.87k | if (tmpin != dcont) |
577 | 2.87k | BIO_free(tmpin); |
578 | 2.87k | } else { |
579 | 587 | if (dcont && (tmpin == dcont)) |
580 | 0 | do_free_upto(cmsbio, dcont); |
581 | 587 | else if (cmsbio != NULL) |
582 | 93 | BIO_free_all(cmsbio); |
583 | 494 | else |
584 | 494 | BIO_free(tmpin); |
585 | 587 | } |
586 | | |
587 | 3.46k | if (out != tmpout) |
588 | 1.46k | BIO_free_all(tmpout); |
589 | | |
590 | 3.46k | if (si_chains != NULL) { |
591 | 0 | for (i = 0; i < scount; ++i) |
592 | 0 | OSSL_STACK_OF_X509_free(si_chains[i]); |
593 | 0 | OPENSSL_free(si_chains); |
594 | 0 | } |
595 | 3.46k | sk_X509_pop_free(untrusted, X509_free); |
596 | 3.46k | sk_X509_CRL_pop_free(crls, X509_CRL_free); |
597 | | |
598 | | /* |
599 | | * On error, keep internal errors for inspection by the caller. Otherwise |
600 | | * clear any new errors queued since the mark. |
601 | | */ |
602 | 3.46k | if (!ret) |
603 | 3.40k | ERR_clear_last_mark(); |
604 | 60 | else |
605 | 60 | ERR_pop_to_mark(); |
606 | | |
607 | 3.46k | return ret; |
608 | 60 | } |
609 | | |
610 | | int CMS_verify_receipt(CMS_ContentInfo *rcms, CMS_ContentInfo *ocms, |
611 | | const STACK_OF(X509) *certs, |
612 | | X509_STORE *store, unsigned int flags) |
613 | 0 | { |
614 | 0 | int r; |
615 | |
|
616 | 0 | flags &= ~(CMS_DETACHED | CMS_TEXT); |
617 | 0 | r = CMS_verify(rcms, certs, store, NULL, NULL, flags); |
618 | 0 | if (r <= 0) |
619 | 0 | return r; |
620 | 0 | return ossl_cms_Receipt_verify(rcms, ocms); |
621 | 0 | } |
622 | | |
623 | | CMS_ContentInfo *CMS_sign_ex(X509 *signcert, EVP_PKEY *pkey, |
624 | | const STACK_OF(X509) *certs, BIO *data, |
625 | | unsigned int flags, OSSL_LIB_CTX *libctx, |
626 | | const char *propq) |
627 | 0 | { |
628 | 0 | CMS_ContentInfo *cms; |
629 | 0 | int i; |
630 | |
|
631 | 0 | cms = CMS_ContentInfo_new_ex(libctx, propq); |
632 | 0 | if (cms == NULL || !CMS_SignedData_init(cms)) { |
633 | 0 | ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB); |
634 | 0 | goto err; |
635 | 0 | } |
636 | 0 | if (flags & CMS_ASCIICRLF |
637 | 0 | && !CMS_set1_eContentType(cms, |
638 | 0 | OBJ_nid2obj(NID_id_ct_asciiTextWithCRLF))) { |
639 | 0 | ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB); |
640 | 0 | goto err; |
641 | 0 | } |
642 | | |
643 | 0 | if (pkey != NULL && !CMS_add1_signer(cms, signcert, pkey, NULL, flags)) { |
644 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_ADD_SIGNER_ERROR); |
645 | 0 | goto err; |
646 | 0 | } |
647 | | |
648 | 0 | for (i = 0; i < sk_X509_num(certs); i++) { |
649 | 0 | X509 *x = sk_X509_value(certs, i); |
650 | |
|
651 | 0 | if (!CMS_add1_cert(cms, x)) { |
652 | 0 | ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB); |
653 | 0 | goto err; |
654 | 0 | } |
655 | 0 | } |
656 | | |
657 | 0 | if (!(flags & CMS_DETACHED)) |
658 | 0 | CMS_set_detached(cms, 0); |
659 | |
|
660 | 0 | if ((flags & (CMS_STREAM | CMS_PARTIAL)) |
661 | 0 | || CMS_final(cms, data, NULL, flags)) |
662 | 0 | return cms; |
663 | 0 | else |
664 | 0 | goto err; |
665 | | |
666 | 0 | err: |
667 | 0 | CMS_ContentInfo_free(cms); |
668 | 0 | return NULL; |
669 | 0 | } |
670 | | |
671 | | CMS_ContentInfo *CMS_sign(X509 *signcert, EVP_PKEY *pkey, |
672 | | const STACK_OF(X509) *certs, BIO *data, unsigned int flags) |
673 | 0 | { |
674 | 0 | return CMS_sign_ex(signcert, pkey, certs, data, flags, NULL, NULL); |
675 | 0 | } |
676 | | |
677 | | CMS_ContentInfo *CMS_sign_receipt(CMS_SignerInfo *si, |
678 | | X509 *signcert, EVP_PKEY *pkey, |
679 | | const STACK_OF(X509) *certs, unsigned int flags) |
680 | 0 | { |
681 | 0 | CMS_SignerInfo *rct_si; |
682 | 0 | CMS_ContentInfo *cms = NULL; |
683 | 0 | ASN1_OCTET_STRING **pos, *os = NULL; |
684 | 0 | BIO *rct_cont = NULL; |
685 | 0 | int r = 0; |
686 | 0 | const CMS_CTX *ctx = si->cms_ctx; |
687 | |
|
688 | 0 | flags &= ~(CMS_STREAM | CMS_TEXT); |
689 | | /* Not really detached but avoids content being allocated */ |
690 | 0 | flags |= CMS_PARTIAL | CMS_BINARY | CMS_DETACHED; |
691 | 0 | if (pkey == NULL || signcert == NULL) { |
692 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_NO_KEY_OR_CERT); |
693 | 0 | return NULL; |
694 | 0 | } |
695 | | |
696 | | /* Initialize signed data */ |
697 | | |
698 | 0 | cms = CMS_sign_ex(NULL, NULL, certs, NULL, flags, |
699 | 0 | ossl_cms_ctx_get0_libctx(ctx), |
700 | 0 | ossl_cms_ctx_get0_propq(ctx)); |
701 | 0 | if (cms == NULL) |
702 | 0 | goto err; |
703 | | |
704 | | /* Set inner content type to signed receipt */ |
705 | 0 | if (!CMS_set1_eContentType(cms, OBJ_nid2obj(NID_id_smime_ct_receipt))) |
706 | 0 | goto err; |
707 | | |
708 | 0 | rct_si = CMS_add1_signer(cms, signcert, pkey, NULL, flags); |
709 | 0 | if (!rct_si) { |
710 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_ADD_SIGNER_ERROR); |
711 | 0 | goto err; |
712 | 0 | } |
713 | | |
714 | 0 | os = ossl_cms_encode_Receipt(si); |
715 | 0 | if (os == NULL) |
716 | 0 | goto err; |
717 | | |
718 | | /* Set content to digest */ |
719 | 0 | rct_cont = BIO_new_mem_buf(os->data, os->length); |
720 | 0 | if (rct_cont == NULL) |
721 | 0 | goto err; |
722 | | |
723 | | /* Add msgSigDigest attribute */ |
724 | | |
725 | 0 | if (!ossl_cms_msgSigDigest_add1(rct_si, si)) |
726 | 0 | goto err; |
727 | | |
728 | | /* Finalize structure */ |
729 | 0 | if (!CMS_final(cms, rct_cont, NULL, flags)) |
730 | 0 | goto err; |
731 | | |
732 | | /* Set embedded content */ |
733 | 0 | pos = CMS_get0_content(cms); |
734 | 0 | if (pos == NULL) |
735 | 0 | goto err; |
736 | 0 | *pos = os; |
737 | |
|
738 | 0 | r = 1; |
739 | |
|
740 | 0 | err: |
741 | 0 | BIO_free(rct_cont); |
742 | 0 | if (r) |
743 | 0 | return cms; |
744 | 0 | CMS_ContentInfo_free(cms); |
745 | 0 | ASN1_OCTET_STRING_free(os); |
746 | 0 | return NULL; |
747 | 0 | } |
748 | | |
749 | | CMS_ContentInfo *CMS_encrypt_ex(const STACK_OF(X509) *certs, BIO *data, |
750 | | const EVP_CIPHER *cipher, unsigned int flags, |
751 | | OSSL_LIB_CTX *libctx, const char *propq) |
752 | 0 | { |
753 | 0 | CMS_ContentInfo *cms; |
754 | 0 | int i; |
755 | 0 | X509 *recip; |
756 | |
|
757 | 0 | cms = (EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER) |
758 | 0 | ? CMS_AuthEnvelopedData_create_ex(cipher, libctx, propq) |
759 | 0 | : CMS_EnvelopedData_create_ex(cipher, libctx, propq); |
760 | 0 | if (cms == NULL) { |
761 | 0 | ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB); |
762 | 0 | goto err; |
763 | 0 | } |
764 | 0 | for (i = 0; i < sk_X509_num(certs); i++) { |
765 | 0 | recip = sk_X509_value(certs, i); |
766 | 0 | if (!CMS_add1_recipient_cert(cms, recip, flags)) { |
767 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_RECIPIENT_ERROR); |
768 | 0 | goto err; |
769 | 0 | } |
770 | 0 | } |
771 | | |
772 | 0 | if (!(flags & CMS_DETACHED)) |
773 | 0 | CMS_set_detached(cms, 0); |
774 | |
|
775 | 0 | if ((flags & (CMS_STREAM | CMS_PARTIAL)) |
776 | 0 | || CMS_final(cms, data, NULL, flags)) |
777 | 0 | return cms; |
778 | 0 | else |
779 | 0 | ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB); |
780 | | |
781 | 0 | err: |
782 | 0 | CMS_ContentInfo_free(cms); |
783 | 0 | return NULL; |
784 | 0 | } |
785 | | |
786 | | CMS_ContentInfo *CMS_encrypt(const STACK_OF(X509) *certs, BIO *data, |
787 | | const EVP_CIPHER *cipher, unsigned int flags) |
788 | 0 | { |
789 | 0 | return CMS_encrypt_ex(certs, data, cipher, flags, NULL, NULL); |
790 | 0 | } |
791 | | |
792 | | static int cms_kari_set1_pkey_and_peer(CMS_ContentInfo *cms, |
793 | | CMS_RecipientInfo *ri, |
794 | | EVP_PKEY *pk, X509 *cert, X509 *peer) |
795 | 0 | { |
796 | 0 | int i; |
797 | 0 | STACK_OF(CMS_RecipientEncryptedKey) *reks; |
798 | 0 | CMS_RecipientEncryptedKey *rek; |
799 | |
|
800 | 0 | reks = CMS_RecipientInfo_kari_get0_reks(ri); |
801 | 0 | for (i = 0; i < sk_CMS_RecipientEncryptedKey_num(reks); i++) { |
802 | 0 | int rv; |
803 | |
|
804 | 0 | rek = sk_CMS_RecipientEncryptedKey_value(reks, i); |
805 | 0 | if (cert != NULL && CMS_RecipientEncryptedKey_cert_cmp(rek, cert)) |
806 | 0 | continue; |
807 | 0 | CMS_RecipientInfo_kari_set0_pkey_and_peer(ri, pk, peer); |
808 | 0 | rv = CMS_RecipientInfo_kari_decrypt(cms, ri, rek); |
809 | 0 | CMS_RecipientInfo_kari_set0_pkey(ri, NULL); |
810 | 0 | if (rv > 0) |
811 | 0 | return 1; |
812 | 0 | return cert == NULL ? 0 : -1; |
813 | 0 | } |
814 | 0 | return 0; |
815 | 0 | } |
816 | | |
817 | | int CMS_decrypt_set1_pkey(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert) |
818 | 0 | { |
819 | 0 | return CMS_decrypt_set1_pkey_and_peer(cms, pk, cert, NULL); |
820 | 0 | } |
821 | | |
822 | | int CMS_decrypt_set1_pkey_and_peer(CMS_ContentInfo *cms, EVP_PKEY *pk, |
823 | | X509 *cert, X509 *peer) |
824 | 0 | { |
825 | 0 | STACK_OF(CMS_RecipientInfo) *ris = CMS_get0_RecipientInfos(cms); |
826 | 0 | CMS_RecipientInfo *ri; |
827 | 0 | int i, r, cms_pkey_ri_type; |
828 | 0 | int debug = 0, match_ri = 0; |
829 | 0 | CMS_EncryptedContentInfo *ec = ossl_cms_get0_env_enc_content(cms); |
830 | | |
831 | | /* Prevent mem leak on earlier CMS_decrypt_set1_{pkey_and_peer,password} */ |
832 | 0 | if (ec != NULL) { |
833 | 0 | OPENSSL_clear_free(ec->key, ec->keylen); |
834 | 0 | ec->key = NULL; |
835 | 0 | ec->keylen = 0; |
836 | 0 | } |
837 | |
|
838 | 0 | if (ris != NULL && ec != NULL) |
839 | 0 | debug = ec->debug; |
840 | |
|
841 | 0 | cms_pkey_ri_type = ossl_cms_pkey_get_ri_type(pk); |
842 | 0 | if (cms_pkey_ri_type == CMS_RECIPINFO_NONE) { |
843 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE); |
844 | 0 | return 0; |
845 | 0 | } |
846 | | |
847 | 0 | for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) { |
848 | 0 | int ri_type; |
849 | |
|
850 | 0 | ri = sk_CMS_RecipientInfo_value(ris, i); |
851 | 0 | ri_type = CMS_RecipientInfo_type(ri); |
852 | 0 | if (!ossl_cms_pkey_is_ri_type_supported(pk, ri_type)) |
853 | 0 | continue; |
854 | 0 | match_ri = 1; |
855 | 0 | if (ri_type == CMS_RECIPINFO_AGREE) { |
856 | 0 | r = cms_kari_set1_pkey_and_peer(cms, ri, pk, cert, peer); |
857 | 0 | if (r > 0) |
858 | 0 | return 1; |
859 | 0 | if (r < 0) |
860 | 0 | return 0; |
861 | 0 | } else if (ri_type == CMS_RECIPINFO_KEM) { |
862 | 0 | if (cert == NULL || !CMS_RecipientInfo_kemri_cert_cmp(ri, cert)) { |
863 | 0 | CMS_RecipientInfo_kemri_set0_pkey(ri, pk); |
864 | 0 | r = CMS_RecipientInfo_decrypt(cms, ri); |
865 | 0 | CMS_RecipientInfo_kemri_set0_pkey(ri, NULL); |
866 | 0 | if (cert != NULL || r > 0) |
867 | 0 | return r; |
868 | 0 | } |
869 | 0 | } |
870 | | /* If we have a cert, try matching RecipientInfo, else try them all */ |
871 | 0 | else if (cert == NULL || !CMS_RecipientInfo_ktri_cert_cmp(ri, cert)) { |
872 | 0 | if (!EVP_PKEY_up_ref(pk)) |
873 | 0 | return 0; |
874 | 0 | CMS_RecipientInfo_set0_pkey(ri, pk); |
875 | 0 | r = CMS_RecipientInfo_decrypt(cms, ri); |
876 | 0 | CMS_RecipientInfo_set0_pkey(ri, NULL); |
877 | 0 | if (cert != NULL) { |
878 | | /* |
879 | | * If not debugging clear any error and return success to |
880 | | * avoid leaking of information useful to MMA |
881 | | */ |
882 | 0 | if (!debug) { |
883 | 0 | ERR_clear_error(); |
884 | 0 | return 1; |
885 | 0 | } |
886 | 0 | if (r > 0) |
887 | 0 | return 1; |
888 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_DECRYPT_ERROR); |
889 | 0 | return 0; |
890 | 0 | } |
891 | | /* |
892 | | * If no cert and not debugging don't leave loop after first |
893 | | * successful decrypt. Always attempt to decrypt all recipients |
894 | | * to avoid leaking timing of a successful decrypt. |
895 | | */ |
896 | 0 | else if (r > 0 && (debug || cms_pkey_ri_type != CMS_RECIPINFO_TRANS)) |
897 | 0 | return 1; |
898 | 0 | } |
899 | 0 | } |
900 | | /* If no cert, key transport and not debugging always return success */ |
901 | 0 | if (cert == NULL |
902 | 0 | && cms_pkey_ri_type == CMS_RECIPINFO_TRANS |
903 | 0 | && match_ri |
904 | 0 | && !debug) { |
905 | 0 | ERR_clear_error(); |
906 | 0 | return 1; |
907 | 0 | } |
908 | | |
909 | 0 | if (!match_ri) |
910 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_NO_MATCHING_RECIPIENT); |
911 | 0 | return 0; |
912 | 0 | } |
913 | | |
914 | | int CMS_decrypt_set1_key(CMS_ContentInfo *cms, |
915 | | unsigned char *key, size_t keylen, |
916 | | const unsigned char *id, size_t idlen) |
917 | 0 | { |
918 | 0 | STACK_OF(CMS_RecipientInfo) *ris; |
919 | 0 | CMS_RecipientInfo *ri; |
920 | 0 | int i, r, match_ri = 0; |
921 | |
|
922 | 0 | ris = CMS_get0_RecipientInfos(cms); |
923 | 0 | for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) { |
924 | 0 | ri = sk_CMS_RecipientInfo_value(ris, i); |
925 | 0 | if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_KEK) |
926 | 0 | continue; |
927 | | |
928 | | /* If we have an id, try matching RecipientInfo, else try them all */ |
929 | 0 | if (id == NULL |
930 | 0 | || (CMS_RecipientInfo_kekri_id_cmp(ri, id, idlen) == 0)) { |
931 | 0 | match_ri = 1; |
932 | 0 | CMS_RecipientInfo_set0_key(ri, key, keylen); |
933 | 0 | r = CMS_RecipientInfo_decrypt(cms, ri); |
934 | 0 | CMS_RecipientInfo_set0_key(ri, NULL, 0); |
935 | 0 | if (r > 0) |
936 | 0 | return 1; |
937 | 0 | if (id != NULL) { |
938 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_DECRYPT_ERROR); |
939 | 0 | return 0; |
940 | 0 | } |
941 | 0 | ERR_clear_error(); |
942 | 0 | } |
943 | 0 | } |
944 | | |
945 | 0 | if (!match_ri) |
946 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_NO_MATCHING_RECIPIENT); |
947 | 0 | return 0; |
948 | 0 | } |
949 | | |
950 | | int CMS_decrypt_set1_password(CMS_ContentInfo *cms, |
951 | | unsigned char *pass, ossl_ssize_t passlen) |
952 | 0 | { |
953 | 0 | STACK_OF(CMS_RecipientInfo) *ris = CMS_get0_RecipientInfos(cms); |
954 | 0 | CMS_RecipientInfo *ri; |
955 | 0 | int i, r, match_ri = 0; |
956 | 0 | CMS_EncryptedContentInfo *ec = ossl_cms_get0_env_enc_content(cms); |
957 | | |
958 | | /* Prevent mem leak on earlier CMS_decrypt_set1_{pkey_and_peer,password} */ |
959 | 0 | if (ec != NULL) { |
960 | 0 | OPENSSL_clear_free(ec->key, ec->keylen); |
961 | 0 | ec->key = NULL; |
962 | 0 | ec->keylen = 0; |
963 | 0 | } |
964 | |
|
965 | 0 | for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) { |
966 | 0 | ri = sk_CMS_RecipientInfo_value(ris, i); |
967 | 0 | if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_PASS) |
968 | 0 | continue; |
969 | | |
970 | | /* Must try each PasswordRecipientInfo */ |
971 | 0 | match_ri = 1; |
972 | 0 | CMS_RecipientInfo_set0_password(ri, pass, passlen); |
973 | 0 | r = CMS_RecipientInfo_decrypt(cms, ri); |
974 | 0 | CMS_RecipientInfo_set0_password(ri, NULL, 0); |
975 | 0 | if (r > 0) |
976 | 0 | return 1; |
977 | 0 | } |
978 | | |
979 | 0 | if (!match_ri) |
980 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_NO_MATCHING_RECIPIENT); |
981 | 0 | return 0; |
982 | 0 | } |
983 | | |
984 | | int CMS_decrypt(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert, |
985 | | BIO *dcont, BIO *out, unsigned int flags) |
986 | 0 | { |
987 | 0 | int r; |
988 | 0 | BIO *cont; |
989 | 0 | CMS_EncryptedContentInfo *ec; |
990 | 0 | int nid = OBJ_obj2nid(CMS_get0_type(cms)); |
991 | |
|
992 | 0 | if (nid != NID_pkcs7_enveloped |
993 | 0 | && nid != NID_id_smime_ct_authEnvelopedData) { |
994 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_TYPE_NOT_ENVELOPED_DATA); |
995 | 0 | return 0; |
996 | 0 | } |
997 | 0 | if (dcont == NULL && !check_content(cms)) |
998 | 0 | return 0; |
999 | 0 | ec = ossl_cms_get0_env_enc_content(cms); |
1000 | 0 | ec->debug = (flags & CMS_DEBUG_DECRYPT) != 0; |
1001 | 0 | ec->havenocert = cert == NULL; |
1002 | 0 | if (pk == NULL && cert == NULL && dcont == NULL && out == NULL) |
1003 | 0 | return 1; |
1004 | 0 | if (pk != NULL && !CMS_decrypt_set1_pkey(cms, pk, cert)) |
1005 | 0 | return 0; |
1006 | 0 | cont = CMS_dataInit(cms, dcont); |
1007 | 0 | if (cont == NULL) |
1008 | 0 | return 0; |
1009 | 0 | r = cms_copy_content(out, cont, flags); |
1010 | 0 | do_free_upto(cont, dcont); |
1011 | 0 | return r; |
1012 | 0 | } |
1013 | | |
1014 | | int CMS_final(CMS_ContentInfo *cms, BIO *data, BIO *dcont, unsigned int flags) |
1015 | 0 | { |
1016 | 0 | BIO *cmsbio; |
1017 | 0 | int ret = 0; |
1018 | |
|
1019 | 0 | if ((cmsbio = CMS_dataInit(cms, dcont)) == NULL) { |
1020 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_CMS_LIB); |
1021 | 0 | return 0; |
1022 | 0 | } |
1023 | | |
1024 | 0 | if (!SMIME_crlf_copy(data, cmsbio, flags)) { |
1025 | 0 | goto err; |
1026 | 0 | } |
1027 | | |
1028 | 0 | (void)BIO_flush(cmsbio); |
1029 | |
|
1030 | 0 | if (!CMS_dataFinal_ex(cms, cmsbio, data)) { |
1031 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_CMS_DATAFINAL_ERROR); |
1032 | 0 | goto err; |
1033 | 0 | } |
1034 | | |
1035 | 0 | ret = 1; |
1036 | |
|
1037 | 0 | err: |
1038 | 0 | do_free_upto(cmsbio, dcont); |
1039 | |
|
1040 | 0 | return ret; |
1041 | 0 | } |
1042 | | |
1043 | | int CMS_final_digest(CMS_ContentInfo *cms, |
1044 | | const unsigned char *md, unsigned int mdlen, |
1045 | | BIO *dcont, unsigned int flags) |
1046 | 0 | { |
1047 | 0 | BIO *cmsbio; |
1048 | 0 | int ret = 0; |
1049 | |
|
1050 | 0 | if ((cmsbio = CMS_dataInit(cms, dcont)) == NULL) { |
1051 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_CMS_LIB); |
1052 | 0 | return 0; |
1053 | 0 | } |
1054 | | |
1055 | 0 | (void)BIO_flush(cmsbio); |
1056 | |
|
1057 | 0 | if (!ossl_cms_DataFinal(cms, cmsbio, NULL, md, mdlen)) { |
1058 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_CMS_DATAFINAL_ERROR); |
1059 | 0 | goto err; |
1060 | 0 | } |
1061 | 0 | ret = 1; |
1062 | |
|
1063 | 0 | err: |
1064 | 0 | do_free_upto(cmsbio, dcont); |
1065 | 0 | return ret; |
1066 | 0 | } |
1067 | | |
1068 | | #ifndef OPENSSL_NO_ZLIB |
1069 | | |
1070 | | int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out, |
1071 | | unsigned int flags) |
1072 | | { |
1073 | | BIO *cont; |
1074 | | int r; |
1075 | | |
1076 | | if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_id_smime_ct_compressedData) { |
1077 | | ERR_raise(ERR_LIB_CMS, CMS_R_TYPE_NOT_COMPRESSED_DATA); |
1078 | | return 0; |
1079 | | } |
1080 | | |
1081 | | if (dcont == NULL && !check_content(cms)) |
1082 | | return 0; |
1083 | | |
1084 | | cont = CMS_dataInit(cms, dcont); |
1085 | | if (cont == NULL) |
1086 | | return 0; |
1087 | | r = cms_copy_content(out, cont, flags); |
1088 | | do_free_upto(cont, dcont); |
1089 | | return r; |
1090 | | } |
1091 | | |
1092 | | CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags) |
1093 | | { |
1094 | | CMS_ContentInfo *cms; |
1095 | | |
1096 | | if (comp_nid <= 0) |
1097 | | comp_nid = NID_zlib_compression; |
1098 | | cms = ossl_cms_CompressedData_create(comp_nid, NULL, NULL); |
1099 | | if (cms == NULL) |
1100 | | return NULL; |
1101 | | |
1102 | | if (!(flags & CMS_DETACHED)) |
1103 | | CMS_set_detached(cms, 0); |
1104 | | |
1105 | | if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags)) |
1106 | | return cms; |
1107 | | |
1108 | | CMS_ContentInfo_free(cms); |
1109 | | return NULL; |
1110 | | } |
1111 | | |
1112 | | #else |
1113 | | |
1114 | | int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out, |
1115 | | unsigned int flags) |
1116 | 0 | { |
1117 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM); |
1118 | 0 | return 0; |
1119 | 0 | } |
1120 | | |
1121 | | CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags) |
1122 | 0 | { |
1123 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM); |
1124 | | return NULL; |
1125 | 0 | } |
1126 | | |
1127 | | #endif |