blob: 7a90b3ba34ea16a27be2b6cbc4520e76ad5c5440 [file] [log] [blame]
Damien Millerd8478b62011-05-29 21:39:36 +10001/* $OpenBSD: monitor_wrap.c,v 1.72 2011/05/23 03:30:07 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;
Damien Millerd8478b62011-05-29 21:39:36 +1000214 u_int len, i;
Darren Tucker1629c072007-02-19 22:25:37 +1100215 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__);
Damien Millerf2e407e2011-05-20 19:04:14 +1000248
249#define M_CP_STROPT(x) do { \
250 if (newopts->x != NULL) \
251 newopts->x = buffer_get_string(&m, NULL); \
252 } while (0)
Damien Millerd8478b62011-05-29 21:39:36 +1000253#define M_CP_STRARRAYOPT(x, nx) do { \
254 for (i = 0; i < newopts->nx; i++) \
255 newopts->x[i] = buffer_get_string(&m, NULL); \
256 } while (0)
Damien Millerf2e407e2011-05-20 19:04:14 +1000257 /* See comment in servconf.h */
258 COPY_MATCH_STRING_OPTS();
259#undef M_CP_STROPT
Damien Millerd8478b62011-05-29 21:39:36 +1000260#undef M_CP_STRARRAYOPT
Damien Millerf2e407e2011-05-20 19:04:14 +1000261
Darren Tucker1629c072007-02-19 22:25:37 +1100262 copy_set_server_options(&options, newopts, 1);
263 xfree(newopts);
264
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000265 buffer_free(&m);
266
267 return (pw);
268}
269
Darren Tucker7eb3de02003-10-15 15:56:58 +1000270char *
271mm_auth2_read_banner(void)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000272{
273 Buffer m;
274 char *banner;
275
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000276 debug3("%s entering", __func__);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000277
278 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000279 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000280 buffer_clear(&m);
281
Darren Tucker7eb3de02003-10-15 15:56:58 +1000282 mm_request_receive_expect(pmonitor->m_recvfd,
283 MONITOR_ANS_AUTH2_READ_BANNER, &m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000284 banner = buffer_get_string(&m, NULL);
285 buffer_free(&m);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000286
Darren Tucker7eb3de02003-10-15 15:56:58 +1000287 /* treat empty banner as missing banner */
288 if (strlen(banner) == 0) {
289 xfree(banner);
290 banner = NULL;
291 }
Damien Miller5ad9fd92002-05-13 11:07:41 +1000292 return (banner);
293}
294
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000295/* Inform the privileged process about service and style */
296
297void
298mm_inform_authserv(char *service, char *style)
299{
300 Buffer m;
301
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000302 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000303
304 buffer_init(&m);
305 buffer_put_cstring(&m, service);
306 buffer_put_cstring(&m, style ? style : "");
307
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000308 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000309
310 buffer_free(&m);
311}
312
313/* Do the password authentication */
314int
315mm_auth_password(Authctxt *authctxt, char *password)
316{
317 Buffer m;
318 int authenticated = 0;
319
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000320 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000321
322 buffer_init(&m);
323 buffer_put_cstring(&m, password);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000324 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000325
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000326 debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000327 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000328
329 authenticated = buffer_get_int(&m);
330
331 buffer_free(&m);
332
333 debug3("%s: user %sauthenticated",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000334 __func__, authenticated ? "" : "not ");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000335 return (authenticated);
336}
337
338int
339mm_user_key_allowed(struct passwd *pw, Key *key)
340{
341 return (mm_key_allowed(MM_USERKEY, NULL, NULL, key));
342}
343
344int
345mm_hostbased_key_allowed(struct passwd *pw, char *user, char *host,
346 Key *key)
347{
348 return (mm_key_allowed(MM_HOSTKEY, user, host, key));
349}
350
351int
352mm_auth_rhosts_rsa_key_allowed(struct passwd *pw, char *user,
353 char *host, Key *key)
354{
355 int ret;
356
357 key->type = KEY_RSA; /* XXX hack for key_to_blob */
358 ret = mm_key_allowed(MM_RSAHOSTKEY, user, host, key);
359 key->type = KEY_RSA1;
360 return (ret);
361}
362
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000363int
364mm_key_allowed(enum mm_keytype type, char *user, char *host, Key *key)
365{
366 Buffer m;
367 u_char *blob;
368 u_int len;
Damien Miller06ebedf2003-02-24 12:03:38 +1100369 int allowed = 0, have_forced = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000370
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000371 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000372
373 /* Convert the key to a blob and the pass it over */
374 if (!key_to_blob(key, &blob, &len))
375 return (0);
376
377 buffer_init(&m);
378 buffer_put_int(&m, type);
379 buffer_put_cstring(&m, user ? user : "");
380 buffer_put_cstring(&m, host ? host : "");
381 buffer_put_string(&m, blob, len);
382 xfree(blob);
383
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000384 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000385
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000386 debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000387 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000388
389 allowed = buffer_get_int(&m);
390
Damien Miller06ebedf2003-02-24 12:03:38 +1100391 /* fake forced command */
392 auth_clear_options();
393 have_forced = buffer_get_int(&m);
394 forced_command = have_forced ? xstrdup("true") : NULL;
395
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000396 buffer_free(&m);
397
398 return (allowed);
399}
400
401/*
402 * This key verify needs to send the key type along, because the
403 * privileged parent makes the decision if the key is allowed
404 * for authentication.
405 */
406
407int
408mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
409{
410 Buffer m;
411 u_char *blob;
412 u_int len;
413 int verified = 0;
414
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000415 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000416
417 /* Convert the key to a blob and the pass it over */
418 if (!key_to_blob(key, &blob, &len))
419 return (0);
420
421 buffer_init(&m);
422 buffer_put_string(&m, blob, len);
423 buffer_put_string(&m, sig, siglen);
424 buffer_put_string(&m, data, datalen);
425 xfree(blob);
426
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000427 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000428
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000429 debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000430 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000431
432 verified = buffer_get_int(&m);
433
434 buffer_free(&m);
435
436 return (verified);
437}
438
439/* Export key state after authentication */
440Newkeys *
441mm_newkeys_from_blob(u_char *blob, int blen)
442{
443 Buffer b;
444 u_int len;
445 Newkeys *newkey = NULL;
446 Enc *enc;
447 Mac *mac;
448 Comp *comp;
449
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000450 debug3("%s: %p(%d)", __func__, blob, blen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000451#ifdef DEBUG_PK
452 dump_base64(stderr, blob, blen);
453#endif
454 buffer_init(&b);
455 buffer_append(&b, blob, blen);
456
457 newkey = xmalloc(sizeof(*newkey));
458 enc = &newkey->enc;
459 mac = &newkey->mac;
460 comp = &newkey->comp;
461
462 /* Enc structure */
463 enc->name = buffer_get_string(&b, NULL);
464 buffer_get(&b, &enc->cipher, sizeof(enc->cipher));
465 enc->enabled = buffer_get_int(&b);
466 enc->block_size = buffer_get_int(&b);
467 enc->key = buffer_get_string(&b, &enc->key_len);
468 enc->iv = buffer_get_string(&b, &len);
469 if (len != enc->block_size)
Ben Lindstrom08512492002-06-27 00:23:02 +0000470 fatal("%s: bad ivlen: expected %u != %u", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000471 enc->block_size, len);
472
473 if (enc->name == NULL || cipher_by_name(enc->name) != enc->cipher)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000474 fatal("%s: bad cipher name %s or pointer %p", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000475 enc->name, enc->cipher);
476
477 /* Mac structure */
478 mac->name = buffer_get_string(&b, NULL);
Darren Tucker5f3d5be2007-06-05 18:30:18 +1000479 if (mac->name == NULL || mac_setup(mac, mac->name) == -1)
Damien Millere45796f2007-06-11 14:01:42 +1000480 fatal("%s: can not setup mac %s", __func__, mac->name);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000481 mac->enabled = buffer_get_int(&b);
482 mac->key = buffer_get_string(&b, &len);
483 if (len > mac->key_len)
Ben Lindstrom08512492002-06-27 00:23:02 +0000484 fatal("%s: bad mac key length: %u > %d", __func__, len,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000485 mac->key_len);
486 mac->key_len = len;
487
488 /* Comp structure */
489 comp->type = buffer_get_int(&b);
490 comp->enabled = buffer_get_int(&b);
491 comp->name = buffer_get_string(&b, NULL);
492
493 len = buffer_len(&b);
494 if (len != 0)
Ben Lindstrom08512492002-06-27 00:23:02 +0000495 error("newkeys_from_blob: remaining bytes in blob %u", len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000496 buffer_free(&b);
497 return (newkey);
498}
499
500int
501mm_newkeys_to_blob(int mode, u_char **blobp, u_int *lenp)
502{
503 Buffer b;
504 int len;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000505 Enc *enc;
506 Mac *mac;
507 Comp *comp;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000508 Newkeys *newkey = (Newkeys *)packet_get_newkeys(mode);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000509
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000510 debug3("%s: converting %p", __func__, newkey);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000511
512 if (newkey == NULL) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000513 error("%s: newkey == NULL", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000514 return 0;
515 }
516 enc = &newkey->enc;
517 mac = &newkey->mac;
518 comp = &newkey->comp;
519
520 buffer_init(&b);
521 /* Enc structure */
522 buffer_put_cstring(&b, enc->name);
523 /* The cipher struct is constant and shared, you export pointer */
524 buffer_append(&b, &enc->cipher, sizeof(enc->cipher));
525 buffer_put_int(&b, enc->enabled);
526 buffer_put_int(&b, enc->block_size);
527 buffer_put_string(&b, enc->key, enc->key_len);
528 packet_get_keyiv(mode, enc->iv, enc->block_size);
529 buffer_put_string(&b, enc->iv, enc->block_size);
530
531 /* Mac structure */
532 buffer_put_cstring(&b, mac->name);
533 buffer_put_int(&b, mac->enabled);
534 buffer_put_string(&b, mac->key, mac->key_len);
535
536 /* Comp structure */
537 buffer_put_int(&b, comp->type);
538 buffer_put_int(&b, comp->enabled);
539 buffer_put_cstring(&b, comp->name);
540
541 len = buffer_len(&b);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000542 if (lenp != NULL)
543 *lenp = len;
Ben Lindstrom2bf759c2002-07-07 22:13:31 +0000544 if (blobp != NULL) {
545 *blobp = xmalloc(len);
546 memcpy(*blobp, buffer_ptr(&b), len);
547 }
548 memset(buffer_ptr(&b), 0, len);
549 buffer_free(&b);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000550 return len;
551}
552
553static void
554mm_send_kex(Buffer *m, Kex *kex)
555{
556 buffer_put_string(m, kex->session_id, kex->session_id_len);
557 buffer_put_int(m, kex->we_need);
558 buffer_put_int(m, kex->hostkey_type);
559 buffer_put_int(m, kex->kex_type);
560 buffer_put_string(m, buffer_ptr(&kex->my), buffer_len(&kex->my));
561 buffer_put_string(m, buffer_ptr(&kex->peer), buffer_len(&kex->peer));
562 buffer_put_int(m, kex->flags);
563 buffer_put_cstring(m, kex->client_version_string);
564 buffer_put_cstring(m, kex->server_version_string);
565}
566
567void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000568mm_send_keystate(struct monitor *monitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000569{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000570 Buffer m, *input, *output;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000571 u_char *blob, *p;
572 u_int bloblen, plen;
Damien Millera5539d22003-04-09 20:50:06 +1000573 u_int32_t seqnr, packets;
Damien Millerb61f3fc2008-07-11 17:36:48 +1000574 u_int64_t blocks, bytes;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000575
576 buffer_init(&m);
577
578 if (!compat20) {
579 u_char iv[24];
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000580 u_char *key;
581 u_int ivlen, keylen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000582
583 buffer_put_int(&m, packet_get_protocol_flags());
584
585 buffer_put_int(&m, packet_get_ssh1_cipher());
586
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000587 debug3("%s: Sending ssh1 KEY+IV", __func__);
588 keylen = packet_get_encryption_key(NULL);
589 key = xmalloc(keylen+1); /* add 1 if keylen == 0 */
590 keylen = packet_get_encryption_key(key);
591 buffer_put_string(&m, key, keylen);
592 memset(key, 0, keylen);
593 xfree(key);
594
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000595 ivlen = packet_get_keyiv_len(MODE_OUT);
596 packet_get_keyiv(MODE_OUT, iv, ivlen);
597 buffer_put_string(&m, iv, ivlen);
598 ivlen = packet_get_keyiv_len(MODE_OUT);
599 packet_get_keyiv(MODE_IN, iv, ivlen);
600 buffer_put_string(&m, iv, ivlen);
601 goto skip;
602 } else {
603 /* Kex for rekeying */
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000604 mm_send_kex(&m, *monitor->m_pkex);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000605 }
606
607 debug3("%s: Sending new keys: %p %p",
Darren Tuckerf7288d72009-06-21 18:12:20 +1000608 __func__, packet_get_newkeys(MODE_OUT),
609 packet_get_newkeys(MODE_IN));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000610
611 /* Keys from Kex */
612 if (!mm_newkeys_to_blob(MODE_OUT, &blob, &bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000613 fatal("%s: conversion of newkeys failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000614
615 buffer_put_string(&m, blob, bloblen);
616 xfree(blob);
617
618 if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000619 fatal("%s: conversion of newkeys failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000620
621 buffer_put_string(&m, blob, bloblen);
622 xfree(blob);
623
Damien Millerb61f3fc2008-07-11 17:36:48 +1000624 packet_get_state(MODE_OUT, &seqnr, &blocks, &packets, &bytes);
Damien Millera5539d22003-04-09 20:50:06 +1000625 buffer_put_int(&m, seqnr);
626 buffer_put_int64(&m, blocks);
627 buffer_put_int(&m, packets);
Damien Millerb61f3fc2008-07-11 17:36:48 +1000628 buffer_put_int64(&m, bytes);
629 packet_get_state(MODE_IN, &seqnr, &blocks, &packets, &bytes);
Damien Millera5539d22003-04-09 20:50:06 +1000630 buffer_put_int(&m, seqnr);
631 buffer_put_int64(&m, blocks);
632 buffer_put_int(&m, packets);
Damien Millerb61f3fc2008-07-11 17:36:48 +1000633 buffer_put_int64(&m, bytes);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000634
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000635 debug3("%s: New keys have been sent", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000636 skip:
637 /* More key context */
638 plen = packet_get_keycontext(MODE_OUT, NULL);
639 p = xmalloc(plen+1);
640 packet_get_keycontext(MODE_OUT, p);
641 buffer_put_string(&m, p, plen);
642 xfree(p);
643
644 plen = packet_get_keycontext(MODE_IN, NULL);
645 p = xmalloc(plen+1);
646 packet_get_keycontext(MODE_IN, p);
647 buffer_put_string(&m, p, plen);
648 xfree(p);
649
650 /* Compression state */
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000651 debug3("%s: Sending compression state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000652 buffer_put_string(&m, &outgoing_stream, sizeof(outgoing_stream));
653 buffer_put_string(&m, &incoming_stream, sizeof(incoming_stream));
654
655 /* Network I/O buffers */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000656 input = (Buffer *)packet_get_input();
657 output = (Buffer *)packet_get_output();
658 buffer_put_string(&m, buffer_ptr(input), buffer_len(input));
659 buffer_put_string(&m, buffer_ptr(output), buffer_len(output));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000660
Darren Tuckerc5564e12009-06-21 18:53:53 +1000661 /* Roaming */
662 if (compat20) {
663 buffer_put_int64(&m, get_sent_bytes());
664 buffer_put_int64(&m, get_recv_bytes());
665 }
666
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000667 mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m);
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000668 debug3("%s: Finished sending state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000669
670 buffer_free(&m);
671}
672
673int
Damien Miller71a73672006-03-26 14:04:36 +1100674mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000675{
676 Buffer m;
Darren Tucker09991742004-07-17 17:05:14 +1000677 char *p, *msg;
Damien Miller7207f642008-05-19 15:34:50 +1000678 int success = 0, tmp1 = -1, tmp2 = -1;
679
680 /* Kludge: ensure there are fds free to receive the pty/tty */
681 if ((tmp1 = dup(pmonitor->m_recvfd)) == -1 ||
682 (tmp2 = dup(pmonitor->m_recvfd)) == -1) {
683 error("%s: cannot allocate fds for pty", __func__);
684 if (tmp1 > 0)
685 close(tmp1);
686 if (tmp2 > 0)
687 close(tmp2);
688 return 0;
689 }
690 close(tmp1);
691 close(tmp2);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000692
693 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000694 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000695
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000696 debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000697 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000698
699 success = buffer_get_int(&m);
700 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000701 debug3("%s: pty alloc failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000702 buffer_free(&m);
703 return (0);
704 }
705 p = buffer_get_string(&m, NULL);
Darren Tucker09991742004-07-17 17:05:14 +1000706 msg = buffer_get_string(&m, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000707 buffer_free(&m);
708
709 strlcpy(namebuf, p, namebuflen); /* Possible truncation */
710 xfree(p);
711
Darren Tucker09991742004-07-17 17:05:14 +1000712 buffer_append(&loginmsg, msg, strlen(msg));
713 xfree(msg);
714
Damien Miller54fd7cf2007-09-17 12:04:08 +1000715 if ((*ptyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1 ||
716 (*ttyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1)
717 fatal("%s: receive fds failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000718
719 /* Success */
720 return (1);
721}
722
723void
Darren Tucker3e33cec2003-10-02 16:12:36 +1000724mm_session_pty_cleanup2(Session *s)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000725{
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000726 Buffer m;
727
728 if (s->ttyfd == -1)
729 return;
730 buffer_init(&m);
731 buffer_put_cstring(&m, s->tty);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000732 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000733 buffer_free(&m);
734
735 /* closed dup'ed master */
Damien Miller7207f642008-05-19 15:34:50 +1000736 if (s->ptymaster != -1 && close(s->ptymaster) < 0)
737 error("close(s->ptymaster/%d): %s",
738 s->ptymaster, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000739
740 /* unlink pty from session */
741 s->ttyfd = -1;
742}
743
Damien Miller79418552002-04-23 20:28:48 +1000744#ifdef USE_PAM
745void
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100746mm_start_pam(Authctxt *authctxt)
Damien Miller79418552002-04-23 20:28:48 +1000747{
748 Buffer m;
749
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000750 debug3("%s entering", __func__);
Damien Miller4e448a32003-05-14 15:11:48 +1000751 if (!options.use_pam)
752 fatal("UsePAM=no, but ended up in %s anyway", __func__);
Damien Miller79418552002-04-23 20:28:48 +1000753
754 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000755 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, &m);
Damien Miller79418552002-04-23 20:28:48 +1000756
757 buffer_free(&m);
758}
Damien Miller4f9f42a2003-05-10 19:28:02 +1000759
Damien Miller1f499fd2003-08-25 13:08:49 +1000760u_int
761mm_do_pam_account(void)
762{
763 Buffer m;
764 u_int ret;
Darren Tucker77fc29e2004-09-11 23:07:03 +1000765 char *msg;
Damien Miller1f499fd2003-08-25 13:08:49 +1000766
767 debug3("%s entering", __func__);
768 if (!options.use_pam)
769 fatal("UsePAM=no, but ended up in %s anyway", __func__);
770
771 buffer_init(&m);
772 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_ACCOUNT, &m);
773
Damien Millera8e06ce2003-11-21 23:48:55 +1100774 mm_request_receive_expect(pmonitor->m_recvfd,
Damien Miller1f499fd2003-08-25 13:08:49 +1000775 MONITOR_ANS_PAM_ACCOUNT, &m);
776 ret = buffer_get_int(&m);
Darren Tucker77fc29e2004-09-11 23:07:03 +1000777 msg = buffer_get_string(&m, NULL);
778 buffer_append(&loginmsg, msg, strlen(msg));
779 xfree(msg);
Damien Miller1f499fd2003-08-25 13:08:49 +1000780
781 buffer_free(&m);
Damien Miller787b2ec2003-11-21 23:56:47 +1100782
Damien Miller1f499fd2003-08-25 13:08:49 +1000783 debug3("%s returning %d", __func__, ret);
784
785 return (ret);
786}
787
Damien Miller4f9f42a2003-05-10 19:28:02 +1000788void *
789mm_sshpam_init_ctx(Authctxt *authctxt)
790{
791 Buffer m;
792 int success;
793
794 debug3("%s", __func__);
795 buffer_init(&m);
796 buffer_put_cstring(&m, authctxt->user);
797 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_INIT_CTX, &m);
798 debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__);
799 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_INIT_CTX, &m);
800 success = buffer_get_int(&m);
801 if (success == 0) {
802 debug3("%s: pam_init_ctx failed", __func__);
803 buffer_free(&m);
804 return (NULL);
805 }
806 buffer_free(&m);
807 return (authctxt);
808}
809
810int
811mm_sshpam_query(void *ctx, char **name, char **info,
812 u_int *num, char ***prompts, u_int **echo_on)
813{
814 Buffer m;
Damien Miller04b65332005-07-17 17:53:31 +1000815 u_int i;
816 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000817
818 debug3("%s", __func__);
819 buffer_init(&m);
820 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_QUERY, &m);
821 debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__);
822 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_QUERY, &m);
823 ret = buffer_get_int(&m);
824 debug3("%s: pam_query returned %d", __func__, ret);
825 *name = buffer_get_string(&m, NULL);
826 *info = buffer_get_string(&m, NULL);
827 *num = buffer_get_int(&m);
Darren Tuckerd8093e42006-05-04 16:24:34 +1000828 if (*num > PAM_MAX_NUM_MSG)
829 fatal("%s: recieved %u PAM messages, expected <= %u",
830 __func__, *num, PAM_MAX_NUM_MSG);
831 *prompts = xcalloc((*num + 1), sizeof(char *));
832 *echo_on = xcalloc((*num + 1), sizeof(u_int));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000833 for (i = 0; i < *num; ++i) {
834 (*prompts)[i] = buffer_get_string(&m, NULL);
835 (*echo_on)[i] = buffer_get_int(&m);
836 }
837 buffer_free(&m);
838 return (ret);
839}
840
841int
842mm_sshpam_respond(void *ctx, u_int num, char **resp)
843{
844 Buffer m;
Damien Miller04b65332005-07-17 17:53:31 +1000845 u_int i;
846 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000847
848 debug3("%s", __func__);
849 buffer_init(&m);
850 buffer_put_int(&m, num);
851 for (i = 0; i < num; ++i)
852 buffer_put_cstring(&m, resp[i]);
853 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_RESPOND, &m);
854 debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__);
855 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_RESPOND, &m);
856 ret = buffer_get_int(&m);
857 debug3("%s: pam_respond returned %d", __func__, ret);
858 buffer_free(&m);
859 return (ret);
860}
861
862void
863mm_sshpam_free_ctx(void *ctxtp)
864{
865 Buffer m;
866
867 debug3("%s", __func__);
868 buffer_init(&m);
869 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_FREE_CTX, &m);
870 debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__);
871 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_FREE_CTX, &m);
872 buffer_free(&m);
873}
Damien Miller79418552002-04-23 20:28:48 +1000874#endif /* USE_PAM */
875
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000876/* Request process termination */
877
878void
879mm_terminate(void)
880{
881 Buffer m;
882
883 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000884 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000885 buffer_free(&m);
886}
887
888int
889mm_ssh1_session_key(BIGNUM *num)
890{
891 int rsafail;
892 Buffer m;
893
894 buffer_init(&m);
895 buffer_put_bignum2(&m, num);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000896 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000897
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000898 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000899
900 rsafail = buffer_get_int(&m);
901 buffer_get_bignum2(&m, num);
902
903 buffer_free(&m);
904
905 return (rsafail);
906}
907
908static void
909mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
910 char ***prompts, u_int **echo_on)
911{
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000912 *name = xstrdup("");
913 *infotxt = xstrdup("");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000914 *numprompts = 1;
Damien Miller07d86be2006-03-26 14:19:21 +1100915 *prompts = xcalloc(*numprompts, sizeof(char *));
916 *echo_on = xcalloc(*numprompts, sizeof(u_int));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000917 (*echo_on)[0] = 0;
918}
919
920int
921mm_bsdauth_query(void *ctx, char **name, char **infotxt,
922 u_int *numprompts, char ***prompts, u_int **echo_on)
923{
924 Buffer m;
Damien Millerb7df3af2003-02-24 11:55:46 +1100925 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000926 char *challenge;
927
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000928 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000929
930 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000931 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000932
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000933 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000934 &m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100935 success = buffer_get_int(&m);
936 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000937 debug3("%s: no challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000938 buffer_free(&m);
939 return (-1);
940 }
941
942 /* Get the challenge, and format the response */
943 challenge = buffer_get_string(&m, NULL);
944 buffer_free(&m);
945
946 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
947 (*prompts)[0] = challenge;
948
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000949 debug3("%s: received challenge: %s", __func__, challenge);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000950
951 return (0);
952}
953
954int
955mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
956{
957 Buffer m;
958 int authok;
959
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000960 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000961 if (numresponses != 1)
962 return (-1);
963
964 buffer_init(&m);
965 buffer_put_cstring(&m, responses[0]);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000966 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000967
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000968 mm_request_receive_expect(pmonitor->m_recvfd,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000969 MONITOR_ANS_BSDAUTHRESPOND, &m);
970
971 authok = buffer_get_int(&m);
972 buffer_free(&m);
973
974 return ((authok == 0) ? -1 : 0);
975}
976
Darren Tucker042e2e82004-07-08 23:09:42 +1000977#ifdef SKEY
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000978int
979mm_skey_query(void *ctx, char **name, char **infotxt,
980 u_int *numprompts, char ***prompts, u_int **echo_on)
981{
982 Buffer m;
Damien Millerb7df3af2003-02-24 11:55:46 +1100983 u_int success;
Darren Tuckerd6a23f22006-08-05 18:50:35 +1000984 char *challenge;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000985
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000986 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000987
988 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000989 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000990
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000991 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000992 &m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100993 success = buffer_get_int(&m);
994 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000995 debug3("%s: no challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000996 buffer_free(&m);
997 return (-1);
998 }
999
1000 /* Get the challenge, and format the response */
1001 challenge = buffer_get_string(&m, NULL);
1002 buffer_free(&m);
1003
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001004 debug3("%s: received challenge: %s", __func__, challenge);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001005
1006 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
1007
Damien Miller07d86be2006-03-26 14:19:21 +11001008 xasprintf(*prompts, "%s%s", challenge, SKEY_PROMPT);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001009 xfree(challenge);
1010
1011 return (0);
1012}
1013
1014int
1015mm_skey_respond(void *ctx, u_int numresponses, char **responses)
1016{
1017 Buffer m;
1018 int authok;
1019
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001020 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001021 if (numresponses != 1)
1022 return (-1);
1023
1024 buffer_init(&m);
1025 buffer_put_cstring(&m, responses[0]);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001026 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001027
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001028 mm_request_receive_expect(pmonitor->m_recvfd,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001029 MONITOR_ANS_SKEYRESPOND, &m);
1030
1031 authok = buffer_get_int(&m);
1032 buffer_free(&m);
1033
1034 return ((authok == 0) ? -1 : 0);
1035}
Darren Tucker042e2e82004-07-08 23:09:42 +10001036#endif /* SKEY */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001037
1038void
1039mm_ssh1_session_id(u_char session_id[16])
1040{
1041 Buffer m;
1042 int i;
1043
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001044 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001045
1046 buffer_init(&m);
1047 for (i = 0; i < 16; i++)
1048 buffer_put_char(&m, session_id[i]);
1049
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001050 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001051 buffer_free(&m);
1052}
1053
1054int
1055mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
1056{
1057 Buffer m;
1058 Key *key;
1059 u_char *blob;
1060 u_int blen;
Damien Miller06ebedf2003-02-24 12:03:38 +11001061 int allowed = 0, have_forced = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001062
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001063 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001064
1065 buffer_init(&m);
1066 buffer_put_bignum2(&m, client_n);
1067
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001068 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m);
1069 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001070
1071 allowed = buffer_get_int(&m);
1072
Damien Miller06ebedf2003-02-24 12:03:38 +11001073 /* fake forced command */
1074 auth_clear_options();
1075 have_forced = buffer_get_int(&m);
1076 forced_command = have_forced ? xstrdup("true") : NULL;
1077
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001078 if (allowed && rkey != NULL) {
1079 blob = buffer_get_string(&m, &blen);
1080 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001081 fatal("%s: key_from_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001082 *rkey = key;
1083 xfree(blob);
1084 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001085 buffer_free(&m);
1086
1087 return (allowed);
1088}
1089
1090BIGNUM *
1091mm_auth_rsa_generate_challenge(Key *key)
1092{
1093 Buffer m;
1094 BIGNUM *challenge;
1095 u_char *blob;
1096 u_int blen;
1097
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001098 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001099
1100 if ((challenge = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001101 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001102
1103 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1104 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001105 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001106 key->type = KEY_RSA1;
1107
1108 buffer_init(&m);
1109 buffer_put_string(&m, blob, blen);
1110 xfree(blob);
1111
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001112 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m);
1113 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001114
1115 buffer_get_bignum2(&m, challenge);
1116 buffer_free(&m);
1117
1118 return (challenge);
1119}
1120
1121int
1122mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16])
1123{
1124 Buffer m;
1125 u_char *blob;
1126 u_int blen;
1127 int success = 0;
1128
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001129 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001130
1131 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1132 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001133 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001134 key->type = KEY_RSA1;
1135
1136 buffer_init(&m);
1137 buffer_put_string(&m, blob, blen);
1138 buffer_put_string(&m, response, 16);
1139 xfree(blob);
1140
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001141 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m);
1142 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001143
1144 success = buffer_get_int(&m);
1145 buffer_free(&m);
1146
1147 return (success);
1148}
Damien Miller25162f22002-09-12 09:47:29 +10001149
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001150#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001151void
1152mm_audit_event(ssh_audit_event_t event)
1153{
1154 Buffer m;
1155
1156 debug3("%s entering", __func__);
1157
1158 buffer_init(&m);
1159 buffer_put_int(&m, event);
1160
1161 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_EVENT, &m);
1162 buffer_free(&m);
1163}
1164
1165void
1166mm_audit_run_command(const char *command)
1167{
1168 Buffer m;
1169
1170 debug3("%s entering command %s", __func__, command);
1171
1172 buffer_init(&m);
1173 buffer_put_cstring(&m, command);
1174
1175 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, &m);
1176 buffer_free(&m);
1177}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001178#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001179
Darren Tucker0efd1552003-08-26 11:49:55 +10001180#ifdef GSSAPI
1181OM_uint32
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001182mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
Darren Tucker0efd1552003-08-26 11:49:55 +10001183{
1184 Buffer m;
1185 OM_uint32 major;
1186
1187 /* Client doesn't get to see the context */
1188 *ctx = NULL;
1189
1190 buffer_init(&m);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001191 buffer_put_string(&m, goid->elements, goid->length);
Darren Tucker0efd1552003-08-26 11:49:55 +10001192
1193 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m);
1194 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m);
1195
1196 major = buffer_get_int(&m);
1197
1198 buffer_free(&m);
1199 return (major);
1200}
1201
1202OM_uint32
1203mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
1204 gss_buffer_desc *out, OM_uint32 *flags)
1205{
1206 Buffer m;
1207 OM_uint32 major;
Darren Tucker600ad8d2003-08-26 12:10:48 +10001208 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001209
1210 buffer_init(&m);
1211 buffer_put_string(&m, in->value, in->length);
1212
1213 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m);
1214 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m);
1215
1216 major = buffer_get_int(&m);
Darren Tucker600ad8d2003-08-26 12:10:48 +10001217 out->value = buffer_get_string(&m, &len);
1218 out->length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001219 if (flags)
1220 *flags = buffer_get_int(&m);
1221
1222 buffer_free(&m);
1223
1224 return (major);
1225}
1226
Damien Miller0425d402003-11-17 22:18:21 +11001227OM_uint32
1228mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
1229{
1230 Buffer m;
1231 OM_uint32 major;
1232
1233 buffer_init(&m);
1234 buffer_put_string(&m, gssbuf->value, gssbuf->length);
1235 buffer_put_string(&m, gssmic->value, gssmic->length);
1236
1237 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, &m);
1238 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSCHECKMIC,
1239 &m);
1240
1241 major = buffer_get_int(&m);
1242 buffer_free(&m);
1243 return(major);
1244}
1245
Darren Tucker0efd1552003-08-26 11:49:55 +10001246int
1247mm_ssh_gssapi_userok(char *user)
1248{
1249 Buffer m;
1250 int authenticated = 0;
1251
1252 buffer_init(&m);
1253
1254 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m);
1255 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK,
1256 &m);
1257
1258 authenticated = buffer_get_int(&m);
1259
1260 buffer_free(&m);
1261 debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
1262 return (authenticated);
1263}
1264#endif /* GSSAPI */
Damien Miller01ed2272008-11-05 16:20:46 +11001265
1266#ifdef JPAKE
1267void
1268mm_auth2_jpake_get_pwdata(Authctxt *authctxt, BIGNUM **s,
1269 char **hash_scheme, char **salt)
1270{
1271 Buffer m;
1272
1273 debug3("%s entering", __func__);
1274
1275 buffer_init(&m);
1276 mm_request_send(pmonitor->m_recvfd,
1277 MONITOR_REQ_JPAKE_GET_PWDATA, &m);
1278
1279 debug3("%s: waiting for MONITOR_ANS_JPAKE_GET_PWDATA", __func__);
1280 mm_request_receive_expect(pmonitor->m_recvfd,
1281 MONITOR_ANS_JPAKE_GET_PWDATA, &m);
1282
1283 *hash_scheme = buffer_get_string(&m, NULL);
1284 *salt = buffer_get_string(&m, NULL);
1285
1286 buffer_free(&m);
1287}
1288
1289void
Damien Millercee85232009-03-06 00:58:22 +11001290mm_jpake_step1(struct modp_group *grp,
Damien Miller01ed2272008-11-05 16:20:46 +11001291 u_char **id, u_int *id_len,
1292 BIGNUM **priv1, BIGNUM **priv2, BIGNUM **g_priv1, BIGNUM **g_priv2,
1293 u_char **priv1_proof, u_int *priv1_proof_len,
1294 u_char **priv2_proof, u_int *priv2_proof_len)
1295{
1296 Buffer m;
1297
1298 debug3("%s entering", __func__);
1299
1300 buffer_init(&m);
1301 mm_request_send(pmonitor->m_recvfd,
1302 MONITOR_REQ_JPAKE_STEP1, &m);
1303
1304 debug3("%s: waiting for MONITOR_ANS_JPAKE_STEP1", __func__);
1305 mm_request_receive_expect(pmonitor->m_recvfd,
1306 MONITOR_ANS_JPAKE_STEP1, &m);
1307
1308 if ((*priv1 = BN_new()) == NULL ||
1309 (*priv2 = BN_new()) == NULL ||
1310 (*g_priv1 = BN_new()) == NULL ||
1311 (*g_priv2 = BN_new()) == NULL)
1312 fatal("%s: BN_new", __func__);
1313
1314 *id = buffer_get_string(&m, id_len);
1315 /* priv1 and priv2 are, well, private */
1316 buffer_get_bignum2(&m, *g_priv1);
1317 buffer_get_bignum2(&m, *g_priv2);
1318 *priv1_proof = buffer_get_string(&m, priv1_proof_len);
1319 *priv2_proof = buffer_get_string(&m, priv2_proof_len);
1320
1321 buffer_free(&m);
1322}
1323
1324void
Damien Millercee85232009-03-06 00:58:22 +11001325mm_jpake_step2(struct modp_group *grp, BIGNUM *s,
Damien Miller01ed2272008-11-05 16:20:46 +11001326 BIGNUM *mypub1, BIGNUM *theirpub1, BIGNUM *theirpub2, BIGNUM *mypriv2,
1327 const u_char *theirid, u_int theirid_len,
1328 const u_char *myid, u_int myid_len,
1329 const u_char *theirpub1_proof, u_int theirpub1_proof_len,
1330 const u_char *theirpub2_proof, u_int theirpub2_proof_len,
1331 BIGNUM **newpub,
1332 u_char **newpub_exponent_proof, u_int *newpub_exponent_proof_len)
1333{
1334 Buffer m;
1335
1336 debug3("%s entering", __func__);
1337
1338 buffer_init(&m);
1339 /* monitor already has all bignums except theirpub1, theirpub2 */
1340 buffer_put_bignum2(&m, theirpub1);
1341 buffer_put_bignum2(&m, theirpub2);
1342 /* monitor already knows our id */
1343 buffer_put_string(&m, theirid, theirid_len);
1344 buffer_put_string(&m, theirpub1_proof, theirpub1_proof_len);
1345 buffer_put_string(&m, theirpub2_proof, theirpub2_proof_len);
1346
1347 mm_request_send(pmonitor->m_recvfd,
1348 MONITOR_REQ_JPAKE_STEP2, &m);
1349
1350 debug3("%s: waiting for MONITOR_ANS_JPAKE_STEP2", __func__);
1351 mm_request_receive_expect(pmonitor->m_recvfd,
1352 MONITOR_ANS_JPAKE_STEP2, &m);
1353
1354 if ((*newpub = BN_new()) == NULL)
1355 fatal("%s: BN_new", __func__);
1356
1357 buffer_get_bignum2(&m, *newpub);
1358 *newpub_exponent_proof = buffer_get_string(&m,
1359 newpub_exponent_proof_len);
1360
1361 buffer_free(&m);
1362}
1363
1364void
Damien Millercee85232009-03-06 00:58:22 +11001365mm_jpake_key_confirm(struct modp_group *grp, BIGNUM *s, BIGNUM *step2_val,
Damien Miller01ed2272008-11-05 16:20:46 +11001366 BIGNUM *mypriv2, BIGNUM *mypub1, BIGNUM *mypub2,
1367 BIGNUM *theirpub1, BIGNUM *theirpub2,
1368 const u_char *my_id, u_int my_id_len,
1369 const u_char *their_id, u_int their_id_len,
1370 const u_char *sess_id, u_int sess_id_len,
1371 const u_char *theirpriv2_s_proof, u_int theirpriv2_s_proof_len,
1372 BIGNUM **k,
1373 u_char **confirm_hash, u_int *confirm_hash_len)
1374{
1375 Buffer m;
1376
1377 debug3("%s entering", __func__);
1378
1379 buffer_init(&m);
1380 /* monitor already has all bignums except step2_val */
1381 buffer_put_bignum2(&m, step2_val);
1382 /* monitor already knows all the ids */
1383 buffer_put_string(&m, theirpriv2_s_proof, theirpriv2_s_proof_len);
1384
1385 mm_request_send(pmonitor->m_recvfd,
1386 MONITOR_REQ_JPAKE_KEY_CONFIRM, &m);
1387
1388 debug3("%s: waiting for MONITOR_ANS_JPAKE_KEY_CONFIRM", __func__);
1389 mm_request_receive_expect(pmonitor->m_recvfd,
1390 MONITOR_ANS_JPAKE_KEY_CONFIRM, &m);
1391
1392 /* 'k' is sensitive and stays in the monitor */
1393 *confirm_hash = buffer_get_string(&m, confirm_hash_len);
1394
1395 buffer_free(&m);
1396}
1397
1398int
1399mm_jpake_check_confirm(const BIGNUM *k,
1400 const u_char *peer_id, u_int peer_id_len,
1401 const u_char *sess_id, u_int sess_id_len,
1402 const u_char *peer_confirm_hash, u_int peer_confirm_hash_len)
1403{
1404 Buffer m;
1405 int success = 0;
1406
1407 debug3("%s entering", __func__);
1408
1409 buffer_init(&m);
1410 /* k is dummy in slave, ignored */
1411 /* monitor knows all the ids */
1412 buffer_put_string(&m, peer_confirm_hash, peer_confirm_hash_len);
1413 mm_request_send(pmonitor->m_recvfd,
1414 MONITOR_REQ_JPAKE_CHECK_CONFIRM, &m);
1415
1416 debug3("%s: waiting for MONITOR_ANS_JPAKE_CHECK_CONFIRM", __func__);
1417 mm_request_receive_expect(pmonitor->m_recvfd,
1418 MONITOR_ANS_JPAKE_CHECK_CONFIRM, &m);
1419
1420 success = buffer_get_int(&m);
1421 buffer_free(&m);
1422
1423 debug3("%s: success = %d", __func__, success);
1424 return success;
1425}
1426#endif /* JPAKE */