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