| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <openssl/pkcs8.h> |
| 16 |
| 17 | #include <limits.h> |
| 18 |
| 19 | #include <openssl/asn1.h> |
| 20 | #include <openssl/asn1t.h> |
| 21 | #include <openssl/bio.h> |
| 22 | #include <openssl/buf.h> |
| 23 | #include <openssl/bytestring.h> |
| 24 | #include <openssl/digest.h> |
| 25 | #include <openssl/err.h> |
| 26 | #include <openssl/evp.h> |
| 27 | #include <openssl/hmac.h> |
| 28 | #include <openssl/mem.h> |
| 29 | #include <openssl/rand.h> |
| 30 | #include <openssl/x509.h> |
| 31 |
| 32 | #include "../bytestring/internal.h" |
| 33 | #include "../internal.h" |
| 34 | #include "../x509/internal.h" |
| 35 | #include "internal.h" |
| 36 |
| 37 |
| 38 | int pkcs12_iterations_acceptable(uint64_t iterations) { |
| 39 | #if defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) |
| 40 | static const uint64_t kIterationsLimit = 2048; |
| 41 | #else |
| 42 | // Windows imposes a limit of 600K. Mozilla say: “so them increasing |
| 43 | // maximum to something like 100M or 1G (to have few decades of breathing |
| 44 | // room) would be very welcome”[1]. So here we set the limit to 100M. |
| 45 | // |
| 46 | // [1] https://bugzilla.mozilla.org/show_bug.cgi?id=1436873#c14 |
| 47 | static const uint64_t kIterationsLimit = 100 * 1000000; |
| 48 | #endif |
| 49 |
| 50 | assert(kIterationsLimit <= UINT32_MAX); |
| 51 | return 0 < iterations && iterations <= kIterationsLimit; |
| 52 | } |
| 53 |
| 54 | ASN1_SEQUENCE(PKCS8_PRIV_KEY_INFO) = { |
| 55 | ASN1_SIMPLE(PKCS8_PRIV_KEY_INFO, version, ASN1_INTEGER), |
| 56 | ASN1_SIMPLE(PKCS8_PRIV_KEY_INFO, pkeyalg, X509_ALGOR), |
| 57 | ASN1_SIMPLE(PKCS8_PRIV_KEY_INFO, pkey, ASN1_OCTET_STRING), |
| 58 | ASN1_IMP_SET_OF_OPT(PKCS8_PRIV_KEY_INFO, attributes, X509_ATTRIBUTE, 0), |
| 59 | } ASN1_SEQUENCE_END(PKCS8_PRIV_KEY_INFO) |
| 60 |
| 61 | IMPLEMENT_ASN1_FUNCTIONS_const(PKCS8_PRIV_KEY_INFO) |
| 62 |
| 63 | EVP_PKEY *EVP_PKCS82PKEY(const PKCS8_PRIV_KEY_INFO *p8) { |
| 64 | uint8_t *der = NULL; |
| 65 | int der_len = i2d_PKCS8_PRIV_KEY_INFO(p8, &der); |
| 66 | if (der_len < 0) { |
| 67 | return NULL; |
| 68 | } |
| 69 |
| 70 | CBS cbs; |
| 71 | CBS_init(&cbs, der, (size_t)der_len); |
| 72 | EVP_PKEY *ret = EVP_parse_private_key(&cbs); |
| 73 | if (ret == NULL || CBS_len(&cbs) != 0) { |
| 74 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_DECODE_ERROR); |
| 75 | EVP_PKEY_free(ret); |
| 76 | OPENSSL_free(der); |
| 77 | return NULL; |
| 78 | } |
| 79 |
| 80 | OPENSSL_free(der); |
| 81 | return ret; |
| 82 | } |
| 83 |
| 84 | PKCS8_PRIV_KEY_INFO *EVP_PKEY2PKCS8(const EVP_PKEY *pkey) { |
| 85 | CBB cbb; |
| 86 | uint8_t *der = NULL; |
| 87 | size_t der_len; |
| 88 | if (!CBB_init(&cbb, 0) || !EVP_marshal_private_key(&cbb, pkey) || |
| 89 | !CBB_finish(&cbb, &der, &der_len) || der_len > LONG_MAX) { |
| 90 | CBB_cleanup(&cbb); |
| 91 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_ENCODE_ERROR); |
| 92 | OPENSSL_free(der); |
| 93 | return NULL; |
| 94 | } |
| 95 |
| 96 | const uint8_t *p = der; |
| 97 | PKCS8_PRIV_KEY_INFO *p8 = d2i_PKCS8_PRIV_KEY_INFO(NULL, &p, (long)der_len); |
| 98 | if (p8 == NULL || p != der + der_len) { |
| 99 | PKCS8_PRIV_KEY_INFO_free(p8); |
| 100 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_DECODE_ERROR); |
| 101 | goto err; |
| 102 | } |
| 103 |
| 104 | OPENSSL_free(der); |
| 105 | return p8; |
| 106 |
| 107 | err: |
| 108 | OPENSSL_free(der); |
| 109 | return NULL; |
| 110 | } |
| 111 |
| 112 | PKCS8_PRIV_KEY_INFO *PKCS8_decrypt(X509_SIG *pkcs8, const char *pass, |
| 113 | int pass_len_in) { |
| 114 | size_t pass_len; |
| 115 | if (pass_len_in == -1 && pass != NULL) { |
| 116 | pass_len = strlen(pass); |
| 117 | } else { |
| 118 | pass_len = (size_t)pass_len_in; |
| 119 | } |
| 120 |
| 121 | PKCS8_PRIV_KEY_INFO *ret = NULL; |
| 122 | EVP_PKEY *pkey = NULL; |
| 123 | uint8_t *in = NULL; |
| 124 |
| 125 | // Convert the legacy ASN.1 object to a byte string. |
| 126 | int in_len = i2d_X509_SIG(pkcs8, &in); |
| 127 | if (in_len < 0) { |
| 128 | goto err; |
| 129 | } |
| 130 |
| 131 | CBS cbs; |
| 132 | CBS_init(&cbs, in, in_len); |
| 133 | pkey = PKCS8_parse_encrypted_private_key(&cbs, pass, pass_len); |
| 134 | if (pkey == NULL || CBS_len(&cbs) != 0) { |
| 135 | goto err; |
| 136 | } |
| 137 |
| 138 | ret = EVP_PKEY2PKCS8(pkey); |
| 139 |
| 140 | err: |
| 141 | OPENSSL_free(in); |
| 142 | EVP_PKEY_free(pkey); |
| 143 | return ret; |
| 144 | } |
| 145 |
| 146 | X509_SIG *PKCS8_encrypt(int pbe_nid, const EVP_CIPHER *cipher, const char *pass, |
| 147 | int pass_len_in, const uint8_t *salt, size_t salt_len, |
| 148 | int iterations, PKCS8_PRIV_KEY_INFO *p8inf) { |
| 149 | size_t pass_len; |
| 150 | if (pass_len_in == -1 && pass != NULL) { |
| 151 | pass_len = strlen(pass); |
| 152 | } else { |
| 153 | pass_len = (size_t)pass_len_in; |
| 154 | } |
| 155 |
| 156 | // Parse out the private key. |
| 157 | EVP_PKEY *pkey = EVP_PKCS82PKEY(p8inf); |
| 158 | if (pkey == NULL) { |
| 159 | return NULL; |
| 160 | } |
| 161 |
| 162 | X509_SIG *ret = NULL; |
| 163 | uint8_t *der = NULL; |
| 164 | const uint8_t *ptr; |
| 165 | size_t der_len; |
| 166 | CBB cbb; |
| 167 | if (!CBB_init(&cbb, 128) || |
| 168 | !PKCS8_marshal_encrypted_private_key(&cbb, pbe_nid, cipher, pass, |
| 169 | pass_len, salt, salt_len, iterations, |
| 170 | pkey) || |
| 171 | !CBB_finish(&cbb, &der, &der_len)) { |
| 172 | CBB_cleanup(&cbb); |
| 173 | goto err; |
| 174 | } |
| 175 |
| 176 | // Convert back to legacy ASN.1 objects. |
| 177 | ptr = der; |
| 178 | ret = d2i_X509_SIG(NULL, &ptr, der_len); |
| 179 | if (ret == NULL || ptr != der + der_len) { |
| 180 | OPENSSL_PUT_ERROR(PKCS8, ERR_R_INTERNAL_ERROR); |
| 181 | X509_SIG_free(ret); |
| 182 | ret = NULL; |
| 183 | } |
| 184 |
| 185 | err: |
| 186 | OPENSSL_free(der); |
| 187 | EVP_PKEY_free(pkey); |
| 188 | return ret; |
| 189 | } |
| 190 |
| 191 | struct pkcs12_context { |
| 192 | EVP_PKEY **out_key; |
| 193 | STACK_OF(X509) *out_certs; |
| 194 | const char *password; |
| 195 | size_t password_len; |
| 196 | }; |
| 197 |
| 198 | // PKCS12_handle_sequence parses a BER-encoded SEQUENCE of elements in a PKCS#12 |
| 199 | // structure. |
| 200 | static int PKCS12_handle_sequence( |
| 201 | CBS *sequence, struct pkcs12_context *ctx, |
| 202 | int (*handle_element)(CBS *cbs, struct pkcs12_context *ctx)) { |
| 203 | uint8_t *storage = NULL; |
| 204 | CBS in; |
| 205 | int ret = 0; |
| 206 |
| 207 | // Although a BER->DER conversion is done at the beginning of |PKCS12_parse|, |
| 208 | // the ASN.1 data gets wrapped in OCTETSTRINGs and/or encrypted and the |
| 209 | // conversion cannot see through those wrappings. So each time we step |
| 210 | // through one we need to convert to DER again. |
| 211 | if (!CBS_asn1_ber_to_der(sequence, &in, &storage)) { |
| 212 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA); |
| 213 | return 0; |
| 214 | } |
| 215 |
| 216 | CBS child; |
| 217 | if (!CBS_get_asn1(&in, &child, CBS_ASN1_SEQUENCE) || CBS_len(&in) != 0) { |
| 218 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA); |
| 219 | goto err; |
| 220 | } |
| 221 |
| 222 | while (CBS_len(&child) > 0) { |
| 223 | CBS element; |
| 224 | if (!CBS_get_asn1(&child, &element, CBS_ASN1_SEQUENCE)) { |
| 225 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA); |
| 226 | goto err; |
| 227 | } |
| 228 |
| 229 | if (!handle_element(&element, ctx)) { |
| 230 | goto err; |
| 231 | } |
| 232 | } |
| 233 |
| 234 | ret = 1; |
| 235 |
| 236 | err: |
| 237 | OPENSSL_free(storage); |
| 238 | return ret; |
| 239 | } |
| 240 |
| 241 | // 1.2.840.113549.1.12.10.1.1 |
| 242 | static const uint8_t kKeyBag[] = {0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, |
| 243 | 0x01, 0x0c, 0x0a, 0x01, 0x01}; |
| 244 |
| 245 | // 1.2.840.113549.1.12.10.1.2 |
| 246 | static const uint8_t kPKCS8ShroudedKeyBag[] = { |
| 247 | 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x0c, 0x0a, 0x01, 0x02}; |
| 248 |
| 249 | // 1.2.840.113549.1.12.10.1.3 |
| 250 | static const uint8_t kCertBag[] = {0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, |
| 251 | 0x01, 0x0c, 0x0a, 0x01, 0x03}; |
| 252 |
| 253 | // 1.2.840.113549.1.9.20 |
| 254 | static const uint8_t kFriendlyName[] = {0x2a, 0x86, 0x48, 0x86, 0xf7, |
| 255 | 0x0d, 0x01, 0x09, 0x14}; |
| 256 |
| 257 | // 1.2.840.113549.1.9.21 |
| 258 | static const uint8_t kLocalKeyID[] = {0x2a, 0x86, 0x48, 0x86, 0xf7, |
| 259 | 0x0d, 0x01, 0x09, 0x15}; |
| 260 |
| 261 | // 1.2.840.113549.1.9.22.1 |
| 262 | static const uint8_t kX509Certificate[] = {0x2a, 0x86, 0x48, 0x86, 0xf7, |
| 263 | 0x0d, 0x01, 0x09, 0x16, 0x01}; |
| 264 |
| 265 | // parse_bag_attributes parses the bagAttributes field of a SafeBag structure. |
| 266 | // It sets |*out_friendly_name| to a newly-allocated copy of the friendly name, |
| 267 | // encoded as a UTF-8 string, or NULL if there is none. It returns one on |
| 268 | // success and zero on error. |
| 269 | static int parse_bag_attributes(CBS *attrs, uint8_t **out_friendly_name, |
| 270 | size_t *out_friendly_name_len) { |
| 271 | *out_friendly_name = NULL; |
| 272 | *out_friendly_name_len = 0; |
| 273 |
| 274 | // See https://tools.ietf.org/html/rfc7292#section-4.2. |
| 275 | while (CBS_len(attrs) != 0) { |
| 276 | CBS attr, oid, values; |
| 277 | if (!CBS_get_asn1(attrs, &attr, CBS_ASN1_SEQUENCE) || |
| 278 | !CBS_get_asn1(&attr, &oid, CBS_ASN1_OBJECT) || |
| 279 | !CBS_get_asn1(&attr, &values, CBS_ASN1_SET) || CBS_len(&attr) != 0) { |
| 280 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA); |
| 281 | goto err; |
| 282 | } |
| 283 | if (CBS_mem_equal(&oid, kFriendlyName, sizeof(kFriendlyName))) { |
| 284 | // See https://tools.ietf.org/html/rfc2985, section 5.5.1. |
| 285 | CBS value; |
| 286 | if (*out_friendly_name != NULL || |
| 287 | !CBS_get_asn1(&values, &value, CBS_ASN1_BMPSTRING) || |
| 288 | CBS_len(&values) != 0 || CBS_len(&value) == 0) { |
| 289 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA); |
| 290 | goto err; |
| 291 | } |
| 292 | // Convert the friendly name to UTF-8. |
| 293 | CBB cbb; |
| 294 | if (!CBB_init(&cbb, CBS_len(&value))) { |
| 295 | goto err; |
| 296 | } |
| 297 | while (CBS_len(&value) != 0) { |
| 298 | uint32_t c; |
| 299 | if (!CBS_get_ucs2_be(&value, &c) || !CBB_add_utf8(&cbb, c)) { |
| 300 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_INVALID_CHARACTERS); |
| 301 | CBB_cleanup(&cbb); |
| 302 | goto err; |
| 303 | } |
| 304 | } |
| 305 | if (!CBB_finish(&cbb, out_friendly_name, out_friendly_name_len)) { |
| 306 | CBB_cleanup(&cbb); |
| 307 | goto err; |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 |
| 312 | return 1; |
| 313 |
| 314 | err: |
| 315 | OPENSSL_free(*out_friendly_name); |
| 316 | *out_friendly_name = NULL; |
| 317 | *out_friendly_name_len = 0; |
| 318 | return 0; |
| 319 | } |
| 320 |
| 321 | // PKCS12_handle_safe_bag parses a single SafeBag element in a PKCS#12 |
| 322 | // structure. |
| 323 | static int PKCS12_handle_safe_bag(CBS *safe_bag, struct pkcs12_context *ctx) { |
| 324 | CBS bag_id, wrapped_value, bag_attrs; |
| 325 | if (!CBS_get_asn1(safe_bag, &bag_id, CBS_ASN1_OBJECT) || |
| 326 | !CBS_get_asn1(safe_bag, &wrapped_value, |
| 327 | CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0)) { |
| 328 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA); |
| 329 | return 0; |
| 330 | } |
| 331 | if (CBS_len(safe_bag) == 0) { |
| 332 | CBS_init(&bag_attrs, NULL, 0); |
| 333 | } else if (!CBS_get_asn1(safe_bag, &bag_attrs, CBS_ASN1_SET) || |
| 334 | CBS_len(safe_bag) != 0) { |
| 335 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA); |
| 336 | return 0; |
| 337 | } |
| 338 |
| 339 | const int is_key_bag = CBS_mem_equal(&bag_id, kKeyBag, sizeof(kKeyBag)); |
| 340 | const int is_shrouded_key_bag = CBS_mem_equal(&bag_id, kPKCS8ShroudedKeyBag, |
| 341 | sizeof(kPKCS8ShroudedKeyBag)); |
| 342 | if (is_key_bag || is_shrouded_key_bag) { |
| 343 | // See RFC 7292, section 4.2.1 and 4.2.2. |
| 344 | if (*ctx->out_key) { |
| 345 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_MULTIPLE_PRIVATE_KEYS_IN_PKCS12); |
| 346 | return 0; |
| 347 | } |
| 348 |
| 349 | EVP_PKEY *pkey = |
| 350 | is_key_bag ? EVP_parse_private_key(&wrapped_value) |
| 351 | : PKCS8_parse_encrypted_private_key( |
| 352 | &wrapped_value, ctx->password, ctx->password_len); |
| 353 | if (pkey == NULL) { |
| 354 | return 0; |
| 355 | } |
| 356 |
| 357 | if (CBS_len(&wrapped_value) != 0) { |
| 358 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA); |
| 359 | EVP_PKEY_free(pkey); |
| 360 | return 0; |
| 361 | } |
| 362 |
| 363 | *ctx->out_key = pkey; |
| 364 | return 1; |
| 365 | } |
| 366 |
| 367 | if (CBS_mem_equal(&bag_id, kCertBag, sizeof(kCertBag))) { |
| 368 | // See RFC 7292, section 4.2.3. |
| 369 | CBS cert_bag, cert_type, wrapped_cert, cert; |
| 370 | if (!CBS_get_asn1(&wrapped_value, &cert_bag, CBS_ASN1_SEQUENCE) || |
| 371 | !CBS_get_asn1(&cert_bag, &cert_type, CBS_ASN1_OBJECT) || |
| 372 | !CBS_get_asn1(&cert_bag, &wrapped_cert, |
| 373 | CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0) || |
| 374 | !CBS_get_asn1(&wrapped_cert, &cert, CBS_ASN1_OCTETSTRING)) { |
| 375 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA); |
| 376 | return 0; |
| 377 | } |
| 378 |
| 379 | // Skip unknown certificate types. |
| 380 | if (!CBS_mem_equal(&cert_type, kX509Certificate, |
| 381 | sizeof(kX509Certificate))) { |
| 382 | return 1; |
| 383 | } |
| 384 |
| 385 | if (CBS_len(&cert) > LONG_MAX) { |
| 386 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA); |
| 387 | return 0; |
| 388 | } |
| 389 |
| 390 | const uint8_t *inp = CBS_data(&cert); |
| 391 | X509 *x509 = d2i_X509(NULL, &inp, (long)CBS_len(&cert)); |
| 392 | if (!x509) { |
| 393 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA); |
| 394 | return 0; |
| 395 | } |
| 396 |
| 397 | if (inp != CBS_data(&cert) + CBS_len(&cert)) { |
| 398 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA); |
| 399 | X509_free(x509); |
| 400 | return 0; |
| 401 | } |
| 402 |
| 403 | uint8_t *friendly_name; |
| 404 | size_t friendly_name_len; |
| 405 | if (!parse_bag_attributes(&bag_attrs, &friendly_name, &friendly_name_len)) { |
| 406 | X509_free(x509); |
| 407 | return 0; |
| 408 | } |
| 409 | int ok = friendly_name_len == 0 || |
| 410 | X509_alias_set1(x509, friendly_name, friendly_name_len); |
| 411 | OPENSSL_free(friendly_name); |
| 412 | if (!ok || 0 == sk_X509_push(ctx->out_certs, x509)) { |
| 413 | X509_free(x509); |
| 414 | return 0; |
| 415 | } |
| 416 |
| 417 | return 1; |
| 418 | } |
| 419 |
| 420 | // Unknown element type - ignore it. |
| 421 | return 1; |
| 422 | } |
| 423 |
| 424 | // 1.2.840.113549.1.7.1 |
| 425 | static const uint8_t kPKCS7Data[] = {0x2a, 0x86, 0x48, 0x86, 0xf7, |
| 426 | 0x0d, 0x01, 0x07, 0x01}; |
| 427 |
| 428 | // 1.2.840.113549.1.7.6 |
| 429 | static const uint8_t kPKCS7EncryptedData[] = {0x2a, 0x86, 0x48, 0x86, 0xf7, |
| 430 | 0x0d, 0x01, 0x07, 0x06}; |
| 431 |
| 432 | // PKCS12_handle_content_info parses a single PKCS#7 ContentInfo element in a |
| 433 | // PKCS#12 structure. |
| 434 | static int PKCS12_handle_content_info(CBS *content_info, |
| 435 | struct pkcs12_context *ctx) { |
| 436 | CBS content_type, wrapped_contents, contents; |
| 437 | int ret = 0; |
| 438 | uint8_t *storage = NULL; |
| 439 |
| 440 | if (!CBS_get_asn1(content_info, &content_type, CBS_ASN1_OBJECT) || |
| 441 | !CBS_get_asn1(content_info, &wrapped_contents, |
| 442 | CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0) || |
| 443 | CBS_len(content_info) != 0) { |
| 444 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA); |
| 445 | goto err; |
| 446 | } |
| 447 |
| 448 | if (CBS_mem_equal(&content_type, kPKCS7EncryptedData, |
| 449 | sizeof(kPKCS7EncryptedData))) { |
| 450 | // See https://tools.ietf.org/html/rfc2315#section-13. |
| 451 | // |
| 452 | // PKCS#7 encrypted data inside a PKCS#12 structure is generally an |
| 453 | // encrypted certificate bag and it's generally encrypted with 40-bit |
| 454 | // RC2-CBC. |
| 455 | CBS version_bytes, eci, contents_type, ai, encrypted_contents; |
| 456 | uint8_t *out; |
| 457 | size_t out_len; |
| 458 |
| 459 | if (!CBS_get_asn1(&wrapped_contents, &contents, CBS_ASN1_SEQUENCE) || |
| 460 | !CBS_get_asn1(&contents, &version_bytes, CBS_ASN1_INTEGER) || |
| 461 | // EncryptedContentInfo, see |
| 462 | // https://tools.ietf.org/html/rfc2315#section-10.1 |
| 463 | !CBS_get_asn1(&contents, &eci, CBS_ASN1_SEQUENCE) || |
| 464 | !CBS_get_asn1(&eci, &contents_type, CBS_ASN1_OBJECT) || |
| 465 | // AlgorithmIdentifier, see |
| 466 | // https://tools.ietf.org/html/rfc5280#section-4.1.1.2 |
| 467 | !CBS_get_asn1(&eci, &ai, CBS_ASN1_SEQUENCE) || |
| 468 | !CBS_get_asn1_implicit_string(&eci, &encrypted_contents, &storage, |
| 469 | CBS_ASN1_CONTEXT_SPECIFIC | 0, |
| 470 | CBS_ASN1_OCTETSTRING)) { |
| 471 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA); |
| 472 | goto err; |
| 473 | } |
| 474 |
| 475 | if (!CBS_mem_equal(&contents_type, kPKCS7Data, sizeof(kPKCS7Data))) { |
| 476 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA); |
| 477 | goto err; |
| 478 | } |
| 479 |
| 480 | if (!pkcs8_pbe_decrypt(&out, &out_len, &ai, ctx->password, |
| 481 | ctx->password_len, CBS_data(&encrypted_contents), |
| 482 | CBS_len(&encrypted_contents))) { |
| 483 | goto err; |
| 484 | } |
| 485 |
| 486 | CBS safe_contents; |
| 487 | CBS_init(&safe_contents, out, out_len); |
| 488 | ret = PKCS12_handle_sequence(&safe_contents, ctx, PKCS12_handle_safe_bag); |
| 489 | OPENSSL_free(out); |
| 490 | } else if (CBS_mem_equal(&content_type, kPKCS7Data, sizeof(kPKCS7Data))) { |
| 491 | CBS octet_string_contents; |
| 492 |
| 493 | if (!CBS_get_asn1(&wrapped_contents, &octet_string_contents, |
| 494 | CBS_ASN1_OCTETSTRING)) { |
| 495 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA); |
| 496 | goto err; |
| 497 | } |
| 498 |
| 499 | ret = PKCS12_handle_sequence(&octet_string_contents, ctx, |
| 500 | PKCS12_handle_safe_bag); |
| 501 | } else { |
| 502 | // Unknown element type - ignore it. |
| 503 | ret = 1; |
| 504 | } |
| 505 |
| 506 | err: |
| 507 | OPENSSL_free(storage); |
| 508 | return ret; |
| 509 | } |
| 510 |
| 511 | static int pkcs12_check_mac(int *out_mac_ok, const char *password, |
| 512 | size_t password_len, const CBS *salt, |
| 513 | uint32_t iterations, const EVP_MD *md, |
| 514 | const CBS *authsafes, const CBS *expected_mac) { |
| 515 | int ret = 0; |
| 516 | uint8_t hmac_key[EVP_MAX_MD_SIZE]; |
| 517 | if (!pkcs12_key_gen(password, password_len, CBS_data(salt), CBS_len(salt), |
| 518 | PKCS12_MAC_ID, iterations, EVP_MD_size(md), hmac_key, |
| 519 | md)) { |
| 520 | goto err; |
| 521 | } |
| 522 |
| 523 | uint8_t hmac[EVP_MAX_MD_SIZE]; |
| 524 | unsigned hmac_len; |
| 525 | if (NULL == HMAC(md, hmac_key, EVP_MD_size(md), CBS_data(authsafes), |
| 526 | CBS_len(authsafes), hmac, &hmac_len)) { |
| 527 | goto err; |
| 528 | } |
| 529 |
| 530 | *out_mac_ok = CBS_mem_equal(expected_mac, hmac, hmac_len); |
| 531 | if (CRYPTO_fuzzer_mode_enabled()) { |
| 532 | *out_mac_ok = 1; |
| 533 | } |
| 534 | ret = 1; |
| 535 |
| 536 | err: |
| 537 | OPENSSL_cleanse(hmac_key, sizeof(hmac_key)); |
| 538 | return ret; |
| 539 | } |
| 540 |
| 541 |
| 542 | int PKCS12_get_key_and_certs(EVP_PKEY **out_key, STACK_OF(X509) *out_certs, |
| 543 | CBS *ber_in, const char *password) { |
| 544 | uint8_t *storage = NULL; |
| 545 | CBS in, pfx, mac_data, authsafe, content_type, wrapped_authsafes, authsafes; |
| 546 | uint64_t version; |
| 547 | int ret = 0; |
| 548 | struct pkcs12_context ctx; |
| 549 | const size_t original_out_certs_len = sk_X509_num(out_certs); |
| 550 |
| 551 | // The input may be in BER format. |
| 552 | if (!CBS_asn1_ber_to_der(ber_in, &in, &storage)) { |
| 553 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA); |
| 554 | return 0; |
| 555 | } |
| 556 |
| 557 | *out_key = NULL; |
| 558 | OPENSSL_memset(&ctx, 0, sizeof(ctx)); |
| 559 |
| 560 | // See ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-12/pkcs-12v1.pdf, section |
| 561 | // four. |
| 562 | if (!CBS_get_asn1(&in, &pfx, CBS_ASN1_SEQUENCE) || CBS_len(&in) != 0 || |
| 563 | !CBS_get_asn1_uint64(&pfx, &version)) { |
| 564 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA); |
| 565 | goto err; |
| 566 | } |
| 567 |
| 568 | if (version < 3) { |
| 569 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_VERSION); |
| 570 | goto err; |
| 571 | } |
| 572 |
| 573 | if (!CBS_get_asn1(&pfx, &authsafe, CBS_ASN1_SEQUENCE)) { |
| 574 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA); |
| 575 | goto err; |
| 576 | } |
| 577 |
| 578 | if (CBS_len(&pfx) == 0) { |
| 579 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_MISSING_MAC); |
| 580 | goto err; |
| 581 | } |
| 582 |
| 583 | if (!CBS_get_asn1(&pfx, &mac_data, CBS_ASN1_SEQUENCE)) { |
| 584 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA); |
| 585 | goto err; |
| 586 | } |
| 587 |
| 588 | // authsafe is a PKCS#7 ContentInfo. See |
| 589 | // https://tools.ietf.org/html/rfc2315#section-7. |
| 590 | if (!CBS_get_asn1(&authsafe, &content_type, CBS_ASN1_OBJECT) || |
| 591 | !CBS_get_asn1(&authsafe, &wrapped_authsafes, |
| 592 | CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0)) { |
| 593 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA); |
| 594 | goto err; |
| 595 | } |
| 596 |
| 597 | // The content type can either be data or signedData. The latter indicates |
| 598 | // that it's signed by a public key, which isn't supported. |
| 599 | if (!CBS_mem_equal(&content_type, kPKCS7Data, sizeof(kPKCS7Data))) { |
| 600 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_PKCS12_PUBLIC_KEY_INTEGRITY_NOT_SUPPORTED); |
| 601 | goto err; |
| 602 | } |
| 603 |
| 604 | if (!CBS_get_asn1(&wrapped_authsafes, &authsafes, CBS_ASN1_OCTETSTRING)) { |
| 605 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA); |
| 606 | goto err; |
| 607 | } |
| 608 |
| 609 | ctx.out_key = out_key; |
| 610 | ctx.out_certs = out_certs; |
| 611 | ctx.password = password; |
| 612 | ctx.password_len = password != NULL ? strlen(password) : 0; |
| 613 |
| 614 | // Verify the MAC. |
| 615 | { |
| 616 | CBS mac, salt, expected_mac; |
| 617 | if (!CBS_get_asn1(&mac_data, &mac, CBS_ASN1_SEQUENCE)) { |
| 618 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA); |
| 619 | goto err; |
| 620 | } |
| 621 |
| 622 | const EVP_MD *md = EVP_parse_digest_algorithm(&mac); |
| 623 | if (md == NULL) { |
| 624 | goto err; |
| 625 | } |
| 626 |
| 627 | if (!CBS_get_asn1(&mac, &expected_mac, CBS_ASN1_OCTETSTRING) || |
| 628 | !CBS_get_asn1(&mac_data, &salt, CBS_ASN1_OCTETSTRING)) { |
| 629 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA); |
| 630 | goto err; |
| 631 | } |
| 632 |
| 633 | // The iteration count is optional and the default is one. |
| 634 | uint32_t iterations = 1; |
| 635 | if (CBS_len(&mac_data) > 0) { |
| 636 | uint64_t iterations_u64; |
| 637 | if (!CBS_get_asn1_uint64(&mac_data, &iterations_u64) || |
| 638 | !pkcs12_iterations_acceptable(iterations_u64)) { |
| 639 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA); |
| 640 | goto err; |
| 641 | } |
| 642 | iterations = (uint32_t)iterations_u64; |
| 643 | } |
| 644 |
| 645 | int mac_ok; |
| 646 | if (!pkcs12_check_mac(&mac_ok, ctx.password, ctx.password_len, &salt, |
| 647 | iterations, md, &authsafes, &expected_mac)) { |
| 648 | goto err; |
| 649 | } |
| 650 | if (!mac_ok && ctx.password_len == 0) { |
| 651 | // PKCS#12 encodes passwords as NUL-terminated UCS-2, so the empty |
| 652 | // password is encoded as {0, 0}. Some implementations use the empty byte |
| 653 | // array for "no password". OpenSSL considers a non-NULL password as {0, |
| 654 | // 0} and a NULL password as {}. It then, in high-level PKCS#12 parsing |
| 655 | // code, tries both options. We match this behavior. |
| 656 | ctx.password = ctx.password != NULL ? NULL : ""; |
| 657 | if (!pkcs12_check_mac(&mac_ok, ctx.password, ctx.password_len, &salt, |
| 658 | iterations, md, &authsafes, &expected_mac)) { |
| 659 | goto err; |
| 660 | } |
| 661 | } |
| 662 | if (!mac_ok) { |
| 663 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_INCORRECT_PASSWORD); |
| 664 | goto err; |
| 665 | } |
| 666 | } |
| 667 |
| 668 | // authsafes contains a series of PKCS#7 ContentInfos. |
| 669 | if (!PKCS12_handle_sequence(&authsafes, &ctx, PKCS12_handle_content_info)) { |
| 670 | goto err; |
| 671 | } |
| 672 |
| 673 | ret = 1; |
| 674 |
| 675 | err: |
| 676 | OPENSSL_free(storage); |
| 677 | if (!ret) { |
| 678 | EVP_PKEY_free(*out_key); |
| 679 | *out_key = NULL; |
| 680 | while (sk_X509_num(out_certs) > original_out_certs_len) { |
| 681 | X509 *x509 = sk_X509_pop(out_certs); |
| 682 | X509_free(x509); |
| 683 | } |
| 684 | } |
| 685 |
| 686 | return ret; |
| 687 | } |
| 688 |
| 689 | void PKCS12_PBE_add(void) {} |
| 690 |
| 691 | struct pkcs12_st { |
| 692 | uint8_t *ber_bytes; |
| 693 | size_t ber_len; |
| 694 | }; |
| 695 |
| 696 | PKCS12 *d2i_PKCS12(PKCS12 **out_p12, const uint8_t **ber_bytes, |
| 697 | size_t ber_len) { |
| 698 | PKCS12 *p12 = reinterpret_cast<PKCS12 *>(OPENSSL_malloc(sizeof(PKCS12))); |
| 699 | if (!p12) { |
| 700 | return NULL; |
| 701 | } |
| 702 |
| 703 | p12->ber_bytes = |
| 704 | reinterpret_cast<uint8_t *>(OPENSSL_memdup(*ber_bytes, ber_len)); |
| 705 | if (!p12->ber_bytes) { |
| 706 | OPENSSL_free(p12); |
| 707 | return NULL; |
| 708 | } |
| 709 |
| 710 | p12->ber_len = ber_len; |
| 711 | *ber_bytes += ber_len; |
| 712 |
| 713 | if (out_p12) { |
| 714 | PKCS12_free(*out_p12); |
| 715 | *out_p12 = p12; |
| 716 | } |
| 717 |
| 718 | return p12; |
| 719 | } |
| 720 |
| 721 | PKCS12 *d2i_PKCS12_bio(BIO *bio, PKCS12 **out_p12) { |
| 722 | size_t used = 0; |
| 723 | BUF_MEM *buf; |
| 724 | const uint8_t *dummy; |
| 725 | static const size_t kMaxSize = 256 * 1024; |
| 726 | PKCS12 *ret = NULL; |
| 727 |
| 728 | buf = BUF_MEM_new(); |
| 729 | if (buf == NULL) { |
| 730 | return NULL; |
| 731 | } |
| 732 | if (BUF_MEM_grow(buf, 8192) == 0) { |
| 733 | goto out; |
| 734 | } |
| 735 |
| 736 | for (;;) { |
| 737 | size_t max_read = buf->length - used; |
| 738 | int n = BIO_read(bio, &buf->data[used], |
| 739 | max_read > INT_MAX ? INT_MAX : (int)max_read); |
| 740 | if (n < 0) { |
| 741 | if (used == 0) { |
| 742 | goto out; |
| 743 | } |
| 744 | // Workaround a bug in node.js. It uses a memory BIO for this in the wrong |
| 745 | // mode. |
| 746 | n = 0; |
| 747 | } |
| 748 |
| 749 | if (n == 0) { |
| 750 | break; |
| 751 | } |
| 752 | used += n; |
| 753 |
| 754 | if (used < buf->length) { |
| 755 | continue; |
| 756 | } |
| 757 |
| 758 | if (buf->length > kMaxSize || BUF_MEM_grow(buf, buf->length * 2) == 0) { |
| 759 | goto out; |
| 760 | } |
| 761 | } |
| 762 |
| 763 | dummy = (uint8_t *)buf->data; |
| 764 | ret = d2i_PKCS12(out_p12, &dummy, used); |
| 765 |
| 766 | out: |
| 767 | BUF_MEM_free(buf); |
| 768 | return ret; |
| 769 | } |
| 770 |
| 771 | PKCS12 *d2i_PKCS12_fp(FILE *fp, PKCS12 **out_p12) { |
| 772 | BIO *bio; |
| 773 | PKCS12 *ret; |
| 774 |
| 775 | bio = BIO_new_fp(fp, 0 /* don't take ownership */); |
| 776 | if (!bio) { |
| 777 | return NULL; |
| 778 | } |
| 779 |
| 780 | ret = d2i_PKCS12_bio(bio, out_p12); |
| 781 | BIO_free(bio); |
| 782 | return ret; |
| 783 | } |
| 784 |
| 785 | int i2d_PKCS12(const PKCS12 *p12, uint8_t **out) { |
| 786 | if (p12->ber_len > INT_MAX) { |
| 787 | OPENSSL_PUT_ERROR(PKCS8, ERR_R_OVERFLOW); |
| 788 | return -1; |
| 789 | } |
| 790 |
| 791 | if (out == NULL) { |
| 792 | return (int)p12->ber_len; |
| 793 | } |
| 794 |
| 795 | if (*out == NULL) { |
| 796 | *out = reinterpret_cast<uint8_t *>( |
| 797 | OPENSSL_memdup(p12->ber_bytes, p12->ber_len)); |
| 798 | if (*out == NULL) { |
| 799 | return -1; |
| 800 | } |
| 801 | } else { |
| 802 | OPENSSL_memcpy(*out, p12->ber_bytes, p12->ber_len); |
| 803 | *out += p12->ber_len; |
| 804 | } |
| 805 | return (int)p12->ber_len; |
| 806 | } |
| 807 |
| 808 | int i2d_PKCS12_bio(BIO *bio, const PKCS12 *p12) { |
| 809 | return BIO_write_all(bio, p12->ber_bytes, p12->ber_len); |
| 810 | } |
| 811 |
| 812 | int i2d_PKCS12_fp(FILE *fp, const PKCS12 *p12) { |
| 813 | BIO *bio = BIO_new_fp(fp, 0 /* don't take ownership */); |
| 814 | if (bio == NULL) { |
| 815 | return 0; |
| 816 | } |
| 817 |
| 818 | int ret = i2d_PKCS12_bio(bio, p12); |
| 819 | BIO_free(bio); |
| 820 | return ret; |
| 821 | } |
| 822 |
| 823 | int PKCS12_parse(const PKCS12 *p12, const char *password, EVP_PKEY **out_pkey, |
| 824 | X509 **out_cert, STACK_OF(X509) **out_ca_certs) { |
| 825 | CBS ber_bytes; |
| 826 | STACK_OF(X509) *ca_certs = NULL; |
| 827 | char ca_certs_alloced = 0; |
| 828 |
| 829 | if (out_ca_certs != NULL && *out_ca_certs != NULL) { |
| 830 | ca_certs = *out_ca_certs; |
| 831 | } |
| 832 |
| 833 | if (!ca_certs) { |
| 834 | ca_certs = sk_X509_new_null(); |
| 835 | if (ca_certs == NULL) { |
| 836 | return 0; |
| 837 | } |
| 838 | ca_certs_alloced = 1; |
| 839 | } |
| 840 |
| 841 | CBS_init(&ber_bytes, p12->ber_bytes, p12->ber_len); |
| 842 | if (!PKCS12_get_key_and_certs(out_pkey, ca_certs, &ber_bytes, password)) { |
| 843 | if (ca_certs_alloced) { |
| 844 | sk_X509_free(ca_certs); |
| 845 | } |
| 846 | return 0; |
| 847 | } |
| 848 |
| 849 | // OpenSSL selects the last certificate which matches the private key as |
| 850 | // |out_cert|. |
| 851 | *out_cert = NULL; |
| 852 | size_t num_certs = sk_X509_num(ca_certs); |
| 853 | if (*out_pkey != NULL && num_certs > 0) { |
| 854 | for (size_t i = num_certs - 1; i < num_certs; i--) { |
| 855 | X509 *cert = sk_X509_value(ca_certs, i); |
| 856 | if (X509_check_private_key(cert, *out_pkey)) { |
| 857 | *out_cert = cert; |
| 858 | sk_X509_delete(ca_certs, i); |
| 859 | break; |
| 860 | } |
| 861 | ERR_clear_error(); |
| 862 | } |
| 863 | } |
| 864 |
| 865 | if (out_ca_certs) { |
| 866 | *out_ca_certs = ca_certs; |
| 867 | } else { |
| 868 | sk_X509_pop_free(ca_certs, X509_free); |
| 869 | } |
| 870 |
| 871 | return 1; |
| 872 | } |
| 873 |
| 874 | int PKCS12_verify_mac(const PKCS12 *p12, const char *password, |
| 875 | int password_len) { |
| 876 | if (password == NULL) { |
| 877 | if (password_len != 0) { |
| 878 | return 0; |
| 879 | } |
| 880 | } else if (password_len != -1 && |
| 881 | (password[password_len] != 0 || |
| 882 | OPENSSL_memchr(password, 0, password_len) != NULL)) { |
| 883 | return 0; |
| 884 | } |
| 885 |
| 886 | EVP_PKEY *pkey = NULL; |
| 887 | X509 *cert = NULL; |
| 888 | if (!PKCS12_parse(p12, password, &pkey, &cert, NULL)) { |
| 889 | ERR_clear_error(); |
| 890 | return 0; |
| 891 | } |
| 892 |
| 893 | EVP_PKEY_free(pkey); |
| 894 | X509_free(cert); |
| 895 |
| 896 | return 1; |
| 897 | } |
| 898 |
| 899 | // add_bag_attributes adds the bagAttributes field of a SafeBag structure, |
| 900 | // containing the specified friendlyName and localKeyId attributes. |
| 901 | static int add_bag_attributes(CBB *bag, const char *name, size_t name_len, |
| 902 | const uint8_t *key_id, size_t key_id_len) { |
| 903 | if (name == NULL && key_id_len == 0) { |
| 904 | return 1; // Omit the OPTIONAL SET. |
| 905 | } |
| 906 | // See https://tools.ietf.org/html/rfc7292#section-4.2. |
| 907 | CBB attrs, attr, oid, values, value; |
| 908 | if (!CBB_add_asn1(bag, &attrs, CBS_ASN1_SET)) { |
| 909 | return 0; |
| 910 | } |
| 911 | if (name_len != 0) { |
| 912 | // See https://tools.ietf.org/html/rfc2985, section 5.5.1. |
| 913 | if (!CBB_add_asn1(&attrs, &attr, CBS_ASN1_SEQUENCE) || |
| 914 | !CBB_add_asn1(&attr, &oid, CBS_ASN1_OBJECT) || |
| 915 | !CBB_add_bytes(&oid, kFriendlyName, sizeof(kFriendlyName)) || |
| 916 | !CBB_add_asn1(&attr, &values, CBS_ASN1_SET) || |
| 917 | !CBB_add_asn1(&values, &value, CBS_ASN1_BMPSTRING)) { |
| 918 | return 0; |
| 919 | } |
| 920 | // Convert the friendly name to a BMPString. |
| 921 | CBS name_cbs; |
| 922 | CBS_init(&name_cbs, (const uint8_t *)name, name_len); |
| 923 | while (CBS_len(&name_cbs) != 0) { |
| 924 | uint32_t c; |
| 925 | if (!CBS_get_utf8(&name_cbs, &c) || !CBB_add_ucs2_be(&value, c)) { |
| 926 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_INVALID_CHARACTERS); |
| 927 | return 0; |
| 928 | } |
| 929 | } |
| 930 | } |
| 931 | if (key_id_len != 0) { |
| 932 | // See https://tools.ietf.org/html/rfc2985, section 5.5.2. |
| 933 | if (!CBB_add_asn1(&attrs, &attr, CBS_ASN1_SEQUENCE) || |
| 934 | !CBB_add_asn1(&attr, &oid, CBS_ASN1_OBJECT) || |
| 935 | !CBB_add_bytes(&oid, kLocalKeyID, sizeof(kLocalKeyID)) || |
| 936 | !CBB_add_asn1(&attr, &values, CBS_ASN1_SET) || |
| 937 | !CBB_add_asn1(&values, &value, CBS_ASN1_OCTETSTRING) || |
| 938 | !CBB_add_bytes(&value, key_id, key_id_len)) { |
| 939 | return 0; |
| 940 | } |
| 941 | } |
| 942 | return CBB_flush_asn1_set_of(&attrs) && CBB_flush(bag); |
| 943 | } |
| 944 |
| 945 | static int add_cert_bag(CBB *cbb, X509 *cert, const char *name, |
| 946 | const uint8_t *key_id, size_t key_id_len) { |
| 947 | CBB bag, bag_oid, bag_contents, cert_bag, cert_type, wrapped_cert, cert_value; |
| 948 | if ( // See https://tools.ietf.org/html/rfc7292#section-4.2. |
| 949 | !CBB_add_asn1(cbb, &bag, CBS_ASN1_SEQUENCE) || |
| 950 | !CBB_add_asn1(&bag, &bag_oid, CBS_ASN1_OBJECT) || |
| 951 | !CBB_add_bytes(&bag_oid, kCertBag, sizeof(kCertBag)) || |
| 952 | !CBB_add_asn1(&bag, &bag_contents, |
| 953 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 0) || |
| 954 | // See https://tools.ietf.org/html/rfc7292#section-4.2.3. |
| 955 | !CBB_add_asn1(&bag_contents, &cert_bag, CBS_ASN1_SEQUENCE) || |
| 956 | !CBB_add_asn1(&cert_bag, &cert_type, CBS_ASN1_OBJECT) || |
| 957 | !CBB_add_bytes(&cert_type, kX509Certificate, sizeof(kX509Certificate)) || |
| 958 | !CBB_add_asn1(&cert_bag, &wrapped_cert, |
| 959 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 0) || |
| 960 | !CBB_add_asn1(&wrapped_cert, &cert_value, CBS_ASN1_OCTETSTRING)) { |
| 961 | return 0; |
| 962 | } |
| 963 | uint8_t *buf; |
| 964 | int len = i2d_X509(cert, NULL); |
| 965 |
| 966 | int int_name_len = 0; |
| 967 | const char *cert_name = (const char *)X509_alias_get0(cert, &int_name_len); |
| 968 | size_t name_len = int_name_len; |
| 969 | if (name) { |
| 970 | if (name_len != 0) { |
| 971 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_AMBIGUOUS_FRIENDLY_NAME); |
| 972 | return 0; |
| 973 | } |
| 974 | name_len = strlen(name); |
| 975 | } else { |
| 976 | name = cert_name; |
| 977 | } |
| 978 |
| 979 | if (len < 0 || !CBB_add_space(&cert_value, &buf, (size_t)len) || |
| 980 | i2d_X509(cert, &buf) < 0 || |
| 981 | !add_bag_attributes(&bag, name, name_len, key_id, key_id_len) || |
| 982 | !CBB_flush(cbb)) { |
| 983 | return 0; |
| 984 | } |
| 985 | return 1; |
| 986 | } |
| 987 |
| 988 | static int add_cert_safe_contents(CBB *cbb, X509 *cert, |
| 989 | const STACK_OF(X509) *chain, const char *name, |
| 990 | const uint8_t *key_id, size_t key_id_len) { |
| 991 | CBB safe_contents; |
| 992 | if (!CBB_add_asn1(cbb, &safe_contents, CBS_ASN1_SEQUENCE) || |
| 993 | (cert != NULL && |
| 994 | !add_cert_bag(&safe_contents, cert, name, key_id, key_id_len))) { |
| 995 | return 0; |
| 996 | } |
| 997 |
| 998 | for (size_t i = 0; i < sk_X509_num(chain); i++) { |
| 999 | // Only the leaf certificate gets attributes. |
| 1000 | if (!add_cert_bag(&safe_contents, sk_X509_value(chain, i), NULL, NULL, 0)) { |
| 1001 | return 0; |
| 1002 | } |
| 1003 | } |
| 1004 |
| 1005 | return CBB_flush(cbb); |
| 1006 | } |
| 1007 |
| 1008 | // add_encrypted_data encrypts |in| with |pbe_nid| and |pbe_cipher|, writing the |
| 1009 | // result to |out|. It returns one on success and zero on error. |pbe_nid| and |
| 1010 | // |pbe_cipher| are interpreted as in |PKCS8_encrypt|. |
| 1011 | static int add_encrypted_data(CBB *out, int pbe_nid, |
| 1012 | const EVP_CIPHER *pbe_cipher, |
| 1013 | const char *password, size_t password_len, |
| 1014 | uint32_t iterations, const uint8_t *in, |
| 1015 | size_t in_len) { |
| 1016 | uint8_t salt[PKCS5_SALT_LEN]; |
| 1017 | if (!RAND_bytes(salt, sizeof(salt))) { |
| 1018 | return 0; |
| 1019 | } |
| 1020 |
| 1021 | bssl::ScopedEVP_CIPHER_CTX ctx; |
| 1022 | CBB content_info, type, wrapper, encrypted_data, encrypted_content_info, |
| 1023 | inner_type, encrypted_content; |
| 1024 | if ( // Add the ContentInfo wrapping. |
| 1025 | !CBB_add_asn1(out, &content_info, CBS_ASN1_SEQUENCE) || |
| 1026 | !CBB_add_asn1(&content_info, &type, CBS_ASN1_OBJECT) || |
| 1027 | !CBB_add_bytes(&type, kPKCS7EncryptedData, sizeof(kPKCS7EncryptedData)) || |
| 1028 | !CBB_add_asn1(&content_info, &wrapper, |
| 1029 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 0) || |
| 1030 | // See https://tools.ietf.org/html/rfc2315#section-13. |
| 1031 | !CBB_add_asn1(&wrapper, &encrypted_data, CBS_ASN1_SEQUENCE) || |
| 1032 | !CBB_add_asn1_uint64(&encrypted_data, 0 /* version */) || |
| 1033 | // See https://tools.ietf.org/html/rfc2315#section-10.1. |
| 1034 | !CBB_add_asn1(&encrypted_data, &encrypted_content_info, |
| 1035 | CBS_ASN1_SEQUENCE) || |
| 1036 | !CBB_add_asn1(&encrypted_content_info, &inner_type, CBS_ASN1_OBJECT) || |
| 1037 | !CBB_add_bytes(&inner_type, kPKCS7Data, sizeof(kPKCS7Data)) || |
| 1038 | // Set up encryption and fill in contentEncryptionAlgorithm. |
| 1039 | !pkcs12_pbe_encrypt_init(&encrypted_content_info, ctx.get(), pbe_nid, |
| 1040 | pbe_cipher, iterations, password, password_len, |
| 1041 | salt, sizeof(salt)) || |
| 1042 | // Note this tag is primitive. It is an implicitly-tagged OCTET_STRING, so |
| 1043 | // it inherits the inner tag's constructed bit. |
| 1044 | !CBB_add_asn1(&encrypted_content_info, &encrypted_content, |
| 1045 | CBS_ASN1_CONTEXT_SPECIFIC | 0)) { |
| 1046 | return 0; |
| 1047 | } |
| 1048 |
| 1049 | size_t max_out = in_len + EVP_CIPHER_CTX_block_size(ctx.get()); |
| 1050 | if (max_out < in_len) { |
| 1051 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_TOO_LONG); |
| 1052 | return 0; |
| 1053 | } |
| 1054 |
| 1055 | uint8_t *ptr; |
| 1056 | int n1, n2; |
| 1057 | if (!CBB_reserve(&encrypted_content, &ptr, max_out) || |
| 1058 | !EVP_CipherUpdate(ctx.get(), ptr, &n1, in, in_len) || |
| 1059 | !EVP_CipherFinal_ex(ctx.get(), ptr + n1, &n2) || |
| 1060 | !CBB_did_write(&encrypted_content, n1 + n2) || !CBB_flush(out)) { |
| 1061 | return 0; |
| 1062 | } |
| 1063 |
| 1064 | return 1; |
| 1065 | } |
| 1066 |
| 1067 | PKCS12 *PKCS12_create(const char *password, const char *name, |
| 1068 | const EVP_PKEY *pkey, X509 *cert, |
| 1069 | const STACK_OF(X509) *chain, int key_nid, int cert_nid, |
| 1070 | int iterations, int mac_iterations, int key_type) { |
| 1071 | // TODO(crbug.com/396434682): Improve these defaults. |
| 1072 | if (key_nid == 0) { |
| 1073 | key_nid = NID_pbe_WithSHA1And3_Key_TripleDES_CBC; |
| 1074 | } |
| 1075 | if (cert_nid == 0) { |
| 1076 | cert_nid = NID_pbe_WithSHA1And40BitRC2_CBC; |
| 1077 | } |
| 1078 | if (iterations == 0) { |
| 1079 | iterations = PKCS12_DEFAULT_ITER; |
| 1080 | } |
| 1081 | if (mac_iterations == 0) { |
| 1082 | mac_iterations = 1; |
| 1083 | } |
| 1084 | if ( // In OpenSSL, this specifies a non-standard Microsoft key usage |
| 1085 | // extension which we do not currently support. |
| 1086 | key_type != 0 || |
| 1087 | // In OpenSSL, -1 here means to omit the MAC, which we do not |
| 1088 | // currently support. Omitting it is also invalid for a password-based |
| 1089 | // PKCS#12 file. |
| 1090 | mac_iterations < 0 || |
| 1091 | // Don't encode empty objects. |
| 1092 | (pkey == NULL && cert == NULL && sk_X509_num(chain) == 0)) { |
| 1093 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_UNSUPPORTED_OPTIONS); |
| 1094 | return 0; |
| 1095 | } |
| 1096 |
| 1097 | // PKCS#12 is a very confusing recursive data format, built out of another |
| 1098 | // recursive data format. Section 5.1 of RFC 7292 describes the encoding |
| 1099 | // algorithm, but there is no clear overview. A quick summary: |
| 1100 | // |
| 1101 | // PKCS#7 defines a ContentInfo structure, which is a overgeneralized typed |
| 1102 | // combinator structure for applying cryptography. We care about two types. A |
| 1103 | // data ContentInfo contains an OCTET STRING and is a leaf node of the |
| 1104 | // combinator tree. An encrypted-data ContentInfo contains encryption |
| 1105 | // parameters (key derivation and encryption) and wraps another ContentInfo, |
| 1106 | // usually data. |
| 1107 | // |
| 1108 | // A PKCS#12 file is a PFX structure (section 4), which contains a single data |
| 1109 | // ContentInfo and a MAC over it. This root ContentInfo is the |
| 1110 | // AuthenticatedSafe and its payload is a SEQUENCE of other ContentInfos, so |
| 1111 | // that different parts of the PKCS#12 file can by differently protected. |
| 1112 | // |
| 1113 | // Each ContentInfo in the AuthenticatedSafe, after undoing all the PKCS#7 |
| 1114 | // combinators, has SafeContents payload. A SafeContents is a SEQUENCE of |
| 1115 | // SafeBag. SafeBag is PKCS#12's typed structure, with subtypes such as KeyBag |
| 1116 | // and CertBag. Confusingly, there is a SafeContents bag type which itself |
| 1117 | // recursively contains more SafeBags, but we do not implement this. Bags also |
| 1118 | // can have attributes. |
| 1119 | // |
| 1120 | // The grouping of SafeBags into intermediate ContentInfos does not appear to |
| 1121 | // be significant, except that all SafeBags sharing a ContentInfo have the |
| 1122 | // same level of protection. Additionally, while keys may be encrypted by |
| 1123 | // placing a KeyBag in an encrypted-data ContentInfo, PKCS#12 also defines a |
| 1124 | // key-specific encryption container, PKCS8ShroudedKeyBag, which is used |
| 1125 | // instead. |
| 1126 |
| 1127 | // Note that |password| may be NULL to specify no password, rather than the |
| 1128 | // empty string. They are encoded differently in PKCS#12. (One is the empty |
| 1129 | // byte array and the other is NUL-terminated UCS-2.) |
| 1130 | size_t password_len = password != NULL ? strlen(password) : 0; |
| 1131 |
| 1132 | uint8_t key_id[EVP_MAX_MD_SIZE]; |
| 1133 | unsigned key_id_len = 0; |
| 1134 | if (cert != NULL && pkey != NULL) { |
| 1135 | if (!X509_check_private_key(cert, pkey) || |
| 1136 | // Matching OpenSSL, use the SHA-1 hash of the certificate as the local |
| 1137 | // key ID. Some PKCS#12 consumers require one to connect the private key |
| 1138 | // and certificate. |
| 1139 | !X509_digest(cert, EVP_sha1(), key_id, &key_id_len)) { |
| 1140 | return 0; |
| 1141 | } |
| 1142 | } |
| 1143 |
| 1144 | // See https://tools.ietf.org/html/rfc7292#section-4. |
| 1145 | PKCS12 *ret = NULL; |
| 1146 | CBB cbb, pfx, auth_safe, auth_safe_oid, auth_safe_wrapper, auth_safe_data, |
| 1147 | content_infos; |
| 1148 | uint8_t mac_key[EVP_MAX_MD_SIZE]; |
| 1149 | if (!CBB_init(&cbb, 0) || !CBB_add_asn1(&cbb, &pfx, CBS_ASN1_SEQUENCE) || |
| 1150 | !CBB_add_asn1_uint64(&pfx, 3) || |
| 1151 | // auth_safe is a data ContentInfo. |
| 1152 | !CBB_add_asn1(&pfx, &auth_safe, CBS_ASN1_SEQUENCE) || |
| 1153 | !CBB_add_asn1(&auth_safe, &auth_safe_oid, CBS_ASN1_OBJECT) || |
| 1154 | !CBB_add_bytes(&auth_safe_oid, kPKCS7Data, sizeof(kPKCS7Data)) || |
| 1155 | !CBB_add_asn1(&auth_safe, &auth_safe_wrapper, |
| 1156 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 0) || |
| 1157 | !CBB_add_asn1(&auth_safe_wrapper, &auth_safe_data, |
| 1158 | CBS_ASN1_OCTETSTRING) || |
| 1159 | // See https://tools.ietf.org/html/rfc7292#section-4.1. |auth_safe|'s |
| 1160 | // contains a SEQUENCE of ContentInfos. |
| 1161 | !CBB_add_asn1(&auth_safe_data, &content_infos, CBS_ASN1_SEQUENCE)) { |
| 1162 | goto err; |
| 1163 | } |
| 1164 |
| 1165 | // If there are any certificates, place them in CertBags wrapped in a single |
| 1166 | // encrypted ContentInfo. |
| 1167 | if (cert != NULL || sk_X509_num(chain) > 0) { |
| 1168 | if (cert_nid < 0) { |
| 1169 | // Place the certificates in an unencrypted ContentInfo. This could be |
| 1170 | // more compactly-encoded by reusing the same ContentInfo as the key, but |
| 1171 | // OpenSSL does not do this. We keep them separate for consistency. (Keys, |
| 1172 | // even when encrypted, are always placed in unencrypted ContentInfos. |
| 1173 | // PKCS#12 defines bag-level encryption for keys.) |
| 1174 | CBB content_info, oid, wrapper, data; |
| 1175 | if (!CBB_add_asn1(&content_infos, &content_info, CBS_ASN1_SEQUENCE) || |
| 1176 | !CBB_add_asn1(&content_info, &oid, CBS_ASN1_OBJECT) || |
| 1177 | !CBB_add_bytes(&oid, kPKCS7Data, sizeof(kPKCS7Data)) || |
| 1178 | !CBB_add_asn1(&content_info, &wrapper, |
| 1179 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 0) || |
| 1180 | !CBB_add_asn1(&wrapper, &data, CBS_ASN1_OCTETSTRING) || |
| 1181 | !add_cert_safe_contents(&data, cert, chain, name, key_id, |
| 1182 | key_id_len) || |
| 1183 | !CBB_flush(&content_infos)) { |
| 1184 | goto err; |
| 1185 | } |
| 1186 | } else { |
| 1187 | // This function differs from other OpenSSL functions in how PBES1 and |
| 1188 | // PBES2 schemes are selected. If the NID matches a cipher, treat this as |
| 1189 | // PBES2 instead. Convert to the other convention. |
| 1190 | const EVP_CIPHER *cipher = pkcs5_pbe2_nid_to_cipher(cert_nid); |
| 1191 | if (cipher != nullptr) { |
| 1192 | cert_nid = -1; |
| 1193 | } |
| 1194 | CBB plaintext_cbb; |
| 1195 | int ok = |
| 1196 | CBB_init(&plaintext_cbb, 0) && |
| 1197 | add_cert_safe_contents(&plaintext_cbb, cert, chain, name, key_id, |
| 1198 | key_id_len) && |
| 1199 | add_encrypted_data(&content_infos, cert_nid, cipher, password, |
| 1200 | password_len, iterations, CBB_data(&plaintext_cbb), |
| 1201 | CBB_len(&plaintext_cbb)); |
| 1202 | CBB_cleanup(&plaintext_cbb); |
| 1203 | if (!ok) { |
| 1204 | goto err; |
| 1205 | } |
| 1206 | } |
| 1207 | } |
| 1208 |
| 1209 | // If there is a key, place it in a single KeyBag or PKCS8ShroudedKeyBag |
| 1210 | // wrapped in an unencrypted ContentInfo. (One could also place it in a KeyBag |
| 1211 | // inside an encrypted ContentInfo, but OpenSSL does not do this and some |
| 1212 | // PKCS#12 consumers do not support KeyBags.) |
| 1213 | if (pkey != NULL) { |
| 1214 | CBB content_info, oid, wrapper, data, safe_contents, bag, bag_oid, |
| 1215 | bag_contents; |
| 1216 | if ( // Add another data ContentInfo. |
| 1217 | !CBB_add_asn1(&content_infos, &content_info, CBS_ASN1_SEQUENCE) || |
| 1218 | !CBB_add_asn1(&content_info, &oid, CBS_ASN1_OBJECT) || |
| 1219 | !CBB_add_bytes(&oid, kPKCS7Data, sizeof(kPKCS7Data)) || |
| 1220 | !CBB_add_asn1(&content_info, &wrapper, |
| 1221 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 0) || |
| 1222 | !CBB_add_asn1(&wrapper, &data, CBS_ASN1_OCTETSTRING) || |
| 1223 | !CBB_add_asn1(&data, &safe_contents, CBS_ASN1_SEQUENCE) || |
| 1224 | // Add a SafeBag containing a PKCS8ShroudedKeyBag. |
| 1225 | !CBB_add_asn1(&safe_contents, &bag, CBS_ASN1_SEQUENCE) || |
| 1226 | !CBB_add_asn1(&bag, &bag_oid, CBS_ASN1_OBJECT)) { |
| 1227 | goto err; |
| 1228 | } |
| 1229 | if (key_nid < 0) { |
| 1230 | if (!CBB_add_bytes(&bag_oid, kKeyBag, sizeof(kKeyBag)) || |
| 1231 | !CBB_add_asn1(&bag, &bag_contents, |
| 1232 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 0) || |
| 1233 | !EVP_marshal_private_key(&bag_contents, pkey)) { |
| 1234 | goto err; |
| 1235 | } |
| 1236 | } else { |
| 1237 | // This function differs from other OpenSSL functions in how PBES1 and |
| 1238 | // PBES2 schemes are selected. If the NID matches a cipher, treat this as |
| 1239 | // PBES2 instead. Convert to the other convention. |
| 1240 | const EVP_CIPHER *cipher = pkcs5_pbe2_nid_to_cipher(key_nid); |
| 1241 | if (cipher != nullptr) { |
| 1242 | key_nid = -1; |
| 1243 | } |
| 1244 | if (!CBB_add_bytes(&bag_oid, kPKCS8ShroudedKeyBag, |
| 1245 | sizeof(kPKCS8ShroudedKeyBag)) || |
| 1246 | !CBB_add_asn1(&bag, &bag_contents, |
| 1247 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 0) || |
| 1248 | !PKCS8_marshal_encrypted_private_key( |
| 1249 | &bag_contents, key_nid, cipher, password, password_len, |
| 1250 | NULL /* generate a random salt */, |
| 1251 | 0 /* use default salt length */, iterations, pkey)) { |
| 1252 | goto err; |
| 1253 | } |
| 1254 | } |
| 1255 | size_t name_len = 0; |
| 1256 | if (name) { |
| 1257 | name_len = strlen(name); |
| 1258 | } |
| 1259 | if (!add_bag_attributes(&bag, name, name_len, key_id, key_id_len) || |
| 1260 | !CBB_flush(&content_infos)) { |
| 1261 | goto err; |
| 1262 | } |
| 1263 | } |
| 1264 |
| 1265 | { |
| 1266 | // Compute the MAC. Match OpenSSL in using SHA-1 as the hash function. The |
| 1267 | // MAC covers |auth_safe_data|. |
| 1268 | const EVP_MD *mac_md = EVP_sha1(); |
| 1269 | uint8_t mac_salt[PKCS5_SALT_LEN]; |
| 1270 | uint8_t mac[EVP_MAX_MD_SIZE]; |
| 1271 | unsigned mac_len; |
| 1272 | if (!CBB_flush(&auth_safe_data) || |
| 1273 | !RAND_bytes(mac_salt, sizeof(mac_salt)) || |
| 1274 | !pkcs12_key_gen(password, password_len, mac_salt, sizeof(mac_salt), |
| 1275 | PKCS12_MAC_ID, mac_iterations, EVP_MD_size(mac_md), |
| 1276 | mac_key, mac_md) || |
| 1277 | !HMAC(mac_md, mac_key, EVP_MD_size(mac_md), CBB_data(&auth_safe_data), |
| 1278 | CBB_len(&auth_safe_data), mac, &mac_len)) { |
| 1279 | goto err; |
| 1280 | } |
| 1281 |
| 1282 | CBB mac_data, digest_info, mac_cbb, mac_salt_cbb; |
| 1283 | if (!CBB_add_asn1(&pfx, &mac_data, CBS_ASN1_SEQUENCE) || |
| 1284 | !CBB_add_asn1(&mac_data, &digest_info, CBS_ASN1_SEQUENCE) || |
| 1285 | !EVP_marshal_digest_algorithm(&digest_info, mac_md) || |
| 1286 | !CBB_add_asn1(&digest_info, &mac_cbb, CBS_ASN1_OCTETSTRING) || |
| 1287 | !CBB_add_bytes(&mac_cbb, mac, mac_len) || |
| 1288 | !CBB_add_asn1(&mac_data, &mac_salt_cbb, CBS_ASN1_OCTETSTRING) || |
| 1289 | !CBB_add_bytes(&mac_salt_cbb, mac_salt, sizeof(mac_salt)) || |
| 1290 | // The iteration count has a DEFAULT of 1, but RFC 7292 says "The |
| 1291 | // default is for historical reasons and its use is deprecated." Thus we |
| 1292 | // explicitly encode the iteration count, though it is not valid DER. |
| 1293 | !CBB_add_asn1_uint64(&mac_data, mac_iterations)) { |
| 1294 | goto err; |
| 1295 | } |
| 1296 |
| 1297 | ret = reinterpret_cast<PKCS12 *>(OPENSSL_malloc(sizeof(PKCS12))); |
| 1298 | if (ret == NULL || !CBB_finish(&cbb, &ret->ber_bytes, &ret->ber_len)) { |
| 1299 | OPENSSL_free(ret); |
| 1300 | ret = NULL; |
| 1301 | goto err; |
| 1302 | } |
| 1303 | } |
| 1304 |
| 1305 | err: |
| 1306 | OPENSSL_cleanse(mac_key, sizeof(mac_key)); |
| 1307 | CBB_cleanup(&cbb); |
| 1308 | return ret; |
| 1309 | } |
| 1310 |
| 1311 | void PKCS12_free(PKCS12 *p12) { |
| 1312 | if (p12 == NULL) { |
| 1313 | return; |
| 1314 | } |
| 1315 | OPENSSL_free(p12->ber_bytes); |
| 1316 | OPENSSL_free(p12); |
| 1317 | } |
| 1318 |