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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
|
/* $OpenBSD: ca.c,v 1.39 2022/01/20 17:56:35 benno Exp $ */
/*
* Copyright (c) 2014 Reyk Floeter <reyk@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/queue.h>
#include <sys/uio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <poll.h>
#include <imsg.h>
#include <openssl/bio.h>
#include <openssl/pem.h>
#include <openssl/evp.h>
#include <openssl/rsa.h>
#include <openssl/engine.h>
#include "relayd.h"
void ca_init(struct privsep *, struct privsep_proc *p, void *);
void ca_launch(void);
int ca_dispatch_parent(int, struct privsep_proc *, struct imsg *);
int ca_dispatch_relay(int, struct privsep_proc *, struct imsg *);
int rsae_pub_enc(int, const u_char *, u_char *, RSA *, int);
int rsae_pub_dec(int,const u_char *, u_char *, RSA *, int);
int rsae_priv_enc(int, const u_char *, u_char *, RSA *, int);
int rsae_priv_dec(int, const u_char *, u_char *, RSA *, int);
int rsae_mod_exp(BIGNUM *, const BIGNUM *, RSA *, BN_CTX *);
int rsae_bn_mod_exp(BIGNUM *, const BIGNUM *, const BIGNUM *,
const BIGNUM *, BN_CTX *, BN_MONT_CTX *);
int rsae_init(RSA *);
int rsae_finish(RSA *);
int rsae_sign(int, const u_char *, u_int, u_char *, u_int *,
const RSA *);
int rsae_verify(int dtype, const u_char *m, u_int, const u_char *,
u_int, const RSA *);
int rsae_keygen(RSA *, int, BIGNUM *, BN_GENCB *);
static struct relayd *env = NULL;
static struct privsep_proc procs[] = {
{ "parent", PROC_PARENT, ca_dispatch_parent },
{ "relay", PROC_RELAY, ca_dispatch_relay },
};
void
ca(struct privsep *ps, struct privsep_proc *p)
{
env = ps->ps_env;
proc_run(ps, p, procs, nitems(procs), ca_init, NULL);
}
void
ca_init(struct privsep *ps, struct privsep_proc *p, void *arg)
{
if (pledge("stdio recvfd", NULL) == -1)
fatal("pledge");
if (config_init(ps->ps_env) == -1)
fatal("failed to initialize configuration");
env->sc_id = getpid() & 0xffff;
}
void
hash_x509(X509 *cert, char *hash, size_t hashlen)
{
static const char hex[] = "0123456789abcdef";
size_t off;
char digest[EVP_MAX_MD_SIZE];
int dlen, i;
if (X509_pubkey_digest(cert, EVP_sha256(), digest, &dlen) != 1)
fatalx("%s: X509_pubkey_digest failed", __func__);
if (hashlen < 2 * dlen + sizeof("SHA256:"))
fatalx("%s: hash buffer to small", __func__);
off = strlcpy(hash, "SHA256:", hashlen);
for (i = 0; i < dlen; i++) {
hash[off++] = hex[(digest[i] >> 4) & 0x0f];
hash[off++] = hex[digest[i] & 0x0f];
}
hash[off] = 0;
}
void
ca_launch(void)
{
char hash[TLS_CERT_HASH_SIZE];
char *buf;
BIO *in = NULL;
EVP_PKEY *pkey = NULL;
struct relay *rlay;
struct relay_cert *cert;
X509 *x509 = NULL;
off_t len;
TAILQ_FOREACH(cert, env->sc_certs, cert_entry) {
if (cert->cert_fd == -1 || cert->cert_key_fd == -1)
continue;
if ((buf = relay_load_fd(cert->cert_fd, &len)) == NULL)
fatal("ca_launch: cert relay_load_fd");
if ((in = BIO_new_mem_buf(buf, len)) == NULL)
fatalx("ca_launch: cert BIO_new_mem_buf");
if ((x509 = PEM_read_bio_X509(in, NULL,
NULL, NULL)) == NULL)
fatalx("ca_launch: cert PEM_read_bio_X509");
hash_x509(x509, hash, sizeof(hash));
BIO_free(in);
X509_free(x509);
purge_key(&buf, len);
if ((buf = relay_load_fd(cert->cert_key_fd, &len)) == NULL)
fatal("ca_launch: key relay_load_fd");
if ((in = BIO_new_mem_buf(buf, len)) == NULL)
fatalx("%s: key", __func__);
if ((pkey = PEM_read_bio_PrivateKey(in,
NULL, NULL, NULL)) == NULL)
fatalx("%s: PEM", __func__);
cert->cert_pkey = pkey;
if (pkey_add(env, pkey, hash) == NULL)
fatalx("tls pkey");
BIO_free(in);
purge_key(&buf, len);
}
TAILQ_FOREACH(rlay, env->sc_relays, rl_entry) {
if ((rlay->rl_conf.flags & (F_TLS|F_TLSCLIENT)) == 0)
continue;
if (rlay->rl_tls_cacert_fd != -1 &&
rlay->rl_conf.tls_cakey_len) {
if ((buf = relay_load_fd(rlay->rl_tls_cacert_fd,
&len)) == NULL)
fatal("ca_launch: cacert relay_load_fd");
if ((in = BIO_new_mem_buf(buf, len)) == NULL)
fatalx("ca_launch: cacert BIO_new_mem_buf");
if ((x509 = PEM_read_bio_X509(in, NULL,
NULL, NULL)) == NULL)
fatalx("ca_launch: cacert PEM_read_bio_X509");
hash_x509(x509, hash, sizeof(hash));
BIO_free(in);
X509_free(x509);
purge_key(&buf, len);
if ((in = BIO_new_mem_buf(rlay->rl_tls_cakey,
rlay->rl_conf.tls_cakey_len)) == NULL)
fatalx("%s: key", __func__);
if ((pkey = PEM_read_bio_PrivateKey(in,
NULL, NULL, NULL)) == NULL)
fatalx("%s: PEM", __func__);
BIO_free(in);
rlay->rl_tls_capkey = pkey;
if (pkey_add(env, pkey, hash) == NULL)
fatalx("ca pkey");
purge_key(&rlay->rl_tls_cakey,
rlay->rl_conf.tls_cakey_len);
}
close(rlay->rl_tls_ca_fd);
}
}
int
ca_dispatch_parent(int fd, struct privsep_proc *p, struct imsg *imsg)
{
switch (imsg->hdr.type) {
case IMSG_CFG_RELAY:
config_getrelay(env, imsg);
break;
case IMSG_CFG_RELAY_FD:
config_getrelayfd(env, imsg);
break;
case IMSG_CFG_DONE:
config_getcfg(env, imsg);
break;
case IMSG_CTL_START:
ca_launch();
break;
case IMSG_CTL_RESET:
config_getreset(env, imsg);
break;
default:
return -1;
}
return 0;
}
int
ca_dispatch_relay(int fd, struct privsep_proc *p, struct imsg *imsg)
{
struct ctl_keyop cko;
EVP_PKEY *pkey;
RSA *rsa;
u_char *from = NULL, *to = NULL;
struct iovec iov[2];
int c = 0;
switch (imsg->hdr.type) {
case IMSG_CA_PRIVENC:
case IMSG_CA_PRIVDEC:
IMSG_SIZE_CHECK(imsg, (&cko));
bcopy(imsg->data, &cko, sizeof(cko));
if (cko.cko_proc > env->sc_conf.prefork_relay)
fatalx("%s: invalid relay proc", __func__);
if (IMSG_DATA_SIZE(imsg) != (sizeof(cko) + cko.cko_flen))
fatalx("%s: invalid key operation", __func__);
if ((pkey = pkey_find(env, cko.cko_hash)) == NULL)
fatalx("%s: invalid relay hash '%s'",
__func__, cko.cko_hash);
if ((rsa = EVP_PKEY_get1_RSA(pkey)) == NULL)
fatalx("%s: invalid relay key", __func__);
DPRINTF("%s:%d: key hash %s proc %d",
__func__, __LINE__, cko.cko_hash, cko.cko_proc);
from = (u_char *)imsg->data + sizeof(cko);
if ((to = calloc(1, cko.cko_tlen)) == NULL)
fatalx("%s: calloc", __func__);
switch (imsg->hdr.type) {
case IMSG_CA_PRIVENC:
cko.cko_tlen = RSA_private_encrypt(cko.cko_flen,
from, to, rsa, cko.cko_padding);
break;
case IMSG_CA_PRIVDEC:
cko.cko_tlen = RSA_private_decrypt(cko.cko_flen,
from, to, rsa, cko.cko_padding);
break;
}
if (cko.cko_tlen == -1) {
char buf[256];
log_warnx("%s: %s", __func__,
ERR_error_string(ERR_get_error(), buf));
}
iov[c].iov_base = &cko;
iov[c++].iov_len = sizeof(cko);
if (cko.cko_tlen > 0) {
iov[c].iov_base = to;
iov[c++].iov_len = cko.cko_tlen;
}
if (proc_composev_imsg(env->sc_ps, PROC_RELAY, cko.cko_proc,
imsg->hdr.type, -1, -1, iov, c) == -1)
log_warn("%s: proc_composev_imsg", __func__);
free(to);
RSA_free(rsa);
break;
default:
return -1;
}
return 0;
}
/*
* RSA privsep engine (called from unprivileged processes)
*/
const RSA_METHOD *rsa_default = NULL;
static RSA_METHOD *rsae_method;
static int
rsae_send_imsg(int flen, const u_char *from, u_char *to, RSA *rsa,
int padding, u_int cmd)
{
struct privsep *ps = env->sc_ps;
struct pollfd pfd[1];
struct ctl_keyop cko;
int ret = 0;
char *hash;
struct iovec iov[2];
struct imsgbuf *ibuf;
struct imsgev *iev;
struct imsg imsg;
int n, done = 0, cnt = 0;
u_char *toptr;
static u_int seq = 0;
if ((hash = RSA_get_ex_data(rsa, 0)) == NULL)
return 0;
iev = proc_iev(ps, PROC_CA, ps->ps_instance);
ibuf = &iev->ibuf;
/*
* XXX this could be nicer...
*/
(void)strlcpy(cko.cko_hash, hash, sizeof(cko.cko_hash));
cko.cko_proc = ps->ps_instance;
cko.cko_flen = flen;
cko.cko_tlen = RSA_size(rsa);
cko.cko_padding = padding;
cko.cko_cookie = seq++;
iov[cnt].iov_base = &cko;
iov[cnt++].iov_len = sizeof(cko);
iov[cnt].iov_base = (void *)(uintptr_t)from;
iov[cnt++].iov_len = flen;
/*
* Send a synchronous imsg because we cannot defer the RSA
* operation in OpenSSL's engine layer.
*/
if (imsg_composev(ibuf, cmd, 0, 0, -1, iov, cnt) == -1)
log_warn("%s: imsg_composev", __func__);
if (imsg_flush(ibuf) == -1)
log_warn("%s: imsg_flush", __func__);
pfd[0].fd = ibuf->fd;
pfd[0].events = POLLIN;
while (!done) {
switch (poll(pfd, 1, RELAY_TLS_PRIV_TIMEOUT)) {
case -1:
if (errno != EINTR)
fatal("%s: poll", __func__);
continue;
case 0:
log_warnx("%s: priv%s poll timeout, keyop #%x",
__func__,
cmd == IMSG_CA_PRIVENC ? "enc" : "dec",
cko.cko_cookie);
return -1;
default:
break;
}
if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
fatalx("imsg_read");
if (n == 0)
fatalx("pipe closed");
while (!done) {
if ((n = imsg_get(ibuf, &imsg)) == -1)
fatalx("imsg_get error");
if (n == 0)
break;
IMSG_SIZE_CHECK(&imsg, (&cko));
memcpy(&cko, imsg.data, sizeof(cko));
/*
* Due to earlier timed out requests, there may be
* responses that need to be skipped.
*/
if (cko.cko_cookie != seq - 1) {
log_warnx(
"%s: priv%s obsolete keyop #%x", __func__,
cmd == IMSG_CA_PRIVENC ? "enc" : "dec",
cko.cko_cookie);
continue;
}
if (imsg.hdr.type != cmd)
fatalx("invalid response");
ret = cko.cko_tlen;
if (ret > 0) {
if (IMSG_DATA_SIZE(&imsg) !=
(sizeof(cko) + ret))
fatalx("data size");
toptr = (u_char *)imsg.data + sizeof(cko);
memcpy(to, toptr, ret);
}
done = 1;
imsg_free(&imsg);
}
}
imsg_event_add(iev);
return ret;
}
int
rsae_pub_enc(int flen,const u_char *from, u_char *to, RSA *rsa,int padding)
{
DPRINTF("%s:%d", __func__, __LINE__);
return RSA_meth_get_pub_enc(rsa_default)(flen, from, to, rsa, padding);
}
int
rsae_pub_dec(int flen,const u_char *from, u_char *to, RSA *rsa,int padding)
{
DPRINTF("%s:%d", __func__, __LINE__);
return RSA_meth_get_pub_dec(rsa_default)(flen, from, to, rsa, padding);
}
int
rsae_priv_enc(int flen, const u_char *from, u_char *to, RSA *rsa, int padding)
{
DPRINTF("%s:%d", __func__, __LINE__);
return rsae_send_imsg(flen, from, to, rsa, padding, IMSG_CA_PRIVENC);
}
int
rsae_priv_dec(int flen, const u_char *from, u_char *to, RSA *rsa, int padding)
{
DPRINTF("%s:%d", __func__, __LINE__);
return rsae_send_imsg(flen, from, to, rsa, padding, IMSG_CA_PRIVDEC);
}
int
rsae_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
{
DPRINTF("%s:%d", __func__, __LINE__);
return RSA_meth_get_mod_exp(rsa_default)(r0, I, rsa, ctx);
}
int
rsae_bn_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
{
DPRINTF("%s:%d", __func__, __LINE__);
return RSA_meth_get_bn_mod_exp(rsa_default)(r, a, p, m, ctx, m_ctx);
}
int
rsae_init(RSA *rsa)
{
DPRINTF("%s:%d", __func__, __LINE__);
if (RSA_meth_get_init(rsa_default) == NULL)
return 1;
return RSA_meth_get_init(rsa_default)(rsa);
}
int
rsae_finish(RSA *rsa)
{
DPRINTF("%s:%d", __func__, __LINE__);
if (RSA_meth_get_finish(rsa_default) == NULL)
return 1;
return RSA_meth_get_finish(rsa_default)(rsa);
}
int
rsae_sign(int type, const u_char *m, u_int m_length, u_char *sigret,
u_int *siglen, const RSA *rsa)
{
DPRINTF("%s:%d", __func__, __LINE__);
return RSA_meth_get_sign(rsa_default)(type, m, m_length,
sigret, siglen, rsa);
}
int
rsae_verify(int dtype, const u_char *m, u_int m_length, const u_char *sigbuf,
u_int siglen, const RSA *rsa)
{
DPRINTF("%s:%d", __func__, __LINE__);
return RSA_meth_get_verify(rsa_default)(dtype, m, m_length,
sigbuf, siglen, rsa);
}
int
rsae_keygen(RSA *rsa, int bits, BIGNUM *e, BN_GENCB *cb)
{
DPRINTF("%s:%d", __func__, __LINE__);
return RSA_meth_get_keygen(rsa_default)(rsa, bits, e, cb);
}
void
ca_engine_init(struct relayd *x_env)
{
ENGINE *e = NULL;
const char *errstr, *name;
if (env == NULL)
env = x_env;
if (rsa_default != NULL)
return;
if ((rsae_method = RSA_meth_new("RSA privsep engine", 0)) == NULL)
goto fail;
RSA_meth_set_pub_enc(rsae_method, rsae_pub_enc);
RSA_meth_set_pub_dec(rsae_method, rsae_pub_dec);
RSA_meth_set_priv_enc(rsae_method, rsae_priv_enc);
RSA_meth_set_priv_dec(rsae_method, rsae_priv_dec);
RSA_meth_set_mod_exp(rsae_method, rsae_mod_exp);
RSA_meth_set_bn_mod_exp(rsae_method, rsae_bn_mod_exp);
RSA_meth_set_init(rsae_method, rsae_init);
RSA_meth_set_finish(rsae_method, rsae_finish);
RSA_meth_set_sign(rsae_method, rsae_sign);
RSA_meth_set_verify(rsae_method, rsae_verify);
RSA_meth_set_keygen(rsae_method, rsae_keygen);
if ((e = ENGINE_get_default_RSA()) == NULL) {
if ((e = ENGINE_new()) == NULL) {
errstr = "ENGINE_new";
goto fail;
}
if (!ENGINE_set_name(e, RSA_meth_get0_name(rsae_method))) {
errstr = "ENGINE_set_name";
goto fail;
}
if ((rsa_default = RSA_get_default_method()) == NULL) {
errstr = "RSA_get_default_method";
goto fail;
}
} else if ((rsa_default = ENGINE_get_RSA(e)) == NULL) {
errstr = "ENGINE_get_RSA";
goto fail;
}
if ((name = ENGINE_get_name(e)) == NULL)
name = "unknown RSA engine";
log_debug("%s: using %s", __func__, name);
if (RSA_meth_get_flags(rsa_default) & RSA_FLAG_SIGN_VER)
fatalx("unsupported RSA engine");
if (RSA_meth_get_mod_exp(rsa_default) == NULL)
RSA_meth_set_mod_exp(rsae_method, NULL);
if (RSA_meth_get_bn_mod_exp(rsa_default) == NULL)
RSA_meth_set_bn_mod_exp(rsae_method, NULL);
if (RSA_meth_get_keygen(rsa_default) == NULL)
RSA_meth_set_keygen(rsae_method, NULL);
RSA_meth_set_flags(rsae_method,
RSA_meth_get_flags(rsa_default) | RSA_METHOD_FLAG_NO_CHECK);
RSA_meth_set0_app_data(rsae_method,
RSA_meth_get0_app_data(rsa_default));
if (!ENGINE_set_RSA(e, rsae_method)) {
errstr = "ENGINE_set_RSA";
goto fail;
}
if (!ENGINE_set_default_RSA(e)) {
errstr = "ENGINE_set_default_RSA";
goto fail;
}
return;
fail:
RSA_meth_free(rsae_method);
fatalx("%s: %s", __func__, errstr);
}
|