Debian Codegraph @

cmd/certdump/util.go

raw
1 package main
2
3 import (
4 "crypto/tls"
5 "crypto/x509"
6 "errors"
7 "fmt"
8 "net"
9 "os"
10 "strings"
11
12 cferr "github.com/cloudflare/cfssl/errors"
13 "github.com/kr/text"
14 )
15
16 // following two lifted from CFSSL, (replace-regexp "\(.+\): \(.+\),"
17 // "\2: \1,")
18
19 var keyUsage = map[x509.KeyUsage]string{
20 x509.KeyUsageDigitalSignature: "digital signature",
21 x509.KeyUsageContentCommitment: "content committment",
22 x509.KeyUsageKeyEncipherment: "key encipherment",
23 x509.KeyUsageKeyAgreement: "key agreement",
24 x509.KeyUsageDataEncipherment: "data encipherment",
25 x509.KeyUsageCertSign: "cert sign",
26 x509.KeyUsageCRLSign: "crl sign",
27 x509.KeyUsageEncipherOnly: "encipher only",
28 x509.KeyUsageDecipherOnly: "decipher only",
29 }
30
31 var extKeyUsages = map[x509.ExtKeyUsage]string{
32 x509.ExtKeyUsageAny: "any",
33 x509.ExtKeyUsageServerAuth: "server auth",
34 x509.ExtKeyUsageClientAuth: "client auth",
35 x509.ExtKeyUsageCodeSigning: "code signing",
36 x509.ExtKeyUsageEmailProtection: "s/mime",
37 x509.ExtKeyUsageIPSECEndSystem: "ipsec end system",
38 x509.ExtKeyUsageIPSECTunnel: "ipsec tunnel",
39 x509.ExtKeyUsageIPSECUser: "ipsec user",
40 x509.ExtKeyUsageTimeStamping: "timestamping",
41 x509.ExtKeyUsageOCSPSigning: "ocsp signing",
42 x509.ExtKeyUsageMicrosoftServerGatedCrypto: "microsoft sgc",
43 x509.ExtKeyUsageNetscapeServerGatedCrypto: "netscape sgc",
44 }
45
46 func pubKeyAlgo(a x509.PublicKeyAlgorithm) string {
47 switch a {
48 case x509.RSA:
49 return "RSA"
50 case x509.ECDSA:
51 return "ECDSA"
52 case x509.DSA:
53 return "DSA"
54 default:
55 return "unknown public key algorithm"
56 }
57 }
58
59 func sigAlgoPK(a x509.SignatureAlgorithm) string {
60 switch a {
61
62 case x509.MD2WithRSA, x509.MD5WithRSA, x509.SHA1WithRSA, x509.SHA256WithRSA, x509.SHA384WithRSA, x509.SHA512WithRSA:
63 return "RSA"
64 case x509.ECDSAWithSHA1, x509.ECDSAWithSHA256, x509.ECDSAWithSHA384, x509.ECDSAWithSHA512:
65 return "ECDSA"
66 case x509.DSAWithSHA1, x509.DSAWithSHA256:
67 return "DSA"
68 default:
69 return "unknown public key algorithm"
70 }
71 }
72
73 func sigAlgoHash(a x509.SignatureAlgorithm) string {
74 switch a {
75 case x509.MD2WithRSA:
76 return "MD2"
77 case x509.MD5WithRSA:
78 return "MD5"
79 case x509.SHA1WithRSA, x509.ECDSAWithSHA1, x509.DSAWithSHA1:
80 return "SHA1"
81 case x509.SHA256WithRSA, x509.ECDSAWithSHA256, x509.DSAWithSHA256:
82 return "SHA256"
83 case x509.SHA384WithRSA, x509.ECDSAWithSHA384:
84 return "SHA384"
85 case x509.SHA512WithRSA, x509.ECDSAWithSHA512:
86 return "SHA512"
87 default:
88 return "unknown hash algorithm"
89 }
90 }
91
92 // TranslateCFSSLError turns a CFSSL error into a more readable string.
93 func TranslateCFSSLError(err error) error {
94 if err == nil {
95 return nil
96 }
97
98 // printing errors as json is terrible
99 if cfsslError, ok := err.(*cferr.Error); ok {
100 err = errors.New(cfsslError.Message)
101 }
102 return err
103 }
104
105 // Warnx displays a formatted error message to standard error, à la
106 // warnx(3).
107 func Warnx(format string, a ...interface{}) (int, error) {
108 format += "\n"
109 return fmt.Fprintf(os.Stderr, format, a...)
110 }
111
112 // Warn displays a formatted error message to standard output,
113 // appending the error string, à la warn(3).
114 func Warn(err error, format string, a ...interface{}) (int, error) {
115 format += ": %v\n"
116 a = append(a, err)
117 return fmt.Fprintf(os.Stderr, format, a...)
118 }
119
120 const maxLine = 78
121
122 func makeIndent(n int) string {
123 s := " "
124 for i := 0; i < n; i++ {
125 s += " "
126 }
127 return s
128 }
129
130 func indentLen(n int) int {
131 return 4 + (8 * n)
132 }
133
134 // this isn't real efficient, but that's not a problem here
135 func wrap(s string, indent int) string {
136 if indent > 3 {
137 indent = 3
138 }
139
140 wrapped := text.Wrap(s, maxLine)
141 lines := strings.SplitN(wrapped, "\n", 2)
142 if len(lines) == 1 {
143 return lines[0]
144 }
145
146 if (maxLine - indentLen(indent)) <= 0 {
147 panic("too much indentation")
148 }
149
150 rest := strings.Join(lines[1:], " ")
151 wrapped = text.Wrap(rest, maxLine-indentLen(indent))
152 return lines[0] + "\n" + text.Indent(wrapped, makeIndent(indent))
153 }
154
155 func dumpHex(in []byte) string {
156 var s string
157 for i := range in {
158 s += fmt.Sprintf("%02X:", in[i])
159 }
160
161 return strings.Trim(s, ":")
162 }
163
164 // permissiveConfig returns a maximally-accepting TLS configuration;
165 // the purpose is to look at the cert, not verify the security properties
166 // of the connection.
167 func permissiveConfig() *tls.Config {
168 return &tls.Config{
169 InsecureSkipVerify: true,
170 }
171 }
172
173 // verifyConfig returns a config that will verify the connection.
174 func verifyConfig(hostname string) *tls.Config {
175 return &tls.Config{
176 ServerName: hostname,
177 }
178 }
179
180 type connInfo struct {
181 // The original URI provided.
182 URI string
183
184 // The hostname of the server.
185 Host string
186
187 // The port to connect on.
188 Port string
189
190 // The address to connect to.
191 Addr string
192 }
193
194 func getConnInfo(uri string) *connInfo {
195 ci := &connInfo{URI: uri}
196 ci.Host = uri[len("https://"):]
197
198 host, port, err := net.SplitHostPort(ci.Host)
199 if err != nil {
200 ci.Port = "443"
201 } else {
202 ci.Host = host
203 ci.Port = port
204 }
205 ci.Addr = net.JoinHostPort(ci.Host, ci.Port)
206 return ci
207 }