packet.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  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. #ifndef OSSL_INTERNAL_PACKET_H
  10. # define OSSL_INTERNAL_PACKET_H
  11. # pragma once
  12. # include <string.h>
  13. # include <openssl/bn.h>
  14. # include <openssl/buffer.h>
  15. # include <openssl/crypto.h>
  16. # include <openssl/e_os2.h>
  17. # include "internal/numbers.h"
  18. # include "internal/quic_vlint.h"
  19. typedef struct {
  20. /* Pointer to where we are currently reading from */
  21. const unsigned char *curr;
  22. /* Number of bytes remaining */
  23. size_t remaining;
  24. } PACKET;
  25. /* Internal unchecked shorthand; don't use outside this file. */
  26. static ossl_inline void packet_forward(PACKET *pkt, size_t len)
  27. {
  28. pkt->curr += len;
  29. pkt->remaining -= len;
  30. }
  31. /*
  32. * Returns the number of bytes remaining to be read in the PACKET
  33. */
  34. static ossl_inline size_t PACKET_remaining(const PACKET *pkt)
  35. {
  36. return pkt->remaining;
  37. }
  38. /*
  39. * Returns a pointer to the first byte after the packet data.
  40. * Useful for integrating with non-PACKET parsing code.
  41. * Specifically, we use PACKET_end() to verify that a d2i_... call
  42. * has consumed the entire packet contents.
  43. */
  44. static ossl_inline const unsigned char *PACKET_end(const PACKET *pkt)
  45. {
  46. return pkt->curr + pkt->remaining;
  47. }
  48. /*
  49. * Returns a pointer to the PACKET's current position.
  50. * For use in non-PACKETized APIs.
  51. */
  52. static ossl_inline const unsigned char *PACKET_data(const PACKET *pkt)
  53. {
  54. return pkt->curr;
  55. }
  56. /*
  57. * Initialise a PACKET with |len| bytes held in |buf|. This does not make a
  58. * copy of the data so |buf| must be present for the whole time that the PACKET
  59. * is being used.
  60. */
  61. __owur static ossl_inline int PACKET_buf_init(PACKET *pkt,
  62. const unsigned char *buf,
  63. size_t len)
  64. {
  65. /* Sanity check for negative values. */
  66. if (len > (size_t)(SIZE_MAX / 2))
  67. return 0;
  68. pkt->curr = buf;
  69. pkt->remaining = len;
  70. return 1;
  71. }
  72. /* Initialize a PACKET to hold zero bytes. */
  73. static ossl_inline void PACKET_null_init(PACKET *pkt)
  74. {
  75. pkt->curr = NULL;
  76. pkt->remaining = 0;
  77. }
  78. /*
  79. * Returns 1 if the packet has length |num| and its contents equal the |num|
  80. * bytes read from |ptr|. Returns 0 otherwise (lengths or contents not equal).
  81. * If lengths are equal, performs the comparison in constant time.
  82. */
  83. __owur static ossl_inline int PACKET_equal(const PACKET *pkt, const void *ptr,
  84. size_t num)
  85. {
  86. if (PACKET_remaining(pkt) != num)
  87. return 0;
  88. return CRYPTO_memcmp(pkt->curr, ptr, num) == 0;
  89. }
  90. /*
  91. * Peek ahead and initialize |subpkt| with the next |len| bytes read from |pkt|.
  92. * Data is not copied: the |subpkt| packet will share its underlying buffer with
  93. * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
  94. */
  95. __owur static ossl_inline int PACKET_peek_sub_packet(const PACKET *pkt,
  96. PACKET *subpkt, size_t len)
  97. {
  98. if (PACKET_remaining(pkt) < len)
  99. return 0;
  100. return PACKET_buf_init(subpkt, pkt->curr, len);
  101. }
  102. /*
  103. * Initialize |subpkt| with the next |len| bytes read from |pkt|. Data is not
  104. * copied: the |subpkt| packet will share its underlying buffer with the
  105. * original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
  106. */
  107. __owur static ossl_inline int PACKET_get_sub_packet(PACKET *pkt,
  108. PACKET *subpkt, size_t len)
  109. {
  110. if (!PACKET_peek_sub_packet(pkt, subpkt, len))
  111. return 0;
  112. packet_forward(pkt, len);
  113. return 1;
  114. }
  115. /*
  116. * Peek ahead at 2 bytes in network order from |pkt| and store the value in
  117. * |*data|
  118. */
  119. __owur static ossl_inline int PACKET_peek_net_2(const PACKET *pkt,
  120. unsigned int *data)
  121. {
  122. if (PACKET_remaining(pkt) < 2)
  123. return 0;
  124. *data = ((unsigned int)(*pkt->curr)) << 8;
  125. *data |= *(pkt->curr + 1);
  126. return 1;
  127. }
  128. /* Equivalent of n2s */
  129. /* Get 2 bytes in network order from |pkt| and store the value in |*data| */
  130. __owur static ossl_inline int PACKET_get_net_2(PACKET *pkt, unsigned int *data)
  131. {
  132. if (!PACKET_peek_net_2(pkt, data))
  133. return 0;
  134. packet_forward(pkt, 2);
  135. return 1;
  136. }
  137. /* Same as PACKET_get_net_2() but for a size_t */
  138. __owur static ossl_inline int PACKET_get_net_2_len(PACKET *pkt, size_t *data)
  139. {
  140. unsigned int i;
  141. int ret = PACKET_get_net_2(pkt, &i);
  142. if (ret)
  143. *data = (size_t)i;
  144. return ret;
  145. }
  146. /*
  147. * Peek ahead at 3 bytes in network order from |pkt| and store the value in
  148. * |*data|
  149. */
  150. __owur static ossl_inline int PACKET_peek_net_3(const PACKET *pkt,
  151. unsigned long *data)
  152. {
  153. if (PACKET_remaining(pkt) < 3)
  154. return 0;
  155. *data = ((unsigned long)(*pkt->curr)) << 16;
  156. *data |= ((unsigned long)(*(pkt->curr + 1))) << 8;
  157. *data |= *(pkt->curr + 2);
  158. return 1;
  159. }
  160. /* Equivalent of n2l3 */
  161. /* Get 3 bytes in network order from |pkt| and store the value in |*data| */
  162. __owur static ossl_inline int PACKET_get_net_3(PACKET *pkt, unsigned long *data)
  163. {
  164. if (!PACKET_peek_net_3(pkt, data))
  165. return 0;
  166. packet_forward(pkt, 3);
  167. return 1;
  168. }
  169. /* Same as PACKET_get_net_3() but for a size_t */
  170. __owur static ossl_inline int PACKET_get_net_3_len(PACKET *pkt, size_t *data)
  171. {
  172. unsigned long i;
  173. int ret = PACKET_get_net_3(pkt, &i);
  174. if (ret)
  175. *data = (size_t)i;
  176. return ret;
  177. }
  178. /*
  179. * Peek ahead at 4 bytes in network order from |pkt| and store the value in
  180. * |*data|
  181. */
  182. __owur static ossl_inline int PACKET_peek_net_4(const PACKET *pkt,
  183. unsigned long *data)
  184. {
  185. if (PACKET_remaining(pkt) < 4)
  186. return 0;
  187. *data = ((unsigned long)(*pkt->curr)) << 24;
  188. *data |= ((unsigned long)(*(pkt->curr + 1))) << 16;
  189. *data |= ((unsigned long)(*(pkt->curr + 2))) << 8;
  190. *data |= *(pkt->curr + 3);
  191. return 1;
  192. }
  193. /*
  194. * Peek ahead at 8 bytes in network order from |pkt| and store the value in
  195. * |*data|
  196. */
  197. __owur static ossl_inline int PACKET_peek_net_8(const PACKET *pkt,
  198. uint64_t *data)
  199. {
  200. if (PACKET_remaining(pkt) < 8)
  201. return 0;
  202. *data = ((uint64_t)(*pkt->curr)) << 56;
  203. *data |= ((uint64_t)(*(pkt->curr + 1))) << 48;
  204. *data |= ((uint64_t)(*(pkt->curr + 2))) << 40;
  205. *data |= ((uint64_t)(*(pkt->curr + 3))) << 32;
  206. *data |= ((uint64_t)(*(pkt->curr + 4))) << 24;
  207. *data |= ((uint64_t)(*(pkt->curr + 5))) << 16;
  208. *data |= ((uint64_t)(*(pkt->curr + 6))) << 8;
  209. *data |= *(pkt->curr + 7);
  210. return 1;
  211. }
  212. /*
  213. * Decodes a QUIC variable-length integer in |pkt| and stores the result in
  214. * |data|.
  215. */
  216. __owur static ossl_inline int PACKET_get_quic_vlint(PACKET *pkt,
  217. uint64_t *data)
  218. {
  219. size_t enclen;
  220. if (PACKET_remaining(pkt) < 1)
  221. return 0;
  222. enclen = ossl_quic_vlint_decode_len(*pkt->curr);
  223. if (PACKET_remaining(pkt) < enclen)
  224. return 0;
  225. *data = ossl_quic_vlint_decode_unchecked(pkt->curr);
  226. packet_forward(pkt, enclen);
  227. return 1;
  228. }
  229. /*
  230. * Decodes a QUIC variable-length integer in |pkt| and stores the result in
  231. * |data|. Unlike PACKET_get_quic_vlint, this does not advance the current
  232. * position.
  233. */
  234. __owur static ossl_inline int PACKET_peek_quic_vlint(PACKET *pkt,
  235. uint64_t *data)
  236. {
  237. size_t enclen;
  238. if (PACKET_remaining(pkt) < 1)
  239. return 0;
  240. enclen = ossl_quic_vlint_decode_len(*pkt->curr);
  241. if (PACKET_remaining(pkt) < enclen)
  242. return 0;
  243. *data = ossl_quic_vlint_decode_unchecked(pkt->curr);
  244. return 1;
  245. }
  246. /*
  247. * Skips over a QUIC variable-length integer in |pkt| without decoding it.
  248. */
  249. __owur static ossl_inline int PACKET_skip_quic_vlint(PACKET *pkt)
  250. {
  251. size_t enclen;
  252. if (PACKET_remaining(pkt) < 1)
  253. return 0;
  254. enclen = ossl_quic_vlint_decode_len(*pkt->curr);
  255. if (PACKET_remaining(pkt) < enclen)
  256. return 0;
  257. packet_forward(pkt, enclen);
  258. return 1;
  259. }
  260. /* Equivalent of n2l */
  261. /* Get 4 bytes in network order from |pkt| and store the value in |*data| */
  262. __owur static ossl_inline int PACKET_get_net_4(PACKET *pkt, unsigned long *data)
  263. {
  264. if (!PACKET_peek_net_4(pkt, data))
  265. return 0;
  266. packet_forward(pkt, 4);
  267. return 1;
  268. }
  269. /* Same as PACKET_get_net_4() but for a size_t */
  270. __owur static ossl_inline int PACKET_get_net_4_len(PACKET *pkt, size_t *data)
  271. {
  272. unsigned long i;
  273. int ret = PACKET_get_net_4(pkt, &i);
  274. if (ret)
  275. *data = (size_t)i;
  276. return ret;
  277. }
  278. /* Get 8 bytes in network order from |pkt| and store the value in |*data| */
  279. __owur static ossl_inline int PACKET_get_net_8(PACKET *pkt, uint64_t *data)
  280. {
  281. if (!PACKET_peek_net_8(pkt, data))
  282. return 0;
  283. packet_forward(pkt, 8);
  284. return 1;
  285. }
  286. /* Peek ahead at 1 byte from |pkt| and store the value in |*data| */
  287. __owur static ossl_inline int PACKET_peek_1(const PACKET *pkt,
  288. unsigned int *data)
  289. {
  290. if (!PACKET_remaining(pkt))
  291. return 0;
  292. *data = *pkt->curr;
  293. return 1;
  294. }
  295. /* Get 1 byte from |pkt| and store the value in |*data| */
  296. __owur static ossl_inline int PACKET_get_1(PACKET *pkt, unsigned int *data)
  297. {
  298. if (!PACKET_peek_1(pkt, data))
  299. return 0;
  300. packet_forward(pkt, 1);
  301. return 1;
  302. }
  303. /* Same as PACKET_get_1() but for a size_t */
  304. __owur static ossl_inline int PACKET_get_1_len(PACKET *pkt, size_t *data)
  305. {
  306. unsigned int i;
  307. int ret = PACKET_get_1(pkt, &i);
  308. if (ret)
  309. *data = (size_t)i;
  310. return ret;
  311. }
  312. /*
  313. * Peek ahead at 4 bytes in reverse network order from |pkt| and store the value
  314. * in |*data|
  315. */
  316. __owur static ossl_inline int PACKET_peek_4(const PACKET *pkt,
  317. unsigned long *data)
  318. {
  319. if (PACKET_remaining(pkt) < 4)
  320. return 0;
  321. *data = *pkt->curr;
  322. *data |= ((unsigned long)(*(pkt->curr + 1))) << 8;
  323. *data |= ((unsigned long)(*(pkt->curr + 2))) << 16;
  324. *data |= ((unsigned long)(*(pkt->curr + 3))) << 24;
  325. return 1;
  326. }
  327. /* Equivalent of c2l */
  328. /*
  329. * Get 4 bytes in reverse network order from |pkt| and store the value in
  330. * |*data|
  331. */
  332. __owur static ossl_inline int PACKET_get_4(PACKET *pkt, unsigned long *data)
  333. {
  334. if (!PACKET_peek_4(pkt, data))
  335. return 0;
  336. packet_forward(pkt, 4);
  337. return 1;
  338. }
  339. /*
  340. * Peek ahead at |len| bytes from the |pkt| and store a pointer to them in
  341. * |*data|. This just points at the underlying buffer that |pkt| is using. The
  342. * caller should not free this data directly (it will be freed when the
  343. * underlying buffer gets freed
  344. */
  345. __owur static ossl_inline int PACKET_peek_bytes(const PACKET *pkt,
  346. const unsigned char **data,
  347. size_t len)
  348. {
  349. if (PACKET_remaining(pkt) < len)
  350. return 0;
  351. *data = pkt->curr;
  352. return 1;
  353. }
  354. /*
  355. * Read |len| bytes from the |pkt| and store a pointer to them in |*data|. This
  356. * just points at the underlying buffer that |pkt| is using. The caller should
  357. * not free this data directly (it will be freed when the underlying buffer gets
  358. * freed
  359. */
  360. __owur static ossl_inline int PACKET_get_bytes(PACKET *pkt,
  361. const unsigned char **data,
  362. size_t len)
  363. {
  364. if (!PACKET_peek_bytes(pkt, data, len))
  365. return 0;
  366. packet_forward(pkt, len);
  367. return 1;
  368. }
  369. /* Peek ahead at |len| bytes from |pkt| and copy them to |data| */
  370. __owur static ossl_inline int PACKET_peek_copy_bytes(const PACKET *pkt,
  371. unsigned char *data,
  372. size_t len)
  373. {
  374. if (PACKET_remaining(pkt) < len)
  375. return 0;
  376. memcpy(data, pkt->curr, len);
  377. return 1;
  378. }
  379. /*
  380. * Read |len| bytes from |pkt| and copy them to |data|.
  381. * The caller is responsible for ensuring that |data| can hold |len| bytes.
  382. */
  383. __owur static ossl_inline int PACKET_copy_bytes(PACKET *pkt,
  384. unsigned char *data, size_t len)
  385. {
  386. if (!PACKET_peek_copy_bytes(pkt, data, len))
  387. return 0;
  388. packet_forward(pkt, len);
  389. return 1;
  390. }
  391. /*
  392. * Copy packet data to |dest|, and set |len| to the number of copied bytes.
  393. * If the packet has more than |dest_len| bytes, nothing is copied.
  394. * Returns 1 if the packet data fits in |dest_len| bytes, 0 otherwise.
  395. * Does not forward PACKET position (because it is typically the last thing
  396. * done with a given PACKET).
  397. */
  398. __owur static ossl_inline int PACKET_copy_all(const PACKET *pkt,
  399. unsigned char *dest,
  400. size_t dest_len, size_t *len)
  401. {
  402. if (PACKET_remaining(pkt) > dest_len) {
  403. *len = 0;
  404. return 0;
  405. }
  406. *len = pkt->remaining;
  407. memcpy(dest, pkt->curr, pkt->remaining);
  408. return 1;
  409. }
  410. /*
  411. * Copy |pkt| bytes to a newly allocated buffer and store a pointer to the
  412. * result in |*data|, and the length in |len|.
  413. * If |*data| is not NULL, the old data is OPENSSL_free'd.
  414. * If the packet is empty, or malloc fails, |*data| will be set to NULL.
  415. * Returns 1 if the malloc succeeds and 0 otherwise.
  416. * Does not forward PACKET position (because it is typically the last thing
  417. * done with a given PACKET).
  418. */
  419. __owur static ossl_inline int PACKET_memdup(const PACKET *pkt,
  420. unsigned char **data, size_t *len)
  421. {
  422. size_t length;
  423. OPENSSL_free(*data);
  424. *data = NULL;
  425. *len = 0;
  426. length = PACKET_remaining(pkt);
  427. if (length == 0)
  428. return 1;
  429. *data = OPENSSL_memdup(pkt->curr, length);
  430. if (*data == NULL)
  431. return 0;
  432. *len = length;
  433. return 1;
  434. }
  435. /*
  436. * Read a C string from |pkt| and copy to a newly allocated, NUL-terminated
  437. * buffer. Store a pointer to the result in |*data|.
  438. * If |*data| is not NULL, the old data is OPENSSL_free'd.
  439. * If the data in |pkt| does not contain a NUL-byte, the entire data is
  440. * copied and NUL-terminated.
  441. * Returns 1 if the malloc succeeds and 0 otherwise.
  442. * Does not forward PACKET position (because it is typically the last thing done
  443. * with a given PACKET).
  444. */
  445. __owur static ossl_inline int PACKET_strndup(const PACKET *pkt, char **data)
  446. {
  447. OPENSSL_free(*data);
  448. /* This will succeed on an empty packet, unless pkt->curr == NULL. */
  449. *data = OPENSSL_strndup((const char *)pkt->curr, PACKET_remaining(pkt));
  450. return (*data != NULL);
  451. }
  452. /* Returns 1 if |pkt| contains at least one 0-byte, 0 otherwise. */
  453. static ossl_inline int PACKET_contains_zero_byte(const PACKET *pkt)
  454. {
  455. return memchr(pkt->curr, 0, pkt->remaining) != NULL;
  456. }
  457. /* Move the current reading position forward |len| bytes */
  458. __owur static ossl_inline int PACKET_forward(PACKET *pkt, size_t len)
  459. {
  460. if (PACKET_remaining(pkt) < len)
  461. return 0;
  462. packet_forward(pkt, len);
  463. return 1;
  464. }
  465. /*
  466. * Reads a variable-length vector prefixed with a one-byte length, and stores
  467. * the contents in |subpkt|. |pkt| can equal |subpkt|.
  468. * Data is not copied: the |subpkt| packet will share its underlying buffer with
  469. * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
  470. * Upon failure, the original |pkt| and |subpkt| are not modified.
  471. */
  472. __owur static ossl_inline int PACKET_get_length_prefixed_1(PACKET *pkt,
  473. PACKET *subpkt)
  474. {
  475. unsigned int length;
  476. const unsigned char *data;
  477. PACKET tmp = *pkt;
  478. if (!PACKET_get_1(&tmp, &length) ||
  479. !PACKET_get_bytes(&tmp, &data, (size_t)length)) {
  480. return 0;
  481. }
  482. *pkt = tmp;
  483. subpkt->curr = data;
  484. subpkt->remaining = length;
  485. return 1;
  486. }
  487. /*
  488. * Like PACKET_get_length_prefixed_1, but additionally, fails when there are
  489. * leftover bytes in |pkt|.
  490. */
  491. __owur static ossl_inline int PACKET_as_length_prefixed_1(PACKET *pkt,
  492. PACKET *subpkt)
  493. {
  494. unsigned int length;
  495. const unsigned char *data;
  496. PACKET tmp = *pkt;
  497. if (!PACKET_get_1(&tmp, &length) ||
  498. !PACKET_get_bytes(&tmp, &data, (size_t)length) ||
  499. PACKET_remaining(&tmp) != 0) {
  500. return 0;
  501. }
  502. *pkt = tmp;
  503. subpkt->curr = data;
  504. subpkt->remaining = length;
  505. return 1;
  506. }
  507. /*
  508. * Reads a variable-length vector prefixed with a two-byte length, and stores
  509. * the contents in |subpkt|. |pkt| can equal |subpkt|.
  510. * Data is not copied: the |subpkt| packet will share its underlying buffer with
  511. * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
  512. * Upon failure, the original |pkt| and |subpkt| are not modified.
  513. */
  514. __owur static ossl_inline int PACKET_get_length_prefixed_2(PACKET *pkt,
  515. PACKET *subpkt)
  516. {
  517. unsigned int length;
  518. const unsigned char *data;
  519. PACKET tmp = *pkt;
  520. if (!PACKET_get_net_2(&tmp, &length) ||
  521. !PACKET_get_bytes(&tmp, &data, (size_t)length)) {
  522. return 0;
  523. }
  524. *pkt = tmp;
  525. subpkt->curr = data;
  526. subpkt->remaining = length;
  527. return 1;
  528. }
  529. /*
  530. * Like PACKET_get_length_prefixed_2, but additionally, fails when there are
  531. * leftover bytes in |pkt|.
  532. */
  533. __owur static ossl_inline int PACKET_as_length_prefixed_2(PACKET *pkt,
  534. PACKET *subpkt)
  535. {
  536. unsigned int length;
  537. const unsigned char *data;
  538. PACKET tmp = *pkt;
  539. if (!PACKET_get_net_2(&tmp, &length) ||
  540. !PACKET_get_bytes(&tmp, &data, (size_t)length) ||
  541. PACKET_remaining(&tmp) != 0) {
  542. return 0;
  543. }
  544. *pkt = tmp;
  545. subpkt->curr = data;
  546. subpkt->remaining = length;
  547. return 1;
  548. }
  549. /*
  550. * Reads a variable-length vector prefixed with a three-byte length, and stores
  551. * the contents in |subpkt|. |pkt| can equal |subpkt|.
  552. * Data is not copied: the |subpkt| packet will share its underlying buffer with
  553. * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
  554. * Upon failure, the original |pkt| and |subpkt| are not modified.
  555. */
  556. __owur static ossl_inline int PACKET_get_length_prefixed_3(PACKET *pkt,
  557. PACKET *subpkt)
  558. {
  559. unsigned long length;
  560. const unsigned char *data;
  561. PACKET tmp = *pkt;
  562. if (!PACKET_get_net_3(&tmp, &length) ||
  563. !PACKET_get_bytes(&tmp, &data, (size_t)length)) {
  564. return 0;
  565. }
  566. *pkt = tmp;
  567. subpkt->curr = data;
  568. subpkt->remaining = length;
  569. return 1;
  570. }
  571. /*
  572. * Reads a variable-length vector prefixed with a QUIC variable-length integer
  573. * denoting the length, and stores the contents in |subpkt|. |pkt| can equal
  574. * |subpkt|. Data is not copied: the |subpkt| packet will share its underlying
  575. * buffer with the original |pkt|, so data wrapped by |pkt| must outlive the
  576. * |subpkt|. Upon failure, the original |pkt| and |subpkt| are not modified.
  577. */
  578. __owur static ossl_inline int PACKET_get_quic_length_prefixed(PACKET *pkt,
  579. PACKET *subpkt)
  580. {
  581. uint64_t length;
  582. const unsigned char *data;
  583. PACKET tmp = *pkt;
  584. if (!PACKET_get_quic_vlint(&tmp, &length) ||
  585. length > SIZE_MAX ||
  586. !PACKET_get_bytes(&tmp, &data, (size_t)length)) {
  587. return 0;
  588. }
  589. *pkt = tmp;
  590. subpkt->curr = data;
  591. subpkt->remaining = (size_t)length;
  592. return 1;
  593. }
  594. /* Writeable packets */
  595. typedef struct wpacket_sub WPACKET_SUB;
  596. struct wpacket_sub {
  597. /* The parent WPACKET_SUB if we have one or NULL otherwise */
  598. WPACKET_SUB *parent;
  599. /*
  600. * Offset into the buffer where the length of this WPACKET goes. We use an
  601. * offset in case the buffer grows and gets reallocated.
  602. */
  603. size_t packet_len;
  604. /* Number of bytes in the packet_len or 0 if we don't write the length */
  605. size_t lenbytes;
  606. /* Number of bytes written to the buf prior to this packet starting */
  607. size_t pwritten;
  608. /* Flags for this sub-packet */
  609. unsigned int flags;
  610. };
  611. typedef struct wpacket_st WPACKET;
  612. struct wpacket_st {
  613. /* The buffer where we store the output data */
  614. BUF_MEM *buf;
  615. /* Fixed sized buffer which can be used as an alternative to buf */
  616. unsigned char *staticbuf;
  617. /*
  618. * Offset into the buffer where we are currently writing. We use an offset
  619. * in case the buffer grows and gets reallocated.
  620. */
  621. size_t curr;
  622. /* Number of bytes written so far */
  623. size_t written;
  624. /* Maximum number of bytes we will allow to be written to this WPACKET */
  625. size_t maxsize;
  626. /* Our sub-packets (always at least one if not finished) */
  627. WPACKET_SUB *subs;
  628. /* Writing from the end first? */
  629. unsigned int endfirst : 1;
  630. };
  631. /* Flags */
  632. /* Default */
  633. #define WPACKET_FLAGS_NONE 0
  634. /* Error on WPACKET_close() if no data written to the WPACKET */
  635. #define WPACKET_FLAGS_NON_ZERO_LENGTH 1
  636. /*
  637. * Abandon all changes on WPACKET_close() if no data written to the WPACKET,
  638. * i.e. this does not write out a zero packet length
  639. */
  640. #define WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH 2
  641. /* QUIC variable-length integer length prefix */
  642. #define WPACKET_FLAGS_QUIC_VLINT 4
  643. /*
  644. * Initialise a WPACKET with the buffer in |buf|. The buffer must exist
  645. * for the whole time that the WPACKET is being used. Additionally |lenbytes| of
  646. * data is preallocated at the start of the buffer to store the length of the
  647. * WPACKET once we know it.
  648. */
  649. int WPACKET_init_len(WPACKET *pkt, BUF_MEM *buf, size_t lenbytes);
  650. /*
  651. * Same as WPACKET_init_len except there is no preallocation of the WPACKET
  652. * length.
  653. */
  654. int WPACKET_init(WPACKET *pkt, BUF_MEM *buf);
  655. /*
  656. * Same as WPACKET_init_len except there is no underlying buffer. No data is
  657. * ever actually written. We just keep track of how much data would have been
  658. * written if a buffer was there.
  659. */
  660. int WPACKET_init_null(WPACKET *pkt, size_t lenbytes);
  661. /*
  662. * Same as WPACKET_init_null except we set the WPACKET to assume DER length
  663. * encoding for sub-packets.
  664. */
  665. int WPACKET_init_null_der(WPACKET *pkt);
  666. /*
  667. * Same as WPACKET_init_len except we do not use a growable BUF_MEM structure.
  668. * A fixed buffer of memory |buf| of size |len| is used instead. A failure will
  669. * occur if you attempt to write beyond the end of the buffer
  670. */
  671. int WPACKET_init_static_len(WPACKET *pkt, unsigned char *buf, size_t len,
  672. size_t lenbytes);
  673. /*
  674. * Same as WPACKET_init_static_len except lenbytes is always 0, and we set the
  675. * WPACKET to write to the end of the buffer moving towards the start and use
  676. * DER length encoding for sub-packets.
  677. */
  678. int WPACKET_init_der(WPACKET *pkt, unsigned char *buf, size_t len);
  679. /*
  680. * Set the flags to be applied to the current sub-packet
  681. */
  682. int WPACKET_set_flags(WPACKET *pkt, unsigned int flags);
  683. /*
  684. * Closes the most recent sub-packet. It also writes out the length of the
  685. * packet to the required location (normally the start of the WPACKET) if
  686. * appropriate. The top level WPACKET should be closed using WPACKET_finish()
  687. * instead of this function.
  688. */
  689. int WPACKET_close(WPACKET *pkt);
  690. /*
  691. * The same as WPACKET_close() but only for the top most WPACKET. Additionally
  692. * frees memory resources for this WPACKET.
  693. */
  694. int WPACKET_finish(WPACKET *pkt);
  695. /*
  696. * Iterate through all the sub-packets and write out their lengths as if they
  697. * were being closed. The lengths will be overwritten with the final lengths
  698. * when the sub-packets are eventually closed (which may be different if more
  699. * data is added to the WPACKET). This function fails if a sub-packet is of 0
  700. * length and WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH is set.
  701. */
  702. int WPACKET_fill_lengths(WPACKET *pkt);
  703. /*
  704. * Initialise a new sub-packet. Additionally |lenbytes| of data is preallocated
  705. * at the start of the sub-packet to store its length once we know it. Don't
  706. * call this directly. Use the convenience macros below instead.
  707. */
  708. int WPACKET_start_sub_packet_len__(WPACKET *pkt, size_t lenbytes);
  709. /*
  710. * Convenience macros for calling WPACKET_start_sub_packet_len with different
  711. * lengths
  712. */
  713. #define WPACKET_start_sub_packet_u8(pkt) \
  714. WPACKET_start_sub_packet_len__((pkt), 1)
  715. #define WPACKET_start_sub_packet_u16(pkt) \
  716. WPACKET_start_sub_packet_len__((pkt), 2)
  717. #define WPACKET_start_sub_packet_u24(pkt) \
  718. WPACKET_start_sub_packet_len__((pkt), 3)
  719. #define WPACKET_start_sub_packet_u32(pkt) \
  720. WPACKET_start_sub_packet_len__((pkt), 4)
  721. /*
  722. * Same as WPACKET_start_sub_packet_len__() except no bytes are pre-allocated
  723. * for the sub-packet length.
  724. */
  725. int WPACKET_start_sub_packet(WPACKET *pkt);
  726. /*
  727. * Allocate bytes in the WPACKET for the output. This reserves the bytes
  728. * and counts them as "written", but doesn't actually do the writing. A pointer
  729. * to the allocated bytes is stored in |*allocbytes|. |allocbytes| may be NULL.
  730. * WARNING: the allocated bytes must be filled in immediately, without further
  731. * WPACKET_* calls. If not then the underlying buffer may be realloc'd and
  732. * change its location.
  733. */
  734. int WPACKET_allocate_bytes(WPACKET *pkt, size_t len,
  735. unsigned char **allocbytes);
  736. /*
  737. * The same as WPACKET_allocate_bytes() except additionally a new sub-packet is
  738. * started for the allocated bytes, and then closed immediately afterwards. The
  739. * number of length bytes for the sub-packet is in |lenbytes|. Don't call this
  740. * directly. Use the convenience macros below instead.
  741. */
  742. int WPACKET_sub_allocate_bytes__(WPACKET *pkt, size_t len,
  743. unsigned char **allocbytes, size_t lenbytes);
  744. /*
  745. * Convenience macros for calling WPACKET_sub_allocate_bytes with different
  746. * lengths
  747. */
  748. #define WPACKET_sub_allocate_bytes_u8(pkt, len, bytes) \
  749. WPACKET_sub_allocate_bytes__((pkt), (len), (bytes), 1)
  750. #define WPACKET_sub_allocate_bytes_u16(pkt, len, bytes) \
  751. WPACKET_sub_allocate_bytes__((pkt), (len), (bytes), 2)
  752. #define WPACKET_sub_allocate_bytes_u24(pkt, len, bytes) \
  753. WPACKET_sub_allocate_bytes__((pkt), (len), (bytes), 3)
  754. #define WPACKET_sub_allocate_bytes_u32(pkt, len, bytes) \
  755. WPACKET_sub_allocate_bytes__((pkt), (len), (bytes), 4)
  756. /*
  757. * The same as WPACKET_allocate_bytes() except the reserved bytes are not
  758. * actually counted as written. Typically this will be for when we don't know
  759. * how big arbitrary data is going to be up front, but we do know what the
  760. * maximum size will be. If this function is used, then it should be immediately
  761. * followed by a WPACKET_allocate_bytes() call before any other WPACKET
  762. * functions are called (unless the write to the allocated bytes is abandoned).
  763. *
  764. * For example: If we are generating a signature, then the size of that
  765. * signature may not be known in advance. We can use WPACKET_reserve_bytes() to
  766. * handle this:
  767. *
  768. * if (!WPACKET_sub_reserve_bytes_u16(&pkt, EVP_PKEY_get_size(pkey), &sigbytes1)
  769. * || EVP_SignFinal(md_ctx, sigbytes1, &siglen, pkey) <= 0
  770. * || !WPACKET_sub_allocate_bytes_u16(&pkt, siglen, &sigbytes2)
  771. * || sigbytes1 != sigbytes2)
  772. * goto err;
  773. */
  774. int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes);
  775. /*
  776. * The "reserve_bytes" equivalent of WPACKET_sub_allocate_bytes__()
  777. */
  778. int WPACKET_sub_reserve_bytes__(WPACKET *pkt, size_t len,
  779. unsigned char **allocbytes, size_t lenbytes);
  780. /*
  781. * Convenience macros for WPACKET_sub_reserve_bytes with different lengths
  782. */
  783. #define WPACKET_sub_reserve_bytes_u8(pkt, len, bytes) \
  784. WPACKET_reserve_bytes__((pkt), (len), (bytes), 1)
  785. #define WPACKET_sub_reserve_bytes_u16(pkt, len, bytes) \
  786. WPACKET_sub_reserve_bytes__((pkt), (len), (bytes), 2)
  787. #define WPACKET_sub_reserve_bytes_u24(pkt, len, bytes) \
  788. WPACKET_sub_reserve_bytes__((pkt), (len), (bytes), 3)
  789. #define WPACKET_sub_reserve_bytes_u32(pkt, len, bytes) \
  790. WPACKET_sub_reserve_bytes__((pkt), (len), (bytes), 4)
  791. /*
  792. * Write the value stored in |val| into the WPACKET. The value will consume
  793. * |bytes| amount of storage. An error will occur if |val| cannot be
  794. * accommodated in |bytes| storage, e.g. attempting to write the value 256 into
  795. * 1 byte will fail. Don't call this directly. Use the convenience macros below
  796. * instead.
  797. */
  798. int WPACKET_put_bytes__(WPACKET *pkt, uint64_t val, size_t bytes);
  799. /*
  800. * Convenience macros for calling WPACKET_put_bytes with different
  801. * lengths
  802. */
  803. #define WPACKET_put_bytes_u8(pkt, val) \
  804. WPACKET_put_bytes__((pkt), (val), 1)
  805. #define WPACKET_put_bytes_u16(pkt, val) \
  806. WPACKET_put_bytes__((pkt), (val), 2)
  807. #define WPACKET_put_bytes_u24(pkt, val) \
  808. WPACKET_put_bytes__((pkt), (val), 3)
  809. #define WPACKET_put_bytes_u32(pkt, val) \
  810. WPACKET_put_bytes__((pkt), (val), 4)
  811. #define WPACKET_put_bytes_u64(pkt, val) \
  812. WPACKET_put_bytes__((pkt), (val), 8)
  813. /* Set a maximum size that we will not allow the WPACKET to grow beyond */
  814. int WPACKET_set_max_size(WPACKET *pkt, size_t maxsize);
  815. /* Copy |len| bytes of data from |*src| into the WPACKET. */
  816. int WPACKET_memcpy(WPACKET *pkt, const void *src, size_t len);
  817. /* Set |len| bytes of data to |ch| into the WPACKET. */
  818. int WPACKET_memset(WPACKET *pkt, int ch, size_t len);
  819. /*
  820. * Copy |len| bytes of data from |*src| into the WPACKET and prefix with its
  821. * length (consuming |lenbytes| of data for the length). Don't call this
  822. * directly. Use the convenience macros below instead.
  823. */
  824. int WPACKET_sub_memcpy__(WPACKET *pkt, const void *src, size_t len,
  825. size_t lenbytes);
  826. /* Convenience macros for calling WPACKET_sub_memcpy with different lengths */
  827. #define WPACKET_sub_memcpy_u8(pkt, src, len) \
  828. WPACKET_sub_memcpy__((pkt), (src), (len), 1)
  829. #define WPACKET_sub_memcpy_u16(pkt, src, len) \
  830. WPACKET_sub_memcpy__((pkt), (src), (len), 2)
  831. #define WPACKET_sub_memcpy_u24(pkt, src, len) \
  832. WPACKET_sub_memcpy__((pkt), (src), (len), 3)
  833. #define WPACKET_sub_memcpy_u32(pkt, src, len) \
  834. WPACKET_sub_memcpy__((pkt), (src), (len), 4)
  835. /*
  836. * Return the total number of bytes written so far to the underlying buffer
  837. * including any storage allocated for length bytes
  838. */
  839. int WPACKET_get_total_written(WPACKET *pkt, size_t *written);
  840. /*
  841. * Returns the length of the current sub-packet. This excludes any bytes
  842. * allocated for the length itself.
  843. */
  844. int WPACKET_get_length(WPACKET *pkt, size_t *len);
  845. /*
  846. * Returns a pointer to the current write location, but does not allocate any
  847. * bytes.
  848. */
  849. unsigned char *WPACKET_get_curr(WPACKET *pkt);
  850. /* Returns true if the underlying buffer is actually NULL */
  851. int WPACKET_is_null_buf(WPACKET *pkt);
  852. /* Release resources in a WPACKET if a failure has occurred. */
  853. void WPACKET_cleanup(WPACKET *pkt);
  854. /*
  855. * Starts a QUIC sub-packet headed by a QUIC variable-length integer. A 4-byte
  856. * representation is used.
  857. */
  858. __owur int WPACKET_start_quic_sub_packet(WPACKET *pkt);
  859. /*
  860. * Starts a QUIC sub-packet headed by a QUIC variable-length integer. max_len
  861. * specifies the upper bound for the sub-packet size at the time the sub-packet
  862. * is closed, which determines the encoding size for tthe variable-length
  863. * integer header. max_len can be a precise figure or a worst-case bound
  864. * if a precise figure is not available.
  865. */
  866. __owur int WPACKET_start_quic_sub_packet_bound(WPACKET *pkt, size_t max_len);
  867. /*
  868. * Allocates a QUIC sub-packet with exactly len bytes of payload, headed by a
  869. * QUIC variable-length integer. The pointer to the payload buffer is output and
  870. * must be filled by the caller. This function assures optimal selection of
  871. * variable-length integer encoding length.
  872. */
  873. __owur int WPACKET_quic_sub_allocate_bytes(WPACKET *pkt, size_t len,
  874. unsigned char **bytes);
  875. /*
  876. * Write a QUIC variable-length integer to the packet.
  877. */
  878. __owur int WPACKET_quic_write_vlint(WPACKET *pkt, uint64_t v);
  879. #endif /* OSSL_INTERNAL_PACKET_H */