| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -13,6 +13,7 @@ | |||
| 13 | 13 | 'ngtcp2/lib/ngtcp2_cc.c', | |
| 14 | 14 | 'ngtcp2/lib/ngtcp2_cid.c', | |
| 15 | 15 | 'ngtcp2/lib/ngtcp2_conn.c', | |
| 16 | + 'ngtcp2/lib/ngtcp2_conn_info.c', | ||
| 16 | 17 | 'ngtcp2/lib/ngtcp2_conv.c', | |
| 17 | 18 | 'ngtcp2/lib/ngtcp2_crypto.c', | |
| 18 | 19 | 'ngtcp2/lib/ngtcp2_dcidtr.c', | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -627,7 +627,10 @@ NGTCP2_EXTERN int ngtcp2_crypto_generate_stateless_reset_token( | |||
| 627 | 627 | * @macro | |
| 628 | 628 | * | |
| 629 | 629 | * :macro:`NGTCP2_CRYPTO_MAX_REGULAR_TOKENLEN` is the maximum length | |
| 630 | - * of a token generated by `ngtcp2_crypto_generate_regular_token`. | ||
| 630 | + * of a token generated by `ngtcp2_crypto_generate_regular_token`. | ||
| 631 | + * `ngtcp2_crypto_generate_regular_token2` generates a token of length | ||
| 632 | + * at most :macro:`NGTCP2_CRYPTO_MAX_REGULAR_TOKENLEN` bytes + the | ||
| 633 | + * length of the provided opaque data. | ||
| 631 | 634 | */ | |
| 632 | 635 | #define NGTCP2_CRYPTO_MAX_REGULAR_TOKENLEN \ | |
| 633 | 636 | (/* magic = */ 1 + sizeof(ngtcp2_tstamp) + /* aead tag = */ 16 + \ | |
@@ -787,6 +790,77 @@ NGTCP2_EXTERN int ngtcp2_crypto_verify_regular_token( | |||
| 787 | 790 | size_t secretlen, const ngtcp2_sockaddr *remote_addr, | |
| 788 | 791 | ngtcp2_socklen remote_addrlen, ngtcp2_duration timeout, ngtcp2_tstamp ts); | |
| 789 | 792 | ||
| 793 | + /** | ||
| 794 | + * @function | ||
| 795 | + * | ||
| 796 | + * `ngtcp2_crypto_generate_regular_token2` generates a token in the | ||
| 797 | + * buffer pointed by |token| that is sent with NEW_TOKEN frame. The | ||
| 798 | + * buffer pointed by |token| must have at least | ||
| 799 | + * :macro:`NGTCP2_CRYPTO_MAX_REGULAR_TOKENLEN` + |datalen| bytes long. | ||
| 800 | + * The successfully generated token starts with | ||
| 801 | + * :macro:`NGTCP2_CRYPTO_TOKEN_MAGIC_REGULAR`. |secret| of length | ||
| 802 | + * |secretlen| is a keying material to generate keys to encrypt the | ||
| 803 | + * token. |remote_addr| of length |remote_addrlen| is an address of | ||
| 804 | + * client. |ts| is the timestamp when the token is generated. |data| | ||
| 805 | + * of length |datalen| is an opaque data embedded in the token. | ||
| 806 | + * |datalen| must be less than or equal to 256. | ||
| 807 | + * | ||
| 808 | + * Calling this function with |datalen| = 0 is equivalent to calling | ||
| 809 | + * `ngtcp2_crypto_generate_regular_token`. | ||
| 810 | + * | ||
| 811 | + * To get the opaque data after successful verification, use | ||
| 812 | + * `ngtcp2_crypto_verify_regular_token2`. | ||
| 813 | + * `ngtcp2_crypto_verify_regular_token` can verify the token with | ||
| 814 | + * |datalen| > 0, but it discards the opaque data. | ||
| 815 | + * | ||
| 816 | + * This function returns the length of generated token if it succeeds, | ||
| 817 | + * or -1. | ||
| 818 | + */ | ||
| 819 | + NGTCP2_EXTERN ngtcp2_ssize ngtcp2_crypto_generate_regular_token2( | ||
| 820 | + uint8_t *token, const uint8_t *secret, size_t secretlen, | ||
| 821 | + const ngtcp2_sockaddr *remote_addr, ngtcp2_socklen remote_addrlen, | ||
| 822 | + const void *data, size_t datalen, ngtcp2_tstamp ts); | ||
| 823 | + | ||
| 824 | + /** | ||
| 825 | + * @function | ||
| 826 | + * | ||
| 827 | + * `ngtcp2_crypto_verify_regular_token2` verifies a regular token | ||
| 828 | + * stored in the buffer pointed by |token| of length |tokenlen|. | ||
| 829 | + * |secret| of length |secretlen| is a keying material to generate | ||
| 830 | + * keys to decrypt the token. |remote_addr| of length | ||
| 831 | + * |remote_addrlen| is an address of client. |timeout| is the period | ||
| 832 | + * during which the token is valid. |ts| is the current timestamp. | ||
| 833 | + * |data| is the pointer to the buffer of length at least | ||
| 834 | + * |max_datalen| bytes. If the token is verified successfully, the | ||
| 835 | + * opaque data embedded in the token is copied to the buffer pointed | ||
| 836 | + * by |data|. | ||
| 837 | + * | ||
| 838 | + * If |tokenlen| is less than | ||
| 839 | + * :macro:`NGTCP2_CRYPTO_MAX_REGULAR_TOKENLEN`, this function returns | ||
| 840 | + * :macro:`NGTCP2_CRYPTO_ERR_UNREADABLE_TOKEN`. | ||
| 841 | + * | ||
| 842 | + * If the length of opaque data is larger than |max_datalen|, the | ||
| 843 | + * verification still succeeds, but nothing is written to the buffer | ||
| 844 | + * pointed by |data|, and this function returns 0. In other words, | ||
| 845 | + * the opaque data is discarded. | ||
| 846 | + * | ||
| 847 | + * This function returns the number of the opaque data written to the | ||
| 848 | + * buffer pointed by |data| if it succeeds, or one of the following | ||
| 849 | + * negative error codes: | ||
| 850 | + * | ||
| 851 | + * :macro:`NGTCP2_CRYPTO_ERR_UNREADABLE_TOKEN` | ||
| 852 | + * A token is badly formatted; or verifying the integrity | ||
| 853 | + * protection failed. | ||
| 854 | + * :macro:`NGTCP2_CRYPTO_ERR_VERIFY_TOKEN` | ||
| 855 | + * A token validity has expired. | ||
| 856 | + * :macro:`NGTCP2_CRYPTO_ERR_INTERNAL` | ||
| 857 | + * Internal error occurred. | ||
| 858 | + */ | ||
| 859 | + NGTCP2_EXTERN ngtcp2_ssize ngtcp2_crypto_verify_regular_token2( | ||
| 860 | + void *data, size_t max_datalen, const uint8_t *token, size_t tokenlen, | ||
| 861 | + const uint8_t *secret, size_t secretlen, const ngtcp2_sockaddr *remote_addr, | ||
| 862 | + ngtcp2_socklen remote_addrlen, ngtcp2_duration timeout, ngtcp2_tstamp ts); | ||
| 863 | + | ||
| 790 | 864 | /** | |
| 791 | 865 | * @function | |
| 792 | 866 | * | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -41,70 +41,40 @@ | |||
| 41 | 41 | #include "ngtcp2_macro.h" | |
| 42 | 42 | #include "shared.h" | |
| 43 | 43 | ||
| 44 | - static int crypto_initialized; | ||
| 44 | + #if defined(OPENSSL_NO_CHACHA) || defined(OPENSSL_NO_POLY1305) | ||
| 45 | + # define NGTCP2_NO_CHACHA_POLY1305 | ||
| 46 | + #endif /* defined(OPENSSL_NO_CHACHA) || \ | ||
| 47 | + defined(OPENSSL_NO_POLY1305) */ | ||
| 48 | + | ||
| 45 | 49 | static EVP_CIPHER *crypto_aes_128_gcm; | |
| 46 | 50 | static EVP_CIPHER *crypto_aes_256_gcm; | |
| 47 | - static EVP_CIPHER *crypto_chacha20_poly1305; | ||
| 48 | 51 | static EVP_CIPHER *crypto_aes_128_ccm; | |
| 49 | 52 | static EVP_CIPHER *crypto_aes_128_ctr; | |
| 50 | 53 | static EVP_CIPHER *crypto_aes_256_ctr; | |
| 54 | + #ifndef NGTCP2_NO_CHACHA_POLY1305 | ||
| 55 | + static EVP_CIPHER *crypto_chacha20_poly1305; | ||
| 51 | 56 | static EVP_CIPHER *crypto_chacha20; | |
| 57 | + #endif /* !defined(NGTCP2_NO_CHACHA_POLY1305) */ | ||
| 52 | 58 | static EVP_MD *crypto_sha256; | |
| 53 | 59 | static EVP_MD *crypto_sha384; | |
| 54 | 60 | static EVP_KDF *crypto_hkdf; | |
| 55 | 61 | ||
| 56 | 62 | int ngtcp2_crypto_ossl_init(void) { | |
| 63 | + /* We do not care whether the pre-fetch succeeds or not. If it | ||
| 64 | + fails, it returns NULL, which is still the default value, and our | ||
| 65 | + code should still work with it. */ | ||
| 57 | 66 | crypto_aes_128_gcm = EVP_CIPHER_fetch(NULL, "AES-128-GCM", NULL); | |
| 58 | - if (crypto_aes_128_gcm == NULL) { | ||
| 59 | - return -1; | ||
| 60 | - } | ||
| 61 | - | ||
| 62 | 67 | crypto_aes_256_gcm = EVP_CIPHER_fetch(NULL, "AES-256-GCM", NULL); | |
| 63 | - if (crypto_aes_256_gcm == NULL) { | ||
| 64 | - return -1; | ||
| 65 | - } | ||
| 66 | - | ||
| 67 | - crypto_chacha20_poly1305 = EVP_CIPHER_fetch(NULL, "ChaCha20-Poly1305", NULL); | ||
| 68 | - if (crypto_chacha20_poly1305 == NULL) { | ||
| 69 | - return -1; | ||
| 70 | - } | ||
| 71 | - | ||
| 72 | 68 | crypto_aes_128_ccm = EVP_CIPHER_fetch(NULL, "AES-128-CCM", NULL); | |
| 73 | - if (crypto_aes_128_ccm == NULL) { | ||
| 74 | - return -1; | ||
| 75 | - } | ||
| 76 | - | ||
| 77 | 69 | crypto_aes_128_ctr = EVP_CIPHER_fetch(NULL, "AES-128-CTR", NULL); | |
| 78 | - if (crypto_aes_128_ctr == NULL) { | ||
| 79 | - return -1; | ||
| 80 | - } | ||
| 81 | - | ||
| 82 | 70 | crypto_aes_256_ctr = EVP_CIPHER_fetch(NULL, "AES-256-CTR", NULL); | |
| 83 | - if (crypto_aes_256_ctr == NULL) { | ||
| 84 | - return -1; | ||
| 85 | - } | ||
| 86 | - | ||
| 71 | + #ifndef NGTCP2_NO_CHACHA_POLY1305 | ||
| 72 | + crypto_chacha20_poly1305 = EVP_CIPHER_fetch(NULL, "ChaCha20-Poly1305", NULL); | ||
| 87 | 73 | crypto_chacha20 = EVP_CIPHER_fetch(NULL, "ChaCha20", NULL); | |
| 88 | - if (crypto_chacha20 == NULL) { | ||
| 89 | - return -1; | ||
| 90 | - } | ||
| 91 | - | ||
| 74 | + #endif /* !defined(NGTCP2_NO_CHACHA_POLY1305) */ | ||
| 92 | 75 | crypto_sha256 = EVP_MD_fetch(NULL, "sha256", NULL); | |
| 93 | - if (crypto_sha256 == NULL) { | ||
| 94 | - return -1; | ||
| 95 | - } | ||
| 96 | - | ||
| 97 | 76 | crypto_sha384 = EVP_MD_fetch(NULL, "sha384", NULL); | |
| 98 | - if (crypto_sha384 == NULL) { | ||
| 99 | - return -1; | ||
| 100 | - } | ||
| 101 | - | ||
| 102 | 77 | crypto_hkdf = EVP_KDF_fetch(NULL, "hkdf", NULL); | |
| 103 | - if (crypto_hkdf == NULL) { | ||
| 104 | - return -1; | ||
| 105 | - } | ||
| 106 | - | ||
| 107 | - crypto_initialized = 1; | ||
| 108 | 78 | ||
| 109 | 79 | return 0; | |
| 110 | 80 | } | |
@@ -125,13 +95,15 @@ static const EVP_CIPHER *crypto_aead_aes_256_gcm(void) { | |||
| 125 | 95 | return EVP_aes_256_gcm(); | |
| 126 | 96 | } | |
| 127 | 97 | ||
| 98 | + #ifndef NGTCP2_NO_CHACHA_POLY1305 | ||
| 128 | 99 | static const EVP_CIPHER *crypto_aead_chacha20_poly1305(void) { | |
| 129 | 100 | if (crypto_chacha20_poly1305) { | |
| 130 | 101 | return crypto_chacha20_poly1305; | |
| 131 | 102 | } | |
| 132 | 103 | ||
| 133 | 104 | return EVP_chacha20_poly1305(); | |
| 134 | 105 | } | |
| 106 | + #endif /* !defined(NGTCP2_NO_CHACHA_POLY1305) */ | ||
| 135 | 107 | ||
| 136 | 108 | static const EVP_CIPHER *crypto_aead_aes_128_ccm(void) { | |
| 137 | 109 | if (crypto_aes_128_ccm) { | |
@@ -157,13 +129,15 @@ static const EVP_CIPHER *crypto_cipher_aes_256_ctr(void) { | |||
| 157 | 129 | return EVP_aes_256_ctr(); | |
| 158 | 130 | } | |
| 159 | 131 | ||
| 132 | + #ifndef NGTCP2_NO_CHACHA_POLY1305 | ||
| 160 | 133 | static const EVP_CIPHER *crypto_cipher_chacha20(void) { | |
| 161 | 134 | if (crypto_chacha20) { | |
| 162 | 135 | return crypto_chacha20; | |
| 163 | 136 | } | |
| 164 | 137 | ||
| 165 | 138 | return EVP_chacha20(); | |
| 166 | 139 | } | |
| 140 | + #endif /* !defined(NGTCP2_NO_CHACHA_POLY1305) */ | ||
| 167 | 141 | ||
| 168 | 142 | static const EVP_MD *crypto_md_sha256(void) { | |
| 169 | 143 | if (crypto_sha256) { | |
@@ -189,13 +163,21 @@ static EVP_KDF *crypto_kdf_hkdf(void) { | |||
| 189 | 163 | return EVP_KDF_fetch(NULL, "hkdf", NULL); | |
| 190 | 164 | } | |
| 191 | 165 | ||
| 166 | + static void crypto_kdf_hkdf_free(EVP_KDF *kdf) { | ||
| 167 | + if (kdf && crypto_hkdf != kdf) { | ||
| 168 | + EVP_KDF_free(kdf); | ||
| 169 | + } | ||
| 170 | + } | ||
| 171 | + | ||
| 192 | 172 | static size_t crypto_aead_max_overhead(const EVP_CIPHER *aead) { | |
| 193 | 173 | switch (EVP_CIPHER_nid(aead)) { | |
| 194 | 174 | case NID_aes_128_gcm: | |
| 195 | 175 | case NID_aes_256_gcm: | |
| 196 | 176 | return EVP_GCM_TLS_TAG_LEN; | |
| 177 | + #ifndef NGTCP2_NO_CHACHA_POLY1305 | ||
| 197 | 178 | case NID_chacha20_poly1305: | |
| 198 | 179 | return EVP_CHACHAPOLY_TLS_TAG_LEN; | |
| 180 | + #endif /* !defined(NGTCP2_NO_CHACHA_POLY1305) */ | ||
| 199 | 181 | case NID_aes_128_ccm: | |
| 200 | 182 | return EVP_CCM_TLS_TAG_LEN; | |
| 201 | 183 | default: | |
@@ -239,8 +221,10 @@ static const EVP_CIPHER *crypto_cipher_id_get_aead(uint32_t cipher_id) { | |||
| 239 | 221 | return crypto_aead_aes_128_gcm(); | |
| 240 | 222 | case TLS1_3_CK_AES_256_GCM_SHA384: | |
| 241 | 223 | return crypto_aead_aes_256_gcm(); | |
| 224 | + #ifndef NGTCP2_NO_CHACHA_POLY1305 | ||
| 242 | 225 | case TLS1_3_CK_CHACHA20_POLY1305_SHA256: | |
| 243 | 226 | return crypto_aead_chacha20_poly1305(); | |
| 227 | + #endif /* !defined(NGTCP2_NO_CHACHA_POLY1305) */ | ||
| 244 | 228 | case TLS1_3_CK_AES_128_CCM_SHA256: | |
| 245 | 229 | return crypto_aead_aes_128_ccm(); | |
| 246 | 230 | default: | |
@@ -253,8 +237,10 @@ static uint64_t crypto_cipher_id_get_aead_max_encryption(uint32_t cipher_id) { | |||
| 253 | 237 | case TLS1_3_CK_AES_128_GCM_SHA256: | |
| 254 | 238 | case TLS1_3_CK_AES_256_GCM_SHA384: | |
| 255 | 239 | return NGTCP2_CRYPTO_MAX_ENCRYPTION_AES_GCM; | |
| 240 | + #ifndef NGTCP2_NO_CHACHA_POLY1305 | ||
| 256 | 241 | case TLS1_3_CK_CHACHA20_POLY1305_SHA256: | |
| 257 | 242 | return NGTCP2_CRYPTO_MAX_ENCRYPTION_CHACHA20_POLY1305; | |
| 243 | + #endif /* !defined(NGTCP2_NO_CHACHA_POLY1305) */ | ||
| 258 | 244 | case TLS1_3_CK_AES_128_CCM_SHA256: | |
| 259 | 245 | return NGTCP2_CRYPTO_MAX_ENCRYPTION_AES_CCM; | |
| 260 | 246 | default: | |
@@ -268,8 +254,10 @@ crypto_cipher_id_get_aead_max_decryption_failure(uint32_t cipher_id) { | |||
| 268 | 254 | case TLS1_3_CK_AES_128_GCM_SHA256: | |
| 269 | 255 | case TLS1_3_CK_AES_256_GCM_SHA384: | |
| 270 | 256 | return NGTCP2_CRYPTO_MAX_DECRYPTION_FAILURE_AES_GCM; | |
| 257 | + #ifndef NGTCP2_NO_CHACHA_POLY1305 | ||
| 271 | 258 | case TLS1_3_CK_CHACHA20_POLY1305_SHA256: | |
| 272 | 259 | return NGTCP2_CRYPTO_MAX_DECRYPTION_FAILURE_CHACHA20_POLY1305; | |
| 260 | + #endif /* !defined(NGTCP2_NO_CHACHA_POLY1305) */ | ||
| 273 | 261 | case TLS1_3_CK_AES_128_CCM_SHA256: | |
| 274 | 262 | return NGTCP2_CRYPTO_MAX_DECRYPTION_FAILURE_AES_CCM; | |
| 275 | 263 | default: | |
@@ -284,8 +272,10 @@ static const EVP_CIPHER *crypto_cipher_id_get_hp(uint32_t cipher_id) { | |||
| 284 | 272 | return crypto_cipher_aes_128_ctr(); | |
| 285 | 273 | case TLS1_3_CK_AES_256_GCM_SHA384: | |
| 286 | 274 | return crypto_cipher_aes_256_ctr(); | |
| 275 | + #ifndef NGTCP2_NO_CHACHA_POLY1305 | ||
| 287 | 276 | case TLS1_3_CK_CHACHA20_POLY1305_SHA256: | |
| 288 | 277 | return crypto_cipher_chacha20(); | |
| 278 | + #endif /* !defined(NGTCP2_NO_CHACHA_POLY1305) */ | ||
| 289 | 279 | default: | |
| 290 | 280 | return NULL; | |
| 291 | 281 | } | |
@@ -294,7 +284,9 @@ static const EVP_CIPHER *crypto_cipher_id_get_hp(uint32_t cipher_id) { | |||
| 294 | 284 | static const EVP_MD *crypto_cipher_id_get_md(uint32_t cipher_id) { | |
| 295 | 285 | switch (cipher_id) { | |
| 296 | 286 | case TLS1_3_CK_AES_128_GCM_SHA256: | |
| 287 | + #ifndef NGTCP2_NO_CHACHA_POLY1305 | ||
| 297 | 288 | case TLS1_3_CK_CHACHA20_POLY1305_SHA256: | |
| 289 | + #endif /* !defined(NGTCP2_NO_CHACHA_POLY1305) */ | ||
| 298 | 290 | case TLS1_3_CK_AES_128_CCM_SHA256: | |
| 299 | 291 | return crypto_md_sha256(); | |
| 300 | 292 | case TLS1_3_CK_AES_256_GCM_SHA384: | |
@@ -308,7 +300,9 @@ static int supported_cipher_id(uint32_t cipher_id) { | |||
| 308 | 300 | switch (cipher_id) { | |
| 309 | 301 | case TLS1_3_CK_AES_128_GCM_SHA256: | |
| 310 | 302 | case TLS1_3_CK_AES_256_GCM_SHA384: | |
| 303 | + #ifndef NGTCP2_NO_CHACHA_POLY1305 | ||
| 311 | 304 | case TLS1_3_CK_CHACHA20_POLY1305_SHA256: | |
| 305 | + #endif /* !defined(NGTCP2_NO_CHACHA_POLY1305) */ | ||
| 312 | 306 | case TLS1_3_CK_AES_128_CCM_SHA256: | |
| 313 | 307 | return 1; | |
| 314 | 308 | default: | |
@@ -697,9 +691,7 @@ int ngtcp2_crypto_hkdf_extract(uint8_t *dest, const ngtcp2_crypto_md *md, | |||
| 697 | 691 | }; | |
| 698 | 692 | int rv = 0; | |
| 699 | 693 | ||
| 700 | - if (!crypto_initialized) { | ||
| 701 | - EVP_KDF_free(kdf); | ||
| 702 | - } | ||
| 694 | + crypto_kdf_hkdf_free(kdf); | ||
| 703 | 695 | ||
| 704 | 696 | if (EVP_KDF_derive(kctx, dest, (size_t)EVP_MD_size(prf), params) <= 0) { | |
| 705 | 697 | rv = -1; | |
@@ -730,9 +722,7 @@ int ngtcp2_crypto_hkdf_expand(uint8_t *dest, size_t destlen, | |||
| 730 | 722 | }; | |
| 731 | 723 | int rv = 0; | |
| 732 | 724 | ||
| 733 | - if (!crypto_initialized) { | ||
| 734 | - EVP_KDF_free(kdf); | ||
| 735 | - } | ||
| 725 | + crypto_kdf_hkdf_free(kdf); | ||
| 736 | 726 | ||
| 737 | 727 | if (EVP_KDF_derive(kctx, dest, destlen, params) <= 0) { | |
| 738 | 728 | rv = -1; | |
@@ -763,9 +753,7 @@ int ngtcp2_crypto_hkdf(uint8_t *dest, size_t destlen, | |||
| 763 | 753 | }; | |
| 764 | 754 | int rv = 0; | |
| 765 | 755 | ||
| 766 | - if (!crypto_initialized) { | ||
| 767 | - EVP_KDF_free(kdf); | ||
| 768 | - } | ||
| 756 | + crypto_kdf_hkdf_free(kdf); | ||
| 769 | 757 | ||
| 770 | 758 | if (EVP_KDF_derive(kctx, dest, destlen, params) <= 0) { | |
| 771 | 759 | rv = -1; | |
| Back | FazBrowse Home | New Git URL |
0 commit comments