Skip to content

Commit c9fa9a5

Browse files
authored
MITM freedom RAW TLS: Allow "fromMitm" to be written at any position in verifyPeerCertInNames, Add checking for alpn "fromMitm"
#4348 (comment)
1 parent db5f18b commit c9fa9a5

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

infra/conf/transport_internet.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,13 @@ func (c *TLSConfig) Build() (proto.Message, error) {
433433
if c.ALPN != nil && len(*c.ALPN) > 0 {
434434
config.NextProtocol = []string(*c.ALPN)
435435
}
436+
if len(config.NextProtocol) > 1 {
437+
for _, p := range config.NextProtocol {
438+
if tcp.IsFromMitm(p) {
439+
return nil, errors.New(`only one element is allowed in "alpn" when using "fromMitm" in it`)
440+
}
441+
}
442+
}
436443
if c.CurvePreferences != nil && len(*c.CurvePreferences) > 0 {
437444
config.CurvePreferences = []string(*c.CurvePreferences)
438445
}
@@ -443,7 +450,7 @@ func (c *TLSConfig) Build() (proto.Message, error) {
443450
config.CipherSuites = c.CipherSuites
444451
config.Fingerprint = strings.ToLower(c.Fingerprint)
445452
if config.Fingerprint != "unsafe" && tls.GetFingerprint(config.Fingerprint) == nil {
446-
return nil, errors.New(`unknown fingerprint: `, config.Fingerprint)
453+
return nil, errors.New(`unknown "fingerprint": `, config.Fingerprint)
447454
}
448455
config.RejectUnknownSni = c.RejectUnknownSNI
449456

@@ -472,7 +479,7 @@ func (c *TLSConfig) Build() (proto.Message, error) {
472479
config.MasterKeyLog = c.MasterKeyLog
473480

474481
if c.ServerNameToVerify != "" {
475-
return nil, errors.PrintRemovedFeatureError("serverNameToVerify", "verifyPeerCertInNames")
482+
return nil, errors.PrintRemovedFeatureError(`"serverNameToVerify"`, `"verifyPeerCertInNames"`)
476483
}
477484
config.VerifyPeerCertInNames = c.VerifyPeerCertInNames
478485

transport/internet/tcp/dialer.go

+19-11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package tcp
22

33
import (
44
"context"
5+
"slices"
56
"strings"
67

78
"github.com/xtls/xray-core/common"
@@ -33,17 +34,24 @@ func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.Me
3334
if IsFromMitm(tlsConfig.ServerName) {
3435
tlsConfig.ServerName = mitmServerName
3536
}
36-
r, ok := tlsConfig.Rand.(*tls.RandCarrier)
37-
isFromMitmVerify := ok && len(r.VerifyPeerCertInNames) > 0 && IsFromMitm(r.VerifyPeerCertInNames[0])
38-
if isFromMitmVerify {
39-
r.VerifyPeerCertInNames = r.VerifyPeerCertInNames[1:]
40-
after := mitmServerName
41-
for {
42-
if len(after) > 0 {
43-
r.VerifyPeerCertInNames = append(r.VerifyPeerCertInNames, after)
44-
}
45-
_, after, _ = strings.Cut(after, ".")
46-
if !strings.Contains(after, ".") {
37+
isFromMitmVerify := false
38+
if r, ok := tlsConfig.Rand.(*tls.RandCarrier); ok && len(r.VerifyPeerCertInNames) > 0 {
39+
for i, name := range r.VerifyPeerCertInNames {
40+
if IsFromMitm(name) {
41+
isFromMitmVerify = true
42+
r.VerifyPeerCertInNames[0], r.VerifyPeerCertInNames[i] = r.VerifyPeerCertInNames[i], r.VerifyPeerCertInNames[0]
43+
r.VerifyPeerCertInNames = r.VerifyPeerCertInNames[1:]
44+
after := mitmServerName
45+
for {
46+
if len(after) > 0 {
47+
r.VerifyPeerCertInNames = append(r.VerifyPeerCertInNames, after)
48+
}
49+
_, after, _ = strings.Cut(after, ".")
50+
if !strings.Contains(after, ".") {
51+
break
52+
}
53+
}
54+
slices.Reverse(r.VerifyPeerCertInNames)
4755
break
4856
}
4957
}

0 commit comments

Comments
 (0)