1 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
4 /* ====================================================================
5 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
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
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/)"
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 * licensing@OpenSSL.org.
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.
33 * 6. Redistributions of any form whatsoever must retain the following
35 * "This product includes software developed by the OpenSSL Project
36 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
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 * ====================================================================
52 * This product includes cryptographic software written by Eric Young
53 * (eay@cryptsoft.com). This product includes software written by Tim
54 * Hudson (tjh@cryptsoft.com). */
56 #include <openssl/pkcs8.h>
58 #include <openssl/asn1.h>
59 #include <openssl/bn.h>
60 #include <openssl/cipher.h>
61 #include <openssl/digest.h>
62 #include <openssl/err.h>
63 #include <openssl/mem.h>
64 #include <openssl/x509.h>
66 #include "../evp/internal.h"
69 #define PKCS12_KEY_ID 1
70 #define PKCS12_IV_ID 2
72 static int ascii_to_ucs2(const char *ascii, size_t ascii_len,
73 uint8_t **out, size_t *out_len) {
77 ulen = ascii_len * 2 + 2;
78 if (ulen < ascii_len) {
81 unitmp = OPENSSL_malloc(ulen);
85 for (i = 0; i < ulen - 2; i += 2) {
87 unitmp[i + 1] = ascii[i >> 1];
90 /* Make result double null terminated */
98 static int pkcs12_key_gen_raw(const uint8_t *pass_raw, size_t pass_raw_len,
99 uint8_t *salt, size_t salt_len,
100 int id, int iterations,
101 size_t out_len, uint8_t *out,
102 const EVP_MD *md_type) {
103 uint8_t *B, *D, *I, *p, *Ai;
104 int Slen, Plen, Ilen, Ijlen;
108 BIGNUM *Ij, *Bpl1; /* These hold Ij and B + 1 */
111 EVP_MD_CTX_init(&ctx);
112 v = EVP_MD_block_size(md_type);
113 u = EVP_MD_size(md_type);
114 D = OPENSSL_malloc(v);
115 Ai = OPENSSL_malloc(u);
116 B = OPENSSL_malloc(v + 1);
117 Slen = v * ((salt_len + v - 1) / v);
119 Plen = v * ((pass_raw_len + v - 1) / v);
123 I = OPENSSL_malloc(Ilen);
126 if (!D || !Ai || !B || !I || !Ij || !Bpl1)
128 for (i = 0; i < v; i++)
131 for (i = 0; i < Slen; i++)
132 *p++ = salt[i % salt_len];
133 for (i = 0; i < Plen; i++)
134 *p++ = pass_raw[i % pass_raw_len];
136 if (!EVP_DigestInit_ex(&ctx, md_type, NULL) ||
137 !EVP_DigestUpdate(&ctx, D, v) ||
138 !EVP_DigestUpdate(&ctx, I, Ilen) ||
139 !EVP_DigestFinal_ex(&ctx, Ai, NULL)) {
142 for (j = 1; j < iterations; j++) {
143 if (!EVP_DigestInit_ex(&ctx, md_type, NULL) ||
144 !EVP_DigestUpdate(&ctx, Ai, u) ||
145 !EVP_DigestFinal_ex(&ctx, Ai, NULL)) {
149 memcpy(out, Ai, out_len < u ? out_len : u);
156 for (j = 0; j < v; j++)
158 /* Work out B + 1 first then can use B as tmp space */
159 if (!BN_bin2bn(B, v, Bpl1))
161 if (!BN_add_word(Bpl1, 1))
163 for (j = 0; j < Ilen; j += v) {
164 if (!BN_bin2bn(I + j, v, Ij))
166 if (!BN_add(Ij, Ij, Bpl1))
168 if (!BN_bn2bin(Ij, B))
170 Ijlen = BN_num_bytes(Ij);
171 /* If more than 2^(v*8) - 1 cut off MSB */
173 if (!BN_bn2bin(Ij, B))
175 memcpy(I + j, B + 1, v);
176 /* If less than v bytes pad with zeroes */
177 } else if (Ijlen < v) {
178 memset(I + j, 0, v - Ijlen);
179 if (!BN_bn2bin(Ij, I + j + v - Ijlen))
181 } else if (!BN_bn2bin(Ij, I + j)) {
188 OPENSSL_PUT_ERROR(PKCS8, pkcs12_key_gen_raw, ERR_R_MALLOC_FAILURE);
197 EVP_MD_CTX_cleanup(&ctx);
202 static int pkcs12_pbe_keyivgen(EVP_CIPHER_CTX *ctx, const uint8_t *pass_raw,
203 size_t pass_raw_len, ASN1_TYPE *param,
204 const EVP_CIPHER *cipher, const EVP_MD *md,
207 int salt_len, iterations, ret;
210 uint8_t key[EVP_MAX_KEY_LENGTH], iv[EVP_MAX_IV_LENGTH];
212 /* Extract useful info from parameter */
213 if (param == NULL || param->type != V_ASN1_SEQUENCE ||
214 param->value.sequence == NULL) {
215 OPENSSL_PUT_ERROR(PKCS8, pkcs12_pbe_keyivgen, PKCS8_R_DECODE_ERROR);
219 pbuf = param->value.sequence->data;
220 pbe = d2i_PBEPARAM(NULL, &pbuf, param->value.sequence->length);
222 OPENSSL_PUT_ERROR(PKCS8, pkcs12_pbe_keyivgen, PKCS8_R_DECODE_ERROR);
229 iterations = ASN1_INTEGER_get(pbe->iter);
231 salt = pbe->salt->data;
232 salt_len = pbe->salt->length;
233 if (!pkcs12_key_gen_raw(pass_raw, pass_raw_len, salt, salt_len, PKCS12_KEY_ID,
234 iterations, EVP_CIPHER_key_length(cipher), key, md)) {
235 OPENSSL_PUT_ERROR(PKCS8, pkcs12_pbe_keyivgen, PKCS8_R_KEY_GEN_ERROR);
239 if (!pkcs12_key_gen_raw(pass_raw, pass_raw_len, salt, salt_len, PKCS12_IV_ID,
240 iterations, EVP_CIPHER_iv_length(cipher), iv, md)) {
241 OPENSSL_PUT_ERROR(PKCS8, pkcs12_pbe_keyivgen, PKCS8_R_KEY_GEN_ERROR);
246 ret = EVP_CipherInit_ex(ctx, cipher, NULL, key, iv, is_encrypt);
247 OPENSSL_cleanse(key, EVP_MAX_KEY_LENGTH);
248 OPENSSL_cleanse(iv, EVP_MAX_IV_LENGTH);
252 typedef int (*keygen_func)(EVP_CIPHER_CTX *ctx, const uint8_t *pass_raw,
253 size_t pass_raw_len, ASN1_TYPE *param,
254 const EVP_CIPHER *cipher, const EVP_MD *md,
264 static const struct pbe_suite kBuiltinPBE[] = {
266 NID_pbe_WithSHA1And128BitRC4, NID_rc4, NID_sha1, pkcs12_pbe_keyivgen,
269 NID_pbe_WithSHA1And3_Key_TripleDES_CBC, NID_des_ede3_cbc, NID_sha1,
274 static int pbe_cipher_init(ASN1_OBJECT *pbe_obj,
275 const uint8_t *pass_raw, size_t pass_raw_len,
277 EVP_CIPHER_CTX *ctx, int is_encrypt) {
278 const EVP_CIPHER *cipher;
282 const struct pbe_suite *suite = NULL;
283 const int pbe_nid = OBJ_obj2nid(pbe_obj);
285 for (i = 0; i < sizeof(kBuiltinPBE) / sizeof(struct pbe_suite); i++) {
286 suite = &kBuiltinPBE[i];
287 if (suite->pbe_nid == pbe_nid) {
294 OPENSSL_PUT_ERROR(PKCS8, pbe_cipher_init, PKCS8_R_UNKNOWN_ALGORITHM);
296 strncpy(obj_str, "NULL", sizeof(obj_str));
298 i2t_ASN1_OBJECT(obj_str, sizeof(obj_str), pbe_obj);
300 ERR_add_error_data(2, "TYPE=", obj_str);
304 if (suite->cipher_nid == -1) {
307 cipher = EVP_get_cipherbynid(suite->cipher_nid);
309 OPENSSL_PUT_ERROR(PKCS8, pbe_cipher_init, PKCS8_R_UNKNOWN_CIPHER);
314 if (suite->md_nid == -1) {
317 md = EVP_get_digestbynid(suite->md_nid);
319 OPENSSL_PUT_ERROR(PKCS8, pbe_cipher_init, PKCS8_R_UNKNOWN_DIGEST);
324 if (!suite->keygen(ctx, pass_raw, pass_raw_len, param, cipher, md,
326 OPENSSL_PUT_ERROR(PKCS8, pbe_cipher_init, PKCS8_R_KEYGEN_FAILURE);
333 static int pbe_crypt(const X509_ALGOR *algor,
334 const uint8_t *pass_raw, size_t pass_raw_len,
335 uint8_t *in, size_t in_len, uint8_t **out, size_t *out_len,
342 EVP_CIPHER_CTX_init(&ctx);
344 if (!pbe_cipher_init(algor->algorithm, pass_raw, pass_raw_len,
345 algor->parameter, &ctx, is_encrypt)) {
346 OPENSSL_PUT_ERROR(PKCS8, pbe_crypt, PKCS8_R_UNKNOWN_CIPHER_ALGORITHM);
349 block_size = EVP_CIPHER_CTX_block_size(&ctx);
351 if (in_len + block_size < in_len) {
352 OPENSSL_PUT_ERROR(PKCS8, pbe_crypt, PKCS8_R_TOO_LONG);
356 buf = OPENSSL_malloc(in_len + block_size);
358 OPENSSL_PUT_ERROR(PKCS8, pbe_crypt, ERR_R_MALLOC_FAILURE);
362 if (!EVP_CipherUpdate(&ctx, buf, &n, in, in_len)) {
364 OPENSSL_PUT_ERROR(PKCS8, pbe_crypt, ERR_R_EVP_LIB);
369 if (!EVP_CipherFinal_ex(&ctx, buf + n, &n)) {
371 OPENSSL_PUT_ERROR(PKCS8, pbe_crypt, ERR_R_EVP_LIB);
379 EVP_CIPHER_CTX_cleanup(&ctx);
383 static void *pkcs12_item_decrypt_d2i(X509_ALGOR *algor, const ASN1_ITEM *it,
384 const uint8_t *pass_raw,
386 ASN1_OCTET_STRING *oct) {
392 if (!pbe_crypt(algor, pass_raw, pass_raw_len, oct->data, oct->length,
393 &out, &out_len, 0 /* decrypt */)) {
394 OPENSSL_PUT_ERROR(PKCS8, pkcs12_item_decrypt_d2i, PKCS8_R_CRYPT_ERROR);
398 ret = ASN1_item_d2i(NULL, &p, out_len, it);
399 OPENSSL_cleanse(out, out_len);
401 OPENSSL_PUT_ERROR(PKCS8, pkcs12_item_decrypt_d2i, PKCS8_R_DECODE_ERROR);
407 PKCS8_PRIV_KEY_INFO *PKCS8_decrypt(X509_SIG *pkcs8, const char *pass,
409 uint8_t *pass_raw = NULL;
410 size_t pass_raw_len = 0;
411 PKCS8_PRIV_KEY_INFO *ret;
414 if (pass_len == -1) {
415 pass_len = strlen(pass);
417 if (!ascii_to_ucs2(pass, pass_len, &pass_raw, &pass_raw_len)) {
418 OPENSSL_PUT_ERROR(PKCS8, pkcs12_key_gen_asc, PKCS8_R_DECODE_ERROR);
423 ret = PKCS8_decrypt_pbe(pkcs8, pass_raw, pass_raw_len);
426 OPENSSL_cleanse(pass_raw, pass_raw_len);
427 OPENSSL_free(pass_raw);
432 PKCS8_PRIV_KEY_INFO *PKCS8_decrypt_pbe(X509_SIG *pkcs8, const uint8_t *pass_raw,
433 size_t pass_raw_len) {
434 return pkcs12_item_decrypt_d2i(pkcs8->algor,
435 ASN1_ITEM_rptr(PKCS8_PRIV_KEY_INFO), pass_raw,
436 pass_raw_len, pkcs8->digest);
439 static ASN1_OCTET_STRING *pkcs12_item_i2d_encrypt(X509_ALGOR *algor,
441 const uint8_t *pass_raw,
442 size_t pass_raw_len, void *obj) {
443 ASN1_OCTET_STRING *oct;
448 oct = M_ASN1_OCTET_STRING_new();
450 OPENSSL_PUT_ERROR(PKCS8, pkcs12_item_i2d_encrypt, ERR_R_MALLOC_FAILURE);
453 in_len = ASN1_item_i2d(obj, &in, it);
455 OPENSSL_PUT_ERROR(PKCS8, pkcs12_item_i2d_encrypt, PKCS8_R_ENCODE_ERROR);
458 if (!pbe_crypt(algor, pass_raw, pass_raw_len, in, in_len, &oct->data, &crypt_len,
460 OPENSSL_PUT_ERROR(PKCS8, pkcs12_item_i2d_encrypt, PKCS8_R_ENCRYPT_ERROR);
464 oct->length = crypt_len;
465 OPENSSL_cleanse(in, in_len);
470 X509_SIG *PKCS8_encrypt(int pbe_nid, const EVP_CIPHER *cipher, const char *pass,
471 int pass_len, uint8_t *salt, size_t salt_len,
472 int iterations, PKCS8_PRIV_KEY_INFO *p8inf) {
473 uint8_t *pass_raw = NULL;
474 size_t pass_raw_len = 0;
478 if (pass_len == -1) {
479 pass_len = strlen(pass);
481 if (!ascii_to_ucs2(pass, pass_len, &pass_raw, &pass_raw_len)) {
482 OPENSSL_PUT_ERROR(PKCS8, pkcs12_key_gen_asc, PKCS8_R_DECODE_ERROR);
487 ret = PKCS8_encrypt_pbe(pbe_nid, pass_raw, pass_raw_len,
488 salt, salt_len, iterations, p8inf);
491 OPENSSL_cleanse(pass_raw, pass_raw_len);
492 OPENSSL_free(pass_raw);
497 X509_SIG *PKCS8_encrypt_pbe(int pbe_nid,
498 const uint8_t *pass_raw, size_t pass_raw_len,
499 uint8_t *salt, size_t salt_len,
500 int iterations, PKCS8_PRIV_KEY_INFO *p8inf) {
501 X509_SIG *pkcs8 = NULL;
504 pkcs8 = X509_SIG_new();
506 OPENSSL_PUT_ERROR(PKCS8, PKCS8_encrypt_pbe, ERR_R_MALLOC_FAILURE);
510 pbe = PKCS5_pbe_set(pbe_nid, iterations, salt, salt_len);
512 OPENSSL_PUT_ERROR(PKCS8, PKCS8_encrypt_pbe, ERR_R_ASN1_LIB);
516 X509_ALGOR_free(pkcs8->algor);
518 M_ASN1_OCTET_STRING_free(pkcs8->digest);
519 pkcs8->digest = pkcs12_item_i2d_encrypt(
520 pbe, ASN1_ITEM_rptr(PKCS8_PRIV_KEY_INFO), pass_raw, pass_raw_len, p8inf);
521 if (!pkcs8->digest) {
522 OPENSSL_PUT_ERROR(PKCS8, PKCS8_encrypt_pbe, PKCS8_R_ENCRYPT_ERROR);
529 X509_SIG_free(pkcs8);
533 EVP_PKEY *EVP_PKCS82PKEY(PKCS8_PRIV_KEY_INFO *p8) {
534 EVP_PKEY *pkey = NULL;
538 if (!PKCS8_pkey_get0(&algoid, NULL, NULL, NULL, p8))
541 pkey = EVP_PKEY_new();
543 OPENSSL_PUT_ERROR(PKCS8, EVP_PKCS82PKEY, ERR_R_MALLOC_FAILURE);
547 if (!EVP_PKEY_set_type(pkey, OBJ_obj2nid(algoid))) {
548 OPENSSL_PUT_ERROR(PKCS8, EVP_PKCS82PKEY,
549 PKCS8_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM);
550 i2t_ASN1_OBJECT(obj_tmp, 80, algoid);
551 ERR_add_error_data(2, "TYPE=", obj_tmp);
555 if (pkey->ameth->priv_decode) {
556 if (!pkey->ameth->priv_decode(pkey, p8)) {
557 OPENSSL_PUT_ERROR(PKCS8, EVP_PKCS82PKEY, PKCS8_R_PRIVATE_KEY_DECODE_ERROR);
561 OPENSSL_PUT_ERROR(PKCS8, EVP_PKCS82PKEY, PKCS8_R_METHOD_NOT_SUPPORTED);
572 PKCS8_PRIV_KEY_INFO *EVP_PKEY2PKCS8(EVP_PKEY *pkey) {
573 PKCS8_PRIV_KEY_INFO *p8;
575 p8 = PKCS8_PRIV_KEY_INFO_new();
577 OPENSSL_PUT_ERROR(PKCS8, EVP_PKEY2PKCS8, ERR_R_MALLOC_FAILURE);
580 p8->broken = PKCS8_OK;
583 if (pkey->ameth->priv_encode) {
584 if (!pkey->ameth->priv_encode(p8, pkey)) {
585 OPENSSL_PUT_ERROR(PKCS8, EVP_PKEY2PKCS8,
586 PKCS8_R_PRIVATE_KEY_ENCODE_ERROR);
590 OPENSSL_PUT_ERROR(PKCS8, EVP_PKEY2PKCS8, PKCS8_R_METHOD_NOT_SUPPORTED);
594 OPENSSL_PUT_ERROR(PKCS8, EVP_PKEY2PKCS8,
595 PKCS8_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM);
601 PKCS8_PRIV_KEY_INFO_free(p8);