-
Notifications
You must be signed in to change notification settings - Fork 27
/
md_acme_order.c
562 lines (491 loc) · 19.2 KB
/
md_acme_order.c
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <assert.h>
#include <stdio.h>
#include <apr_lib.h>
#include <apr_buckets.h>
#include <apr_file_info.h>
#include <apr_file_io.h>
#include <apr_fnmatch.h>
#include <apr_hash.h>
#include <apr_strings.h>
#include <apr_tables.h>
#include "md.h"
#include "md_crypt.h"
#include "md_json.h"
#include "md_http.h"
#include "md_log.h"
#include "md_jws.h"
#include "md_result.h"
#include "md_store.h"
#include "md_util.h"
#include "md_acme.h"
#include "md_acme_authz.h"
#include "md_acme_order.h"
md_acme_order_t *md_acme_order_create(apr_pool_t *p)
{
md_acme_order_t *order;
order = apr_pcalloc(p, sizeof(*order));
order->p = p;
order->authz_urls = apr_array_make(p, 5, sizeof(const char *));
order->challenge_setups = apr_array_make(p, 5, sizeof(const char *));
return order;
}
/**************************************************************************************************/
/* order conversion */
#define MD_KEY_CHALLENGE_SETUPS "challenge-setups"
static md_acme_order_st order_st_from_str(const char *s)
{
if (s) {
if (!strcmp("valid", s)) {
return MD_ACME_ORDER_ST_VALID;
}
else if (!strcmp("invalid", s)) {
return MD_ACME_ORDER_ST_INVALID;
}
else if (!strcmp("ready", s)) {
return MD_ACME_ORDER_ST_READY;
}
else if (!strcmp("pending", s)) {
return MD_ACME_ORDER_ST_PENDING;
}
else if (!strcmp("processing", s)) {
return MD_ACME_ORDER_ST_PROCESSING;
}
}
return MD_ACME_ORDER_ST_PENDING;
}
static const char *order_st_to_str(md_acme_order_st status)
{
switch (status) {
case MD_ACME_ORDER_ST_PENDING:
return "pending";
case MD_ACME_ORDER_ST_READY:
return "ready";
case MD_ACME_ORDER_ST_PROCESSING:
return "processing";
case MD_ACME_ORDER_ST_VALID:
return "valid";
case MD_ACME_ORDER_ST_INVALID:
return "invalid";
default:
return "invalid";
}
}
md_json_t *md_acme_order_to_json(md_acme_order_t *order, apr_pool_t *p)
{
md_json_t *json = md_json_create(p);
if (order->url) {
md_json_sets(order->url, json, MD_KEY_URL, NULL);
}
md_json_sets(order_st_to_str(order->status), json, MD_KEY_STATUS, NULL);
md_json_setsa(order->authz_urls, json, MD_KEY_AUTHORIZATIONS, NULL);
md_json_setsa(order->challenge_setups, json, MD_KEY_CHALLENGE_SETUPS, NULL);
if (order->finalize) {
md_json_sets(order->finalize, json, MD_KEY_FINALIZE, NULL);
}
if (order->certificate) {
md_json_sets(order->certificate, json, MD_KEY_CERTIFICATE, NULL);
}
return json;
}
static void order_update_from_json(md_acme_order_t *order, md_json_t *json, apr_pool_t *p)
{
if (!order->url && md_json_has_key(json, MD_KEY_URL, NULL)) {
order->url = md_json_dups(p, json, MD_KEY_URL, NULL);
}
order->status = order_st_from_str(md_json_gets(json, MD_KEY_STATUS, NULL));
if (md_json_has_key(json, MD_KEY_AUTHORIZATIONS, NULL)) {
md_json_dupsa(order->authz_urls, p, json, MD_KEY_AUTHORIZATIONS, NULL);
}
if (md_json_has_key(json, MD_KEY_CHALLENGE_SETUPS, NULL)) {
md_json_dupsa(order->challenge_setups, p, json, MD_KEY_CHALLENGE_SETUPS, NULL);
}
if (md_json_has_key(json, MD_KEY_FINALIZE, NULL)) {
order->finalize = md_json_dups(p, json, MD_KEY_FINALIZE, NULL);
}
if (md_json_has_key(json, MD_KEY_CERTIFICATE, NULL)) {
order->certificate = md_json_dups(p, json, MD_KEY_CERTIFICATE, NULL);
}
}
md_acme_order_t *md_acme_order_from_json(md_json_t *json, apr_pool_t *p)
{
md_acme_order_t *order = md_acme_order_create(p);
order_update_from_json(order, json, p);
return order;
}
apr_status_t md_acme_order_add(md_acme_order_t *order, const char *authz_url)
{
assert(authz_url);
if (md_array_str_index(order->authz_urls, authz_url, 0, 1) < 0) {
APR_ARRAY_PUSH(order->authz_urls, const char*) = apr_pstrdup(order->p, authz_url);
}
return APR_SUCCESS;
}
apr_status_t md_acme_order_remove(md_acme_order_t *order, const char *authz_url)
{
int i;
assert(authz_url);
i = md_array_str_index(order->authz_urls, authz_url, 0, 1);
if (i >= 0) {
order->authz_urls = md_array_str_remove(order->p, order->authz_urls, authz_url, 1);
return APR_SUCCESS;
}
return APR_ENOENT;
}
static apr_status_t add_setup_token(md_acme_order_t *order, const char *token)
{
if (md_array_str_index(order->challenge_setups, token, 0, 1) < 0) {
APR_ARRAY_PUSH(order->challenge_setups, const char*) = apr_pstrdup(order->p, token);
}
return APR_SUCCESS;
}
/**************************************************************************************************/
/* persistence */
apr_status_t md_acme_order_load(struct md_store_t *store, md_store_group_t group,
const char *md_name, md_acme_order_t **pauthz_set,
apr_pool_t *p)
{
apr_status_t rv;
md_json_t *json;
md_acme_order_t *authz_set;
rv = md_store_load_json(store, group, md_name, MD_FN_ORDER, &json, p);
if (APR_SUCCESS == rv) {
authz_set = md_acme_order_from_json(json, p);
}
*pauthz_set = (APR_SUCCESS == rv)? authz_set : NULL;
return rv;
}
static apr_status_t p_save(void *baton, apr_pool_t *p, apr_pool_t *ptemp, va_list ap)
{
md_store_t *store = baton;
md_json_t *json;
md_store_group_t group;
md_acme_order_t *set;
const char *md_name;
int create;
(void)p;
group = (md_store_group_t)va_arg(ap, int);
md_name = va_arg(ap, const char *);
set = va_arg(ap, md_acme_order_t *);
create = va_arg(ap, int);
json = md_acme_order_to_json(set, ptemp);
assert(json);
return md_store_save_json(store, ptemp, group, md_name, MD_FN_ORDER, json, create);
}
apr_status_t md_acme_order_save(struct md_store_t *store, apr_pool_t *p,
md_store_group_t group, const char *md_name,
md_acme_order_t *authz_set, int create)
{
return md_util_pool_vdo(p_save, store, p, group, md_name, authz_set, create, NULL);
}
static apr_status_t p_purge(void *baton, apr_pool_t *p, apr_pool_t *ptemp, va_list ap)
{
md_store_t *store = baton;
md_acme_order_t *order;
md_store_group_t group;
const md_t *md;
const char *setup_token;
apr_table_t *env;
int i;
group = (md_store_group_t)va_arg(ap, int);
md = va_arg(ap, const md_t *);
env = va_arg(ap, apr_table_t *);
if (APR_SUCCESS == md_acme_order_load(store, group, md->name, &order, p)) {
md_log_perror(MD_LOG_MARK, MD_LOG_DEBUG, 0, p, "order loaded for %s", md->name);
for (i = 0; i < order->challenge_setups->nelts; ++i) {
setup_token = APR_ARRAY_IDX(order->challenge_setups, i, const char*);
if (setup_token) {
md_log_perror(MD_LOG_MARK, MD_LOG_DEBUG, 0, p,
"order teardown setup %s", setup_token);
md_acme_authz_teardown(store, setup_token, md, env, p);
}
}
}
return md_store_remove(store, group, md->name, MD_FN_ORDER, ptemp, 1);
}
apr_status_t md_acme_order_purge(md_store_t *store, apr_pool_t *p, md_store_group_t group,
const md_t *md, apr_table_t *env)
{
return md_util_pool_vdo(p_purge, store, p, group, md, env, NULL);
}
/**************************************************************************************************/
/* ACMEv2 order requests */
typedef struct {
apr_pool_t *p;
md_acme_order_t *order;
md_acme_t *acme;
const char *name;
apr_array_header_t *domains;
md_result_t *result;
} order_ctx_t;
#define ORDER_CTX_INIT(ctx, p, o, a, n, d, r) \
(ctx)->p = (p); (ctx)->order = (o); (ctx)->acme = (a); \
(ctx)->name = (n); (ctx)->domains = d; (ctx)->result = r
static apr_status_t identifier_to_json(void *value, md_json_t *json, apr_pool_t *p, void *baton)
{
md_json_t *jid;
(void)baton;
jid = md_json_create(p);
md_json_sets("dns", jid, "type", NULL);
md_json_sets(value, jid, "value", NULL);
return md_json_setj(jid, json, NULL);
}
static apr_status_t on_init_order_register(md_acme_req_t *req, void *baton)
{
order_ctx_t *ctx = baton;
md_json_t *jpayload;
jpayload = md_json_create(req->p);
md_json_seta(ctx->domains, identifier_to_json, NULL, jpayload, "identifiers", NULL);
return md_acme_req_body_init(req, jpayload);
}
static apr_status_t on_order_upd(md_acme_t *acme, apr_pool_t *p, const apr_table_t *hdrs,
md_json_t *body, void *baton)
{
order_ctx_t *ctx = baton;
const char *location = apr_table_get(hdrs, "location");
apr_status_t rv = APR_SUCCESS;
(void)acme;
(void)p;
if (!ctx->order) {
if (location) {
ctx->order = md_acme_order_create(ctx->p);
ctx->order->url = apr_pstrdup(ctx->p, location);
md_log_perror(MD_LOG_MARK, MD_LOG_TRACE1, rv, ctx->p, "new order at %s", location);
}
else {
rv = APR_EINVAL;
md_log_perror(MD_LOG_MARK, MD_LOG_WARNING, rv, ctx->p, "new order, no location header");
goto out;
}
}
order_update_from_json(ctx->order, body, ctx->p);
out:
return rv;
}
apr_status_t md_acme_order_register(md_acme_order_t **porder, md_acme_t *acme, apr_pool_t *p,
const char *name, apr_array_header_t *domains)
{
order_ctx_t ctx;
apr_status_t rv;
assert(MD_ACME_VERSION_MAJOR(acme->version) > 1);
ORDER_CTX_INIT(&ctx, p, NULL, acme, name, domains, NULL);
rv = md_acme_POST(acme, acme->api.v2.new_order, on_init_order_register, on_order_upd, NULL, NULL, &ctx);
*porder = (APR_SUCCESS == rv)? ctx.order : NULL;
return rv;
}
apr_status_t md_acme_order_update(md_acme_order_t *order, md_acme_t *acme,
md_result_t *result, apr_pool_t *p)
{
order_ctx_t ctx;
apr_status_t rv;
assert(MD_ACME_VERSION_MAJOR(acme->version) > 1);
ORDER_CTX_INIT(&ctx, p, order, acme, NULL, NULL, result);
rv = md_acme_GET(acme, order->url, NULL, on_order_upd, NULL, NULL, &ctx);
if (APR_SUCCESS != rv && APR_SUCCESS != acme->last->status) {
md_result_dup(result, acme->last);
}
return rv;
}
static apr_status_t await_ready(void *baton, int attempt)
{
order_ctx_t *ctx = baton;
apr_status_t rv = APR_SUCCESS;
(void)attempt;
if (APR_SUCCESS != (rv = md_acme_order_update(ctx->order, ctx->acme,
ctx->result, ctx->p))) goto out;
switch (ctx->order->status) {
case MD_ACME_ORDER_ST_READY:
case MD_ACME_ORDER_ST_PROCESSING:
case MD_ACME_ORDER_ST_VALID:
break;
case MD_ACME_ORDER_ST_PENDING:
rv = APR_EAGAIN;
break;
default:
rv = APR_EINVAL;
break;
}
out:
return rv;
}
apr_status_t md_acme_order_await_ready(md_acme_order_t *order, md_acme_t *acme,
const md_t *md, apr_interval_time_t timeout,
md_result_t *result, apr_pool_t *p)
{
order_ctx_t ctx;
apr_status_t rv;
assert(MD_ACME_VERSION_MAJOR(acme->version) > 1);
ORDER_CTX_INIT(&ctx, p, order, acme, md->name, NULL, result);
md_result_activity_setn(result, "Waiting for order to become ready");
rv = md_util_try(await_ready, &ctx, 0, timeout, 0, 0, 1);
md_result_log(result, MD_LOG_DEBUG);
return rv;
}
static apr_status_t await_valid(void *baton, int attempt)
{
order_ctx_t *ctx = baton;
apr_status_t rv = APR_SUCCESS;
(void)attempt;
if (APR_SUCCESS != (rv = md_acme_order_update(ctx->order, ctx->acme,
ctx->result, ctx->p))) goto out;
switch (ctx->order->status) {
case MD_ACME_ORDER_ST_VALID:
md_result_set(ctx->result, APR_EINVAL, "ACME server order status is 'valid'.");
break;
case MD_ACME_ORDER_ST_PROCESSING:
rv = APR_EAGAIN;
break;
case MD_ACME_ORDER_ST_INVALID:
md_result_set(ctx->result, APR_EINVAL, "ACME server order status is 'invalid'.");
rv = APR_EINVAL;
break;
default:
rv = APR_EINVAL;
break;
}
out:
return rv;
}
apr_status_t md_acme_order_await_valid(md_acme_order_t *order, md_acme_t *acme,
const md_t *md, apr_interval_time_t timeout,
md_result_t *result, apr_pool_t *p)
{
order_ctx_t ctx;
apr_status_t rv;
assert(MD_ACME_VERSION_MAJOR(acme->version) > 1);
ORDER_CTX_INIT(&ctx, p, order, acme, md->name, NULL, result);
md_result_activity_setn(result, "Waiting for finalized order to become valid");
rv = md_util_try(await_valid, &ctx, 0, timeout, 0, 0, 1);
md_result_log(result, MD_LOG_DEBUG);
return rv;
}
/**************************************************************************************************/
/* processing */
apr_status_t md_acme_order_start_challenges(md_acme_order_t *order, md_acme_t *acme,
apr_array_header_t *challenge_types,
md_store_t *store, const md_t *md,
apr_table_t *env, md_result_t *result,
apr_pool_t *p)
{
apr_status_t rv = APR_SUCCESS;
md_acme_authz_t *authz;
const char *url, *setup_token;
int i;
md_result_activity_printf(result, "Starting challenges for domains");
for (i = 0; i < order->authz_urls->nelts; ++i) {
url = APR_ARRAY_IDX(order->authz_urls, i, const char*);
md_log_perror(MD_LOG_MARK, MD_LOG_DEBUG, rv, p, "%s: check AUTHZ at %s", md->name, url);
if (APR_SUCCESS != (rv = md_acme_authz_retrieve(acme, p, url, &authz))) {
md_log_perror(MD_LOG_MARK, MD_LOG_DEBUG, rv, p, "%s: check authz for %s",
md->name, authz->domain);
goto leave;
}
switch (authz->state) {
case MD_ACME_AUTHZ_S_VALID:
break;
case MD_ACME_AUTHZ_S_PENDING:
md_log_perror(MD_LOG_MARK, MD_LOG_DEBUG, rv, p,
"%s: authorization pending for %s",
md->name, authz->domain);
rv = md_acme_authz_respond(authz, acme, store, challenge_types,
md->pks,
md->acme_tls_1_domains, md,
env, p, &setup_token, result);
if (APR_SUCCESS != rv) {
goto leave;
}
add_setup_token(order, setup_token);
md_acme_order_save(store, p, MD_SG_STAGING, md->name, order, 0);
break;
case MD_ACME_AUTHZ_S_INVALID:
rv = APR_EINVAL;
if (authz->error_type) {
md_result_problem_set(result, rv, authz->error_type, authz->error_detail, NULL);
goto leave;
}
/* fall through */
default:
rv = APR_EINVAL;
md_result_printf(result, rv, "unexpected AUTHZ state %d for domain %s",
authz->state, authz->domain);
md_result_log(result, MD_LOG_ERR);
goto leave;
}
}
leave:
return rv;
}
static apr_status_t check_challenges(void *baton, int attempt)
{
order_ctx_t *ctx = baton;
const char *url;
md_acme_authz_t *authz;
apr_status_t rv = APR_SUCCESS;
int i;
for (i = 0; i < ctx->order->authz_urls->nelts; ++i) {
url = APR_ARRAY_IDX(ctx->order->authz_urls, i, const char*);
md_log_perror(MD_LOG_MARK, MD_LOG_DEBUG, rv, ctx->p, "%s: check AUTHZ at %s (attempt %d)",
ctx->name, url, attempt);
rv = md_acme_authz_retrieve(ctx->acme, ctx->p, url, &authz);
if (APR_SUCCESS == rv) {
switch (authz->state) {
case MD_ACME_AUTHZ_S_VALID:
md_result_printf(ctx->result, rv,
"domain authorization for %s is valid", authz->domain);
break;
case MD_ACME_AUTHZ_S_PENDING:
rv = APR_EAGAIN;
md_log_perror(MD_LOG_MARK, MD_LOG_DEBUG, rv, ctx->p,
"%s: status pending at %s", authz->domain, authz->url);
goto leave;
case MD_ACME_AUTHZ_S_INVALID:
rv = APR_EINVAL;
md_result_printf(ctx->result, rv,
"domain authorization for %s failed, CA considers "
"answer to challenge invalid%s.",
authz->domain, authz->error_type? "" : ", no error given");
md_result_log(ctx->result, MD_LOG_ERR);
goto leave;
default:
rv = APR_EINVAL;
md_result_printf(ctx->result, rv,
"domain authorization for %s failed with state %d",
authz->domain, authz->state);
md_result_log(ctx->result, MD_LOG_ERR);
goto leave;
}
}
else {
md_result_printf(ctx->result, rv, "authorization retrieval failed for %s on <%s>",
ctx->name, url);
}
}
leave:
return rv;
}
apr_status_t md_acme_order_monitor_authzs(md_acme_order_t *order, md_acme_t *acme,
const md_t *md, apr_interval_time_t timeout,
md_result_t *result, apr_pool_t *p)
{
order_ctx_t ctx;
apr_status_t rv;
ORDER_CTX_INIT(&ctx, p, order, acme, md->name, NULL, result);
md_result_activity_printf(result, "Monitoring challenge status for %s", md->name);
rv = md_util_try(check_challenges, &ctx, 0, timeout, 0, 0, 1);
md_log_perror(MD_LOG_MARK, MD_LOG_DEBUG, rv, p, "%s: checked authorizations", md->name);
return rv;
}