packet.c 9.9 KB

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