rsaes.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 <jose/jwk.h>
  20. #include "../hooks.h"
  21. #include <jose/openssl.h>
  22. #include <openssl/rand.h>
  23. #include <string.h>
  24. #ifdef EVP_PKEY_CTX_set_rsa_oaep_md
  25. #define NAMES "RSA1_5", "RSA-OAEP", "RSA-OAEP-256"
  26. #define HAVE_OAEP
  27. #else
  28. #define NAMES "RSA1_5"
  29. #define EVP_PKEY_CTX_set_rsa_oaep_md(cfg, md) (-1)
  30. #endif
  31. declare_cleanup(EVP_PKEY_CTX)
  32. declare_cleanup(EVP_PKEY)
  33. static bool
  34. jwk_prep_handles(jose_cfg_t *cfg, const json_t *jwk)
  35. {
  36. const char *alg = NULL;
  37. if (json_unpack((json_t *) jwk, "{s:s}", "alg", &alg) == -1)
  38. return false;
  39. return str2enum(alg, NAMES, NULL) != SIZE_MAX;
  40. }
  41. static json_t *
  42. jwk_prep_execute(jose_cfg_t *cfg, const json_t *jwk)
  43. {
  44. if (!jwk_prep_handles(cfg, jwk))
  45. return NULL;
  46. return json_pack("{s:{s:s}}", "upd", "kty", "RSA");
  47. }
  48. static const char *
  49. alg_wrap_alg(const jose_hook_alg_t *alg, jose_cfg_t *cfg, const json_t *jwk)
  50. {
  51. const char *name = NULL;
  52. const char *type = NULL;
  53. if (json_unpack((json_t *) jwk, "{s?s,s?s}",
  54. "alg", &name, "kty", &type) < 0)
  55. return NULL;
  56. if (name)
  57. return str2enum(name, NAMES, NULL) != SIZE_MAX ? name : NULL;
  58. if (!type || strcmp(type, "RSA") != 0)
  59. return NULL;
  60. #ifdef HAVE_OAEP
  61. return "RSA-OAEP";
  62. #else
  63. return "RSA1_5";
  64. #endif
  65. }
  66. static const char *
  67. alg_wrap_enc(const jose_hook_alg_t *alg, jose_cfg_t *cfg, const json_t *jwk)
  68. {
  69. size_t len = 0;
  70. len = jose_b64_dec(json_object_get(jwk, "n"), NULL, 0) * 8;
  71. if (len >= 15360)
  72. return "A256CBC-HS512";
  73. else if (len >= 7680)
  74. return "A192CBC-HS384";
  75. else
  76. return "A128CBC-HS256";
  77. }
  78. static bool
  79. alg_wrap_wrp(const jose_hook_alg_t *alg, jose_cfg_t *cfg, json_t *jwe,
  80. json_t *rcp, const json_t *jwk, json_t *cek)
  81. {
  82. openssl_auto(EVP_PKEY_CTX) *epc = NULL;
  83. openssl_auto(EVP_PKEY) *key = NULL;
  84. const EVP_MD *md = NULL;
  85. const RSA *rsa = NULL;
  86. uint8_t *pt = NULL;
  87. uint8_t *ct = NULL;
  88. bool ret = false;
  89. size_t ptl = 0;
  90. size_t ctl = 0;
  91. int tmp = 0;
  92. int pad = 0;
  93. if (!json_object_get(cek, "k") && !jose_jwk_gen(cfg, cek))
  94. return false;
  95. switch (str2enum(alg->name, NAMES, NULL)) {
  96. case 0: pad = RSA_PKCS1_PADDING; tmp = 11; md = EVP_sha1(); break;
  97. case 1: pad = RSA_PKCS1_OAEP_PADDING; tmp = 41; md = EVP_sha1(); break;
  98. case 2: pad = RSA_PKCS1_OAEP_PADDING; tmp = 41; md = EVP_sha256(); break;
  99. default: return false;
  100. }
  101. key = jose_openssl_jwk_to_EVP_PKEY(cfg, jwk);
  102. if (!key || EVP_PKEY_base_id(key) != EVP_PKEY_RSA)
  103. return false;
  104. ptl = jose_b64_dec(json_object_get(cek, "k"), NULL, 0);
  105. if (ptl == SIZE_MAX)
  106. return false;
  107. rsa = EVP_PKEY_get0_RSA(key);
  108. if (!rsa)
  109. return false;
  110. if ((int) ptl >= RSA_size(rsa) - tmp)
  111. return false;
  112. epc = EVP_PKEY_CTX_new(key, NULL);
  113. if (!epc)
  114. return false;
  115. if (EVP_PKEY_encrypt_init(epc) <= 0)
  116. return false;
  117. if (EVP_PKEY_CTX_set_rsa_padding(epc, pad) <= 0)
  118. return false;
  119. if (pad == RSA_PKCS1_OAEP_PADDING) {
  120. if (EVP_PKEY_CTX_set_rsa_oaep_md(epc, md) <= 0)
  121. return false;
  122. if (EVP_PKEY_CTX_set_rsa_mgf1_md(epc, md) <= 0)
  123. return false;
  124. }
  125. pt = malloc(ptl);
  126. if (!pt)
  127. return false;
  128. if (jose_b64_dec(json_object_get(cek, "k"), pt, ptl) != ptl)
  129. goto egress;
  130. if (EVP_PKEY_encrypt(epc, NULL, &ctl, pt, ptl) <= 0)
  131. goto egress;
  132. ct = malloc(ctl);
  133. if (!ct)
  134. goto egress;
  135. if (EVP_PKEY_encrypt(epc, ct, &ctl, pt, ptl) <= 0)
  136. goto egress;
  137. if (json_object_set_new(rcp, "encrypted_key", jose_b64_enc(ct, ctl)) < 0)
  138. goto egress;
  139. ret = add_entity(jwe, rcp, "recipients", "header", "encrypted_key", NULL);
  140. egress:
  141. if (pt) {
  142. OPENSSL_cleanse(pt, ptl);
  143. free(pt);
  144. }
  145. free(ct);
  146. return ret;
  147. }
  148. static bool
  149. alg_wrap_unw(const jose_hook_alg_t *alg, jose_cfg_t *cfg, const json_t *jwe,
  150. const json_t *rcp, const json_t *jwk, json_t *cek)
  151. {
  152. openssl_auto(EVP_PKEY_CTX) *epc = NULL;
  153. openssl_auto(EVP_PKEY) *key = NULL;
  154. const uint8_t *tt = NULL;
  155. const EVP_MD *md = NULL;
  156. uint8_t *ct = NULL;
  157. uint8_t *pt = NULL;
  158. uint8_t *rt = NULL;
  159. bool ret = false;
  160. size_t ctl = 0;
  161. size_t ptl = 0;
  162. size_t rtl = 0;
  163. size_t ttl = 0;
  164. int pad = 0;
  165. switch (str2enum(alg->name, NAMES, NULL)) {
  166. case 0: pad = RSA_PKCS1_PADDING; md = EVP_sha1(); break;
  167. case 1: pad = RSA_PKCS1_OAEP_PADDING; md = EVP_sha1(); break;
  168. case 2: pad = RSA_PKCS1_OAEP_PADDING; md = EVP_sha256(); break;
  169. default: return false;
  170. }
  171. key = jose_openssl_jwk_to_EVP_PKEY(cfg, jwk);
  172. if (!key || EVP_PKEY_base_id(key) != EVP_PKEY_RSA)
  173. goto egress;
  174. ctl = jose_b64_dec(json_object_get(rcp, "encrypted_key"), NULL, 0);
  175. if (ctl == SIZE_MAX)
  176. goto egress;
  177. ct = malloc(ctl);
  178. if (!ct)
  179. goto egress;
  180. if (jose_b64_dec(json_object_get(rcp, "encrypted_key"), ct, ctl) != ctl)
  181. goto egress;
  182. ptl = ctl;
  183. pt = malloc(ptl);
  184. if (!pt)
  185. goto egress;
  186. epc = EVP_PKEY_CTX_new(key, NULL);
  187. if (!epc)
  188. goto egress;
  189. if (EVP_PKEY_decrypt_init(epc) <= 0)
  190. goto egress;
  191. if (EVP_PKEY_CTX_set_rsa_padding(epc, pad) <= 0)
  192. goto egress;
  193. if (pad == RSA_PKCS1_OAEP_PADDING) {
  194. if (EVP_PKEY_CTX_set_rsa_oaep_md(epc, md) <= 0)
  195. goto egress;
  196. if (EVP_PKEY_CTX_set_rsa_mgf1_md(epc, md) <= 0)
  197. goto egress;
  198. }
  199. /* Handle MMA Attack as prescribed by RFC 3218, always generate a
  200. * random buffer of appropriate length so that the same operations
  201. * are performed whether decrypt succeeds or not, in an attempt to
  202. * foil timing attacks */
  203. rtl = ptl;
  204. rt = malloc(rtl);
  205. if (!rt)
  206. goto egress;
  207. if (RAND_bytes(rt, rtl) <= 0)
  208. goto egress;
  209. ret |= EVP_PKEY_decrypt(epc, pt, &ptl, ct, ctl) > 0;
  210. ttl = ret ? ptl : rtl;
  211. tt = ret ? pt : rt;
  212. ret |= pad == RSA_PKCS1_PADDING;
  213. if (json_object_set_new(cek, "k", jose_b64_enc(tt, ttl)) < 0)
  214. ret = false;
  215. egress:
  216. if (pt) {
  217. OPENSSL_cleanse(pt, ptl);
  218. free(pt);
  219. }
  220. if (rt) {
  221. OPENSSL_cleanse(rt, rtl);
  222. free(rt);
  223. }
  224. free(ct);
  225. return ret;
  226. }
  227. static void __attribute__((constructor))
  228. constructor(void)
  229. {
  230. static jose_hook_jwk_t jwk = {
  231. .kind = JOSE_HOOK_JWK_KIND_PREP,
  232. .prep.handles = jwk_prep_handles,
  233. .prep.execute = jwk_prep_execute
  234. };
  235. static jose_hook_alg_t alg[] = {
  236. { .kind = JOSE_HOOK_ALG_KIND_WRAP,
  237. .name = "RSA1_5",
  238. .wrap.eprm = "wrapKey",
  239. .wrap.dprm = "unwrapKey",
  240. .wrap.alg = alg_wrap_alg,
  241. .wrap.enc = alg_wrap_enc,
  242. .wrap.wrp = alg_wrap_wrp,
  243. .wrap.unw = alg_wrap_unw },
  244. #ifdef HAVE_OAEP
  245. { .kind = JOSE_HOOK_ALG_KIND_WRAP,
  246. .name = "RSA-OAEP",
  247. .wrap.eprm = "wrapKey",
  248. .wrap.dprm = "unwrapKey",
  249. .wrap.alg = alg_wrap_alg,
  250. .wrap.enc = alg_wrap_enc,
  251. .wrap.wrp = alg_wrap_wrp,
  252. .wrap.unw = alg_wrap_unw },
  253. { .kind = JOSE_HOOK_ALG_KIND_WRAP,
  254. .name = "RSA-OAEP-256",
  255. .wrap.eprm = "wrapKey",
  256. .wrap.dprm = "unwrapKey",
  257. .wrap.alg = alg_wrap_alg,
  258. .wrap.enc = alg_wrap_enc,
  259. .wrap.wrp = alg_wrap_wrp,
  260. .wrap.unw = alg_wrap_unw },
  261. #endif
  262. {}
  263. };
  264. jose_hook_jwk_push(&jwk);
  265. for (size_t i = 0; alg[i].name; i++)
  266. jose_hook_alg_push(&alg[i]);
  267. }