| 1 |
/* Written by Dr Stephen N Henson ([email protected]) for the OpenSSL |
| 2 |
* project 1999. |
| 3 |
*/ |
| 4 |
/* ==================================================================== |
| 5 |
* Copyright (c) 1999 The OpenSSL Project. All rights reserved. |
| 6 |
* |
| 7 |
* Redistribution and use in source and binary forms, with or without |
| 8 |
* modification, are permitted provided that the following conditions |
| 9 |
* are met: |
| 10 |
* |
| 11 |
* 1. Redistributions of source code must retain the above copyright |
| 12 |
* notice, this list of conditions and the following disclaimer. |
| 13 |
* |
| 14 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 15 |
* notice, this list of conditions and the following disclaimer in |
| 16 |
* the documentation and/or other materials provided with the |
| 17 |
* distribution. |
| 18 |
* |
| 19 |
* 3. All advertising materials mentioning features or use of this |
| 20 |
* software must display the following acknowledgment: |
| 21 |
* "This product includes software developed by the OpenSSL Project |
| 22 |
* for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" |
| 23 |
* |
| 24 |
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 25 |
* endorse or promote products derived from this software without |
| 26 |
* prior written permission. For written permission, please contact |
| 27 |
* [email protected]. |
| 28 |
* |
| 29 |
* 5. Products derived from this software may not be called "OpenSSL" |
| 30 |
* nor may "OpenSSL" appear in their names without prior written |
| 31 |
* permission of the OpenSSL Project. |
| 32 |
* |
| 33 |
* 6. Redistributions of any form whatsoever must retain the following |
| 34 |
* acknowledgment: |
| 35 |
* "This product includes software developed by the OpenSSL Project |
| 36 |
* for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" |
| 37 |
* |
| 38 |
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 39 |
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 40 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 41 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 42 |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 43 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 44 |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 45 |
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 46 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 47 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 48 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 49 |
* OF THE POSSIBILITY OF SUCH DAMAGE. |
| 50 |
* ==================================================================== |
| 51 |
* |
| 52 |
* This product includes cryptographic software written by Eric Young |
| 53 |
* ([email protected]). This product includes software written by Tim |
| 54 |
* Hudson ([email protected]). */ |
| 55 |
|
| 56 |
#include <openssl/pkcs8.h> |
| 57 |
|
| 58 |
#include <assert.h> |
| 59 |
#include <limits.h> |
| 60 |
#include <string.h> |
| 61 |
|
| 62 |
#include <openssl/asn1.h> |
| 63 |
#include <openssl/buf.h> |
| 64 |
#include <openssl/bytestring.h> |
| 65 |
#include <openssl/cipher.h> |
| 66 |
#include <openssl/digest.h> |
| 67 |
#include <openssl/err.h> |
| 68 |
#include <openssl/hmac.h> |
| 69 |
#include <openssl/mem.h> |
| 70 |
#include <openssl/obj.h> |
| 71 |
#include <openssl/x509.h> |
| 72 |
|
| 73 |
#include "internal.h" |
| 74 |
#include "../bytestring/internal.h" |
| 75 |
|
| 76 |
|
| 77 |
#define PKCS12_KEY_ID 1 |
| 78 |
#define PKCS12_IV_ID 2 |
| 79 |
#define PKCS12_MAC_ID 3 |
| 80 |
|
| 81 |
static int ascii_to_ucs2(const char *ascii, size_t ascii_len, |
| 82 |
uint8_t **out, size_t *out_len) { |
| 83 |
uint8_t *unitmp; |
| 84 |
size_t ulen, i; |
| 85 |
|
| 86 |
if (!ascii) { |
| 87 |
*out = NULL; |
| 88 |
*out_len = 0; |
| 89 |
return 1; |
| 90 |
} |
| 91 |
|
| 92 |
ulen = ascii_len * 2 + 2; |
| 93 |
if (ulen < ascii_len) { |
| 94 |
return 0; |
| 95 |
} |
| 96 |
unitmp = OPENSSL_malloc(ulen); |
| 97 |
if (unitmp == NULL) { |
| 98 |
return 0; |
| 99 |
} |
| 100 |
for (i = 0; i < ulen - 2; i += 2) { |
| 101 |
unitmp[i] = 0; |
| 102 |
unitmp[i + 1] = ascii[i >> 1]; |
| 103 |
} |
| 104 |
|
| 105 |
/* Make result double null terminated */ |
| 106 |
unitmp[ulen - 2] = 0; |
| 107 |
unitmp[ulen - 1] = 0; |
| 108 |
*out_len = ulen; |
| 109 |
*out = unitmp; |
| 110 |
return 1; |
| 111 |
} |
| 112 |
|
| 113 |
static int pkcs12_key_gen_raw(const uint8_t *pass_raw, size_t pass_raw_len, |
| 114 |
const uint8_t *salt, size_t salt_len, |
| 115 |
uint8_t id, int iterations, |
| 116 |
size_t out_len, uint8_t *out, |
| 117 |
const EVP_MD *md) { |
| 118 |
/* See https://tools.ietf.org/html/rfc7292#appendix-B. Quoted parts of the |
| 119 |
* specification have errata applied and other typos fixed. */ |
| 120 |
|
| 121 |
if (iterations < 1) { |
| 122 |
OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_ITERATION_COUNT); |
| 123 |
return 0; |
| 124 |
} |
| 125 |
|
| 126 |
/* In the spec, |block_size| is called "v", but measured in bits. */ |
| 127 |
size_t block_size = EVP_MD_block_size(md); |
| 128 |
|
| 129 |
/* 1. Construct a string, D (the "diversifier"), by concatenating v/8 copies |
| 130 |
* of ID. */ |
| 131 |
uint8_t D[EVP_MAX_MD_BLOCK_SIZE]; |
| 132 |
memset(D, id, block_size); |
| 133 |
|
| 134 |
/* 2. Concatenate copies of the salt together to create a string S of length |
| 135 |
* v(ceiling(s/v)) bits (the final copy of the salt may be truncated to |
| 136 |
* create S). Note that if the salt is the empty string, then so is S. |
| 137 |
* |
| 138 |
* 3. Concatenate copies of the password together to create a string P of |
| 139 |
* length v(ceiling(p/v)) bits (the final copy of the password may be |
| 140 |
* truncated to create P). Note that if the password is the empty string, |
| 141 |
* then so is P. |
| 142 |
* |
| 143 |
* 4. Set I=S||P to be the concatenation of S and P. */ |
| 144 |
if (salt_len + block_size - 1 < salt_len || |
| 145 |
pass_raw_len + block_size - 1 < pass_raw_len) { |
| 146 |
OPENSSL_PUT_ERROR(PKCS8, ERR_R_OVERFLOW); |
| 147 |
return 0; |
| 148 |
} |
| 149 |
size_t S_len = block_size * ((salt_len + block_size - 1) / block_size); |
| 150 |
size_t P_len = block_size * ((pass_raw_len + block_size - 1) / block_size); |
| 151 |
size_t I_len = S_len + P_len; |
| 152 |
if (I_len < S_len) { |
| 153 |
OPENSSL_PUT_ERROR(PKCS8, ERR_R_OVERFLOW); |
| 154 |
return 0; |
| 155 |
} |
| 156 |
|
| 157 |
uint8_t *I = OPENSSL_malloc(I_len); |
| 158 |
if (I_len != 0 && I == NULL) { |
| 159 |
OPENSSL_PUT_ERROR(PKCS8, ERR_R_MALLOC_FAILURE); |
| 160 |
return 0; |
| 161 |
} |
| 162 |
|
| 163 |
size_t i; |
| 164 |
for (i = 0; i < S_len; i++) { |
| 165 |
I[i] = salt[i % salt_len]; |
| 166 |
} |
| 167 |
for (i = 0; i < P_len; i++) { |
| 168 |
I[i + S_len] = pass_raw[i % pass_raw_len]; |
| 169 |
} |
| 170 |
|
| 171 |
int ret = 0; |
| 172 |
EVP_MD_CTX ctx; |
| 173 |
EVP_MD_CTX_init(&ctx); |
| 174 |
|
| 175 |
while (out_len != 0) { |
| 176 |
/* A. Set A_i=H^r(D||I). (i.e., the r-th hash of D||I, |
| 177 |
* H(H(H(... H(D||I)))) */ |
| 178 |
uint8_t A[EVP_MAX_MD_SIZE]; |
| 179 |
unsigned A_len; |
| 180 |
if (!EVP_DigestInit_ex(&ctx, md, NULL) || |
| 181 |
!EVP_DigestUpdate(&ctx, D, block_size) || |
| 182 |
!EVP_DigestUpdate(&ctx, I, I_len) || |
| 183 |
!EVP_DigestFinal_ex(&ctx, A, &A_len)) { |
| 184 |
goto err; |
| 185 |
} |
| 186 |
int iter; |
| 187 |
for (iter = 1; iter < iterations; iter++) { |
| 188 |
if (!EVP_DigestInit_ex(&ctx, md, NULL) || |
| 189 |
!EVP_DigestUpdate(&ctx, A, A_len) || |
| 190 |
!EVP_DigestFinal_ex(&ctx, A, &A_len)) { |
| 191 |
goto err; |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
size_t todo = out_len < A_len ? out_len : A_len; |
| 196 |
memcpy(out, A, todo); |
| 197 |
out += todo; |
| 198 |
out_len -= todo; |
| 199 |
if (out_len == 0) { |
| 200 |
break; |
| 201 |
} |
| 202 |
|
| 203 |
/* B. Concatenate copies of A_i to create a string B of length v bits (the |
| 204 |
* final copy of A_i may be truncated to create B). */ |
| 205 |
uint8_t B[EVP_MAX_MD_BLOCK_SIZE]; |
| 206 |
for (i = 0; i < block_size; i++) { |
| 207 |
B[i] = A[i % A_len]; |
| 208 |
} |
| 209 |
|
| 210 |
/* C. Treating I as a concatenation I_0, I_1, ..., I_(k-1) of v-bit blocks, |
| 211 |
* where k=ceiling(s/v)+ceiling(p/v), modify I by setting I_j=(I_j+B+1) mod |
| 212 |
* 2^v for each j. */ |
| 213 |
assert(I_len % block_size == 0); |
| 214 |
for (i = 0; i < I_len; i += block_size) { |
| 215 |
unsigned carry = 1; |
| 216 |
size_t j; |
| 217 |
for (j = block_size - 1; j < block_size; j--) { |
| 218 |
carry += I[i + j] + B[j]; |
| 219 |
I[i + j] = (uint8_t)carry; |
| 220 |
carry >>= 8; |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
ret = 1; |
| 226 |
|
| 227 |
err: |
| 228 |
OPENSSL_cleanse(I, I_len); |
| 229 |
OPENSSL_free(I); |
| 230 |
EVP_MD_CTX_cleanup(&ctx); |
| 231 |
return ret; |
| 232 |
} |
| 233 |
|
| 234 |
static int pkcs12_pbe_keyivgen(EVP_CIPHER_CTX *ctx, const uint8_t *pass_raw, |
| 235 |
size_t pass_raw_len, ASN1_TYPE *param, |
| 236 |
const EVP_CIPHER *cipher, const EVP_MD *md, |
| 237 |
int is_encrypt) { |
| 238 |
PBEPARAM *pbe; |
| 239 |
int salt_len, iterations, ret; |
| 240 |
uint8_t *salt; |
| 241 |
const uint8_t *pbuf; |
| 242 |
uint8_t key[EVP_MAX_KEY_LENGTH], iv[EVP_MAX_IV_LENGTH]; |
| 243 |
|
| 244 |
/* Extract useful info from parameter */ |
| 245 |
if (param == NULL || param->type != V_ASN1_SEQUENCE || |
| 246 |
param->value.sequence == NULL) { |
| 247 |
OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_DECODE_ERROR); |
| 248 |
return 0; |
| 249 |
} |
| 250 |
|
| 251 |
pbuf = param->value.sequence->data; |
| 252 |
pbe = d2i_PBEPARAM(NULL, &pbuf, param->value.sequence->length); |
| 253 |
if (pbe == NULL) { |
| 254 |
OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_DECODE_ERROR); |
| 255 |
return 0; |
| 256 |
} |
| 257 |
|
| 258 |
if (!pbe->iter) { |
| 259 |
iterations = 1; |
| 260 |
} else { |
| 261 |
iterations = ASN1_INTEGER_get(pbe->iter); |
| 262 |
} |
| 263 |
salt = pbe->salt->data; |
| 264 |
salt_len = pbe->salt->length; |
| 265 |
if (!pkcs12_key_gen_raw(pass_raw, pass_raw_len, salt, salt_len, PKCS12_KEY_ID, |
| 266 |
iterations, EVP_CIPHER_key_length(cipher), key, md)) { |
| 267 |
OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_KEY_GEN_ERROR); |
| 268 |
PBEPARAM_free(pbe); |
| 269 |
return 0; |
| 270 |
} |
| 271 |
if (!pkcs12_key_gen_raw(pass_raw, pass_raw_len, salt, salt_len, PKCS12_IV_ID, |
| 272 |
iterations, EVP_CIPHER_iv_length(cipher), iv, md)) { |
| 273 |
OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_KEY_GEN_ERROR); |
| 274 |
PBEPARAM_free(pbe); |
| 275 |
return 0; |
| 276 |
} |
| 277 |
PBEPARAM_free(pbe); |
| 278 |
ret = EVP_CipherInit_ex(ctx, cipher, NULL, key, iv, is_encrypt); |
| 279 |
OPENSSL_cleanse(key, EVP_MAX_KEY_LENGTH); |
| 280 |
OPENSSL_cleanse(iv, EVP_MAX_IV_LENGTH); |
| 281 |
return ret; |
| 282 |
} |
| 283 |
|
| 284 |
typedef int (*keygen_func)(EVP_CIPHER_CTX *ctx, const uint8_t *pass_raw, |
| 285 |
size_t pass_raw_len, ASN1_TYPE *param, |
| 286 |
const EVP_CIPHER *cipher, const EVP_MD *md, |
| 287 |
int is_encrypt); |
| 288 |
|
| 289 |
struct pbe_suite { |
| 290 |
int pbe_nid; |
| 291 |
const EVP_CIPHER* (*cipher_func)(void); |
| 292 |
const EVP_MD* (*md_func)(void); |
| 293 |
keygen_func keygen; |
| 294 |
int flags; |
| 295 |
}; |
| 296 |
|
| 297 |
#define PBE_UCS2_CONVERT_PASSWORD 0x1 |
| 298 |
|
| 299 |
static const struct pbe_suite kBuiltinPBE[] = { |
| 300 |
{ |
| 301 |
NID_pbe_WithSHA1And40BitRC2_CBC, EVP_rc2_40_cbc, EVP_sha1, |
| 302 |
pkcs12_pbe_keyivgen, PBE_UCS2_CONVERT_PASSWORD |
| 303 |
}, |
| 304 |
{ |
| 305 |
NID_pbe_WithSHA1And128BitRC4, EVP_rc4, EVP_sha1, pkcs12_pbe_keyivgen, |
| 306 |
PBE_UCS2_CONVERT_PASSWORD |
| 307 |
}, |
| 308 |
{ |
| 309 |
NID_pbe_WithSHA1And3_Key_TripleDES_CBC, EVP_des_ede3_cbc, EVP_sha1, |
| 310 |
pkcs12_pbe_keyivgen, PBE_UCS2_CONVERT_PASSWORD |
| 311 |
}, |
| 312 |
{ |
| 313 |
NID_pbes2, NULL, NULL, PKCS5_v2_PBE_keyivgen, 0 |
| 314 |
}, |
| 315 |
}; |
| 316 |
|
| 317 |
static const struct pbe_suite *get_pbe_suite(int pbe_nid) { |
| 318 |
unsigned i; |
| 319 |
for (i = 0; i < sizeof(kBuiltinPBE) / sizeof(kBuiltinPBE[0]); i++) { |
| 320 |
if (kBuiltinPBE[i].pbe_nid == pbe_nid) { |
| 321 |
return &kBuiltinPBE[i]; |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
return NULL; |
| 326 |
} |
| 327 |
|
| 328 |
/* pass_to_pass_raw performs a password conversion (possibly a no-op) |
| 329 |
* appropriate to the supplied |pbe_nid|. The input |pass| is treated as a |
| 330 |
* NUL-terminated string if |pass_len| is -1, otherwise it is treated as a |
| 331 |
* buffer of the specified length. If the supplied PBE NID sets the |
| 332 |
* |PBE_UCS2_CONVERT_PASSWORD| flag, the supplied |pass| will be converted to |
| 333 |
* UCS-2. |
| 334 |
* |
| 335 |
* It sets |*out_pass_raw| to a new buffer that must be freed by the caller. It |
| 336 |
* returns one on success and zero on error. */ |
| 337 |
static int pass_to_pass_raw(int pbe_nid, const char *pass, int pass_len, |
| 338 |
uint8_t **out_pass_raw, size_t *out_pass_raw_len) { |
| 339 |
if (pass == NULL) { |
| 340 |
*out_pass_raw = NULL; |
| 341 |
*out_pass_raw_len = 0; |
| 342 |
return 1; |
| 343 |
} |
| 344 |
|
| 345 |
if (pass_len == -1) { |
| 346 |
pass_len = strlen(pass); |
| 347 |
} else if (pass_len < 0 || pass_len > 2000000000) { |
| 348 |
OPENSSL_PUT_ERROR(PKCS8, ERR_R_OVERFLOW); |
| 349 |
return 0; |
| 350 |
} |
| 351 |
|
| 352 |
const struct pbe_suite *suite = get_pbe_suite(pbe_nid); |
| 353 |
if (suite != NULL && (suite->flags & PBE_UCS2_CONVERT_PASSWORD)) { |
| 354 |
if (!ascii_to_ucs2(pass, pass_len, out_pass_raw, out_pass_raw_len)) { |
| 355 |
OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_DECODE_ERROR); |
| 356 |
return 0; |
| 357 |
} |
| 358 |
} else { |
| 359 |
*out_pass_raw = BUF_memdup(pass, pass_len); |
| 360 |
if (*out_pass_raw == NULL) { |
| 361 |
OPENSSL_PUT_ERROR(PKCS8, ERR_R_MALLOC_FAILURE); |
| 362 |
return 0; |
| 363 |
} |
| 364 |
*out_pass_raw_len = (size_t)pass_len; |
| 365 |
} |
| 366 |
|
| 367 |
return 1; |
| 368 |
} |
| 369 |
|
| 370 |
static int pbe_cipher_init(ASN1_OBJECT *pbe_obj, |
| 371 |
const uint8_t *pass_raw, size_t pass_raw_len, |
| 372 |
ASN1_TYPE *param, |
| 373 |
EVP_CIPHER_CTX *ctx, int is_encrypt) { |
| 374 |
const EVP_CIPHER *cipher; |
| 375 |
const EVP_MD *md; |
| 376 |
|
| 377 |
const struct pbe_suite *suite = get_pbe_suite(OBJ_obj2nid(pbe_obj)); |
| 378 |
if (suite == NULL) { |
| 379 |
char obj_str[80]; |
| 380 |
OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_UNKNOWN_ALGORITHM); |
| 381 |
if (!pbe_obj) { |
| 382 |
strncpy(obj_str, "NULL", sizeof(obj_str)); |
| 383 |
} else { |
| 384 |
i2t_ASN1_OBJECT(obj_str, sizeof(obj_str), pbe_obj); |
| 385 |
} |
| 386 |
ERR_add_error_data(2, "TYPE=", obj_str); |
| 387 |
return 0; |
| 388 |
} |
| 389 |
|
| 390 |
if (suite->cipher_func == NULL) { |
| 391 |
cipher = NULL; |
| 392 |
} else { |
| 393 |
cipher = suite->cipher_func(); |
| 394 |
if (!cipher) { |
| 395 |
OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_UNKNOWN_CIPHER); |
| 396 |
return 0; |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
if (suite->md_func == NULL) { |
| 401 |
md = NULL; |
| 402 |
} else { |
| 403 |
md = suite->md_func(); |
| 404 |
if (!md) { |
| 405 |
OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_UNKNOWN_DIGEST); |
| 406 |
return 0; |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
if (!suite->keygen(ctx, pass_raw, pass_raw_len, param, cipher, md, |
| 411 |
is_encrypt)) { |
| 412 |
OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_KEYGEN_FAILURE); |
| 413 |
return 0; |
| 414 |
} |
| 415 |
|
| 416 |
return 1; |
| 417 |
} |
| 418 |
|
| 419 |
static int pbe_crypt(const X509_ALGOR *algor, |
| 420 |
const uint8_t *pass_raw, size_t pass_raw_len, |
| 421 |
const uint8_t *in, size_t in_len, |
| 422 |
uint8_t **out, size_t *out_len, |
| 423 |
int is_encrypt) { |
| 424 |
uint8_t *buf; |
| 425 |
int n, ret = 0; |
| 426 |
EVP_CIPHER_CTX ctx; |
| 427 |
unsigned block_size; |
| 428 |
|
| 429 |
EVP_CIPHER_CTX_init(&ctx); |
| 430 |
|
| 431 |
if (!pbe_cipher_init(algor->algorithm, pass_raw, pass_raw_len, |
| 432 |
algor->parameter, &ctx, is_encrypt)) { |
| 433 |
OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_UNKNOWN_CIPHER_ALGORITHM); |
| 434 |
return 0; |
| 435 |
} |
| 436 |
block_size = EVP_CIPHER_CTX_block_size(&ctx); |
| 437 |
|
| 438 |
if (in_len + block_size < in_len) { |
| 439 |
OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_TOO_LONG); |
| 440 |
goto err; |
| 441 |
} |
| 442 |
|
| 443 |
buf = OPENSSL_malloc(in_len + block_size); |
| 444 |
if (buf == NULL) { |
| 445 |
OPENSSL_PUT_ERROR(PKCS8, ERR_R_MALLOC_FAILURE); |
| 446 |
goto err; |
| 447 |
} |
| 448 |
|
| 449 |
if (!EVP_CipherUpdate(&ctx, buf, &n, in, in_len)) { |
| 450 |
OPENSSL_free(buf); |
| 451 |
OPENSSL_PUT_ERROR(PKCS8, ERR_R_EVP_LIB); |
| 452 |
goto err; |
| 453 |
} |
| 454 |
*out_len = n; |
| 455 |
|
| 456 |
if (!EVP_CipherFinal_ex(&ctx, buf + n, &n)) { |
| 457 |
OPENSSL_free(buf); |
| 458 |
OPENSSL_PUT_ERROR(PKCS8, ERR_R_EVP_LIB); |
| 459 |
goto err; |
| 460 |
} |
| 461 |
*out_len += n; |
| 462 |
*out = buf; |
| 463 |
ret = 1; |
| 464 |
|
| 465 |
err: |
| 466 |
EVP_CIPHER_CTX_cleanup(&ctx); |
| 467 |
return ret; |
| 468 |
} |
| 469 |
|
| 470 |
static void *pkcs12_item_decrypt_d2i(X509_ALGOR *algor, const ASN1_ITEM *it, |
| 471 |
const uint8_t *pass_raw, |
| 472 |
size_t pass_raw_len, |
| 473 |
ASN1_OCTET_STRING *oct) { |
| 474 |
uint8_t *out; |
| 475 |
const uint8_t *p; |
| 476 |
void *ret; |
| 477 |
size_t out_len; |
| 478 |
|
| 479 |
if (!pbe_crypt(algor, pass_raw, pass_raw_len, oct->data, oct->length, |
| 480 |
&out, &out_len, 0 /* decrypt */)) { |
| 481 |
OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_CRYPT_ERROR); |
| 482 |
return NULL; |
| 483 |
} |
| 484 |
p = out; |
| 485 |
ret = ASN1_item_d2i(NULL, &p, out_len, it); |
| 486 |
OPENSSL_cleanse(out, out_len); |
| 487 |
if (!ret) { |
| 488 |
OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_DECODE_ERROR); |
| 489 |
} |
| 490 |
OPENSSL_free(out); |
| 491 |
return ret; |
| 492 |
} |
| 493 |
|
| 494 |
PKCS8_PRIV_KEY_INFO *PKCS8_decrypt(X509_SIG *pkcs8, const char *pass, |
| 495 |
int pass_len) { |
| 496 |
uint8_t *pass_raw = NULL; |
| 497 |
size_t pass_raw_len = 0; |
| 498 |
if (!pass_to_pass_raw(OBJ_obj2nid(pkcs8->algor->algorithm), pass, pass_len, |
| 499 |
&pass_raw, &pass_raw_len)) { |
| 500 |
return NULL; |
| 501 |
} |
| 502 |
|
| 503 |
PKCS8_PRIV_KEY_INFO *ret = PKCS8_decrypt_pbe(pkcs8, pass_raw, pass_raw_len); |
| 504 |
|
| 505 |
if (pass_raw) { |
| 506 |
OPENSSL_cleanse(pass_raw, pass_raw_len); |
| 507 |
OPENSSL_free(pass_raw); |
| 508 |
} |
| 509 |
return ret; |
| 510 |
} |
| 511 |
|
| 512 |
PKCS8_PRIV_KEY_INFO *PKCS8_decrypt_pbe(X509_SIG *pkcs8, const uint8_t *pass_raw, |
| 513 |
size_t pass_raw_len) { |
| 514 |
return pkcs12_item_decrypt_d2i(pkcs8->algor, |
| 515 |
ASN1_ITEM_rptr(PKCS8_PRIV_KEY_INFO), pass_raw, |
| 516 |
pass_raw_len, pkcs8->digest); |
| 517 |
} |
| 518 |
|
| 519 |
static ASN1_OCTET_STRING *pkcs12_item_i2d_encrypt(X509_ALGOR *algor, |
| 520 |
const ASN1_ITEM *it, |
| 521 |
const uint8_t *pass_raw, |
| 522 |
size_t pass_raw_len, void *obj) { |
| 523 |
ASN1_OCTET_STRING *oct; |
| 524 |
uint8_t *in = NULL; |
| 525 |
int in_len; |
| 526 |
size_t crypt_len; |
| 527 |
|
| 528 |
oct = M_ASN1_OCTET_STRING_new(); |
| 529 |
if (oct == NULL) { |
| 530 |
OPENSSL_PUT_ERROR(PKCS8, ERR_R_MALLOC_FAILURE); |
| 531 |
return NULL; |
| 532 |
} |
| 533 |
in_len = ASN1_item_i2d(obj, &in, it); |
| 534 |
if (!in) { |
| 535 |
OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_ENCODE_ERROR); |
| 536 |
return NULL; |
| 537 |
} |
| 538 |
if (!pbe_crypt(algor, pass_raw, pass_raw_len, in, in_len, &oct->data, &crypt_len, |
| 539 |
1 /* encrypt */)) { |
| 540 |
OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_ENCRYPT_ERROR); |
| 541 |
OPENSSL_free(in); |
| 542 |
return NULL; |
| 543 |
} |
| 544 |
oct->length = crypt_len; |
| 545 |
OPENSSL_cleanse(in, in_len); |
| 546 |
OPENSSL_free(in); |
| 547 |
return oct; |
| 548 |
} |
| 549 |
|
| 550 |
X509_SIG *PKCS8_encrypt(int pbe_nid, const EVP_CIPHER *cipher, const char *pass, |
| 551 |
int pass_len, uint8_t *salt, size_t salt_len, |
| 552 |
int iterations, PKCS8_PRIV_KEY_INFO *p8inf) { |
| 553 |
uint8_t *pass_raw = NULL; |
| 554 |
size_t pass_raw_len = 0; |
| 555 |
if (!pass_to_pass_raw(pbe_nid, pass, pass_len, &pass_raw, &pass_raw_len)) { |
| 556 |
return NULL; |
| 557 |
} |
| 558 |
|
| 559 |
X509_SIG *ret = PKCS8_encrypt_pbe(pbe_nid, cipher, pass_raw, pass_raw_len, |
| 560 |
salt, salt_len, iterations, p8inf); |
| 561 |
|
| 562 |
if (pass_raw) { |
| 563 |
OPENSSL_cleanse(pass_raw, pass_raw_len); |
| 564 |
OPENSSL_free(pass_raw); |
| 565 |
} |
| 566 |
return ret; |
| 567 |
} |
| 568 |
|
| 569 |
X509_SIG *PKCS8_encrypt_pbe(int pbe_nid, const EVP_CIPHER *cipher, |
| 570 |
const uint8_t *pass_raw, size_t pass_raw_len, |
| 571 |
uint8_t *salt, size_t salt_len, |
| 572 |
int iterations, PKCS8_PRIV_KEY_INFO *p8inf) { |
| 573 |
X509_SIG *pkcs8 = NULL; |
| 574 |
X509_ALGOR *pbe; |
| 575 |
|
| 576 |
pkcs8 = X509_SIG_new(); |
| 577 |
if (pkcs8 == NULL) { |
| 578 |
OPENSSL_PUT_ERROR(PKCS8, ERR_R_MALLOC_FAILURE); |
| 579 |
goto err; |
| 580 |
} |
| 581 |
|
| 582 |
if (pbe_nid == -1) { |
| 583 |
pbe = PKCS5_pbe2_set(cipher, iterations, salt, salt_len); |
| 584 |
} else { |
| 585 |
pbe = PKCS5_pbe_set(pbe_nid, iterations, salt, salt_len); |
| 586 |
} |
| 587 |
if (!pbe) { |
| 588 |
OPENSSL_PUT_ERROR(PKCS8, ERR_R_ASN1_LIB); |
| 589 |
goto err; |
| 590 |
} |
| 591 |
|
| 592 |
X509_ALGOR_free(pkcs8->algor); |
| 593 |
pkcs8->algor = pbe; |
| 594 |
M_ASN1_OCTET_STRING_free(pkcs8->digest); |
| 595 |
pkcs8->digest = pkcs12_item_i2d_encrypt( |
| 596 |
pbe, ASN1_ITEM_rptr(PKCS8_PRIV_KEY_INFO), pass_raw, pass_raw_len, p8inf); |
| 597 |
if (!pkcs8->digest) { |
| 598 |
OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_ENCRYPT_ERROR); |
| 599 |
goto err; |
| 600 |
} |
| 601 |
|
| 602 |
return pkcs8; |
| 603 |
|
| 604 |
err: |
| 605 |
X509_SIG_free(pkcs8); |
| 606 |
return NULL; |
| 607 |
} |
| 608 |
|
| 609 |
EVP_PKEY *EVP_PKCS82PKEY(PKCS8_PRIV_KEY_INFO *p8) { |
| 610 |
uint8_t *der = NULL; |
| 611 |
int der_len = i2d_PKCS8_PRIV_KEY_INFO(p8, &der); |
| 612 |
if (der_len < 0) { |
| 613 |
return NULL; |
| 614 |
} |
| 615 |
|
| 616 |
CBS cbs; |
| 617 |
CBS_init(&cbs, der, (size_t)der_len); |
| 618 |
EVP_PKEY *ret = EVP_parse_private_key(&cbs); |
| 619 |
if (ret == NULL || CBS_len(&cbs) != 0) { |
| 620 |
OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_DECODE_ERROR); |
| 621 |
EVP_PKEY_free(ret); |
| 622 |
OPENSSL_free(der); |
| 623 |
return NULL; |
| 624 |
} |
| 625 |
|
| 626 |
OPENSSL_free(der); |
| 627 |
return ret; |
| 628 |
} |
| 629 |
|
| 630 |
PKCS8_PRIV_KEY_INFO *EVP_PKEY2PKCS8(EVP_PKEY *pkey) { |
| 631 |
CBB cbb; |
| 632 |
uint8_t *der = NULL; |
| 633 |
size_t der_len; |
| 634 |
if (!CBB_init(&cbb, 0) || |
| 635 |
!EVP_marshal_private_key(&cbb, pkey) || |
| 636 |
!CBB_finish(&cbb, &der, &der_len) || |
| 637 |
der_len > LONG_MAX) { |
| 638 |
CBB_cleanup(&cbb); |
| 639 |
OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_ENCODE_ERROR); |
| 640 |
goto err; |
| 641 |
} |
| 642 |
|
| 643 |
const uint8_t *p = der; |
| 644 |
PKCS8_PRIV_KEY_INFO *p8 = d2i_PKCS8_PRIV_KEY_INFO(NULL, &p, (long)der_len); |
| 645 |
if (p8 == NULL || p != der + der_len) { |
| 646 |
PKCS8_PRIV_KEY_INFO_free(p8); |
| 647 |
OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_DECODE_ERROR); |
| 648 |
goto err; |
| 649 |
} |
| 650 |
|
| 651 |
OPENSSL_free(der); |
| 652 |
return p8; |
| 653 |
|
| 654 |
err: |
| 655 |
OPENSSL_free(der); |
| 656 |
return NULL; |
| 657 |
} |
| 658 |
|
| 659 |
struct pkcs12_context { |
| 660 |
EVP_PKEY **out_key; |
| 661 |
STACK_OF(X509) *out_certs; |
| 662 |
uint8_t *password; |
| 663 |
size_t password_len; |
| 664 |
}; |
| 665 |
|
| 666 |
static int PKCS12_handle_content_info(CBS *content_info, unsigned depth, |
| 667 |
struct pkcs12_context *ctx); |
| 668 |
|
| 669 |
/* PKCS12_handle_content_infos parses a series of PKCS#7 ContentInfos in a |
| 670 |
* SEQUENCE. */ |
| 671 |
static int PKCS12_handle_content_infos(CBS *content_infos, |
| 672 |
unsigned depth, |
| 673 |
struct pkcs12_context *ctx) { |
| 674 |
uint8_t *der_bytes = NULL; |
| 675 |
size_t der_len; |
| 676 |
CBS in; |
| 677 |
int ret = 0; |
| 678 |
|
| 679 |
/* Generally we only expect depths 0 (the top level, with a |
| 680 |
* pkcs7-encryptedData and a pkcs7-data) and depth 1 (the various PKCS#12 |
| 681 |
* bags). */ |
| 682 |
if (depth > 3) { |
| 683 |
OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_PKCS12_TOO_DEEPLY_NESTED); |
| 684 |
return 0; |
| 685 |
} |
| 686 |
|
| 687 |
/* Although a BER->DER conversion is done at the beginning of |PKCS12_parse|, |
| 688 |
* the ASN.1 data gets wrapped in OCTETSTRINGs and/or encrypted and the |
| 689 |
* conversion cannot see through those wrappings. So each time we step |
| 690 |
* through one we need to convert to DER again. */ |
| 691 |
if (!CBS_asn1_ber_to_der(content_infos, &der_bytes, &der_len)) { |
| 692 |
OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA); |
| 693 |
return 0; |
| 694 |
} |
| 695 |
|
| 696 |
if (der_bytes != NULL) { |
| 697 |
CBS_init(&in, der_bytes, der_len); |
| 698 |
} else { |
| 699 |
CBS_init(&in, CBS_data(content_infos), CBS_len(content_infos)); |
| 700 |
} |
| 701 |
|
| 702 |
if (!CBS_get_asn1(&in, &in, CBS_ASN1_SEQUENCE)) { |
| 703 |
OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA); |
| 704 |
goto err; |
| 705 |
} |
| 706 |
|
| 707 |
while (CBS_len(&in) > 0) { |
| 708 |
CBS content_info; |
| 709 |
if (!CBS_get_asn1(&in, &content_info, CBS_ASN1_SEQUENCE)) { |
| 710 |
OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA); |
| 711 |
goto err; |
| 712 |
} |
| 713 |
|
| 714 |
if (!PKCS12_handle_content_info(&content_info, depth + 1, ctx)) { |
| 715 |
goto err; |
| 716 |
} |
| 717 |
} |
| 718 |
|
| 719 |
/* NSS includes additional data after the SEQUENCE, but it's an (unwrapped) |
| 720 |
* copy of the same encrypted private key (with the same IV and |
| 721 |
* ciphertext)! */ |
| 722 |
|
| 723 |
ret = 1; |
| 724 |
|
| 725 |
err: |
| 726 |
OPENSSL_free(der_bytes); |
| 727 |
return ret; |
| 728 |
} |
| 729 |
|
| 730 |
/* PKCS12_handle_content_info parses a single PKCS#7 ContentInfo element in a |
| 731 |
* PKCS#12 structure. */ |
| 732 |
static int PKCS12_handle_content_info(CBS *content_info, unsigned depth, |
| 733 |
struct pkcs12_context *ctx) { |
| 734 |
CBS content_type, wrapped_contents, contents, content_infos; |
| 735 |
int nid, ret = 0; |
| 736 |
uint8_t *storage = NULL; |
| 737 |
|
| 738 |
if (!CBS_get_asn1(content_info, &content_type, CBS_ASN1_OBJECT) || |
| 739 |
!CBS_get_asn1(content_info, &wrapped_contents, |
| 740 |
CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0)) { |
| 741 |
OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA); |
| 742 |
goto err; |
| 743 |
} |
| 744 |
|
| 745 |
nid = OBJ_cbs2nid(&content_type); |
| 746 |
if (nid == NID_pkcs7_encrypted) { |
| 747 |
/* See https://tools.ietf.org/html/rfc2315#section-13. |
| 748 |
* |
| 749 |
* PKCS#7 encrypted data inside a PKCS#12 structure is generally an |
| 750 |
* encrypted certificate bag and it's generally encrypted with 40-bit |
| 751 |
* RC2-CBC. */ |
| 752 |
CBS version_bytes, eci, contents_type, ai, encrypted_contents; |
| 753 |
X509_ALGOR *algor = NULL; |
| 754 |
const uint8_t *inp; |
| 755 |
uint8_t *out; |
| 756 |
size_t out_len; |
| 757 |
|
| 758 |
if (!CBS_get_asn1(&wrapped_contents, &contents, CBS_ASN1_SEQUENCE) || |
| 759 |
!CBS_get_asn1(&contents, &version_bytes, CBS_ASN1_INTEGER) || |
| 760 |
/* EncryptedContentInfo, see |
| 761 |
* https://tools.ietf.org/html/rfc2315#section-10.1 */ |
| 762 |
!CBS_get_asn1(&contents, &eci, CBS_ASN1_SEQUENCE) || |
| 763 |
!CBS_get_asn1(&eci, &contents_type, CBS_ASN1_OBJECT) || |
| 764 |
/* AlgorithmIdentifier, see |
| 765 |
* https://tools.ietf.org/html/rfc5280#section-4.1.1.2 */ |
| 766 |
!CBS_get_asn1_element(&eci, &ai, CBS_ASN1_SEQUENCE) || |
| 767 |
!CBS_get_asn1_implicit_string( |
| 768 |
&eci, &encrypted_contents, &storage, |
| 769 |
CBS_ASN1_CONTEXT_SPECIFIC | 0, CBS_ASN1_OCTETSTRING)) { |
| 770 |
OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA); |
| 771 |
goto err; |
| 772 |
} |
| 773 |
|
| 774 |
if (OBJ_cbs2nid(&contents_type) != NID_pkcs7_data || |
| 775 |
CBS_len(&ai) > LONG_MAX) { |
| 776 |
OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA); |
| 777 |
goto err; |
| 778 |
} |
| 779 |
|
| 780 |
inp = CBS_data(&ai); |
| 781 |
algor = d2i_X509_ALGOR(NULL, &inp, (long)CBS_len(&ai)); |
| 782 |
if (algor == NULL) { |
| 783 |
goto err; |
| 784 |
} |
| 785 |
if (inp != CBS_data(&ai) + CBS_len(&ai)) { |
| 786 |
X509_ALGOR_free(algor); |
| 787 |
OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA); |
| 788 |
goto err; |
| 789 |
} |
| 790 |
|
| 791 |
if (!pbe_crypt(algor, ctx->password, ctx->password_len, |
| 792 |
CBS_data(&encrypted_contents), CBS_len(&encrypted_contents), |
| 793 |
&out, &out_len, 0 /* decrypt */)) { |
| 794 |
X509_ALGOR_free(algor); |
| 795 |
goto err; |
| 796 |
} |
| 797 |
X509_ALGOR_free(algor); |
| 798 |
|
| 799 |
CBS_init(&content_infos, out, out_len); |
| 800 |
ret = PKCS12_handle_content_infos(&content_infos, depth + 1, ctx); |
| 801 |
OPENSSL_free(out); |
| 802 |
} else if (nid == NID_pkcs7_data) { |
| 803 |
CBS octet_string_contents; |
| 804 |
|
| 805 |
if (!CBS_get_asn1(&wrapped_contents, &octet_string_contents, |
| 806 |
CBS_ASN1_OCTETSTRING)) { |
| 807 |
OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA); |
| 808 |
goto err; |
| 809 |
} |
| 810 |
|
| 811 |
ret = PKCS12_handle_content_infos(&octet_string_contents, depth + 1, ctx); |
| 812 |
} else if (nid == NID_pkcs8ShroudedKeyBag) { |
| 813 |
/* See ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-12/pkcs-12v1.pdf, section |
| 814 |
* 4.2.2. */ |
| 815 |
const uint8_t *inp = CBS_data(&wrapped_contents); |
| 816 |
PKCS8_PRIV_KEY_INFO *pki = NULL; |
| 817 |
X509_SIG *encrypted = NULL; |
| 818 |
|
| 819 |
if (*ctx->out_key) { |
| 820 |
OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_MULTIPLE_PRIVATE_KEYS_IN_PKCS12); |
| 821 |
goto err; |
| 822 |
} |
| 823 |
|
| 824 |
if (CBS_len(&wrapped_contents) > LONG_MAX) { |
| 825 |
OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA); |
| 826 |
goto err; |
| 827 |
} |
| 828 |
|
| 829 |
/* encrypted isn't actually an X.509 signature, but it has the same |
| 830 |
* structure as one and so |X509_SIG| is reused to store it. */ |
| 831 |
encrypted = d2i_X509_SIG(NULL, &inp, (long)CBS_len(&wrapped_contents)); |
| 832 |
if (encrypted == NULL) { |
| 833 |
OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA); |
| 834 |
goto err; |
| 835 |
} |
| 836 |
if (inp != CBS_data(&wrapped_contents) + CBS_len(&wrapped_contents)) { |
| 837 |
OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA); |
| 838 |
X509_SIG_free(encrypted); |
| 839 |
goto err; |
| 840 |
} |
| 841 |
|
| 842 |
pki = PKCS8_decrypt_pbe(encrypted, ctx->password, ctx->password_len); |
| 843 |
X509_SIG_free(encrypted); |
| 844 |
if (pki == NULL) { |
| 845 |
goto err; |
| 846 |
} |
| 847 |
|
| 848 |
*ctx->out_key = EVP_PKCS82PKEY(pki); |
| 849 |
PKCS8_PRIV_KEY_INFO_free(pki); |
| 850 |
|
| 851 |
if (ctx->out_key == NULL) { |
| 852 |
goto err; |
| 853 |
} |
| 854 |
ret = 1; |
| 855 |
} else if (nid == NID_certBag) { |
| 856 |
CBS cert_bag, cert_type, wrapped_cert, cert; |
| 857 |
|
| 858 |
if (!CBS_get_asn1(&wrapped_contents, &cert_bag, CBS_ASN1_SEQUENCE) || |
| 859 |
!CBS_get_asn1(&cert_bag, &cert_type, CBS_ASN1_OBJECT) || |
| 860 |
!CBS_get_asn1(&cert_bag, &wrapped_cert, |
| 861 |
CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0) || |
| 862 |
!CBS_get_asn1(&wrapped_cert, &cert, CBS_ASN1_OCTETSTRING)) { |
| 863 |
OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA); |
| 864 |
goto err; |
| 865 |
} |
| 866 |
|
| 867 |
if (OBJ_cbs2nid(&cert_type) == NID_x509Certificate) { |
| 868 |
if (CBS_len(&cert) > LONG_MAX) { |
| 869 |
OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA); |
| 870 |
goto err; |
| 871 |
} |
| 872 |
const uint8_t *inp = CBS_data(&cert); |
| 873 |
X509 *x509 = d2i_X509(NULL, &inp, (long)CBS_len(&cert)); |
| 874 |
if (!x509) { |
| 875 |
OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA); |
| 876 |
goto err; |
| 877 |
} |
| 878 |
if (inp != CBS_data(&cert) + CBS_len(&cert)) { |
| 879 |
OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA); |
| 880 |
X509_free(x509); |
| 881 |
goto err; |
| 882 |
} |
| 883 |
|
| 884 |
if (0 == sk_X509_push(ctx->out_certs, x509)) { |
| 885 |
X509_free(x509); |
| 886 |
goto err; |
| 887 |
} |
| 888 |
} |
| 889 |
ret = 1; |
| 890 |
} else { |
| 891 |
/* Unknown element type - ignore it. */ |
| 892 |
ret = 1; |
| 893 |
} |
| 894 |
|
| 895 |
err: |
| 896 |
OPENSSL_free(storage); |
| 897 |
return ret; |
| 898 |
} |
| 899 |
|
| 900 |
int PKCS12_get_key_and_certs(EVP_PKEY **out_key, STACK_OF(X509) *out_certs, |
| 901 |
CBS *ber_in, const char *password) { |
| 902 |
uint8_t *der_bytes = NULL; |
| 903 |
size_t der_len; |
| 904 |
CBS in, pfx, mac_data, authsafe, content_type, wrapped_authsafes, authsafes; |
| 905 |
uint64_t version; |
| 906 |
int ret = 0; |
| 907 |
struct pkcs12_context ctx; |
| 908 |
const size_t original_out_certs_len = sk_X509_num(out_certs); |
| 909 |
|
| 910 |
/* The input may be in BER format. */ |
| 911 |
if (!CBS_asn1_ber_to_der(ber_in, &der_bytes, &der_len)) { |
| 912 |
OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA); |
| 913 |
return 0; |
| 914 |
} |
| 915 |
if (der_bytes != NULL) { |
| 916 |
CBS_init(&in, der_bytes, der_len); |
| 917 |
} else { |
| 918 |
CBS_init(&in, CBS_data(ber_in), CBS_len(ber_in)); |
| 919 |
} |
| 920 |
|
| 921 |
*out_key = NULL; |
| 922 |
memset(&ctx, 0, sizeof(ctx)); |
| 923 |
|
| 924 |
/* See ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-12/pkcs-12v1.pdf, section |
| 925 |
* four. */ |
| 926 |
if (!CBS_get_asn1(&in, &pfx, CBS_ASN1_SEQUENCE) || |
| 927 |
CBS_len(&in) != 0 || |
| 928 |
!CBS_get_asn1_uint64(&pfx, &version)) { |
| 929 |
OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA); |
| 930 |
goto err; |
| 931 |
} |
| 932 |
|
| 933 |
if (version < 3) { |
| 934 |
OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_VERSION); |
| 935 |
goto err; |
| 936 |
} |
| 937 |
|
| 938 |
if (!CBS_get_asn1(&pfx, &authsafe, CBS_ASN1_SEQUENCE)) { |
| 939 |
OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA); |
| 940 |
goto err; |
| 941 |
} |
| 942 |
|
| 943 |
if (CBS_len(&pfx) == 0) { |
| 944 |
OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_MISSING_MAC); |
| 945 |
goto err; |
| 946 |
} |
| 947 |
|
| 948 |
if (!CBS_get_asn1(&pfx, &mac_data, CBS_ASN1_SEQUENCE)) { |
| 949 |
OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA); |
| 950 |
goto err; |
| 951 |
} |
| 952 |
|
| 953 |
/* authsafe is a PKCS#7 ContentInfo. See |
| 954 |
* https://tools.ietf.org/html/rfc2315#section-7. */ |
| 955 |
if (!CBS_get_asn1(&authsafe, &content_type, CBS_ASN1_OBJECT) || |
| 956 |
!CBS_get_asn1(&authsafe, &wrapped_authsafes, |
| 957 |
CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0)) { |
| 958 |
OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA); |
| 959 |
goto err; |
| 960 |
} |
| 961 |
|
| 962 |
/* The content type can either be |NID_pkcs7_data| or |NID_pkcs7_signed|. The |
| 963 |
* latter indicates that it's signed by a public key, which isn't |
| 964 |
* supported. */ |
| 965 |
if (OBJ_cbs2nid(&content_type) != NID_pkcs7_data) { |
| 966 |
OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_PKCS12_PUBLIC_KEY_INTEGRITY_NOT_SUPPORTED); |
| 967 |
goto err; |
| 968 |
} |
| 969 |
|
| 970 |
if (!CBS_get_asn1(&wrapped_authsafes, &authsafes, CBS_ASN1_OCTETSTRING)) { |
| 971 |
OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA); |
| 972 |
goto err; |
| 973 |
} |
| 974 |
|
| 975 |
ctx.out_key = out_key; |
| 976 |
ctx.out_certs = out_certs; |
| 977 |
if (!ascii_to_ucs2(password, password ? strlen(password) : 0, &ctx.password, |
| 978 |
&ctx.password_len)) { |
| 979 |
OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_DECODE_ERROR); |
| 980 |
goto err; |
| 981 |
} |
| 982 |
|
| 983 |
/* Verify the MAC. */ |
| 984 |
{ |
| 985 |
CBS mac, hash_type_seq, hash_oid, salt, expected_mac; |
| 986 |
uint64_t iterations; |
| 987 |
int hash_nid; |
| 988 |
const EVP_MD *md; |
| 989 |
uint8_t hmac_key[EVP_MAX_MD_SIZE]; |
| 990 |
uint8_t hmac[EVP_MAX_MD_SIZE]; |
| 991 |
unsigned hmac_len; |
| 992 |
|
| 993 |
if (!CBS_get_asn1(&mac_data, &mac, CBS_ASN1_SEQUENCE) || |
| 994 |
!CBS_get_asn1(&mac, &hash_type_seq, CBS_ASN1_SEQUENCE) || |
| 995 |
!CBS_get_asn1(&hash_type_seq, &hash_oid, CBS_ASN1_OBJECT) || |
| 996 |
!CBS_get_asn1(&mac, &expected_mac, CBS_ASN1_OCTETSTRING) || |
| 997 |
!CBS_get_asn1(&mac_data, &salt, CBS_ASN1_OCTETSTRING)) { |
| 998 |
OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA); |
| 999 |
goto err; |
| 1000 |
} |
| 1001 |
|
| 1002 |
/* The iteration count is optional and the default is one. */ |
| 1003 |
iterations = 1; |
| 1004 |
if (CBS_len(&mac_data) > 0) { |
| 1005 |
if (!CBS_get_asn1_uint64(&mac_data, &iterations) || |
| 1006 |
iterations > INT_MAX) { |
| 1007 |
OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA); |
| 1008 |
goto err; |
| 1009 |
} |
| 1010 |
} |
| 1011 |
|
| 1012 |
hash_nid = OBJ_cbs2nid(&hash_oid); |
| 1013 |
if (hash_nid == NID_undef || |
| 1014 |
(md = EVP_get_digestbynid(hash_nid)) == NULL) { |
| 1015 |
OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_UNKNOWN_HASH); |
| 1016 |
goto err; |
| 1017 |
} |
| 1018 |
|
| 1019 |
if (!pkcs12_key_gen_raw(ctx.password, ctx.password_len, CBS_data(&salt), |
| 1020 |
CBS_len(&salt), PKCS12_MAC_ID, iterations, |
| 1021 |
EVP_MD_size(md), hmac_key, md)) { |
| 1022 |
goto err; |
| 1023 |
} |
| 1024 |
|
| 1025 |
if (NULL == HMAC(md, hmac_key, EVP_MD_size(md), CBS_data(&authsafes), |
| 1026 |
CBS_len(&authsafes), hmac, &hmac_len)) { |
| 1027 |
goto err; |
| 1028 |
} |
| 1029 |
|
| 1030 |
if (!CBS_mem_equal(&expected_mac, hmac, hmac_len)) { |
| 1031 |
OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_INCORRECT_PASSWORD); |
| 1032 |
goto err; |
| 1033 |
} |
| 1034 |
} |
| 1035 |
|
| 1036 |
/* authsafes contains a series of PKCS#7 ContentInfos. */ |
| 1037 |
if (!PKCS12_handle_content_infos(&authsafes, 0, &ctx)) { |
| 1038 |
goto err; |
| 1039 |
} |
| 1040 |
|
| 1041 |
ret = 1; |
| 1042 |
|
| 1043 |
err: |
| 1044 |
OPENSSL_free(ctx.password); |
| 1045 |
OPENSSL_free(der_bytes); |
| 1046 |
if (!ret) { |
| 1047 |
EVP_PKEY_free(*out_key); |
| 1048 |
*out_key = NULL; |
| 1049 |
while (sk_X509_num(out_certs) > original_out_certs_len) { |
| 1050 |
X509 *x509 = sk_X509_pop(out_certs); |
| 1051 |
X509_free(x509); |
| 1052 |
} |
| 1053 |
} |
| 1054 |
|
| 1055 |
return ret; |
| 1056 |
} |
| 1057 |
|
| 1058 |
void PKCS12_PBE_add(void) {} |
| 1059 |
|
| 1060 |
struct pkcs12_st { |
| 1061 |
uint8_t *ber_bytes; |
| 1062 |
size_t ber_len; |
| 1063 |
}; |
| 1064 |
|
| 1065 |
PKCS12* d2i_PKCS12(PKCS12 **out_p12, const uint8_t **ber_bytes, size_t ber_len) { |
| 1066 |
PKCS12 *p12; |
| 1067 |
|
| 1068 |
p12 = OPENSSL_malloc(sizeof(PKCS12)); |
| 1069 |
if (!p12) { |
| 1070 |
return NULL; |
| 1071 |
} |
| 1072 |
|
| 1073 |
p12->ber_bytes = OPENSSL_malloc(ber_len); |
| 1074 |
if (!p12->ber_bytes) { |
| 1075 |
OPENSSL_free(p12); |
| 1076 |
return NULL; |
| 1077 |
} |
| 1078 |
|
| 1079 |
memcpy(p12->ber_bytes, *ber_bytes, ber_len); |
| 1080 |
p12->ber_len = ber_len; |
| 1081 |
*ber_bytes += ber_len; |
| 1082 |
|
| 1083 |
if (out_p12) { |
| 1084 |
PKCS12_free(*out_p12); |
| 1085 |
|
| 1086 |
*out_p12 = p12; |
| 1087 |
} |
| 1088 |
|
| 1089 |
return p12; |
| 1090 |
} |
| 1091 |
|
| 1092 |
PKCS12* d2i_PKCS12_bio(BIO *bio, PKCS12 **out_p12) { |
| 1093 |
size_t used = 0; |
| 1094 |
BUF_MEM *buf; |
| 1095 |
const uint8_t *dummy; |
| 1096 |
static const size_t kMaxSize = 256 * 1024; |
| 1097 |
PKCS12 *ret = NULL; |
| 1098 |
|
| 1099 |
buf = BUF_MEM_new(); |
| 1100 |
if (buf == NULL) { |
| 1101 |
return NULL; |
| 1102 |
} |
| 1103 |
if (BUF_MEM_grow(buf, 8192) == 0) { |
| 1104 |
goto out; |
| 1105 |
} |
| 1106 |
|
| 1107 |
for (;;) { |
| 1108 |
int n = BIO_read(bio, &buf->data[used], buf->length - used); |
| 1109 |
if (n < 0) { |
| 1110 |
if (used == 0) { |
| 1111 |
goto out; |
| 1112 |
} |
| 1113 |
/* Workaround a bug in node.js. It uses a memory BIO for this in the wrong |
| 1114 |
* mode. */ |
| 1115 |
n = 0; |
| 1116 |
} |
| 1117 |
|
| 1118 |
if (n == 0) { |
| 1119 |
break; |
| 1120 |
} |
| 1121 |
used += n; |
| 1122 |
|
| 1123 |
if (used < buf->length) { |
| 1124 |
continue; |
| 1125 |
} |
| 1126 |
|
| 1127 |
if (buf->length > kMaxSize || |
| 1128 |
BUF_MEM_grow(buf, buf->length * 2) == 0) { |
| 1129 |
goto out; |
| 1130 |
} |
| 1131 |
} |
| 1132 |
|
| 1133 |
dummy = (uint8_t*) buf->data; |
| 1134 |
ret = d2i_PKCS12(out_p12, &dummy, used); |
| 1135 |
|
| 1136 |
out: |
| 1137 |
BUF_MEM_free(buf); |
| 1138 |
return ret; |
| 1139 |
} |
| 1140 |
|
| 1141 |
PKCS12* d2i_PKCS12_fp(FILE *fp, PKCS12 **out_p12) { |
| 1142 |
BIO *bio; |
| 1143 |
PKCS12 *ret; |
| 1144 |
|
| 1145 |
bio = BIO_new_fp(fp, 0 /* don't take ownership */); |
| 1146 |
if (!bio) { |
| 1147 |
return NULL; |
| 1148 |
} |
| 1149 |
|
| 1150 |
ret = d2i_PKCS12_bio(bio, out_p12); |
| 1151 |
BIO_free(bio); |
| 1152 |
return ret; |
| 1153 |
} |
| 1154 |
|
| 1155 |
int PKCS12_parse(const PKCS12 *p12, const char *password, EVP_PKEY **out_pkey, |
| 1156 |
X509 **out_cert, STACK_OF(X509) **out_ca_certs) { |
| 1157 |
CBS ber_bytes; |
| 1158 |
STACK_OF(X509) *ca_certs = NULL; |
| 1159 |
char ca_certs_alloced = 0; |
| 1160 |
|
| 1161 |
if (out_ca_certs != NULL && *out_ca_certs != NULL) { |
| 1162 |
ca_certs = *out_ca_certs; |
| 1163 |
} |
| 1164 |
|
| 1165 |
if (!ca_certs) { |
| 1166 |
ca_certs = sk_X509_new_null(); |
| 1167 |
if (ca_certs == NULL) { |
| 1168 |
OPENSSL_PUT_ERROR(PKCS8, ERR_R_MALLOC_FAILURE); |
| 1169 |
return 0; |
| 1170 |
} |
| 1171 |
ca_certs_alloced = 1; |
| 1172 |
} |
| 1173 |
|
| 1174 |
CBS_init(&ber_bytes, p12->ber_bytes, p12->ber_len); |
| 1175 |
if (!PKCS12_get_key_and_certs(out_pkey, ca_certs, &ber_bytes, password)) { |
| 1176 |
if (ca_certs_alloced) { |
| 1177 |
sk_X509_free(ca_certs); |
| 1178 |
} |
| 1179 |
return 0; |
| 1180 |
} |
| 1181 |
|
| 1182 |
*out_cert = NULL; |
| 1183 |
if (sk_X509_num(ca_certs) > 0) { |
| 1184 |
*out_cert = sk_X509_shift(ca_certs); |
| 1185 |
} |
| 1186 |
|
| 1187 |
if (out_ca_certs) { |
| 1188 |
*out_ca_certs = ca_certs; |
| 1189 |
} else { |
| 1190 |
sk_X509_pop_free(ca_certs, X509_free); |
| 1191 |
} |
| 1192 |
|
| 1193 |
return 1; |
| 1194 |
} |
| 1195 |
|
| 1196 |
int PKCS12_verify_mac(const PKCS12 *p12, const char *password, |
| 1197 |
int password_len) { |
| 1198 |
if (password == NULL) { |
| 1199 |
if (password_len != 0) { |
| 1200 |
return 0; |
| 1201 |
} |
| 1202 |
} else if (password_len != -1 && |
| 1203 |
(password[password_len] != 0 || |
| 1204 |
memchr(password, 0, password_len) != NULL)) { |
| 1205 |
return 0; |
| 1206 |
} |
| 1207 |
|
| 1208 |
EVP_PKEY *pkey = NULL; |
| 1209 |
X509 *cert = NULL; |
| 1210 |
if (!PKCS12_parse(p12, password, &pkey, &cert, NULL)) { |
| 1211 |
ERR_clear_error(); |
| 1212 |
return 0; |
| 1213 |
} |
| 1214 |
|
| 1215 |
EVP_PKEY_free(pkey); |
| 1216 |
X509_free(cert); |
| 1217 |
|
| 1218 |
return 1; |
| 1219 |
} |
| 1220 |
|
| 1221 |
void PKCS12_free(PKCS12 *p12) { |
| 1222 |
if (p12 == NULL) { |
| 1223 |
return; |
| 1224 |
} |
| 1225 |
OPENSSL_free(p12->ber_bytes); |
| 1226 |
OPENSSL_free(p12); |
| 1227 |
} |