This repository has been archived by the owner on Aug 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
letsencrypt.go
176 lines (147 loc) · 3.45 KB
/
letsencrypt.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package main
import (
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"encoding/pem"
"errors"
"github.com/ericchiang/letsencrypt"
"io/ioutil"
"os"
)
type LetsEncrypt struct {
client *letsencrypt.Client
accountKey *rsa.PrivateKey
}
func (e *LetsEncrypt) ensureClient() error {
if e.client != nil {
return nil
}
client, err := letsencrypt.NewClient("https://acme-v01.api.letsencrypt.org/directory")
if err != nil {
return err
}
e.client = client
return nil
}
func (e *LetsEncrypt) createAccountKey() error {
err := e.ensureClient()
if err != nil {
return err
}
// Create a private key for your account and register
key, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return err
}
// Register the key
if _, err := e.client.NewRegistration(key); err != nil {
return err
}
// Write account key to file
data := pem.EncodeToMemory(&pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(key),
})
err = ioutil.WriteFile(*accountKey, data, 0600)
if err != nil {
return err
}
e.accountKey = key
return nil
}
func (e *LetsEncrypt) loadAccountKey() error {
data, err := ioutil.ReadFile(*accountKey)
if err != nil {
return err
}
block, _ := pem.Decode(data)
if block == nil {
return errors.New("pem decode: no key found")
}
key, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return err
}
e.accountKey = key
return nil
}
func (e *LetsEncrypt) ensureAccountKey() (err error) {
if e.accountKey != nil {
return nil
}
err = e.loadAccountKey()
if os.IsNotExist(err) {
err = e.createAccountKey()
}
return err
}
func (e *LetsEncrypt) requestChallenge(serverName, challengeName string) (challenge letsencrypt.Challenge, err error) {
err = e.ensureClient()
if err != nil {
return
}
err = e.ensureAccountKey()
if err != nil {
return
}
// ask for a set of challenges for a given domain
auth, _, err := e.client.NewAuthorization(e.accountKey, "dns", serverName)
if err != nil {
return
}
// Find possible challenges
challenges := auth.Combinations(challengeName)
if len(challenges) == 0 {
err = errors.New("no supported challenge combinations")
return
}
challenge = challenges[0][0]
return
}
func (e *LetsEncrypt) requestTlsSni(serverName string) (certs map[string]*tls.Certificate, challenge letsencrypt.Challenge, err error) {
challenge, err = e.requestChallenge(serverName, letsencrypt.ChallengeTLSSNI)
if err != nil {
return
}
// Request TLS-SNI-01 challenge
certs, err = challenge.TLSSNI(e.accountKey)
return
}
func (e *LetsEncrypt) requestHttp(serverName string) (urlPath, resource string, challenge letsencrypt.Challenge, err error) {
challenge, err = e.requestChallenge(serverName, letsencrypt.ChallengeHTTP)
if err != nil {
return
}
// Request HTTP-01 challenge
urlPath, resource, err = challenge.HTTP(e.accountKey)
return
}
func (e *LetsEncrypt) finishChallenge(challenge letsencrypt.Challenge) (err error) {
err = e.ensureClient()
if err != nil {
return
}
err = e.ensureAccountKey()
if err != nil {
return
}
// Notify about the challenge
err = e.client.ChallengeReady(e.accountKey, challenge)
if err != nil {
return
}
return
}
func (e *LetsEncrypt) createCertificate(csr *x509.CertificateRequest) (*letsencrypt.CertificateResponse, error) {
err := e.ensureClient()
if err != nil {
return nil, err
}
err = e.ensureAccountKey()
if err != nil {
return nil, err
}
return e.client.NewCertificate(e.accountKey, csr)
}