packet_local.h 27 KB

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