shared.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650
  1. /*
  2. * ngtcp2
  3. *
  4. * Copyright (c) 2019 ngtcp2 contributors
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sublicense, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  21. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. #include "shared.h"
  26. #ifdef WIN32
  27. # include <winsock2.h>
  28. # include <ws2tcpip.h>
  29. #elif defined(HAVE_NETINET_IN_H)
  30. # include <netinet/in.h>
  31. #endif /* defined(HAVE_NETINET_IN_H) */
  32. #include <string.h>
  33. #include <assert.h>
  34. #include "ngtcp2_macro.h"
  35. #include "ngtcp2_net.h"
  36. ngtcp2_crypto_md *ngtcp2_crypto_md_init(ngtcp2_crypto_md *md,
  37. void *md_native_handle) {
  38. md->native_handle = md_native_handle;
  39. return md;
  40. }
  41. int ngtcp2_crypto_hkdf_expand_label(uint8_t *dest, size_t destlen,
  42. const ngtcp2_crypto_md *md,
  43. const uint8_t *secret, size_t secretlen,
  44. const uint8_t *label, size_t labellen) {
  45. static const uint8_t LABEL[] = "tls13 ";
  46. uint8_t info[256];
  47. uint8_t *p = info;
  48. *p++ = (uint8_t)(destlen / 256);
  49. *p++ = (uint8_t)(destlen % 256);
  50. *p++ = (uint8_t)(sizeof(LABEL) - 1 + labellen);
  51. memcpy(p, LABEL, sizeof(LABEL) - 1);
  52. p += sizeof(LABEL) - 1;
  53. memcpy(p, label, labellen);
  54. p += labellen;
  55. *p++ = 0;
  56. return ngtcp2_crypto_hkdf_expand(dest, destlen, md, secret, secretlen, info,
  57. (size_t)(p - info));
  58. }
  59. int ngtcp2_crypto_derive_initial_secrets(uint8_t *rx_secret, uint8_t *tx_secret,
  60. uint8_t *initial_secret,
  61. uint32_t version,
  62. const ngtcp2_cid *client_dcid,
  63. ngtcp2_crypto_side side) {
  64. static const uint8_t CLABEL[] = "client in";
  65. static const uint8_t SLABEL[] = "server in";
  66. uint8_t initial_secret_buf[NGTCP2_CRYPTO_INITIAL_SECRETLEN];
  67. uint8_t *client_secret;
  68. uint8_t *server_secret;
  69. ngtcp2_crypto_ctx ctx;
  70. const uint8_t *salt;
  71. size_t saltlen;
  72. if (!initial_secret) {
  73. initial_secret = initial_secret_buf;
  74. }
  75. ngtcp2_crypto_ctx_initial(&ctx);
  76. switch (version) {
  77. case NGTCP2_PROTO_VER_V1:
  78. default:
  79. salt = (const uint8_t *)NGTCP2_INITIAL_SALT_V1;
  80. saltlen = sizeof(NGTCP2_INITIAL_SALT_V1) - 1;
  81. break;
  82. case NGTCP2_PROTO_VER_V2:
  83. salt = (const uint8_t *)NGTCP2_INITIAL_SALT_V2;
  84. saltlen = sizeof(NGTCP2_INITIAL_SALT_V2) - 1;
  85. break;
  86. }
  87. if (ngtcp2_crypto_hkdf_extract(initial_secret, &ctx.md, client_dcid->data,
  88. client_dcid->datalen, salt, saltlen) != 0) {
  89. return -1;
  90. }
  91. if (side == NGTCP2_CRYPTO_SIDE_SERVER) {
  92. client_secret = rx_secret;
  93. server_secret = tx_secret;
  94. } else {
  95. client_secret = tx_secret;
  96. server_secret = rx_secret;
  97. }
  98. if (ngtcp2_crypto_hkdf_expand_label(
  99. client_secret, NGTCP2_CRYPTO_INITIAL_SECRETLEN, &ctx.md, initial_secret,
  100. NGTCP2_CRYPTO_INITIAL_SECRETLEN, CLABEL, sizeof(CLABEL) - 1) != 0 ||
  101. ngtcp2_crypto_hkdf_expand_label(
  102. server_secret, NGTCP2_CRYPTO_INITIAL_SECRETLEN, &ctx.md, initial_secret,
  103. NGTCP2_CRYPTO_INITIAL_SECRETLEN, SLABEL, sizeof(SLABEL) - 1) != 0) {
  104. return -1;
  105. }
  106. return 0;
  107. }
  108. size_t ngtcp2_crypto_packet_protection_ivlen(const ngtcp2_crypto_aead *aead) {
  109. size_t noncelen = ngtcp2_crypto_aead_noncelen(aead);
  110. return ngtcp2_max_size(8, noncelen);
  111. }
  112. int ngtcp2_crypto_derive_packet_protection_key(
  113. uint8_t *key, uint8_t *iv, uint8_t *hp_key, uint32_t version,
  114. const ngtcp2_crypto_aead *aead, const ngtcp2_crypto_md *md,
  115. const uint8_t *secret, size_t secretlen) {
  116. static const uint8_t KEY_LABEL_V1[] = "quic key";
  117. static const uint8_t IV_LABEL_V1[] = "quic iv";
  118. static const uint8_t HP_KEY_LABEL_V1[] = "quic hp";
  119. static const uint8_t KEY_LABEL_V2[] = "quicv2 key";
  120. static const uint8_t IV_LABEL_V2[] = "quicv2 iv";
  121. static const uint8_t HP_KEY_LABEL_V2[] = "quicv2 hp";
  122. size_t keylen = ngtcp2_crypto_aead_keylen(aead);
  123. size_t ivlen = ngtcp2_crypto_packet_protection_ivlen(aead);
  124. const uint8_t *key_label;
  125. size_t key_labellen;
  126. const uint8_t *iv_label;
  127. size_t iv_labellen;
  128. const uint8_t *hp_key_label;
  129. size_t hp_key_labellen;
  130. switch (version) {
  131. case NGTCP2_PROTO_VER_V2:
  132. key_label = KEY_LABEL_V2;
  133. key_labellen = sizeof(KEY_LABEL_V2) - 1;
  134. iv_label = IV_LABEL_V2;
  135. iv_labellen = sizeof(IV_LABEL_V2) - 1;
  136. hp_key_label = HP_KEY_LABEL_V2;
  137. hp_key_labellen = sizeof(HP_KEY_LABEL_V2) - 1;
  138. break;
  139. default:
  140. key_label = KEY_LABEL_V1;
  141. key_labellen = sizeof(KEY_LABEL_V1) - 1;
  142. iv_label = IV_LABEL_V1;
  143. iv_labellen = sizeof(IV_LABEL_V1) - 1;
  144. hp_key_label = HP_KEY_LABEL_V1;
  145. hp_key_labellen = sizeof(HP_KEY_LABEL_V1) - 1;
  146. }
  147. if (ngtcp2_crypto_hkdf_expand_label(key, keylen, md, secret, secretlen,
  148. key_label, key_labellen) != 0) {
  149. return -1;
  150. }
  151. if (ngtcp2_crypto_hkdf_expand_label(iv, ivlen, md, secret, secretlen,
  152. iv_label, iv_labellen) != 0) {
  153. return -1;
  154. }
  155. if (hp_key != NULL &&
  156. ngtcp2_crypto_hkdf_expand_label(hp_key, keylen, md, secret, secretlen,
  157. hp_key_label, hp_key_labellen) != 0) {
  158. return -1;
  159. }
  160. return 0;
  161. }
  162. int ngtcp2_crypto_update_traffic_secret(uint8_t *dest, uint32_t version,
  163. const ngtcp2_crypto_md *md,
  164. const uint8_t *secret,
  165. size_t secretlen) {
  166. static const uint8_t LABEL[] = "quic ku";
  167. static const uint8_t LABEL_V2[] = "quicv2 ku";
  168. const uint8_t *label;
  169. size_t labellen;
  170. switch (version) {
  171. case NGTCP2_PROTO_VER_V2:
  172. label = LABEL_V2;
  173. labellen = sizeof(LABEL_V2) - 1;
  174. break;
  175. default:
  176. label = LABEL;
  177. labellen = sizeof(LABEL) - 1;
  178. }
  179. if (ngtcp2_crypto_hkdf_expand_label(dest, secretlen, md, secret, secretlen,
  180. label, labellen) != 0) {
  181. return -1;
  182. }
  183. return 0;
  184. }
  185. int ngtcp2_crypto_derive_and_install_rx_key(ngtcp2_conn *conn, uint8_t *key,
  186. uint8_t *iv, uint8_t *hp_key,
  187. ngtcp2_encryption_level level,
  188. const uint8_t *secret,
  189. size_t secretlen) {
  190. const ngtcp2_crypto_ctx *ctx;
  191. const ngtcp2_crypto_aead *aead;
  192. const ngtcp2_crypto_md *md;
  193. const ngtcp2_crypto_cipher *hp;
  194. ngtcp2_crypto_aead_ctx aead_ctx = {0};
  195. ngtcp2_crypto_cipher_ctx hp_ctx = {0};
  196. void *tls = ngtcp2_conn_get_tls_native_handle(conn);
  197. uint8_t keybuf[64], ivbuf[64], hp_keybuf[64];
  198. size_t ivlen;
  199. int rv;
  200. ngtcp2_crypto_ctx cctx;
  201. uint32_t version;
  202. if (level == NGTCP2_ENCRYPTION_LEVEL_0RTT && !ngtcp2_conn_is_server(conn)) {
  203. return 0;
  204. }
  205. if (!key) {
  206. key = keybuf;
  207. }
  208. if (!iv) {
  209. iv = ivbuf;
  210. }
  211. if (!hp_key) {
  212. hp_key = hp_keybuf;
  213. }
  214. switch (level) {
  215. case NGTCP2_ENCRYPTION_LEVEL_0RTT:
  216. if (ngtcp2_crypto_ctx_tls_early(&cctx, tls) == NULL) {
  217. return -1;
  218. }
  219. ngtcp2_conn_set_0rtt_crypto_ctx(conn, &cctx);
  220. ctx = ngtcp2_conn_get_0rtt_crypto_ctx(conn);
  221. version = ngtcp2_conn_get_client_chosen_version(conn);
  222. break;
  223. case NGTCP2_ENCRYPTION_LEVEL_HANDSHAKE:
  224. if (ngtcp2_conn_is_server(conn) &&
  225. !ngtcp2_conn_get_negotiated_version(conn)) {
  226. rv = ngtcp2_crypto_set_remote_transport_params(conn, tls);
  227. if (rv != 0) {
  228. return -1;
  229. }
  230. }
  231. /* fall through */
  232. case NGTCP2_ENCRYPTION_LEVEL_1RTT:
  233. ctx = ngtcp2_conn_get_crypto_ctx(conn);
  234. version = ngtcp2_conn_get_negotiated_version(conn);
  235. if (!ctx->aead.native_handle) {
  236. if (ngtcp2_crypto_ctx_tls(&cctx, tls) == NULL) {
  237. return -1;
  238. }
  239. ngtcp2_conn_set_crypto_ctx(conn, &cctx);
  240. ctx = ngtcp2_conn_get_crypto_ctx(conn);
  241. }
  242. break;
  243. default:
  244. return -1;
  245. }
  246. aead = &ctx->aead;
  247. md = &ctx->md;
  248. hp = &ctx->hp;
  249. ivlen = ngtcp2_crypto_packet_protection_ivlen(aead);
  250. if (ngtcp2_crypto_derive_packet_protection_key(key, iv, hp_key, version, aead,
  251. md, secret, secretlen) != 0) {
  252. return -1;
  253. }
  254. if (ngtcp2_crypto_aead_ctx_decrypt_init(&aead_ctx, aead, key, ivlen) != 0) {
  255. goto fail;
  256. }
  257. if (ngtcp2_crypto_cipher_ctx_encrypt_init(&hp_ctx, hp, hp_key) != 0) {
  258. goto fail;
  259. }
  260. switch (level) {
  261. case NGTCP2_ENCRYPTION_LEVEL_0RTT:
  262. rv = ngtcp2_conn_install_0rtt_key(conn, &aead_ctx, iv, ivlen, &hp_ctx);
  263. if (rv != 0) {
  264. goto fail;
  265. }
  266. break;
  267. case NGTCP2_ENCRYPTION_LEVEL_HANDSHAKE:
  268. rv =
  269. ngtcp2_conn_install_rx_handshake_key(conn, &aead_ctx, iv, ivlen, &hp_ctx);
  270. if (rv != 0) {
  271. goto fail;
  272. }
  273. break;
  274. case NGTCP2_ENCRYPTION_LEVEL_1RTT:
  275. if (!ngtcp2_conn_is_server(conn)) {
  276. rv = ngtcp2_crypto_set_remote_transport_params(conn, tls);
  277. if (rv != 0) {
  278. goto fail;
  279. }
  280. }
  281. rv = ngtcp2_conn_install_rx_key(conn, secret, secretlen, &aead_ctx, iv,
  282. ivlen, &hp_ctx);
  283. if (rv != 0) {
  284. goto fail;
  285. }
  286. break;
  287. default:
  288. goto fail;
  289. }
  290. return 0;
  291. fail:
  292. ngtcp2_crypto_cipher_ctx_free(&hp_ctx);
  293. ngtcp2_crypto_aead_ctx_free(&aead_ctx);
  294. return -1;
  295. }
  296. /*
  297. * crypto_set_local_transport_params gets local QUIC transport
  298. * parameters from |conn| and sets it to |tls|.
  299. *
  300. * This function returns 0 if it succeeds, or -1.
  301. */
  302. static int crypto_set_local_transport_params(ngtcp2_conn *conn, void *tls) {
  303. ngtcp2_ssize nwrite;
  304. uint8_t buf[256];
  305. nwrite = ngtcp2_conn_encode_local_transport_params(conn, buf, sizeof(buf));
  306. if (nwrite < 0) {
  307. return -1;
  308. }
  309. if (ngtcp2_crypto_set_local_transport_params(tls, buf, (size_t)nwrite) != 0) {
  310. return -1;
  311. }
  312. return 0;
  313. }
  314. int ngtcp2_crypto_derive_and_install_tx_key(ngtcp2_conn *conn, uint8_t *key,
  315. uint8_t *iv, uint8_t *hp_key,
  316. ngtcp2_encryption_level level,
  317. const uint8_t *secret,
  318. size_t secretlen) {
  319. const ngtcp2_crypto_ctx *ctx;
  320. const ngtcp2_crypto_aead *aead;
  321. const ngtcp2_crypto_md *md;
  322. const ngtcp2_crypto_cipher *hp;
  323. ngtcp2_crypto_aead_ctx aead_ctx = {0};
  324. ngtcp2_crypto_cipher_ctx hp_ctx = {0};
  325. void *tls = ngtcp2_conn_get_tls_native_handle(conn);
  326. uint8_t keybuf[64], ivbuf[64], hp_keybuf[64];
  327. size_t ivlen;
  328. int rv;
  329. ngtcp2_crypto_ctx cctx;
  330. uint32_t version;
  331. if (level == NGTCP2_ENCRYPTION_LEVEL_0RTT && ngtcp2_conn_is_server(conn)) {
  332. return 0;
  333. }
  334. if (!key) {
  335. key = keybuf;
  336. }
  337. if (!iv) {
  338. iv = ivbuf;
  339. }
  340. if (!hp_key) {
  341. hp_key = hp_keybuf;
  342. }
  343. switch (level) {
  344. case NGTCP2_ENCRYPTION_LEVEL_0RTT:
  345. if (ngtcp2_crypto_ctx_tls_early(&cctx, tls) == NULL) {
  346. return -1;
  347. }
  348. ngtcp2_conn_set_0rtt_crypto_ctx(conn, &cctx);
  349. ctx = ngtcp2_conn_get_0rtt_crypto_ctx(conn);
  350. version = ngtcp2_conn_get_client_chosen_version(conn);
  351. break;
  352. case NGTCP2_ENCRYPTION_LEVEL_HANDSHAKE:
  353. if (ngtcp2_conn_is_server(conn) &&
  354. !ngtcp2_conn_get_negotiated_version(conn)) {
  355. rv = ngtcp2_crypto_set_remote_transport_params(conn, tls);
  356. if (rv != 0) {
  357. return -1;
  358. }
  359. }
  360. /* fall through */
  361. case NGTCP2_ENCRYPTION_LEVEL_1RTT:
  362. ctx = ngtcp2_conn_get_crypto_ctx(conn);
  363. version = ngtcp2_conn_get_negotiated_version(conn);
  364. if (!ctx->aead.native_handle) {
  365. if (ngtcp2_crypto_ctx_tls(&cctx, tls) == NULL) {
  366. return -1;
  367. }
  368. ngtcp2_conn_set_crypto_ctx(conn, &cctx);
  369. ctx = ngtcp2_conn_get_crypto_ctx(conn);
  370. }
  371. break;
  372. default:
  373. return -1;
  374. }
  375. aead = &ctx->aead;
  376. md = &ctx->md;
  377. hp = &ctx->hp;
  378. ivlen = ngtcp2_crypto_packet_protection_ivlen(aead);
  379. if (ngtcp2_crypto_derive_packet_protection_key(key, iv, hp_key, version, aead,
  380. md, secret, secretlen) != 0) {
  381. return -1;
  382. }
  383. if (ngtcp2_crypto_aead_ctx_encrypt_init(&aead_ctx, aead, key, ivlen) != 0) {
  384. goto fail;
  385. }
  386. if (ngtcp2_crypto_cipher_ctx_encrypt_init(&hp_ctx, hp, hp_key) != 0) {
  387. goto fail;
  388. }
  389. switch (level) {
  390. case NGTCP2_ENCRYPTION_LEVEL_0RTT:
  391. rv = ngtcp2_conn_install_0rtt_key(conn, &aead_ctx, iv, ivlen, &hp_ctx);
  392. if (rv != 0) {
  393. goto fail;
  394. }
  395. break;
  396. case NGTCP2_ENCRYPTION_LEVEL_HANDSHAKE:
  397. rv =
  398. ngtcp2_conn_install_tx_handshake_key(conn, &aead_ctx, iv, ivlen, &hp_ctx);
  399. if (rv != 0) {
  400. goto fail;
  401. }
  402. if (ngtcp2_conn_is_server(conn) &&
  403. crypto_set_local_transport_params(conn, tls) != 0) {
  404. goto fail;
  405. }
  406. break;
  407. case NGTCP2_ENCRYPTION_LEVEL_1RTT:
  408. rv = ngtcp2_conn_install_tx_key(conn, secret, secretlen, &aead_ctx, iv,
  409. ivlen, &hp_ctx);
  410. if (rv != 0) {
  411. goto fail;
  412. }
  413. break;
  414. default:
  415. goto fail;
  416. }
  417. return 0;
  418. fail:
  419. ngtcp2_crypto_cipher_ctx_free(&hp_ctx);
  420. ngtcp2_crypto_aead_ctx_free(&aead_ctx);
  421. return -1;
  422. }
  423. int ngtcp2_crypto_derive_and_install_initial_key(
  424. ngtcp2_conn *conn, uint8_t *rx_secret, uint8_t *tx_secret,
  425. uint8_t *initial_secret, uint8_t *rx_key, uint8_t *rx_iv, uint8_t *rx_hp_key,
  426. uint8_t *tx_key, uint8_t *tx_iv, uint8_t *tx_hp_key, uint32_t version,
  427. const ngtcp2_cid *client_dcid) {
  428. uint8_t rx_secretbuf[NGTCP2_CRYPTO_INITIAL_SECRETLEN];
  429. uint8_t tx_secretbuf[NGTCP2_CRYPTO_INITIAL_SECRETLEN];
  430. uint8_t initial_secretbuf[NGTCP2_CRYPTO_INITIAL_SECRETLEN];
  431. uint8_t rx_keybuf[NGTCP2_CRYPTO_INITIAL_KEYLEN];
  432. uint8_t rx_ivbuf[NGTCP2_CRYPTO_INITIAL_IVLEN];
  433. uint8_t rx_hp_keybuf[NGTCP2_CRYPTO_INITIAL_KEYLEN];
  434. uint8_t tx_keybuf[NGTCP2_CRYPTO_INITIAL_KEYLEN];
  435. uint8_t tx_ivbuf[NGTCP2_CRYPTO_INITIAL_IVLEN];
  436. uint8_t tx_hp_keybuf[NGTCP2_CRYPTO_INITIAL_KEYLEN];
  437. ngtcp2_crypto_ctx ctx;
  438. ngtcp2_crypto_aead retry_aead;
  439. ngtcp2_crypto_aead_ctx rx_aead_ctx = {0};
  440. ngtcp2_crypto_cipher_ctx rx_hp_ctx = {0};
  441. ngtcp2_crypto_aead_ctx tx_aead_ctx = {0};
  442. ngtcp2_crypto_cipher_ctx tx_hp_ctx = {0};
  443. ngtcp2_crypto_aead_ctx retry_aead_ctx = {0};
  444. int rv;
  445. int server = ngtcp2_conn_is_server(conn);
  446. const uint8_t *retry_key;
  447. size_t retry_noncelen;
  448. ngtcp2_crypto_ctx_initial(&ctx);
  449. if (!rx_secret) {
  450. rx_secret = rx_secretbuf;
  451. }
  452. if (!tx_secret) {
  453. tx_secret = tx_secretbuf;
  454. }
  455. if (!initial_secret) {
  456. initial_secret = initial_secretbuf;
  457. }
  458. if (!rx_key) {
  459. rx_key = rx_keybuf;
  460. }
  461. if (!rx_iv) {
  462. rx_iv = rx_ivbuf;
  463. }
  464. if (!rx_hp_key) {
  465. rx_hp_key = rx_hp_keybuf;
  466. }
  467. if (!tx_key) {
  468. tx_key = tx_keybuf;
  469. }
  470. if (!tx_iv) {
  471. tx_iv = tx_ivbuf;
  472. }
  473. if (!tx_hp_key) {
  474. tx_hp_key = tx_hp_keybuf;
  475. }
  476. ngtcp2_conn_set_initial_crypto_ctx(conn, &ctx);
  477. if (ngtcp2_crypto_derive_initial_secrets(
  478. rx_secret, tx_secret, initial_secret, version, client_dcid,
  479. server ? NGTCP2_CRYPTO_SIDE_SERVER : NGTCP2_CRYPTO_SIDE_CLIENT) != 0) {
  480. return -1;
  481. }
  482. if (ngtcp2_crypto_derive_packet_protection_key(
  483. rx_key, rx_iv, rx_hp_key, version, &ctx.aead, &ctx.md, rx_secret,
  484. NGTCP2_CRYPTO_INITIAL_SECRETLEN) != 0) {
  485. return -1;
  486. }
  487. if (ngtcp2_crypto_derive_packet_protection_key(
  488. tx_key, tx_iv, tx_hp_key, version, &ctx.aead, &ctx.md, tx_secret,
  489. NGTCP2_CRYPTO_INITIAL_SECRETLEN) != 0) {
  490. return -1;
  491. }
  492. if (ngtcp2_crypto_aead_ctx_decrypt_init(&rx_aead_ctx, &ctx.aead, rx_key,
  493. NGTCP2_CRYPTO_INITIAL_IVLEN) != 0) {
  494. goto fail;
  495. }
  496. if (ngtcp2_crypto_cipher_ctx_encrypt_init(&rx_hp_ctx, &ctx.hp, rx_hp_key) !=
  497. 0) {
  498. goto fail;
  499. }
  500. if (ngtcp2_crypto_aead_ctx_encrypt_init(&tx_aead_ctx, &ctx.aead, tx_key,
  501. NGTCP2_CRYPTO_INITIAL_IVLEN) != 0) {
  502. goto fail;
  503. }
  504. if (ngtcp2_crypto_cipher_ctx_encrypt_init(&tx_hp_ctx, &ctx.hp, tx_hp_key) !=
  505. 0) {
  506. goto fail;
  507. }
  508. if (!server && !ngtcp2_conn_after_retry(conn)) {
  509. ngtcp2_crypto_aead_retry(&retry_aead);
  510. switch (version) {
  511. case NGTCP2_PROTO_VER_V1:
  512. default:
  513. retry_key = (const uint8_t *)NGTCP2_RETRY_KEY_V1;
  514. retry_noncelen = sizeof(NGTCP2_RETRY_NONCE_V1) - 1;
  515. break;
  516. case NGTCP2_PROTO_VER_V2:
  517. retry_key = (const uint8_t *)NGTCP2_RETRY_KEY_V2;
  518. retry_noncelen = sizeof(NGTCP2_RETRY_NONCE_V2) - 1;
  519. break;
  520. }
  521. if (ngtcp2_crypto_aead_ctx_encrypt_init(&retry_aead_ctx, &retry_aead,
  522. retry_key, retry_noncelen) != 0) {
  523. goto fail;
  524. }
  525. }
  526. rv = ngtcp2_conn_install_initial_key(conn, &rx_aead_ctx, rx_iv, &rx_hp_ctx,
  527. &tx_aead_ctx, tx_iv, &tx_hp_ctx,
  528. NGTCP2_CRYPTO_INITIAL_IVLEN);
  529. if (rv != 0) {
  530. goto fail;
  531. }
  532. if (retry_aead_ctx.native_handle) {
  533. ngtcp2_conn_set_retry_aead(conn, &retry_aead, &retry_aead_ctx);
  534. }
  535. return 0;
  536. fail:
  537. ngtcp2_crypto_aead_ctx_free(&retry_aead_ctx);
  538. ngtcp2_crypto_cipher_ctx_free(&tx_hp_ctx);
  539. ngtcp2_crypto_aead_ctx_free(&tx_aead_ctx);
  540. ngtcp2_crypto_cipher_ctx_free(&rx_hp_ctx);
  541. ngtcp2_crypto_aead_ctx_free(&rx_aead_ctx);
  542. return -1;
  543. }
  544. int ngtcp2_crypto_derive_and_install_vneg_initial_key(
  545. ngtcp2_conn *conn, uint8_t *rx_secret, uint8_t *tx_secret,
  546. uint8_t *initial_secret, uint8_t *rx_key, uint8_t *rx_iv, uint8_t *rx_hp_key,
  547. uint8_t *tx_key, uint8_t *tx_iv, uint8_t *tx_hp_key, uint32_t version,
  548. const ngtcp2_cid *client_dcid) {
  549. uint8_t rx_secretbuf[NGTCP2_CRYPTO_INITIAL_SECRETLEN];
  550. uint8_t tx_secretbuf[NGTCP2_CRYPTO_INITIAL_SECRETLEN];
  551. uint8_t initial_secretbuf[NGTCP2_CRYPTO_INITIAL_SECRETLEN];
  552. uint8_t rx_keybuf[NGTCP2_CRYPTO_INITIAL_KEYLEN];
  553. uint8_t rx_ivbuf[NGTCP2_CRYPTO_INITIAL_IVLEN];
  554. uint8_t rx_hp_keybuf[NGTCP2_CRYPTO_INITIAL_KEYLEN];
  555. uint8_t tx_keybuf[NGTCP2_CRYPTO_INITIAL_KEYLEN];
  556. uint8_t tx_ivbuf[NGTCP2_CRYPTO_INITIAL_IVLEN];
  557. uint8_t tx_hp_keybuf[NGTCP2_CRYPTO_INITIAL_KEYLEN];
  558. const ngtcp2_crypto_ctx *ctx = ngtcp2_conn_get_initial_crypto_ctx(conn);
  559. ngtcp2_crypto_aead_ctx rx_aead_ctx = {0};
  560. ngtcp2_crypto_cipher_ctx rx_hp_ctx = {0};
  561. ngtcp2_crypto_aead_ctx tx_aead_ctx = {0};
  562. ngtcp2_crypto_cipher_ctx tx_hp_ctx = {0};
  563. int rv;
  564. int server = ngtcp2_conn_is_server(conn);
  565. if (!rx_secret) {
  566. rx_secret = rx_secretbuf;
  567. }
  568. if (!tx_secret) {
  569. tx_secret = tx_secretbuf;
  570. }
  571. if (!initial_secret) {
  572. initial_secret = initial_secretbuf;
  573. }
  574. if (!rx_key) {
  575. rx_key = rx_keybuf;
  576. }
  577. if (!rx_iv) {
  578. rx_iv = rx_ivbuf;
  579. }
  580. if (!rx_hp_key) {
  581. rx_hp_key = rx_hp_keybuf;
  582. }
  583. if (!tx_key) {
  584. tx_key = tx_keybuf;
  585. }
  586. if (!tx_iv) {
  587. tx_iv = tx_ivbuf;
  588. }
  589. if (!tx_hp_key) {
  590. tx_hp_key = tx_hp_keybuf;
  591. }
  592. if (ngtcp2_crypto_derive_initial_secrets(
  593. rx_secret, tx_secret, initial_secret, version, client_dcid,
  594. server ? NGTCP2_CRYPTO_SIDE_SERVER : NGTCP2_CRYPTO_SIDE_CLIENT) != 0) {
  595. return -1;
  596. }
  597. if (ngtcp2_crypto_derive_packet_protection_key(
  598. rx_key, rx_iv, rx_hp_key, version, &ctx->aead, &ctx->md, rx_secret,
  599. NGTCP2_CRYPTO_INITIAL_SECRETLEN) != 0) {
  600. return -1;
  601. }
  602. if (ngtcp2_crypto_derive_packet_protection_key(
  603. tx_key, tx_iv, tx_hp_key, version, &ctx->aead, &ctx->md, tx_secret,
  604. NGTCP2_CRYPTO_INITIAL_SECRETLEN) != 0) {
  605. return -1;
  606. }
  607. if (ngtcp2_crypto_aead_ctx_decrypt_init(&rx_aead_ctx, &ctx->aead, rx_key,
  608. NGTCP2_CRYPTO_INITIAL_IVLEN) != 0) {
  609. goto fail;
  610. }
  611. if (ngtcp2_crypto_cipher_ctx_encrypt_init(&rx_hp_ctx, &ctx->hp, rx_hp_key) !=
  612. 0) {
  613. goto fail;
  614. }
  615. if (ngtcp2_crypto_aead_ctx_encrypt_init(&tx_aead_ctx, &ctx->aead, tx_key,
  616. NGTCP2_CRYPTO_INITIAL_IVLEN) != 0) {
  617. goto fail;
  618. }
  619. if (ngtcp2_crypto_cipher_ctx_encrypt_init(&tx_hp_ctx, &ctx->hp, tx_hp_key) !=
  620. 0) {
  621. goto fail;
  622. }
  623. rv = ngtcp2_conn_install_vneg_initial_key(
  624. conn, version, &rx_aead_ctx, rx_iv, &rx_hp_ctx, &tx_aead_ctx, tx_iv,
  625. &tx_hp_ctx, NGTCP2_CRYPTO_INITIAL_IVLEN);
  626. if (rv != 0) {
  627. goto fail;
  628. }
  629. return 0;
  630. fail:
  631. ngtcp2_crypto_cipher_ctx_free(&tx_hp_ctx);
  632. ngtcp2_crypto_aead_ctx_free(&tx_aead_ctx);
  633. ngtcp2_crypto_cipher_ctx_free(&rx_hp_ctx);
  634. ngtcp2_crypto_aead_ctx_free(&rx_aead_ctx);
  635. return -1;
  636. }
  637. int ngtcp2_crypto_update_key(
  638. ngtcp2_conn *conn, uint8_t *rx_secret, uint8_t *tx_secret,
  639. ngtcp2_crypto_aead_ctx *rx_aead_ctx, uint8_t *rx_key, uint8_t *rx_iv,
  640. ngtcp2_crypto_aead_ctx *tx_aead_ctx, uint8_t *tx_key, uint8_t *tx_iv,
  641. const uint8_t *current_rx_secret, const uint8_t *current_tx_secret,
  642. size_t secretlen) {
  643. const ngtcp2_crypto_ctx *ctx = ngtcp2_conn_get_crypto_ctx(conn);
  644. const ngtcp2_crypto_aead *aead = &ctx->aead;
  645. const ngtcp2_crypto_md *md = &ctx->md;
  646. size_t ivlen = ngtcp2_crypto_packet_protection_ivlen(aead);
  647. uint32_t version = ngtcp2_conn_get_negotiated_version(conn);
  648. if (ngtcp2_crypto_update_traffic_secret(rx_secret, version, md,
  649. current_rx_secret, secretlen) != 0) {
  650. return -1;
  651. }
  652. if (ngtcp2_crypto_derive_packet_protection_key(
  653. rx_key, rx_iv, NULL, version, aead, md, rx_secret, secretlen) != 0) {
  654. return -1;
  655. }
  656. if (ngtcp2_crypto_update_traffic_secret(tx_secret, version, md,
  657. current_tx_secret, secretlen) != 0) {
  658. return -1;
  659. }
  660. if (ngtcp2_crypto_derive_packet_protection_key(
  661. tx_key, tx_iv, NULL, version, aead, md, tx_secret, secretlen) != 0) {
  662. return -1;
  663. }
  664. if (ngtcp2_crypto_aead_ctx_decrypt_init(rx_aead_ctx, aead, rx_key, ivlen) !=
  665. 0) {
  666. return -1;
  667. }
  668. if (ngtcp2_crypto_aead_ctx_encrypt_init(tx_aead_ctx, aead, tx_key, ivlen) !=
  669. 0) {
  670. ngtcp2_crypto_aead_ctx_free(rx_aead_ctx);
  671. rx_aead_ctx->native_handle = NULL;
  672. return -1;
  673. }
  674. return 0;
  675. }
  676. int ngtcp2_crypto_encrypt_cb(uint8_t *dest, const ngtcp2_crypto_aead *aead,
  677. const ngtcp2_crypto_aead_ctx *aead_ctx,
  678. const uint8_t *plaintext, size_t plaintextlen,
  679. const uint8_t *nonce, size_t noncelen,
  680. const uint8_t *aad, size_t aadlen) {
  681. if (ngtcp2_crypto_encrypt(dest, aead, aead_ctx, plaintext, plaintextlen,
  682. nonce, noncelen, aad, aadlen) != 0) {
  683. return NGTCP2_ERR_CALLBACK_FAILURE;
  684. }
  685. return 0;
  686. }
  687. int ngtcp2_crypto_decrypt_cb(uint8_t *dest, const ngtcp2_crypto_aead *aead,
  688. const ngtcp2_crypto_aead_ctx *aead_ctx,
  689. const uint8_t *ciphertext, size_t ciphertextlen,
  690. const uint8_t *nonce, size_t noncelen,
  691. const uint8_t *aad, size_t aadlen) {
  692. if (ngtcp2_crypto_decrypt(dest, aead, aead_ctx, ciphertext, ciphertextlen,
  693. nonce, noncelen, aad, aadlen) != 0) {
  694. return NGTCP2_ERR_DECRYPT;
  695. }
  696. return 0;
  697. }
  698. int ngtcp2_crypto_hp_mask_cb(uint8_t *dest, const ngtcp2_crypto_cipher *hp,
  699. const ngtcp2_crypto_cipher_ctx *hp_ctx,
  700. const uint8_t *sample) {
  701. if (ngtcp2_crypto_hp_mask(dest, hp, hp_ctx, sample) != 0) {
  702. return NGTCP2_ERR_CALLBACK_FAILURE;
  703. }
  704. return 0;
  705. }
  706. int ngtcp2_crypto_update_key_cb(
  707. ngtcp2_conn *conn, uint8_t *rx_secret, uint8_t *tx_secret,
  708. ngtcp2_crypto_aead_ctx *rx_aead_ctx, uint8_t *rx_iv,
  709. ngtcp2_crypto_aead_ctx *tx_aead_ctx, uint8_t *tx_iv,
  710. const uint8_t *current_rx_secret, const uint8_t *current_tx_secret,
  711. size_t secretlen, void *user_data) {
  712. uint8_t rx_key[64];
  713. uint8_t tx_key[64];
  714. (void)conn;
  715. (void)user_data;
  716. if (ngtcp2_crypto_update_key(
  717. conn, rx_secret, tx_secret, rx_aead_ctx, rx_key, rx_iv, tx_aead_ctx,
  718. tx_key, tx_iv, current_rx_secret, current_tx_secret, secretlen) != 0) {
  719. return NGTCP2_ERR_CALLBACK_FAILURE;
  720. }
  721. return 0;
  722. }
  723. int ngtcp2_crypto_generate_stateless_reset_token(uint8_t *token,
  724. const uint8_t *secret,
  725. size_t secretlen,
  726. const ngtcp2_cid *cid) {
  727. static const uint8_t info[] = "stateless_reset";
  728. ngtcp2_crypto_md md;
  729. if (ngtcp2_crypto_hkdf(token, NGTCP2_STATELESS_RESET_TOKENLEN,
  730. ngtcp2_crypto_md_sha256(&md), secret, secretlen,
  731. cid->data, cid->datalen, info,
  732. sizeof(info) - 1) != 0) {
  733. return -1;
  734. }
  735. return 0;
  736. }
  737. static int crypto_derive_token_key(uint8_t *key, size_t keylen, uint8_t *iv,
  738. size_t ivlen, const ngtcp2_crypto_md *md,
  739. const uint8_t *secret, size_t secretlen,
  740. const uint8_t *salt, size_t saltlen,
  741. const uint8_t *info_prefix,
  742. size_t info_prefixlen) {
  743. static const uint8_t key_info_suffix[] = " key";
  744. static const uint8_t iv_info_suffix[] = " iv";
  745. uint8_t intsecret[32];
  746. uint8_t info[32];
  747. uint8_t *p;
  748. assert(ngtcp2_crypto_md_hashlen(md) == sizeof(intsecret));
  749. assert(info_prefixlen + sizeof(key_info_suffix) - 1 <= sizeof(info));
  750. assert(info_prefixlen + sizeof(iv_info_suffix) - 1 <= sizeof(info));
  751. if (ngtcp2_crypto_hkdf_extract(intsecret, md, secret, secretlen, salt,
  752. saltlen) != 0) {
  753. return -1;
  754. }
  755. memcpy(info, info_prefix, info_prefixlen);
  756. p = info + info_prefixlen;
  757. memcpy(p, key_info_suffix, sizeof(key_info_suffix) - 1);
  758. p += sizeof(key_info_suffix) - 1;
  759. if (ngtcp2_crypto_hkdf_expand(key, keylen, md, intsecret, sizeof(intsecret),
  760. info, (size_t)(p - info)) != 0) {
  761. return -1;
  762. }
  763. p = info + info_prefixlen;
  764. memcpy(p, iv_info_suffix, sizeof(iv_info_suffix) - 1);
  765. p += sizeof(iv_info_suffix) - 1;
  766. if (ngtcp2_crypto_hkdf_expand(iv, ivlen, md, intsecret, sizeof(intsecret),
  767. info, (size_t)(p - info)) != 0) {
  768. return -1;
  769. }
  770. return 0;
  771. }
  772. static size_t crypto_generate_retry_token_aad(uint8_t *dest, uint32_t version,
  773. const ngtcp2_sockaddr *sa,
  774. ngtcp2_socklen salen,
  775. const ngtcp2_cid *retry_scid) {
  776. uint8_t *p = dest;
  777. version = ngtcp2_htonl(version);
  778. memcpy(p, &version, sizeof(version));
  779. p += sizeof(version);
  780. memcpy(p, sa, (size_t)salen);
  781. p += salen;
  782. memcpy(p, retry_scid->data, retry_scid->datalen);
  783. p += retry_scid->datalen;
  784. return (size_t)(p - dest);
  785. }
  786. static const uint8_t retry_token_info_prefix[] = "retry_token";
  787. ngtcp2_ssize ngtcp2_crypto_generate_retry_token(
  788. uint8_t *token, const uint8_t *secret, size_t secretlen, uint32_t version,
  789. const ngtcp2_sockaddr *remote_addr, ngtcp2_socklen remote_addrlen,
  790. const ngtcp2_cid *retry_scid, const ngtcp2_cid *odcid, ngtcp2_tstamp ts) {
  791. uint8_t
  792. plaintext[/* cid len = */ 1 + NGTCP2_MAX_CIDLEN + sizeof(ngtcp2_tstamp)];
  793. uint8_t rand_data[NGTCP2_CRYPTO_TOKEN_RAND_DATALEN];
  794. uint8_t key[16];
  795. uint8_t iv[12];
  796. size_t keylen;
  797. size_t ivlen;
  798. ngtcp2_crypto_aead aead;
  799. ngtcp2_crypto_md md;
  800. ngtcp2_crypto_aead_ctx aead_ctx;
  801. size_t plaintextlen;
  802. uint8_t
  803. aad[sizeof(version) + sizeof(ngtcp2_sockaddr_union) + NGTCP2_MAX_CIDLEN];
  804. size_t aadlen;
  805. uint8_t *p = plaintext;
  806. ngtcp2_tstamp ts_be = ngtcp2_htonl64(ts);
  807. int rv;
  808. assert((size_t)remote_addrlen <= sizeof(ngtcp2_sockaddr_union));
  809. memset(plaintext, 0, sizeof(plaintext));
  810. *p++ = (uint8_t)odcid->datalen;
  811. memcpy(p, odcid->data, odcid->datalen);
  812. p += NGTCP2_MAX_CIDLEN;
  813. memcpy(p, &ts_be, sizeof(ts_be));
  814. p += sizeof(ts_be);
  815. plaintextlen = (size_t)(p - plaintext);
  816. if (ngtcp2_crypto_random(rand_data, sizeof(rand_data)) != 0) {
  817. return -1;
  818. }
  819. ngtcp2_crypto_aead_aes_128_gcm(&aead);
  820. ngtcp2_crypto_md_sha256(&md);
  821. keylen = ngtcp2_crypto_aead_keylen(&aead);
  822. ivlen = ngtcp2_crypto_aead_noncelen(&aead);
  823. assert(sizeof(key) == keylen);
  824. assert(sizeof(iv) == ivlen);
  825. if (crypto_derive_token_key(key, keylen, iv, ivlen, &md, secret, secretlen,
  826. rand_data, sizeof(rand_data),
  827. retry_token_info_prefix,
  828. sizeof(retry_token_info_prefix) - 1) != 0) {
  829. return -1;
  830. }
  831. aadlen = crypto_generate_retry_token_aad(aad, version, remote_addr,
  832. remote_addrlen, retry_scid);
  833. p = token;
  834. *p++ = NGTCP2_CRYPTO_TOKEN_MAGIC_RETRY;
  835. if (ngtcp2_crypto_aead_ctx_encrypt_init(&aead_ctx, &aead, key, ivlen) != 0) {
  836. return -1;
  837. }
  838. rv = ngtcp2_crypto_encrypt(p, &aead, &aead_ctx, plaintext, plaintextlen, iv,
  839. ivlen, aad, aadlen);
  840. ngtcp2_crypto_aead_ctx_free(&aead_ctx);
  841. if (rv != 0) {
  842. return -1;
  843. }
  844. p += plaintextlen + aead.max_overhead;
  845. memcpy(p, rand_data, sizeof(rand_data));
  846. p += sizeof(rand_data);
  847. return p - token;
  848. }
  849. int ngtcp2_crypto_verify_retry_token(
  850. ngtcp2_cid *odcid, const uint8_t *token, size_t tokenlen,
  851. const uint8_t *secret, size_t secretlen, uint32_t version,
  852. const ngtcp2_sockaddr *remote_addr, ngtcp2_socklen remote_addrlen,
  853. const ngtcp2_cid *dcid, ngtcp2_duration timeout, ngtcp2_tstamp ts) {
  854. uint8_t
  855. plaintext[/* cid len = */ 1 + NGTCP2_MAX_CIDLEN + sizeof(ngtcp2_tstamp)];
  856. uint8_t key[16];
  857. uint8_t iv[12];
  858. size_t keylen;
  859. size_t ivlen;
  860. ngtcp2_crypto_aead_ctx aead_ctx;
  861. ngtcp2_crypto_aead aead;
  862. ngtcp2_crypto_md md;
  863. uint8_t
  864. aad[sizeof(version) + sizeof(ngtcp2_sockaddr_union) + NGTCP2_MAX_CIDLEN];
  865. size_t aadlen;
  866. const uint8_t *rand_data;
  867. const uint8_t *ciphertext;
  868. size_t ciphertextlen;
  869. size_t cil;
  870. int rv;
  871. ngtcp2_tstamp gen_ts;
  872. assert((size_t)remote_addrlen <= sizeof(ngtcp2_sockaddr_union));
  873. if (tokenlen != NGTCP2_CRYPTO_MAX_RETRY_TOKENLEN ||
  874. token[0] != NGTCP2_CRYPTO_TOKEN_MAGIC_RETRY) {
  875. return -1;
  876. }
  877. rand_data = token + tokenlen - NGTCP2_CRYPTO_TOKEN_RAND_DATALEN;
  878. ciphertext = token + 1;
  879. ciphertextlen = tokenlen - 1 - NGTCP2_CRYPTO_TOKEN_RAND_DATALEN;
  880. ngtcp2_crypto_aead_aes_128_gcm(&aead);
  881. ngtcp2_crypto_md_sha256(&md);
  882. keylen = ngtcp2_crypto_aead_keylen(&aead);
  883. ivlen = ngtcp2_crypto_aead_noncelen(&aead);
  884. assert(sizeof(key) == keylen);
  885. assert(sizeof(iv) == ivlen);
  886. if (crypto_derive_token_key(key, keylen, iv, ivlen, &md, secret, secretlen,
  887. rand_data, NGTCP2_CRYPTO_TOKEN_RAND_DATALEN,
  888. retry_token_info_prefix,
  889. sizeof(retry_token_info_prefix) - 1) != 0) {
  890. return -1;
  891. }
  892. aadlen = crypto_generate_retry_token_aad(aad, version, remote_addr,
  893. remote_addrlen, dcid);
  894. if (ngtcp2_crypto_aead_ctx_decrypt_init(&aead_ctx, &aead, key, ivlen) != 0) {
  895. return -1;
  896. }
  897. rv = ngtcp2_crypto_decrypt(plaintext, &aead, &aead_ctx, ciphertext,
  898. ciphertextlen, iv, ivlen, aad, aadlen);
  899. ngtcp2_crypto_aead_ctx_free(&aead_ctx);
  900. if (rv != 0) {
  901. return -1;
  902. }
  903. cil = plaintext[0];
  904. if (cil != 0 && (cil < NGTCP2_MIN_CIDLEN || cil > NGTCP2_MAX_CIDLEN)) {
  905. return -1;
  906. }
  907. memcpy(&gen_ts, plaintext + /* cid len = */ 1 + NGTCP2_MAX_CIDLEN,
  908. sizeof(gen_ts));
  909. gen_ts = ngtcp2_ntohl64(gen_ts);
  910. if (gen_ts + timeout <= ts) {
  911. return -1;
  912. }
  913. ngtcp2_cid_init(odcid, plaintext + /* cid len = */ 1, cil);
  914. return 0;
  915. }
  916. static size_t crypto_generate_retry_token_aad2(uint8_t *dest, uint32_t version,
  917. const ngtcp2_cid *retry_scid) {
  918. uint8_t *p = dest;
  919. version = ngtcp2_htonl(version);
  920. memcpy(p, &version, sizeof(version));
  921. p += sizeof(version);
  922. memcpy(p, retry_scid->data, retry_scid->datalen);
  923. p += retry_scid->datalen;
  924. return (size_t)(p - dest);
  925. }
  926. static const uint8_t retry_token_info_prefix2[] = "retry_token2";
  927. ngtcp2_ssize ngtcp2_crypto_generate_retry_token2(
  928. uint8_t *token, const uint8_t *secret, size_t secretlen, uint32_t version,
  929. const ngtcp2_sockaddr *remote_addr, ngtcp2_socklen remote_addrlen,
  930. const ngtcp2_cid *retry_scid, const ngtcp2_cid *odcid, ngtcp2_tstamp ts) {
  931. uint8_t plaintext[sizeof(ngtcp2_sockaddr_union) + /* cid len = */ 1 +
  932. NGTCP2_MAX_CIDLEN + sizeof(ngtcp2_tstamp)];
  933. uint8_t rand_data[NGTCP2_CRYPTO_TOKEN_RAND_DATALEN];
  934. uint8_t key[16];
  935. uint8_t iv[12];
  936. size_t keylen;
  937. size_t ivlen;
  938. ngtcp2_crypto_aead aead;
  939. ngtcp2_crypto_md md;
  940. ngtcp2_crypto_aead_ctx aead_ctx;
  941. uint8_t aad[sizeof(version) + NGTCP2_MAX_CIDLEN];
  942. size_t aadlen;
  943. uint8_t *p = plaintext;
  944. ngtcp2_tstamp ts_be = ngtcp2_htonl64(ts);
  945. int rv;
  946. assert((size_t)remote_addrlen <= sizeof(ngtcp2_sockaddr_union));
  947. memset(plaintext, 0, sizeof(plaintext));
  948. memcpy(p, remote_addr, (size_t)remote_addrlen);
  949. p += sizeof(ngtcp2_sockaddr_union);
  950. *p++ = (uint8_t)odcid->datalen;
  951. memcpy(p, odcid->data, odcid->datalen);
  952. p += NGTCP2_MAX_CIDLEN;
  953. memcpy(p, &ts_be, sizeof(ts_be));
  954. assert((size_t)(p + sizeof(ts_be) - plaintext) == sizeof(plaintext));
  955. if (ngtcp2_crypto_random(rand_data, sizeof(rand_data)) != 0) {
  956. return -1;
  957. }
  958. ngtcp2_crypto_aead_aes_128_gcm(&aead);
  959. ngtcp2_crypto_md_sha256(&md);
  960. keylen = ngtcp2_crypto_aead_keylen(&aead);
  961. ivlen = ngtcp2_crypto_aead_noncelen(&aead);
  962. assert(sizeof(key) == keylen);
  963. assert(sizeof(iv) == ivlen);
  964. if (crypto_derive_token_key(key, keylen, iv, ivlen, &md, secret, secretlen,
  965. rand_data, sizeof(rand_data),
  966. retry_token_info_prefix2,
  967. sizeof(retry_token_info_prefix2) - 1) != 0) {
  968. return -1;
  969. }
  970. aadlen = crypto_generate_retry_token_aad2(aad, version, retry_scid);
  971. p = token;
  972. *p++ = NGTCP2_CRYPTO_TOKEN_MAGIC_RETRY2;
  973. if (ngtcp2_crypto_aead_ctx_encrypt_init(&aead_ctx, &aead, key, ivlen) != 0) {
  974. return -1;
  975. }
  976. rv = ngtcp2_crypto_encrypt(p, &aead, &aead_ctx, plaintext, sizeof(plaintext),
  977. iv, ivlen, aad, aadlen);
  978. ngtcp2_crypto_aead_ctx_free(&aead_ctx);
  979. if (rv != 0) {
  980. return -1;
  981. }
  982. p += sizeof(plaintext) + aead.max_overhead;
  983. memcpy(p, rand_data, sizeof(rand_data));
  984. p += sizeof(rand_data);
  985. return p - token;
  986. }
  987. int ngtcp2_crypto_verify_retry_token2(
  988. ngtcp2_cid *odcid, const uint8_t *token, size_t tokenlen,
  989. const uint8_t *secret, size_t secretlen, uint32_t version,
  990. const ngtcp2_sockaddr *remote_addr, ngtcp2_socklen remote_addrlen,
  991. const ngtcp2_cid *dcid, ngtcp2_duration timeout, ngtcp2_tstamp ts) {
  992. uint8_t plaintext[sizeof(ngtcp2_sockaddr_union) + /* cid len = */ 1 +
  993. NGTCP2_MAX_CIDLEN + sizeof(ngtcp2_tstamp)];
  994. uint8_t key[16];
  995. uint8_t iv[12];
  996. size_t keylen;
  997. size_t ivlen;
  998. ngtcp2_crypto_aead_ctx aead_ctx;
  999. ngtcp2_crypto_aead aead;
  1000. ngtcp2_crypto_md md;
  1001. uint8_t aad[sizeof(version) + NGTCP2_MAX_CIDLEN];
  1002. size_t aadlen;
  1003. const uint8_t *rand_data;
  1004. const uint8_t *ciphertext;
  1005. size_t ciphertextlen;
  1006. size_t cil;
  1007. int rv;
  1008. ngtcp2_tstamp gen_ts;
  1009. ngtcp2_sockaddr_union addr;
  1010. size_t addrlen;
  1011. uint8_t *p;
  1012. assert((size_t)remote_addrlen <= sizeof(ngtcp2_sockaddr_union));
  1013. if (tokenlen != NGTCP2_CRYPTO_MAX_RETRY_TOKENLEN2 ||
  1014. token[0] != NGTCP2_CRYPTO_TOKEN_MAGIC_RETRY2) {
  1015. return NGTCP2_CRYPTO_ERR_UNREADABLE_TOKEN;
  1016. }
  1017. rand_data = token + tokenlen - NGTCP2_CRYPTO_TOKEN_RAND_DATALEN;
  1018. ciphertext = token + 1;
  1019. ciphertextlen = (size_t)(rand_data - ciphertext);
  1020. ngtcp2_crypto_aead_aes_128_gcm(&aead);
  1021. ngtcp2_crypto_md_sha256(&md);
  1022. keylen = ngtcp2_crypto_aead_keylen(&aead);
  1023. ivlen = ngtcp2_crypto_aead_noncelen(&aead);
  1024. assert(sizeof(key) == keylen);
  1025. assert(sizeof(iv) == ivlen);
  1026. if (crypto_derive_token_key(key, keylen, iv, ivlen, &md, secret, secretlen,
  1027. rand_data, NGTCP2_CRYPTO_TOKEN_RAND_DATALEN,
  1028. retry_token_info_prefix2,
  1029. sizeof(retry_token_info_prefix2) - 1) != 0) {
  1030. return NGTCP2_CRYPTO_ERR_INTERNAL;
  1031. }
  1032. aadlen = crypto_generate_retry_token_aad2(aad, version, dcid);
  1033. if (ngtcp2_crypto_aead_ctx_decrypt_init(&aead_ctx, &aead, key, ivlen) != 0) {
  1034. return NGTCP2_CRYPTO_ERR_INTERNAL;
  1035. }
  1036. rv = ngtcp2_crypto_decrypt(plaintext, &aead, &aead_ctx, ciphertext,
  1037. ciphertextlen, iv, ivlen, aad, aadlen);
  1038. ngtcp2_crypto_aead_ctx_free(&aead_ctx);
  1039. if (rv != 0) {
  1040. return NGTCP2_CRYPTO_ERR_UNREADABLE_TOKEN;
  1041. }
  1042. p = plaintext;
  1043. memcpy(&addr, p, sizeof(addr));
  1044. switch (addr.sa.sa_family) {
  1045. case NGTCP2_AF_INET:
  1046. addrlen = sizeof(ngtcp2_sockaddr_in);
  1047. break;
  1048. case NGTCP2_AF_INET6:
  1049. addrlen = sizeof(ngtcp2_sockaddr_in6);
  1050. break;
  1051. default:
  1052. return NGTCP2_CRYPTO_ERR_VERIFY_TOKEN;
  1053. }
  1054. if (addrlen != (size_t)remote_addrlen ||
  1055. memcmp(&addr, remote_addr, addrlen) != 0) {
  1056. return NGTCP2_CRYPTO_ERR_VERIFY_TOKEN;
  1057. }
  1058. p += sizeof(addr);
  1059. cil = *p++;
  1060. if (cil != 0 && (cil < NGTCP2_MIN_CIDLEN || cil > NGTCP2_MAX_CIDLEN)) {
  1061. return NGTCP2_CRYPTO_ERR_VERIFY_TOKEN;
  1062. }
  1063. memcpy(&gen_ts, p + NGTCP2_MAX_CIDLEN, sizeof(gen_ts));
  1064. gen_ts = ngtcp2_ntohl64(gen_ts);
  1065. if (gen_ts + timeout <= ts) {
  1066. return NGTCP2_CRYPTO_ERR_VERIFY_TOKEN;
  1067. }
  1068. ngtcp2_cid_init(odcid, p, cil);
  1069. return 0;
  1070. }
  1071. static size_t crypto_generate_regular_token_aad(uint8_t *dest,
  1072. const ngtcp2_sockaddr *sa) {
  1073. const uint8_t *addr;
  1074. size_t addrlen;
  1075. switch (sa->sa_family) {
  1076. case NGTCP2_AF_INET:
  1077. addr = (const uint8_t *)&((const ngtcp2_sockaddr_in *)(void *)sa)->sin_addr;
  1078. addrlen = sizeof(((const ngtcp2_sockaddr_in *)(void *)sa)->sin_addr);
  1079. break;
  1080. case NGTCP2_AF_INET6:
  1081. addr =
  1082. (const uint8_t *)&((const ngtcp2_sockaddr_in6 *)(void *)sa)->sin6_addr;
  1083. addrlen = sizeof(((const ngtcp2_sockaddr_in6 *)(void *)sa)->sin6_addr);
  1084. break;
  1085. default:
  1086. assert(0);
  1087. abort();
  1088. }
  1089. memcpy(dest, addr, addrlen);
  1090. return addrlen;
  1091. }
  1092. static const uint8_t regular_token_info_prefix[] = "regular_token";
  1093. ngtcp2_ssize ngtcp2_crypto_generate_regular_token(
  1094. uint8_t *token, const uint8_t *secret, size_t secretlen,
  1095. const ngtcp2_sockaddr *remote_addr, ngtcp2_socklen remote_addrlen,
  1096. ngtcp2_tstamp ts) {
  1097. uint8_t plaintext[sizeof(ngtcp2_tstamp)];
  1098. uint8_t rand_data[NGTCP2_CRYPTO_TOKEN_RAND_DATALEN];
  1099. uint8_t key[16];
  1100. uint8_t iv[12];
  1101. size_t keylen;
  1102. size_t ivlen;
  1103. ngtcp2_crypto_aead aead;
  1104. ngtcp2_crypto_md md;
  1105. ngtcp2_crypto_aead_ctx aead_ctx;
  1106. size_t plaintextlen;
  1107. uint8_t aad[sizeof(ngtcp2_sockaddr_in6)];
  1108. size_t aadlen;
  1109. uint8_t *p = plaintext;
  1110. ngtcp2_tstamp ts_be = ngtcp2_htonl64(ts);
  1111. int rv;
  1112. (void)remote_addrlen;
  1113. memcpy(p, &ts_be, sizeof(ts_be));
  1114. p += sizeof(ts_be);
  1115. plaintextlen = (size_t)(p - plaintext);
  1116. if (ngtcp2_crypto_random(rand_data, sizeof(rand_data)) != 0) {
  1117. return -1;
  1118. }
  1119. ngtcp2_crypto_aead_aes_128_gcm(&aead);
  1120. ngtcp2_crypto_md_sha256(&md);
  1121. keylen = ngtcp2_crypto_aead_keylen(&aead);
  1122. ivlen = ngtcp2_crypto_aead_noncelen(&aead);
  1123. assert(sizeof(key) == keylen);
  1124. assert(sizeof(iv) == ivlen);
  1125. if (crypto_derive_token_key(key, keylen, iv, ivlen, &md, secret, secretlen,
  1126. rand_data, sizeof(rand_data),
  1127. regular_token_info_prefix,
  1128. sizeof(regular_token_info_prefix) - 1) != 0) {
  1129. return -1;
  1130. }
  1131. aadlen = crypto_generate_regular_token_aad(aad, remote_addr);
  1132. p = token;
  1133. *p++ = NGTCP2_CRYPTO_TOKEN_MAGIC_REGULAR;
  1134. if (ngtcp2_crypto_aead_ctx_encrypt_init(&aead_ctx, &aead, key, ivlen) != 0) {
  1135. return -1;
  1136. }
  1137. rv = ngtcp2_crypto_encrypt(p, &aead, &aead_ctx, plaintext, plaintextlen, iv,
  1138. ivlen, aad, aadlen);
  1139. ngtcp2_crypto_aead_ctx_free(&aead_ctx);
  1140. if (rv != 0) {
  1141. return -1;
  1142. }
  1143. p += plaintextlen + aead.max_overhead;
  1144. memcpy(p, rand_data, sizeof(rand_data));
  1145. p += sizeof(rand_data);
  1146. return p - token;
  1147. }
  1148. int ngtcp2_crypto_verify_regular_token(const uint8_t *token, size_t tokenlen,
  1149. const uint8_t *secret, size_t secretlen,
  1150. const ngtcp2_sockaddr *remote_addr,
  1151. ngtcp2_socklen remote_addrlen,
  1152. ngtcp2_duration timeout,
  1153. ngtcp2_tstamp ts) {
  1154. uint8_t plaintext[sizeof(ngtcp2_tstamp)];
  1155. uint8_t key[16];
  1156. uint8_t iv[12];
  1157. size_t keylen;
  1158. size_t ivlen;
  1159. ngtcp2_crypto_aead_ctx aead_ctx;
  1160. ngtcp2_crypto_aead aead;
  1161. ngtcp2_crypto_md md;
  1162. uint8_t aad[sizeof(ngtcp2_sockaddr_in6)];
  1163. size_t aadlen;
  1164. const uint8_t *rand_data;
  1165. const uint8_t *ciphertext;
  1166. size_t ciphertextlen;
  1167. int rv;
  1168. ngtcp2_tstamp gen_ts;
  1169. (void)remote_addrlen;
  1170. if (tokenlen != NGTCP2_CRYPTO_MAX_REGULAR_TOKENLEN ||
  1171. token[0] != NGTCP2_CRYPTO_TOKEN_MAGIC_REGULAR) {
  1172. return -1;
  1173. }
  1174. rand_data = token + tokenlen - NGTCP2_CRYPTO_TOKEN_RAND_DATALEN;
  1175. ciphertext = token + 1;
  1176. ciphertextlen = tokenlen - 1 - NGTCP2_CRYPTO_TOKEN_RAND_DATALEN;
  1177. ngtcp2_crypto_aead_aes_128_gcm(&aead);
  1178. ngtcp2_crypto_md_sha256(&md);
  1179. keylen = ngtcp2_crypto_aead_keylen(&aead);
  1180. ivlen = ngtcp2_crypto_aead_noncelen(&aead);
  1181. assert(sizeof(key) == keylen);
  1182. assert(sizeof(iv) == ivlen);
  1183. if (crypto_derive_token_key(key, keylen, iv, ivlen, &md, secret, secretlen,
  1184. rand_data, NGTCP2_CRYPTO_TOKEN_RAND_DATALEN,
  1185. regular_token_info_prefix,
  1186. sizeof(regular_token_info_prefix) - 1) != 0) {
  1187. return -1;
  1188. }
  1189. aadlen = crypto_generate_regular_token_aad(aad, remote_addr);
  1190. if (ngtcp2_crypto_aead_ctx_decrypt_init(&aead_ctx, &aead, key, ivlen) != 0) {
  1191. return -1;
  1192. }
  1193. rv = ngtcp2_crypto_decrypt(plaintext, &aead, &aead_ctx, ciphertext,
  1194. ciphertextlen, iv, ivlen, aad, aadlen);
  1195. ngtcp2_crypto_aead_ctx_free(&aead_ctx);
  1196. if (rv != 0) {
  1197. return -1;
  1198. }
  1199. memcpy(&gen_ts, plaintext, sizeof(gen_ts));
  1200. gen_ts = ngtcp2_ntohl64(gen_ts);
  1201. if (gen_ts + timeout <= ts) {
  1202. return -1;
  1203. }
  1204. return 0;
  1205. }
  1206. ngtcp2_ssize ngtcp2_crypto_write_connection_close(
  1207. uint8_t *dest, size_t destlen, uint32_t version, const ngtcp2_cid *dcid,
  1208. const ngtcp2_cid *scid, uint64_t error_code, const uint8_t *reason,
  1209. size_t reasonlen) {
  1210. uint8_t rx_secret[NGTCP2_CRYPTO_INITIAL_SECRETLEN];
  1211. uint8_t tx_secret[NGTCP2_CRYPTO_INITIAL_SECRETLEN];
  1212. uint8_t initial_secret[NGTCP2_CRYPTO_INITIAL_SECRETLEN];
  1213. uint8_t tx_key[NGTCP2_CRYPTO_INITIAL_KEYLEN];
  1214. uint8_t tx_iv[NGTCP2_CRYPTO_INITIAL_IVLEN];
  1215. uint8_t tx_hp_key[NGTCP2_CRYPTO_INITIAL_KEYLEN];
  1216. ngtcp2_crypto_ctx ctx;
  1217. ngtcp2_ssize spktlen;
  1218. ngtcp2_crypto_aead_ctx aead_ctx = {0};
  1219. ngtcp2_crypto_cipher_ctx hp_ctx = {0};
  1220. ngtcp2_crypto_ctx_initial(&ctx);
  1221. if (ngtcp2_crypto_derive_initial_secrets(rx_secret, tx_secret, initial_secret,
  1222. version, scid,
  1223. NGTCP2_CRYPTO_SIDE_SERVER) != 0) {
  1224. return -1;
  1225. }
  1226. if (ngtcp2_crypto_derive_packet_protection_key(
  1227. tx_key, tx_iv, tx_hp_key, version, &ctx.aead, &ctx.md, tx_secret,
  1228. NGTCP2_CRYPTO_INITIAL_SECRETLEN) != 0) {
  1229. return -1;
  1230. }
  1231. if (ngtcp2_crypto_aead_ctx_encrypt_init(&aead_ctx, &ctx.aead, tx_key,
  1232. NGTCP2_CRYPTO_INITIAL_IVLEN) != 0) {
  1233. spktlen = -1;
  1234. goto end;
  1235. }
  1236. if (ngtcp2_crypto_cipher_ctx_encrypt_init(&hp_ctx, &ctx.hp, tx_hp_key) != 0) {
  1237. spktlen = -1;
  1238. goto end;
  1239. }
  1240. spktlen = ngtcp2_pkt_write_connection_close(
  1241. dest, destlen, version, dcid, scid, error_code, reason, reasonlen,
  1242. ngtcp2_crypto_encrypt_cb, &ctx.aead, &aead_ctx, tx_iv,
  1243. ngtcp2_crypto_hp_mask_cb, &ctx.hp, &hp_ctx);
  1244. if (spktlen < 0) {
  1245. spktlen = -1;
  1246. }
  1247. end:
  1248. ngtcp2_crypto_cipher_ctx_free(&hp_ctx);
  1249. ngtcp2_crypto_aead_ctx_free(&aead_ctx);
  1250. return spktlen;
  1251. }
  1252. ngtcp2_ssize ngtcp2_crypto_write_retry(uint8_t *dest, size_t destlen,
  1253. uint32_t version, const ngtcp2_cid *dcid,
  1254. const ngtcp2_cid *scid,
  1255. const ngtcp2_cid *odcid,
  1256. const uint8_t *token, size_t tokenlen) {
  1257. ngtcp2_crypto_aead aead;
  1258. ngtcp2_ssize spktlen;
  1259. ngtcp2_crypto_aead_ctx aead_ctx = {0};
  1260. const uint8_t *key;
  1261. size_t noncelen;
  1262. ngtcp2_crypto_aead_retry(&aead);
  1263. switch (version) {
  1264. case NGTCP2_PROTO_VER_V1:
  1265. default:
  1266. key = (const uint8_t *)NGTCP2_RETRY_KEY_V1;
  1267. noncelen = sizeof(NGTCP2_RETRY_NONCE_V1) - 1;
  1268. break;
  1269. case NGTCP2_PROTO_VER_V2:
  1270. key = (const uint8_t *)NGTCP2_RETRY_KEY_V2;
  1271. noncelen = sizeof(NGTCP2_RETRY_NONCE_V2) - 1;
  1272. break;
  1273. }
  1274. if (ngtcp2_crypto_aead_ctx_encrypt_init(&aead_ctx, &aead, key, noncelen) !=
  1275. 0) {
  1276. return -1;
  1277. }
  1278. spktlen = ngtcp2_pkt_write_retry(dest, destlen, version, dcid, scid, odcid,
  1279. token, tokenlen, ngtcp2_crypto_encrypt_cb,
  1280. &aead, &aead_ctx);
  1281. if (spktlen < 0) {
  1282. spktlen = -1;
  1283. }
  1284. ngtcp2_crypto_aead_ctx_free(&aead_ctx);
  1285. return spktlen;
  1286. }
  1287. int ngtcp2_crypto_client_initial_cb(ngtcp2_conn *conn, void *user_data) {
  1288. const ngtcp2_cid *dcid = ngtcp2_conn_get_dcid(conn);
  1289. void *tls = ngtcp2_conn_get_tls_native_handle(conn);
  1290. (void)user_data;
  1291. if (ngtcp2_crypto_derive_and_install_initial_key(
  1292. conn, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  1293. ngtcp2_conn_get_client_chosen_version(conn), dcid) != 0) {
  1294. return NGTCP2_ERR_CALLBACK_FAILURE;
  1295. }
  1296. if (crypto_set_local_transport_params(conn, tls) != 0) {
  1297. return NGTCP2_ERR_CALLBACK_FAILURE;
  1298. }
  1299. if (ngtcp2_crypto_read_write_crypto_data(
  1300. conn, NGTCP2_ENCRYPTION_LEVEL_INITIAL, NULL, 0) != 0) {
  1301. return NGTCP2_ERR_CALLBACK_FAILURE;
  1302. }
  1303. return 0;
  1304. }
  1305. int ngtcp2_crypto_recv_retry_cb(ngtcp2_conn *conn, const ngtcp2_pkt_hd *hd,
  1306. void *user_data) {
  1307. (void)user_data;
  1308. if (ngtcp2_crypto_derive_and_install_initial_key(
  1309. conn, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  1310. ngtcp2_conn_get_client_chosen_version(conn), &hd->scid) != 0) {
  1311. return NGTCP2_ERR_CALLBACK_FAILURE;
  1312. }
  1313. return 0;
  1314. }
  1315. int ngtcp2_crypto_recv_client_initial_cb(ngtcp2_conn *conn,
  1316. const ngtcp2_cid *dcid,
  1317. void *user_data) {
  1318. (void)user_data;
  1319. if (ngtcp2_crypto_derive_and_install_initial_key(
  1320. conn, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  1321. ngtcp2_conn_get_client_chosen_version(conn), dcid) != 0) {
  1322. return NGTCP2_ERR_CALLBACK_FAILURE;
  1323. }
  1324. return 0;
  1325. }
  1326. int ngtcp2_crypto_version_negotiation_cb(ngtcp2_conn *conn, uint32_t version,
  1327. const ngtcp2_cid *client_dcid,
  1328. void *user_data) {
  1329. (void)user_data;
  1330. if (ngtcp2_crypto_derive_and_install_vneg_initial_key(
  1331. conn, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, version,
  1332. client_dcid) != 0) {
  1333. return NGTCP2_ERR_CALLBACK_FAILURE;
  1334. }
  1335. return 0;
  1336. }
  1337. void ngtcp2_crypto_delete_crypto_aead_ctx_cb(ngtcp2_conn *conn,
  1338. ngtcp2_crypto_aead_ctx *aead_ctx,
  1339. void *user_data) {
  1340. (void)conn;
  1341. (void)user_data;
  1342. ngtcp2_crypto_aead_ctx_free(aead_ctx);
  1343. }
  1344. void ngtcp2_crypto_delete_crypto_cipher_ctx_cb(
  1345. ngtcp2_conn *conn, ngtcp2_crypto_cipher_ctx *cipher_ctx, void *user_data) {
  1346. (void)conn;
  1347. (void)user_data;
  1348. ngtcp2_crypto_cipher_ctx_free(cipher_ctx);
  1349. }
  1350. int ngtcp2_crypto_recv_crypto_data_cb(ngtcp2_conn *conn,
  1351. ngtcp2_encryption_level encryption_level,
  1352. uint64_t offset, const uint8_t *data,
  1353. size_t datalen, void *user_data) {
  1354. int rv;
  1355. (void)offset;
  1356. (void)user_data;
  1357. if (ngtcp2_crypto_read_write_crypto_data(conn, encryption_level, data,
  1358. datalen) != 0) {
  1359. rv = ngtcp2_conn_get_tls_error(conn);
  1360. if (rv) {
  1361. return rv;
  1362. }
  1363. return NGTCP2_ERR_CRYPTO;
  1364. }
  1365. return 0;
  1366. }