ecmr.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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, "ECMR") == 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, "ECMR") != 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. return NULL;
  48. }
  49. static json_t *
  50. alg_exch_exc(const jose_hook_alg_t *alg, jose_cfg_t *cfg,
  51. const json_t *prv, const json_t *pub)
  52. {
  53. openssl_auto(EC_KEY) *lcl = NULL;
  54. openssl_auto(EC_KEY) *rem = NULL;
  55. openssl_auto(BN_CTX) *bnc = NULL;
  56. openssl_auto(EC_POINT) *p = NULL;
  57. const EC_GROUP *grp = NULL;
  58. bnc = BN_CTX_new();
  59. if (!bnc)
  60. return NULL;
  61. lcl = jose_openssl_jwk_to_EC_KEY(cfg, prv);
  62. if (!lcl)
  63. return NULL;
  64. rem = jose_openssl_jwk_to_EC_KEY(cfg, pub);
  65. if (!rem)
  66. return NULL;
  67. grp = EC_KEY_get0_group(lcl);
  68. if (EC_GROUP_cmp(grp, EC_KEY_get0_group(rem), bnc) != 0)
  69. return NULL;
  70. p = EC_POINT_new(grp);
  71. if (!p)
  72. return NULL;
  73. if (EC_KEY_get0_private_key(lcl)) {
  74. if (EC_POINT_mul(grp, p, NULL, EC_KEY_get0_public_key(rem),
  75. EC_KEY_get0_private_key(lcl), bnc) <= 0)
  76. return NULL;
  77. } else {
  78. if (EC_POINT_copy(p, EC_KEY_get0_public_key(rem)) < 0)
  79. return NULL;
  80. if (!EC_KEY_get0_private_key(rem)) {
  81. if (EC_POINT_invert(grp, p, bnc) < 0)
  82. return NULL;
  83. }
  84. if (EC_POINT_add(grp, p, EC_KEY_get0_public_key(lcl), p, bnc) <= 0)
  85. return NULL;
  86. }
  87. return jose_openssl_jwk_from_EC_POINT(cfg, EC_KEY_get0_group(rem), p, NULL);
  88. }
  89. static void __attribute__((constructor))
  90. constructor(void)
  91. {
  92. static jose_hook_jwk_t jwk = {
  93. .kind = JOSE_HOOK_JWK_KIND_PREP,
  94. .prep.handles = jwk_prep_handles,
  95. .prep.execute = jwk_prep_execute
  96. };
  97. static jose_hook_alg_t ecdh[] = {
  98. { .name = "ECMR",
  99. .kind = JOSE_HOOK_ALG_KIND_EXCH,
  100. .exch.prm = "deriveKey",
  101. .exch.sug = alg_exch_sug,
  102. .exch.exc = alg_exch_exc },
  103. {}
  104. };
  105. jose_hook_jwk_push(&jwk);
  106. for (size_t i = 0; ecdh[i].name; i++)
  107. jose_hook_alg_push(&ecdh[i]);
  108. }