2
0

packet.h 27 KB

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