2015-01-22 03:40:55 +00:00
|
|
|
/*
|
2016-05-17 18:24:46 +00:00
|
|
|
* Copyright 2005-2016 The OpenSSL Project Authors. All Rights Reserved.
|
2005-08-31 16:37:54 +00:00
|
|
|
*
|
2016-05-17 18:24:46 +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
|
2005-08-31 16:37:54 +00:00
|
|
|
*/
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/*
|
|
|
|
* Support for PVK format keys and related structures (such a PUBLICKEYBLOB
|
2005-08-31 16:37:54 +00:00
|
|
|
* and PRIVATEKEYBLOB).
|
|
|
|
*/
|
|
|
|
|
2015-05-14 14:56:48 +00:00
|
|
|
#include "internal/cryptlib.h"
|
2005-08-31 16:37:54 +00:00
|
|
|
#include <openssl/pem.h>
|
|
|
|
#include <openssl/rand.h>
|
2008-03-16 21:05:46 +00:00
|
|
|
#include <openssl/bn.h>
|
2009-04-26 22:18:22 +00:00
|
|
|
#if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DSA)
|
2015-01-22 03:40:55 +00:00
|
|
|
# include <openssl/dsa.h>
|
|
|
|
# include <openssl/rsa.h>
|
2005-08-31 16:37:54 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/*
|
|
|
|
* Utility function: read a DWORD (4 byte unsigned integer) in little endian
|
2005-08-31 16:37:54 +00:00
|
|
|
* format
|
|
|
|
*/
|
|
|
|
|
|
|
|
static unsigned int read_ledword(const unsigned char **in)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
const unsigned char *p = *in;
|
|
|
|
unsigned int ret;
|
|
|
|
ret = *p++;
|
|
|
|
ret |= (*p++ << 8);
|
|
|
|
ret |= (*p++ << 16);
|
|
|
|
ret |= (*p++ << 24);
|
|
|
|
*in = p;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read a BIGNUM in little endian format. The docs say that this should take
|
|
|
|
* up bitlen/8 bytes.
|
2005-08-31 16:37:54 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
static int read_lebn(const unsigned char **in, unsigned int nbyte, BIGNUM **r)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2016-02-01 00:02:05 +00:00
|
|
|
*r = BN_lebin2bn(*in, nbyte, NULL);
|
|
|
|
if (*r == NULL)
|
2015-01-22 03:40:55 +00:00
|
|
|
return 0;
|
2016-02-01 00:02:05 +00:00
|
|
|
*in += nbyte;
|
|
|
|
return 1;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2005-08-31 16:37:54 +00:00
|
|
|
|
|
|
|
/* Convert private key blob to EVP_PKEY: RSA and DSA keys supported */
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
# define MS_PUBLICKEYBLOB 0x6
|
|
|
|
# define MS_PRIVATEKEYBLOB 0x7
|
|
|
|
# define MS_RSA1MAGIC 0x31415352L
|
|
|
|
# define MS_RSA2MAGIC 0x32415352L
|
|
|
|
# define MS_DSS1MAGIC 0x31535344L
|
|
|
|
# define MS_DSS2MAGIC 0x32535344L
|
2005-08-31 16:37:54 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
# define MS_KEYALG_RSA_KEYX 0xa400
|
|
|
|
# define MS_KEYALG_DSS_SIGN 0x2200
|
2005-08-31 16:37:54 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
# define MS_KEYTYPE_KEYX 0x1
|
|
|
|
# define MS_KEYTYPE_SIGN 0x2
|
2005-08-31 16:37:54 +00:00
|
|
|
|
2016-08-15 15:52:21 +00:00
|
|
|
/* Maximum length of a blob after header */
|
|
|
|
# define BLOB_MAX_LENGTH 102400
|
|
|
|
|
2005-08-31 16:37:54 +00:00
|
|
|
/* The PVK file magic number: seems to spell out "bobsfile", who is Bob? */
|
2015-01-22 03:40:55 +00:00
|
|
|
# define MS_PVKMAGIC 0xb0b5f11eL
|
2005-08-31 16:37:54 +00:00
|
|
|
/* Salt length for PVK files */
|
2015-01-22 03:40:55 +00:00
|
|
|
# define PVK_SALTLEN 0x10
|
2016-03-03 23:37:36 +00:00
|
|
|
/* Maximum length in PVK header */
|
|
|
|
# define PVK_MAX_KEYLEN 102400
|
|
|
|
/* Maximum salt length */
|
|
|
|
# define PVK_MAX_SALTLEN 10240
|
2005-08-31 16:37:54 +00:00
|
|
|
|
2016-02-14 03:33:56 +00:00
|
|
|
static EVP_PKEY *b2i_rsa(const unsigned char **in,
|
2015-01-22 03:40:55 +00:00
|
|
|
unsigned int bitlen, int ispub);
|
2016-02-14 03:33:56 +00:00
|
|
|
static EVP_PKEY *b2i_dss(const unsigned char **in,
|
2015-01-22 03:40:55 +00:00
|
|
|
unsigned int bitlen, int ispub);
|
2005-08-31 16:37:54 +00:00
|
|
|
|
|
|
|
static int do_blob_header(const unsigned char **in, unsigned int length,
|
2015-01-22 03:40:55 +00:00
|
|
|
unsigned int *pmagic, unsigned int *pbitlen,
|
|
|
|
int *pisdss, int *pispub)
|
|
|
|
{
|
|
|
|
const unsigned char *p = *in;
|
|
|
|
if (length < 16)
|
|
|
|
return 0;
|
|
|
|
/* bType */
|
|
|
|
if (*p == MS_PUBLICKEYBLOB) {
|
|
|
|
if (*pispub == 0) {
|
|
|
|
PEMerr(PEM_F_DO_BLOB_HEADER, PEM_R_EXPECTING_PRIVATE_KEY_BLOB);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*pispub = 1;
|
|
|
|
} else if (*p == MS_PRIVATEKEYBLOB) {
|
|
|
|
if (*pispub == 1) {
|
|
|
|
PEMerr(PEM_F_DO_BLOB_HEADER, PEM_R_EXPECTING_PUBLIC_KEY_BLOB);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*pispub = 0;
|
|
|
|
} else
|
|
|
|
return 0;
|
|
|
|
p++;
|
|
|
|
/* Version */
|
|
|
|
if (*p++ != 0x2) {
|
|
|
|
PEMerr(PEM_F_DO_BLOB_HEADER, PEM_R_BAD_VERSION_NUMBER);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* Ignore reserved, aiKeyAlg */
|
|
|
|
p += 6;
|
|
|
|
*pmagic = read_ledword(&p);
|
|
|
|
*pbitlen = read_ledword(&p);
|
|
|
|
*pisdss = 0;
|
|
|
|
switch (*pmagic) {
|
|
|
|
|
|
|
|
case MS_DSS1MAGIC:
|
|
|
|
*pisdss = 1;
|
2017-05-11 14:21:37 +00:00
|
|
|
/* fall thru */
|
2015-01-22 03:40:55 +00:00
|
|
|
case MS_RSA1MAGIC:
|
|
|
|
if (*pispub == 0) {
|
|
|
|
PEMerr(PEM_F_DO_BLOB_HEADER, PEM_R_EXPECTING_PRIVATE_KEY_BLOB);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MS_DSS2MAGIC:
|
|
|
|
*pisdss = 1;
|
2017-05-11 14:21:37 +00:00
|
|
|
/* fall thru */
|
2015-01-22 03:40:55 +00:00
|
|
|
case MS_RSA2MAGIC:
|
|
|
|
if (*pispub == 1) {
|
|
|
|
PEMerr(PEM_F_DO_BLOB_HEADER, PEM_R_EXPECTING_PUBLIC_KEY_BLOB);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
PEMerr(PEM_F_DO_BLOB_HEADER, PEM_R_BAD_MAGIC_NUMBER);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*in = p;
|
|
|
|
return 1;
|
|
|
|
}
|
2005-08-31 16:37:54 +00:00
|
|
|
|
|
|
|
static unsigned int blob_length(unsigned bitlen, int isdss, int ispub)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
unsigned int nbyte, hnbyte;
|
|
|
|
nbyte = (bitlen + 7) >> 3;
|
|
|
|
hnbyte = (bitlen + 15) >> 4;
|
|
|
|
if (isdss) {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Expected length: 20 for q + 3 components bitlen each + 24 for seed
|
|
|
|
* structure.
|
|
|
|
*/
|
|
|
|
if (ispub)
|
|
|
|
return 44 + 3 * nbyte;
|
|
|
|
/*
|
|
|
|
* Expected length: 20 for q, priv, 2 bitlen components + 24 for seed
|
|
|
|
* structure.
|
|
|
|
*/
|
|
|
|
else
|
|
|
|
return 64 + 2 * nbyte;
|
|
|
|
} else {
|
|
|
|
/* Expected length: 4 for 'e' + 'n' */
|
|
|
|
if (ispub)
|
|
|
|
return 4 + nbyte;
|
|
|
|
else
|
|
|
|
/*
|
|
|
|
* Expected length: 4 for 'e' and 7 other components. 2
|
|
|
|
* components are bitlen size, 5 are bitlen/2
|
|
|
|
*/
|
|
|
|
return 4 + 2 * nbyte + 5 * hnbyte;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2005-08-31 16:37:54 +00:00
|
|
|
|
|
|
|
static EVP_PKEY *do_b2i(const unsigned char **in, unsigned int length,
|
2015-01-22 03:40:55 +00:00
|
|
|
int ispub)
|
|
|
|
{
|
|
|
|
const unsigned char *p = *in;
|
|
|
|
unsigned int bitlen, magic;
|
|
|
|
int isdss;
|
|
|
|
if (do_blob_header(&p, length, &magic, &bitlen, &isdss, &ispub) <= 0) {
|
|
|
|
PEMerr(PEM_F_DO_B2I, PEM_R_KEYBLOB_HEADER_PARSE_ERROR);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
length -= 16;
|
|
|
|
if (length < blob_length(bitlen, isdss, ispub)) {
|
|
|
|
PEMerr(PEM_F_DO_B2I, PEM_R_KEYBLOB_TOO_SHORT);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (isdss)
|
2016-02-14 03:33:56 +00:00
|
|
|
return b2i_dss(&p, bitlen, ispub);
|
2015-01-22 03:40:55 +00:00
|
|
|
else
|
2016-02-14 03:33:56 +00:00
|
|
|
return b2i_rsa(&p, bitlen, ispub);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2005-08-31 16:37:54 +00:00
|
|
|
|
|
|
|
static EVP_PKEY *do_b2i_bio(BIO *in, int ispub)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
const unsigned char *p;
|
|
|
|
unsigned char hdr_buf[16], *buf = NULL;
|
|
|
|
unsigned int bitlen, magic, length;
|
|
|
|
int isdss;
|
|
|
|
EVP_PKEY *ret = NULL;
|
|
|
|
if (BIO_read(in, hdr_buf, 16) != 16) {
|
|
|
|
PEMerr(PEM_F_DO_B2I_BIO, PEM_R_KEYBLOB_TOO_SHORT);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
p = hdr_buf;
|
|
|
|
if (do_blob_header(&p, 16, &magic, &bitlen, &isdss, &ispub) <= 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
length = blob_length(bitlen, isdss, ispub);
|
2016-08-15 15:52:21 +00:00
|
|
|
if (length > BLOB_MAX_LENGTH) {
|
|
|
|
PEMerr(PEM_F_DO_B2I_BIO, PEM_R_HEADER_TOO_LONG);
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-01-22 03:40:55 +00:00
|
|
|
buf = OPENSSL_malloc(length);
|
2015-10-30 11:12:26 +00:00
|
|
|
if (buf == NULL) {
|
2015-01-22 03:40:55 +00:00
|
|
|
PEMerr(PEM_F_DO_B2I_BIO, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
p = buf;
|
|
|
|
if (BIO_read(in, buf, length) != (int)length) {
|
|
|
|
PEMerr(PEM_F_DO_B2I_BIO, PEM_R_KEYBLOB_TOO_SHORT);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isdss)
|
2016-02-14 03:33:56 +00:00
|
|
|
ret = b2i_dss(&p, bitlen, ispub);
|
2015-01-22 03:40:55 +00:00
|
|
|
else
|
2016-02-14 03:33:56 +00:00
|
|
|
ret = b2i_rsa(&p, bitlen, ispub);
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
err:
|
2015-05-01 14:02:07 +00:00
|
|
|
OPENSSL_free(buf);
|
2015-01-22 03:40:55 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2005-08-31 16:37:54 +00:00
|
|
|
|
2016-02-14 03:33:56 +00:00
|
|
|
static EVP_PKEY *b2i_dss(const unsigned char **in,
|
2015-01-22 03:40:55 +00:00
|
|
|
unsigned int bitlen, int ispub)
|
|
|
|
{
|
|
|
|
const unsigned char *p = *in;
|
|
|
|
EVP_PKEY *ret = NULL;
|
|
|
|
DSA *dsa = NULL;
|
|
|
|
BN_CTX *ctx = NULL;
|
|
|
|
unsigned int nbyte;
|
2016-03-30 14:21:39 +00:00
|
|
|
BIGNUM *pbn = NULL, *qbn = NULL, *gbn = NULL, *priv_key = NULL;
|
|
|
|
BIGNUM *pub_key = NULL;
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
nbyte = (bitlen + 7) >> 3;
|
|
|
|
|
|
|
|
dsa = DSA_new();
|
|
|
|
ret = EVP_PKEY_new();
|
2015-10-30 11:12:26 +00:00
|
|
|
if (dsa == NULL || ret == NULL)
|
2015-01-22 03:40:55 +00:00
|
|
|
goto memerr;
|
2016-03-30 14:21:39 +00:00
|
|
|
if (!read_lebn(&p, nbyte, &pbn))
|
2015-01-22 03:40:55 +00:00
|
|
|
goto memerr;
|
2016-03-30 14:21:39 +00:00
|
|
|
|
|
|
|
if (!read_lebn(&p, 20, &qbn))
|
2015-01-22 03:40:55 +00:00
|
|
|
goto memerr;
|
2016-03-30 14:21:39 +00:00
|
|
|
|
|
|
|
if (!read_lebn(&p, nbyte, &gbn))
|
2015-01-22 03:40:55 +00:00
|
|
|
goto memerr;
|
2016-03-30 14:21:39 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (ispub) {
|
2016-03-30 14:21:39 +00:00
|
|
|
if (!read_lebn(&p, nbyte, &pub_key))
|
2015-01-22 03:40:55 +00:00
|
|
|
goto memerr;
|
|
|
|
} else {
|
2016-03-30 14:21:39 +00:00
|
|
|
if (!read_lebn(&p, 20, &priv_key))
|
2015-01-22 03:40:55 +00:00
|
|
|
goto memerr;
|
2016-03-30 14:21:39 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/* Calculate public key */
|
2016-03-30 14:21:39 +00:00
|
|
|
pub_key = BN_new();
|
|
|
|
if (pub_key == NULL)
|
2015-01-22 03:40:55 +00:00
|
|
|
goto memerr;
|
2015-05-06 17:43:59 +00:00
|
|
|
if ((ctx = BN_CTX_new()) == NULL)
|
2015-01-22 03:40:55 +00:00
|
|
|
goto memerr;
|
|
|
|
|
2016-03-30 14:21:39 +00:00
|
|
|
if (!BN_mod_exp(pub_key, gbn, priv_key, pbn, ctx))
|
2015-01-22 03:40:55 +00:00
|
|
|
goto memerr;
|
2016-03-30 14:21:39 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
BN_CTX_free(ctx);
|
|
|
|
}
|
2016-03-30 14:21:39 +00:00
|
|
|
if (!DSA_set0_pqg(dsa, pbn, qbn, gbn))
|
|
|
|
goto memerr;
|
|
|
|
pbn = qbn = gbn = NULL;
|
|
|
|
if (!DSA_set0_key(dsa, pub_key, priv_key))
|
|
|
|
goto memerr;
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
EVP_PKEY_set1_DSA(ret, dsa);
|
|
|
|
DSA_free(dsa);
|
|
|
|
*in = p;
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
memerr:
|
|
|
|
PEMerr(PEM_F_B2I_DSS, ERR_R_MALLOC_FAILURE);
|
2015-03-24 14:17:37 +00:00
|
|
|
DSA_free(dsa);
|
2016-03-30 14:21:39 +00:00
|
|
|
BN_free(pbn);
|
|
|
|
BN_free(qbn);
|
|
|
|
BN_free(gbn);
|
|
|
|
BN_free(pub_key);
|
|
|
|
BN_free(priv_key);
|
2015-03-28 14:54:15 +00:00
|
|
|
EVP_PKEY_free(ret);
|
2015-05-01 01:37:06 +00:00
|
|
|
BN_CTX_free(ctx);
|
2015-01-22 03:40:55 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2005-08-31 16:37:54 +00:00
|
|
|
|
2016-02-14 03:33:56 +00:00
|
|
|
static EVP_PKEY *b2i_rsa(const unsigned char **in,
|
2015-01-22 03:40:55 +00:00
|
|
|
unsigned int bitlen, int ispub)
|
|
|
|
{
|
2016-04-02 13:12:58 +00:00
|
|
|
const unsigned char *pin = *in;
|
2015-01-22 03:40:55 +00:00
|
|
|
EVP_PKEY *ret = NULL;
|
2016-04-02 13:12:58 +00:00
|
|
|
BIGNUM *e = NULL, *n = NULL, *d = NULL;
|
2016-04-27 12:52:37 +00:00
|
|
|
BIGNUM *p = NULL, *q = NULL, *dmp1 = NULL, *dmq1 = NULL, *iqmp = NULL;
|
2015-01-22 03:40:55 +00:00
|
|
|
RSA *rsa = NULL;
|
|
|
|
unsigned int nbyte, hnbyte;
|
|
|
|
nbyte = (bitlen + 7) >> 3;
|
|
|
|
hnbyte = (bitlen + 15) >> 4;
|
|
|
|
rsa = RSA_new();
|
|
|
|
ret = EVP_PKEY_new();
|
2015-10-30 11:12:26 +00:00
|
|
|
if (rsa == NULL || ret == NULL)
|
2015-01-22 03:40:55 +00:00
|
|
|
goto memerr;
|
2016-04-02 13:12:58 +00:00
|
|
|
e = BN_new();
|
|
|
|
if (e == NULL)
|
2015-01-22 03:40:55 +00:00
|
|
|
goto memerr;
|
2016-04-02 13:12:58 +00:00
|
|
|
if (!BN_set_word(e, read_ledword(&pin)))
|
2015-01-22 03:40:55 +00:00
|
|
|
goto memerr;
|
2016-04-02 13:12:58 +00:00
|
|
|
if (!read_lebn(&pin, nbyte, &n))
|
2015-01-22 03:40:55 +00:00
|
|
|
goto memerr;
|
|
|
|
if (!ispub) {
|
2016-04-02 13:12:58 +00:00
|
|
|
if (!read_lebn(&pin, hnbyte, &p))
|
2015-01-22 03:40:55 +00:00
|
|
|
goto memerr;
|
2016-04-02 13:12:58 +00:00
|
|
|
if (!read_lebn(&pin, hnbyte, &q))
|
2015-01-22 03:40:55 +00:00
|
|
|
goto memerr;
|
2016-04-02 13:12:58 +00:00
|
|
|
if (!read_lebn(&pin, hnbyte, &dmp1))
|
2015-01-22 03:40:55 +00:00
|
|
|
goto memerr;
|
2016-04-02 13:12:58 +00:00
|
|
|
if (!read_lebn(&pin, hnbyte, &dmq1))
|
2015-01-22 03:40:55 +00:00
|
|
|
goto memerr;
|
2016-04-02 13:12:58 +00:00
|
|
|
if (!read_lebn(&pin, hnbyte, &iqmp))
|
2015-01-22 03:40:55 +00:00
|
|
|
goto memerr;
|
2016-04-02 13:12:58 +00:00
|
|
|
if (!read_lebn(&pin, nbyte, &d))
|
2015-01-22 03:40:55 +00:00
|
|
|
goto memerr;
|
2016-04-02 13:12:58 +00:00
|
|
|
RSA_set0_factors(rsa, p, q);
|
|
|
|
RSA_set0_crt_params(rsa, dmp1, dmq1, iqmp);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2016-04-28 14:20:52 +00:00
|
|
|
RSA_set0_key(rsa, n, e, d);
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
EVP_PKEY_set1_RSA(ret, rsa);
|
|
|
|
RSA_free(rsa);
|
2016-04-02 13:12:58 +00:00
|
|
|
*in = pin;
|
2015-01-22 03:40:55 +00:00
|
|
|
return ret;
|
|
|
|
memerr:
|
|
|
|
PEMerr(PEM_F_B2I_RSA, ERR_R_MALLOC_FAILURE);
|
2016-04-27 12:52:37 +00:00
|
|
|
BN_free(e);
|
|
|
|
BN_free(n);
|
|
|
|
BN_free(p);
|
|
|
|
BN_free(q);
|
|
|
|
BN_free(dmp1);
|
|
|
|
BN_free(dmq1);
|
|
|
|
BN_free(iqmp);
|
|
|
|
BN_free(d);
|
2015-03-24 14:17:37 +00:00
|
|
|
RSA_free(rsa);
|
2015-03-28 14:54:15 +00:00
|
|
|
EVP_PKEY_free(ret);
|
2015-01-22 03:40:55 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2005-08-31 16:37:54 +00:00
|
|
|
|
|
|
|
EVP_PKEY *b2i_PrivateKey(const unsigned char **in, long length)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
return do_b2i(in, length, 0);
|
|
|
|
}
|
2005-08-31 16:37:54 +00:00
|
|
|
|
|
|
|
EVP_PKEY *b2i_PublicKey(const unsigned char **in, long length)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
return do_b2i(in, length, 1);
|
|
|
|
}
|
2005-08-31 16:37:54 +00:00
|
|
|
|
|
|
|
EVP_PKEY *b2i_PrivateKey_bio(BIO *in)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
return do_b2i_bio(in, 0);
|
|
|
|
}
|
2005-08-31 16:37:54 +00:00
|
|
|
|
|
|
|
EVP_PKEY *b2i_PublicKey_bio(BIO *in)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
return do_b2i_bio(in, 1);
|
|
|
|
}
|
2005-08-31 16:37:54 +00:00
|
|
|
|
|
|
|
static void write_ledword(unsigned char **out, unsigned int dw)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
unsigned char *p = *out;
|
|
|
|
*p++ = dw & 0xff;
|
|
|
|
*p++ = (dw >> 8) & 0xff;
|
|
|
|
*p++ = (dw >> 16) & 0xff;
|
|
|
|
*p++ = (dw >> 24) & 0xff;
|
|
|
|
*out = p;
|
|
|
|
}
|
2005-08-31 16:37:54 +00:00
|
|
|
|
|
|
|
static void write_lebn(unsigned char **out, const BIGNUM *bn, int len)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2016-02-01 00:02:05 +00:00
|
|
|
BN_bn2lebinpad(bn, *out, len);
|
|
|
|
*out += len;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2005-08-31 16:37:54 +00:00
|
|
|
|
|
|
|
static int check_bitlen_rsa(RSA *rsa, int ispub, unsigned int *magic);
|
|
|
|
static int check_bitlen_dsa(DSA *dsa, int ispub, unsigned int *magic);
|
|
|
|
|
|
|
|
static void write_rsa(unsigned char **out, RSA *rsa, int ispub);
|
|
|
|
static void write_dsa(unsigned char **out, DSA *dsa, int ispub);
|
2015-01-22 03:40:55 +00:00
|
|
|
|
2005-08-31 16:37:54 +00:00
|
|
|
static int do_i2b(unsigned char **out, EVP_PKEY *pk, int ispub)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
unsigned char *p;
|
|
|
|
unsigned int bitlen, magic = 0, keyalg;
|
|
|
|
int outlen, noinc = 0;
|
2016-01-19 00:21:12 +00:00
|
|
|
int pktype = EVP_PKEY_id(pk);
|
|
|
|
if (pktype == EVP_PKEY_DSA) {
|
|
|
|
bitlen = check_bitlen_dsa(EVP_PKEY_get0_DSA(pk), ispub, &magic);
|
2015-01-22 03:40:55 +00:00
|
|
|
keyalg = MS_KEYALG_DSS_SIGN;
|
2016-01-19 00:21:12 +00:00
|
|
|
} else if (pktype == EVP_PKEY_RSA) {
|
|
|
|
bitlen = check_bitlen_rsa(EVP_PKEY_get0_RSA(pk), ispub, &magic);
|
2015-01-22 03:40:55 +00:00
|
|
|
keyalg = MS_KEYALG_RSA_KEYX;
|
|
|
|
} else
|
|
|
|
return -1;
|
|
|
|
if (bitlen == 0)
|
|
|
|
return -1;
|
|
|
|
outlen = 16 + blob_length(bitlen,
|
|
|
|
keyalg == MS_KEYALG_DSS_SIGN ? 1 : 0, ispub);
|
|
|
|
if (out == NULL)
|
|
|
|
return outlen;
|
|
|
|
if (*out)
|
|
|
|
p = *out;
|
|
|
|
else {
|
|
|
|
p = OPENSSL_malloc(outlen);
|
2015-10-30 11:12:26 +00:00
|
|
|
if (p == NULL)
|
2015-01-22 03:40:55 +00:00
|
|
|
return -1;
|
|
|
|
*out = p;
|
|
|
|
noinc = 1;
|
|
|
|
}
|
|
|
|
if (ispub)
|
|
|
|
*p++ = MS_PUBLICKEYBLOB;
|
|
|
|
else
|
|
|
|
*p++ = MS_PRIVATEKEYBLOB;
|
|
|
|
*p++ = 0x2;
|
|
|
|
*p++ = 0;
|
|
|
|
*p++ = 0;
|
|
|
|
write_ledword(&p, keyalg);
|
|
|
|
write_ledword(&p, magic);
|
|
|
|
write_ledword(&p, bitlen);
|
|
|
|
if (keyalg == MS_KEYALG_DSS_SIGN)
|
2016-01-19 00:21:12 +00:00
|
|
|
write_dsa(&p, EVP_PKEY_get0_DSA(pk), ispub);
|
2015-01-22 03:40:55 +00:00
|
|
|
else
|
2016-01-19 00:21:12 +00:00
|
|
|
write_rsa(&p, EVP_PKEY_get0_RSA(pk), ispub);
|
2015-01-22 03:40:55 +00:00
|
|
|
if (!noinc)
|
|
|
|
*out += outlen;
|
|
|
|
return outlen;
|
|
|
|
}
|
2005-08-31 16:37:54 +00:00
|
|
|
|
|
|
|
static int do_i2b_bio(BIO *out, EVP_PKEY *pk, int ispub)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
unsigned char *tmp = NULL;
|
|
|
|
int outlen, wrlen;
|
|
|
|
outlen = do_i2b(&tmp, pk, ispub);
|
|
|
|
if (outlen < 0)
|
|
|
|
return -1;
|
|
|
|
wrlen = BIO_write(out, tmp, outlen);
|
|
|
|
OPENSSL_free(tmp);
|
|
|
|
if (wrlen == outlen)
|
|
|
|
return outlen;
|
|
|
|
return -1;
|
|
|
|
}
|
2005-08-31 16:37:54 +00:00
|
|
|
|
|
|
|
static int check_bitlen_dsa(DSA *dsa, int ispub, unsigned int *pmagic)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
int bitlen;
|
2016-06-14 13:49:05 +00:00
|
|
|
const BIGNUM *p = NULL, *q = NULL, *g = NULL;
|
|
|
|
const BIGNUM *pub_key = NULL, *priv_key = NULL;
|
2016-03-30 16:18:55 +00:00
|
|
|
|
|
|
|
DSA_get0_pqg(dsa, &p, &q, &g);
|
|
|
|
DSA_get0_key(dsa, &pub_key, &priv_key);
|
|
|
|
bitlen = BN_num_bits(p);
|
|
|
|
if ((bitlen & 7) || (BN_num_bits(q) != 160)
|
|
|
|
|| (BN_num_bits(g) > bitlen))
|
2015-01-22 03:40:55 +00:00
|
|
|
goto badkey;
|
|
|
|
if (ispub) {
|
2016-03-30 16:18:55 +00:00
|
|
|
if (BN_num_bits(pub_key) > bitlen)
|
2015-01-22 03:40:55 +00:00
|
|
|
goto badkey;
|
|
|
|
*pmagic = MS_DSS1MAGIC;
|
|
|
|
} else {
|
2016-03-30 16:18:55 +00:00
|
|
|
if (BN_num_bits(priv_key) > 160)
|
2015-01-22 03:40:55 +00:00
|
|
|
goto badkey;
|
|
|
|
*pmagic = MS_DSS2MAGIC;
|
|
|
|
}
|
|
|
|
|
|
|
|
return bitlen;
|
|
|
|
badkey:
|
|
|
|
PEMerr(PEM_F_CHECK_BITLEN_DSA, PEM_R_UNSUPPORTED_KEY_COMPONENTS);
|
|
|
|
return 0;
|
|
|
|
}
|
2005-08-31 16:37:54 +00:00
|
|
|
|
|
|
|
static int check_bitlen_rsa(RSA *rsa, int ispub, unsigned int *pmagic)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
int nbyte, hnbyte, bitlen;
|
2016-06-14 13:49:05 +00:00
|
|
|
const BIGNUM *e;
|
2016-04-02 13:12:58 +00:00
|
|
|
|
2016-08-01 11:10:11 +00:00
|
|
|
RSA_get0_key(rsa, NULL, &e, NULL);
|
2016-04-02 13:12:58 +00:00
|
|
|
if (BN_num_bits(e) > 32)
|
2015-01-22 03:40:55 +00:00
|
|
|
goto badkey;
|
2016-04-02 13:12:58 +00:00
|
|
|
bitlen = RSA_bits(rsa);
|
|
|
|
nbyte = RSA_size(rsa);
|
|
|
|
hnbyte = (bitlen + 15) >> 4;
|
2015-01-22 03:40:55 +00:00
|
|
|
if (ispub) {
|
|
|
|
*pmagic = MS_RSA1MAGIC;
|
|
|
|
return bitlen;
|
|
|
|
} else {
|
2016-06-14 13:49:05 +00:00
|
|
|
const BIGNUM *d, *p, *q, *iqmp, *dmp1, *dmq1;
|
2016-04-02 13:12:58 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
*pmagic = MS_RSA2MAGIC;
|
2016-04-02 13:12:58 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/*
|
|
|
|
* For private key each component must fit within nbyte or hnbyte.
|
|
|
|
*/
|
2016-04-02 13:12:58 +00:00
|
|
|
RSA_get0_key(rsa, NULL, NULL, &d);
|
|
|
|
if (BN_num_bytes(d) > nbyte)
|
2015-01-22 03:40:55 +00:00
|
|
|
goto badkey;
|
2016-04-02 13:12:58 +00:00
|
|
|
RSA_get0_factors(rsa, &p, &q);
|
|
|
|
RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
|
|
|
|
if ((BN_num_bytes(iqmp) > hnbyte)
|
|
|
|
|| (BN_num_bytes(p) > hnbyte)
|
|
|
|
|| (BN_num_bytes(q) > hnbyte)
|
|
|
|
|| (BN_num_bytes(dmp1) > hnbyte)
|
|
|
|
|| (BN_num_bytes(dmq1) > hnbyte))
|
2015-01-22 03:40:55 +00:00
|
|
|
goto badkey;
|
|
|
|
}
|
|
|
|
return bitlen;
|
|
|
|
badkey:
|
|
|
|
PEMerr(PEM_F_CHECK_BITLEN_RSA, PEM_R_UNSUPPORTED_KEY_COMPONENTS);
|
|
|
|
return 0;
|
|
|
|
}
|
2005-08-31 16:37:54 +00:00
|
|
|
|
|
|
|
static void write_rsa(unsigned char **out, RSA *rsa, int ispub)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
int nbyte, hnbyte;
|
2016-06-14 13:49:05 +00:00
|
|
|
const BIGNUM *n, *d, *e, *p, *q, *iqmp, *dmp1, *dmq1;
|
2016-04-02 13:12:58 +00:00
|
|
|
|
|
|
|
nbyte = RSA_size(rsa);
|
|
|
|
hnbyte = (RSA_bits(rsa) + 15) >> 4;
|
2016-08-01 11:10:11 +00:00
|
|
|
RSA_get0_key(rsa, &n, &e, &d);
|
2016-04-02 13:12:58 +00:00
|
|
|
write_lebn(out, e, 4);
|
2016-11-16 23:03:43 +00:00
|
|
|
write_lebn(out, n, nbyte);
|
2015-01-22 03:40:55 +00:00
|
|
|
if (ispub)
|
|
|
|
return;
|
2016-04-02 13:12:58 +00:00
|
|
|
RSA_get0_factors(rsa, &p, &q);
|
|
|
|
RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
|
|
|
|
write_lebn(out, p, hnbyte);
|
|
|
|
write_lebn(out, q, hnbyte);
|
|
|
|
write_lebn(out, dmp1, hnbyte);
|
|
|
|
write_lebn(out, dmq1, hnbyte);
|
|
|
|
write_lebn(out, iqmp, hnbyte);
|
|
|
|
write_lebn(out, d, nbyte);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
|
2005-08-31 16:37:54 +00:00
|
|
|
static void write_dsa(unsigned char **out, DSA *dsa, int ispub)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
int nbyte;
|
2016-06-14 13:49:05 +00:00
|
|
|
const BIGNUM *p = NULL, *q = NULL, *g = NULL;
|
|
|
|
const BIGNUM *pub_key = NULL, *priv_key = NULL;
|
2016-03-30 16:18:55 +00:00
|
|
|
|
|
|
|
DSA_get0_pqg(dsa, &p, &q, &g);
|
|
|
|
DSA_get0_key(dsa, &pub_key, &priv_key);
|
|
|
|
nbyte = BN_num_bytes(p);
|
|
|
|
write_lebn(out, p, nbyte);
|
|
|
|
write_lebn(out, q, 20);
|
|
|
|
write_lebn(out, g, nbyte);
|
2015-01-22 03:40:55 +00:00
|
|
|
if (ispub)
|
2016-03-30 16:18:55 +00:00
|
|
|
write_lebn(out, pub_key, nbyte);
|
2015-01-22 03:40:55 +00:00
|
|
|
else
|
2016-03-30 16:18:55 +00:00
|
|
|
write_lebn(out, priv_key, 20);
|
2015-01-22 03:40:55 +00:00
|
|
|
/* Set "invalid" for seed structure values */
|
|
|
|
memset(*out, 0xff, 24);
|
|
|
|
*out += 24;
|
|
|
|
return;
|
|
|
|
}
|
2005-08-31 16:37:54 +00:00
|
|
|
|
|
|
|
int i2b_PrivateKey_bio(BIO *out, EVP_PKEY *pk)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
return do_i2b_bio(out, pk, 0);
|
|
|
|
}
|
2005-08-31 16:37:54 +00:00
|
|
|
|
|
|
|
int i2b_PublicKey_bio(BIO *out, EVP_PKEY *pk)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
return do_i2b_bio(out, pk, 1);
|
|
|
|
}
|
2005-08-31 16:37:54 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
# ifndef OPENSSL_NO_RC4
|
2010-04-06 11:18:59 +00:00
|
|
|
|
2005-08-31 16:37:54 +00:00
|
|
|
static int do_PVK_header(const unsigned char **in, unsigned int length,
|
2015-01-22 03:40:55 +00:00
|
|
|
int skip_magic,
|
|
|
|
unsigned int *psaltlen, unsigned int *pkeylen)
|
|
|
|
{
|
|
|
|
const unsigned char *p = *in;
|
|
|
|
unsigned int pvk_magic, is_encrypted;
|
|
|
|
if (skip_magic) {
|
|
|
|
if (length < 20) {
|
|
|
|
PEMerr(PEM_F_DO_PVK_HEADER, PEM_R_PVK_TOO_SHORT);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (length < 24) {
|
|
|
|
PEMerr(PEM_F_DO_PVK_HEADER, PEM_R_PVK_TOO_SHORT);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
pvk_magic = read_ledword(&p);
|
|
|
|
if (pvk_magic != MS_PVKMAGIC) {
|
|
|
|
PEMerr(PEM_F_DO_PVK_HEADER, PEM_R_BAD_MAGIC_NUMBER);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Skip reserved */
|
|
|
|
p += 4;
|
|
|
|
/*
|
|
|
|
* keytype =
|
|
|
|
*/ read_ledword(&p);
|
|
|
|
is_encrypted = read_ledword(&p);
|
|
|
|
*psaltlen = read_ledword(&p);
|
|
|
|
*pkeylen = read_ledword(&p);
|
|
|
|
|
2016-03-03 23:37:36 +00:00
|
|
|
if (*pkeylen > PVK_MAX_KEYLEN || *psaltlen > PVK_MAX_SALTLEN)
|
|
|
|
return 0;
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (is_encrypted && !*psaltlen) {
|
|
|
|
PEMerr(PEM_F_DO_PVK_HEADER, PEM_R_INCONSISTENT_HEADER);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
*in = p;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int derive_pvk_key(unsigned char *key,
|
|
|
|
const unsigned char *salt, unsigned int saltlen,
|
|
|
|
const unsigned char *pass, int passlen)
|
|
|
|
{
|
2015-12-07 23:11:47 +00:00
|
|
|
EVP_MD_CTX *mctx = EVP_MD_CTX_new();
|
2015-01-22 03:40:55 +00:00
|
|
|
int rv = 1;
|
2015-11-27 13:02:12 +00:00
|
|
|
if (mctx == NULL
|
|
|
|
|| !EVP_DigestInit_ex(mctx, EVP_sha1(), NULL)
|
|
|
|
|| !EVP_DigestUpdate(mctx, salt, saltlen)
|
|
|
|
|| !EVP_DigestUpdate(mctx, pass, passlen)
|
|
|
|
|| !EVP_DigestFinal_ex(mctx, key, NULL))
|
2015-01-22 03:40:55 +00:00
|
|
|
rv = 0;
|
|
|
|
|
2015-12-01 23:49:35 +00:00
|
|
|
EVP_MD_CTX_free(mctx);
|
2015-01-22 03:40:55 +00:00
|
|
|
return rv;
|
|
|
|
}
|
2005-08-31 16:37:54 +00:00
|
|
|
|
|
|
|
static EVP_PKEY *do_PVK_body(const unsigned char **in,
|
2015-01-22 03:40:55 +00:00
|
|
|
unsigned int saltlen, unsigned int keylen,
|
|
|
|
pem_password_cb *cb, void *u)
|
|
|
|
{
|
|
|
|
EVP_PKEY *ret = NULL;
|
|
|
|
const unsigned char *p = *in;
|
|
|
|
unsigned int magic;
|
|
|
|
unsigned char *enctmp = NULL, *q;
|
2015-05-01 18:37:16 +00:00
|
|
|
|
2015-12-13 21:08:41 +00:00
|
|
|
EVP_CIPHER_CTX *cctx = EVP_CIPHER_CTX_new();
|
2015-01-22 03:40:55 +00:00
|
|
|
if (saltlen) {
|
|
|
|
char psbuf[PEM_BUFSIZE];
|
|
|
|
unsigned char keybuf[20];
|
|
|
|
int enctmplen, inlen;
|
|
|
|
if (cb)
|
|
|
|
inlen = cb(psbuf, PEM_BUFSIZE, 0, u);
|
|
|
|
else
|
|
|
|
inlen = PEM_def_callback(psbuf, PEM_BUFSIZE, 0, u);
|
|
|
|
if (inlen <= 0) {
|
|
|
|
PEMerr(PEM_F_DO_PVK_BODY, PEM_R_BAD_PASSWORD_READ);
|
2015-10-08 12:38:57 +00:00
|
|
|
goto err;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
enctmp = OPENSSL_malloc(keylen + 8);
|
2015-10-30 11:12:26 +00:00
|
|
|
if (enctmp == NULL) {
|
2015-01-22 03:40:55 +00:00
|
|
|
PEMerr(PEM_F_DO_PVK_BODY, ERR_R_MALLOC_FAILURE);
|
2015-10-08 12:38:57 +00:00
|
|
|
goto err;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
if (!derive_pvk_key(keybuf, p, saltlen,
|
|
|
|
(unsigned char *)psbuf, inlen))
|
2015-10-08 12:38:57 +00:00
|
|
|
goto err;
|
2015-01-22 03:40:55 +00:00
|
|
|
p += saltlen;
|
|
|
|
/* Copy BLOBHEADER across, decrypt rest */
|
|
|
|
memcpy(enctmp, p, 8);
|
|
|
|
p += 8;
|
|
|
|
if (keylen < 8) {
|
|
|
|
PEMerr(PEM_F_DO_PVK_BODY, PEM_R_PVK_TOO_SHORT);
|
2015-10-08 12:38:57 +00:00
|
|
|
goto err;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
inlen = keylen - 8;
|
|
|
|
q = enctmp + 8;
|
2015-12-13 21:08:41 +00:00
|
|
|
if (!EVP_DecryptInit_ex(cctx, EVP_rc4(), NULL, keybuf, NULL))
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
2015-12-13 21:08:41 +00:00
|
|
|
if (!EVP_DecryptUpdate(cctx, q, &enctmplen, p, inlen))
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
2015-12-13 21:08:41 +00:00
|
|
|
if (!EVP_DecryptFinal_ex(cctx, q + enctmplen, &enctmplen))
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
|
|
|
magic = read_ledword((const unsigned char **)&q);
|
|
|
|
if (magic != MS_RSA2MAGIC && magic != MS_DSS2MAGIC) {
|
|
|
|
q = enctmp + 8;
|
|
|
|
memset(keybuf + 5, 0, 11);
|
2015-12-13 21:08:41 +00:00
|
|
|
if (!EVP_DecryptInit_ex(cctx, EVP_rc4(), NULL, keybuf, NULL))
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
|
|
|
OPENSSL_cleanse(keybuf, 20);
|
2015-12-13 21:08:41 +00:00
|
|
|
if (!EVP_DecryptUpdate(cctx, q, &enctmplen, p, inlen))
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
2015-12-13 21:08:41 +00:00
|
|
|
if (!EVP_DecryptFinal_ex(cctx, q + enctmplen, &enctmplen))
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
|
|
|
magic = read_ledword((const unsigned char **)&q);
|
|
|
|
if (magic != MS_RSA2MAGIC && magic != MS_DSS2MAGIC) {
|
|
|
|
PEMerr(PEM_F_DO_PVK_BODY, PEM_R_BAD_DECRYPT);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
OPENSSL_cleanse(keybuf, 20);
|
|
|
|
p = enctmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = b2i_PrivateKey(&p, keylen);
|
|
|
|
err:
|
2015-12-13 21:08:41 +00:00
|
|
|
EVP_CIPHER_CTX_free(cctx);
|
2015-05-01 18:37:16 +00:00
|
|
|
OPENSSL_free(enctmp);
|
2015-01-22 03:40:55 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2005-08-31 16:37:54 +00:00
|
|
|
|
|
|
|
EVP_PKEY *b2i_PVK_bio(BIO *in, pem_password_cb *cb, void *u)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
unsigned char pvk_hdr[24], *buf = NULL;
|
|
|
|
const unsigned char *p;
|
|
|
|
int buflen;
|
|
|
|
EVP_PKEY *ret = NULL;
|
|
|
|
unsigned int saltlen, keylen;
|
|
|
|
if (BIO_read(in, pvk_hdr, 24) != 24) {
|
|
|
|
PEMerr(PEM_F_B2I_PVK_BIO, PEM_R_PVK_DATA_TOO_SHORT);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
p = pvk_hdr;
|
|
|
|
|
|
|
|
if (!do_PVK_header(&p, 24, 0, &saltlen, &keylen))
|
|
|
|
return 0;
|
|
|
|
buflen = (int)keylen + saltlen;
|
|
|
|
buf = OPENSSL_malloc(buflen);
|
2015-10-30 11:12:26 +00:00
|
|
|
if (buf == NULL) {
|
2015-01-22 03:40:55 +00:00
|
|
|
PEMerr(PEM_F_B2I_PVK_BIO, ERR_R_MALLOC_FAILURE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
p = buf;
|
|
|
|
if (BIO_read(in, buf, buflen) != buflen) {
|
|
|
|
PEMerr(PEM_F_B2I_PVK_BIO, PEM_R_PVK_DATA_TOO_SHORT);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
ret = do_PVK_body(&p, saltlen, keylen, cb, u);
|
|
|
|
|
|
|
|
err:
|
2015-04-30 21:57:32 +00:00
|
|
|
OPENSSL_clear_free(buf, buflen);
|
2015-01-22 03:40:55 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int i2b_PVK(unsigned char **out, EVP_PKEY *pk, int enclevel,
|
|
|
|
pem_password_cb *cb, void *u)
|
|
|
|
{
|
|
|
|
int outlen = 24, pklen;
|
2016-08-01 11:11:21 +00:00
|
|
|
unsigned char *p = NULL, *start = NULL, *salt = NULL;
|
2016-04-27 13:00:37 +00:00
|
|
|
EVP_CIPHER_CTX *cctx = NULL;
|
2015-01-22 03:40:55 +00:00
|
|
|
if (enclevel)
|
|
|
|
outlen += PVK_SALTLEN;
|
|
|
|
pklen = do_i2b(NULL, pk, 0);
|
|
|
|
if (pklen < 0)
|
|
|
|
return -1;
|
|
|
|
outlen += pklen;
|
2016-04-27 13:00:37 +00:00
|
|
|
if (out == NULL)
|
2015-01-22 03:40:55 +00:00
|
|
|
return outlen;
|
2016-04-27 13:00:37 +00:00
|
|
|
if (*out != NULL) {
|
2015-01-22 03:40:55 +00:00
|
|
|
p = *out;
|
2016-04-27 13:00:37 +00:00
|
|
|
} else {
|
2016-08-01 11:11:21 +00:00
|
|
|
start = p = OPENSSL_malloc(outlen);
|
2015-10-30 11:12:26 +00:00
|
|
|
if (p == NULL) {
|
2015-01-22 03:40:55 +00:00
|
|
|
PEMerr(PEM_F_I2B_PVK, ERR_R_MALLOC_FAILURE);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-27 13:00:37 +00:00
|
|
|
cctx = EVP_CIPHER_CTX_new();
|
|
|
|
if (cctx == NULL)
|
2016-04-28 18:49:17 +00:00
|
|
|
goto error;
|
2016-04-27 13:00:37 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
write_ledword(&p, MS_PVKMAGIC);
|
|
|
|
write_ledword(&p, 0);
|
2016-01-19 00:21:12 +00:00
|
|
|
if (EVP_PKEY_id(pk) == EVP_PKEY_DSA)
|
2015-01-22 03:40:55 +00:00
|
|
|
write_ledword(&p, MS_KEYTYPE_SIGN);
|
|
|
|
else
|
|
|
|
write_ledword(&p, MS_KEYTYPE_KEYX);
|
|
|
|
write_ledword(&p, enclevel ? 1 : 0);
|
|
|
|
write_ledword(&p, enclevel ? PVK_SALTLEN : 0);
|
|
|
|
write_ledword(&p, pklen);
|
|
|
|
if (enclevel) {
|
|
|
|
if (RAND_bytes(p, PVK_SALTLEN) <= 0)
|
|
|
|
goto error;
|
|
|
|
salt = p;
|
|
|
|
p += PVK_SALTLEN;
|
|
|
|
}
|
|
|
|
do_i2b(&p, pk, 0);
|
2016-04-27 13:00:37 +00:00
|
|
|
if (enclevel != 0) {
|
2015-01-22 03:40:55 +00:00
|
|
|
char psbuf[PEM_BUFSIZE];
|
|
|
|
unsigned char keybuf[20];
|
|
|
|
int enctmplen, inlen;
|
|
|
|
if (cb)
|
|
|
|
inlen = cb(psbuf, PEM_BUFSIZE, 1, u);
|
|
|
|
else
|
|
|
|
inlen = PEM_def_callback(psbuf, PEM_BUFSIZE, 1, u);
|
|
|
|
if (inlen <= 0) {
|
|
|
|
PEMerr(PEM_F_I2B_PVK, PEM_R_BAD_PASSWORD_READ);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if (!derive_pvk_key(keybuf, salt, PVK_SALTLEN,
|
|
|
|
(unsigned char *)psbuf, inlen))
|
|
|
|
goto error;
|
|
|
|
if (enclevel == 1)
|
|
|
|
memset(keybuf + 5, 0, 11);
|
|
|
|
p = salt + PVK_SALTLEN + 8;
|
2015-12-13 21:08:41 +00:00
|
|
|
if (!EVP_EncryptInit_ex(cctx, EVP_rc4(), NULL, keybuf, NULL))
|
2015-01-22 03:40:55 +00:00
|
|
|
goto error;
|
|
|
|
OPENSSL_cleanse(keybuf, 20);
|
2015-12-13 21:08:41 +00:00
|
|
|
if (!EVP_DecryptUpdate(cctx, p, &enctmplen, p, pklen - 8))
|
2015-01-22 03:40:55 +00:00
|
|
|
goto error;
|
2015-12-13 21:08:41 +00:00
|
|
|
if (!EVP_DecryptFinal_ex(cctx, p + enctmplen, &enctmplen))
|
2015-01-22 03:40:55 +00:00
|
|
|
goto error;
|
|
|
|
}
|
2016-04-27 13:00:37 +00:00
|
|
|
|
2015-12-13 21:08:41 +00:00
|
|
|
EVP_CIPHER_CTX_free(cctx);
|
2016-04-27 13:00:37 +00:00
|
|
|
|
|
|
|
if (*out == NULL)
|
2016-08-01 11:11:21 +00:00
|
|
|
*out = start;
|
2016-04-27 13:00:37 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
return outlen;
|
|
|
|
|
|
|
|
error:
|
2015-12-13 21:08:41 +00:00
|
|
|
EVP_CIPHER_CTX_free(cctx);
|
2016-04-28 18:49:17 +00:00
|
|
|
if (*out == NULL)
|
2016-08-01 11:11:21 +00:00
|
|
|
OPENSSL_free(start);
|
2015-01-22 03:40:55 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2005-08-31 16:37:54 +00:00
|
|
|
|
|
|
|
int i2b_PVK_bio(BIO *out, EVP_PKEY *pk, int enclevel,
|
2015-01-22 03:40:55 +00:00
|
|
|
pem_password_cb *cb, void *u)
|
|
|
|
{
|
|
|
|
unsigned char *tmp = NULL;
|
|
|
|
int outlen, wrlen;
|
|
|
|
outlen = i2b_PVK(&tmp, pk, enclevel, cb, u);
|
|
|
|
if (outlen < 0)
|
|
|
|
return -1;
|
|
|
|
wrlen = BIO_write(out, tmp, outlen);
|
|
|
|
OPENSSL_free(tmp);
|
|
|
|
if (wrlen == outlen) {
|
|
|
|
PEMerr(PEM_F_I2B_PVK_BIO, PEM_R_BIO_WRITE_FAILURE);
|
|
|
|
return outlen;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
# endif
|
2010-04-06 11:18:59 +00:00
|
|
|
|
2009-04-26 22:18:22 +00:00
|
|
|
#endif
|