2 * libwebsockets - generic EC api hiding the backend - common parts
4 * Copyright (C) 2017 - 2019 Andy Green <andy@warmcat.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation:
9 * version 2.1 of the License.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21 * lws_genec provides an EC abstraction api in lws that works the
22 * same whether you are using openssl or mbedtls crypto functions underneath.
24 #include "private-lib-core.h"
26 const struct lws_ec_curves *
27 lws_genec_curve(const struct lws_ec_curves *table, const char *name)
29 const struct lws_ec_curves *c = lws_ec_curves;
35 if (!strcmp(name, c->name))
43 //extern const struct lws_ec_curves *lws_ec_curves;
46 lws_genec_confirm_curve_allowed_by_tls_id(const char *allowed, int id,
49 struct lws_tokenize ts;
53 lws_tokenize_init(&ts, allowed, LWS_TOKENIZE_F_COMMA_SEP_LIST |
54 LWS_TOKENIZE_F_MINUS_NONTERM);
55 ts.len = strlen(allowed);
57 e = lws_tokenize(&ts);
61 while (lws_ec_curves[n].name) {
62 if (id != lws_ec_curves[n].tls_lib_nid) {
66 lwsl_info("match curve %s\n",
67 lws_ec_curves[n].name);
68 len = strlen(lws_ec_curves[n].name);
69 jwk->e[LWS_GENCRYPTO_EC_KEYEL_CRV].len = len;
70 jwk->e[LWS_GENCRYPTO_EC_KEYEL_CRV].buf =
71 lws_malloc(len + 1, "cert crv");
72 if (!jwk->e[LWS_GENCRYPTO_EC_KEYEL_CRV].buf) {
73 lwsl_err("%s: OOM\n", __func__);
76 memcpy(jwk->e[LWS_GENCRYPTO_EC_KEYEL_CRV].buf,
77 lws_ec_curves[n].name, len + 1);
82 case LWS_TOKZE_DELIMITER:
85 default: /* includes ENDED */
86 lwsl_err("%s: malformed or curve name in list\n",
93 lwsl_err("%s: unsupported curve group nid %d\n", __func__, n);
99 lws_genec_destroy_elements(struct lws_gencrypto_keyelem *el)
103 for (n = 0; n < LWS_GENCRYPTO_EC_KEYEL_COUNT; n++)
105 lws_free_set_NULL(el[n].buf);
108 static const char *enames[] = { "crv", "x", "d", "y" };
111 lws_genec_dump(struct lws_gencrypto_keyelem *el)
117 lwsl_info(" genec %p: crv: '%s'\n", el,
118 !!el[LWS_GENCRYPTO_EC_KEYEL_CRV].buf ?
119 (char *)el[LWS_GENCRYPTO_EC_KEYEL_CRV].buf: "no curve name");
121 for (n = LWS_GENCRYPTO_EC_KEYEL_X; n < LWS_GENCRYPTO_EC_KEYEL_COUNT;
123 lwsl_info(" e: %s\n", enames[n]);
124 lwsl_hexdump_info(el[n].buf, el[n].len);