-
Notifications
You must be signed in to change notification settings - Fork 295
Expand file tree
/
Copy pathhandshake.go
More file actions
260 lines (234 loc) · 6.96 KB
/
handshake.go
File metadata and controls
260 lines (234 loc) · 6.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package certificate
import (
"crypto/tls"
"crypto/x509"
"encoding/pem"
"fmt"
"net"
"reflect"
"github.com/smallstep/cli-utils/errs"
"github.com/smallstep/cli/flags"
"github.com/smallstep/cli/internal/cryptoutil"
"github.com/urfave/cli"
"go.step.sm/crypto/pemutil"
"go.step.sm/crypto/x509util"
)
func handshakeCommand() cli.Command {
return cli.Command{
Name: "handshake",
Action: cli.ActionFunc(handshakeAction),
Usage: `print handshake details`,
UsageText: `**step certificate handshake** <url>`,
Description: `**step certificate handshake** displays detailed handshake information for a TLS connection.`,
Flags: []cli.Flag{
flags.ServerName,
cli.StringFlag{
Name: "tls",
Usage: `Defines the TLS <version> in the handshake. By default it will use TLS 1.3 or TLS 1.2.
: The supported versions are **1.3**, **1.2**, **1.1**, and **1.0**.`,
},
cli.StringFlag{
Name: "cert",
Usage: `The path to the <file> containing the client certificate to use.`,
},
cli.StringFlag{
Name: "key",
Usage: `The path to the <file> or KMS <uri> containing the certificate key to use.`,
},
cli.StringFlag{
Name: "roots",
Usage: `Root certificate(s) that will be used to verify the
authenticity of the remote server.
: <roots> is a case-sensitive string and may be one of:
**file**
: Relative or full path to a file. All certificates in the file will be used for path validation.
**list of files**
: Comma-separated list of relative or full file paths. Every PEM encoded certificate from each file will be used for path validation.
**directory**
: Relative or full path to a directory. Every PEM encoded certificate from each file in the directory will be used for path validation.`,
},
cli.StringFlag{
Name: "password-file",
Usage: "The path to the <file> containing the password to decrypt the private key.",
},
cli.BoolFlag{
Name: "chain",
Usage: "Print only the chain of verified certificates.",
},
cli.BoolFlag{
Name: "peer",
Usage: `Print only the peer certificates sent by the server.`,
},
cli.BoolFlag{
Name: "insecure",
Usage: `Use an insecure client to retrieve a remote peer certificate. Useful for
debugging invalid certificates remotely.`,
},
},
}
}
func handshakeAction(ctx *cli.Context) error {
if err := errs.NumberOfArguments(ctx, 1); err != nil {
return err
}
var (
addr = ctx.Args().First()
tlsVersion = ctx.String("tls")
roots = ctx.String("roots")
serverName = ctx.String("servername")
certFile = ctx.String("cert")
keyFile = ctx.String("key")
passwordFile = ctx.String("password-file")
printChains = ctx.Bool("chain")
printPeer = ctx.Bool("peer")
insecure = ctx.Bool("insecure")
rootCAs *x509.CertPool
err error
)
switch {
case certFile != "" && keyFile == "":
return errs.RequiredWithFlag(ctx, "cert", "key")
case keyFile != "" && certFile == "":
return errs.RequiredWithFlag(ctx, "key", "cert")
}
// Parse address
if u, ok, err := trimURL(addr); err != nil {
return err
} else if ok {
addr = u
}
if _, _, err := net.SplitHostPort(addr); err != nil {
addr = net.JoinHostPort(addr, "443")
}
// Load certificate and if
var certificates []tls.Certificate
if certFile != "" && keyFile != "" {
opts := []pemutil.Options{}
if passwordFile != "" {
opts = append(opts, pemutil.WithPasswordFile(passwordFile))
}
crt, err := cryptoutil.LoadTLSCertificate(certFile, keyFile, opts...)
if err != nil {
return err
}
certificates = []tls.Certificate{crt}
}
// Get the list of roots used to validate the certificate.
if roots != "" {
rootCAs, err = x509util.ReadCertPool(roots)
if err != nil {
return fmt.Errorf("error loading root certificate pool from %q: %w", roots, err)
}
} else {
rootCAs, err = x509.SystemCertPool()
if err != nil {
return fmt.Errorf("error loading the system cert pool: %w", err)
}
}
// Get the tls version to use. Defaults to TLS 1.2+
minVersion, maxVersion, err := getTLSVersions(tlsVersion)
if err != nil {
return err
}
tlsConfig := &tls.Config{
MinVersion: minVersion,
MaxVersion: maxVersion,
RootCAs: rootCAs,
InsecureSkipVerify: insecure,
ServerName: serverName,
Certificates: certificates,
}
cs, err := tlsDialWithFallback(addr, tlsConfig)
if err != nil {
return err
}
// Print only the list of verified chains
if printChains {
for _, chain := range cs.VerifiedChains {
for _, crt := range chain {
fmt.Print(string(pem.EncodeToMemory(&pem.Block{
Type: "CERTIFICATE",
Bytes: crt.Raw,
})))
}
}
return nil
}
// Print only the peer certificates
if printPeer {
for _, crt := range cs.PeerCertificates {
fmt.Print(string(pem.EncodeToMemory(&pem.Block{
Type: "CERTIFICATE", Bytes: crt.Raw,
})))
}
return nil
}
// Check if the certificates is verified
var intermediates *x509.CertPool
if len(cs.PeerCertificates) > 1 {
intermediates = x509.NewCertPool()
for _, crt := range cs.PeerCertificates[1:] {
intermediates.AddCert(crt)
}
}
_, verifyErr := cs.PeerCertificates[0].Verify(x509.VerifyOptions{
Roots: rootCAs,
Intermediates: intermediates,
DNSName: serverName,
KeyUsages: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
})
connStateValue := reflect.ValueOf(cs)
curveIDField := connStateValue.FieldByName("testingOnlyCurveID")
fmt.Printf("Server Name: %s\n", cs.ServerName)
fmt.Printf("Version: %s\n", tls.VersionName(cs.Version))
fmt.Printf("Cipher Suite: %s\n", tls.CipherSuiteName(cs.CipherSuite))
fmt.Printf("KEM: %s\n", curveIDName(curveIDField.Uint()))
fmt.Printf("Insecure: %v\n", tlsConfig.InsecureSkipVerify)
fmt.Printf("Verified: %v\n", verifyErr == nil)
return nil
}
func curveIDName(curveID uint64) string {
switch tls.CurveID(curveID) {
case tls.CurveP256:
return "P-256"
case tls.CurveP384:
return "P-384"
case tls.CurveP521:
return "P-521"
case tls.X25519:
return "X25519"
case tls.X25519MLKEM768:
return "X25519MLKEM768"
default:
return "Unknown"
}
}
func getTLSVersions(s string) (uint16, uint16, error) {
switch s {
case "":
return tls.VersionTLS12, 0, nil
case "1.3":
return tls.VersionTLS13, tls.VersionTLS13, nil
case "1.2":
return tls.VersionTLS12, tls.VersionTLS12, nil
case "1.1":
return tls.VersionTLS11, tls.VersionTLS11, nil
case "1.0":
return tls.VersionTLS10, tls.VersionTLS10, nil
default:
return 0, 0, fmt.Errorf("unsupported TLS version %q", s)
}
}
func tlsDialWithFallback(addr string, tlsConfig *tls.Config) (tls.ConnectionState, error) {
conn, err := tls.Dial("tcp", addr, tlsConfig)
if err != nil {
if tlsConfig.InsecureSkipVerify {
return tls.ConnectionState{}, fmt.Errorf("error connecting to %q: %w", addr, err)
}
tlsConfig.InsecureSkipVerify = true
return tlsDialWithFallback(addr, tlsConfig)
}
defer conn.Close()
conn.Handshake()
return conn.ConnectionState(), nil
}