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