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