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