-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathngx_http_est.c
459 lines (371 loc) · 13.8 KB
/
ngx_http_est.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
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <ngx_http_ssl_module.h>
#include <openssl/asn1.h>
#include <openssl/bio.h>
#include <openssl/objects.h>
#include <openssl/pem.h>
#include <openssl/x509.h>
#include "ngx_http_est.h"
static char * ngx_http_est_command_csr_attrs(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static char * ngx_http_est_command_enable(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static char * ngx_http_est_command_pop(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static char * ngx_http_est_command_root_certificate(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static void * ngx_http_est_create_loc_conf(ngx_conf_t *cf);
static ngx_int_t ngx_http_est_match_attribute(ngx_http_est_loc_conf_t *lcf, ngx_str_t *str);
static char * ngx_http_est_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child);
static ngx_int_t ngx_http_est_initialise(ngx_conf_t *cf);
ngx_http_est_dispatch_t ngx_http_est_dispatch[] = {
/* 4.1.2 CA Certificates Request */
{ ngx_string("cacerts"),
NGX_HTTP_GET,
0,
ngx_http_est_request_cacerts },
/* 4.2.1 Simple Enrollment of Clients */
{ ngx_string("simpleenroll"),
NGX_HTTP_POST,
1,
ngx_http_est_request_simple_request },
/* 4.2.2 Simple Re-enrollment of Clients */
{ ngx_string("simplereenroll"),
NGX_HTTP_POST,
1,
ngx_http_est_request_simple_request },
/* 4.3.1 Full CMC Request */
{ ngx_string("fullcmc"),
NGX_HTTP_POST,
1,
ngx_http_est_request_not_implemented },
/* 4.4.1 Server-Side Key Generation Request */
{ ngx_string("serverkeygen"),
NGX_HTTP_POST,
1,
ngx_http_est_request_simple_request },
/* 4.5.1 CSR Attributes Request */
{ ngx_string("csrattrs"),
NGX_HTTP_GET,
0,
ngx_http_est_request_csrattrs },
{ ngx_string(""), 0, 0, NULL }
};
static ngx_command_t ngx_http_est_commands[] = {
/* Directives associated with EST operations */
{ ngx_string("est"),
NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
ngx_http_est_command_enable,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_est_loc_conf_t, enable),
NULL },
{ ngx_string("est_auth_request"),
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_est_loc_conf_t, auth_request),
NULL },
{ ngx_string("est_csr_attrs"),
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_http_est_command_csr_attrs,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_est_loc_conf_t, csr_attrs),
NULL },
{ ngx_string("est_pop"),
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_http_est_command_pop,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_est_loc_conf_t, pop),
NULL },
{ ngx_string("est_root_certificate"),
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_http_est_command_root_certificate,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_est_loc_conf_t, root_certificate),
NULL },
/* Directives associated with CA operations */
{ ngx_string("est_ca_private_key"),
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_est_loc_conf_t, ca_private_key),
NULL },
{ ngx_string("est_ca_root_certificate"),
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_est_loc_conf_t, ca_root_certificate),
NULL },
{ ngx_string("est_ca_serial_number"),
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_est_loc_conf_t, ca_serial_number),
NULL },
{ ngx_string("est_ca_validity_days"),
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_est_loc_conf_t, ca_validity_days),
NULL },
ngx_null_command
};
static ngx_http_module_t ngx_http_est_module_ctx = {
NULL, /* preconfiguration */
ngx_http_est_initialise, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
ngx_http_est_create_loc_conf, /* create location configuration */
ngx_http_est_merge_loc_conf /* merge location configuration */
};
ngx_module_t ngx_http_est_module = {
NGX_MODULE_V1,
&ngx_http_est_module_ctx, /* module context */
ngx_http_est_commands, /* module directives */
NGX_HTTP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
static char *
ngx_http_est_command_csr_attrs(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
ngx_http_est_loc_conf_t *lcf = conf;
BIO *in;
BUF_MEM *buf;
size_t length;
char *pp, *rv;
int ret;
/* assert(lcf != NULL); */
rv = ngx_conf_set_str_slot(cf, cmd, conf);
if (rv != NGX_CONF_OK) {
return rv;
}
/*
The following code attempts to parse and cache the ASN.1 sequence specified
in the est_csr_attrs directive. This information will be used for both the
validation of attributes in CSRs received from clients and when responding
to requests for a list of such attributes ("/csrattrs"). As such, this
information is cached in both as a buffer of Distinguished Encoding Rules
(DER) bytes as read from the file and returned to CSR attributes operations
("/csrattrs") and as an array of parsed attributes for validating
certificate requests.
*/
rv = NGX_CONF_ERROR;
in = NULL;
buf = NULL;
if ((in = BIO_new_file((char *) lcf->csr_attrs.data, "rb")) == NULL) {
goto error;
}
if ((buf = BUF_MEM_new()) == NULL) {
goto error;
}
for (length = 0;;) {
if (!BUF_MEM_grow(buf, BUFSIZ + length)) {
goto error;
}
ret = BIO_read(in, &(buf->data[length]), BUFSIZ);
if (ret <= 0) {
break;
}
length += ret;
}
if (length > 0) {
if (lcf->attributes == NULL) {
lcf->attributes = ngx_array_create(cf->pool, 8, sizeof(ngx_str_t));
if (lcf->attributes == NULL) {
goto error;
}
}
pp = buf->data;
if (ngx_http_est_asn1_parse(lcf->attributes, (const unsigned char **) &pp, length, 0) != 0) {
ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
"%s: error reading certificate attributes: \"%*s\"",
MODULE_NAME,
lcf->csr_attrs.len,
lcf->csr_attrs.data);
goto error;
}
}
rv = NGX_CONF_OK;
error:
BIO_free(in);
BUF_MEM_free(buf);
return rv;
}
static char *
ngx_http_est_command_enable(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
ngx_http_est_loc_conf_t *lcf = conf;
ngx_http_core_loc_conf_t *clcf;
char *rv;
/* assert(lcf != NULL); */
rv = ngx_conf_set_flag_slot(cf, cmd, conf);
if (rv != NGX_CONF_OK) {
return rv;
}
if (lcf->enable) {
clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
clcf->handler = ngx_http_est_request;
}
return NGX_CONF_OK;
}
static char *
ngx_http_est_command_pop(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
ngx_http_est_loc_conf_t *lcf = conf;
ngx_str_t cp = ngx_string("challengePassword");
ngx_str_t *attribute;
char *rv;
/* assert((obj = OBJ_nid2obj(NID_pkcs9_challengePassword)) != NULL); */
/* assert(OBJ_obj2txt(buf, sizeof(buf), obj, 0) > 0); */
/* assert(lcf != NULL); */
rv = ngx_conf_set_flag_slot(cf, cmd, conf);
if (rv != NGX_CONF_OK) {
return rv;
}
if (lcf->pop) {
/*
When proof-of-possession is configured for the EST server, the object
identifier for the challengePassword is included within the list of
attributes mandated for certificate signing requests (CSR). Before this
object identifier is added to this list of attributes however, a check is
made to ensure that this object identifier is not already included.
*/
rv = NGX_CONF_ERROR;
/* assert(ngx_http_est_match_attribute(lcf, &attr) >= 0); */
if (ngx_http_est_match_attribute(lcf, &cp) == 0) {
attribute = ngx_array_push(lcf->attributes);
if (attribute == NULL) {
goto error;
}
attribute->len = cp.len + 1;
attribute->data = ngx_pnalloc(cf->pool, attribute->len);
if (attribute->data == NULL) {
goto error;
}
(void) ngx_copy(attribute->data, cp.data, cp.len);
}
}
rv = NGX_CONF_OK;
error:
return rv;
}
static char *
ngx_http_est_command_root_certificate(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
ngx_http_est_loc_conf_t *lcf = conf;
BIO *bp;
char path[PATH_MAX], *rv;;
/* assert(lcf != NULL); */
rv = ngx_conf_set_str_slot(cf, cmd, conf);
if (rv != NGX_CONF_OK) {
return rv;
}
/*
The following code attempts to cache the root certificate specified in the
est_ca_root_certificate directive such that this can be more readily used
for certificate retrieval, enrollment and reenrollment requests.
*/
bp = NULL;
/* ngx_memzero(path, sizeof(path)); */
ngx_snprintf((u_char *)path, sizeof(path),
"%*s",
lcf->root_certificate.len,
lcf->root_certificate.data);
path[lcf->root_certificate.len] = '\0';
if (ngx_strlen(path) == 0) {
goto error;
}
if (((bp = BIO_new_file(path, "r")) == NULL) ||
((lcf->ca_root = ngx_http_est_pkcs7(bp)) == NULL)) {
ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
"%s: error opening root certificate: \"%*s\"",
MODULE_NAME,
lcf->root_certificate.len,
lcf->root_certificate.data);
goto error;
}
rv = NGX_CONF_OK;
error:
BIO_free(bp);
return rv;
}
static void *
ngx_http_est_create_loc_conf(ngx_conf_t *cf) {
ngx_http_est_loc_conf_t *lcf;
lcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_est_loc_conf_t));
if (lcf == NULL) {
return NULL;
}
lcf->cf = cf;
/*
set by ngx_pcalloc():
lcf->auth_request = { 0, NULL };
lcf->ca_private_key = { 0, NULL };
lcf->ca_root_certificate = { 0, NULL };
lcf->ca_serial_number = { 0, NULL };
lcf->csr_attrs = { 0, NULL };
lcf->root_certificate = { 0, NULL };
*/
lcf->attributes = NULL;
lcf->ca_key = NGX_CONF_UNSET_PTR;
lcf->ca_root = NGX_CONF_UNSET_PTR;
lcf->ca_validity_days = NGX_CONF_UNSET;
lcf->ca_x509 = NGX_CONF_UNSET_PTR;
lcf->enable = NGX_CONF_UNSET;
lcf->pop = NGX_CONF_UNSET;
return lcf;
}
static ngx_int_t
ngx_http_est_match_attribute(ngx_http_est_loc_conf_t *lcf, ngx_str_t *str) {
ngx_uint_t i;
if (lcf->attributes == NULL) {
if ((lcf->attributes = ngx_array_create(lcf->cf->pool, 8, sizeof(ngx_str_t))) == NULL) {
return -1;
}
}
for (i = 0; i < lcf->attributes->nelts; ++i) {
if (ngx_strcmp(((ngx_str_t *) lcf->attributes->elts)[i].data, str->data) == 0) {
return 1;
}
}
return 0;
}
static char *
ngx_http_est_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) {
ngx_http_est_loc_conf_t *prev = parent;
ngx_http_est_loc_conf_t *conf = child;
ngx_conf_merge_ptr_value(conf->attributes, prev->attributes, NULL);
ngx_conf_merge_str_value(conf->auth_request, prev->auth_request, "");
ngx_conf_merge_ptr_value(conf->ca_key, prev->ca_key, NULL);
ngx_conf_merge_str_value(conf->ca_private_key, prev->ca_private_key, "");
ngx_conf_merge_ptr_value(conf->ca_root, prev->ca_root, NULL);
ngx_conf_merge_str_value(conf->ca_root_certificate, prev->ca_root_certificate, "");
ngx_conf_merge_str_value(conf->ca_serial_number, prev->ca_serial_number, "");
ngx_conf_merge_value(conf->ca_validity_days, prev->ca_validity_days, 30);
ngx_conf_merge_ptr_value(conf->ca_x509, prev->ca_x509, NULL);
ngx_conf_merge_str_value(conf->csr_attrs, prev->csr_attrs, "");
ngx_conf_merge_value(conf->enable, prev->enable, 0);
ngx_conf_merge_value(conf->pop, prev->pop, 0);
ngx_conf_merge_str_value(conf->root_certificate, prev->root_certificate, "");
return NGX_CONF_OK;
}
static ngx_int_t
ngx_http_est_initialise(ngx_conf_t *cf) {
ngx_http_core_main_conf_t *cmcf;
ngx_http_handler_pt *h;
/*
The following installs this module as an access handler for HTTP requests.
This will be used to selectively perform HTTP authorization checks depending
upon the client verification configuration in place for a given location.
*/
cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
h = ngx_array_push(&cmcf->phases[NGX_HTTP_ACCESS_PHASE].handlers);
if (h == NULL) {
return NGX_ERROR;
}
*h = ngx_http_est_auth;
return NGX_OK;
}