blob: faeb02cfa35942fd5bc9644d798b9c9cc683d560 [file] [log] [blame]
Darren Tuckercd70e1b2010-03-07 23:05:17 +11001/* $OpenBSD: monitor_wrap.c,v 1.69 2010/03/07 11:57:13 dtucker 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"
Darren Tuckerc5564e12009-06-21 18:53:53 +100080#include "roaming.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000081
82/* Imports */
83extern int compat20;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000084extern z_stream incoming_stream;
85extern z_stream outgoing_stream;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +000086extern struct monitor *pmonitor;
Darren Tucker09991742004-07-17 17:05:14 +100087extern Buffer loginmsg;
Damien Miller4e448a32003-05-14 15:11:48 +100088extern ServerOptions options;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000089
Darren Tucker3e33cec2003-10-02 16:12:36 +100090int
91mm_is_monitor(void)
92{
93 /*
94 * m_pid is only set in the privileged part, and
95 * points to the unprivileged child.
96 */
Darren Tuckera47c9bc2003-11-03 20:03:25 +110097 return (pmonitor && pmonitor->m_pid > 0);
Darren Tucker3e33cec2003-10-02 16:12:36 +100098}
99
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000100void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000101mm_request_send(int sock, enum monitor_reqtype type, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000102{
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000103 u_int mlen = buffer_len(m);
Ben Lindstromb1bdc5a2002-07-04 00:09:26 +0000104 u_char buf[5];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000105
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000106 debug3("%s entering: type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000107
Damien Miller3f941882006-03-31 23:13:02 +1100108 put_u32(buf, mlen + 1);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000109 buf[4] = (u_char) type; /* 1st byte of payload is mesg-type */
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000110 if (atomicio(vwrite, sock, buf, sizeof(buf)) != sizeof(buf))
Damien Millerb253cc42005-05-26 12:23:44 +1000111 fatal("%s: write: %s", __func__, strerror(errno));
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000112 if (atomicio(vwrite, sock, buffer_ptr(m), mlen) != mlen)
Damien Millerb253cc42005-05-26 12:23:44 +1000113 fatal("%s: write: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000114}
115
116void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000117mm_request_receive(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000118{
119 u_char buf[4];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000120 u_int msg_len;
121
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000122 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000123
Damien Millerb253cc42005-05-26 12:23:44 +1000124 if (atomicio(read, sock, buf, sizeof(buf)) != sizeof(buf)) {
125 if (errno == EPIPE)
Darren Tucker3e33cec2003-10-02 16:12:36 +1000126 cleanup_exit(255);
Damien Millerb253cc42005-05-26 12:23:44 +1000127 fatal("%s: read: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000128 }
Damien Miller3f941882006-03-31 23:13:02 +1100129 msg_len = get_u32(buf);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000130 if (msg_len > 256 * 1024)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000131 fatal("%s: read: bad msg_len %d", __func__, msg_len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000132 buffer_clear(m);
133 buffer_append_space(m, msg_len);
Damien Millerb253cc42005-05-26 12:23:44 +1000134 if (atomicio(read, sock, buffer_ptr(m), msg_len) != msg_len)
135 fatal("%s: read: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000136}
137
138void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000139mm_request_receive_expect(int sock, enum monitor_reqtype type, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000140{
141 u_char rtype;
142
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000143 debug3("%s entering: type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000144
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000145 mm_request_receive(sock, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000146 rtype = buffer_get_char(m);
147 if (rtype != type)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000148 fatal("%s: read: rtype %d != type %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000149 rtype, type);
150}
151
152DH *
153mm_choose_dh(int min, int nbits, int max)
154{
155 BIGNUM *p, *g;
156 int success = 0;
157 Buffer m;
158
159 buffer_init(&m);
160 buffer_put_int(&m, min);
161 buffer_put_int(&m, nbits);
162 buffer_put_int(&m, max);
163
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000164 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000165
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000166 debug3("%s: waiting for MONITOR_ANS_MODULI", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000167 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000168
169 success = buffer_get_char(&m);
170 if (success == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000171 fatal("%s: MONITOR_ANS_MODULI failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000172
173 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000174 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000175 if ((g = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000176 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000177 buffer_get_bignum2(&m, p);
178 buffer_get_bignum2(&m, g);
179
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000180 debug3("%s: remaining %d", __func__, buffer_len(&m));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000181 buffer_free(&m);
182
183 return (dh_new_group(g, p));
184}
185
186int
187mm_key_sign(Key *key, u_char **sigp, u_int *lenp, u_char *data, u_int datalen)
188{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000189 Kex *kex = *pmonitor->m_pkex;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000190 Buffer m;
191
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000192 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000193
194 buffer_init(&m);
195 buffer_put_int(&m, kex->host_key_index(key));
196 buffer_put_string(&m, data, datalen);
197
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000198 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000199
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000200 debug3("%s: waiting for MONITOR_ANS_SIGN", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000201 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000202 *sigp = buffer_get_string(&m, lenp);
203 buffer_free(&m);
204
205 return (0);
206}
207
208struct passwd *
Darren Tuckerb09b6772004-06-22 15:06:46 +1000209mm_getpwnamallow(const char *username)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000210{
211 Buffer m;
212 struct passwd *pw;
Darren Tucker1629c072007-02-19 22:25:37 +1100213 u_int len;
214 ServerOptions *newopts;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000215
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000216 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000217
218 buffer_init(&m);
Darren Tuckerb09b6772004-06-22 15:06:46 +1000219 buffer_put_cstring(&m, username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000220
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000221 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000222
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000223 debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000224 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000225
226 if (buffer_get_char(&m) == 0) {
Darren Tucker2f8b3d92007-12-02 23:02:15 +1100227 pw = NULL;
228 goto out;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000229 }
Darren Tucker1629c072007-02-19 22:25:37 +1100230 pw = buffer_get_string(&m, &len);
231 if (len != sizeof(struct passwd))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000232 fatal("%s: struct passwd size mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000233 pw->pw_name = buffer_get_string(&m, NULL);
234 pw->pw_passwd = buffer_get_string(&m, NULL);
235 pw->pw_gecos = buffer_get_string(&m, NULL);
Kevin Steves7e147602002-03-22 18:07:17 +0000236#ifdef HAVE_PW_CLASS_IN_PASSWD
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000237 pw->pw_class = buffer_get_string(&m, NULL);
Kevin Steves7e147602002-03-22 18:07:17 +0000238#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000239 pw->pw_dir = buffer_get_string(&m, NULL);
240 pw->pw_shell = buffer_get_string(&m, NULL);
Darren Tucker1629c072007-02-19 22:25:37 +1100241
Darren Tucker2f8b3d92007-12-02 23:02:15 +1100242out:
Darren Tucker1629c072007-02-19 22:25:37 +1100243 /* copy options block as a Match directive may have changed some */
244 newopts = buffer_get_string(&m, &len);
245 if (len != sizeof(*newopts))
246 fatal("%s: option block size mismatch", __func__);
247 if (newopts->banner != NULL)
248 newopts->banner = buffer_get_string(&m, NULL);
249 copy_set_server_options(&options, newopts, 1);
250 xfree(newopts);
251
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000252 buffer_free(&m);
253
254 return (pw);
255}
256
Darren Tucker7eb3de02003-10-15 15:56:58 +1000257char *
258mm_auth2_read_banner(void)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000259{
260 Buffer m;
261 char *banner;
262
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000263 debug3("%s entering", __func__);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000264
265 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000266 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000267 buffer_clear(&m);
268
Darren Tucker7eb3de02003-10-15 15:56:58 +1000269 mm_request_receive_expect(pmonitor->m_recvfd,
270 MONITOR_ANS_AUTH2_READ_BANNER, &m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000271 banner = buffer_get_string(&m, NULL);
272 buffer_free(&m);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000273
Darren Tucker7eb3de02003-10-15 15:56:58 +1000274 /* treat empty banner as missing banner */
275 if (strlen(banner) == 0) {
276 xfree(banner);
277 banner = NULL;
278 }
Damien Miller5ad9fd92002-05-13 11:07:41 +1000279 return (banner);
280}
281
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000282/* Inform the privileged process about service and style */
283
284void
285mm_inform_authserv(char *service, char *style)
286{
287 Buffer m;
288
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000289 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000290
291 buffer_init(&m);
292 buffer_put_cstring(&m, service);
293 buffer_put_cstring(&m, style ? style : "");
294
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000295 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000296
297 buffer_free(&m);
298}
299
300/* Do the password authentication */
301int
302mm_auth_password(Authctxt *authctxt, char *password)
303{
304 Buffer m;
305 int authenticated = 0;
306
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000307 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000308
309 buffer_init(&m);
310 buffer_put_cstring(&m, password);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000311 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000312
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000313 debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000314 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000315
316 authenticated = buffer_get_int(&m);
317
318 buffer_free(&m);
319
320 debug3("%s: user %sauthenticated",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000321 __func__, authenticated ? "" : "not ");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000322 return (authenticated);
323}
324
325int
326mm_user_key_allowed(struct passwd *pw, Key *key)
327{
328 return (mm_key_allowed(MM_USERKEY, NULL, NULL, key));
329}
330
331int
332mm_hostbased_key_allowed(struct passwd *pw, char *user, char *host,
333 Key *key)
334{
335 return (mm_key_allowed(MM_HOSTKEY, user, host, key));
336}
337
338int
339mm_auth_rhosts_rsa_key_allowed(struct passwd *pw, char *user,
340 char *host, Key *key)
341{
342 int ret;
343
344 key->type = KEY_RSA; /* XXX hack for key_to_blob */
345 ret = mm_key_allowed(MM_RSAHOSTKEY, user, host, key);
346 key->type = KEY_RSA1;
347 return (ret);
348}
349
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000350int
351mm_key_allowed(enum mm_keytype type, char *user, char *host, Key *key)
352{
353 Buffer m;
354 u_char *blob;
355 u_int len;
Damien Miller06ebedf2003-02-24 12:03:38 +1100356 int allowed = 0, have_forced = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000357
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000358 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000359
360 /* Convert the key to a blob and the pass it over */
361 if (!key_to_blob(key, &blob, &len))
362 return (0);
363
364 buffer_init(&m);
365 buffer_put_int(&m, type);
366 buffer_put_cstring(&m, user ? user : "");
367 buffer_put_cstring(&m, host ? host : "");
368 buffer_put_string(&m, blob, len);
369 xfree(blob);
370
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000371 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000372
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000373 debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000374 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000375
376 allowed = buffer_get_int(&m);
377
Damien Miller06ebedf2003-02-24 12:03:38 +1100378 /* fake forced command */
379 auth_clear_options();
380 have_forced = buffer_get_int(&m);
381 forced_command = have_forced ? xstrdup("true") : NULL;
382
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000383 buffer_free(&m);
384
385 return (allowed);
386}
387
388/*
389 * This key verify needs to send the key type along, because the
390 * privileged parent makes the decision if the key is allowed
391 * for authentication.
392 */
393
394int
395mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
396{
397 Buffer m;
398 u_char *blob;
399 u_int len;
400 int verified = 0;
401
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000402 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000403
404 /* Convert the key to a blob and the pass it over */
405 if (!key_to_blob(key, &blob, &len))
406 return (0);
407
408 buffer_init(&m);
409 buffer_put_string(&m, blob, len);
410 buffer_put_string(&m, sig, siglen);
411 buffer_put_string(&m, data, datalen);
412 xfree(blob);
413
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000414 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000415
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000416 debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000417 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000418
419 verified = buffer_get_int(&m);
420
421 buffer_free(&m);
422
423 return (verified);
424}
425
426/* Export key state after authentication */
427Newkeys *
428mm_newkeys_from_blob(u_char *blob, int blen)
429{
430 Buffer b;
431 u_int len;
432 Newkeys *newkey = NULL;
433 Enc *enc;
434 Mac *mac;
435 Comp *comp;
436
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000437 debug3("%s: %p(%d)", __func__, blob, blen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000438#ifdef DEBUG_PK
439 dump_base64(stderr, blob, blen);
440#endif
441 buffer_init(&b);
442 buffer_append(&b, blob, blen);
443
444 newkey = xmalloc(sizeof(*newkey));
445 enc = &newkey->enc;
446 mac = &newkey->mac;
447 comp = &newkey->comp;
448
449 /* Enc structure */
450 enc->name = buffer_get_string(&b, NULL);
451 buffer_get(&b, &enc->cipher, sizeof(enc->cipher));
452 enc->enabled = buffer_get_int(&b);
453 enc->block_size = buffer_get_int(&b);
454 enc->key = buffer_get_string(&b, &enc->key_len);
455 enc->iv = buffer_get_string(&b, &len);
456 if (len != enc->block_size)
Ben Lindstrom08512492002-06-27 00:23:02 +0000457 fatal("%s: bad ivlen: expected %u != %u", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000458 enc->block_size, len);
459
460 if (enc->name == NULL || cipher_by_name(enc->name) != enc->cipher)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000461 fatal("%s: bad cipher name %s or pointer %p", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000462 enc->name, enc->cipher);
463
464 /* Mac structure */
465 mac->name = buffer_get_string(&b, NULL);
Darren Tucker5f3d5be2007-06-05 18:30:18 +1000466 if (mac->name == NULL || mac_setup(mac, mac->name) == -1)
Damien Millere45796f2007-06-11 14:01:42 +1000467 fatal("%s: can not setup mac %s", __func__, mac->name);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000468 mac->enabled = buffer_get_int(&b);
469 mac->key = buffer_get_string(&b, &len);
470 if (len > mac->key_len)
Ben Lindstrom08512492002-06-27 00:23:02 +0000471 fatal("%s: bad mac key length: %u > %d", __func__, len,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000472 mac->key_len);
473 mac->key_len = len;
474
475 /* Comp structure */
476 comp->type = buffer_get_int(&b);
477 comp->enabled = buffer_get_int(&b);
478 comp->name = buffer_get_string(&b, NULL);
479
480 len = buffer_len(&b);
481 if (len != 0)
Ben Lindstrom08512492002-06-27 00:23:02 +0000482 error("newkeys_from_blob: remaining bytes in blob %u", len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000483 buffer_free(&b);
484 return (newkey);
485}
486
487int
488mm_newkeys_to_blob(int mode, u_char **blobp, u_int *lenp)
489{
490 Buffer b;
491 int len;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000492 Enc *enc;
493 Mac *mac;
494 Comp *comp;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000495 Newkeys *newkey = (Newkeys *)packet_get_newkeys(mode);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000496
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000497 debug3("%s: converting %p", __func__, newkey);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000498
499 if (newkey == NULL) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000500 error("%s: newkey == NULL", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000501 return 0;
502 }
503 enc = &newkey->enc;
504 mac = &newkey->mac;
505 comp = &newkey->comp;
506
507 buffer_init(&b);
508 /* Enc structure */
509 buffer_put_cstring(&b, enc->name);
510 /* The cipher struct is constant and shared, you export pointer */
511 buffer_append(&b, &enc->cipher, sizeof(enc->cipher));
512 buffer_put_int(&b, enc->enabled);
513 buffer_put_int(&b, enc->block_size);
514 buffer_put_string(&b, enc->key, enc->key_len);
515 packet_get_keyiv(mode, enc->iv, enc->block_size);
516 buffer_put_string(&b, enc->iv, enc->block_size);
517
518 /* Mac structure */
519 buffer_put_cstring(&b, mac->name);
520 buffer_put_int(&b, mac->enabled);
521 buffer_put_string(&b, mac->key, mac->key_len);
522
523 /* Comp structure */
524 buffer_put_int(&b, comp->type);
525 buffer_put_int(&b, comp->enabled);
526 buffer_put_cstring(&b, comp->name);
527
528 len = buffer_len(&b);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000529 if (lenp != NULL)
530 *lenp = len;
Ben Lindstrom2bf759c2002-07-07 22:13:31 +0000531 if (blobp != NULL) {
532 *blobp = xmalloc(len);
533 memcpy(*blobp, buffer_ptr(&b), len);
534 }
535 memset(buffer_ptr(&b), 0, len);
536 buffer_free(&b);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000537 return len;
538}
539
540static void
541mm_send_kex(Buffer *m, Kex *kex)
542{
543 buffer_put_string(m, kex->session_id, kex->session_id_len);
544 buffer_put_int(m, kex->we_need);
545 buffer_put_int(m, kex->hostkey_type);
546 buffer_put_int(m, kex->kex_type);
547 buffer_put_string(m, buffer_ptr(&kex->my), buffer_len(&kex->my));
548 buffer_put_string(m, buffer_ptr(&kex->peer), buffer_len(&kex->peer));
549 buffer_put_int(m, kex->flags);
550 buffer_put_cstring(m, kex->client_version_string);
551 buffer_put_cstring(m, kex->server_version_string);
552}
553
554void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000555mm_send_keystate(struct monitor *monitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000556{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000557 Buffer m, *input, *output;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000558 u_char *blob, *p;
559 u_int bloblen, plen;
Damien Millera5539d22003-04-09 20:50:06 +1000560 u_int32_t seqnr, packets;
Damien Millerb61f3fc2008-07-11 17:36:48 +1000561 u_int64_t blocks, bytes;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000562
563 buffer_init(&m);
564
565 if (!compat20) {
566 u_char iv[24];
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000567 u_char *key;
568 u_int ivlen, keylen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000569
570 buffer_put_int(&m, packet_get_protocol_flags());
571
572 buffer_put_int(&m, packet_get_ssh1_cipher());
573
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000574 debug3("%s: Sending ssh1 KEY+IV", __func__);
575 keylen = packet_get_encryption_key(NULL);
576 key = xmalloc(keylen+1); /* add 1 if keylen == 0 */
577 keylen = packet_get_encryption_key(key);
578 buffer_put_string(&m, key, keylen);
579 memset(key, 0, keylen);
580 xfree(key);
581
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000582 ivlen = packet_get_keyiv_len(MODE_OUT);
583 packet_get_keyiv(MODE_OUT, iv, ivlen);
584 buffer_put_string(&m, iv, ivlen);
585 ivlen = packet_get_keyiv_len(MODE_OUT);
586 packet_get_keyiv(MODE_IN, iv, ivlen);
587 buffer_put_string(&m, iv, ivlen);
588 goto skip;
589 } else {
590 /* Kex for rekeying */
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000591 mm_send_kex(&m, *monitor->m_pkex);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000592 }
593
594 debug3("%s: Sending new keys: %p %p",
Darren Tuckerf7288d72009-06-21 18:12:20 +1000595 __func__, packet_get_newkeys(MODE_OUT),
596 packet_get_newkeys(MODE_IN));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000597
598 /* Keys from Kex */
599 if (!mm_newkeys_to_blob(MODE_OUT, &blob, &bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000600 fatal("%s: conversion of newkeys failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000601
602 buffer_put_string(&m, blob, bloblen);
603 xfree(blob);
604
605 if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000606 fatal("%s: conversion of newkeys failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000607
608 buffer_put_string(&m, blob, bloblen);
609 xfree(blob);
610
Damien Millerb61f3fc2008-07-11 17:36:48 +1000611 packet_get_state(MODE_OUT, &seqnr, &blocks, &packets, &bytes);
Damien Millera5539d22003-04-09 20:50:06 +1000612 buffer_put_int(&m, seqnr);
613 buffer_put_int64(&m, blocks);
614 buffer_put_int(&m, packets);
Damien Millerb61f3fc2008-07-11 17:36:48 +1000615 buffer_put_int64(&m, bytes);
616 packet_get_state(MODE_IN, &seqnr, &blocks, &packets, &bytes);
Damien Millera5539d22003-04-09 20:50:06 +1000617 buffer_put_int(&m, seqnr);
618 buffer_put_int64(&m, blocks);
619 buffer_put_int(&m, packets);
Damien Millerb61f3fc2008-07-11 17:36:48 +1000620 buffer_put_int64(&m, bytes);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000621
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000622 debug3("%s: New keys have been sent", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000623 skip:
624 /* More key context */
625 plen = packet_get_keycontext(MODE_OUT, NULL);
626 p = xmalloc(plen+1);
627 packet_get_keycontext(MODE_OUT, p);
628 buffer_put_string(&m, p, plen);
629 xfree(p);
630
631 plen = packet_get_keycontext(MODE_IN, NULL);
632 p = xmalloc(plen+1);
633 packet_get_keycontext(MODE_IN, p);
634 buffer_put_string(&m, p, plen);
635 xfree(p);
636
637 /* Compression state */
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000638 debug3("%s: Sending compression state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000639 buffer_put_string(&m, &outgoing_stream, sizeof(outgoing_stream));
640 buffer_put_string(&m, &incoming_stream, sizeof(incoming_stream));
641
642 /* Network I/O buffers */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000643 input = (Buffer *)packet_get_input();
644 output = (Buffer *)packet_get_output();
645 buffer_put_string(&m, buffer_ptr(input), buffer_len(input));
646 buffer_put_string(&m, buffer_ptr(output), buffer_len(output));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000647
Darren Tuckerc5564e12009-06-21 18:53:53 +1000648 /* Roaming */
649 if (compat20) {
650 buffer_put_int64(&m, get_sent_bytes());
651 buffer_put_int64(&m, get_recv_bytes());
652 }
653
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000654 mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m);
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000655 debug3("%s: Finished sending state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000656
657 buffer_free(&m);
658}
659
660int
Damien Miller71a73672006-03-26 14:04:36 +1100661mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000662{
663 Buffer m;
Darren Tucker09991742004-07-17 17:05:14 +1000664 char *p, *msg;
Damien Miller7207f642008-05-19 15:34:50 +1000665 int success = 0, tmp1 = -1, tmp2 = -1;
666
667 /* Kludge: ensure there are fds free to receive the pty/tty */
668 if ((tmp1 = dup(pmonitor->m_recvfd)) == -1 ||
669 (tmp2 = dup(pmonitor->m_recvfd)) == -1) {
670 error("%s: cannot allocate fds for pty", __func__);
671 if (tmp1 > 0)
672 close(tmp1);
673 if (tmp2 > 0)
674 close(tmp2);
675 return 0;
676 }
677 close(tmp1);
678 close(tmp2);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000679
680 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000681 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000682
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000683 debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000684 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000685
686 success = buffer_get_int(&m);
687 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000688 debug3("%s: pty alloc failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000689 buffer_free(&m);
690 return (0);
691 }
692 p = buffer_get_string(&m, NULL);
Darren Tucker09991742004-07-17 17:05:14 +1000693 msg = buffer_get_string(&m, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000694 buffer_free(&m);
695
696 strlcpy(namebuf, p, namebuflen); /* Possible truncation */
697 xfree(p);
698
Darren Tucker09991742004-07-17 17:05:14 +1000699 buffer_append(&loginmsg, msg, strlen(msg));
700 xfree(msg);
701
Damien Miller54fd7cf2007-09-17 12:04:08 +1000702 if ((*ptyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1 ||
703 (*ttyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1)
704 fatal("%s: receive fds failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000705
706 /* Success */
707 return (1);
708}
709
710void
Darren Tucker3e33cec2003-10-02 16:12:36 +1000711mm_session_pty_cleanup2(Session *s)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000712{
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000713 Buffer m;
714
715 if (s->ttyfd == -1)
716 return;
717 buffer_init(&m);
718 buffer_put_cstring(&m, s->tty);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000719 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000720 buffer_free(&m);
721
722 /* closed dup'ed master */
Damien Miller7207f642008-05-19 15:34:50 +1000723 if (s->ptymaster != -1 && close(s->ptymaster) < 0)
724 error("close(s->ptymaster/%d): %s",
725 s->ptymaster, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000726
727 /* unlink pty from session */
728 s->ttyfd = -1;
729}
730
Damien Miller79418552002-04-23 20:28:48 +1000731#ifdef USE_PAM
732void
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100733mm_start_pam(Authctxt *authctxt)
Damien Miller79418552002-04-23 20:28:48 +1000734{
735 Buffer m;
736
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000737 debug3("%s entering", __func__);
Damien Miller4e448a32003-05-14 15:11:48 +1000738 if (!options.use_pam)
739 fatal("UsePAM=no, but ended up in %s anyway", __func__);
Damien Miller79418552002-04-23 20:28:48 +1000740
741 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000742 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, &m);
Damien Miller79418552002-04-23 20:28:48 +1000743
744 buffer_free(&m);
745}
Damien Miller4f9f42a2003-05-10 19:28:02 +1000746
Damien Miller1f499fd2003-08-25 13:08:49 +1000747u_int
748mm_do_pam_account(void)
749{
750 Buffer m;
751 u_int ret;
Darren Tucker77fc29e2004-09-11 23:07:03 +1000752 char *msg;
Damien Miller1f499fd2003-08-25 13:08:49 +1000753
754 debug3("%s entering", __func__);
755 if (!options.use_pam)
756 fatal("UsePAM=no, but ended up in %s anyway", __func__);
757
758 buffer_init(&m);
759 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_ACCOUNT, &m);
760
Damien Millera8e06ce2003-11-21 23:48:55 +1100761 mm_request_receive_expect(pmonitor->m_recvfd,
Damien Miller1f499fd2003-08-25 13:08:49 +1000762 MONITOR_ANS_PAM_ACCOUNT, &m);
763 ret = buffer_get_int(&m);
Darren Tucker77fc29e2004-09-11 23:07:03 +1000764 msg = buffer_get_string(&m, NULL);
765 buffer_append(&loginmsg, msg, strlen(msg));
766 xfree(msg);
Damien Miller1f499fd2003-08-25 13:08:49 +1000767
768 buffer_free(&m);
Damien Miller787b2ec2003-11-21 23:56:47 +1100769
Damien Miller1f499fd2003-08-25 13:08:49 +1000770 debug3("%s returning %d", __func__, ret);
771
772 return (ret);
773}
774
Damien Miller4f9f42a2003-05-10 19:28:02 +1000775void *
776mm_sshpam_init_ctx(Authctxt *authctxt)
777{
778 Buffer m;
779 int success;
780
781 debug3("%s", __func__);
782 buffer_init(&m);
783 buffer_put_cstring(&m, authctxt->user);
784 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_INIT_CTX, &m);
785 debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__);
786 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_INIT_CTX, &m);
787 success = buffer_get_int(&m);
788 if (success == 0) {
789 debug3("%s: pam_init_ctx failed", __func__);
790 buffer_free(&m);
791 return (NULL);
792 }
793 buffer_free(&m);
794 return (authctxt);
795}
796
797int
798mm_sshpam_query(void *ctx, char **name, char **info,
799 u_int *num, char ***prompts, u_int **echo_on)
800{
801 Buffer m;
Damien Miller04b65332005-07-17 17:53:31 +1000802 u_int i;
803 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000804
805 debug3("%s", __func__);
806 buffer_init(&m);
807 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_QUERY, &m);
808 debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__);
809 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_QUERY, &m);
810 ret = buffer_get_int(&m);
811 debug3("%s: pam_query returned %d", __func__, ret);
812 *name = buffer_get_string(&m, NULL);
813 *info = buffer_get_string(&m, NULL);
814 *num = buffer_get_int(&m);
Darren Tuckerd8093e42006-05-04 16:24:34 +1000815 if (*num > PAM_MAX_NUM_MSG)
816 fatal("%s: recieved %u PAM messages, expected <= %u",
817 __func__, *num, PAM_MAX_NUM_MSG);
818 *prompts = xcalloc((*num + 1), sizeof(char *));
819 *echo_on = xcalloc((*num + 1), sizeof(u_int));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000820 for (i = 0; i < *num; ++i) {
821 (*prompts)[i] = buffer_get_string(&m, NULL);
822 (*echo_on)[i] = buffer_get_int(&m);
823 }
824 buffer_free(&m);
825 return (ret);
826}
827
828int
829mm_sshpam_respond(void *ctx, u_int num, char **resp)
830{
831 Buffer m;
Damien Miller04b65332005-07-17 17:53:31 +1000832 u_int i;
833 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000834
835 debug3("%s", __func__);
836 buffer_init(&m);
837 buffer_put_int(&m, num);
838 for (i = 0; i < num; ++i)
839 buffer_put_cstring(&m, resp[i]);
840 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_RESPOND, &m);
841 debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__);
842 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_RESPOND, &m);
843 ret = buffer_get_int(&m);
844 debug3("%s: pam_respond returned %d", __func__, ret);
845 buffer_free(&m);
846 return (ret);
847}
848
849void
850mm_sshpam_free_ctx(void *ctxtp)
851{
852 Buffer m;
853
854 debug3("%s", __func__);
855 buffer_init(&m);
856 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_FREE_CTX, &m);
857 debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__);
858 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_FREE_CTX, &m);
859 buffer_free(&m);
860}
Damien Miller79418552002-04-23 20:28:48 +1000861#endif /* USE_PAM */
862
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000863/* Request process termination */
864
865void
866mm_terminate(void)
867{
868 Buffer m;
869
870 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000871 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000872 buffer_free(&m);
873}
874
875int
876mm_ssh1_session_key(BIGNUM *num)
877{
878 int rsafail;
879 Buffer m;
880
881 buffer_init(&m);
882 buffer_put_bignum2(&m, num);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000883 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000884
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000885 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000886
887 rsafail = buffer_get_int(&m);
888 buffer_get_bignum2(&m, num);
889
890 buffer_free(&m);
891
892 return (rsafail);
893}
894
895static void
896mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
897 char ***prompts, u_int **echo_on)
898{
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000899 *name = xstrdup("");
900 *infotxt = xstrdup("");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000901 *numprompts = 1;
Damien Miller07d86be2006-03-26 14:19:21 +1100902 *prompts = xcalloc(*numprompts, sizeof(char *));
903 *echo_on = xcalloc(*numprompts, sizeof(u_int));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000904 (*echo_on)[0] = 0;
905}
906
907int
908mm_bsdauth_query(void *ctx, char **name, char **infotxt,
909 u_int *numprompts, char ***prompts, u_int **echo_on)
910{
911 Buffer m;
Damien Millerb7df3af2003-02-24 11:55:46 +1100912 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000913 char *challenge;
914
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000915 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000916
917 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000918 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000919
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000920 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000921 &m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100922 success = buffer_get_int(&m);
923 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000924 debug3("%s: no challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000925 buffer_free(&m);
926 return (-1);
927 }
928
929 /* Get the challenge, and format the response */
930 challenge = buffer_get_string(&m, NULL);
931 buffer_free(&m);
932
933 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
934 (*prompts)[0] = challenge;
935
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000936 debug3("%s: received challenge: %s", __func__, challenge);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000937
938 return (0);
939}
940
941int
942mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
943{
944 Buffer m;
945 int authok;
946
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000947 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000948 if (numresponses != 1)
949 return (-1);
950
951 buffer_init(&m);
952 buffer_put_cstring(&m, responses[0]);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000953 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000954
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000955 mm_request_receive_expect(pmonitor->m_recvfd,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000956 MONITOR_ANS_BSDAUTHRESPOND, &m);
957
958 authok = buffer_get_int(&m);
959 buffer_free(&m);
960
961 return ((authok == 0) ? -1 : 0);
962}
963
Darren Tucker042e2e82004-07-08 23:09:42 +1000964#ifdef SKEY
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000965int
966mm_skey_query(void *ctx, char **name, char **infotxt,
967 u_int *numprompts, char ***prompts, u_int **echo_on)
968{
969 Buffer m;
Damien Millerb7df3af2003-02-24 11:55:46 +1100970 u_int success;
Darren Tuckerd6a23f22006-08-05 18:50:35 +1000971 char *challenge;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000972
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000973 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000974
975 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000976 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000977
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000978 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000979 &m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100980 success = buffer_get_int(&m);
981 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000982 debug3("%s: no challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000983 buffer_free(&m);
984 return (-1);
985 }
986
987 /* Get the challenge, and format the response */
988 challenge = buffer_get_string(&m, NULL);
989 buffer_free(&m);
990
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000991 debug3("%s: received challenge: %s", __func__, challenge);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000992
993 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
994
Damien Miller07d86be2006-03-26 14:19:21 +1100995 xasprintf(*prompts, "%s%s", challenge, SKEY_PROMPT);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000996 xfree(challenge);
997
998 return (0);
999}
1000
1001int
1002mm_skey_respond(void *ctx, u_int numresponses, char **responses)
1003{
1004 Buffer m;
1005 int authok;
1006
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001007 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001008 if (numresponses != 1)
1009 return (-1);
1010
1011 buffer_init(&m);
1012 buffer_put_cstring(&m, responses[0]);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001013 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001014
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001015 mm_request_receive_expect(pmonitor->m_recvfd,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001016 MONITOR_ANS_SKEYRESPOND, &m);
1017
1018 authok = buffer_get_int(&m);
1019 buffer_free(&m);
1020
1021 return ((authok == 0) ? -1 : 0);
1022}
Darren Tucker042e2e82004-07-08 23:09:42 +10001023#endif /* SKEY */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001024
1025void
1026mm_ssh1_session_id(u_char session_id[16])
1027{
1028 Buffer m;
1029 int i;
1030
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001031 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001032
1033 buffer_init(&m);
1034 for (i = 0; i < 16; i++)
1035 buffer_put_char(&m, session_id[i]);
1036
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001037 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001038 buffer_free(&m);
1039}
1040
1041int
1042mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
1043{
1044 Buffer m;
1045 Key *key;
1046 u_char *blob;
1047 u_int blen;
Damien Miller06ebedf2003-02-24 12:03:38 +11001048 int allowed = 0, have_forced = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001049
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001050 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001051
1052 buffer_init(&m);
1053 buffer_put_bignum2(&m, client_n);
1054
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001055 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m);
1056 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001057
1058 allowed = buffer_get_int(&m);
1059
Damien Miller06ebedf2003-02-24 12:03:38 +11001060 /* fake forced command */
1061 auth_clear_options();
1062 have_forced = buffer_get_int(&m);
1063 forced_command = have_forced ? xstrdup("true") : NULL;
1064
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001065 if (allowed && rkey != NULL) {
1066 blob = buffer_get_string(&m, &blen);
1067 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001068 fatal("%s: key_from_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001069 *rkey = key;
1070 xfree(blob);
1071 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001072 buffer_free(&m);
1073
1074 return (allowed);
1075}
1076
1077BIGNUM *
1078mm_auth_rsa_generate_challenge(Key *key)
1079{
1080 Buffer m;
1081 BIGNUM *challenge;
1082 u_char *blob;
1083 u_int blen;
1084
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001085 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001086
1087 if ((challenge = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001088 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001089
1090 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1091 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001092 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001093 key->type = KEY_RSA1;
1094
1095 buffer_init(&m);
1096 buffer_put_string(&m, blob, blen);
1097 xfree(blob);
1098
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001099 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m);
1100 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001101
1102 buffer_get_bignum2(&m, challenge);
1103 buffer_free(&m);
1104
1105 return (challenge);
1106}
1107
1108int
1109mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16])
1110{
1111 Buffer m;
1112 u_char *blob;
1113 u_int blen;
1114 int success = 0;
1115
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001116 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001117
1118 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1119 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001120 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001121 key->type = KEY_RSA1;
1122
1123 buffer_init(&m);
1124 buffer_put_string(&m, blob, blen);
1125 buffer_put_string(&m, response, 16);
1126 xfree(blob);
1127
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001128 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m);
1129 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001130
1131 success = buffer_get_int(&m);
1132 buffer_free(&m);
1133
1134 return (success);
1135}
Damien Miller25162f22002-09-12 09:47:29 +10001136
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001137#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001138void
1139mm_audit_event(ssh_audit_event_t event)
1140{
1141 Buffer m;
1142
1143 debug3("%s entering", __func__);
1144
1145 buffer_init(&m);
1146 buffer_put_int(&m, event);
1147
1148 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_EVENT, &m);
1149 buffer_free(&m);
1150}
1151
1152void
1153mm_audit_run_command(const char *command)
1154{
1155 Buffer m;
1156
1157 debug3("%s entering command %s", __func__, command);
1158
1159 buffer_init(&m);
1160 buffer_put_cstring(&m, command);
1161
1162 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, &m);
1163 buffer_free(&m);
1164}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001165#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001166
Darren Tucker0efd1552003-08-26 11:49:55 +10001167#ifdef GSSAPI
1168OM_uint32
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001169mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
Darren Tucker0efd1552003-08-26 11:49:55 +10001170{
1171 Buffer m;
1172 OM_uint32 major;
1173
1174 /* Client doesn't get to see the context */
1175 *ctx = NULL;
1176
1177 buffer_init(&m);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001178 buffer_put_string(&m, goid->elements, goid->length);
Darren Tucker0efd1552003-08-26 11:49:55 +10001179
1180 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m);
1181 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m);
1182
1183 major = buffer_get_int(&m);
1184
1185 buffer_free(&m);
1186 return (major);
1187}
1188
1189OM_uint32
1190mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
1191 gss_buffer_desc *out, OM_uint32 *flags)
1192{
1193 Buffer m;
1194 OM_uint32 major;
Darren Tucker600ad8d2003-08-26 12:10:48 +10001195 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001196
1197 buffer_init(&m);
1198 buffer_put_string(&m, in->value, in->length);
1199
1200 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m);
1201 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m);
1202
1203 major = buffer_get_int(&m);
Darren Tucker600ad8d2003-08-26 12:10:48 +10001204 out->value = buffer_get_string(&m, &len);
1205 out->length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001206 if (flags)
1207 *flags = buffer_get_int(&m);
1208
1209 buffer_free(&m);
1210
1211 return (major);
1212}
1213
Damien Miller0425d402003-11-17 22:18:21 +11001214OM_uint32
1215mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
1216{
1217 Buffer m;
1218 OM_uint32 major;
1219
1220 buffer_init(&m);
1221 buffer_put_string(&m, gssbuf->value, gssbuf->length);
1222 buffer_put_string(&m, gssmic->value, gssmic->length);
1223
1224 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, &m);
1225 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSCHECKMIC,
1226 &m);
1227
1228 major = buffer_get_int(&m);
1229 buffer_free(&m);
1230 return(major);
1231}
1232
Darren Tucker0efd1552003-08-26 11:49:55 +10001233int
1234mm_ssh_gssapi_userok(char *user)
1235{
1236 Buffer m;
1237 int authenticated = 0;
1238
1239 buffer_init(&m);
1240
1241 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m);
1242 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK,
1243 &m);
1244
1245 authenticated = buffer_get_int(&m);
1246
1247 buffer_free(&m);
1248 debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
1249 return (authenticated);
1250}
1251#endif /* GSSAPI */
Damien Miller01ed2272008-11-05 16:20:46 +11001252
1253#ifdef JPAKE
1254void
1255mm_auth2_jpake_get_pwdata(Authctxt *authctxt, BIGNUM **s,
1256 char **hash_scheme, char **salt)
1257{
1258 Buffer m;
1259
1260 debug3("%s entering", __func__);
1261
1262 buffer_init(&m);
1263 mm_request_send(pmonitor->m_recvfd,
1264 MONITOR_REQ_JPAKE_GET_PWDATA, &m);
1265
1266 debug3("%s: waiting for MONITOR_ANS_JPAKE_GET_PWDATA", __func__);
1267 mm_request_receive_expect(pmonitor->m_recvfd,
1268 MONITOR_ANS_JPAKE_GET_PWDATA, &m);
1269
1270 *hash_scheme = buffer_get_string(&m, NULL);
1271 *salt = buffer_get_string(&m, NULL);
1272
1273 buffer_free(&m);
1274}
1275
1276void
Damien Millercee85232009-03-06 00:58:22 +11001277mm_jpake_step1(struct modp_group *grp,
Damien Miller01ed2272008-11-05 16:20:46 +11001278 u_char **id, u_int *id_len,
1279 BIGNUM **priv1, BIGNUM **priv2, BIGNUM **g_priv1, BIGNUM **g_priv2,
1280 u_char **priv1_proof, u_int *priv1_proof_len,
1281 u_char **priv2_proof, u_int *priv2_proof_len)
1282{
1283 Buffer m;
1284
1285 debug3("%s entering", __func__);
1286
1287 buffer_init(&m);
1288 mm_request_send(pmonitor->m_recvfd,
1289 MONITOR_REQ_JPAKE_STEP1, &m);
1290
1291 debug3("%s: waiting for MONITOR_ANS_JPAKE_STEP1", __func__);
1292 mm_request_receive_expect(pmonitor->m_recvfd,
1293 MONITOR_ANS_JPAKE_STEP1, &m);
1294
1295 if ((*priv1 = BN_new()) == NULL ||
1296 (*priv2 = BN_new()) == NULL ||
1297 (*g_priv1 = BN_new()) == NULL ||
1298 (*g_priv2 = BN_new()) == NULL)
1299 fatal("%s: BN_new", __func__);
1300
1301 *id = buffer_get_string(&m, id_len);
1302 /* priv1 and priv2 are, well, private */
1303 buffer_get_bignum2(&m, *g_priv1);
1304 buffer_get_bignum2(&m, *g_priv2);
1305 *priv1_proof = buffer_get_string(&m, priv1_proof_len);
1306 *priv2_proof = buffer_get_string(&m, priv2_proof_len);
1307
1308 buffer_free(&m);
1309}
1310
1311void
Damien Millercee85232009-03-06 00:58:22 +11001312mm_jpake_step2(struct modp_group *grp, BIGNUM *s,
Damien Miller01ed2272008-11-05 16:20:46 +11001313 BIGNUM *mypub1, BIGNUM *theirpub1, BIGNUM *theirpub2, BIGNUM *mypriv2,
1314 const u_char *theirid, u_int theirid_len,
1315 const u_char *myid, u_int myid_len,
1316 const u_char *theirpub1_proof, u_int theirpub1_proof_len,
1317 const u_char *theirpub2_proof, u_int theirpub2_proof_len,
1318 BIGNUM **newpub,
1319 u_char **newpub_exponent_proof, u_int *newpub_exponent_proof_len)
1320{
1321 Buffer m;
1322
1323 debug3("%s entering", __func__);
1324
1325 buffer_init(&m);
1326 /* monitor already has all bignums except theirpub1, theirpub2 */
1327 buffer_put_bignum2(&m, theirpub1);
1328 buffer_put_bignum2(&m, theirpub2);
1329 /* monitor already knows our id */
1330 buffer_put_string(&m, theirid, theirid_len);
1331 buffer_put_string(&m, theirpub1_proof, theirpub1_proof_len);
1332 buffer_put_string(&m, theirpub2_proof, theirpub2_proof_len);
1333
1334 mm_request_send(pmonitor->m_recvfd,
1335 MONITOR_REQ_JPAKE_STEP2, &m);
1336
1337 debug3("%s: waiting for MONITOR_ANS_JPAKE_STEP2", __func__);
1338 mm_request_receive_expect(pmonitor->m_recvfd,
1339 MONITOR_ANS_JPAKE_STEP2, &m);
1340
1341 if ((*newpub = BN_new()) == NULL)
1342 fatal("%s: BN_new", __func__);
1343
1344 buffer_get_bignum2(&m, *newpub);
1345 *newpub_exponent_proof = buffer_get_string(&m,
1346 newpub_exponent_proof_len);
1347
1348 buffer_free(&m);
1349}
1350
1351void
Damien Millercee85232009-03-06 00:58:22 +11001352mm_jpake_key_confirm(struct modp_group *grp, BIGNUM *s, BIGNUM *step2_val,
Damien Miller01ed2272008-11-05 16:20:46 +11001353 BIGNUM *mypriv2, BIGNUM *mypub1, BIGNUM *mypub2,
1354 BIGNUM *theirpub1, BIGNUM *theirpub2,
1355 const u_char *my_id, u_int my_id_len,
1356 const u_char *their_id, u_int their_id_len,
1357 const u_char *sess_id, u_int sess_id_len,
1358 const u_char *theirpriv2_s_proof, u_int theirpriv2_s_proof_len,
1359 BIGNUM **k,
1360 u_char **confirm_hash, u_int *confirm_hash_len)
1361{
1362 Buffer m;
1363
1364 debug3("%s entering", __func__);
1365
1366 buffer_init(&m);
1367 /* monitor already has all bignums except step2_val */
1368 buffer_put_bignum2(&m, step2_val);
1369 /* monitor already knows all the ids */
1370 buffer_put_string(&m, theirpriv2_s_proof, theirpriv2_s_proof_len);
1371
1372 mm_request_send(pmonitor->m_recvfd,
1373 MONITOR_REQ_JPAKE_KEY_CONFIRM, &m);
1374
1375 debug3("%s: waiting for MONITOR_ANS_JPAKE_KEY_CONFIRM", __func__);
1376 mm_request_receive_expect(pmonitor->m_recvfd,
1377 MONITOR_ANS_JPAKE_KEY_CONFIRM, &m);
1378
1379 /* 'k' is sensitive and stays in the monitor */
1380 *confirm_hash = buffer_get_string(&m, confirm_hash_len);
1381
1382 buffer_free(&m);
1383}
1384
1385int
1386mm_jpake_check_confirm(const BIGNUM *k,
1387 const u_char *peer_id, u_int peer_id_len,
1388 const u_char *sess_id, u_int sess_id_len,
1389 const u_char *peer_confirm_hash, u_int peer_confirm_hash_len)
1390{
1391 Buffer m;
1392 int success = 0;
1393
1394 debug3("%s entering", __func__);
1395
1396 buffer_init(&m);
1397 /* k is dummy in slave, ignored */
1398 /* monitor knows all the ids */
1399 buffer_put_string(&m, peer_confirm_hash, peer_confirm_hash_len);
1400 mm_request_send(pmonitor->m_recvfd,
1401 MONITOR_REQ_JPAKE_CHECK_CONFIRM, &m);
1402
1403 debug3("%s: waiting for MONITOR_ANS_JPAKE_CHECK_CONFIRM", __func__);
1404 mm_request_receive_expect(pmonitor->m_recvfd,
1405 MONITOR_ANS_JPAKE_CHECK_CONFIRM, &m);
1406
1407 success = buffer_get_int(&m);
1408 buffer_free(&m);
1409
1410 debug3("%s: success = %d", __func__, success);
1411 return success;
1412}
1413#endif /* JPAKE */