74 #include "../bytestring/internal.h"
75 #include "../internal.h"
79 #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
80 static const uint64_t kIterationsLimit = 2048;
87 static const uint64_t kIterationsLimit = 100 * 1000000;
90 return 0 < iterations && iterations <= kIterationsLimit;
116 int ptype,
void *pval,
uint8_t *penc,
int penclen) {
176 der_len > LONG_MAX) {
184 if (p8 == NULL ||
p != der + der_len) {
201 if (pass_len_in == -1 && pass != NULL) {
202 pass_len = strlen(pass);
204 pass_len = (size_t)pass_len_in;
233 int pass_len_in,
const uint8_t *salt,
size_t salt_len,
236 if (pass_len_in == -1 && pass != NULL) {
237 pass_len = strlen(pass);
239 pass_len = (size_t)pass_len_in;
254 pass_len, salt, salt_len, iterations,
264 if (
ret == NULL ||
ptr != der + der_len) {
276 struct pkcs12_context {
279 const char *password;
286 CBS *sequence,
struct pkcs12_context *
ctx,
287 int (*handle_element)(
CBS *
cbs,
struct pkcs12_context *
ctx)) {
329 0x01, 0x0c, 0x0a, 0x01, 0x01};
333 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x0c, 0x0a, 0x01, 0x02};
337 0x01, 0x0c, 0x0a, 0x01, 0x03};
341 0x0d, 0x01, 0x09, 0x14};
345 0x0d, 0x01, 0x09, 0x15};
349 0x0d, 0x01, 0x09, 0x16, 0x01};
356 size_t *out_friendly_name_len) {
357 *out_friendly_name = NULL;
358 *out_friendly_name_len = 0;
373 if (*out_friendly_name != NULL ||
395 if (!
CBB_finish(&cbb, out_friendly_name, out_friendly_name_len)) {
407 *out_friendly_name = NULL;
408 *out_friendly_name_len = 0;
415 CBS bag_id, wrapped_value, bag_attrs;
433 if (is_key_bag || is_shrouded_key_bag) {
443 &wrapped_value,
ctx->password,
ctx->password_len);
448 if (
CBS_len(&wrapped_value) != 0) {
454 *
ctx->out_key = pkey;
460 CBS cert_bag, cert_type, wrapped_cert, cert;
476 if (
CBS_len(&cert) > LONG_MAX) {
495 size_t friendly_name_len;
500 int ok = friendly_name_len == 0 ||
504 0 == sk_X509_push(
ctx->out_certs, x509)) {
518 0x0d, 0x01, 0x07, 0x01};
522 0x0d, 0x01, 0x07, 0x06};
527 struct pkcs12_context *
ctx) {
547 CBS version_bytes, eci, contents_type, ai, encrypted_contents;
561 &eci, &encrypted_contents, &
storage,
574 CBS_len(&encrypted_contents))) {
583 CBS octet_string_contents;
585 if (!
CBS_get_asn1(&wrapped_contents, &octet_string_contents,
604 size_t password_len,
const CBS *salt,
605 unsigned iterations,
const EVP_MD *
md,
606 const CBS *authsafes,
const CBS *expected_mac) {
618 CBS_len(authsafes), hmac, &hmac_len)) {
623 #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
635 CBS *ber_in,
const char *password) {
637 CBS in, pfx, mac_data, authsafe, content_type, wrapped_authsafes, authsafes;
640 struct pkcs12_context
ctx;
641 const size_t original_out_certs_len =
sk_X509_num(out_certs);
702 ctx.out_key = out_key;
703 ctx.out_certs = out_certs;
704 ctx.password = password;
705 ctx.password_len = password != NULL ? strlen(password) : 0;
709 CBS mac, salt, expected_mac;
738 iterations,
md, &authsafes, &expected_mac)) {
741 if (!mac_ok &&
ctx.password_len == 0) {
747 ctx.password =
ctx.password != NULL ? NULL :
"";
749 iterations,
md, &authsafes, &expected_mac)) {
771 while (
sk_X509_num(out_certs) > original_out_certs_len) {
772 X509 *x509 = sk_X509_pop(out_certs);
804 *ber_bytes += ber_len;
819 static const size_t kMaxSize = 256 * 1024;
850 if (
buf->length > kMaxSize ||
921 char ca_certs_alloced = 0;
923 if (out_ca_certs != NULL && *out_ca_certs != NULL) {
924 ca_certs = *out_ca_certs;
929 if (ca_certs == NULL) {
933 ca_certs_alloced = 1;
938 if (ca_certs_alloced) {
939 sk_X509_free(ca_certs);
948 if (*out_pkey != NULL && num_certs > 0) {
949 for (
size_t i = num_certs - 1;
i < num_certs;
i--) {
953 sk_X509_delete(ca_certs,
i);
961 *out_ca_certs = ca_certs;
971 if (password == NULL) {
972 if (password_len != 0) {
975 }
else if (password_len != -1 &&
976 (password[password_len] != 0 ||
998 if (
name == NULL && key_id_len == 0) {
1018 while (
CBS_len(&name_cbs) != 0) {
1027 if (key_id_len != 0) {
1044 CBB bag, bag_oid, bag_contents, cert_bag, cert_type, wrapped_cert, cert_value;
1093 size_t password_len,
unsigned iterations,
1103 CBB content_info,
type,
wrapper, encrypted_data, encrypted_content_info,
1104 inner_type, encrypted_content;
1115 !
CBB_add_asn1(&encrypted_data, &encrypted_content_info,
1121 iterations, password, password_len, salt,
1125 !
CBB_add_asn1(&encrypted_content_info, &encrypted_content,
1131 if (max_out < in_len) {
1155 const STACK_OF(
X509)* chain,
int key_nid,
int cert_nid,
1156 int iterations,
int mac_iterations,
int key_type) {
1160 if (cert_nid == 0) {
1163 if (iterations == 0) {
1166 if (mac_iterations == 0) {
1175 mac_iterations < 0 ||
1177 (pkey == NULL && cert == NULL &&
sk_X509_num(chain) == 0)) {
1215 size_t password_len = password != NULL ? strlen(password) : 0;
1218 unsigned key_id_len = 0;
1219 if (cert != NULL && pkey != NULL) {
1231 CBB cbb, pfx, auth_safe, auth_safe_oid, auth_safe_wrapper, auth_safe_data,
1278 &content_infos, cert_nid, password, password_len, iterations,
1320 &bag_contents, key_nid, NULL, password, password_len,
1322 0 , iterations, pkey)) {
1340 !
pkcs12_key_gen(password, password_len, mac_salt,
sizeof(mac_salt),
1344 CBB_len(&auth_safe_data), mac, &mac_len)) {
1348 CBB mac_data, digest_info, mac_cbb, mac_salt_cbb;
1355 !
CBB_add_bytes(&mac_salt_cbb, mac_salt,
sizeof(mac_salt)) ||