Debian Codegraph @

crypto/x509util/crt.go

raw symbols SCIP
1 package x509util
2
3 import (
4 "crypto/sha256"
5 "crypto/x509"
6 "encoding/base64"
7 "encoding/hex"
8 "encoding/pem"
9 "io/ioutil"
10 "net"
11 "net/url"
12 "os"
13 "path/filepath"
14 "strings"
15
16 "github.com/pkg/errors"
17 "github.com/smallstep/cli/errs"
18 )
19
20 // Fingerprint returns the SHA-256 fingerprint of the certificate.
21 func Fingerprint(cert *x509.Certificate) string {
22 return EncodedFingerprint(cert, HexFingerprint)
23 }
24
25 type FingerprintEncoding int
26
27 const (
28 HexFingerprint FingerprintEncoding = iota
29 Base64Fingerprint
30 Base64UrlFingerprint
31 )
32
33 // EncodedFingerprint returns an encoded the SHA-256 fingerprint of the certificate. Defaults to hex encoding
34 func EncodedFingerprint(cert *x509.Certificate, encoding FingerprintEncoding) string {
35 sum := sha256.Sum256(cert.Raw)
36 if encoding == HexFingerprint {
37 return strings.ToLower(hex.EncodeToString(sum[:]))
38 }
39 src := make([]byte, len(sum))
40 for i, b := range sum {
41 src[i] = b
42 }
43 switch encoding {
44 case Base64Fingerprint:
45 return base64.StdEncoding.EncodeToString(src)
46 case Base64UrlFingerprint:
47 return base64.URLEncoding.EncodeToString(src)
48 }
49 // should not get here
50 return ""
51 }
52
53 // SplitSANs splits a slice of Subject Alternative Names into slices of
54 // IP Addresses and DNS Names. If an element is not an IP address, then it
55 // is bucketed as a DNS Name.
56 func SplitSANs(sans []string) (dnsNames []string, ips []net.IP, emails []string, uris []*url.URL) {
57 dnsNames = []string{}
58 ips = []net.IP{}
59 emails = []string{}
60 uris = []*url.URL{}
61 if sans == nil {
62 return
63 }
64 for _, san := range sans {
65 if ip := net.ParseIP(san); ip != nil {
66 ips = append(ips, ip)
67 } else if u, err := url.Parse(san); err == nil && u.Scheme != "" {
68 uris = append(uris, u)
69 } else if strings.Contains(san, "@") {
70 emails = append(emails, san)
71 } else {
72 dnsNames = append(dnsNames, san)
73 }
74 }
75 return
76 }
77
78 // ReadCertPool loads a certificate pool from disk.
79 // *path*: a file, a directory, or a comma-separated list of files.
80 func ReadCertPool(path string) (*x509.CertPool, error) {
81 info, err := os.Stat(path)
82 if err != nil && !os.IsNotExist(err) {
83 return nil, errors.Wrapf(err, "os.Stat %s failed", path)
84 }
85
86 var (
87 files []string
88 pool = x509.NewCertPool()
89 )
90 if info != nil && info.IsDir() {
91 finfos, err := ioutil.ReadDir(path)
92 if err != nil {
93 return nil, errs.FileError(err, path)
94 }
95 for _, finfo := range finfos {
96 files = append(files, filepath.Join(path, finfo.Name()))
97 }
98 } else {
99 files = strings.Split(path, ",")
100 for i := range files {
101 files[i] = strings.TrimSpace(files[i])
102 }
103 }
104
105 var pems []byte
106 for _, f := range files {
107 bytes, err := ioutil.ReadFile(f)
108 if err != nil {
109 return nil, errs.FileError(err, f)
110 }
111 for len(bytes) > 0 {
112 var block *pem.Block
113 block, bytes = pem.Decode(bytes)
114 if block == nil {
115 // TODO: at a higher log level we should log the file we could not find.
116 break
117 }
118 // Ignore PEM blocks that are not CERTIFICATEs.
119 if block.Type != "CERTIFICATE" {
120 continue
121 }
122 pems = append(pems, pem.EncodeToMemory(block)...)
123 }
124 }
125 if ok := pool.AppendCertsFromPEM(pems); !ok {
126 return nil, errors.Errorf("error loading Root certificates")
127 }
128 return pool, nil
129 }