2015-01-22 03:40:55 +00:00
|
|
|
/*
|
2016-05-17 18:52:22 +00:00
|
|
|
* Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
|
1999-03-28 23:17:34 +00:00
|
|
|
*
|
2018-12-06 12:49:51 +00:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-17 18:52:22 +00:00
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
1999-03-28 23:17:34 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2015-05-14 14:56:48 +00:00
|
|
|
#include "internal/cryptlib.h"
|
1999-04-23 22:13:45 +00:00
|
|
|
#include <openssl/pkcs12.h>
|
2004-05-17 19:14:22 +00:00
|
|
|
#include <openssl/bn.h>
|
2018-12-13 07:07:25 +00:00
|
|
|
#include <openssl/trace.h>
|
1999-03-28 23:17:34 +00:00
|
|
|
|
|
|
|
/* PKCS12 compatible key/IV generation */
|
|
|
|
#ifndef min
|
2015-01-22 03:40:55 +00:00
|
|
|
# define min(a,b) ((a) < (b) ? (a) : (b))
|
1999-03-28 23:17:34 +00:00
|
|
|
#endif
|
|
|
|
|
2000-05-04 00:08:35 +00:00
|
|
|
int PKCS12_key_gen_asc(const char *pass, int passlen, unsigned char *salt,
|
2015-01-22 03:40:55 +00:00
|
|
|
int saltlen, int id, int iter, int n,
|
|
|
|
unsigned char *out, const EVP_MD *md_type)
|
1999-03-28 23:17:34 +00:00
|
|
|
{
|
2015-01-22 03:40:55 +00:00
|
|
|
int ret;
|
|
|
|
unsigned char *unipass;
|
|
|
|
int uniplen;
|
2008-12-29 16:11:58 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (!pass) {
|
|
|
|
unipass = NULL;
|
|
|
|
uniplen = 0;
|
|
|
|
} else if (!OPENSSL_asc2uni(pass, passlen, &unipass, &uniplen)) {
|
|
|
|
PKCS12err(PKCS12_F_PKCS12_KEY_GEN_ASC, ERR_R_MALLOC_FAILURE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
ret = PKCS12_key_gen_uni(unipass, uniplen, salt, saltlen,
|
|
|
|
id, iter, n, out, md_type);
|
|
|
|
if (ret <= 0)
|
|
|
|
return 0;
|
2015-04-30 21:57:32 +00:00
|
|
|
OPENSSL_clear_free(unipass, uniplen);
|
2015-01-22 03:40:55 +00:00
|
|
|
return ret;
|
1999-03-28 23:17:34 +00:00
|
|
|
}
|
|
|
|
|
2015-11-24 22:34:51 +00:00
|
|
|
int PKCS12_key_gen_utf8(const char *pass, int passlen, unsigned char *salt,
|
|
|
|
int saltlen, int id, int iter, int n,
|
|
|
|
unsigned char *out, const EVP_MD *md_type)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
unsigned char *unipass;
|
|
|
|
int uniplen;
|
|
|
|
|
|
|
|
if (!pass) {
|
|
|
|
unipass = NULL;
|
|
|
|
uniplen = 0;
|
|
|
|
} else if (!OPENSSL_utf82uni(pass, passlen, &unipass, &uniplen)) {
|
|
|
|
PKCS12err(PKCS12_F_PKCS12_KEY_GEN_UTF8, ERR_R_MALLOC_FAILURE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
ret = PKCS12_key_gen_uni(unipass, uniplen, salt, saltlen,
|
|
|
|
id, iter, n, out, md_type);
|
|
|
|
if (ret <= 0)
|
|
|
|
return 0;
|
|
|
|
OPENSSL_clear_free(unipass, uniplen);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2000-05-04 00:08:35 +00:00
|
|
|
int PKCS12_key_gen_uni(unsigned char *pass, int passlen, unsigned char *salt,
|
2015-01-22 03:40:55 +00:00
|
|
|
int saltlen, int id, int iter, int n,
|
|
|
|
unsigned char *out, const EVP_MD *md_type)
|
1999-03-28 23:17:34 +00:00
|
|
|
{
|
2015-12-07 19:37:08 +00:00
|
|
|
unsigned char *B = NULL, *D = NULL, *I = NULL, *p = NULL, *Ai = NULL;
|
2016-10-01 20:19:41 +00:00
|
|
|
int Slen, Plen, Ilen;
|
2015-01-22 03:40:55 +00:00
|
|
|
int i, j, u, v;
|
|
|
|
int ret = 0;
|
2015-12-07 19:37:08 +00:00
|
|
|
EVP_MD_CTX *ctx = NULL;
|
2015-01-22 03:40:55 +00:00
|
|
|
unsigned char *tmpout = out;
|
|
|
|
int tmpn = n;
|
2000-03-07 22:35:27 +00:00
|
|
|
|
2015-12-01 23:49:35 +00:00
|
|
|
ctx = EVP_MD_CTX_new();
|
2015-11-27 13:02:12 +00:00
|
|
|
if (ctx == NULL)
|
|
|
|
goto err;
|
|
|
|
|
2018-12-13 07:07:25 +00:00
|
|
|
OSSL_TRACE_BEGIN(PKCS12_KEYGEN) {
|
|
|
|
BIO_printf(trc_out, "PKCS12_key_gen_uni(): ID %d, ITER %d\n", id, iter);
|
|
|
|
BIO_printf(trc_out, "Password (length %d):\n", passlen);
|
|
|
|
BIO_hex_string(trc_out, 0, passlen, pass, passlen);
|
|
|
|
BIO_printf(trc_out, "\n");
|
|
|
|
BIO_printf(trc_out, "Salt (length %d):\n", saltlen);
|
|
|
|
BIO_hex_string(trc_out, 0, saltlen, salt, saltlen);
|
|
|
|
BIO_printf(trc_out, "\n");
|
|
|
|
} OSSL_TRACE_END(PKCS12_KEYGEN);
|
2015-01-22 03:40:55 +00:00
|
|
|
v = EVP_MD_block_size(md_type);
|
|
|
|
u = EVP_MD_size(md_type);
|
2016-05-04 12:13:24 +00:00
|
|
|
if (u < 0 || v <= 0)
|
2016-04-27 13:06:40 +00:00
|
|
|
goto err;
|
2015-01-22 03:40:55 +00:00
|
|
|
D = OPENSSL_malloc(v);
|
|
|
|
Ai = OPENSSL_malloc(u);
|
|
|
|
B = OPENSSL_malloc(v + 1);
|
|
|
|
Slen = v * ((saltlen + v - 1) / v);
|
|
|
|
if (passlen)
|
|
|
|
Plen = v * ((passlen + v - 1) / v);
|
|
|
|
else
|
|
|
|
Plen = 0;
|
|
|
|
Ilen = Slen + Plen;
|
|
|
|
I = OPENSSL_malloc(Ilen);
|
2016-10-01 20:19:41 +00:00
|
|
|
if (D == NULL || Ai == NULL || B == NULL || I == NULL)
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
|
|
|
for (i = 0; i < v; i++)
|
|
|
|
D[i] = id;
|
|
|
|
p = I;
|
|
|
|
for (i = 0; i < Slen; i++)
|
|
|
|
*p++ = salt[i % saltlen];
|
|
|
|
for (i = 0; i < Plen; i++)
|
|
|
|
*p++ = pass[i % passlen];
|
|
|
|
for (;;) {
|
2015-11-27 13:02:12 +00:00
|
|
|
if (!EVP_DigestInit_ex(ctx, md_type, NULL)
|
|
|
|
|| !EVP_DigestUpdate(ctx, D, v)
|
|
|
|
|| !EVP_DigestUpdate(ctx, I, Ilen)
|
|
|
|
|| !EVP_DigestFinal_ex(ctx, Ai, NULL))
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
|
|
|
for (j = 1; j < iter; j++) {
|
2015-11-27 13:02:12 +00:00
|
|
|
if (!EVP_DigestInit_ex(ctx, md_type, NULL)
|
|
|
|
|| !EVP_DigestUpdate(ctx, Ai, u)
|
|
|
|
|| !EVP_DigestFinal_ex(ctx, Ai, NULL))
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
memcpy(out, Ai, min(n, u));
|
|
|
|
if (u >= n) {
|
2018-12-13 07:07:25 +00:00
|
|
|
OSSL_TRACE_BEGIN(PKCS12_KEYGEN) {
|
|
|
|
BIO_printf(trc_out, "Output KEY (length %d)\n", tmpn);
|
|
|
|
BIO_hex_string(trc_out, 0, tmpn, tmpout, tmpn);
|
|
|
|
BIO_printf(trc_out, "\n");
|
|
|
|
} OSSL_TRACE_END(PKCS12_KEYGEN);
|
2015-01-22 03:40:55 +00:00
|
|
|
ret = 1;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
n -= u;
|
|
|
|
out += u;
|
|
|
|
for (j = 0; j < v; j++)
|
|
|
|
B[j] = Ai[j % u];
|
|
|
|
for (j = 0; j < Ilen; j += v) {
|
2016-10-01 20:19:41 +00:00
|
|
|
int k;
|
|
|
|
unsigned char *Ij = I + j;
|
|
|
|
uint16_t c = 1;
|
|
|
|
|
|
|
|
/* Work out Ij = Ij + B + 1 */
|
|
|
|
for (k = v - 1; k >= 0; k--) {
|
|
|
|
c += Ij[k] + B[k];
|
|
|
|
Ij[k] = (unsigned char)c;
|
|
|
|
c >>= 8;
|
|
|
|
}
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
}
|
2012-06-03 22:00:21 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
err:
|
|
|
|
PKCS12err(PKCS12_F_PKCS12_KEY_GEN_UNI, ERR_R_MALLOC_FAILURE);
|
2010-10-11 23:49:22 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
end:
|
|
|
|
OPENSSL_free(Ai);
|
|
|
|
OPENSSL_free(B);
|
|
|
|
OPENSSL_free(D);
|
|
|
|
OPENSSL_free(I);
|
2015-12-01 23:49:35 +00:00
|
|
|
EVP_MD_CTX_free(ctx);
|
2015-01-22 03:40:55 +00:00
|
|
|
return ret;
|
1999-03-28 23:17:34 +00:00
|
|
|
}
|