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