1 /*
2 * Copyright 1999-2024 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 /* Simple PKCS#7 processing functions */
11
12 #include <stdio.h>
13 #include "internal/cryptlib.h"
14 #include "crypto/x509.h"
15 #include <openssl/x509.h>
16 #include <openssl/x509v3.h>
17 #include "pk7_local.h"
18
19 #define BUFFERSIZE 4096
20
21
22 static int pkcs7_copy_existing_digest(PKCS7 *p7, PKCS7_SIGNER_INFO *si);
23
PKCS7_sign_ex(X509 * signcert,EVP_PKEY * pkey,STACK_OF (X509)* certs,BIO * data,int flags,OSSL_LIB_CTX * libctx,const char * propq)24 PKCS7 *PKCS7_sign_ex(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs,
25 BIO *data, int flags, OSSL_LIB_CTX *libctx,
26 const char *propq)
27 {
28 PKCS7 *p7;
29 int i;
30
31 if ((p7 = PKCS7_new_ex(libctx, propq)) == NULL) {
32 ERR_raise(ERR_LIB_PKCS7, ERR_R_PKCS7_LIB);
33 return NULL;
34 }
35
36 if (!PKCS7_set_type(p7, NID_pkcs7_signed))
37 goto err;
38
39 if (!PKCS7_content_new(p7, NID_pkcs7_data))
40 goto err;
41
42 if (pkey && !PKCS7_sign_add_signer(p7, signcert, pkey, NULL, flags)) {
43 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_PKCS7_ADD_SIGNER_ERROR);
44 goto err;
45 }
46
47 if (!(flags & PKCS7_NOCERTS)) {
48 for (i = 0; i < sk_X509_num(certs); i++) {
49 if (!PKCS7_add_certificate(p7, sk_X509_value(certs, i)))
50 goto err;
51 }
52 }
53
54 if (flags & PKCS7_DETACHED)
55 PKCS7_set_detached(p7, 1);
56
57 if (flags & (PKCS7_STREAM | PKCS7_PARTIAL))
58 return p7;
59
60 if (PKCS7_final(p7, data, flags))
61 return p7;
62
63 err:
64 PKCS7_free(p7);
65 return NULL;
66 }
67
PKCS7_sign(X509 * signcert,EVP_PKEY * pkey,STACK_OF (X509)* certs,BIO * data,int flags)68 PKCS7 *PKCS7_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs,
69 BIO *data, int flags)
70 {
71 return PKCS7_sign_ex(signcert, pkey, certs, data, flags, NULL, NULL);
72 }
73
74
PKCS7_final(PKCS7 * p7,BIO * data,int flags)75 int PKCS7_final(PKCS7 *p7, BIO *data, int flags)
76 {
77 BIO *p7bio;
78 int ret = 0;
79
80 if ((p7bio = PKCS7_dataInit(p7, NULL)) == NULL) {
81 ERR_raise(ERR_LIB_PKCS7, ERR_R_PKCS7_LIB);
82 return 0;
83 }
84
85 if (!SMIME_crlf_copy(data, p7bio, flags))
86 goto err;
87
88 (void)BIO_flush(p7bio);
89
90 if (!PKCS7_dataFinal(p7, p7bio)) {
91 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_PKCS7_DATASIGN);
92 goto err;
93 }
94 ret = 1;
95 err:
96 BIO_free_all(p7bio);
97
98 return ret;
99
100 }
101
102 /* Check to see if a cipher exists and if so add S/MIME capabilities */
103
add_cipher_smcap(STACK_OF (X509_ALGOR)* sk,int nid,int arg)104 static int add_cipher_smcap(STACK_OF(X509_ALGOR) *sk, int nid, int arg)
105 {
106 if (EVP_get_cipherbynid(nid))
107 return PKCS7_simple_smimecap(sk, nid, arg);
108 return 1;
109 }
110
add_digest_smcap(STACK_OF (X509_ALGOR)* sk,int nid,int arg)111 static int add_digest_smcap(STACK_OF(X509_ALGOR) *sk, int nid, int arg)
112 {
113 if (EVP_get_digestbynid(nid))
114 return PKCS7_simple_smimecap(sk, nid, arg);
115 return 1;
116 }
117
PKCS7_sign_add_signer(PKCS7 * p7,X509 * signcert,EVP_PKEY * pkey,const EVP_MD * md,int flags)118 PKCS7_SIGNER_INFO *PKCS7_sign_add_signer(PKCS7 *p7, X509 *signcert,
119 EVP_PKEY *pkey, const EVP_MD *md,
120 int flags)
121 {
122 PKCS7_SIGNER_INFO *si = NULL;
123 STACK_OF(X509_ALGOR) *smcap = NULL;
124
125 if (!X509_check_private_key(signcert, pkey)) {
126 ERR_raise(ERR_LIB_PKCS7,
127 PKCS7_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
128 return NULL;
129 }
130
131 if ((si = PKCS7_add_signature(p7, signcert, pkey, md)) == NULL) {
132 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_PKCS7_ADD_SIGNATURE_ERROR);
133 return NULL;
134 }
135
136 si->ctx = ossl_pkcs7_get0_ctx(p7);
137 if (!(flags & PKCS7_NOCERTS)) {
138 if (!PKCS7_add_certificate(p7, signcert))
139 goto err;
140 }
141
142 if (!(flags & PKCS7_NOATTR)) {
143 if (!PKCS7_add_attrib_content_type(si, NULL))
144 goto err;
145 /* Add SMIMECapabilities */
146 if (!(flags & PKCS7_NOSMIMECAP)) {
147 if ((smcap = sk_X509_ALGOR_new_null()) == NULL) {
148 ERR_raise(ERR_LIB_PKCS7, ERR_R_CRYPTO_LIB);
149 goto err;
150 }
151 if (!add_cipher_smcap(smcap, NID_aes_256_cbc, -1)
152 || !add_digest_smcap(smcap, NID_id_GostR3411_2012_256, -1)
153 || !add_digest_smcap(smcap, NID_id_GostR3411_2012_512, -1)
154 || !add_digest_smcap(smcap, NID_id_GostR3411_94, -1)
155 || !add_cipher_smcap(smcap, NID_id_Gost28147_89, -1)
156 || !add_cipher_smcap(smcap, NID_aes_192_cbc, -1)
157 || !add_cipher_smcap(smcap, NID_aes_128_cbc, -1)
158 || !add_cipher_smcap(smcap, NID_des_ede3_cbc, -1)
159 || !add_cipher_smcap(smcap, NID_rc2_cbc, 128)
160 || !add_cipher_smcap(smcap, NID_rc2_cbc, 64)
161 || !add_cipher_smcap(smcap, NID_des_cbc, -1)
162 || !add_cipher_smcap(smcap, NID_rc2_cbc, 40)
163 || !PKCS7_add_attrib_smimecap(si, smcap))
164 goto err;
165 sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free);
166 smcap = NULL;
167 }
168 if (flags & PKCS7_REUSE_DIGEST) {
169 if (!pkcs7_copy_existing_digest(p7, si))
170 goto err;
171 if (!(flags & PKCS7_PARTIAL)
172 && !PKCS7_SIGNER_INFO_sign(si))
173 goto err;
174 }
175 }
176 return si;
177 err:
178 sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free);
179 return NULL;
180 }
181
182 /*
183 * Search for a digest matching SignerInfo digest type and if found copy
184 * across.
185 */
186
pkcs7_copy_existing_digest(PKCS7 * p7,PKCS7_SIGNER_INFO * si)187 static int pkcs7_copy_existing_digest(PKCS7 *p7, PKCS7_SIGNER_INFO *si)
188 {
189 int i;
190 STACK_OF(PKCS7_SIGNER_INFO) *sinfos;
191 PKCS7_SIGNER_INFO *sitmp;
192 ASN1_OCTET_STRING *osdig = NULL;
193 sinfos = PKCS7_get_signer_info(p7);
194 for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(sinfos); i++) {
195 sitmp = sk_PKCS7_SIGNER_INFO_value(sinfos, i);
196 if (si == sitmp)
197 break;
198 if (sk_X509_ATTRIBUTE_num(sitmp->auth_attr) <= 0)
199 continue;
200 if (!OBJ_cmp(si->digest_alg->algorithm, sitmp->digest_alg->algorithm)) {
201 osdig = PKCS7_digest_from_attributes(sitmp->auth_attr);
202 break;
203 }
204
205 }
206
207 if (osdig != NULL)
208 return PKCS7_add1_attrib_digest(si, osdig->data, osdig->length);
209
210 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_MATCHING_DIGEST_TYPE_FOUND);
211 return 0;
212 }
213
214 /* This strongly overlaps with CMS_verify(), partly with PKCS7_dataVerify() */
PKCS7_verify(PKCS7 * p7,STACK_OF (X509)* certs,X509_STORE * store,BIO * indata,BIO * out,int flags)215 int PKCS7_verify(PKCS7 *p7, STACK_OF(X509) *certs, X509_STORE *store,
216 BIO *indata, BIO *out, int flags)
217 {
218 STACK_OF(X509) *signers;
219 STACK_OF(X509) *included_certs;
220 STACK_OF(X509) *untrusted = NULL;
221 X509 *signer;
222 STACK_OF(PKCS7_SIGNER_INFO) *sinfos;
223 PKCS7_SIGNER_INFO *si;
224 X509_STORE_CTX *cert_ctx = NULL;
225 char *buf = NULL;
226 int i, j = 0, k, ret = 0;
227 BIO *p7bio = NULL;
228 BIO *tmpout = NULL;
229 const PKCS7_CTX *p7_ctx;
230
231 if (p7 == NULL) {
232 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_INVALID_NULL_POINTER);
233 return 0;
234 }
235
236 if (!PKCS7_type_is_signed(p7)) {
237 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_WRONG_CONTENT_TYPE);
238 return 0;
239 }
240
241 /* Check for no data and no content: no data to verify signature */
242 if (PKCS7_get_detached(p7) && indata == NULL) {
243 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_CONTENT);
244 return 0;
245 }
246
247 if (flags & PKCS7_NO_DUAL_CONTENT) {
248 /*
249 * This was originally "#if 0" because we thought that only old broken
250 * Netscape did this. It turns out that Authenticode uses this kind
251 * of "extended" PKCS7 format, and things like UEFI secure boot and
252 * tools like osslsigncode need it. In Authenticode the verification
253 * process is different, but the existing PKCs7 verification works.
254 */
255 if (!PKCS7_get_detached(p7) && indata != NULL) {
256 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_CONTENT_AND_DATA_PRESENT);
257 return 0;
258 }
259 }
260
261 sinfos = PKCS7_get_signer_info(p7);
262
263 if (!sinfos || !sk_PKCS7_SIGNER_INFO_num(sinfos)) {
264 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_SIGNATURES_ON_DATA);
265 return 0;
266 }
267
268 signers = PKCS7_get0_signers(p7, certs, flags);
269 if (signers == NULL)
270 return 0;
271
272 /* Now verify the certificates */
273 p7_ctx = ossl_pkcs7_get0_ctx(p7);
274 cert_ctx = X509_STORE_CTX_new_ex(ossl_pkcs7_ctx_get0_libctx(p7_ctx),
275 ossl_pkcs7_ctx_get0_propq(p7_ctx));
276 if (cert_ctx == NULL)
277 goto err;
278 if ((flags & PKCS7_NOVERIFY) == 0) {
279 if (!ossl_x509_add_certs_new(&untrusted, certs, X509_ADD_FLAG_NO_DUP))
280 goto err;
281 included_certs = pkcs7_get0_certificates(p7);
282 if ((flags & PKCS7_NOCHAIN) == 0
283 && !ossl_x509_add_certs_new(&untrusted, included_certs,
284 X509_ADD_FLAG_NO_DUP))
285 goto err;
286
287 for (k = 0; k < sk_X509_num(signers); k++) {
288 signer = sk_X509_value(signers, k);
289 if (!X509_STORE_CTX_init(cert_ctx, store, signer, untrusted)) {
290 ERR_raise(ERR_LIB_PKCS7, ERR_R_X509_LIB);
291 goto err;
292 }
293 if ((flags & PKCS7_NOCHAIN) == 0
294 && !X509_STORE_CTX_set_default(cert_ctx, "smime_sign"))
295 goto err;
296 if (!(flags & PKCS7_NOCRL))
297 X509_STORE_CTX_set0_crls(cert_ctx, p7->d.sign->crl);
298 i = X509_verify_cert(cert_ctx);
299 if (i <= 0) {
300 j = X509_STORE_CTX_get_error(cert_ctx);
301 ERR_raise_data(ERR_LIB_PKCS7, PKCS7_R_CERTIFICATE_VERIFY_ERROR,
302 "Verify error: %s",
303 X509_verify_cert_error_string(j));
304 goto err;
305 }
306 /* Check for revocation status here */
307 }
308 }
309
310 if ((p7bio = PKCS7_dataInit(p7, indata)) == NULL)
311 goto err;
312
313 if (flags & PKCS7_TEXT) {
314 if ((tmpout = BIO_new(BIO_s_mem())) == NULL) {
315 ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB);
316 goto err;
317 }
318 BIO_set_mem_eof_return(tmpout, 0);
319 } else
320 tmpout = out;
321
322 /* We now have to 'read' from p7bio to calculate digests etc. */
323 if ((buf = OPENSSL_malloc(BUFFERSIZE)) == NULL)
324 goto err;
325 for (;;) {
326 i = BIO_read(p7bio, buf, BUFFERSIZE);
327 if (i <= 0)
328 break;
329 if (tmpout)
330 BIO_write(tmpout, buf, i);
331 }
332
333 if (flags & PKCS7_TEXT) {
334 if (!SMIME_text(tmpout, out)) {
335 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_SMIME_TEXT_ERROR);
336 BIO_free(tmpout);
337 goto err;
338 }
339 BIO_free(tmpout);
340 }
341
342 /* Now Verify All Signatures */
343 if (!(flags & PKCS7_NOSIGS))
344 for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(sinfos); i++) {
345 si = sk_PKCS7_SIGNER_INFO_value(sinfos, i);
346 signer = sk_X509_value(signers, i);
347 j = PKCS7_signatureVerify(p7bio, p7, si, signer);
348 if (j <= 0) {
349 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_SIGNATURE_FAILURE);
350 goto err;
351 }
352 }
353
354 ret = 1;
355
356 err:
357 X509_STORE_CTX_free(cert_ctx);
358 OPENSSL_free(buf);
359 if (indata != NULL)
360 BIO_pop(p7bio);
361 BIO_free_all(p7bio);
362 sk_X509_free(signers);
363 sk_X509_free(untrusted);
364 return ret;
365 }
366
STACK_OF(X509)367 STACK_OF(X509) *PKCS7_get0_signers(PKCS7 *p7, STACK_OF(X509) *certs,
368 int flags)
369 {
370 STACK_OF(X509) *signers, *included_certs;
371 STACK_OF(PKCS7_SIGNER_INFO) *sinfos;
372 PKCS7_SIGNER_INFO *si;
373 PKCS7_ISSUER_AND_SERIAL *ias;
374 X509 *signer;
375 int i;
376
377 if (p7 == NULL) {
378 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_INVALID_NULL_POINTER);
379 return NULL;
380 }
381
382 if (!PKCS7_type_is_signed(p7)) {
383 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_WRONG_CONTENT_TYPE);
384 return NULL;
385 }
386 included_certs = pkcs7_get0_certificates(p7);
387
388 /* Collect all the signers together */
389
390 sinfos = PKCS7_get_signer_info(p7);
391
392 if (sk_PKCS7_SIGNER_INFO_num(sinfos) <= 0) {
393 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_SIGNERS);
394 return 0;
395 }
396
397 if ((signers = sk_X509_new_null()) == NULL) {
398 ERR_raise(ERR_LIB_PKCS7, ERR_R_CRYPTO_LIB);
399 return NULL;
400 }
401
402 for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(sinfos); i++) {
403 si = sk_PKCS7_SIGNER_INFO_value(sinfos, i);
404 ias = si->issuer_and_serial;
405 signer = NULL;
406 /* If any certificates passed they take priority */
407 signer = X509_find_by_issuer_and_serial(certs,
408 ias->issuer, ias->serial);
409 if (signer == NULL && (flags & PKCS7_NOINTERN) == 0)
410 signer = X509_find_by_issuer_and_serial(included_certs,
411 ias->issuer, ias->serial);
412 if (signer == NULL) {
413 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_SIGNER_CERTIFICATE_NOT_FOUND);
414 sk_X509_free(signers);
415 return 0;
416 }
417
418 if (!sk_X509_push(signers, signer)) {
419 sk_X509_free(signers);
420 return NULL;
421 }
422 }
423 return signers;
424 }
425
426 /* Build a complete PKCS#7 enveloped data */
427
PKCS7_encrypt_ex(STACK_OF (X509)* certs,BIO * in,const EVP_CIPHER * cipher,int flags,OSSL_LIB_CTX * libctx,const char * propq)428 PKCS7 *PKCS7_encrypt_ex(STACK_OF(X509) *certs, BIO *in,
429 const EVP_CIPHER *cipher, int flags,
430 OSSL_LIB_CTX *libctx, const char *propq)
431 {
432 PKCS7 *p7;
433 BIO *p7bio = NULL;
434 int i;
435 X509 *x509;
436
437 if ((p7 = PKCS7_new_ex(libctx, propq)) == NULL) {
438 ERR_raise(ERR_LIB_PKCS7, ERR_R_PKCS7_LIB);
439 return NULL;
440 }
441
442 if (!PKCS7_set_type(p7, NID_pkcs7_enveloped))
443 goto err;
444 if (!PKCS7_set_cipher(p7, cipher)) {
445 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_ERROR_SETTING_CIPHER);
446 goto err;
447 }
448
449 for (i = 0; i < sk_X509_num(certs); i++) {
450 x509 = sk_X509_value(certs, i);
451 if (!PKCS7_add_recipient(p7, x509)) {
452 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_ERROR_ADDING_RECIPIENT);
453 goto err;
454 }
455 }
456
457 if (flags & PKCS7_STREAM)
458 return p7;
459
460 if (PKCS7_final(p7, in, flags))
461 return p7;
462
463 err:
464
465 BIO_free_all(p7bio);
466 PKCS7_free(p7);
467 return NULL;
468
469 }
470
PKCS7_encrypt(STACK_OF (X509)* certs,BIO * in,const EVP_CIPHER * cipher,int flags)471 PKCS7 *PKCS7_encrypt(STACK_OF(X509) *certs, BIO *in, const EVP_CIPHER *cipher,
472 int flags)
473 {
474 return PKCS7_encrypt_ex(certs, in, cipher, flags, NULL, NULL);
475 }
476
477
PKCS7_decrypt(PKCS7 * p7,EVP_PKEY * pkey,X509 * cert,BIO * data,int flags)478 int PKCS7_decrypt(PKCS7 *p7, EVP_PKEY *pkey, X509 *cert, BIO *data, int flags)
479 {
480 BIO *tmpmem;
481 int ret = 0, i;
482 char *buf = NULL;
483
484 if (p7 == NULL) {
485 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_INVALID_NULL_POINTER);
486 return 0;
487 }
488
489 if (!PKCS7_type_is_enveloped(p7)
490 && !PKCS7_type_is_signedAndEnveloped(p7)) {
491 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_WRONG_CONTENT_TYPE);
492 return 0;
493 }
494
495 if (cert && !X509_check_private_key(cert, pkey)) {
496 ERR_raise(ERR_LIB_PKCS7,
497 PKCS7_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
498 return 0;
499 }
500
501 if ((tmpmem = PKCS7_dataDecode(p7, pkey, NULL, cert)) == NULL) {
502 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_DECRYPT_ERROR);
503 return 0;
504 }
505
506 if (flags & PKCS7_TEXT) {
507 BIO *tmpbuf, *bread;
508 /* Encrypt BIOs can't do BIO_gets() so add a buffer BIO */
509 if ((tmpbuf = BIO_new(BIO_f_buffer())) == NULL) {
510 ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB);
511 BIO_free_all(tmpmem);
512 return 0;
513 }
514 if ((bread = BIO_push(tmpbuf, tmpmem)) == NULL) {
515 ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB);
516 BIO_free_all(tmpbuf);
517 BIO_free_all(tmpmem);
518 return 0;
519 }
520 ret = SMIME_text(bread, data);
521 if (ret > 0 && BIO_method_type(tmpmem) == BIO_TYPE_CIPHER) {
522 if (BIO_get_cipher_status(tmpmem) <= 0)
523 ret = 0;
524 }
525 BIO_free_all(bread);
526 return ret;
527 }
528 if ((buf = OPENSSL_malloc(BUFFERSIZE)) == NULL)
529 goto err;
530 for (;;) {
531 i = BIO_read(tmpmem, buf, BUFFERSIZE);
532 if (i <= 0) {
533 ret = 1;
534 if (BIO_method_type(tmpmem) == BIO_TYPE_CIPHER) {
535 if (BIO_get_cipher_status(tmpmem) <= 0)
536 ret = 0;
537 }
538
539 break;
540 }
541 if (BIO_write(data, buf, i) != i) {
542 break;
543 }
544 }
545 err:
546 OPENSSL_free(buf);
547 BIO_free_all(tmpmem);
548 return ret;
549 }
550