Debian Codegraph @

third_party/ngtcp2/crypto/boringssl/boringssl.c

raw
1 /*
2 * ngtcp2
3 *
4 * Copyright (c) 2020 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
29 #include <assert.h>
30 #include <string.h>
31
32 #include <ngtcp2/ngtcp2_crypto.h>
33 #include <ngtcp2/ngtcp2_crypto_boringssl.h>
34
35 #include <openssl/ssl.h>
36 #include <openssl/evp.h>
37 #include <openssl/hkdf.h>
38 #include <openssl/aes.h>
39 #include <openssl/chacha.h>
40 #include <openssl/rand.h>
41
42 #include "ngtcp2_macro.h"
43 #include "shared.h"
44
45 typedef enum ngtcp2_crypto_boringssl_cipher_type {
46 NGTCP2_CRYPTO_BORINGSSL_CIPHER_TYPE_AES_128,
47 NGTCP2_CRYPTO_BORINGSSL_CIPHER_TYPE_AES_256,
48 NGTCP2_CRYPTO_BORINGSSL_CIPHER_TYPE_CHACHA20,
49 } ngtcp2_crypto_boringssl_cipher_type;
50
51 typedef struct ngtcp2_crypto_boringssl_cipher {
52 ngtcp2_crypto_boringssl_cipher_type type;
53 } ngtcp2_crypto_boringssl_cipher;
54
55 static ngtcp2_crypto_boringssl_cipher crypto_cipher_aes_128 = {
56 NGTCP2_CRYPTO_BORINGSSL_CIPHER_TYPE_AES_128,
57 };
58
59 static ngtcp2_crypto_boringssl_cipher crypto_cipher_aes_256 = {
60 NGTCP2_CRYPTO_BORINGSSL_CIPHER_TYPE_AES_256,
61 };
62
63 static ngtcp2_crypto_boringssl_cipher crypto_cipher_chacha20 = {
64 NGTCP2_CRYPTO_BORINGSSL_CIPHER_TYPE_CHACHA20,
65 };
66
67 ngtcp2_crypto_aead *ngtcp2_crypto_aead_aes_128_gcm(ngtcp2_crypto_aead *aead) {
68 return ngtcp2_crypto_aead_init(aead, (void *)EVP_aead_aes_128_gcm());
69 }
70
71 ngtcp2_crypto_md *ngtcp2_crypto_md_sha256(ngtcp2_crypto_md *md) {
72 md->native_handle = (void *)EVP_sha256();
73 return md;
74 }
75
76 ngtcp2_crypto_ctx *ngtcp2_crypto_ctx_initial(ngtcp2_crypto_ctx *ctx) {
77 ngtcp2_crypto_aead_init(&ctx->aead, (void *)EVP_aead_aes_128_gcm());
78 ctx->md.native_handle = (void *)EVP_sha256();
79 ctx->hp.native_handle = (void *)&crypto_cipher_aes_128;
80 ctx->max_encryption = 0;
81 ctx->max_decryption_failure = 0;
82 return ctx;
83 }
84
85 ngtcp2_crypto_aead *ngtcp2_crypto_aead_init(ngtcp2_crypto_aead *aead,
86 void *aead_native_handle) {
87 aead->native_handle = aead_native_handle;
88 aead->max_overhead = EVP_AEAD_max_overhead(aead->native_handle);
89 return aead;
90 }
91
92 ngtcp2_crypto_aead *ngtcp2_crypto_aead_retry(ngtcp2_crypto_aead *aead) {
93 return ngtcp2_crypto_aead_init(aead, (void *)EVP_aead_aes_128_gcm());
94 }
95
96 static const EVP_AEAD *crypto_cipher_id_get_aead(uint32_t cipher_id) {
97 switch (cipher_id) {
98 case TLS1_CK_AES_128_GCM_SHA256:
99 return EVP_aead_aes_128_gcm();
100 case TLS1_CK_AES_256_GCM_SHA384:
101 return EVP_aead_aes_256_gcm();
102 case TLS1_CK_CHACHA20_POLY1305_SHA256:
103 return EVP_aead_chacha20_poly1305();
104 default:
105 return NULL;
106 }
107 }
108
109 static uint64_t crypto_cipher_id_get_aead_max_encryption(uint32_t cipher_id) {
110 switch (cipher_id) {
111 case TLS1_CK_AES_128_GCM_SHA256:
112 case TLS1_CK_AES_256_GCM_SHA384:
113 return NGTCP2_CRYPTO_MAX_ENCRYPTION_AES_GCM;
114 case TLS1_CK_CHACHA20_POLY1305_SHA256:
115 return NGTCP2_CRYPTO_MAX_ENCRYPTION_CHACHA20_POLY1305;
116 default:
117 return 0;
118 }
119 }
120
121 static uint64_t
122 crypto_cipher_id_get_aead_max_decryption_failure(uint32_t cipher_id) {
123 switch (cipher_id) {
124 case TLS1_CK_AES_128_GCM_SHA256:
125 case TLS1_CK_AES_256_GCM_SHA384:
126 return NGTCP2_CRYPTO_MAX_DECRYPTION_FAILURE_AES_GCM;
127 case TLS1_CK_CHACHA20_POLY1305_SHA256:
128 return NGTCP2_CRYPTO_MAX_DECRYPTION_FAILURE_CHACHA20_POLY1305;
129 default:
130 return 0;
131 }
132 }
133
134 static const ngtcp2_crypto_boringssl_cipher *
135 crypto_cipher_id_get_hp(uint32_t cipher_id) {
136 switch (cipher_id) {
137 case TLS1_CK_AES_128_GCM_SHA256:
138 return &crypto_cipher_aes_128;
139 case TLS1_CK_AES_256_GCM_SHA384:
140 return &crypto_cipher_aes_256;
141 case TLS1_CK_CHACHA20_POLY1305_SHA256:
142 return &crypto_cipher_chacha20;
143 default:
144 return NULL;
145 }
146 }
147
148 static const EVP_MD *crypto_cipher_id_get_md(uint32_t cipher_id) {
149 switch (cipher_id) {
150 case TLS1_CK_AES_128_GCM_SHA256:
151 case TLS1_CK_CHACHA20_POLY1305_SHA256:
152 return EVP_sha256();
153 case TLS1_CK_AES_256_GCM_SHA384:
154 return EVP_sha384();
155 default:
156 return NULL;
157 }
158 }
159
160 static int supported_cipher_id(uint32_t cipher_id) {
161 switch (cipher_id) {
162 case TLS1_CK_AES_128_GCM_SHA256:
163 case TLS1_CK_AES_256_GCM_SHA384:
164 case TLS1_CK_CHACHA20_POLY1305_SHA256:
165 return 1;
166 default:
167 return 0;
168 }
169 }
170
171 static ngtcp2_crypto_ctx *crypto_ctx_cipher_id(ngtcp2_crypto_ctx *ctx,
172 uint32_t cipher_id) {
173 ngtcp2_crypto_aead_init(&ctx->aead,
174 (void *)crypto_cipher_id_get_aead(cipher_id));
175 ctx->md.native_handle = (void *)crypto_cipher_id_get_md(cipher_id);
176 ctx->hp.native_handle = (void *)crypto_cipher_id_get_hp(cipher_id);
177 ctx->max_encryption = crypto_cipher_id_get_aead_max_encryption(cipher_id);
178 ctx->max_decryption_failure =
179 crypto_cipher_id_get_aead_max_decryption_failure(cipher_id);
180
181 return ctx;
182 }
183
184 ngtcp2_crypto_ctx *ngtcp2_crypto_ctx_tls(ngtcp2_crypto_ctx *ctx,
185 void *tls_native_handle) {
186 SSL *ssl = tls_native_handle;
187 const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl);
188 uint32_t cipher_id;
189
190 if (cipher == NULL) {
191 return NULL;
192 }
193
194 cipher_id = SSL_CIPHER_get_id(cipher);
195
196 if (!supported_cipher_id(cipher_id)) {
197 return NULL;
198 }
199
200 return crypto_ctx_cipher_id(ctx, cipher_id);
201 }
202
203 ngtcp2_crypto_ctx *ngtcp2_crypto_ctx_tls_early(ngtcp2_crypto_ctx *ctx,
204 void *tls_native_handle) {
205 return ngtcp2_crypto_ctx_tls(ctx, tls_native_handle);
206 }
207
208 static size_t crypto_md_hashlen(const EVP_MD *md) {
209 return (size_t)EVP_MD_size(md);
210 }
211
212 size_t ngtcp2_crypto_md_hashlen(const ngtcp2_crypto_md *md) {
213 return crypto_md_hashlen(md->native_handle);
214 }
215
216 static size_t crypto_aead_keylen(const EVP_AEAD *aead) {
217 return (size_t)EVP_AEAD_key_length(aead);
218 }
219
220 size_t ngtcp2_crypto_aead_keylen(const ngtcp2_crypto_aead *aead) {
221 return crypto_aead_keylen(aead->native_handle);
222 }
223
224 static size_t crypto_aead_noncelen(const EVP_AEAD *aead) {
225 return (size_t)EVP_AEAD_nonce_length(aead);
226 }
227
228 size_t ngtcp2_crypto_aead_noncelen(const ngtcp2_crypto_aead *aead) {
229 return crypto_aead_noncelen(aead->native_handle);
230 }
231
232 int ngtcp2_crypto_aead_ctx_encrypt_init(ngtcp2_crypto_aead_ctx *aead_ctx,
233 const ngtcp2_crypto_aead *aead,
234 const uint8_t *key, size_t noncelen) {
235 const EVP_AEAD *cipher = aead->native_handle;
236 size_t keylen = crypto_aead_keylen(cipher);
237 EVP_AEAD_CTX *actx;
238
239 (void)noncelen;
240
241 actx = EVP_AEAD_CTX_new(cipher, key, keylen, EVP_AEAD_DEFAULT_TAG_LENGTH);
242 if (actx == NULL) {
243 return -1;
244 }
245
246 aead_ctx->native_handle = actx;
247
248 return 0;
249 }
250
251 int ngtcp2_crypto_aead_ctx_decrypt_init(ngtcp2_crypto_aead_ctx *aead_ctx,
252 const ngtcp2_crypto_aead *aead,
253 const uint8_t *key, size_t noncelen) {
254 return ngtcp2_crypto_aead_ctx_encrypt_init(aead_ctx, aead, key, noncelen);
255 }
256
257 void ngtcp2_crypto_aead_ctx_free(ngtcp2_crypto_aead_ctx *aead_ctx) {
258 if (aead_ctx->native_handle) {
259 EVP_AEAD_CTX_free(aead_ctx->native_handle);
260 }
261 }
262
263 typedef struct ngtcp2_crypto_boringssl_cipher_ctx {
264 ngtcp2_crypto_boringssl_cipher_type type;
265 union {
266 /* aes_key is an encryption key when type is either
267 NGTCP2_CRYPTO_BORINGSSL_CIPHER_TYPE_AES_128 or
268 NGTCP2_CRYPTO_BORINGSSL_CIPHER_TYPE_AES_256. */
269 AES_KEY aes_key;
270 /* key contains an encryption key when type ==
271 NGTCP2_CRYPTO_BORINGSSL_CIPHER_TYPE_CHACHA20. */
272 uint8_t key[32];
273 };
274 } ngtcp2_crypto_boringssl_cipher_ctx;
275
276 int ngtcp2_crypto_cipher_ctx_encrypt_init(ngtcp2_crypto_cipher_ctx *cipher_ctx,
277 const ngtcp2_crypto_cipher *cipher,
278 const uint8_t *key) {
279 ngtcp2_crypto_boringssl_cipher *hp_cipher = cipher->native_handle;
280 ngtcp2_crypto_boringssl_cipher_ctx *ctx;
281 int rv;
282 (void)rv;
283
284 ctx = malloc(sizeof(*ctx));
285 if (ctx == NULL) {
286 return -1;
287 }
288
289 ctx->type = hp_cipher->type;
290 cipher_ctx->native_handle = ctx;
291
292 switch (hp_cipher->type) {
293 case NGTCP2_CRYPTO_BORINGSSL_CIPHER_TYPE_AES_128:
294 rv = AES_set_encrypt_key(key, 128, &ctx->aes_key);
295 assert(0 == rv);
296 return 0;
297 case NGTCP2_CRYPTO_BORINGSSL_CIPHER_TYPE_AES_256:
298 rv = AES_set_encrypt_key(key, 256, &ctx->aes_key);
299 assert(0 == rv);
300 return 0;
301 case NGTCP2_CRYPTO_BORINGSSL_CIPHER_TYPE_CHACHA20:
302 memcpy(ctx->key, key, sizeof(ctx->key));
303 return 0;
304 default:
305 assert(0);
306 abort();
307 };
308 }
309
310 void ngtcp2_crypto_cipher_ctx_free(ngtcp2_crypto_cipher_ctx *cipher_ctx) {
311 if (!cipher_ctx->native_handle) {
312 return;
313 }
314
315 free(cipher_ctx->native_handle);
316 }
317
318 int ngtcp2_crypto_hkdf_extract(uint8_t *dest, const ngtcp2_crypto_md *md,
319 const uint8_t *secret, size_t secretlen,
320 const uint8_t *salt, size_t saltlen) {
321 const EVP_MD *prf = md->native_handle;
322 size_t destlen = (size_t)EVP_MD_size(prf);
323
324 if (HKDF_extract(dest, &destlen, prf, secret, secretlen, salt, saltlen) !=
325 1) {
326 return -1;
327 }
328
329 return 0;
330 }
331
332 int ngtcp2_crypto_hkdf_expand(uint8_t *dest, size_t destlen,
333 const ngtcp2_crypto_md *md, const uint8_t *secret,
334 size_t secretlen, const uint8_t *info,
335 size_t infolen) {
336 const EVP_MD *prf = md->native_handle;
337
338 if (HKDF_expand(dest, destlen, prf, secret, secretlen, info, infolen) != 1) {
339 return -1;
340 }
341
342 return 0;
343 }
344
345 int ngtcp2_crypto_hkdf(uint8_t *dest, size_t destlen,
346 const ngtcp2_crypto_md *md, const uint8_t *secret,
347 size_t secretlen, const uint8_t *salt, size_t saltlen,
348 const uint8_t *info, size_t infolen) {
349 const EVP_MD *prf = md->native_handle;
350
351 if (HKDF(dest, destlen, prf, secret, secretlen, salt, saltlen, info,
352 infolen) != 1) {
353 return -1;
354 }
355
356 return 0;
357 }
358
359 int ngtcp2_crypto_encrypt(uint8_t *dest, const ngtcp2_crypto_aead *aead,
360 const ngtcp2_crypto_aead_ctx *aead_ctx,
361 const uint8_t *plaintext, size_t plaintextlen,
362 const uint8_t *nonce, size_t noncelen,
363 const uint8_t *aad, size_t aadlen) {
364 const EVP_AEAD *cipher = aead->native_handle;
365 EVP_AEAD_CTX *actx = aead_ctx->native_handle;
366 size_t max_outlen = plaintextlen + EVP_AEAD_max_overhead(cipher);
367 size_t outlen;
368
369 if (EVP_AEAD_CTX_seal(actx, dest, &outlen, max_outlen, nonce, noncelen,
370 plaintext, plaintextlen, aad, aadlen) != 1) {
371 return -1;
372 }
373
374 return 0;
375 }
376
377 int ngtcp2_crypto_decrypt(uint8_t *dest, const ngtcp2_crypto_aead *aead,
378 const ngtcp2_crypto_aead_ctx *aead_ctx,
379 const uint8_t *ciphertext, size_t ciphertextlen,
380 const uint8_t *nonce, size_t noncelen,
381 const uint8_t *aad, size_t aadlen) {
382 const EVP_AEAD *cipher = aead->native_handle;
383 EVP_AEAD_CTX *actx = aead_ctx->native_handle;
384 size_t max_overhead = EVP_AEAD_max_overhead(cipher);
385 size_t max_outlen;
386 size_t outlen;
387
388 if (ciphertextlen < max_overhead) {
389 return -1;
390 }
391
392 max_outlen = ciphertextlen - max_overhead;
393
394 if (EVP_AEAD_CTX_open(actx, dest, &outlen, max_outlen, nonce, noncelen,
395 ciphertext, ciphertextlen, aad, aadlen) != 1) {
396 return -1;
397 }
398
399 return 0;
400 }
401
402 int ngtcp2_crypto_hp_mask(uint8_t *dest, const ngtcp2_crypto_cipher *hp,
403 const ngtcp2_crypto_cipher_ctx *hp_ctx,
404 const uint8_t *sample) {
405 static const uint8_t PLAINTEXT[16] = {0};
406 ngtcp2_crypto_boringssl_cipher_ctx *ctx = hp_ctx->native_handle;
407 uint32_t counter;
408
409 (void)hp;
410
411 switch (ctx->type) {
412 case NGTCP2_CRYPTO_BORINGSSL_CIPHER_TYPE_AES_128:
413 case NGTCP2_CRYPTO_BORINGSSL_CIPHER_TYPE_AES_256:
414 AES_ecb_encrypt(sample, dest, &ctx->aes_key, 1);
415 return 0;
416 case NGTCP2_CRYPTO_BORINGSSL_CIPHER_TYPE_CHACHA20:
417 #ifdef WORDS_BIGENDIAN
418 counter = (uint32_t)sample[0] + (uint32_t)(sample[1] << 8) +
419 (uint32_t)(sample[2] << 16) + (uint32_t)(sample[3] << 24);
420 #else /* !defined(WORDS_BIGENDIAN) */
421 memcpy(&counter, sample, sizeof(counter));
422 #endif /* !defined(WORDS_BIGENDIAN) */
423 CRYPTO_chacha_20(dest, PLAINTEXT, sizeof(PLAINTEXT), ctx->key,
424 sample + sizeof(counter), counter);
425 return 0;
426 default:
427 assert(0);
428 abort();
429 }
430 }
431
432 int ngtcp2_crypto_read_write_crypto_data(
433 ngtcp2_conn *conn, ngtcp2_encryption_level encryption_level,
434 const uint8_t *data, size_t datalen) {
435 SSL *ssl = ngtcp2_conn_get_tls_native_handle(conn);
436 int rv;
437 int err;
438
439 if (datalen &&
440 SSL_provide_quic_data(
441 ssl,
442 ngtcp2_crypto_boringssl_from_ngtcp2_encryption_level(encryption_level),
443 data, datalen) != 1) {
444 return -1;
445 }
446
447 if (!ngtcp2_conn_get_handshake_completed(conn)) {
448 retry:
449 rv = SSL_do_handshake(ssl);
450 if (rv <= 0) {
451 err = SSL_get_error(ssl, rv);
452 switch (err) {
453 case SSL_ERROR_WANT_READ:
454 case SSL_ERROR_WANT_WRITE:
455 return 0;
456 case SSL_ERROR_SSL:
457 return -1;
458 case SSL_ERROR_EARLY_DATA_REJECTED:
459 assert(!ngtcp2_conn_is_server(conn));
460
461 SSL_reset_early_data_reject(ssl);
462
463 rv = ngtcp2_conn_tls_early_data_rejected(conn);
464 if (rv != 0) {
465 return -1;
466 }
467
468 goto retry;
469 case SSL_ERROR_WANT_X509_LOOKUP:
470 case SSL_ERROR_WANT_PRIVATE_KEY_OPERATION:
471 case SSL_ERROR_WANT_CERTIFICATE_VERIFY:
472 /* It might be better to return this error, but ngtcp2 does
473 not need to know whether handshake has been interrupted or
474 not. We expect that necessary plumbing should be done by
475 application when handshake is interrupted (e.g., via
476 SSL_PRIVATE_KEY_METHOD). If it does not work, we will
477 reconsider this. */
478 return 0;
479 default:
480 return -1;
481 }
482 }
483
484 if (SSL_in_early_data(ssl)) {
485 return 0;
486 }
487
488 ngtcp2_conn_tls_handshake_completed(conn);
489 }
490
491 rv = SSL_process_quic_post_handshake(ssl);
492 if (rv != 1) {
493 err = SSL_get_error(ssl, rv);
494 switch (err) {
495 case SSL_ERROR_WANT_READ:
496 case SSL_ERROR_WANT_WRITE:
497 return 0;
498 case SSL_ERROR_SSL:
499 case SSL_ERROR_ZERO_RETURN:
500 return -1;
501 default:
502 return -1;
503 }
504 }
505
506 return 0;
507 }
508
509 int ngtcp2_crypto_set_remote_transport_params(ngtcp2_conn *conn, void *tls) {
510 SSL *ssl = tls;
511 const uint8_t *tp;
512 size_t tplen;
513 int rv;
514
515 SSL_get_peer_quic_transport_params(ssl, &tp, &tplen);
516
517 rv = ngtcp2_conn_decode_and_set_remote_transport_params(conn, tp, tplen);
518 if (rv != 0) {
519 ngtcp2_conn_set_tls_error(conn, rv);
520 return -1;
521 }
522
523 return 0;
524 }
525
526 int ngtcp2_crypto_set_local_transport_params(void *tls, const uint8_t *buf,
527 size_t len) {
528 if (SSL_set_quic_transport_params(tls, buf, len) != 1) {
529 return -1;
530 }
531
532 return 0;
533 }
534
535 ngtcp2_encryption_level ngtcp2_crypto_boringssl_from_ssl_encryption_level(
536 enum ssl_encryption_level_t ssl_level) {
537 switch (ssl_level) {
538 case ssl_encryption_initial:
539 return NGTCP2_ENCRYPTION_LEVEL_INITIAL;
540 case ssl_encryption_early_data:
541 return NGTCP2_ENCRYPTION_LEVEL_0RTT;
542 case ssl_encryption_handshake:
543 return NGTCP2_ENCRYPTION_LEVEL_HANDSHAKE;
544 case ssl_encryption_application:
545 return NGTCP2_ENCRYPTION_LEVEL_1RTT;
546 default:
547 assert(0);
548 abort();
549 }
550 }
551
552 enum ssl_encryption_level_t
553 ngtcp2_crypto_boringssl_from_ngtcp2_encryption_level(
554 ngtcp2_encryption_level encryption_level) {
555 switch (encryption_level) {
556 case NGTCP2_ENCRYPTION_LEVEL_INITIAL:
557 return ssl_encryption_initial;
558 case NGTCP2_ENCRYPTION_LEVEL_HANDSHAKE:
559 return ssl_encryption_handshake;
560 case NGTCP2_ENCRYPTION_LEVEL_1RTT:
561 return ssl_encryption_application;
562 case NGTCP2_ENCRYPTION_LEVEL_0RTT:
563 return ssl_encryption_early_data;
564 default:
565 assert(0);
566 abort();
567 }
568 }
569
570 int ngtcp2_crypto_get_path_challenge_data_cb(ngtcp2_conn *conn, uint8_t *data,
571 void *user_data) {
572 (void)conn;
573 (void)user_data;
574
575 if (RAND_bytes(data, NGTCP2_PATH_CHALLENGE_DATALEN) != 1) {
576 return NGTCP2_ERR_CALLBACK_FAILURE;
577 }
578
579 return 0;
580 }
581
582 int ngtcp2_crypto_get_path_challenge_data2_cb(ngtcp2_conn *conn,
583 ngtcp2_path_challenge_data *data,
584 void *user_data) {
585 (void)conn;
586 (void)user_data;
587
588 if (RAND_bytes(data->data, NGTCP2_PATH_CHALLENGE_DATALEN) != 1) {
589 return NGTCP2_ERR_CALLBACK_FAILURE;
590 }
591
592 return 0;
593 }
594
595 int ngtcp2_crypto_random(uint8_t *data, size_t datalen) {
596 if (RAND_bytes(data, datalen) != 1) {
597 return -1;
598 }
599
600 return 0;
601 }
602
603 static int set_read_secret(SSL *ssl, enum ssl_encryption_level_t bssl_level,
604 const SSL_CIPHER *cipher, const uint8_t *secret,
605 size_t secretlen) {
606 ngtcp2_crypto_conn_ref *conn_ref = SSL_get_app_data(ssl);
607 ngtcp2_conn *conn = conn_ref->get_conn(conn_ref);
608 ngtcp2_encryption_level level =
609 ngtcp2_crypto_boringssl_from_ssl_encryption_level(bssl_level);
610 (void)cipher;
611
612 if (ngtcp2_crypto_derive_and_install_rx_key(conn, NULL, NULL, NULL, level,
613 secret, secretlen) != 0) {
614 return 0;
615 }
616
617 return 1;
618 }
619
620 static int set_write_secret(SSL *ssl, enum ssl_encryption_level_t bssl_level,
621 const SSL_CIPHER *cipher, const uint8_t *secret,
622 size_t secretlen) {
623 ngtcp2_crypto_conn_ref *conn_ref = SSL_get_app_data(ssl);
624 ngtcp2_conn *conn = conn_ref->get_conn(conn_ref);
625 ngtcp2_encryption_level level =
626 ngtcp2_crypto_boringssl_from_ssl_encryption_level(bssl_level);
627 (void)cipher;
628
629 if (ngtcp2_crypto_derive_and_install_tx_key(conn, NULL, NULL, NULL, level,
630 secret, secretlen) != 0) {
631 return 0;
632 }
633
634 return 1;
635 }
636
637 static int add_handshake_data(SSL *ssl, enum ssl_encryption_level_t bssl_level,
638 const uint8_t *data, size_t datalen) {
639 ngtcp2_crypto_conn_ref *conn_ref = SSL_get_app_data(ssl);
640 ngtcp2_conn *conn = conn_ref->get_conn(conn_ref);
641 ngtcp2_encryption_level level =
642 ngtcp2_crypto_boringssl_from_ssl_encryption_level(bssl_level);
643 int rv;
644
645 rv = ngtcp2_conn_submit_crypto_data(conn, level, data, datalen);
646 if (rv != 0) {
647 ngtcp2_conn_set_tls_error(conn, rv);
648 return 0;
649 }
650
651 return 1;
652 }
653
654 static int flush_flight(SSL *ssl) {
655 (void)ssl;
656 return 1;
657 }
658
659 static int send_alert(SSL *ssl, enum ssl_encryption_level_t bssl_level,
660 uint8_t alert) {
661 ngtcp2_crypto_conn_ref *conn_ref = SSL_get_app_data(ssl);
662 ngtcp2_conn *conn = conn_ref->get_conn(conn_ref);
663 (void)bssl_level;
664
665 ngtcp2_conn_set_tls_alert(conn, alert);
666
667 return 1;
668 }
669
670 static SSL_QUIC_METHOD quic_method = {
671 set_read_secret, set_write_secret, add_handshake_data,
672 flush_flight, send_alert,
673 };
674
675 static void crypto_boringssl_configure_context(SSL_CTX *ssl_ctx) {
676 SSL_CTX_set_min_proto_version(ssl_ctx, TLS1_3_VERSION);
677 SSL_CTX_set_max_proto_version(ssl_ctx, TLS1_3_VERSION);
678 SSL_CTX_set_quic_method(ssl_ctx, &quic_method);
679 }
680
681 int ngtcp2_crypto_boringssl_configure_server_context(SSL_CTX *ssl_ctx) {
682 crypto_boringssl_configure_context(ssl_ctx);
683
684 return 0;
685 }
686
687 int ngtcp2_crypto_boringssl_configure_client_context(SSL_CTX *ssl_ctx) {
688 crypto_boringssl_configure_context(ssl_ctx);
689
690 return 0;
691 }