rsa.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. static RSA *
  22. mkrsa(const json_t *jwk)
  23. {
  24. openssl_auto(BIGNUM) *bn = NULL;
  25. json_auto_t *exp = NULL;
  26. RSA *key = NULL;
  27. int bits = 2048;
  28. if (json_unpack((json_t *) jwk, "{s?i,s?O}",
  29. "bits", &bits, "e", &exp) == -1)
  30. return NULL;
  31. if (bits < 2048)
  32. return NULL;
  33. if (!exp)
  34. exp = json_integer(65537);
  35. switch (exp ? exp->type : JSON_NULL) {
  36. case JSON_STRING:
  37. bn = bn_decode_json(exp);
  38. if (!bn)
  39. return NULL;
  40. break;
  41. case JSON_INTEGER:
  42. bn = BN_new();
  43. if (!bn)
  44. return NULL;
  45. if (BN_set_word(bn, json_integer_value(exp)) <= 0)
  46. return NULL;
  47. break;
  48. default:
  49. break;
  50. }
  51. key = RSA_new();
  52. if (!key)
  53. return NULL;
  54. bits = RSA_generate_key_ex(key, bits, bn, NULL);
  55. if (bits <= 0) {
  56. RSA_free(key);
  57. key = NULL;
  58. }
  59. return key;
  60. }
  61. static bool
  62. jwk_make_handles(jose_cfg_t *cfg, const json_t *jwk)
  63. {
  64. const char *kty = NULL;
  65. if (json_unpack((json_t *) jwk, "{s:s}", "kty", &kty) == -1)
  66. return false;
  67. return strcmp(kty, "RSA") == 0;
  68. }
  69. static json_t *
  70. jwk_make_execute(jose_cfg_t *cfg, const json_t *jwk)
  71. {
  72. json_auto_t *key = NULL;
  73. RSA *rsa = NULL;
  74. if (!jwk_make_handles(cfg, jwk))
  75. return NULL;
  76. rsa = mkrsa(jwk);
  77. if (!rsa)
  78. return NULL;
  79. key = jose_openssl_jwk_from_RSA(cfg, rsa);
  80. RSA_free(rsa);
  81. if (!key)
  82. return NULL;
  83. return json_pack("{s:[s,s],s:O}", "del", "bits", "e", "upd", key);
  84. }
  85. static void __attribute__((constructor))
  86. constructor(void)
  87. {
  88. static jose_hook_jwk_t jwk = {
  89. .kind = JOSE_HOOK_JWK_KIND_MAKE,
  90. .make.handles = jwk_make_handles,
  91. .make.execute = jwk_make_execute
  92. };
  93. jose_hook_jwk_push(&jwk);
  94. }