buffer.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. if (size > 0) {
  43. buf->data = (unsigned char*)buf + sizeof(buffer);
  44. } else {
  45. buf->data = NULL;
  46. }
  47. buf->size = size;
  48. return buf;
  49. }
  50. /* free the buffer's data and the buffer itself */
  51. void buf_free(buffer* buf) {
  52. m_free(buf);
  53. }
  54. /* overwrite the contents of the buffer to clear it */
  55. void buf_burn(buffer* buf) {
  56. m_burn(buf->data, buf->size);
  57. }
  58. /* resize a buffer, pos and len will be repositioned if required when
  59. * downsizing */
  60. buffer* buf_resize(buffer *buf, unsigned int newsize) {
  61. if (newsize > BUF_MAX_SIZE) {
  62. dropbear_exit("buf->size too big");
  63. }
  64. buf = m_realloc(buf, sizeof(buffer)+newsize);
  65. buf->data = (unsigned char*)buf + sizeof(buffer);
  66. buf->size = newsize;
  67. buf->len = MIN(newsize, buf->len);
  68. buf->pos = MIN(newsize, buf->pos);
  69. return buf;
  70. }
  71. /* Create a copy of buf, allocating required memory etc. */
  72. /* The new buffer is sized the same as the length of the source buffer. */
  73. buffer* buf_newcopy(buffer* buf) {
  74. buffer* ret;
  75. ret = buf_new(buf->len);
  76. ret->len = buf->len;
  77. if (buf->len > 0) {
  78. memcpy(ret->data, buf->data, buf->len);
  79. }
  80. return ret;
  81. }
  82. /* Set the length of the buffer */
  83. void buf_setlen(buffer* buf, unsigned int len) {
  84. if (len > buf->size) {
  85. dropbear_exit("Bad buf_setlen");
  86. }
  87. buf->len = len;
  88. buf->pos = MIN(buf->pos, buf->len);
  89. }
  90. /* Increment the length of the buffer */
  91. void buf_incrlen(buffer* buf, unsigned int incr) {
  92. if (incr > BUF_MAX_INCR || buf->len + incr > buf->size) {
  93. dropbear_exit("Bad buf_incrlen");
  94. }
  95. buf->len += incr;
  96. }
  97. /* Set the position of the buffer */
  98. void buf_setpos(buffer* buf, unsigned int pos) {
  99. if (pos > buf->len) {
  100. dropbear_exit("Bad buf_setpos");
  101. }
  102. buf->pos = pos;
  103. }
  104. /* increment the position by incr, increasing the buffer length if required */
  105. void buf_incrwritepos(buffer* buf, unsigned int incr) {
  106. if (incr > BUF_MAX_INCR || buf->pos + incr > buf->size) {
  107. dropbear_exit("Bad buf_incrwritepos");
  108. }
  109. buf->pos += incr;
  110. if (buf->pos > buf->len) {
  111. buf->len = buf->pos;
  112. }
  113. }
  114. /* increment the position by incr, negative values are allowed, to
  115. * decrement the pos*/
  116. void buf_incrpos(buffer* buf, int incr) {
  117. if (incr > BUF_MAX_INCR
  118. || incr < -BUF_MAX_INCR
  119. || (unsigned int)((int)buf->pos + incr) > buf->len
  120. || ((int)buf->pos + incr) < 0) {
  121. dropbear_exit("Bad buf_incrpos");
  122. }
  123. buf->pos += incr;
  124. }
  125. /* Get a byte from the buffer and increment the pos */
  126. unsigned char buf_getbyte(buffer* buf) {
  127. /* This check is really just ==, but the >= allows us to check for the
  128. * bad case of pos > len, which should _never_ happen. */
  129. if (buf->pos >= buf->len) {
  130. dropbear_exit("Bad buf_getbyte");
  131. }
  132. return buf->data[buf->pos++];
  133. }
  134. /* Get a bool from the buffer and increment the pos */
  135. unsigned char buf_getbool(buffer* buf) {
  136. unsigned char b;
  137. b = buf_getbyte(buf);
  138. if (b != 0)
  139. b = 1;
  140. return b;
  141. }
  142. /* put a byte, incrementing the length if required */
  143. void buf_putbyte(buffer* buf, unsigned char val) {
  144. if (buf->pos >= buf->len) {
  145. buf_incrlen(buf, 1);
  146. }
  147. buf->data[buf->pos] = val;
  148. buf->pos++;
  149. }
  150. /* returns an in-place pointer to the buffer, checking that
  151. * the next len bytes from that position can be used */
  152. unsigned char* buf_getptr(buffer* buf, unsigned int len) {
  153. if (len > BUF_MAX_INCR || buf->pos + len > buf->len) {
  154. dropbear_exit("Bad buf_getptr");
  155. }
  156. return &buf->data[buf->pos];
  157. }
  158. /* like buf_getptr, but checks against total size, not used length.
  159. * This allows writing past the used length, but not past the size */
  160. unsigned char* buf_getwriteptr(buffer* buf, unsigned int len) {
  161. if (len > BUF_MAX_INCR || buf->pos + len > buf->size) {
  162. dropbear_exit("Bad buf_getwriteptr");
  163. }
  164. return &buf->data[buf->pos];
  165. }
  166. /* Return a null-terminated string, it is malloced, so must be free()ed
  167. * Note that the string isn't checked for null bytes, hence the retlen
  168. * may be longer than what is returned by strlen */
  169. char* buf_getstring(buffer* buf, unsigned int *retlen) {
  170. unsigned int len;
  171. char* ret;
  172. len = buf_getint(buf);
  173. if (len > MAX_STRING_LEN) {
  174. dropbear_exit("String too long");
  175. }
  176. if (retlen != NULL) {
  177. *retlen = len;
  178. }
  179. ret = m_malloc(len+1);
  180. memcpy(ret, buf_getptr(buf, len), len);
  181. buf_incrpos(buf, len);
  182. ret[len] = '\0';
  183. return ret;
  184. }
  185. /* Return a string as a newly allocated buffer */
  186. buffer * buf_getstringbuf(buffer *buf) {
  187. buffer *ret = NULL;
  188. unsigned int len = buf_getint(buf);
  189. if (len > MAX_STRING_LEN) {
  190. dropbear_exit("String too long");
  191. }
  192. ret = buf_new(len);
  193. memcpy(buf_getwriteptr(ret, len), buf_getptr(buf, len), len);
  194. buf_incrpos(buf, len);
  195. buf_incrlen(ret, len);
  196. return ret;
  197. }
  198. /* Just increment the buffer position the same as if we'd used buf_getstring,
  199. * but don't bother copying/malloc()ing for it */
  200. void buf_eatstring(buffer *buf) {
  201. buf_incrpos( buf, buf_getint(buf) );
  202. }
  203. /* Get an uint32 from the buffer and increment the pos */
  204. unsigned int buf_getint(buffer* buf) {
  205. unsigned int ret;
  206. LOAD32H(ret, buf_getptr(buf, 4));
  207. buf_incrpos(buf, 4);
  208. return ret;
  209. }
  210. /* put a 32bit uint into the buffer, incr bufferlen & pos if required */
  211. void buf_putint(buffer* buf, int unsigned val) {
  212. STORE32H(val, buf_getwriteptr(buf, 4));
  213. buf_incrwritepos(buf, 4);
  214. }
  215. /* put a SSH style string into the buffer, increasing buffer len if required */
  216. void buf_putstring(buffer* buf, const char* str, unsigned int len) {
  217. buf_putint(buf, len);
  218. buf_putbytes(buf, (const unsigned char*)str, len);
  219. }
  220. /* puts an entire buffer as a SSH string. ignore pos of buf_str. */
  221. void buf_putbufstring(buffer *buf, const buffer* buf_str) {
  222. buf_putstring(buf, (const char*)buf_str->data, buf_str->len);
  223. }
  224. /* put the set of len bytes into the buffer, incrementing the pos, increasing
  225. * len if required */
  226. void buf_putbytes(buffer *buf, const unsigned char *bytes, unsigned int len) {
  227. memcpy(buf_getwriteptr(buf, len), bytes, len);
  228. buf_incrwritepos(buf, len);
  229. }
  230. /* for our purposes we only need positive (or 0) numbers, so will
  231. * fail if we get negative numbers */
  232. void buf_putmpint(buffer* buf, mp_int * mp) {
  233. unsigned int len, pad = 0;
  234. TRACE2(("enter buf_putmpint"))
  235. dropbear_assert(mp != NULL);
  236. if (SIGN(mp) == MP_NEG) {
  237. dropbear_exit("negative bignum");
  238. }
  239. /* zero check */
  240. if (USED(mp) == 1 && DIGIT(mp, 0) == 0) {
  241. len = 0;
  242. } else {
  243. /* SSH spec requires padding for mpints with the MSB set, this code
  244. * implements it */
  245. len = mp_count_bits(mp);
  246. /* if the top bit of MSB is set, we need to pad */
  247. pad = (len%8 == 0) ? 1 : 0;
  248. len = len / 8 + 1; /* don't worry about rounding, we need it for
  249. padding anyway when len%8 == 0 */
  250. }
  251. /* store the length */
  252. buf_putint(buf, len);
  253. /* store the actual value */
  254. if (len > 0) {
  255. if (pad) {
  256. buf_putbyte(buf, 0x00);
  257. }
  258. if (mp_to_unsigned_bin(mp, buf_getwriteptr(buf, len-pad)) != MP_OKAY) {
  259. dropbear_exit("mpint error");
  260. }
  261. buf_incrwritepos(buf, len-pad);
  262. }
  263. TRACE2(("leave buf_putmpint"))
  264. }
  265. /* Retrieve an mp_int from the buffer.
  266. * Will fail for -ve since they shouldn't be required here.
  267. * Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */
  268. int buf_getmpint(buffer* buf, mp_int* mp) {
  269. unsigned int len;
  270. len = buf_getint(buf);
  271. if (len == 0) {
  272. mp_zero(mp);
  273. return DROPBEAR_SUCCESS;
  274. }
  275. if (len > BUF_MAX_MPINT) {
  276. return DROPBEAR_FAILURE;
  277. }
  278. /* check for negative */
  279. if (*buf_getptr(buf, 1) & (1 << (CHAR_BIT-1))) {
  280. return DROPBEAR_FAILURE;
  281. }
  282. if (mp_read_unsigned_bin(mp, buf_getptr(buf, len), len) != MP_OKAY) {
  283. return DROPBEAR_FAILURE;
  284. }
  285. buf_incrpos(buf, len);
  286. return DROPBEAR_SUCCESS;
  287. }