quictls.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  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. #ifdef HAVE_CONFIG_H
  26. # include <config.h>
  27. #endif /* defined(HAVE_CONFIG_H) */
  28. #include <assert.h>
  29. #include <ngtcp2/ngtcp2_crypto.h>
  30. #include <ngtcp2/ngtcp2_crypto_quictls.h>
  31. #include <openssl/ssl.h>
  32. #include <openssl/evp.h>
  33. #include <openssl/kdf.h>
  34. #include <openssl/rand.h>
  35. #if OPENSSL_VERSION_NUMBER >= 0x30000000L
  36. # error #include <openssl/core_names.h>
  37. #endif /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
  38. #include "shared.h"
  39. #if OPENSSL_VERSION_NUMBER >= 0x30000000L
  40. static int crypto_initialized;
  41. static EVP_CIPHER *crypto_aes_128_gcm;
  42. static EVP_CIPHER *crypto_aes_256_gcm;
  43. static EVP_CIPHER *crypto_chacha20_poly1305;
  44. static EVP_CIPHER *crypto_aes_128_ccm;
  45. static EVP_CIPHER *crypto_aes_128_ctr;
  46. static EVP_CIPHER *crypto_aes_256_ctr;
  47. static EVP_CIPHER *crypto_chacha20;
  48. static EVP_MD *crypto_sha256;
  49. static EVP_MD *crypto_sha384;
  50. static EVP_KDF *crypto_hkdf;
  51. int ngtcp2_crypto_quictls_init(void) {
  52. crypto_aes_128_gcm = EVP_CIPHER_fetch(NULL, "AES-128-GCM", NULL);
  53. if (crypto_aes_128_gcm == NULL) {
  54. return -1;
  55. }
  56. crypto_aes_256_gcm = EVP_CIPHER_fetch(NULL, "AES-256-GCM", NULL);
  57. if (crypto_aes_256_gcm == NULL) {
  58. return -1;
  59. }
  60. crypto_chacha20_poly1305 = EVP_CIPHER_fetch(NULL, "ChaCha20-Poly1305", NULL);
  61. if (crypto_chacha20_poly1305 == NULL) {
  62. return -1;
  63. }
  64. crypto_aes_128_ccm = EVP_CIPHER_fetch(NULL, "AES-128-CCM", NULL);
  65. if (crypto_aes_128_ccm == NULL) {
  66. return -1;
  67. }
  68. crypto_aes_128_ctr = EVP_CIPHER_fetch(NULL, "AES-128-CTR", NULL);
  69. if (crypto_aes_128_ctr == NULL) {
  70. return -1;
  71. }
  72. crypto_aes_256_ctr = EVP_CIPHER_fetch(NULL, "AES-256-CTR", NULL);
  73. if (crypto_aes_256_ctr == NULL) {
  74. return -1;
  75. }
  76. crypto_chacha20 = EVP_CIPHER_fetch(NULL, "ChaCha20", NULL);
  77. if (crypto_chacha20 == NULL) {
  78. return -1;
  79. }
  80. crypto_sha256 = EVP_MD_fetch(NULL, "sha256", NULL);
  81. if (crypto_sha256 == NULL) {
  82. return -1;
  83. }
  84. crypto_sha384 = EVP_MD_fetch(NULL, "sha384", NULL);
  85. if (crypto_sha384 == NULL) {
  86. return -1;
  87. }
  88. crypto_hkdf = EVP_KDF_fetch(NULL, "hkdf", NULL);
  89. if (crypto_hkdf == NULL) {
  90. return -1;
  91. }
  92. crypto_initialized = 1;
  93. return 0;
  94. }
  95. static const EVP_CIPHER *crypto_aead_aes_128_gcm(void) {
  96. if (crypto_aes_128_gcm) {
  97. return crypto_aes_128_gcm;
  98. }
  99. return EVP_aes_128_gcm();
  100. }
  101. static const EVP_CIPHER *crypto_aead_aes_256_gcm(void) {
  102. if (crypto_aes_256_gcm) {
  103. return crypto_aes_256_gcm;
  104. }
  105. return EVP_aes_256_gcm();
  106. }
  107. static const EVP_CIPHER *crypto_aead_chacha20_poly1305(void) {
  108. if (crypto_chacha20_poly1305) {
  109. return crypto_chacha20_poly1305;
  110. }
  111. return EVP_chacha20_poly1305();
  112. }
  113. static const EVP_CIPHER *crypto_aead_aes_128_ccm(void) {
  114. if (crypto_aes_128_ccm) {
  115. return crypto_aes_128_ccm;
  116. }
  117. return EVP_aes_128_ccm();
  118. }
  119. static const EVP_CIPHER *crypto_cipher_aes_128_ctr(void) {
  120. if (crypto_aes_128_ctr) {
  121. return crypto_aes_128_ctr;
  122. }
  123. return EVP_aes_128_ctr();
  124. }
  125. static const EVP_CIPHER *crypto_cipher_aes_256_ctr(void) {
  126. if (crypto_aes_256_ctr) {
  127. return crypto_aes_256_ctr;
  128. }
  129. return EVP_aes_256_ctr();
  130. }
  131. static const EVP_CIPHER *crypto_cipher_chacha20(void) {
  132. if (crypto_chacha20) {
  133. return crypto_chacha20;
  134. }
  135. return EVP_chacha20();
  136. }
  137. static const EVP_MD *crypto_md_sha256(void) {
  138. if (crypto_sha256) {
  139. return crypto_sha256;
  140. }
  141. return EVP_sha256();
  142. }
  143. static const EVP_MD *crypto_md_sha384(void) {
  144. if (crypto_sha384) {
  145. return crypto_sha384;
  146. }
  147. return EVP_sha384();
  148. }
  149. static EVP_KDF *crypto_kdf_hkdf(void) {
  150. if (crypto_hkdf) {
  151. return crypto_hkdf;
  152. }
  153. return EVP_KDF_fetch(NULL, "hkdf", NULL);
  154. }
  155. #else /* !(OPENSSL_VERSION_NUMBER >= 0x30000000L) */
  156. # define crypto_aead_aes_128_gcm EVP_aes_128_gcm
  157. # define crypto_aead_aes_256_gcm EVP_aes_256_gcm
  158. # define crypto_aead_chacha20_poly1305 EVP_chacha20_poly1305
  159. # define crypto_aead_aes_128_ccm EVP_aes_128_ccm
  160. # define crypto_cipher_aes_128_ctr EVP_aes_128_ctr
  161. # define crypto_cipher_aes_256_ctr EVP_aes_256_ctr
  162. # define crypto_cipher_chacha20 EVP_chacha20
  163. # define crypto_md_sha256 EVP_sha256
  164. # define crypto_md_sha384 EVP_sha384
  165. int ngtcp2_crypto_quictls_init(void) { return 0; }
  166. #endif /* !(OPENSSL_VERSION_NUMBER >= 0x30000000L) */
  167. static size_t crypto_aead_max_overhead(const EVP_CIPHER *aead) {
  168. switch (EVP_CIPHER_nid(aead)) {
  169. case NID_aes_128_gcm:
  170. case NID_aes_256_gcm:
  171. return EVP_GCM_TLS_TAG_LEN;
  172. case NID_chacha20_poly1305:
  173. return EVP_CHACHAPOLY_TLS_TAG_LEN;
  174. case NID_aes_128_ccm:
  175. return EVP_CCM_TLS_TAG_LEN;
  176. default:
  177. assert(0);
  178. abort(); /* if NDEBUG is set */
  179. }
  180. }
  181. ngtcp2_crypto_aead *ngtcp2_crypto_aead_aes_128_gcm(ngtcp2_crypto_aead *aead) {
  182. return ngtcp2_crypto_aead_init(aead, (void *)crypto_aead_aes_128_gcm());
  183. }
  184. ngtcp2_crypto_md *ngtcp2_crypto_md_sha256(ngtcp2_crypto_md *md) {
  185. md->native_handle = (void *)crypto_md_sha256();
  186. return md;
  187. }
  188. ngtcp2_crypto_ctx *ngtcp2_crypto_ctx_initial(ngtcp2_crypto_ctx *ctx) {
  189. ngtcp2_crypto_aead_init(&ctx->aead, (void *)crypto_aead_aes_128_gcm());
  190. ctx->md.native_handle = (void *)crypto_md_sha256();
  191. ctx->hp.native_handle = (void *)crypto_cipher_aes_128_ctr();
  192. ctx->max_encryption = 0;
  193. ctx->max_decryption_failure = 0;
  194. return ctx;
  195. }
  196. ngtcp2_crypto_aead *ngtcp2_crypto_aead_init(ngtcp2_crypto_aead *aead,
  197. void *aead_native_handle) {
  198. aead->native_handle = aead_native_handle;
  199. aead->max_overhead = crypto_aead_max_overhead(aead_native_handle);
  200. return aead;
  201. }
  202. ngtcp2_crypto_aead *ngtcp2_crypto_aead_retry(ngtcp2_crypto_aead *aead) {
  203. return ngtcp2_crypto_aead_init(aead, (void *)crypto_aead_aes_128_gcm());
  204. }
  205. static const EVP_CIPHER *crypto_cipher_id_get_aead(uint32_t cipher_id) {
  206. switch (cipher_id) {
  207. case TLS1_3_CK_AES_128_GCM_SHA256:
  208. return crypto_aead_aes_128_gcm();
  209. case TLS1_3_CK_AES_256_GCM_SHA384:
  210. return crypto_aead_aes_256_gcm();
  211. case TLS1_3_CK_CHACHA20_POLY1305_SHA256:
  212. return crypto_aead_chacha20_poly1305();
  213. case TLS1_3_CK_AES_128_CCM_SHA256:
  214. return crypto_aead_aes_128_ccm();
  215. default:
  216. return NULL;
  217. }
  218. }
  219. static uint64_t crypto_cipher_id_get_aead_max_encryption(uint32_t cipher_id) {
  220. switch (cipher_id) {
  221. case TLS1_3_CK_AES_128_GCM_SHA256:
  222. case TLS1_3_CK_AES_256_GCM_SHA384:
  223. return NGTCP2_CRYPTO_MAX_ENCRYPTION_AES_GCM;
  224. case TLS1_3_CK_CHACHA20_POLY1305_SHA256:
  225. return NGTCP2_CRYPTO_MAX_ENCRYPTION_CHACHA20_POLY1305;
  226. case TLS1_3_CK_AES_128_CCM_SHA256:
  227. return NGTCP2_CRYPTO_MAX_ENCRYPTION_AES_CCM;
  228. default:
  229. return 0;
  230. }
  231. }
  232. static uint64_t
  233. crypto_cipher_id_get_aead_max_decryption_failure(uint32_t cipher_id) {
  234. switch (cipher_id) {
  235. case TLS1_3_CK_AES_128_GCM_SHA256:
  236. case TLS1_3_CK_AES_256_GCM_SHA384:
  237. return NGTCP2_CRYPTO_MAX_DECRYPTION_FAILURE_AES_GCM;
  238. case TLS1_3_CK_CHACHA20_POLY1305_SHA256:
  239. return NGTCP2_CRYPTO_MAX_DECRYPTION_FAILURE_CHACHA20_POLY1305;
  240. case TLS1_3_CK_AES_128_CCM_SHA256:
  241. return NGTCP2_CRYPTO_MAX_DECRYPTION_FAILURE_AES_CCM;
  242. default:
  243. return 0;
  244. }
  245. }
  246. static const EVP_CIPHER *crypto_cipher_id_get_hp(uint32_t cipher_id) {
  247. switch (cipher_id) {
  248. case TLS1_3_CK_AES_128_GCM_SHA256:
  249. case TLS1_3_CK_AES_128_CCM_SHA256:
  250. return crypto_cipher_aes_128_ctr();
  251. case TLS1_3_CK_AES_256_GCM_SHA384:
  252. return crypto_cipher_aes_256_ctr();
  253. case TLS1_3_CK_CHACHA20_POLY1305_SHA256:
  254. return crypto_cipher_chacha20();
  255. default:
  256. return NULL;
  257. }
  258. }
  259. static const EVP_MD *crypto_cipher_id_get_md(uint32_t cipher_id) {
  260. switch (cipher_id) {
  261. case TLS1_3_CK_AES_128_GCM_SHA256:
  262. case TLS1_3_CK_CHACHA20_POLY1305_SHA256:
  263. case TLS1_3_CK_AES_128_CCM_SHA256:
  264. return crypto_md_sha256();
  265. case TLS1_3_CK_AES_256_GCM_SHA384:
  266. return crypto_md_sha384();
  267. default:
  268. return NULL;
  269. }
  270. }
  271. static int supported_cipher_id(uint32_t cipher_id) {
  272. switch (cipher_id) {
  273. case TLS1_3_CK_AES_128_GCM_SHA256:
  274. case TLS1_3_CK_AES_256_GCM_SHA384:
  275. case TLS1_3_CK_CHACHA20_POLY1305_SHA256:
  276. case TLS1_3_CK_AES_128_CCM_SHA256:
  277. return 1;
  278. default:
  279. return 0;
  280. }
  281. }
  282. static ngtcp2_crypto_ctx *crypto_ctx_cipher_id(ngtcp2_crypto_ctx *ctx,
  283. uint32_t cipher_id) {
  284. ngtcp2_crypto_aead_init(&ctx->aead,
  285. (void *)crypto_cipher_id_get_aead(cipher_id));
  286. ctx->md.native_handle = (void *)crypto_cipher_id_get_md(cipher_id);
  287. ctx->hp.native_handle = (void *)crypto_cipher_id_get_hp(cipher_id);
  288. ctx->max_encryption = crypto_cipher_id_get_aead_max_encryption(cipher_id);
  289. ctx->max_decryption_failure =
  290. crypto_cipher_id_get_aead_max_decryption_failure(cipher_id);
  291. return ctx;
  292. }
  293. ngtcp2_crypto_ctx *ngtcp2_crypto_ctx_tls(ngtcp2_crypto_ctx *ctx,
  294. void *tls_native_handle) {
  295. SSL *ssl = tls_native_handle;
  296. const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl);
  297. uint32_t cipher_id;
  298. if (cipher == NULL) {
  299. return NULL;
  300. }
  301. cipher_id = (uint32_t)SSL_CIPHER_get_id(cipher);
  302. if (!supported_cipher_id(cipher_id)) {
  303. return NULL;
  304. }
  305. return crypto_ctx_cipher_id(ctx, cipher_id);
  306. }
  307. ngtcp2_crypto_ctx *ngtcp2_crypto_ctx_tls_early(ngtcp2_crypto_ctx *ctx,
  308. void *tls_native_handle) {
  309. return ngtcp2_crypto_ctx_tls(ctx, tls_native_handle);
  310. }
  311. static size_t crypto_md_hashlen(const EVP_MD *md) {
  312. return (size_t)EVP_MD_size(md);
  313. }
  314. size_t ngtcp2_crypto_md_hashlen(const ngtcp2_crypto_md *md) {
  315. return crypto_md_hashlen(md->native_handle);
  316. }
  317. static size_t crypto_aead_keylen(const EVP_CIPHER *aead) {
  318. return (size_t)EVP_CIPHER_key_length(aead);
  319. }
  320. size_t ngtcp2_crypto_aead_keylen(const ngtcp2_crypto_aead *aead) {
  321. return crypto_aead_keylen(aead->native_handle);
  322. }
  323. static size_t crypto_aead_noncelen(const EVP_CIPHER *aead) {
  324. return (size_t)EVP_CIPHER_iv_length(aead);
  325. }
  326. size_t ngtcp2_crypto_aead_noncelen(const ngtcp2_crypto_aead *aead) {
  327. return crypto_aead_noncelen(aead->native_handle);
  328. }
  329. int ngtcp2_crypto_aead_ctx_encrypt_init(ngtcp2_crypto_aead_ctx *aead_ctx,
  330. const ngtcp2_crypto_aead *aead,
  331. const uint8_t *key, size_t noncelen) {
  332. const EVP_CIPHER *cipher = aead->native_handle;
  333. int cipher_nid = EVP_CIPHER_nid(cipher);
  334. EVP_CIPHER_CTX *actx;
  335. size_t taglen = crypto_aead_max_overhead(cipher);
  336. #if OPENSSL_VERSION_NUMBER >= 0x30000000L
  337. OSSL_PARAM params[3];
  338. #endif /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
  339. actx = EVP_CIPHER_CTX_new();
  340. if (actx == NULL) {
  341. return -1;
  342. }
  343. #if OPENSSL_VERSION_NUMBER >= 0x30000000L
  344. params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &noncelen);
  345. if (cipher_nid == NID_aes_128_ccm) {
  346. params[1] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG,
  347. NULL, taglen);
  348. params[2] = OSSL_PARAM_construct_end();
  349. } else {
  350. params[1] = OSSL_PARAM_construct_end();
  351. }
  352. #endif /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
  353. if (!EVP_EncryptInit_ex(actx, cipher, NULL, NULL, NULL) ||
  354. #if OPENSSL_VERSION_NUMBER >= 0x30000000L
  355. !EVP_CIPHER_CTX_set_params(actx, params) ||
  356. #else /* !(OPENSSL_VERSION_NUMBER >= 0x30000000L) */
  357. !EVP_CIPHER_CTX_ctrl(actx, EVP_CTRL_AEAD_SET_IVLEN, (int)noncelen,
  358. NULL) ||
  359. (cipher_nid == NID_aes_128_ccm &&
  360. !EVP_CIPHER_CTX_ctrl(actx, EVP_CTRL_AEAD_SET_TAG, (int)taglen, NULL)) ||
  361. #endif /* !(OPENSSL_VERSION_NUMBER >= 0x30000000L) */
  362. !EVP_EncryptInit_ex(actx, NULL, NULL, key, NULL)) {
  363. EVP_CIPHER_CTX_free(actx);
  364. return -1;
  365. }
  366. aead_ctx->native_handle = actx;
  367. return 0;
  368. }
  369. int ngtcp2_crypto_aead_ctx_decrypt_init(ngtcp2_crypto_aead_ctx *aead_ctx,
  370. const ngtcp2_crypto_aead *aead,
  371. const uint8_t *key, size_t noncelen) {
  372. const EVP_CIPHER *cipher = aead->native_handle;
  373. int cipher_nid = EVP_CIPHER_nid(cipher);
  374. EVP_CIPHER_CTX *actx;
  375. size_t taglen = crypto_aead_max_overhead(cipher);
  376. #if OPENSSL_VERSION_NUMBER >= 0x30000000L
  377. OSSL_PARAM params[3];
  378. #endif /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
  379. actx = EVP_CIPHER_CTX_new();
  380. if (actx == NULL) {
  381. return -1;
  382. }
  383. #if OPENSSL_VERSION_NUMBER >= 0x30000000L
  384. params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &noncelen);
  385. if (cipher_nid == NID_aes_128_ccm) {
  386. params[1] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG,
  387. NULL, taglen);
  388. params[2] = OSSL_PARAM_construct_end();
  389. } else {
  390. params[1] = OSSL_PARAM_construct_end();
  391. }
  392. #endif /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
  393. if (!EVP_DecryptInit_ex(actx, cipher, NULL, NULL, NULL) ||
  394. #if OPENSSL_VERSION_NUMBER >= 0x30000000L
  395. !EVP_CIPHER_CTX_set_params(actx, params) ||
  396. #else /* !(OPENSSL_VERSION_NUMBER >= 0x30000000L) */
  397. !EVP_CIPHER_CTX_ctrl(actx, EVP_CTRL_AEAD_SET_IVLEN, (int)noncelen,
  398. NULL) ||
  399. (cipher_nid == NID_aes_128_ccm &&
  400. !EVP_CIPHER_CTX_ctrl(actx, EVP_CTRL_AEAD_SET_TAG, (int)taglen, NULL)) ||
  401. #endif /* !(OPENSSL_VERSION_NUMBER >= 0x30000000L) */
  402. !EVP_DecryptInit_ex(actx, NULL, NULL, key, NULL)) {
  403. EVP_CIPHER_CTX_free(actx);
  404. return -1;
  405. }
  406. aead_ctx->native_handle = actx;
  407. return 0;
  408. }
  409. void ngtcp2_crypto_aead_ctx_free(ngtcp2_crypto_aead_ctx *aead_ctx) {
  410. if (aead_ctx->native_handle) {
  411. EVP_CIPHER_CTX_free(aead_ctx->native_handle);
  412. }
  413. }
  414. int ngtcp2_crypto_cipher_ctx_encrypt_init(ngtcp2_crypto_cipher_ctx *cipher_ctx,
  415. const ngtcp2_crypto_cipher *cipher,
  416. const uint8_t *key) {
  417. EVP_CIPHER_CTX *actx;
  418. actx = EVP_CIPHER_CTX_new();
  419. if (actx == NULL) {
  420. return -1;
  421. }
  422. if (!EVP_EncryptInit_ex(actx, cipher->native_handle, NULL, key, NULL)) {
  423. EVP_CIPHER_CTX_free(actx);
  424. return -1;
  425. }
  426. cipher_ctx->native_handle = actx;
  427. return 0;
  428. }
  429. void ngtcp2_crypto_cipher_ctx_free(ngtcp2_crypto_cipher_ctx *cipher_ctx) {
  430. if (cipher_ctx->native_handle) {
  431. EVP_CIPHER_CTX_free(cipher_ctx->native_handle);
  432. }
  433. }
  434. int ngtcp2_crypto_hkdf_extract(uint8_t *dest, const ngtcp2_crypto_md *md,
  435. const uint8_t *secret, size_t secretlen,
  436. const uint8_t *salt, size_t saltlen) {
  437. #if OPENSSL_VERSION_NUMBER >= 0x30000000L
  438. const EVP_MD *prf = md->native_handle;
  439. EVP_KDF *kdf = crypto_kdf_hkdf();
  440. EVP_KDF_CTX *kctx = EVP_KDF_CTX_new(kdf);
  441. int mode = EVP_KDF_HKDF_MODE_EXTRACT_ONLY;
  442. OSSL_PARAM params[] = {
  443. OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE, &mode),
  444. OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
  445. (char *)EVP_MD_get0_name(prf), 0),
  446. OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, (void *)secret,
  447. secretlen),
  448. OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT, (void *)salt,
  449. saltlen),
  450. OSSL_PARAM_construct_end(),
  451. };
  452. int rv = 0;
  453. if (!crypto_initialized) {
  454. EVP_KDF_free(kdf);
  455. }
  456. if (EVP_KDF_derive(kctx, dest, (size_t)EVP_MD_size(prf), params) <= 0) {
  457. rv = -1;
  458. }
  459. EVP_KDF_CTX_free(kctx);
  460. return rv;
  461. #else /* !(OPENSSL_VERSION_NUMBER >= 0x30000000L) */
  462. const EVP_MD *prf = md->native_handle;
  463. int rv = 0;
  464. EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL);
  465. size_t destlen = (size_t)EVP_MD_size(prf);
  466. if (pctx == NULL) {
  467. return -1;
  468. }
  469. if (EVP_PKEY_derive_init(pctx) != 1 ||
  470. EVP_PKEY_CTX_hkdf_mode(pctx, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY) != 1 ||
  471. EVP_PKEY_CTX_set_hkdf_md(pctx, prf) != 1 ||
  472. EVP_PKEY_CTX_set1_hkdf_salt(pctx, salt, (int)saltlen) != 1 ||
  473. EVP_PKEY_CTX_set1_hkdf_key(pctx, secret, (int)secretlen) != 1 ||
  474. EVP_PKEY_derive(pctx, dest, &destlen) != 1) {
  475. rv = -1;
  476. }
  477. EVP_PKEY_CTX_free(pctx);
  478. return rv;
  479. #endif /* !(OPENSSL_VERSION_NUMBER >= 0x30000000L) */
  480. }
  481. int ngtcp2_crypto_hkdf_expand(uint8_t *dest, size_t destlen,
  482. const ngtcp2_crypto_md *md, const uint8_t *secret,
  483. size_t secretlen, const uint8_t *info,
  484. size_t infolen) {
  485. #if OPENSSL_VERSION_NUMBER >= 0x30000000L
  486. const EVP_MD *prf = md->native_handle;
  487. EVP_KDF *kdf = crypto_kdf_hkdf();
  488. EVP_KDF_CTX *kctx = EVP_KDF_CTX_new(kdf);
  489. int mode = EVP_KDF_HKDF_MODE_EXPAND_ONLY;
  490. OSSL_PARAM params[] = {
  491. OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE, &mode),
  492. OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
  493. (char *)EVP_MD_get0_name(prf), 0),
  494. OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, (void *)secret,
  495. secretlen),
  496. OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO, (void *)info,
  497. infolen),
  498. OSSL_PARAM_construct_end(),
  499. };
  500. int rv = 0;
  501. if (!crypto_initialized) {
  502. EVP_KDF_free(kdf);
  503. }
  504. if (EVP_KDF_derive(kctx, dest, destlen, params) <= 0) {
  505. rv = -1;
  506. }
  507. EVP_KDF_CTX_free(kctx);
  508. return rv;
  509. #else /* !(OPENSSL_VERSION_NUMBER >= 0x30000000L) */
  510. const EVP_MD *prf = md->native_handle;
  511. int rv = 0;
  512. EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL);
  513. if (pctx == NULL) {
  514. return -1;
  515. }
  516. if (EVP_PKEY_derive_init(pctx) != 1 ||
  517. EVP_PKEY_CTX_hkdf_mode(pctx, EVP_PKEY_HKDEF_MODE_EXPAND_ONLY) != 1 ||
  518. EVP_PKEY_CTX_set_hkdf_md(pctx, prf) != 1 ||
  519. EVP_PKEY_CTX_set1_hkdf_salt(pctx, (const unsigned char *)"", 0) != 1 ||
  520. EVP_PKEY_CTX_set1_hkdf_key(pctx, secret, (int)secretlen) != 1 ||
  521. EVP_PKEY_CTX_add1_hkdf_info(pctx, info, (int)infolen) != 1 ||
  522. EVP_PKEY_derive(pctx, dest, &destlen) != 1) {
  523. rv = -1;
  524. }
  525. EVP_PKEY_CTX_free(pctx);
  526. return rv;
  527. #endif /* !(OPENSSL_VERSION_NUMBER >= 0x30000000L) */
  528. }
  529. int ngtcp2_crypto_hkdf(uint8_t *dest, size_t destlen,
  530. const ngtcp2_crypto_md *md, const uint8_t *secret,
  531. size_t secretlen, const uint8_t *salt, size_t saltlen,
  532. const uint8_t *info, size_t infolen) {
  533. #if OPENSSL_VERSION_NUMBER >= 0x30000000L
  534. const EVP_MD *prf = md->native_handle;
  535. EVP_KDF *kdf = crypto_kdf_hkdf();
  536. EVP_KDF_CTX *kctx = EVP_KDF_CTX_new(kdf);
  537. OSSL_PARAM params[] = {
  538. OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
  539. (char *)EVP_MD_get0_name(prf), 0),
  540. OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, (void *)secret,
  541. secretlen),
  542. OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT, (void *)salt,
  543. saltlen),
  544. OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO, (void *)info,
  545. infolen),
  546. OSSL_PARAM_construct_end(),
  547. };
  548. int rv = 0;
  549. if (!crypto_initialized) {
  550. EVP_KDF_free(kdf);
  551. }
  552. if (EVP_KDF_derive(kctx, dest, destlen, params) <= 0) {
  553. rv = -1;
  554. }
  555. EVP_KDF_CTX_free(kctx);
  556. return rv;
  557. #else /* !(OPENSSL_VERSION_NUMBER >= 0x30000000L) */
  558. const EVP_MD *prf = md->native_handle;
  559. int rv = 0;
  560. EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL);
  561. if (pctx == NULL) {
  562. return -1;
  563. }
  564. if (EVP_PKEY_derive_init(pctx) != 1 ||
  565. EVP_PKEY_CTX_hkdf_mode(pctx, EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND) !=
  566. 1 ||
  567. EVP_PKEY_CTX_set_hkdf_md(pctx, prf) != 1 ||
  568. EVP_PKEY_CTX_set1_hkdf_salt(pctx, salt, (int)saltlen) != 1 ||
  569. EVP_PKEY_CTX_set1_hkdf_key(pctx, secret, (int)secretlen) != 1 ||
  570. EVP_PKEY_CTX_add1_hkdf_info(pctx, info, (int)infolen) != 1 ||
  571. EVP_PKEY_derive(pctx, dest, &destlen) != 1) {
  572. rv = -1;
  573. }
  574. EVP_PKEY_CTX_free(pctx);
  575. return rv;
  576. #endif /* !(OPENSSL_VERSION_NUMBER >= 0x30000000L) */
  577. }
  578. int ngtcp2_crypto_encrypt(uint8_t *dest, const ngtcp2_crypto_aead *aead,
  579. const ngtcp2_crypto_aead_ctx *aead_ctx,
  580. const uint8_t *plaintext, size_t plaintextlen,
  581. const uint8_t *nonce, size_t noncelen,
  582. const uint8_t *aad, size_t aadlen) {
  583. const EVP_CIPHER *cipher = aead->native_handle;
  584. size_t taglen = crypto_aead_max_overhead(cipher);
  585. int cipher_nid = EVP_CIPHER_nid(cipher);
  586. EVP_CIPHER_CTX *actx = aead_ctx->native_handle;
  587. int len;
  588. #if OPENSSL_VERSION_NUMBER >= 0x30000000L
  589. OSSL_PARAM params[] = {
  590. OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG,
  591. dest + plaintextlen, taglen),
  592. OSSL_PARAM_construct_end(),
  593. };
  594. #endif /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
  595. (void)noncelen;
  596. if (!EVP_EncryptInit_ex(actx, NULL, NULL, NULL, nonce) ||
  597. (cipher_nid == NID_aes_128_ccm &&
  598. !EVP_EncryptUpdate(actx, NULL, &len, NULL, (int)plaintextlen)) ||
  599. !EVP_EncryptUpdate(actx, NULL, &len, aad, (int)aadlen) ||
  600. !EVP_EncryptUpdate(actx, dest, &len, plaintext, (int)plaintextlen) ||
  601. !EVP_EncryptFinal_ex(actx, dest + len, &len) ||
  602. #if OPENSSL_VERSION_NUMBER >= 0x30000000L
  603. !EVP_CIPHER_CTX_get_params(actx, params)
  604. #else /* !(OPENSSL_VERSION_NUMBER >= 0x30000000L) */
  605. !EVP_CIPHER_CTX_ctrl(actx, EVP_CTRL_AEAD_GET_TAG, (int)taglen,
  606. dest + plaintextlen)
  607. #endif /* !(OPENSSL_VERSION_NUMBER >= 0x30000000L) */
  608. ) {
  609. return -1;
  610. }
  611. return 0;
  612. }
  613. int ngtcp2_crypto_decrypt(uint8_t *dest, const ngtcp2_crypto_aead *aead,
  614. const ngtcp2_crypto_aead_ctx *aead_ctx,
  615. const uint8_t *ciphertext, size_t ciphertextlen,
  616. const uint8_t *nonce, size_t noncelen,
  617. const uint8_t *aad, size_t aadlen) {
  618. const EVP_CIPHER *cipher = aead->native_handle;
  619. size_t taglen = crypto_aead_max_overhead(cipher);
  620. int cipher_nid = EVP_CIPHER_nid(cipher);
  621. EVP_CIPHER_CTX *actx = aead_ctx->native_handle;
  622. int len;
  623. const uint8_t *tag;
  624. #if OPENSSL_VERSION_NUMBER >= 0x30000000L
  625. OSSL_PARAM params[2];
  626. #endif /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
  627. (void)noncelen;
  628. if (taglen > ciphertextlen) {
  629. return -1;
  630. }
  631. ciphertextlen -= taglen;
  632. tag = ciphertext + ciphertextlen;
  633. #if OPENSSL_VERSION_NUMBER >= 0x30000000L
  634. params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG,
  635. (void *)tag, taglen);
  636. params[1] = OSSL_PARAM_construct_end();
  637. #endif /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
  638. if (!EVP_DecryptInit_ex(actx, NULL, NULL, NULL, nonce) ||
  639. #if OPENSSL_VERSION_NUMBER >= 0x30000000L
  640. !EVP_CIPHER_CTX_set_params(actx, params) ||
  641. #else /* !(OPENSSL_VERSION_NUMBER >= 0x30000000L) */
  642. !EVP_CIPHER_CTX_ctrl(actx, EVP_CTRL_AEAD_SET_TAG, (int)taglen,
  643. (uint8_t *)tag) ||
  644. #endif /* !(OPENSSL_VERSION_NUMBER >= 0x30000000L) */
  645. (cipher_nid == NID_aes_128_ccm &&
  646. !EVP_DecryptUpdate(actx, NULL, &len, NULL, (int)ciphertextlen)) ||
  647. !EVP_DecryptUpdate(actx, NULL, &len, aad, (int)aadlen) ||
  648. !EVP_DecryptUpdate(actx, dest, &len, ciphertext, (int)ciphertextlen) ||
  649. (cipher_nid != NID_aes_128_ccm &&
  650. !EVP_DecryptFinal_ex(actx, dest + ciphertextlen, &len))) {
  651. return -1;
  652. }
  653. return 0;
  654. }
  655. int ngtcp2_crypto_hp_mask(uint8_t *dest, const ngtcp2_crypto_cipher *hp,
  656. const ngtcp2_crypto_cipher_ctx *hp_ctx,
  657. const uint8_t *sample) {
  658. static const uint8_t PLAINTEXT[] = "\x00\x00\x00\x00\x00";
  659. EVP_CIPHER_CTX *actx = hp_ctx->native_handle;
  660. int len;
  661. (void)hp;
  662. if (!EVP_EncryptInit_ex(actx, NULL, NULL, NULL, sample) ||
  663. !EVP_EncryptUpdate(actx, dest, &len, PLAINTEXT, sizeof(PLAINTEXT) - 1) ||
  664. !EVP_EncryptFinal_ex(actx, dest + sizeof(PLAINTEXT) - 1, &len)) {
  665. return -1;
  666. }
  667. return 0;
  668. }
  669. int ngtcp2_crypto_read_write_crypto_data(
  670. ngtcp2_conn *conn, ngtcp2_encryption_level encryption_level,
  671. const uint8_t *data, size_t datalen) {
  672. SSL *ssl = ngtcp2_conn_get_tls_native_handle(conn);
  673. int rv;
  674. int err;
  675. if (SSL_provide_quic_data(
  676. ssl,
  677. ngtcp2_crypto_quictls_from_ngtcp2_encryption_level(encryption_level),
  678. data, datalen) != 1) {
  679. return -1;
  680. }
  681. if (!ngtcp2_conn_get_handshake_completed(conn)) {
  682. rv = SSL_do_handshake(ssl);
  683. if (rv <= 0) {
  684. err = SSL_get_error(ssl, rv);
  685. switch (err) {
  686. case SSL_ERROR_WANT_READ:
  687. case SSL_ERROR_WANT_WRITE:
  688. return 0;
  689. case SSL_ERROR_WANT_CLIENT_HELLO_CB:
  690. return NGTCP2_CRYPTO_QUICTLS_ERR_TLS_WANT_CLIENT_HELLO_CB;
  691. case SSL_ERROR_WANT_X509_LOOKUP:
  692. return NGTCP2_CRYPTO_QUICTLS_ERR_TLS_WANT_X509_LOOKUP;
  693. case SSL_ERROR_SSL:
  694. return -1;
  695. default:
  696. return -1;
  697. }
  698. }
  699. ngtcp2_conn_tls_handshake_completed(conn);
  700. }
  701. rv = SSL_process_quic_post_handshake(ssl);
  702. if (rv != 1) {
  703. err = SSL_get_error(ssl, rv);
  704. switch (err) {
  705. case SSL_ERROR_WANT_READ:
  706. case SSL_ERROR_WANT_WRITE:
  707. return 0;
  708. case SSL_ERROR_SSL:
  709. case SSL_ERROR_ZERO_RETURN:
  710. return -1;
  711. default:
  712. return -1;
  713. }
  714. }
  715. return 0;
  716. }
  717. int ngtcp2_crypto_set_remote_transport_params(ngtcp2_conn *conn, void *tls) {
  718. SSL *ssl = tls;
  719. const uint8_t *tp;
  720. size_t tplen;
  721. int rv;
  722. SSL_get_peer_quic_transport_params(ssl, &tp, &tplen);
  723. rv = ngtcp2_conn_decode_and_set_remote_transport_params(conn, tp, tplen);
  724. if (rv != 0) {
  725. ngtcp2_conn_set_tls_error(conn, rv);
  726. return -1;
  727. }
  728. return 0;
  729. }
  730. int ngtcp2_crypto_set_local_transport_params(void *tls, const uint8_t *buf,
  731. size_t len) {
  732. if (SSL_set_quic_transport_params(tls, buf, len) != 1) {
  733. return -1;
  734. }
  735. return 0;
  736. }
  737. ngtcp2_encryption_level ngtcp2_crypto_quictls_from_ossl_encryption_level(
  738. OSSL_ENCRYPTION_LEVEL ossl_level) {
  739. switch (ossl_level) {
  740. case ssl_encryption_initial:
  741. return NGTCP2_ENCRYPTION_LEVEL_INITIAL;
  742. case ssl_encryption_early_data:
  743. return NGTCP2_ENCRYPTION_LEVEL_0RTT;
  744. case ssl_encryption_handshake:
  745. return NGTCP2_ENCRYPTION_LEVEL_HANDSHAKE;
  746. case ssl_encryption_application:
  747. return NGTCP2_ENCRYPTION_LEVEL_1RTT;
  748. default:
  749. assert(0);
  750. abort(); /* if NDEBUG is set */
  751. }
  752. }
  753. OSSL_ENCRYPTION_LEVEL
  754. ngtcp2_crypto_quictls_from_ngtcp2_encryption_level(
  755. ngtcp2_encryption_level encryption_level) {
  756. switch (encryption_level) {
  757. case NGTCP2_ENCRYPTION_LEVEL_INITIAL:
  758. return ssl_encryption_initial;
  759. case NGTCP2_ENCRYPTION_LEVEL_HANDSHAKE:
  760. return ssl_encryption_handshake;
  761. case NGTCP2_ENCRYPTION_LEVEL_1RTT:
  762. return ssl_encryption_application;
  763. case NGTCP2_ENCRYPTION_LEVEL_0RTT:
  764. return ssl_encryption_early_data;
  765. default:
  766. assert(0);
  767. abort(); /* if NDEBUG is set */
  768. }
  769. }
  770. int ngtcp2_crypto_get_path_challenge_data_cb(ngtcp2_conn *conn, uint8_t *data,
  771. void *user_data) {
  772. (void)conn;
  773. (void)user_data;
  774. if (RAND_bytes(data, NGTCP2_PATH_CHALLENGE_DATALEN) != 1) {
  775. return NGTCP2_ERR_CALLBACK_FAILURE;
  776. }
  777. return 0;
  778. }
  779. int ngtcp2_crypto_random(uint8_t *data, size_t datalen) {
  780. if (RAND_bytes(data, (int)datalen) != 1) {
  781. return -1;
  782. }
  783. return 0;
  784. }
  785. static int set_encryption_secrets(SSL *ssl, OSSL_ENCRYPTION_LEVEL ossl_level,
  786. const uint8_t *rx_secret,
  787. const uint8_t *tx_secret, size_t secretlen) {
  788. ngtcp2_crypto_conn_ref *conn_ref = SSL_get_app_data(ssl);
  789. ngtcp2_conn *conn = conn_ref->get_conn(conn_ref);
  790. ngtcp2_encryption_level level =
  791. ngtcp2_crypto_quictls_from_ossl_encryption_level(ossl_level);
  792. if (rx_secret &&
  793. ngtcp2_crypto_derive_and_install_rx_key(conn, NULL, NULL, NULL, level,
  794. rx_secret, secretlen) != 0) {
  795. return 0;
  796. }
  797. if (tx_secret &&
  798. ngtcp2_crypto_derive_and_install_tx_key(conn, NULL, NULL, NULL, level,
  799. tx_secret, secretlen) != 0) {
  800. return 0;
  801. }
  802. return 1;
  803. }
  804. static int add_handshake_data(SSL *ssl, OSSL_ENCRYPTION_LEVEL ossl_level,
  805. const uint8_t *data, size_t datalen) {
  806. ngtcp2_crypto_conn_ref *conn_ref = SSL_get_app_data(ssl);
  807. ngtcp2_conn *conn = conn_ref->get_conn(conn_ref);
  808. ngtcp2_encryption_level level =
  809. ngtcp2_crypto_quictls_from_ossl_encryption_level(ossl_level);
  810. int rv;
  811. rv = ngtcp2_conn_submit_crypto_data(conn, level, data, datalen);
  812. if (rv != 0) {
  813. ngtcp2_conn_set_tls_error(conn, rv);
  814. return 0;
  815. }
  816. return 1;
  817. }
  818. static int flush_flight(SSL *ssl) {
  819. (void)ssl;
  820. return 1;
  821. }
  822. static int send_alert(SSL *ssl, OSSL_ENCRYPTION_LEVEL level, uint8_t alert) {
  823. ngtcp2_crypto_conn_ref *conn_ref = SSL_get_app_data(ssl);
  824. ngtcp2_conn *conn = conn_ref->get_conn(conn_ref);
  825. (void)level;
  826. ngtcp2_conn_set_tls_alert(conn, alert);
  827. return 1;
  828. }
  829. static SSL_QUIC_METHOD quic_method = {
  830. set_encryption_secrets,
  831. add_handshake_data,
  832. flush_flight,
  833. send_alert,
  834. #ifdef LIBRESSL_VERSION_NUMBER
  835. NULL,
  836. NULL,
  837. #endif /* defined(LIBRESSL_VERSION_NUMBER) */
  838. };
  839. static void crypto_quictls_configure_context(SSL_CTX *ssl_ctx) {
  840. SSL_CTX_set_min_proto_version(ssl_ctx, TLS1_3_VERSION);
  841. SSL_CTX_set_max_proto_version(ssl_ctx, TLS1_3_VERSION);
  842. SSL_CTX_set_quic_method(ssl_ctx, &quic_method);
  843. }
  844. int ngtcp2_crypto_quictls_configure_server_context(SSL_CTX *ssl_ctx) {
  845. crypto_quictls_configure_context(ssl_ctx);
  846. return 0;
  847. }
  848. int ngtcp2_crypto_quictls_configure_client_context(SSL_CTX *ssl_ctx) {
  849. crypto_quictls_configure_context(ssl_ctx);
  850. return 0;
  851. }