Make EVP_PKEY_CTX_[get|set]_group_name work for DH too
[openssl.git] / crypto / ffc / ffc_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 #include <openssl/core_names.h>
11 #include "internal/ffc.h"
12 #include "internal/sizes.h"
13
14 /*
15  * The intention with the "backend" source file is to offer backend support
16  * for legacy backends (EVP_PKEY_ASN1_METHOD and EVP_PKEY_METHOD) and provider
17  * implementations alike.
18  */
19
20 int ffc_params_fromdata(FFC_PARAMS *ffc, const OSSL_PARAM params[])
21 {
22     const OSSL_PARAM *prm;
23     const OSSL_PARAM *param_p, *param_q, *param_g;
24     BIGNUM *p = NULL, *q = NULL, *g = NULL, *j = NULL;
25     int i;
26
27     if (ffc == NULL)
28         return 0;
29
30     prm  = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_GROUP_NAME);
31     if (prm != NULL) {
32         if (prm->data_type != OSSL_PARAM_UTF8_STRING)
33             goto err;
34 #ifndef OPENSSL_NO_DH
35         /*
36          * In a no-dh build we just go straight to err because we have no
37          * support for this.
38          */
39         if (!ffc_set_group_pqg(ffc, prm->data))
40 #endif
41             goto err;
42     }
43
44     param_p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_P);
45     param_g = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_G);
46     param_q = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_Q);
47
48     if ((param_p != NULL && !OSSL_PARAM_get_BN(param_p, &p))
49         || (param_q != NULL && !OSSL_PARAM_get_BN(param_q, &q))
50         || (param_g != NULL && !OSSL_PARAM_get_BN(param_g, &g)))
51         goto err;
52
53     prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_GINDEX);
54     if (prm != NULL) {
55         if (!OSSL_PARAM_get_int(prm, &i))
56             goto err;
57         ffc->gindex =  i;
58     }
59     prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_PCOUNTER);
60     if (prm != NULL) {
61         if (!OSSL_PARAM_get_int(prm, &i))
62             goto err;
63         ffc->pcounter = i;
64     }
65     prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_COFACTOR);
66     if (prm != NULL && !OSSL_PARAM_get_BN(prm, &j))
67         goto err;
68     prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_H);
69     if (prm != NULL) {
70         if (!OSSL_PARAM_get_int(prm, &i))
71             goto err;
72         ffc->h =  i;
73     }
74     prm  = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_SEED);
75     if (prm != NULL) {
76         if (prm->data_type != OSSL_PARAM_OCTET_STRING)
77             goto err;
78         if (!ffc_params_set_seed(ffc, prm->data, prm->data_size))
79             goto err;
80     }
81     prm  = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_VALIDATE_TYPE);
82     if (prm != NULL) {
83         if (prm->data_type != OSSL_PARAM_UTF8_STRING)
84             goto err;
85         ffc_params_set_flags(ffc, ffc_params_flags_from_name(prm->data));
86     }
87     prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_DIGEST);
88     if (prm != NULL) {
89         const OSSL_PARAM *p1;
90         const char *props = NULL;
91
92         if (prm->data_type != OSSL_PARAM_UTF8_STRING)
93             goto err;
94         p1 = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_DIGEST_PROPS);
95         if (p1 != NULL) {
96             if (p1->data_type != OSSL_PARAM_UTF8_STRING)
97                 goto err;
98         }
99         if (!ffc_set_digest(ffc, prm->data, props))
100             goto err;
101     }
102
103     ffc_params_set0_pqg(ffc, p, q, g);
104     ffc_params_set0_j(ffc, j);
105     return 1;
106
107  err:
108     BN_free(j);
109     BN_free(p);
110     BN_free(q);
111     BN_free(g);
112     return 0;
113 }