Drop OPENSSL_NO_RSA everywhere
[openssl.git] / crypto / asn1 / d2i_pu.c
1 /*
2  * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
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
16 #include <stdio.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>
25
26 #include "crypto/evp.h"
27
28 EVP_PKEY *d2i_PublicKey(int type, EVP_PKEY **a, const unsigned char **pp,
29                         long length)
30 {
31     EVP_PKEY *ret;
32
33     if ((a == NULL) || (*a == NULL)) {
34         if ((ret = EVP_PKEY_new()) == NULL) {
35             ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
36             return NULL;
37         }
38     } else
39         ret = *a;
40
41     if (type != EVP_PKEY_id(ret) && !EVP_PKEY_set_type(ret, type)) {
42         ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
43         goto err;
44     }
45
46     switch (EVP_PKEY_id(ret)) {
47     case EVP_PKEY_RSA:
48         if ((ret->pkey.rsa = d2i_RSAPublicKey(NULL, pp, length)) == NULL) {
49             ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
50             goto err;
51         }
52         break;
53 #ifndef OPENSSL_NO_DSA
54     case EVP_PKEY_DSA:
55         /* TMP UGLY CAST */
56         if (!d2i_DSAPublicKey(&ret->pkey.dsa, pp, length)) {
57             ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
58             goto err;
59         }
60         break;
61 #endif
62 #ifndef OPENSSL_NO_EC
63     case EVP_PKEY_EC:
64         if (!o2i_ECPublicKey(&ret->pkey.ec, pp, length)) {
65             ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
66             goto err;
67         }
68         break;
69 #endif
70     default:
71         ERR_raise(ERR_LIB_ASN1, ASN1_R_UNKNOWN_PUBLIC_KEY_TYPE);
72         goto err;
73     }
74     if (a != NULL)
75         (*a) = ret;
76     return ret;
77  err:
78     if (a == NULL || *a != ret)
79         EVP_PKEY_free(ret);
80     return NULL;
81 }