summaryrefslogtreecommitdiff
path: root/sshbuf-getput-basic.c
diff options
context:
space:
mode:
authorColin Watson <cjwatson@debian.org>2019-10-09 22:59:48 +0100
committerColin Watson <cjwatson@debian.org>2019-10-09 22:59:48 +0100
commit4213eec74e74de6310c27a40c3e9759a08a73996 (patch)
treee97a6dcafc6763aea7c804e4e113c2750cb1400d /sshbuf-getput-basic.c
parent102062f825fb26a74295a1c089c00c4c4c76b68a (diff)
parentcdf1d0a9f5d18535e0a18ff34860e81a6d83aa5c (diff)
Import openssh_8.1p1.orig.tar.gz
Diffstat (limited to 'sshbuf-getput-basic.c')
-rw-r--r--sshbuf-getput-basic.c166
1 files changed, 165 insertions, 1 deletions
diff --git a/sshbuf-getput-basic.c b/sshbuf-getput-basic.c
index 50648258f..d401a7265 100644
--- a/sshbuf-getput-basic.c
+++ b/sshbuf-getput-basic.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: sshbuf-getput-basic.c,v 1.7 2017/06/01 04:51:58 djm Exp $ */ 1/* $OpenBSD: sshbuf-getput-basic.c,v 1.9 2019/09/06 04:53:27 djm Exp $ */
2/* 2/*
3 * Copyright (c) 2011 Damien Miller 3 * Copyright (c) 2011 Damien Miller
4 * 4 *
@@ -24,6 +24,9 @@
24#include <stdlib.h> 24#include <stdlib.h>
25#include <stdio.h> 25#include <stdio.h>
26#include <string.h> 26#include <string.h>
27#ifdef HAVE_STDINT_H
28# include <stdint.h>
29#endif
27 30
28#include "ssherr.h" 31#include "ssherr.h"
29#include "sshbuf.h" 32#include "sshbuf.h"
@@ -93,6 +96,93 @@ sshbuf_get_u8(struct sshbuf *buf, u_char *valp)
93 return 0; 96 return 0;
94} 97}
95 98
99static int
100check_offset(const struct sshbuf *buf, int wr, size_t offset, size_t len)
101{
102 if (sshbuf_ptr(buf) == NULL) /* calls sshbuf_check_sanity() */
103 return SSH_ERR_INTERNAL_ERROR;
104 if (offset >= SIZE_MAX - len)
105 return SSH_ERR_INVALID_ARGUMENT;
106 if (offset + len > sshbuf_len(buf)) {
107 return wr ?
108 SSH_ERR_NO_BUFFER_SPACE : SSH_ERR_MESSAGE_INCOMPLETE;
109 }
110 return 0;
111}
112
113static int
114check_roffset(const struct sshbuf *buf, size_t offset, size_t len,
115 const u_char **p)
116{
117 int r;
118
119 *p = NULL;
120 if ((r = check_offset(buf, 0, offset, len)) != 0)
121 return r;
122 *p = sshbuf_ptr(buf) + offset;
123 return 0;
124}
125
126int
127sshbuf_peek_u64(const struct sshbuf *buf, size_t offset, u_int64_t *valp)
128{
129 const u_char *p = NULL;
130 int r;
131
132 if (valp != NULL)
133 *valp = 0;
134 if ((r = check_roffset(buf, offset, 8, &p)) != 0)
135 return r;
136 if (valp != NULL)
137 *valp = PEEK_U64(p);
138 return 0;
139}
140
141int
142sshbuf_peek_u32(const struct sshbuf *buf, size_t offset, u_int32_t *valp)
143{
144 const u_char *p = NULL;
145 int r;
146
147 if (valp != NULL)
148 *valp = 0;
149 if ((r = check_roffset(buf, offset, 4, &p)) != 0)
150 return r;
151 if (valp != NULL)
152 *valp = PEEK_U32(p);
153 return 0;
154}
155
156int
157sshbuf_peek_u16(const struct sshbuf *buf, size_t offset, u_int16_t *valp)
158{
159 const u_char *p = NULL;
160 int r;
161
162 if (valp != NULL)
163 *valp = 0;
164 if ((r = check_roffset(buf, offset, 2, &p)) != 0)
165 return r;
166 if (valp != NULL)
167 *valp = PEEK_U16(p);
168 return 0;
169}
170
171int
172sshbuf_peek_u8(const struct sshbuf *buf, size_t offset, u_char *valp)
173{
174 const u_char *p = NULL;
175 int r;
176
177 if (valp != NULL)
178 *valp = 0;
179 if ((r = check_roffset(buf, offset, 1, &p)) != 0)
180 return r;
181 if (valp != NULL)
182 *valp = *p;
183 return 0;
184}
185
96int 186int
97sshbuf_get_string(struct sshbuf *buf, u_char **valp, size_t *lenp) 187sshbuf_get_string(struct sshbuf *buf, u_char **valp, size_t *lenp)
98{ 188{
@@ -344,6 +434,80 @@ sshbuf_put_u8(struct sshbuf *buf, u_char val)
344 return 0; 434 return 0;
345} 435}
346 436
437static int
438check_woffset(struct sshbuf *buf, size_t offset, size_t len, u_char **p)
439{
440 int r;
441
442 *p = NULL;
443 if ((r = check_offset(buf, 1, offset, len)) != 0)
444 return r;
445 if (sshbuf_mutable_ptr(buf) == NULL)
446 return SSH_ERR_BUFFER_READ_ONLY;
447 *p = sshbuf_mutable_ptr(buf) + offset;
448 return 0;
449}
450
451int
452sshbuf_poke_u64(struct sshbuf *buf, size_t offset, u_int64_t val)
453{
454 u_char *p = NULL;
455 int r;
456
457 if ((r = check_woffset(buf, offset, 8, &p)) != 0)
458 return r;
459 POKE_U64(p, val);
460 return 0;
461}
462
463int
464sshbuf_poke_u32(struct sshbuf *buf, size_t offset, u_int32_t val)
465{
466 u_char *p = NULL;
467 int r;
468
469 if ((r = check_woffset(buf, offset, 4, &p)) != 0)
470 return r;
471 POKE_U32(p, val);
472 return 0;
473}
474
475int
476sshbuf_poke_u16(struct sshbuf *buf, size_t offset, u_int16_t val)
477{
478 u_char *p = NULL;
479 int r;
480
481 if ((r = check_woffset(buf, offset, 2, &p)) != 0)
482 return r;
483 POKE_U16(p, val);
484 return 0;
485}
486
487int
488sshbuf_poke_u8(struct sshbuf *buf, size_t offset, u_char val)
489{
490 u_char *p = NULL;
491 int r;
492
493 if ((r = check_woffset(buf, offset, 1, &p)) != 0)
494 return r;
495 *p = val;
496 return 0;
497}
498
499int
500sshbuf_poke(struct sshbuf *buf, size_t offset, void *v, size_t len)
501{
502 u_char *p = NULL;
503 int r;
504
505 if ((r = check_woffset(buf, offset, len, &p)) != 0)
506 return r;
507 memcpy(p, v, len);
508 return 0;
509}
510
347int 511int
348sshbuf_put_string(struct sshbuf *buf, const void *v, size_t len) 512sshbuf_put_string(struct sshbuf *buf, const void *v, size_t len)
349{ 513{