@@ -5,8 +5,6 @@ import chronos/apps/http/httpclient, jwt, results, bearssl/pem
5
5
import ./ utils
6
6
import ../../ crypto/ crypto
7
7
import ../../ crypto/ rsa
8
- import ../../ transports/ tls/ certificate_ffi
9
- import ../../ transports/ tls/ certificate
10
8
11
9
const
12
10
LetsEncryptURL* = " https://acme-v02.api.letsencrypt.org"
@@ -30,7 +28,7 @@ type ACMEDirectory = object
30
28
31
29
type ACMEApi* = object
32
30
directory: ACMEDirectory
33
- session* : HttpSessionRef
31
+ session: HttpSessionRef
34
32
acmeServerURL: string
35
33
36
34
type JWK = object
@@ -113,6 +111,8 @@ template handleError(msg: string, body: untyped): untyped =
113
111
raise newException(ACMEError, msg & " : Failed to decode JSON" , exc)
114
112
except HttpError as exc:
115
113
raise newException(ACMEError, msg & " : Failed to connect to ACME server" , exc)
114
+ except CancelledError as exc:
115
+ raise newException(CancelledError, msg & " : Future cancelled" , exc)
116
116
except CatchableError as exc:
117
117
raise newException(ACMEError, msg & " : Unexpected error" , exc)
118
118
@@ -121,13 +121,12 @@ proc new*(
121
121
) : Future[ACMEApi] {.async: (raises: [ACMEError, CancelledError]) .} =
122
122
let session = HttpSessionRef.new()
123
123
let directory = handleError(" new API" ):
124
- (
125
- await (
126
- await HttpClientRequestRef.get(session, acmeServerURL & " /directory" ).get().send()
127
- ).getResponseBody()
128
- ).to(ACMEDirectory)
124
+ let rawResponse =
125
+ await HttpClientRequestRef.get(session, acmeServerURL & " /directory" ).get().send()
126
+ let body = await rawResponse.getResponseBody()
127
+ body.to(ACMEDirectory)
129
128
130
- return ACMEApi(session: session, directory: directory, acmeServerURL: acmeServerURL)
129
+ ACMEApi(session: session, directory: directory, acmeServerURL: acmeServerURL)
131
130
132
131
proc newNonce(
133
132
self: ACMEApi
@@ -245,14 +244,14 @@ proc requestChallenge*(
245
244
finalizeURL: challengeResponse.finalize, orderURL: orderURL, dns01: dns01
246
245
)
247
246
248
- proc notifyChallengeCompleted * (
247
+ proc challengeCompleted * (
249
248
self: ACMEApi,
250
249
chalURL: string ,
251
250
key: KeyPair,
252
251
kid: Kid,
253
252
retries: int = DefaultChalCompletedRetries,
254
253
): Future[void ] {.async: (raises: [ACMEError, CancelledError]) .} =
255
- let completedResponse = handleError(" notifyChallengeCompleted (send notify)" ):
254
+ let completedResponse = handleError(" challengeCompleted (send notify)" ):
256
255
let payload =
257
256
await self.createSignedAcmeRequest(chalURL, %* {}, key, kid = Opt.some(kid))
258
257
let rawResponse = await HttpClientRequestRef
@@ -265,7 +264,7 @@ proc notifyChallengeCompleted*(
265
264
# check until acme server is done (poll validation)
266
265
for i in 0 .. retries:
267
266
let (retryAfterHeader, checkResponse) = handleError(
268
- " notifyChallengeCompleted (check " & $ i & " )"
267
+ " challengeCompleted (check " & $ i & " )"
269
268
):
270
269
let rawResponse = await HttpClientRequestRef
271
270
.get(self.session, completedResponse.checkURL)
@@ -300,21 +299,7 @@ proc finalizeCertificate*(
300
299
kid: Kid,
301
300
retries: int = DefaultFinalizeRetries,
302
301
): Future[bool ] {.async: (raises: [ACMEError, CancelledError]) .} =
303
- var certKey: cert_key_t
304
- var certCtx: cert_context_t
305
- var derCSR: ptr cert_buffer = nil
306
-
307
- let personalizationStr = " libp2p_autotls"
308
- if cert_init_drbg(
309
- personalizationStr.cstring , personalizationStr.len.csize_t , certCtx.addr
310
- ) != CERT_SUCCESS:
311
- raise newException(ACMEError, " Failed to initialize certCtx" )
312
- if cert_generate_key(certCtx, certKey.addr ) != CERT_SUCCESS:
313
- raise newException(ACMEError, " Failed to generate cert key" )
314
-
315
- if cert_signing_req(domain.cstring , certKey, derCSR.addr ) != CERT_SUCCESS:
316
- raise newException(ACMEError, " Failed to create CSR" )
317
-
302
+ let derCSR = createCSR(domain)
318
303
let b64CSR = base64.encode(derCSR.toSeq, safe = true )
319
304
320
305
# call finalize and keep checking order until cert is valid (done)
@@ -373,3 +358,6 @@ proc downloadCertificate*(
373
358
ACMECertificateResponse(
374
359
rawCertificate: bytesToString(rawBody), certificateExpiry: certificateExpiry
375
360
)
361
+
362
+ proc close* (self: ACMEApi): Future[void ] {.async: (raises: [CancelledError]) .} =
363
+ await self.session.closeWait()
0 commit comments