-
Notifications
You must be signed in to change notification settings - Fork 5
/
keyprint.go
66 lines (60 loc) · 1.49 KB
/
keyprint.go
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
package keyprint
import (
"crypto/sha256"
"crypto/tls"
"encoding/base32"
"encoding/pem"
"errors"
"io"
"io/ioutil"
"net/url"
)
// FromCertificate returns keyprints of all certificates in cert.
func FromCertificate(cert tls.Certificate) []string {
if len(cert.Certificate) == 0 {
return nil
}
kps := make([]string, 0, len(cert.Certificate))
for _, c := range cert.Certificate {
kps = append(kps, FromBytes(c))
}
return kps
}
var base32enc = base32.StdEncoding.WithPadding(base32.NoPadding)
// FromBytes calculates a SHA256 keyprint from cert bytes.
// This function cannot be used to parse cert files, use FromFile instead.
func FromBytes(b []byte) string {
h := sha256.Sum256(b)
const pref = "SHA256/"
out := make([]byte, len(pref)+base32enc.EncodedLen(len(h)))
copy(out, pref)
base32enc.Encode(out[len(pref):], h[:])
return string(out)
}
// FromBytes calculates a SHA256 keyprint from a PEM cert file.
func FromFile(r io.Reader) ([]string, error) {
data, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
var kps []string
for {
b, rest := pem.Decode(data)
if b == nil {
break
}
data = rest
if b.Type != "CERTIFICATE" {
continue
}
kps = append(kps, FromBytes(b.Bytes))
}
if len(kps) == 0 {
return nil, errors.New("invalid PEM file, or missing a CERTIFICATE block")
}
return kps, nil
}
// FromURL extracts a keyprint from URL query parameters. If it's not set, it returns an empty string.
func FromURL(u *url.URL) string {
return u.Query().Get("kp")
}