2
0

packet_locl.h 26 KB

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