ecdh.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80: */
  2. /*
  3. * Copyright 2016 Red Hat, Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. #include "misc.h"
  18. #include "../hooks.h"
  19. #include <jose/openssl.h>
  20. #include <string.h>
  21. declare_cleanup(EC_POINT)
  22. declare_cleanup(EC_KEY)
  23. declare_cleanup(BN_CTX)
  24. static bool
  25. jwk_prep_handles(jose_cfg_t *cfg, const json_t *jwk)
  26. {
  27. const char *alg = NULL;
  28. if (json_unpack((json_t *) jwk, "{s:s}", "alg", &alg) == -1)
  29. return false;
  30. return strcmp(alg, "ECDH") == 0;
  31. }
  32. static bool
  33. jwk_prep_execute(jose_cfg_t *cfg, json_t *jwk)
  34. {
  35. const char *crv = "P-521";
  36. const char *alg = NULL;
  37. const char *kty = NULL;
  38. if (json_unpack(jwk, "{s:s,s?s,s?s}",
  39. "alg", &alg, "crv", &crv, "kty", &kty) < 0)
  40. return false;
  41. if (strcmp(alg, "ECDH") != 0)
  42. return false;
  43. if (kty && strcmp(kty, "EC") != 0)
  44. return false;
  45. if (json_object_set_new(jwk, "kty", json_string("EC")) < 0)
  46. return false;
  47. if (json_object_set_new(jwk, "crv", json_string(crv)) < 0)
  48. return false;
  49. return true;
  50. }
  51. static const char *
  52. alg_exch_sug(const jose_hook_alg_t *alg, jose_cfg_t *cfg,
  53. const json_t *prv, const json_t *pub)
  54. {
  55. const char *crva = NULL;
  56. const char *crvb = NULL;
  57. const char *ktya = NULL;
  58. const char *ktyb = NULL;
  59. if (json_unpack((json_t *) prv, "{s:s,s:s}", "kty", &ktya, "crv", &crva) < 0)
  60. return NULL;
  61. if (json_unpack((json_t *) pub, "{s:s,s:s}", "kty", &ktyb, "crv", &crvb) < 0)
  62. return NULL;
  63. if (strcmp("EC", ktya) != 0 || strcmp("EC", ktyb) != 0)
  64. return NULL;
  65. if (strcmp(crva, crvb) != 0)
  66. return NULL;
  67. if (str2enum(crva, "P-256", "P-384", "P-521", NULL) == SIZE_MAX)
  68. return NULL;
  69. return "ECDH";
  70. }
  71. static json_t *
  72. alg_exch_exc(const jose_hook_alg_t *alg, jose_cfg_t *cfg,
  73. const json_t *prv, const json_t *pub)
  74. {
  75. openssl_auto(EC_KEY) *lcl = NULL;
  76. openssl_auto(EC_KEY) *rem = NULL;
  77. openssl_auto(BN_CTX) *bnc = NULL;
  78. openssl_auto(EC_POINT) *p = NULL;
  79. const EC_GROUP *grp = NULL;
  80. bnc = BN_CTX_new();
  81. if (!bnc)
  82. return NULL;
  83. lcl = jose_openssl_jwk_to_EC_KEY(cfg, prv);
  84. if (!lcl)
  85. return NULL;
  86. rem = jose_openssl_jwk_to_EC_KEY(cfg, pub);
  87. if (!rem)
  88. return NULL;
  89. grp = EC_KEY_get0_group(lcl);
  90. if (EC_GROUP_cmp(grp, EC_KEY_get0_group(rem), bnc) != 0)
  91. return NULL;
  92. p = EC_POINT_new(grp);
  93. if (!p)
  94. return NULL;
  95. if (EC_POINT_mul(grp, p, NULL, EC_KEY_get0_public_key(rem),
  96. EC_KEY_get0_private_key(lcl), bnc) <= 0)
  97. return NULL;
  98. return jose_openssl_jwk_from_EC_POINT(cfg, EC_KEY_get0_group(rem), p, NULL);
  99. }
  100. static void __attribute__((constructor))
  101. constructor(void)
  102. {
  103. static jose_hook_jwk_t jwk = {
  104. .kind = JOSE_HOOK_JWK_KIND_PREP,
  105. .prep.handles = jwk_prep_handles,
  106. .prep.execute = jwk_prep_execute
  107. };
  108. static jose_hook_alg_t ecdh[] = {
  109. { .name = "ECDH",
  110. .kind = JOSE_HOOK_ALG_KIND_EXCH,
  111. .exch.prm = "deriveKey",
  112. .exch.sug = alg_exch_sug,
  113. .exch.exc = alg_exch_exc },
  114. {}
  115. };
  116. jose_hook_jwk_push(&jwk);
  117. for (size_t i = 0; ecdh[i].name; i++)
  118. jose_hook_alg_push(&ecdh[i]);
  119. }