xref: /freebsd/crypto/openssh/msg.c (revision 19261079b74319502c6ffa1249920079f0f69a72)
1*19261079SEd Maste /* $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 
37bc5531deSDag-Erling Smørgrav #include "sshbuf.h"
38bc5531deSDag-Erling Smørgrav #include "ssherr.h"
39545d5ecaSDag-Erling Smørgrav #include "log.h"
40545d5ecaSDag-Erling Smørgrav #include "atomicio.h"
41545d5ecaSDag-Erling Smørgrav #include "msg.h"
42761efaa7SDag-Erling Smørgrav #include "misc.h"
43545d5ecaSDag-Erling Smørgrav 
44efcad6b7SDag-Erling Smørgrav int
ssh_msg_send(int fd,u_char type,struct sshbuf * m)45bc5531deSDag-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];
48bc5531deSDag-Erling Smørgrav 	u_int mlen = sshbuf_len(m);
49545d5ecaSDag-Erling Smørgrav 
50*19261079SEd Maste 	debug3_f("type %u", (unsigned int)type & 0xff);
51545d5ecaSDag-Erling Smørgrav 
52761efaa7SDag-Erling Smørgrav 	put_u32(buf, mlen + 1);
53545d5ecaSDag-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)) {
55*19261079SEd Maste 		error_f("write: %s", strerror(errno));
56efcad6b7SDag-Erling Smørgrav 		return (-1);
57efcad6b7SDag-Erling Smørgrav 	}
58190cef3dSDag-Erling Smørgrav 	if (atomicio(vwrite, fd, sshbuf_mutable_ptr(m), mlen) != mlen) {
59*19261079SEd Maste 		error_f("write: %s", strerror(errno));
60efcad6b7SDag-Erling Smørgrav 		return (-1);
61efcad6b7SDag-Erling Smørgrav 	}
62efcad6b7SDag-Erling Smørgrav 	return (0);
63545d5ecaSDag-Erling Smørgrav }
64545d5ecaSDag-Erling Smørgrav 
65545d5ecaSDag-Erling Smørgrav int
ssh_msg_recv(int fd,struct sshbuf * m)66bc5531deSDag-Erling Smørgrav ssh_msg_recv(int fd, struct sshbuf *m)
67545d5ecaSDag-Erling Smørgrav {
68bc5531deSDag-Erling Smørgrav 	u_char buf[4], *p;
69545d5ecaSDag-Erling Smørgrav 	u_int msg_len;
70bc5531deSDag-Erling Smørgrav 	int r;
71545d5ecaSDag-Erling Smørgrav 
724b17dab0SDag-Erling Smørgrav 	debug3("ssh_msg_recv entering");
73545d5ecaSDag-Erling Smørgrav 
74043840dfSDag-Erling Smørgrav 	if (atomicio(read, fd, buf, sizeof(buf)) != sizeof(buf)) {
75043840dfSDag-Erling Smørgrav 		if (errno != EPIPE)
76*19261079SEd Maste 			error_f("read header: %s", strerror(errno));
77efcad6b7SDag-Erling Smørgrav 		return (-1);
78545d5ecaSDag-Erling Smørgrav 	}
79761efaa7SDag-Erling Smørgrav 	msg_len = get_u32(buf);
80*19261079SEd Maste 	if (msg_len > sshbuf_max_size(m)) {
81*19261079SEd Maste 		error_f("read: bad msg_len %u", msg_len);
82efcad6b7SDag-Erling Smørgrav 		return (-1);
83efcad6b7SDag-Erling Smørgrav 	}
84bc5531deSDag-Erling Smørgrav 	sshbuf_reset(m);
85bc5531deSDag-Erling Smørgrav 	if ((r = sshbuf_reserve(m, msg_len, &p)) != 0) {
86*19261079SEd Maste 		error_fr(r, "reserve");
87bc5531deSDag-Erling Smørgrav 		return -1;
88bc5531deSDag-Erling Smørgrav 	}
89bc5531deSDag-Erling Smørgrav 	if (atomicio(read, fd, p, msg_len) != msg_len) {
90*19261079SEd Maste 		error_f("read: %s", strerror(errno));
91efcad6b7SDag-Erling Smørgrav 		return (-1);
92efcad6b7SDag-Erling Smørgrav 	}
93efcad6b7SDag-Erling Smørgrav 	return (0);
94545d5ecaSDag-Erling Smørgrav }
95