forked from sapcc/go-vice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
69 lines (59 loc) · 1.5 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
67
68
69
package vice
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/asn1"
"encoding/pem"
"net"
)
func CreateCSR(name pkix.Name, email string, sans []string, key *rsa.PrivateKey) ([]byte, error) {
rawSubject, err := asn1.Marshal(name.ToRDNSequence())
if err != nil {
return nil, err
}
template := x509.CertificateRequest{
RawSubject: rawSubject,
EmailAddresses: []string{email},
SignatureAlgorithm: x509.SHA256WithRSA,
}
if len(sans) > 0 {
rawSANS, err := marshalSANs(sans, nil, nil)
if err != nil {
return nil, err
}
template.ExtraExtensions = []pkix.Extension{
pkix.Extension{
Id: []int{2, 5, 29, 17},
Value: rawSANS,
},
}
}
csr, err := x509.CreateCertificateRequest(rand.Reader, &template, key)
if err != nil {
return nil, err
}
block := pem.Block{
Type: "CERTIFICATE REQUEST",
Bytes: csr,
}
return pem.EncodeToMemory(&block), nil
}
func marshalSANs(dnsNames, emailAddresses []string, ipAddresses []net.IP) (derBytes []byte, err error) {
var rawValues []asn1.RawValue
for _, name := range dnsNames {
rawValues = append(rawValues, asn1.RawValue{Tag: 2, Class: 2, Bytes: []byte(name)})
}
for _, email := range emailAddresses {
rawValues = append(rawValues, asn1.RawValue{Tag: 1, Class: 2, Bytes: []byte(email)})
}
for _, rawIP := range ipAddresses {
ip := rawIP.To4()
if ip == nil {
ip = rawIP
}
rawValues = append(rawValues, asn1.RawValue{Tag: 7, Class: 2, Bytes: ip})
}
return asn1.Marshal(rawValues)
}