packet_locl.h 26 KB

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