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
|
|
|
*
|
2016-05-17 18:52:22 +00:00
|
|
|
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
* 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>
|
1999-03-28 23:17:34 +00:00
|
|
|
|
|
|
|
/* Uncomment out this line to get debugging info about key generation */
|
2015-01-22 03:40:55 +00:00
|
|
|
/*
|
Remove outdated DEBUG flags.
Add -DBIO_DEBUG to --strict-warnings.
Remove comments about outdated debugging ifdef guards.
Remove md_rand ifdef guarding an assert; it doesn't seem used.
Remove the conf guards in conf_api since we use OPENSSL_assert, not assert.
For pkcs12 stuff put OPENSSL_ in front of the macro name.
Merge TLS_DEBUG into SSL_DEBUG.
Various things just turned on/off asserts, mainly for checking non-NULL
arguments, which is now removed: camellia, bn_ctx, crypto/modes.
Remove some old debug code, that basically just printed things to stderr:
DEBUG_PRINT_UNKNOWN_CIPHERSUITES, DEBUG_ZLIB, OPENSSL_RI_DEBUG,
RL_DEBUG, RSA_DEBUG, SCRYPT_DEBUG.
Remove OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL.
Reviewed-by: Richard Levitte <levitte@openssl.org>
2016-02-18 16:33:21 +00:00
|
|
|
* #define OPENSSL_DEBUG_KEYGEN
|
2015-01-22 03:40:55 +00:00
|
|
|
*/
|
Remove outdated DEBUG flags.
Add -DBIO_DEBUG to --strict-warnings.
Remove comments about outdated debugging ifdef guards.
Remove md_rand ifdef guarding an assert; it doesn't seem used.
Remove the conf guards in conf_api since we use OPENSSL_assert, not assert.
For pkcs12 stuff put OPENSSL_ in front of the macro name.
Merge TLS_DEBUG into SSL_DEBUG.
Various things just turned on/off asserts, mainly for checking non-NULL
arguments, which is now removed: camellia, bn_ctx, crypto/modes.
Remove some old debug code, that basically just printed things to stderr:
DEBUG_PRINT_UNKNOWN_CIPHERSUITES, DEBUG_ZLIB, OPENSSL_RI_DEBUG,
RL_DEBUG, RSA_DEBUG, SCRYPT_DEBUG.
Remove OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL.
Reviewed-by: Richard Levitte <levitte@openssl.org>
2016-02-18 16:33:21 +00:00
|
|
|
#ifdef OPENSSL_DEBUG_KEYGEN
|
2015-01-22 03:40:55 +00:00
|
|
|
# include <openssl/bio.h>
|
1999-03-28 23:17:34 +00:00
|
|
|
extern BIO *bio_err;
|
2015-01-22 03:40:55 +00:00
|
|
|
void h__dump(unsigned char *p, int len);
|
1999-03-28 23:17:34 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* 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;
|
Remove outdated DEBUG flags.
Add -DBIO_DEBUG to --strict-warnings.
Remove comments about outdated debugging ifdef guards.
Remove md_rand ifdef guarding an assert; it doesn't seem used.
Remove the conf guards in conf_api since we use OPENSSL_assert, not assert.
For pkcs12 stuff put OPENSSL_ in front of the macro name.
Merge TLS_DEBUG into SSL_DEBUG.
Various things just turned on/off asserts, mainly for checking non-NULL
arguments, which is now removed: camellia, bn_ctx, crypto/modes.
Remove some old debug code, that basically just printed things to stderr:
DEBUG_PRINT_UNKNOWN_CIPHERSUITES, DEBUG_ZLIB, OPENSSL_RI_DEBUG,
RL_DEBUG, RSA_DEBUG, SCRYPT_DEBUG.
Remove OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL.
Reviewed-by: Richard Levitte <levitte@openssl.org>
2016-02-18 16:33:21 +00:00
|
|
|
#ifdef OPENSSL_DEBUG_KEYGEN
|
2015-01-22 03:40:55 +00:00
|
|
|
unsigned char *tmpout = out;
|
|
|
|
int tmpn = n;
|
2000-03-07 22:35:27 +00:00
|
|
|
#endif
|
|
|
|
|
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;
|
|
|
|
|
Remove outdated DEBUG flags.
Add -DBIO_DEBUG to --strict-warnings.
Remove comments about outdated debugging ifdef guards.
Remove md_rand ifdef guarding an assert; it doesn't seem used.
Remove the conf guards in conf_api since we use OPENSSL_assert, not assert.
For pkcs12 stuff put OPENSSL_ in front of the macro name.
Merge TLS_DEBUG into SSL_DEBUG.
Various things just turned on/off asserts, mainly for checking non-NULL
arguments, which is now removed: camellia, bn_ctx, crypto/modes.
Remove some old debug code, that basically just printed things to stderr:
DEBUG_PRINT_UNKNOWN_CIPHERSUITES, DEBUG_ZLIB, OPENSSL_RI_DEBUG,
RL_DEBUG, RSA_DEBUG, SCRYPT_DEBUG.
Remove OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL.
Reviewed-by: Richard Levitte <levitte@openssl.org>
2016-02-18 16:33:21 +00:00
|
|
|
#ifdef OPENSSL_DEBUG_KEYGEN
|
2015-01-22 03:40:55 +00:00
|
|
|
fprintf(stderr, "KEYGEN DEBUG\n");
|
|
|
|
fprintf(stderr, "ID %d, ITER %d\n", id, iter);
|
|
|
|
fprintf(stderr, "Password (length %d):\n", passlen);
|
|
|
|
h__dump(pass, passlen);
|
|
|
|
fprintf(stderr, "Salt (length %d):\n", saltlen);
|
|
|
|
h__dump(salt, saltlen);
|
1999-03-28 23:17:34 +00:00
|
|
|
#endif
|
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) {
|
Remove outdated DEBUG flags.
Add -DBIO_DEBUG to --strict-warnings.
Remove comments about outdated debugging ifdef guards.
Remove md_rand ifdef guarding an assert; it doesn't seem used.
Remove the conf guards in conf_api since we use OPENSSL_assert, not assert.
For pkcs12 stuff put OPENSSL_ in front of the macro name.
Merge TLS_DEBUG into SSL_DEBUG.
Various things just turned on/off asserts, mainly for checking non-NULL
arguments, which is now removed: camellia, bn_ctx, crypto/modes.
Remove some old debug code, that basically just printed things to stderr:
DEBUG_PRINT_UNKNOWN_CIPHERSUITES, DEBUG_ZLIB, OPENSSL_RI_DEBUG,
RL_DEBUG, RSA_DEBUG, SCRYPT_DEBUG.
Remove OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL.
Reviewed-by: Richard Levitte <levitte@openssl.org>
2016-02-18 16:33:21 +00:00
|
|
|
#ifdef OPENSSL_DEBUG_KEYGEN
|
2015-01-22 03:40:55 +00:00
|
|
|
fprintf(stderr, "Output KEY (length %d)\n", tmpn);
|
|
|
|
h__dump(tmpout, tmpn);
|
1999-03-28 23:17:34 +00:00
|
|
|
#endif
|
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
|
|
|
}
|
2015-01-22 03:40:55 +00:00
|
|
|
|
Remove outdated DEBUG flags.
Add -DBIO_DEBUG to --strict-warnings.
Remove comments about outdated debugging ifdef guards.
Remove md_rand ifdef guarding an assert; it doesn't seem used.
Remove the conf guards in conf_api since we use OPENSSL_assert, not assert.
For pkcs12 stuff put OPENSSL_ in front of the macro name.
Merge TLS_DEBUG into SSL_DEBUG.
Various things just turned on/off asserts, mainly for checking non-NULL
arguments, which is now removed: camellia, bn_ctx, crypto/modes.
Remove some old debug code, that basically just printed things to stderr:
DEBUG_PRINT_UNKNOWN_CIPHERSUITES, DEBUG_ZLIB, OPENSSL_RI_DEBUG,
RL_DEBUG, RSA_DEBUG, SCRYPT_DEBUG.
Remove OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL.
Reviewed-by: Richard Levitte <levitte@openssl.org>
2016-02-18 16:33:21 +00:00
|
|
|
#ifdef OPENSSL_DEBUG_KEYGEN
|
2015-01-22 03:40:55 +00:00
|
|
|
void h__dump(unsigned char *p, int len)
|
1999-03-28 23:17:34 +00:00
|
|
|
{
|
2015-01-22 03:40:55 +00:00
|
|
|
for (; len--; p++)
|
|
|
|
fprintf(stderr, "%02X", *p);
|
|
|
|
fprintf(stderr, "\n");
|
1999-03-28 23:17:34 +00:00
|
|
|
}
|
|
|
|
#endif
|