kex-25519.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010 - 2019 Andy Green <[email protected]>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to
  8. * deal in the Software without restriction, including without limitation the
  9. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10. * sell copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. * IN THE SOFTWARE.
  23. */
  24. #include "libwebsockets.h"
  25. #include "lws-ssh.h"
  26. #include <string.h>
  27. /*
  28. * ssh-keygen -t ed25519
  29. * head -n-1 srv-key-25519 | tail -n +2 | base64 -d | hexdump -C
  30. */
  31. static void
  32. lws_sized_blob(uint8_t **p, void *blob, uint32_t len)
  33. {
  34. lws_p32((*p), len);
  35. *p += 4;
  36. memcpy(*p, blob, len);
  37. *p += len;
  38. }
  39. static const char key_leadin[] = "openssh-key-v1\x00\x00\x00\x00\x04none"
  40. "\x00\x00\x00\x04none\x00"
  41. "\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x33"
  42. "\x00\x00\x00\x0bssh-ed25519\x00\x00\x00\x20",
  43. key_sep[] = "\x00\x00\x00\x90\xb1\x4f\xa7\x28"
  44. "\xb1\x4f\xa7\x28\x00\x00\x00\x0bssh-ed25519"
  45. "\x00\x00\x00\x20",
  46. key_privl[] = "\x00\x00\x00\x40",
  47. key_trail[] = "\x00\x00\x00\x0cself-gen@cbl\x01";
  48. static size_t
  49. lws_gen_server_key_ed25519(struct lws_context *context, uint8_t *buf256,
  50. size_t max_len)
  51. {
  52. uint8_t *p = buf256 + sizeof(key_leadin) - 1;
  53. if (max_len < sizeof(key_leadin) - 1 + 32 + sizeof(key_sep) - 1 + 32 +
  54. sizeof(key_privl) - 1 + 64 + sizeof(key_trail) - 1)
  55. return 0;
  56. memcpy(buf256, key_leadin, sizeof(key_leadin) - 1);
  57. crypto_sign_ed25519_keypair(context, p, p + 32 + sizeof(key_sep) - 1 +
  58. 32 + sizeof(key_privl) - 1);
  59. memcpy(p + 32 + sizeof(key_sep) - 1, p, 32);
  60. p += 32;
  61. memcpy(p, key_sep, sizeof(key_sep) - 1);
  62. p += sizeof(key_sep) - 1 + 32;
  63. memcpy(p, key_privl, sizeof(key_privl) - 1);
  64. p += sizeof(key_privl) - 1 + 64;
  65. memcpy(p, key_trail, sizeof(key_trail) - 1);
  66. p += sizeof(key_trail) - 1;
  67. lwsl_notice("%s: Generated key len %ld\n", __func__, (long)(p - buf256));
  68. return p - buf256;
  69. }
  70. static int
  71. lws_mpint_rfc4251(uint8_t *dest, const uint8_t *src, int bytes, int uns)
  72. {
  73. uint8_t *odest = dest;
  74. while (!*src && bytes > 1) {
  75. src++;
  76. bytes--;
  77. }
  78. if (!*src) {
  79. *dest++ = 0;
  80. *dest++ = 0;
  81. *dest++ = 0;
  82. *dest++ = 0;
  83. return 4;
  84. }
  85. if (uns && (*src) & 0x80)
  86. bytes++;
  87. *dest++ = bytes >> 24;
  88. *dest++ = bytes >> 16;
  89. *dest++ = bytes >> 8;
  90. *dest++ = bytes;
  91. if (uns && (*src) & 0x80) {
  92. *dest++ = 0;
  93. bytes--;
  94. }
  95. while (bytes--)
  96. *dest++ = *src++;
  97. return lws_ptr_diff(dest, odest);
  98. }
  99. int
  100. ed25519_key_parse(uint8_t *p, size_t len, char *type, size_t type_len,
  101. uint8_t *pub, uint8_t *pri)
  102. {
  103. uint32_t l, publ, m;
  104. uint8_t *op = p;
  105. if (len < 180)
  106. return 1;
  107. if (memcmp(p, "openssh-key-v1", 14))
  108. return 2;
  109. p += 15;
  110. l = lws_g32(&p); /* ciphername */
  111. if (l != 4 || memcmp(p, "none", 4))
  112. return 3;
  113. p += l;
  114. l = lws_g32(&p); /* kdfname */
  115. if (l != 4 || memcmp(p, "none", 4))
  116. return 4;
  117. p += l;
  118. l = lws_g32(&p); /* kdfoptions */
  119. if (l)
  120. return 5;
  121. l = lws_g32(&p); /* number of keys */
  122. if (l != 1)
  123. return 6;
  124. publ = lws_g32(&p); /* length of pubkey block */
  125. if ((size_t)((p - op) + publ) >= len)
  126. return 7;
  127. l = lws_g32(&p); /* key type length */
  128. if (l > 31)
  129. return 8;
  130. m = l;
  131. if (m >= type_len)
  132. m = (uint32_t)type_len -1 ;
  133. lws_strncpy(type, (const char *)p, m + 1);
  134. p += l;
  135. l = lws_g32(&p); /* pub key length */
  136. if (l != 32)
  137. return 10;
  138. p += l;
  139. publ = lws_g32(&p); /* length of private key block */
  140. if ((size_t)((p - op) + publ) != len)
  141. return 11;
  142. l = lws_g32(&p); /* checkint 1 */
  143. if (lws_g32(&p) != l) /* must match checkint 2 */
  144. return 12;
  145. l = lws_g32(&p); /* key type length */
  146. p += l;
  147. l = lws_g32(&p); /* public key part length */
  148. if (l != LWS_SIZE_EC25519_PUBKEY)
  149. return 15;
  150. if (pub)
  151. memcpy(pub, p, LWS_SIZE_EC25519_PUBKEY);
  152. p += l;
  153. l = lws_g32(&p); /* private key part length */
  154. if (l != LWS_SIZE_EC25519_PRIKEY)
  155. return 16;
  156. if (pri)
  157. memcpy(pri, p, LWS_SIZE_EC25519_PRIKEY);
  158. return 0;
  159. }
  160. static int
  161. _genhash_update_len(struct lws_genhash_ctx *ctx, const void *input, size_t ilen)
  162. {
  163. uint32_t be;
  164. lws_p32((uint8_t *)&be, (uint32_t)ilen);
  165. if (lws_genhash_update(ctx, (uint8_t *)&be, 4))
  166. return 1;
  167. if (lws_genhash_update(ctx, input, ilen))
  168. return 1;
  169. return 0;
  170. }
  171. static int
  172. kex_ecdh_dv(uint8_t *dest, int dest_len, const uint8_t *kbi, int kbi_len,
  173. const uint8_t *H, char c, const uint8_t *session_id)
  174. {
  175. uint8_t pool[LWS_SIZE_SHA256];
  176. struct lws_genhash_ctx ctx;
  177. int n = 0, m;
  178. /*
  179. * Key data MUST be taken from the beginning of the hash output.
  180. * As many bytes as needed are taken from the beginning of the hash
  181. * value.
  182. *
  183. * If the key length needed is longer than the output of the HASH,
  184. * the key is extended by computing HASH of the concatenation of K
  185. * and H and the entire key so far, and appending the resulting
  186. * bytes (as many as HASH generates) to the key. This process is
  187. * repeated until enough key material is available; the key is taken
  188. * from the beginning of this value. In other words:
  189. *
  190. * K1 = HASH(K || H || X || session_id) (X is e.g., "A")
  191. * K2 = HASH(K || H || K1)
  192. * K3 = HASH(K || H || K1 || K2)
  193. * ...
  194. * key = K1 || K2 || K3 || ...
  195. */
  196. while (n < dest_len) {
  197. if (lws_genhash_init(&ctx, LWS_GENHASH_TYPE_SHA256))
  198. return 1;
  199. if (lws_genhash_update(&ctx, kbi, kbi_len))
  200. goto hash_failed;
  201. if (lws_genhash_update(&ctx, H, LWS_SIZE_SHA256))
  202. goto hash_failed;
  203. if (!n) {
  204. if (lws_genhash_update(&ctx, (void *)&c, 1))
  205. goto hash_failed;
  206. if (lws_genhash_update(&ctx, session_id,
  207. LWS_SIZE_EC25519))
  208. goto hash_failed;
  209. } else
  210. if (lws_genhash_update(&ctx, pool, LWS_SIZE_EC25519))
  211. goto hash_failed;
  212. lws_genhash_destroy(&ctx, pool);
  213. m = LWS_SIZE_EC25519;
  214. if (m > (dest_len - n))
  215. m = dest_len - n;
  216. memcpy(dest, pool, m);
  217. n += m;
  218. dest += m;
  219. }
  220. return 0;
  221. hash_failed:
  222. lws_genhash_destroy(&ctx, NULL);
  223. return 1;
  224. }
  225. static const unsigned char basepoint[32] = { 9 };
  226. size_t
  227. get_gen_server_key_25519(struct per_session_data__sshd *pss, uint8_t *b,
  228. size_t len)
  229. {
  230. size_t s, mylen;
  231. mylen = pss->vhd->ops->get_server_key(pss->wsi, b, len);
  232. if (mylen)
  233. return mylen;
  234. /* create one then */
  235. lwsl_notice("Generating server hostkey\n");
  236. s = lws_gen_server_key_ed25519(pss->vhd->context, b, len);
  237. lwsl_notice(" gen key len %ld\n", (long)s);
  238. if (!s)
  239. return 0;
  240. /* set the key */
  241. if (!pss->vhd->ops->set_server_key(pss->wsi, b, s))
  242. return 0;
  243. /* new key stored OK */
  244. return s;
  245. }
  246. int
  247. kex_ecdh(struct per_session_data__sshd *pss, uint8_t *reply, uint32_t *plen)
  248. {
  249. uint8_t pri_key[64], temp[64], payload_sig[64 + 32], a, *lp, kbi[64];
  250. struct lws_kex *kex = pss->kex;
  251. struct lws_genhash_ctx ctx;
  252. unsigned long long smlen;
  253. uint8_t *p = reply + 5;
  254. uint32_t be, kbi_len;
  255. uint8_t servkey[256];
  256. char keyt[33];
  257. int r, c;
  258. r = (int)get_gen_server_key_25519(pss, servkey, (int)sizeof(servkey));
  259. if (!r) {
  260. lwsl_err("%s: Failed to get or gen server key\n", __func__);
  261. return 1;
  262. }
  263. r = ed25519_key_parse(servkey, r, keyt, sizeof(keyt),
  264. pss->K_S /* public key */, pri_key);
  265. if (r) {
  266. lwsl_notice("%s: server key parse failed: %d\n", __func__, r);
  267. return 1;
  268. }
  269. keyt[32] = '\0';
  270. lwsl_info("Server key type: %s\n", keyt);
  271. /*
  272. * 1) Generate ephemeral key pair [ eph_pri_key | kex->Q_S ]
  273. * 2) Compute shared secret.
  274. * 3) Generate and sign exchange hash.
  275. *
  276. * 1) A 32 bytes private key should be generated for each new
  277. * connection, using a secure PRNG. The following actions
  278. * must be done on the private key:
  279. *
  280. * mysecret[0] &= 248;
  281. * mysecret[31] &= 127;
  282. * mysecret[31] |= 64;
  283. */
  284. lws_get_random(pss->vhd->context, kex->eph_pri_key, LWS_SIZE_EC25519);
  285. kex->eph_pri_key[0] &= 248;
  286. kex->eph_pri_key[31] &= 127;
  287. kex->eph_pri_key[31] |= 64;
  288. /*
  289. * 2) The public key is calculated using the cryptographic scalar
  290. * multiplication:
  291. *
  292. * const unsigned char privkey[32];
  293. * unsigned char pubkey[32];
  294. *
  295. * crypto_scalarmult (pubkey, privkey, basepoint);
  296. */
  297. crypto_scalarmult_curve25519(kex->Q_S, kex->eph_pri_key, basepoint);
  298. a = 0;
  299. for (r = 0; r < (int)sizeof(kex->Q_S); r++)
  300. a |= kex->Q_S[r];
  301. if (!a) {
  302. lwsl_notice("all zero pubkey\n");
  303. return SSH_DISCONNECT_KEY_EXCHANGE_FAILED;
  304. }
  305. /*
  306. * The shared secret, k, is defined in SSH specifications to be a big
  307. * integer. This number is calculated using the following procedure:
  308. *
  309. * X is the 32 bytes point obtained by the scalar multiplication of
  310. * the other side's public key and the local private key scalar.
  311. */
  312. crypto_scalarmult_curve25519(pss->K, kex->eph_pri_key, kex->Q_C);
  313. /*
  314. * The whole 32 bytes of the number X are then converted into a big
  315. * integer k. This conversion follows the network byte order. This
  316. * step differs from RFC5656.
  317. */
  318. kbi_len = lws_mpint_rfc4251(kbi, pss->K, LWS_SIZE_EC25519, 1);
  319. /*
  320. * The exchange hash H is computed as the hash of the concatenation of
  321. * the following:
  322. *
  323. * string V_C, the client's identification string (CR and LF
  324. * excluded)
  325. * string V_S, the server's identification string (CR and LF
  326. * excluded)
  327. * string I_C, the payload of the client's SSH_MSG_KEXINIT
  328. * string I_S, the payload of the server's SSH_MSG_KEXINIT
  329. * string K_S, the host key
  330. * mpint Q_C, exchange value sent by the client
  331. * mpint Q_S, exchange value sent by the server
  332. * mpint K, the shared secret
  333. *
  334. * However there are a lot of unwritten details in the hash
  335. * definition...
  336. */
  337. if (lws_genhash_init(&ctx, LWS_GENHASH_TYPE_SHA256)) {
  338. lwsl_notice("genhash init failed\n");
  339. return 1;
  340. }
  341. if (_genhash_update_len(&ctx, pss->V_C, strlen(pss->V_C)))
  342. goto hash_probs;
  343. if (_genhash_update_len(&ctx, pss->vhd->ops->server_string, /* aka V_S */
  344. strlen(pss->vhd->ops->server_string)))
  345. goto hash_probs;
  346. if (_genhash_update_len(&ctx, kex->I_C, kex->I_C_payload_len))
  347. goto hash_probs;
  348. if (_genhash_update_len(&ctx, kex->I_S, kex->I_S_payload_len))
  349. goto hash_probs;
  350. /*
  351. * K_S (host public key)
  352. *
  353. * sum of name + key lengths and headers
  354. * name length: name
  355. * key length: key
  356. * ---> */
  357. lws_p32((uint8_t *)&be, 8 + (int)strlen(keyt) + LWS_SIZE_EC25519);
  358. if (lws_genhash_update(&ctx, (void *)&be, 4))
  359. goto hash_probs;
  360. if (_genhash_update_len(&ctx, keyt, strlen(keyt)))
  361. goto hash_probs;
  362. if (_genhash_update_len(&ctx, pss->K_S, LWS_SIZE_EC25519))
  363. goto hash_probs;
  364. /* <---- */
  365. if (_genhash_update_len(&ctx, kex->Q_C, LWS_SIZE_EC25519))
  366. goto hash_probs;
  367. if (_genhash_update_len(&ctx, kex->Q_S, LWS_SIZE_EC25519))
  368. goto hash_probs;
  369. if (lws_genhash_update(&ctx, kbi, kbi_len))
  370. goto hash_probs;
  371. if (lws_genhash_destroy(&ctx, temp))
  372. goto hash_probs;
  373. /*
  374. * Sign the 32-byte SHA256 "exchange hash" in temp
  375. * The signature is itself 64 bytes
  376. */
  377. smlen = LWS_SIZE_EC25519 + 64;
  378. if (crypto_sign_ed25519(payload_sig, &smlen, temp, LWS_SIZE_EC25519,
  379. pri_key))
  380. return 1;
  381. #if 0
  382. l = LWS_SIZE_EC25519;
  383. n = crypto_sign_ed25519_open(temp, &l, payload_sig, smlen, pss->K_S);
  384. lwsl_notice("own sig sanity check says %d\n", n);
  385. #endif
  386. /* sig [64] and payload [32] concatenated in payload_sig
  387. *
  388. * The server then responds with the following
  389. *
  390. * uint32 packet length (exl self + mac)
  391. * byte padding len
  392. * byte SSH_MSG_KEX_ECDH_REPLY
  393. * string server public host key and certificates (K_S)
  394. * string Q_S (exchange value sent by the server)
  395. * string signature of H
  396. * padding
  397. */
  398. *p++ = SSH_MSG_KEX_ECDH_REPLY;
  399. /* server public host key and certificates (K_S) */
  400. lp = p;
  401. p +=4;
  402. lws_sized_blob(&p, keyt, (int)strlen(keyt));
  403. lws_sized_blob(&p, pss->K_S, LWS_SIZE_EC25519);
  404. lws_p32(lp, lws_ptr_diff(p, lp) - 4);
  405. /* Q_S (exchange value sent by the server) */
  406. lws_sized_blob(&p, kex->Q_S, LWS_SIZE_EC25519);
  407. /* signature of H */
  408. lp = p;
  409. p +=4;
  410. lws_sized_blob(&p, keyt, (int)strlen(keyt));
  411. lws_sized_blob(&p, payload_sig, 64);
  412. lws_p32(lp, lws_ptr_diff(p, lp) - 4);
  413. /* end of message */
  414. lws_pad_set_length(pss, reply, &p, &pss->active_keys_stc);
  415. *plen = lws_ptr_diff(p, reply);
  416. if (!pss->active_keys_stc.valid)
  417. memcpy(pss->session_id, temp, LWS_SIZE_EC25519);
  418. /* RFC4253 7.2:
  419. *
  420. * The key exchange produces two values: a shared secret K,
  421. * and an exchange hash H. Encryption and authentication
  422. * keys are derived from these. The exchange hash H from the
  423. * first key exchange is additionally used as the session
  424. * identifier, which is a unique identifier for this connection.
  425. * It is used by authentication methods as a part of the data
  426. * that is signed as a proof of possession of a private key.
  427. * Once computed, the session identifier is not changed,
  428. * even if keys are later re-exchanged.
  429. *
  430. * The hash alg used in the KEX must be used for key derivation.
  431. *
  432. * 1) Initial IV client to server:
  433. *
  434. * HASH(K || H || "A" || session_id)
  435. *
  436. * (Here K is encoded as mpint and "A" as byte and session_id
  437. * as raw data. "A" means the single character A, ASCII 65).
  438. *
  439. *
  440. */
  441. for (c = 0; c < 3; c++) {
  442. kex_ecdh_dv(kex->keys_next_cts.key[c], LWS_SIZE_CHACHA256_KEY,
  443. kbi, kbi_len, temp, 'A' + (c * 2), pss->session_id);
  444. kex_ecdh_dv(kex->keys_next_stc.key[c], LWS_SIZE_CHACHA256_KEY,
  445. kbi, kbi_len, temp, 'B' + (c * 2), pss->session_id);
  446. }
  447. lws_explicit_bzero(temp, sizeof(temp));
  448. return 0;
  449. hash_probs:
  450. lws_genhash_destroy(&ctx, NULL);
  451. return 1;
  452. }