diff options
author | Colin Watson <cjwatson@debian.org> | 2019-10-09 22:59:48 +0100 |
---|---|---|
committer | Colin Watson <cjwatson@debian.org> | 2019-10-09 23:39:39 +0100 |
commit | 767ee84d3465b6d244a9108de5c167a9ab866df9 (patch) | |
tree | 69b14ef6a62d7f133298a21d2ad6046f130b7801 /sshbuf-getput-basic.c | |
parent | ddeaf9ee7d5c6612b88f1c4a83fc6fbccb93bf60 (diff) | |
parent | efef12825b9582c1710da3b7e50135870963d4f4 (diff) |
New upstream release (8.1p1)
Diffstat (limited to 'sshbuf-getput-basic.c')
-rw-r--r-- | sshbuf-getput-basic.c | 166 |
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 | ||
99 | static int | ||
100 | check_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 | |||
113 | static int | ||
114 | check_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 | |||
126 | int | ||
127 | sshbuf_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 | |||
141 | int | ||
142 | sshbuf_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 | |||
156 | int | ||
157 | sshbuf_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 | |||
171 | int | ||
172 | sshbuf_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 | |||
96 | int | 186 | int |
97 | sshbuf_get_string(struct sshbuf *buf, u_char **valp, size_t *lenp) | 187 | sshbuf_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 | ||
437 | static int | ||
438 | check_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 | |||
451 | int | ||
452 | sshbuf_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 | |||
463 | int | ||
464 | sshbuf_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 | |||
475 | int | ||
476 | sshbuf_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 | |||
487 | int | ||
488 | sshbuf_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 | |||
499 | int | ||
500 | sshbuf_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 | |||
347 | int | 511 | int |
348 | sshbuf_put_string(struct sshbuf *buf, const void *v, size_t len) | 512 | sshbuf_put_string(struct sshbuf *buf, const void *v, size_t len) |
349 | { | 513 | { |