6a053611e16a7e38cbc8f62b23d9ff4cff23496a
[openssl.git] / crypto / dsa / dsa_backend.c
1 /*
2  * Copyright 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 <openssl/core_names.h>
17 #include "crypto/dsa.h"
18
19 /*
20  * The intention with the "backend" source file is to offer backend support
21  * for legacy backends (EVP_PKEY_ASN1_METHOD and EVP_PKEY_METHOD) and provider
22  * implementations alike.
23  */
24
25 int dsa_key_fromdata(DSA *dsa, const OSSL_PARAM params[])
26 {
27     const OSSL_PARAM *param_priv_key, *param_pub_key;
28     BIGNUM *priv_key = NULL, *pub_key = NULL;
29
30     if (dsa == NULL)
31         return 0;
32
33     param_priv_key =
34         OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
35     param_pub_key =
36         OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY);
37
38     /* It's ok if neither half is present */
39     if (param_priv_key == NULL && param_pub_key == NULL)
40         return 1;
41
42     if (param_pub_key != NULL && !OSSL_PARAM_get_BN(param_pub_key, &pub_key))
43         goto err;
44     if (param_priv_key != NULL && !OSSL_PARAM_get_BN(param_priv_key, &priv_key))
45         goto err;
46
47     if (!DSA_set0_key(dsa, pub_key, priv_key))
48         goto err;
49
50     return 1;
51
52  err:
53     BN_clear_free(priv_key);
54     BN_free(pub_key);
55     return 0;
56 }