blob: db3251b932e086a6f1da48c03d69d70145721b2a [file] [log] [blame]
Damien Millercee85232009-03-06 00:58:22 +11001/* $OpenBSD: monitor_wrap.c,v 1.65 2009/03/05 07:18:19 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 Millercee85232009-03-06 00:58:22 +110074#include "schnorr.h"
Damien Miller01ed2272008-11-05 16:20:46 +110075#include "jpake.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000076
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000077#include "channels.h"
78#include "session.h"
Darren Tucker1629c072007-02-19 22:25:37 +110079#include "servconf.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000080
81/* Imports */
82extern int compat20;
83extern Newkeys *newkeys[];
84extern z_stream incoming_stream;
85extern z_stream outgoing_stream;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +000086extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000087extern Buffer input, output;
Darren Tucker09991742004-07-17 17:05:14 +100088extern Buffer loginmsg;
Damien Miller4e448a32003-05-14 15:11:48 +100089extern ServerOptions options;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000090
Darren Tucker3e33cec2003-10-02 16:12:36 +100091int
92mm_is_monitor(void)
93{
94 /*
95 * m_pid is only set in the privileged part, and
96 * points to the unprivileged child.
97 */
Darren Tuckera47c9bc2003-11-03 20:03:25 +110098 return (pmonitor && pmonitor->m_pid > 0);
Darren Tucker3e33cec2003-10-02 16:12:36 +100099}
100
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000101void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000102mm_request_send(int sock, enum monitor_reqtype type, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000103{
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000104 u_int mlen = buffer_len(m);
Ben Lindstromb1bdc5a2002-07-04 00:09:26 +0000105 u_char buf[5];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000106
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000107 debug3("%s entering: type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000108
Damien Miller3f941882006-03-31 23:13:02 +1100109 put_u32(buf, mlen + 1);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000110 buf[4] = (u_char) type; /* 1st byte of payload is mesg-type */
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000111 if (atomicio(vwrite, sock, buf, sizeof(buf)) != sizeof(buf))
Damien Millerb253cc42005-05-26 12:23:44 +1000112 fatal("%s: write: %s", __func__, strerror(errno));
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000113 if (atomicio(vwrite, sock, buffer_ptr(m), mlen) != mlen)
Damien Millerb253cc42005-05-26 12:23:44 +1000114 fatal("%s: write: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000115}
116
117void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000118mm_request_receive(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000119{
120 u_char buf[4];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000121 u_int msg_len;
122
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000123 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000124
Damien Millerb253cc42005-05-26 12:23:44 +1000125 if (atomicio(read, sock, buf, sizeof(buf)) != sizeof(buf)) {
126 if (errno == EPIPE)
Darren Tucker3e33cec2003-10-02 16:12:36 +1000127 cleanup_exit(255);
Damien Millerb253cc42005-05-26 12:23:44 +1000128 fatal("%s: read: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000129 }
Damien Miller3f941882006-03-31 23:13:02 +1100130 msg_len = get_u32(buf);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000131 if (msg_len > 256 * 1024)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000132 fatal("%s: read: bad msg_len %d", __func__, msg_len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000133 buffer_clear(m);
134 buffer_append_space(m, msg_len);
Damien Millerb253cc42005-05-26 12:23:44 +1000135 if (atomicio(read, sock, buffer_ptr(m), msg_len) != msg_len)
136 fatal("%s: read: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000137}
138
139void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000140mm_request_receive_expect(int sock, enum monitor_reqtype type, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000141{
142 u_char rtype;
143
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000144 debug3("%s entering: type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000145
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000146 mm_request_receive(sock, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000147 rtype = buffer_get_char(m);
148 if (rtype != type)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000149 fatal("%s: read: rtype %d != type %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000150 rtype, type);
151}
152
153DH *
154mm_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
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000165 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000166
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000167 debug3("%s: waiting for MONITOR_ANS_MODULI", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000168 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000169
170 success = buffer_get_char(&m);
171 if (success == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000172 fatal("%s: MONITOR_ANS_MODULI failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000173
174 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000175 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000176 if ((g = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000177 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000178 buffer_get_bignum2(&m, p);
179 buffer_get_bignum2(&m, g);
180
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000181 debug3("%s: remaining %d", __func__, buffer_len(&m));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000182 buffer_free(&m);
183
184 return (dh_new_group(g, p));
185}
186
187int
188mm_key_sign(Key *key, u_char **sigp, u_int *lenp, u_char *data, u_int datalen)
189{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000190 Kex *kex = *pmonitor->m_pkex;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000191 Buffer m;
192
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000193 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000194
195 buffer_init(&m);
196 buffer_put_int(&m, kex->host_key_index(key));
197 buffer_put_string(&m, data, datalen);
198
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000199 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000200
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000201 debug3("%s: waiting for MONITOR_ANS_SIGN", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000202 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000203 *sigp = buffer_get_string(&m, lenp);
204 buffer_free(&m);
205
206 return (0);
207}
208
209struct passwd *
Darren Tuckerb09b6772004-06-22 15:06:46 +1000210mm_getpwnamallow(const char *username)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000211{
212 Buffer m;
213 struct passwd *pw;
Darren Tucker1629c072007-02-19 22:25:37 +1100214 u_int len;
215 ServerOptions *newopts;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000216
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000217 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000218
219 buffer_init(&m);
Darren Tuckerb09b6772004-06-22 15:06:46 +1000220 buffer_put_cstring(&m, username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000221
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000222 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000223
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000224 debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000225 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000226
227 if (buffer_get_char(&m) == 0) {
Darren Tucker2f8b3d92007-12-02 23:02:15 +1100228 pw = NULL;
229 goto out;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000230 }
Darren Tucker1629c072007-02-19 22:25:37 +1100231 pw = buffer_get_string(&m, &len);
232 if (len != sizeof(struct passwd))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000233 fatal("%s: struct passwd size mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000234 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);
Kevin Steves7e147602002-03-22 18:07:17 +0000237#ifdef HAVE_PW_CLASS_IN_PASSWD
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000238 pw->pw_class = buffer_get_string(&m, NULL);
Kevin Steves7e147602002-03-22 18:07:17 +0000239#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000240 pw->pw_dir = buffer_get_string(&m, NULL);
241 pw->pw_shell = buffer_get_string(&m, NULL);
Darren Tucker1629c072007-02-19 22:25:37 +1100242
Darren Tucker2f8b3d92007-12-02 23:02:15 +1100243out:
Darren Tucker1629c072007-02-19 22:25:37 +1100244 /* 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
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000253 buffer_free(&m);
254
255 return (pw);
256}
257
Darren Tucker7eb3de02003-10-15 15:56:58 +1000258char *
259mm_auth2_read_banner(void)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000260{
261 Buffer m;
262 char *banner;
263
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000264 debug3("%s entering", __func__);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000265
266 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000267 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000268 buffer_clear(&m);
269
Darren Tucker7eb3de02003-10-15 15:56:58 +1000270 mm_request_receive_expect(pmonitor->m_recvfd,
271 MONITOR_ANS_AUTH2_READ_BANNER, &m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000272 banner = buffer_get_string(&m, NULL);
273 buffer_free(&m);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000274
Darren Tucker7eb3de02003-10-15 15:56:58 +1000275 /* treat empty banner as missing banner */
276 if (strlen(banner) == 0) {
277 xfree(banner);
278 banner = NULL;
279 }
Damien Miller5ad9fd92002-05-13 11:07:41 +1000280 return (banner);
281}
282
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000283/* Inform the privileged process about service and style */
284
285void
286mm_inform_authserv(char *service, char *style)
287{
288 Buffer m;
289
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000290 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000291
292 buffer_init(&m);
293 buffer_put_cstring(&m, service);
294 buffer_put_cstring(&m, style ? style : "");
295
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000296 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000297
298 buffer_free(&m);
299}
300
301/* Do the password authentication */
302int
303mm_auth_password(Authctxt *authctxt, char *password)
304{
305 Buffer m;
306 int authenticated = 0;
307
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000308 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000309
310 buffer_init(&m);
311 buffer_put_cstring(&m, password);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000312 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000313
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000314 debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000315 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000316
317 authenticated = buffer_get_int(&m);
318
319 buffer_free(&m);
320
321 debug3("%s: user %sauthenticated",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000322 __func__, authenticated ? "" : "not ");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000323 return (authenticated);
324}
325
326int
327mm_user_key_allowed(struct passwd *pw, Key *key)
328{
329 return (mm_key_allowed(MM_USERKEY, NULL, NULL, key));
330}
331
332int
333mm_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
339int
340mm_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
351static void
352mm_send_debug(Buffer *m)
353{
354 char *msg;
355
356 while (buffer_len(m)) {
357 msg = buffer_get_string(m, NULL);
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000358 debug3("%s: Sending debug: %s", __func__, msg);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000359 packet_send_debug("%s", msg);
360 xfree(msg);
361 }
362}
363
364int
365mm_key_allowed(enum mm_keytype type, char *user, char *host, Key *key)
366{
367 Buffer m;
368 u_char *blob;
369 u_int len;
Damien Miller06ebedf2003-02-24 12:03:38 +1100370 int allowed = 0, have_forced = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000371
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000372 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000373
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
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000385 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000386
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000387 debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000388 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000389
390 allowed = buffer_get_int(&m);
391
Damien Miller06ebedf2003-02-24 12:03:38 +1100392 /* fake forced command */
393 auth_clear_options();
394 have_forced = buffer_get_int(&m);
395 forced_command = have_forced ? xstrdup("true") : NULL;
396
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000397 /* 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
411int
412mm_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
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000419 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000420
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
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000431 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000432
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000433 debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000434 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000435
436 verified = buffer_get_int(&m);
437
438 buffer_free(&m);
439
440 return (verified);
441}
442
443/* Export key state after authentication */
444Newkeys *
445mm_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
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000454 debug3("%s: %p(%d)", __func__, blob, blen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000455#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)
Ben Lindstrom08512492002-06-27 00:23:02 +0000474 fatal("%s: bad ivlen: expected %u != %u", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000475 enc->block_size, len);
476
477 if (enc->name == NULL || cipher_by_name(enc->name) != enc->cipher)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000478 fatal("%s: bad cipher name %s or pointer %p", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000479 enc->name, enc->cipher);
480
481 /* Mac structure */
482 mac->name = buffer_get_string(&b, NULL);
Darren Tucker5f3d5be2007-06-05 18:30:18 +1000483 if (mac->name == NULL || mac_setup(mac, mac->name) == -1)
Damien Millere45796f2007-06-11 14:01:42 +1000484 fatal("%s: can not setup mac %s", __func__, mac->name);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000485 mac->enabled = buffer_get_int(&b);
486 mac->key = buffer_get_string(&b, &len);
487 if (len > mac->key_len)
Ben Lindstrom08512492002-06-27 00:23:02 +0000488 fatal("%s: bad mac key length: %u > %d", __func__, len,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000489 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)
Ben Lindstrom08512492002-06-27 00:23:02 +0000499 error("newkeys_from_blob: remaining bytes in blob %u", len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000500 buffer_free(&b);
501 return (newkey);
502}
503
504int
505mm_newkeys_to_blob(int mode, u_char **blobp, u_int *lenp)
506{
507 Buffer b;
508 int len;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000509 Enc *enc;
510 Mac *mac;
511 Comp *comp;
512 Newkeys *newkey = newkeys[mode];
513
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000514 debug3("%s: converting %p", __func__, newkey);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000515
516 if (newkey == NULL) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000517 error("%s: newkey == NULL", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000518 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);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000546 if (lenp != NULL)
547 *lenp = len;
Ben Lindstrom2bf759c2002-07-07 22:13:31 +0000548 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);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000554 return len;
555}
556
557static void
558mm_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
571void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000572mm_send_keystate(struct monitor *monitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000573{
574 Buffer m;
575 u_char *blob, *p;
576 u_int bloblen, plen;
Damien Millera5539d22003-04-09 20:50:06 +1000577 u_int32_t seqnr, packets;
Damien Millerb61f3fc2008-07-11 17:36:48 +1000578 u_int64_t blocks, bytes;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000579
580 buffer_init(&m);
581
582 if (!compat20) {
583 u_char iv[24];
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000584 u_char *key;
585 u_int ivlen, keylen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000586
587 buffer_put_int(&m, packet_get_protocol_flags());
588
589 buffer_put_int(&m, packet_get_ssh1_cipher());
590
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000591 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
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000599 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 */
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000608 mm_send_kex(&m, *monitor->m_pkex);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000609 }
610
611 debug3("%s: Sending new keys: %p %p",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000612 __func__, newkeys[MODE_OUT], newkeys[MODE_IN]);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000613
614 /* Keys from Kex */
615 if (!mm_newkeys_to_blob(MODE_OUT, &blob, &bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000616 fatal("%s: conversion of newkeys failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000617
618 buffer_put_string(&m, blob, bloblen);
619 xfree(blob);
620
621 if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000622 fatal("%s: conversion of newkeys failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000623
624 buffer_put_string(&m, blob, bloblen);
625 xfree(blob);
626
Damien Millerb61f3fc2008-07-11 17:36:48 +1000627 packet_get_state(MODE_OUT, &seqnr, &blocks, &packets, &bytes);
Damien Millera5539d22003-04-09 20:50:06 +1000628 buffer_put_int(&m, seqnr);
629 buffer_put_int64(&m, blocks);
630 buffer_put_int(&m, packets);
Damien Millerb61f3fc2008-07-11 17:36:48 +1000631 buffer_put_int64(&m, bytes);
632 packet_get_state(MODE_IN, &seqnr, &blocks, &packets, &bytes);
Damien Millera5539d22003-04-09 20:50:06 +1000633 buffer_put_int(&m, seqnr);
634 buffer_put_int64(&m, blocks);
635 buffer_put_int(&m, packets);
Damien Millerb61f3fc2008-07-11 17:36:48 +1000636 buffer_put_int64(&m, bytes);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000637
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000638 debug3("%s: New keys have been sent", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000639 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 */
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000654 debug3("%s: Sending compression state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000655 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
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000662 mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m);
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000663 debug3("%s: Finished sending state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000664
665 buffer_free(&m);
666}
667
668int
Damien Miller71a73672006-03-26 14:04:36 +1100669mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000670{
671 Buffer m;
Darren Tucker09991742004-07-17 17:05:14 +1000672 char *p, *msg;
Damien Miller7207f642008-05-19 15:34:50 +1000673 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);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000687
688 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000689 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000690
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000691 debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000692 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000693
694 success = buffer_get_int(&m);
695 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000696 debug3("%s: pty alloc failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000697 buffer_free(&m);
698 return (0);
699 }
700 p = buffer_get_string(&m, NULL);
Darren Tucker09991742004-07-17 17:05:14 +1000701 msg = buffer_get_string(&m, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000702 buffer_free(&m);
703
704 strlcpy(namebuf, p, namebuflen); /* Possible truncation */
705 xfree(p);
706
Darren Tucker09991742004-07-17 17:05:14 +1000707 buffer_append(&loginmsg, msg, strlen(msg));
708 xfree(msg);
709
Damien Miller54fd7cf2007-09-17 12:04:08 +1000710 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__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000713
714 /* Success */
715 return (1);
716}
717
718void
Darren Tucker3e33cec2003-10-02 16:12:36 +1000719mm_session_pty_cleanup2(Session *s)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000720{
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000721 Buffer m;
722
723 if (s->ttyfd == -1)
724 return;
725 buffer_init(&m);
726 buffer_put_cstring(&m, s->tty);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000727 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000728 buffer_free(&m);
729
730 /* closed dup'ed master */
Damien Miller7207f642008-05-19 15:34:50 +1000731 if (s->ptymaster != -1 && close(s->ptymaster) < 0)
732 error("close(s->ptymaster/%d): %s",
733 s->ptymaster, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000734
735 /* unlink pty from session */
736 s->ttyfd = -1;
737}
738
Damien Miller79418552002-04-23 20:28:48 +1000739#ifdef USE_PAM
740void
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100741mm_start_pam(Authctxt *authctxt)
Damien Miller79418552002-04-23 20:28:48 +1000742{
743 Buffer m;
744
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000745 debug3("%s entering", __func__);
Damien Miller4e448a32003-05-14 15:11:48 +1000746 if (!options.use_pam)
747 fatal("UsePAM=no, but ended up in %s anyway", __func__);
Damien Miller79418552002-04-23 20:28:48 +1000748
749 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000750 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, &m);
Damien Miller79418552002-04-23 20:28:48 +1000751
752 buffer_free(&m);
753}
Damien Miller4f9f42a2003-05-10 19:28:02 +1000754
Damien Miller1f499fd2003-08-25 13:08:49 +1000755u_int
756mm_do_pam_account(void)
757{
758 Buffer m;
759 u_int ret;
Darren Tucker77fc29e2004-09-11 23:07:03 +1000760 char *msg;
Damien Miller1f499fd2003-08-25 13:08:49 +1000761
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
Damien Millera8e06ce2003-11-21 23:48:55 +1100769 mm_request_receive_expect(pmonitor->m_recvfd,
Damien Miller1f499fd2003-08-25 13:08:49 +1000770 MONITOR_ANS_PAM_ACCOUNT, &m);
771 ret = buffer_get_int(&m);
Darren Tucker77fc29e2004-09-11 23:07:03 +1000772 msg = buffer_get_string(&m, NULL);
773 buffer_append(&loginmsg, msg, strlen(msg));
774 xfree(msg);
Damien Miller1f499fd2003-08-25 13:08:49 +1000775
776 buffer_free(&m);
Damien Miller787b2ec2003-11-21 23:56:47 +1100777
Damien Miller1f499fd2003-08-25 13:08:49 +1000778 debug3("%s returning %d", __func__, ret);
779
780 return (ret);
781}
782
Damien Miller4f9f42a2003-05-10 19:28:02 +1000783void *
784mm_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
805int
806mm_sshpam_query(void *ctx, char **name, char **info,
807 u_int *num, char ***prompts, u_int **echo_on)
808{
809 Buffer m;
Damien Miller04b65332005-07-17 17:53:31 +1000810 u_int i;
811 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000812
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);
Darren Tuckerd8093e42006-05-04 16:24:34 +1000823 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));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000828 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
836int
837mm_sshpam_respond(void *ctx, u_int num, char **resp)
838{
839 Buffer m;
Damien Miller04b65332005-07-17 17:53:31 +1000840 u_int i;
841 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000842
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
857void
858mm_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}
Damien Miller79418552002-04-23 20:28:48 +1000869#endif /* USE_PAM */
870
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000871/* Request process termination */
872
873void
874mm_terminate(void)
875{
876 Buffer m;
877
878 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000879 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000880 buffer_free(&m);
881}
882
883int
884mm_ssh1_session_key(BIGNUM *num)
885{
886 int rsafail;
887 Buffer m;
888
889 buffer_init(&m);
890 buffer_put_bignum2(&m, num);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000891 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000892
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000893 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000894
895 rsafail = buffer_get_int(&m);
896 buffer_get_bignum2(&m, num);
897
898 buffer_free(&m);
899
900 return (rsafail);
901}
902
903static void
904mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
905 char ***prompts, u_int **echo_on)
906{
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000907 *name = xstrdup("");
908 *infotxt = xstrdup("");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000909 *numprompts = 1;
Damien Miller07d86be2006-03-26 14:19:21 +1100910 *prompts = xcalloc(*numprompts, sizeof(char *));
911 *echo_on = xcalloc(*numprompts, sizeof(u_int));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000912 (*echo_on)[0] = 0;
913}
914
915int
916mm_bsdauth_query(void *ctx, char **name, char **infotxt,
917 u_int *numprompts, char ***prompts, u_int **echo_on)
918{
919 Buffer m;
Damien Millerb7df3af2003-02-24 11:55:46 +1100920 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000921 char *challenge;
922
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000923 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000924
925 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000926 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000927
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000928 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000929 &m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100930 success = buffer_get_int(&m);
931 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000932 debug3("%s: no challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000933 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
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000944 debug3("%s: received challenge: %s", __func__, challenge);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000945
946 return (0);
947}
948
949int
950mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
951{
952 Buffer m;
953 int authok;
954
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000955 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000956 if (numresponses != 1)
957 return (-1);
958
959 buffer_init(&m);
960 buffer_put_cstring(&m, responses[0]);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000961 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000962
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000963 mm_request_receive_expect(pmonitor->m_recvfd,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000964 MONITOR_ANS_BSDAUTHRESPOND, &m);
965
966 authok = buffer_get_int(&m);
967 buffer_free(&m);
968
969 return ((authok == 0) ? -1 : 0);
970}
971
Darren Tucker042e2e82004-07-08 23:09:42 +1000972#ifdef SKEY
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000973int
974mm_skey_query(void *ctx, char **name, char **infotxt,
975 u_int *numprompts, char ***prompts, u_int **echo_on)
976{
977 Buffer m;
Damien Millerb7df3af2003-02-24 11:55:46 +1100978 u_int success;
Darren Tuckerd6a23f22006-08-05 18:50:35 +1000979 char *challenge;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000980
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000981 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000982
983 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000984 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000985
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000986 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000987 &m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100988 success = buffer_get_int(&m);
989 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000990 debug3("%s: no challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000991 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
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000999 debug3("%s: received challenge: %s", __func__, challenge);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001000
1001 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
1002
Damien Miller07d86be2006-03-26 14:19:21 +11001003 xasprintf(*prompts, "%s%s", challenge, SKEY_PROMPT);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001004 xfree(challenge);
1005
1006 return (0);
1007}
1008
1009int
1010mm_skey_respond(void *ctx, u_int numresponses, char **responses)
1011{
1012 Buffer m;
1013 int authok;
1014
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001015 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001016 if (numresponses != 1)
1017 return (-1);
1018
1019 buffer_init(&m);
1020 buffer_put_cstring(&m, responses[0]);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001021 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001022
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001023 mm_request_receive_expect(pmonitor->m_recvfd,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001024 MONITOR_ANS_SKEYRESPOND, &m);
1025
1026 authok = buffer_get_int(&m);
1027 buffer_free(&m);
1028
1029 return ((authok == 0) ? -1 : 0);
1030}
Darren Tucker042e2e82004-07-08 23:09:42 +10001031#endif /* SKEY */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001032
1033void
1034mm_ssh1_session_id(u_char session_id[16])
1035{
1036 Buffer m;
1037 int i;
1038
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001039 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001040
1041 buffer_init(&m);
1042 for (i = 0; i < 16; i++)
1043 buffer_put_char(&m, session_id[i]);
1044
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001045 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001046 buffer_free(&m);
1047}
1048
1049int
1050mm_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;
Damien Miller06ebedf2003-02-24 12:03:38 +11001056 int allowed = 0, have_forced = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001057
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001058 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001059
1060 buffer_init(&m);
1061 buffer_put_bignum2(&m, client_n);
1062
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001063 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m);
1064 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001065
1066 allowed = buffer_get_int(&m);
1067
Damien Miller06ebedf2003-02-24 12:03:38 +11001068 /* fake forced command */
1069 auth_clear_options();
1070 have_forced = buffer_get_int(&m);
1071 forced_command = have_forced ? xstrdup("true") : NULL;
1072
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001073 if (allowed && rkey != NULL) {
1074 blob = buffer_get_string(&m, &blen);
1075 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001076 fatal("%s: key_from_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001077 *rkey = key;
1078 xfree(blob);
1079 }
1080 mm_send_debug(&m);
1081 buffer_free(&m);
1082
1083 return (allowed);
1084}
1085
1086BIGNUM *
1087mm_auth_rsa_generate_challenge(Key *key)
1088{
1089 Buffer m;
1090 BIGNUM *challenge;
1091 u_char *blob;
1092 u_int blen;
1093
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001094 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001095
1096 if ((challenge = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001097 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001098
1099 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1100 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001101 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001102 key->type = KEY_RSA1;
1103
1104 buffer_init(&m);
1105 buffer_put_string(&m, blob, blen);
1106 xfree(blob);
1107
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001108 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m);
1109 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001110
1111 buffer_get_bignum2(&m, challenge);
1112 buffer_free(&m);
1113
1114 return (challenge);
1115}
1116
1117int
1118mm_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
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001125 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001126
1127 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1128 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001129 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001130 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
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001137 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m);
1138 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001139
1140 success = buffer_get_int(&m);
1141 buffer_free(&m);
1142
1143 return (success);
1144}
Damien Miller25162f22002-09-12 09:47:29 +10001145
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001146#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001147void
1148mm_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
1161void
1162mm_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}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001174#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001175
Darren Tucker0efd1552003-08-26 11:49:55 +10001176#ifdef GSSAPI
1177OM_uint32
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001178mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
Darren Tucker0efd1552003-08-26 11:49:55 +10001179{
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);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001187 buffer_put_string(&m, goid->elements, goid->length);
Darren Tucker0efd1552003-08-26 11:49:55 +10001188
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
1198OM_uint32
1199mm_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;
Darren Tucker600ad8d2003-08-26 12:10:48 +10001204 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001205
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);
Darren Tucker600ad8d2003-08-26 12:10:48 +10001213 out->value = buffer_get_string(&m, &len);
1214 out->length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001215 if (flags)
1216 *flags = buffer_get_int(&m);
1217
1218 buffer_free(&m);
1219
1220 return (major);
1221}
1222
Damien Miller0425d402003-11-17 22:18:21 +11001223OM_uint32
1224mm_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
Darren Tucker0efd1552003-08-26 11:49:55 +10001242int
1243mm_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 */
Damien Miller01ed2272008-11-05 16:20:46 +11001261
1262#ifdef JPAKE
1263void
1264mm_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
1285void
Damien Millercee85232009-03-06 00:58:22 +11001286mm_jpake_step1(struct modp_group *grp,
Damien Miller01ed2272008-11-05 16:20:46 +11001287 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
1320void
Damien Millercee85232009-03-06 00:58:22 +11001321mm_jpake_step2(struct modp_group *grp, BIGNUM *s,
Damien Miller01ed2272008-11-05 16:20:46 +11001322 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
1360void
Damien Millercee85232009-03-06 00:58:22 +11001361mm_jpake_key_confirm(struct modp_group *grp, BIGNUM *s, BIGNUM *step2_val,
Damien Miller01ed2272008-11-05 16:20:46 +11001362 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
1394int
1395mm_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 */