| 1 |
// Copyright (c) 2014, Google Inc. |
| 2 |
// SPDX-License-Identifier: ISC |
| 3 |
|
| 4 |
#include <openssl/pkcs7.h> |
| 5 |
|
| 6 |
#include <openssl/bytestring.h> |
| 7 |
#include <openssl/err.h> |
| 8 |
#include <openssl/mem.h> |
| 9 |
#include <openssl/obj.h> |
| 10 |
#include <openssl/pem.h> |
| 11 |
#include <openssl/pool.h> |
| 12 |
#include <openssl/rand.h> |
| 13 |
#include <openssl/stack.h> |
| 14 |
#include <openssl/x509.h> |
| 15 |
|
| 16 |
#include "../bytestring/internal.h" |
| 17 |
#include "../fipsmodule/digest/internal.h" |
| 18 |
#include "../internal.h" |
| 19 |
#include "internal.h" |
| 20 |
|
| 21 |
OPENSSL_BEGIN_ALLOW_DEPRECATED |
| 22 |
|
| 23 |
// 1.2.840.113549.1.7.1 |
| 24 |
static const uint8_t kPKCS7Data[] = {0x2a, 0x86, 0x48, 0x86, 0xf7, |
| 25 |
0x0d, 0x01, 0x07, 0x01}; |
| 26 |
|
| 27 |
// 1.2.840.113549.1.7.2 |
| 28 |
static const uint8_t kPKCS7SignedData[] = {0x2a, 0x86, 0x48, 0x86, 0xf7, |
| 29 |
0x0d, 0x01, 0x07, 0x02}; |
| 30 |
|
| 31 |
// pkcs7_parse_header reads the non-certificate/non-CRL prefix of a PKCS#7 |
| 32 |
// SignedData blob from |cbs| and sets |*out| to point to the rest of the |
| 33 |
// input. If the input is in BER format, then |*der_bytes| will be set to a |
| 34 |
// pointer that needs to be freed by the caller once they have finished |
| 35 |
// processing |*out| (which will be pointing into |*der_bytes|). |
| 36 |
// |
| 37 |
// It returns one on success or zero on error. On error, |*der_bytes| is |
| 38 |
// NULL. |
| 39 |
int pkcs7_parse_header(uint8_t **der_bytes, CBS *out, CBS *cbs) { |
| 40 |
CBS in, content_info, content_type, wrapped_signed_data, signed_data; |
| 41 |
uint64_t version; |
| 42 |
|
| 43 |
// The input may be in BER format. |
| 44 |
*der_bytes = NULL; |
| 45 |
if (!CBS_asn1_ber_to_der(cbs, &in, der_bytes) || |
| 46 |
// See https://tools.ietf.org/html/rfc2315#section-7 |
| 47 |
!CBS_get_asn1(&in, &content_info, CBS_ASN1_SEQUENCE) || |
| 48 |
!CBS_get_asn1(&content_info, &content_type, CBS_ASN1_OBJECT)) { |
| 49 |
goto err; |
| 50 |
} |
| 51 |
|
| 52 |
if (!CBS_mem_equal(&content_type, kPKCS7SignedData, |
| 53 |
sizeof(kPKCS7SignedData))) { |
| 54 |
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_NOT_PKCS7_SIGNED_DATA); |
| 55 |
goto err; |
| 56 |
} |
| 57 |
|
| 58 |
// See https://tools.ietf.org/html/rfc2315#section-9.1 |
| 59 |
if (!CBS_get_asn1(&content_info, &wrapped_signed_data, |
| 60 |
CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0) || |
| 61 |
!CBS_get_asn1(&wrapped_signed_data, &signed_data, CBS_ASN1_SEQUENCE) || |
| 62 |
!CBS_get_asn1_uint64(&signed_data, &version) || |
| 63 |
!CBS_get_asn1(&signed_data, NULL /* digests */, CBS_ASN1_SET) || |
| 64 |
!CBS_get_asn1(&signed_data, NULL /* content */, CBS_ASN1_SEQUENCE)) { |
| 65 |
goto err; |
| 66 |
} |
| 67 |
|
| 68 |
if (version < 1) { |
| 69 |
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_BAD_PKCS7_VERSION); |
| 70 |
goto err; |
| 71 |
} |
| 72 |
|
| 73 |
CBS_init(out, CBS_data(&signed_data), CBS_len(&signed_data)); |
| 74 |
return 1; |
| 75 |
|
| 76 |
err: |
| 77 |
OPENSSL_free(*der_bytes); |
| 78 |
*der_bytes = NULL; |
| 79 |
return 0; |
| 80 |
} |
| 81 |
|
| 82 |
int PKCS7_get_raw_certificates(STACK_OF(CRYPTO_BUFFER) *out_certs, CBS *cbs, |
| 83 |
CRYPTO_BUFFER_POOL *pool) { |
| 84 |
CBS signed_data, certificates; |
| 85 |
uint8_t *der_bytes = NULL; |
| 86 |
int ret = 0, has_certificates; |
| 87 |
const size_t initial_certs_len = sk_CRYPTO_BUFFER_num(out_certs); |
| 88 |
|
| 89 |
// See https://tools.ietf.org/html/rfc2315#section-9.1 |
| 90 |
if (!pkcs7_parse_header(&der_bytes, &signed_data, cbs) || |
| 91 |
!CBS_get_optional_asn1( |
| 92 |
&signed_data, &certificates, &has_certificates, |
| 93 |
CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0)) { |
| 94 |
goto err; |
| 95 |
} |
| 96 |
|
| 97 |
if (!has_certificates) { |
| 98 |
CBS_init(&certificates, NULL, 0); |
| 99 |
} |
| 100 |
|
| 101 |
while (CBS_len(&certificates) > 0) { |
| 102 |
CBS cert; |
| 103 |
if (!CBS_get_asn1_element(&certificates, &cert, CBS_ASN1_SEQUENCE)) { |
| 104 |
goto err; |
| 105 |
} |
| 106 |
|
| 107 |
CRYPTO_BUFFER *buf = CRYPTO_BUFFER_new_from_CBS(&cert, pool); |
| 108 |
if (buf == NULL || !sk_CRYPTO_BUFFER_push(out_certs, buf)) { |
| 109 |
CRYPTO_BUFFER_free(buf); |
| 110 |
goto err; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
ret = 1; |
| 115 |
|
| 116 |
err: |
| 117 |
OPENSSL_free(der_bytes); |
| 118 |
|
| 119 |
if (!ret) { |
| 120 |
while (sk_CRYPTO_BUFFER_num(out_certs) != initial_certs_len) { |
| 121 |
CRYPTO_BUFFER *buf = sk_CRYPTO_BUFFER_pop(out_certs); |
| 122 |
CRYPTO_BUFFER_free(buf); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
return ret; |
| 127 |
} |
| 128 |
|
| 129 |
static int pkcs7_bundle_raw_certificates_cb(CBB *out, const void *arg) { |
| 130 |
const STACK_OF(CRYPTO_BUFFER) *certs = arg; |
| 131 |
CBB certificates; |
| 132 |
|
| 133 |
// See https://tools.ietf.org/html/rfc2315#section-9.1 |
| 134 |
if (!CBB_add_asn1(out, &certificates, |
| 135 |
CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0)) { |
| 136 |
return 0; |
| 137 |
} |
| 138 |
|
| 139 |
for (size_t i = 0; i < sk_CRYPTO_BUFFER_num(certs); i++) { |
| 140 |
CRYPTO_BUFFER *cert = sk_CRYPTO_BUFFER_value(certs, i); |
| 141 |
if (!CBB_add_bytes(&certificates, CRYPTO_BUFFER_data(cert), |
| 142 |
CRYPTO_BUFFER_len(cert))) { |
| 143 |
return 0; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
// |certificates| is a implicitly-tagged SET OF. |
| 148 |
return CBB_flush_asn1_set_of(&certificates) && CBB_flush(out); |
| 149 |
} |
| 150 |
|
| 151 |
int PKCS7_bundle_raw_certificates(CBB *out, |
| 152 |
const STACK_OF(CRYPTO_BUFFER) *certs) { |
| 153 |
return pkcs7_add_signed_data(out, /*digest_algos_cb=*/NULL, |
| 154 |
pkcs7_bundle_raw_certificates_cb, |
| 155 |
/*signer_infos_cb=*/NULL, certs); |
| 156 |
} |
| 157 |
|
| 158 |
int pkcs7_add_signed_data(CBB *out, |
| 159 |
int (*digest_algos_cb)(CBB *out, const void *arg), |
| 160 |
int (*cert_crl_cb)(CBB *out, const void *arg), |
| 161 |
int (*signer_infos_cb)(CBB *out, const void *arg), |
| 162 |
const void *arg) { |
| 163 |
CBB outer_seq, oid, wrapped_seq, seq, version_bytes, digest_algos_set, |
| 164 |
content_info, signer_infos; |
| 165 |
|
| 166 |
// See https://tools.ietf.org/html/rfc2315#section-7 |
| 167 |
if (!CBB_add_asn1(out, &outer_seq, CBS_ASN1_SEQUENCE) || |
| 168 |
!CBB_add_asn1(&outer_seq, &oid, CBS_ASN1_OBJECT) || |
| 169 |
!CBB_add_bytes(&oid, kPKCS7SignedData, sizeof(kPKCS7SignedData)) || |
| 170 |
!CBB_add_asn1(&outer_seq, &wrapped_seq, |
| 171 |
CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0) || |
| 172 |
// See https://tools.ietf.org/html/rfc2315#section-9.1 |
| 173 |
!CBB_add_asn1(&wrapped_seq, &seq, CBS_ASN1_SEQUENCE) || |
| 174 |
!CBB_add_asn1(&seq, &version_bytes, CBS_ASN1_INTEGER) || |
| 175 |
!CBB_add_u8(&version_bytes, 1) || |
| 176 |
!CBB_add_asn1(&seq, &digest_algos_set, CBS_ASN1_SET) || |
| 177 |
(digest_algos_cb != NULL && !digest_algos_cb(&digest_algos_set, arg)) || |
| 178 |
!CBB_add_asn1(&seq, &content_info, CBS_ASN1_SEQUENCE) || |
| 179 |
!CBB_add_asn1(&content_info, &oid, CBS_ASN1_OBJECT) || |
| 180 |
!CBB_add_bytes(&oid, kPKCS7Data, sizeof(kPKCS7Data)) || |
| 181 |
(cert_crl_cb != NULL && !cert_crl_cb(&seq, arg)) || |
| 182 |
!CBB_add_asn1(&seq, &signer_infos, CBS_ASN1_SET) || |
| 183 |
(signer_infos_cb != NULL && !signer_infos_cb(&signer_infos, arg))) { |
| 184 |
return 0; |
| 185 |
} |
| 186 |
|
| 187 |
return CBB_flush(out); |
| 188 |
} |
| 189 |
|
| 190 |
int PKCS7_set_type(PKCS7 *p7, int type) { |
| 191 |
if (p7 == NULL) { |
| 192 |
OPENSSL_PUT_ERROR(PKCS7, ERR_R_PASSED_NULL_PARAMETER); |
| 193 |
return 0; |
| 194 |
} |
| 195 |
ASN1_OBJECT *obj = OBJ_nid2obj(type); |
| 196 |
if (obj == NULL) { |
| 197 |
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_UNSUPPORTED_CONTENT_TYPE); |
| 198 |
return 0; |
| 199 |
} |
| 200 |
|
| 201 |
switch (type) { |
| 202 |
case NID_pkcs7_signed: |
| 203 |
p7->type = obj; |
| 204 |
PKCS7_SIGNED_free(p7->d.sign); |
| 205 |
p7->d.sign = PKCS7_SIGNED_new(); |
| 206 |
if (p7->d.sign == NULL) { |
| 207 |
return 0; |
| 208 |
} |
| 209 |
if (!ASN1_INTEGER_set(p7->d.sign->version, 1)) { |
| 210 |
PKCS7_SIGNED_free(p7->d.sign); |
| 211 |
p7->d.sign = NULL; |
| 212 |
return 0; |
| 213 |
} |
| 214 |
break; |
| 215 |
case NID_pkcs7_digest: |
| 216 |
p7->type = obj; |
| 217 |
PKCS7_DIGEST_free(p7->d.digest); |
| 218 |
p7->d.digest = PKCS7_DIGEST_new(); |
| 219 |
if (p7->d.digest == NULL) { |
| 220 |
return 0; |
| 221 |
} |
| 222 |
if (!ASN1_INTEGER_set(p7->d.digest->version, 0)) { |
| 223 |
PKCS7_DIGEST_free(p7->d.digest); |
| 224 |
p7->d.digest = NULL; |
| 225 |
return 0; |
| 226 |
} |
| 227 |
break; |
| 228 |
case NID_pkcs7_data: |
| 229 |
p7->type = obj; |
| 230 |
ASN1_OCTET_STRING_free(p7->d.data); |
| 231 |
p7->d.data = ASN1_OCTET_STRING_new(); |
| 232 |
if (p7->d.data == NULL) { |
| 233 |
return 0; |
| 234 |
} |
| 235 |
break; |
| 236 |
case NID_pkcs7_signedAndEnveloped: |
| 237 |
p7->type = obj; |
| 238 |
PKCS7_SIGN_ENVELOPE_free(p7->d.signed_and_enveloped); |
| 239 |
p7->d.signed_and_enveloped = PKCS7_SIGN_ENVELOPE_new(); |
| 240 |
if (p7->d.signed_and_enveloped == NULL) { |
| 241 |
return 0; |
| 242 |
} |
| 243 |
if (!ASN1_INTEGER_set(p7->d.signed_and_enveloped->version, 1)) { |
| 244 |
PKCS7_SIGN_ENVELOPE_free(p7->d.signed_and_enveloped); |
| 245 |
p7->d.signed_and_enveloped = NULL; |
| 246 |
return 0; |
| 247 |
} |
| 248 |
p7->d.signed_and_enveloped->enc_data->content_type = |
| 249 |
OBJ_nid2obj(NID_pkcs7_data); |
| 250 |
break; |
| 251 |
case NID_pkcs7_enveloped: |
| 252 |
p7->type = obj; |
| 253 |
PKCS7_ENVELOPE_free(p7->d.enveloped); |
| 254 |
p7->d.enveloped = PKCS7_ENVELOPE_new(); |
| 255 |
if (p7->d.enveloped == NULL) { |
| 256 |
return 0; |
| 257 |
} |
| 258 |
if (!ASN1_INTEGER_set(p7->d.enveloped->version, 0)) { |
| 259 |
PKCS7_ENVELOPE_free(p7->d.enveloped); |
| 260 |
p7->d.enveloped = NULL; |
| 261 |
return 0; |
| 262 |
} |
| 263 |
p7->d.enveloped->enc_data->content_type = OBJ_nid2obj(NID_pkcs7_data); |
| 264 |
break; |
| 265 |
case NID_pkcs7_encrypted: |
| 266 |
p7->type = obj; |
| 267 |
PKCS7_ENCRYPT_free(p7->d.encrypted); |
| 268 |
p7->d.encrypted = PKCS7_ENCRYPT_new(); |
| 269 |
if (p7->d.encrypted == NULL) { |
| 270 |
return 0; |
| 271 |
} |
| 272 |
if (!ASN1_INTEGER_set(p7->d.encrypted->version, 0)) { |
| 273 |
PKCS7_ENCRYPT_free(p7->d.encrypted); |
| 274 |
p7->d.encrypted = NULL; |
| 275 |
return 0; |
| 276 |
} |
| 277 |
p7->d.encrypted->enc_data->content_type = OBJ_nid2obj(NID_pkcs7_data); |
| 278 |
break; |
| 279 |
default: |
| 280 |
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_UNSUPPORTED_CONTENT_TYPE); |
| 281 |
return 0; |
| 282 |
} |
| 283 |
return 1; |
| 284 |
} |
| 285 |
|
| 286 |
int PKCS7_set_cipher(PKCS7 *p7, const EVP_CIPHER *cipher) { |
| 287 |
if (p7 == NULL || cipher == NULL) { |
| 288 |
OPENSSL_PUT_ERROR(PKCS7, ERR_R_PASSED_NULL_PARAMETER); |
| 289 |
return 0; |
| 290 |
} |
| 291 |
if (EVP_get_cipherbynid(EVP_CIPHER_nid(cipher)) == NULL) { |
| 292 |
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER); |
| 293 |
return 0; |
| 294 |
} |
| 295 |
|
| 296 |
PKCS7_ENC_CONTENT *ec; |
| 297 |
switch (OBJ_obj2nid(p7->type)) { |
| 298 |
case NID_pkcs7_signedAndEnveloped: |
| 299 |
ec = p7->d.signed_and_enveloped->enc_data; |
| 300 |
break; |
| 301 |
case NID_pkcs7_enveloped: |
| 302 |
ec = p7->d.enveloped->enc_data; |
| 303 |
break; |
| 304 |
default: |
| 305 |
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_WRONG_CONTENT_TYPE); |
| 306 |
return 0; |
| 307 |
} |
| 308 |
|
| 309 |
ec->cipher = cipher; |
| 310 |
return 1; |
| 311 |
} |
| 312 |
|
| 313 |
int PKCS7_set_content(PKCS7 *p7, PKCS7 *p7_data) { |
| 314 |
if (p7 == NULL) { |
| 315 |
OPENSSL_PUT_ERROR(PKCS7, ERR_R_PASSED_NULL_PARAMETER); |
| 316 |
return 0; |
| 317 |
} |
| 318 |
|
| 319 |
switch (OBJ_obj2nid(p7->type)) { |
| 320 |
case NID_pkcs7_signed: |
| 321 |
PKCS7_free(p7->d.sign->contents); |
| 322 |
p7->d.sign->contents = p7_data; |
| 323 |
break; |
| 324 |
case NID_pkcs7_digest: |
| 325 |
PKCS7_free(p7->d.digest->contents); |
| 326 |
p7->d.digest->contents = p7_data; |
| 327 |
break; |
| 328 |
default: |
| 329 |
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_UNSUPPORTED_CONTENT_TYPE); |
| 330 |
return 0; |
| 331 |
} |
| 332 |
return 1; |
| 333 |
} |
| 334 |
|
| 335 |
int PKCS7_content_new(PKCS7 *p7, int type) { |
| 336 |
PKCS7 *ret = PKCS7_new(); |
| 337 |
if (ret == NULL) { |
| 338 |
goto err; |
| 339 |
} |
| 340 |
if (!PKCS7_set_type(ret, type)) { |
| 341 |
goto err; |
| 342 |
} |
| 343 |
if (!PKCS7_set_content(p7, ret)) { |
| 344 |
goto err; |
| 345 |
} |
| 346 |
return 1; |
| 347 |
err: |
| 348 |
PKCS7_free(ret); |
| 349 |
return 0; |
| 350 |
} |
| 351 |
|
| 352 |
int PKCS7_add_recipient_info(PKCS7 *p7, PKCS7_RECIP_INFO *ri) { |
| 353 |
STACK_OF(PKCS7_RECIP_INFO) *sk; |
| 354 |
|
| 355 |
if (p7 == NULL || ri == NULL) { |
| 356 |
OPENSSL_PUT_ERROR(PKCS7, ERR_R_PASSED_NULL_PARAMETER); |
| 357 |
return 0; |
| 358 |
} |
| 359 |
|
| 360 |
switch (OBJ_obj2nid(p7->type)) { |
| 361 |
case NID_pkcs7_signedAndEnveloped: |
| 362 |
sk = p7->d.signed_and_enveloped->recipientinfo; |
| 363 |
break; |
| 364 |
case NID_pkcs7_enveloped: |
| 365 |
sk = p7->d.enveloped->recipientinfo; |
| 366 |
break; |
| 367 |
default: |
| 368 |
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_WRONG_CONTENT_TYPE); |
| 369 |
return 0; |
| 370 |
} |
| 371 |
|
| 372 |
if (!sk_PKCS7_RECIP_INFO_push(sk, ri)) { |
| 373 |
return 0; |
| 374 |
} |
| 375 |
return 1; |
| 376 |
} |
| 377 |
|
| 378 |
int PKCS7_add_signer(PKCS7 *p7, PKCS7_SIGNER_INFO *p7i) { |
| 379 |
GUARD_PTR(p7); |
| 380 |
GUARD_PTR(p7i); |
| 381 |
ASN1_OBJECT *obj; |
| 382 |
X509_ALGOR *alg; |
| 383 |
STACK_OF(PKCS7_SIGNER_INFO) *signer_sk; |
| 384 |
STACK_OF(X509_ALGOR) *md_sk; |
| 385 |
|
| 386 |
switch (OBJ_obj2nid(p7->type)) { |
| 387 |
case NID_pkcs7_signed: |
| 388 |
signer_sk = p7->d.sign->signer_info; |
| 389 |
md_sk = p7->d.sign->md_algs; |
| 390 |
break; |
| 391 |
case NID_pkcs7_signedAndEnveloped: |
| 392 |
signer_sk = p7->d.signed_and_enveloped->signer_info; |
| 393 |
md_sk = p7->d.signed_and_enveloped->md_algs; |
| 394 |
break; |
| 395 |
default: |
| 396 |
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_WRONG_CONTENT_TYPE); |
| 397 |
return 0; |
| 398 |
} |
| 399 |
|
| 400 |
obj = p7i->digest_alg->algorithm; |
| 401 |
// If the digest is not currently listed, add it |
| 402 |
int alg_found = 0; |
| 403 |
for (size_t i = 0; i < sk_X509_ALGOR_num(md_sk); i++) { |
| 404 |
alg = sk_X509_ALGOR_value(md_sk, i); |
| 405 |
if (OBJ_cmp(obj, alg->algorithm) == 0) { |
| 406 |
alg_found = 1; |
| 407 |
break; |
| 408 |
} |
| 409 |
} |
| 410 |
if (!alg_found) { |
| 411 |
if ((alg = X509_ALGOR_new()) == NULL || |
| 412 |
(alg->parameter = ASN1_TYPE_new()) == NULL) { |
| 413 |
X509_ALGOR_free(alg); |
| 414 |
OPENSSL_PUT_ERROR(PKCS7, ERR_R_ASN1_LIB); |
| 415 |
return 0; |
| 416 |
} |
| 417 |
// If there is a constant copy of the ASN1 OBJECT in libcrypto, then |
| 418 |
// use that. Otherwise, use a dynamically duplicated copy. |
| 419 |
int nid = OBJ_obj2nid(obj); |
| 420 |
if (nid != NID_undef) { |
| 421 |
alg->algorithm = OBJ_nid2obj(nid); |
| 422 |
} else { |
| 423 |
alg->algorithm = OBJ_dup(obj); |
| 424 |
} |
| 425 |
alg->parameter->type = V_ASN1_NULL; |
| 426 |
if (alg->algorithm == NULL || !sk_X509_ALGOR_push(md_sk, alg)) { |
| 427 |
X509_ALGOR_free(alg); |
| 428 |
return 0; |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
if (!sk_PKCS7_SIGNER_INFO_push(signer_sk, p7i)) { |
| 433 |
return 0; |
| 434 |
} |
| 435 |
return 1; |
| 436 |
} |
| 437 |
|
| 438 |
static ASN1_TYPE *get_attribute(STACK_OF(X509_ATTRIBUTE) *sk, int nid) { |
| 439 |
for (size_t i = 0; i < sk_X509_ATTRIBUTE_num(sk); i++) { |
| 440 |
X509_ATTRIBUTE *attr = sk_X509_ATTRIBUTE_value(sk, i); |
| 441 |
ASN1_OBJECT *obj = X509_ATTRIBUTE_get0_object(attr); |
| 442 |
if (OBJ_obj2nid(obj) == nid) { |
| 443 |
return X509_ATTRIBUTE_get0_type(attr, 0); |
| 444 |
} |
| 445 |
} |
| 446 |
return NULL; |
| 447 |
} |
| 448 |
|
| 449 |
ASN1_TYPE *PKCS7_get_signed_attribute(const PKCS7_SIGNER_INFO *si, int nid) { |
| 450 |
if (si == NULL) { |
| 451 |
OPENSSL_PUT_ERROR(PKCS7, ERR_R_PASSED_NULL_PARAMETER); |
| 452 |
return NULL; |
| 453 |
} |
| 454 |
return get_attribute(si->auth_attr, nid); |
| 455 |
} |
| 456 |
|
| 457 |
static ASN1_OCTET_STRING *PKCS7_digest_from_attributes( |
| 458 |
STACK_OF(X509_ATTRIBUTE) *sk) { |
| 459 |
if (sk == NULL) { |
| 460 |
OPENSSL_PUT_ERROR(PKCS7, ERR_R_PASSED_NULL_PARAMETER); |
| 461 |
return NULL; |
| 462 |
} |
| 463 |
ASN1_TYPE *astype = get_attribute(sk, NID_pkcs9_messageDigest); |
| 464 |
if (astype == NULL || astype->type != V_ASN1_OCTET_STRING) { |
| 465 |
return NULL; |
| 466 |
} |
| 467 |
return astype->value.octet_string; |
| 468 |
} |
| 469 |
|
| 470 |
STACK_OF(PKCS7_SIGNER_INFO) *PKCS7_get_signer_info(PKCS7 *p7) { |
| 471 |
if (p7 == NULL || p7->d.ptr == NULL) { |
| 472 |
OPENSSL_PUT_ERROR(PKCS7, ERR_R_PASSED_NULL_PARAMETER); |
| 473 |
return NULL; |
| 474 |
} |
| 475 |
|
| 476 |
switch (OBJ_obj2nid(p7->type)) { |
| 477 |
case NID_pkcs7_signed: |
| 478 |
return p7->d.sign->signer_info; |
| 479 |
case NID_pkcs7_signedAndEnveloped: |
| 480 |
return p7->d.signed_and_enveloped->signer_info; |
| 481 |
default: |
| 482 |
return NULL; |
| 483 |
} |
| 484 |
} |
| 485 |
|
| 486 |
int PKCS7_SIGNER_INFO_set(PKCS7_SIGNER_INFO *p7i, X509 *x509, EVP_PKEY *pkey, |
| 487 |
const EVP_MD *dgst) { |
| 488 |
if (!p7i || !x509 || !pkey || !dgst) { |
| 489 |
OPENSSL_PUT_ERROR(PKCS7, ERR_R_PASSED_NULL_PARAMETER); |
| 490 |
return 0; |
| 491 |
} else if (!ASN1_INTEGER_set(p7i->version, 1)) { |
| 492 |
return 0; |
| 493 |
} else if (!X509_NAME_set(&p7i->issuer_and_serial->issuer, |
| 494 |
X509_get_issuer_name(x509))) { |
| 495 |
return 0; |
| 496 |
} |
| 497 |
|
| 498 |
ASN1_INTEGER_free(p7i->issuer_and_serial->serial); |
| 499 |
if (!(p7i->issuer_and_serial->serial = |
| 500 |
ASN1_INTEGER_dup(X509_get0_serialNumber(x509)))) { |
| 501 |
return 0; |
| 502 |
} |
| 503 |
|
| 504 |
// NOTE: OpenSSL does not free |p7i->pkey| before setting it. we do so here |
| 505 |
// to avoid potential memory leaks. |
| 506 |
EVP_PKEY_free(p7i->pkey); |
| 507 |
EVP_PKEY_up_ref(pkey); |
| 508 |
p7i->pkey = pkey; |
| 509 |
|
| 510 |
if (!X509_ALGOR_set0(p7i->digest_alg, OBJ_nid2obj(EVP_MD_type(dgst)), |
| 511 |
V_ASN1_NULL, NULL)) { |
| 512 |
return 0; |
| 513 |
} |
| 514 |
|
| 515 |
switch (EVP_PKEY_id(pkey)) { |
| 516 |
case EVP_PKEY_EC: |
| 517 |
case EVP_PKEY_DH: { |
| 518 |
int snid, hnid; |
| 519 |
X509_ALGOR *alg1, *alg2; |
| 520 |
PKCS7_SIGNER_INFO_get0_algs(p7i, NULL, &alg1, &alg2); |
| 521 |
if (alg1 == NULL || alg1->algorithm == NULL) { |
| 522 |
return 0; |
| 523 |
} |
| 524 |
hnid = OBJ_obj2nid(alg1->algorithm); |
| 525 |
if (hnid == NID_undef || |
| 526 |
!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)) || |
| 527 |
!X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, NULL)) { |
| 528 |
return 0; |
| 529 |
} |
| 530 |
break; |
| 531 |
} |
| 532 |
case EVP_PKEY_RSA: |
| 533 |
case EVP_PKEY_RSA_PSS: { |
| 534 |
X509_ALGOR *alg = NULL; |
| 535 |
PKCS7_SIGNER_INFO_get0_algs(p7i, NULL, NULL, &alg); |
| 536 |
if (alg != NULL) { |
| 537 |
return X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption), V_ASN1_NULL, |
| 538 |
NULL); |
| 539 |
} |
| 540 |
break; |
| 541 |
} |
| 542 |
default: |
| 543 |
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_SIGNING_NOT_SUPPORTED_FOR_THIS_KEY_TYPE); |
| 544 |
return 0; |
| 545 |
} |
| 546 |
|
| 547 |
return 1; |
| 548 |
} |
| 549 |
|
| 550 |
int PKCS7_RECIP_INFO_set(PKCS7_RECIP_INFO *p7i, X509 *x509) { |
| 551 |
if (!p7i || !x509) { |
| 552 |
OPENSSL_PUT_ERROR(PKCS7, ERR_R_PASSED_NULL_PARAMETER); |
| 553 |
return 0; |
| 554 |
} |
| 555 |
if (!ASN1_INTEGER_set(p7i->version, 0)) { |
| 556 |
return 0; |
| 557 |
} else if (!X509_NAME_set(&p7i->issuer_and_serial->issuer, |
| 558 |
X509_get_issuer_name(x509))) { |
| 559 |
return 0; |
| 560 |
} |
| 561 |
|
| 562 |
ASN1_INTEGER_free(p7i->issuer_and_serial->serial); |
| 563 |
if (!(p7i->issuer_and_serial->serial = |
| 564 |
ASN1_INTEGER_dup(X509_get0_serialNumber(x509)))) { |
| 565 |
return 0; |
| 566 |
} |
| 567 |
|
| 568 |
EVP_PKEY *pkey = X509_get0_pubkey(x509); |
| 569 |
if (pkey == NULL) { |
| 570 |
return 0; |
| 571 |
} |
| 572 |
|
| 573 |
if (EVP_PKEY_id(pkey) == EVP_PKEY_RSA_PSS) { |
| 574 |
return 0; |
| 575 |
} else if (EVP_PKEY_id(pkey) == EVP_PKEY_RSA) { |
| 576 |
X509_ALGOR *alg; |
| 577 |
PKCS7_RECIP_INFO_get0_alg(p7i, &alg); |
| 578 |
if (!X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption), V_ASN1_NULL, |
| 579 |
NULL)) { |
| 580 |
return 0; |
| 581 |
} |
| 582 |
} |
| 583 |
|
| 584 |
// NOTE: OpenSSL does not free |p7i->cert| before setting it. we do so here |
| 585 |
// to avoid potential memory leaks. |
| 586 |
X509_free(p7i->cert); |
| 587 |
X509_up_ref(x509); |
| 588 |
p7i->cert = x509; |
| 589 |
|
| 590 |
return 1; |
| 591 |
} |
| 592 |
|
| 593 |
void PKCS7_SIGNER_INFO_get0_algs(PKCS7_SIGNER_INFO *si, EVP_PKEY **pk, |
| 594 |
X509_ALGOR **pdig, X509_ALGOR **psig) { |
| 595 |
if (!si) { |
| 596 |
return; |
| 597 |
} |
| 598 |
if (pk) { |
| 599 |
*pk = si->pkey; |
| 600 |
} |
| 601 |
if (pdig) { |
| 602 |
*pdig = si->digest_alg; |
| 603 |
} |
| 604 |
if (psig) { |
| 605 |
*psig = si->digest_enc_alg; |
| 606 |
} |
| 607 |
} |
| 608 |
|
| 609 |
void PKCS7_RECIP_INFO_get0_alg(PKCS7_RECIP_INFO *ri, X509_ALGOR **penc) { |
| 610 |
if (!ri) { |
| 611 |
return; |
| 612 |
} |
| 613 |
if (penc) { |
| 614 |
*penc = ri->key_enc_algor; |
| 615 |
} |
| 616 |
} |
| 617 |
|
| 618 |
static ASN1_OCTET_STRING *PKCS7_get_octet_string(PKCS7 *p7) { |
| 619 |
GUARD_PTR(p7); |
| 620 |
if (PKCS7_type_is_data(p7)) { |
| 621 |
return p7->d.data; |
| 622 |
} |
| 623 |
return NULL; |
| 624 |
} |
| 625 |
|
| 626 |
static int pkcs7_bio_add_digest(BIO **pbio, X509_ALGOR *alg) { |
| 627 |
GUARD_PTR(pbio); |
| 628 |
GUARD_PTR(alg); |
| 629 |
BIO *btmp = NULL; |
| 630 |
|
| 631 |
const EVP_MD *md = EVP_get_digestbynid(OBJ_obj2nid(alg->algorithm)); |
| 632 |
if (md == NULL) { |
| 633 |
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_UNKNOWN_DIGEST_TYPE); |
| 634 |
goto err; |
| 635 |
} |
| 636 |
|
| 637 |
if ((btmp = BIO_new(BIO_f_md())) == NULL) { |
| 638 |
OPENSSL_PUT_ERROR(PKCS7, ERR_R_BIO_LIB); |
| 639 |
goto err; |
| 640 |
} |
| 641 |
|
| 642 |
if (BIO_set_md(btmp, (EVP_MD *)md) <= 0) { |
| 643 |
OPENSSL_PUT_ERROR(PKCS7, ERR_R_BIO_LIB); |
| 644 |
goto err; |
| 645 |
} |
| 646 |
if (*pbio == NULL) { |
| 647 |
*pbio = btmp; |
| 648 |
} else if (!BIO_push(*pbio, btmp)) { |
| 649 |
OPENSSL_PUT_ERROR(PKCS7, ERR_R_BIO_LIB); |
| 650 |
goto err; |
| 651 |
} |
| 652 |
|
| 653 |
return 1; |
| 654 |
|
| 655 |
err: |
| 656 |
BIO_free(btmp); |
| 657 |
return 0; |
| 658 |
} |
| 659 |
|
| 660 |
static int pkcs7_encode_rinfo(PKCS7_RECIP_INFO *ri, unsigned char *key, |
| 661 |
int keylen) { |
| 662 |
GUARD_PTR(ri); |
| 663 |
GUARD_PTR(key); |
| 664 |
EVP_PKEY_CTX *pctx = NULL; |
| 665 |
EVP_PKEY *pkey = NULL; |
| 666 |
unsigned char *ek = NULL; |
| 667 |
int ret = 0; |
| 668 |
size_t eklen; |
| 669 |
|
| 670 |
if ((pkey = X509_get0_pubkey(ri->cert)) == NULL) { |
| 671 |
goto err; |
| 672 |
} |
| 673 |
|
| 674 |
pctx = EVP_PKEY_CTX_new(pkey, NULL); |
| 675 |
if (pctx == NULL || EVP_PKEY_encrypt_init(pctx) <= 0 || |
| 676 |
EVP_PKEY_encrypt(pctx, NULL, &eklen, key, keylen) <= 0 || |
| 677 |
(NULL == (ek = OPENSSL_malloc(eklen))) || |
| 678 |
EVP_PKEY_encrypt(pctx, ek, &eklen, key, keylen) <= 0) { |
| 679 |
goto err; |
| 680 |
} |
| 681 |
|
| 682 |
ASN1_STRING_set0(ri->enc_key, ek, eklen); |
| 683 |
ek = NULL; // NULL out |ek| ptr because |ri| takes ownership of the alloc |
| 684 |
|
| 685 |
ret = 1; |
| 686 |
|
| 687 |
err: |
| 688 |
EVP_PKEY_CTX_free(pctx); |
| 689 |
OPENSSL_free(ek); |
| 690 |
return ret; |
| 691 |
} |
| 692 |
|
| 693 |
BIO *PKCS7_dataInit(PKCS7 *p7, BIO *bio) { |
| 694 |
GUARD_PTR(p7); |
| 695 |
BIO *out = NULL, *btmp = NULL; |
| 696 |
const EVP_CIPHER *evp_cipher = NULL; |
| 697 |
STACK_OF(PKCS7_RECIP_INFO) *rsk = NULL; |
| 698 |
STACK_OF(X509_ALGOR) *md_sk = NULL; |
| 699 |
X509_ALGOR *xalg = NULL; |
| 700 |
PKCS7_RECIP_INFO *ri = NULL; |
| 701 |
ASN1_OCTET_STRING *content = NULL; |
| 702 |
|
| 703 |
// The content field in the PKCS7 ContentInfo is optional, but that only |
| 704 |
// applies to inner content (precisely, detached signatures). When reading |
| 705 |
// content, missing outer content is therefore treated as an error. When |
| 706 |
// creating content, PKCS7_content_new() must be called before calling this |
| 707 |
// method, so a NULL p7->d is always an error. |
| 708 |
if (p7->d.ptr == NULL) { |
| 709 |
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_NO_CONTENT); |
| 710 |
return NULL; |
| 711 |
} |
| 712 |
|
| 713 |
switch (OBJ_obj2nid(p7->type)) { |
| 714 |
case NID_pkcs7_signed: |
| 715 |
md_sk = p7->d.sign->md_algs; |
| 716 |
content = PKCS7_get_octet_string(p7->d.sign->contents); |
| 717 |
break; |
| 718 |
case NID_pkcs7_signedAndEnveloped: |
| 719 |
md_sk = p7->d.signed_and_enveloped->md_algs; |
| 720 |
rsk = p7->d.signed_and_enveloped->recipientinfo; |
| 721 |
xalg = p7->d.signed_and_enveloped->enc_data->algorithm; |
| 722 |
evp_cipher = p7->d.signed_and_enveloped->enc_data->cipher; |
| 723 |
if (evp_cipher == NULL) { |
| 724 |
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_CIPHER_NOT_INITIALIZED); |
| 725 |
goto err; |
| 726 |
} |
| 727 |
break; |
| 728 |
case NID_pkcs7_enveloped: |
| 729 |
rsk = p7->d.enveloped->recipientinfo; |
| 730 |
xalg = p7->d.enveloped->enc_data->algorithm; |
| 731 |
evp_cipher = p7->d.enveloped->enc_data->cipher; |
| 732 |
if (evp_cipher == NULL) { |
| 733 |
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_CIPHER_NOT_INITIALIZED); |
| 734 |
goto err; |
| 735 |
} |
| 736 |
break; |
| 737 |
case NID_pkcs7_digest: |
| 738 |
content = PKCS7_get_octet_string(p7->d.digest->contents); |
| 739 |
if (!pkcs7_bio_add_digest(&out, p7->d.digest->digest_alg)) { |
| 740 |
goto err; |
| 741 |
} |
| 742 |
break; |
| 743 |
case NID_pkcs7_data: |
| 744 |
break; |
| 745 |
default: |
| 746 |
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_UNSUPPORTED_CONTENT_TYPE); |
| 747 |
goto err; |
| 748 |
} |
| 749 |
|
| 750 |
|
| 751 |
for (size_t i = 0; i < sk_X509_ALGOR_num(md_sk); i++) { |
| 752 |
if (!pkcs7_bio_add_digest(&out, sk_X509_ALGOR_value(md_sk, i))) { |
| 753 |
goto err; |
| 754 |
} |
| 755 |
} |
| 756 |
|
| 757 |
if (evp_cipher != NULL) { |
| 758 |
unsigned char key[EVP_MAX_KEY_LENGTH]; |
| 759 |
unsigned char iv[EVP_MAX_IV_LENGTH]; |
| 760 |
int keylen, ivlen; |
| 761 |
EVP_CIPHER_CTX *ctx; |
| 762 |
|
| 763 |
if ((btmp = BIO_new(BIO_f_cipher())) == NULL) { |
| 764 |
OPENSSL_PUT_ERROR(PKCS7, ERR_R_BIO_LIB); |
| 765 |
goto err; |
| 766 |
} |
| 767 |
if (!BIO_get_cipher_ctx(btmp, &ctx)) { |
| 768 |
OPENSSL_PUT_ERROR(PKCS7, ERR_R_BIO_LIB); |
| 769 |
goto err; |
| 770 |
} |
| 771 |
keylen = EVP_CIPHER_key_length(evp_cipher); |
| 772 |
ivlen = EVP_CIPHER_iv_length(evp_cipher); |
| 773 |
ASN1_OBJECT_free(xalg->algorithm); |
| 774 |
xalg->algorithm = OBJ_nid2obj(EVP_CIPHER_nid(evp_cipher)); |
| 775 |
if (ivlen > 0) { |
| 776 |
AWSLC_ABORT_IF_NOT_ONE(RAND_bytes(iv, ivlen)); |
| 777 |
} |
| 778 |
if (keylen > 0) { |
| 779 |
AWSLC_ABORT_IF_NOT_ONE(RAND_bytes(key, keylen)); |
| 780 |
} |
| 781 |
|
| 782 |
if (EVP_CipherInit_ex(ctx, evp_cipher, NULL, key, iv, /*enc*/ 1) <= 0) { |
| 783 |
OPENSSL_cleanse(key, keylen); |
| 784 |
goto err; |
| 785 |
} |
| 786 |
|
| 787 |
if (ivlen > 0) { |
| 788 |
ASN1_TYPE_free(xalg->parameter); |
| 789 |
xalg->parameter = ASN1_TYPE_new(); |
| 790 |
if (xalg->parameter == NULL) { |
| 791 |
goto err; |
| 792 |
} |
| 793 |
xalg->parameter->type = V_ASN1_OCTET_STRING; |
| 794 |
xalg->parameter->value.octet_string = ASN1_OCTET_STRING_new(); |
| 795 |
// Set |p7|'s parameter value to the IV |
| 796 |
if (!ASN1_OCTET_STRING_set(xalg->parameter->value.octet_string, iv, |
| 797 |
ivlen)) { |
| 798 |
goto err; |
| 799 |
} |
| 800 |
} |
| 801 |
|
| 802 |
for (size_t i = 0; i < sk_PKCS7_RECIP_INFO_num(rsk); i++) { |
| 803 |
ri = sk_PKCS7_RECIP_INFO_value(rsk, i); |
| 804 |
if (pkcs7_encode_rinfo(ri, key, keylen) <= 0) { |
| 805 |
OPENSSL_cleanse(key, keylen); |
| 806 |
goto err; |
| 807 |
} |
| 808 |
} |
| 809 |
OPENSSL_cleanse(key, keylen); |
| 810 |
|
| 811 |
if (out == NULL) { |
| 812 |
out = btmp; |
| 813 |
} else { |
| 814 |
BIO_push(out, btmp); |
| 815 |
} |
| 816 |
btmp = NULL; |
| 817 |
} |
| 818 |
|
| 819 |
if (bio == NULL) { |
| 820 |
bio = BIO_new(BIO_s_mem()); |
| 821 |
if (bio == NULL) { |
| 822 |
goto err; |
| 823 |
} |
| 824 |
BIO_set_mem_eof_return(bio, /*eof_value*/ 0); |
| 825 |
if (!PKCS7_is_detached(p7) && content && content->length > 0) { |
| 826 |
// |bio| needs a copy of |content->data| instead of a pointer because the |
| 827 |
// data may be used after |content| has been freed |
| 828 |
if (BIO_write(bio, content->data, content->length) != content->length) { |
| 829 |
BIO_free(bio); |
| 830 |
goto err; |
| 831 |
} |
| 832 |
} |
| 833 |
} |
| 834 |
if (out) { |
| 835 |
BIO_push(out, bio); |
| 836 |
} else { |
| 837 |
out = bio; |
| 838 |
} |
| 839 |
return out; |
| 840 |
|
| 841 |
err: |
| 842 |
BIO_free_all(out); |
| 843 |
BIO_free_all(btmp); |
| 844 |
return NULL; |
| 845 |
} |
| 846 |
|
| 847 |
int PKCS7_is_detached(PKCS7 *p7) { |
| 848 |
GUARD_PTR(p7); |
| 849 |
if (PKCS7_type_is_signed(p7)) { |
| 850 |
return (p7->d.sign == NULL || p7->d.sign->contents->d.ptr == NULL); |
| 851 |
} |
| 852 |
return 0; |
| 853 |
} |
| 854 |
|
| 855 |
int PKCS7_set_detached(PKCS7 *p7, int detach) { |
| 856 |
GUARD_PTR(p7); |
| 857 |
if (detach != 0 && detach != 1) { |
| 858 |
// |detach| is meant to be used as a boolean int. |
| 859 |
return 0; |
| 860 |
} |
| 861 |
|
| 862 |
if (PKCS7_type_is_signed(p7)) { |
| 863 |
if (p7->d.sign == NULL) { |
| 864 |
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_NO_CONTENT); |
| 865 |
return 0; |
| 866 |
} |
| 867 |
if (detach && PKCS7_type_is_data(p7->d.sign->contents)) { |
| 868 |
ASN1_OCTET_STRING_free(p7->d.sign->contents->d.data); |
| 869 |
p7->d.sign->contents->d.data = NULL; |
| 870 |
} |
| 871 |
return 1; |
| 872 |
} else { |
| 873 |
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE); |
| 874 |
return 0; |
| 875 |
} |
| 876 |
} |
| 877 |
|
| 878 |
int PKCS7_get_detached(PKCS7 *p7) { return PKCS7_is_detached(p7); } |
| 879 |
|
| 880 |
|
| 881 |
static BIO *pkcs7_find_digest(EVP_MD_CTX **pmd, BIO *bio, int nid) { |
| 882 |
GUARD_PTR(pmd); |
| 883 |
while (bio != NULL) { |
| 884 |
bio = BIO_find_type(bio, BIO_TYPE_MD); |
| 885 |
if (bio == NULL) { |
| 886 |
return NULL; |
| 887 |
} |
| 888 |
if (!BIO_get_md_ctx(bio, pmd) || *pmd == NULL) { |
| 889 |
OPENSSL_PUT_ERROR(PKCS7, ERR_R_INTERNAL_ERROR); |
| 890 |
return NULL; |
| 891 |
} |
| 892 |
if (EVP_MD_CTX_type(*pmd) == nid) { |
| 893 |
return bio; |
| 894 |
} |
| 895 |
bio = BIO_next(bio); |
| 896 |
} |
| 897 |
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST); |
| 898 |
return NULL; |
| 899 |
} |
| 900 |
|
| 901 |
int PKCS7_set_digest(PKCS7 *p7, const EVP_MD *md) { |
| 902 |
GUARD_PTR(p7); |
| 903 |
GUARD_PTR(md); |
| 904 |
switch (OBJ_obj2nid(p7->type)) { |
| 905 |
case NID_pkcs7_digest: |
| 906 |
if (EVP_MD_nid(md) == NID_undef) { |
| 907 |
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_UNKNOWN_DIGEST_TYPE); |
| 908 |
return 0; |
| 909 |
} |
| 910 |
if (p7->d.digest->digest_alg == NULL) { |
| 911 |
OPENSSL_PUT_ERROR(PKCS7, ERR_R_ASN1_LIB); |
| 912 |
return 0; |
| 913 |
} |
| 914 |
OPENSSL_free(p7->d.digest->digest_alg->parameter); |
| 915 |
if ((p7->d.digest->digest_alg->parameter = ASN1_TYPE_new()) == NULL) { |
| 916 |
OPENSSL_PUT_ERROR(PKCS7, ERR_R_ASN1_LIB); |
| 917 |
return 0; |
| 918 |
} |
| 919 |
p7->d.digest->md = md; |
| 920 |
p7->d.digest->digest_alg->parameter->type = V_ASN1_NULL; |
| 921 |
p7->d.digest->digest_alg->algorithm = OBJ_nid2obj(EVP_MD_nid(md)); |
| 922 |
return 1; |
| 923 |
default: |
| 924 |
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_WRONG_CONTENT_TYPE); |
| 925 |
return 0; |
| 926 |
} |
| 927 |
} |
| 928 |
|
| 929 |
|
| 930 |
STACK_OF(PKCS7_RECIP_INFO) *PKCS7_get_recipient_info(PKCS7 *p7) { |
| 931 |
GUARD_PTR(p7); |
| 932 |
GUARD_PTR(p7->d.ptr); |
| 933 |
switch (OBJ_obj2nid(p7->type)) { |
| 934 |
case NID_pkcs7_enveloped: |
| 935 |
return p7->d.enveloped->recipientinfo; |
| 936 |
default: |
| 937 |
return NULL; |
| 938 |
} |
| 939 |
} |
| 940 |
|
| 941 |
int PKCS7_dataFinal(PKCS7 *p7, BIO *bio) { |
| 942 |
GUARD_PTR(p7); |
| 943 |
GUARD_PTR(bio); |
| 944 |
int ret = 0; |
| 945 |
BIO *bio_tmp = NULL; |
| 946 |
PKCS7_SIGNER_INFO *si; |
| 947 |
EVP_MD_CTX *md_ctx = NULL, *md_ctx_tmp; |
| 948 |
STACK_OF(PKCS7_SIGNER_INFO) *si_sk = NULL; |
| 949 |
ASN1_OCTET_STRING *content = NULL; |
| 950 |
|
| 951 |
if (p7->d.ptr == NULL) { |
| 952 |
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_NO_CONTENT); |
| 953 |
return 0; |
| 954 |
} |
| 955 |
|
| 956 |
md_ctx_tmp = EVP_MD_CTX_new(); |
| 957 |
if (md_ctx_tmp == NULL) { |
| 958 |
OPENSSL_PUT_ERROR(PKCS7, ERR_R_EVP_LIB); |
| 959 |
return 0; |
| 960 |
} |
| 961 |
|
| 962 |
switch (OBJ_obj2nid(p7->type)) { |
| 963 |
case NID_pkcs7_data: |
| 964 |
content = p7->d.data; |
| 965 |
break; |
| 966 |
case NID_pkcs7_signedAndEnveloped: |
| 967 |
si_sk = p7->d.signed_and_enveloped->signer_info; |
| 968 |
content = p7->d.signed_and_enveloped->enc_data->enc_data; |
| 969 |
if (content == NULL) { |
| 970 |
content = ASN1_OCTET_STRING_new(); |
| 971 |
if (content == NULL) { |
| 972 |
OPENSSL_PUT_ERROR(PKCS7, ERR_R_ASN1_LIB); |
| 973 |
goto err; |
| 974 |
} |
| 975 |
p7->d.signed_and_enveloped->enc_data->enc_data = content; |
| 976 |
} |
| 977 |
break; |
| 978 |
case NID_pkcs7_enveloped: |
| 979 |
content = p7->d.enveloped->enc_data->enc_data; |
| 980 |
if (content == NULL) { |
| 981 |
content = ASN1_OCTET_STRING_new(); |
| 982 |
if (content == NULL) { |
| 983 |
OPENSSL_PUT_ERROR(PKCS7, ERR_R_ASN1_LIB); |
| 984 |
goto err; |
| 985 |
} |
| 986 |
p7->d.enveloped->enc_data->enc_data = content; |
| 987 |
} |
| 988 |
break; |
| 989 |
case NID_pkcs7_signed: |
| 990 |
si_sk = p7->d.sign->signer_info; |
| 991 |
// clang-format off |
| 992 |
content = PKCS7_get_octet_string(p7->d.sign->contents); |
| 993 |
// If detached data then the content is excluded |
| 994 |
if (PKCS7_type_is_data(p7->d.sign->contents) && PKCS7_is_detached(p7)) { |
| 995 |
// clang-format on |
| 996 |
ASN1_OCTET_STRING_free(content); |
| 997 |
content = NULL; |
| 998 |
p7->d.sign->contents->d.data = NULL; |
| 999 |
} |
| 1000 |
break; |
| 1001 |
|
| 1002 |
case NID_pkcs7_digest: |
| 1003 |
content = PKCS7_get_octet_string(p7->d.digest->contents); |
| 1004 |
// If detached data, then the content is excluded |
| 1005 |
if (PKCS7_type_is_data(p7->d.digest->contents) && PKCS7_is_detached(p7)) { |
| 1006 |
ASN1_OCTET_STRING_free(content); |
| 1007 |
content = NULL; |
| 1008 |
p7->d.digest->contents->d.data = NULL; |
| 1009 |
} |
| 1010 |
break; |
| 1011 |
|
| 1012 |
default: |
| 1013 |
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_UNSUPPORTED_CONTENT_TYPE); |
| 1014 |
goto err; |
| 1015 |
} |
| 1016 |
|
| 1017 |
if (si_sk != NULL) { |
| 1018 |
for (size_t ii = 0; ii < sk_PKCS7_SIGNER_INFO_num(si_sk); ii++) { |
| 1019 |
si = sk_PKCS7_SIGNER_INFO_value(si_sk, ii); |
| 1020 |
if (si == NULL || si->pkey == NULL) { |
| 1021 |
continue; |
| 1022 |
} |
| 1023 |
int sign_nid = OBJ_obj2nid(si->digest_alg->algorithm); |
| 1024 |
bio_tmp = pkcs7_find_digest(&md_ctx, bio, sign_nid); |
| 1025 |
if (bio_tmp == NULL) { |
| 1026 |
goto err; |
| 1027 |
} |
| 1028 |
// We now have the EVP_MD_CTX, lets do the signing. |
| 1029 |
if (!EVP_MD_CTX_copy_ex(md_ctx_tmp, md_ctx)) { |
| 1030 |
goto err; |
| 1031 |
} |
| 1032 |
// We don't currently support signed attributes |
| 1033 |
if (sk_X509_ATTRIBUTE_num(si->auth_attr) > 0) { |
| 1034 |
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_PKCS7_DATASIGN); |
| 1035 |
goto err; |
| 1036 |
} |
| 1037 |
unsigned char *abuf = NULL; |
| 1038 |
unsigned int abuflen = EVP_PKEY_size(si->pkey); |
| 1039 |
if (abuflen == 0 || (abuf = OPENSSL_malloc(abuflen)) == NULL) { |
| 1040 |
goto err; |
| 1041 |
} |
| 1042 |
// |md_ctx_tmp| was initialized by |BIO_set_md| called on |bio|. Do not |
| 1043 |
// modify that context, as it contains the content digest, and we need |
| 1044 |
// to calculate the signature over it. Proceed to finalization. |
| 1045 |
if (!EVP_SignFinal(md_ctx_tmp, abuf, &abuflen, si->pkey)) { |
| 1046 |
OPENSSL_free(abuf); |
| 1047 |
OPENSSL_PUT_ERROR(PKCS7, ERR_R_EVP_LIB); |
| 1048 |
goto err; |
| 1049 |
} |
| 1050 |
ASN1_STRING_set0(si->enc_digest, abuf, abuflen); |
| 1051 |
} |
| 1052 |
} else if (OBJ_obj2nid(p7->type) == NID_pkcs7_digest) { |
| 1053 |
unsigned char md_data[EVP_MAX_MD_SIZE]; |
| 1054 |
unsigned int md_len; |
| 1055 |
if (!pkcs7_find_digest(&md_ctx, bio, EVP_MD_nid(p7->d.digest->md)) || |
| 1056 |
!EVP_DigestFinal_ex(md_ctx, md_data, &md_len) || |
| 1057 |
!ASN1_OCTET_STRING_set(p7->d.digest->digest, md_data, md_len)) { |
| 1058 |
goto err; |
| 1059 |
} |
| 1060 |
} |
| 1061 |
|
| 1062 |
if (!PKCS7_is_detached(p7)) { |
| 1063 |
if (content == NULL) { |
| 1064 |
goto err; |
| 1065 |
} |
| 1066 |
const uint8_t *cont; |
| 1067 |
size_t contlen; |
| 1068 |
bio_tmp = BIO_find_type(bio, BIO_TYPE_MEM); |
| 1069 |
if (bio_tmp == NULL) { |
| 1070 |
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_UNABLE_TO_FIND_MEM_BIO); |
| 1071 |
goto err; |
| 1072 |
} |
| 1073 |
if (!BIO_mem_contents(bio_tmp, &cont, &contlen)) { |
| 1074 |
goto err; |
| 1075 |
} |
| 1076 |
// Mark the BIO read only then we can use its copy of the data instead of |
| 1077 |
// making an extra copy. |content| will intentionally point directly into |
| 1078 |
// the relevant BIO's buffer, as the BIO's lifetime is owned by |p7|. |
| 1079 |
BIO_set_flags(bio_tmp, BIO_FLAGS_MEM_RDONLY); |
| 1080 |
BIO_set_mem_eof_return(bio_tmp, /*eof_value*/ 0); |
| 1081 |
ASN1_STRING_set0(content, (unsigned char *)cont, contlen); |
| 1082 |
} |
| 1083 |
|
| 1084 |
ret = 1; |
| 1085 |
err: |
| 1086 |
EVP_MD_CTX_free(md_ctx_tmp); |
| 1087 |
return ret; |
| 1088 |
} |
| 1089 |
|
| 1090 |
// pkcs7_bio_copy_content copies the contents of |src| into |dst|. |src| must be |
| 1091 |
// non-null. If |dst| is null, |src| is read from until empty and its contents |
| 1092 |
// are discarded. If |dst| is present, only full copies are considered |
| 1093 |
// successful. It returns 1 on success and 0 on failure. |
| 1094 |
static int pkcs7_bio_copy_content(BIO *src, BIO *dst) { |
| 1095 |
uint8_t buf[1024]; |
| 1096 |
int bytes_processed = 0, ret = 0; |
| 1097 |
while ((bytes_processed = BIO_read(src, buf, sizeof(buf))) > 0) { |
| 1098 |
if (dst && !BIO_write_all(dst, buf, bytes_processed)) { |
| 1099 |
goto err; |
| 1100 |
} |
| 1101 |
} |
| 1102 |
if (bytes_processed < 0 || (dst && !BIO_flush(dst))) { |
| 1103 |
goto err; |
| 1104 |
} |
| 1105 |
ret = 1; |
| 1106 |
err: |
| 1107 |
OPENSSL_cleanse(buf, sizeof(buf)); |
| 1108 |
return ret; |
| 1109 |
} |
| 1110 |
|
| 1111 |
// PKCS7_final copies the contents of |data| into |p7| before finalizing |p7|. |
| 1112 |
int pkcs7_final(PKCS7 *p7, BIO *data) { |
| 1113 |
BIO *p7bio; |
| 1114 |
int ret = 0; |
| 1115 |
|
| 1116 |
if ((p7bio = PKCS7_dataInit(p7, NULL)) == NULL) { |
| 1117 |
OPENSSL_PUT_ERROR(PKCS7, ERR_R_PKCS7_LIB); |
| 1118 |
goto err; |
| 1119 |
} |
| 1120 |
|
| 1121 |
if (!pkcs7_bio_copy_content(data, p7bio)) { |
| 1122 |
goto err; |
| 1123 |
} |
| 1124 |
|
| 1125 |
if (!PKCS7_dataFinal(p7, p7bio)) { |
| 1126 |
OPENSSL_PUT_ERROR(PKCS7, ERR_R_PKCS7_LIB); |
| 1127 |
goto err; |
| 1128 |
} |
| 1129 |
ret = 1; |
| 1130 |
err: |
| 1131 |
BIO_free_all(p7bio); |
| 1132 |
|
| 1133 |
return ret; |
| 1134 |
} |
| 1135 |
|
| 1136 |
PKCS7 *PKCS7_encrypt(STACK_OF(X509) *certs, BIO *in, const EVP_CIPHER *cipher, |
| 1137 |
int flags) { |
| 1138 |
GUARD_PTR(certs); |
| 1139 |
GUARD_PTR(in); |
| 1140 |
GUARD_PTR(cipher); |
| 1141 |
PKCS7 *p7; |
| 1142 |
X509 *x509; |
| 1143 |
|
| 1144 |
if ((p7 = PKCS7_new()) == NULL) { |
| 1145 |
OPENSSL_PUT_ERROR(PKCS7, ERR_R_PKCS7_LIB); |
| 1146 |
return NULL; |
| 1147 |
} |
| 1148 |
if (!PKCS7_set_type(p7, NID_pkcs7_enveloped)) { |
| 1149 |
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_WRONG_CONTENT_TYPE); |
| 1150 |
goto err; |
| 1151 |
} |
| 1152 |
if (!PKCS7_set_cipher(p7, cipher)) { |
| 1153 |
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_ERROR_SETTING_CIPHER); |
| 1154 |
goto err; |
| 1155 |
} |
| 1156 |
|
| 1157 |
for (size_t i = 0; i < sk_X509_num(certs); i++) { |
| 1158 |
x509 = sk_X509_value(certs, i); |
| 1159 |
if (!PKCS7_add_recipient(p7, x509)) { |
| 1160 |
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_ERROR_ADDING_RECIPIENT); |
| 1161 |
goto err; |
| 1162 |
} |
| 1163 |
} |
| 1164 |
|
| 1165 |
if (pkcs7_final(p7, in)) { |
| 1166 |
return p7; |
| 1167 |
} |
| 1168 |
|
| 1169 |
err: |
| 1170 |
PKCS7_free(p7); |
| 1171 |
return NULL; |
| 1172 |
} |
| 1173 |
|
| 1174 |
static int pkcs7_decrypt_rinfo(unsigned char **ek_out, PKCS7_RECIP_INFO *ri, |
| 1175 |
EVP_PKEY *pkey) { |
| 1176 |
GUARD_PTR(ri); |
| 1177 |
GUARD_PTR(ek_out); |
| 1178 |
unsigned char *ek = NULL; |
| 1179 |
int ret = 0; |
| 1180 |
|
| 1181 |
EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new(pkey, /*engine*/ NULL); |
| 1182 |
if (ctx == NULL || !EVP_PKEY_decrypt_init(ctx)) { |
| 1183 |
goto err; |
| 1184 |
} |
| 1185 |
size_t len; |
| 1186 |
if (!EVP_PKEY_decrypt(ctx, NULL, &len, ri->enc_key->data, |
| 1187 |
ri->enc_key->length) || |
| 1188 |
(ek = OPENSSL_malloc(len)) == NULL) { |
| 1189 |
OPENSSL_PUT_ERROR(EVP, ERR_R_EVP_LIB); |
| 1190 |
goto err; |
| 1191 |
} |
| 1192 |
|
| 1193 |
int ok = |
| 1194 |
EVP_PKEY_decrypt(ctx, ek, &len, ri->enc_key->data, ri->enc_key->length); |
| 1195 |
// We return 0 any failure except for decryption failure. On decrypt failure, |
| 1196 |
// we still need to set |ek| to NULL to signal decryption failure to callers |
| 1197 |
// so they can use random bytes as content encryption key for MMA defense. |
| 1198 |
if (!ok) { |
| 1199 |
OPENSSL_free(ek); |
| 1200 |
ek = NULL; |
| 1201 |
} |
| 1202 |
|
| 1203 |
ret = 1; |
| 1204 |
*ek_out = ek; |
| 1205 |
|
| 1206 |
err: |
| 1207 |
EVP_PKEY_CTX_free(ctx); |
| 1208 |
return ret; |
| 1209 |
} |
| 1210 |
|
| 1211 |
// pkcs7_cmp_ri is a comparison function, so it returns 0 if |ri| and |pcert| |
| 1212 |
// match and 1 if they do not. |
| 1213 |
static int pkcs7_cmp_ri(PKCS7_RECIP_INFO *ri, X509 *pcert) { |
| 1214 |
if (ri == NULL || ri->issuer_and_serial == NULL || pcert == NULL) { |
| 1215 |
return 1; |
| 1216 |
} |
| 1217 |
int ret = |
| 1218 |
X509_NAME_cmp(ri->issuer_and_serial->issuer, X509_get_issuer_name(pcert)); |
| 1219 |
if (ret) { |
| 1220 |
return ret; |
| 1221 |
} |
| 1222 |
return ASN1_INTEGER_cmp(X509_get0_serialNumber(pcert), |
| 1223 |
ri->issuer_and_serial->serial); |
| 1224 |
} |
| 1225 |
|
| 1226 |
static BIO *pkcs7_data_decode(PKCS7 *p7, EVP_PKEY *pkey, X509 *pcert) { |
| 1227 |
GUARD_PTR(p7); |
| 1228 |
GUARD_PTR(pkey); |
| 1229 |
BIO *out = NULL, *cipher_bio = NULL, *data_bio = NULL; |
| 1230 |
ASN1_OCTET_STRING *data_body = NULL; |
| 1231 |
const EVP_CIPHER *cipher = NULL; |
| 1232 |
X509_ALGOR *enc_alg = NULL; |
| 1233 |
STACK_OF(PKCS7_RECIP_INFO) *rsk = NULL; |
| 1234 |
PKCS7_RECIP_INFO *ri = NULL; |
| 1235 |
uint8_t *cek = NULL, *dummy_key = NULL; // cek means "content encryption key" |
| 1236 |
|
| 1237 |
if (p7->d.ptr == NULL) { |
| 1238 |
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_NO_CONTENT); |
| 1239 |
return NULL; |
| 1240 |
} |
| 1241 |
|
| 1242 |
switch (OBJ_obj2nid(p7->type)) { |
| 1243 |
case NID_pkcs7_enveloped: |
| 1244 |
rsk = p7->d.enveloped->recipientinfo; |
| 1245 |
enc_alg = p7->d.enveloped->enc_data->algorithm; |
| 1246 |
if (enc_alg == NULL || enc_alg->parameter == NULL || |
| 1247 |
enc_alg->parameter->type != V_ASN1_OCTET_STRING || |
| 1248 |
enc_alg->parameter->value.octet_string == NULL || |
| 1249 |
enc_alg->algorithm == NULL) { |
| 1250 |
OPENSSL_PUT_ERROR(PKCS7, ERR_R_PKCS7_LIB); |
| 1251 |
goto err; |
| 1252 |
} |
| 1253 |
// |data_body| is NULL if the optional EncryptedContent is missing. |
| 1254 |
data_body = p7->d.enveloped->enc_data->enc_data; |
| 1255 |
cipher = EVP_get_cipherbynid(OBJ_obj2nid(enc_alg->algorithm)); |
| 1256 |
if (cipher == NULL) { |
| 1257 |
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_UNSUPPORTED_CIPHER_TYPE); |
| 1258 |
goto err; |
| 1259 |
} |
| 1260 |
break; |
| 1261 |
default: |
| 1262 |
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_UNSUPPORTED_CONTENT_TYPE); |
| 1263 |
goto err; |
| 1264 |
} |
| 1265 |
|
| 1266 |
// envelopedData must have data content to decrypt |
| 1267 |
if (data_body == NULL) { |
| 1268 |
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_NO_CONTENT); |
| 1269 |
goto err; |
| 1270 |
} |
| 1271 |
|
| 1272 |
if ((cipher_bio = BIO_new(BIO_f_cipher())) == NULL) { |
| 1273 |
OPENSSL_PUT_ERROR(PKCS7, ERR_R_BIO_LIB); |
| 1274 |
goto err; |
| 1275 |
} |
| 1276 |
|
| 1277 |
// RFC 3218 provides an overview of, and mitigations for, the "Million Message |
| 1278 |
// Attack" (MMA) on RSA encryption with PKCS-1 padding. Section 2.3 describes |
| 1279 |
// implementor countermeasures. We implement the following countermeasures, as |
| 1280 |
// does OpenSSL. |
| 1281 |
// |
| 1282 |
// 1. Do not branch on |cek| decryption failure when checking recip infos |
| 1283 |
// 2. Clear error state after |cek| decrypt is attempted |
| 1284 |
// 3. If no cek was decrypted, use same-size random bytes |
| 1285 |
// to output gibberish "plaintext" |
| 1286 |
// 4. Always pay same allocation costs, regardless of |cek| decrypt result |
| 1287 |
|
| 1288 |
// If |pcert| was specified, find the matching recipient info |
| 1289 |
if (pcert) { |
| 1290 |
for (size_t ii = 0; ii < sk_PKCS7_RECIP_INFO_num(rsk); ii++) { |
| 1291 |
ri = sk_PKCS7_RECIP_INFO_value(rsk, ii); |
| 1292 |
// No decryption operation here, so we can return early without divulging |
| 1293 |
// information that could be used in MMA. |
| 1294 |
if (!pkcs7_cmp_ri(ri, pcert)) { |
| 1295 |
break; |
| 1296 |
} |
| 1297 |
#if !defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 1298 |
// For fuzz testing, we do not want to bail out early. |
| 1299 |
ri = NULL; |
| 1300 |
#endif |
| 1301 |
} |
| 1302 |
if (ri == NULL) { |
| 1303 |
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_NO_RECIPIENT_MATCHES_CERTIFICATE); |
| 1304 |
goto err; |
| 1305 |
} |
| 1306 |
// |pkcs7_decrypt_rinfo| will only return false on critical failure, not |
| 1307 |
// on decryption failure. Decryption check happens below, after we populate |
| 1308 |
// |dummy_key| with random bytes. |
| 1309 |
if (!pkcs7_decrypt_rinfo(&cek, ri, pkey)) { |
| 1310 |
goto err; |
| 1311 |
} |
| 1312 |
} else { |
| 1313 |
// Attempt to decrypt every recipient info. Don't exit early as |
| 1314 |
// countermeasure for MMA. |
| 1315 |
for (size_t ii = 0; ii < sk_PKCS7_RECIP_INFO_num(rsk); ii++) { |
| 1316 |
ri = sk_PKCS7_RECIP_INFO_value(rsk, ii); |
| 1317 |
uint8_t *tmp_cek; |
| 1318 |
// |pkcs7_decrypt_rinfo| will only return false on critical failure, not |
| 1319 |
// on decryption failure. Check whether |tmp_cek| is present after the |
| 1320 |
// call to determine if decryption succeeded. |
| 1321 |
if (!pkcs7_decrypt_rinfo(&tmp_cek, ri, pkey)) { |
| 1322 |
goto err; |
| 1323 |
} |
| 1324 |
// OpenSSL sets encryption key to last successfully decrypted key. Copy |
| 1325 |
// that behavior, but free previously allocated key memory. |
| 1326 |
if (tmp_cek) { |
| 1327 |
OPENSSL_free(cek); |
| 1328 |
cek = tmp_cek; |
| 1329 |
} |
| 1330 |
} |
| 1331 |
} |
| 1332 |
// Clear any decryption errors to minimize behavioral difference under MMA |
| 1333 |
ERR_clear_error(); |
| 1334 |
|
| 1335 |
EVP_CIPHER_CTX *evp_ctx = NULL; |
| 1336 |
if (!BIO_get_cipher_ctx(cipher_bio, &evp_ctx) || |
| 1337 |
!EVP_CipherInit_ex(evp_ctx, cipher, NULL, NULL, NULL, 0)) { |
| 1338 |
goto err; |
| 1339 |
} |
| 1340 |
const int expected_iv_len = EVP_CIPHER_CTX_iv_length(evp_ctx); |
| 1341 |
if (enc_alg->parameter->value.octet_string->length != expected_iv_len) { |
| 1342 |
OPENSSL_PUT_ERROR(PKCS7, ERR_R_PKCS7_LIB); |
| 1343 |
goto err; |
| 1344 |
} |
| 1345 |
uint8_t iv[EVP_MAX_IV_LENGTH]; |
| 1346 |
OPENSSL_memcpy(iv, enc_alg->parameter->value.octet_string->data, |
| 1347 |
expected_iv_len); |
| 1348 |
if (!EVP_CipherInit_ex(evp_ctx, NULL, NULL, NULL, iv, 0)) { |
| 1349 |
goto err; |
| 1350 |
} |
| 1351 |
// Get the key length from cipher context so we don't condition on |cek_len| |
| 1352 |
int len = EVP_CIPHER_CTX_key_length(evp_ctx); |
| 1353 |
if (!len) { |
| 1354 |
goto err; |
| 1355 |
} |
| 1356 |
// Always generate random bytes for the dummy key, regardless of |cek| decrypt |
| 1357 |
dummy_key = OPENSSL_malloc(len); |
| 1358 |
if (dummy_key == NULL) { |
| 1359 |
goto err; |
| 1360 |
} |
| 1361 |
AWSLC_ABORT_IF_NOT_ONE(RAND_bytes(dummy_key, len)); |
| 1362 |
// At this point, null |cek| indicates that no content encryption key was |
| 1363 |
// successfully decrypted. We don't want to return early due to MMA. So, swap |
| 1364 |
// in the dummy key and proceed. Content decryption result will be gibberish. |
| 1365 |
if (cek == NULL) { |
| 1366 |
cek = dummy_key; |
| 1367 |
dummy_key = NULL; |
| 1368 |
} |
| 1369 |
|
| 1370 |
if (!EVP_CipherInit_ex(evp_ctx, NULL, NULL, cek, NULL, 0)) { |
| 1371 |
goto err; |
| 1372 |
} |
| 1373 |
|
| 1374 |
OPENSSL_free(cek); |
| 1375 |
OPENSSL_free(dummy_key); |
| 1376 |
cek = NULL; |
| 1377 |
dummy_key = NULL; |
| 1378 |
out = cipher_bio; |
| 1379 |
cipher_bio = NULL; |
| 1380 |
|
| 1381 |
// We verify data_body != NULL above |
| 1382 |
if (data_body->length > 0) { |
| 1383 |
data_bio = BIO_new_mem_buf(data_body->data, data_body->length); |
| 1384 |
} else { |
| 1385 |
data_bio = BIO_new(BIO_s_mem()); |
| 1386 |
if (data_bio == NULL || !BIO_set_mem_eof_return(data_bio, 0)) { |
| 1387 |
goto err; |
| 1388 |
} |
| 1389 |
} |
| 1390 |
if (data_bio == NULL) { |
| 1391 |
goto err; |
| 1392 |
} |
| 1393 |
BIO_push(out, data_bio); |
| 1394 |
return out; |
| 1395 |
|
| 1396 |
err: |
| 1397 |
OPENSSL_free(cek); |
| 1398 |
OPENSSL_free(dummy_key); |
| 1399 |
BIO_free_all(out); |
| 1400 |
BIO_free_all(cipher_bio); |
| 1401 |
BIO_free_all(data_bio); |
| 1402 |
return NULL; |
| 1403 |
} |
| 1404 |
|
| 1405 |
PKCS7_RECIP_INFO *PKCS7_add_recipient(PKCS7 *p7, X509 *x509) { |
| 1406 |
GUARD_PTR(p7); |
| 1407 |
GUARD_PTR(x509); |
| 1408 |
PKCS7_RECIP_INFO *ri; |
| 1409 |
if ((ri = PKCS7_RECIP_INFO_new()) == NULL || |
| 1410 |
!PKCS7_RECIP_INFO_set(ri, x509) || !PKCS7_add_recipient_info(p7, ri)) { |
| 1411 |
PKCS7_RECIP_INFO_free(ri); |
| 1412 |
return NULL; |
| 1413 |
} |
| 1414 |
return ri; |
| 1415 |
} |
| 1416 |
|
| 1417 |
int PKCS7_decrypt(PKCS7 *p7, EVP_PKEY *pkey, X509 *cert, BIO *data, |
| 1418 |
int _flags) { |
| 1419 |
GUARD_PTR(p7); |
| 1420 |
GUARD_PTR(pkey); |
| 1421 |
GUARD_PTR(data); |
| 1422 |
BIO *bio = NULL; |
| 1423 |
int ret = 0; |
| 1424 |
|
| 1425 |
switch (OBJ_obj2nid(p7->type)) { |
| 1426 |
case NID_pkcs7_enveloped: |
| 1427 |
break; |
| 1428 |
default: |
| 1429 |
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_WRONG_CONTENT_TYPE); |
| 1430 |
goto err; |
| 1431 |
} |
| 1432 |
|
| 1433 |
if (cert && !X509_check_private_key(cert, pkey)) { |
| 1434 |
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE); |
| 1435 |
goto err; |
| 1436 |
} |
| 1437 |
|
| 1438 |
if ((bio = pkcs7_data_decode(p7, pkey, cert)) == NULL || |
| 1439 |
!pkcs7_bio_copy_content(bio, data)) { |
| 1440 |
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_DECRYPT_ERROR); |
| 1441 |
goto err; |
| 1442 |
} |
| 1443 |
|
| 1444 |
// Check whether content decryption was successful |
| 1445 |
if (1 != BIO_get_cipher_status(bio)) { |
| 1446 |
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_DECRYPT_ERROR); |
| 1447 |
goto err; |
| 1448 |
} |
| 1449 |
|
| 1450 |
ret = 1; |
| 1451 |
|
| 1452 |
err: |
| 1453 |
BIO_free_all(bio); |
| 1454 |
return ret; |
| 1455 |
} |
| 1456 |
|
| 1457 |
static STACK_OF(X509) *pkcs7_get0_certificates(const PKCS7 *p7) { |
| 1458 |
GUARD_PTR(p7); |
| 1459 |
GUARD_PTR(p7->d.ptr); |
| 1460 |
switch (OBJ_obj2nid(p7->type)) { |
| 1461 |
case NID_pkcs7_signed: |
| 1462 |
return p7->d.sign->cert; |
| 1463 |
default: |
| 1464 |
return NULL; |
| 1465 |
} |
| 1466 |
} |
| 1467 |
|
| 1468 |
STACK_OF(X509) *PKCS7_get0_signers(PKCS7 *p7, STACK_OF(X509) *certs, |
| 1469 |
int flags) { |
| 1470 |
GUARD_PTR(p7); |
| 1471 |
STACK_OF(X509) *signers = NULL; |
| 1472 |
X509 *signer = NULL; |
| 1473 |
|
| 1474 |
STACK_OF(X509) *included_certs = pkcs7_get0_certificates(p7); |
| 1475 |
STACK_OF(PKCS7_SIGNER_INFO) *sinfos = PKCS7_get_signer_info(p7); |
| 1476 |
|
| 1477 |
if (sk_PKCS7_SIGNER_INFO_num(sinfos) <= 0) { |
| 1478 |
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_NO_SIGNERS); |
| 1479 |
return NULL; |
| 1480 |
} |
| 1481 |
|
| 1482 |
if ((signers = sk_X509_new_null()) == NULL) { |
| 1483 |
OPENSSL_PUT_ERROR(PKCS7, ERR_R_CRYPTO_LIB); |
| 1484 |
return NULL; |
| 1485 |
} |
| 1486 |
|
| 1487 |
for (size_t i = 0; i < sk_PKCS7_SIGNER_INFO_num(sinfos); i++) { |
| 1488 |
PKCS7_SIGNER_INFO *si = sk_PKCS7_SIGNER_INFO_value(sinfos, i); |
| 1489 |
PKCS7_ISSUER_AND_SERIAL *ias = si->issuer_and_serial; |
| 1490 |
// Prioritize |certs| passed by caller |
| 1491 |
signer = X509_find_by_issuer_and_serial(certs, ias->issuer, ias->serial); |
| 1492 |
if (!(flags & PKCS7_NOINTERN) && !signer) { |
| 1493 |
signer = X509_find_by_issuer_and_serial(included_certs, ias->issuer, |
| 1494 |
ias->serial); |
| 1495 |
} |
| 1496 |
if (!signer) { // Signer cert not found in bundled/caller-specified certs |
| 1497 |
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_SIGNER_CERTIFICATE_NOT_FOUND); |
| 1498 |
sk_X509_free(signers); |
| 1499 |
return NULL; |
| 1500 |
} |
| 1501 |
if (!sk_X509_push(signers, signer)) { |
| 1502 |
OPENSSL_PUT_ERROR(PKCS7, ERR_R_PKCS7_LIB); |
| 1503 |
sk_X509_free(signers); |
| 1504 |
return NULL; |
| 1505 |
} |
| 1506 |
} |
| 1507 |
|
| 1508 |
return signers; |
| 1509 |
} |
| 1510 |
|
| 1511 |
static int pkcs7_x509_add_cert_new(STACK_OF(X509) **p_sk, X509 *cert) { |
| 1512 |
GUARD_PTR(p_sk); |
| 1513 |
if (*p_sk == NULL && (*p_sk = sk_X509_new_null()) == NULL) { |
| 1514 |
goto err; |
| 1515 |
} |
| 1516 |
if (!sk_X509_push(*p_sk, cert)) { |
| 1517 |
goto err; |
| 1518 |
} |
| 1519 |
return 1; |
| 1520 |
err: |
| 1521 |
OPENSSL_PUT_ERROR(X509, ERR_R_CRYPTO_LIB); |
| 1522 |
return 0; |
| 1523 |
} |
| 1524 |
|
| 1525 |
static int pkcs7_x509_add_certs_new(STACK_OF(X509) **p_sk, |
| 1526 |
STACK_OF(X509) *certs) { |
| 1527 |
GUARD_PTR(p_sk); |
| 1528 |
if (!certs) { // |certs| can be null in the caller |
| 1529 |
return 1; |
| 1530 |
} |
| 1531 |
for (size_t i = 0; i < sk_X509_num(certs); i++) { |
| 1532 |
if (!pkcs7_x509_add_cert_new(p_sk, sk_X509_value(certs, i))) |
| 1533 |
return 0; |
| 1534 |
} |
| 1535 |
return 1; |
| 1536 |
} |
| 1537 |
|
| 1538 |
static int pkcs7_signature_verify(BIO *in_bio, PKCS7 *p7, PKCS7_SIGNER_INFO *si, |
| 1539 |
X509 *signer) { |
| 1540 |
GUARD_PTR(in_bio); |
| 1541 |
GUARD_PTR(p7); |
| 1542 |
GUARD_PTR(si); |
| 1543 |
GUARD_PTR(si->digest_alg); |
| 1544 |
GUARD_PTR(signer); |
| 1545 |
int ret = 0; |
| 1546 |
|
| 1547 |
// Create a new |EVP_MD_CTX| to be consumed, so that the original |EVP_MD_CTX| |
| 1548 |
// is still in a usable state. |
| 1549 |
EVP_MD_CTX *mdc_tmp = EVP_MD_CTX_new(); |
| 1550 |
if (mdc_tmp == NULL) { |
| 1551 |
OPENSSL_PUT_ERROR(PKCS7, ERR_R_MALLOC_FAILURE); |
| 1552 |
goto out; |
| 1553 |
} |
| 1554 |
|
| 1555 |
const int md_type = OBJ_obj2nid(si->digest_alg->algorithm); |
| 1556 |
if (md_type == NID_undef) { |
| 1557 |
goto out; |
| 1558 |
} |
| 1559 |
EVP_MD_CTX *mdc = NULL; |
| 1560 |
BIO *bio = in_bio; |
| 1561 |
// There may be multiple MD-type BIOs in the chain, so iterate until we find |
| 1562 |
// the BIO with MD type we're looking for. |
| 1563 |
while (bio) { |
| 1564 |
if ((bio = BIO_find_type(bio, BIO_TYPE_MD)) == NULL) { |
| 1565 |
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST); |
| 1566 |
goto out; |
| 1567 |
} |
| 1568 |
if (!BIO_get_md_ctx(bio, &mdc) || !mdc) { |
| 1569 |
OPENSSL_PUT_ERROR(PKCS7, ERR_R_PKCS7_LIB); |
| 1570 |
goto out; |
| 1571 |
} |
| 1572 |
if (EVP_MD_CTX_type(mdc) == md_type) { // found it! |
| 1573 |
break; |
| 1574 |
} |
| 1575 |
bio = BIO_next(bio); |
| 1576 |
} |
| 1577 |
|
| 1578 |
// mdc is the digest ctx that we want, unless there are attributes, in |
| 1579 |
// which case the digest is the signed attributes. |
| 1580 |
if (!EVP_MD_CTX_copy_ex(mdc_tmp, mdc)) { |
| 1581 |
goto out; |
| 1582 |
} |
| 1583 |
|
| 1584 |
if (si->auth_attr != NULL && sk_X509_ATTRIBUTE_num(si->auth_attr) != 0) { |
| 1585 |
unsigned char md_data[EVP_MAX_MD_SIZE], *abuf = NULL; |
| 1586 |
unsigned int md_len; |
| 1587 |
|
| 1588 |
if (!EVP_DigestFinal_ex(mdc_tmp, md_data, &md_len)) { |
| 1589 |
goto out; |
| 1590 |
} |
| 1591 |
ASN1_OCTET_STRING *message_digest = |
| 1592 |
PKCS7_digest_from_attributes(si->auth_attr); |
| 1593 |
if (message_digest == NULL) { |
| 1594 |
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST); |
| 1595 |
goto out; |
| 1596 |
} |
| 1597 |
if (message_digest->length != (int)md_len || |
| 1598 |
OPENSSL_memcmp(message_digest->data, md_data, md_len) != 0) { |
| 1599 |
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_DIGEST_FAILURE); |
| 1600 |
goto out; |
| 1601 |
} |
| 1602 |
|
| 1603 |
const EVP_MD *md = EVP_get_digestbynid(md_type); |
| 1604 |
if (md == NULL || !EVP_VerifyInit_ex(mdc_tmp, md, NULL)) { |
| 1605 |
goto out; |
| 1606 |
} |
| 1607 |
|
| 1608 |
int alen = ASN1_item_i2d((ASN1_VALUE *)si->auth_attr, &abuf, |
| 1609 |
ASN1_ITEM_rptr(PKCS7_ATTR_VERIFY)); |
| 1610 |
if (alen <= 0 || abuf == NULL) { |
| 1611 |
OPENSSL_PUT_ERROR(PKCS7, ERR_R_ASN1_LIB); |
| 1612 |
goto out; |
| 1613 |
} |
| 1614 |
if (!EVP_VerifyUpdate(mdc_tmp, abuf, alen)) { |
| 1615 |
OPENSSL_free(abuf); |
| 1616 |
goto out; |
| 1617 |
} |
| 1618 |
OPENSSL_free(abuf); |
| 1619 |
} |
| 1620 |
|
| 1621 |
EVP_PKEY *pkey = X509_get0_pubkey(signer); |
| 1622 |
if (pkey == NULL) { |
| 1623 |
goto out; |
| 1624 |
} |
| 1625 |
|
| 1626 |
ASN1_OCTET_STRING *data_body = si->enc_digest; |
| 1627 |
if (!EVP_VerifyFinal(mdc_tmp, data_body->data, data_body->length, pkey)) { |
| 1628 |
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_SIGNATURE_FAILURE); |
| 1629 |
goto out; |
| 1630 |
} |
| 1631 |
|
| 1632 |
ret = 1; |
| 1633 |
|
| 1634 |
out: |
| 1635 |
EVP_MD_CTX_free(mdc_tmp); |
| 1636 |
return ret; |
| 1637 |
} |
| 1638 |
|
| 1639 |
int PKCS7_verify(PKCS7 *p7, STACK_OF(X509) *certs, X509_STORE *store, |
| 1640 |
BIO *indata, BIO *outdata, int flags) { |
| 1641 |
GUARD_PTR(p7); |
| 1642 |
GUARD_PTR(store); |
| 1643 |
STACK_OF(X509) *signers = NULL, *untrusted = NULL; |
| 1644 |
X509_STORE_CTX *cert_ctx = NULL; |
| 1645 |
BIO *p7bio = NULL; |
| 1646 |
int ret = 0; |
| 1647 |
|
| 1648 |
if (!PKCS7_type_is_signed(p7)) { |
| 1649 |
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_WRONG_CONTENT_TYPE); |
| 1650 |
goto out; |
| 1651 |
} |
| 1652 |
|
| 1653 |
// If |p7| is detached, caller must supply data via |indata| |
| 1654 |
if (PKCS7_is_detached(p7) && indata == NULL) { |
| 1655 |
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_NO_CONTENT); |
| 1656 |
goto out; |
| 1657 |
} |
| 1658 |
|
| 1659 |
// We enforce OpenSSL's PKCS7_NO_DUAL_CONTENT flag in all cases for signed |
| 1660 |
if (!PKCS7_is_detached(p7) && indata) { |
| 1661 |
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_CONTENT_AND_DATA_PRESENT); |
| 1662 |
goto out; |
| 1663 |
} |
| 1664 |
|
| 1665 |
STACK_OF(PKCS7_SIGNER_INFO) *sinfos = PKCS7_get_signer_info(p7); |
| 1666 |
if (sinfos == NULL || sk_PKCS7_SIGNER_INFO_num(sinfos) == 0UL) { |
| 1667 |
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_NO_SIGNATURES_ON_DATA); |
| 1668 |
goto out; |
| 1669 |
} |
| 1670 |
|
| 1671 |
if ((signers = PKCS7_get0_signers(p7, certs, flags)) == NULL) { |
| 1672 |
goto out; |
| 1673 |
} |
| 1674 |
|
| 1675 |
if (!(flags & PKCS7_NOVERIFY)) { |
| 1676 |
STACK_OF(X509) *included_certs = pkcs7_get0_certificates(p7); |
| 1677 |
if ((cert_ctx = X509_STORE_CTX_new()) == NULL || |
| 1678 |
!pkcs7_x509_add_certs_new(&untrusted, certs) || |
| 1679 |
!pkcs7_x509_add_certs_new(&untrusted, included_certs)) { |
| 1680 |
OPENSSL_PUT_ERROR(PKCS7, ERR_R_PKCS7_LIB); |
| 1681 |
goto out; |
| 1682 |
} |
| 1683 |
|
| 1684 |
for (size_t k = 0; k < sk_X509_num(signers); k++) { |
| 1685 |
X509 *signer = sk_X509_value(signers, k); |
| 1686 |
if (!X509_STORE_CTX_init(cert_ctx, store, signer, untrusted)) { |
| 1687 |
OPENSSL_PUT_ERROR(PKCS7, ERR_R_X509_LIB); |
| 1688 |
goto out; |
| 1689 |
} |
| 1690 |
if (!X509_STORE_CTX_set_default(cert_ctx, "smime_sign")) { |
| 1691 |
goto out; |
| 1692 |
} |
| 1693 |
X509_STORE_CTX_set0_crls(cert_ctx, p7->d.sign->crl); |
| 1694 |
// NOTE: unlike most of our functions, |X509_verify_cert| can return <= 0 |
| 1695 |
if (X509_verify_cert(cert_ctx) <= 0) { |
| 1696 |
#if !defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 1697 |
// For fuzz testing, we do not want to bail out early. |
| 1698 |
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_CERTIFICATE_VERIFY_ERROR); |
| 1699 |
goto out; |
| 1700 |
#endif |
| 1701 |
} |
| 1702 |
} |
| 1703 |
} |
| 1704 |
|
| 1705 |
// In copying data into out, we also read it through digest filters on |p7| to |
| 1706 |
// calculate digest for verification. |
| 1707 |
if ((p7bio = PKCS7_dataInit(p7, indata)) == NULL || |
| 1708 |
!pkcs7_bio_copy_content(p7bio, outdata)) { |
| 1709 |
goto out; |
| 1710 |
} |
| 1711 |
|
| 1712 |
// Verify signatures against signers |
| 1713 |
for (size_t ii = 0; ii < sk_PKCS7_SIGNER_INFO_num(sinfos); ii++) { |
| 1714 |
PKCS7_SIGNER_INFO *si = sk_PKCS7_SIGNER_INFO_value(sinfos, ii); |
| 1715 |
X509 *signer = sk_X509_value(signers, ii); |
| 1716 |
if (pkcs7_signature_verify(p7bio, p7, si, signer) != 1) { |
| 1717 |
#if !defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 1718 |
// For fuzz testing, we do not want to bail out early. |
| 1719 |
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_SIGNATURE_FAILURE); |
| 1720 |
goto out; |
| 1721 |
#endif |
| 1722 |
} |
| 1723 |
} |
| 1724 |
|
| 1725 |
ret = 1; |
| 1726 |
|
| 1727 |
out: |
| 1728 |
X509_STORE_CTX_free(cert_ctx); |
| 1729 |
// If |indata| was passed for detached signature, |PKCS7_dataInit| has pushed |
| 1730 |
// it onto the end of |p7bio|'s chain. Walk the chain freeing BIOs until we |
| 1731 |
// find |indata| so the caller retains ownership |
| 1732 |
while (p7bio != NULL && p7bio != indata) { |
| 1733 |
BIO *b = BIO_pop(p7bio); |
| 1734 |
BIO_free(p7bio); |
| 1735 |
p7bio = b; |
| 1736 |
} |
| 1737 |
sk_X509_free(signers); |
| 1738 |
sk_X509_free(untrusted); |
| 1739 |
return ret; |
| 1740 |
} |
| 1741 |
|
| 1742 |
PKCS7 *SMIME_read_PKCS7(BIO *in, BIO **bcont) { return NULL; } |
| 1743 |
|
| 1744 |
int SMIME_write_PKCS7(BIO *out, PKCS7 *p7, BIO *data, int flags) { return 0; } |
| 1745 |
|
| 1746 |
OPENSSL_END_ALLOW_DEPRECATED |