/src/openssl/crypto/ec/ec_backend.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * Copyright 2020-2024 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  |  |  * Low level APIs related to EC_KEY are deprecated for public use,  | 
12  |  |  * but still ok for internal use.  | 
13  |  |  */  | 
14  |  | #include "internal/deprecated.h"  | 
15  |  |  | 
16  |  | #include <openssl/core_names.h>  | 
17  |  | #include <openssl/objects.h>  | 
18  |  | #include <openssl/params.h>  | 
19  |  | #include <openssl/err.h>  | 
20  |  | #ifndef FIPS_MODULE  | 
21  |  | # include <openssl/engine.h>  | 
22  |  | # include <openssl/x509.h>  | 
23  |  | #endif  | 
24  |  | #include "crypto/bn.h"  | 
25  |  | #include "crypto/ec.h"  | 
26  |  | #include "ec_local.h"  | 
27  |  | #include "internal/e_os.h"  | 
28  |  | #include "internal/nelem.h"  | 
29  |  | #include "internal/param_build_set.h"  | 
30  |  |  | 
31  |  | /* Mapping between a flag and a name */  | 
32  |  | static const OSSL_ITEM encoding_nameid_map[] = { | 
33  |  |     { OPENSSL_EC_EXPLICIT_CURVE, OSSL_PKEY_EC_ENCODING_EXPLICIT }, | 
34  |  |     { OPENSSL_EC_NAMED_CURVE, OSSL_PKEY_EC_ENCODING_GROUP }, | 
35  |  | };  | 
36  |  |  | 
37  |  | static const OSSL_ITEM check_group_type_nameid_map[] = { | 
38  |  |     { 0, OSSL_PKEY_EC_GROUP_CHECK_DEFAULT }, | 
39  |  |     { EC_FLAG_CHECK_NAMED_GROUP, OSSL_PKEY_EC_GROUP_CHECK_NAMED }, | 
40  |  |     { EC_FLAG_CHECK_NAMED_GROUP_NIST, OSSL_PKEY_EC_GROUP_CHECK_NAMED_NIST }, | 
41  |  | };  | 
42  |  |  | 
43  |  | static const OSSL_ITEM format_nameid_map[] = { | 
44  |  |     { (int)POINT_CONVERSION_UNCOMPRESSED, OSSL_PKEY_EC_POINT_CONVERSION_FORMAT_UNCOMPRESSED }, | 
45  |  |     { (int)POINT_CONVERSION_COMPRESSED, OSSL_PKEY_EC_POINT_CONVERSION_FORMAT_COMPRESSED }, | 
46  |  |     { (int)POINT_CONVERSION_HYBRID, OSSL_PKEY_EC_POINT_CONVERSION_FORMAT_HYBRID }, | 
47  |  | };  | 
48  |  |  | 
49  |  | int ossl_ec_encoding_name2id(const char *name)  | 
50  | 0  | { | 
51  | 0  |     size_t i, sz;  | 
52  |  |  | 
53  |  |     /* Return the default value if there is no name */  | 
54  | 0  |     if (name == NULL)  | 
55  | 0  |         return OPENSSL_EC_NAMED_CURVE;  | 
56  |  |  | 
57  | 0  |     for (i = 0, sz = OSSL_NELEM(encoding_nameid_map); i < sz; i++) { | 
58  | 0  |         if (OPENSSL_strcasecmp(name, encoding_nameid_map[i].ptr) == 0)  | 
59  | 0  |             return encoding_nameid_map[i].id;  | 
60  | 0  |     }  | 
61  | 0  |     return -1;  | 
62  | 0  | }  | 
63  |  |  | 
64  |  | static char *ec_param_encoding_id2name(int id)  | 
65  | 0  | { | 
66  | 0  |     size_t i, sz;  | 
67  |  | 
  | 
68  | 0  |     for (i = 0, sz = OSSL_NELEM(encoding_nameid_map); i < sz; i++) { | 
69  | 0  |         if (id == (int)encoding_nameid_map[i].id)  | 
70  | 0  |             return encoding_nameid_map[i].ptr;  | 
71  | 0  |     }  | 
72  | 0  |     return NULL;  | 
73  | 0  | }  | 
74  |  |  | 
75  |  | char *ossl_ec_check_group_type_id2name(int id)  | 
76  | 0  | { | 
77  | 0  |     size_t i, sz;  | 
78  |  | 
  | 
79  | 0  |     for (i = 0, sz = OSSL_NELEM(check_group_type_nameid_map); i < sz; i++) { | 
80  | 0  |         if (id == (int)check_group_type_nameid_map[i].id)  | 
81  | 0  |             return check_group_type_nameid_map[i].ptr;  | 
82  | 0  |     }  | 
83  | 0  |     return NULL;  | 
84  | 0  | }  | 
85  |  |  | 
86  |  | static int ec_check_group_type_name2id(const char *name)  | 
87  | 0  | { | 
88  | 0  |     size_t i, sz;  | 
89  |  |  | 
90  |  |     /* Return the default value if there is no name */  | 
91  | 0  |     if (name == NULL)  | 
92  | 0  |         return 0;  | 
93  |  |  | 
94  | 0  |     for (i = 0, sz = OSSL_NELEM(check_group_type_nameid_map); i < sz; i++) { | 
95  | 0  |         if (OPENSSL_strcasecmp(name, check_group_type_nameid_map[i].ptr) == 0)  | 
96  | 0  |             return check_group_type_nameid_map[i].id;  | 
97  | 0  |     }  | 
98  | 0  |     return -1;  | 
99  | 0  | }  | 
100  |  |  | 
101  |  | int ossl_ec_set_check_group_type_from_name(EC_KEY *ec, const char *name)  | 
102  | 0  | { | 
103  | 0  |     int flags = ec_check_group_type_name2id(name);  | 
104  |  | 
  | 
105  | 0  |     if (flags == -1)  | 
106  | 0  |         return 0;  | 
107  | 0  |     EC_KEY_clear_flags(ec, EC_FLAG_CHECK_NAMED_GROUP_MASK);  | 
108  | 0  |     EC_KEY_set_flags(ec, flags);  | 
109  | 0  |     return 1;  | 
110  | 0  | }  | 
111  |  |  | 
112  |  | static int ec_set_check_group_type_from_param(EC_KEY *ec, const OSSL_PARAM *p)  | 
113  | 0  | { | 
114  | 0  |     const char *name = NULL;  | 
115  | 0  |     int status = 0;  | 
116  |  | 
  | 
117  | 0  |     switch (p->data_type) { | 
118  | 0  |     case OSSL_PARAM_UTF8_STRING:  | 
119  | 0  |         name = p->data;  | 
120  | 0  |         status = (name != NULL);  | 
121  | 0  |         break;  | 
122  | 0  |     case OSSL_PARAM_UTF8_PTR:  | 
123  | 0  |         status = OSSL_PARAM_get_utf8_ptr(p, &name);  | 
124  | 0  |         break;  | 
125  | 0  |     }  | 
126  | 0  |     if (status)  | 
127  | 0  |         return ossl_ec_set_check_group_type_from_name(ec, name);  | 
128  | 0  |     return 0;  | 
129  | 0  | }  | 
130  |  |  | 
131  |  | int ossl_ec_pt_format_name2id(const char *name)  | 
132  | 0  | { | 
133  | 0  |     size_t i, sz;  | 
134  |  |  | 
135  |  |     /* Return the default value if there is no name */  | 
136  | 0  |     if (name == NULL)  | 
137  | 0  |         return (int)POINT_CONVERSION_UNCOMPRESSED;  | 
138  |  |  | 
139  | 0  |     for (i = 0, sz = OSSL_NELEM(format_nameid_map); i < sz; i++) { | 
140  | 0  |         if (OPENSSL_strcasecmp(name, format_nameid_map[i].ptr) == 0)  | 
141  | 0  |             return format_nameid_map[i].id;  | 
142  | 0  |     }  | 
143  | 0  |     return -1;  | 
144  | 0  | }  | 
145  |  |  | 
146  |  | char *ossl_ec_pt_format_id2name(int id)  | 
147  | 0  | { | 
148  | 0  |     size_t i, sz;  | 
149  |  | 
  | 
150  | 0  |     for (i = 0, sz = OSSL_NELEM(format_nameid_map); i < sz; i++) { | 
151  | 0  |         if (id == (int)format_nameid_map[i].id)  | 
152  | 0  |             return format_nameid_map[i].ptr;  | 
153  | 0  |     }  | 
154  | 0  |     return NULL;  | 
155  | 0  | }  | 
156  |  |  | 
157  |  | static int ec_group_explicit_todata(const EC_GROUP *group, OSSL_PARAM_BLD *tmpl,  | 
158  |  |                                     OSSL_PARAM params[], BN_CTX *bnctx,  | 
159  |  |                                     unsigned char **genbuf)  | 
160  | 0  | { | 
161  | 0  |     int ret = 0, fid;  | 
162  | 0  |     const char *field_type;  | 
163  | 0  |     const OSSL_PARAM *param = NULL;  | 
164  | 0  |     const OSSL_PARAM *param_p = NULL;  | 
165  | 0  |     const OSSL_PARAM *param_a = NULL;  | 
166  | 0  |     const OSSL_PARAM *param_b = NULL;  | 
167  |  | 
  | 
168  | 0  |     fid = EC_GROUP_get_field_type(group);  | 
169  |  | 
  | 
170  | 0  |     if (fid == NID_X9_62_prime_field) { | 
171  | 0  |         field_type = SN_X9_62_prime_field;  | 
172  | 0  |     } else if (fid == NID_X9_62_characteristic_two_field) { | 
173  |  | #ifdef OPENSSL_NO_EC2M  | 
174  |  |         ERR_raise(ERR_LIB_EC, EC_R_GF2M_NOT_SUPPORTED);  | 
175  |  |         goto err;  | 
176  |  | #else  | 
177  | 0  |         field_type = SN_X9_62_characteristic_two_field;  | 
178  | 0  | #endif  | 
179  | 0  |     } else { | 
180  | 0  |         ERR_raise(ERR_LIB_EC, EC_R_INVALID_FIELD);  | 
181  | 0  |         return 0;  | 
182  | 0  |     }  | 
183  |  |  | 
184  | 0  |     param_p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_P);  | 
185  | 0  |     param_a = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_A);  | 
186  | 0  |     param_b = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_B);  | 
187  | 0  |     if (tmpl != NULL || param_p != NULL || param_a != NULL || param_b != NULL) { | 
188  | 0  |         BIGNUM *p = BN_CTX_get(bnctx);  | 
189  | 0  |         BIGNUM *a = BN_CTX_get(bnctx);  | 
190  | 0  |         BIGNUM *b = BN_CTX_get(bnctx);  | 
191  |  | 
  | 
192  | 0  |         if (b == NULL) { | 
193  | 0  |             ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);  | 
194  | 0  |             goto err;  | 
195  | 0  |         }  | 
196  |  |  | 
197  | 0  |         if (!EC_GROUP_get_curve(group, p, a, b, bnctx)) { | 
198  | 0  |             ERR_raise(ERR_LIB_EC, EC_R_INVALID_CURVE);  | 
199  | 0  |             goto err;  | 
200  | 0  |         }  | 
201  | 0  |         if (!ossl_param_build_set_bn(tmpl, params, OSSL_PKEY_PARAM_EC_P, p)  | 
202  | 0  |             || !ossl_param_build_set_bn(tmpl, params, OSSL_PKEY_PARAM_EC_A, a)  | 
203  | 0  |             || !ossl_param_build_set_bn(tmpl, params, OSSL_PKEY_PARAM_EC_B, b)) { | 
204  | 0  |             ERR_raise(ERR_LIB_EC, ERR_R_CRYPTO_LIB);  | 
205  | 0  |             goto err;  | 
206  | 0  |         }  | 
207  | 0  |     }  | 
208  |  |  | 
209  | 0  |     param = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_ORDER);  | 
210  | 0  |     if (tmpl != NULL || param != NULL) { | 
211  | 0  |         const BIGNUM *order = EC_GROUP_get0_order(group);  | 
212  |  | 
  | 
213  | 0  |         if (order == NULL) { | 
214  | 0  |             ERR_raise(ERR_LIB_EC, EC_R_INVALID_GROUP_ORDER);  | 
215  | 0  |             goto err;  | 
216  | 0  |         }  | 
217  | 0  |         if (!ossl_param_build_set_bn(tmpl, params, OSSL_PKEY_PARAM_EC_ORDER,  | 
218  | 0  |                                     order)) { | 
219  | 0  |             ERR_raise(ERR_LIB_EC, ERR_R_CRYPTO_LIB);  | 
220  | 0  |             goto err;  | 
221  | 0  |         }  | 
222  | 0  |     }  | 
223  |  |  | 
224  | 0  |     param = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_FIELD_TYPE);  | 
225  | 0  |     if (tmpl != NULL || param != NULL) { | 
226  | 0  |         if (!ossl_param_build_set_utf8_string(tmpl, params,  | 
227  | 0  |                                               OSSL_PKEY_PARAM_EC_FIELD_TYPE,  | 
228  | 0  |                                               field_type)) { | 
229  | 0  |             ERR_raise(ERR_LIB_EC, ERR_R_CRYPTO_LIB);  | 
230  | 0  |             goto err;  | 
231  | 0  |         }  | 
232  | 0  |     }  | 
233  |  |  | 
234  | 0  |     param = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_GENERATOR);  | 
235  | 0  |     if (tmpl != NULL || param != NULL) { | 
236  | 0  |         size_t genbuf_len;  | 
237  | 0  |         const EC_POINT *genpt = EC_GROUP_get0_generator(group);  | 
238  | 0  |         point_conversion_form_t genform = EC_GROUP_get_point_conversion_form(group);  | 
239  |  | 
  | 
240  | 0  |         if (genpt == NULL) { | 
241  | 0  |             ERR_raise(ERR_LIB_EC, EC_R_INVALID_GENERATOR);  | 
242  | 0  |             goto err;  | 
243  | 0  |         }  | 
244  | 0  |         genbuf_len = EC_POINT_point2buf(group, genpt, genform, genbuf, bnctx);  | 
245  | 0  |         if (genbuf_len == 0) { | 
246  | 0  |             ERR_raise(ERR_LIB_EC, EC_R_INVALID_GENERATOR);  | 
247  | 0  |             goto err;  | 
248  | 0  |         }  | 
249  | 0  |         if (!ossl_param_build_set_octet_string(tmpl, params,  | 
250  | 0  |                                                OSSL_PKEY_PARAM_EC_GENERATOR,  | 
251  | 0  |                                                *genbuf, genbuf_len)) { | 
252  | 0  |             ERR_raise(ERR_LIB_EC, ERR_R_CRYPTO_LIB);  | 
253  | 0  |             goto err;  | 
254  | 0  |         }  | 
255  | 0  |     }  | 
256  |  |  | 
257  | 0  |     param = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_COFACTOR);  | 
258  | 0  |     if (tmpl != NULL || param != NULL) { | 
259  | 0  |         const BIGNUM *cofactor = EC_GROUP_get0_cofactor(group);  | 
260  |  | 
  | 
261  | 0  |         if (cofactor != NULL  | 
262  | 0  |             && !ossl_param_build_set_bn(tmpl, params,  | 
263  | 0  |                                         OSSL_PKEY_PARAM_EC_COFACTOR, cofactor)) { | 
264  | 0  |             ERR_raise(ERR_LIB_EC, ERR_R_CRYPTO_LIB);  | 
265  | 0  |             goto err;  | 
266  | 0  |         }  | 
267  | 0  |     }  | 
268  |  |  | 
269  | 0  |     param = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_SEED);  | 
270  | 0  |     if (tmpl != NULL || param != NULL) { | 
271  | 0  |         unsigned char *seed = EC_GROUP_get0_seed(group);  | 
272  | 0  |         size_t seed_len = EC_GROUP_get_seed_len(group);  | 
273  |  | 
  | 
274  | 0  |         if (seed != NULL  | 
275  | 0  |             && seed_len > 0  | 
276  | 0  |             && !ossl_param_build_set_octet_string(tmpl, params,  | 
277  | 0  |                                                   OSSL_PKEY_PARAM_EC_SEED,  | 
278  | 0  |                                                   seed, seed_len)) { | 
279  | 0  |             ERR_raise(ERR_LIB_EC, ERR_R_CRYPTO_LIB);  | 
280  | 0  |             goto err;  | 
281  | 0  |         }  | 
282  | 0  |     }  | 
283  | 0  |     ret = 1;  | 
284  | 0  | err:  | 
285  | 0  |     return ret;  | 
286  | 0  | }  | 
287  |  |  | 
288  |  | int ossl_ec_group_todata(const EC_GROUP *group, OSSL_PARAM_BLD *tmpl,  | 
289  |  |                          OSSL_PARAM params[], OSSL_LIB_CTX *libctx,  | 
290  |  |                          const char *propq,  | 
291  |  |                          BN_CTX *bnctx, unsigned char **genbuf)  | 
292  | 0  | { | 
293  | 0  |     int ret = 0, curve_nid, encoding_flag;  | 
294  | 0  |     const char *encoding_name, *pt_form_name;  | 
295  | 0  |     point_conversion_form_t genform;  | 
296  |  | 
  | 
297  | 0  |     if (group == NULL) { | 
298  | 0  |         ERR_raise(ERR_LIB_EC, EC_R_PASSED_NULL_PARAMETER);  | 
299  | 0  |         return 0;  | 
300  | 0  |     }  | 
301  |  |  | 
302  | 0  |     genform = EC_GROUP_get_point_conversion_form(group);  | 
303  | 0  |     pt_form_name = ossl_ec_pt_format_id2name(genform);  | 
304  | 0  |     if (pt_form_name == NULL  | 
305  | 0  |         || !ossl_param_build_set_utf8_string(  | 
306  | 0  |                 tmpl, params,  | 
307  | 0  |                 OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT, pt_form_name)) { | 
308  | 0  |         ERR_raise(ERR_LIB_EC, EC_R_INVALID_FORM);  | 
309  | 0  |         return 0;  | 
310  | 0  |     }  | 
311  | 0  |     encoding_flag = EC_GROUP_get_asn1_flag(group) & OPENSSL_EC_NAMED_CURVE;  | 
312  | 0  |     encoding_name = ec_param_encoding_id2name(encoding_flag);  | 
313  | 0  |     if (encoding_name == NULL  | 
314  | 0  |         || !ossl_param_build_set_utf8_string(tmpl, params,  | 
315  | 0  |                                              OSSL_PKEY_PARAM_EC_ENCODING,  | 
316  | 0  |                                              encoding_name)) { | 
317  | 0  |         ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);  | 
318  | 0  |         return 0;  | 
319  | 0  |     }  | 
320  |  |  | 
321  | 0  |     if (!ossl_param_build_set_int(tmpl, params,  | 
322  | 0  |                                   OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS,  | 
323  | 0  |                                   group->decoded_from_explicit_params))  | 
324  | 0  |         return 0;  | 
325  |  |  | 
326  | 0  |     curve_nid = EC_GROUP_get_curve_name(group);  | 
327  |  |  | 
328  |  |     /*  | 
329  |  |      * Get the explicit parameters in these two cases:  | 
330  |  |      * - We do not have a template, i.e. specific parameters are requested  | 
331  |  |      * - The curve is not a named curve  | 
332  |  |      */  | 
333  | 0  |     if (tmpl == NULL || curve_nid == NID_undef)  | 
334  | 0  |         if (!ec_group_explicit_todata(group, tmpl, params, bnctx, genbuf))  | 
335  | 0  |             goto err;  | 
336  |  |  | 
337  | 0  |     if (curve_nid != NID_undef) { | 
338  |  |         /* Named curve */  | 
339  | 0  |         const char *curve_name = OSSL_EC_curve_nid2name(curve_nid);  | 
340  |  | 
  | 
341  | 0  |         if (curve_name == NULL  | 
342  | 0  |             || !ossl_param_build_set_utf8_string(tmpl, params,  | 
343  | 0  |                                                  OSSL_PKEY_PARAM_GROUP_NAME,  | 
344  | 0  |                                                  curve_name)) { | 
345  | 0  |             ERR_raise(ERR_LIB_EC, EC_R_INVALID_CURVE);  | 
346  | 0  |             goto err;  | 
347  | 0  |         }  | 
348  | 0  |     }  | 
349  | 0  |     ret = 1;  | 
350  | 0  | err:  | 
351  | 0  |     return ret;  | 
352  | 0  | }  | 
353  |  |  | 
354  |  | /*  | 
355  |  |  * The intention with the "backend" source file is to offer backend support  | 
356  |  |  * for legacy backends (EVP_PKEY_ASN1_METHOD and EVP_PKEY_METHOD) and provider  | 
357  |  |  * implementations alike.  | 
358  |  |  */  | 
359  |  | int ossl_ec_set_ecdh_cofactor_mode(EC_KEY *ec, int mode)  | 
360  | 0  | { | 
361  | 0  |     const EC_GROUP *ecg = EC_KEY_get0_group(ec);  | 
362  | 0  |     const BIGNUM *cofactor;  | 
363  |  |     /*  | 
364  |  |      * mode can be only 0 for disable, or 1 for enable here.  | 
365  |  |      *  | 
366  |  |      * This is in contrast with the same parameter on an ECDH EVP_PKEY_CTX that  | 
367  |  |      * also supports mode == -1 with the meaning of "reset to the default for  | 
368  |  |      * the associated key".  | 
369  |  |      */  | 
370  | 0  |     if (mode < 0 || mode > 1)  | 
371  | 0  |         return 0;  | 
372  |  |  | 
373  | 0  |     if ((cofactor = EC_GROUP_get0_cofactor(ecg)) == NULL )  | 
374  | 0  |         return 0;  | 
375  |  |  | 
376  |  |     /* ECDH cofactor mode has no effect if cofactor is 1 */  | 
377  | 0  |     if (BN_is_one(cofactor))  | 
378  | 0  |         return 1;  | 
379  |  |  | 
380  | 0  |     if (mode == 1)  | 
381  | 0  |         EC_KEY_set_flags(ec, EC_FLAG_COFACTOR_ECDH);  | 
382  | 0  |     else if (mode == 0)  | 
383  | 0  |         EC_KEY_clear_flags(ec, EC_FLAG_COFACTOR_ECDH);  | 
384  |  | 
  | 
385  | 0  |     return 1;  | 
386  | 0  | }  | 
387  |  |  | 
388  |  | /*  | 
389  |  |  * Callers of ossl_ec_key_fromdata MUST make sure that ec_key_params_fromdata has  | 
390  |  |  * been called before!  | 
391  |  |  *  | 
392  |  |  * This function only gets the bare keypair, domain parameters and other  | 
393  |  |  * parameters are treated separately, and domain parameters are required to  | 
394  |  |  * define a keypair.  | 
395  |  |  */  | 
396  |  | int ossl_ec_key_fromdata(EC_KEY *ec, const OSSL_PARAM params[], int include_private)  | 
397  | 0  | { | 
398  | 0  |     const OSSL_PARAM *param_priv_key = NULL, *param_pub_key = NULL;  | 
399  | 0  |     BN_CTX *ctx = NULL;  | 
400  | 0  |     BIGNUM *priv_key = NULL;  | 
401  | 0  |     unsigned char *pub_key = NULL;  | 
402  | 0  |     size_t pub_key_len;  | 
403  | 0  |     const EC_GROUP *ecg = NULL;  | 
404  | 0  |     EC_POINT *pub_point = NULL;  | 
405  | 0  |     int ok = 0;  | 
406  |  | 
  | 
407  | 0  |     ecg = EC_KEY_get0_group(ec);  | 
408  | 0  |     if (ecg == NULL)  | 
409  | 0  |         return 0;  | 
410  |  |  | 
411  | 0  |     param_pub_key =  | 
412  | 0  |         OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY);  | 
413  | 0  |     if (include_private)  | 
414  | 0  |         param_priv_key =  | 
415  | 0  |             OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);  | 
416  |  | 
  | 
417  | 0  |     ctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(ec));  | 
418  | 0  |     if (ctx == NULL)  | 
419  | 0  |         goto err;  | 
420  |  |  | 
421  | 0  |     if (param_pub_key != NULL)  | 
422  | 0  |         if (!OSSL_PARAM_get_octet_string(param_pub_key,  | 
423  | 0  |                                          (void **)&pub_key, 0, &pub_key_len)  | 
424  | 0  |             || (pub_point = EC_POINT_new(ecg)) == NULL  | 
425  | 0  |             || !EC_POINT_oct2point(ecg, pub_point, pub_key, pub_key_len, ctx))  | 
426  | 0  |         goto err;  | 
427  |  |  | 
428  | 0  |     if (param_priv_key != NULL && include_private) { | 
429  | 0  |         int fixed_words;  | 
430  | 0  |         const BIGNUM *order;  | 
431  |  |  | 
432  |  |         /*  | 
433  |  |          * Key import/export should never leak the bit length of the secret  | 
434  |  |          * scalar in the key.  | 
435  |  |          *  | 
436  |  |          * For this reason, on export we use padded BIGNUMs with fixed length.  | 
437  |  |          *  | 
438  |  |          * When importing we also should make sure that, even if short lived,  | 
439  |  |          * the newly created BIGNUM is marked with the BN_FLG_CONSTTIME flag as  | 
440  |  |          * soon as possible, so that any processing of this BIGNUM might opt for  | 
441  |  |          * constant time implementations in the backend.  | 
442  |  |          *  | 
443  |  |          * Setting the BN_FLG_CONSTTIME flag alone is never enough, we also have  | 
444  |  |          * to preallocate the BIGNUM internal buffer to a fixed public size big  | 
445  |  |          * enough that operations performed during the processing never trigger  | 
446  |  |          * a realloc which would leak the size of the scalar through memory  | 
447  |  |          * accesses.  | 
448  |  |          *  | 
449  |  |          * Fixed Length  | 
450  |  |          * ------------  | 
451  |  |          *  | 
452  |  |          * The order of the large prime subgroup of the curve is our choice for  | 
453  |  |          * a fixed public size, as that is generally the upper bound for  | 
454  |  |          * generating a private key in EC cryptosystems and should fit all valid  | 
455  |  |          * secret scalars.  | 
456  |  |          *  | 
457  |  |          * For padding on export we just use the bit length of the order  | 
458  |  |          * converted to bytes (rounding up).  | 
459  |  |          *  | 
460  |  |          * For preallocating the BIGNUM storage we look at the number of "words"  | 
461  |  |          * required for the internal representation of the order, and we  | 
462  |  |          * preallocate 2 extra "words" in case any of the subsequent processing  | 
463  |  |          * might temporarily overflow the order length.  | 
464  |  |          */  | 
465  | 0  |         order = EC_GROUP_get0_order(ecg);  | 
466  | 0  |         if (order == NULL || BN_is_zero(order))  | 
467  | 0  |             goto err;  | 
468  |  |  | 
469  | 0  |         fixed_words = bn_get_top(order) + 2;  | 
470  |  | 
  | 
471  | 0  |         if ((priv_key = BN_secure_new()) == NULL)  | 
472  | 0  |             goto err;  | 
473  | 0  |         if (bn_wexpand(priv_key, fixed_words) == NULL)  | 
474  | 0  |             goto err;  | 
475  | 0  |         BN_set_flags(priv_key, BN_FLG_CONSTTIME);  | 
476  |  | 
  | 
477  | 0  |         if (!OSSL_PARAM_get_BN(param_priv_key, &priv_key))  | 
478  | 0  |             goto err;  | 
479  | 0  |     }  | 
480  |  |  | 
481  | 0  |     if (priv_key != NULL  | 
482  | 0  |         && !EC_KEY_set_private_key(ec, priv_key))  | 
483  | 0  |         goto err;  | 
484  |  |  | 
485  | 0  |     if (pub_point != NULL  | 
486  | 0  |         && !EC_KEY_set_public_key(ec, pub_point))  | 
487  | 0  |         goto err;  | 
488  |  |  | 
489  | 0  |     ok = 1;  | 
490  |  | 
  | 
491  | 0  |  err:  | 
492  | 0  |     BN_CTX_free(ctx);  | 
493  | 0  |     BN_clear_free(priv_key);  | 
494  | 0  |     OPENSSL_free(pub_key);  | 
495  | 0  |     EC_POINT_free(pub_point);  | 
496  | 0  |     return ok;  | 
497  | 0  | }  | 
498  |  |  | 
499  |  | int ossl_ec_group_fromdata(EC_KEY *ec, const OSSL_PARAM params[])  | 
500  | 0  | { | 
501  | 0  |     int ok = 0;  | 
502  | 0  |     EC_GROUP *group = NULL;  | 
503  |  | 
  | 
504  | 0  |     if (ec == NULL)  | 
505  | 0  |         return 0;  | 
506  |  |  | 
507  | 0  |      group = EC_GROUP_new_from_params(params, ossl_ec_key_get_libctx(ec),  | 
508  | 0  |                                       ossl_ec_key_get0_propq(ec));  | 
509  |  | 
  | 
510  | 0  |     if (!EC_KEY_set_group(ec, group))  | 
511  | 0  |         goto err;  | 
512  | 0  |     ok = 1;  | 
513  | 0  | err:  | 
514  | 0  |     EC_GROUP_free(group);  | 
515  | 0  |     return ok;  | 
516  | 0  | }  | 
517  |  |  | 
518  |  | static int ec_key_point_format_fromdata(EC_KEY *ec, const OSSL_PARAM params[])  | 
519  | 0  | { | 
520  | 0  |     const OSSL_PARAM *p;  | 
521  | 0  |     int format = -1;  | 
522  |  | 
  | 
523  | 0  |     p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT);  | 
524  | 0  |     if (p != NULL) { | 
525  | 0  |         if (!ossl_ec_pt_format_param2id(p, &format)) { | 
526  | 0  |             ERR_raise(ERR_LIB_EC, EC_R_INVALID_FORM);  | 
527  | 0  |             return 0;  | 
528  | 0  |         }  | 
529  | 0  |         EC_KEY_set_conv_form(ec, format);  | 
530  | 0  |     }  | 
531  | 0  |     return 1;  | 
532  | 0  | }  | 
533  |  |  | 
534  |  | static int ec_key_group_check_fromdata(EC_KEY *ec, const OSSL_PARAM params[])  | 
535  | 0  | { | 
536  | 0  |     const OSSL_PARAM *p;  | 
537  |  | 
  | 
538  | 0  |     p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_GROUP_CHECK_TYPE);  | 
539  | 0  |     if (p != NULL)  | 
540  | 0  |         return ec_set_check_group_type_from_param(ec, p);  | 
541  | 0  |     return 1;  | 
542  | 0  | }  | 
543  |  |  | 
544  |  | static int ec_set_include_public(EC_KEY *ec, int include)  | 
545  | 0  | { | 
546  | 0  |     int flags = EC_KEY_get_enc_flags(ec);  | 
547  |  | 
  | 
548  | 0  |     if (!include)  | 
549  | 0  |         flags |= EC_PKEY_NO_PUBKEY;  | 
550  | 0  |     else  | 
551  | 0  |         flags &= ~EC_PKEY_NO_PUBKEY;  | 
552  | 0  |     EC_KEY_set_enc_flags(ec, flags);  | 
553  | 0  |     return 1;  | 
554  | 0  | }  | 
555  |  |  | 
556  |  | int ossl_ec_key_otherparams_fromdata(EC_KEY *ec, const OSSL_PARAM params[])  | 
557  | 0  | { | 
558  | 0  |     const OSSL_PARAM *p;  | 
559  |  | 
  | 
560  | 0  |     if (ec == NULL)  | 
561  | 0  |         return 0;  | 
562  |  |  | 
563  | 0  |     p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH);  | 
564  | 0  |     if (p != NULL) { | 
565  | 0  |         int mode;  | 
566  |  | 
  | 
567  | 0  |         if (!OSSL_PARAM_get_int(p, &mode)  | 
568  | 0  |             || !ossl_ec_set_ecdh_cofactor_mode(ec, mode))  | 
569  | 0  |             return 0;  | 
570  | 0  |     }  | 
571  |  |  | 
572  | 0  |     p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_INCLUDE_PUBLIC);  | 
573  | 0  |     if (p != NULL) { | 
574  | 0  |         int include = 1;  | 
575  |  | 
  | 
576  | 0  |         if (!OSSL_PARAM_get_int(p, &include)  | 
577  | 0  |             || !ec_set_include_public(ec, include))  | 
578  | 0  |             return 0;  | 
579  | 0  |     }  | 
580  | 0  |     if (!ec_key_point_format_fromdata(ec, params))  | 
581  | 0  |         return 0;  | 
582  | 0  |     if (!ec_key_group_check_fromdata(ec, params))  | 
583  | 0  |         return 0;  | 
584  | 0  |     return 1;  | 
585  | 0  | }  | 
586  |  |  | 
587  |  | int ossl_ec_key_is_foreign(const EC_KEY *ec)  | 
588  | 0  | { | 
589  | 0  | #ifndef FIPS_MODULE  | 
590  | 0  |     if (ec->engine != NULL || EC_KEY_get_method(ec) != EC_KEY_OpenSSL())  | 
591  | 0  |         return 1;  | 
592  | 0  | #endif  | 
593  | 0  |     return 0;  | 
594  |  | 
  | 
595  | 0  | }  | 
596  |  |  | 
597  |  | EC_KEY *ossl_ec_key_dup(const EC_KEY *src, int selection)  | 
598  | 0  | { | 
599  | 0  |     EC_KEY *ret;  | 
600  |  | 
  | 
601  | 0  |     if (src == NULL) { | 
602  | 0  |         ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);  | 
603  | 0  |         return NULL;  | 
604  | 0  |     }  | 
605  |  |  | 
606  | 0  |     if ((ret = ossl_ec_key_new_method_int(src->libctx, src->propq,  | 
607  | 0  |                                           src->engine)) == NULL)  | 
608  | 0  |         return NULL;  | 
609  |  |  | 
610  |  |     /* copy the parameters */  | 
611  | 0  |     if (src->group != NULL  | 
612  | 0  |         && (selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) { | 
613  | 0  |         ret->group = ossl_ec_group_new_ex(src->libctx, src->propq,  | 
614  | 0  |                                           src->group->meth);  | 
615  | 0  |         if (ret->group == NULL  | 
616  | 0  |             || !EC_GROUP_copy(ret->group, src->group))  | 
617  | 0  |             goto err;  | 
618  |  |  | 
619  | 0  |         if (src->meth != NULL)  | 
620  | 0  |             ret->meth = src->meth;  | 
621  | 0  |     }  | 
622  |  |  | 
623  |  |     /*  copy the public key */  | 
624  | 0  |     if (src->pub_key != NULL  | 
625  | 0  |         && (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) { | 
626  | 0  |         if (ret->group == NULL)  | 
627  |  |             /* no parameter-less keys allowed */  | 
628  | 0  |             goto err;  | 
629  | 0  |         ret->pub_key = EC_POINT_new(ret->group);  | 
630  | 0  |         if (ret->pub_key == NULL  | 
631  | 0  |             || !EC_POINT_copy(ret->pub_key, src->pub_key))  | 
632  | 0  |                 goto err;  | 
633  | 0  |     }  | 
634  |  |  | 
635  |  |     /* copy the private key */  | 
636  | 0  |     if (src->priv_key != NULL  | 
637  | 0  |         && (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) { | 
638  | 0  |         if (ret->group == NULL)  | 
639  |  |             /* no parameter-less keys allowed */  | 
640  | 0  |             goto err;  | 
641  | 0  |         ret->priv_key = BN_new();  | 
642  | 0  |         if (ret->priv_key == NULL || !BN_copy(ret->priv_key, src->priv_key))  | 
643  | 0  |             goto err;  | 
644  | 0  |         if (ret->group->meth->keycopy  | 
645  | 0  |             && ret->group->meth->keycopy(ret, src) == 0)  | 
646  | 0  |             goto err;  | 
647  | 0  |     }  | 
648  |  |  | 
649  |  |     /* copy the rest */  | 
650  | 0  |     if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0) { | 
651  | 0  |         ret->enc_flag = src->enc_flag;  | 
652  | 0  |         ret->conv_form = src->conv_form;  | 
653  | 0  |     }  | 
654  |  | 
  | 
655  | 0  |     ret->version = src->version;  | 
656  | 0  |     ret->flags = src->flags;  | 
657  |  | 
  | 
658  | 0  | #ifndef FIPS_MODULE  | 
659  | 0  |     if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_EC_KEY,  | 
660  | 0  |                             &ret->ex_data, &src->ex_data))  | 
661  | 0  |         goto err;  | 
662  | 0  | #endif  | 
663  |  |  | 
664  | 0  |     if (ret->meth != NULL && ret->meth->copy != NULL) { | 
665  | 0  |         if ((selection  | 
666  | 0  |              & OSSL_KEYMGMT_SELECT_KEYPAIR) != OSSL_KEYMGMT_SELECT_KEYPAIR)  | 
667  | 0  |             goto err;  | 
668  | 0  |         if (ret->meth->copy(ret, src) == 0)  | 
669  | 0  |             goto err;  | 
670  | 0  |     }  | 
671  |  |  | 
672  | 0  |     return ret;  | 
673  | 0  |  err:  | 
674  | 0  |     EC_KEY_free(ret);  | 
675  | 0  |     return NULL;  | 
676  | 0  | }  | 
677  |  |  | 
678  |  | int ossl_ec_encoding_param2id(const OSSL_PARAM *p, int *id)  | 
679  | 0  | { | 
680  | 0  |     const char *name = NULL;  | 
681  | 0  |     int status = 0;  | 
682  |  | 
  | 
683  | 0  |     switch (p->data_type) { | 
684  | 0  |     case OSSL_PARAM_UTF8_STRING:  | 
685  |  |         /* The OSSL_PARAM functions have no support for this */  | 
686  | 0  |         name = p->data;  | 
687  | 0  |         status = (name != NULL);  | 
688  | 0  |         break;  | 
689  | 0  |     case OSSL_PARAM_UTF8_PTR:  | 
690  | 0  |         status = OSSL_PARAM_get_utf8_ptr(p, &name);  | 
691  | 0  |         break;  | 
692  | 0  |     }  | 
693  | 0  |     if (status) { | 
694  | 0  |         int i = ossl_ec_encoding_name2id(name);  | 
695  |  | 
  | 
696  | 0  |         if (i >= 0) { | 
697  | 0  |             *id = i;  | 
698  | 0  |             return 1;  | 
699  | 0  |         }  | 
700  | 0  |     }  | 
701  | 0  |     return 0;  | 
702  | 0  | }  | 
703  |  |  | 
704  |  | int ossl_ec_pt_format_param2id(const OSSL_PARAM *p, int *id)  | 
705  | 0  | { | 
706  | 0  |     const char *name = NULL;  | 
707  | 0  |     int status = 0;  | 
708  |  | 
  | 
709  | 0  |     switch (p->data_type) { | 
710  | 0  |     case OSSL_PARAM_UTF8_STRING:  | 
711  |  |         /* The OSSL_PARAM functions have no support for this */  | 
712  | 0  |         name = p->data;  | 
713  | 0  |         status = (name != NULL);  | 
714  | 0  |         break;  | 
715  | 0  |     case OSSL_PARAM_UTF8_PTR:  | 
716  | 0  |         status = OSSL_PARAM_get_utf8_ptr(p, &name);  | 
717  | 0  |         break;  | 
718  | 0  |     }  | 
719  | 0  |     if (status) { | 
720  | 0  |         int i = ossl_ec_pt_format_name2id(name);  | 
721  |  | 
  | 
722  | 0  |         if (i >= 0) { | 
723  | 0  |             *id = i;  | 
724  | 0  |             return 1;  | 
725  | 0  |         }  | 
726  | 0  |     }  | 
727  | 0  |     return 0;  | 
728  | 0  | }  | 
729  |  |  | 
730  |  | #ifndef FIPS_MODULE  | 
731  |  | int ossl_x509_algor_is_sm2(const X509_ALGOR *palg)  | 
732  | 0  | { | 
733  | 0  |     int ptype = 0;  | 
734  | 0  |     const void *pval = NULL;  | 
735  |  | 
  | 
736  | 0  |     X509_ALGOR_get0(NULL, &ptype, &pval, palg);  | 
737  |  | 
  | 
738  | 0  |     if (ptype == V_ASN1_OBJECT)  | 
739  | 0  |         return OBJ_obj2nid((ASN1_OBJECT *)pval) == NID_sm2;  | 
740  |  |  | 
741  | 0  |     if (ptype == V_ASN1_SEQUENCE) { | 
742  | 0  |         const ASN1_STRING *str = pval;  | 
743  | 0  |         const unsigned char *der = str->data;  | 
744  | 0  |         int derlen = str->length;  | 
745  | 0  |         EC_GROUP *group;  | 
746  | 0  |         int ret;  | 
747  |  | 
  | 
748  | 0  |         if ((group = d2i_ECPKParameters(NULL, &der, derlen)) == NULL)  | 
749  | 0  |             ret = 0;  | 
750  | 0  |         else  | 
751  | 0  |             ret = (EC_GROUP_get_curve_name(group) == NID_sm2);  | 
752  |  | 
  | 
753  | 0  |         EC_GROUP_free(group);  | 
754  | 0  |         return ret;  | 
755  | 0  |     }  | 
756  |  |  | 
757  | 0  |     return 0;  | 
758  | 0  | }  | 
759  |  |  | 
760  |  | EC_KEY *ossl_ec_key_param_from_x509_algor(const X509_ALGOR *palg,  | 
761  |  |                                      OSSL_LIB_CTX *libctx, const char *propq)  | 
762  | 0  | { | 
763  | 0  |     int ptype = 0;  | 
764  | 0  |     const void *pval = NULL;  | 
765  | 0  |     EC_KEY *eckey = NULL;  | 
766  | 0  |     EC_GROUP *group = NULL;  | 
767  |  | 
  | 
768  | 0  |     X509_ALGOR_get0(NULL, &ptype, &pval, palg);  | 
769  | 0  |     if ((eckey = EC_KEY_new_ex(libctx, propq)) == NULL) { | 
770  | 0  |         ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);  | 
771  | 0  |         goto ecerr;  | 
772  | 0  |     }  | 
773  |  |  | 
774  | 0  |     if (ptype == V_ASN1_SEQUENCE) { | 
775  | 0  |         const ASN1_STRING *pstr = pval;  | 
776  | 0  |         const unsigned char *pm = pstr->data;  | 
777  | 0  |         int pmlen = pstr->length;  | 
778  |  |  | 
779  |  | 
  | 
780  | 0  |         if (d2i_ECParameters(&eckey, &pm, pmlen) == NULL) { | 
781  | 0  |             ERR_raise(ERR_LIB_EC, EC_R_DECODE_ERROR);  | 
782  | 0  |             goto ecerr;  | 
783  | 0  |         }  | 
784  | 0  |     } else if (ptype == V_ASN1_OBJECT) { | 
785  | 0  |         const ASN1_OBJECT *poid = pval;  | 
786  |  |  | 
787  |  |         /*  | 
788  |  |          * type == V_ASN1_OBJECT => the parameters are given by an asn1 OID  | 
789  |  |          */  | 
790  |  | 
  | 
791  | 0  |         group = EC_GROUP_new_by_curve_name_ex(libctx, propq, OBJ_obj2nid(poid));  | 
792  | 0  |         if (group == NULL)  | 
793  | 0  |             goto ecerr;  | 
794  | 0  |         EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);  | 
795  | 0  |         if (EC_KEY_set_group(eckey, group) == 0)  | 
796  | 0  |             goto ecerr;  | 
797  | 0  |         EC_GROUP_free(group);  | 
798  | 0  |     } else { | 
799  | 0  |         ERR_raise(ERR_LIB_EC, EC_R_DECODE_ERROR);  | 
800  | 0  |         goto ecerr;  | 
801  | 0  |     }  | 
802  |  |  | 
803  | 0  |     return eckey;  | 
804  |  |  | 
805  | 0  |  ecerr:  | 
806  | 0  |     EC_KEY_free(eckey);  | 
807  | 0  |     EC_GROUP_free(group);  | 
808  | 0  |     return NULL;  | 
809  | 0  | }  | 
810  |  |  | 
811  |  | EC_KEY *ossl_ec_key_from_pkcs8(const PKCS8_PRIV_KEY_INFO *p8inf,  | 
812  |  |                                OSSL_LIB_CTX *libctx, const char *propq)  | 
813  | 0  | { | 
814  | 0  |     const unsigned char *p = NULL;  | 
815  | 0  |     int pklen;  | 
816  | 0  |     EC_KEY *eckey = NULL;  | 
817  | 0  |     const X509_ALGOR *palg;  | 
818  |  | 
  | 
819  | 0  |     if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8inf))  | 
820  | 0  |         return 0;  | 
821  | 0  |     eckey = ossl_ec_key_param_from_x509_algor(palg, libctx, propq);  | 
822  | 0  |     if (eckey == NULL)  | 
823  | 0  |         goto err;  | 
824  |  |  | 
825  |  |     /* We have parameters now set private key */  | 
826  | 0  |     if (!d2i_ECPrivateKey(&eckey, &p, pklen)) { | 
827  | 0  |         ERR_raise(ERR_LIB_EC, EC_R_DECODE_ERROR);  | 
828  | 0  |         goto err;  | 
829  | 0  |     }  | 
830  |  |  | 
831  | 0  |     return eckey;  | 
832  | 0  |  err:  | 
833  | 0  |     EC_KEY_free(eckey);  | 
834  | 0  |     return NULL;  | 
835  | 0  | }  | 
836  |  | #endif  |