jws.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  1. /*
  2. * lws-api-test-jose - RFC7515 jws tests
  3. *
  4. * Written in 2010-2019 by Andy Green <[email protected]>
  5. *
  6. * This file is made available under the Creative Commons CC0 1.0
  7. * Universal Public Domain Dedication.
  8. */
  9. #include <libwebsockets.h>
  10. /*
  11. * JSON Web Signature is defined in RFC7515
  12. *
  13. * https://tools.ietf.org/html/rfc7515
  14. *
  15. * It's basically a way to wrap some JSON with a JSON "header" describing the
  16. * crypto, and a signature, all in a BASE64 wrapper with elided terminating '='.
  17. *
  18. * The signature stays with the content, it serves a different purpose than eg
  19. * a TLS tunnel to transfer it.
  20. *
  21. */
  22. /* for none, the compact serialization format is b64u(jose hdr).b64u(payload) */
  23. static const char *none_cser =
  24. "eyJhbGciOiJub25lIn0"
  25. "."
  26. "eyJpc3MiOiJqb2UiLA0KICJleHAiOjEzMDA4MTkzODAsDQogImh0dHA6Ly9leGFt"
  27. "cGxlLmNvbS9pc19yb290Ijp0cnVlfQ",
  28. *none_jose = "{\"alg\":\"none\"}",
  29. *none_payload = "{\"iss\":\"joe\",\r\n \"exp\":1300819380,\r\n"
  30. " \"http://example.com/is_root\":true}";
  31. int
  32. test_jws_none(struct lws_context *context)
  33. {
  34. struct lws_jws_map map;
  35. struct lws_jose jose;
  36. char temp[2048];
  37. int n, temp_len = sizeof(temp), ret = -1;
  38. lws_jose_init(&jose);
  39. /* A.5 Unsecured JSON "none" RFC7515 worked example */
  40. /* decode the b64.b64[.b64] compact serialization blocks */
  41. n = lws_jws_compact_decode(none_cser, (int)strlen(none_cser), &map, NULL,
  42. temp, &temp_len);
  43. if (n != 2) {
  44. lwsl_err("%s: concat_map failed\n", __func__);
  45. goto bail;
  46. }
  47. /* confirm the decoded JOSE header is exactly what we expect */
  48. if (strncmp(none_jose, map.buf[LJWS_JOSE], map.len[LJWS_JOSE])) {
  49. lwsl_err("%s: jose b64 decode wrong\n", __func__);
  50. goto bail;
  51. }
  52. /* parse the JOSE header */
  53. if (lws_jws_parse_jose(&jose, map.buf[LJWS_JOSE],
  54. map.len[LJWS_JOSE],
  55. (char *)lws_concat_temp(temp, temp_len),
  56. &temp_len) < 0 || !jose.alg) {
  57. lwsl_err("%s: JOSE parse failed\n", __func__);
  58. goto bail;
  59. }
  60. /* confirm we used the "none" alg as expected from JOSE hdr */
  61. if (strcmp(jose.alg->alg, "none")) {
  62. lwsl_err("%s: JOSE header has wrong alg\n", __func__);
  63. goto bail;
  64. }
  65. /* confirm the payload is literally what we expect */
  66. if (strncmp(none_payload, map.buf[LJWS_PYLD],
  67. map.len[LJWS_PYLD])) {
  68. lwsl_err("%s: payload b64 decode wrong\n", __func__);
  69. goto bail;
  70. }
  71. /* end */
  72. ret = 0;
  73. bail:
  74. lws_jose_destroy(&jose);
  75. if (ret)
  76. lwsl_err("%s: selftest failed ++++++++++++++++++++\n", __func__);
  77. else
  78. lwsl_notice("%s: selftest OK\n", __func__);
  79. return ret;
  80. }
  81. static const char
  82. *test1 = "{\"typ\":\"JWT\",\r\n \"alg\":\"HS256\"}",
  83. *test1_enc = "eyJ0eXAiOiJKV1QiLA0KICJhbGciOiJIUzI1NiJ9",
  84. *test2 = "{\"iss\":\"joe\",\r\n \"exp\":1300819380,\r\n"
  85. " \"http://example.com/is_root\":true}",
  86. *test2_enc = "eyJpc3MiOiJqb2UiLA0KICJleHAiOjEzMDA4MTkzODAsDQ"
  87. "ogImh0dHA6Ly9leGFtcGxlLmNvbS9pc19yb290Ijp0cnVlfQ",
  88. *key_jwk = "{\"kty\":\"oct\",\r\n"
  89. " \"k\":\"AyM1SysPpbyDfgZld3umj1qzKObwVMkoqQ-EstJQ"
  90. "Lr_T-1qS0gZH75aKtMN3Yj0iPS4hcgUuTwjAzZr1Z9CAow\"}",
  91. *hash_enc = "dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk"
  92. ;
  93. int
  94. test_jws_HS256(struct lws_context *context)
  95. {
  96. char buf[2048], temp[256], *p = buf, *end = buf + sizeof(buf) - 1, *enc_ptr;
  97. uint8_t digest[LWS_GENHASH_LARGEST];
  98. struct lws_jws_map map;
  99. int temp_len = sizeof(temp);
  100. struct lws_genhmac_ctx ctx;
  101. struct lws_jose jose;
  102. struct lws_jwk jwk;
  103. struct lws_jws jws;
  104. int n;
  105. lws_jose_init(&jose);
  106. lws_jws_init(&jws, &jwk, context);
  107. /* Test 1: SHA256 on RFC7515 worked example */
  108. /* parse the JOSE header */
  109. if (lws_jws_parse_jose(&jose, test1, (int)strlen(test1), temp,
  110. &temp_len) < 0 || !jose.alg) {
  111. lwsl_err("%s: JOSE parse failed\n", __func__);
  112. goto bail;
  113. }
  114. /* confirm we used the "none" alg as expected from JOSE hdr */
  115. if (strcmp(jose.alg->alg, "HS256")) {
  116. lwsl_err("%s: JOSE header has wrong alg\n", __func__);
  117. goto bail;
  118. }
  119. /* 1.1: import the JWK oct key */
  120. if (lws_jwk_import(&jwk, NULL, NULL, key_jwk, strlen(key_jwk)) < 0) {
  121. lwsl_notice("Failed to decode JWK test key\n");
  122. return -1;
  123. }
  124. if (jwk.kty != LWS_GENCRYPTO_KTY_OCT) {
  125. lwsl_err("%s: unexpected kty %d\n", __func__, jwk.kty);
  126. return -1;
  127. }
  128. /* 1.2: create JWS known hdr + known payload */
  129. n = lws_jws_encode_section(test1, strlen(test1), 1, &p, end);
  130. if (n < 0) {
  131. goto bail;
  132. }
  133. if (strcmp(buf, test1_enc))
  134. goto bail;
  135. enc_ptr = p + 1; /* + 1 skips the . */
  136. n = lws_jws_encode_section(test2, strlen(test2), 0, &p, end);
  137. if (n < 0) {
  138. goto bail;
  139. }
  140. if (strcmp(enc_ptr, test2_enc))
  141. goto bail;
  142. /* 1.3: use HMAC SHA-256 with known key on the hdr . payload */
  143. if (lws_genhmac_init(&ctx, jose.alg->hmac_type,
  144. jwk.e[LWS_GENCRYPTO_OCT_KEYEL_K].buf,
  145. jwk.e[LWS_GENCRYPTO_OCT_KEYEL_K].len))
  146. goto bail;
  147. if (lws_genhmac_update(&ctx, (uint8_t *)buf, p - buf))
  148. goto bail_destroy_hmac;
  149. lws_genhmac_destroy(&ctx, digest);
  150. /* 1.4: append a base64 encode of the computed HMAC digest */
  151. enc_ptr = p + 1; /* + 1 skips the . */
  152. n = lws_jws_encode_section((const char *)digest, 32, 0, &p, end);
  153. if (n < 0)
  154. goto bail;
  155. if (strcmp(enc_ptr, hash_enc)) { /* check against known B64URL hash */
  156. lwsl_err("%s: b64 enc of computed HMAC mismatches '%s' '%s'\n",
  157. __func__, enc_ptr, hash_enc);
  158. goto bail;
  159. }
  160. /* 1.5: Check we can agree the signature matches the payload */
  161. if (lws_jws_sig_confirm_compact_b64(buf, p - buf, &map, &jwk, context,
  162. lws_concat_temp(temp, temp_len), &temp_len) < 0) {
  163. lwsl_notice("%s: confirm sig failed\n", __func__);
  164. goto bail;
  165. }
  166. lws_jws_destroy(&jws);
  167. lws_jwk_destroy(&jwk);
  168. lws_jose_destroy(&jose);
  169. /* end */
  170. lwsl_notice("%s: selftest OK\n", __func__);
  171. return 0;
  172. bail_destroy_hmac:
  173. lws_genhmac_destroy(&ctx, NULL);
  174. bail:
  175. lws_jws_destroy(&jws);
  176. lws_jwk_destroy(&jwk);
  177. lws_jose_destroy(&jose);
  178. lwsl_err("%s: selftest failed ++++++++++++++++++++\n", __func__);
  179. return 1;
  180. }
  181. static const char
  182. /* the key from worked example in RFC7515 A-2, as a JWK */
  183. *rfc7515_rsa_key =
  184. "{\"kty\":\"RSA\","
  185. " \"n\":\"ofgWCuLjybRlzo0tZWJjNiuSfb4p4fAkd_wWJcyQoTbji9k0l8W26mPddx"
  186. "HmfHQp-Vaw-4qPCJrcS2mJPMEzP1Pt0Bm4d4QlL-yRT-SFd2lZS-pCgNMs"
  187. "D1W_YpRPEwOWvG6b32690r2jZ47soMZo9wGzjb_7OMg0LOL-bSf63kpaSH"
  188. "SXndS5z5rexMdbBYUsLA9e-KXBdQOS-UTo7WTBEMa2R2CapHg665xsmtdV"
  189. "MTBQY4uDZlxvb3qCo5ZwKh9kG4LT6_I5IhlJH7aGhyxXFvUK-DWNmoudF8"
  190. "NAco9_h9iaGNj8q2ethFkMLs91kzk2PAcDTW9gb54h4FRWyuXpoQ\","
  191. "\"e\":\"AQAB\","
  192. "\"d\":\"Eq5xpGnNCivDflJsRQBXHx1hdR1k6Ulwe2JZD50LpXyWPEAeP88vLNO97I"
  193. "jlA7_GQ5sLKMgvfTeXZx9SE-7YwVol2NXOoAJe46sui395IW_GO-pWJ1O0"
  194. "BkTGoVEn2bKVRUCgu-GjBVaYLU6f3l9kJfFNS3E0QbVdxzubSu3Mkqzjkn"
  195. "439X0M_V51gfpRLI9JYanrC4D4qAdGcopV_0ZHHzQlBjudU2QvXt4ehNYT"
  196. "CBr6XCLQUShb1juUO1ZdiYoFaFQT5Tw8bGUl_x_jTj3ccPDVZFD9pIuhLh"
  197. "BOneufuBiB4cS98l2SR_RQyGWSeWjnczT0QU91p1DhOVRuOopznQ\","
  198. "\"p\":\"4BzEEOtIpmVdVEZNCqS7baC4crd0pqnRH_5IB3jw3bcxGn6QLvnEtfdUdi"
  199. "YrqBdss1l58BQ3KhooKeQTa9AB0Hw_Py5PJdTJNPY8cQn7ouZ2KKDcmnPG"
  200. "BY5t7yLc1QlQ5xHdwW1VhvKn-nXqhJTBgIPgtldC-KDV5z-y2XDwGUc\","
  201. "\"q\":\"uQPEfgmVtjL0Uyyx88GZFF1fOunH3-7cepKmtH4pxhtCoHqpWmT8YAmZxa"
  202. "ewHgHAjLYsp1ZSe7zFYHj7C6ul7TjeLQeZD_YwD66t62wDmpe_HlB-TnBA"
  203. "-njbglfIsRLtXlnDzQkv5dTltRJ11BKBBypeeF6689rjcJIDEz9RWdc\","
  204. "\"dp\":\"BwKfV3Akq5_MFZDFZCnW-wzl-CCo83WoZvnLQwCTeDv8uzluRSnm71I3Q"
  205. "CLdhrqE2e9YkxvuxdBfpT_PI7Yz-FOKnu1R6HsJeDCjn12Sk3vmAktV2zb"
  206. "34MCdy7cpdTh_YVr7tss2u6vneTwrA86rZtu5Mbr1C1XsmvkxHQAdYo0\","
  207. "\"dq\":\"h_96-mK1R_7glhsum81dZxjTnYynPbZpHziZjeeHcXYsXaaMwkOlODsWa"
  208. "7I9xXDoRwbKgB719rrmI2oKr6N3Do9U0ajaHF-NKJnwgjMd2w9cjz3_-ky"
  209. "NlxAr2v4IKhGNpmM5iIgOS1VZnOZ68m6_pbLBSp3nssTdlqvd0tIiTHU\","
  210. "\"qi\":\"IYd7DHOhrWvxkwPQsRM2tOgrjbcrfvtQJipd-DlcxyVuuM9sQLdgjVk2o"
  211. "y26F0EmpScGLq2MowX7fhd_QJQ3ydy5cY7YIBi87w93IKLEdfnbJtoOPLU"
  212. "W0ITrJReOgo1cq9SbsxYawBgfp_gh6A5603k2-ZQwVK0JKSHuLFkuQ3U\""
  213. "}",
  214. *rfc7515_rsa_a1 = /* the signed worked example in RFC7515 A-1 */
  215. "eyJhbGciOiJSUzI1NiJ9"
  216. ".eyJpc3MiOiJqb2UiLA0KICJleHAiOjEzMDA4MTkzODAsDQogImh0dHA6Ly9leGFt"
  217. "cGxlLmNvbS9pc19yb290Ijp0cnVlfQ"
  218. ".cC4hiUPoj9Eetdgtv3hF80EGrhuB__dzERat0XF9g2VtQgr9PJbu3XOiZj5RZmh7"
  219. "AAuHIm4Bh-0Qc_lF5YKt_O8W2Fp5jujGbds9uJdbF9CUAr7t1dnZcAcQjbKBYNX4"
  220. "BAynRFdiuB--f_nZLgrnbyTyWzO75vRK5h6xBArLIARNPvkSjtQBMHlb1L07Qe7K"
  221. "0GarZRmB_eSN9383LcOLn6_dO--xi12jzDwusC-eOkHWEsqtFZESc6BfI7noOPqv"
  222. "hJ1phCnvWh6IeYI2w9QOYEUipUTI8np6LbgGY9Fs98rqVt5AXLIhWkWywlVmtVrB"
  223. "p0igcN_IoypGlUPQGe77Rw"
  224. ;
  225. int
  226. test_jws_RS256(struct lws_context *context)
  227. {
  228. struct lws_jws_map map;
  229. struct lws_jose jose;
  230. struct lws_jwk jwk;
  231. struct lws_jws jws;
  232. char temp[2048], *in;
  233. int n, l, temp_len = sizeof(temp);
  234. lws_jose_init(&jose);
  235. lws_jws_init(&jws, &jwk, context);
  236. /* Test 2: RS256 on RFC7515 worked example */
  237. if (lws_gencrypto_jws_alg_to_definition("RS256", &jose.alg)) {
  238. lwsl_err("%s: RS256 not supported\n", __func__);
  239. goto bail;
  240. }
  241. /* 2.1: import the jwk */
  242. if (lws_jwk_import(&jwk, NULL, NULL,
  243. rfc7515_rsa_key, strlen(rfc7515_rsa_key))) {
  244. lwsl_notice("%s: 2.2: Failed to read JWK key\n", __func__);
  245. goto bail2;
  246. }
  247. if (jwk.kty != LWS_GENCRYPTO_KTY_RSA) {
  248. lwsl_err("%s: 2.2: kty: %d instead of RSA\n", __func__, jwk.kty);
  249. goto bail;
  250. }
  251. /* 2.2: check the signature on the test packet from RFC7515 A-1 */
  252. if (lws_jws_sig_confirm_compact_b64(rfc7515_rsa_a1,
  253. strlen(rfc7515_rsa_a1), &map,
  254. &jwk, context, temp, &temp_len) < 0) {
  255. lwsl_notice("%s: 2.2: confirm rsa sig failed\n", __func__);
  256. goto bail;
  257. }
  258. if (lws_jws_b64_compact_map(rfc7515_rsa_a1, (int)strlen(rfc7515_rsa_a1),
  259. &jws.map_b64) != 3) {
  260. lwsl_notice("%s: lws_jws_b64_compact_map failed\n", __func__);
  261. goto bail;
  262. }
  263. /* 2.3: generate our own signature for a copy of the test packet */
  264. in = lws_concat_temp(temp, temp_len);
  265. l = (int)strlen(rfc7515_rsa_a1);
  266. if (temp_len < l + 1)
  267. goto bail;
  268. memcpy(in, rfc7515_rsa_a1, l + 1);
  269. temp_len -= l + 1;
  270. if (lws_jws_b64_compact_map(in, l, &jws.map_b64) != 3) {
  271. lwsl_notice("%s: lws_jws_b64_compact_map failed\n", __func__);
  272. goto bail;
  273. }
  274. /* overwrite the copy of the known b64 sig (it's all placed inside temp) */
  275. n = lws_jws_sign_from_b64(&jose, &jws,
  276. (char *)jws.map_b64.buf[LJWS_SIG],
  277. jws.map_b64.len[LJWS_SIG] + 8);
  278. if (n < 0) {
  279. lwsl_err("%s: failed signing test packet\n", __func__);
  280. goto bail;
  281. }
  282. jws.map_b64.len[LJWS_SIG] = n;
  283. /* 2.4: confirm our signature can be verified */
  284. in[l] = '\0';
  285. if (lws_jws_sig_confirm_compact_b64(in, l, &map, &jwk, context, lws_concat_temp(temp, temp_len), &temp_len) < 0) {
  286. lwsl_notice("%s: 2.2: confirm rsa sig failed\n", __func__);
  287. goto bail;
  288. }
  289. lws_jwk_destroy(&jwk);
  290. /* end */
  291. lwsl_notice("%s: selftest OK\n", __func__);
  292. return 0;
  293. bail:
  294. lws_jwk_destroy(&jwk);
  295. bail2:
  296. lws_jws_destroy(&jws);
  297. lwsl_err("%s: selftest failed ++++++++++++++++++++\n", __func__);
  298. return 1;
  299. }
  300. static const char
  301. *es256_jose = "{\"alg\":\"ES256\"}",
  302. *es256_payload = "{\"iss\":\"joe\",\r\n \"exp\":1300819380,\r\n"
  303. " \"http://example.com/is_root\":true}",
  304. *es256_cser =
  305. "eyJhbGciOiJFUzI1NiJ9"
  306. "."
  307. "eyJpc3MiOiJqb2UiLA0KICJleHAiOjEzMDA4MTkzODAsDQogImh0dHA6Ly9leGFt"
  308. "cGxlLmNvbS9pc19yb290Ijp0cnVlfQ"
  309. "."
  310. "DtEhU3ljbEg8L38VWAfUAqOyKAM6-Xx-F4GawxaepmXFCgfTjDxw5djxLa8ISlSA"
  311. "pmWQxfKTUJqPP3-Kg6NU1Q",
  312. *es256_jwk =
  313. "{"
  314. "\"kty\":\"EC\","
  315. "\"crv\":\"P-256\","
  316. "\"x\":\"f83OJ3D2xF1Bg8vub9tLe1gHMzV76e8Tus9uPHvRVEU\","
  317. "\"y\":\"x_FEzRu9m36HLN_tue659LNpXW6pCyStikYjKIWI5a0\","
  318. "\"d\":\"jpsQnnGQmL-YBIffH1136cspYG6-0iY7X1fCE9-E9LI\""
  319. "}"
  320. #if 0
  321. ,
  322. rfc7515_ec_a3_R[] = {
  323. 14, 209, 33, 83, 121, 99, 108, 72, 60, 47, 127, 21, 88,
  324. 7, 212, 2, 163, 178, 40, 3, 58, 249, 124, 126, 23, 129,
  325. 154, 195, 22, 158, 166, 101
  326. },
  327. rfc7515_ec_a3_S[] = {
  328. 197, 10, 7, 211, 140, 60, 112, 229, 216, 241, 45, 175,
  329. 8, 74, 84, 128, 166, 101, 144, 197, 242, 147, 80, 154,
  330. 143, 63, 127, 138, 131, 163, 84, 213
  331. }
  332. #endif
  333. ;
  334. int
  335. test_jws_ES256(struct lws_context *context)
  336. {
  337. uint8_t digest[LWS_GENHASH_LARGEST];
  338. struct lws_genhash_ctx hash_ctx;
  339. struct lws_jws_map map;
  340. struct lws_jose jose;
  341. struct lws_jwk jwk;
  342. struct lws_jws jws;
  343. char temp[2048], *p;
  344. int ret = -1, l, n, temp_len = sizeof(temp);
  345. /* A.3 "ES256" RFC7515 worked example - verify */
  346. lws_jose_init(&jose);
  347. /* decode the b64.b64[.b64] compact serialization blocks */
  348. if (lws_jws_compact_decode(es256_cser, (int)strlen(es256_cser),
  349. &jws.map, &jws.map_b64,
  350. temp, &temp_len) != 3) {
  351. lwsl_err("%s: concat_map failed\n", __func__);
  352. goto bail;
  353. }
  354. /* confirm the decoded JOSE header is exactly what we expect */
  355. if (jws.map.len[LJWS_JOSE] != strlen(es256_jose) ||
  356. strncmp(es256_jose, jws.map.buf[LJWS_JOSE],
  357. jws.map.len[LJWS_JOSE])) {
  358. lwsl_err("%s: jose b64 decode wrong\n", __func__);
  359. goto bail;
  360. }
  361. /* confirm the decoded payload is exactly what we expect */
  362. if (jws.map.len[LJWS_PYLD] != strlen(es256_payload) ||
  363. strncmp(es256_payload, jws.map.buf[LJWS_PYLD],
  364. jws.map.len[LJWS_PYLD])) {
  365. lwsl_err("%s: payload b64 decode wrong\n", __func__);
  366. goto bail;
  367. }
  368. /* parse the JOSE header */
  369. if (lws_jws_parse_jose(&jose, jws.map.buf[LJWS_JOSE],
  370. jws.map.len[LJWS_JOSE],
  371. (char *)lws_concat_temp(temp, temp_len), &temp_len) < 0) {
  372. lwsl_err("%s: JOSE parse failed\n", __func__);
  373. goto bail;
  374. }
  375. /* confirm we used "ES256" alg we expect from the JOSE hdr */
  376. if (strcmp(jose.alg->alg, "ES256")) {
  377. lwsl_err("%s: JOSE header has wrong alg\n", __func__);
  378. goto bail;
  379. }
  380. jws.jwk = &jwk;
  381. jws.context = context;
  382. /* import the ES256 jwk */
  383. if (lws_jwk_import(&jwk, NULL, NULL, es256_jwk, strlen(es256_jwk))) {
  384. lwsl_notice("%s: Failed to read JWK key\n", __func__);
  385. goto bail;
  386. }
  387. /* sanity */
  388. if (jwk.kty != LWS_GENCRYPTO_KTY_EC) {
  389. lwsl_err("%s: kty: %d instead of EC\n",
  390. __func__, jwk.kty);
  391. goto bail1;
  392. }
  393. if (lws_jws_sig_confirm(&jws.map_b64, &jws.map, &jwk, context) < 0) {
  394. lwsl_notice("%s: confirm EC sig failed\n", __func__);
  395. goto bail1;
  396. }
  397. /* A.3 "ES256" RFC7515 worked example - sign */
  398. l = (int)strlen(es256_cser);
  399. if (temp_len < l + 1)
  400. goto bail1;
  401. p = lws_concat_temp(temp, temp_len);
  402. memcpy(p, es256_cser, l + 1);
  403. temp_len -= l + 1;
  404. /* scan the b64 compact serialization string to map the blocks */
  405. if (lws_jws_b64_compact_map(p, l, &jws.map_b64) != 3)
  406. goto bail1;
  407. /* create the hash of the protected b64 part */
  408. if (lws_genhash_init(&hash_ctx, jose.alg->hash_type) ||
  409. lws_genhash_update(&hash_ctx, jws.map_b64.buf[LJWS_JOSE],
  410. jws.map_b64.len[LJWS_JOSE]) ||
  411. lws_genhash_update(&hash_ctx, ".", 1) ||
  412. lws_genhash_update(&hash_ctx, jws.map_b64.buf[LJWS_PYLD],
  413. jws.map_b64.len[LJWS_PYLD]) ||
  414. lws_genhash_destroy(&hash_ctx, digest)) {
  415. lws_genhash_destroy(&hash_ctx, NULL);
  416. goto bail1;
  417. }
  418. lwsl_hexdump(jws.map_b64.buf[LJWS_SIG], jws.map_b64.len[LJWS_SIG]);
  419. /* overwrite the copy of the known b64 sig (it's placed inside buf) */
  420. n = lws_jws_sign_from_b64(&jose, &jws,
  421. (char *)jws.map_b64.buf[LJWS_SIG],
  422. jws.map_b64.len[LJWS_SIG] + 8);
  423. if (n < 0) {
  424. lwsl_err("%s: failed signing test packet\n", __func__);
  425. goto bail1;
  426. }
  427. jws.map_b64.len[LJWS_SIG] = n;
  428. lwsl_hexdump(jws.map_b64.buf[LJWS_SIG], jws.map_b64.len[LJWS_SIG]);
  429. /* 2.4: confirm our generated signature can be verified */
  430. // lwsl_err("p %p, l %d\n", p, (int)l);
  431. p[l] = '\0';
  432. if (lws_jws_sig_confirm_compact_b64(p, l, &map, &jwk, context, lws_concat_temp(temp, temp_len), &temp_len) < 0) {
  433. lwsl_notice("%s: confirm our EC sig failed\n", __func__);
  434. goto bail1;
  435. }
  436. /* end */
  437. ret = 0;
  438. bail1:
  439. lws_jwk_destroy(&jwk);
  440. lws_jose_destroy(&jose);
  441. bail:
  442. lwsl_notice("%s: selftest %s\n", __func__, ret ? "FAIL" : "OK");
  443. return ret;
  444. }
  445. static const char
  446. *es512_jose = "{\"alg\":\"ES512\"}",
  447. *es512_payload = "Payload",
  448. *es512_cser =
  449. "eyJhbGciOiJFUzUxMiJ9"
  450. "."
  451. "UGF5bG9hZA"
  452. "."
  453. "AdwMgeerwtHoh-l192l60hp9wAHZFVJbLfD_UxMi70cwnZOYaRI1bKPWROc-mZZq"
  454. "wqT2SI-KGDKB34XO0aw_7XdtAG8GaSwFKdCAPZgoXD2YBJZCPEX3xKpRwcdOO8Kp"
  455. "EHwJjyqOgzDO7iKvU8vcnwNrmxYbSW9ERBXukOXolLzeO_Jn",
  456. *es512_jwk =
  457. "{"
  458. "\"kty\":\"EC\","
  459. "\"crv\":\"P-521\","
  460. "\"x\":\"AekpBQ8ST8a8VcfVOTNl353vSrDCLLJXmPk06wTjxrrjcBpXp5EOnYG_"
  461. "NjFZ6OvLFV1jSfS9tsz4qUxcWceqwQGk\","
  462. "\"y\":\"ADSmRA43Z1DSNx_RvcLI87cdL07l6jQyyBXMoxVg_l2Th-x3S1WDhjDl"
  463. "y79ajL4Kkd0AZMaZmh9ubmf63e3kyMj2\","
  464. "\"d\":\"AY5pb7A0UFiB3RELSD64fTLOSV_jazdF7fLYyuTw8lOfRhWg6Y6rUrPA"
  465. "xerEzgdRhajnu0ferB0d53vM9mE15j2C\""
  466. "}"
  467. ;
  468. int
  469. test_jws_ES512(struct lws_context *context)
  470. {
  471. uint8_t digest[LWS_GENHASH_LARGEST];
  472. struct lws_genhash_ctx hash_ctx;
  473. struct lws_jws_map map;
  474. struct lws_jose jose;
  475. struct lws_jwk jwk;
  476. struct lws_jws jws;
  477. char temp[2048], *p;
  478. int ret = -1, l, n, temp_len = sizeof(temp);
  479. /* A.4 "ES512" RFC7515 worked example - verify */
  480. lws_jose_init(&jose);
  481. /* decode the b64.b64[.b64] compact serialization blocks */
  482. if (lws_jws_compact_decode(es512_cser, (int)strlen(es512_cser),
  483. &jws.map, &jws.map_b64, temp,
  484. &temp_len) != 3) {
  485. lwsl_err("%s: concat_map failed\n", __func__);
  486. goto bail;
  487. }
  488. /* confirm the decoded JOSE header is exactly what we expect */
  489. if (jws.map.len[LJWS_JOSE] != strlen(es512_jose) ||
  490. strncmp(es512_jose, jws.map.buf[LJWS_JOSE],
  491. jws.map.len[LJWS_JOSE])) {
  492. lwsl_err("%s: jose b64 decode wrong\n", __func__);
  493. goto bail;
  494. }
  495. /* confirm the decoded payload is exactly what we expect */
  496. if (jws.map.len[LJWS_PYLD] != strlen(es512_payload) ||
  497. strncmp(es512_payload, jws.map.buf[LJWS_PYLD],
  498. jws.map.len[LJWS_PYLD])) {
  499. lwsl_err("%s: payload b64 decode wrong\n", __func__);
  500. goto bail;
  501. }
  502. /* parse the JOSE header */
  503. if (lws_jws_parse_jose(&jose, jws.map.buf[LJWS_JOSE],
  504. jws.map.len[LJWS_JOSE],
  505. lws_concat_temp(temp, temp_len), &temp_len) < 0) {
  506. lwsl_err("%s: JOSE parse failed\n", __func__);
  507. goto bail;
  508. }
  509. /* confirm we used "es512" alg we expect from the JOSE hdr */
  510. if (strcmp(jose.alg->alg, "ES512")) {
  511. lwsl_err("%s: JOSE header has wrong alg\n", __func__);
  512. goto bail;
  513. }
  514. jws.jwk = &jwk;
  515. jws.context = context;
  516. /* import the es512 jwk */
  517. if (lws_jwk_import(&jwk, NULL, NULL, es512_jwk, strlen(es512_jwk))) {
  518. lwsl_notice("%s: Failed to read JWK key\n", __func__);
  519. goto bail;
  520. }
  521. /* sanity */
  522. if (jwk.kty != LWS_GENCRYPTO_KTY_EC) {
  523. lwsl_err("%s: kty: %d instead of EC\n",
  524. __func__, jwk.kty);
  525. goto bail1;
  526. }
  527. if (lws_jws_sig_confirm(&jws.map_b64, &jws.map, &jwk, context) < 0) {
  528. lwsl_notice("%s: confirm EC sig failed\n", __func__);
  529. goto bail1;
  530. }
  531. /* A.3 "es512" RFC7515 worked example - sign */
  532. l = (int)strlen(es512_cser);
  533. if (temp_len < l)
  534. goto bail1;
  535. p = lws_concat_temp(temp, temp_len);
  536. memcpy(p, es512_cser, l + 1);
  537. temp_len -= (l + 1);
  538. /* scan the b64 compact serialization string to map the blocks */
  539. if (lws_jws_b64_compact_map(p, l, &jws.map_b64) != 3)
  540. goto bail1;
  541. /* create the hash of the protected b64 part */
  542. if (lws_genhash_init(&hash_ctx, jose.alg->hash_type) ||
  543. lws_genhash_update(&hash_ctx, jws.map_b64.buf[LJWS_JOSE],
  544. jws.map_b64.len[LJWS_JOSE]) ||
  545. lws_genhash_update(&hash_ctx, ".", 1) ||
  546. lws_genhash_update(&hash_ctx, jws.map_b64.buf[LJWS_PYLD],
  547. jws.map_b64.len[LJWS_PYLD]) ||
  548. lws_genhash_destroy(&hash_ctx, digest)) {
  549. lws_genhash_destroy(&hash_ctx, NULL);
  550. goto bail1;
  551. }
  552. /* overwrite the copy of the known b64 sig (it's placed inside buf) */
  553. n = lws_jws_sign_from_b64(&jose, &jws,
  554. (char *)jws.map_b64.buf[LJWS_SIG], 1024);
  555. if (n < 0) {
  556. lwsl_err("%s: failed signing test packet\n", __func__);
  557. goto bail1;
  558. }
  559. jws.map_b64.len[LJWS_SIG] = n;
  560. /* 2.4: confirm our generated signature can be verified */
  561. p[l] = '\0';
  562. if (lws_jws_sig_confirm_compact_b64(p, l, &map, &jwk, context,
  563. lws_concat_temp(temp, temp_len), &temp_len) < 0) {
  564. lwsl_notice("%s: confirm our ECDSA sig failed\n", __func__);
  565. goto bail1;
  566. }
  567. /* jwt test */
  568. {
  569. unsigned long long ull = lws_now_secs();
  570. char buf[8192];
  571. size_t cml = 2048, cml2 = 2048;
  572. if (lws_jwt_sign_compact(context, &jwk, "ES512",
  573. (char *)buf, &cml2,
  574. (char *)buf + 2048, 4096,
  575. "{\"iss\":\"warmcat.com\",\"aud\":"
  576. "\"https://libwebsockets.org/sai\","
  577. "\"iat\":%llu,"
  578. "\"nbf\":%llu,"
  579. "\"exp\":%llu,"
  580. "\"sub\":\"manage\"}", ull,
  581. ull - 60, ull + (30 * 24 * 3600)
  582. )) {
  583. lwsl_err("%s: failed to create JWT\n", __func__);
  584. goto bail1;
  585. }
  586. lwsl_notice("%s: jwt test '%s'\n", __func__, buf);
  587. if (lws_jwt_signed_validate(context, &jwk, "ES512",
  588. (const char *)buf, cml2,
  589. (char *)buf + 2048, 2048,
  590. (char *)buf + 4096, &cml)) {
  591. lwsl_err("%s: failed to parse JWT\n", __func__);
  592. goto bail1;
  593. }
  594. lwsl_notice("%s: jwt valid, payload '%s'\n",
  595. __func__, buf + 4096);
  596. }
  597. /* end */
  598. ret = 0;
  599. bail1:
  600. lws_jwk_destroy(&jwk);
  601. lws_jose_destroy(&jose);
  602. bail:
  603. lwsl_notice("%s: selftest %s\n", __func__, ret ? "FAIL" : "OK");
  604. return ret;
  605. }
  606. int
  607. test_jws(struct lws_context *context)
  608. {
  609. int n = 0;
  610. n |= test_jws_none(context);
  611. n |= test_jws_HS256(context);
  612. n |= test_jws_RS256(context);
  613. n |= test_jws_ES256(context);
  614. n |= test_jws_ES512(context);
  615. return n;
  616. }