2016-05-18 02:51:34 +08:00
|
|
|
/*
|
2021-06-17 20:24:59 +08:00
|
|
|
* Copyright 1995-2021 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-01-30 05:23:39 +08:00
|
|
|
/*
|
|
|
|
* DSA low level APIs are deprecated for public use, but still ok for
|
|
|
|
* internal use.
|
|
|
|
*/
|
|
|
|
#include "internal/deprecated.h"
|
|
|
|
|
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>
|
1999-07-24 11:09:01 +08:00
|
|
|
#include <openssl/asn1.h>
|
2016-03-19 02:30:20 +08:00
|
|
|
#include <openssl/rsa.h>
|
|
|
|
#include <openssl/dsa.h>
|
|
|
|
#include <openssl/ec.h>
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2019-09-28 06:45:33 +08:00
|
|
|
#include "crypto/evp.h"
|
2016-01-19 08:21:12 +08:00
|
|
|
|
2004-03-16 07:15:26 +08:00
|
|
|
EVP_PKEY *d2i_PublicKey(int type, EVP_PKEY **a, const unsigned char **pp,
|
2015-01-22 11:40:55 +08:00
|
|
|
long length)
|
|
|
|
{
|
|
|
|
EVP_PKEY *ret;
|
2021-11-19 03:09:57 +08:00
|
|
|
EVP_PKEY *copy = NULL;
|
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-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
|
2017-10-17 22:04:09 +08:00
|
|
|
return NULL;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2021-11-19 03:09:57 +08:00
|
|
|
} else {
|
2015-01-22 11:40:55 +08:00
|
|
|
ret = *a;
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2021-11-19 03:09:57 +08:00
|
|
|
#ifndef OPENSSL_NO_EC
|
|
|
|
if (evp_pkey_is_provided(ret)
|
|
|
|
&& EVP_PKEY_get_base_id(ret) == EVP_PKEY_EC) {
|
|
|
|
if (!evp_pkey_copy_downgraded(©, ret))
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((type != EVP_PKEY_get_id(ret) || copy != NULL)
|
|
|
|
&& !EVP_PKEY_set_type(ret, type)) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
}
|
2009-11-13 03:56:56 +08:00
|
|
|
|
Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
2021-05-21 22:58:08 +08:00
|
|
|
switch (EVP_PKEY_get_id(ret)) {
|
2015-01-22 11:40:55 +08:00
|
|
|
case EVP_PKEY_RSA:
|
2016-01-19 08:21:12 +08:00
|
|
|
if ((ret->pkey.rsa = d2i_RSAPublicKey(NULL, pp, length)) == NULL) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
break;
|
2001-02-20 00:06:34 +08:00
|
|
|
#ifndef OPENSSL_NO_DSA
|
2015-01-22 11:40:55 +08:00
|
|
|
case EVP_PKEY_DSA:
|
2016-01-19 08:21:12 +08:00
|
|
|
if (!d2i_DSAPublicKey(&ret->pkey.dsa, pp, length)) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
break;
|
2002-02-14 02:21:51 +08:00
|
|
|
#endif
|
2002-08-07 18:49:54 +08:00
|
|
|
#ifndef OPENSSL_NO_EC
|
2015-01-22 11:40:55 +08:00
|
|
|
case EVP_PKEY_EC:
|
2021-11-19 03:09:57 +08:00
|
|
|
if (copy != NULL) {
|
|
|
|
/* use downgraded parameters from copy */
|
|
|
|
ret->pkey.ec = copy->pkey.ec;
|
|
|
|
copy->pkey.ec = NULL;
|
|
|
|
}
|
2016-01-19 08:21:12 +08:00
|
|
|
if (!o2i_ECPublicKey(&ret->pkey.ec, pp, length)) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
break;
|
1998-12-21 18:52:47 +08:00
|
|
|
#endif
|
2015-01-22 11:40:55 +08:00
|
|
|
default:
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_ASN1, ASN1_R_UNKNOWN_PUBLIC_KEY_TYPE);
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (a != NULL)
|
|
|
|
(*a) = ret;
|
2021-11-19 03:09:57 +08:00
|
|
|
EVP_PKEY_free(copy);
|
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);
|
2021-11-19 03:09:57 +08:00
|
|
|
EVP_PKEY_free(copy);
|
2017-10-17 22:04:09 +08:00
|
|
|
return NULL;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|