packet.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*
  2. * Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include "internal/cryptlib.h"
  10. #include "packet_locl.h"
  11. #define DEFAULT_BUF_SIZE 256
  12. int WPACKET_allocate_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
  13. {
  14. if (!WPACKET_reserve_bytes(pkt, len, allocbytes))
  15. return 0;
  16. pkt->written += len;
  17. pkt->curr += len;
  18. return 1;
  19. }
  20. int WPACKET_sub_allocate_bytes__(WPACKET *pkt, size_t len,
  21. unsigned char **allocbytes, size_t lenbytes)
  22. {
  23. if (!WPACKET_start_sub_packet_len__(pkt, lenbytes)
  24. || !WPACKET_allocate_bytes(pkt, len, allocbytes)
  25. || !WPACKET_close(pkt))
  26. return 0;
  27. return 1;
  28. }
  29. #define GETBUF(p) (((p)->staticbuf != NULL) \
  30. ? (p)->staticbuf : (unsigned char *)(p)->buf->data)
  31. int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
  32. {
  33. /* Internal API, so should not fail */
  34. if (!ossl_assert(pkt->subs != NULL && len != 0))
  35. return 0;
  36. if (pkt->maxsize - pkt->written < len)
  37. return 0;
  38. if (pkt->staticbuf == NULL && (pkt->buf->length - pkt->written < len)) {
  39. size_t newlen;
  40. size_t reflen;
  41. reflen = (len > pkt->buf->length) ? len : pkt->buf->length;
  42. if (reflen > SIZE_MAX / 2) {
  43. newlen = SIZE_MAX;
  44. } else {
  45. newlen = reflen * 2;
  46. if (newlen < DEFAULT_BUF_SIZE)
  47. newlen = DEFAULT_BUF_SIZE;
  48. }
  49. if (BUF_MEM_grow(pkt->buf, newlen) == 0)
  50. return 0;
  51. }
  52. if (allocbytes != NULL)
  53. *allocbytes = WPACKET_get_curr(pkt);
  54. return 1;
  55. }
  56. int WPACKET_sub_reserve_bytes__(WPACKET *pkt, size_t len,
  57. unsigned char **allocbytes, size_t lenbytes)
  58. {
  59. if (!WPACKET_reserve_bytes(pkt, lenbytes + len, allocbytes))
  60. return 0;
  61. *allocbytes += lenbytes;
  62. return 1;
  63. }
  64. static size_t maxmaxsize(size_t lenbytes)
  65. {
  66. if (lenbytes >= sizeof(size_t) || lenbytes == 0)
  67. return SIZE_MAX;
  68. return ((size_t)1 << (lenbytes * 8)) - 1 + lenbytes;
  69. }
  70. static int wpacket_intern_init_len(WPACKET *pkt, size_t lenbytes)
  71. {
  72. unsigned char *lenchars;
  73. pkt->curr = 0;
  74. pkt->written = 0;
  75. pkt->subs = OPENSSL_zalloc(sizeof(*pkt->subs));
  76. if (pkt->subs == NULL)
  77. return 0;
  78. if (lenbytes == 0)
  79. return 1;
  80. pkt->subs->pwritten = lenbytes;
  81. pkt->subs->lenbytes = lenbytes;
  82. if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars)) {
  83. OPENSSL_free(pkt->subs);
  84. pkt->subs = NULL;
  85. return 0;
  86. }
  87. pkt->subs->packet_len = lenchars - GETBUF(pkt);
  88. return 1;
  89. }
  90. int WPACKET_init_static_len(WPACKET *pkt, unsigned char *buf, size_t len,
  91. size_t lenbytes)
  92. {
  93. size_t max = maxmaxsize(lenbytes);
  94. /* Internal API, so should not fail */
  95. if (!ossl_assert(buf != NULL && len > 0))
  96. return 0;
  97. pkt->staticbuf = buf;
  98. pkt->buf = NULL;
  99. pkt->maxsize = (max < len) ? max : len;
  100. return wpacket_intern_init_len(pkt, lenbytes);
  101. }
  102. int WPACKET_init_len(WPACKET *pkt, BUF_MEM *buf, size_t lenbytes)
  103. {
  104. /* Internal API, so should not fail */
  105. if (!ossl_assert(buf != NULL))
  106. return 0;
  107. pkt->staticbuf = NULL;
  108. pkt->buf = buf;
  109. pkt->maxsize = maxmaxsize(lenbytes);
  110. return wpacket_intern_init_len(pkt, lenbytes);
  111. }
  112. int WPACKET_init(WPACKET *pkt, BUF_MEM *buf)
  113. {
  114. return WPACKET_init_len(pkt, buf, 0);
  115. }
  116. int WPACKET_set_flags(WPACKET *pkt, unsigned int flags)
  117. {
  118. /* Internal API, so should not fail */
  119. if (!ossl_assert(pkt->subs != NULL))
  120. return 0;
  121. pkt->subs->flags = flags;
  122. return 1;
  123. }
  124. /* Store the |value| of length |len| at location |data| */
  125. static int put_value(unsigned char *data, size_t value, size_t len)
  126. {
  127. for (data += len - 1; len > 0; len--) {
  128. *data = (unsigned char)(value & 0xff);
  129. data--;
  130. value >>= 8;
  131. }
  132. /* Check whether we could fit the value in the assigned number of bytes */
  133. if (value > 0)
  134. return 0;
  135. return 1;
  136. }
  137. /*
  138. * Internal helper function used by WPACKET_close(), WPACKET_finish() and
  139. * WPACKET_fill_lengths() to close a sub-packet and write out its length if
  140. * necessary. If |doclose| is 0 then it goes through the motions of closing
  141. * (i.e. it fills in all the lengths), but doesn't actually close anything.
  142. */
  143. static int wpacket_intern_close(WPACKET *pkt, WPACKET_SUB *sub, int doclose)
  144. {
  145. size_t packlen = pkt->written - sub->pwritten;
  146. if (packlen == 0
  147. && (sub->flags & WPACKET_FLAGS_NON_ZERO_LENGTH) != 0)
  148. return 0;
  149. if (packlen == 0
  150. && sub->flags & WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH) {
  151. /* We can't handle this case. Return an error */
  152. if (!doclose)
  153. return 0;
  154. /* Deallocate any bytes allocated for the length of the WPACKET */
  155. if ((pkt->curr - sub->lenbytes) == sub->packet_len) {
  156. pkt->written -= sub->lenbytes;
  157. pkt->curr -= sub->lenbytes;
  158. }
  159. /* Don't write out the packet length */
  160. sub->packet_len = 0;
  161. sub->lenbytes = 0;
  162. }
  163. /* Write out the WPACKET length if needed */
  164. if (sub->lenbytes > 0
  165. && !put_value(&GETBUF(pkt)[sub->packet_len], packlen,
  166. sub->lenbytes))
  167. return 0;
  168. if (doclose) {
  169. pkt->subs = sub->parent;
  170. OPENSSL_free(sub);
  171. }
  172. return 1;
  173. }
  174. int WPACKET_fill_lengths(WPACKET *pkt)
  175. {
  176. WPACKET_SUB *sub;
  177. if (!ossl_assert(pkt->subs != NULL))
  178. return 0;
  179. for (sub = pkt->subs; sub != NULL; sub = sub->parent) {
  180. if (!wpacket_intern_close(pkt, sub, 0))
  181. return 0;
  182. }
  183. return 1;
  184. }
  185. int WPACKET_close(WPACKET *pkt)
  186. {
  187. /*
  188. * Internal API, so should not fail - but we do negative testing of this
  189. * so no assert (otherwise the tests fail)
  190. */
  191. if (pkt->subs == NULL || pkt->subs->parent == NULL)
  192. return 0;
  193. return wpacket_intern_close(pkt, pkt->subs, 1);
  194. }
  195. int WPACKET_finish(WPACKET *pkt)
  196. {
  197. int ret;
  198. /*
  199. * Internal API, so should not fail - but we do negative testing of this
  200. * so no assert (otherwise the tests fail)
  201. */
  202. if (pkt->subs == NULL || pkt->subs->parent != NULL)
  203. return 0;
  204. ret = wpacket_intern_close(pkt, pkt->subs, 1);
  205. if (ret) {
  206. OPENSSL_free(pkt->subs);
  207. pkt->subs = NULL;
  208. }
  209. return ret;
  210. }
  211. int WPACKET_start_sub_packet_len__(WPACKET *pkt, size_t lenbytes)
  212. {
  213. WPACKET_SUB *sub;
  214. unsigned char *lenchars;
  215. /* Internal API, so should not fail */
  216. if (!ossl_assert(pkt->subs != NULL))
  217. return 0;
  218. sub = OPENSSL_zalloc(sizeof(*sub));
  219. if (sub == NULL)
  220. return 0;
  221. sub->parent = pkt->subs;
  222. pkt->subs = sub;
  223. sub->pwritten = pkt->written + lenbytes;
  224. sub->lenbytes = lenbytes;
  225. if (lenbytes == 0) {
  226. sub->packet_len = 0;
  227. return 1;
  228. }
  229. if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars))
  230. return 0;
  231. /* Convert to an offset in case the underlying BUF_MEM gets realloc'd */
  232. sub->packet_len = lenchars - GETBUF(pkt);
  233. return 1;
  234. }
  235. int WPACKET_start_sub_packet(WPACKET *pkt)
  236. {
  237. return WPACKET_start_sub_packet_len__(pkt, 0);
  238. }
  239. int WPACKET_put_bytes__(WPACKET *pkt, unsigned int val, size_t size)
  240. {
  241. unsigned char *data;
  242. /* Internal API, so should not fail */
  243. if (!ossl_assert(size <= sizeof(unsigned int))
  244. || !WPACKET_allocate_bytes(pkt, size, &data)
  245. || !put_value(data, val, size))
  246. return 0;
  247. return 1;
  248. }
  249. int WPACKET_set_max_size(WPACKET *pkt, size_t maxsize)
  250. {
  251. WPACKET_SUB *sub;
  252. size_t lenbytes;
  253. /* Internal API, so should not fail */
  254. if (!ossl_assert(pkt->subs != NULL))
  255. return 0;
  256. /* Find the WPACKET_SUB for the top level */
  257. for (sub = pkt->subs; sub->parent != NULL; sub = sub->parent)
  258. continue;
  259. lenbytes = sub->lenbytes;
  260. if (lenbytes == 0)
  261. lenbytes = sizeof(pkt->maxsize);
  262. if (maxmaxsize(lenbytes) < maxsize || maxsize < pkt->written)
  263. return 0;
  264. pkt->maxsize = maxsize;
  265. return 1;
  266. }
  267. int WPACKET_memset(WPACKET *pkt, int ch, size_t len)
  268. {
  269. unsigned char *dest;
  270. if (len == 0)
  271. return 1;
  272. if (!WPACKET_allocate_bytes(pkt, len, &dest))
  273. return 0;
  274. memset(dest, ch, len);
  275. return 1;
  276. }
  277. int WPACKET_memcpy(WPACKET *pkt, const void *src, size_t len)
  278. {
  279. unsigned char *dest;
  280. if (len == 0)
  281. return 1;
  282. if (!WPACKET_allocate_bytes(pkt, len, &dest))
  283. return 0;
  284. memcpy(dest, src, len);
  285. return 1;
  286. }
  287. int WPACKET_sub_memcpy__(WPACKET *pkt, const void *src, size_t len,
  288. size_t lenbytes)
  289. {
  290. if (!WPACKET_start_sub_packet_len__(pkt, lenbytes)
  291. || !WPACKET_memcpy(pkt, src, len)
  292. || !WPACKET_close(pkt))
  293. return 0;
  294. return 1;
  295. }
  296. int WPACKET_get_total_written(WPACKET *pkt, size_t *written)
  297. {
  298. /* Internal API, so should not fail */
  299. if (!ossl_assert(written != NULL))
  300. return 0;
  301. *written = pkt->written;
  302. return 1;
  303. }
  304. int WPACKET_get_length(WPACKET *pkt, size_t *len)
  305. {
  306. /* Internal API, so should not fail */
  307. if (!ossl_assert(pkt->subs != NULL && len != NULL))
  308. return 0;
  309. *len = pkt->written - pkt->subs->pwritten;
  310. return 1;
  311. }
  312. unsigned char *WPACKET_get_curr(WPACKET *pkt)
  313. {
  314. return GETBUF(pkt) + pkt->curr;
  315. }
  316. void WPACKET_cleanup(WPACKET *pkt)
  317. {
  318. WPACKET_SUB *sub, *parent;
  319. for (sub = pkt->subs; sub != NULL; sub = parent) {
  320. parent = sub->parent;
  321. OPENSSL_free(sub);
  322. }
  323. pkt->subs = NULL;
  324. }