packet.h 28 KB

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