-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
66 lines (54 loc) · 1.33 KB
/
util.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 go_certcentral
import (
"crypto/x509"
"encoding/pem"
"errors"
"strings"
)
const (
separator = "/"
certificateType = "CERTIFICATE"
)
func makeURL(urlParts ...string) string {
normalizedParts := normalizeURLParts(urlParts, separator)
url := ensureSuffix(baseURL, separator)
url += strings.Join(normalizedParts, separator)
return url
}
func normalizeURLParts(urlParts []string, separator string) []string {
normalizedParts := make([]string, len(urlParts))
for idx, part := range urlParts {
part = strings.TrimPrefix(part, separator)
part = strings.TrimSuffix(part, separator)
normalizedParts[idx] = part
}
return normalizedParts
}
func ensureSuffix(s, suffix string) string {
if !strings.HasSuffix(s, suffix) {
return s + suffix
}
return s
}
func decodePEM(data []byte) ([]*x509.Certificate, error) {
crtList := make([]*x509.Certificate, 0)
for {
block, rest := pem.Decode(data)
if block == nil {
return crtList, errors.New("couldn't decode certificate from PEM block")
}
if block.Type != certificateType {
return crtList, errors.New("certificate contains invalid date")
}
crt, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return crtList, err
}
crtList = append(crtList, crt)
data = rest
if data == nil || len(data) == 0 {
break
}
}
return crtList, nil
}