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->hdr.type) {
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 iovec iov[2];
121 const void *from;
122 unsigned char *data, *to;
123 size_t datalen;
124 int n, ret;
125 unsigned int len;
127 data = imsg->data;
128 datalen = IMSG_DATA_SIZE(imsg);
130 switch (imsg->hdr.type) {
131 case IMSG_CRYPTO_RSA_PRIVENC:
132 case IMSG_CRYPTO_RSA_PRIVDEC:
133 if (datalen < sizeof(req))
134 fatalx("size mismatch for imsg %d", imsg->hdr.type);
135 memcpy(&req, data, sizeof(req));
136 if (datalen != sizeof(req) + req.flen)
137 fatalx("size mismatch for imsg %d", imsg->hdr.type);
138 from = data + sizeof(req);
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 (imsg->hdr.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", imsg->hdr.pid);
172 if (proc_composev_imsg(ps, PROC_SERVER, imsg->hdr.pid - 1,
173 imsg->hdr.type, 0, -1, iov, n) == -1)
174 fatal("proc_composev_imsg");
176 if (proc_flush_imsg(ps, PROC_SERVER, imsg->hdr.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 (datalen < sizeof(req))
185 fatalx("size mismatch for imsg %d", imsg->hdr.type);
186 memcpy(&req, data, sizeof(req));
187 if (datalen != sizeof(req) + req.flen)
188 fatalx("size mismatch for imsg %d", imsg->hdr.type);
189 from = data + sizeof(req);
191 if ((pkey = get_pkey(req.hash)) == NULL ||
192 (ecdsa = EVP_PKEY_get1_EC_KEY(pkey)) == NULL)
193 fatalx("invalid pkey hash");
195 len = ECDSA_size(ecdsa);
196 if ((to = calloc(1, len)) == NULL)
197 fatal("calloc");
198 ret = ECDSA_sign(0, from, req.flen, to, &len, ecdsa);
200 memset(&res, 0, sizeof(res));
201 res.id = req.id;
202 res.ret = ret;
204 memset(&iov, 0, sizeof(iov));
205 n = 0;
206 iov[0].iov_base = &res;
207 iov[0].iov_len = sizeof(res);
208 n++;
210 if (ret > 0) {
211 res.len = len;
212 iov[n].iov_base = to;
213 iov[n].iov_len = len;
214 n++;
217 log_debug("replying to server #%d", imsg->hdr.pid);
218 if (proc_composev_imsg(ps, PROC_SERVER, imsg->hdr.pid - 1,
219 imsg->hdr.type, 0, -1, iov, n) == -1)
220 fatal("proc_composev_imsg");
222 if (proc_flush_imsg(ps, PROC_SERVER, imsg->hdr.pid - 1) == -1)
223 fatal("proc_flush_imsg");
225 free(to);
226 EC_KEY_free(ecdsa);
227 break;
229 default:
230 return -1;
233 return 0;
237 /*
238 * RSA privsep engine (called from unprivileged processes)
239 */
241 static const RSA_METHOD *rsa_default;
242 static RSA_METHOD *rsae_method;
244 static int
245 rsae_send_imsg(int flen, const unsigned char *from, unsigned char *to,
246 RSA *rsa, int padding, unsigned int cmd)
248 struct imsg_crypto_req req;
249 struct iovec iov[2];
250 struct imsg_crypto_res res;
251 struct imsgev *iev;
252 struct privsep_proc *p;
253 struct privsep *ps = conf->ps;
254 struct imsgbuf *imsgbuf;
255 struct imsg imsg;
256 struct ibuf ibuf;
257 int ret = 0;
258 int n, done = 0;
259 const void *toptr;
260 char *hash;
262 if ((hash = RSA_get_ex_data(rsa, 0)) == NULL)
263 return (0);
265 /*
266 * Send a synchronous imsg because we cannot defer the RSA
267 * operation in OpenSSL's engine layer.
268 */
269 memset(&req, 0, sizeof(req));
270 req.id = ++reqid;
271 if (strlcpy(req.hash, hash, sizeof(req.hash)) >= sizeof(req.hash))
272 fatalx("%s: hash too long (%zu)", __func__, strlen(hash));
273 req.flen = flen;
274 req.tlen = RSA_size(rsa);
275 req.padding = padding;
277 memset(&iov, 0, sizeof(iov));
278 iov[0].iov_base = &req;
279 iov[0].iov_len = sizeof(req);
280 iov[1].iov_base = (void *)from;
281 iov[1].iov_len = flen;
283 if (proc_composev(ps, PROC_CRYPTO, cmd, iov, 2) == -1)
284 fatal("proc_composev");
286 if (proc_flush_imsg(ps, PROC_CRYPTO, -1) == -1)
287 fatal("proc_flush_imsg");
289 iev = ps->ps_ievs[PROC_CRYPTO];
290 p = iev->proc;
291 imsgbuf = &iev->ibuf;
293 while (!done) {
294 if ((n = imsg_read(imsgbuf)) == -1 && errno != EAGAIN)
295 fatalx("imsg_read");
296 if (n == 0)
297 fatalx("pipe closed");
299 while (!done) {
300 if ((n = imsg_get(imsgbuf, &imsg)) == -1)
301 fatalx("imsg_get error");
302 if (n == 0)
303 break;
305 #if DEBUG > 1
306 log_debug(
307 "%s: %s %d got imsg %d id %d from %s %d",
308 __func__, title, 1, imsg_get_type(&imsg),
309 imsg_get_id(&imsg), "crypto", imsg_get_pid(&imsg));
310 #endif
312 if ((p->p_cb)(imsgbuf->fd, p, &imsg) == 0) {
313 /* Message was handled by the callback */
314 imsg_free(&imsg);
315 continue;
318 switch (imsg_get_type(&imsg)) {
319 case IMSG_CRYPTO_RSA_PRIVENC:
320 case IMSG_CRYPTO_RSA_PRIVDEC:
321 break;
322 default:
323 fatalx("%s: %s %d got invalid imsg %d"
324 " id %d from %s %d",
325 __func__, "server", ps->ps_instance + 1,
326 imsg_get_type(&imsg), imsg_get_id(&imsg),
327 "crypto", imsg_get_pid(&imsg));
330 if (imsg_get_ibuf(&imsg, &ibuf) == -1 ||
331 ibuf_get(&ibuf, &res, sizeof(res)) == -1 ||
332 (int)ibuf_size(&ibuf) != res.ret)
333 fatalx("size mismatch for imsg %d",
334 imsg.hdr.type);
335 ret = res.ret;
336 toptr = ibuf_data(&ibuf);
338 if (res.id != reqid)
339 fatalx("invalid id; got %llu, want %llu",
340 (unsigned long long)res.id,
341 (unsigned long long)reqid);
342 if (res.ret > 0)
343 memcpy(to, toptr, res.len);
345 done = 1;
347 imsg_free(&imsg);
350 imsg_event_add(iev);
352 return (ret);
355 static int
356 rsae_priv_enc(int flen, const unsigned char *from, unsigned char *to, RSA *rsa,
357 int padding)
359 log_debug("debug: %s", __func__);
360 if (RSA_get_ex_data(rsa, 0) != NULL)
361 return (rsae_send_imsg(flen, from, to, rsa, padding,
362 IMSG_CRYPTO_RSA_PRIVENC));
363 return (RSA_meth_get_priv_enc(rsa_default)(flen, from, to, rsa, padding));
366 static int
367 rsae_priv_dec(int flen, const unsigned char *from, unsigned char *to, RSA *rsa,
368 int padding)
370 log_debug("debug: %s", __func__);
371 if (RSA_get_ex_data(rsa, 0) != NULL)
372 return (rsae_send_imsg(flen, from, to, rsa, padding,
373 IMSG_CRYPTO_RSA_PRIVDEC));
375 return (RSA_meth_get_priv_dec(rsa_default)(flen, from, to, rsa, padding));
379 /*
380 * ECDSA privsep engine (called from unprivileged processes)
381 */
383 static const EC_KEY_METHOD *ecdsa_default;
384 static EC_KEY_METHOD *ecdsae_method;
386 static ECDSA_SIG *
387 ecdsae_send_enc_imsg(const unsigned char *dgst, int dgst_len,
388 const BIGNUM *inv, const BIGNUM *rp, EC_KEY *eckey)
390 ECDSA_SIG *sig = NULL;
391 struct imsg_crypto_req req;
392 struct iovec iov[2];
393 struct imsg_crypto_res res;
394 struct imsgev *iev;
395 struct privsep_proc *p;
396 struct privsep *ps = conf->ps;
397 struct imsgbuf *imsgbuf;
398 struct imsg imsg;
399 struct ibuf ibuf;
400 int n, done = 0;
401 const void *toptr;
402 char *hash;
404 if ((hash = EC_KEY_get_ex_data(eckey, 0)) == NULL)
405 return (0);
407 /*
408 * Send a synchronous imsg because we cannot defer the RSA
409 * operation in OpenSSL's engine layer.
410 */
411 memset(&req, 0, sizeof(req));
412 req.id = ++reqid;
413 if (strlcpy(req.hash, hash, sizeof(req.hash)) >= sizeof(req.hash))
414 fatalx("%s: hash too long (%zu)", __func__, strlen(hash));
415 req.flen = dgst_len;
417 memset(&iov, 0, sizeof(iov));
418 iov[0].iov_base = &req;
419 iov[0].iov_len = sizeof(req);
420 iov[1].iov_base = (void *)dgst;
421 iov[1].iov_len = dgst_len;
423 if (proc_composev(ps, PROC_CRYPTO, IMSG_CRYPTO_ECDSA_SIGN, iov, 2) == -1)
424 fatal("proc_composev");
426 if (proc_flush_imsg(ps, PROC_CRYPTO, -1) == -1)
427 fatal("proc_flush_imsg");
429 iev = ps->ps_ievs[PROC_CRYPTO];
430 p = iev->proc;
431 imsgbuf = &iev->ibuf;
433 while (!done) {
434 if ((n = imsg_read(imsgbuf)) == -1 && errno != EAGAIN)
435 fatalx("imsg_read");
436 if (n == 0)
437 fatalx("pipe closed");
439 while (!done) {
440 if ((n = imsg_get(imsgbuf, &imsg)) == -1)
441 fatalx("imsg_get error");
442 if (n == 0)
443 break;
445 #if DEBUG > 1
446 log_debug(
447 "%s: %s %d got imsg %d peerid %d from %s %d",
448 __func__, title, 1, imsg.hdr.type,
449 imsg.hdr.peerid, "crypto", imsg.hdr.pid);
450 #endif
452 if (imsg.hdr.type != IMSG_CRYPTO_ECDSA_SIGN &&
453 crypto_dispatch_server(imsgbuf->fd, p, &imsg)
454 == 0) {
455 /* Message was handled by the callback */
456 imsg_free(&imsg);
457 continue;
460 if (imsg.hdr.type != IMSG_CRYPTO_ECDSA_SIGN)
461 fatalx("%s: %s %d got invalid imsg %d"
462 " peerid %d from %s %d",
463 __func__, "server", ps->ps_instance + 1,
464 imsg.hdr.type, imsg.hdr.peerid,
465 "crypto", imsg.hdr.pid);
467 if (imsg_get_ibuf(&imsg, &ibuf) == -1 ||
468 ibuf_get(&ibuf, &res, sizeof(res)) == -1 ||
469 ibuf_size(&ibuf) != res.len)
470 fatalx("size mismatch for imsg %d",
471 imsg.hdr.type);
473 toptr = ibuf_data(&ibuf);
475 if (res.id != reqid)
476 fatalx("invalid response id");
477 if (res.ret > 0) {
478 d2i_ECDSA_SIG(&sig,
479 (const unsigned char **)&toptr, res.len);
482 done = 1;
484 imsg_free(&imsg);
487 imsg_event_add(iev);
489 return (sig);
492 static ECDSA_SIG *
493 ecdsae_do_sign(const unsigned char *dgst, int dgst_len, const BIGNUM *inv,
494 const BIGNUM *rp, EC_KEY *eckey)
496 ECDSA_SIG *(*psign_sig)(const unsigned char *, int, const BIGNUM *,
497 const BIGNUM *, EC_KEY *);
499 log_debug("debug: %s", __func__);
500 if (EC_KEY_get_ex_data(eckey, 0) != NULL)
501 return (ecdsae_send_enc_imsg(dgst, dgst_len, inv, rp, eckey));
502 EC_KEY_METHOD_get_sign(ecdsa_default, NULL, NULL, &psign_sig);
503 return (psign_sig(dgst, dgst_len, inv, rp, eckey));
507 /*
508 * Initialize the two engines.
509 */
511 static void
512 rsa_engine_init(void)
514 const char *errstr;
516 if ((rsa_default = RSA_get_default_method()) == NULL) {
517 errstr = "RSA_get_default_method";
518 goto fail;
521 if ((rsae_method = RSA_meth_dup(rsa_default)) == NULL) {
522 errstr = "RSA_meth_dup";
523 goto fail;
526 RSA_meth_set_priv_enc(rsae_method, rsae_priv_enc);
527 RSA_meth_set_priv_dec(rsae_method, rsae_priv_dec);
529 RSA_meth_set_flags(rsae_method,
530 RSA_meth_get_flags(rsa_default) | RSA_METHOD_FLAG_NO_CHECK);
531 RSA_meth_set0_app_data(rsae_method,
532 RSA_meth_get0_app_data(rsa_default));
534 RSA_set_default_method(rsae_method);
536 return;
538 fail:
539 ssl_error(errstr);
540 fatalx("%s", errstr);
543 static void
544 ecdsa_engine_init(void)
546 int (*sign)(int, const unsigned char *, int, unsigned char *,
547 unsigned int *, const BIGNUM *, const BIGNUM *, EC_KEY *);
548 int (*sign_setup)(EC_KEY *, BN_CTX *, BIGNUM **, BIGNUM **);
549 const char *errstr;
551 if ((ecdsa_default = EC_KEY_get_default_method()) == NULL) {
552 errstr = "EC_KEY_get_default_method";
553 goto fail;
556 if ((ecdsae_method = EC_KEY_METHOD_new(ecdsa_default)) == NULL) {
557 errstr = "EC_KEY_METHOD_new";
558 goto fail;
561 EC_KEY_METHOD_get_sign(ecdsa_default, &sign, &sign_setup, NULL);
562 EC_KEY_METHOD_set_sign(ecdsae_method, sign, sign_setup,
563 ecdsae_do_sign);
565 EC_KEY_set_default_method(ecdsae_method);
567 return;
569 fail:
570 ssl_error(errstr);
571 fatalx("%s", errstr);
574 void
575 crypto_engine_init(struct conf *c)
577 conf = c;
579 rsa_engine_init();
580 ecdsa_engine_init();