• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* $OpenBSD: monitor_wrap.c,v 1.73 2011/06/17 21:44:31 djm Exp $ */
2 /*
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"
29 
30 #include <sys/types.h>
31 #include <sys/uio.h>
32 
33 #include <errno.h>
34 #include <pwd.h>
35 #include <signal.h>
36 #include <stdarg.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <unistd.h>
40 
41 #include <openssl/bn.h>
42 #include <openssl/dh.h>
43 #include <openssl/evp.h>
44 
45 #include "openbsd-compat/sys-queue.h"
46 #include "xmalloc.h"
47 #include "ssh.h"
48 #include "dh.h"
49 #include "buffer.h"
50 #include "key.h"
51 #include "cipher.h"
52 #include "kex.h"
53 #include "hostfile.h"
54 #include "auth.h"
55 #include "auth-options.h"
56 #include "packet.h"
57 #include "mac.h"
58 #include "log.h"
59 #ifdef TARGET_OS_MAC    /* XXX Broken krb5 headers on Mac */
60 #undef TARGET_OS_MAC
61 #include "zlib.h"
62 #define TARGET_OS_MAC 1
63 #else
64 #include "zlib.h"
65 #endif
66 #include "monitor.h"
67 #ifdef GSSAPI
68 #include "ssh-gss.h"
69 #endif
70 #include "monitor_wrap.h"
71 #include "atomicio.h"
72 #include "monitor_fdpass.h"
73 #include "misc.h"
74 #include "schnorr.h"
75 #include "jpake.h"
76 #include "uuencode.h"
77 
78 #include "channels.h"
79 #include "session.h"
80 #include "servconf.h"
81 #include "roaming.h"
82 
83 /* Imports */
84 extern int compat20;
85 extern z_stream incoming_stream;
86 extern z_stream outgoing_stream;
87 extern struct monitor *pmonitor;
88 extern Buffer loginmsg;
89 extern ServerOptions options;
90 
91 void
mm_log_handler(LogLevel level,const char * msg,void * ctx)92 mm_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 
117 int
mm_is_monitor(void)118 mm_is_monitor(void)
119 {
120 	/*
121 	 * m_pid is only set in the privileged part, and
122 	 * points to the unprivileged child.
123 	 */
124 	return (pmonitor && pmonitor->m_pid > 0);
125 }
126 
127 void
mm_request_send(int sock,enum monitor_reqtype type,Buffer * m)128 mm_request_send(int sock, enum monitor_reqtype type, Buffer *m)
129 {
130 	u_int mlen = buffer_len(m);
131 	u_char buf[5];
132 
133 	debug3("%s entering: type %d", __func__, type);
134 
135 	put_u32(buf, mlen + 1);
136 	buf[4] = (u_char) type;		/* 1st byte of payload is mesg-type */
137 	if (atomicio(vwrite, sock, buf, sizeof(buf)) != sizeof(buf))
138 		fatal("%s: write: %s", __func__, strerror(errno));
139 	if (atomicio(vwrite, sock, buffer_ptr(m), mlen) != mlen)
140 		fatal("%s: write: %s", __func__, strerror(errno));
141 }
142 
143 void
mm_request_receive(int sock,Buffer * m)144 mm_request_receive(int sock, Buffer *m)
145 {
146 	u_char buf[4];
147 	u_int msg_len;
148 
149 	debug3("%s entering", __func__);
150 
151 	if (atomicio(read, sock, buf, sizeof(buf)) != sizeof(buf)) {
152 		if (errno == EPIPE)
153 			cleanup_exit(255);
154 		fatal("%s: read: %s", __func__, strerror(errno));
155 	}
156 	msg_len = get_u32(buf);
157 	if (msg_len > 256 * 1024)
158 		fatal("%s: read: bad msg_len %d", __func__, msg_len);
159 	buffer_clear(m);
160 	buffer_append_space(m, msg_len);
161 	if (atomicio(read, sock, buffer_ptr(m), msg_len) != msg_len)
162 		fatal("%s: read: %s", __func__, strerror(errno));
163 }
164 
165 void
mm_request_receive_expect(int sock,enum monitor_reqtype type,Buffer * m)166 mm_request_receive_expect(int sock, enum monitor_reqtype type, Buffer *m)
167 {
168 	u_char rtype;
169 
170 	debug3("%s entering: type %d", __func__, type);
171 
172 	mm_request_receive(sock, m);
173 	rtype = buffer_get_char(m);
174 	if (rtype != type)
175 		fatal("%s: read: rtype %d != type %d", __func__,
176 		    rtype, type);
177 }
178 
179 DH *
mm_choose_dh(int min,int nbits,int max)180 mm_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 
191 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, &m);
192 
193 	debug3("%s: waiting for MONITOR_ANS_MODULI", __func__);
194 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, &m);
195 
196 	success = buffer_get_char(&m);
197 	if (success == 0)
198 		fatal("%s: MONITOR_ANS_MODULI failed", __func__);
199 
200 	if ((p = BN_new()) == NULL)
201 		fatal("%s: BN_new failed", __func__);
202 	if ((g = BN_new()) == NULL)
203 		fatal("%s: BN_new failed", __func__);
204 	buffer_get_bignum2(&m, p);
205 	buffer_get_bignum2(&m, g);
206 
207 	debug3("%s: remaining %d", __func__, buffer_len(&m));
208 	buffer_free(&m);
209 
210 	return (dh_new_group(g, p));
211 }
212 
213 int
mm_key_sign(Key * key,u_char ** sigp,u_int * lenp,u_char * data,u_int datalen)214 mm_key_sign(Key *key, u_char **sigp, u_int *lenp, u_char *data, u_int datalen)
215 {
216 	Kex *kex = *pmonitor->m_pkex;
217 	Buffer m;
218 
219 	debug3("%s entering", __func__);
220 
221 	buffer_init(&m);
222 	buffer_put_int(&m, kex->host_key_index(key));
223 	buffer_put_string(&m, data, datalen);
224 
225 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, &m);
226 
227 	debug3("%s: waiting for MONITOR_ANS_SIGN", __func__);
228 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, &m);
229 	*sigp  = buffer_get_string(&m, lenp);
230 	buffer_free(&m);
231 
232 	return (0);
233 }
234 
235 struct passwd *
mm_getpwnamallow(const char * username)236 mm_getpwnamallow(const char *username)
237 {
238 	Buffer m;
239 	struct passwd *pw;
240 	u_int len, i;
241 	ServerOptions *newopts;
242 
243 	debug3("%s entering", __func__);
244 
245 	buffer_init(&m);
246 	buffer_put_cstring(&m, username);
247 
248 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, &m);
249 
250 	debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__);
251 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, &m);
252 
253 	if (buffer_get_char(&m) == 0) {
254 		pw = NULL;
255 		goto out;
256 	}
257 	pw = buffer_get_string(&m, &len);
258 	if (len != sizeof(struct passwd))
259 		fatal("%s: struct passwd size mismatch", __func__);
260 	pw->pw_name = buffer_get_string(&m, NULL);
261 	pw->pw_passwd = buffer_get_string(&m, NULL);
262 #ifdef HAVE_PW_GECOS_IN_PASSWD
263 	pw->pw_gecos = buffer_get_string(&m, NULL);
264 #endif
265 #ifdef HAVE_PW_CLASS_IN_PASSWD
266 	pw->pw_class = buffer_get_string(&m, NULL);
267 #endif
268 	pw->pw_dir = buffer_get_string(&m, NULL);
269 	pw->pw_shell = buffer_get_string(&m, NULL);
270 
271 out:
272 	/* copy options block as a Match directive may have changed some */
273 	newopts = buffer_get_string(&m, &len);
274 	if (len != sizeof(*newopts))
275 		fatal("%s: option block size mismatch", __func__);
276 
277 #define M_CP_STROPT(x) do { \
278 		if (newopts->x != NULL) \
279 			newopts->x = buffer_get_string(&m, NULL); \
280 	} while (0)
281 #define M_CP_STRARRAYOPT(x, nx) do { \
282 		for (i = 0; i < newopts->nx; i++) \
283 			newopts->x[i] = buffer_get_string(&m, NULL); \
284 	} while (0)
285 	/* See comment in servconf.h */
286 	COPY_MATCH_STRING_OPTS();
287 #undef M_CP_STROPT
288 #undef M_CP_STRARRAYOPT
289 
290 	copy_set_server_options(&options, newopts, 1);
291 	xfree(newopts);
292 
293 	buffer_free(&m);
294 
295 	return (pw);
296 }
297 
298 char *
mm_auth2_read_banner(void)299 mm_auth2_read_banner(void)
300 {
301 	Buffer m;
302 	char *banner;
303 
304 	debug3("%s entering", __func__);
305 
306 	buffer_init(&m);
307 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m);
308 	buffer_clear(&m);
309 
310 	mm_request_receive_expect(pmonitor->m_recvfd,
311 	    MONITOR_ANS_AUTH2_READ_BANNER, &m);
312 	banner = buffer_get_string(&m, NULL);
313 	buffer_free(&m);
314 
315 	/* treat empty banner as missing banner */
316 	if (strlen(banner) == 0) {
317 		xfree(banner);
318 		banner = NULL;
319 	}
320 	return (banner);
321 }
322 
323 /* Inform the privileged process about service and style */
324 
325 void
mm_inform_authserv(char * service,char * style)326 mm_inform_authserv(char *service, char *style)
327 {
328 	Buffer m;
329 
330 	debug3("%s entering", __func__);
331 
332 	buffer_init(&m);
333 	buffer_put_cstring(&m, service);
334 	buffer_put_cstring(&m, style ? style : "");
335 
336 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m);
337 
338 	buffer_free(&m);
339 }
340 
341 /* Do the password authentication */
342 int
mm_auth_password(Authctxt * authctxt,char * password)343 mm_auth_password(Authctxt *authctxt, char *password)
344 {
345 	Buffer m;
346 	int authenticated = 0;
347 
348 	debug3("%s entering", __func__);
349 
350 	buffer_init(&m);
351 	buffer_put_cstring(&m, password);
352 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m);
353 
354 	debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
355 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m);
356 
357 	authenticated = buffer_get_int(&m);
358 
359 	buffer_free(&m);
360 
361 	debug3("%s: user %sauthenticated",
362 	    __func__, authenticated ? "" : "not ");
363 	return (authenticated);
364 }
365 
366 int
mm_user_key_allowed(struct passwd * pw,Key * key)367 mm_user_key_allowed(struct passwd *pw, Key *key)
368 {
369 	return (mm_key_allowed(MM_USERKEY, NULL, NULL, key));
370 }
371 
372 int
mm_hostbased_key_allowed(struct passwd * pw,char * user,char * host,Key * key)373 mm_hostbased_key_allowed(struct passwd *pw, char *user, char *host,
374     Key *key)
375 {
376 	return (mm_key_allowed(MM_HOSTKEY, user, host, key));
377 }
378 
379 int
mm_auth_rhosts_rsa_key_allowed(struct passwd * pw,char * user,char * host,Key * key)380 mm_auth_rhosts_rsa_key_allowed(struct passwd *pw, char *user,
381     char *host, Key *key)
382 {
383 	int ret;
384 
385 	key->type = KEY_RSA; /* XXX hack for key_to_blob */
386 	ret = mm_key_allowed(MM_RSAHOSTKEY, user, host, key);
387 	key->type = KEY_RSA1;
388 	return (ret);
389 }
390 
391 int
mm_key_allowed(enum mm_keytype type,char * user,char * host,Key * key)392 mm_key_allowed(enum mm_keytype type, char *user, char *host, Key *key)
393 {
394 	Buffer m;
395 	u_char *blob;
396 	u_int len;
397 	int allowed = 0, have_forced = 0;
398 
399 	debug3("%s entering", __func__);
400 
401 	/* Convert the key to a blob and the pass it over */
402 	if (!key_to_blob(key, &blob, &len))
403 		return (0);
404 
405 	buffer_init(&m);
406 	buffer_put_int(&m, type);
407 	buffer_put_cstring(&m, user ? user : "");
408 	buffer_put_cstring(&m, host ? host : "");
409 	buffer_put_string(&m, blob, len);
410 	xfree(blob);
411 
412 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m);
413 
414 	debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
415 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m);
416 
417 	allowed = buffer_get_int(&m);
418 
419 	/* fake forced command */
420 	auth_clear_options();
421 	have_forced = buffer_get_int(&m);
422 	forced_command = have_forced ? xstrdup("true") : NULL;
423 
424 	buffer_free(&m);
425 
426 	return (allowed);
427 }
428 
429 /*
430  * This key verify needs to send the key type along, because the
431  * privileged parent makes the decision if the key is allowed
432  * for authentication.
433  */
434 
435 int
mm_key_verify(Key * key,u_char * sig,u_int siglen,u_char * data,u_int datalen)436 mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
437 {
438 	Buffer m;
439 	u_char *blob;
440 	u_int len;
441 	int verified = 0;
442 
443 	debug3("%s entering", __func__);
444 
445 	/* Convert the key to a blob and the pass it over */
446 	if (!key_to_blob(key, &blob, &len))
447 		return (0);
448 
449 	buffer_init(&m);
450 	buffer_put_string(&m, blob, len);
451 	buffer_put_string(&m, sig, siglen);
452 	buffer_put_string(&m, data, datalen);
453 	xfree(blob);
454 
455 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m);
456 
457 	debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
458 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m);
459 
460 	verified = buffer_get_int(&m);
461 
462 	buffer_free(&m);
463 
464 	return (verified);
465 }
466 
467 /* Export key state after authentication */
468 Newkeys *
mm_newkeys_from_blob(u_char * blob,int blen)469 mm_newkeys_from_blob(u_char *blob, int blen)
470 {
471 	Buffer b;
472 	u_int len;
473 	Newkeys *newkey = NULL;
474 	Enc *enc;
475 	Mac *mac;
476 	Comp *comp;
477 
478 	debug3("%s: %p(%d)", __func__, blob, blen);
479 #ifdef DEBUG_PK
480 	dump_base64(stderr, blob, blen);
481 #endif
482 	buffer_init(&b);
483 	buffer_append(&b, blob, blen);
484 
485 	newkey = xmalloc(sizeof(*newkey));
486 	enc = &newkey->enc;
487 	mac = &newkey->mac;
488 	comp = &newkey->comp;
489 
490 	/* Enc structure */
491 	enc->name = buffer_get_string(&b, NULL);
492 	buffer_get(&b, &enc->cipher, sizeof(enc->cipher));
493 	enc->enabled = buffer_get_int(&b);
494 	enc->block_size = buffer_get_int(&b);
495 	enc->key = buffer_get_string(&b, &enc->key_len);
496 	enc->iv = buffer_get_string(&b, &len);
497 	if (len != enc->block_size)
498 		fatal("%s: bad ivlen: expected %u != %u", __func__,
499 		    enc->block_size, len);
500 
501 	if (enc->name == NULL || cipher_by_name(enc->name) != enc->cipher)
502 		fatal("%s: bad cipher name %s or pointer %p", __func__,
503 		    enc->name, enc->cipher);
504 
505 	/* Mac structure */
506 	mac->name = buffer_get_string(&b, NULL);
507 	if (mac->name == NULL || mac_setup(mac, mac->name) == -1)
508 		fatal("%s: can not setup mac %s", __func__, mac->name);
509 	mac->enabled = buffer_get_int(&b);
510 	mac->key = buffer_get_string(&b, &len);
511 	if (len > mac->key_len)
512 		fatal("%s: bad mac key length: %u > %d", __func__, len,
513 		    mac->key_len);
514 	mac->key_len = len;
515 
516 	/* Comp structure */
517 	comp->type = buffer_get_int(&b);
518 	comp->enabled = buffer_get_int(&b);
519 	comp->name = buffer_get_string(&b, NULL);
520 
521 	len = buffer_len(&b);
522 	if (len != 0)
523 		error("newkeys_from_blob: remaining bytes in blob %u", len);
524 	buffer_free(&b);
525 	return (newkey);
526 }
527 
528 int
mm_newkeys_to_blob(int mode,u_char ** blobp,u_int * lenp)529 mm_newkeys_to_blob(int mode, u_char **blobp, u_int *lenp)
530 {
531 	Buffer b;
532 	int len;
533 	Enc *enc;
534 	Mac *mac;
535 	Comp *comp;
536 	Newkeys *newkey = (Newkeys *)packet_get_newkeys(mode);
537 
538 	debug3("%s: converting %p", __func__, newkey);
539 
540 	if (newkey == NULL) {
541 		error("%s: newkey == NULL", __func__);
542 		return 0;
543 	}
544 	enc = &newkey->enc;
545 	mac = &newkey->mac;
546 	comp = &newkey->comp;
547 
548 	buffer_init(&b);
549 	/* Enc structure */
550 	buffer_put_cstring(&b, enc->name);
551 	/* The cipher struct is constant and shared, you export pointer */
552 	buffer_append(&b, &enc->cipher, sizeof(enc->cipher));
553 	buffer_put_int(&b, enc->enabled);
554 	buffer_put_int(&b, enc->block_size);
555 	buffer_put_string(&b, enc->key, enc->key_len);
556 	packet_get_keyiv(mode, enc->iv, enc->block_size);
557 	buffer_put_string(&b, enc->iv, enc->block_size);
558 
559 	/* Mac structure */
560 	buffer_put_cstring(&b, mac->name);
561 	buffer_put_int(&b, mac->enabled);
562 	buffer_put_string(&b, mac->key, mac->key_len);
563 
564 	/* Comp structure */
565 	buffer_put_int(&b, comp->type);
566 	buffer_put_int(&b, comp->enabled);
567 	buffer_put_cstring(&b, comp->name);
568 
569 	len = buffer_len(&b);
570 	if (lenp != NULL)
571 		*lenp = len;
572 	if (blobp != NULL) {
573 		*blobp = xmalloc(len);
574 		memcpy(*blobp, buffer_ptr(&b), len);
575 	}
576 	memset(buffer_ptr(&b), 0, len);
577 	buffer_free(&b);
578 	return len;
579 }
580 
581 static void
mm_send_kex(Buffer * m,Kex * kex)582 mm_send_kex(Buffer *m, Kex *kex)
583 {
584 	buffer_put_string(m, kex->session_id, kex->session_id_len);
585 	buffer_put_int(m, kex->we_need);
586 	buffer_put_int(m, kex->hostkey_type);
587 	buffer_put_int(m, kex->kex_type);
588 	buffer_put_string(m, buffer_ptr(&kex->my), buffer_len(&kex->my));
589 	buffer_put_string(m, buffer_ptr(&kex->peer), buffer_len(&kex->peer));
590 	buffer_put_int(m, kex->flags);
591 	buffer_put_cstring(m, kex->client_version_string);
592 	buffer_put_cstring(m, kex->server_version_string);
593 }
594 
595 void
mm_send_keystate(struct monitor * monitor)596 mm_send_keystate(struct monitor *monitor)
597 {
598 	Buffer m, *input, *output;
599 	u_char *blob, *p;
600 	u_int bloblen, plen;
601 	u_int32_t seqnr, packets;
602 	u_int64_t blocks, bytes;
603 
604 	buffer_init(&m);
605 
606 	if (!compat20) {
607 		u_char iv[24];
608 		u_char *key;
609 		u_int ivlen, keylen;
610 
611 		buffer_put_int(&m, packet_get_protocol_flags());
612 
613 		buffer_put_int(&m, packet_get_ssh1_cipher());
614 
615 		debug3("%s: Sending ssh1 KEY+IV", __func__);
616 		keylen = packet_get_encryption_key(NULL);
617 		key = xmalloc(keylen+1);	/* add 1 if keylen == 0 */
618 		keylen = packet_get_encryption_key(key);
619 		buffer_put_string(&m, key, keylen);
620 		memset(key, 0, keylen);
621 		xfree(key);
622 
623 		ivlen = packet_get_keyiv_len(MODE_OUT);
624 		packet_get_keyiv(MODE_OUT, iv, ivlen);
625 		buffer_put_string(&m, iv, ivlen);
626 		ivlen = packet_get_keyiv_len(MODE_OUT);
627 		packet_get_keyiv(MODE_IN, iv, ivlen);
628 		buffer_put_string(&m, iv, ivlen);
629 		goto skip;
630 	} else {
631 		/* Kex for rekeying */
632 		mm_send_kex(&m, *monitor->m_pkex);
633 	}
634 
635 	debug3("%s: Sending new keys: %p %p",
636 	    __func__, packet_get_newkeys(MODE_OUT),
637 	    packet_get_newkeys(MODE_IN));
638 
639 	/* Keys from Kex */
640 	if (!mm_newkeys_to_blob(MODE_OUT, &blob, &bloblen))
641 		fatal("%s: conversion of newkeys failed", __func__);
642 
643 	buffer_put_string(&m, blob, bloblen);
644 	xfree(blob);
645 
646 	if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen))
647 		fatal("%s: conversion of newkeys failed", __func__);
648 
649 	buffer_put_string(&m, blob, bloblen);
650 	xfree(blob);
651 
652 	packet_get_state(MODE_OUT, &seqnr, &blocks, &packets, &bytes);
653 	buffer_put_int(&m, seqnr);
654 	buffer_put_int64(&m, blocks);
655 	buffer_put_int(&m, packets);
656 	buffer_put_int64(&m, bytes);
657 	packet_get_state(MODE_IN, &seqnr, &blocks, &packets, &bytes);
658 	buffer_put_int(&m, seqnr);
659 	buffer_put_int64(&m, blocks);
660 	buffer_put_int(&m, packets);
661 	buffer_put_int64(&m, bytes);
662 
663 	debug3("%s: New keys have been sent", __func__);
664  skip:
665 	/* More key context */
666 	plen = packet_get_keycontext(MODE_OUT, NULL);
667 	p = xmalloc(plen+1);
668 	packet_get_keycontext(MODE_OUT, p);
669 	buffer_put_string(&m, p, plen);
670 	xfree(p);
671 
672 	plen = packet_get_keycontext(MODE_IN, NULL);
673 	p = xmalloc(plen+1);
674 	packet_get_keycontext(MODE_IN, p);
675 	buffer_put_string(&m, p, plen);
676 	xfree(p);
677 
678 	/* Compression state */
679 	debug3("%s: Sending compression state", __func__);
680 	buffer_put_string(&m, &outgoing_stream, sizeof(outgoing_stream));
681 	buffer_put_string(&m, &incoming_stream, sizeof(incoming_stream));
682 
683 	/* Network I/O buffers */
684 	input = (Buffer *)packet_get_input();
685 	output = (Buffer *)packet_get_output();
686 	buffer_put_string(&m, buffer_ptr(input), buffer_len(input));
687 	buffer_put_string(&m, buffer_ptr(output), buffer_len(output));
688 
689 	/* Roaming */
690 	if (compat20) {
691 		buffer_put_int64(&m, get_sent_bytes());
692 		buffer_put_int64(&m, get_recv_bytes());
693 	}
694 
695 	mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m);
696 	debug3("%s: Finished sending state", __func__);
697 
698 	buffer_free(&m);
699 }
700 
701 int
mm_pty_allocate(int * ptyfd,int * ttyfd,char * namebuf,size_t namebuflen)702 mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
703 {
704 	Buffer m;
705 	char *p, *msg;
706 	int success = 0, tmp1 = -1, tmp2 = -1;
707 
708 	/* Kludge: ensure there are fds free to receive the pty/tty */
709 	if ((tmp1 = dup(pmonitor->m_recvfd)) == -1 ||
710 	    (tmp2 = dup(pmonitor->m_recvfd)) == -1) {
711 		error("%s: cannot allocate fds for pty", __func__);
712 		if (tmp1 > 0)
713 			close(tmp1);
714 		if (tmp2 > 0)
715 			close(tmp2);
716 		return 0;
717 	}
718 	close(tmp1);
719 	close(tmp2);
720 
721 	buffer_init(&m);
722 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
723 
724 	debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
725 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
726 
727 	success = buffer_get_int(&m);
728 	if (success == 0) {
729 		debug3("%s: pty alloc failed", __func__);
730 		buffer_free(&m);
731 		return (0);
732 	}
733 	p = buffer_get_string(&m, NULL);
734 	msg = buffer_get_string(&m, NULL);
735 	buffer_free(&m);
736 
737 	strlcpy(namebuf, p, namebuflen); /* Possible truncation */
738 	xfree(p);
739 
740 	buffer_append(&loginmsg, msg, strlen(msg));
741 	xfree(msg);
742 
743 	if ((*ptyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1 ||
744 	    (*ttyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1)
745 		fatal("%s: receive fds failed", __func__);
746 
747 	/* Success */
748 	return (1);
749 }
750 
751 void
mm_session_pty_cleanup2(Session * s)752 mm_session_pty_cleanup2(Session *s)
753 {
754 	Buffer m;
755 
756 	if (s->ttyfd == -1)
757 		return;
758 	buffer_init(&m);
759 	buffer_put_cstring(&m, s->tty);
760 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
761 	buffer_free(&m);
762 
763 	/* closed dup'ed master */
764 	if (s->ptymaster != -1 && close(s->ptymaster) < 0)
765 		error("close(s->ptymaster/%d): %s",
766 		    s->ptymaster, strerror(errno));
767 
768 	/* unlink pty from session */
769 	s->ttyfd = -1;
770 }
771 
772 #ifdef USE_PAM
773 void
mm_start_pam(Authctxt * authctxt)774 mm_start_pam(Authctxt *authctxt)
775 {
776 	Buffer m;
777 
778 	debug3("%s entering", __func__);
779 	if (!options.use_pam)
780 		fatal("UsePAM=no, but ended up in %s anyway", __func__);
781 
782 	buffer_init(&m);
783 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, &m);
784 
785 	buffer_free(&m);
786 }
787 
788 u_int
mm_do_pam_account(void)789 mm_do_pam_account(void)
790 {
791 	Buffer m;
792 	u_int ret;
793 	char *msg;
794 
795 	debug3("%s entering", __func__);
796 	if (!options.use_pam)
797 		fatal("UsePAM=no, but ended up in %s anyway", __func__);
798 
799 	buffer_init(&m);
800 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_ACCOUNT, &m);
801 
802 	mm_request_receive_expect(pmonitor->m_recvfd,
803 	    MONITOR_ANS_PAM_ACCOUNT, &m);
804 	ret = buffer_get_int(&m);
805 	msg = buffer_get_string(&m, NULL);
806 	buffer_append(&loginmsg, msg, strlen(msg));
807 	xfree(msg);
808 
809 	buffer_free(&m);
810 
811 	debug3("%s returning %d", __func__, ret);
812 
813 	return (ret);
814 }
815 
816 void *
mm_sshpam_init_ctx(Authctxt * authctxt)817 mm_sshpam_init_ctx(Authctxt *authctxt)
818 {
819 	Buffer m;
820 	int success;
821 
822 	debug3("%s", __func__);
823 	buffer_init(&m);
824 	buffer_put_cstring(&m, authctxt->user);
825 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_INIT_CTX, &m);
826 	debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__);
827 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_INIT_CTX, &m);
828 	success = buffer_get_int(&m);
829 	if (success == 0) {
830 		debug3("%s: pam_init_ctx failed", __func__);
831 		buffer_free(&m);
832 		return (NULL);
833 	}
834 	buffer_free(&m);
835 	return (authctxt);
836 }
837 
838 int
mm_sshpam_query(void * ctx,char ** name,char ** info,u_int * num,char *** prompts,u_int ** echo_on)839 mm_sshpam_query(void *ctx, char **name, char **info,
840     u_int *num, char ***prompts, u_int **echo_on)
841 {
842 	Buffer m;
843 	u_int i;
844 	int ret;
845 
846 	debug3("%s", __func__);
847 	buffer_init(&m);
848 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_QUERY, &m);
849 	debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__);
850 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_QUERY, &m);
851 	ret = buffer_get_int(&m);
852 	debug3("%s: pam_query returned %d", __func__, ret);
853 	*name = buffer_get_string(&m, NULL);
854 	*info = buffer_get_string(&m, NULL);
855 	*num = buffer_get_int(&m);
856 	if (*num > PAM_MAX_NUM_MSG)
857 		fatal("%s: recieved %u PAM messages, expected <= %u",
858 		    __func__, *num, PAM_MAX_NUM_MSG);
859 	*prompts = xcalloc((*num + 1), sizeof(char *));
860 	*echo_on = xcalloc((*num + 1), sizeof(u_int));
861 	for (i = 0; i < *num; ++i) {
862 		(*prompts)[i] = buffer_get_string(&m, NULL);
863 		(*echo_on)[i] = buffer_get_int(&m);
864 	}
865 	buffer_free(&m);
866 	return (ret);
867 }
868 
869 int
mm_sshpam_respond(void * ctx,u_int num,char ** resp)870 mm_sshpam_respond(void *ctx, u_int num, char **resp)
871 {
872 	Buffer m;
873 	u_int i;
874 	int ret;
875 
876 	debug3("%s", __func__);
877 	buffer_init(&m);
878 	buffer_put_int(&m, num);
879 	for (i = 0; i < num; ++i)
880 		buffer_put_cstring(&m, resp[i]);
881 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_RESPOND, &m);
882 	debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__);
883 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_RESPOND, &m);
884 	ret = buffer_get_int(&m);
885 	debug3("%s: pam_respond returned %d", __func__, ret);
886 	buffer_free(&m);
887 	return (ret);
888 }
889 
890 void
mm_sshpam_free_ctx(void * ctxtp)891 mm_sshpam_free_ctx(void *ctxtp)
892 {
893 	Buffer m;
894 
895 	debug3("%s", __func__);
896 	buffer_init(&m);
897 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_FREE_CTX, &m);
898 	debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__);
899 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_FREE_CTX, &m);
900 	buffer_free(&m);
901 }
902 #endif /* USE_PAM */
903 
904 /* Request process termination */
905 
906 void
mm_terminate(void)907 mm_terminate(void)
908 {
909 	Buffer m;
910 
911 	buffer_init(&m);
912 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
913 	buffer_free(&m);
914 }
915 
916 int
mm_ssh1_session_key(BIGNUM * num)917 mm_ssh1_session_key(BIGNUM *num)
918 {
919 	int rsafail;
920 	Buffer m;
921 
922 	buffer_init(&m);
923 	buffer_put_bignum2(&m, num);
924 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m);
925 
926 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m);
927 
928 	rsafail = buffer_get_int(&m);
929 	buffer_get_bignum2(&m, num);
930 
931 	buffer_free(&m);
932 
933 	return (rsafail);
934 }
935 
936 static void
mm_chall_setup(char ** name,char ** infotxt,u_int * numprompts,char *** prompts,u_int ** echo_on)937 mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
938     char ***prompts, u_int **echo_on)
939 {
940 	*name = xstrdup("");
941 	*infotxt = xstrdup("");
942 	*numprompts = 1;
943 	*prompts = xcalloc(*numprompts, sizeof(char *));
944 	*echo_on = xcalloc(*numprompts, sizeof(u_int));
945 	(*echo_on)[0] = 0;
946 }
947 
948 int
mm_bsdauth_query(void * ctx,char ** name,char ** infotxt,u_int * numprompts,char *** prompts,u_int ** echo_on)949 mm_bsdauth_query(void *ctx, char **name, char **infotxt,
950    u_int *numprompts, char ***prompts, u_int **echo_on)
951 {
952 	Buffer m;
953 	u_int success;
954 	char *challenge;
955 
956 	debug3("%s: entering", __func__);
957 
958 	buffer_init(&m);
959 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
960 
961 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
962 	    &m);
963 	success = buffer_get_int(&m);
964 	if (success == 0) {
965 		debug3("%s: no challenge", __func__);
966 		buffer_free(&m);
967 		return (-1);
968 	}
969 
970 	/* Get the challenge, and format the response */
971 	challenge  = buffer_get_string(&m, NULL);
972 	buffer_free(&m);
973 
974 	mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
975 	(*prompts)[0] = challenge;
976 
977 	debug3("%s: received challenge: %s", __func__, challenge);
978 
979 	return (0);
980 }
981 
982 int
mm_bsdauth_respond(void * ctx,u_int numresponses,char ** responses)983 mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
984 {
985 	Buffer m;
986 	int authok;
987 
988 	debug3("%s: entering", __func__);
989 	if (numresponses != 1)
990 		return (-1);
991 
992 	buffer_init(&m);
993 	buffer_put_cstring(&m, responses[0]);
994 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
995 
996 	mm_request_receive_expect(pmonitor->m_recvfd,
997 	    MONITOR_ANS_BSDAUTHRESPOND, &m);
998 
999 	authok = buffer_get_int(&m);
1000 	buffer_free(&m);
1001 
1002 	return ((authok == 0) ? -1 : 0);
1003 }
1004 
1005 #ifdef SKEY
1006 int
mm_skey_query(void * ctx,char ** name,char ** infotxt,u_int * numprompts,char *** prompts,u_int ** echo_on)1007 mm_skey_query(void *ctx, char **name, char **infotxt,
1008    u_int *numprompts, char ***prompts, u_int **echo_on)
1009 {
1010 	Buffer m;
1011 	u_int success;
1012 	char *challenge;
1013 
1014 	debug3("%s: entering", __func__);
1015 
1016 	buffer_init(&m);
1017 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m);
1018 
1019 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY,
1020 	    &m);
1021 	success = buffer_get_int(&m);
1022 	if (success == 0) {
1023 		debug3("%s: no challenge", __func__);
1024 		buffer_free(&m);
1025 		return (-1);
1026 	}
1027 
1028 	/* Get the challenge, and format the response */
1029 	challenge  = buffer_get_string(&m, NULL);
1030 	buffer_free(&m);
1031 
1032 	debug3("%s: received challenge: %s", __func__, challenge);
1033 
1034 	mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
1035 
1036 	xasprintf(*prompts, "%s%s", challenge, SKEY_PROMPT);
1037 	xfree(challenge);
1038 
1039 	return (0);
1040 }
1041 
1042 int
mm_skey_respond(void * ctx,u_int numresponses,char ** responses)1043 mm_skey_respond(void *ctx, u_int numresponses, char **responses)
1044 {
1045 	Buffer m;
1046 	int authok;
1047 
1048 	debug3("%s: entering", __func__);
1049 	if (numresponses != 1)
1050 		return (-1);
1051 
1052 	buffer_init(&m);
1053 	buffer_put_cstring(&m, responses[0]);
1054 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m);
1055 
1056 	mm_request_receive_expect(pmonitor->m_recvfd,
1057 	    MONITOR_ANS_SKEYRESPOND, &m);
1058 
1059 	authok = buffer_get_int(&m);
1060 	buffer_free(&m);
1061 
1062 	return ((authok == 0) ? -1 : 0);
1063 }
1064 #endif /* SKEY */
1065 
1066 void
mm_ssh1_session_id(u_char session_id[16])1067 mm_ssh1_session_id(u_char session_id[16])
1068 {
1069 	Buffer m;
1070 	int i;
1071 
1072 	debug3("%s entering", __func__);
1073 
1074 	buffer_init(&m);
1075 	for (i = 0; i < 16; i++)
1076 		buffer_put_char(&m, session_id[i]);
1077 
1078 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m);
1079 	buffer_free(&m);
1080 }
1081 
1082 int
mm_auth_rsa_key_allowed(struct passwd * pw,BIGNUM * client_n,Key ** rkey)1083 mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
1084 {
1085 	Buffer m;
1086 	Key *key;
1087 	u_char *blob;
1088 	u_int blen;
1089 	int allowed = 0, have_forced = 0;
1090 
1091 	debug3("%s entering", __func__);
1092 
1093 	buffer_init(&m);
1094 	buffer_put_bignum2(&m, client_n);
1095 
1096 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m);
1097 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m);
1098 
1099 	allowed = buffer_get_int(&m);
1100 
1101 	/* fake forced command */
1102 	auth_clear_options();
1103 	have_forced = buffer_get_int(&m);
1104 	forced_command = have_forced ? xstrdup("true") : NULL;
1105 
1106 	if (allowed && rkey != NULL) {
1107 		blob = buffer_get_string(&m, &blen);
1108 		if ((key = key_from_blob(blob, blen)) == NULL)
1109 			fatal("%s: key_from_blob failed", __func__);
1110 		*rkey = key;
1111 		xfree(blob);
1112 	}
1113 	buffer_free(&m);
1114 
1115 	return (allowed);
1116 }
1117 
1118 BIGNUM *
mm_auth_rsa_generate_challenge(Key * key)1119 mm_auth_rsa_generate_challenge(Key *key)
1120 {
1121 	Buffer m;
1122 	BIGNUM *challenge;
1123 	u_char *blob;
1124 	u_int blen;
1125 
1126 	debug3("%s entering", __func__);
1127 
1128 	if ((challenge = BN_new()) == NULL)
1129 		fatal("%s: BN_new failed", __func__);
1130 
1131 	key->type = KEY_RSA;    /* XXX cheat for key_to_blob */
1132 	if (key_to_blob(key, &blob, &blen) == 0)
1133 		fatal("%s: key_to_blob failed", __func__);
1134 	key->type = KEY_RSA1;
1135 
1136 	buffer_init(&m);
1137 	buffer_put_string(&m, blob, blen);
1138 	xfree(blob);
1139 
1140 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m);
1141 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m);
1142 
1143 	buffer_get_bignum2(&m, challenge);
1144 	buffer_free(&m);
1145 
1146 	return (challenge);
1147 }
1148 
1149 int
mm_auth_rsa_verify_response(Key * key,BIGNUM * p,u_char response[16])1150 mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16])
1151 {
1152 	Buffer m;
1153 	u_char *blob;
1154 	u_int blen;
1155 	int success = 0;
1156 
1157 	debug3("%s entering", __func__);
1158 
1159 	key->type = KEY_RSA;    /* XXX cheat for key_to_blob */
1160 	if (key_to_blob(key, &blob, &blen) == 0)
1161 		fatal("%s: key_to_blob failed", __func__);
1162 	key->type = KEY_RSA1;
1163 
1164 	buffer_init(&m);
1165 	buffer_put_string(&m, blob, blen);
1166 	buffer_put_string(&m, response, 16);
1167 	xfree(blob);
1168 
1169 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m);
1170 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m);
1171 
1172 	success = buffer_get_int(&m);
1173 	buffer_free(&m);
1174 
1175 	return (success);
1176 }
1177 
1178 #ifdef SSH_AUDIT_EVENTS
1179 void
mm_audit_event(ssh_audit_event_t event)1180 mm_audit_event(ssh_audit_event_t event)
1181 {
1182 	Buffer m;
1183 
1184 	debug3("%s entering", __func__);
1185 
1186 	buffer_init(&m);
1187 	buffer_put_int(&m, event);
1188 
1189 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_EVENT, &m);
1190 	buffer_free(&m);
1191 }
1192 
1193 void
mm_audit_run_command(const char * command)1194 mm_audit_run_command(const char *command)
1195 {
1196 	Buffer m;
1197 
1198 	debug3("%s entering command %s", __func__, command);
1199 
1200 	buffer_init(&m);
1201 	buffer_put_cstring(&m, command);
1202 
1203 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, &m);
1204 	buffer_free(&m);
1205 }
1206 #endif /* SSH_AUDIT_EVENTS */
1207 
1208 #ifdef GSSAPI
1209 OM_uint32
mm_ssh_gssapi_server_ctx(Gssctxt ** ctx,gss_OID goid)1210 mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
1211 {
1212 	Buffer m;
1213 	OM_uint32 major;
1214 
1215 	/* Client doesn't get to see the context */
1216 	*ctx = NULL;
1217 
1218 	buffer_init(&m);
1219 	buffer_put_string(&m, goid->elements, goid->length);
1220 
1221 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m);
1222 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m);
1223 
1224 	major = buffer_get_int(&m);
1225 
1226 	buffer_free(&m);
1227 	return (major);
1228 }
1229 
1230 OM_uint32
mm_ssh_gssapi_accept_ctx(Gssctxt * ctx,gss_buffer_desc * in,gss_buffer_desc * out,OM_uint32 * flags)1231 mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
1232     gss_buffer_desc *out, OM_uint32 *flags)
1233 {
1234 	Buffer m;
1235 	OM_uint32 major;
1236 	u_int len;
1237 
1238 	buffer_init(&m);
1239 	buffer_put_string(&m, in->value, in->length);
1240 
1241 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m);
1242 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m);
1243 
1244 	major = buffer_get_int(&m);
1245 	out->value = buffer_get_string(&m, &len);
1246 	out->length = len;
1247 	if (flags)
1248 		*flags = buffer_get_int(&m);
1249 
1250 	buffer_free(&m);
1251 
1252 	return (major);
1253 }
1254 
1255 OM_uint32
mm_ssh_gssapi_checkmic(Gssctxt * ctx,gss_buffer_t gssbuf,gss_buffer_t gssmic)1256 mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
1257 {
1258 	Buffer m;
1259 	OM_uint32 major;
1260 
1261 	buffer_init(&m);
1262 	buffer_put_string(&m, gssbuf->value, gssbuf->length);
1263 	buffer_put_string(&m, gssmic->value, gssmic->length);
1264 
1265 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, &m);
1266 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSCHECKMIC,
1267 	    &m);
1268 
1269 	major = buffer_get_int(&m);
1270 	buffer_free(&m);
1271 	return(major);
1272 }
1273 
1274 int
mm_ssh_gssapi_userok(char * user)1275 mm_ssh_gssapi_userok(char *user)
1276 {
1277 	Buffer m;
1278 	int authenticated = 0;
1279 
1280 	buffer_init(&m);
1281 
1282 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m);
1283 	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK,
1284 				  &m);
1285 
1286 	authenticated = buffer_get_int(&m);
1287 
1288 	buffer_free(&m);
1289 	debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
1290 	return (authenticated);
1291 }
1292 #endif /* GSSAPI */
1293 
1294 #ifdef JPAKE
1295 void
mm_auth2_jpake_get_pwdata(Authctxt * authctxt,BIGNUM ** s,char ** hash_scheme,char ** salt)1296 mm_auth2_jpake_get_pwdata(Authctxt *authctxt, BIGNUM **s,
1297     char **hash_scheme, char **salt)
1298 {
1299 	Buffer m;
1300 
1301 	debug3("%s entering", __func__);
1302 
1303 	buffer_init(&m);
1304 	mm_request_send(pmonitor->m_recvfd,
1305 	    MONITOR_REQ_JPAKE_GET_PWDATA, &m);
1306 
1307 	debug3("%s: waiting for MONITOR_ANS_JPAKE_GET_PWDATA", __func__);
1308 	mm_request_receive_expect(pmonitor->m_recvfd,
1309 	    MONITOR_ANS_JPAKE_GET_PWDATA, &m);
1310 
1311 	*hash_scheme = buffer_get_string(&m, NULL);
1312 	*salt = buffer_get_string(&m, NULL);
1313 
1314 	buffer_free(&m);
1315 }
1316 
1317 void
mm_jpake_step1(struct modp_group * grp,u_char ** id,u_int * id_len,BIGNUM ** priv1,BIGNUM ** priv2,BIGNUM ** g_priv1,BIGNUM ** g_priv2,u_char ** priv1_proof,u_int * priv1_proof_len,u_char ** priv2_proof,u_int * priv2_proof_len)1318 mm_jpake_step1(struct modp_group *grp,
1319     u_char **id, u_int *id_len,
1320     BIGNUM **priv1, BIGNUM **priv2, BIGNUM **g_priv1, BIGNUM **g_priv2,
1321     u_char **priv1_proof, u_int *priv1_proof_len,
1322     u_char **priv2_proof, u_int *priv2_proof_len)
1323 {
1324 	Buffer m;
1325 
1326 	debug3("%s entering", __func__);
1327 
1328 	buffer_init(&m);
1329 	mm_request_send(pmonitor->m_recvfd,
1330 	    MONITOR_REQ_JPAKE_STEP1, &m);
1331 
1332 	debug3("%s: waiting for MONITOR_ANS_JPAKE_STEP1", __func__);
1333 	mm_request_receive_expect(pmonitor->m_recvfd,
1334 	    MONITOR_ANS_JPAKE_STEP1, &m);
1335 
1336 	if ((*priv1 = BN_new()) == NULL ||
1337 	    (*priv2 = BN_new()) == NULL ||
1338 	    (*g_priv1 = BN_new()) == NULL ||
1339 	    (*g_priv2 = BN_new()) == NULL)
1340 		fatal("%s: BN_new", __func__);
1341 
1342 	*id = buffer_get_string(&m, id_len);
1343 	/* priv1 and priv2 are, well, private */
1344 	buffer_get_bignum2(&m, *g_priv1);
1345 	buffer_get_bignum2(&m, *g_priv2);
1346 	*priv1_proof = buffer_get_string(&m, priv1_proof_len);
1347 	*priv2_proof = buffer_get_string(&m, priv2_proof_len);
1348 
1349 	buffer_free(&m);
1350 }
1351 
1352 void
mm_jpake_step2(struct modp_group * grp,BIGNUM * s,BIGNUM * mypub1,BIGNUM * theirpub1,BIGNUM * theirpub2,BIGNUM * mypriv2,const u_char * theirid,u_int theirid_len,const u_char * myid,u_int myid_len,const u_char * theirpub1_proof,u_int theirpub1_proof_len,const u_char * theirpub2_proof,u_int theirpub2_proof_len,BIGNUM ** newpub,u_char ** newpub_exponent_proof,u_int * newpub_exponent_proof_len)1353 mm_jpake_step2(struct modp_group *grp, BIGNUM *s,
1354     BIGNUM *mypub1, BIGNUM *theirpub1, BIGNUM *theirpub2, BIGNUM *mypriv2,
1355     const u_char *theirid, u_int theirid_len,
1356     const u_char *myid, u_int myid_len,
1357     const u_char *theirpub1_proof, u_int theirpub1_proof_len,
1358     const u_char *theirpub2_proof, u_int theirpub2_proof_len,
1359     BIGNUM **newpub,
1360     u_char **newpub_exponent_proof, u_int *newpub_exponent_proof_len)
1361 {
1362 	Buffer m;
1363 
1364 	debug3("%s entering", __func__);
1365 
1366 	buffer_init(&m);
1367 	/* monitor already has all bignums except theirpub1, theirpub2 */
1368 	buffer_put_bignum2(&m, theirpub1);
1369 	buffer_put_bignum2(&m, theirpub2);
1370 	/* monitor already knows our id */
1371 	buffer_put_string(&m, theirid, theirid_len);
1372 	buffer_put_string(&m, theirpub1_proof, theirpub1_proof_len);
1373 	buffer_put_string(&m, theirpub2_proof, theirpub2_proof_len);
1374 
1375 	mm_request_send(pmonitor->m_recvfd,
1376 	    MONITOR_REQ_JPAKE_STEP2, &m);
1377 
1378 	debug3("%s: waiting for MONITOR_ANS_JPAKE_STEP2", __func__);
1379 	mm_request_receive_expect(pmonitor->m_recvfd,
1380 	    MONITOR_ANS_JPAKE_STEP2, &m);
1381 
1382 	if ((*newpub = BN_new()) == NULL)
1383 		fatal("%s: BN_new", __func__);
1384 
1385 	buffer_get_bignum2(&m, *newpub);
1386 	*newpub_exponent_proof = buffer_get_string(&m,
1387 	    newpub_exponent_proof_len);
1388 
1389 	buffer_free(&m);
1390 }
1391 
1392 void
mm_jpake_key_confirm(struct modp_group * grp,BIGNUM * s,BIGNUM * step2_val,BIGNUM * mypriv2,BIGNUM * mypub1,BIGNUM * mypub2,BIGNUM * theirpub1,BIGNUM * theirpub2,const u_char * my_id,u_int my_id_len,const u_char * their_id,u_int their_id_len,const u_char * sess_id,u_int sess_id_len,const u_char * theirpriv2_s_proof,u_int theirpriv2_s_proof_len,BIGNUM ** k,u_char ** confirm_hash,u_int * confirm_hash_len)1393 mm_jpake_key_confirm(struct modp_group *grp, BIGNUM *s, BIGNUM *step2_val,
1394     BIGNUM *mypriv2, BIGNUM *mypub1, BIGNUM *mypub2,
1395     BIGNUM *theirpub1, BIGNUM *theirpub2,
1396     const u_char *my_id, u_int my_id_len,
1397     const u_char *their_id, u_int their_id_len,
1398     const u_char *sess_id, u_int sess_id_len,
1399     const u_char *theirpriv2_s_proof, u_int theirpriv2_s_proof_len,
1400     BIGNUM **k,
1401     u_char **confirm_hash, u_int *confirm_hash_len)
1402 {
1403 	Buffer m;
1404 
1405 	debug3("%s entering", __func__);
1406 
1407 	buffer_init(&m);
1408 	/* monitor already has all bignums except step2_val */
1409 	buffer_put_bignum2(&m, step2_val);
1410 	/* monitor already knows all the ids */
1411 	buffer_put_string(&m, theirpriv2_s_proof, theirpriv2_s_proof_len);
1412 
1413 	mm_request_send(pmonitor->m_recvfd,
1414 	    MONITOR_REQ_JPAKE_KEY_CONFIRM, &m);
1415 
1416 	debug3("%s: waiting for MONITOR_ANS_JPAKE_KEY_CONFIRM", __func__);
1417 	mm_request_receive_expect(pmonitor->m_recvfd,
1418 	    MONITOR_ANS_JPAKE_KEY_CONFIRM, &m);
1419 
1420 	/* 'k' is sensitive and stays in the monitor */
1421 	*confirm_hash = buffer_get_string(&m, confirm_hash_len);
1422 
1423 	buffer_free(&m);
1424 }
1425 
1426 int
mm_jpake_check_confirm(const BIGNUM * k,const u_char * peer_id,u_int peer_id_len,const u_char * sess_id,u_int sess_id_len,const u_char * peer_confirm_hash,u_int peer_confirm_hash_len)1427 mm_jpake_check_confirm(const BIGNUM *k,
1428     const u_char *peer_id, u_int peer_id_len,
1429     const u_char *sess_id, u_int sess_id_len,
1430     const u_char *peer_confirm_hash, u_int peer_confirm_hash_len)
1431 {
1432 	Buffer m;
1433 	int success = 0;
1434 
1435 	debug3("%s entering", __func__);
1436 
1437 	buffer_init(&m);
1438 	/* k is dummy in slave, ignored */
1439 	/* monitor knows all the ids */
1440 	buffer_put_string(&m, peer_confirm_hash, peer_confirm_hash_len);
1441 	mm_request_send(pmonitor->m_recvfd,
1442 	    MONITOR_REQ_JPAKE_CHECK_CONFIRM, &m);
1443 
1444 	debug3("%s: waiting for MONITOR_ANS_JPAKE_CHECK_CONFIRM", __func__);
1445 	mm_request_receive_expect(pmonitor->m_recvfd,
1446 	    MONITOR_ANS_JPAKE_CHECK_CONFIRM, &m);
1447 
1448 	success = buffer_get_int(&m);
1449 	buffer_free(&m);
1450 
1451 	debug3("%s: success = %d", __func__, success);
1452 	return success;
1453 }
1454 #endif /* JPAKE */
1455