lws-genec-common.c 3.5 KB

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