Blame


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