-
Notifications
You must be signed in to change notification settings - Fork 5.6k
TLS client & server: Support Encrypted Client Hello (ECH) #3813
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from 25 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
caa1a55
ECH: client support TLS Encrypted Client Hello
Fangliding 88b942e
Update mutex usage
Fangliding 79c39ad
Add new doh server format
Fangliding 5bae374
Use sync.Map
Fangliding 7bbf7a7
Update goech to v0.0.1
Fangliding ebf4d19
Add server support
Fangliding e3bd972
Refine xray tls ech output format
Fangliding b21630c
bugfix
Fangliding 2417c28
Add classic UDP DNS support for ECH Config
Fangliding ff4b10e
Do some rename
Fangliding f099e26
Missing rename
Fangliding afdcad5
Update utls and little change
Fangliding 7d516ee
Change dependence to reality
Fangliding e2fe7fe
Allow concurrent DNS queries
Fangliding 442f6b7
Use old value and update in other goroutine
Fangliding d6f1ec4
Reduce meaningless goroutines
Fangliding efd1538
Fix nil panic
Fangliding a15f47d
Add test
Fangliding 90e2efc
sync
Fangliding cb72a79
Rename
Fangliding a3e347b
Restore ECHConfig from keysets
Fangliding af41ee4
rename
Fangliding bc8ba76
Bug fix and refine
Fangliding db54522
Remove global lock
Fangliding 61fcfb8
typo
Fangliding 9bd719a
Fmt
Fangliding 53cf8ca
Reuse Client
Fangliding File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,69 +1,93 @@ | ||
| package tls | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "encoding/base64" | ||
| "encoding/pem" | ||
| "os" | ||
| "strings" | ||
|
|
||
| "github.com/OmarTariq612/goech" | ||
| "github.com/cloudflare/circl/hpke" | ||
| "github.com/xtls/reality/hpke" | ||
| "github.com/xtls/xray-core/common" | ||
| "github.com/xtls/xray-core/main/commands/base" | ||
| "github.com/xtls/xray-core/transport/internet/tls" | ||
| "golang.org/x/crypto/cryptobyte" | ||
| ) | ||
|
|
||
| var cmdECH = &base.Command{ | ||
| UsageLine: `{{.Exec}} tls ech [--serverName (string)] [--json]`, | ||
| UsageLine: `{{.Exec}} tls ech [--serverName (string)] [--pem] [-i "ECHServerKeys (base64.StdEncoding)"]`, | ||
| Short: `Generate TLS-ECH certificates`, | ||
| Long: ` | ||
| Generate TLS-ECH certificates. | ||
|
|
||
| Set serverName to your custom string: {{.Exec}} tls ech --serverName (string) | ||
| Generate into json format: {{.Exec}} tls ech --json | ||
| Generate into pem format: {{.Exec}} tls ech --pem | ||
| Restore ECHConfigs from ECHServerKeys: {{.Exec}} tls ech -i "ECHServerKeys (base64.StdEncoding)" | ||
| `, // Enable PQ signature schemes: {{.Exec}} tls ech --pq-signature-schemes-enabled | ||
| } | ||
|
|
||
| func init() { | ||
| cmdECH.Run = executeECH | ||
| } | ||
|
|
||
| var input_pqSignatureSchemesEnabled = cmdECH.Flag.Bool("pqSignatureSchemesEnabled", false, "") | ||
| var input_echServerKeys = cmdECH.Flag.String("i", "", "ECHServerKeys (base64.StdEncoding)") | ||
|
|
||
| // var input_pqSignatureSchemesEnabled = cmdECH.Flag.Bool("pqSignatureSchemesEnabled", false, "") | ||
| var input_serverName = cmdECH.Flag.String("serverName", "cloudflare-ech.com", "") | ||
| var input_json = cmdECH.Flag.Bool("json", false, "True == turn on json output") | ||
| var input_pem = cmdECH.Flag.Bool("pem", false, "True == turn on pem output") | ||
|
|
||
| func executeECH(cmd *base.Command, args []string) { | ||
| var kem hpke.KEM | ||
| var kem uint16 | ||
|
|
||
| if *input_pqSignatureSchemesEnabled { | ||
| kem = hpke.KEM_X25519_KYBER768_DRAFT00 | ||
| } else { | ||
| kem = hpke.KEM_X25519_HKDF_SHA256 | ||
| } | ||
| // if *input_pqSignatureSchemesEnabled { | ||
| // kem = 0x30 // hpke.KEM_X25519_KYBER768_DRAFT00 | ||
| // } else { | ||
| kem = hpke.DHKEM_X25519_HKDF_SHA256 | ||
| // } | ||
|
|
||
| echKeySet, err := goech.GenerateECHKeySet(0, *input_serverName, kem) | ||
| echConfig, priv, err := tls.GenerateECHKeySet(0, *input_serverName, kem) | ||
| common.Must(err) | ||
|
|
||
| configBuffer, _ := echKeySet.ECHConfig.MarshalBinary() | ||
| keyBuffer, _ := echKeySet.MarshalBinary() | ||
|
|
||
| configPEM := string(pem.EncodeToMemory(&pem.Block{Type: "ECH CONFIGS", Bytes: configBuffer})) | ||
| keyPEM := string(pem.EncodeToMemory(&pem.Block{Type: "ECH KEYS", Bytes: keyBuffer})) | ||
| if *input_json { | ||
| jECHConfigs := map[string]interface{}{ | ||
| "configs": strings.Split(strings.TrimSpace(string(configPEM)), "\n"), | ||
| var configBuffer, keyBuffer []byte | ||
| if *input_echServerKeys == "" { | ||
| configBytes, _ := tls.MarshalBinary(echConfig) | ||
| var b cryptobyte.Builder | ||
| b.AddUint16LengthPrefixed(func(child *cryptobyte.Builder) { | ||
| child.AddBytes(configBytes) | ||
| }) | ||
| configBuffer, _ = b.Bytes() | ||
| var b2 cryptobyte.Builder | ||
| b2.AddUint16(uint16(len(priv))) | ||
| b2.AddBytes(priv) | ||
| b2.AddUint16(uint16(len(configBytes))) | ||
| b2.AddBytes(configBytes) | ||
| keyBuffer, _ = b2.Bytes() | ||
| } else { | ||
| keySetsByte, err := base64.StdEncoding.DecodeString(*input_echServerKeys) | ||
| if err != nil { | ||
| os.Stdout.WriteString("Failed to decode ECHServerKeys: " + err.Error() + "\n") | ||
| return | ||
| } | ||
| jECHKey := map[string]interface{}{ | ||
| "key": strings.Split(strings.TrimSpace(string(keyPEM)), "\n"), | ||
| keyBuffer = keySetsByte | ||
| KeySets, err := tls.ConvertToGoECHKeys(keySetsByte) | ||
| if err != nil { | ||
| os.Stdout.WriteString("Failed to decode ECHServerKeys: " + err.Error() + "\n") | ||
| return | ||
| } | ||
|
|
||
| for _, i := range []map[string]interface{}{jECHConfigs, jECHKey} { | ||
| content, err := json.MarshalIndent(i, "", " ") | ||
| common.Must(err) | ||
| os.Stdout.Write(content) | ||
| os.Stdout.WriteString("\n") | ||
| var b cryptobyte.Builder | ||
| for _, keySet := range KeySets { | ||
| b.AddUint16LengthPrefixed(func(child *cryptobyte.Builder) { | ||
| child.AddBytes(keySet.Config) | ||
| }) | ||
| } | ||
| } else { | ||
| configBuffer, _ = b.Bytes() | ||
| } | ||
|
|
||
| if *input_pem { | ||
| configPEM := string(pem.EncodeToMemory(&pem.Block{Type: "ECH CONFIGS", Bytes: configBuffer})) | ||
| keyPEM := string(pem.EncodeToMemory(&pem.Block{Type: "ECH KEYS", Bytes: keyBuffer})) | ||
| os.Stdout.WriteString(configPEM) | ||
| os.Stdout.WriteString(keyPEM) | ||
| } else { | ||
| os.Stdout.WriteString("ECH config list: \n" + base64.StdEncoding.EncodeToString(configBuffer) + "\n") | ||
| os.Stdout.WriteString("ECH server keys: \n" + base64.StdEncoding.EncodeToString(keyBuffer) + "\n") | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.