Blob


1 /*
2 * Copyright (c) 2023 Omar Polo <op@omarpolo.com>
3 * Copyright (c) 2014 Reyk Floeter <reyk@openbsd.org>
4 * Copyright (c) 2012 Gilles Chehade <gilles@poolp.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
19 #include "gmid.h"
21 #include <string.h>
23 #include <openssl/err.h>
24 #include <openssl/pem.h>
26 #include "log.h"
27 #include "proc.h"
29 #ifndef nitems
30 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
31 #endif
33 static void crypto_init(struct privsep *, struct privsep_proc *, void *);
34 static int crypto_dispatch_parent(int, struct privsep_proc *, struct imsg *);
35 static int crypto_dispatch_server(int, struct privsep_proc *, struct imsg *);
37 static struct privsep_proc procs[] = {
38 { "parent", PROC_PARENT, crypto_dispatch_parent },
39 { "server", PROC_SERVER, crypto_dispatch_server },
40 };
42 struct imsg_crypto_req {
43 uint64_t id;
44 char hash[TLS_CERT_HASH_SIZE];
45 size_t flen;
46 size_t tlen;
47 int padding;
48 /* followed by flen bytes of `from'. */
49 };
51 struct imsg_crypto_res {
52 uint64_t id;
53 int ret;
54 size_t len;
55 /* followed by len bytes of reply */
56 };
58 static uint64_t reqid;
59 static struct conf *conf;
61 void
62 crypto(struct privsep *ps, struct privsep_proc *p)
63 {
64 proc_run(ps, p, procs, nitems(procs), crypto_init, NULL);
65 }
67 static void
68 crypto_init(struct privsep *ps, struct privsep_proc *p, void *arg)
69 {
70 #if 0
71 static volatile int attached;
72 while (!attached) sleep(1);
73 #endif
75 conf = ps->ps_env;
77 sandbox_crypto_process();
78 }
80 static int
81 crypto_dispatch_parent(int fd, struct privsep_proc *p, struct imsg *imsg)
82 {
83 switch (imsg_get_type(imsg)) {
84 case IMSG_RECONF_START:
85 case IMSG_RECONF_CERT:
86 case IMSG_RECONF_KEY:
87 case IMSG_RECONF_END:
88 if (config_recv(conf, imsg) == -1)
89 return -1;
90 break;
91 default:
92 return -1;
93 }
95 return 0;
96 }
98 static EVP_PKEY *
99 get_pkey(const char *hash)
101 struct pki *pki;
103 TAILQ_FOREACH(pki, &conf->pkis, pkis) {
104 if (!strcmp(pki->hash, hash))
105 return pki->pkey;
108 return NULL;
111 static int
112 crypto_dispatch_server(int fd, struct privsep_proc *p, struct imsg *imsg)
114 struct privsep *ps = p->p_ps;
115 RSA *rsa = NULL;
116 EC_KEY *ecdsa = NULL;
117 EVP_PKEY *pkey;
118 struct imsg_crypto_req req;
119 struct imsg_crypto_res res;
120 struct ibuf ibuf;
121 struct iovec iov[2];
122 const void *from;
123 unsigned char *to;
124 int n, ret, type;
125 unsigned int len;
126 pid_t pid;
128 if (imsg_get_ibuf(imsg, &ibuf) == -1)
129 fatalx("%s: couldn't get an ibuf", __func__);
131 pid = imsg_get_pid(imsg);
132 switch (type = imsg_get_type(imsg)) {
133 case IMSG_CRYPTO_RSA_PRIVENC:
134 case IMSG_CRYPTO_RSA_PRIVDEC:
135 if (ibuf_get(&ibuf, &req, sizeof(req)) == -1 ||
136 ibuf_size(&ibuf) != req.flen)
137 fatalx("size mismatch for imsg %d", type);
138 from = ibuf_data(&ibuf);
140 if ((pkey = get_pkey(req.hash)) == NULL ||
141 (rsa = EVP_PKEY_get1_RSA(pkey)) == NULL)
142 fatalx("invalid pkey hash");
144 if ((to = calloc(1, req.tlen)) == NULL)
145 fatal("calloc");
147 if (type == IMSG_CRYPTO_RSA_PRIVENC)
148 ret = RSA_private_encrypt(req.flen, from,
149 to, rsa, req.padding);
150 else
151 ret = RSA_private_decrypt(req.flen, from,
152 to, rsa, req.padding);
154 memset(&res, 0, sizeof(res));
155 res.id = req.id;
156 res.ret = ret;
158 memset(&iov, 0, sizeof(iov));
159 n = 0;
160 iov[n].iov_base = &res;
161 iov[n].iov_len = sizeof(res);
162 n++;
164 if (ret > 0) {
165 res.len = ret;
166 iov[n].iov_base = to;
167 iov[n].iov_len = ret;
168 n++;
171 log_debug("replying to server #%d", pid);
172 if (proc_composev_imsg(ps, PROC_SERVER, pid - 1,
173 type, 0, -1, iov, n) == -1)
174 fatal("proc_composev_imsg");
176 if (proc_flush_imsg(ps, PROC_SERVER, pid - 1) == -1)
177 fatal("proc_flush_imsg");
179 free(to);
180 RSA_free(rsa);
181 break;
183 case IMSG_CRYPTO_ECDSA_SIGN:
184 if (ibuf_get(&ibuf, &req, sizeof(req)) == -1 ||
185 ibuf_size(&ibuf) != req.flen)
186 fatalx("size mismatch for imsg %d", type);
187 from = ibuf_data(&ibuf);
189 if ((pkey = get_pkey(req.hash)) == NULL ||
190 (ecdsa = EVP_PKEY_get1_EC_KEY(pkey)) == NULL)
191 fatalx("invalid pkey hash");
193 len = ECDSA_size(ecdsa);
194 if ((to = calloc(1, len)) == NULL)
195 fatal("calloc");
196 ret = ECDSA_sign(0, from, req.flen, to, &len, ecdsa);
198 memset(&res, 0, sizeof(res));
199 res.id = req.id;
200 res.ret = ret;
202 memset(&iov, 0, sizeof(iov));
203 n = 0;
204 iov[0].iov_base = &res;
205 iov[0].iov_len = sizeof(res);
206 n++;
208 if (ret > 0) {
209 res.len = len;
210 iov[n].iov_base = to;
211 iov[n].iov_len = len;
212 n++;
215 log_debug("replying to server #%d", pid);
216 if (proc_composev_imsg(ps, PROC_SERVER, pid - 1,
217 type, 0, -1, iov, n) == -1)
218 fatal("proc_composev_imsg");
220 if (proc_flush_imsg(ps, PROC_SERVER, pid - 1) == -1)
221 fatal("proc_flush_imsg");
223 free(to);
224 EC_KEY_free(ecdsa);
225 break;
227 default:
228 return -1;
231 return 0;
235 /*
236 * RSA privsep engine (called from unprivileged processes)
237 */
239 static const RSA_METHOD *rsa_default;
240 static RSA_METHOD *rsae_method;
242 static int
243 rsae_send_imsg(int flen, const unsigned char *from, unsigned char *to,
244 RSA *rsa, int padding, unsigned int cmd)
246 struct imsg_crypto_req req;
247 struct iovec iov[2];
248 struct imsg_crypto_res res;
249 struct imsgev *iev;
250 struct privsep_proc *p;
251 struct privsep *ps = conf->ps;
252 struct imsgbuf *imsgbuf;
253 struct imsg imsg;
254 struct ibuf ibuf;
255 int ret = 0;
256 int n, done = 0;
257 const void *toptr;
258 char *hash;
260 if ((hash = RSA_get_ex_data(rsa, 0)) == NULL)
261 return (0);
263 /*
264 * Send a synchronous imsg because we cannot defer the RSA
265 * operation in OpenSSL's engine layer.
266 */
267 memset(&req, 0, sizeof(req));
268 req.id = ++reqid;
269 if (strlcpy(req.hash, hash, sizeof(req.hash)) >= sizeof(req.hash))
270 fatalx("%s: hash too long (%zu)", __func__, strlen(hash));
271 req.flen = flen;
272 req.tlen = RSA_size(rsa);
273 req.padding = padding;
275 memset(&iov, 0, sizeof(iov));
276 iov[0].iov_base = &req;
277 iov[0].iov_len = sizeof(req);
278 iov[1].iov_base = (void *)from;
279 iov[1].iov_len = flen;
281 if (proc_composev(ps, PROC_CRYPTO, cmd, iov, 2) == -1)
282 fatal("proc_composev");
284 if (proc_flush_imsg(ps, PROC_CRYPTO, -1) == -1)
285 fatal("proc_flush_imsg");
287 iev = ps->ps_ievs[PROC_CRYPTO];
288 p = iev->proc;
289 imsgbuf = &iev->ibuf;
291 while (!done) {
292 if ((n = imsg_read(imsgbuf)) == -1 && errno != EAGAIN)
293 fatalx("imsg_read");
294 if (n == 0)
295 fatalx("pipe closed");
297 while (!done) {
298 if ((n = imsg_get(imsgbuf, &imsg)) == -1)
299 fatalx("imsg_get error");
300 if (n == 0)
301 break;
303 #if DEBUG > 1
304 log_debug(
305 "%s: %s %d got imsg %d id %d from %s %d",
306 __func__, title, 1, imsg_get_type(&imsg),
307 imsg_get_id(&imsg), "crypto", imsg_get_pid(&imsg));
308 #endif
310 if ((p->p_cb)(imsgbuf->fd, p, &imsg) == 0) {
311 /* Message was handled by the callback */
312 imsg_free(&imsg);
313 continue;
316 switch (imsg_get_type(&imsg)) {
317 case IMSG_CRYPTO_RSA_PRIVENC:
318 case IMSG_CRYPTO_RSA_PRIVDEC:
319 break;
320 default:
321 fatalx("%s: %s %d got invalid imsg %d"
322 " id %d from %s %d",
323 __func__, "server", ps->ps_instance + 1,
324 imsg_get_type(&imsg), imsg_get_id(&imsg),
325 "crypto", imsg_get_pid(&imsg));
328 if (imsg_get_ibuf(&imsg, &ibuf) == -1 ||
329 ibuf_get(&ibuf, &res, sizeof(res)) == -1 ||
330 (int)ibuf_size(&ibuf) != res.ret)
331 fatalx("size mismatch for imsg %d",
332 imsg.hdr.type);
333 ret = res.ret;
334 toptr = ibuf_data(&ibuf);
336 if (res.id != reqid)
337 fatalx("invalid id; got %llu, want %llu",
338 (unsigned long long)res.id,
339 (unsigned long long)reqid);
340 if (res.ret > 0)
341 memcpy(to, toptr, res.len);
343 done = 1;
345 imsg_free(&imsg);
348 imsg_event_add(iev);
350 return (ret);
353 static int
354 rsae_priv_enc(int flen, const unsigned char *from, unsigned char *to, RSA *rsa,
355 int padding)
357 log_debug("debug: %s", __func__);
358 if (RSA_get_ex_data(rsa, 0) != NULL)
359 return (rsae_send_imsg(flen, from, to, rsa, padding,
360 IMSG_CRYPTO_RSA_PRIVENC));
361 return (RSA_meth_get_priv_enc(rsa_default)(flen, from, to, rsa, padding));
364 static int
365 rsae_priv_dec(int flen, const unsigned char *from, unsigned char *to, RSA *rsa,
366 int padding)
368 log_debug("debug: %s", __func__);
369 if (RSA_get_ex_data(rsa, 0) != NULL)
370 return (rsae_send_imsg(flen, from, to, rsa, padding,
371 IMSG_CRYPTO_RSA_PRIVDEC));
373 return (RSA_meth_get_priv_dec(rsa_default)(flen, from, to, rsa, padding));
377 /*
378 * ECDSA privsep engine (called from unprivileged processes)
379 */
381 static const EC_KEY_METHOD *ecdsa_default;
382 static EC_KEY_METHOD *ecdsae_method;
384 static ECDSA_SIG *
385 ecdsae_send_enc_imsg(const unsigned char *dgst, int dgst_len,
386 const BIGNUM *inv, const BIGNUM *rp, EC_KEY *eckey)
388 ECDSA_SIG *sig = NULL;
389 struct imsg_crypto_req req;
390 struct iovec iov[2];
391 struct imsg_crypto_res res;
392 struct imsgev *iev;
393 struct privsep_proc *p;
394 struct privsep *ps = conf->ps;
395 struct imsgbuf *imsgbuf;
396 struct imsg imsg;
397 struct ibuf ibuf;
398 int n, done = 0;
399 const void *toptr;
400 char *hash;
402 if ((hash = EC_KEY_get_ex_data(eckey, 0)) == NULL)
403 return (0);
405 /*
406 * Send a synchronous imsg because we cannot defer the RSA
407 * operation in OpenSSL's engine layer.
408 */
409 memset(&req, 0, sizeof(req));
410 req.id = ++reqid;
411 if (strlcpy(req.hash, hash, sizeof(req.hash)) >= sizeof(req.hash))
412 fatalx("%s: hash too long (%zu)", __func__, strlen(hash));
413 req.flen = dgst_len;
415 memset(&iov, 0, sizeof(iov));
416 iov[0].iov_base = &req;
417 iov[0].iov_len = sizeof(req);
418 iov[1].iov_base = (void *)dgst;
419 iov[1].iov_len = dgst_len;
421 if (proc_composev(ps, PROC_CRYPTO, IMSG_CRYPTO_ECDSA_SIGN, iov, 2) == -1)
422 fatal("proc_composev");
424 if (proc_flush_imsg(ps, PROC_CRYPTO, -1) == -1)
425 fatal("proc_flush_imsg");
427 iev = ps->ps_ievs[PROC_CRYPTO];
428 p = iev->proc;
429 imsgbuf = &iev->ibuf;
431 while (!done) {
432 if ((n = imsg_read(imsgbuf)) == -1 && errno != EAGAIN)
433 fatalx("imsg_read");
434 if (n == 0)
435 fatalx("pipe closed");
437 while (!done) {
438 if ((n = imsg_get(imsgbuf, &imsg)) == -1)
439 fatalx("imsg_get error");
440 if (n == 0)
441 break;
443 #if DEBUG > 1
444 log_debug(
445 "%s: %s %d got imsg %d peerid %d from %s %d",
446 __func__, title, 1, imsg.hdr.type,
447 imsg.hdr.peerid, "crypto", imsg.hdr.pid);
448 #endif
450 if (imsg.hdr.type != IMSG_CRYPTO_ECDSA_SIGN &&
451 crypto_dispatch_server(imsgbuf->fd, p, &imsg)
452 == 0) {
453 /* Message was handled by the callback */
454 imsg_free(&imsg);
455 continue;
458 if (imsg.hdr.type != IMSG_CRYPTO_ECDSA_SIGN)
459 fatalx("%s: %s %d got invalid imsg %d"
460 " peerid %d from %s %d",
461 __func__, "server", ps->ps_instance + 1,
462 imsg.hdr.type, imsg.hdr.peerid,
463 "crypto", imsg.hdr.pid);
465 if (imsg_get_ibuf(&imsg, &ibuf) == -1 ||
466 ibuf_get(&ibuf, &res, sizeof(res)) == -1 ||
467 ibuf_size(&ibuf) != res.len)
468 fatalx("size mismatch for imsg %d",
469 imsg.hdr.type);
471 toptr = ibuf_data(&ibuf);
473 if (res.id != reqid)
474 fatalx("invalid response id");
475 if (res.ret > 0) {
476 d2i_ECDSA_SIG(&sig,
477 (const unsigned char **)&toptr, res.len);
480 done = 1;
482 imsg_free(&imsg);
485 imsg_event_add(iev);
487 return (sig);
490 static ECDSA_SIG *
491 ecdsae_do_sign(const unsigned char *dgst, int dgst_len, const BIGNUM *inv,
492 const BIGNUM *rp, EC_KEY *eckey)
494 ECDSA_SIG *(*psign_sig)(const unsigned char *, int, const BIGNUM *,
495 const BIGNUM *, EC_KEY *);
497 log_debug("debug: %s", __func__);
498 if (EC_KEY_get_ex_data(eckey, 0) != NULL)
499 return (ecdsae_send_enc_imsg(dgst, dgst_len, inv, rp, eckey));
500 EC_KEY_METHOD_get_sign(ecdsa_default, NULL, NULL, &psign_sig);
501 return (psign_sig(dgst, dgst_len, inv, rp, eckey));
505 /*
506 * Initialize the two engines.
507 */
509 static void
510 rsa_engine_init(void)
512 const char *errstr;
514 if ((rsa_default = RSA_get_default_method()) == NULL) {
515 errstr = "RSA_get_default_method";
516 goto fail;
519 if ((rsae_method = RSA_meth_dup(rsa_default)) == NULL) {
520 errstr = "RSA_meth_dup";
521 goto fail;
524 RSA_meth_set_priv_enc(rsae_method, rsae_priv_enc);
525 RSA_meth_set_priv_dec(rsae_method, rsae_priv_dec);
527 RSA_meth_set_flags(rsae_method,
528 RSA_meth_get_flags(rsa_default) | RSA_METHOD_FLAG_NO_CHECK);
529 RSA_meth_set0_app_data(rsae_method,
530 RSA_meth_get0_app_data(rsa_default));
532 RSA_set_default_method(rsae_method);
534 return;
536 fail:
537 ssl_error(errstr);
538 fatalx("%s", errstr);
541 static void
542 ecdsa_engine_init(void)
544 int (*sign)(int, const unsigned char *, int, unsigned char *,
545 unsigned int *, const BIGNUM *, const BIGNUM *, EC_KEY *);
546 int (*sign_setup)(EC_KEY *, BN_CTX *, BIGNUM **, BIGNUM **);
547 const char *errstr;
549 if ((ecdsa_default = EC_KEY_get_default_method()) == NULL) {
550 errstr = "EC_KEY_get_default_method";
551 goto fail;
554 if ((ecdsae_method = EC_KEY_METHOD_new(ecdsa_default)) == NULL) {
555 errstr = "EC_KEY_METHOD_new";
556 goto fail;
559 EC_KEY_METHOD_get_sign(ecdsa_default, &sign, &sign_setup, NULL);
560 EC_KEY_METHOD_set_sign(ecdsae_method, sign, sign_setup,
561 ecdsae_do_sign);
563 EC_KEY_set_default_method(ecdsae_method);
565 return;
567 fail:
568 ssl_error(errstr);
569 fatalx("%s", errstr);
572 void
573 crypto_engine_init(struct conf *c)
575 conf = c;
577 rsa_engine_init();
578 ecdsa_engine_init();