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 /* 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 "shared.h"
43 
44 typedef enum ngtcp2_crypto_boringssl_cipher_type {
45   NGTCP2_CRYPTO_BORINGSSL_CIPHER_TYPE_AES_128,
46   NGTCP2_CRYPTO_BORINGSSL_CIPHER_TYPE_AES_256,
47   NGTCP2_CRYPTO_BORINGSSL_CIPHER_TYPE_CHACHA20,
48 } ngtcp2_crypto_boringssl_cipher_type;
49 
50 typedef struct ngtcp2_crypto_boringssl_cipher {
51   ngtcp2_crypto_boringssl_cipher_type type;
52 } ngtcp2_crypto_boringssl_cipher;
53 
54 static ngtcp2_crypto_boringssl_cipher crypto_cipher_aes_128 = {
55     NGTCP2_CRYPTO_BORINGSSL_CIPHER_TYPE_AES_128,
56 };
57 
58 static ngtcp2_crypto_boringssl_cipher crypto_cipher_aes_256 = {
59     NGTCP2_CRYPTO_BORINGSSL_CIPHER_TYPE_AES_256,
60 };
61 
62 static ngtcp2_crypto_boringssl_cipher crypto_cipher_chacha20 = {
63     NGTCP2_CRYPTO_BORINGSSL_CIPHER_TYPE_CHACHA20,
64 };
65 
ngtcp2_crypto_aead_aes_128_gcm(ngtcp2_crypto_aead * aead)66 ngtcp2_crypto_aead *ngtcp2_crypto_aead_aes_128_gcm(ngtcp2_crypto_aead *aead) {
67   return ngtcp2_crypto_aead_init(aead, (void *)EVP_aead_aes_128_gcm());
68 }
69 
ngtcp2_crypto_md_sha256(ngtcp2_crypto_md * md)70 ngtcp2_crypto_md *ngtcp2_crypto_md_sha256(ngtcp2_crypto_md *md) {
71   md->native_handle = (void *)EVP_sha256();
72   return md;
73 }
74 
ngtcp2_crypto_ctx_initial(ngtcp2_crypto_ctx * ctx)75 ngtcp2_crypto_ctx *ngtcp2_crypto_ctx_initial(ngtcp2_crypto_ctx *ctx) {
76   ngtcp2_crypto_aead_init(&ctx->aead, (void *)EVP_aead_aes_128_gcm());
77   ctx->md.native_handle = (void *)EVP_sha256();
78   ctx->hp.native_handle = (void *)&crypto_cipher_aes_128;
79   ctx->max_encryption = 0;
80   ctx->max_decryption_failure = 0;
81   return ctx;
82 }
83 
ngtcp2_crypto_aead_init(ngtcp2_crypto_aead * aead,void * aead_native_handle)84 ngtcp2_crypto_aead *ngtcp2_crypto_aead_init(ngtcp2_crypto_aead *aead,
85                                             void *aead_native_handle) {
86   aead->native_handle = aead_native_handle;
87   aead->max_overhead = EVP_AEAD_max_overhead(aead->native_handle);
88   return aead;
89 }
90 
ngtcp2_crypto_aead_retry(ngtcp2_crypto_aead * aead)91 ngtcp2_crypto_aead *ngtcp2_crypto_aead_retry(ngtcp2_crypto_aead *aead) {
92   return ngtcp2_crypto_aead_init(aead, (void *)EVP_aead_aes_128_gcm());
93 }
94 
crypto_ssl_get_aead(SSL * ssl)95 static const EVP_AEAD *crypto_ssl_get_aead(SSL *ssl) {
96   switch (SSL_CIPHER_get_id(SSL_get_current_cipher(ssl))) {
97   case TLS1_CK_AES_128_GCM_SHA256:
98     return EVP_aead_aes_128_gcm();
99   case TLS1_CK_AES_256_GCM_SHA384:
100     return EVP_aead_aes_256_gcm();
101   case TLS1_CK_CHACHA20_POLY1305_SHA256:
102     return EVP_aead_chacha20_poly1305();
103   default:
104     return NULL;
105   }
106 }
107 
crypto_ssl_get_aead_max_encryption(SSL * ssl)108 static uint64_t crypto_ssl_get_aead_max_encryption(SSL *ssl) {
109   switch (SSL_CIPHER_get_id(SSL_get_current_cipher(ssl))) {
110   case TLS1_CK_AES_128_GCM_SHA256:
111   case TLS1_CK_AES_256_GCM_SHA384:
112     return NGTCP2_CRYPTO_MAX_ENCRYPTION_AES_GCM;
113   case TLS1_CK_CHACHA20_POLY1305_SHA256:
114     return NGTCP2_CRYPTO_MAX_ENCRYPTION_CHACHA20_POLY1305;
115   default:
116     return 0;
117   }
118 }
119 
crypto_ssl_get_aead_max_decryption_failure(SSL * ssl)120 static uint64_t crypto_ssl_get_aead_max_decryption_failure(SSL *ssl) {
121   switch (SSL_CIPHER_get_id(SSL_get_current_cipher(ssl))) {
122   case TLS1_CK_AES_128_GCM_SHA256:
123   case TLS1_CK_AES_256_GCM_SHA384:
124     return NGTCP2_CRYPTO_MAX_DECRYPTION_FAILURE_AES_GCM;
125   case TLS1_CK_CHACHA20_POLY1305_SHA256:
126     return NGTCP2_CRYPTO_MAX_DECRYPTION_FAILURE_CHACHA20_POLY1305;
127   default:
128     return 0;
129   }
130 }
131 
crypto_ssl_get_hp(SSL * ssl)132 static const ngtcp2_crypto_boringssl_cipher *crypto_ssl_get_hp(SSL *ssl) {
133   switch (SSL_CIPHER_get_id(SSL_get_current_cipher(ssl))) {
134   case TLS1_CK_AES_128_GCM_SHA256:
135     return &crypto_cipher_aes_128;
136   case TLS1_CK_AES_256_GCM_SHA384:
137     return &crypto_cipher_aes_256;
138   case TLS1_CK_CHACHA20_POLY1305_SHA256:
139     return &crypto_cipher_chacha20;
140   default:
141     return NULL;
142   }
143 }
144 
crypto_ssl_get_md(SSL * ssl)145 static const EVP_MD *crypto_ssl_get_md(SSL *ssl) {
146   switch (SSL_CIPHER_get_id(SSL_get_current_cipher(ssl))) {
147   case TLS1_CK_AES_128_GCM_SHA256:
148   case TLS1_CK_CHACHA20_POLY1305_SHA256:
149     return EVP_sha256();
150   case TLS1_CK_AES_256_GCM_SHA384:
151     return EVP_sha384();
152   default:
153     return NULL;
154   }
155 }
156 
ngtcp2_crypto_ctx_tls(ngtcp2_crypto_ctx * ctx,void * tls_native_handle)157 ngtcp2_crypto_ctx *ngtcp2_crypto_ctx_tls(ngtcp2_crypto_ctx *ctx,
158                                          void *tls_native_handle) {
159   SSL *ssl = tls_native_handle;
160   ngtcp2_crypto_aead_init(&ctx->aead, (void *)crypto_ssl_get_aead(ssl));
161   ctx->md.native_handle = (void *)crypto_ssl_get_md(ssl);
162   ctx->hp.native_handle = (void *)crypto_ssl_get_hp(ssl);
163   ctx->max_encryption = crypto_ssl_get_aead_max_encryption(ssl);
164   ctx->max_decryption_failure = crypto_ssl_get_aead_max_decryption_failure(ssl);
165   return ctx;
166 }
167 
ngtcp2_crypto_ctx_tls_early(ngtcp2_crypto_ctx * ctx,void * tls_native_handle)168 ngtcp2_crypto_ctx *ngtcp2_crypto_ctx_tls_early(ngtcp2_crypto_ctx *ctx,
169                                                void *tls_native_handle) {
170   return ngtcp2_crypto_ctx_tls(ctx, tls_native_handle);
171 }
172 
crypto_md_hashlen(const EVP_MD * md)173 static size_t crypto_md_hashlen(const EVP_MD *md) {
174   return (size_t)EVP_MD_size(md);
175 }
176 
ngtcp2_crypto_md_hashlen(const ngtcp2_crypto_md * md)177 size_t ngtcp2_crypto_md_hashlen(const ngtcp2_crypto_md *md) {
178   return crypto_md_hashlen(md->native_handle);
179 }
180 
crypto_aead_keylen(const EVP_AEAD * aead)181 static size_t crypto_aead_keylen(const EVP_AEAD *aead) {
182   return (size_t)EVP_AEAD_key_length(aead);
183 }
184 
ngtcp2_crypto_aead_keylen(const ngtcp2_crypto_aead * aead)185 size_t ngtcp2_crypto_aead_keylen(const ngtcp2_crypto_aead *aead) {
186   return crypto_aead_keylen(aead->native_handle);
187 }
188 
crypto_aead_noncelen(const EVP_AEAD * aead)189 static size_t crypto_aead_noncelen(const EVP_AEAD *aead) {
190   return (size_t)EVP_AEAD_nonce_length(aead);
191 }
192 
ngtcp2_crypto_aead_noncelen(const ngtcp2_crypto_aead * aead)193 size_t ngtcp2_crypto_aead_noncelen(const ngtcp2_crypto_aead *aead) {
194   return crypto_aead_noncelen(aead->native_handle);
195 }
196 
ngtcp2_crypto_aead_ctx_encrypt_init(ngtcp2_crypto_aead_ctx * aead_ctx,const ngtcp2_crypto_aead * aead,const uint8_t * key,size_t noncelen)197 int ngtcp2_crypto_aead_ctx_encrypt_init(ngtcp2_crypto_aead_ctx *aead_ctx,
198                                         const ngtcp2_crypto_aead *aead,
199                                         const uint8_t *key, size_t noncelen) {
200   const EVP_AEAD *cipher = aead->native_handle;
201   size_t keylen = crypto_aead_keylen(cipher);
202   EVP_AEAD_CTX *actx;
203 
204   (void)noncelen;
205 
206   actx = EVP_AEAD_CTX_new(cipher, key, keylen, EVP_AEAD_DEFAULT_TAG_LENGTH);
207   if (actx == NULL) {
208     return -1;
209   }
210 
211   aead_ctx->native_handle = actx;
212 
213   return 0;
214 }
215 
ngtcp2_crypto_aead_ctx_decrypt_init(ngtcp2_crypto_aead_ctx * aead_ctx,const ngtcp2_crypto_aead * aead,const uint8_t * key,size_t noncelen)216 int ngtcp2_crypto_aead_ctx_decrypt_init(ngtcp2_crypto_aead_ctx *aead_ctx,
217                                         const ngtcp2_crypto_aead *aead,
218                                         const uint8_t *key, size_t noncelen) {
219   return ngtcp2_crypto_aead_ctx_encrypt_init(aead_ctx, aead, key, noncelen);
220 }
221 
ngtcp2_crypto_aead_ctx_free(ngtcp2_crypto_aead_ctx * aead_ctx)222 void ngtcp2_crypto_aead_ctx_free(ngtcp2_crypto_aead_ctx *aead_ctx) {
223   if (aead_ctx->native_handle) {
224     EVP_AEAD_CTX_free(aead_ctx->native_handle);
225   }
226 }
227 
228 typedef struct ngtcp2_crypto_boringssl_cipher_ctx {
229   ngtcp2_crypto_boringssl_cipher_type type;
230   union {
231     /* aes_key is an encryption key when type is either
232        NGTCP2_CRYPTO_BORINGSSL_CIPHER_TYPE_AES_128 or
233        NGTCP2_CRYPTO_BORINGSSL_CIPHER_TYPE_AES_256. */
234     AES_KEY aes_key;
235     /* key contains an encryption key when type ==
236        NGTCP2_CRYPTO_BORINGSSL_CIPHER_TYPE_CHACHA20. */
237     uint8_t key[32];
238   };
239 } ngtcp2_crypto_boringssl_cipher_ctx;
240 
ngtcp2_crypto_cipher_ctx_encrypt_init(ngtcp2_crypto_cipher_ctx * cipher_ctx,const ngtcp2_crypto_cipher * cipher,const uint8_t * key)241 int ngtcp2_crypto_cipher_ctx_encrypt_init(ngtcp2_crypto_cipher_ctx *cipher_ctx,
242                                           const ngtcp2_crypto_cipher *cipher,
243                                           const uint8_t *key) {
244   ngtcp2_crypto_boringssl_cipher *hp_cipher = cipher->native_handle;
245   ngtcp2_crypto_boringssl_cipher_ctx *ctx;
246   int rv;
247   (void)rv;
248 
249   ctx = malloc(sizeof(*ctx));
250   if (ctx == NULL) {
251     return -1;
252   }
253 
254   ctx->type = hp_cipher->type;
255   cipher_ctx->native_handle = ctx;
256 
257   switch (hp_cipher->type) {
258   case NGTCP2_CRYPTO_BORINGSSL_CIPHER_TYPE_AES_128:
259     rv = AES_set_encrypt_key(key, 128, &ctx->aes_key);
260     assert(0 == rv);
261     return 0;
262   case NGTCP2_CRYPTO_BORINGSSL_CIPHER_TYPE_AES_256:
263     rv = AES_set_encrypt_key(key, 256, &ctx->aes_key);
264     assert(0 == rv);
265     return 0;
266   case NGTCP2_CRYPTO_BORINGSSL_CIPHER_TYPE_CHACHA20:
267     memcpy(ctx->key, key, sizeof(ctx->key));
268     return 0;
269   default:
270     assert(0);
271     abort();
272   };
273 }
274 
ngtcp2_crypto_cipher_ctx_free(ngtcp2_crypto_cipher_ctx * cipher_ctx)275 void ngtcp2_crypto_cipher_ctx_free(ngtcp2_crypto_cipher_ctx *cipher_ctx) {
276   if (!cipher_ctx->native_handle) {
277     return;
278   }
279 
280   free(cipher_ctx->native_handle);
281 }
282 
ngtcp2_crypto_hkdf_extract(uint8_t * dest,const ngtcp2_crypto_md * md,const uint8_t * secret,size_t secretlen,const uint8_t * salt,size_t saltlen)283 int ngtcp2_crypto_hkdf_extract(uint8_t *dest, const ngtcp2_crypto_md *md,
284                                const uint8_t *secret, size_t secretlen,
285                                const uint8_t *salt, size_t saltlen) {
286   const EVP_MD *prf = md->native_handle;
287   size_t destlen = (size_t)EVP_MD_size(prf);
288 
289   if (HKDF_extract(dest, &destlen, prf, secret, secretlen, salt, saltlen) !=
290       1) {
291     return -1;
292   }
293 
294   return 0;
295 }
296 
ngtcp2_crypto_hkdf_expand(uint8_t * dest,size_t destlen,const ngtcp2_crypto_md * md,const uint8_t * secret,size_t secretlen,const uint8_t * info,size_t infolen)297 int ngtcp2_crypto_hkdf_expand(uint8_t *dest, size_t destlen,
298                               const ngtcp2_crypto_md *md, const uint8_t *secret,
299                               size_t secretlen, const uint8_t *info,
300                               size_t infolen) {
301   const EVP_MD *prf = md->native_handle;
302 
303   if (HKDF_expand(dest, destlen, prf, secret, secretlen, info, infolen) != 1) {
304     return -1;
305   }
306 
307   return 0;
308 }
309 
ngtcp2_crypto_hkdf(uint8_t * dest,size_t destlen,const ngtcp2_crypto_md * md,const uint8_t * secret,size_t secretlen,const uint8_t * salt,size_t saltlen,const uint8_t * info,size_t infolen)310 int ngtcp2_crypto_hkdf(uint8_t *dest, size_t destlen,
311                        const ngtcp2_crypto_md *md, const uint8_t *secret,
312                        size_t secretlen, const uint8_t *salt, size_t saltlen,
313                        const uint8_t *info, size_t infolen) {
314   const EVP_MD *prf = md->native_handle;
315 
316   if (HKDF(dest, destlen, prf, secret, secretlen, salt, saltlen, info,
317            infolen) != 1) {
318     return -1;
319   }
320 
321   return 0;
322 }
323 
ngtcp2_crypto_encrypt(uint8_t * dest,const ngtcp2_crypto_aead * aead,const ngtcp2_crypto_aead_ctx * aead_ctx,const uint8_t * plaintext,size_t plaintextlen,const uint8_t * nonce,size_t noncelen,const uint8_t * aad,size_t aadlen)324 int ngtcp2_crypto_encrypt(uint8_t *dest, const ngtcp2_crypto_aead *aead,
325                           const ngtcp2_crypto_aead_ctx *aead_ctx,
326                           const uint8_t *plaintext, size_t plaintextlen,
327                           const uint8_t *nonce, size_t noncelen,
328                           const uint8_t *aad, size_t aadlen) {
329   const EVP_AEAD *cipher = aead->native_handle;
330   EVP_AEAD_CTX *actx = aead_ctx->native_handle;
331   size_t max_outlen = plaintextlen + EVP_AEAD_max_overhead(cipher);
332   size_t outlen;
333 
334   if (EVP_AEAD_CTX_seal(actx, dest, &outlen, max_outlen, nonce, noncelen,
335                         plaintext, plaintextlen, aad, aadlen) != 1) {
336     return -1;
337   }
338 
339   return 0;
340 }
341 
ngtcp2_crypto_decrypt(uint8_t * dest,const ngtcp2_crypto_aead * aead,const ngtcp2_crypto_aead_ctx * aead_ctx,const uint8_t * ciphertext,size_t ciphertextlen,const uint8_t * nonce,size_t noncelen,const uint8_t * aad,size_t aadlen)342 int ngtcp2_crypto_decrypt(uint8_t *dest, const ngtcp2_crypto_aead *aead,
343                           const ngtcp2_crypto_aead_ctx *aead_ctx,
344                           const uint8_t *ciphertext, size_t ciphertextlen,
345                           const uint8_t *nonce, size_t noncelen,
346                           const uint8_t *aad, size_t aadlen) {
347   EVP_AEAD_CTX *actx = aead_ctx->native_handle;
348   size_t max_outlen = ciphertextlen;
349   size_t outlen;
350 
351   (void)aead;
352 
353   if (EVP_AEAD_CTX_open(actx, dest, &outlen, max_outlen, nonce, noncelen,
354                         ciphertext, ciphertextlen, aad, aadlen) != 1) {
355     return -1;
356   }
357 
358   return 0;
359 }
360 
ngtcp2_crypto_hp_mask(uint8_t * dest,const ngtcp2_crypto_cipher * hp,const ngtcp2_crypto_cipher_ctx * hp_ctx,const uint8_t * sample)361 int ngtcp2_crypto_hp_mask(uint8_t *dest, const ngtcp2_crypto_cipher *hp,
362                           const ngtcp2_crypto_cipher_ctx *hp_ctx,
363                           const uint8_t *sample) {
364   static const uint8_t PLAINTEXT[] = "\x00\x00\x00\x00\x00";
365   ngtcp2_crypto_boringssl_cipher_ctx *ctx = hp_ctx->native_handle;
366   uint32_t counter;
367 
368   (void)hp;
369 
370   switch (ctx->type) {
371   case NGTCP2_CRYPTO_BORINGSSL_CIPHER_TYPE_AES_128:
372   case NGTCP2_CRYPTO_BORINGSSL_CIPHER_TYPE_AES_256:
373     AES_ecb_encrypt(sample, dest, &ctx->aes_key, 1);
374     return 0;
375   case NGTCP2_CRYPTO_BORINGSSL_CIPHER_TYPE_CHACHA20:
376 #if defined(WORDS_BIGENDIAN)
377     counter = (uint32_t)sample[0] + (uint32_t)(sample[1] << 8) +
378               (uint32_t)(sample[2] << 16) + (uint32_t)(sample[3] << 24);
379 #else  /* !WORDS_BIGENDIAN */
380     memcpy(&counter, sample, sizeof(counter));
381 #endif /* !WORDS_BIGENDIAN */
382     CRYPTO_chacha_20(dest, PLAINTEXT, sizeof(PLAINTEXT) - 1, ctx->key,
383                      sample + sizeof(counter), counter);
384     return 0;
385   default:
386     assert(0);
387     abort();
388   }
389 }
390 
ngtcp2_crypto_read_write_crypto_data(ngtcp2_conn * conn,ngtcp2_crypto_level crypto_level,const uint8_t * data,size_t datalen)391 int ngtcp2_crypto_read_write_crypto_data(ngtcp2_conn *conn,
392                                          ngtcp2_crypto_level crypto_level,
393                                          const uint8_t *data, size_t datalen) {
394   SSL *ssl = ngtcp2_conn_get_tls_native_handle(conn);
395   int rv;
396   int err;
397 
398   if (SSL_provide_quic_data(
399           ssl, ngtcp2_crypto_boringssl_from_ngtcp2_crypto_level(crypto_level),
400           data, datalen) != 1) {
401     return -1;
402   }
403 
404   if (!ngtcp2_conn_get_handshake_completed(conn)) {
405   retry:
406     rv = SSL_do_handshake(ssl);
407     if (rv <= 0) {
408       err = SSL_get_error(ssl, rv);
409       switch (err) {
410       case SSL_ERROR_WANT_READ:
411       case SSL_ERROR_WANT_WRITE:
412         return 0;
413       case SSL_ERROR_SSL:
414         return -1;
415       case SSL_ERROR_EARLY_DATA_REJECTED:
416         assert(!ngtcp2_conn_is_server(conn));
417 
418         SSL_reset_early_data_reject(ssl);
419 
420         rv = ngtcp2_conn_early_data_rejected(conn);
421         if (rv != 0) {
422           return -1;
423         }
424 
425         goto retry;
426       default:
427         return -1;
428       }
429     }
430 
431     if (SSL_in_early_data(ssl)) {
432       return 0;
433     }
434 
435     ngtcp2_conn_handshake_completed(conn);
436   }
437 
438   rv = SSL_process_quic_post_handshake(ssl);
439   if (rv != 1) {
440     err = SSL_get_error(ssl, rv);
441     switch (err) {
442     case SSL_ERROR_WANT_READ:
443     case SSL_ERROR_WANT_WRITE:
444       return 0;
445     case SSL_ERROR_SSL:
446     case SSL_ERROR_ZERO_RETURN:
447       return -1;
448     default:
449       return -1;
450     }
451   }
452 
453   return 0;
454 }
455 
ngtcp2_crypto_set_remote_transport_params(ngtcp2_conn * conn,void * tls)456 int ngtcp2_crypto_set_remote_transport_params(ngtcp2_conn *conn, void *tls) {
457   SSL *ssl = tls;
458   ngtcp2_transport_params_type exttype =
459       ngtcp2_conn_is_server(conn)
460           ? NGTCP2_TRANSPORT_PARAMS_TYPE_CLIENT_HELLO
461           : NGTCP2_TRANSPORT_PARAMS_TYPE_ENCRYPTED_EXTENSIONS;
462   const uint8_t *tp;
463   size_t tplen;
464   ngtcp2_transport_params params;
465   int rv;
466 
467   SSL_get_peer_quic_transport_params(ssl, &tp, &tplen);
468 
469   rv = ngtcp2_decode_transport_params(&params, exttype, tp, tplen);
470   if (rv != 0) {
471     ngtcp2_conn_set_tls_error(conn, rv);
472     return -1;
473   }
474 
475   rv = ngtcp2_conn_set_remote_transport_params(conn, &params);
476   if (rv != 0) {
477     ngtcp2_conn_set_tls_error(conn, rv);
478     return -1;
479   }
480 
481   return 0;
482 }
483 
ngtcp2_crypto_set_local_transport_params(void * tls,const uint8_t * buf,size_t len)484 int ngtcp2_crypto_set_local_transport_params(void *tls, const uint8_t *buf,
485                                              size_t len) {
486   if (SSL_set_quic_transport_params(tls, buf, len) != 1) {
487     return -1;
488   }
489 
490   return 0;
491 }
492 
ngtcp2_crypto_boringssl_from_ssl_encryption_level(enum ssl_encryption_level_t ssl_level)493 ngtcp2_crypto_level ngtcp2_crypto_boringssl_from_ssl_encryption_level(
494     enum ssl_encryption_level_t ssl_level) {
495   switch (ssl_level) {
496   case ssl_encryption_initial:
497     return NGTCP2_CRYPTO_LEVEL_INITIAL;
498   case ssl_encryption_early_data:
499     return NGTCP2_CRYPTO_LEVEL_EARLY;
500   case ssl_encryption_handshake:
501     return NGTCP2_CRYPTO_LEVEL_HANDSHAKE;
502   case ssl_encryption_application:
503     return NGTCP2_CRYPTO_LEVEL_APPLICATION;
504   default:
505     assert(0);
506     abort();
507   }
508 }
509 
ngtcp2_crypto_boringssl_from_ngtcp2_crypto_level(ngtcp2_crypto_level crypto_level)510 enum ssl_encryption_level_t ngtcp2_crypto_boringssl_from_ngtcp2_crypto_level(
511     ngtcp2_crypto_level crypto_level) {
512   switch (crypto_level) {
513   case NGTCP2_CRYPTO_LEVEL_INITIAL:
514     return ssl_encryption_initial;
515   case NGTCP2_CRYPTO_LEVEL_HANDSHAKE:
516     return ssl_encryption_handshake;
517   case NGTCP2_CRYPTO_LEVEL_APPLICATION:
518     return ssl_encryption_application;
519   case NGTCP2_CRYPTO_LEVEL_EARLY:
520     return ssl_encryption_early_data;
521   default:
522     assert(0);
523     abort();
524   }
525 }
526 
ngtcp2_crypto_get_path_challenge_data_cb(ngtcp2_conn * conn,uint8_t * data,void * user_data)527 int ngtcp2_crypto_get_path_challenge_data_cb(ngtcp2_conn *conn, uint8_t *data,
528                                              void *user_data) {
529   (void)conn;
530   (void)user_data;
531 
532   if (RAND_bytes(data, NGTCP2_PATH_CHALLENGE_DATALEN) != 1) {
533     return NGTCP2_ERR_CALLBACK_FAILURE;
534   }
535 
536   return 0;
537 }
538 
ngtcp2_crypto_random(uint8_t * data,size_t datalen)539 int ngtcp2_crypto_random(uint8_t *data, size_t datalen) {
540   if (RAND_bytes(data, datalen) != 1) {
541     return -1;
542   }
543 
544   return 0;
545 }
546