packet.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. /*
  2. * Copyright 2015-2023 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. #if !defined OPENSSL_NO_QUIC && !defined FIPS_MODULE
  12. # include "internal/packet_quic.h"
  13. #endif
  14. #include <openssl/err.h>
  15. #define DEFAULT_BUF_SIZE 256
  16. int WPACKET_allocate_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
  17. {
  18. if (!WPACKET_reserve_bytes(pkt, len, allocbytes))
  19. return 0;
  20. pkt->written += len;
  21. pkt->curr += len;
  22. return 1;
  23. }
  24. int WPACKET_sub_allocate_bytes__(WPACKET *pkt, size_t len,
  25. unsigned char **allocbytes, size_t lenbytes)
  26. {
  27. if (!WPACKET_start_sub_packet_len__(pkt, lenbytes)
  28. || !WPACKET_allocate_bytes(pkt, len, allocbytes)
  29. || !WPACKET_close(pkt))
  30. return 0;
  31. return 1;
  32. }
  33. #define GETBUF(p) (((p)->staticbuf != NULL) \
  34. ? (p)->staticbuf \
  35. : ((p)->buf != NULL \
  36. ? (unsigned char *)(p)->buf->data \
  37. : NULL))
  38. int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
  39. {
  40. /* Internal API, so should not fail */
  41. if (!ossl_assert(pkt->subs != NULL && len != 0))
  42. return 0;
  43. if (pkt->maxsize - pkt->written < len)
  44. return 0;
  45. if (pkt->buf != NULL && (pkt->buf->length - pkt->written < len)) {
  46. size_t newlen;
  47. size_t reflen;
  48. reflen = (len > pkt->buf->length) ? len : pkt->buf->length;
  49. if (reflen > SIZE_MAX / 2) {
  50. newlen = SIZE_MAX;
  51. } else {
  52. newlen = reflen * 2;
  53. if (newlen < DEFAULT_BUF_SIZE)
  54. newlen = DEFAULT_BUF_SIZE;
  55. }
  56. if (BUF_MEM_grow(pkt->buf, newlen) == 0)
  57. return 0;
  58. }
  59. if (allocbytes != NULL) {
  60. *allocbytes = WPACKET_get_curr(pkt);
  61. if (pkt->endfirst && *allocbytes != NULL)
  62. *allocbytes -= len;
  63. }
  64. return 1;
  65. }
  66. int WPACKET_sub_reserve_bytes__(WPACKET *pkt, size_t len,
  67. unsigned char **allocbytes, size_t lenbytes)
  68. {
  69. if (pkt->endfirst && lenbytes > 0)
  70. return 0;
  71. if (!WPACKET_reserve_bytes(pkt, lenbytes + len, allocbytes))
  72. return 0;
  73. if (*allocbytes != NULL)
  74. *allocbytes += lenbytes;
  75. return 1;
  76. }
  77. static size_t maxmaxsize(size_t lenbytes)
  78. {
  79. if (lenbytes >= sizeof(size_t) || lenbytes == 0)
  80. return SIZE_MAX;
  81. return ((size_t)1 << (lenbytes * 8)) - 1 + lenbytes;
  82. }
  83. static int wpacket_intern_init_len(WPACKET *pkt, size_t lenbytes)
  84. {
  85. unsigned char *lenchars;
  86. pkt->curr = 0;
  87. pkt->written = 0;
  88. if ((pkt->subs = OPENSSL_zalloc(sizeof(*pkt->subs))) == NULL)
  89. return 0;
  90. if (lenbytes == 0)
  91. return 1;
  92. pkt->subs->pwritten = lenbytes;
  93. pkt->subs->lenbytes = lenbytes;
  94. if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars)) {
  95. OPENSSL_free(pkt->subs);
  96. pkt->subs = NULL;
  97. return 0;
  98. }
  99. pkt->subs->packet_len = 0;
  100. return 1;
  101. }
  102. int WPACKET_init_static_len(WPACKET *pkt, unsigned char *buf, size_t len,
  103. size_t lenbytes)
  104. {
  105. size_t max = maxmaxsize(lenbytes);
  106. /* Internal API, so should not fail */
  107. if (!ossl_assert(buf != NULL && len > 0))
  108. return 0;
  109. pkt->staticbuf = buf;
  110. pkt->buf = NULL;
  111. pkt->maxsize = (max < len) ? max : len;
  112. pkt->endfirst = 0;
  113. return wpacket_intern_init_len(pkt, lenbytes);
  114. }
  115. int WPACKET_init_der(WPACKET *pkt, unsigned char *buf, size_t len)
  116. {
  117. /* Internal API, so should not fail */
  118. if (!ossl_assert(buf != NULL && len > 0))
  119. return 0;
  120. pkt->staticbuf = buf;
  121. pkt->buf = NULL;
  122. pkt->maxsize = len;
  123. pkt->endfirst = 1;
  124. return wpacket_intern_init_len(pkt, 0);
  125. }
  126. int WPACKET_init_len(WPACKET *pkt, BUF_MEM *buf, size_t lenbytes)
  127. {
  128. /* Internal API, so should not fail */
  129. if (!ossl_assert(buf != NULL))
  130. return 0;
  131. pkt->staticbuf = NULL;
  132. pkt->buf = buf;
  133. pkt->maxsize = maxmaxsize(lenbytes);
  134. pkt->endfirst = 0;
  135. return wpacket_intern_init_len(pkt, lenbytes);
  136. }
  137. int WPACKET_init(WPACKET *pkt, BUF_MEM *buf)
  138. {
  139. return WPACKET_init_len(pkt, buf, 0);
  140. }
  141. int WPACKET_init_null(WPACKET *pkt, size_t lenbytes)
  142. {
  143. pkt->staticbuf = NULL;
  144. pkt->buf = NULL;
  145. pkt->maxsize = maxmaxsize(lenbytes);
  146. pkt->endfirst = 0;
  147. return wpacket_intern_init_len(pkt, 0);
  148. }
  149. int WPACKET_init_null_der(WPACKET *pkt)
  150. {
  151. pkt->staticbuf = NULL;
  152. pkt->buf = NULL;
  153. pkt->maxsize = SIZE_MAX;
  154. pkt->endfirst = 1;
  155. return wpacket_intern_init_len(pkt, 0);
  156. }
  157. int WPACKET_set_flags(WPACKET *pkt, unsigned int flags)
  158. {
  159. /* Internal API, so should not fail */
  160. if (!ossl_assert(pkt->subs != NULL))
  161. return 0;
  162. pkt->subs->flags = flags;
  163. return 1;
  164. }
  165. /* Store the |value| of length |len| at location |data| */
  166. static int put_value(unsigned char *data, uint64_t value, size_t len)
  167. {
  168. if (data == NULL)
  169. return 1;
  170. for (data += len - 1; len > 0; len--) {
  171. *data = (unsigned char)(value & 0xff);
  172. data--;
  173. value >>= 8;
  174. }
  175. /* Check whether we could fit the value in the assigned number of bytes */
  176. if (value > 0)
  177. return 0;
  178. return 1;
  179. }
  180. #if !defined OPENSSL_NO_QUIC && !defined FIPS_MODULE
  181. static int put_quic_value(unsigned char *data, size_t value, size_t len)
  182. {
  183. if (data == NULL)
  184. return 1;
  185. /* Value too large for field. */
  186. if (ossl_quic_vlint_encode_len(value) > len)
  187. return 0;
  188. ossl_quic_vlint_encode_n(data, value, len);
  189. return 1;
  190. }
  191. #endif
  192. /*
  193. * Internal helper function used by WPACKET_close(), WPACKET_finish() and
  194. * WPACKET_fill_lengths() to close a sub-packet and write out its length if
  195. * necessary. If |doclose| is 0 then it goes through the motions of closing
  196. * (i.e. it fills in all the lengths), but doesn't actually close anything.
  197. */
  198. static int wpacket_intern_close(WPACKET *pkt, WPACKET_SUB *sub, int doclose)
  199. {
  200. size_t packlen = pkt->written - sub->pwritten;
  201. if (packlen == 0
  202. && (sub->flags & WPACKET_FLAGS_NON_ZERO_LENGTH) != 0)
  203. return 0;
  204. if (packlen == 0
  205. && sub->flags & WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH) {
  206. /* We can't handle this case. Return an error */
  207. if (!doclose)
  208. return 0;
  209. /* Deallocate any bytes allocated for the length of the WPACKET */
  210. if ((pkt->curr - sub->lenbytes) == sub->packet_len) {
  211. pkt->written -= sub->lenbytes;
  212. pkt->curr -= sub->lenbytes;
  213. }
  214. /* Don't write out the packet length */
  215. sub->packet_len = 0;
  216. sub->lenbytes = 0;
  217. }
  218. /* Write out the WPACKET length if needed */
  219. if (sub->lenbytes > 0) {
  220. unsigned char *buf = GETBUF(pkt);
  221. if (buf != NULL) {
  222. #if !defined OPENSSL_NO_QUIC && !defined FIPS_MODULE
  223. if ((sub->flags & WPACKET_FLAGS_QUIC_VLINT) == 0) {
  224. if (!put_value(&buf[sub->packet_len], packlen, sub->lenbytes))
  225. return 0;
  226. } else {
  227. if (!put_quic_value(&buf[sub->packet_len], packlen, sub->lenbytes))
  228. return 0;
  229. }
  230. #else
  231. if (!put_value(&buf[sub->packet_len], packlen, sub->lenbytes))
  232. return 0;
  233. #endif
  234. }
  235. } else if (pkt->endfirst && sub->parent != NULL
  236. && (packlen != 0
  237. || (sub->flags
  238. & WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH) == 0)) {
  239. size_t tmplen = packlen;
  240. size_t numlenbytes = 1;
  241. while ((tmplen = tmplen >> 8) > 0)
  242. numlenbytes++;
  243. if (!WPACKET_put_bytes__(pkt, packlen, numlenbytes))
  244. return 0;
  245. if (packlen > 0x7f) {
  246. numlenbytes |= 0x80;
  247. if (!WPACKET_put_bytes_u8(pkt, numlenbytes))
  248. return 0;
  249. }
  250. }
  251. if (doclose) {
  252. pkt->subs = sub->parent;
  253. OPENSSL_free(sub);
  254. }
  255. return 1;
  256. }
  257. int WPACKET_fill_lengths(WPACKET *pkt)
  258. {
  259. WPACKET_SUB *sub;
  260. if (!ossl_assert(pkt->subs != NULL))
  261. return 0;
  262. for (sub = pkt->subs; sub != NULL; sub = sub->parent) {
  263. if (!wpacket_intern_close(pkt, sub, 0))
  264. return 0;
  265. }
  266. return 1;
  267. }
  268. int WPACKET_close(WPACKET *pkt)
  269. {
  270. /*
  271. * Internal API, so should not fail - but we do negative testing of this
  272. * so no assert (otherwise the tests fail)
  273. */
  274. if (pkt->subs == NULL || pkt->subs->parent == NULL)
  275. return 0;
  276. return wpacket_intern_close(pkt, pkt->subs, 1);
  277. }
  278. int WPACKET_finish(WPACKET *pkt)
  279. {
  280. int ret;
  281. /*
  282. * Internal API, so should not fail - but we do negative testing of this
  283. * so no assert (otherwise the tests fail)
  284. */
  285. if (pkt->subs == NULL || pkt->subs->parent != NULL)
  286. return 0;
  287. ret = wpacket_intern_close(pkt, pkt->subs, 1);
  288. if (ret) {
  289. OPENSSL_free(pkt->subs);
  290. pkt->subs = NULL;
  291. }
  292. return ret;
  293. }
  294. int WPACKET_start_sub_packet_len__(WPACKET *pkt, size_t lenbytes)
  295. {
  296. WPACKET_SUB *sub;
  297. unsigned char *lenchars;
  298. /* Internal API, so should not fail */
  299. if (!ossl_assert(pkt->subs != NULL))
  300. return 0;
  301. /* We don't support lenbytes greater than 0 when doing endfirst writing */
  302. if (lenbytes > 0 && pkt->endfirst)
  303. return 0;
  304. if ((sub = OPENSSL_zalloc(sizeof(*sub))) == NULL)
  305. return 0;
  306. sub->parent = pkt->subs;
  307. pkt->subs = sub;
  308. sub->pwritten = pkt->written + lenbytes;
  309. sub->lenbytes = lenbytes;
  310. if (lenbytes == 0) {
  311. sub->packet_len = 0;
  312. return 1;
  313. }
  314. sub->packet_len = pkt->written;
  315. if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars))
  316. return 0;
  317. return 1;
  318. }
  319. int WPACKET_start_sub_packet(WPACKET *pkt)
  320. {
  321. return WPACKET_start_sub_packet_len__(pkt, 0);
  322. }
  323. int WPACKET_put_bytes__(WPACKET *pkt, uint64_t val, size_t size)
  324. {
  325. unsigned char *data;
  326. /* Internal API, so should not fail */
  327. if (!ossl_assert(size <= sizeof(uint64_t))
  328. || !WPACKET_allocate_bytes(pkt, size, &data)
  329. || !put_value(data, val, size))
  330. return 0;
  331. return 1;
  332. }
  333. int WPACKET_set_max_size(WPACKET *pkt, size_t maxsize)
  334. {
  335. WPACKET_SUB *sub;
  336. size_t lenbytes;
  337. /* Internal API, so should not fail */
  338. if (!ossl_assert(pkt->subs != NULL))
  339. return 0;
  340. /* Find the WPACKET_SUB for the top level */
  341. for (sub = pkt->subs; sub->parent != NULL; sub = sub->parent)
  342. continue;
  343. lenbytes = sub->lenbytes;
  344. if (lenbytes == 0)
  345. lenbytes = sizeof(pkt->maxsize);
  346. if (maxmaxsize(lenbytes) < maxsize || maxsize < pkt->written)
  347. return 0;
  348. pkt->maxsize = maxsize;
  349. return 1;
  350. }
  351. int WPACKET_memset(WPACKET *pkt, int ch, size_t len)
  352. {
  353. unsigned char *dest;
  354. if (len == 0)
  355. return 1;
  356. if (!WPACKET_allocate_bytes(pkt, len, &dest))
  357. return 0;
  358. if (dest != NULL)
  359. memset(dest, ch, len);
  360. return 1;
  361. }
  362. int WPACKET_memcpy(WPACKET *pkt, const void *src, size_t len)
  363. {
  364. unsigned char *dest;
  365. if (len == 0)
  366. return 1;
  367. if (!WPACKET_allocate_bytes(pkt, len, &dest))
  368. return 0;
  369. if (dest != NULL)
  370. memcpy(dest, src, len);
  371. return 1;
  372. }
  373. int WPACKET_sub_memcpy__(WPACKET *pkt, const void *src, size_t len,
  374. size_t lenbytes)
  375. {
  376. if (!WPACKET_start_sub_packet_len__(pkt, lenbytes)
  377. || !WPACKET_memcpy(pkt, src, len)
  378. || !WPACKET_close(pkt))
  379. return 0;
  380. return 1;
  381. }
  382. int WPACKET_get_total_written(WPACKET *pkt, size_t *written)
  383. {
  384. /* Internal API, so should not fail */
  385. if (!ossl_assert(written != NULL))
  386. return 0;
  387. *written = pkt->written;
  388. return 1;
  389. }
  390. int WPACKET_get_length(WPACKET *pkt, size_t *len)
  391. {
  392. /* Internal API, so should not fail */
  393. if (!ossl_assert(pkt->subs != NULL && len != NULL))
  394. return 0;
  395. *len = pkt->written - pkt->subs->pwritten;
  396. return 1;
  397. }
  398. unsigned char *WPACKET_get_curr(WPACKET *pkt)
  399. {
  400. unsigned char *buf = GETBUF(pkt);
  401. if (buf == NULL)
  402. return NULL;
  403. if (pkt->endfirst)
  404. return buf + pkt->maxsize - pkt->curr;
  405. return buf + pkt->curr;
  406. }
  407. int WPACKET_is_null_buf(WPACKET *pkt)
  408. {
  409. return pkt->buf == NULL && pkt->staticbuf == NULL;
  410. }
  411. void WPACKET_cleanup(WPACKET *pkt)
  412. {
  413. WPACKET_SUB *sub, *parent;
  414. for (sub = pkt->subs; sub != NULL; sub = parent) {
  415. parent = sub->parent;
  416. OPENSSL_free(sub);
  417. }
  418. pkt->subs = NULL;
  419. }
  420. #if !defined OPENSSL_NO_QUIC && !defined FIPS_MODULE
  421. int WPACKET_start_quic_sub_packet_bound(WPACKET *pkt, size_t max_len)
  422. {
  423. size_t enclen = ossl_quic_vlint_encode_len(max_len);
  424. if (enclen == 0)
  425. return 0;
  426. if (WPACKET_start_sub_packet_len__(pkt, enclen) == 0)
  427. return 0;
  428. pkt->subs->flags |= WPACKET_FLAGS_QUIC_VLINT;
  429. return 1;
  430. }
  431. int WPACKET_start_quic_sub_packet(WPACKET *pkt)
  432. {
  433. /*
  434. * Assume no (sub)packet will exceed 4GiB, thus the 8-byte encoding need not
  435. * be used.
  436. */
  437. return WPACKET_start_quic_sub_packet_bound(pkt, OSSL_QUIC_VLINT_4B_MIN);
  438. }
  439. int WPACKET_quic_sub_allocate_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
  440. {
  441. if (!WPACKET_start_quic_sub_packet_bound(pkt, len)
  442. || !WPACKET_allocate_bytes(pkt, len, allocbytes)
  443. || !WPACKET_close(pkt))
  444. return 0;
  445. return 1;
  446. }
  447. /*
  448. * Write a QUIC variable-length integer to the packet.
  449. */
  450. int WPACKET_quic_write_vlint(WPACKET *pkt, uint64_t v)
  451. {
  452. unsigned char *b = NULL;
  453. size_t enclen = ossl_quic_vlint_encode_len(v);
  454. if (enclen == 0)
  455. return 0;
  456. if (WPACKET_allocate_bytes(pkt, enclen, &b) == 0)
  457. return 0;
  458. ossl_quic_vlint_encode(b, v);
  459. return 1;
  460. }
  461. #endif