packet.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. /*
  2. * Copyright 2015-2020 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. if (pkt->endfirst && *allocbytes != NULL)
  59. *allocbytes -= len;
  60. }
  61. return 1;
  62. }
  63. int WPACKET_sub_reserve_bytes__(WPACKET *pkt, size_t len,
  64. unsigned char **allocbytes, size_t lenbytes)
  65. {
  66. if (pkt->endfirst && lenbytes > 0)
  67. return 0;
  68. if (!WPACKET_reserve_bytes(pkt, lenbytes + len, allocbytes))
  69. return 0;
  70. if (*allocbytes != NULL)
  71. *allocbytes += lenbytes;
  72. return 1;
  73. }
  74. static size_t maxmaxsize(size_t lenbytes)
  75. {
  76. if (lenbytes >= sizeof(size_t) || lenbytes == 0)
  77. return SIZE_MAX;
  78. return ((size_t)1 << (lenbytes * 8)) - 1 + lenbytes;
  79. }
  80. static int wpacket_intern_init_len(WPACKET *pkt, size_t lenbytes)
  81. {
  82. unsigned char *lenchars;
  83. pkt->curr = 0;
  84. pkt->written = 0;
  85. if ((pkt->subs = OPENSSL_zalloc(sizeof(*pkt->subs))) == NULL) {
  86. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  87. return 0;
  88. }
  89. if (lenbytes == 0)
  90. return 1;
  91. pkt->subs->pwritten = lenbytes;
  92. pkt->subs->lenbytes = lenbytes;
  93. if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars)) {
  94. OPENSSL_free(pkt->subs);
  95. pkt->subs = NULL;
  96. return 0;
  97. }
  98. pkt->subs->packet_len = 0;
  99. return 1;
  100. }
  101. int WPACKET_init_static_len(WPACKET *pkt, unsigned char *buf, size_t len,
  102. size_t lenbytes)
  103. {
  104. size_t max = maxmaxsize(lenbytes);
  105. /* Internal API, so should not fail */
  106. if (!ossl_assert(buf != NULL && len > 0))
  107. return 0;
  108. pkt->staticbuf = buf;
  109. pkt->buf = NULL;
  110. pkt->maxsize = (max < len) ? max : len;
  111. pkt->endfirst = 0;
  112. return wpacket_intern_init_len(pkt, lenbytes);
  113. }
  114. int WPACKET_init_der(WPACKET *pkt, unsigned char *buf, size_t len)
  115. {
  116. /* Internal API, so should not fail */
  117. if (!ossl_assert(buf != NULL && len > 0))
  118. return 0;
  119. pkt->staticbuf = buf;
  120. pkt->buf = NULL;
  121. pkt->maxsize = len;
  122. pkt->endfirst = 1;
  123. return wpacket_intern_init_len(pkt, 0);
  124. }
  125. int WPACKET_init_len(WPACKET *pkt, BUF_MEM *buf, size_t lenbytes)
  126. {
  127. /* Internal API, so should not fail */
  128. if (!ossl_assert(buf != NULL))
  129. return 0;
  130. pkt->staticbuf = NULL;
  131. pkt->buf = buf;
  132. pkt->maxsize = maxmaxsize(lenbytes);
  133. pkt->endfirst = 0;
  134. return wpacket_intern_init_len(pkt, lenbytes);
  135. }
  136. int WPACKET_init(WPACKET *pkt, BUF_MEM *buf)
  137. {
  138. return WPACKET_init_len(pkt, buf, 0);
  139. }
  140. int WPACKET_init_null(WPACKET *pkt, size_t lenbytes)
  141. {
  142. pkt->staticbuf = NULL;
  143. pkt->buf = NULL;
  144. pkt->maxsize = maxmaxsize(lenbytes);
  145. pkt->endfirst = 0;
  146. return wpacket_intern_init_len(pkt, 0);
  147. }
  148. int WPACKET_init_null_der(WPACKET *pkt)
  149. {
  150. pkt->staticbuf = NULL;
  151. pkt->buf = NULL;
  152. pkt->maxsize = SIZE_MAX;
  153. pkt->endfirst = 1;
  154. return wpacket_intern_init_len(pkt, 0);
  155. }
  156. int WPACKET_set_flags(WPACKET *pkt, unsigned int flags)
  157. {
  158. /* Internal API, so should not fail */
  159. if (!ossl_assert(pkt->subs != NULL))
  160. return 0;
  161. pkt->subs->flags = flags;
  162. return 1;
  163. }
  164. /* Store the |value| of length |len| at location |data| */
  165. static int put_value(unsigned char *data, size_t value, size_t len)
  166. {
  167. if (data == NULL)
  168. return 1;
  169. for (data += len - 1; len > 0; len--) {
  170. *data = (unsigned char)(value & 0xff);
  171. data--;
  172. value >>= 8;
  173. }
  174. /* Check whether we could fit the value in the assigned number of bytes */
  175. if (value > 0)
  176. return 0;
  177. return 1;
  178. }
  179. /*
  180. * Internal helper function used by WPACKET_close(), WPACKET_finish() and
  181. * WPACKET_fill_lengths() to close a sub-packet and write out its length if
  182. * necessary. If |doclose| is 0 then it goes through the motions of closing
  183. * (i.e. it fills in all the lengths), but doesn't actually close anything.
  184. */
  185. static int wpacket_intern_close(WPACKET *pkt, WPACKET_SUB *sub, int doclose)
  186. {
  187. size_t packlen = pkt->written - sub->pwritten;
  188. if (packlen == 0
  189. && (sub->flags & WPACKET_FLAGS_NON_ZERO_LENGTH) != 0)
  190. return 0;
  191. if (packlen == 0
  192. && sub->flags & WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH) {
  193. /* We can't handle this case. Return an error */
  194. if (!doclose)
  195. return 0;
  196. /* Deallocate any bytes allocated for the length of the WPACKET */
  197. if ((pkt->curr - sub->lenbytes) == sub->packet_len) {
  198. pkt->written -= sub->lenbytes;
  199. pkt->curr -= sub->lenbytes;
  200. }
  201. /* Don't write out the packet length */
  202. sub->packet_len = 0;
  203. sub->lenbytes = 0;
  204. }
  205. /* Write out the WPACKET length if needed */
  206. if (sub->lenbytes > 0) {
  207. unsigned char *buf = GETBUF(pkt);
  208. if (buf != NULL
  209. && !put_value(&buf[sub->packet_len], packlen,
  210. sub->lenbytes))
  211. return 0;
  212. } else if (pkt->endfirst && sub->parent != NULL
  213. && (packlen != 0
  214. || (sub->flags
  215. & WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH) == 0)) {
  216. size_t tmplen = packlen;
  217. size_t numlenbytes = 1;
  218. while ((tmplen = tmplen >> 8) > 0)
  219. numlenbytes++;
  220. if (!WPACKET_put_bytes__(pkt, packlen, numlenbytes))
  221. return 0;
  222. if (packlen > 0x7f) {
  223. numlenbytes |= 0x80;
  224. if (!WPACKET_put_bytes_u8(pkt, numlenbytes))
  225. return 0;
  226. }
  227. }
  228. if (doclose) {
  229. pkt->subs = sub->parent;
  230. OPENSSL_free(sub);
  231. }
  232. return 1;
  233. }
  234. int WPACKET_fill_lengths(WPACKET *pkt)
  235. {
  236. WPACKET_SUB *sub;
  237. if (!ossl_assert(pkt->subs != NULL))
  238. return 0;
  239. for (sub = pkt->subs; sub != NULL; sub = sub->parent) {
  240. if (!wpacket_intern_close(pkt, sub, 0))
  241. return 0;
  242. }
  243. return 1;
  244. }
  245. int WPACKET_close(WPACKET *pkt)
  246. {
  247. /*
  248. * Internal API, so should not fail - but we do negative testing of this
  249. * so no assert (otherwise the tests fail)
  250. */
  251. if (pkt->subs == NULL || pkt->subs->parent == NULL)
  252. return 0;
  253. return wpacket_intern_close(pkt, pkt->subs, 1);
  254. }
  255. int WPACKET_finish(WPACKET *pkt)
  256. {
  257. int ret;
  258. /*
  259. * Internal API, so should not fail - but we do negative testing of this
  260. * so no assert (otherwise the tests fail)
  261. */
  262. if (pkt->subs == NULL || pkt->subs->parent != NULL)
  263. return 0;
  264. ret = wpacket_intern_close(pkt, pkt->subs, 1);
  265. if (ret) {
  266. OPENSSL_free(pkt->subs);
  267. pkt->subs = NULL;
  268. }
  269. return ret;
  270. }
  271. int WPACKET_start_sub_packet_len__(WPACKET *pkt, size_t lenbytes)
  272. {
  273. WPACKET_SUB *sub;
  274. unsigned char *lenchars;
  275. /* Internal API, so should not fail */
  276. if (!ossl_assert(pkt->subs != NULL))
  277. return 0;
  278. /* We don't support lenbytes greater than 0 when doing endfirst writing */
  279. if (lenbytes > 0 && pkt->endfirst)
  280. return 0;
  281. if ((sub = OPENSSL_zalloc(sizeof(*sub))) == NULL) {
  282. ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
  283. return 0;
  284. }
  285. sub->parent = pkt->subs;
  286. pkt->subs = sub;
  287. sub->pwritten = pkt->written + lenbytes;
  288. sub->lenbytes = lenbytes;
  289. if (lenbytes == 0) {
  290. sub->packet_len = 0;
  291. return 1;
  292. }
  293. sub->packet_len = pkt->written;
  294. if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars))
  295. return 0;
  296. return 1;
  297. }
  298. int WPACKET_start_sub_packet(WPACKET *pkt)
  299. {
  300. return WPACKET_start_sub_packet_len__(pkt, 0);
  301. }
  302. int WPACKET_put_bytes__(WPACKET *pkt, unsigned int val, size_t size)
  303. {
  304. unsigned char *data;
  305. /* Internal API, so should not fail */
  306. if (!ossl_assert(size <= sizeof(unsigned int))
  307. || !WPACKET_allocate_bytes(pkt, size, &data)
  308. || !put_value(data, val, size))
  309. return 0;
  310. return 1;
  311. }
  312. int WPACKET_set_max_size(WPACKET *pkt, size_t maxsize)
  313. {
  314. WPACKET_SUB *sub;
  315. size_t lenbytes;
  316. /* Internal API, so should not fail */
  317. if (!ossl_assert(pkt->subs != NULL))
  318. return 0;
  319. /* Find the WPACKET_SUB for the top level */
  320. for (sub = pkt->subs; sub->parent != NULL; sub = sub->parent)
  321. continue;
  322. lenbytes = sub->lenbytes;
  323. if (lenbytes == 0)
  324. lenbytes = sizeof(pkt->maxsize);
  325. if (maxmaxsize(lenbytes) < maxsize || maxsize < pkt->written)
  326. return 0;
  327. pkt->maxsize = maxsize;
  328. return 1;
  329. }
  330. int WPACKET_memset(WPACKET *pkt, int ch, size_t len)
  331. {
  332. unsigned char *dest;
  333. if (len == 0)
  334. return 1;
  335. if (!WPACKET_allocate_bytes(pkt, len, &dest))
  336. return 0;
  337. if (dest != NULL)
  338. memset(dest, ch, len);
  339. return 1;
  340. }
  341. int WPACKET_memcpy(WPACKET *pkt, const void *src, size_t len)
  342. {
  343. unsigned char *dest;
  344. if (len == 0)
  345. return 1;
  346. if (!WPACKET_allocate_bytes(pkt, len, &dest))
  347. return 0;
  348. if (dest != NULL)
  349. memcpy(dest, src, len);
  350. return 1;
  351. }
  352. int WPACKET_sub_memcpy__(WPACKET *pkt, const void *src, size_t len,
  353. size_t lenbytes)
  354. {
  355. if (!WPACKET_start_sub_packet_len__(pkt, lenbytes)
  356. || !WPACKET_memcpy(pkt, src, len)
  357. || !WPACKET_close(pkt))
  358. return 0;
  359. return 1;
  360. }
  361. int WPACKET_get_total_written(WPACKET *pkt, size_t *written)
  362. {
  363. /* Internal API, so should not fail */
  364. if (!ossl_assert(written != NULL))
  365. return 0;
  366. *written = pkt->written;
  367. return 1;
  368. }
  369. int WPACKET_get_length(WPACKET *pkt, size_t *len)
  370. {
  371. /* Internal API, so should not fail */
  372. if (!ossl_assert(pkt->subs != NULL && len != NULL))
  373. return 0;
  374. *len = pkt->written - pkt->subs->pwritten;
  375. return 1;
  376. }
  377. unsigned char *WPACKET_get_curr(WPACKET *pkt)
  378. {
  379. unsigned char *buf = GETBUF(pkt);
  380. if (buf == NULL)
  381. return NULL;
  382. if (pkt->endfirst)
  383. return buf + pkt->maxsize - pkt->curr;
  384. return buf + pkt->curr;
  385. }
  386. int WPACKET_is_null_buf(WPACKET *pkt)
  387. {
  388. return pkt->buf == NULL && pkt->staticbuf == NULL;
  389. }
  390. void WPACKET_cleanup(WPACKET *pkt)
  391. {
  392. WPACKET_SUB *sub, *parent;
  393. for (sub = pkt->subs; sub != NULL; sub = parent) {
  394. parent = sub->parent;
  395. OPENSSL_free(sub);
  396. }
  397. pkt->subs = NULL;
  398. }