|
| 1 | +import json, uri |
| 2 | +from times import DateTime, parse |
| 3 | +import chronos/apps/http/httpclient, results, chronicles |
| 4 | + |
| 5 | +import ./utils |
| 6 | +import ../../crypto/crypto |
| 7 | +import ../../crypto/rsa |
| 8 | + |
| 9 | +export ACMEError |
| 10 | + |
| 11 | +logScope: |
| 12 | + topics = "libp2p acme api" |
| 13 | + |
| 14 | +const |
| 15 | + LetsEncryptURL* = "https://acme-v02.api.letsencrypt.org" |
| 16 | + LetsEncryptURLStaging* = "https://acme-staging-v02.api.letsencrypt.org" |
| 17 | + Alg = "RS256" |
| 18 | + DefaultChalCompletedRetries = 10 |
| 19 | + DefaultChalCompletedRetryTime = 1.seconds |
| 20 | + DefaultFinalizeRetries = 10 |
| 21 | + DefaultFinalizeRetryTime = 1.seconds |
| 22 | + DefaultRandStringSize = 256 |
| 23 | + ACMEHttpHeaders = [("Content-Type", "application/jose+json")] |
| 24 | + |
| 25 | +type Authorization* = string |
| 26 | +type Domain* = string |
| 27 | +type Kid* = string |
| 28 | +type Nonce* = string |
| 29 | + |
| 30 | +type ACMEDirectory* = object |
| 31 | + newNonce*: string |
| 32 | + newOrder*: string |
| 33 | + newAccount*: string |
| 34 | + |
| 35 | +type ACMEApi* = ref object of RootObj |
| 36 | + directory: Opt[ACMEDirectory] |
| 37 | + session: HttpSessionRef |
| 38 | + acmeServerURL*: Uri |
| 39 | + |
| 40 | +type HTTPResponse* = object |
| 41 | + body*: JsonNode |
| 42 | + headers*: HttpTable |
| 43 | + |
| 44 | +type JWK = object |
| 45 | + kty: string |
| 46 | + n: string |
| 47 | + e: string |
| 48 | + |
| 49 | +# whether the request uses Kid or not |
| 50 | +type ACMERequestType = enum |
| 51 | + ACMEJwkRequest |
| 52 | + ACMEKidRequest |
| 53 | + |
| 54 | +type ACMERequestHeader = object |
| 55 | + alg: string |
| 56 | + typ: string |
| 57 | + nonce: Nonce |
| 58 | + url: string |
| 59 | + case kind: ACMERequestType |
| 60 | + of ACMEJwkRequest: |
| 61 | + jwk: JWK |
| 62 | + of ACMEKidRequest: |
| 63 | + kid: Kid |
| 64 | + |
| 65 | +type Email = string |
| 66 | + |
| 67 | +type ACMERegisterRequest* = object |
| 68 | + termsOfServiceAgreed: bool |
| 69 | + contact: seq[Email] |
| 70 | + |
| 71 | +type ACMEAccountStatus = enum |
| 72 | + valid = "valid" |
| 73 | + deactivated = "deactivated" |
| 74 | + revoked = "revoked" |
| 75 | + |
| 76 | +type ACMERegisterResponseBody = object |
| 77 | + status*: ACMEAccountStatus |
| 78 | + |
| 79 | +type ACMERegisterResponse* = object |
| 80 | + kid*: Kid |
| 81 | + status*: ACMEAccountStatus |
| 82 | + |
| 83 | +type ACMEChallengeStatus* {.pure.} = enum |
| 84 | + PENDING = "pending" |
| 85 | + PROCESSING = "processing" |
| 86 | + VALID = "valid" |
| 87 | + INVALID = "invalid" |
| 88 | + |
| 89 | +type ACMEOrderStatus* {.pure.} = enum |
| 90 | + PENDING = "pending" |
| 91 | + READY = "ready" |
| 92 | + PROCESSING = "processing" |
| 93 | + VALID = "valid" |
| 94 | + INVALID = "invalid" |
| 95 | + |
| 96 | +type ACMEChallengeType* {.pure.} = enum |
| 97 | + DNS01 = "dns-01" |
| 98 | + HTTP01 = "http-01" |
| 99 | + TLSALPN01 = "tls-alpn-01" |
| 100 | + |
| 101 | +type ACMEChallengeToken* = string |
| 102 | + |
| 103 | +type ACMEChallenge* = object |
| 104 | + url*: string |
| 105 | + `type`*: ACMEChallengeType |
| 106 | + status*: ACMEChallengeStatus |
| 107 | + token*: ACMEChallengeToken |
| 108 | + |
| 109 | +type ACMEChallengeIdentifier = object |
| 110 | + `type`: string |
| 111 | + value: string |
| 112 | + |
| 113 | +type ACMEChallengeRequest = object |
| 114 | + identifiers: seq[ACMEChallengeIdentifier] |
| 115 | + |
| 116 | +type ACMEChallengeResponseBody = object |
| 117 | + status: ACMEOrderStatus |
| 118 | + authorizations: seq[Authorization] |
| 119 | + finalize: string |
| 120 | + |
| 121 | +type ACMEChallengeResponse* = object |
| 122 | + status*: ACMEOrderStatus |
| 123 | + authorizations*: seq[Authorization] |
| 124 | + finalize*: string |
| 125 | + order*: string |
| 126 | + |
| 127 | +type ACMEChallengeResponseWrapper* = object |
| 128 | + finalize*: string |
| 129 | + order*: string |
| 130 | + dns01*: ACMEChallenge |
| 131 | + |
| 132 | +type ACMEAuthorizationsResponse* = object |
| 133 | + challenges*: seq[ACMEChallenge] |
| 134 | + |
| 135 | +type ACMECompletedResponse* = object |
| 136 | + url: string |
| 137 | + |
| 138 | +type ACMECheckKind* = enum |
| 139 | + ACMEOrderCheck |
| 140 | + ACMEChallengeCheck |
| 141 | + |
| 142 | +type ACMECheckResponse* = object |
| 143 | + case kind: ACMECheckKind |
| 144 | + of ACMEOrderCheck: |
| 145 | + orderStatus: ACMEOrderStatus |
| 146 | + of ACMEChallengeCheck: |
| 147 | + chalStatus: ACMEChallengeStatus |
| 148 | + retryAfter: Duration |
| 149 | + |
| 150 | +type ACMEFinalizeResponse* = object |
| 151 | + status: ACMEOrderStatus |
| 152 | + |
| 153 | +type ACMEOrderResponse* = object |
| 154 | + certificate: string |
| 155 | + expires: string |
| 156 | + |
| 157 | +type ACMECertificateResponse* = object |
| 158 | + rawCertificate*: string |
| 159 | + certificateExpiry*: DateTime |
| 160 | + |
1 | 161 | when defined(libp2p_autotls_support): |
2 | | - import options, sequtils, strutils, json, uri |
3 | | - from times import DateTime, parse |
4 | | - import chronos/apps/http/httpclient, jwt, results, bearssl/pem, chronicles |
5 | | - |
6 | | - import ./utils |
7 | | - import ../../crypto/crypto |
8 | | - import ../../crypto/rsa |
9 | | - |
10 | | - export ACMEError |
11 | | - |
12 | | - logScope: |
13 | | - topics = "libp2p acme api" |
14 | | - |
15 | | - const |
16 | | - LetsEncryptURL* = "https://acme-v02.api.letsencrypt.org" |
17 | | - LetsEncryptURLStaging* = "https://acme-staging-v02.api.letsencrypt.org" |
18 | | - Alg = "RS256" |
19 | | - DefaultChalCompletedRetries = 10 |
20 | | - DefaultChalCompletedRetryTime = 1.seconds |
21 | | - DefaultFinalizeRetries = 10 |
22 | | - DefaultFinalizeRetryTime = 1.seconds |
23 | | - DefaultRandStringSize = 256 |
24 | | - ACMEHttpHeaders = [("Content-Type", "application/jose+json")] |
25 | | - |
26 | | - type Authorization* = string |
27 | | - type Domain* = string |
28 | | - type Kid* = string |
29 | | - type Nonce* = string |
30 | | - |
31 | | - type ACMEDirectory* = object |
32 | | - newNonce*: string |
33 | | - newOrder*: string |
34 | | - newAccount*: string |
35 | | - |
36 | | - type ACMEApi* = ref object of RootObj |
37 | | - directory: Opt[ACMEDirectory] |
38 | | - session: HttpSessionRef |
39 | | - acmeServerURL*: Uri |
40 | | - |
41 | | - type HTTPResponse* = object |
42 | | - body*: JsonNode |
43 | | - headers*: HttpTable |
44 | | - |
45 | | - type JWK = object |
46 | | - kty: string |
47 | | - n: string |
48 | | - e: string |
49 | | - |
50 | | - # whether the request uses Kid or not |
51 | | - type ACMERequestType = enum |
52 | | - ACMEJwkRequest |
53 | | - ACMEKidRequest |
54 | | - |
55 | | - type ACMERequestHeader = object |
56 | | - alg: string |
57 | | - typ: string |
58 | | - nonce: Nonce |
59 | | - url: string |
60 | | - case kind: ACMERequestType |
61 | | - of ACMEJwkRequest: |
62 | | - jwk: JWK |
63 | | - of ACMEKidRequest: |
64 | | - kid: Kid |
65 | | - |
66 | | - type Email = string |
67 | | - |
68 | | - type ACMERegisterRequest* = object |
69 | | - termsOfServiceAgreed: bool |
70 | | - contact: seq[Email] |
71 | | - |
72 | | - type ACMEAccountStatus = enum |
73 | | - valid = "valid" |
74 | | - deactivated = "deactivated" |
75 | | - revoked = "revoked" |
76 | | - |
77 | | - type ACMERegisterResponseBody = object |
78 | | - status*: ACMEAccountStatus |
79 | | - |
80 | | - type ACMERegisterResponse* = object |
81 | | - kid*: Kid |
82 | | - status*: ACMEAccountStatus |
83 | | - |
84 | | - type ACMEChallengeStatus* {.pure.} = enum |
85 | | - PENDING = "pending" |
86 | | - PROCESSING = "processing" |
87 | | - VALID = "valid" |
88 | | - INVALID = "invalid" |
89 | | - |
90 | | - type ACMEOrderStatus* {.pure.} = enum |
91 | | - PENDING = "pending" |
92 | | - READY = "ready" |
93 | | - PROCESSING = "processing" |
94 | | - VALID = "valid" |
95 | | - INVALID = "invalid" |
96 | | - |
97 | | - type ACMEChallengeType* {.pure.} = enum |
98 | | - DNS01 = "dns-01" |
99 | | - HTTP01 = "http-01" |
100 | | - TLSALPN01 = "tls-alpn-01" |
101 | | - |
102 | | - type ACMEChallengeToken* = string |
103 | | - |
104 | | - type ACMEChallenge* = object |
105 | | - url*: string |
106 | | - `type`*: ACMEChallengeType |
107 | | - status*: ACMEChallengeStatus |
108 | | - token*: ACMEChallengeToken |
109 | | - |
110 | | - type ACMEChallengeIdentifier = object |
111 | | - `type`: string |
112 | | - value: string |
113 | | - |
114 | | - type ACMEChallengeRequest = object |
115 | | - identifiers: seq[ACMEChallengeIdentifier] |
116 | | - |
117 | | - type ACMEChallengeResponseBody = object |
118 | | - status: ACMEOrderStatus |
119 | | - authorizations: seq[Authorization] |
120 | | - finalize: string |
121 | | - |
122 | | - type ACMEChallengeResponse* = object |
123 | | - status*: ACMEOrderStatus |
124 | | - authorizations*: seq[Authorization] |
125 | | - finalize*: string |
126 | | - order*: string |
127 | | - |
128 | | - type ACMEChallengeResponseWrapper* = object |
129 | | - finalize*: string |
130 | | - order*: string |
131 | | - dns01*: ACMEChallenge |
132 | | - |
133 | | - type ACMEAuthorizationsResponse* = object |
134 | | - challenges*: seq[ACMEChallenge] |
135 | | - |
136 | | - type ACMECompletedResponse* = object |
137 | | - url: string |
138 | | - |
139 | | - type ACMECheckKind* = enum |
140 | | - ACMEOrderCheck |
141 | | - ACMEChallengeCheck |
142 | | - |
143 | | - type ACMECheckResponse* = object |
144 | | - case kind: ACMECheckKind |
145 | | - of ACMEOrderCheck: |
146 | | - orderStatus: ACMEOrderStatus |
147 | | - of ACMEChallengeCheck: |
148 | | - chalStatus: ACMEChallengeStatus |
149 | | - retryAfter: Duration |
150 | | - |
151 | | - type ACMEFinalizeResponse* = object |
152 | | - status: ACMEOrderStatus |
153 | | - |
154 | | - type ACMEOrderResponse* = object |
155 | | - certificate: string |
156 | | - expires: string |
157 | | - |
158 | | - type ACMECertificateResponse* = object |
159 | | - rawCertificate*: string |
160 | | - certificateExpiry*: DateTime |
| 162 | + import options, sequtils, strutils, jwt, bearssl/pem |
161 | 163 |
|
162 | 164 | template handleError*(msg: string, body: untyped): untyped = |
163 | 165 | try: |
|
0 commit comments