2
0

packet.c 10 KB

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