1/* $OpenBSD: monitor_wrap.c,v 1.75 2013/01/08 18:49:04 markus Exp $ */
2/*
3 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
4 * Copyright 2002 Markus Friedl <markus@openbsd.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "includes.h"
29
30#include <sys/types.h>
31#include <sys/uio.h>
32
33#include <errno.h>
34#include <pwd.h>
35#include <signal.h>
36#include <stdarg.h>
37#include <stdio.h>
38#include <string.h>
39#include <unistd.h>
40
41#ifdef __APPLE_CRYPTO__
42#include "ossl-bn.h"
43#include "ossl-dh.h"
44#include "ossl-evp.h"
45#else
46#include <openssl/bn.h>
47#include <openssl/dh.h>
48#include <openssl/evp.h>
49#endif
50
51#include "openbsd-compat/sys-queue.h"
52#include "xmalloc.h"
53#include "ssh.h"
54#include "dh.h"
55#include "buffer.h"
56#include "key.h"
57#include "cipher.h"
58#include "kex.h"
59#include "hostfile.h"
60#include "auth.h"
61#include "auth-options.h"
62#include "packet.h"
63#include "mac.h"
64#include "log.h"
65#ifdef TARGET_OS_MAC    /* XXX Broken krb5 headers on Mac */
66#undef TARGET_OS_MAC
67#include "zlib.h"
68#define TARGET_OS_MAC 1
69#else
70#include "zlib.h"
71#endif
72#include "monitor.h"
73#ifdef GSSAPI
74#include "ssh-gss.h"
75#endif
76#include "monitor_wrap.h"
77#include "atomicio.h"
78#include "monitor_fdpass.h"
79#include "misc.h"
80#include "schnorr.h"
81#include "jpake.h"
82#include "uuencode.h"
83
84#include "channels.h"
85#include "session.h"
86#include "servconf.h"
87#include "roaming.h"
88
89/* Imports */
90extern int compat20;
91extern z_stream incoming_stream;
92extern z_stream outgoing_stream;
93extern struct monitor *pmonitor;
94extern Buffer loginmsg;
95extern ServerOptions options;
96
97void
98mm_log_handler(LogLevel level, const char *msg, void *ctx)
99{
100	Buffer log_msg;
101	struct monitor *mon = (struct monitor *)ctx;
102
103	if (mon->m_log_sendfd == -1)
104		fatal("%s: no log channel", __func__);
105
106	buffer_init(&log_msg);
107	/*
108	 * Placeholder for packet length. Will be filled in with the actual
109	 * packet length once the packet has been constucted. This saves
110	 * fragile math.
111	 */
112	buffer_put_int(&log_msg, 0);
113
114	buffer_put_int(&log_msg, level);
115	buffer_put_cstring(&log_msg, msg);
116	put_u32(buffer_ptr(&log_msg), buffer_len(&log_msg) - 4);
117	if (atomicio(vwrite, mon->m_log_sendfd, buffer_ptr(&log_msg),
118	    buffer_len(&log_msg)) != buffer_len(&log_msg))
119		fatal("%s: write: %s", __func__, strerror(errno));
120	buffer_free(&log_msg);
121}
122
123int
124mm_is_monitor(void)
125{
126	/*
127	 * m_pid is only set in the privileged part, and
128	 * points to the unprivileged child.
129	 */
130	return (pmonitor && pmonitor->m_pid > 0);
131}
132
133void
134mm_request_send(int sock, enum monitor_reqtype type, Buffer *m)
135{
136	u_int mlen = buffer_len(m);
137	u_char buf[5];
138
139	debug3("%s entering: type %d", __func__, type);
140
141	put_u32(buf, mlen + 1);
142	buf[4] = (u_char) type;		/* 1st byte of payload is mesg-type */
143	if (atomicio(vwrite, sock, buf, sizeof(buf)) != sizeof(buf))
144		fatal("%s: write: %s", __func__, strerror(errno));
145	if (atomicio(vwrite, sock, buffer_ptr(m), mlen) != mlen)
146		fatal("%s: write: %s", __func__, strerror(errno));
147}
148
149void
150mm_request_receive(int sock, Buffer *m)
151{
152	u_char buf[4];
153	u_int msg_len;
154
155	debug3("%s entering", __func__);
156
157	if (atomicio(read, sock, buf, sizeof(buf)) != sizeof(buf)) {
158		if (errno == EPIPE)
159			cleanup_exit(255);
160		fatal("%s: read: %s", __func__, strerror(errno));
161	}
162	msg_len = get_u32(buf);
163	if (msg_len > 256 * 1024)
164		fatal("%s: read: bad msg_len %d", __func__, msg_len);
165	buffer_clear(m);
166	buffer_append_space(m, msg_len);
167	if (atomicio(read, sock, buffer_ptr(m), msg_len) != msg_len)
168		fatal("%s: read: %s", __func__, strerror(errno));
169}
170
171void
172mm_request_receive_expect(int sock, enum monitor_reqtype type, Buffer *m)
173{
174	u_char rtype;
175
176	debug3("%s entering: type %d", __func__, type);
177
178	mm_request_receive(sock, m);
179	rtype = buffer_get_char(m);
180	if (rtype != type)
181		fatal("%s: read: rtype %d != type %d", __func__,
182		    rtype, type);
183}
184
185DH *
186mm_choose_dh(int min, int nbits, int max)
187{
188	BIGNUM *p, *g;
189	int success = 0;
190	Buffer m;
191
192	buffer_init(&m);
193	buffer_put_int(&m, min);
194	buffer_put_int(&m, nbits);
195	buffer_put_int(&m, max);
196
197	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, &m);
198
199	debug3("%s: waiting for MONITOR_ANS_MODULI", __func__);
200	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, &m);
201
202	success = buffer_get_char(&m);
203	if (success == 0)
204		fatal("%s: MONITOR_ANS_MODULI failed", __func__);
205
206	if ((p = BN_new()) == NULL)
207		fatal("%s: BN_new failed", __func__);
208	if ((g = BN_new()) == NULL)
209		fatal("%s: BN_new failed", __func__);
210	buffer_get_bignum2(&m, p);
211	buffer_get_bignum2(&m, g);
212
213	debug3("%s: remaining %d", __func__, buffer_len(&m));
214	buffer_free(&m);
215
216	return (dh_new_group(g, p));
217}
218
219int
220mm_key_sign(Key *key, u_char **sigp, u_int *lenp, u_char *data, u_int datalen)
221{
222	Kex *kex = *pmonitor->m_pkex;
223	Buffer m;
224
225	debug3("%s entering", __func__);
226
227	buffer_init(&m);
228	buffer_put_int(&m, kex->host_key_index(key));
229	buffer_put_string(&m, data, datalen);
230
231	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, &m);
232
233	debug3("%s: waiting for MONITOR_ANS_SIGN", __func__);
234	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, &m);
235	*sigp  = buffer_get_string(&m, lenp);
236	buffer_free(&m);
237
238	return (0);
239}
240
241struct passwd *
242mm_getpwnamallow(const char *username)
243{
244	Buffer m;
245	struct passwd *pw;
246	u_int len, i;
247	ServerOptions *newopts;
248
249	debug3("%s entering", __func__);
250
251	buffer_init(&m);
252	buffer_put_cstring(&m, username);
253
254	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, &m);
255
256	debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__);
257	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, &m);
258
259	if (buffer_get_char(&m) == 0) {
260		pw = NULL;
261		goto out;
262	}
263	pw = buffer_get_string(&m, &len);
264	if (len != sizeof(struct passwd))
265		fatal("%s: struct passwd size mismatch", __func__);
266	pw->pw_name = buffer_get_string(&m, NULL);
267	pw->pw_passwd = buffer_get_string(&m, NULL);
268	pw->pw_gecos = buffer_get_string(&m, NULL);
269#ifdef HAVE_PW_CLASS_IN_PASSWD
270	pw->pw_class = buffer_get_string(&m, NULL);
271#endif
272	pw->pw_dir = buffer_get_string(&m, NULL);
273	pw->pw_shell = buffer_get_string(&m, NULL);
274
275out:
276	/* copy options block as a Match directive may have changed some */
277	newopts = buffer_get_string(&m, &len);
278	if (len != sizeof(*newopts))
279		fatal("%s: option block size mismatch", __func__);
280
281#define M_CP_STROPT(x) do { \
282		if (newopts->x != NULL) \
283			newopts->x = buffer_get_string(&m, NULL); \
284	} while (0)
285#define M_CP_STRARRAYOPT(x, nx) do { \
286		for (i = 0; i < newopts->nx; i++) \
287			newopts->x[i] = buffer_get_string(&m, NULL); \
288	} while (0)
289	/* See comment in servconf.h */
290	COPY_MATCH_STRING_OPTS();
291#undef M_CP_STROPT
292#undef M_CP_STRARRAYOPT
293
294	copy_set_server_options(&options, newopts, 1);
295	xfree(newopts);
296
297	buffer_free(&m);
298
299	return (pw);
300}
301
302char *
303mm_auth2_read_banner(void)
304{
305	Buffer m;
306	char *banner;
307
308	debug3("%s entering", __func__);
309
310	buffer_init(&m);
311	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m);
312	buffer_clear(&m);
313
314	mm_request_receive_expect(pmonitor->m_recvfd,
315	    MONITOR_ANS_AUTH2_READ_BANNER, &m);
316	banner = buffer_get_string(&m, NULL);
317	buffer_free(&m);
318
319	/* treat empty banner as missing banner */
320	if (strlen(banner) == 0) {
321		xfree(banner);
322		banner = NULL;
323	}
324	return (banner);
325}
326
327/* Inform the privileged process about service and style */
328
329void
330mm_inform_authserv(char *service, char *style)
331{
332	Buffer m;
333
334	debug3("%s entering", __func__);
335
336	buffer_init(&m);
337	buffer_put_cstring(&m, service);
338	buffer_put_cstring(&m, style ? style : "");
339
340	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m);
341
342	buffer_free(&m);
343}
344
345/* Do the password authentication */
346int
347mm_auth_password(Authctxt *authctxt, char *password)
348{
349	Buffer m;
350	int authenticated = 0;
351
352	debug3("%s entering", __func__);
353
354	buffer_init(&m);
355	buffer_put_cstring(&m, password);
356	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m);
357
358	debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
359	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m);
360
361	authenticated = buffer_get_int(&m);
362
363	buffer_free(&m);
364
365	debug3("%s: user %sauthenticated",
366	    __func__, authenticated ? "" : "not ");
367	return (authenticated);
368}
369
370int
371mm_user_key_allowed(struct passwd *pw, Key *key)
372{
373	return (mm_key_allowed(MM_USERKEY, NULL, NULL, key));
374}
375
376int
377mm_hostbased_key_allowed(struct passwd *pw, char *user, char *host,
378    Key *key)
379{
380	return (mm_key_allowed(MM_HOSTKEY, user, host, key));
381}
382
383int
384mm_auth_rhosts_rsa_key_allowed(struct passwd *pw, char *user,
385    char *host, Key *key)
386{
387	int ret;
388
389	key->type = KEY_RSA; /* XXX hack for key_to_blob */
390	ret = mm_key_allowed(MM_RSAHOSTKEY, user, host, key);
391	key->type = KEY_RSA1;
392	return (ret);
393}
394
395int
396mm_key_allowed(enum mm_keytype type, char *user, char *host, Key *key)
397{
398	Buffer m;
399	u_char *blob;
400	u_int len;
401	int allowed = 0, have_forced = 0;
402
403	debug3("%s entering", __func__);
404
405	/* Convert the key to a blob and the pass it over */
406	if (!key_to_blob(key, &blob, &len))
407		return (0);
408
409	buffer_init(&m);
410	buffer_put_int(&m, type);
411	buffer_put_cstring(&m, user ? user : "");
412	buffer_put_cstring(&m, host ? host : "");
413	buffer_put_string(&m, blob, len);
414	xfree(blob);
415
416	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m);
417
418	debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
419	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m);
420
421	allowed = buffer_get_int(&m);
422
423	/* fake forced command */
424	auth_clear_options();
425	have_forced = buffer_get_int(&m);
426	forced_command = have_forced ? xstrdup("true") : NULL;
427
428	buffer_free(&m);
429
430	return (allowed);
431}
432
433/*
434 * This key verify needs to send the key type along, because the
435 * privileged parent makes the decision if the key is allowed
436 * for authentication.
437 */
438
439int
440mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
441{
442	Buffer m;
443	u_char *blob;
444	u_int len;
445	int verified = 0;
446
447	debug3("%s entering", __func__);
448
449	/* Convert the key to a blob and the pass it over */
450	if (!key_to_blob(key, &blob, &len))
451		return (0);
452
453	buffer_init(&m);
454	buffer_put_string(&m, blob, len);
455	buffer_put_string(&m, sig, siglen);
456	buffer_put_string(&m, data, datalen);
457	xfree(blob);
458
459	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m);
460
461	debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
462	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m);
463
464	verified = buffer_get_int(&m);
465
466	buffer_free(&m);
467
468	return (verified);
469}
470
471/* Export key state after authentication */
472Newkeys *
473mm_newkeys_from_blob(u_char *blob, int blen)
474{
475	Buffer b;
476	u_int len;
477	Newkeys *newkey = NULL;
478	Enc *enc;
479	Mac *mac;
480	Comp *comp;
481
482	debug3("%s: %p(%d)", __func__, blob, blen);
483#ifdef DEBUG_PK
484	dump_base64(stderr, blob, blen);
485#endif
486	buffer_init(&b);
487	buffer_append(&b, blob, blen);
488
489	newkey = xmalloc(sizeof(*newkey));
490	enc = &newkey->enc;
491	mac = &newkey->mac;
492	comp = &newkey->comp;
493
494	/* Enc structure */
495	enc->name = buffer_get_string(&b, NULL);
496	buffer_get(&b, &enc->cipher, sizeof(enc->cipher));
497	enc->enabled = buffer_get_int(&b);
498	enc->block_size = buffer_get_int(&b);
499	enc->key = buffer_get_string(&b, &enc->key_len);
500	enc->iv = buffer_get_string(&b, &enc->iv_len);
501
502	if (enc->name == NULL || cipher_by_name(enc->name) != enc->cipher)
503		fatal("%s: bad cipher name %s or pointer %p", __func__,
504		    enc->name, enc->cipher);
505
506	/* Mac structure */
507	if (cipher_authlen(enc->cipher) == 0) {
508		mac->name = buffer_get_string(&b, NULL);
509		if (mac->name == NULL || mac_setup(mac, mac->name) == -1)
510			fatal("%s: can not setup mac %s", __func__, mac->name);
511		mac->enabled = buffer_get_int(&b);
512		mac->key = buffer_get_string(&b, &len);
513		if (len > mac->key_len)
514			fatal("%s: bad mac key length: %u > %d", __func__, len,
515			    mac->key_len);
516		mac->key_len = len;
517	}
518
519	/* Comp structure */
520	comp->type = buffer_get_int(&b);
521	comp->enabled = buffer_get_int(&b);
522	comp->name = buffer_get_string(&b, NULL);
523
524	len = buffer_len(&b);
525	if (len != 0)
526		error("newkeys_from_blob: remaining bytes in blob %u", len);
527	buffer_free(&b);
528	return (newkey);
529}
530
531int
532mm_newkeys_to_blob(int mode, u_char **blobp, u_int *lenp)
533{
534	Buffer b;
535	int len;
536	Enc *enc;
537	Mac *mac;
538	Comp *comp;
539	Newkeys *newkey = (Newkeys *)packet_get_newkeys(mode);
540
541	debug3("%s: converting %p", __func__, newkey);
542
543	if (newkey == NULL) {
544		error("%s: newkey == NULL", __func__);
545		return 0;
546	}
547	enc = &newkey->enc;
548	mac = &newkey->mac;
549	comp = &newkey->comp;
550
551	buffer_init(&b);
552	/* Enc structure */
553	buffer_put_cstring(&b, enc->name);
554	/* The cipher struct is constant and shared, you export pointer */
555	buffer_append(&b, &enc->cipher, sizeof(enc->cipher));
556	buffer_put_int(&b, enc->enabled);
557	buffer_put_int(&b, enc->block_size);
558	buffer_put_string(&b, enc->key, enc->key_len);
559	packet_get_keyiv(mode, enc->iv, enc->iv_len);
560	buffer_put_string(&b, enc->iv, enc->iv_len);
561
562	/* Mac structure */
563	if (cipher_authlen(enc->cipher) == 0) {
564		buffer_put_cstring(&b, mac->name);
565		buffer_put_int(&b, mac->enabled);
566		buffer_put_string(&b, mac->key, mac->key_len);
567	}
568
569	/* Comp structure */
570	buffer_put_int(&b, comp->type);
571	buffer_put_int(&b, comp->enabled);
572	buffer_put_cstring(&b, comp->name);
573
574	len = buffer_len(&b);
575	if (lenp != NULL)
576		*lenp = len;
577	if (blobp != NULL) {
578		*blobp = xmalloc(len);
579		memcpy(*blobp, buffer_ptr(&b), len);
580	}
581	memset(buffer_ptr(&b), 0, len);
582	buffer_free(&b);
583	return len;
584}
585
586static void
587mm_send_kex(Buffer *m, Kex *kex)
588{
589	buffer_put_string(m, kex->session_id, kex->session_id_len);
590	buffer_put_int(m, kex->we_need);
591	buffer_put_int(m, kex->hostkey_type);
592	buffer_put_int(m, kex->kex_type);
593	buffer_put_string(m, buffer_ptr(&kex->my), buffer_len(&kex->my));
594	buffer_put_string(m, buffer_ptr(&kex->peer), buffer_len(&kex->peer));
595	buffer_put_int(m, kex->flags);
596	buffer_put_cstring(m, kex->client_version_string);
597	buffer_put_cstring(m, kex->server_version_string);
598}
599
600void
601mm_send_keystate(struct monitor *monitor)
602{
603	Buffer m, *input, *output;
604	u_char *blob, *p;
605	u_int bloblen, plen;
606	u_int32_t seqnr, packets;
607	u_int64_t blocks, bytes;
608
609	buffer_init(&m);
610
611	if (!compat20) {
612		u_char iv[24];
613		u_char *key;
614		u_int ivlen, keylen;
615
616		buffer_put_int(&m, packet_get_protocol_flags());
617
618		buffer_put_int(&m, packet_get_ssh1_cipher());
619
620		debug3("%s: Sending ssh1 KEY+IV", __func__);
621		keylen = packet_get_encryption_key(NULL);
622		key = xmalloc(keylen+1);	/* add 1 if keylen == 0 */
623		keylen = packet_get_encryption_key(key);
624		buffer_put_string(&m, key, keylen);
625		memset(key, 0, keylen);
626		xfree(key);
627
628		ivlen = packet_get_keyiv_len(MODE_OUT);
629		packet_get_keyiv(MODE_OUT, iv, ivlen);
630		buffer_put_string(&m, iv, ivlen);
631		ivlen = packet_get_keyiv_len(MODE_IN);
632		packet_get_keyiv(MODE_IN, iv, ivlen);
633		buffer_put_string(&m, iv, ivlen);
634		goto skip;
635	} else {
636		/* Kex for rekeying */
637		mm_send_kex(&m, *monitor->m_pkex);
638	}
639
640	debug3("%s: Sending new keys: %p %p",
641	    __func__, packet_get_newkeys(MODE_OUT),
642	    packet_get_newkeys(MODE_IN));
643
644	/* Keys from Kex */
645	if (!mm_newkeys_to_blob(MODE_OUT, &blob, &bloblen))
646		fatal("%s: conversion of newkeys failed", __func__);
647
648	buffer_put_string(&m, blob, bloblen);
649	xfree(blob);
650
651	if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen))
652		fatal("%s: conversion of newkeys failed", __func__);
653
654	buffer_put_string(&m, blob, bloblen);
655	xfree(blob);
656
657	packet_get_state(MODE_OUT, &seqnr, &blocks, &packets, &bytes);
658	buffer_put_int(&m, seqnr);
659	buffer_put_int64(&m, blocks);
660	buffer_put_int(&m, packets);
661	buffer_put_int64(&m, bytes);
662	packet_get_state(MODE_IN, &seqnr, &blocks, &packets, &bytes);
663	buffer_put_int(&m, seqnr);
664	buffer_put_int64(&m, blocks);
665	buffer_put_int(&m, packets);
666	buffer_put_int64(&m, bytes);
667
668	debug3("%s: New keys have been sent", __func__);
669 skip:
670	/* More key context */
671	plen = packet_get_keycontext(MODE_OUT, NULL);
672	p = xmalloc(plen+1);
673	packet_get_keycontext(MODE_OUT, p);
674	buffer_put_string(&m, p, plen);
675	xfree(p);
676
677	plen = packet_get_keycontext(MODE_IN, NULL);
678	p = xmalloc(plen+1);
679	packet_get_keycontext(MODE_IN, p);
680	buffer_put_string(&m, p, plen);
681	xfree(p);
682
683	/* Compression state */
684	debug3("%s: Sending compression state", __func__);
685	buffer_put_string(&m, &outgoing_stream, sizeof(outgoing_stream));
686	buffer_put_string(&m, &incoming_stream, sizeof(incoming_stream));
687
688	/* Network I/O buffers */
689	input = (Buffer *)packet_get_input();
690	output = (Buffer *)packet_get_output();
691	buffer_put_string(&m, buffer_ptr(input), buffer_len(input));
692	buffer_put_string(&m, buffer_ptr(output), buffer_len(output));
693
694	/* Roaming */
695	if (compat20) {
696		buffer_put_int64(&m, get_sent_bytes());
697		buffer_put_int64(&m, get_recv_bytes());
698	}
699
700	mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m);
701	debug3("%s: Finished sending state", __func__);
702
703	buffer_free(&m);
704}
705
706int
707mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
708{
709	Buffer m;
710	char *p, *msg;
711	int success = 0, tmp1 = -1, tmp2 = -1;
712
713	/* Kludge: ensure there are fds free to receive the pty/tty */
714	if ((tmp1 = dup(pmonitor->m_recvfd)) == -1 ||
715	    (tmp2 = dup(pmonitor->m_recvfd)) == -1) {
716		error("%s: cannot allocate fds for pty", __func__);
717		if (tmp1 > 0)
718			close(tmp1);
719		if (tmp2 > 0)
720			close(tmp2);
721		return 0;
722	}
723	close(tmp1);
724	close(tmp2);
725
726	buffer_init(&m);
727	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
728
729	debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
730	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
731
732	success = buffer_get_int(&m);
733	if (success == 0) {
734		debug3("%s: pty alloc failed", __func__);
735		buffer_free(&m);
736		return (0);
737	}
738	p = buffer_get_string(&m, NULL);
739	msg = buffer_get_string(&m, NULL);
740	buffer_free(&m);
741
742	strlcpy(namebuf, p, namebuflen); /* Possible truncation */
743	xfree(p);
744
745	buffer_append(&loginmsg, msg, strlen(msg));
746	xfree(msg);
747
748	if ((*ptyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1 ||
749	    (*ttyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1)
750		fatal("%s: receive fds failed", __func__);
751
752	/* Success */
753	return (1);
754}
755
756void
757mm_session_pty_cleanup2(Session *s)
758{
759	Buffer m;
760
761	if (s->ttyfd == -1)
762		return;
763	buffer_init(&m);
764	buffer_put_cstring(&m, s->tty);
765	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
766	buffer_free(&m);
767
768	/* closed dup'ed master */
769	if (s->ptymaster != -1 && close(s->ptymaster) < 0)
770		error("close(s->ptymaster/%d): %s",
771		    s->ptymaster, strerror(errno));
772
773	/* unlink pty from session */
774	s->ttyfd = -1;
775}
776
777#ifdef USE_PAM
778void
779mm_start_pam(Authctxt *authctxt)
780{
781	Buffer m;
782
783	debug3("%s entering", __func__);
784	if (!options.use_pam)
785		fatal("UsePAM=no, but ended up in %s anyway", __func__);
786
787	buffer_init(&m);
788	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, &m);
789
790	buffer_free(&m);
791}
792
793u_int
794mm_do_pam_account(void)
795{
796	Buffer m;
797	u_int ret;
798	char *msg;
799
800	debug3("%s entering", __func__);
801	if (!options.use_pam)
802		fatal("UsePAM=no, but ended up in %s anyway", __func__);
803
804	buffer_init(&m);
805	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_ACCOUNT, &m);
806
807	mm_request_receive_expect(pmonitor->m_recvfd,
808	    MONITOR_ANS_PAM_ACCOUNT, &m);
809	ret = buffer_get_int(&m);
810	msg = buffer_get_string(&m, NULL);
811	buffer_append(&loginmsg, msg, strlen(msg));
812	xfree(msg);
813
814	buffer_free(&m);
815
816	debug3("%s returning %d", __func__, ret);
817
818	return (ret);
819}
820
821void *
822mm_sshpam_init_ctx(Authctxt *authctxt)
823{
824	Buffer m;
825	int success;
826
827	debug3("%s", __func__);
828	buffer_init(&m);
829	buffer_put_cstring(&m, authctxt->user);
830	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_INIT_CTX, &m);
831	debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__);
832	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_INIT_CTX, &m);
833	success = buffer_get_int(&m);
834	if (success == 0) {
835		debug3("%s: pam_init_ctx failed", __func__);
836		buffer_free(&m);
837		return (NULL);
838	}
839	buffer_free(&m);
840	return (authctxt);
841}
842
843int
844mm_sshpam_query(void *ctx, char **name, char **info,
845    u_int *num, char ***prompts, u_int **echo_on)
846{
847	Buffer m;
848	u_int i;
849	int ret;
850
851	debug3("%s", __func__);
852	buffer_init(&m);
853	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_QUERY, &m);
854	debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__);
855	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_QUERY, &m);
856	ret = buffer_get_int(&m);
857	debug3("%s: pam_query returned %d", __func__, ret);
858	*name = buffer_get_string(&m, NULL);
859	*info = buffer_get_string(&m, NULL);
860	*num = buffer_get_int(&m);
861	if (*num > PAM_MAX_NUM_MSG)
862		fatal("%s: recieved %u PAM messages, expected <= %u",
863		    __func__, *num, PAM_MAX_NUM_MSG);
864	*prompts = xcalloc((*num + 1), sizeof(char *));
865	*echo_on = xcalloc((*num + 1), sizeof(u_int));
866	for (i = 0; i < *num; ++i) {
867		(*prompts)[i] = buffer_get_string(&m, NULL);
868		(*echo_on)[i] = buffer_get_int(&m);
869	}
870	buffer_free(&m);
871	return (ret);
872}
873
874int
875mm_sshpam_respond(void *ctx, u_int num, char **resp)
876{
877	Buffer m;
878	u_int i;
879	int ret;
880
881	debug3("%s", __func__);
882	buffer_init(&m);
883	buffer_put_int(&m, num);
884	for (i = 0; i < num; ++i)
885		buffer_put_cstring(&m, resp[i]);
886	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_RESPOND, &m);
887	debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__);
888	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_RESPOND, &m);
889	ret = buffer_get_int(&m);
890	debug3("%s: pam_respond returned %d", __func__, ret);
891	buffer_free(&m);
892	return (ret);
893}
894
895void
896mm_sshpam_free_ctx(void *ctxtp)
897{
898	Buffer m;
899
900	debug3("%s", __func__);
901	buffer_init(&m);
902	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_FREE_CTX, &m);
903	debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__);
904	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_FREE_CTX, &m);
905	buffer_free(&m);
906}
907#endif /* USE_PAM */
908
909/* Request process termination */
910
911void
912mm_terminate(void)
913{
914	Buffer m;
915
916	buffer_init(&m);
917	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
918	buffer_free(&m);
919}
920
921int
922mm_ssh1_session_key(BIGNUM *num)
923{
924	int rsafail;
925	Buffer m;
926
927	buffer_init(&m);
928	buffer_put_bignum2(&m, num);
929	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m);
930
931	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m);
932
933	rsafail = buffer_get_int(&m);
934	buffer_get_bignum2(&m, num);
935
936	buffer_free(&m);
937
938	return (rsafail);
939}
940
941static void
942mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
943    char ***prompts, u_int **echo_on)
944{
945	*name = xstrdup("");
946	*infotxt = xstrdup("");
947	*numprompts = 1;
948	*prompts = xcalloc(*numprompts, sizeof(char *));
949	*echo_on = xcalloc(*numprompts, sizeof(u_int));
950	(*echo_on)[0] = 0;
951}
952
953int
954mm_bsdauth_query(void *ctx, char **name, char **infotxt,
955   u_int *numprompts, char ***prompts, u_int **echo_on)
956{
957	Buffer m;
958	u_int success;
959	char *challenge;
960
961	debug3("%s: entering", __func__);
962
963	buffer_init(&m);
964	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
965
966	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
967	    &m);
968	success = buffer_get_int(&m);
969	if (success == 0) {
970		debug3("%s: no challenge", __func__);
971		buffer_free(&m);
972		return (-1);
973	}
974
975	/* Get the challenge, and format the response */
976	challenge  = buffer_get_string(&m, NULL);
977	buffer_free(&m);
978
979	mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
980	(*prompts)[0] = challenge;
981
982	debug3("%s: received challenge: %s", __func__, challenge);
983
984	return (0);
985}
986
987int
988mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
989{
990	Buffer m;
991	int authok;
992
993	debug3("%s: entering", __func__);
994	if (numresponses != 1)
995		return (-1);
996
997	buffer_init(&m);
998	buffer_put_cstring(&m, responses[0]);
999	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
1000
1001	mm_request_receive_expect(pmonitor->m_recvfd,
1002	    MONITOR_ANS_BSDAUTHRESPOND, &m);
1003
1004	authok = buffer_get_int(&m);
1005	buffer_free(&m);
1006
1007	return ((authok == 0) ? -1 : 0);
1008}
1009
1010#ifdef SKEY
1011int
1012mm_skey_query(void *ctx, char **name, char **infotxt,
1013   u_int *numprompts, char ***prompts, u_int **echo_on)
1014{
1015	Buffer m;
1016	u_int success;
1017	char *challenge;
1018
1019	debug3("%s: entering", __func__);
1020
1021	buffer_init(&m);
1022	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m);
1023
1024	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY,
1025	    &m);
1026	success = buffer_get_int(&m);
1027	if (success == 0) {
1028		debug3("%s: no challenge", __func__);
1029		buffer_free(&m);
1030		return (-1);
1031	}
1032
1033	/* Get the challenge, and format the response */
1034	challenge  = buffer_get_string(&m, NULL);
1035	buffer_free(&m);
1036
1037	debug3("%s: received challenge: %s", __func__, challenge);
1038
1039	mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
1040
1041	xasprintf(*prompts, "%s%s", challenge, SKEY_PROMPT);
1042	xfree(challenge);
1043
1044	return (0);
1045}
1046
1047int
1048mm_skey_respond(void *ctx, u_int numresponses, char **responses)
1049{
1050	Buffer m;
1051	int authok;
1052
1053	debug3("%s: entering", __func__);
1054	if (numresponses != 1)
1055		return (-1);
1056
1057	buffer_init(&m);
1058	buffer_put_cstring(&m, responses[0]);
1059	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m);
1060
1061	mm_request_receive_expect(pmonitor->m_recvfd,
1062	    MONITOR_ANS_SKEYRESPOND, &m);
1063
1064	authok = buffer_get_int(&m);
1065	buffer_free(&m);
1066
1067	return ((authok == 0) ? -1 : 0);
1068}
1069#endif /* SKEY */
1070
1071void
1072mm_ssh1_session_id(u_char session_id[16])
1073{
1074	Buffer m;
1075	int i;
1076
1077	debug3("%s entering", __func__);
1078
1079	buffer_init(&m);
1080	for (i = 0; i < 16; i++)
1081		buffer_put_char(&m, session_id[i]);
1082
1083	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m);
1084	buffer_free(&m);
1085}
1086
1087int
1088mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
1089{
1090	Buffer m;
1091	Key *key;
1092	u_char *blob;
1093	u_int blen;
1094	int allowed = 0, have_forced = 0;
1095
1096	debug3("%s entering", __func__);
1097
1098	buffer_init(&m);
1099	buffer_put_bignum2(&m, client_n);
1100
1101	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m);
1102	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m);
1103
1104	allowed = buffer_get_int(&m);
1105
1106	/* fake forced command */
1107	auth_clear_options();
1108	have_forced = buffer_get_int(&m);
1109	forced_command = have_forced ? xstrdup("true") : NULL;
1110
1111	if (allowed && rkey != NULL) {
1112		blob = buffer_get_string(&m, &blen);
1113		if ((key = key_from_blob(blob, blen)) == NULL)
1114			fatal("%s: key_from_blob failed", __func__);
1115		*rkey = key;
1116		xfree(blob);
1117	}
1118	buffer_free(&m);
1119
1120	return (allowed);
1121}
1122
1123BIGNUM *
1124mm_auth_rsa_generate_challenge(Key *key)
1125{
1126	Buffer m;
1127	BIGNUM *challenge;
1128	u_char *blob;
1129	u_int blen;
1130
1131	debug3("%s entering", __func__);
1132
1133	if ((challenge = BN_new()) == NULL)
1134		fatal("%s: BN_new failed", __func__);
1135
1136	key->type = KEY_RSA;    /* XXX cheat for key_to_blob */
1137	if (key_to_blob(key, &blob, &blen) == 0)
1138		fatal("%s: key_to_blob failed", __func__);
1139	key->type = KEY_RSA1;
1140
1141	buffer_init(&m);
1142	buffer_put_string(&m, blob, blen);
1143	xfree(blob);
1144
1145	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m);
1146	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m);
1147
1148	buffer_get_bignum2(&m, challenge);
1149	buffer_free(&m);
1150
1151	return (challenge);
1152}
1153
1154int
1155mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16])
1156{
1157	Buffer m;
1158	u_char *blob;
1159	u_int blen;
1160	int success = 0;
1161
1162	debug3("%s entering", __func__);
1163
1164	key->type = KEY_RSA;    /* XXX cheat for key_to_blob */
1165	if (key_to_blob(key, &blob, &blen) == 0)
1166		fatal("%s: key_to_blob failed", __func__);
1167	key->type = KEY_RSA1;
1168
1169	buffer_init(&m);
1170	buffer_put_string(&m, blob, blen);
1171	buffer_put_string(&m, response, 16);
1172	xfree(blob);
1173
1174	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m);
1175	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m);
1176
1177	success = buffer_get_int(&m);
1178	buffer_free(&m);
1179
1180	return (success);
1181}
1182
1183#ifdef SSH_AUDIT_EVENTS
1184void
1185mm_audit_event(ssh_audit_event_t event)
1186{
1187	Buffer m;
1188
1189	debug3("%s entering", __func__);
1190
1191	buffer_init(&m);
1192	buffer_put_int(&m, event);
1193
1194	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_EVENT, &m);
1195	buffer_free(&m);
1196}
1197
1198void
1199mm_audit_run_command(const char *command)
1200{
1201	Buffer m;
1202
1203	debug3("%s entering command %s", __func__, command);
1204
1205	buffer_init(&m);
1206	buffer_put_cstring(&m, command);
1207
1208	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, &m);
1209	buffer_free(&m);
1210}
1211#endif /* SSH_AUDIT_EVENTS */
1212
1213#ifdef GSSAPI
1214OM_uint32
1215mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
1216{
1217	Buffer m;
1218	OM_uint32 major;
1219
1220	/* Client doesn't get to see the context */
1221	*ctx = NULL;
1222
1223	buffer_init(&m);
1224	buffer_put_string(&m, goid->elements, goid->length);
1225
1226	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m);
1227	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m);
1228
1229	major = buffer_get_int(&m);
1230
1231	buffer_free(&m);
1232	return (major);
1233}
1234
1235OM_uint32
1236mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
1237    gss_buffer_desc *out, OM_uint32 *flags)
1238{
1239	Buffer m;
1240	OM_uint32 major;
1241	u_int len;
1242
1243	buffer_init(&m);
1244	buffer_put_string(&m, in->value, in->length);
1245
1246	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m);
1247	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m);
1248
1249	major = buffer_get_int(&m);
1250	out->value = buffer_get_string(&m, &len);
1251	out->length = len;
1252	if (flags)
1253		*flags = buffer_get_int(&m);
1254
1255	buffer_free(&m);
1256
1257	return (major);
1258}
1259
1260OM_uint32
1261mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
1262{
1263	Buffer m;
1264	OM_uint32 major;
1265
1266	buffer_init(&m);
1267	buffer_put_string(&m, gssbuf->value, gssbuf->length);
1268	buffer_put_string(&m, gssmic->value, gssmic->length);
1269
1270	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, &m);
1271	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSCHECKMIC,
1272	    &m);
1273
1274	major = buffer_get_int(&m);
1275	buffer_free(&m);
1276	return(major);
1277}
1278
1279int
1280mm_ssh_gssapi_userok(char *user, struct passwd *pw)
1281{
1282	Buffer m;
1283	int authenticated = 0;
1284
1285	buffer_init(&m);
1286
1287	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m);
1288	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK,
1289				  &m);
1290
1291	authenticated = buffer_get_int(&m);
1292
1293	buffer_free(&m);
1294	debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
1295	return (authenticated);
1296}
1297
1298OM_uint32
1299mm_ssh_gssapi_sign(Gssctxt *ctx, gss_buffer_desc *data, gss_buffer_desc *hash)
1300{
1301	Buffer m;
1302	OM_uint32 major;
1303	u_int len;
1304
1305	buffer_init(&m);
1306	buffer_put_string(&m, data->value, data->length);
1307
1308	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSIGN, &m);
1309	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSIGN, &m);
1310
1311	major = buffer_get_int(&m);
1312	hash->value = buffer_get_string(&m, &len);
1313	hash->length = len;
1314
1315	buffer_free(&m);
1316
1317	return(major);
1318}
1319
1320int
1321mm_ssh_gssapi_update_creds(ssh_gssapi_ccache *store)
1322{
1323	Buffer m;
1324	int ok;
1325
1326	buffer_init(&m);
1327
1328	buffer_put_cstring(&m, store->filename ? store->filename : "");
1329	buffer_put_cstring(&m, store->envvar ? store->envvar : "");
1330	buffer_put_cstring(&m, store->envval ? store->envval : "");
1331
1332	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUPCREDS, &m);
1333	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUPCREDS, &m);
1334
1335	ok = buffer_get_int(&m);
1336
1337	buffer_free(&m);
1338
1339	return (ok);
1340}
1341
1342#endif /* GSSAPI */
1343
1344#ifdef JPAKE
1345void
1346mm_auth2_jpake_get_pwdata(Authctxt *authctxt, BIGNUM **s,
1347    char **hash_scheme, char **salt)
1348{
1349	Buffer m;
1350
1351	debug3("%s entering", __func__);
1352
1353	buffer_init(&m);
1354	mm_request_send(pmonitor->m_recvfd,
1355	    MONITOR_REQ_JPAKE_GET_PWDATA, &m);
1356
1357	debug3("%s: waiting for MONITOR_ANS_JPAKE_GET_PWDATA", __func__);
1358	mm_request_receive_expect(pmonitor->m_recvfd,
1359	    MONITOR_ANS_JPAKE_GET_PWDATA, &m);
1360
1361	*hash_scheme = buffer_get_string(&m, NULL);
1362	*salt = buffer_get_string(&m, NULL);
1363
1364	buffer_free(&m);
1365}
1366
1367void
1368mm_jpake_step1(struct modp_group *grp,
1369    u_char **id, u_int *id_len,
1370    BIGNUM **priv1, BIGNUM **priv2, BIGNUM **g_priv1, BIGNUM **g_priv2,
1371    u_char **priv1_proof, u_int *priv1_proof_len,
1372    u_char **priv2_proof, u_int *priv2_proof_len)
1373{
1374	Buffer m;
1375
1376	debug3("%s entering", __func__);
1377
1378	buffer_init(&m);
1379	mm_request_send(pmonitor->m_recvfd,
1380	    MONITOR_REQ_JPAKE_STEP1, &m);
1381
1382	debug3("%s: waiting for MONITOR_ANS_JPAKE_STEP1", __func__);
1383	mm_request_receive_expect(pmonitor->m_recvfd,
1384	    MONITOR_ANS_JPAKE_STEP1, &m);
1385
1386	if ((*priv1 = BN_new()) == NULL ||
1387	    (*priv2 = BN_new()) == NULL ||
1388	    (*g_priv1 = BN_new()) == NULL ||
1389	    (*g_priv2 = BN_new()) == NULL)
1390		fatal("%s: BN_new", __func__);
1391
1392	*id = buffer_get_string(&m, id_len);
1393	/* priv1 and priv2 are, well, private */
1394	buffer_get_bignum2(&m, *g_priv1);
1395	buffer_get_bignum2(&m, *g_priv2);
1396	*priv1_proof = buffer_get_string(&m, priv1_proof_len);
1397	*priv2_proof = buffer_get_string(&m, priv2_proof_len);
1398
1399	buffer_free(&m);
1400}
1401
1402void
1403mm_jpake_step2(struct modp_group *grp, BIGNUM *s,
1404    BIGNUM *mypub1, BIGNUM *theirpub1, BIGNUM *theirpub2, BIGNUM *mypriv2,
1405    const u_char *theirid, u_int theirid_len,
1406    const u_char *myid, u_int myid_len,
1407    const u_char *theirpub1_proof, u_int theirpub1_proof_len,
1408    const u_char *theirpub2_proof, u_int theirpub2_proof_len,
1409    BIGNUM **newpub,
1410    u_char **newpub_exponent_proof, u_int *newpub_exponent_proof_len)
1411{
1412	Buffer m;
1413
1414	debug3("%s entering", __func__);
1415
1416	buffer_init(&m);
1417	/* monitor already has all bignums except theirpub1, theirpub2 */
1418	buffer_put_bignum2(&m, theirpub1);
1419	buffer_put_bignum2(&m, theirpub2);
1420	/* monitor already knows our id */
1421	buffer_put_string(&m, theirid, theirid_len);
1422	buffer_put_string(&m, theirpub1_proof, theirpub1_proof_len);
1423	buffer_put_string(&m, theirpub2_proof, theirpub2_proof_len);
1424
1425	mm_request_send(pmonitor->m_recvfd,
1426	    MONITOR_REQ_JPAKE_STEP2, &m);
1427
1428	debug3("%s: waiting for MONITOR_ANS_JPAKE_STEP2", __func__);
1429	mm_request_receive_expect(pmonitor->m_recvfd,
1430	    MONITOR_ANS_JPAKE_STEP2, &m);
1431
1432	if ((*newpub = BN_new()) == NULL)
1433		fatal("%s: BN_new", __func__);
1434
1435	buffer_get_bignum2(&m, *newpub);
1436	*newpub_exponent_proof = buffer_get_string(&m,
1437	    newpub_exponent_proof_len);
1438
1439	buffer_free(&m);
1440}
1441
1442void
1443mm_jpake_key_confirm(struct modp_group *grp, BIGNUM *s, BIGNUM *step2_val,
1444    BIGNUM *mypriv2, BIGNUM *mypub1, BIGNUM *mypub2,
1445    BIGNUM *theirpub1, BIGNUM *theirpub2,
1446    const u_char *my_id, u_int my_id_len,
1447    const u_char *their_id, u_int their_id_len,
1448    const u_char *sess_id, u_int sess_id_len,
1449    const u_char *theirpriv2_s_proof, u_int theirpriv2_s_proof_len,
1450    BIGNUM **k,
1451    u_char **confirm_hash, u_int *confirm_hash_len)
1452{
1453	Buffer m;
1454
1455	debug3("%s entering", __func__);
1456
1457	buffer_init(&m);
1458	/* monitor already has all bignums except step2_val */
1459	buffer_put_bignum2(&m, step2_val);
1460	/* monitor already knows all the ids */
1461	buffer_put_string(&m, theirpriv2_s_proof, theirpriv2_s_proof_len);
1462
1463	mm_request_send(pmonitor->m_recvfd,
1464	    MONITOR_REQ_JPAKE_KEY_CONFIRM, &m);
1465
1466	debug3("%s: waiting for MONITOR_ANS_JPAKE_KEY_CONFIRM", __func__);
1467	mm_request_receive_expect(pmonitor->m_recvfd,
1468	    MONITOR_ANS_JPAKE_KEY_CONFIRM, &m);
1469
1470	/* 'k' is sensitive and stays in the monitor */
1471	*confirm_hash = buffer_get_string(&m, confirm_hash_len);
1472
1473	buffer_free(&m);
1474}
1475
1476int
1477mm_jpake_check_confirm(const BIGNUM *k,
1478    const u_char *peer_id, u_int peer_id_len,
1479    const u_char *sess_id, u_int sess_id_len,
1480    const u_char *peer_confirm_hash, u_int peer_confirm_hash_len)
1481{
1482	Buffer m;
1483	int success = 0;
1484
1485	debug3("%s entering", __func__);
1486
1487	buffer_init(&m);
1488	/* k is dummy in slave, ignored */
1489	/* monitor knows all the ids */
1490	buffer_put_string(&m, peer_confirm_hash, peer_confirm_hash_len);
1491	mm_request_send(pmonitor->m_recvfd,
1492	    MONITOR_REQ_JPAKE_CHECK_CONFIRM, &m);
1493
1494	debug3("%s: waiting for MONITOR_ANS_JPAKE_CHECK_CONFIRM", __func__);
1495	mm_request_receive_expect(pmonitor->m_recvfd,
1496	    MONITOR_ANS_JPAKE_CHECK_CONFIRM, &m);
1497
1498	success = buffer_get_int(&m);
1499	buffer_free(&m);
1500
1501	debug3("%s: success = %d", __func__, success);
1502	return success;
1503}
1504#endif /* JPAKE */
1505