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