-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmethods.go
More file actions
473 lines (388 loc) · 10.8 KB
/
methods.go
File metadata and controls
473 lines (388 loc) · 10.8 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
package acme
import (
"encoding/base64"
"encoding/json"
"fmt"
"io"
"strings"
"time"
"github.com/hashicorp/go-retryablehttp"
)
// AccountResponse represents the ACME account response
type AccountResponse struct {
Contact []string `json:"contact"`
CreatedAt string `json:"createdAt"`
Status string `json:"status"`
}
// OrderResponse represents the ACME order response
type OrderResponse struct {
Status string `json:"status"`
Expires string `json:"expires"`
Identifiers []map[string]string `json:"identifiers"`
Authorizations []string `json:"authorizations"`
Finalize string `json:"finalize"`
Certificate string `json:"certificate,omitempty"`
}
// AuthorizationResponse represents the ACME authorization response
type AuthorizationResponse struct {
Identifier map[string]string `json:"identifier"`
Status string `json:"status"`
Expires string `json:"expires"`
Challenges []ChallengeResponse `json:"challenges"`
}
// ChallengeResponse represents the ACME challenge response
type ChallengeResponse struct {
Type string `json:"type"`
Status string `json:"status"`
URL string `json:"url"`
Token string `json:"token"`
}
// GetAccount retrieves account information (public method)
func (c *Client) GetAccount() (*Account, error) {
return c.getAccount()
}
// getOrder retrieves an existing order by ID (internal helper)
func (c *Client) getOrder(id string) (*Order, error) {
url := strings.Replace(c.directories[DirectoryNewOrder], "new-order", "order", 1)
url = fmt.Sprintf("%s/%s/%s", url, c.account.GetID(), id)
jws, err := c.signPayloadKID(nil, url)
if err != nil {
return nil, err
}
resp, err := c.request("POST", url, jws)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var orderResp OrderResponse
if err := json.Unmarshal(body, &orderResp); err != nil {
return nil, err
}
domains := make([]string, len(orderResp.Identifiers))
for i, identifier := range orderResp.Identifiers {
domains[i] = identifier["value"]
}
return NewOrder(
domains,
url,
orderResp.Status,
orderResp.Expires,
orderResp.Identifiers,
orderResp.Authorizations,
orderResp.Finalize,
)
}
// GetOrder retrieves an existing order by ID (public method)
func (c *Client) GetOrder(id string) (*Order, error) {
return c.getOrder(id)
}
// IsReady checks if an order is ready for finalization
func (c *Client) IsReady(order *Order) bool {
updatedOrder, err := c.getOrder(order.GetID())
if err != nil {
fmt.Printf("Error checking order status: %v\n", err)
return false
}
fmt.Printf("Order status: %s\n", updatedOrder.GetStatus())
return updatedOrder.GetStatus() == "ready"
}
// CreateOrder creates a new ACME order
func (c *Client) CreateOrder(domains []string) (*Order, error) {
identifiers := make([]map[string]string, len(domains))
for i, domain := range domains {
identifiers[i] = map[string]string{
"type": "dns",
"value": domain,
}
}
payload := map[string]interface{}{
"identifiers": identifiers,
}
url := c.directories[DirectoryNewOrder]
jws, err := c.signPayloadKID(payload, url)
if err != nil {
return nil, err
}
resp, err := c.request("POST", url, jws)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var orderResp OrderResponse
if err := json.Unmarshal(body, &orderResp); err != nil {
return nil, err
}
orderURL := resp.Header.Get("Location")
return NewOrder(
domains,
orderURL,
orderResp.Status,
orderResp.Expires,
orderResp.Identifiers,
orderResp.Authorizations,
orderResp.Finalize,
)
}
// Authorize obtains authorizations for an order
func (c *Client) Authorize(order *Order) ([]*Authorization, error) {
digest, err := c.getDigest()
if err != nil {
return nil, err
}
var authorizations []*Authorization
for _, authURL := range order.GetAuthorizationURLs() {
jws, err := c.signPayloadKID(nil, authURL)
if err != nil {
return nil, err
}
resp, err := c.request("POST", authURL, jws)
if err != nil {
return nil, err
}
body, err := io.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return nil, err
}
var authResp AuthorizationResponse
if err := json.Unmarshal(body, &authResp); err != nil {
return nil, err
}
authorization, err := NewAuthorization(
authResp.Identifier["value"],
authResp.Expires,
digest,
)
if err != nil {
return nil, err
}
for _, challengeData := range authResp.Challenges {
challenge := NewChallenge(
authURL,
challengeData.Type,
challengeData.Status,
challengeData.URL,
challengeData.Token,
)
authorization.AddChallenge(challenge)
}
authorizations = append(authorizations, authorization)
}
return authorizations, nil
}
// SelfTest performs a DNS self-test for the authorization
func (c *Client) SelfTest(authorization *Authorization) bool {
return c.selfDNSTest(authorization, c.maxAttempts)
}
// selfDNSTest performs DNS validation self-test using Cloudflare DNS
func (c *Client) selfDNSTest(authorization *Authorization, maxAttempts int) bool {
txtRecord := authorization.GetTxtRecord()
if txtRecord == nil {
return false
}
fmt.Printf("🔍 Testing DNS record: %s = %s\n", txtRecord.GetName(), txtRecord.GetValue())
for maxAttempts > 0 {
url := fmt.Sprintf("https://cloudflare-dns.com/dns-query?name=%s&type=TXT",
txtRecord.GetName())
req, err := retryablehttp.NewRequest("GET", url, nil)
if err != nil {
maxAttempts--
continue
}
req.Header.Set("Accept", "application/dns-json")
resp, err := c.httpClient.Do(req)
if err != nil {
maxAttempts--
continue
}
body, err := io.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
maxAttempts--
continue
}
var dnsResp struct {
Answer []struct {
Data string `json:"data"`
} `json:"Answer"`
}
if err := json.Unmarshal(body, &dnsResp); err != nil {
maxAttempts--
continue
}
for _, answer := range dnsResp.Answer {
data := strings.Trim(answer.Data, "\"")
if data == txtRecord.GetValue() {
fmt.Printf("✅ DNS record found and verified!\n")
return true
}
}
if maxAttempts > 1 {
fmt.Printf("🔄 DNS record not found yet, retrying... (%d attempts left)\n", maxAttempts-1)
time.Sleep(time.Duration(45/maxAttempts) * time.Second)
}
maxAttempts--
}
fmt.Printf("❌ DNS record not found after all attempts\n")
return false
}
// Validate validates a DNS challenge
func (c *Client) Validate(challenge *Challenge, maxAttempts int) bool {
digest, err := c.getDigest()
if err != nil {
return false
}
payload := map[string]any{
"keyAuthorization": challenge.GetToken() + "." + digest,
}
jws, err := c.signPayloadKID(payload, challenge.GetURL())
if err != nil {
return false
}
// Submit challenge
resp, err := c.request("POST", challenge.GetURL(), jws)
if err != nil {
return false
}
resp.Body.Close()
fmt.Printf("🔐 Challenge submitted to Let's Encrypt, waiting for validation...\n")
// Poll for validation
for maxAttempts > 0 {
jws, err := c.signPayloadKID(nil, challenge.GetAuthorizationURL())
if err != nil {
return false
}
resp, err := c.request("POST", challenge.GetAuthorizationURL(), jws)
if err != nil {
return false
}
body, err := io.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return false
}
var authResp AuthorizationResponse
if err := json.Unmarshal(body, &authResp); err != nil {
return false
}
fmt.Printf("📋 Authorization status: %s\n", authResp.Status)
if authResp.Status == "valid" {
fmt.Printf("✅ Domain validation successful!\n")
return true
}
if authResp.Status == "invalid" {
fmt.Printf("❌ Domain validation failed\n")
return false
}
if maxAttempts > 1 && authResp.Status != "valid" {
fmt.Printf("⏳ Waiting for validation... (%d attempts remaining)\n", maxAttempts-1)
time.Sleep(time.Duration(15/maxAttempts) * time.Second)
}
maxAttempts--
}
return false
}
// GetCertificate retrieves the certificate for an order
func (c *Client) GetCertificate(order *Order) (*Certificate, error) {
// Generate new private key for certificate
privateKey, privateKeyPEM, err := generateNewKey(c.config.KeyLength)
if err != nil {
return nil, fmt.Errorf("failed to generate private key: %w", err)
}
// Generate CSR
csr, err := generateCSR(order.GetDomains(), privateKey)
if err != nil {
return nil, fmt.Errorf("failed to generate CSR: %w", err)
}
// Convert CSR to DER format
csrDER, err := toDER(csr)
if err != nil {
return nil, fmt.Errorf("failed to convert CSR to DER: %w", err)
}
// Submit CSR for finalization
payload := map[string]interface{}{
"csr": base64.RawURLEncoding.EncodeToString(csrDER),
}
jws, err := c.signPayloadKID(payload, order.GetFinalizeURL())
if err != nil {
return nil, err
}
resp, err := c.request("POST", order.GetFinalizeURL(), jws)
if err != nil {
return nil, err
}
body, err := io.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return nil, err
}
var orderResp OrderResponse
if err := json.Unmarshal(body, &orderResp); err != nil {
return nil, err
}
// Poll for order completion and certificate URL
maxAttempts := 30 // Wait up to 30 attempts (30 seconds)
orderURL := order.GetURL()
fmt.Println("📜 Waiting for certificate to be issued...")
for maxAttempts > 0 {
// Get updated order status
jws, err := c.signPayloadKID(nil, orderURL)
if err != nil {
return nil, err
}
resp, err := c.request("POST", orderURL, jws)
if err != nil {
return nil, err
}
body, err := io.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return nil, err
}
if err := json.Unmarshal(body, &orderResp); err != nil {
return nil, err
}
fmt.Printf("📋 Order status: %s\n", orderResp.Status)
if orderResp.Status == "valid" && orderResp.Certificate != "" {
fmt.Println("🎉 Certificate is ready!")
break
}
if orderResp.Status == "invalid" {
return nil, fmt.Errorf("order became invalid")
}
if maxAttempts > 1 {
fmt.Printf("⏳ Waiting for certificate... (%d attempts remaining)\n", maxAttempts-1)
time.Sleep(1 * time.Second)
}
maxAttempts--
}
if orderResp.Certificate == "" {
return nil, fmt.Errorf("certificate URL not available after polling")
}
fmt.Printf("📥 Downloading certificate from: %s\n", orderResp.Certificate)
// Download certificate
jws, err = c.signPayloadKID(nil, orderResp.Certificate)
if err != nil {
return nil, err
}
certResp, err := c.request("POST", orderResp.Certificate, jws)
if err != nil {
return nil, err
}
certChain, err := io.ReadAll(certResp.Body)
certResp.Body.Close()
if err != nil {
return nil, err
}
// Clean up the certificate chain
chainStr := strings.TrimSpace(string(certChain))
return NewCertificate(privateKeyPEM, csr, chainStr)
}