2016-05-18 02:51:34 +08:00
|
|
|
/*
|
2020-04-23 20:55:52 +08:00
|
|
|
* Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
|
1998-12-21 18:52:47 +08:00
|
|
|
*
|
2018-12-06 20:17:34 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-18 02:51:34 +08: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
|
1998-12-21 18:52:47 +08:00
|
|
|
*/
|
|
|
|
|
2020-07-14 07:40:29 +08:00
|
|
|
/* We need to use some engine deprecated APIs */
|
|
|
|
#define OPENSSL_SUPPRESS_DEPRECATED
|
|
|
|
|
1998-12-21 18:52:47 +08:00
|
|
|
#include <stdio.h>
|
2015-05-14 22:56:48 +08:00
|
|
|
#include "internal/cryptlib.h"
|
1999-04-24 06:13:45 +08:00
|
|
|
#include <openssl/bn.h>
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/objects.h>
|
2016-03-19 02:30:20 +08:00
|
|
|
#include <openssl/engine.h>
|
2007-11-20 21:37:51 +08:00
|
|
|
#include <openssl/x509.h>
|
1999-07-24 11:09:01 +08:00
|
|
|
#include <openssl/asn1.h>
|
2019-09-28 06:45:33 +08:00
|
|
|
#include "crypto/asn1.h"
|
|
|
|
#include "crypto/evp.h"
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2020-04-07 01:18:18 +08:00
|
|
|
EVP_PKEY *d2i_PrivateKey_ex(int type, EVP_PKEY **a, const unsigned char **pp,
|
|
|
|
long length, OPENSSL_CTX *libctx, const char *propq)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
EVP_PKEY *ret;
|
2015-08-17 22:02:18 +08:00
|
|
|
const unsigned char *p = *pp;
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if ((a == NULL) || (*a == NULL)) {
|
|
|
|
if ((ret = EVP_PKEY_new()) == NULL) {
|
2020-04-07 01:18:18 +08:00
|
|
|
ASN1err(0, ERR_R_EVP_LIB);
|
2017-10-17 22:04:09 +08:00
|
|
|
return NULL;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ret = *a;
|
2006-06-05 20:38:22 +08:00
|
|
|
#ifndef OPENSSL_NO_ENGINE
|
2016-02-26 01:09:06 +08:00
|
|
|
ENGINE_finish(ret->engine);
|
|
|
|
ret->engine = NULL;
|
2006-06-05 20:38:22 +08:00
|
|
|
#endif
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2006-06-05 19:52:46 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (!EVP_PKEY_set_type(ret, type)) {
|
2020-04-07 01:18:18 +08:00
|
|
|
ASN1err(0, ASN1_R_UNKNOWN_PUBLIC_KEY_TYPE);
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
}
|
2006-06-05 19:52:46 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (!ret->ameth->old_priv_decode ||
|
2015-08-17 22:02:18 +08:00
|
|
|
!ret->ameth->old_priv_decode(ret, &p, length)) {
|
2020-04-07 01:18:18 +08:00
|
|
|
if (ret->ameth->priv_decode != NULL
|
2020-09-24 17:42:23 +08:00
|
|
|
|| ret->ameth->priv_decode_ex != NULL) {
|
2016-05-03 22:05:31 +08:00
|
|
|
EVP_PKEY *tmp;
|
2015-01-22 11:40:55 +08:00
|
|
|
PKCS8_PRIV_KEY_INFO *p8 = NULL;
|
2015-08-17 22:02:18 +08:00
|
|
|
p8 = d2i_PKCS8_PRIV_KEY_INFO(NULL, &p, length);
|
2019-09-17 03:28:57 +08:00
|
|
|
if (p8 == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
2020-09-24 17:42:23 +08:00
|
|
|
tmp = EVP_PKCS82PKEY_ex(p8, libctx, propq);
|
2015-01-22 11:40:55 +08:00
|
|
|
PKCS8_PRIV_KEY_INFO_free(p8);
|
2016-05-03 22:05:31 +08:00
|
|
|
if (tmp == NULL)
|
2015-09-30 01:59:48 +08:00
|
|
|
goto err;
|
2016-05-03 22:05:31 +08:00
|
|
|
EVP_PKEY_free(ret);
|
|
|
|
ret = tmp;
|
2020-05-11 15:14:11 +08:00
|
|
|
if (EVP_PKEY_type(type) != EVP_PKEY_base_id(ret))
|
|
|
|
goto err;
|
2015-01-22 11:40:55 +08:00
|
|
|
} else {
|
2020-04-07 01:18:18 +08:00
|
|
|
ASN1err(0, ERR_R_ASN1_LIB);
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
2015-08-17 22:02:18 +08:00
|
|
|
*pp = p;
|
2015-01-22 11:40:55 +08:00
|
|
|
if (a != NULL)
|
|
|
|
(*a) = ret;
|
2017-10-17 22:04:09 +08:00
|
|
|
return ret;
|
2015-01-22 11:40:55 +08:00
|
|
|
err:
|
2015-03-28 22:54:15 +08:00
|
|
|
if (a == NULL || *a != ret)
|
2015-01-22 11:40:55 +08:00
|
|
|
EVP_PKEY_free(ret);
|
2017-10-17 22:04:09 +08:00
|
|
|
return NULL;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2020-04-07 01:18:18 +08:00
|
|
|
EVP_PKEY *d2i_PrivateKey(int type, EVP_PKEY **a, const unsigned char **pp,
|
|
|
|
long length)
|
|
|
|
{
|
|
|
|
return d2i_PrivateKey_ex(type, a, pp, length, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
/*
|
|
|
|
* This works like d2i_PrivateKey() except it automatically works out the
|
|
|
|
* type
|
|
|
|
*/
|
2000-01-02 00:42:49 +08:00
|
|
|
|
2020-04-07 01:18:18 +08:00
|
|
|
EVP_PKEY *d2i_AutoPrivateKey_ex(EVP_PKEY **a, const unsigned char **pp,
|
|
|
|
long length, OPENSSL_CTX *libctx,
|
|
|
|
const char *propq)
|
2000-01-02 00:42:49 +08:00
|
|
|
{
|
2015-01-22 11:40:55 +08:00
|
|
|
STACK_OF(ASN1_TYPE) *inkey;
|
|
|
|
const unsigned char *p;
|
|
|
|
int keytype;
|
|
|
|
p = *pp;
|
|
|
|
/*
|
|
|
|
* Dirty trick: read in the ASN1 data into a STACK_OF(ASN1_TYPE): by
|
|
|
|
* analyzing it we can determine the passed structure: this assumes the
|
|
|
|
* input is surrounded by an ASN1 SEQUENCE.
|
|
|
|
*/
|
|
|
|
inkey = d2i_ASN1_SEQUENCE_ANY(NULL, &p, length);
|
2015-08-17 22:02:18 +08:00
|
|
|
p = *pp;
|
2015-01-22 11:40:55 +08:00
|
|
|
/*
|
|
|
|
* Since we only need to discern "traditional format" RSA and DSA keys we
|
|
|
|
* can just count the elements.
|
|
|
|
*/
|
2020-04-07 01:18:18 +08:00
|
|
|
if (sk_ASN1_TYPE_num(inkey) == 6) {
|
2015-01-22 11:40:55 +08:00
|
|
|
keytype = EVP_PKEY_DSA;
|
2020-04-07 01:18:18 +08:00
|
|
|
} else if (sk_ASN1_TYPE_num(inkey) == 4) {
|
2015-01-22 11:40:55 +08:00
|
|
|
keytype = EVP_PKEY_EC;
|
2020-04-07 01:18:18 +08:00
|
|
|
} else if (sk_ASN1_TYPE_num(inkey) == 3) { /* This seems to be PKCS8, not
|
2015-01-22 11:40:55 +08:00
|
|
|
* traditional format */
|
2015-08-17 22:02:18 +08:00
|
|
|
PKCS8_PRIV_KEY_INFO *p8 = d2i_PKCS8_PRIV_KEY_INFO(NULL, &p, length);
|
2015-01-22 11:40:55 +08:00
|
|
|
EVP_PKEY *ret;
|
2007-11-20 21:37:51 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
sk_ASN1_TYPE_pop_free(inkey, ASN1_TYPE_free);
|
2019-09-17 03:28:57 +08:00
|
|
|
if (p8 == NULL) {
|
2020-04-07 01:18:18 +08:00
|
|
|
ASN1err(0, ASN1_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
|
2015-01-22 11:40:55 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2020-09-24 17:42:23 +08:00
|
|
|
ret = EVP_PKCS82PKEY_ex(p8, libctx, propq);
|
2015-01-22 11:40:55 +08:00
|
|
|
PKCS8_PRIV_KEY_INFO_free(p8);
|
2015-09-30 01:59:48 +08:00
|
|
|
if (ret == NULL)
|
|
|
|
return NULL;
|
|
|
|
*pp = p;
|
2015-01-22 11:40:55 +08:00
|
|
|
if (a) {
|
|
|
|
*a = ret;
|
|
|
|
}
|
|
|
|
return ret;
|
2020-04-07 01:18:18 +08:00
|
|
|
} else {
|
2015-01-22 11:40:55 +08:00
|
|
|
keytype = EVP_PKEY_RSA;
|
2020-04-07 01:18:18 +08:00
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
sk_ASN1_TYPE_pop_free(inkey, ASN1_TYPE_free);
|
2020-04-07 01:18:18 +08:00
|
|
|
return d2i_PrivateKey_ex(keytype, a, pp, length, libctx, propq);
|
|
|
|
}
|
|
|
|
|
|
|
|
EVP_PKEY *d2i_AutoPrivateKey(EVP_PKEY **a, const unsigned char **pp,
|
|
|
|
long length)
|
|
|
|
{
|
|
|
|
return d2i_AutoPrivateKey_ex(a, pp, length, NULL, NULL);
|
2000-01-02 00:42:49 +08:00
|
|
|
}
|