xref: /freebsd/crypto/openssh/msg.c (revision 761efaa7)
1761efaa7SDag-Erling Smørgrav /* $OpenBSD: msg.c,v 1.20 2020/10/18 11:32:01 djm Exp $ */
2545d5ecaSDag-Erling Smørgrav /*
3545d5ecaSDag-Erling Smørgrav  * Copyright (c) 2002 Markus Friedl.  All rights reserved.
4545d5ecaSDag-Erling Smørgrav  *
5545d5ecaSDag-Erling Smørgrav  * Redistribution and use in source and binary forms, with or without
6545d5ecaSDag-Erling Smørgrav  * modification, are permitted provided that the following conditions
7545d5ecaSDag-Erling Smørgrav  * are met:
8545d5ecaSDag-Erling Smørgrav  * 1. Redistributions of source code must retain the above copyright
9545d5ecaSDag-Erling Smørgrav  *    notice, this list of conditions and the following disclaimer.
10545d5ecaSDag-Erling Smørgrav  * 2. Redistributions in binary form must reproduce the above copyright
11545d5ecaSDag-Erling Smørgrav  *    notice, this list of conditions and the following disclaimer in the
12545d5ecaSDag-Erling Smørgrav  *    documentation and/or other materials provided with the distribution.
13545d5ecaSDag-Erling Smørgrav  *
14545d5ecaSDag-Erling Smørgrav  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15545d5ecaSDag-Erling Smørgrav  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16545d5ecaSDag-Erling Smørgrav  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17545d5ecaSDag-Erling Smørgrav  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18545d5ecaSDag-Erling Smørgrav  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19545d5ecaSDag-Erling Smørgrav  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20545d5ecaSDag-Erling Smørgrav  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21545d5ecaSDag-Erling Smørgrav  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22545d5ecaSDag-Erling Smørgrav  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23545d5ecaSDag-Erling Smørgrav  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24545d5ecaSDag-Erling Smørgrav  */
25761efaa7SDag-Erling Smørgrav 
26545d5ecaSDag-Erling Smørgrav #include "includes.h"
27761efaa7SDag-Erling Smørgrav 
28761efaa7SDag-Erling Smørgrav #include <sys/types.h>
29761efaa7SDag-Erling Smørgrav #include <sys/uio.h>
30761efaa7SDag-Erling Smørgrav 
31761efaa7SDag-Erling Smørgrav #include <errno.h>
32761efaa7SDag-Erling Smørgrav #include <stdio.h>
33761efaa7SDag-Erling Smørgrav #include <string.h>
34761efaa7SDag-Erling Smørgrav #include <unistd.h>
35761efaa7SDag-Erling Smørgrav #include <stdarg.h>
36545d5ecaSDag-Erling Smørgrav 
37545d5ecaSDag-Erling Smørgrav #include "sshbuf.h"
38545d5ecaSDag-Erling Smørgrav #include "ssherr.h"
39545d5ecaSDag-Erling Smørgrav #include "log.h"
40545d5ecaSDag-Erling Smørgrav #include "atomicio.h"
41761efaa7SDag-Erling Smørgrav #include "msg.h"
42545d5ecaSDag-Erling Smørgrav #include "misc.h"
43efcad6b7SDag-Erling Smørgrav 
444b17dab0SDag-Erling Smørgrav int
ssh_msg_send(int fd,u_char type,struct sshbuf * m)45545d5ecaSDag-Erling Smørgrav ssh_msg_send(int fd, u_char type, struct sshbuf *m)
46545d5ecaSDag-Erling Smørgrav {
47545d5ecaSDag-Erling Smørgrav 	u_char buf[5];
48545d5ecaSDag-Erling Smørgrav 	u_int mlen = sshbuf_len(m);
494b17dab0SDag-Erling Smørgrav 
50545d5ecaSDag-Erling Smørgrav 	debug3_f("type %u", (unsigned int)type & 0xff);
51761efaa7SDag-Erling Smørgrav 
52545d5ecaSDag-Erling Smørgrav 	put_u32(buf, mlen + 1);
53efcad6b7SDag-Erling Smørgrav 	buf[4] = type;		/* 1st byte of payload is mesg-type */
54efcad6b7SDag-Erling Smørgrav 	if (atomicio(vwrite, fd, buf, sizeof(buf)) != sizeof(buf)) {
55efcad6b7SDag-Erling Smørgrav 		error_f("write: %s", strerror(errno));
56efcad6b7SDag-Erling Smørgrav 		return (-1);
57efcad6b7SDag-Erling Smørgrav 	}
58efcad6b7SDag-Erling Smørgrav 	if (atomicio(vwrite, fd, sshbuf_mutable_ptr(m), mlen) != mlen) {
59efcad6b7SDag-Erling Smørgrav 		error_f("write: %s", strerror(errno));
60efcad6b7SDag-Erling Smørgrav 		return (-1);
61efcad6b7SDag-Erling Smørgrav 	}
62545d5ecaSDag-Erling Smørgrav 	return (0);
63545d5ecaSDag-Erling Smørgrav }
64545d5ecaSDag-Erling Smørgrav 
654b17dab0SDag-Erling Smørgrav int
ssh_msg_recv(int fd,struct sshbuf * m)66545d5ecaSDag-Erling Smørgrav ssh_msg_recv(int fd, struct sshbuf *m)
67545d5ecaSDag-Erling Smørgrav {
68545d5ecaSDag-Erling Smørgrav 	u_char buf[4], *p;
69545d5ecaSDag-Erling Smørgrav 	u_int msg_len;
704b17dab0SDag-Erling Smørgrav 	int r;
71545d5ecaSDag-Erling Smørgrav 
72043840dfSDag-Erling Smørgrav 	debug3("ssh_msg_recv entering");
73043840dfSDag-Erling Smørgrav 
74043840dfSDag-Erling Smørgrav 	if (atomicio(read, fd, buf, sizeof(buf)) != sizeof(buf)) {
75efcad6b7SDag-Erling Smørgrav 		if (errno != EPIPE)
76545d5ecaSDag-Erling Smørgrav 			error_f("read header: %s", strerror(errno));
77761efaa7SDag-Erling Smørgrav 		return (-1);
78efcad6b7SDag-Erling Smørgrav 	}
79efcad6b7SDag-Erling Smørgrav 	msg_len = get_u32(buf);
80efcad6b7SDag-Erling Smørgrav 	if (msg_len > sshbuf_max_size(m)) {
81efcad6b7SDag-Erling Smørgrav 		error_f("read: bad msg_len %u", msg_len);
82545d5ecaSDag-Erling Smørgrav 		return (-1);
83545d5ecaSDag-Erling Smørgrav 	}
84043840dfSDag-Erling Smørgrav 	sshbuf_reset(m);
85043840dfSDag-Erling Smørgrav 	if ((r = sshbuf_reserve(m, msg_len, &p)) != 0) {
86efcad6b7SDag-Erling Smørgrav 		error_fr(r, "reserve");
87efcad6b7SDag-Erling Smørgrav 		return -1;
88efcad6b7SDag-Erling Smørgrav 	}
89545d5ecaSDag-Erling Smørgrav 	if (atomicio(read, fd, p, msg_len) != msg_len) {
90 		error_f("read: %s", strerror(errno));
91 		return (-1);
92 	}
93 	return (0);
94 }
95