blob: ea654a73f10af33a8f0c3ea294d6af2f9f128d96 [file] [log] [blame]
Damien Miller1d75abf2013-01-09 16:12:19 +11001/* $OpenBSD: monitor_wrap.c,v 1.75 2013/01/08 18:49:04 markus 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
Damien Miller8f0bf232011-06-20 14:42:23 +100091void
92mm_log_handler(LogLevel level, const char *msg, void *ctx)
93{
94 Buffer log_msg;
95 struct monitor *mon = (struct monitor *)ctx;
96
97 if (mon->m_log_sendfd == -1)
98 fatal("%s: no log channel", __func__);
99
100 buffer_init(&log_msg);
101 /*
102 * Placeholder for packet length. Will be filled in with the actual
103 * packet length once the packet has been constucted. This saves
104 * fragile math.
105 */
106 buffer_put_int(&log_msg, 0);
107
108 buffer_put_int(&log_msg, level);
109 buffer_put_cstring(&log_msg, msg);
110 put_u32(buffer_ptr(&log_msg), buffer_len(&log_msg) - 4);
111 if (atomicio(vwrite, mon->m_log_sendfd, buffer_ptr(&log_msg),
112 buffer_len(&log_msg)) != buffer_len(&log_msg))
113 fatal("%s: write: %s", __func__, strerror(errno));
114 buffer_free(&log_msg);
115}
116
Darren Tucker3e33cec2003-10-02 16:12:36 +1000117int
118mm_is_monitor(void)
119{
120 /*
121 * m_pid is only set in the privileged part, and
122 * points to the unprivileged child.
123 */
Darren Tuckera47c9bc2003-11-03 20:03:25 +1100124 return (pmonitor && pmonitor->m_pid > 0);
Darren Tucker3e33cec2003-10-02 16:12:36 +1000125}
126
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000127void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000128mm_request_send(int sock, enum monitor_reqtype type, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000129{
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000130 u_int mlen = buffer_len(m);
Ben Lindstromb1bdc5a2002-07-04 00:09:26 +0000131 u_char buf[5];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000132
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000133 debug3("%s entering: type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000134
Damien Miller3f941882006-03-31 23:13:02 +1100135 put_u32(buf, mlen + 1);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000136 buf[4] = (u_char) type; /* 1st byte of payload is mesg-type */
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000137 if (atomicio(vwrite, sock, buf, sizeof(buf)) != sizeof(buf))
Damien Millerb253cc42005-05-26 12:23:44 +1000138 fatal("%s: write: %s", __func__, strerror(errno));
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000139 if (atomicio(vwrite, sock, buffer_ptr(m), mlen) != mlen)
Damien Millerb253cc42005-05-26 12:23:44 +1000140 fatal("%s: write: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000141}
142
143void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000144mm_request_receive(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000145{
146 u_char buf[4];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000147 u_int msg_len;
148
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000149 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000150
Damien Millerb253cc42005-05-26 12:23:44 +1000151 if (atomicio(read, sock, buf, sizeof(buf)) != sizeof(buf)) {
152 if (errno == EPIPE)
Darren Tucker3e33cec2003-10-02 16:12:36 +1000153 cleanup_exit(255);
Damien Millerb253cc42005-05-26 12:23:44 +1000154 fatal("%s: read: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000155 }
Damien Miller3f941882006-03-31 23:13:02 +1100156 msg_len = get_u32(buf);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000157 if (msg_len > 256 * 1024)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000158 fatal("%s: read: bad msg_len %d", __func__, msg_len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000159 buffer_clear(m);
160 buffer_append_space(m, msg_len);
Damien Millerb253cc42005-05-26 12:23:44 +1000161 if (atomicio(read, sock, buffer_ptr(m), msg_len) != msg_len)
162 fatal("%s: read: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000163}
164
165void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000166mm_request_receive_expect(int sock, enum monitor_reqtype type, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000167{
168 u_char rtype;
169
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000170 debug3("%s entering: type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000171
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000172 mm_request_receive(sock, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000173 rtype = buffer_get_char(m);
174 if (rtype != type)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000175 fatal("%s: read: rtype %d != type %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000176 rtype, type);
177}
178
179DH *
180mm_choose_dh(int min, int nbits, int max)
181{
182 BIGNUM *p, *g;
183 int success = 0;
184 Buffer m;
185
186 buffer_init(&m);
187 buffer_put_int(&m, min);
188 buffer_put_int(&m, nbits);
189 buffer_put_int(&m, max);
190
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000191 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000192
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000193 debug3("%s: waiting for MONITOR_ANS_MODULI", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000194 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000195
196 success = buffer_get_char(&m);
197 if (success == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000198 fatal("%s: MONITOR_ANS_MODULI failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000199
200 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000201 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000202 if ((g = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000203 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000204 buffer_get_bignum2(&m, p);
205 buffer_get_bignum2(&m, g);
206
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000207 debug3("%s: remaining %d", __func__, buffer_len(&m));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000208 buffer_free(&m);
209
210 return (dh_new_group(g, p));
211}
212
213int
214mm_key_sign(Key *key, u_char **sigp, u_int *lenp, u_char *data, u_int datalen)
215{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000216 Kex *kex = *pmonitor->m_pkex;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000217 Buffer m;
218
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000219 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000220
221 buffer_init(&m);
222 buffer_put_int(&m, kex->host_key_index(key));
223 buffer_put_string(&m, data, datalen);
224
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000225 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000226
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000227 debug3("%s: waiting for MONITOR_ANS_SIGN", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000228 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000229 *sigp = buffer_get_string(&m, lenp);
230 buffer_free(&m);
231
232 return (0);
233}
234
235struct passwd *
Darren Tuckerb09b6772004-06-22 15:06:46 +1000236mm_getpwnamallow(const char *username)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000237{
238 Buffer m;
239 struct passwd *pw;
Damien Millerd8478b62011-05-29 21:39:36 +1000240 u_int len, i;
Darren Tucker1629c072007-02-19 22:25:37 +1100241 ServerOptions *newopts;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000242
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000243 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000244
245 buffer_init(&m);
Darren Tuckerb09b6772004-06-22 15:06:46 +1000246 buffer_put_cstring(&m, username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000247
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000248 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000249
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000250 debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000251 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000252
253 if (buffer_get_char(&m) == 0) {
Darren Tucker2f8b3d92007-12-02 23:02:15 +1100254 pw = NULL;
255 goto out;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000256 }
Darren Tucker1629c072007-02-19 22:25:37 +1100257 pw = buffer_get_string(&m, &len);
258 if (len != sizeof(struct passwd))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000259 fatal("%s: struct passwd size mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000260 pw->pw_name = buffer_get_string(&m, NULL);
261 pw->pw_passwd = buffer_get_string(&m, NULL);
262 pw->pw_gecos = buffer_get_string(&m, NULL);
Kevin Steves7e147602002-03-22 18:07:17 +0000263#ifdef HAVE_PW_CLASS_IN_PASSWD
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000264 pw->pw_class = buffer_get_string(&m, NULL);
Kevin Steves7e147602002-03-22 18:07:17 +0000265#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000266 pw->pw_dir = buffer_get_string(&m, NULL);
267 pw->pw_shell = buffer_get_string(&m, NULL);
Darren Tucker1629c072007-02-19 22:25:37 +1100268
Darren Tucker2f8b3d92007-12-02 23:02:15 +1100269out:
Darren Tucker1629c072007-02-19 22:25:37 +1100270 /* copy options block as a Match directive may have changed some */
271 newopts = buffer_get_string(&m, &len);
272 if (len != sizeof(*newopts))
273 fatal("%s: option block size mismatch", __func__);
Damien Millerf2e407e2011-05-20 19:04:14 +1000274
275#define M_CP_STROPT(x) do { \
276 if (newopts->x != NULL) \
277 newopts->x = buffer_get_string(&m, NULL); \
278 } while (0)
Damien Millerd8478b62011-05-29 21:39:36 +1000279#define M_CP_STRARRAYOPT(x, nx) do { \
280 for (i = 0; i < newopts->nx; i++) \
281 newopts->x[i] = buffer_get_string(&m, NULL); \
282 } while (0)
Damien Millerf2e407e2011-05-20 19:04:14 +1000283 /* See comment in servconf.h */
284 COPY_MATCH_STRING_OPTS();
285#undef M_CP_STROPT
Damien Millerd8478b62011-05-29 21:39:36 +1000286#undef M_CP_STRARRAYOPT
Damien Millerf2e407e2011-05-20 19:04:14 +1000287
Darren Tucker1629c072007-02-19 22:25:37 +1100288 copy_set_server_options(&options, newopts, 1);
289 xfree(newopts);
290
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000291 buffer_free(&m);
292
293 return (pw);
294}
295
Darren Tucker7eb3de02003-10-15 15:56:58 +1000296char *
297mm_auth2_read_banner(void)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000298{
299 Buffer m;
300 char *banner;
301
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000302 debug3("%s entering", __func__);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000303
304 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000305 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000306 buffer_clear(&m);
307
Darren Tucker7eb3de02003-10-15 15:56:58 +1000308 mm_request_receive_expect(pmonitor->m_recvfd,
309 MONITOR_ANS_AUTH2_READ_BANNER, &m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000310 banner = buffer_get_string(&m, NULL);
311 buffer_free(&m);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000312
Darren Tucker7eb3de02003-10-15 15:56:58 +1000313 /* treat empty banner as missing banner */
314 if (strlen(banner) == 0) {
315 xfree(banner);
316 banner = NULL;
317 }
Damien Miller5ad9fd92002-05-13 11:07:41 +1000318 return (banner);
319}
320
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000321/* Inform the privileged process about service and style */
322
323void
324mm_inform_authserv(char *service, char *style)
325{
326 Buffer m;
327
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000328 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000329
330 buffer_init(&m);
331 buffer_put_cstring(&m, service);
332 buffer_put_cstring(&m, style ? style : "");
333
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000334 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000335
336 buffer_free(&m);
337}
338
339/* Do the password authentication */
340int
341mm_auth_password(Authctxt *authctxt, char *password)
342{
343 Buffer m;
344 int authenticated = 0;
345
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000346 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000347
348 buffer_init(&m);
349 buffer_put_cstring(&m, password);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000350 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000351
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000352 debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000353 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000354
355 authenticated = buffer_get_int(&m);
356
357 buffer_free(&m);
358
359 debug3("%s: user %sauthenticated",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000360 __func__, authenticated ? "" : "not ");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000361 return (authenticated);
362}
363
364int
365mm_user_key_allowed(struct passwd *pw, Key *key)
366{
367 return (mm_key_allowed(MM_USERKEY, NULL, NULL, key));
368}
369
370int
371mm_hostbased_key_allowed(struct passwd *pw, char *user, char *host,
372 Key *key)
373{
374 return (mm_key_allowed(MM_HOSTKEY, user, host, key));
375}
376
377int
378mm_auth_rhosts_rsa_key_allowed(struct passwd *pw, char *user,
379 char *host, Key *key)
380{
381 int ret;
382
383 key->type = KEY_RSA; /* XXX hack for key_to_blob */
384 ret = mm_key_allowed(MM_RSAHOSTKEY, user, host, key);
385 key->type = KEY_RSA1;
386 return (ret);
387}
388
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000389int
390mm_key_allowed(enum mm_keytype type, char *user, char *host, Key *key)
391{
392 Buffer m;
393 u_char *blob;
394 u_int len;
Damien Miller06ebedf2003-02-24 12:03:38 +1100395 int allowed = 0, have_forced = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000396
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000397 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000398
399 /* Convert the key to a blob and the pass it over */
400 if (!key_to_blob(key, &blob, &len))
401 return (0);
402
403 buffer_init(&m);
404 buffer_put_int(&m, type);
405 buffer_put_cstring(&m, user ? user : "");
406 buffer_put_cstring(&m, host ? host : "");
407 buffer_put_string(&m, blob, len);
408 xfree(blob);
409
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000410 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000411
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000412 debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000413 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000414
415 allowed = buffer_get_int(&m);
416
Damien Miller06ebedf2003-02-24 12:03:38 +1100417 /* fake forced command */
418 auth_clear_options();
419 have_forced = buffer_get_int(&m);
420 forced_command = have_forced ? xstrdup("true") : NULL;
421
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000422 buffer_free(&m);
423
424 return (allowed);
425}
426
427/*
428 * This key verify needs to send the key type along, because the
429 * privileged parent makes the decision if the key is allowed
430 * for authentication.
431 */
432
433int
434mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
435{
436 Buffer m;
437 u_char *blob;
438 u_int len;
439 int verified = 0;
440
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000441 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000442
443 /* Convert the key to a blob and the pass it over */
444 if (!key_to_blob(key, &blob, &len))
445 return (0);
446
447 buffer_init(&m);
448 buffer_put_string(&m, blob, len);
449 buffer_put_string(&m, sig, siglen);
450 buffer_put_string(&m, data, datalen);
451 xfree(blob);
452
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000453 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000454
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000455 debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000456 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000457
458 verified = buffer_get_int(&m);
459
460 buffer_free(&m);
461
462 return (verified);
463}
464
465/* Export key state after authentication */
466Newkeys *
467mm_newkeys_from_blob(u_char *blob, int blen)
468{
469 Buffer b;
470 u_int len;
471 Newkeys *newkey = NULL;
472 Enc *enc;
473 Mac *mac;
474 Comp *comp;
475
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000476 debug3("%s: %p(%d)", __func__, blob, blen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000477#ifdef DEBUG_PK
478 dump_base64(stderr, blob, blen);
479#endif
480 buffer_init(&b);
481 buffer_append(&b, blob, blen);
482
483 newkey = xmalloc(sizeof(*newkey));
484 enc = &newkey->enc;
485 mac = &newkey->mac;
486 comp = &newkey->comp;
487
488 /* Enc structure */
489 enc->name = buffer_get_string(&b, NULL);
490 buffer_get(&b, &enc->cipher, sizeof(enc->cipher));
491 enc->enabled = buffer_get_int(&b);
492 enc->block_size = buffer_get_int(&b);
493 enc->key = buffer_get_string(&b, &enc->key_len);
Damien Miller1d75abf2013-01-09 16:12:19 +1100494 enc->iv = buffer_get_string(&b, &enc->iv_len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000495
496 if (enc->name == NULL || cipher_by_name(enc->name) != enc->cipher)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000497 fatal("%s: bad cipher name %s or pointer %p", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000498 enc->name, enc->cipher);
499
500 /* Mac structure */
Damien Miller1d75abf2013-01-09 16:12:19 +1100501 if (cipher_authlen(enc->cipher) == 0) {
502 mac->name = buffer_get_string(&b, NULL);
503 if (mac->name == NULL || mac_setup(mac, mac->name) == -1)
504 fatal("%s: can not setup mac %s", __func__, mac->name);
505 mac->enabled = buffer_get_int(&b);
506 mac->key = buffer_get_string(&b, &len);
507 if (len > mac->key_len)
508 fatal("%s: bad mac key length: %u > %d", __func__, len,
509 mac->key_len);
510 mac->key_len = len;
511 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000512
513 /* Comp structure */
514 comp->type = buffer_get_int(&b);
515 comp->enabled = buffer_get_int(&b);
516 comp->name = buffer_get_string(&b, NULL);
517
518 len = buffer_len(&b);
519 if (len != 0)
Ben Lindstrom08512492002-06-27 00:23:02 +0000520 error("newkeys_from_blob: remaining bytes in blob %u", len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000521 buffer_free(&b);
522 return (newkey);
523}
524
525int
526mm_newkeys_to_blob(int mode, u_char **blobp, u_int *lenp)
527{
528 Buffer b;
529 int len;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000530 Enc *enc;
531 Mac *mac;
532 Comp *comp;
Darren Tuckerf7288d72009-06-21 18:12:20 +1000533 Newkeys *newkey = (Newkeys *)packet_get_newkeys(mode);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000534
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000535 debug3("%s: converting %p", __func__, newkey);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000536
537 if (newkey == NULL) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000538 error("%s: newkey == NULL", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000539 return 0;
540 }
541 enc = &newkey->enc;
542 mac = &newkey->mac;
543 comp = &newkey->comp;
544
545 buffer_init(&b);
546 /* Enc structure */
547 buffer_put_cstring(&b, enc->name);
548 /* The cipher struct is constant and shared, you export pointer */
549 buffer_append(&b, &enc->cipher, sizeof(enc->cipher));
550 buffer_put_int(&b, enc->enabled);
551 buffer_put_int(&b, enc->block_size);
552 buffer_put_string(&b, enc->key, enc->key_len);
Damien Miller1d75abf2013-01-09 16:12:19 +1100553 packet_get_keyiv(mode, enc->iv, enc->iv_len);
554 buffer_put_string(&b, enc->iv, enc->iv_len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000555
556 /* Mac structure */
Damien Miller1d75abf2013-01-09 16:12:19 +1100557 if (cipher_authlen(enc->cipher) == 0) {
558 buffer_put_cstring(&b, mac->name);
559 buffer_put_int(&b, mac->enabled);
560 buffer_put_string(&b, mac->key, mac->key_len);
561 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000562
563 /* Comp structure */
564 buffer_put_int(&b, comp->type);
565 buffer_put_int(&b, comp->enabled);
566 buffer_put_cstring(&b, comp->name);
567
568 len = buffer_len(&b);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000569 if (lenp != NULL)
570 *lenp = len;
Ben Lindstrom2bf759c2002-07-07 22:13:31 +0000571 if (blobp != NULL) {
572 *blobp = xmalloc(len);
573 memcpy(*blobp, buffer_ptr(&b), len);
574 }
575 memset(buffer_ptr(&b), 0, len);
576 buffer_free(&b);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000577 return len;
578}
579
580static void
581mm_send_kex(Buffer *m, Kex *kex)
582{
583 buffer_put_string(m, kex->session_id, kex->session_id_len);
584 buffer_put_int(m, kex->we_need);
585 buffer_put_int(m, kex->hostkey_type);
586 buffer_put_int(m, kex->kex_type);
587 buffer_put_string(m, buffer_ptr(&kex->my), buffer_len(&kex->my));
588 buffer_put_string(m, buffer_ptr(&kex->peer), buffer_len(&kex->peer));
589 buffer_put_int(m, kex->flags);
590 buffer_put_cstring(m, kex->client_version_string);
591 buffer_put_cstring(m, kex->server_version_string);
592}
593
594void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000595mm_send_keystate(struct monitor *monitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000596{
Darren Tuckerf7288d72009-06-21 18:12:20 +1000597 Buffer m, *input, *output;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000598 u_char *blob, *p;
599 u_int bloblen, plen;
Damien Millera5539d22003-04-09 20:50:06 +1000600 u_int32_t seqnr, packets;
Damien Millerb61f3fc2008-07-11 17:36:48 +1000601 u_int64_t blocks, bytes;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000602
603 buffer_init(&m);
604
605 if (!compat20) {
606 u_char iv[24];
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000607 u_char *key;
608 u_int ivlen, keylen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000609
610 buffer_put_int(&m, packet_get_protocol_flags());
611
612 buffer_put_int(&m, packet_get_ssh1_cipher());
613
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000614 debug3("%s: Sending ssh1 KEY+IV", __func__);
615 keylen = packet_get_encryption_key(NULL);
616 key = xmalloc(keylen+1); /* add 1 if keylen == 0 */
617 keylen = packet_get_encryption_key(key);
618 buffer_put_string(&m, key, keylen);
619 memset(key, 0, keylen);
620 xfree(key);
621
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000622 ivlen = packet_get_keyiv_len(MODE_OUT);
623 packet_get_keyiv(MODE_OUT, iv, ivlen);
624 buffer_put_string(&m, iv, ivlen);
Darren Tucker3a7c0412012-10-05 10:51:59 +1000625 ivlen = packet_get_keyiv_len(MODE_IN);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000626 packet_get_keyiv(MODE_IN, iv, ivlen);
627 buffer_put_string(&m, iv, ivlen);
628 goto skip;
629 } else {
630 /* Kex for rekeying */
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000631 mm_send_kex(&m, *monitor->m_pkex);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000632 }
633
634 debug3("%s: Sending new keys: %p %p",
Darren Tuckerf7288d72009-06-21 18:12:20 +1000635 __func__, packet_get_newkeys(MODE_OUT),
636 packet_get_newkeys(MODE_IN));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000637
638 /* Keys from Kex */
639 if (!mm_newkeys_to_blob(MODE_OUT, &blob, &bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000640 fatal("%s: conversion of newkeys failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000641
642 buffer_put_string(&m, blob, bloblen);
643 xfree(blob);
644
645 if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000646 fatal("%s: conversion of newkeys failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000647
648 buffer_put_string(&m, blob, bloblen);
649 xfree(blob);
650
Damien Millerb61f3fc2008-07-11 17:36:48 +1000651 packet_get_state(MODE_OUT, &seqnr, &blocks, &packets, &bytes);
Damien Millera5539d22003-04-09 20:50:06 +1000652 buffer_put_int(&m, seqnr);
653 buffer_put_int64(&m, blocks);
654 buffer_put_int(&m, packets);
Damien Millerb61f3fc2008-07-11 17:36:48 +1000655 buffer_put_int64(&m, bytes);
656 packet_get_state(MODE_IN, &seqnr, &blocks, &packets, &bytes);
Damien Millera5539d22003-04-09 20:50:06 +1000657 buffer_put_int(&m, seqnr);
658 buffer_put_int64(&m, blocks);
659 buffer_put_int(&m, packets);
Damien Millerb61f3fc2008-07-11 17:36:48 +1000660 buffer_put_int64(&m, bytes);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000661
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000662 debug3("%s: New keys have been sent", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000663 skip:
664 /* More key context */
665 plen = packet_get_keycontext(MODE_OUT, NULL);
666 p = xmalloc(plen+1);
667 packet_get_keycontext(MODE_OUT, p);
668 buffer_put_string(&m, p, plen);
669 xfree(p);
670
671 plen = packet_get_keycontext(MODE_IN, NULL);
672 p = xmalloc(plen+1);
673 packet_get_keycontext(MODE_IN, p);
674 buffer_put_string(&m, p, plen);
675 xfree(p);
676
677 /* Compression state */
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000678 debug3("%s: Sending compression state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000679 buffer_put_string(&m, &outgoing_stream, sizeof(outgoing_stream));
680 buffer_put_string(&m, &incoming_stream, sizeof(incoming_stream));
681
682 /* Network I/O buffers */
Darren Tuckerf7288d72009-06-21 18:12:20 +1000683 input = (Buffer *)packet_get_input();
684 output = (Buffer *)packet_get_output();
685 buffer_put_string(&m, buffer_ptr(input), buffer_len(input));
686 buffer_put_string(&m, buffer_ptr(output), buffer_len(output));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000687
Darren Tuckerc5564e12009-06-21 18:53:53 +1000688 /* Roaming */
689 if (compat20) {
690 buffer_put_int64(&m, get_sent_bytes());
691 buffer_put_int64(&m, get_recv_bytes());
692 }
693
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000694 mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m);
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000695 debug3("%s: Finished sending state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000696
697 buffer_free(&m);
698}
699
700int
Damien Miller71a73672006-03-26 14:04:36 +1100701mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000702{
703 Buffer m;
Darren Tucker09991742004-07-17 17:05:14 +1000704 char *p, *msg;
Damien Miller7207f642008-05-19 15:34:50 +1000705 int success = 0, tmp1 = -1, tmp2 = -1;
706
707 /* Kludge: ensure there are fds free to receive the pty/tty */
708 if ((tmp1 = dup(pmonitor->m_recvfd)) == -1 ||
709 (tmp2 = dup(pmonitor->m_recvfd)) == -1) {
710 error("%s: cannot allocate fds for pty", __func__);
711 if (tmp1 > 0)
712 close(tmp1);
713 if (tmp2 > 0)
714 close(tmp2);
715 return 0;
716 }
717 close(tmp1);
718 close(tmp2);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000719
720 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000721 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000722
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000723 debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000724 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000725
726 success = buffer_get_int(&m);
727 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000728 debug3("%s: pty alloc failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000729 buffer_free(&m);
730 return (0);
731 }
732 p = buffer_get_string(&m, NULL);
Darren Tucker09991742004-07-17 17:05:14 +1000733 msg = buffer_get_string(&m, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000734 buffer_free(&m);
735
736 strlcpy(namebuf, p, namebuflen); /* Possible truncation */
737 xfree(p);
738
Darren Tucker09991742004-07-17 17:05:14 +1000739 buffer_append(&loginmsg, msg, strlen(msg));
740 xfree(msg);
741
Damien Miller54fd7cf2007-09-17 12:04:08 +1000742 if ((*ptyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1 ||
743 (*ttyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1)
744 fatal("%s: receive fds failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000745
746 /* Success */
747 return (1);
748}
749
750void
Darren Tucker3e33cec2003-10-02 16:12:36 +1000751mm_session_pty_cleanup2(Session *s)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000752{
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000753 Buffer m;
754
755 if (s->ttyfd == -1)
756 return;
757 buffer_init(&m);
758 buffer_put_cstring(&m, s->tty);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000759 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000760 buffer_free(&m);
761
762 /* closed dup'ed master */
Damien Miller7207f642008-05-19 15:34:50 +1000763 if (s->ptymaster != -1 && close(s->ptymaster) < 0)
764 error("close(s->ptymaster/%d): %s",
765 s->ptymaster, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000766
767 /* unlink pty from session */
768 s->ttyfd = -1;
769}
770
Damien Miller79418552002-04-23 20:28:48 +1000771#ifdef USE_PAM
772void
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100773mm_start_pam(Authctxt *authctxt)
Damien Miller79418552002-04-23 20:28:48 +1000774{
775 Buffer m;
776
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000777 debug3("%s entering", __func__);
Damien Miller4e448a32003-05-14 15:11:48 +1000778 if (!options.use_pam)
779 fatal("UsePAM=no, but ended up in %s anyway", __func__);
Damien Miller79418552002-04-23 20:28:48 +1000780
781 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000782 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, &m);
Damien Miller79418552002-04-23 20:28:48 +1000783
784 buffer_free(&m);
785}
Damien Miller4f9f42a2003-05-10 19:28:02 +1000786
Damien Miller1f499fd2003-08-25 13:08:49 +1000787u_int
788mm_do_pam_account(void)
789{
790 Buffer m;
791 u_int ret;
Darren Tucker77fc29e2004-09-11 23:07:03 +1000792 char *msg;
Damien Miller1f499fd2003-08-25 13:08:49 +1000793
794 debug3("%s entering", __func__);
795 if (!options.use_pam)
796 fatal("UsePAM=no, but ended up in %s anyway", __func__);
797
798 buffer_init(&m);
799 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_ACCOUNT, &m);
800
Damien Millera8e06ce2003-11-21 23:48:55 +1100801 mm_request_receive_expect(pmonitor->m_recvfd,
Damien Miller1f499fd2003-08-25 13:08:49 +1000802 MONITOR_ANS_PAM_ACCOUNT, &m);
803 ret = buffer_get_int(&m);
Darren Tucker77fc29e2004-09-11 23:07:03 +1000804 msg = buffer_get_string(&m, NULL);
805 buffer_append(&loginmsg, msg, strlen(msg));
806 xfree(msg);
Damien Miller1f499fd2003-08-25 13:08:49 +1000807
808 buffer_free(&m);
Damien Miller787b2ec2003-11-21 23:56:47 +1100809
Damien Miller1f499fd2003-08-25 13:08:49 +1000810 debug3("%s returning %d", __func__, ret);
811
812 return (ret);
813}
814
Damien Miller4f9f42a2003-05-10 19:28:02 +1000815void *
816mm_sshpam_init_ctx(Authctxt *authctxt)
817{
818 Buffer m;
819 int success;
820
821 debug3("%s", __func__);
822 buffer_init(&m);
823 buffer_put_cstring(&m, authctxt->user);
824 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_INIT_CTX, &m);
825 debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__);
826 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_INIT_CTX, &m);
827 success = buffer_get_int(&m);
828 if (success == 0) {
829 debug3("%s: pam_init_ctx failed", __func__);
830 buffer_free(&m);
831 return (NULL);
832 }
833 buffer_free(&m);
834 return (authctxt);
835}
836
837int
838mm_sshpam_query(void *ctx, char **name, char **info,
839 u_int *num, char ***prompts, u_int **echo_on)
840{
841 Buffer m;
Damien Miller04b65332005-07-17 17:53:31 +1000842 u_int i;
843 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000844
845 debug3("%s", __func__);
846 buffer_init(&m);
847 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_QUERY, &m);
848 debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__);
849 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_QUERY, &m);
850 ret = buffer_get_int(&m);
851 debug3("%s: pam_query returned %d", __func__, ret);
852 *name = buffer_get_string(&m, NULL);
853 *info = buffer_get_string(&m, NULL);
854 *num = buffer_get_int(&m);
Darren Tuckerd8093e42006-05-04 16:24:34 +1000855 if (*num > PAM_MAX_NUM_MSG)
856 fatal("%s: recieved %u PAM messages, expected <= %u",
857 __func__, *num, PAM_MAX_NUM_MSG);
858 *prompts = xcalloc((*num + 1), sizeof(char *));
859 *echo_on = xcalloc((*num + 1), sizeof(u_int));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000860 for (i = 0; i < *num; ++i) {
861 (*prompts)[i] = buffer_get_string(&m, NULL);
862 (*echo_on)[i] = buffer_get_int(&m);
863 }
864 buffer_free(&m);
865 return (ret);
866}
867
868int
869mm_sshpam_respond(void *ctx, u_int num, char **resp)
870{
871 Buffer m;
Damien Miller04b65332005-07-17 17:53:31 +1000872 u_int i;
873 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000874
875 debug3("%s", __func__);
876 buffer_init(&m);
877 buffer_put_int(&m, num);
878 for (i = 0; i < num; ++i)
879 buffer_put_cstring(&m, resp[i]);
880 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_RESPOND, &m);
881 debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__);
882 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_RESPOND, &m);
883 ret = buffer_get_int(&m);
884 debug3("%s: pam_respond returned %d", __func__, ret);
885 buffer_free(&m);
886 return (ret);
887}
888
889void
890mm_sshpam_free_ctx(void *ctxtp)
891{
892 Buffer m;
893
894 debug3("%s", __func__);
895 buffer_init(&m);
896 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_FREE_CTX, &m);
897 debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__);
898 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_FREE_CTX, &m);
899 buffer_free(&m);
900}
Damien Miller79418552002-04-23 20:28:48 +1000901#endif /* USE_PAM */
902
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000903/* Request process termination */
904
905void
906mm_terminate(void)
907{
908 Buffer m;
909
910 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000911 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000912 buffer_free(&m);
913}
914
915int
916mm_ssh1_session_key(BIGNUM *num)
917{
918 int rsafail;
919 Buffer m;
920
921 buffer_init(&m);
922 buffer_put_bignum2(&m, num);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000923 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000924
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000925 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000926
927 rsafail = buffer_get_int(&m);
928 buffer_get_bignum2(&m, num);
929
930 buffer_free(&m);
931
932 return (rsafail);
933}
934
935static void
936mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
937 char ***prompts, u_int **echo_on)
938{
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000939 *name = xstrdup("");
940 *infotxt = xstrdup("");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000941 *numprompts = 1;
Damien Miller07d86be2006-03-26 14:19:21 +1100942 *prompts = xcalloc(*numprompts, sizeof(char *));
943 *echo_on = xcalloc(*numprompts, sizeof(u_int));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000944 (*echo_on)[0] = 0;
945}
946
947int
948mm_bsdauth_query(void *ctx, char **name, char **infotxt,
949 u_int *numprompts, char ***prompts, u_int **echo_on)
950{
951 Buffer m;
Damien Millerb7df3af2003-02-24 11:55:46 +1100952 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000953 char *challenge;
954
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000955 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000956
957 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000958 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000959
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000960 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000961 &m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100962 success = buffer_get_int(&m);
963 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000964 debug3("%s: no challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000965 buffer_free(&m);
966 return (-1);
967 }
968
969 /* Get the challenge, and format the response */
970 challenge = buffer_get_string(&m, NULL);
971 buffer_free(&m);
972
973 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
974 (*prompts)[0] = challenge;
975
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000976 debug3("%s: received challenge: %s", __func__, challenge);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000977
978 return (0);
979}
980
981int
982mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
983{
984 Buffer m;
985 int authok;
986
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000987 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000988 if (numresponses != 1)
989 return (-1);
990
991 buffer_init(&m);
992 buffer_put_cstring(&m, responses[0]);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000993 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000994
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000995 mm_request_receive_expect(pmonitor->m_recvfd,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000996 MONITOR_ANS_BSDAUTHRESPOND, &m);
997
998 authok = buffer_get_int(&m);
999 buffer_free(&m);
1000
1001 return ((authok == 0) ? -1 : 0);
1002}
1003
Darren Tucker042e2e82004-07-08 23:09:42 +10001004#ifdef SKEY
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001005int
1006mm_skey_query(void *ctx, char **name, char **infotxt,
1007 u_int *numprompts, char ***prompts, u_int **echo_on)
1008{
1009 Buffer m;
Damien Millerb7df3af2003-02-24 11:55:46 +11001010 u_int success;
Darren Tuckerd6a23f22006-08-05 18:50:35 +10001011 char *challenge;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001012
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001013 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001014
1015 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001016 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001017
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001018 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001019 &m);
Damien Millerb7df3af2003-02-24 11:55:46 +11001020 success = buffer_get_int(&m);
1021 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001022 debug3("%s: no challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001023 buffer_free(&m);
1024 return (-1);
1025 }
1026
1027 /* Get the challenge, and format the response */
1028 challenge = buffer_get_string(&m, NULL);
1029 buffer_free(&m);
1030
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001031 debug3("%s: received challenge: %s", __func__, challenge);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001032
1033 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
1034
Damien Miller07d86be2006-03-26 14:19:21 +11001035 xasprintf(*prompts, "%s%s", challenge, SKEY_PROMPT);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001036 xfree(challenge);
1037
1038 return (0);
1039}
1040
1041int
1042mm_skey_respond(void *ctx, u_int numresponses, char **responses)
1043{
1044 Buffer m;
1045 int authok;
1046
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001047 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001048 if (numresponses != 1)
1049 return (-1);
1050
1051 buffer_init(&m);
1052 buffer_put_cstring(&m, responses[0]);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001053 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001054
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001055 mm_request_receive_expect(pmonitor->m_recvfd,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001056 MONITOR_ANS_SKEYRESPOND, &m);
1057
1058 authok = buffer_get_int(&m);
1059 buffer_free(&m);
1060
1061 return ((authok == 0) ? -1 : 0);
1062}
Darren Tucker042e2e82004-07-08 23:09:42 +10001063#endif /* SKEY */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001064
1065void
1066mm_ssh1_session_id(u_char session_id[16])
1067{
1068 Buffer m;
1069 int i;
1070
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001071 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001072
1073 buffer_init(&m);
1074 for (i = 0; i < 16; i++)
1075 buffer_put_char(&m, session_id[i]);
1076
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001077 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001078 buffer_free(&m);
1079}
1080
1081int
1082mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
1083{
1084 Buffer m;
1085 Key *key;
1086 u_char *blob;
1087 u_int blen;
Damien Miller06ebedf2003-02-24 12:03:38 +11001088 int allowed = 0, have_forced = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001089
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001090 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001091
1092 buffer_init(&m);
1093 buffer_put_bignum2(&m, client_n);
1094
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001095 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m);
1096 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001097
1098 allowed = buffer_get_int(&m);
1099
Damien Miller06ebedf2003-02-24 12:03:38 +11001100 /* fake forced command */
1101 auth_clear_options();
1102 have_forced = buffer_get_int(&m);
1103 forced_command = have_forced ? xstrdup("true") : NULL;
1104
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001105 if (allowed && rkey != NULL) {
1106 blob = buffer_get_string(&m, &blen);
1107 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001108 fatal("%s: key_from_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001109 *rkey = key;
1110 xfree(blob);
1111 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001112 buffer_free(&m);
1113
1114 return (allowed);
1115}
1116
1117BIGNUM *
1118mm_auth_rsa_generate_challenge(Key *key)
1119{
1120 Buffer m;
1121 BIGNUM *challenge;
1122 u_char *blob;
1123 u_int blen;
1124
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001125 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001126
1127 if ((challenge = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001128 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001129
1130 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1131 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001132 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001133 key->type = KEY_RSA1;
1134
1135 buffer_init(&m);
1136 buffer_put_string(&m, blob, blen);
1137 xfree(blob);
1138
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001139 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m);
1140 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001141
1142 buffer_get_bignum2(&m, challenge);
1143 buffer_free(&m);
1144
1145 return (challenge);
1146}
1147
1148int
1149mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16])
1150{
1151 Buffer m;
1152 u_char *blob;
1153 u_int blen;
1154 int success = 0;
1155
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001156 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001157
1158 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1159 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001160 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001161 key->type = KEY_RSA1;
1162
1163 buffer_init(&m);
1164 buffer_put_string(&m, blob, blen);
1165 buffer_put_string(&m, response, 16);
1166 xfree(blob);
1167
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001168 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m);
1169 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001170
1171 success = buffer_get_int(&m);
1172 buffer_free(&m);
1173
1174 return (success);
1175}
Damien Miller25162f22002-09-12 09:47:29 +10001176
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001177#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001178void
1179mm_audit_event(ssh_audit_event_t event)
1180{
1181 Buffer m;
1182
1183 debug3("%s entering", __func__);
1184
1185 buffer_init(&m);
1186 buffer_put_int(&m, event);
1187
1188 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_EVENT, &m);
1189 buffer_free(&m);
1190}
1191
1192void
1193mm_audit_run_command(const char *command)
1194{
1195 Buffer m;
1196
1197 debug3("%s entering command %s", __func__, command);
1198
1199 buffer_init(&m);
1200 buffer_put_cstring(&m, command);
1201
1202 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, &m);
1203 buffer_free(&m);
1204}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001205#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001206
Darren Tucker0efd1552003-08-26 11:49:55 +10001207#ifdef GSSAPI
1208OM_uint32
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001209mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
Darren Tucker0efd1552003-08-26 11:49:55 +10001210{
1211 Buffer m;
1212 OM_uint32 major;
1213
1214 /* Client doesn't get to see the context */
1215 *ctx = NULL;
1216
1217 buffer_init(&m);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001218 buffer_put_string(&m, goid->elements, goid->length);
Darren Tucker0efd1552003-08-26 11:49:55 +10001219
1220 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m);
1221 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m);
1222
1223 major = buffer_get_int(&m);
1224
1225 buffer_free(&m);
1226 return (major);
1227}
1228
1229OM_uint32
1230mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
1231 gss_buffer_desc *out, OM_uint32 *flags)
1232{
1233 Buffer m;
1234 OM_uint32 major;
Darren Tucker600ad8d2003-08-26 12:10:48 +10001235 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001236
1237 buffer_init(&m);
1238 buffer_put_string(&m, in->value, in->length);
1239
1240 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m);
1241 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m);
1242
1243 major = buffer_get_int(&m);
Darren Tucker600ad8d2003-08-26 12:10:48 +10001244 out->value = buffer_get_string(&m, &len);
1245 out->length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001246 if (flags)
1247 *flags = buffer_get_int(&m);
1248
1249 buffer_free(&m);
1250
1251 return (major);
1252}
1253
Damien Miller0425d402003-11-17 22:18:21 +11001254OM_uint32
1255mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
1256{
1257 Buffer m;
1258 OM_uint32 major;
1259
1260 buffer_init(&m);
1261 buffer_put_string(&m, gssbuf->value, gssbuf->length);
1262 buffer_put_string(&m, gssmic->value, gssmic->length);
1263
1264 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, &m);
1265 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSCHECKMIC,
1266 &m);
1267
1268 major = buffer_get_int(&m);
1269 buffer_free(&m);
1270 return(major);
1271}
1272
Darren Tucker0efd1552003-08-26 11:49:55 +10001273int
1274mm_ssh_gssapi_userok(char *user)
1275{
1276 Buffer m;
1277 int authenticated = 0;
1278
1279 buffer_init(&m);
1280
1281 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m);
1282 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK,
1283 &m);
1284
1285 authenticated = buffer_get_int(&m);
1286
1287 buffer_free(&m);
1288 debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
1289 return (authenticated);
1290}
1291#endif /* GSSAPI */
Damien Miller01ed2272008-11-05 16:20:46 +11001292
1293#ifdef JPAKE
1294void
1295mm_auth2_jpake_get_pwdata(Authctxt *authctxt, BIGNUM **s,
1296 char **hash_scheme, char **salt)
1297{
1298 Buffer m;
1299
1300 debug3("%s entering", __func__);
1301
1302 buffer_init(&m);
1303 mm_request_send(pmonitor->m_recvfd,
1304 MONITOR_REQ_JPAKE_GET_PWDATA, &m);
1305
1306 debug3("%s: waiting for MONITOR_ANS_JPAKE_GET_PWDATA", __func__);
1307 mm_request_receive_expect(pmonitor->m_recvfd,
1308 MONITOR_ANS_JPAKE_GET_PWDATA, &m);
1309
1310 *hash_scheme = buffer_get_string(&m, NULL);
1311 *salt = buffer_get_string(&m, NULL);
1312
1313 buffer_free(&m);
1314}
1315
1316void
Damien Millercee85232009-03-06 00:58:22 +11001317mm_jpake_step1(struct modp_group *grp,
Damien Miller01ed2272008-11-05 16:20:46 +11001318 u_char **id, u_int *id_len,
1319 BIGNUM **priv1, BIGNUM **priv2, BIGNUM **g_priv1, BIGNUM **g_priv2,
1320 u_char **priv1_proof, u_int *priv1_proof_len,
1321 u_char **priv2_proof, u_int *priv2_proof_len)
1322{
1323 Buffer m;
1324
1325 debug3("%s entering", __func__);
1326
1327 buffer_init(&m);
1328 mm_request_send(pmonitor->m_recvfd,
1329 MONITOR_REQ_JPAKE_STEP1, &m);
1330
1331 debug3("%s: waiting for MONITOR_ANS_JPAKE_STEP1", __func__);
1332 mm_request_receive_expect(pmonitor->m_recvfd,
1333 MONITOR_ANS_JPAKE_STEP1, &m);
1334
1335 if ((*priv1 = BN_new()) == NULL ||
1336 (*priv2 = BN_new()) == NULL ||
1337 (*g_priv1 = BN_new()) == NULL ||
1338 (*g_priv2 = BN_new()) == NULL)
1339 fatal("%s: BN_new", __func__);
1340
1341 *id = buffer_get_string(&m, id_len);
1342 /* priv1 and priv2 are, well, private */
1343 buffer_get_bignum2(&m, *g_priv1);
1344 buffer_get_bignum2(&m, *g_priv2);
1345 *priv1_proof = buffer_get_string(&m, priv1_proof_len);
1346 *priv2_proof = buffer_get_string(&m, priv2_proof_len);
1347
1348 buffer_free(&m);
1349}
1350
1351void
Damien Millercee85232009-03-06 00:58:22 +11001352mm_jpake_step2(struct modp_group *grp, BIGNUM *s,
Damien Miller01ed2272008-11-05 16:20:46 +11001353 BIGNUM *mypub1, BIGNUM *theirpub1, BIGNUM *theirpub2, BIGNUM *mypriv2,
1354 const u_char *theirid, u_int theirid_len,
1355 const u_char *myid, u_int myid_len,
1356 const u_char *theirpub1_proof, u_int theirpub1_proof_len,
1357 const u_char *theirpub2_proof, u_int theirpub2_proof_len,
1358 BIGNUM **newpub,
1359 u_char **newpub_exponent_proof, u_int *newpub_exponent_proof_len)
1360{
1361 Buffer m;
1362
1363 debug3("%s entering", __func__);
1364
1365 buffer_init(&m);
1366 /* monitor already has all bignums except theirpub1, theirpub2 */
1367 buffer_put_bignum2(&m, theirpub1);
1368 buffer_put_bignum2(&m, theirpub2);
1369 /* monitor already knows our id */
1370 buffer_put_string(&m, theirid, theirid_len);
1371 buffer_put_string(&m, theirpub1_proof, theirpub1_proof_len);
1372 buffer_put_string(&m, theirpub2_proof, theirpub2_proof_len);
1373
1374 mm_request_send(pmonitor->m_recvfd,
1375 MONITOR_REQ_JPAKE_STEP2, &m);
1376
1377 debug3("%s: waiting for MONITOR_ANS_JPAKE_STEP2", __func__);
1378 mm_request_receive_expect(pmonitor->m_recvfd,
1379 MONITOR_ANS_JPAKE_STEP2, &m);
1380
1381 if ((*newpub = BN_new()) == NULL)
1382 fatal("%s: BN_new", __func__);
1383
1384 buffer_get_bignum2(&m, *newpub);
1385 *newpub_exponent_proof = buffer_get_string(&m,
1386 newpub_exponent_proof_len);
1387
1388 buffer_free(&m);
1389}
1390
1391void
Damien Millercee85232009-03-06 00:58:22 +11001392mm_jpake_key_confirm(struct modp_group *grp, BIGNUM *s, BIGNUM *step2_val,
Damien Miller01ed2272008-11-05 16:20:46 +11001393 BIGNUM *mypriv2, BIGNUM *mypub1, BIGNUM *mypub2,
1394 BIGNUM *theirpub1, BIGNUM *theirpub2,
1395 const u_char *my_id, u_int my_id_len,
1396 const u_char *their_id, u_int their_id_len,
1397 const u_char *sess_id, u_int sess_id_len,
1398 const u_char *theirpriv2_s_proof, u_int theirpriv2_s_proof_len,
1399 BIGNUM **k,
1400 u_char **confirm_hash, u_int *confirm_hash_len)
1401{
1402 Buffer m;
1403
1404 debug3("%s entering", __func__);
1405
1406 buffer_init(&m);
1407 /* monitor already has all bignums except step2_val */
1408 buffer_put_bignum2(&m, step2_val);
1409 /* monitor already knows all the ids */
1410 buffer_put_string(&m, theirpriv2_s_proof, theirpriv2_s_proof_len);
1411
1412 mm_request_send(pmonitor->m_recvfd,
1413 MONITOR_REQ_JPAKE_KEY_CONFIRM, &m);
1414
1415 debug3("%s: waiting for MONITOR_ANS_JPAKE_KEY_CONFIRM", __func__);
1416 mm_request_receive_expect(pmonitor->m_recvfd,
1417 MONITOR_ANS_JPAKE_KEY_CONFIRM, &m);
1418
1419 /* 'k' is sensitive and stays in the monitor */
1420 *confirm_hash = buffer_get_string(&m, confirm_hash_len);
1421
1422 buffer_free(&m);
1423}
1424
1425int
1426mm_jpake_check_confirm(const BIGNUM *k,
1427 const u_char *peer_id, u_int peer_id_len,
1428 const u_char *sess_id, u_int sess_id_len,
1429 const u_char *peer_confirm_hash, u_int peer_confirm_hash_len)
1430{
1431 Buffer m;
1432 int success = 0;
1433
1434 debug3("%s entering", __func__);
1435
1436 buffer_init(&m);
1437 /* k is dummy in slave, ignored */
1438 /* monitor knows all the ids */
1439 buffer_put_string(&m, peer_confirm_hash, peer_confirm_hash_len);
1440 mm_request_send(pmonitor->m_recvfd,
1441 MONITOR_REQ_JPAKE_CHECK_CONFIRM, &m);
1442
1443 debug3("%s: waiting for MONITOR_ANS_JPAKE_CHECK_CONFIRM", __func__);
1444 mm_request_receive_expect(pmonitor->m_recvfd,
1445 MONITOR_ANS_JPAKE_CHECK_CONFIRM, &m);
1446
1447 success = buffer_get_int(&m);
1448 buffer_free(&m);
1449
1450 debug3("%s: success = %d", __func__, success);
1451 return success;
1452}
1453#endif /* JPAKE */