packet.c 14 KB

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