blob: 1a5dda56158e5c9c9f053a12a379959c8bb3c799 [file] [log] [blame]
Damien Millereb8b60e2010-08-31 22:41:14 +10001/* $OpenBSD: monitor_wrap.c,v 1.70 2010/08/31 11:54:45 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"
Damien Millereb8b60e2010-08-31 22:41:14 +100076#include "uuencode.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000077
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000078#include "channels.h"
79#include "session.h"
Darren Tucker1629c072007-02-19 22:25:37 +110080#include "servconf.h"
Darren Tuckerc5564e12009-06-21 18:53:53 +100081#include "roaming.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000082
83/* Imports */
84extern int compat20;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000085extern z_stream incoming_stream;
86extern z_stream outgoing_stream;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +000087extern struct monitor *pmonitor;
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
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000351int
352mm_key_allowed(enum mm_keytype type, char *user, char *host, Key *key)
353{
354 Buffer m;
355 u_char *blob;
356 u_int len;
Damien Miller06ebedf2003-02-24 12:03:38 +1100357 int allowed = 0, have_forced = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000358
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000359 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000360
361 /* Convert the key to a blob and the pass it over */
362 if (!key_to_blob(key, &blob, &len))
363 return (0);
364
365 buffer_init(&m);
366 buffer_put_int(&m, type);
367 buffer_put_cstring(&m, user ? user : "");
368 buffer_put_cstring(&m, host ? host : "");
369 buffer_put_string(&m, blob, len);
370 xfree(blob);
371
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000372 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000373
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000374 debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000375 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000376
377 allowed = buffer_get_int(&m);
378
Damien Miller06ebedf2003-02-24 12:03:38 +1100379 /* fake forced command */
380 auth_clear_options();
381 have_forced = buffer_get_int(&m);
382 forced_command = have_forced ? xstrdup("true") : NULL;
383
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000384 buffer_free(&m);
385
386 return (allowed);
387}
388
389/*
390 * This key verify needs to send the key type along, because the
391 * privileged parent makes the decision if the key is allowed
392 * for authentication.
393 */
394
395int
396mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
397{
398 Buffer m;
399 u_char *blob;
400 u_int len;
401 int verified = 0;
402
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000403 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000404
405 /* Convert the key to a blob and the pass it over */
406 if (!key_to_blob(key, &blob, &len))
407 return (0);
408
409 buffer_init(&m);
410 buffer_put_string(&m, blob, len);
411 buffer_put_string(&m, sig, siglen);
412 buffer_put_string(&m, data, datalen);
413 xfree(blob);
414
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000415 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000416
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000417 debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000418 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000419
420 verified = buffer_get_int(&m);
421
422 buffer_free(&m);
423
424 return (verified);
425}
426
427/* Export key state after authentication */
428Newkeys *
429mm_newkeys_from_blob(u_char *blob, int blen)
430{
431 Buffer b;
432 u_int len;
433 Newkeys *newkey = NULL;
434 Enc *enc;
435 Mac *mac;
436 Comp *comp;
437
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000438 debug3("%s: %p(%d)", __func__, blob, blen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000439#ifdef DEBUG_PK
440 dump_base64(stderr, blob, blen);
441#endif
442 buffer_init(&b);
443 buffer_append(&b, blob, blen);
444
445 newkey = xmalloc(sizeof(*newkey));
446 enc = &newkey->enc;
447 mac = &newkey->mac;
448 comp = &newkey->comp;
449
450 /* Enc structure */
451 enc->name = buffer_get_string(&b, NULL);
452 buffer_get(&b, &enc->cipher, sizeof(enc->cipher));
453 enc->enabled = buffer_get_int(&b);
454 enc->block_size = buffer_get_int(&b);
455 enc->key = buffer_get_string(&b, &enc->key_len);
456 enc->iv = buffer_get_string(&b, &len);
457 if (len != enc->block_size)
Ben Lindstrom08512492002-06-27 00:23:02 +0000458 fatal("%s: bad ivlen: expected %u != %u", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000459 enc->block_size, len);
460
461 if (enc->name == NULL || cipher_by_name(enc->name) != enc->cipher)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000462 fatal("%s: bad cipher name %s or pointer %p", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000463 enc->name, enc->cipher);
464
465 /* Mac structure */
466 mac->name = buffer_get_string(&b, NULL);
Darren Tucker5f3d5be2007-06-05 18:30:18 +1000467 if (mac->name == NULL || mac_setup(mac, mac->name) == -1)
Damien Millere45796f2007-06-11 14:01:42 +1000468 fatal("%s: can not setup mac %s", __func__, mac->name);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000469 mac->enabled = buffer_get_int(&b);
470 mac->key = buffer_get_string(&b, &len);
471 if (len > mac->key_len)
Ben Lindstrom08512492002-06-27 00:23:02 +0000472 fatal("%s: bad mac key length: %u > %d", __func__, len,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000473 mac->key_len);
474 mac->key_len = len;
475
476 /* Comp structure */
477 comp->type = buffer_get_int(&b);
478 comp->enabled = buffer_get_int(&b);
479 comp->name = buffer_get_string(&b, NULL);
480
481 len = buffer_len(&b);
482 if (len != 0)
Ben Lindstrom08512492002-06-27 00:23:02 +0000483 error("newkeys_from_blob: remaining bytes in blob %u", len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000484 buffer_free(&b);
485 return (newkey);
486}
487
488int
489mm_newkeys_to_blob(int mode, u_char **blobp, u_int *lenp)
490{
491 Buffer b;
492 int len;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000493 Enc *enc;
494 Mac *mac;
495 Comp *comp;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000496 Newkeys *newkey = (Newkeys *)packet_get_newkeys(mode);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000497
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000498 debug3("%s: converting %p", __func__, newkey);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000499
500 if (newkey == NULL) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000501 error("%s: newkey == NULL", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000502 return 0;
503 }
504 enc = &newkey->enc;
505 mac = &newkey->mac;
506 comp = &newkey->comp;
507
508 buffer_init(&b);
509 /* Enc structure */
510 buffer_put_cstring(&b, enc->name);
511 /* The cipher struct is constant and shared, you export pointer */
512 buffer_append(&b, &enc->cipher, sizeof(enc->cipher));
513 buffer_put_int(&b, enc->enabled);
514 buffer_put_int(&b, enc->block_size);
515 buffer_put_string(&b, enc->key, enc->key_len);
516 packet_get_keyiv(mode, enc->iv, enc->block_size);
517 buffer_put_string(&b, enc->iv, enc->block_size);
518
519 /* Mac structure */
520 buffer_put_cstring(&b, mac->name);
521 buffer_put_int(&b, mac->enabled);
522 buffer_put_string(&b, mac->key, mac->key_len);
523
524 /* Comp structure */
525 buffer_put_int(&b, comp->type);
526 buffer_put_int(&b, comp->enabled);
527 buffer_put_cstring(&b, comp->name);
528
529 len = buffer_len(&b);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000530 if (lenp != NULL)
531 *lenp = len;
Ben Lindstrom2bf759c2002-07-07 22:13:31 +0000532 if (blobp != NULL) {
533 *blobp = xmalloc(len);
534 memcpy(*blobp, buffer_ptr(&b), len);
535 }
536 memset(buffer_ptr(&b), 0, len);
537 buffer_free(&b);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000538 return len;
539}
540
541static void
542mm_send_kex(Buffer *m, Kex *kex)
543{
544 buffer_put_string(m, kex->session_id, kex->session_id_len);
545 buffer_put_int(m, kex->we_need);
546 buffer_put_int(m, kex->hostkey_type);
547 buffer_put_int(m, kex->kex_type);
548 buffer_put_string(m, buffer_ptr(&kex->my), buffer_len(&kex->my));
549 buffer_put_string(m, buffer_ptr(&kex->peer), buffer_len(&kex->peer));
550 buffer_put_int(m, kex->flags);
551 buffer_put_cstring(m, kex->client_version_string);
552 buffer_put_cstring(m, kex->server_version_string);
553}
554
555void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000556mm_send_keystate(struct monitor *monitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000557{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000558 Buffer m, *input, *output;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000559 u_char *blob, *p;
560 u_int bloblen, plen;
Damien Millera5539d22003-04-09 20:50:06 +1000561 u_int32_t seqnr, packets;
Damien Millerb61f3fc2008-07-11 17:36:48 +1000562 u_int64_t blocks, bytes;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000563
564 buffer_init(&m);
565
566 if (!compat20) {
567 u_char iv[24];
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000568 u_char *key;
569 u_int ivlen, keylen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000570
571 buffer_put_int(&m, packet_get_protocol_flags());
572
573 buffer_put_int(&m, packet_get_ssh1_cipher());
574
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000575 debug3("%s: Sending ssh1 KEY+IV", __func__);
576 keylen = packet_get_encryption_key(NULL);
577 key = xmalloc(keylen+1); /* add 1 if keylen == 0 */
578 keylen = packet_get_encryption_key(key);
579 buffer_put_string(&m, key, keylen);
580 memset(key, 0, keylen);
581 xfree(key);
582
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000583 ivlen = packet_get_keyiv_len(MODE_OUT);
584 packet_get_keyiv(MODE_OUT, iv, ivlen);
585 buffer_put_string(&m, iv, ivlen);
586 ivlen = packet_get_keyiv_len(MODE_OUT);
587 packet_get_keyiv(MODE_IN, iv, ivlen);
588 buffer_put_string(&m, iv, ivlen);
589 goto skip;
590 } else {
591 /* Kex for rekeying */
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000592 mm_send_kex(&m, *monitor->m_pkex);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000593 }
594
595 debug3("%s: Sending new keys: %p %p",
Darren Tuckerf7288d72009-06-21 18:12:20 +1000596 __func__, packet_get_newkeys(MODE_OUT),
597 packet_get_newkeys(MODE_IN));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000598
599 /* Keys from Kex */
600 if (!mm_newkeys_to_blob(MODE_OUT, &blob, &bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000601 fatal("%s: conversion of newkeys failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000602
603 buffer_put_string(&m, blob, bloblen);
604 xfree(blob);
605
606 if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000607 fatal("%s: conversion of newkeys failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000608
609 buffer_put_string(&m, blob, bloblen);
610 xfree(blob);
611
Damien Millerb61f3fc2008-07-11 17:36:48 +1000612 packet_get_state(MODE_OUT, &seqnr, &blocks, &packets, &bytes);
Damien Millera5539d22003-04-09 20:50:06 +1000613 buffer_put_int(&m, seqnr);
614 buffer_put_int64(&m, blocks);
615 buffer_put_int(&m, packets);
Damien Millerb61f3fc2008-07-11 17:36:48 +1000616 buffer_put_int64(&m, bytes);
617 packet_get_state(MODE_IN, &seqnr, &blocks, &packets, &bytes);
Damien Millera5539d22003-04-09 20:50:06 +1000618 buffer_put_int(&m, seqnr);
619 buffer_put_int64(&m, blocks);
620 buffer_put_int(&m, packets);
Damien Millerb61f3fc2008-07-11 17:36:48 +1000621 buffer_put_int64(&m, bytes);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000622
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000623 debug3("%s: New keys have been sent", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000624 skip:
625 /* More key context */
626 plen = packet_get_keycontext(MODE_OUT, NULL);
627 p = xmalloc(plen+1);
628 packet_get_keycontext(MODE_OUT, p);
629 buffer_put_string(&m, p, plen);
630 xfree(p);
631
632 plen = packet_get_keycontext(MODE_IN, NULL);
633 p = xmalloc(plen+1);
634 packet_get_keycontext(MODE_IN, p);
635 buffer_put_string(&m, p, plen);
636 xfree(p);
637
638 /* Compression state */
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000639 debug3("%s: Sending compression state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000640 buffer_put_string(&m, &outgoing_stream, sizeof(outgoing_stream));
641 buffer_put_string(&m, &incoming_stream, sizeof(incoming_stream));
642
643 /* Network I/O buffers */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000644 input = (Buffer *)packet_get_input();
645 output = (Buffer *)packet_get_output();
646 buffer_put_string(&m, buffer_ptr(input), buffer_len(input));
647 buffer_put_string(&m, buffer_ptr(output), buffer_len(output));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000648
Darren Tuckerc5564e12009-06-21 18:53:53 +1000649 /* Roaming */
650 if (compat20) {
651 buffer_put_int64(&m, get_sent_bytes());
652 buffer_put_int64(&m, get_recv_bytes());
653 }
654
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000655 mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m);
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000656 debug3("%s: Finished sending state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000657
658 buffer_free(&m);
659}
660
661int
Damien Miller71a73672006-03-26 14:04:36 +1100662mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000663{
664 Buffer m;
Darren Tucker09991742004-07-17 17:05:14 +1000665 char *p, *msg;
Damien Miller7207f642008-05-19 15:34:50 +1000666 int success = 0, tmp1 = -1, tmp2 = -1;
667
668 /* Kludge: ensure there are fds free to receive the pty/tty */
669 if ((tmp1 = dup(pmonitor->m_recvfd)) == -1 ||
670 (tmp2 = dup(pmonitor->m_recvfd)) == -1) {
671 error("%s: cannot allocate fds for pty", __func__);
672 if (tmp1 > 0)
673 close(tmp1);
674 if (tmp2 > 0)
675 close(tmp2);
676 return 0;
677 }
678 close(tmp1);
679 close(tmp2);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000680
681 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000682 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000683
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000684 debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000685 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000686
687 success = buffer_get_int(&m);
688 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000689 debug3("%s: pty alloc failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000690 buffer_free(&m);
691 return (0);
692 }
693 p = buffer_get_string(&m, NULL);
Darren Tucker09991742004-07-17 17:05:14 +1000694 msg = buffer_get_string(&m, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000695 buffer_free(&m);
696
697 strlcpy(namebuf, p, namebuflen); /* Possible truncation */
698 xfree(p);
699
Darren Tucker09991742004-07-17 17:05:14 +1000700 buffer_append(&loginmsg, msg, strlen(msg));
701 xfree(msg);
702
Damien Miller54fd7cf2007-09-17 12:04:08 +1000703 if ((*ptyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1 ||
704 (*ttyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1)
705 fatal("%s: receive fds failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000706
707 /* Success */
708 return (1);
709}
710
711void
Darren Tucker3e33cec2003-10-02 16:12:36 +1000712mm_session_pty_cleanup2(Session *s)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000713{
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000714 Buffer m;
715
716 if (s->ttyfd == -1)
717 return;
718 buffer_init(&m);
719 buffer_put_cstring(&m, s->tty);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000720 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000721 buffer_free(&m);
722
723 /* closed dup'ed master */
Damien Miller7207f642008-05-19 15:34:50 +1000724 if (s->ptymaster != -1 && close(s->ptymaster) < 0)
725 error("close(s->ptymaster/%d): %s",
726 s->ptymaster, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000727
728 /* unlink pty from session */
729 s->ttyfd = -1;
730}
731
Damien Miller79418552002-04-23 20:28:48 +1000732#ifdef USE_PAM
733void
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100734mm_start_pam(Authctxt *authctxt)
Damien Miller79418552002-04-23 20:28:48 +1000735{
736 Buffer m;
737
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000738 debug3("%s entering", __func__);
Damien Miller4e448a32003-05-14 15:11:48 +1000739 if (!options.use_pam)
740 fatal("UsePAM=no, but ended up in %s anyway", __func__);
Damien Miller79418552002-04-23 20:28:48 +1000741
742 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000743 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, &m);
Damien Miller79418552002-04-23 20:28:48 +1000744
745 buffer_free(&m);
746}
Damien Miller4f9f42a2003-05-10 19:28:02 +1000747
Damien Miller1f499fd2003-08-25 13:08:49 +1000748u_int
749mm_do_pam_account(void)
750{
751 Buffer m;
752 u_int ret;
Darren Tucker77fc29e2004-09-11 23:07:03 +1000753 char *msg;
Damien Miller1f499fd2003-08-25 13:08:49 +1000754
755 debug3("%s entering", __func__);
756 if (!options.use_pam)
757 fatal("UsePAM=no, but ended up in %s anyway", __func__);
758
759 buffer_init(&m);
760 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_ACCOUNT, &m);
761
Damien Millera8e06ce2003-11-21 23:48:55 +1100762 mm_request_receive_expect(pmonitor->m_recvfd,
Damien Miller1f499fd2003-08-25 13:08:49 +1000763 MONITOR_ANS_PAM_ACCOUNT, &m);
764 ret = buffer_get_int(&m);
Darren Tucker77fc29e2004-09-11 23:07:03 +1000765 msg = buffer_get_string(&m, NULL);
766 buffer_append(&loginmsg, msg, strlen(msg));
767 xfree(msg);
Damien Miller1f499fd2003-08-25 13:08:49 +1000768
769 buffer_free(&m);
Damien Miller787b2ec2003-11-21 23:56:47 +1100770
Damien Miller1f499fd2003-08-25 13:08:49 +1000771 debug3("%s returning %d", __func__, ret);
772
773 return (ret);
774}
775
Damien Miller4f9f42a2003-05-10 19:28:02 +1000776void *
777mm_sshpam_init_ctx(Authctxt *authctxt)
778{
779 Buffer m;
780 int success;
781
782 debug3("%s", __func__);
783 buffer_init(&m);
784 buffer_put_cstring(&m, authctxt->user);
785 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_INIT_CTX, &m);
786 debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__);
787 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_INIT_CTX, &m);
788 success = buffer_get_int(&m);
789 if (success == 0) {
790 debug3("%s: pam_init_ctx failed", __func__);
791 buffer_free(&m);
792 return (NULL);
793 }
794 buffer_free(&m);
795 return (authctxt);
796}
797
798int
799mm_sshpam_query(void *ctx, char **name, char **info,
800 u_int *num, char ***prompts, u_int **echo_on)
801{
802 Buffer m;
Damien Miller04b65332005-07-17 17:53:31 +1000803 u_int i;
804 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000805
806 debug3("%s", __func__);
807 buffer_init(&m);
808 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_QUERY, &m);
809 debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__);
810 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_QUERY, &m);
811 ret = buffer_get_int(&m);
812 debug3("%s: pam_query returned %d", __func__, ret);
813 *name = buffer_get_string(&m, NULL);
814 *info = buffer_get_string(&m, NULL);
815 *num = buffer_get_int(&m);
Darren Tuckerd8093e42006-05-04 16:24:34 +1000816 if (*num > PAM_MAX_NUM_MSG)
817 fatal("%s: recieved %u PAM messages, expected <= %u",
818 __func__, *num, PAM_MAX_NUM_MSG);
819 *prompts = xcalloc((*num + 1), sizeof(char *));
820 *echo_on = xcalloc((*num + 1), sizeof(u_int));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000821 for (i = 0; i < *num; ++i) {
822 (*prompts)[i] = buffer_get_string(&m, NULL);
823 (*echo_on)[i] = buffer_get_int(&m);
824 }
825 buffer_free(&m);
826 return (ret);
827}
828
829int
830mm_sshpam_respond(void *ctx, u_int num, char **resp)
831{
832 Buffer m;
Damien Miller04b65332005-07-17 17:53:31 +1000833 u_int i;
834 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000835
836 debug3("%s", __func__);
837 buffer_init(&m);
838 buffer_put_int(&m, num);
839 for (i = 0; i < num; ++i)
840 buffer_put_cstring(&m, resp[i]);
841 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_RESPOND, &m);
842 debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__);
843 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_RESPOND, &m);
844 ret = buffer_get_int(&m);
845 debug3("%s: pam_respond returned %d", __func__, ret);
846 buffer_free(&m);
847 return (ret);
848}
849
850void
851mm_sshpam_free_ctx(void *ctxtp)
852{
853 Buffer m;
854
855 debug3("%s", __func__);
856 buffer_init(&m);
857 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_FREE_CTX, &m);
858 debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__);
859 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_FREE_CTX, &m);
860 buffer_free(&m);
861}
Damien Miller79418552002-04-23 20:28:48 +1000862#endif /* USE_PAM */
863
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000864/* Request process termination */
865
866void
867mm_terminate(void)
868{
869 Buffer m;
870
871 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000872 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000873 buffer_free(&m);
874}
875
876int
877mm_ssh1_session_key(BIGNUM *num)
878{
879 int rsafail;
880 Buffer m;
881
882 buffer_init(&m);
883 buffer_put_bignum2(&m, num);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000884 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000885
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000886 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000887
888 rsafail = buffer_get_int(&m);
889 buffer_get_bignum2(&m, num);
890
891 buffer_free(&m);
892
893 return (rsafail);
894}
895
896static void
897mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
898 char ***prompts, u_int **echo_on)
899{
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000900 *name = xstrdup("");
901 *infotxt = xstrdup("");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000902 *numprompts = 1;
Damien Miller07d86be2006-03-26 14:19:21 +1100903 *prompts = xcalloc(*numprompts, sizeof(char *));
904 *echo_on = xcalloc(*numprompts, sizeof(u_int));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000905 (*echo_on)[0] = 0;
906}
907
908int
909mm_bsdauth_query(void *ctx, char **name, char **infotxt,
910 u_int *numprompts, char ***prompts, u_int **echo_on)
911{
912 Buffer m;
Damien Millerb7df3af2003-02-24 11:55:46 +1100913 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000914 char *challenge;
915
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000916 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000917
918 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000919 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000920
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000921 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000922 &m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100923 success = buffer_get_int(&m);
924 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000925 debug3("%s: no challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000926 buffer_free(&m);
927 return (-1);
928 }
929
930 /* Get the challenge, and format the response */
931 challenge = buffer_get_string(&m, NULL);
932 buffer_free(&m);
933
934 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
935 (*prompts)[0] = challenge;
936
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000937 debug3("%s: received challenge: %s", __func__, challenge);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000938
939 return (0);
940}
941
942int
943mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
944{
945 Buffer m;
946 int authok;
947
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000948 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000949 if (numresponses != 1)
950 return (-1);
951
952 buffer_init(&m);
953 buffer_put_cstring(&m, responses[0]);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000954 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000955
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000956 mm_request_receive_expect(pmonitor->m_recvfd,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000957 MONITOR_ANS_BSDAUTHRESPOND, &m);
958
959 authok = buffer_get_int(&m);
960 buffer_free(&m);
961
962 return ((authok == 0) ? -1 : 0);
963}
964
Darren Tucker042e2e82004-07-08 23:09:42 +1000965#ifdef SKEY
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000966int
967mm_skey_query(void *ctx, char **name, char **infotxt,
968 u_int *numprompts, char ***prompts, u_int **echo_on)
969{
970 Buffer m;
Damien Millerb7df3af2003-02-24 11:55:46 +1100971 u_int success;
Darren Tuckerd6a23f22006-08-05 18:50:35 +1000972 char *challenge;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000973
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000974 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000975
976 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000977 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000978
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000979 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000980 &m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100981 success = buffer_get_int(&m);
982 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000983 debug3("%s: no challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000984 buffer_free(&m);
985 return (-1);
986 }
987
988 /* Get the challenge, and format the response */
989 challenge = buffer_get_string(&m, NULL);
990 buffer_free(&m);
991
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000992 debug3("%s: received challenge: %s", __func__, challenge);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000993
994 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
995
Damien Miller07d86be2006-03-26 14:19:21 +1100996 xasprintf(*prompts, "%s%s", challenge, SKEY_PROMPT);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000997 xfree(challenge);
998
999 return (0);
1000}
1001
1002int
1003mm_skey_respond(void *ctx, u_int numresponses, char **responses)
1004{
1005 Buffer m;
1006 int authok;
1007
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001008 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001009 if (numresponses != 1)
1010 return (-1);
1011
1012 buffer_init(&m);
1013 buffer_put_cstring(&m, responses[0]);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001014 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001015
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001016 mm_request_receive_expect(pmonitor->m_recvfd,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001017 MONITOR_ANS_SKEYRESPOND, &m);
1018
1019 authok = buffer_get_int(&m);
1020 buffer_free(&m);
1021
1022 return ((authok == 0) ? -1 : 0);
1023}
Darren Tucker042e2e82004-07-08 23:09:42 +10001024#endif /* SKEY */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001025
1026void
1027mm_ssh1_session_id(u_char session_id[16])
1028{
1029 Buffer m;
1030 int i;
1031
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001032 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001033
1034 buffer_init(&m);
1035 for (i = 0; i < 16; i++)
1036 buffer_put_char(&m, session_id[i]);
1037
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001038 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001039 buffer_free(&m);
1040}
1041
1042int
1043mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
1044{
1045 Buffer m;
1046 Key *key;
1047 u_char *blob;
1048 u_int blen;
Damien Miller06ebedf2003-02-24 12:03:38 +11001049 int allowed = 0, have_forced = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001050
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001051 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001052
1053 buffer_init(&m);
1054 buffer_put_bignum2(&m, client_n);
1055
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001056 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m);
1057 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001058
1059 allowed = buffer_get_int(&m);
1060
Damien Miller06ebedf2003-02-24 12:03:38 +11001061 /* fake forced command */
1062 auth_clear_options();
1063 have_forced = buffer_get_int(&m);
1064 forced_command = have_forced ? xstrdup("true") : NULL;
1065
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001066 if (allowed && rkey != NULL) {
1067 blob = buffer_get_string(&m, &blen);
1068 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001069 fatal("%s: key_from_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001070 *rkey = key;
1071 xfree(blob);
1072 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001073 buffer_free(&m);
1074
1075 return (allowed);
1076}
1077
1078BIGNUM *
1079mm_auth_rsa_generate_challenge(Key *key)
1080{
1081 Buffer m;
1082 BIGNUM *challenge;
1083 u_char *blob;
1084 u_int blen;
1085
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001086 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001087
1088 if ((challenge = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001089 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001090
1091 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1092 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001093 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001094 key->type = KEY_RSA1;
1095
1096 buffer_init(&m);
1097 buffer_put_string(&m, blob, blen);
1098 xfree(blob);
1099
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001100 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m);
1101 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001102
1103 buffer_get_bignum2(&m, challenge);
1104 buffer_free(&m);
1105
1106 return (challenge);
1107}
1108
1109int
1110mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16])
1111{
1112 Buffer m;
1113 u_char *blob;
1114 u_int blen;
1115 int success = 0;
1116
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001117 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001118
1119 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1120 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001121 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001122 key->type = KEY_RSA1;
1123
1124 buffer_init(&m);
1125 buffer_put_string(&m, blob, blen);
1126 buffer_put_string(&m, response, 16);
1127 xfree(blob);
1128
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001129 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m);
1130 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001131
1132 success = buffer_get_int(&m);
1133 buffer_free(&m);
1134
1135 return (success);
1136}
Damien Miller25162f22002-09-12 09:47:29 +10001137
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001138#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001139void
1140mm_audit_event(ssh_audit_event_t event)
1141{
1142 Buffer m;
1143
1144 debug3("%s entering", __func__);
1145
1146 buffer_init(&m);
1147 buffer_put_int(&m, event);
1148
1149 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_EVENT, &m);
1150 buffer_free(&m);
1151}
1152
1153void
1154mm_audit_run_command(const char *command)
1155{
1156 Buffer m;
1157
1158 debug3("%s entering command %s", __func__, command);
1159
1160 buffer_init(&m);
1161 buffer_put_cstring(&m, command);
1162
1163 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, &m);
1164 buffer_free(&m);
1165}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001166#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001167
Darren Tucker0efd1552003-08-26 11:49:55 +10001168#ifdef GSSAPI
1169OM_uint32
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001170mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
Darren Tucker0efd1552003-08-26 11:49:55 +10001171{
1172 Buffer m;
1173 OM_uint32 major;
1174
1175 /* Client doesn't get to see the context */
1176 *ctx = NULL;
1177
1178 buffer_init(&m);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001179 buffer_put_string(&m, goid->elements, goid->length);
Darren Tucker0efd1552003-08-26 11:49:55 +10001180
1181 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m);
1182 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m);
1183
1184 major = buffer_get_int(&m);
1185
1186 buffer_free(&m);
1187 return (major);
1188}
1189
1190OM_uint32
1191mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
1192 gss_buffer_desc *out, OM_uint32 *flags)
1193{
1194 Buffer m;
1195 OM_uint32 major;
Darren Tucker600ad8d2003-08-26 12:10:48 +10001196 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001197
1198 buffer_init(&m);
1199 buffer_put_string(&m, in->value, in->length);
1200
1201 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m);
1202 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m);
1203
1204 major = buffer_get_int(&m);
Darren Tucker600ad8d2003-08-26 12:10:48 +10001205 out->value = buffer_get_string(&m, &len);
1206 out->length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001207 if (flags)
1208 *flags = buffer_get_int(&m);
1209
1210 buffer_free(&m);
1211
1212 return (major);
1213}
1214
Damien Miller0425d402003-11-17 22:18:21 +11001215OM_uint32
1216mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
1217{
1218 Buffer m;
1219 OM_uint32 major;
1220
1221 buffer_init(&m);
1222 buffer_put_string(&m, gssbuf->value, gssbuf->length);
1223 buffer_put_string(&m, gssmic->value, gssmic->length);
1224
1225 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, &m);
1226 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSCHECKMIC,
1227 &m);
1228
1229 major = buffer_get_int(&m);
1230 buffer_free(&m);
1231 return(major);
1232}
1233
Darren Tucker0efd1552003-08-26 11:49:55 +10001234int
1235mm_ssh_gssapi_userok(char *user)
1236{
1237 Buffer m;
1238 int authenticated = 0;
1239
1240 buffer_init(&m);
1241
1242 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m);
1243 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK,
1244 &m);
1245
1246 authenticated = buffer_get_int(&m);
1247
1248 buffer_free(&m);
1249 debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
1250 return (authenticated);
1251}
1252#endif /* GSSAPI */
Damien Miller01ed2272008-11-05 16:20:46 +11001253
1254#ifdef JPAKE
1255void
1256mm_auth2_jpake_get_pwdata(Authctxt *authctxt, BIGNUM **s,
1257 char **hash_scheme, char **salt)
1258{
1259 Buffer m;
1260
1261 debug3("%s entering", __func__);
1262
1263 buffer_init(&m);
1264 mm_request_send(pmonitor->m_recvfd,
1265 MONITOR_REQ_JPAKE_GET_PWDATA, &m);
1266
1267 debug3("%s: waiting for MONITOR_ANS_JPAKE_GET_PWDATA", __func__);
1268 mm_request_receive_expect(pmonitor->m_recvfd,
1269 MONITOR_ANS_JPAKE_GET_PWDATA, &m);
1270
1271 *hash_scheme = buffer_get_string(&m, NULL);
1272 *salt = buffer_get_string(&m, NULL);
1273
1274 buffer_free(&m);
1275}
1276
1277void
Damien Millercee85232009-03-06 00:58:22 +11001278mm_jpake_step1(struct modp_group *grp,
Damien Miller01ed2272008-11-05 16:20:46 +11001279 u_char **id, u_int *id_len,
1280 BIGNUM **priv1, BIGNUM **priv2, BIGNUM **g_priv1, BIGNUM **g_priv2,
1281 u_char **priv1_proof, u_int *priv1_proof_len,
1282 u_char **priv2_proof, u_int *priv2_proof_len)
1283{
1284 Buffer m;
1285
1286 debug3("%s entering", __func__);
1287
1288 buffer_init(&m);
1289 mm_request_send(pmonitor->m_recvfd,
1290 MONITOR_REQ_JPAKE_STEP1, &m);
1291
1292 debug3("%s: waiting for MONITOR_ANS_JPAKE_STEP1", __func__);
1293 mm_request_receive_expect(pmonitor->m_recvfd,
1294 MONITOR_ANS_JPAKE_STEP1, &m);
1295
1296 if ((*priv1 = BN_new()) == NULL ||
1297 (*priv2 = BN_new()) == NULL ||
1298 (*g_priv1 = BN_new()) == NULL ||
1299 (*g_priv2 = BN_new()) == NULL)
1300 fatal("%s: BN_new", __func__);
1301
1302 *id = buffer_get_string(&m, id_len);
1303 /* priv1 and priv2 are, well, private */
1304 buffer_get_bignum2(&m, *g_priv1);
1305 buffer_get_bignum2(&m, *g_priv2);
1306 *priv1_proof = buffer_get_string(&m, priv1_proof_len);
1307 *priv2_proof = buffer_get_string(&m, priv2_proof_len);
1308
1309 buffer_free(&m);
1310}
1311
1312void
Damien Millercee85232009-03-06 00:58:22 +11001313mm_jpake_step2(struct modp_group *grp, BIGNUM *s,
Damien Miller01ed2272008-11-05 16:20:46 +11001314 BIGNUM *mypub1, BIGNUM *theirpub1, BIGNUM *theirpub2, BIGNUM *mypriv2,
1315 const u_char *theirid, u_int theirid_len,
1316 const u_char *myid, u_int myid_len,
1317 const u_char *theirpub1_proof, u_int theirpub1_proof_len,
1318 const u_char *theirpub2_proof, u_int theirpub2_proof_len,
1319 BIGNUM **newpub,
1320 u_char **newpub_exponent_proof, u_int *newpub_exponent_proof_len)
1321{
1322 Buffer m;
1323
1324 debug3("%s entering", __func__);
1325
1326 buffer_init(&m);
1327 /* monitor already has all bignums except theirpub1, theirpub2 */
1328 buffer_put_bignum2(&m, theirpub1);
1329 buffer_put_bignum2(&m, theirpub2);
1330 /* monitor already knows our id */
1331 buffer_put_string(&m, theirid, theirid_len);
1332 buffer_put_string(&m, theirpub1_proof, theirpub1_proof_len);
1333 buffer_put_string(&m, theirpub2_proof, theirpub2_proof_len);
1334
1335 mm_request_send(pmonitor->m_recvfd,
1336 MONITOR_REQ_JPAKE_STEP2, &m);
1337
1338 debug3("%s: waiting for MONITOR_ANS_JPAKE_STEP2", __func__);
1339 mm_request_receive_expect(pmonitor->m_recvfd,
1340 MONITOR_ANS_JPAKE_STEP2, &m);
1341
1342 if ((*newpub = BN_new()) == NULL)
1343 fatal("%s: BN_new", __func__);
1344
1345 buffer_get_bignum2(&m, *newpub);
1346 *newpub_exponent_proof = buffer_get_string(&m,
1347 newpub_exponent_proof_len);
1348
1349 buffer_free(&m);
1350}
1351
1352void
Damien Millercee85232009-03-06 00:58:22 +11001353mm_jpake_key_confirm(struct modp_group *grp, BIGNUM *s, BIGNUM *step2_val,
Damien Miller01ed2272008-11-05 16:20:46 +11001354 BIGNUM *mypriv2, BIGNUM *mypub1, BIGNUM *mypub2,
1355 BIGNUM *theirpub1, BIGNUM *theirpub2,
1356 const u_char *my_id, u_int my_id_len,
1357 const u_char *their_id, u_int their_id_len,
1358 const u_char *sess_id, u_int sess_id_len,
1359 const u_char *theirpriv2_s_proof, u_int theirpriv2_s_proof_len,
1360 BIGNUM **k,
1361 u_char **confirm_hash, u_int *confirm_hash_len)
1362{
1363 Buffer m;
1364
1365 debug3("%s entering", __func__);
1366
1367 buffer_init(&m);
1368 /* monitor already has all bignums except step2_val */
1369 buffer_put_bignum2(&m, step2_val);
1370 /* monitor already knows all the ids */
1371 buffer_put_string(&m, theirpriv2_s_proof, theirpriv2_s_proof_len);
1372
1373 mm_request_send(pmonitor->m_recvfd,
1374 MONITOR_REQ_JPAKE_KEY_CONFIRM, &m);
1375
1376 debug3("%s: waiting for MONITOR_ANS_JPAKE_KEY_CONFIRM", __func__);
1377 mm_request_receive_expect(pmonitor->m_recvfd,
1378 MONITOR_ANS_JPAKE_KEY_CONFIRM, &m);
1379
1380 /* 'k' is sensitive and stays in the monitor */
1381 *confirm_hash = buffer_get_string(&m, confirm_hash_len);
1382
1383 buffer_free(&m);
1384}
1385
1386int
1387mm_jpake_check_confirm(const BIGNUM *k,
1388 const u_char *peer_id, u_int peer_id_len,
1389 const u_char *sess_id, u_int sess_id_len,
1390 const u_char *peer_confirm_hash, u_int peer_confirm_hash_len)
1391{
1392 Buffer m;
1393 int success = 0;
1394
1395 debug3("%s entering", __func__);
1396
1397 buffer_init(&m);
1398 /* k is dummy in slave, ignored */
1399 /* monitor knows all the ids */
1400 buffer_put_string(&m, peer_confirm_hash, peer_confirm_hash_len);
1401 mm_request_send(pmonitor->m_recvfd,
1402 MONITOR_REQ_JPAKE_CHECK_CONFIRM, &m);
1403
1404 debug3("%s: waiting for MONITOR_ANS_JPAKE_CHECK_CONFIRM", __func__);
1405 mm_request_receive_expect(pmonitor->m_recvfd,
1406 MONITOR_ANS_JPAKE_CHECK_CONFIRM, &m);
1407
1408 success = buffer_get_int(&m);
1409 buffer_free(&m);
1410
1411 debug3("%s: success = %d", __func__, success);
1412 return success;
1413}
1414#endif /* JPAKE */