xref: /third_party/openssl/crypto/evp/p_legacy.c (revision e1051a39)
1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci/*
11e1051a39Sopenharmony_ci * Legacy EVP_PKEY assign/set/get APIs are deprecated for public use, but
12e1051a39Sopenharmony_ci * still ok for internal use, particularly in providers.
13e1051a39Sopenharmony_ci */
14e1051a39Sopenharmony_ci#include "internal/deprecated.h"
15e1051a39Sopenharmony_ci
16e1051a39Sopenharmony_ci#include <openssl/types.h>
17e1051a39Sopenharmony_ci#include <openssl/evp.h>
18e1051a39Sopenharmony_ci#include <openssl/err.h>
19e1051a39Sopenharmony_ci#include <openssl/rsa.h>
20e1051a39Sopenharmony_ci#include <openssl/ec.h>
21e1051a39Sopenharmony_ci#include "crypto/types.h"
22e1051a39Sopenharmony_ci#include "crypto/evp.h"
23e1051a39Sopenharmony_ci#include "evp_local.h"
24e1051a39Sopenharmony_ci
25e1051a39Sopenharmony_ciint EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key)
26e1051a39Sopenharmony_ci{
27e1051a39Sopenharmony_ci    int ret = EVP_PKEY_assign_RSA(pkey, key);
28e1051a39Sopenharmony_ci
29e1051a39Sopenharmony_ci    if (ret)
30e1051a39Sopenharmony_ci        RSA_up_ref(key);
31e1051a39Sopenharmony_ci    return ret;
32e1051a39Sopenharmony_ci}
33e1051a39Sopenharmony_ci
34e1051a39Sopenharmony_ciRSA *evp_pkey_get0_RSA_int(const EVP_PKEY *pkey)
35e1051a39Sopenharmony_ci{
36e1051a39Sopenharmony_ci    if (pkey->type != EVP_PKEY_RSA && pkey->type != EVP_PKEY_RSA_PSS) {
37e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_AN_RSA_KEY);
38e1051a39Sopenharmony_ci        return NULL;
39e1051a39Sopenharmony_ci    }
40e1051a39Sopenharmony_ci    return evp_pkey_get_legacy((EVP_PKEY *)pkey);
41e1051a39Sopenharmony_ci}
42e1051a39Sopenharmony_ci
43e1051a39Sopenharmony_ciconst RSA *EVP_PKEY_get0_RSA(const EVP_PKEY *pkey)
44e1051a39Sopenharmony_ci{
45e1051a39Sopenharmony_ci    return evp_pkey_get0_RSA_int(pkey);
46e1051a39Sopenharmony_ci}
47e1051a39Sopenharmony_ci
48e1051a39Sopenharmony_ciRSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey)
49e1051a39Sopenharmony_ci{
50e1051a39Sopenharmony_ci    RSA *ret = evp_pkey_get0_RSA_int(pkey);
51e1051a39Sopenharmony_ci
52e1051a39Sopenharmony_ci    if (ret != NULL)
53e1051a39Sopenharmony_ci        RSA_up_ref(ret);
54e1051a39Sopenharmony_ci    return ret;
55e1051a39Sopenharmony_ci}
56e1051a39Sopenharmony_ci
57e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_EC
58e1051a39Sopenharmony_ciint EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key)
59e1051a39Sopenharmony_ci{
60e1051a39Sopenharmony_ci    if (!EC_KEY_up_ref(key))
61e1051a39Sopenharmony_ci        return 0;
62e1051a39Sopenharmony_ci    if (!EVP_PKEY_assign_EC_KEY(pkey, key)) {
63e1051a39Sopenharmony_ci        EC_KEY_free(key);
64e1051a39Sopenharmony_ci        return 0;
65e1051a39Sopenharmony_ci    }
66e1051a39Sopenharmony_ci    return 1;
67e1051a39Sopenharmony_ci}
68e1051a39Sopenharmony_ci
69e1051a39Sopenharmony_ciEC_KEY *evp_pkey_get0_EC_KEY_int(const EVP_PKEY *pkey)
70e1051a39Sopenharmony_ci{
71e1051a39Sopenharmony_ci    if (EVP_PKEY_get_base_id(pkey) != EVP_PKEY_EC) {
72e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_EC_KEY);
73e1051a39Sopenharmony_ci        return NULL;
74e1051a39Sopenharmony_ci    }
75e1051a39Sopenharmony_ci    return evp_pkey_get_legacy((EVP_PKEY *)pkey);
76e1051a39Sopenharmony_ci}
77e1051a39Sopenharmony_ci
78e1051a39Sopenharmony_ciconst EC_KEY *EVP_PKEY_get0_EC_KEY(const EVP_PKEY *pkey)
79e1051a39Sopenharmony_ci{
80e1051a39Sopenharmony_ci    return evp_pkey_get0_EC_KEY_int(pkey);
81e1051a39Sopenharmony_ci}
82e1051a39Sopenharmony_ci
83e1051a39Sopenharmony_ciEC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey)
84e1051a39Sopenharmony_ci{
85e1051a39Sopenharmony_ci    EC_KEY *ret = evp_pkey_get0_EC_KEY_int(pkey);
86e1051a39Sopenharmony_ci
87e1051a39Sopenharmony_ci    if (ret != NULL && !EC_KEY_up_ref(ret))
88e1051a39Sopenharmony_ci        ret = NULL;
89e1051a39Sopenharmony_ci    return ret;
90e1051a39Sopenharmony_ci}
91e1051a39Sopenharmony_ci#endif /* OPENSSL_NO_EC */
92