jwk.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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 <jose/b64.h>
  19. #include "../hooks.h"
  20. #include <jose/openssl.h>
  21. #include <openssl/rand.h>
  22. #include <openssl/objects.h>
  23. #include <string.h>
  24. declare_cleanup(EC_POINT)
  25. declare_cleanup(EC_KEY)
  26. declare_cleanup(BN_CTX)
  27. declare_cleanup(RSA)
  28. static EC_POINT *
  29. mkpub(const EC_GROUP *grp, const json_t *x, const json_t *y, const BIGNUM *D)
  30. {
  31. openssl_auto(EC_POINT) *pub = NULL;
  32. openssl_auto(BN_CTX) *cfg = NULL;
  33. openssl_auto(BIGNUM) *X = NULL;
  34. openssl_auto(BIGNUM) *Y = NULL;
  35. cfg = BN_CTX_new();
  36. if (!cfg)
  37. return NULL;
  38. pub = EC_POINT_new(grp);
  39. if (!pub)
  40. return NULL;
  41. if (x && y) {
  42. X = bn_decode_json(x);
  43. Y = bn_decode_json(y);
  44. if (!X || !Y)
  45. return NULL;
  46. if (EC_POINT_set_affine_coordinates_GFp(grp, pub, X, Y, cfg) < 0)
  47. return NULL;
  48. } else if (D) {
  49. if (EC_POINT_mul(grp, pub, D, NULL, NULL, cfg) < 0)
  50. return NULL;
  51. } else {
  52. return NULL;
  53. }
  54. return EC_POINT_dup(pub, grp);
  55. }
  56. json_t *
  57. jose_openssl_jwk_from_EVP_PKEY(jose_cfg_t *cfg, EVP_PKEY *key)
  58. {
  59. const uint8_t *buf = NULL;
  60. size_t len = 0;
  61. switch (EVP_PKEY_base_id(key)) {
  62. case EVP_PKEY_HMAC:
  63. buf = EVP_PKEY_get0_hmac(key, &len);
  64. if (!buf)
  65. return NULL;
  66. return json_pack("{s:s,s:o}", "kty", "oct", "k",
  67. jose_b64_enc(buf, len));
  68. case EVP_PKEY_RSA:
  69. return jose_openssl_jwk_from_RSA(cfg, EVP_PKEY_get0_RSA(key));
  70. case EVP_PKEY_EC:
  71. return jose_openssl_jwk_from_EC_KEY(cfg, EVP_PKEY_get0_EC_KEY(key));
  72. default: return NULL;
  73. }
  74. }
  75. json_t *
  76. jose_openssl_jwk_from_RSA(jose_cfg_t *cfg, const RSA *key)
  77. {
  78. const BIGNUM *n = NULL;
  79. const BIGNUM *e = NULL;
  80. const BIGNUM *d = NULL;
  81. const BIGNUM *p = NULL;
  82. const BIGNUM *q = NULL;
  83. const BIGNUM *dp = NULL;
  84. const BIGNUM *dq = NULL;
  85. const BIGNUM *qi = NULL;
  86. json_auto_t *jwk = NULL;
  87. if (!key)
  88. return NULL;
  89. RSA_get0_key(key, &n, &e, &d);
  90. RSA_get0_factors(key, &p, &q);
  91. RSA_get0_crt_params(key, &dp, &dq, &qi);
  92. if (!n || !e)
  93. return NULL;
  94. jwk = json_pack("{s:s,s:o,s:o}",
  95. "kty", "RSA",
  96. "n", bn_encode_json(n, 0),
  97. "e", bn_encode_json(e, 0));
  98. if (d && json_object_set_new(jwk, "d", bn_encode_json(d, 0)) != 0)
  99. return NULL;
  100. if (p && json_object_set_new(jwk, "p", bn_encode_json(p, 0)) != 0)
  101. return NULL;
  102. if (q && json_object_set_new(jwk, "q", bn_encode_json(q, 0)) != 0)
  103. return NULL;
  104. if (dp && json_object_set_new(jwk, "dp", bn_encode_json(dp, 0)) != 0)
  105. return NULL;
  106. if (dq && json_object_set_new(jwk, "dq", bn_encode_json(dq, 0)) != 0)
  107. return NULL;
  108. if (qi && json_object_set_new(jwk, "qi", bn_encode_json(qi, 0)) != 0)
  109. return NULL;
  110. return json_incref(jwk);
  111. }
  112. json_t *
  113. jose_openssl_jwk_from_EC_KEY(jose_cfg_t *cfg, const EC_KEY *key)
  114. {
  115. return jose_openssl_jwk_from_EC_POINT(
  116. cfg,
  117. EC_KEY_get0_group(key),
  118. EC_KEY_get0_public_key(key),
  119. EC_KEY_get0_private_key(key)
  120. );
  121. }
  122. json_t *
  123. jose_openssl_jwk_from_EC_POINT(jose_cfg_t *cfg, const EC_GROUP *grp,
  124. const EC_POINT *pub, const BIGNUM *prv)
  125. {
  126. openssl_auto(EC_POINT) *p = NULL;
  127. openssl_auto(BN_CTX) *ctx = NULL;
  128. openssl_auto(BIGNUM) *x = NULL;
  129. openssl_auto(BIGNUM) *y = NULL;
  130. json_auto_t *jwk = NULL;
  131. const char *crv = NULL;
  132. int len = 0;
  133. if (!grp)
  134. return NULL;
  135. len = (EC_GROUP_get_degree(grp) + 7) / 8;
  136. switch (EC_GROUP_get_curve_name(grp)) {
  137. case NID_X9_62_prime256v1: crv = "P-256"; break;
  138. case NID_secp384r1: crv = "P-384"; break;
  139. case NID_secp521r1: crv = "P-521"; break;
  140. default: return NULL;
  141. }
  142. ctx = BN_CTX_new();
  143. if (!ctx)
  144. return NULL;
  145. if (!pub) {
  146. if (!prv)
  147. return NULL;
  148. pub = p = EC_POINT_new(grp);
  149. if (!pub)
  150. return NULL;
  151. if (EC_POINT_mul(grp, p, prv, NULL, NULL, ctx) < 0)
  152. return NULL;
  153. }
  154. x = BN_new();
  155. y = BN_new();
  156. if (!x || !y)
  157. return NULL;
  158. if (EC_POINT_get_affine_coordinates_GFp(grp, pub, x, y, ctx) < 0)
  159. return NULL;
  160. jwk = json_pack("{s:s,s:s,s:o,s:o}", "kty", "EC", "crv", crv,
  161. "x", bn_encode_json(x, len), "y", bn_encode_json(y, len));
  162. if (prv && json_object_set_new(jwk, "d", bn_encode_json(prv, len)) == -1)
  163. return NULL;
  164. return json_incref(jwk);
  165. }
  166. EVP_PKEY *
  167. jose_openssl_jwk_to_EVP_PKEY(jose_cfg_t *cfg, const json_t *jwk)
  168. {
  169. openssl_auto(EC_KEY) *ec = NULL;
  170. openssl_auto(RSA) *rsa = NULL;
  171. const char *kty = NULL;
  172. EVP_PKEY *key = NULL;
  173. uint8_t *buf = NULL;
  174. size_t len = 0;
  175. if (json_unpack((json_t *) jwk, "{s:s}", "kty", &kty) == -1)
  176. return NULL;
  177. switch (str2enum(kty, "EC", "RSA", "oct", NULL)) {
  178. case 0:
  179. ec = jose_openssl_jwk_to_EC_KEY(cfg, jwk);
  180. if (!ec)
  181. return NULL;
  182. key = EVP_PKEY_new();
  183. if (!key)
  184. return NULL;
  185. if (EVP_PKEY_set1_EC_KEY(key, ec) > 0)
  186. return key;
  187. EVP_PKEY_free(key);
  188. return NULL;
  189. case 1:
  190. rsa = jose_openssl_jwk_to_RSA(cfg, jwk);
  191. if (!rsa)
  192. return NULL;
  193. key = EVP_PKEY_new();
  194. if (!key)
  195. return NULL;
  196. if (EVP_PKEY_set1_RSA(key, rsa) > 0)
  197. return key;
  198. EVP_PKEY_free(key);
  199. return NULL;
  200. case 2:
  201. len = jose_b64_dec(json_object_get(jwk, "k"), NULL, 0);
  202. if (len == SIZE_MAX)
  203. return NULL;
  204. buf = malloc(len);
  205. if (!buf)
  206. return NULL;
  207. if (jose_b64_dec(json_object_get(jwk, "k"), buf, len) != len) {
  208. OPENSSL_cleanse(buf, len);
  209. free(buf);
  210. return NULL;
  211. }
  212. key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, buf, len);
  213. OPENSSL_cleanse(buf, len);
  214. free(buf);
  215. return key;
  216. default: return NULL;
  217. }
  218. }
  219. RSA *
  220. jose_openssl_jwk_to_RSA(jose_cfg_t *cfg, const json_t *jwk)
  221. {
  222. openssl_auto(RSA) *rsa = NULL;
  223. const json_t *n = NULL;
  224. const json_t *e = NULL;
  225. const json_t *d = NULL;
  226. const json_t *p = NULL;
  227. const json_t *q = NULL;
  228. const json_t *dp = NULL;
  229. const json_t *dq = NULL;
  230. const json_t *qi = NULL;
  231. const char *kty = NULL;
  232. BIGNUM *N = NULL;
  233. BIGNUM *E = NULL;
  234. BIGNUM *D = NULL;
  235. BIGNUM *P = NULL;
  236. BIGNUM *Q = NULL;
  237. BIGNUM *DP = NULL;
  238. BIGNUM *DQ = NULL;
  239. BIGNUM *QI = NULL;
  240. if (json_unpack((json_t *) jwk, "{s:s,s:o,s:o,s?o,s?o,s?o,s?o,s?o,s?o}",
  241. "kty", &kty, "n", &n, "e", &e, "d", &d, "p", &p,
  242. "q", &q, "dp", &dp, "dq", &dq, "qi", &qi) != 0)
  243. return NULL;
  244. rsa = RSA_new();
  245. if (!rsa)
  246. return NULL;
  247. N = bn_decode_json(n);
  248. E = bn_decode_json(e);
  249. P = bn_decode_json(p);
  250. Q = bn_decode_json(q);
  251. DP = bn_decode_json(dp);
  252. DQ = bn_decode_json(dq);
  253. QI = bn_decode_json(qi);
  254. if ((!n || N) && (!e || E) && (!p || P) && (!q || Q) &&
  255. (!dp || DP) && (!dq || DQ) && (!qi || QI)) {
  256. if (RSA_set0_key(rsa, N, E, D) > 0) {
  257. N = NULL;
  258. E = NULL;
  259. D = NULL;
  260. if ((!P && !Q) ||
  261. RSA_set0_factors(rsa, P, Q) > 0) {
  262. P = NULL;
  263. Q = NULL;
  264. if ((!DP && !DQ && !QI) ||
  265. RSA_set0_crt_params(rsa, DP, DQ, QI) > 0) {
  266. DP = NULL;
  267. DQ = NULL;
  268. QI = NULL;
  269. if (RSA_up_ref(rsa) > 0)
  270. return rsa;
  271. }
  272. }
  273. }
  274. }
  275. BN_free(N);
  276. BN_free(E);
  277. BN_free(P);
  278. BN_free(Q);
  279. BN_free(DP);
  280. BN_free(DQ);
  281. BN_free(QI);
  282. return NULL;
  283. }
  284. EC_KEY *
  285. jose_openssl_jwk_to_EC_KEY(jose_cfg_t *cfg, const json_t *jwk)
  286. {
  287. openssl_auto(EC_POINT) *pub = NULL;
  288. openssl_auto(EC_KEY) *key = NULL;
  289. openssl_auto(BIGNUM) *D = NULL;
  290. const char *kty = NULL;
  291. const char *crv = NULL;
  292. const json_t *x = NULL;
  293. const json_t *y = NULL;
  294. const json_t *d = NULL;
  295. int nid = NID_undef;
  296. if (json_unpack((json_t *) jwk, "{s:s,s:s,s:o,s:o,s?o}", "kty", &kty,
  297. "crv", &crv, "x", &x, "y", &y, "d", &d) == -1)
  298. return NULL;
  299. if (strcmp(kty, "EC") != 0)
  300. return NULL;
  301. switch (str2enum(crv, "P-256", "P-384", "P-521", NULL)) {
  302. case 0: nid = NID_X9_62_prime256v1; break;
  303. case 1: nid = NID_secp384r1; break;
  304. case 2: nid = NID_secp521r1; break;
  305. default: return NULL;
  306. }
  307. key = EC_KEY_new_by_curve_name(nid);
  308. if (!key)
  309. return NULL;
  310. if (d) {
  311. D = bn_decode_json(d);
  312. if (!D)
  313. return NULL;
  314. if (EC_KEY_set_private_key(key, D) < 0)
  315. return NULL;
  316. }
  317. pub = mkpub(EC_KEY_get0_group(key), x, y, D);
  318. if (!pub)
  319. return NULL;
  320. if (EC_KEY_set_public_key(key, pub) < 0)
  321. return NULL;
  322. if (EC_KEY_check_key(key) == 0)
  323. return NULL;
  324. return EC_KEY_up_ref(key) <= 0 ? NULL : key;
  325. }