]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/asn1/d2i_pu.c
rand: remove unimplemented librandom stub code
[thirdparty/openssl.git] / crypto / asn1 / d2i_pu.c
CommitLineData
2039c421 1/*
fecb3aae 2 * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
365a2d99 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
2039c421
RS
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
d02b48c6
RE
8 */
9
f41ac0ee
P
10/*
11 * DSA low level APIs are deprecated for public use, but still ok for
12 * internal use.
13 */
14#include "internal/deprecated.h"
15
d02b48c6 16#include <stdio.h>
b39fc560 17#include "internal/cryptlib.h"
ec577822
BM
18#include <openssl/bn.h>
19#include <openssl/evp.h>
20#include <openssl/objects.h>
f66c3032 21#include <openssl/asn1.h>
3c27208f
RS
22#include <openssl/rsa.h>
23#include <openssl/dsa.h>
24#include <openssl/ec.h>
d02b48c6 25
25f2138b 26#include "crypto/evp.h"
3aeb9348 27
875a644a 28EVP_PKEY *d2i_PublicKey(int type, EVP_PKEY **a, const unsigned char **pp,
0f113f3e
MC
29 long length)
30{
31 EVP_PKEY *ret;
615a9b87 32 EVP_PKEY *copy = NULL;
d02b48c6 33
0f113f3e
MC
34 if ((a == NULL) || (*a == NULL)) {
35 if ((ret = EVP_PKEY_new()) == NULL) {
9311d0c4 36 ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
26a7d938 37 return NULL;
0f113f3e 38 }
615a9b87 39 } else {
0f113f3e 40 ret = *a;
d02b48c6 41
615a9b87
TM
42#ifndef OPENSSL_NO_EC
43 if (evp_pkey_is_provided(ret)
44 && EVP_PKEY_get_base_id(ret) == EVP_PKEY_EC) {
45 if (!evp_pkey_copy_downgraded(&copy, ret))
46 goto err;
47 }
48#endif
49 }
50
51 if ((type != EVP_PKEY_get_id(ret) || copy != NULL)
52 && !EVP_PKEY_set_type(ret, type)) {
9311d0c4 53 ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
0f113f3e
MC
54 goto err;
55 }
c18e51ba 56
8582dccc 57 switch (EVP_PKEY_get_base_id(ret)) {
0f113f3e 58 case EVP_PKEY_RSA:
3aeb9348 59 if ((ret->pkey.rsa = d2i_RSAPublicKey(NULL, pp, length)) == NULL) {
9311d0c4 60 ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
0f113f3e
MC
61 goto err;
62 }
63 break;
cf1b7d96 64#ifndef OPENSSL_NO_DSA
0f113f3e 65 case EVP_PKEY_DSA:
3aeb9348 66 if (!d2i_DSAPublicKey(&ret->pkey.dsa, pp, length)) {
9311d0c4 67 ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
0f113f3e
MC
68 goto err;
69 }
70 break;
4d94ae00 71#endif
14a7cfb3 72#ifndef OPENSSL_NO_EC
0f113f3e 73 case EVP_PKEY_EC:
615a9b87
TM
74 if (copy != NULL) {
75 /* use downgraded parameters from copy */
76 ret->pkey.ec = copy->pkey.ec;
77 copy->pkey.ec = NULL;
78 }
3aeb9348 79 if (!o2i_ECPublicKey(&ret->pkey.ec, pp, length)) {
9311d0c4 80 ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
0f113f3e
MC
81 goto err;
82 }
83 break;
d02b48c6 84#endif
0f113f3e 85 default:
9311d0c4 86 ERR_raise(ERR_LIB_ASN1, ASN1_R_UNKNOWN_PUBLIC_KEY_TYPE);
0f113f3e 87 goto err;
0f113f3e
MC
88 }
89 if (a != NULL)
90 (*a) = ret;
615a9b87 91 EVP_PKEY_free(copy);
26a7d938 92 return ret;
0f113f3e 93 err:
c5ba2d99 94 if (a == NULL || *a != ret)
0f113f3e 95 EVP_PKEY_free(ret);
615a9b87 96 EVP_PKEY_free(copy);
26a7d938 97 return NULL;
0f113f3e 98}