ecdh.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 json_t *
  33. jwk_prep_execute(jose_cfg_t *cfg, const json_t *jwk)
  34. {
  35. const char *crv = "P-521";
  36. const char *alg = NULL;
  37. if (json_unpack((json_t *) jwk, "{s:s,s?s}", "alg", &alg, "crv", &crv) < 0)
  38. return false;
  39. if (strcmp(alg, "ECDH") != 0)
  40. return false;
  41. return json_pack("{s:{s:s,s:s}}", "upd", "kty", "EC", "crv", crv);
  42. }
  43. static const char *
  44. alg_exch_sug(const jose_hook_alg_t *alg, jose_cfg_t *cfg,
  45. const json_t *prv, const json_t *pub)
  46. {
  47. const char *crva = NULL;
  48. const char *crvb = NULL;
  49. const char *ktya = NULL;
  50. const char *ktyb = NULL;
  51. if (json_unpack((json_t *) prv, "{s:s,s:s}", "kty", &ktya, "crv", &crva) < 0)
  52. return NULL;
  53. if (json_unpack((json_t *) pub, "{s:s,s:s}", "kty", &ktyb, "crv", &crvb) < 0)
  54. return NULL;
  55. if (strcmp("EC", ktya) != 0 || strcmp("EC", ktyb) != 0)
  56. return NULL;
  57. if (strcmp(crva, crvb) != 0)
  58. return NULL;
  59. if (str2enum(crva, "P-256", "P-384", "P-521", NULL) == SIZE_MAX)
  60. return NULL;
  61. return "ECDH";
  62. }
  63. static json_t *
  64. alg_exch_exc(const jose_hook_alg_t *alg, jose_cfg_t *cfg,
  65. const json_t *prv, const json_t *pub)
  66. {
  67. openssl_auto(EC_KEY) *lcl = NULL;
  68. openssl_auto(EC_KEY) *rem = NULL;
  69. openssl_auto(BN_CTX) *bnc = NULL;
  70. openssl_auto(EC_POINT) *p = NULL;
  71. const EC_GROUP *grp = NULL;
  72. bnc = BN_CTX_new();
  73. if (!bnc)
  74. return NULL;
  75. lcl = jose_openssl_jwk_to_EC_KEY(cfg, prv);
  76. if (!lcl)
  77. return NULL;
  78. rem = jose_openssl_jwk_to_EC_KEY(cfg, pub);
  79. if (!rem)
  80. return NULL;
  81. grp = EC_KEY_get0_group(lcl);
  82. if (EC_GROUP_cmp(grp, EC_KEY_get0_group(rem), bnc) != 0)
  83. return NULL;
  84. p = EC_POINT_new(grp);
  85. if (!p)
  86. return NULL;
  87. if (EC_POINT_mul(grp, p, NULL, EC_KEY_get0_public_key(rem),
  88. EC_KEY_get0_private_key(lcl), bnc) <= 0)
  89. return NULL;
  90. return jose_openssl_jwk_from_EC_POINT(cfg, EC_KEY_get0_group(rem), p, NULL);
  91. }
  92. static void __attribute__((constructor))
  93. constructor(void)
  94. {
  95. static jose_hook_jwk_t jwk = {
  96. .kind = JOSE_HOOK_JWK_KIND_PREP,
  97. .prep.handles = jwk_prep_handles,
  98. .prep.execute = jwk_prep_execute
  99. };
  100. static jose_hook_alg_t ecdh[] = {
  101. { .name = "ECDH",
  102. .kind = JOSE_HOOK_ALG_KIND_EXCH,
  103. .exch.prm = "deriveKey",
  104. .exch.sug = alg_exch_sug,
  105. .exch.exc = alg_exch_exc },
  106. {}
  107. };
  108. jose_hook_jwk_push(&jwk);
  109. for (size_t i = 0; ecdh[i].name; i++)
  110. jose_hook_alg_push(&ecdh[i]);
  111. }