blob: 0986fc51827ca46f18740741461448bf88672932 [file] [log] [blame]
Damien Miller01ed2272008-11-05 16:20:46 +11001/* $OpenBSD: monitor_wrap.c,v 1.64 2008/11/04 08:22:13 djm Exp $ */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002/*
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"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000029
Damien Miller9f2abc42006-07-10 20:53:08 +100030#include <sys/types.h>
Darren Tucker1a3d6e72006-08-05 18:46:47 +100031#include <sys/uio.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100032
Darren Tucker39972492006-07-12 22:22:46 +100033#include <errno.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100034#include <pwd.h>
Damien Millerd7834352006-08-05 12:39:39 +100035#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100036#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100037#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100038#include <string.h>
Darren Tucker1a3d6e72006-08-05 18:46:47 +100039#include <unistd.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100040
Damien Millerd7834352006-08-05 12:39:39 +100041#include <openssl/bn.h>
42#include <openssl/dh.h>
Damien Miller01ed2272008-11-05 16:20:46 +110043#include <openssl/evp.h>
Damien Millerd7834352006-08-05 12:39:39 +100044
Damien Millerb84886b2008-05-19 15:05:07 +100045#include "openbsd-compat/sys-queue.h"
Damien Millerd7834352006-08-05 12:39:39 +100046#include "xmalloc.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000047#include "ssh.h"
48#include "dh.h"
Damien Millerd7834352006-08-05 12:39:39 +100049#include "buffer.h"
50#include "key.h"
51#include "cipher.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000052#include "kex.h"
Damien Millerd7834352006-08-05 12:39:39 +100053#include "hostfile.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000054#include "auth.h"
Damien Miller06ebedf2003-02-24 12:03:38 +110055#include "auth-options.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000056#include "packet.h"
57#include "mac.h"
58#include "log.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000059#ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
60#undef TARGET_OS_MAC
Ben Lindstrom1b9f2a62004-04-08 05:11:03 +000061#include "zlib.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000062#define TARGET_OS_MAC 1
63#else
64#include "zlib.h"
65#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000066#include "monitor.h"
Damien Millerd7834352006-08-05 12:39:39 +100067#ifdef GSSAPI
68#include "ssh-gss.h"
69#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000070#include "monitor_wrap.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000071#include "atomicio.h"
72#include "monitor_fdpass.h"
Damien Miller3f941882006-03-31 23:13:02 +110073#include "misc.h"
Damien Miller01ed2272008-11-05 16:20:46 +110074#include "jpake.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000075
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000076#include "channels.h"
77#include "session.h"
Darren Tucker1629c072007-02-19 22:25:37 +110078#include "servconf.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000079
80/* Imports */
81extern int compat20;
82extern Newkeys *newkeys[];
83extern z_stream incoming_stream;
84extern z_stream outgoing_stream;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +000085extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000086extern Buffer input, output;
Darren Tucker09991742004-07-17 17:05:14 +100087extern Buffer loginmsg;
Damien Miller4e448a32003-05-14 15:11:48 +100088extern ServerOptions options;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000089
Darren Tucker3e33cec2003-10-02 16:12:36 +100090int
91mm_is_monitor(void)
92{
93 /*
94 * m_pid is only set in the privileged part, and
95 * points to the unprivileged child.
96 */
Darren Tuckera47c9bc2003-11-03 20:03:25 +110097 return (pmonitor && pmonitor->m_pid > 0);
Darren Tucker3e33cec2003-10-02 16:12:36 +100098}
99
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000100void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000101mm_request_send(int sock, enum monitor_reqtype type, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000102{
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000103 u_int mlen = buffer_len(m);
Ben Lindstromb1bdc5a2002-07-04 00:09:26 +0000104 u_char buf[5];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000105
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000106 debug3("%s entering: type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000107
Damien Miller3f941882006-03-31 23:13:02 +1100108 put_u32(buf, mlen + 1);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000109 buf[4] = (u_char) type; /* 1st byte of payload is mesg-type */
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000110 if (atomicio(vwrite, sock, buf, sizeof(buf)) != sizeof(buf))
Damien Millerb253cc42005-05-26 12:23:44 +1000111 fatal("%s: write: %s", __func__, strerror(errno));
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000112 if (atomicio(vwrite, sock, buffer_ptr(m), mlen) != mlen)
Damien Millerb253cc42005-05-26 12:23:44 +1000113 fatal("%s: write: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000114}
115
116void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000117mm_request_receive(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000118{
119 u_char buf[4];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000120 u_int msg_len;
121
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000122 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000123
Damien Millerb253cc42005-05-26 12:23:44 +1000124 if (atomicio(read, sock, buf, sizeof(buf)) != sizeof(buf)) {
125 if (errno == EPIPE)
Darren Tucker3e33cec2003-10-02 16:12:36 +1000126 cleanup_exit(255);
Damien Millerb253cc42005-05-26 12:23:44 +1000127 fatal("%s: read: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000128 }
Damien Miller3f941882006-03-31 23:13:02 +1100129 msg_len = get_u32(buf);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000130 if (msg_len > 256 * 1024)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000131 fatal("%s: read: bad msg_len %d", __func__, msg_len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000132 buffer_clear(m);
133 buffer_append_space(m, msg_len);
Damien Millerb253cc42005-05-26 12:23:44 +1000134 if (atomicio(read, sock, buffer_ptr(m), msg_len) != msg_len)
135 fatal("%s: read: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000136}
137
138void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000139mm_request_receive_expect(int sock, enum monitor_reqtype type, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000140{
141 u_char rtype;
142
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000143 debug3("%s entering: type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000144
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000145 mm_request_receive(sock, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000146 rtype = buffer_get_char(m);
147 if (rtype != type)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000148 fatal("%s: read: rtype %d != type %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000149 rtype, type);
150}
151
152DH *
153mm_choose_dh(int min, int nbits, int max)
154{
155 BIGNUM *p, *g;
156 int success = 0;
157 Buffer m;
158
159 buffer_init(&m);
160 buffer_put_int(&m, min);
161 buffer_put_int(&m, nbits);
162 buffer_put_int(&m, max);
163
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000164 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000165
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000166 debug3("%s: waiting for MONITOR_ANS_MODULI", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000167 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000168
169 success = buffer_get_char(&m);
170 if (success == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000171 fatal("%s: MONITOR_ANS_MODULI failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000172
173 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000174 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000175 if ((g = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000176 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000177 buffer_get_bignum2(&m, p);
178 buffer_get_bignum2(&m, g);
179
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000180 debug3("%s: remaining %d", __func__, buffer_len(&m));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000181 buffer_free(&m);
182
183 return (dh_new_group(g, p));
184}
185
186int
187mm_key_sign(Key *key, u_char **sigp, u_int *lenp, u_char *data, u_int datalen)
188{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000189 Kex *kex = *pmonitor->m_pkex;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000190 Buffer m;
191
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000192 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000193
194 buffer_init(&m);
195 buffer_put_int(&m, kex->host_key_index(key));
196 buffer_put_string(&m, data, datalen);
197
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000198 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000199
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000200 debug3("%s: waiting for MONITOR_ANS_SIGN", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000201 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000202 *sigp = buffer_get_string(&m, lenp);
203 buffer_free(&m);
204
205 return (0);
206}
207
208struct passwd *
Darren Tuckerb09b6772004-06-22 15:06:46 +1000209mm_getpwnamallow(const char *username)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000210{
211 Buffer m;
212 struct passwd *pw;
Darren Tucker1629c072007-02-19 22:25:37 +1100213 u_int len;
214 ServerOptions *newopts;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000215
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000216 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000217
218 buffer_init(&m);
Darren Tuckerb09b6772004-06-22 15:06:46 +1000219 buffer_put_cstring(&m, username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000220
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000221 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000222
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000223 debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000224 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000225
226 if (buffer_get_char(&m) == 0) {
Darren Tucker2f8b3d92007-12-02 23:02:15 +1100227 pw = NULL;
228 goto out;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000229 }
Darren Tucker1629c072007-02-19 22:25:37 +1100230 pw = buffer_get_string(&m, &len);
231 if (len != sizeof(struct passwd))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000232 fatal("%s: struct passwd size mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000233 pw->pw_name = buffer_get_string(&m, NULL);
234 pw->pw_passwd = buffer_get_string(&m, NULL);
235 pw->pw_gecos = buffer_get_string(&m, NULL);
Kevin Steves7e147602002-03-22 18:07:17 +0000236#ifdef HAVE_PW_CLASS_IN_PASSWD
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000237 pw->pw_class = buffer_get_string(&m, NULL);
Kevin Steves7e147602002-03-22 18:07:17 +0000238#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000239 pw->pw_dir = buffer_get_string(&m, NULL);
240 pw->pw_shell = buffer_get_string(&m, NULL);
Darren Tucker1629c072007-02-19 22:25:37 +1100241
Darren Tucker2f8b3d92007-12-02 23:02:15 +1100242out:
Darren Tucker1629c072007-02-19 22:25:37 +1100243 /* copy options block as a Match directive may have changed some */
244 newopts = buffer_get_string(&m, &len);
245 if (len != sizeof(*newopts))
246 fatal("%s: option block size mismatch", __func__);
247 if (newopts->banner != NULL)
248 newopts->banner = buffer_get_string(&m, NULL);
249 copy_set_server_options(&options, newopts, 1);
250 xfree(newopts);
251
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000252 buffer_free(&m);
253
254 return (pw);
255}
256
Darren Tucker7eb3de02003-10-15 15:56:58 +1000257char *
258mm_auth2_read_banner(void)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000259{
260 Buffer m;
261 char *banner;
262
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000263 debug3("%s entering", __func__);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000264
265 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000266 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000267 buffer_clear(&m);
268
Darren Tucker7eb3de02003-10-15 15:56:58 +1000269 mm_request_receive_expect(pmonitor->m_recvfd,
270 MONITOR_ANS_AUTH2_READ_BANNER, &m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000271 banner = buffer_get_string(&m, NULL);
272 buffer_free(&m);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000273
Darren Tucker7eb3de02003-10-15 15:56:58 +1000274 /* treat empty banner as missing banner */
275 if (strlen(banner) == 0) {
276 xfree(banner);
277 banner = NULL;
278 }
Damien Miller5ad9fd92002-05-13 11:07:41 +1000279 return (banner);
280}
281
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000282/* Inform the privileged process about service and style */
283
284void
285mm_inform_authserv(char *service, char *style)
286{
287 Buffer m;
288
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000289 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000290
291 buffer_init(&m);
292 buffer_put_cstring(&m, service);
293 buffer_put_cstring(&m, style ? style : "");
294
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000295 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000296
297 buffer_free(&m);
298}
299
300/* Do the password authentication */
301int
302mm_auth_password(Authctxt *authctxt, char *password)
303{
304 Buffer m;
305 int authenticated = 0;
306
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000307 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000308
309 buffer_init(&m);
310 buffer_put_cstring(&m, password);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000311 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000312
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000313 debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000314 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000315
316 authenticated = buffer_get_int(&m);
317
318 buffer_free(&m);
319
320 debug3("%s: user %sauthenticated",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000321 __func__, authenticated ? "" : "not ");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000322 return (authenticated);
323}
324
325int
326mm_user_key_allowed(struct passwd *pw, Key *key)
327{
328 return (mm_key_allowed(MM_USERKEY, NULL, NULL, key));
329}
330
331int
332mm_hostbased_key_allowed(struct passwd *pw, char *user, char *host,
333 Key *key)
334{
335 return (mm_key_allowed(MM_HOSTKEY, user, host, key));
336}
337
338int
339mm_auth_rhosts_rsa_key_allowed(struct passwd *pw, char *user,
340 char *host, Key *key)
341{
342 int ret;
343
344 key->type = KEY_RSA; /* XXX hack for key_to_blob */
345 ret = mm_key_allowed(MM_RSAHOSTKEY, user, host, key);
346 key->type = KEY_RSA1;
347 return (ret);
348}
349
350static void
351mm_send_debug(Buffer *m)
352{
353 char *msg;
354
355 while (buffer_len(m)) {
356 msg = buffer_get_string(m, NULL);
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000357 debug3("%s: Sending debug: %s", __func__, msg);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000358 packet_send_debug("%s", msg);
359 xfree(msg);
360 }
361}
362
363int
364mm_key_allowed(enum mm_keytype type, char *user, char *host, Key *key)
365{
366 Buffer m;
367 u_char *blob;
368 u_int len;
Damien Miller06ebedf2003-02-24 12:03:38 +1100369 int allowed = 0, have_forced = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000370
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000371 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000372
373 /* Convert the key to a blob and the pass it over */
374 if (!key_to_blob(key, &blob, &len))
375 return (0);
376
377 buffer_init(&m);
378 buffer_put_int(&m, type);
379 buffer_put_cstring(&m, user ? user : "");
380 buffer_put_cstring(&m, host ? host : "");
381 buffer_put_string(&m, blob, len);
382 xfree(blob);
383
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000384 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000385
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000386 debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000387 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000388
389 allowed = buffer_get_int(&m);
390
Damien Miller06ebedf2003-02-24 12:03:38 +1100391 /* fake forced command */
392 auth_clear_options();
393 have_forced = buffer_get_int(&m);
394 forced_command = have_forced ? xstrdup("true") : NULL;
395
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000396 /* Send potential debug messages */
397 mm_send_debug(&m);
398
399 buffer_free(&m);
400
401 return (allowed);
402}
403
404/*
405 * This key verify needs to send the key type along, because the
406 * privileged parent makes the decision if the key is allowed
407 * for authentication.
408 */
409
410int
411mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
412{
413 Buffer m;
414 u_char *blob;
415 u_int len;
416 int verified = 0;
417
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000418 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000419
420 /* Convert the key to a blob and the pass it over */
421 if (!key_to_blob(key, &blob, &len))
422 return (0);
423
424 buffer_init(&m);
425 buffer_put_string(&m, blob, len);
426 buffer_put_string(&m, sig, siglen);
427 buffer_put_string(&m, data, datalen);
428 xfree(blob);
429
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000430 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000431
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000432 debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000433 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000434
435 verified = buffer_get_int(&m);
436
437 buffer_free(&m);
438
439 return (verified);
440}
441
442/* Export key state after authentication */
443Newkeys *
444mm_newkeys_from_blob(u_char *blob, int blen)
445{
446 Buffer b;
447 u_int len;
448 Newkeys *newkey = NULL;
449 Enc *enc;
450 Mac *mac;
451 Comp *comp;
452
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000453 debug3("%s: %p(%d)", __func__, blob, blen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000454#ifdef DEBUG_PK
455 dump_base64(stderr, blob, blen);
456#endif
457 buffer_init(&b);
458 buffer_append(&b, blob, blen);
459
460 newkey = xmalloc(sizeof(*newkey));
461 enc = &newkey->enc;
462 mac = &newkey->mac;
463 comp = &newkey->comp;
464
465 /* Enc structure */
466 enc->name = buffer_get_string(&b, NULL);
467 buffer_get(&b, &enc->cipher, sizeof(enc->cipher));
468 enc->enabled = buffer_get_int(&b);
469 enc->block_size = buffer_get_int(&b);
470 enc->key = buffer_get_string(&b, &enc->key_len);
471 enc->iv = buffer_get_string(&b, &len);
472 if (len != enc->block_size)
Ben Lindstrom08512492002-06-27 00:23:02 +0000473 fatal("%s: bad ivlen: expected %u != %u", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000474 enc->block_size, len);
475
476 if (enc->name == NULL || cipher_by_name(enc->name) != enc->cipher)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000477 fatal("%s: bad cipher name %s or pointer %p", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000478 enc->name, enc->cipher);
479
480 /* Mac structure */
481 mac->name = buffer_get_string(&b, NULL);
Darren Tucker5f3d5be2007-06-05 18:30:18 +1000482 if (mac->name == NULL || mac_setup(mac, mac->name) == -1)
Damien Millere45796f2007-06-11 14:01:42 +1000483 fatal("%s: can not setup mac %s", __func__, mac->name);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000484 mac->enabled = buffer_get_int(&b);
485 mac->key = buffer_get_string(&b, &len);
486 if (len > mac->key_len)
Ben Lindstrom08512492002-06-27 00:23:02 +0000487 fatal("%s: bad mac key length: %u > %d", __func__, len,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000488 mac->key_len);
489 mac->key_len = len;
490
491 /* Comp structure */
492 comp->type = buffer_get_int(&b);
493 comp->enabled = buffer_get_int(&b);
494 comp->name = buffer_get_string(&b, NULL);
495
496 len = buffer_len(&b);
497 if (len != 0)
Ben Lindstrom08512492002-06-27 00:23:02 +0000498 error("newkeys_from_blob: remaining bytes in blob %u", len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000499 buffer_free(&b);
500 return (newkey);
501}
502
503int
504mm_newkeys_to_blob(int mode, u_char **blobp, u_int *lenp)
505{
506 Buffer b;
507 int len;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000508 Enc *enc;
509 Mac *mac;
510 Comp *comp;
511 Newkeys *newkey = newkeys[mode];
512
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000513 debug3("%s: converting %p", __func__, newkey);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000514
515 if (newkey == NULL) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000516 error("%s: newkey == NULL", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000517 return 0;
518 }
519 enc = &newkey->enc;
520 mac = &newkey->mac;
521 comp = &newkey->comp;
522
523 buffer_init(&b);
524 /* Enc structure */
525 buffer_put_cstring(&b, enc->name);
526 /* The cipher struct is constant and shared, you export pointer */
527 buffer_append(&b, &enc->cipher, sizeof(enc->cipher));
528 buffer_put_int(&b, enc->enabled);
529 buffer_put_int(&b, enc->block_size);
530 buffer_put_string(&b, enc->key, enc->key_len);
531 packet_get_keyiv(mode, enc->iv, enc->block_size);
532 buffer_put_string(&b, enc->iv, enc->block_size);
533
534 /* Mac structure */
535 buffer_put_cstring(&b, mac->name);
536 buffer_put_int(&b, mac->enabled);
537 buffer_put_string(&b, mac->key, mac->key_len);
538
539 /* Comp structure */
540 buffer_put_int(&b, comp->type);
541 buffer_put_int(&b, comp->enabled);
542 buffer_put_cstring(&b, comp->name);
543
544 len = buffer_len(&b);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000545 if (lenp != NULL)
546 *lenp = len;
Ben Lindstrom2bf759c2002-07-07 22:13:31 +0000547 if (blobp != NULL) {
548 *blobp = xmalloc(len);
549 memcpy(*blobp, buffer_ptr(&b), len);
550 }
551 memset(buffer_ptr(&b), 0, len);
552 buffer_free(&b);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000553 return len;
554}
555
556static void
557mm_send_kex(Buffer *m, Kex *kex)
558{
559 buffer_put_string(m, kex->session_id, kex->session_id_len);
560 buffer_put_int(m, kex->we_need);
561 buffer_put_int(m, kex->hostkey_type);
562 buffer_put_int(m, kex->kex_type);
563 buffer_put_string(m, buffer_ptr(&kex->my), buffer_len(&kex->my));
564 buffer_put_string(m, buffer_ptr(&kex->peer), buffer_len(&kex->peer));
565 buffer_put_int(m, kex->flags);
566 buffer_put_cstring(m, kex->client_version_string);
567 buffer_put_cstring(m, kex->server_version_string);
568}
569
570void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000571mm_send_keystate(struct monitor *monitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000572{
573 Buffer m;
574 u_char *blob, *p;
575 u_int bloblen, plen;
Damien Millera5539d22003-04-09 20:50:06 +1000576 u_int32_t seqnr, packets;
Damien Millerb61f3fc2008-07-11 17:36:48 +1000577 u_int64_t blocks, bytes;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000578
579 buffer_init(&m);
580
581 if (!compat20) {
582 u_char iv[24];
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000583 u_char *key;
584 u_int ivlen, keylen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000585
586 buffer_put_int(&m, packet_get_protocol_flags());
587
588 buffer_put_int(&m, packet_get_ssh1_cipher());
589
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000590 debug3("%s: Sending ssh1 KEY+IV", __func__);
591 keylen = packet_get_encryption_key(NULL);
592 key = xmalloc(keylen+1); /* add 1 if keylen == 0 */
593 keylen = packet_get_encryption_key(key);
594 buffer_put_string(&m, key, keylen);
595 memset(key, 0, keylen);
596 xfree(key);
597
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000598 ivlen = packet_get_keyiv_len(MODE_OUT);
599 packet_get_keyiv(MODE_OUT, iv, ivlen);
600 buffer_put_string(&m, iv, ivlen);
601 ivlen = packet_get_keyiv_len(MODE_OUT);
602 packet_get_keyiv(MODE_IN, iv, ivlen);
603 buffer_put_string(&m, iv, ivlen);
604 goto skip;
605 } else {
606 /* Kex for rekeying */
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000607 mm_send_kex(&m, *monitor->m_pkex);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000608 }
609
610 debug3("%s: Sending new keys: %p %p",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000611 __func__, newkeys[MODE_OUT], newkeys[MODE_IN]);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000612
613 /* Keys from Kex */
614 if (!mm_newkeys_to_blob(MODE_OUT, &blob, &bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000615 fatal("%s: conversion of newkeys failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000616
617 buffer_put_string(&m, blob, bloblen);
618 xfree(blob);
619
620 if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000621 fatal("%s: conversion of newkeys failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000622
623 buffer_put_string(&m, blob, bloblen);
624 xfree(blob);
625
Damien Millerb61f3fc2008-07-11 17:36:48 +1000626 packet_get_state(MODE_OUT, &seqnr, &blocks, &packets, &bytes);
Damien Millera5539d22003-04-09 20:50:06 +1000627 buffer_put_int(&m, seqnr);
628 buffer_put_int64(&m, blocks);
629 buffer_put_int(&m, packets);
Damien Millerb61f3fc2008-07-11 17:36:48 +1000630 buffer_put_int64(&m, bytes);
631 packet_get_state(MODE_IN, &seqnr, &blocks, &packets, &bytes);
Damien Millera5539d22003-04-09 20:50:06 +1000632 buffer_put_int(&m, seqnr);
633 buffer_put_int64(&m, blocks);
634 buffer_put_int(&m, packets);
Damien Millerb61f3fc2008-07-11 17:36:48 +1000635 buffer_put_int64(&m, bytes);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000636
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000637 debug3("%s: New keys have been sent", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000638 skip:
639 /* More key context */
640 plen = packet_get_keycontext(MODE_OUT, NULL);
641 p = xmalloc(plen+1);
642 packet_get_keycontext(MODE_OUT, p);
643 buffer_put_string(&m, p, plen);
644 xfree(p);
645
646 plen = packet_get_keycontext(MODE_IN, NULL);
647 p = xmalloc(plen+1);
648 packet_get_keycontext(MODE_IN, p);
649 buffer_put_string(&m, p, plen);
650 xfree(p);
651
652 /* Compression state */
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000653 debug3("%s: Sending compression state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000654 buffer_put_string(&m, &outgoing_stream, sizeof(outgoing_stream));
655 buffer_put_string(&m, &incoming_stream, sizeof(incoming_stream));
656
657 /* Network I/O buffers */
658 buffer_put_string(&m, buffer_ptr(&input), buffer_len(&input));
659 buffer_put_string(&m, buffer_ptr(&output), buffer_len(&output));
660
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000661 mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m);
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000662 debug3("%s: Finished sending state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000663
664 buffer_free(&m);
665}
666
667int
Damien Miller71a73672006-03-26 14:04:36 +1100668mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000669{
670 Buffer m;
Darren Tucker09991742004-07-17 17:05:14 +1000671 char *p, *msg;
Damien Miller7207f642008-05-19 15:34:50 +1000672 int success = 0, tmp1 = -1, tmp2 = -1;
673
674 /* Kludge: ensure there are fds free to receive the pty/tty */
675 if ((tmp1 = dup(pmonitor->m_recvfd)) == -1 ||
676 (tmp2 = dup(pmonitor->m_recvfd)) == -1) {
677 error("%s: cannot allocate fds for pty", __func__);
678 if (tmp1 > 0)
679 close(tmp1);
680 if (tmp2 > 0)
681 close(tmp2);
682 return 0;
683 }
684 close(tmp1);
685 close(tmp2);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000686
687 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000688 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000689
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000690 debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000691 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000692
693 success = buffer_get_int(&m);
694 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000695 debug3("%s: pty alloc failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000696 buffer_free(&m);
697 return (0);
698 }
699 p = buffer_get_string(&m, NULL);
Darren Tucker09991742004-07-17 17:05:14 +1000700 msg = buffer_get_string(&m, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000701 buffer_free(&m);
702
703 strlcpy(namebuf, p, namebuflen); /* Possible truncation */
704 xfree(p);
705
Darren Tucker09991742004-07-17 17:05:14 +1000706 buffer_append(&loginmsg, msg, strlen(msg));
707 xfree(msg);
708
Damien Miller54fd7cf2007-09-17 12:04:08 +1000709 if ((*ptyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1 ||
710 (*ttyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1)
711 fatal("%s: receive fds failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000712
713 /* Success */
714 return (1);
715}
716
717void
Darren Tucker3e33cec2003-10-02 16:12:36 +1000718mm_session_pty_cleanup2(Session *s)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000719{
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000720 Buffer m;
721
722 if (s->ttyfd == -1)
723 return;
724 buffer_init(&m);
725 buffer_put_cstring(&m, s->tty);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000726 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000727 buffer_free(&m);
728
729 /* closed dup'ed master */
Damien Miller7207f642008-05-19 15:34:50 +1000730 if (s->ptymaster != -1 && close(s->ptymaster) < 0)
731 error("close(s->ptymaster/%d): %s",
732 s->ptymaster, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000733
734 /* unlink pty from session */
735 s->ttyfd = -1;
736}
737
Damien Miller79418552002-04-23 20:28:48 +1000738#ifdef USE_PAM
739void
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100740mm_start_pam(Authctxt *authctxt)
Damien Miller79418552002-04-23 20:28:48 +1000741{
742 Buffer m;
743
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000744 debug3("%s entering", __func__);
Damien Miller4e448a32003-05-14 15:11:48 +1000745 if (!options.use_pam)
746 fatal("UsePAM=no, but ended up in %s anyway", __func__);
Damien Miller79418552002-04-23 20:28:48 +1000747
748 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000749 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, &m);
Damien Miller79418552002-04-23 20:28:48 +1000750
751 buffer_free(&m);
752}
Damien Miller4f9f42a2003-05-10 19:28:02 +1000753
Damien Miller1f499fd2003-08-25 13:08:49 +1000754u_int
755mm_do_pam_account(void)
756{
757 Buffer m;
758 u_int ret;
Darren Tucker77fc29e2004-09-11 23:07:03 +1000759 char *msg;
Damien Miller1f499fd2003-08-25 13:08:49 +1000760
761 debug3("%s entering", __func__);
762 if (!options.use_pam)
763 fatal("UsePAM=no, but ended up in %s anyway", __func__);
764
765 buffer_init(&m);
766 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_ACCOUNT, &m);
767
Damien Millera8e06ce2003-11-21 23:48:55 +1100768 mm_request_receive_expect(pmonitor->m_recvfd,
Damien Miller1f499fd2003-08-25 13:08:49 +1000769 MONITOR_ANS_PAM_ACCOUNT, &m);
770 ret = buffer_get_int(&m);
Darren Tucker77fc29e2004-09-11 23:07:03 +1000771 msg = buffer_get_string(&m, NULL);
772 buffer_append(&loginmsg, msg, strlen(msg));
773 xfree(msg);
Damien Miller1f499fd2003-08-25 13:08:49 +1000774
775 buffer_free(&m);
Damien Miller787b2ec2003-11-21 23:56:47 +1100776
Damien Miller1f499fd2003-08-25 13:08:49 +1000777 debug3("%s returning %d", __func__, ret);
778
779 return (ret);
780}
781
Damien Miller4f9f42a2003-05-10 19:28:02 +1000782void *
783mm_sshpam_init_ctx(Authctxt *authctxt)
784{
785 Buffer m;
786 int success;
787
788 debug3("%s", __func__);
789 buffer_init(&m);
790 buffer_put_cstring(&m, authctxt->user);
791 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_INIT_CTX, &m);
792 debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__);
793 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_INIT_CTX, &m);
794 success = buffer_get_int(&m);
795 if (success == 0) {
796 debug3("%s: pam_init_ctx failed", __func__);
797 buffer_free(&m);
798 return (NULL);
799 }
800 buffer_free(&m);
801 return (authctxt);
802}
803
804int
805mm_sshpam_query(void *ctx, char **name, char **info,
806 u_int *num, char ***prompts, u_int **echo_on)
807{
808 Buffer m;
Damien Miller04b65332005-07-17 17:53:31 +1000809 u_int i;
810 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000811
812 debug3("%s", __func__);
813 buffer_init(&m);
814 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_QUERY, &m);
815 debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__);
816 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_QUERY, &m);
817 ret = buffer_get_int(&m);
818 debug3("%s: pam_query returned %d", __func__, ret);
819 *name = buffer_get_string(&m, NULL);
820 *info = buffer_get_string(&m, NULL);
821 *num = buffer_get_int(&m);
Darren Tuckerd8093e42006-05-04 16:24:34 +1000822 if (*num > PAM_MAX_NUM_MSG)
823 fatal("%s: recieved %u PAM messages, expected <= %u",
824 __func__, *num, PAM_MAX_NUM_MSG);
825 *prompts = xcalloc((*num + 1), sizeof(char *));
826 *echo_on = xcalloc((*num + 1), sizeof(u_int));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000827 for (i = 0; i < *num; ++i) {
828 (*prompts)[i] = buffer_get_string(&m, NULL);
829 (*echo_on)[i] = buffer_get_int(&m);
830 }
831 buffer_free(&m);
832 return (ret);
833}
834
835int
836mm_sshpam_respond(void *ctx, u_int num, char **resp)
837{
838 Buffer m;
Damien Miller04b65332005-07-17 17:53:31 +1000839 u_int i;
840 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000841
842 debug3("%s", __func__);
843 buffer_init(&m);
844 buffer_put_int(&m, num);
845 for (i = 0; i < num; ++i)
846 buffer_put_cstring(&m, resp[i]);
847 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_RESPOND, &m);
848 debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__);
849 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_RESPOND, &m);
850 ret = buffer_get_int(&m);
851 debug3("%s: pam_respond returned %d", __func__, ret);
852 buffer_free(&m);
853 return (ret);
854}
855
856void
857mm_sshpam_free_ctx(void *ctxtp)
858{
859 Buffer m;
860
861 debug3("%s", __func__);
862 buffer_init(&m);
863 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_FREE_CTX, &m);
864 debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__);
865 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_FREE_CTX, &m);
866 buffer_free(&m);
867}
Damien Miller79418552002-04-23 20:28:48 +1000868#endif /* USE_PAM */
869
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000870/* Request process termination */
871
872void
873mm_terminate(void)
874{
875 Buffer m;
876
877 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000878 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000879 buffer_free(&m);
880}
881
882int
883mm_ssh1_session_key(BIGNUM *num)
884{
885 int rsafail;
886 Buffer m;
887
888 buffer_init(&m);
889 buffer_put_bignum2(&m, num);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000890 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000891
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000892 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000893
894 rsafail = buffer_get_int(&m);
895 buffer_get_bignum2(&m, num);
896
897 buffer_free(&m);
898
899 return (rsafail);
900}
901
902static void
903mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
904 char ***prompts, u_int **echo_on)
905{
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000906 *name = xstrdup("");
907 *infotxt = xstrdup("");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000908 *numprompts = 1;
Damien Miller07d86be2006-03-26 14:19:21 +1100909 *prompts = xcalloc(*numprompts, sizeof(char *));
910 *echo_on = xcalloc(*numprompts, sizeof(u_int));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000911 (*echo_on)[0] = 0;
912}
913
914int
915mm_bsdauth_query(void *ctx, char **name, char **infotxt,
916 u_int *numprompts, char ***prompts, u_int **echo_on)
917{
918 Buffer m;
Damien Millerb7df3af2003-02-24 11:55:46 +1100919 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000920 char *challenge;
921
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000922 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000923
924 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000925 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000926
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000927 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000928 &m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100929 success = buffer_get_int(&m);
930 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000931 debug3("%s: no challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000932 buffer_free(&m);
933 return (-1);
934 }
935
936 /* Get the challenge, and format the response */
937 challenge = buffer_get_string(&m, NULL);
938 buffer_free(&m);
939
940 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
941 (*prompts)[0] = challenge;
942
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000943 debug3("%s: received challenge: %s", __func__, challenge);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000944
945 return (0);
946}
947
948int
949mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
950{
951 Buffer m;
952 int authok;
953
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000954 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000955 if (numresponses != 1)
956 return (-1);
957
958 buffer_init(&m);
959 buffer_put_cstring(&m, responses[0]);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000960 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000961
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000962 mm_request_receive_expect(pmonitor->m_recvfd,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000963 MONITOR_ANS_BSDAUTHRESPOND, &m);
964
965 authok = buffer_get_int(&m);
966 buffer_free(&m);
967
968 return ((authok == 0) ? -1 : 0);
969}
970
Darren Tucker042e2e82004-07-08 23:09:42 +1000971#ifdef SKEY
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000972int
973mm_skey_query(void *ctx, char **name, char **infotxt,
974 u_int *numprompts, char ***prompts, u_int **echo_on)
975{
976 Buffer m;
Damien Millerb7df3af2003-02-24 11:55:46 +1100977 u_int success;
Darren Tuckerd6a23f22006-08-05 18:50:35 +1000978 char *challenge;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000979
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000980 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000981
982 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000983 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000984
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000985 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000986 &m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100987 success = buffer_get_int(&m);
988 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000989 debug3("%s: no challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000990 buffer_free(&m);
991 return (-1);
992 }
993
994 /* Get the challenge, and format the response */
995 challenge = buffer_get_string(&m, NULL);
996 buffer_free(&m);
997
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000998 debug3("%s: received challenge: %s", __func__, challenge);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000999
1000 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
1001
Damien Miller07d86be2006-03-26 14:19:21 +11001002 xasprintf(*prompts, "%s%s", challenge, SKEY_PROMPT);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001003 xfree(challenge);
1004
1005 return (0);
1006}
1007
1008int
1009mm_skey_respond(void *ctx, u_int numresponses, char **responses)
1010{
1011 Buffer m;
1012 int authok;
1013
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001014 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001015 if (numresponses != 1)
1016 return (-1);
1017
1018 buffer_init(&m);
1019 buffer_put_cstring(&m, responses[0]);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001020 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001021
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001022 mm_request_receive_expect(pmonitor->m_recvfd,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001023 MONITOR_ANS_SKEYRESPOND, &m);
1024
1025 authok = buffer_get_int(&m);
1026 buffer_free(&m);
1027
1028 return ((authok == 0) ? -1 : 0);
1029}
Darren Tucker042e2e82004-07-08 23:09:42 +10001030#endif /* SKEY */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001031
1032void
1033mm_ssh1_session_id(u_char session_id[16])
1034{
1035 Buffer m;
1036 int i;
1037
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001038 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001039
1040 buffer_init(&m);
1041 for (i = 0; i < 16; i++)
1042 buffer_put_char(&m, session_id[i]);
1043
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001044 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001045 buffer_free(&m);
1046}
1047
1048int
1049mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
1050{
1051 Buffer m;
1052 Key *key;
1053 u_char *blob;
1054 u_int blen;
Damien Miller06ebedf2003-02-24 12:03:38 +11001055 int allowed = 0, have_forced = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001056
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001057 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001058
1059 buffer_init(&m);
1060 buffer_put_bignum2(&m, client_n);
1061
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001062 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m);
1063 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001064
1065 allowed = buffer_get_int(&m);
1066
Damien Miller06ebedf2003-02-24 12:03:38 +11001067 /* fake forced command */
1068 auth_clear_options();
1069 have_forced = buffer_get_int(&m);
1070 forced_command = have_forced ? xstrdup("true") : NULL;
1071
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001072 if (allowed && rkey != NULL) {
1073 blob = buffer_get_string(&m, &blen);
1074 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001075 fatal("%s: key_from_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001076 *rkey = key;
1077 xfree(blob);
1078 }
1079 mm_send_debug(&m);
1080 buffer_free(&m);
1081
1082 return (allowed);
1083}
1084
1085BIGNUM *
1086mm_auth_rsa_generate_challenge(Key *key)
1087{
1088 Buffer m;
1089 BIGNUM *challenge;
1090 u_char *blob;
1091 u_int blen;
1092
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001093 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001094
1095 if ((challenge = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001096 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001097
1098 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1099 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001100 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001101 key->type = KEY_RSA1;
1102
1103 buffer_init(&m);
1104 buffer_put_string(&m, blob, blen);
1105 xfree(blob);
1106
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001107 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m);
1108 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001109
1110 buffer_get_bignum2(&m, challenge);
1111 buffer_free(&m);
1112
1113 return (challenge);
1114}
1115
1116int
1117mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16])
1118{
1119 Buffer m;
1120 u_char *blob;
1121 u_int blen;
1122 int success = 0;
1123
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001124 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001125
1126 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1127 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001128 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001129 key->type = KEY_RSA1;
1130
1131 buffer_init(&m);
1132 buffer_put_string(&m, blob, blen);
1133 buffer_put_string(&m, response, 16);
1134 xfree(blob);
1135
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001136 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m);
1137 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001138
1139 success = buffer_get_int(&m);
1140 buffer_free(&m);
1141
1142 return (success);
1143}
Damien Miller25162f22002-09-12 09:47:29 +10001144
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001145#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001146void
1147mm_audit_event(ssh_audit_event_t event)
1148{
1149 Buffer m;
1150
1151 debug3("%s entering", __func__);
1152
1153 buffer_init(&m);
1154 buffer_put_int(&m, event);
1155
1156 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_EVENT, &m);
1157 buffer_free(&m);
1158}
1159
1160void
1161mm_audit_run_command(const char *command)
1162{
1163 Buffer m;
1164
1165 debug3("%s entering command %s", __func__, command);
1166
1167 buffer_init(&m);
1168 buffer_put_cstring(&m, command);
1169
1170 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, &m);
1171 buffer_free(&m);
1172}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001173#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001174
Darren Tucker0efd1552003-08-26 11:49:55 +10001175#ifdef GSSAPI
1176OM_uint32
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001177mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
Darren Tucker0efd1552003-08-26 11:49:55 +10001178{
1179 Buffer m;
1180 OM_uint32 major;
1181
1182 /* Client doesn't get to see the context */
1183 *ctx = NULL;
1184
1185 buffer_init(&m);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001186 buffer_put_string(&m, goid->elements, goid->length);
Darren Tucker0efd1552003-08-26 11:49:55 +10001187
1188 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m);
1189 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m);
1190
1191 major = buffer_get_int(&m);
1192
1193 buffer_free(&m);
1194 return (major);
1195}
1196
1197OM_uint32
1198mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
1199 gss_buffer_desc *out, OM_uint32 *flags)
1200{
1201 Buffer m;
1202 OM_uint32 major;
Darren Tucker600ad8d2003-08-26 12:10:48 +10001203 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001204
1205 buffer_init(&m);
1206 buffer_put_string(&m, in->value, in->length);
1207
1208 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m);
1209 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m);
1210
1211 major = buffer_get_int(&m);
Darren Tucker600ad8d2003-08-26 12:10:48 +10001212 out->value = buffer_get_string(&m, &len);
1213 out->length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001214 if (flags)
1215 *flags = buffer_get_int(&m);
1216
1217 buffer_free(&m);
1218
1219 return (major);
1220}
1221
Damien Miller0425d402003-11-17 22:18:21 +11001222OM_uint32
1223mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
1224{
1225 Buffer m;
1226 OM_uint32 major;
1227
1228 buffer_init(&m);
1229 buffer_put_string(&m, gssbuf->value, gssbuf->length);
1230 buffer_put_string(&m, gssmic->value, gssmic->length);
1231
1232 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, &m);
1233 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSCHECKMIC,
1234 &m);
1235
1236 major = buffer_get_int(&m);
1237 buffer_free(&m);
1238 return(major);
1239}
1240
Darren Tucker0efd1552003-08-26 11:49:55 +10001241int
1242mm_ssh_gssapi_userok(char *user)
1243{
1244 Buffer m;
1245 int authenticated = 0;
1246
1247 buffer_init(&m);
1248
1249 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m);
1250 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK,
1251 &m);
1252
1253 authenticated = buffer_get_int(&m);
1254
1255 buffer_free(&m);
1256 debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
1257 return (authenticated);
1258}
1259#endif /* GSSAPI */
Damien Miller01ed2272008-11-05 16:20:46 +11001260
1261#ifdef JPAKE
1262void
1263mm_auth2_jpake_get_pwdata(Authctxt *authctxt, BIGNUM **s,
1264 char **hash_scheme, char **salt)
1265{
1266 Buffer m;
1267
1268 debug3("%s entering", __func__);
1269
1270 buffer_init(&m);
1271 mm_request_send(pmonitor->m_recvfd,
1272 MONITOR_REQ_JPAKE_GET_PWDATA, &m);
1273
1274 debug3("%s: waiting for MONITOR_ANS_JPAKE_GET_PWDATA", __func__);
1275 mm_request_receive_expect(pmonitor->m_recvfd,
1276 MONITOR_ANS_JPAKE_GET_PWDATA, &m);
1277
1278 *hash_scheme = buffer_get_string(&m, NULL);
1279 *salt = buffer_get_string(&m, NULL);
1280
1281 buffer_free(&m);
1282}
1283
1284void
1285mm_jpake_step1(struct jpake_group *grp,
1286 u_char **id, u_int *id_len,
1287 BIGNUM **priv1, BIGNUM **priv2, BIGNUM **g_priv1, BIGNUM **g_priv2,
1288 u_char **priv1_proof, u_int *priv1_proof_len,
1289 u_char **priv2_proof, u_int *priv2_proof_len)
1290{
1291 Buffer m;
1292
1293 debug3("%s entering", __func__);
1294
1295 buffer_init(&m);
1296 mm_request_send(pmonitor->m_recvfd,
1297 MONITOR_REQ_JPAKE_STEP1, &m);
1298
1299 debug3("%s: waiting for MONITOR_ANS_JPAKE_STEP1", __func__);
1300 mm_request_receive_expect(pmonitor->m_recvfd,
1301 MONITOR_ANS_JPAKE_STEP1, &m);
1302
1303 if ((*priv1 = BN_new()) == NULL ||
1304 (*priv2 = BN_new()) == NULL ||
1305 (*g_priv1 = BN_new()) == NULL ||
1306 (*g_priv2 = BN_new()) == NULL)
1307 fatal("%s: BN_new", __func__);
1308
1309 *id = buffer_get_string(&m, id_len);
1310 /* priv1 and priv2 are, well, private */
1311 buffer_get_bignum2(&m, *g_priv1);
1312 buffer_get_bignum2(&m, *g_priv2);
1313 *priv1_proof = buffer_get_string(&m, priv1_proof_len);
1314 *priv2_proof = buffer_get_string(&m, priv2_proof_len);
1315
1316 buffer_free(&m);
1317}
1318
1319void
1320mm_jpake_step2(struct jpake_group *grp, BIGNUM *s,
1321 BIGNUM *mypub1, BIGNUM *theirpub1, BIGNUM *theirpub2, BIGNUM *mypriv2,
1322 const u_char *theirid, u_int theirid_len,
1323 const u_char *myid, u_int myid_len,
1324 const u_char *theirpub1_proof, u_int theirpub1_proof_len,
1325 const u_char *theirpub2_proof, u_int theirpub2_proof_len,
1326 BIGNUM **newpub,
1327 u_char **newpub_exponent_proof, u_int *newpub_exponent_proof_len)
1328{
1329 Buffer m;
1330
1331 debug3("%s entering", __func__);
1332
1333 buffer_init(&m);
1334 /* monitor already has all bignums except theirpub1, theirpub2 */
1335 buffer_put_bignum2(&m, theirpub1);
1336 buffer_put_bignum2(&m, theirpub2);
1337 /* monitor already knows our id */
1338 buffer_put_string(&m, theirid, theirid_len);
1339 buffer_put_string(&m, theirpub1_proof, theirpub1_proof_len);
1340 buffer_put_string(&m, theirpub2_proof, theirpub2_proof_len);
1341
1342 mm_request_send(pmonitor->m_recvfd,
1343 MONITOR_REQ_JPAKE_STEP2, &m);
1344
1345 debug3("%s: waiting for MONITOR_ANS_JPAKE_STEP2", __func__);
1346 mm_request_receive_expect(pmonitor->m_recvfd,
1347 MONITOR_ANS_JPAKE_STEP2, &m);
1348
1349 if ((*newpub = BN_new()) == NULL)
1350 fatal("%s: BN_new", __func__);
1351
1352 buffer_get_bignum2(&m, *newpub);
1353 *newpub_exponent_proof = buffer_get_string(&m,
1354 newpub_exponent_proof_len);
1355
1356 buffer_free(&m);
1357}
1358
1359void
1360mm_jpake_key_confirm(struct jpake_group *grp, BIGNUM *s, BIGNUM *step2_val,
1361 BIGNUM *mypriv2, BIGNUM *mypub1, BIGNUM *mypub2,
1362 BIGNUM *theirpub1, BIGNUM *theirpub2,
1363 const u_char *my_id, u_int my_id_len,
1364 const u_char *their_id, u_int their_id_len,
1365 const u_char *sess_id, u_int sess_id_len,
1366 const u_char *theirpriv2_s_proof, u_int theirpriv2_s_proof_len,
1367 BIGNUM **k,
1368 u_char **confirm_hash, u_int *confirm_hash_len)
1369{
1370 Buffer m;
1371
1372 debug3("%s entering", __func__);
1373
1374 buffer_init(&m);
1375 /* monitor already has all bignums except step2_val */
1376 buffer_put_bignum2(&m, step2_val);
1377 /* monitor already knows all the ids */
1378 buffer_put_string(&m, theirpriv2_s_proof, theirpriv2_s_proof_len);
1379
1380 mm_request_send(pmonitor->m_recvfd,
1381 MONITOR_REQ_JPAKE_KEY_CONFIRM, &m);
1382
1383 debug3("%s: waiting for MONITOR_ANS_JPAKE_KEY_CONFIRM", __func__);
1384 mm_request_receive_expect(pmonitor->m_recvfd,
1385 MONITOR_ANS_JPAKE_KEY_CONFIRM, &m);
1386
1387 /* 'k' is sensitive and stays in the monitor */
1388 *confirm_hash = buffer_get_string(&m, confirm_hash_len);
1389
1390 buffer_free(&m);
1391}
1392
1393int
1394mm_jpake_check_confirm(const BIGNUM *k,
1395 const u_char *peer_id, u_int peer_id_len,
1396 const u_char *sess_id, u_int sess_id_len,
1397 const u_char *peer_confirm_hash, u_int peer_confirm_hash_len)
1398{
1399 Buffer m;
1400 int success = 0;
1401
1402 debug3("%s entering", __func__);
1403
1404 buffer_init(&m);
1405 /* k is dummy in slave, ignored */
1406 /* monitor knows all the ids */
1407 buffer_put_string(&m, peer_confirm_hash, peer_confirm_hash_len);
1408 mm_request_send(pmonitor->m_recvfd,
1409 MONITOR_REQ_JPAKE_CHECK_CONFIRM, &m);
1410
1411 debug3("%s: waiting for MONITOR_ANS_JPAKE_CHECK_CONFIRM", __func__);
1412 mm_request_receive_expect(pmonitor->m_recvfd,
1413 MONITOR_ANS_JPAKE_CHECK_CONFIRM, &m);
1414
1415 success = buffer_get_int(&m);
1416 buffer_free(&m);
1417
1418 debug3("%s: success = %d", __func__, success);
1419 return success;
1420}
1421#endif /* JPAKE */