Debian Codegraph @

command/certificate/verify.go

raw symbols SCIP
1 package certificate
2
3 import (
4 "crypto/x509"
5 "encoding/pem"
6 "io/ioutil"
7
8 "github.com/pkg/errors"
9 "github.com/smallstep/cli/crypto/x509util"
10 "github.com/smallstep/cli/errs"
11 "github.com/smallstep/cli/flags"
12 "github.com/urfave/cli"
13 )
14
15 func verifyCommand() cli.Command {
16 return cli.Command{
17 Name: "verify",
18 Action: cli.ActionFunc(verifyAction),
19 Usage: `verify a certificate`,
20 UsageText: `**step certificate verify** <crt_file> [**--host**=<host>]
21 [**--roots**=<root-bundle>] [**--servername**=<servername>]`,
22 Description: `**step certificate verify** executes the certificate path
23 validation algorithm for x.509 certificates defined in RFC 5280. If the
24 certificate is valid this command will return '0'. If validation fails, or if
25 an error occurs, this command will produce a non-zero return value.
26
27 ## POSITIONAL ARGUMENTS
28
29 <crt_file>
30 : The path to a certificate to validate.
31
32 ## EXIT CODES
33
34 This command returns 0 on success and \>0 if any error occurs.
35
36 ## EXAMPLES
37
38 Verify a certificate using your operating system's default root certificate bundle:
39
40 '''
41 $ step certificate verify ./certificate.crt
42 '''
43
44 Verify a remote certificate using your operating system's default root certificate bundle:
45
46 '''
47 $ step certificate verify https://smallstep.com
48 '''
49
50 Verify a certificate using a custom root certificate for path validation:
51
52 '''
53 $ step certificate verify ./certificate.crt --roots ./root-certificate.crt
54 '''
55
56 Verify a certificate using a custom list of root certificates for path validation:
57
58 '''
59 $ step certificate verify ./certificate.crt \
60 --roots "./root-certificate.crt,./root-certificate2.crt,/root-certificate3.crt"
61 '''
62
63 Verify a certificate using a custom directory of root certificates for path validation:
64
65 '''
66 $ step certificate verify ./certificate.crt --roots ./root-certificates/
67 '''
68 `,
69 Flags: []cli.Flag{
70 cli.StringFlag{
71 Name: "host",
72 Usage: `Check whether the certificate is for the specified host.`,
73 },
74 cli.StringFlag{
75 Name: "roots",
76 Usage: `Root certificate(s) that will be used to verify the
77 authenticity of the remote server.
78
79 : <roots> is a case-sensitive string and may be one of:
80
81 **file**
82 : Relative or full path to a file. All certificates in the file will be used for path validation.
83
84 **list of files**
85 : Comma-separated list of relative or full file paths. Every PEM encoded certificate from each file will be used for path validation.
86
87 **directory**
88 : Relative or full path to a directory. Every PEM encoded certificate from each file in the directory will be used for path validation.`,
89 },
90 flags.ServerName,
91 },
92 }
93 }
94
95 func verifyAction(ctx *cli.Context) error {
96 if err := errs.NumberOfArguments(ctx, 1); err != nil {
97 return err
98 }
99
100 var (
101 crtFile = ctx.Args().Get(0)
102 host = ctx.String("host")
103 serverName = ctx.String("servername")
104 roots = ctx.String("roots")
105 intermediatePool = x509.NewCertPool()
106 rootPool *x509.CertPool
107 cert *x509.Certificate
108 )
109
110 if addr, isURL, err := trimURL(crtFile); err != nil {
111 return err
112 } else if isURL {
113 peerCertificates, err := getPeerCertificates(addr, serverName, roots, false)
114 if err != nil {
115 return err
116 }
117 cert = peerCertificates[0]
118 for _, pc := range peerCertificates {
119 intermediatePool.AddCert(pc)
120 }
121 } else {
122 crtBytes, err := ioutil.ReadFile(crtFile)
123 if err != nil {
124 return errs.FileError(err, crtFile)
125 }
126
127 var (
128 ipems []byte
129 block *pem.Block
130 )
131 // The first certificate PEM in the file is our leaf Certificate.
132 // Any certificate after the first is added to the list of Intermediate
133 // certificates used for path validation.
134 for len(crtBytes) > 0 {
135 block, crtBytes = pem.Decode(crtBytes)
136 if block == nil {
137 return errors.Errorf("%s contains an invalid PEM block", crtFile)
138 }
139 if block.Type != "CERTIFICATE" {
140 continue
141 }
142 if cert == nil {
143 cert, err = x509.ParseCertificate(block.Bytes)
144 if err != nil {
145 return errors.WithStack(err)
146 }
147 } else {
148 ipems = append(ipems, pem.EncodeToMemory(block)...)
149 }
150 }
151 if cert == nil {
152 return errors.Errorf("%s contains no PEM certificate blocks", crtFile)
153 }
154 if len(ipems) > 0 && !intermediatePool.AppendCertsFromPEM(ipems) {
155 return errors.Errorf("failure creating intermediate list from certificate '%s'", crtFile)
156 }
157 }
158
159 if roots != "" {
160 var err error
161 rootPool, err = x509util.ReadCertPool(roots)
162 if err != nil {
163 errors.Wrapf(err, "failure to load root certificate pool from input path '%s'", roots)
164 }
165 }
166
167 opts := x509.VerifyOptions{
168 DNSName: host,
169 Roots: rootPool,
170 Intermediates: intermediatePool,
171 }
172
173 if _, err := cert.Verify(opts); err != nil {
174 return errors.Wrapf(err, "failed to verify certificate")
175 }
176
177 return nil
178 }