xref: /openbsd-src/sbin/iked/config.c (revision 46035553bfdd96e63c94e32da0210227ec2e3cf1)
1 /*	$OpenBSD: config.c,v 1.74 2020/11/29 21:00:43 tobhe Exp $	*/
2 
3 /*
4  * Copyright (c) 2019 Tobias Heider <tobias.heider@stusta.de>
5  * Copyright (c) 2010-2013 Reyk Floeter <reyk@openbsd.org>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include <sys/queue.h>
21 #include <sys/socket.h>
22 #include <sys/uio.h>
23 
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <signal.h>
29 #include <errno.h>
30 #include <err.h>
31 #include <event.h>
32 
33 #include <openssl/evp.h>
34 #include <openssl/pem.h>
35 
36 #include "iked.h"
37 #include "ikev2.h"
38 
39 struct iked_sa *
40 config_new_sa(struct iked *env, int initiator)
41 {
42 	struct iked_sa	*sa;
43 
44 	if ((sa = calloc(1, sizeof(*sa))) == NULL)
45 		return (NULL);
46 
47 	TAILQ_INIT(&sa->sa_proposals);
48 	TAILQ_INIT(&sa->sa_childsas);
49 	TAILQ_INIT(&sa->sa_flows);
50 	TAILQ_INIT(&sa->sa_requests);
51 	TAILQ_INIT(&sa->sa_responses);
52 	sa->sa_hdr.sh_initiator = initiator;
53 	sa->sa_type = IKED_SATYPE_LOCAL;
54 
55 	if (initiator)
56 		sa->sa_hdr.sh_ispi = config_getspi();
57 	else
58 		sa->sa_hdr.sh_rspi = config_getspi();
59 
60 	gettimeofday(&sa->sa_timecreated, NULL);
61 	memcpy(&sa->sa_timeused, &sa->sa_timecreated, sizeof(sa->sa_timeused));
62 
63 	return (sa);
64 }
65 
66 uint64_t
67 config_getspi(void)
68 {
69 	uint64_t	 spi;
70 
71 	do {
72 		arc4random_buf(&spi, sizeof spi);
73 	} while (spi == 0);
74 
75 	return (spi);
76 }
77 
78 void
79 config_free_kex(struct iked_kex *kex)
80 {
81 	if (kex == NULL)
82 		return;
83 
84 	ibuf_release(kex->kex_inonce);
85 	ibuf_release(kex->kex_rnonce);
86 
87 	if (kex->kex_dhgroup != NULL)
88 		group_free(kex->kex_dhgroup);
89 	ibuf_release(kex->kex_dhiexchange);
90 	ibuf_release(kex->kex_dhrexchange);
91 
92 	free(kex);
93 }
94 
95 void
96 config_free_fragments(struct iked_frag *frag)
97 {
98 	size_t i;
99 
100 	if (frag && frag->frag_arr) {
101 		for (i = 0; i < frag->frag_total; i++) {
102 			if (frag->frag_arr[i] != NULL)
103 				free(frag->frag_arr[i]->frag_data);
104 			free(frag->frag_arr[i]);
105 		}
106 		free(frag->frag_arr);
107 		bzero(frag, sizeof(struct iked_frag));
108 	}
109 }
110 
111 void
112 config_free_sa(struct iked *env, struct iked_sa *sa)
113 {
114 	timer_del(env, &sa->sa_timer);
115 	timer_del(env, &sa->sa_keepalive);
116 	timer_del(env, &sa->sa_rekey);
117 
118 	config_free_fragments(&sa->sa_fragments);
119 	config_free_proposals(&sa->sa_proposals, 0);
120 	config_free_childsas(env, &sa->sa_childsas, NULL, NULL);
121 	sa_free_flows(env, &sa->sa_flows);
122 
123 	if (sa->sa_addrpool) {
124 		(void)RB_REMOVE(iked_addrpool, &env->sc_addrpool, sa);
125 		free(sa->sa_addrpool);
126 	}
127 	if (sa->sa_addrpool6) {
128 		(void)RB_REMOVE(iked_addrpool6, &env->sc_addrpool6, sa);
129 		free(sa->sa_addrpool6);
130 	}
131 
132 	if (sa->sa_policy) {
133 		TAILQ_REMOVE(&sa->sa_policy->pol_sapeers, sa, sa_peer_entry);
134 		policy_unref(env, sa->sa_policy);
135 	}
136 
137 	ikev2_msg_flushqueue(env, &sa->sa_requests);
138 	ikev2_msg_flushqueue(env, &sa->sa_responses);
139 
140 	ibuf_release(sa->sa_inonce);
141 	ibuf_release(sa->sa_rnonce);
142 
143 	if (sa->sa_dhgroup != NULL)
144 		group_free(sa->sa_dhgroup);
145 	ibuf_release(sa->sa_dhiexchange);
146 	ibuf_release(sa->sa_dhrexchange);
147 
148 	ibuf_release(sa->sa_simult);
149 
150 	hash_free(sa->sa_prf);
151 	hash_free(sa->sa_integr);
152 	cipher_free(sa->sa_encr);
153 
154 	ibuf_release(sa->sa_key_d);
155 	ibuf_release(sa->sa_key_iauth);
156 	ibuf_release(sa->sa_key_rauth);
157 	ibuf_release(sa->sa_key_iencr);
158 	ibuf_release(sa->sa_key_rencr);
159 	ibuf_release(sa->sa_key_iprf);
160 	ibuf_release(sa->sa_key_rprf);
161 
162 	ibuf_release(sa->sa_1stmsg);
163 	ibuf_release(sa->sa_2ndmsg);
164 
165 	ibuf_release(sa->sa_iid.id_buf);
166 	ibuf_release(sa->sa_rid.id_buf);
167 	ibuf_release(sa->sa_icert.id_buf);
168 	ibuf_release(sa->sa_rcert.id_buf);
169 	ibuf_release(sa->sa_localauth.id_buf);
170 	ibuf_release(sa->sa_peerauth.id_buf);
171 
172 	ibuf_release(sa->sa_eap.id_buf);
173 	free(sa->sa_eapid);
174 	ibuf_release(sa->sa_eapmsk);
175 
176 	free(sa->sa_cp_addr);
177 	free(sa->sa_cp_addr6);
178 
179 	free(sa->sa_tag);
180 	free(sa);
181 }
182 
183 struct iked_policy *
184 config_new_policy(struct iked *env)
185 {
186 	struct iked_policy	*pol;
187 
188 	if ((pol = calloc(1, sizeof(*pol))) == NULL)
189 		return (NULL);
190 
191 	/* XXX caller does this again */
192 	TAILQ_INIT(&pol->pol_proposals);
193 	TAILQ_INIT(&pol->pol_sapeers);
194 	TAILQ_INIT(&pol->pol_tssrc);
195 	TAILQ_INIT(&pol->pol_tsdst);
196 	RB_INIT(&pol->pol_flows);
197 
198 	return (pol);
199 }
200 
201 void
202 config_free_policy(struct iked *env, struct iked_policy *pol)
203 {
204 	struct iked_sa		*sa;
205 	struct iked_ts	*tsi;
206 
207 	if (pol->pol_flags & IKED_POLICY_REFCNT)
208 		goto remove;
209 
210 	TAILQ_REMOVE(&env->sc_policies, pol, pol_entry);
211 
212 	TAILQ_FOREACH(sa, &pol->pol_sapeers, sa_peer_entry) {
213 		/* Remove from the policy list, but keep for existing SAs */
214 		if (sa->sa_policy == pol)
215 			policy_ref(env, pol);
216 		else
217 			log_warnx("%s: ERROR: sa_policy %p != pol %p",
218 			    __func__, sa->sa_policy, pol);
219 	}
220 
221 	if (pol->pol_refcnt)
222 		return;
223 
224  remove:
225 	while ((tsi = TAILQ_FIRST(&pol->pol_tssrc))) {
226 		TAILQ_REMOVE(&pol->pol_tssrc, tsi, ts_entry);
227 		free(tsi);
228 	}
229 	while ((tsi = TAILQ_FIRST(&pol->pol_tsdst))) {
230 		TAILQ_REMOVE(&pol->pol_tsdst, tsi, ts_entry);
231 		free(tsi);
232 	}
233 	config_free_proposals(&pol->pol_proposals, 0);
234 	config_free_flows(env, &pol->pol_flows);
235 	free(pol);
236 }
237 
238 struct iked_proposal *
239 config_add_proposal(struct iked_proposals *head, unsigned int id,
240     unsigned int proto)
241 {
242 	struct iked_proposal	*pp;
243 
244 	TAILQ_FOREACH(pp, head, prop_entry) {
245 		if (pp->prop_protoid == proto &&
246 		    pp->prop_id == id)
247 			return (pp);
248 	}
249 
250 	if ((pp = calloc(1, sizeof(*pp))) == NULL)
251 		return (NULL);
252 
253 	pp->prop_protoid = proto;
254 	pp->prop_id = id;
255 
256 	TAILQ_INSERT_TAIL(head, pp, prop_entry);
257 
258 	return (pp);
259 }
260 
261 void
262 config_free_proposal(struct iked_proposals *head, struct iked_proposal *prop)
263 {
264 	TAILQ_REMOVE(head, prop, prop_entry);
265 	if (prop->prop_nxforms)
266 		free(prop->prop_xforms);
267 	free(prop);
268 }
269 
270 void
271 config_free_proposals(struct iked_proposals *head, unsigned int proto)
272 {
273 	struct iked_proposal	*prop, *proptmp;
274 
275 	TAILQ_FOREACH_SAFE(prop, head, prop_entry, proptmp) {
276 		/* Free any proposal or only selected SA proto */
277 		if (proto != 0 && prop->prop_protoid != proto)
278 			continue;
279 
280 		log_debug("%s: free %p", __func__, prop);
281 
282 		config_free_proposal(head, prop);
283 	}
284 }
285 
286 void
287 config_free_flows(struct iked *env, struct iked_flows *head)
288 {
289 	struct iked_flow	*flow;
290 
291 	while ((flow = RB_MIN(iked_flows, head))) {
292 		log_debug("%s: free %p", __func__, flow);
293 		RB_REMOVE(iked_flows, head, flow);
294 		flow_free(flow);
295 	}
296 }
297 
298 void
299 config_free_childsas(struct iked *env, struct iked_childsas *head,
300     struct iked_spi *peerspi, struct iked_spi *localspi)
301 {
302 	struct iked_childsa	*csa, *csatmp, *ipcomp;
303 
304 	if (localspi != NULL)
305 		bzero(localspi, sizeof(*localspi));
306 
307 	TAILQ_FOREACH_SAFE(csa, head, csa_entry, csatmp) {
308 		if (peerspi != NULL) {
309 			/* Only delete matching peer SPIs */
310 			if (peerspi->spi != csa->csa_peerspi)
311 				continue;
312 
313 			/* Store assigned local SPI */
314 			if (localspi != NULL && localspi->spi == 0)
315 				memcpy(localspi, &csa->csa_spi,
316 				    sizeof(*localspi));
317 		}
318 		log_debug("%s: free %p", __func__, csa);
319 
320 		TAILQ_REMOVE(head, csa, csa_entry);
321 		if (csa->csa_loaded) {
322 			RB_REMOVE(iked_activesas, &env->sc_activesas, csa);
323 			(void)pfkey_sa_delete(env->sc_pfkey, csa);
324 		}
325 		if ((ipcomp = csa->csa_bundled) != NULL) {
326 			log_debug("%s: free IPCOMP %p", __func__, ipcomp);
327 			if (ipcomp->csa_loaded)
328 				(void)pfkey_sa_delete(env->sc_pfkey, ipcomp);
329 			childsa_free(ipcomp);
330 		}
331 		childsa_free(csa);
332 	}
333 }
334 
335 int
336 config_add_transform(struct iked_proposal *prop, unsigned int type,
337     unsigned int id, unsigned int length, unsigned int keylength)
338 {
339 	struct iked_transform	*xform;
340 	struct iked_constmap	*map = NULL;
341 	int			 score = 1;
342 	unsigned int		 i;
343 
344 	switch (type) {
345 	case IKEV2_XFORMTYPE_ENCR:
346 		map = ikev2_xformencr_map;
347 		break;
348 	case IKEV2_XFORMTYPE_PRF:
349 		map = ikev2_xformprf_map;
350 		break;
351 	case IKEV2_XFORMTYPE_INTEGR:
352 		map = ikev2_xformauth_map;
353 		break;
354 	case IKEV2_XFORMTYPE_DH:
355 		map = ikev2_xformdh_map;
356 		break;
357 	case IKEV2_XFORMTYPE_ESN:
358 		map = ikev2_xformesn_map;
359 		break;
360 	default:
361 		log_debug("%s: invalid transform type %d", __func__, type);
362 		return (-2);
363 	}
364 
365 	for (i = 0; i < prop->prop_nxforms; i++) {
366 		xform = prop->prop_xforms + i;
367 		if (xform->xform_type == type &&
368 		    xform->xform_id == id &&
369 		    xform->xform_length == length)
370 			return (0);
371 	}
372 
373 	for (i = 0; i < prop->prop_nxforms; i++) {
374 		xform = prop->prop_xforms + i;
375 		if (xform->xform_type == type) {
376 			switch (type) {
377 			case IKEV2_XFORMTYPE_ENCR:
378 			case IKEV2_XFORMTYPE_INTEGR:
379 				score += 3;
380 				break;
381 			case IKEV2_XFORMTYPE_DH:
382 				score += 2;
383 				break;
384 			default:
385 				score += 1;
386 				break;
387 			}
388 		}
389 	}
390 
391 	if ((xform = reallocarray(prop->prop_xforms,
392 	    prop->prop_nxforms + 1, sizeof(*xform))) == NULL) {
393 		return (-1);
394 	}
395 
396 	prop->prop_xforms = xform;
397 	xform = prop->prop_xforms + prop->prop_nxforms++;
398 	bzero(xform, sizeof(*xform));
399 
400 	xform->xform_type = type;
401 	xform->xform_id = id;
402 	xform->xform_length = length;
403 	xform->xform_keylength = keylength;
404 	xform->xform_score = score;
405 	xform->xform_map = map;
406 
407 	return (0);
408 }
409 
410 struct iked_transform *
411 config_findtransform(struct iked_proposals *props, uint8_t type,
412     unsigned int proto)
413 {
414 	struct iked_proposal	*prop;
415 	struct iked_transform	*xform;
416 	unsigned int		 i;
417 
418 	/* Search of the first transform with the desired type */
419 	TAILQ_FOREACH(prop, props, prop_entry) {
420 		/* Find any proposal or only selected SA proto */
421 		if (proto != 0 && prop->prop_protoid != proto)
422 			continue;
423 		for (i = 0; i < prop->prop_nxforms; i++) {
424 			xform = prop->prop_xforms + i;
425 			if (xform->xform_type == type)
426 				return (xform);
427 		}
428 	}
429 
430 	return (NULL);
431 }
432 
433 struct iked_user *
434 config_new_user(struct iked *env, struct iked_user *new)
435 {
436 	struct iked_user	*usr, *old;
437 
438 	if ((usr = calloc(1, sizeof(*usr))) == NULL)
439 		return (NULL);
440 
441 	memcpy(usr, new, sizeof(*usr));
442 
443 	if ((old = RB_INSERT(iked_users, &env->sc_users, usr)) != NULL) {
444 		/* Update the password of an existing user*/
445 		memcpy(old->usr_pass, new->usr_pass, IKED_PASSWORD_SIZE);
446 
447 		log_debug("%s: updating user %s", __func__, usr->usr_name);
448 		free(usr);
449 
450 		return (old);
451 	}
452 
453 	log_debug("%s: inserting new user %s", __func__, usr->usr_name);
454 	return (usr);
455 }
456 
457 /*
458  * Inter-process communication of configuration items.
459  */
460 
461 int
462 config_setcoupled(struct iked *env, unsigned int couple)
463 {
464 	unsigned int	 type;
465 
466 	type = couple ? IMSG_CTL_COUPLE : IMSG_CTL_DECOUPLE;
467 	proc_compose(&env->sc_ps, PROC_IKEV2, type, NULL, 0);
468 
469 	return (0);
470 }
471 
472 int
473 config_getcoupled(struct iked *env, unsigned int type)
474 {
475 	return (pfkey_couple(env->sc_pfkey, &env->sc_sas,
476 	    type == IMSG_CTL_COUPLE ? 1 : 0));
477 }
478 
479 int
480 config_setmode(struct iked *env, unsigned int passive)
481 {
482 	unsigned int	 type;
483 
484 	type = passive ? IMSG_CTL_PASSIVE : IMSG_CTL_ACTIVE;
485 	proc_compose(&env->sc_ps, PROC_IKEV2, type, NULL, 0);
486 
487 	return (0);
488 }
489 
490 int
491 config_getmode(struct iked *env, unsigned int type)
492 {
493 	uint8_t		 old;
494 	unsigned char	*mode[] = { "active", "passive" };
495 
496 	old = env->sc_passive ? 1 : 0;
497 	env->sc_passive = type == IMSG_CTL_PASSIVE ? 1 : 0;
498 
499 	if (old == env->sc_passive)
500 		return (0);
501 
502 	log_debug("%s: mode %s -> %s", __func__,
503 	    mode[old], mode[env->sc_passive]);
504 
505 	return (0);
506 }
507 
508 int
509 config_setreset(struct iked *env, unsigned int mode, enum privsep_procid id)
510 {
511 	proc_compose(&env->sc_ps, id, IMSG_CTL_RESET, &mode, sizeof(mode));
512 	return (0);
513 }
514 
515 int
516 config_getreset(struct iked *env, struct imsg *imsg)
517 {
518 	struct iked_policy	*pol, *poltmp;
519 	struct iked_sa		*sa;
520 	struct iked_user	*usr;
521 	unsigned int		 mode;
522 
523 	IMSG_SIZE_CHECK(imsg, &mode);
524 	memcpy(&mode, imsg->data, sizeof(mode));
525 
526 	if (mode == RESET_ALL || mode == RESET_POLICY) {
527 		log_debug("%s: flushing policies", __func__);
528 		TAILQ_FOREACH_SAFE(pol, &env->sc_policies, pol_entry, poltmp) {
529 			config_free_policy(env, pol);
530 		}
531 	}
532 
533 	if (mode == RESET_ALL || mode == RESET_SA) {
534 		log_debug("%s: flushing SAs", __func__);
535 		while ((sa = RB_MIN(iked_sas, &env->sc_sas))) {
536 			/* for RESET_SA we try send a DELETE */
537 			if (mode == RESET_ALL ||
538 			    ikev2_ike_sa_delete(env, sa) != 0) {
539 				RB_REMOVE(iked_sas, &env->sc_sas, sa);
540 				if (sa->sa_dstid_entry_valid)
541 					sa_dstid_remove(env, sa);
542 				config_free_sa(env, sa);
543 			}
544 		}
545 	}
546 
547 	if (mode == RESET_ALL || mode == RESET_USER) {
548 		log_debug("%s: flushing users", __func__);
549 		while ((usr = RB_MIN(iked_users, &env->sc_users))) {
550 			RB_REMOVE(iked_users, &env->sc_users, usr);
551 			free(usr);
552 		}
553 	}
554 
555 	return (0);
556 }
557 
558 /*
559  * The first call of this function sets the UDP socket for IKEv2.
560  * The second call is optional, setting the UDP socket used for NAT-T.
561  */
562 int
563 config_setsocket(struct iked *env, struct sockaddr_storage *ss,
564     in_port_t port, enum privsep_procid id)
565 {
566 	int	 s;
567 
568 	if ((s = udp_bind((struct sockaddr *)ss, port)) == -1)
569 		return (-1);
570 	proc_compose_imsg(&env->sc_ps, id, -1,
571 	    IMSG_UDP_SOCKET, -1, s, ss, sizeof(*ss));
572 	return (0);
573 }
574 
575 int
576 config_getsocket(struct iked *env, struct imsg *imsg,
577     void (*cb)(int, short, void *))
578 {
579 	struct iked_socket	*sock, **sock0, **sock1;
580 
581 	log_debug("%s: received socket fd %d", __func__, imsg->fd);
582 
583 	if ((sock = calloc(1, sizeof(*sock))) == NULL)
584 		fatal("config_getsocket: calloc");
585 
586 	IMSG_SIZE_CHECK(imsg, &sock->sock_addr);
587 
588 	memcpy(&sock->sock_addr, imsg->data, sizeof(sock->sock_addr));
589 	sock->sock_fd = imsg->fd;
590 	sock->sock_env = env;
591 
592 	switch (sock->sock_addr.ss_family) {
593 	case AF_INET:
594 		sock0 = &env->sc_sock4[0];
595 		sock1 = &env->sc_sock4[1];
596 		break;
597 	case AF_INET6:
598 		sock0 = &env->sc_sock6[0];
599 		sock1 = &env->sc_sock6[1];
600 		break;
601 	default:
602 		fatal("config_getsocket: socket af: %u",
603 		    sock->sock_addr.ss_family);
604 		/* NOTREACHED */
605 	}
606 	if (*sock0 == NULL)
607 		*sock0 = sock;
608 	else if (*sock1 == NULL)
609 		*sock1 = sock;
610 	else
611 		fatalx("%s: too many call", __func__);
612 
613 	event_set(&sock->sock_ev, sock->sock_fd,
614 	    EV_READ|EV_PERSIST, cb, sock);
615 	event_add(&sock->sock_ev, NULL);
616 
617 	return (0);
618 }
619 
620 int
621 config_setpfkey(struct iked *env, enum privsep_procid id)
622 {
623 	int	 s;
624 
625 	if ((s = pfkey_socket()) == -1)
626 		return (-1);
627 	proc_compose_imsg(&env->sc_ps, id, -1,
628 	    IMSG_PFKEY_SOCKET, -1, s, NULL, 0);
629 	return (0);
630 }
631 
632 int
633 config_getpfkey(struct iked *env, struct imsg *imsg)
634 {
635 	log_debug("%s: received pfkey fd %d", __func__, imsg->fd);
636 	pfkey_init(env, imsg->fd);
637 	return (0);
638 }
639 
640 int
641 config_setuser(struct iked *env, struct iked_user *usr, enum privsep_procid id)
642 {
643 	if (env->sc_opts & IKED_OPT_NOACTION) {
644 		print_user(usr);
645 		return (0);
646 	}
647 
648 	proc_compose(&env->sc_ps, id, IMSG_CFG_USER, usr, sizeof(*usr));
649 	return (0);
650 }
651 
652 int
653 config_getuser(struct iked *env, struct imsg *imsg)
654 {
655 	struct iked_user	 usr;
656 
657 	IMSG_SIZE_CHECK(imsg, &usr);
658 	memcpy(&usr, imsg->data, sizeof(usr));
659 
660 	if (config_new_user(env, &usr) == NULL)
661 		return (-1);
662 
663 	print_user(&usr);
664 
665 	return (0);
666 }
667 
668 int
669 config_setpolicy(struct iked *env, struct iked_policy *pol,
670     enum privsep_procid id)
671 {
672 	struct iked_proposal	*prop;
673 	struct iked_transform	*xform;
674 	size_t			 iovcnt, j, c = 0;
675 	struct iovec		 iov[IOV_MAX];
676 
677 	iovcnt = 1;
678 	TAILQ_FOREACH(prop, &pol->pol_proposals, prop_entry) {
679 		iovcnt += prop->prop_nxforms + 1;
680 	}
681 
682 	if (iovcnt > IOV_MAX) {
683 		log_warn("%s: too many proposals", __func__);
684 		return (-1);
685 	}
686 
687 	iov[c].iov_base = pol;
688 	iov[c++].iov_len = sizeof(*pol);
689 
690 	TAILQ_FOREACH(prop, &pol->pol_proposals, prop_entry) {
691 		iov[c].iov_base = prop;
692 		iov[c++].iov_len = sizeof(*prop);
693 
694 		for (j = 0; j < prop->prop_nxforms; j++) {
695 			xform = prop->prop_xforms + j;
696 
697 			iov[c].iov_base = xform;
698 			iov[c++].iov_len = sizeof(*xform);
699 		}
700 	}
701 
702 	print_policy(pol);
703 
704 	if (env->sc_opts & IKED_OPT_NOACTION)
705 		return (0);
706 
707 	if (proc_composev(&env->sc_ps, id, IMSG_CFG_POLICY, iov,
708 	    iovcnt) == -1) {
709 		log_debug("%s: proc_composev failed", __func__);
710 		return (-1);
711 	}
712 
713 	return (0);
714 }
715 
716 int
717 config_setflow(struct iked *env, struct iked_policy *pol,
718     enum privsep_procid id)
719 {
720 	struct iked_flow	*flow;
721 	struct iovec		 iov[2];
722 
723 	if (env->sc_opts & IKED_OPT_NOACTION)
724 		return (0);
725 
726 	RB_FOREACH(flow, iked_flows, &pol->pol_flows) {
727 		iov[0].iov_base = &pol->pol_id;
728 		iov[0].iov_len = sizeof(pol->pol_id);
729 		iov[1].iov_base = flow;
730 		iov[1].iov_len = sizeof(*flow);
731 
732 		if (proc_composev(&env->sc_ps, id, IMSG_CFG_FLOW,
733 		    iov, 2) == -1) {
734 			log_debug("%s: proc_composev failed", __func__);
735 			return (-1);
736 		}
737 	}
738 
739 	return (0);
740 }
741 
742 int
743 config_getpolicy(struct iked *env, struct imsg *imsg)
744 {
745 	struct iked_policy	*pol;
746 	struct iked_proposal	 pp, *prop;
747 	struct iked_transform	 xf;
748 	off_t			 offset = 0;
749 	unsigned int		 i, j;
750 	uint8_t			*buf = (uint8_t *)imsg->data;
751 
752 	IMSG_SIZE_CHECK(imsg, pol);
753 	log_debug("%s: received policy", __func__);
754 
755 	if ((pol = config_new_policy(NULL)) == NULL)
756 		fatal("config_getpolicy: new policy");
757 
758 	memcpy(pol, buf, sizeof(*pol));
759 	offset += sizeof(*pol);
760 
761 	TAILQ_INIT(&pol->pol_tssrc);
762 	TAILQ_INIT(&pol->pol_tsdst);
763 	TAILQ_INIT(&pol->pol_proposals);
764 	TAILQ_INIT(&pol->pol_sapeers);
765 	RB_INIT(&pol->pol_flows);
766 
767 	for (i = 0; i < pol->pol_nproposals; i++) {
768 		memcpy(&pp, buf + offset, sizeof(pp));
769 		offset += sizeof(pp);
770 
771 		if ((prop = config_add_proposal(&pol->pol_proposals,
772 		    pp.prop_id, pp.prop_protoid)) == NULL)
773 			fatal("config_getpolicy: add proposal");
774 
775 		for (j = 0; j < pp.prop_nxforms; j++) {
776 			memcpy(&xf, buf + offset, sizeof(xf));
777 			offset += sizeof(xf);
778 
779 			if (config_add_transform(prop, xf.xform_type,
780 			    xf.xform_id, xf.xform_length,
781 			    xf.xform_keylength) != 0)
782 				fatal("config_getpolicy: add transform");
783 		}
784 	}
785 
786 	/* Flows are sent separately */
787 	pol->pol_nflows = 0;
788 
789 	TAILQ_INSERT_TAIL(&env->sc_policies, pol, pol_entry);
790 
791 	if (pol->pol_flags & IKED_POLICY_DEFAULT) {
792 		/* Only one default policy, just free/unref the old one */
793 		if (env->sc_defaultcon != NULL)
794 			config_free_policy(env, env->sc_defaultcon);
795 		env->sc_defaultcon = pol;
796 	}
797 
798 	return (0);
799 }
800 
801 int
802 config_getflow(struct iked *env, struct imsg *imsg)
803 {
804 	struct iked_policy	*pol;
805 	struct iked_flow	*flow;
806 	off_t			 offset = 0;
807 	unsigned int		 id;
808 	uint8_t			*buf = (uint8_t *)imsg->data;
809 
810 	if (IMSG_DATA_SIZE(imsg) < sizeof(id))
811 		fatalx("bad length imsg received");
812 
813 	memcpy(&id, buf, sizeof(id));
814 	offset += sizeof(id);
815 
816 	TAILQ_FOREACH(pol, &env->sc_policies, pol_entry) {
817 		if (pol->pol_id == id)
818 			break;
819 	}
820 	if (pol == NULL) {
821 		log_warnx("%s: unknown policy %u", __func__, id);
822 		return (-1);
823 	}
824 
825 	if ((flow = calloc(1, sizeof(*flow))) == NULL)
826 		fatal("config_getpolicy: new flow");
827 
828 	memcpy(flow, buf + offset, sizeof(*flow));
829 
830 	if (RB_INSERT(iked_flows, &pol->pol_flows, flow)) {
831 		log_warnx("%s: received duplicate flow", __func__);
832 		free(flow);
833 		return (-1);
834 	}
835 	pol->pol_nflows++;
836 
837 	return (0);
838 }
839 
840 int
841 config_setcompile(struct iked *env, enum privsep_procid id)
842 {
843 	if (env->sc_opts & IKED_OPT_NOACTION)
844 		return (0);
845 
846 	proc_compose(&env->sc_ps, id, IMSG_COMPILE, NULL, 0);
847 	return (0);
848 }
849 
850 int
851 config_getcompile(struct iked *env)
852 {
853 	/*
854 	 * Do any necessary steps after configuration, for now we
855 	 * only need to compile the skip steps.
856 	 */
857 	policy_calc_skip_steps(&env->sc_policies);
858 
859 	log_debug("%s: compilation done", __func__);
860 	return (0);
861 }
862 
863 int
864 config_setstatic(struct iked *env)
865 {
866 	proc_compose(&env->sc_ps, PROC_IKEV2, IMSG_CTL_STATIC,
867 	    &env->sc_static, sizeof(env->sc_static));
868 	return (0);
869 }
870 
871 int
872 config_getstatic(struct iked *env, struct imsg *imsg)
873 {
874 	IMSG_SIZE_CHECK(imsg, &env->sc_static);
875 	memcpy(&env->sc_static, imsg->data, sizeof(env->sc_static));
876 
877 	log_debug("%s: dpd_check_interval %llu", __func__, env->sc_alive_timeout);
878 	log_debug("%s: %senforcesingleikesa", __func__,
879 	    env->sc_enforcesingleikesa ? "" : "no ");
880 	log_debug("%s: %sfragmentation", __func__, env->sc_frag ? "" : "no ");
881 	log_debug("%s: %smobike", __func__, env->sc_mobike ? "" : "no ");
882 	log_debug("%s: nattport %u", __func__, env->sc_nattport);
883 	log_debug("%s: %sstickyaddress", __func__,
884 	    env->sc_stickyaddress ? "" : "no ");
885 
886 	return (0);
887 }
888 
889 int
890 config_setocsp(struct iked *env)
891 {
892 	struct iovec		 iov[3];
893 	int			 iovcnt = 0;
894 
895 	if (env->sc_opts & IKED_OPT_NOACTION)
896 		return (0);
897 
898 	iov[0].iov_base = &env->sc_ocsp_tolerate;
899 	iov[0].iov_len = sizeof(env->sc_ocsp_tolerate);
900 	iovcnt++;
901 	iov[1].iov_base = &env->sc_ocsp_maxage;
902 	iov[1].iov_len = sizeof(env->sc_ocsp_maxage);
903 	iovcnt++;
904 	if (env->sc_ocsp_url) {
905 		iov[2].iov_base = env->sc_ocsp_url;
906 		iov[2].iov_len = strlen(env->sc_ocsp_url);
907 		iovcnt++;
908 	}
909 	return (proc_composev(&env->sc_ps, PROC_CERT, IMSG_OCSP_CFG,
910 	    iov, iovcnt));
911 }
912 
913 int
914 config_getocsp(struct iked *env, struct imsg *imsg)
915 {
916 	size_t			 have, need;
917 	u_int8_t		*ptr;
918 
919 	free(env->sc_ocsp_url);
920 	ptr = (u_int8_t *)imsg->data;
921 	have = IMSG_DATA_SIZE(imsg);
922 
923 	/* get tolerate */
924 	need = sizeof(env->sc_ocsp_tolerate);
925 	if (have < need)
926 		fatalx("bad 'tolerate' length imsg received");
927 	memcpy(&env->sc_ocsp_tolerate, ptr, need);
928 	ptr += need;
929 	have -= need;
930 
931 	/* get maxage */
932 	need = sizeof(env->sc_ocsp_maxage);
933 	if (have < need)
934 		fatalx("bad 'maxage' length imsg received");
935 	memcpy(&env->sc_ocsp_maxage, ptr, need);
936 	ptr += need;
937 	have -= need;
938 
939 	/* get url */
940 	if (have > 0)
941 		env->sc_ocsp_url = get_string(ptr, have);
942 	else
943 		env->sc_ocsp_url = NULL;
944 	log_debug("%s: ocsp_url %s tolerate %ld maxage %ld", __func__,
945 	    env->sc_ocsp_url ? env->sc_ocsp_url : "none",
946 	    env->sc_ocsp_tolerate, env->sc_ocsp_maxage);
947 	return (0);
948 }
949 
950 int
951 config_setcertpartialchain(struct iked *env)
952 {
953 	unsigned int boolval;
954 
955 	boolval = env->sc_cert_partial_chain;
956 	proc_compose(&env->sc_ps, PROC_CERT, IMSG_CERT_PARTIAL_CHAIN,
957 	    &boolval, sizeof(boolval));
958 	return (0);
959 }
960 
961 int
962 config_getcertpartialchain(struct iked *env, struct imsg *imsg)
963 {
964 	unsigned int boolval;
965 
966 	IMSG_SIZE_CHECK(imsg, &boolval);
967 	memcpy(&boolval, imsg->data, sizeof(boolval));
968 	env->sc_cert_partial_chain = boolval;
969 	return (0);
970 }
971 
972 int
973 config_setkeys(struct iked *env)
974 {
975 	FILE			*fp = NULL;
976 	EVP_PKEY		*key = NULL;
977 	struct iked_id		 privkey;
978 	struct iked_id		 pubkey;
979 	struct iovec		 iov[2];
980 	int			 ret = -1;
981 
982 	memset(&privkey, 0, sizeof(privkey));
983 	memset(&pubkey, 0, sizeof(pubkey));
984 
985 	/* Read private key */
986 	if ((fp = fopen(IKED_PRIVKEY, "r")) == NULL) {
987 		log_warn("%s: failed to open private key", __func__);
988 		goto done;
989 	}
990 
991 	if ((key = PEM_read_PrivateKey(fp, NULL, NULL, NULL)) == NULL) {
992 		log_warnx("%s: failed to read private key", __func__);
993 		goto done;
994 	}
995 
996 	if (ca_privkey_serialize(key, &privkey) != 0) {
997 		log_warnx("%s: failed to serialize private key", __func__);
998 		goto done;
999 	}
1000 	if (ca_pubkey_serialize(key, &pubkey) != 0) {
1001 		log_warnx("%s: failed to serialize public key", __func__);
1002 		goto done;
1003 	}
1004 
1005 	iov[0].iov_base = &privkey;
1006 	iov[0].iov_len = sizeof(privkey);
1007 	iov[1].iov_base = ibuf_data(privkey.id_buf);
1008 	iov[1].iov_len = ibuf_length(privkey.id_buf);
1009 
1010 	if (proc_composev(&env->sc_ps, PROC_CERT, IMSG_PRIVKEY, iov, 2) == -1) {
1011 		log_warnx("%s: failed to send private key", __func__);
1012 		goto done;
1013 	}
1014 
1015 	iov[0].iov_base = &pubkey;
1016 	iov[0].iov_len = sizeof(pubkey);
1017 	iov[1].iov_base = ibuf_data(pubkey.id_buf);
1018 	iov[1].iov_len = ibuf_length(pubkey.id_buf);
1019 
1020 	if (proc_composev(&env->sc_ps, PROC_CERT, IMSG_PUBKEY, iov, 2) == -1) {
1021 		log_warnx("%s: failed to send public key", __func__);
1022 		goto done;
1023 	}
1024 
1025 	ret = 0;
1026  done:
1027 	if (fp != NULL)
1028 		fclose(fp);
1029 
1030 	ibuf_release(pubkey.id_buf);
1031 	ibuf_release(privkey.id_buf);
1032 	EVP_PKEY_free(key);
1033 
1034 	return (ret);
1035 }
1036 
1037 int
1038 config_getkey(struct iked *env, struct imsg *imsg)
1039 {
1040 	size_t		 len;
1041 	struct iked_id	 id;
1042 
1043 	len = IMSG_DATA_SIZE(imsg);
1044 	if (len <= sizeof(id))
1045 		fatalx("%s: invalid key message", __func__);
1046 
1047 	memcpy(&id, imsg->data, sizeof(id));
1048 	if ((id.id_buf = ibuf_new((uint8_t *)imsg->data + sizeof(id),
1049 	    len - sizeof(id))) == NULL)
1050 		fatalx("%s: failed to get key", __func__);
1051 
1052 	explicit_bzero(imsg->data, len);
1053 	ca_getkey(&env->sc_ps, &id, imsg->hdr.type);
1054 
1055 	ikev2_reset_alive_timer(env);
1056 
1057 	return (0);
1058 }
1059