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
use openssl::sha::sha256;
use std::sync::Arc;
use std::thread;
use std::time::Duration;
use crate::acc::AccountInner;
use crate::acc::AcmeKey;
use crate::api::{ApiAuth, ApiChallenge, ApiEmptyObject, ApiEmptyString};
use crate::error::*;
use crate::jwt::*;
use crate::util::{base64url, read_json};
#[derive(Debug)]
pub struct Auth {
inner: Arc<AccountInner>,
api_auth: ApiAuth,
auth_url: String,
}
impl Auth {
pub(crate) fn new(inner: &Arc<AccountInner>, api_auth: ApiAuth, auth_url: &str) -> Self {
Auth {
inner: inner.clone(),
api_auth,
auth_url: auth_url.into(),
}
}
pub fn domain_name(&self) -> &str {
&self.api_auth.identifier.value
}
pub fn need_challenge(&self) -> bool {
!self.api_auth.is_status_valid()
}
pub fn http_challenge(&self) -> Challenge<Http> {
self.api_auth
.http_challenge()
.map(|c| Challenge::new(&self.inner, c.clone(), &self.auth_url))
.expect("http-challenge")
}
pub fn dns_challenge(&self) -> Challenge<Dns> {
self.api_auth
.dns_challenge()
.map(|c| Challenge::new(&self.inner, c.clone(), &self.auth_url))
.expect("dns-challenge")
}
pub fn tls_alpn_challenge(&self) -> Challenge<TlsAlpn> {
self.api_auth
.tls_alpn_challenge()
.map(|c| Challenge::new(&self.inner, c.clone(), &self.auth_url))
.expect("tls-alpn-challenge")
}
pub fn api_auth(&self) -> &ApiAuth {
&self.api_auth
}
}
#[doc(hidden)]
pub struct Http;
#[doc(hidden)]
pub struct Dns;
#[doc(hidden)]
pub struct TlsAlpn;
pub struct Challenge<A> {
inner: Arc<AccountInner>,
api_challenge: ApiChallenge,
auth_url: String,
_ph: std::marker::PhantomData<A>,
}
impl Challenge<Http> {
pub fn http_token(&self) -> &str {
&self.api_challenge.token
}
pub fn http_proof(&self) -> String {
let acme_key = self.inner.transport.acme_key();
key_authorization(&self.api_challenge.token, acme_key, false)
}
}
impl Challenge<Dns> {
pub fn dns_proof(&self) -> String {
let acme_key = self.inner.transport.acme_key();
key_authorization(&self.api_challenge.token, acme_key, true)
}
}
impl Challenge<TlsAlpn> {
pub fn tls_alpn_proof(&self) -> [u8; 32] {
let acme_key = self.inner.transport.acme_key();
sha256(key_authorization(&self.api_challenge.token, acme_key, false).as_bytes())
}
}
impl<A> Challenge<A> {
fn new(inner: &Arc<AccountInner>, api_challenge: ApiChallenge, auth_url: &str) -> Self {
Challenge {
inner: inner.clone(),
api_challenge,
auth_url: auth_url.into(),
_ph: std::marker::PhantomData,
}
}
pub fn need_validate(&self) -> bool {
self.api_challenge.is_status_pending()
}
pub fn validate(self, delay_millis: u64) -> Result<()> {
let url_chall = &self.api_challenge.url;
let res = self.inner.transport.call(url_chall, &ApiEmptyObject)?;
let _: ApiChallenge = read_json(res)?;
let auth = wait_for_auth_status(&self.inner, &self.auth_url, delay_millis)?;
if !auth.is_status_valid() {
let error = auth
.challenges
.iter()
.filter_map(|c| c.error.as_ref())
.nth(0);
let reason = if let Some(error) = error {
format!(
"Failed: {}",
error.detail.clone().unwrap_or_else(|| error._type.clone())
)
} else {
"Validation failed and no error found".into()
};
bail!("Validation failed: {:?}", reason);
}
Ok(())
}
pub fn api_challenge(&self) -> &ApiChallenge {
&self.api_challenge
}
}
fn key_authorization(token: &str, key: &AcmeKey, extra_sha256: bool) -> String {
let jwk: Jwk = key.into();
let jwk_thumb: JwkThumb = (&jwk).into();
let jwk_json = serde_json::to_string(&jwk_thumb).expect("jwk_thumb");
let digest = base64url(&sha256(jwk_json.as_bytes()));
let key_auth = format!("{}.{}", token, digest);
if extra_sha256 {
base64url(&sha256(key_auth.as_bytes()))
} else {
key_auth
}
}
fn wait_for_auth_status(
inner: &Arc<AccountInner>,
auth_url: &str,
delay_millis: u64,
) -> Result<ApiAuth> {
let auth = loop {
let res = inner.transport.call(auth_url, &ApiEmptyString)?;
let auth: ApiAuth = read_json(res)?;
if !auth.is_status_pending() {
break auth;
}
thread::sleep(Duration::from_millis(delay_millis));
};
Ok(auth)
}
#[cfg(test)]
mod test {
use crate::*;
#[test]
fn test_get_challenges() -> Result<()> {
let server = crate::test::with_directory_server();
let url = DirectoryUrl::Other(&server.dir_url);
let dir = Directory::from_url(url)?;
let acc = dir.register_account(vec![
"mailto:foo@bar.com".to_string(),
])?;
let ord = acc.new_order("acmetest.example.com", &[])?;
let authz = ord.authorizations()?;
assert!(authz.len() == 1);
let auth = &authz[0];
{
let http = auth.http_challenge();
assert!(http.need_validate());
}
{
let dns = auth.dns_challenge();
assert!(dns.need_validate());
}
Ok(())
}
}