2
0

packet_locl.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. /* ssl/packet_locl.h */
  2. /*
  3. * Written by Matt Caswell for the OpenSSL project.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 2015 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * openssl-core@openssl.org.
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. *
  53. * This product includes cryptographic software written by Eric Young
  54. * (eay@cryptsoft.com). This product includes software written by Tim
  55. * Hudson (tjh@cryptsoft.com).
  56. *
  57. */
  58. #ifndef HEADER_PACKET_LOCL_H
  59. # define HEADER_PACKET_LOCL_H
  60. # include <string.h>
  61. # include <openssl/bn.h>
  62. # include <openssl/buffer.h>
  63. # include <openssl/crypto.h>
  64. # include <openssl/e_os2.h>
  65. # include "internal/numbers.h"
  66. # ifdef __cplusplus
  67. extern "C" {
  68. # endif
  69. typedef struct {
  70. /* Pointer to where we are currently reading from */
  71. unsigned char *curr;
  72. /* Number of bytes remaining */
  73. size_t remaining;
  74. } PACKET;
  75. /* Internal unchecked shorthand; don't use outside this file. */
  76. static ossl_inline void packet_forward(PACKET *pkt, size_t len)
  77. {
  78. pkt->curr += len;
  79. pkt->remaining -= len;
  80. }
  81. /*
  82. * Returns the number of bytes remaining to be read in the PACKET
  83. */
  84. static ossl_inline size_t PACKET_remaining(const PACKET *pkt)
  85. {
  86. return pkt->remaining;
  87. }
  88. /*
  89. * Returns a pointer to the PACKET's current position.
  90. * For use in non-PACKETized APIs.
  91. * TODO(openssl-team): this should return 'const unsigned char*' but can't
  92. * currently because legacy code passes 'unsigned char*'s around.
  93. */
  94. static ossl_inline unsigned char *PACKET_data(const PACKET *pkt)
  95. {
  96. return pkt->curr;
  97. }
  98. /*
  99. * Initialise a PACKET with |len| bytes held in |buf|. This does not make a
  100. * copy of the data so |buf| must be present for the whole time that the PACKET
  101. * is being used.
  102. */
  103. __owur static ossl_inline int PACKET_buf_init(PACKET *pkt, unsigned char *buf,
  104. size_t len)
  105. {
  106. /* Sanity check for negative values. */
  107. if (len > (size_t)(SIZE_MAX / 2))
  108. return 0;
  109. pkt->curr = buf;
  110. pkt->remaining = len;
  111. return 1;
  112. }
  113. /* Initialize a PACKET to hold zero bytes. */
  114. static ossl_inline void PACKET_null_init(PACKET *pkt)
  115. {
  116. pkt->curr = NULL;
  117. pkt->remaining = 0;
  118. }
  119. /*
  120. * Returns 1 if the packet has length |num| and its contents equal the |num|
  121. * bytes read from |ptr|. Returns 0 otherwise (lengths or contents not equal).
  122. * If lengths are equal, performs the comparison in constant time.
  123. */
  124. __owur static ossl_inline int PACKET_equal(const PACKET *pkt, const void *ptr,
  125. size_t num)
  126. {
  127. if (PACKET_remaining(pkt) != num)
  128. return 0;
  129. return CRYPTO_memcmp(pkt->curr, ptr, num) == 0;
  130. }
  131. /*
  132. * Peek ahead and initialize |subpkt| with the next |len| bytes read from |pkt|.
  133. * Data is not copied: the |subpkt| packet will share its underlying buffer with
  134. * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
  135. */
  136. __owur static ossl_inline int PACKET_peek_sub_packet(const PACKET *pkt,
  137. PACKET *subpkt,
  138. size_t len)
  139. {
  140. if (PACKET_remaining(pkt) < len)
  141. return 0;
  142. return PACKET_buf_init(subpkt, pkt->curr, len);
  143. }
  144. /*
  145. * Initialize |subpkt| with the next |len| bytes read from |pkt|. Data is not
  146. * copied: the |subpkt| packet will share its underlying buffer with the
  147. * original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
  148. */
  149. __owur static ossl_inline int PACKET_get_sub_packet(PACKET *pkt,
  150. PACKET *subpkt,
  151. size_t len)
  152. {
  153. if (!PACKET_peek_sub_packet(pkt, subpkt, len))
  154. return 0;
  155. packet_forward(pkt, len);
  156. return 1;
  157. }
  158. /*
  159. * Peek ahead at 2 bytes in network order from |pkt| and store the value in
  160. * |*data|
  161. */
  162. __owur static ossl_inline int PACKET_peek_net_2(const PACKET *pkt,
  163. unsigned int *data)
  164. {
  165. if (PACKET_remaining(pkt) < 2)
  166. return 0;
  167. *data = ((unsigned int)(*pkt->curr)) << 8;
  168. *data |= *(pkt->curr + 1);
  169. return 1;
  170. }
  171. /* Equivalent of n2s */
  172. /* Get 2 bytes in network order from |pkt| and store the value in |*data| */
  173. __owur static ossl_inline int PACKET_get_net_2(PACKET *pkt,
  174. unsigned int *data)
  175. {
  176. if (!PACKET_peek_net_2(pkt, data))
  177. return 0;
  178. packet_forward(pkt, 2);
  179. return 1;
  180. }
  181. /*
  182. * Peek ahead at 3 bytes in network order from |pkt| and store the value in
  183. * |*data|
  184. */
  185. __owur static ossl_inline int PACKET_peek_net_3(const PACKET *pkt,
  186. unsigned long *data)
  187. {
  188. if (PACKET_remaining(pkt) < 3)
  189. return 0;
  190. *data = ((unsigned long)(*pkt->curr)) << 16;
  191. *data |= ((unsigned long)(*(pkt->curr + 1))) << 8;
  192. *data |= *(pkt->curr + 2);
  193. return 1;
  194. }
  195. /* Equivalent of n2l3 */
  196. /* Get 3 bytes in network order from |pkt| and store the value in |*data| */
  197. __owur static ossl_inline int PACKET_get_net_3(PACKET *pkt,
  198. unsigned long *data)
  199. {
  200. if (!PACKET_peek_net_3(pkt, data))
  201. return 0;
  202. packet_forward(pkt, 3);
  203. return 1;
  204. }
  205. /*
  206. * Peek ahead at 4 bytes in network order from |pkt| and store the value in
  207. * |*data|
  208. */
  209. __owur static ossl_inline int PACKET_peek_net_4(const PACKET *pkt,
  210. unsigned long *data)
  211. {
  212. if (PACKET_remaining(pkt) < 4)
  213. return 0;
  214. *data = ((unsigned long)(*pkt->curr)) << 24;
  215. *data |= ((unsigned long)(*(pkt->curr + 1))) << 16;
  216. *data |= ((unsigned long)(*(pkt->curr + 2))) << 8;
  217. *data |= *(pkt->curr + 3);
  218. return 1;
  219. }
  220. /* Equivalent of n2l */
  221. /* Get 4 bytes in network order from |pkt| and store the value in |*data| */
  222. __owur static ossl_inline int PACKET_get_net_4(PACKET *pkt,
  223. unsigned long *data)
  224. {
  225. if (!PACKET_peek_net_4(pkt, data))
  226. return 0;
  227. packet_forward(pkt, 4);
  228. return 1;
  229. }
  230. /* Peek ahead at 1 byte from |pkt| and store the value in |*data| */
  231. __owur static ossl_inline int PACKET_peek_1(const PACKET *pkt,
  232. unsigned int *data)
  233. {
  234. if (!PACKET_remaining(pkt))
  235. return 0;
  236. *data = *pkt->curr;
  237. return 1;
  238. }
  239. /* Get 1 byte from |pkt| and store the value in |*data| */
  240. __owur static ossl_inline int PACKET_get_1(PACKET *pkt, unsigned int *data)
  241. {
  242. if (!PACKET_peek_1(pkt, data))
  243. return 0;
  244. packet_forward(pkt, 1);
  245. return 1;
  246. }
  247. /*
  248. * Peek ahead at 4 bytes in reverse network order from |pkt| and store the value
  249. * in |*data|
  250. */
  251. __owur static ossl_inline int PACKET_peek_4(const PACKET *pkt,
  252. unsigned long *data)
  253. {
  254. if (PACKET_remaining(pkt) < 4)
  255. return 0;
  256. *data = *pkt->curr;
  257. *data |= ((unsigned long)(*(pkt->curr + 1))) << 8;
  258. *data |= ((unsigned long)(*(pkt->curr + 2))) << 16;
  259. *data |= ((unsigned long)(*(pkt->curr + 3))) << 24;
  260. return 1;
  261. }
  262. /* Equivalent of c2l */
  263. /*
  264. * Get 4 bytes in reverse network order from |pkt| and store the value in
  265. * |*data|
  266. */
  267. __owur static ossl_inline int PACKET_get_4(PACKET *pkt, unsigned long *data)
  268. {
  269. if (!PACKET_peek_4(pkt, data))
  270. return 0;
  271. packet_forward(pkt, 4);
  272. return 1;
  273. }
  274. /*
  275. * Peek ahead at |len| bytes from the |pkt| and store a pointer to them in
  276. * |*data|. This just points at the underlying buffer that |pkt| is using. The
  277. * caller should not free this data directly (it will be freed when the
  278. * underlying buffer gets freed
  279. */
  280. __owur static ossl_inline int PACKET_peek_bytes(const PACKET *pkt,
  281. unsigned char **data,
  282. size_t len)
  283. {
  284. if (PACKET_remaining(pkt) < len)
  285. return 0;
  286. *data = pkt->curr;
  287. return 1;
  288. }
  289. /*
  290. * Read |len| bytes from the |pkt| and store a pointer to them in |*data|. This
  291. * just points at the underlying buffer that |pkt| is using. The caller should
  292. * not free this data directly (it will be freed when the underlying buffer gets
  293. * freed
  294. */
  295. __owur static ossl_inline int PACKET_get_bytes(PACKET *pkt,
  296. unsigned char **data,
  297. size_t len)
  298. {
  299. if (!PACKET_peek_bytes(pkt, data, len))
  300. return 0;
  301. packet_forward(pkt, len);
  302. return 1;
  303. }
  304. /* Peek ahead at |len| bytes from |pkt| and copy them to |data| */
  305. __owur static ossl_inline int PACKET_peek_copy_bytes(const PACKET *pkt,
  306. unsigned char *data,
  307. size_t len)
  308. {
  309. if (PACKET_remaining(pkt) < len)
  310. return 0;
  311. memcpy(data, pkt->curr, len);
  312. return 1;
  313. }
  314. /*
  315. * Read |len| bytes from |pkt| and copy them to |data|.
  316. * The caller is responsible for ensuring that |data| can hold |len| bytes.
  317. */
  318. __owur static ossl_inline int PACKET_copy_bytes(PACKET *pkt,
  319. unsigned char *data,
  320. size_t len)
  321. {
  322. if (!PACKET_peek_copy_bytes(pkt, data, len))
  323. return 0;
  324. packet_forward(pkt, len);
  325. return 1;
  326. }
  327. /*
  328. * Copy packet data to |dest|, and set |len| to the number of copied bytes.
  329. * If the packet has more than |dest_len| bytes, nothing is copied.
  330. * Returns 1 if the packet data fits in |dest_len| bytes, 0 otherwise.
  331. * Does not forward PACKET position (because it is typically the last thing
  332. * done with a given PACKET).
  333. */
  334. __owur static ossl_inline int PACKET_copy_all(const PACKET *pkt,
  335. unsigned char *dest,
  336. size_t dest_len, size_t *len)
  337. {
  338. if (PACKET_remaining(pkt) > dest_len) {
  339. *len = 0;
  340. return 0;
  341. }
  342. *len = pkt->remaining;
  343. memcpy(dest, pkt->curr, pkt->remaining);
  344. return 1;
  345. }
  346. /*
  347. * Copy |pkt| bytes to a newly allocated buffer and store a pointer to the
  348. * result in |*data|, and the length in |len|.
  349. * If |*data| is not NULL, the old data is OPENSSL_free'd.
  350. * If the packet is empty, or malloc fails, |*data| will be set to NULL.
  351. * Returns 1 if the malloc succeeds and 0 otherwise.
  352. * Does not forward PACKET position (because it is typically the last thing
  353. * done with a given PACKET).
  354. */
  355. __owur static ossl_inline int PACKET_memdup(const PACKET *pkt,
  356. unsigned char **data, size_t *len)
  357. {
  358. size_t length;
  359. OPENSSL_free(*data);
  360. *data = NULL;
  361. *len = 0;
  362. length = PACKET_remaining(pkt);
  363. if (length == 0)
  364. return 1;
  365. *data = OPENSSL_memdup(pkt->curr, length);
  366. if (*data == NULL)
  367. return 0;
  368. *len = length;
  369. return 1;
  370. }
  371. /*
  372. * Read a C string from |pkt| and copy to a newly allocated, NUL-terminated
  373. * buffer. Store a pointer to the result in |*data|.
  374. * If |*data| is not NULL, the old data is OPENSSL_free'd.
  375. * If the data in |pkt| does not contain a NUL-byte, the entire data is
  376. * copied and NUL-terminated.
  377. * Returns 1 if the malloc succeeds and 0 otherwise.
  378. * Does not forward PACKET position (because it is typically the last thing done
  379. * with a given PACKET).
  380. */
  381. __owur static ossl_inline int PACKET_strndup(const PACKET *pkt, char **data)
  382. {
  383. OPENSSL_free(*data);
  384. /* This will succeed on an empty packet, unless pkt->curr == NULL. */
  385. *data = OPENSSL_strndup((const char *)pkt->curr, PACKET_remaining(pkt));
  386. return (*data != NULL);
  387. }
  388. /* Move the current reading position forward |len| bytes */
  389. __owur static ossl_inline int PACKET_forward(PACKET *pkt, size_t len)
  390. {
  391. if (PACKET_remaining(pkt) < len)
  392. return 0;
  393. packet_forward(pkt, len);
  394. return 1;
  395. }
  396. /*
  397. * Reads a variable-length vector prefixed with a one-byte length, and stores
  398. * the contents in |subpkt|. |pkt| can equal |subpkt|.
  399. * Data is not copied: the |subpkt| packet will share its underlying buffer with
  400. * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
  401. * Upon failure, the original |pkt| and |subpkt| are not modified.
  402. */
  403. __owur static ossl_inline int PACKET_get_length_prefixed_1(PACKET *pkt,
  404. PACKET *subpkt)
  405. {
  406. unsigned int length;
  407. unsigned char *data;
  408. PACKET tmp = *pkt;
  409. if (!PACKET_get_1(&tmp, &length) ||
  410. !PACKET_get_bytes(&tmp, &data, (size_t)length)) {
  411. return 0;
  412. }
  413. *pkt = tmp;
  414. subpkt->curr = data;
  415. subpkt->remaining = length;
  416. return 1;
  417. }
  418. /*
  419. * Reads a variable-length vector prefixed with a two-byte length, and stores
  420. * the contents in |subpkt|. |pkt| can equal |subpkt|.
  421. * Data is not copied: the |subpkt| packet will share its underlying buffer with
  422. * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
  423. * Upon failure, the original |pkt| and |subpkt| are not modified.
  424. */
  425. __owur static ossl_inline int PACKET_get_length_prefixed_2(PACKET *pkt,
  426. PACKET *subpkt)
  427. {
  428. unsigned int length;
  429. unsigned char *data;
  430. PACKET tmp = *pkt;
  431. if (!PACKET_get_net_2(&tmp, &length) ||
  432. !PACKET_get_bytes(&tmp, &data, (size_t)length)) {
  433. return 0;
  434. }
  435. *pkt = tmp;
  436. subpkt->curr = data;
  437. subpkt->remaining = length;
  438. return 1;
  439. }
  440. /*
  441. * Reads a variable-length vector prefixed with a three-byte length, and stores
  442. * the contents in |subpkt|. |pkt| can equal |subpkt|.
  443. * Data is not copied: the |subpkt| packet will share its underlying buffer with
  444. * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
  445. * Upon failure, the original |pkt| and |subpkt| are not modified.
  446. */
  447. __owur static ossl_inline int PACKET_get_length_prefixed_3(PACKET *pkt,
  448. PACKET *subpkt)
  449. {
  450. unsigned long length;
  451. unsigned char *data;
  452. PACKET tmp = *pkt;
  453. if (!PACKET_get_net_3(&tmp, &length) ||
  454. !PACKET_get_bytes(&tmp, &data, (size_t)length)) {
  455. return 0;
  456. }
  457. *pkt = tmp;
  458. subpkt->curr = data;
  459. subpkt->remaining = length;
  460. return 1;
  461. }
  462. # ifdef __cplusplus
  463. }
  464. # endif
  465. #endif /* HEADER_PACKET_LOCL_H */