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