1d4afb5ceSopenharmony_ci /* 2d4afb5ceSopenharmony_ci * libwebsockets - small server side websockets and web server implementation 3d4afb5ceSopenharmony_ci * 4d4afb5ceSopenharmony_ci * Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com> 5d4afb5ceSopenharmony_ci * 6d4afb5ceSopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a copy 7d4afb5ceSopenharmony_ci * of this software and associated documentation files (the "Software"), to 8d4afb5ceSopenharmony_ci * deal in the Software without restriction, including without limitation the 9d4afb5ceSopenharmony_ci * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 10d4afb5ceSopenharmony_ci * sell copies of the Software, and to permit persons to whom the Software is 11d4afb5ceSopenharmony_ci * furnished to do so, subject to the following conditions: 12d4afb5ceSopenharmony_ci * 13d4afb5ceSopenharmony_ci * The above copyright notice and this permission notice shall be included in 14d4afb5ceSopenharmony_ci * all copies or substantial portions of the Software. 15d4afb5ceSopenharmony_ci * 16d4afb5ceSopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17d4afb5ceSopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18d4afb5ceSopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19d4afb5ceSopenharmony_ci * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20d4afb5ceSopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21d4afb5ceSopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 22d4afb5ceSopenharmony_ci * IN THE SOFTWARE. 23d4afb5ceSopenharmony_ci * 24d4afb5ceSopenharmony_ci * lws_genec provides an EC abstraction api in lws that works the 25d4afb5ceSopenharmony_ci * same whether you are using openssl or mbedtls crypto functions underneath. 26d4afb5ceSopenharmony_ci */ 27d4afb5ceSopenharmony_ci#include "private-lib-core.h" 28d4afb5ceSopenharmony_ci 29d4afb5ceSopenharmony_ciconst struct lws_ec_curves * 30d4afb5ceSopenharmony_cilws_genec_curve(const struct lws_ec_curves *table, const char *name) 31d4afb5ceSopenharmony_ci{ 32d4afb5ceSopenharmony_ci const struct lws_ec_curves *c = lws_ec_curves; 33d4afb5ceSopenharmony_ci 34d4afb5ceSopenharmony_ci if (table) 35d4afb5ceSopenharmony_ci c = table; 36d4afb5ceSopenharmony_ci 37d4afb5ceSopenharmony_ci while (c->name) { 38d4afb5ceSopenharmony_ci if (!strcmp(name, c->name)) 39d4afb5ceSopenharmony_ci return c; 40d4afb5ceSopenharmony_ci c++; 41d4afb5ceSopenharmony_ci } 42d4afb5ceSopenharmony_ci 43d4afb5ceSopenharmony_ci return NULL; 44d4afb5ceSopenharmony_ci} 45d4afb5ceSopenharmony_ci 46d4afb5ceSopenharmony_ci//extern const struct lws_ec_curves *lws_ec_curves; 47d4afb5ceSopenharmony_ci 48d4afb5ceSopenharmony_ciint 49d4afb5ceSopenharmony_cilws_genec_confirm_curve_allowed_by_tls_id(const char *allowed, int id, 50d4afb5ceSopenharmony_ci struct lws_jwk *jwk) 51d4afb5ceSopenharmony_ci{ 52d4afb5ceSopenharmony_ci struct lws_tokenize ts; 53d4afb5ceSopenharmony_ci lws_tokenize_elem e; 54d4afb5ceSopenharmony_ci size_t len; 55d4afb5ceSopenharmony_ci int n; 56d4afb5ceSopenharmony_ci 57d4afb5ceSopenharmony_ci lws_tokenize_init(&ts, allowed, LWS_TOKENIZE_F_COMMA_SEP_LIST | 58d4afb5ceSopenharmony_ci LWS_TOKENIZE_F_MINUS_NONTERM); 59d4afb5ceSopenharmony_ci ts.len = strlen(allowed); 60d4afb5ceSopenharmony_ci do { 61d4afb5ceSopenharmony_ci e = lws_tokenize(&ts); 62d4afb5ceSopenharmony_ci switch (e) { 63d4afb5ceSopenharmony_ci case LWS_TOKZE_TOKEN: 64d4afb5ceSopenharmony_ci n = 0; 65d4afb5ceSopenharmony_ci while (lws_ec_curves[n].name) { 66d4afb5ceSopenharmony_ci if (id != lws_ec_curves[n].tls_lib_nid) { 67d4afb5ceSopenharmony_ci n++; 68d4afb5ceSopenharmony_ci continue; 69d4afb5ceSopenharmony_ci } 70d4afb5ceSopenharmony_ci lwsl_info("match curve %s\n", 71d4afb5ceSopenharmony_ci lws_ec_curves[n].name); 72d4afb5ceSopenharmony_ci len = strlen(lws_ec_curves[n].name); 73d4afb5ceSopenharmony_ci jwk->e[LWS_GENCRYPTO_EC_KEYEL_CRV].len = (uint32_t)len; 74d4afb5ceSopenharmony_ci jwk->e[LWS_GENCRYPTO_EC_KEYEL_CRV].buf = 75d4afb5ceSopenharmony_ci lws_malloc(len + 1, "cert crv"); 76d4afb5ceSopenharmony_ci if (!jwk->e[LWS_GENCRYPTO_EC_KEYEL_CRV].buf) { 77d4afb5ceSopenharmony_ci lwsl_err("%s: OOM\n", __func__); 78d4afb5ceSopenharmony_ci return 1; 79d4afb5ceSopenharmony_ci } 80d4afb5ceSopenharmony_ci memcpy(jwk->e[LWS_GENCRYPTO_EC_KEYEL_CRV].buf, 81d4afb5ceSopenharmony_ci lws_ec_curves[n].name, len + 1); 82d4afb5ceSopenharmony_ci return 0; 83d4afb5ceSopenharmony_ci } 84d4afb5ceSopenharmony_ci break; 85d4afb5ceSopenharmony_ci 86d4afb5ceSopenharmony_ci case LWS_TOKZE_DELIMITER: 87d4afb5ceSopenharmony_ci break; 88d4afb5ceSopenharmony_ci 89d4afb5ceSopenharmony_ci default: /* includes ENDED */ 90d4afb5ceSopenharmony_ci lwsl_err("%s: malformed or curve name in list\n", 91d4afb5ceSopenharmony_ci __func__); 92d4afb5ceSopenharmony_ci 93d4afb5ceSopenharmony_ci return -1; 94d4afb5ceSopenharmony_ci } 95d4afb5ceSopenharmony_ci } while (e > 0); 96d4afb5ceSopenharmony_ci 97d4afb5ceSopenharmony_ci lwsl_err("%s: unsupported curve group nid %d\n", __func__, id); 98d4afb5ceSopenharmony_ci 99d4afb5ceSopenharmony_ci return -1; 100d4afb5ceSopenharmony_ci} 101d4afb5ceSopenharmony_ci 102d4afb5ceSopenharmony_civoid 103d4afb5ceSopenharmony_cilws_genec_destroy_elements(struct lws_gencrypto_keyelem *el) 104d4afb5ceSopenharmony_ci{ 105d4afb5ceSopenharmony_ci int n; 106d4afb5ceSopenharmony_ci 107d4afb5ceSopenharmony_ci for (n = 0; n < LWS_GENCRYPTO_EC_KEYEL_COUNT; n++) 108d4afb5ceSopenharmony_ci if (el[n].buf) 109d4afb5ceSopenharmony_ci lws_free_set_NULL(el[n].buf); 110d4afb5ceSopenharmony_ci} 111d4afb5ceSopenharmony_ci 112d4afb5ceSopenharmony_cistatic const char *enames[] = { "crv", "x", "d", "y" }; 113d4afb5ceSopenharmony_ci 114d4afb5ceSopenharmony_ciint 115d4afb5ceSopenharmony_cilws_genec_dump(struct lws_gencrypto_keyelem *el) 116d4afb5ceSopenharmony_ci{ 117d4afb5ceSopenharmony_ci int n; 118d4afb5ceSopenharmony_ci 119d4afb5ceSopenharmony_ci (void)enames; 120d4afb5ceSopenharmony_ci 121d4afb5ceSopenharmony_ci lwsl_info(" genec %p: crv: '%s'\n", el, 122d4afb5ceSopenharmony_ci !!el[LWS_GENCRYPTO_EC_KEYEL_CRV].buf ? 123d4afb5ceSopenharmony_ci (char *)el[LWS_GENCRYPTO_EC_KEYEL_CRV].buf: "no curve name"); 124d4afb5ceSopenharmony_ci 125d4afb5ceSopenharmony_ci for (n = LWS_GENCRYPTO_EC_KEYEL_X; n < LWS_GENCRYPTO_EC_KEYEL_COUNT; 126d4afb5ceSopenharmony_ci n++) { 127d4afb5ceSopenharmony_ci lwsl_info(" e: %s\n", enames[n]); 128d4afb5ceSopenharmony_ci lwsl_hexdump_info(el[n].buf, el[n].len); 129d4afb5ceSopenharmony_ci } 130d4afb5ceSopenharmony_ci 131d4afb5ceSopenharmony_ci lwsl_info("\n"); 132d4afb5ceSopenharmony_ci 133d4afb5ceSopenharmony_ci return 0; 134d4afb5ceSopenharmony_ci} 135