1/*
2 * Copyright 1995-2021 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 * Legacy EVP_PKEY assign/set/get APIs are deprecated for public use, but
12 * still ok for internal use, particularly in providers.
13 */
14#include "internal/deprecated.h"
15
16#include <openssl/types.h>
17#include <openssl/evp.h>
18#include <openssl/err.h>
19#include <openssl/rsa.h>
20#include <openssl/ec.h>
21#include "crypto/types.h"
22#include "crypto/evp.h"
23#include "evp_local.h"
24
25int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key)
26{
27    int ret = EVP_PKEY_assign_RSA(pkey, key);
28
29    if (ret)
30        RSA_up_ref(key);
31    return ret;
32}
33
34RSA *evp_pkey_get0_RSA_int(const EVP_PKEY *pkey)
35{
36    if (pkey->type != EVP_PKEY_RSA && pkey->type != EVP_PKEY_RSA_PSS) {
37        ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_AN_RSA_KEY);
38        return NULL;
39    }
40    return evp_pkey_get_legacy((EVP_PKEY *)pkey);
41}
42
43const RSA *EVP_PKEY_get0_RSA(const EVP_PKEY *pkey)
44{
45    return evp_pkey_get0_RSA_int(pkey);
46}
47
48RSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey)
49{
50    RSA *ret = evp_pkey_get0_RSA_int(pkey);
51
52    if (ret != NULL)
53        RSA_up_ref(ret);
54    return ret;
55}
56
57#ifndef OPENSSL_NO_EC
58int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key)
59{
60    if (!EC_KEY_up_ref(key))
61        return 0;
62    if (!EVP_PKEY_assign_EC_KEY(pkey, key)) {
63        EC_KEY_free(key);
64        return 0;
65    }
66    return 1;
67}
68
69EC_KEY *evp_pkey_get0_EC_KEY_int(const EVP_PKEY *pkey)
70{
71    if (EVP_PKEY_get_base_id(pkey) != EVP_PKEY_EC) {
72        ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_EC_KEY);
73        return NULL;
74    }
75    return evp_pkey_get_legacy((EVP_PKEY *)pkey);
76}
77
78const EC_KEY *EVP_PKEY_get0_EC_KEY(const EVP_PKEY *pkey)
79{
80    return evp_pkey_get0_EC_KEY_int(pkey);
81}
82
83EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey)
84{
85    EC_KEY *ret = evp_pkey_get0_EC_KEY_int(pkey);
86
87    if (ret != NULL && !EC_KEY_up_ref(ret))
88        ret = NULL;
89    return ret;
90}
91#endif /* OPENSSL_NO_EC */
92