2 * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 * DSA low level APIs are deprecated for public use, but still ok for
14 #include "internal/deprecated.h"
17 #include "internal/cryptlib.h"
18 #include <openssl/bn.h>
19 #include <openssl/evp.h>
20 #include <openssl/objects.h>
21 #include <openssl/asn1.h>
22 #include <openssl/rsa.h>
23 #include <openssl/dsa.h>
24 #include <openssl/ec.h>
26 #include "crypto/evp.h"
28 EVP_PKEY *d2i_PublicKey(int type, EVP_PKEY **a, const unsigned char **pp,
32 EVP_PKEY *copy = NULL;
34 if ((a == NULL) || (*a == NULL)) {
35 if ((ret = EVP_PKEY_new()) == NULL) {
36 ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
43 if (evp_pkey_is_provided(ret)
44 && EVP_PKEY_get_base_id(ret) == EVP_PKEY_EC) {
45 if (!evp_pkey_copy_downgraded(©, ret))
51 if ((type != EVP_PKEY_get_id(ret) || copy != NULL)
52 && !EVP_PKEY_set_type(ret, type)) {
53 ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
57 switch (EVP_PKEY_get_base_id(ret)) {
59 if ((ret->pkey.rsa = d2i_RSAPublicKey(NULL, pp, length)) == NULL) {
60 ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
64 #ifndef OPENSSL_NO_DSA
66 if (!d2i_DSAPublicKey(&ret->pkey.dsa, pp, length)) {
67 ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
75 /* use downgraded parameters from copy */
76 ret->pkey.ec = copy->pkey.ec;
79 if (!o2i_ECPublicKey(&ret->pkey.ec, pp, length)) {
80 ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
86 ERR_raise(ERR_LIB_ASN1, ASN1_R_UNKNOWN_PUBLIC_KEY_TYPE);
94 if (a == NULL || *a != ret)