ecmr.c 3.6 KB

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