-
Notifications
You must be signed in to change notification settings - Fork 2
/
default_manager.go
138 lines (108 loc) · 3.19 KB
/
default_manager.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package profile
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/asn1"
"encoding/pem"
"fmt"
"os"
"os/user"
"time"
"github.com/pkg/errors"
"github.com/dpb587/ssoca/service/openvpn/api"
"github.com/dpb587/ssoca/service/openvpn/httpclient"
)
type DefaultManager struct {
client httpclient.Client
service string
privateKey *rsa.PrivateKey
profile string
certificate *x509.Certificate
certificateBytes []byte
}
var _ Manager = &DefaultManager{}
func NewDefaultManager(client httpclient.Client, service string, privateKey *rsa.PrivateKey) DefaultManager {
return DefaultManager{
client: client,
service: service,
privateKey: privateKey,
}
}
func CreateManagerAndPrivateKey(client httpclient.Client, service string) (DefaultManager, error) {
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return DefaultManager{}, errors.Wrap(err, "generating private key")
}
return NewDefaultManager(client, service, privateKey), nil
}
func (m DefaultManager) Sign(data []byte) ([]byte, error) {
return rsa.SignPKCS1v15(rand.Reader, m.privateKey, 0, data)
}
func (m *DefaultManager) GetProfile() (Profile, error) {
if !m.IsCertificateValid() {
err := m.Renew()
if err != nil {
return Profile{}, errors.Wrap(err, "renewing certificate")
}
}
return NewProfile(m.profile, m.privateKey, m.certificateBytes), nil
}
func (m DefaultManager) IsCertificateValid() bool {
return m.certificate != nil && time.Now().Before(m.certificate.NotAfter)
}
func (m *DefaultManager) Renew() error {
csrBytes, err := m.createCSR()
if err != nil {
return errors.Wrap(err, "creating CSR")
}
response, err := m.client.SignUserCSR(api.SignUserCSRRequest{
CSR: string(pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE REQUEST", Bytes: csrBytes})),
})
if err != nil {
return errors.Wrap(err, "requesting signed profile")
}
m.profile = response.Profile
m.certificateBytes = []byte(response.Certificate)
pem, _ := pem.Decode(m.certificateBytes)
if pem == nil {
return errors.New("failed to decode PEM from certificate")
}
certificate, err := x509.ParseCertificate(pem.Bytes)
if err != nil {
return errors.Wrap(err, "parsing certificate")
}
m.certificate = certificate
return nil
}
func (m DefaultManager) createCSR() ([]byte, error) {
localuser, err := user.Current()
if err != nil {
return nil, errors.Wrap(err, "checking local user")
}
localhost, err := os.Hostname()
if err != nil {
return nil, errors.Wrap(err, "checking local hostname")
}
emailAddress := fmt.Sprintf("%s@%s", localuser.Username, localhost)
subj := pkix.Name{CommonName: m.service}
rawSubj := subj.ToRDNSequence()
rawSubj = append(rawSubj, []pkix.AttributeTypeAndValue{
{
Type: asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 9, 1},
Value: emailAddress,
},
})
asn1Subj, _ := asn1.Marshal(rawSubj)
template := x509.CertificateRequest{
RawSubject: asn1Subj,
EmailAddresses: []string{emailAddress},
SignatureAlgorithm: x509.SHA256WithRSA,
}
csrBytes, err := x509.CreateCertificateRequest(rand.Reader, &template, m.privateKey)
if err != nil {
return nil, errors.Wrap(err, "creating certificate request")
}
return csrBytes, nil
}