buffer.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. * Dropbear SSH
  3. *
  4. * Copyright (c) 2002,2003 Matt Johnston
  5. * All rights reserved.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. * SOFTWARE. */
  24. /* Buffer handling routines, designed to avoid overflows/using invalid data */
  25. #include "includes.h"
  26. #include "dbutil.h"
  27. #include "buffer.h"
  28. /* Prevent integer overflows when incrementing buffer position/length.
  29. * Calling functions should check arguments first, but this provides a
  30. * backstop */
  31. #define BUF_MAX_INCR 1000000000
  32. #define BUF_MAX_SIZE 1000000000
  33. /* avoid excessively large numbers, > ~8192 bits */
  34. #define BUF_MAX_MPINT (8240 / 8)
  35. /* Create (malloc) a new buffer of size */
  36. buffer* buf_new(unsigned int size) {
  37. buffer* buf;
  38. if (size > BUF_MAX_SIZE) {
  39. dropbear_exit("buf->size too big");
  40. }
  41. buf = (buffer*)m_malloc(sizeof(buffer)+size);
  42. buf->data = (unsigned char*)buf + sizeof(buffer);
  43. buf->size = size;
  44. return buf;
  45. }
  46. /* free the buffer's data and the buffer itself */
  47. void buf_free(buffer* buf) {
  48. m_free(buf);
  49. }
  50. /* overwrite the contents of the buffer then free it */
  51. void buf_burn_free(buffer* buf) {
  52. m_burn(buf->data, buf->size);
  53. m_free(buf);
  54. }
  55. /* resize a buffer, pos and len will be repositioned if required when
  56. * downsizing */
  57. buffer* buf_resize(buffer *buf, unsigned int newsize) {
  58. if (newsize > BUF_MAX_SIZE) {
  59. dropbear_exit("buf->size too big");
  60. }
  61. buf = m_realloc(buf, sizeof(buffer)+newsize);
  62. buf->data = (unsigned char*)buf + sizeof(buffer);
  63. buf->size = newsize;
  64. buf->len = MIN(newsize, buf->len);
  65. buf->pos = MIN(newsize, buf->pos);
  66. return buf;
  67. }
  68. /* Create a copy of buf, allocating required memory etc. */
  69. /* The new buffer is sized the same as the length of the source buffer. */
  70. buffer* buf_newcopy(const buffer* buf) {
  71. buffer* ret;
  72. ret = buf_new(buf->len);
  73. ret->len = buf->len;
  74. if (buf->len > 0) {
  75. memcpy(ret->data, buf->data, buf->len);
  76. }
  77. return ret;
  78. }
  79. /* Set the length of the buffer */
  80. void buf_setlen(buffer* buf, unsigned int len) {
  81. if (len > buf->size) {
  82. dropbear_exit("Bad buf_setlen");
  83. }
  84. buf->len = len;
  85. buf->pos = MIN(buf->pos, buf->len);
  86. }
  87. /* Increment the length of the buffer */
  88. void buf_incrlen(buffer* buf, unsigned int incr) {
  89. if (incr > BUF_MAX_INCR || buf->len + incr > buf->size) {
  90. dropbear_exit("Bad buf_incrlen");
  91. }
  92. buf->len += incr;
  93. }
  94. /* Set the position of the buffer */
  95. void buf_setpos(buffer* buf, unsigned int pos) {
  96. if (pos > buf->len) {
  97. dropbear_exit("Bad buf_setpos");
  98. }
  99. buf->pos = pos;
  100. }
  101. /* increment the position by incr, increasing the buffer length if required */
  102. void buf_incrwritepos(buffer* buf, unsigned int incr) {
  103. if (incr > BUF_MAX_INCR || buf->pos + incr > buf->size) {
  104. dropbear_exit("Bad buf_incrwritepos");
  105. }
  106. buf->pos += incr;
  107. if (buf->pos > buf->len) {
  108. buf->len = buf->pos;
  109. }
  110. }
  111. /* increment the position by incr */
  112. void buf_incrpos(buffer* buf, unsigned int incr) {
  113. if (incr > BUF_MAX_INCR
  114. || (buf->pos + incr) > buf->len) {
  115. dropbear_exit("Bad buf_incrpos");
  116. }
  117. buf->pos += incr;
  118. }
  119. /* decrement the position by decr */
  120. void buf_decrpos(buffer* buf, unsigned int decr) {
  121. if (decr > buf->pos) {
  122. dropbear_exit("Bad buf_decrpos");
  123. }
  124. buf->pos -= decr;
  125. }
  126. /* Get a byte from the buffer and increment the pos */
  127. unsigned char buf_getbyte(buffer* buf) {
  128. /* This check is really just ==, but the >= allows us to check for the
  129. * bad case of pos > len, which should _never_ happen. */
  130. if (buf->pos >= buf->len) {
  131. dropbear_exit("Bad buf_getbyte");
  132. }
  133. return buf->data[buf->pos++];
  134. }
  135. /* Get a bool from the buffer and increment the pos */
  136. unsigned char buf_getbool(buffer* buf) {
  137. unsigned char b;
  138. b = buf_getbyte(buf);
  139. if (b != 0)
  140. b = 1;
  141. return b;
  142. }
  143. /* put a byte, incrementing the length if required */
  144. void buf_putbyte(buffer* buf, unsigned char val) {
  145. if (buf->pos >= buf->len) {
  146. buf_incrlen(buf, 1);
  147. }
  148. buf->data[buf->pos] = val;
  149. buf->pos++;
  150. }
  151. /* returns an in-place pointer to the buffer, checking that
  152. * the next len bytes from that position can be used */
  153. unsigned char* buf_getptr(const buffer* buf, unsigned int len) {
  154. if (len > BUF_MAX_INCR || buf->pos + len > buf->len) {
  155. dropbear_exit("Bad buf_getptr");
  156. }
  157. return &buf->data[buf->pos];
  158. }
  159. /* like buf_getptr, but checks against total size, not used length.
  160. * This allows writing past the used length, but not past the size */
  161. unsigned char* buf_getwriteptr(const buffer* buf, unsigned int len) {
  162. if (len > BUF_MAX_INCR || buf->pos + len > buf->size) {
  163. dropbear_exit("Bad buf_getwriteptr");
  164. }
  165. return &buf->data[buf->pos];
  166. }
  167. /* Return a null-terminated string, it is malloced, so must be free()ed
  168. * Note that the string isn't checked for null bytes, hence the retlen
  169. * may be longer than what is returned by strlen */
  170. char* buf_getstring(buffer* buf, unsigned int *retlen) {
  171. unsigned int len;
  172. char* ret;
  173. void* src = NULL;
  174. len = buf_getint(buf);
  175. if (len > MAX_STRING_LEN) {
  176. dropbear_exit("String too long");
  177. }
  178. if (retlen != NULL) {
  179. *retlen = len;
  180. }
  181. src = buf_getptr(buf, len);
  182. ret = m_malloc(len+1);
  183. memcpy(ret, src, len);
  184. buf_incrpos(buf, len);
  185. ret[len] = '\0';
  186. return ret;
  187. }
  188. /* Return a string as a newly allocated buffer */
  189. static buffer * buf_getstringbuf_int(buffer *buf, int incllen) {
  190. buffer *ret = NULL;
  191. unsigned int len = buf_getint(buf);
  192. int extra = 0;
  193. if (len > MAX_STRING_LEN) {
  194. dropbear_exit("String too long");
  195. }
  196. if (incllen) {
  197. extra = 4;
  198. }
  199. ret = buf_new(len+extra);
  200. if (incllen) {
  201. buf_putint(ret, len);
  202. }
  203. memcpy(buf_getwriteptr(ret, len), buf_getptr(buf, len), len);
  204. buf_incrpos(buf, len);
  205. buf_incrlen(ret, len);
  206. buf_setpos(ret, 0);
  207. return ret;
  208. }
  209. /* Return a string as a newly allocated buffer */
  210. buffer * buf_getstringbuf(buffer *buf) {
  211. return buf_getstringbuf_int(buf, 0);
  212. }
  213. /* Returns a string in a new buffer, including the length */
  214. buffer * buf_getbuf(buffer *buf) {
  215. return buf_getstringbuf_int(buf, 1);
  216. }
  217. /* Just increment the buffer position the same as if we'd used buf_getstring,
  218. * but don't bother copying/malloc()ing for it */
  219. void buf_eatstring(buffer *buf) {
  220. buf_incrpos( buf, buf_getint(buf) );
  221. }
  222. /* Get an uint32 from the buffer and increment the pos */
  223. unsigned int buf_getint(buffer* buf) {
  224. unsigned int ret;
  225. LOAD32H(ret, buf_getptr(buf, 4));
  226. buf_incrpos(buf, 4);
  227. return ret;
  228. }
  229. /* put a 32bit uint into the buffer, incr bufferlen & pos if required */
  230. void buf_putint(buffer* buf, int unsigned val) {
  231. STORE32H(val, buf_getwriteptr(buf, 4));
  232. buf_incrwritepos(buf, 4);
  233. }
  234. /* put a SSH style string into the buffer, increasing buffer len if required */
  235. void buf_putstring(buffer* buf, const char* str, unsigned int len) {
  236. buf_putint(buf, len);
  237. buf_putbytes(buf, (const unsigned char*)str, len);
  238. }
  239. /* puts an entire buffer as a SSH string. ignore pos of buf_str. */
  240. void buf_putbufstring(buffer *buf, const buffer* buf_str) {
  241. buf_putstring(buf, (const char*)buf_str->data, buf_str->len);
  242. }
  243. /* put the set of len bytes into the buffer, incrementing the pos, increasing
  244. * len if required */
  245. void buf_putbytes(buffer *buf, const unsigned char *bytes, unsigned int len) {
  246. memcpy(buf_getwriteptr(buf, len), bytes, len);
  247. buf_incrwritepos(buf, len);
  248. }
  249. /* for our purposes we only need positive (or 0) numbers, so will
  250. * fail if we get negative numbers */
  251. void buf_putmpint(buffer* buf, mp_int * mp) {
  252. size_t written;
  253. unsigned int len, pad = 0;
  254. TRACE2(("enter buf_putmpint"))
  255. dropbear_assert(mp != NULL);
  256. if (mp_isneg(mp)) {
  257. dropbear_exit("negative bignum");
  258. }
  259. /* zero check */
  260. if (mp_iszero(mp)) {
  261. len = 0;
  262. } else {
  263. /* SSH spec requires padding for mpints with the MSB set, this code
  264. * implements it */
  265. len = mp_count_bits(mp);
  266. /* if the top bit of MSB is set, we need to pad */
  267. pad = (len%8 == 0) ? 1 : 0;
  268. len = len / 8 + 1; /* don't worry about rounding, we need it for
  269. padding anyway when len%8 == 0 */
  270. }
  271. /* store the length */
  272. buf_putint(buf, len);
  273. /* store the actual value */
  274. if (len > 0) {
  275. if (pad) {
  276. buf_putbyte(buf, 0x00);
  277. }
  278. if (mp_to_ubin(mp, buf_getwriteptr(buf, len-pad), len-pad, &written) != MP_OKAY) {
  279. dropbear_exit("mpint error");
  280. }
  281. buf_incrwritepos(buf, written);
  282. }
  283. TRACE2(("leave buf_putmpint"))
  284. }
  285. /* Retrieve an mp_int from the buffer.
  286. * Will fail for -ve since they shouldn't be required here.
  287. * Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */
  288. int buf_getmpint(buffer* buf, mp_int* mp) {
  289. unsigned int len;
  290. len = buf_getint(buf);
  291. if (len == 0) {
  292. mp_zero(mp);
  293. return DROPBEAR_SUCCESS;
  294. }
  295. if (len > BUF_MAX_MPINT) {
  296. return DROPBEAR_FAILURE;
  297. }
  298. /* check for negative */
  299. if (*buf_getptr(buf, 1) & (1 << (CHAR_BIT-1))) {
  300. return DROPBEAR_FAILURE;
  301. }
  302. if (mp_from_ubin(mp, buf_getptr(buf, len), len) != MP_OKAY) {
  303. return DROPBEAR_FAILURE;
  304. }
  305. buf_incrpos(buf, len);
  306. return DROPBEAR_SUCCESS;
  307. }