[331] | 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 |
|
---|
| 10 | #ifndef HEADER_PACKET_LOCL_H
|
---|
| 11 | # define HEADER_PACKET_LOCL_H
|
---|
| 12 |
|
---|
| 13 | # include <string.h>
|
---|
| 14 | # include <openssl/bn.h>
|
---|
| 15 | # include <openssl/buffer.h>
|
---|
| 16 | # include <openssl/crypto.h>
|
---|
| 17 | # include <openssl/e_os2.h>
|
---|
| 18 |
|
---|
| 19 | # include "internal/numbers.h"
|
---|
| 20 |
|
---|
| 21 | # ifdef __cplusplus
|
---|
| 22 | extern "C" {
|
---|
| 23 | # endif
|
---|
| 24 |
|
---|
| 25 | typedef struct {
|
---|
| 26 | /* Pointer to where we are currently reading from */
|
---|
| 27 | const unsigned char *curr;
|
---|
| 28 | /* Number of bytes remaining */
|
---|
| 29 | size_t remaining;
|
---|
| 30 | } PACKET;
|
---|
| 31 |
|
---|
| 32 | /* Internal unchecked shorthand; don't use outside this file. */
|
---|
| 33 | static ossl_inline void packet_forward(PACKET *pkt, size_t len)
|
---|
| 34 | {
|
---|
| 35 | pkt->curr += len;
|
---|
| 36 | pkt->remaining -= len;
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | /*
|
---|
| 40 | * Returns the number of bytes remaining to be read in the PACKET
|
---|
| 41 | */
|
---|
| 42 | static ossl_inline size_t PACKET_remaining(const PACKET *pkt)
|
---|
| 43 | {
|
---|
| 44 | return pkt->remaining;
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | /*
|
---|
| 48 | * Returns a pointer to the first byte after the packet data.
|
---|
| 49 | * Useful for integrating with non-PACKET parsing code.
|
---|
| 50 | * Specifically, we use PACKET_end() to verify that a d2i_... call
|
---|
| 51 | * has consumed the entire packet contents.
|
---|
| 52 | */
|
---|
| 53 | static ossl_inline const unsigned char *PACKET_end(const PACKET *pkt)
|
---|
| 54 | {
|
---|
| 55 | return pkt->curr + pkt->remaining;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | /*
|
---|
| 59 | * Returns a pointer to the PACKET's current position.
|
---|
| 60 | * For use in non-PACKETized APIs.
|
---|
| 61 | */
|
---|
| 62 | static ossl_inline const unsigned char *PACKET_data(const PACKET *pkt)
|
---|
| 63 | {
|
---|
| 64 | return pkt->curr;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | /*
|
---|
| 68 | * Initialise a PACKET with |len| bytes held in |buf|. This does not make a
|
---|
| 69 | * copy of the data so |buf| must be present for the whole time that the PACKET
|
---|
| 70 | * is being used.
|
---|
| 71 | */
|
---|
| 72 | __owur static ossl_inline int PACKET_buf_init(PACKET *pkt,
|
---|
| 73 | const unsigned char *buf,
|
---|
| 74 | size_t len)
|
---|
| 75 | {
|
---|
| 76 | /* Sanity check for negative values. */
|
---|
| 77 | if (len > (size_t)(SIZE_MAX / 2))
|
---|
| 78 | return 0;
|
---|
| 79 |
|
---|
| 80 | pkt->curr = buf;
|
---|
| 81 | pkt->remaining = len;
|
---|
| 82 | return 1;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | /* Initialize a PACKET to hold zero bytes. */
|
---|
| 86 | static ossl_inline void PACKET_null_init(PACKET *pkt)
|
---|
| 87 | {
|
---|
| 88 | pkt->curr = NULL;
|
---|
| 89 | pkt->remaining = 0;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | /*
|
---|
| 93 | * Returns 1 if the packet has length |num| and its contents equal the |num|
|
---|
| 94 | * bytes read from |ptr|. Returns 0 otherwise (lengths or contents not equal).
|
---|
| 95 | * If lengths are equal, performs the comparison in constant time.
|
---|
| 96 | */
|
---|
| 97 | __owur static ossl_inline int PACKET_equal(const PACKET *pkt, const void *ptr,
|
---|
| 98 | size_t num)
|
---|
| 99 | {
|
---|
| 100 | if (PACKET_remaining(pkt) != num)
|
---|
| 101 | return 0;
|
---|
| 102 | return CRYPTO_memcmp(pkt->curr, ptr, num) == 0;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | /*
|
---|
| 106 | * Peek ahead and initialize |subpkt| with the next |len| bytes read from |pkt|.
|
---|
| 107 | * Data is not copied: the |subpkt| packet will share its underlying buffer with
|
---|
| 108 | * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
|
---|
| 109 | */
|
---|
| 110 | __owur static ossl_inline int PACKET_peek_sub_packet(const PACKET *pkt,
|
---|
| 111 | PACKET *subpkt, size_t len)
|
---|
| 112 | {
|
---|
| 113 | if (PACKET_remaining(pkt) < len)
|
---|
| 114 | return 0;
|
---|
| 115 |
|
---|
| 116 | return PACKET_buf_init(subpkt, pkt->curr, len);
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | /*
|
---|
| 120 | * Initialize |subpkt| with the next |len| bytes read from |pkt|. Data is not
|
---|
| 121 | * copied: the |subpkt| packet will share its underlying buffer with the
|
---|
| 122 | * original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
|
---|
| 123 | */
|
---|
| 124 | __owur static ossl_inline int PACKET_get_sub_packet(PACKET *pkt,
|
---|
| 125 | PACKET *subpkt, size_t len)
|
---|
| 126 | {
|
---|
| 127 | if (!PACKET_peek_sub_packet(pkt, subpkt, len))
|
---|
| 128 | return 0;
|
---|
| 129 |
|
---|
| 130 | packet_forward(pkt, len);
|
---|
| 131 |
|
---|
| 132 | return 1;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | /*
|
---|
| 136 | * Peek ahead at 2 bytes in network order from |pkt| and store the value in
|
---|
| 137 | * |*data|
|
---|
| 138 | */
|
---|
| 139 | __owur static ossl_inline int PACKET_peek_net_2(const PACKET *pkt,
|
---|
| 140 | unsigned int *data)
|
---|
| 141 | {
|
---|
| 142 | if (PACKET_remaining(pkt) < 2)
|
---|
| 143 | return 0;
|
---|
| 144 |
|
---|
| 145 | *data = ((unsigned int)(*pkt->curr)) << 8;
|
---|
| 146 | *data |= *(pkt->curr + 1);
|
---|
| 147 |
|
---|
| 148 | return 1;
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | /* Equivalent of n2s */
|
---|
| 152 | /* Get 2 bytes in network order from |pkt| and store the value in |*data| */
|
---|
| 153 | __owur static ossl_inline int PACKET_get_net_2(PACKET *pkt, unsigned int *data)
|
---|
| 154 | {
|
---|
| 155 | if (!PACKET_peek_net_2(pkt, data))
|
---|
| 156 | return 0;
|
---|
| 157 |
|
---|
| 158 | packet_forward(pkt, 2);
|
---|
| 159 |
|
---|
| 160 | return 1;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | /*
|
---|
| 164 | * Peek ahead at 3 bytes in network order from |pkt| and store the value in
|
---|
| 165 | * |*data|
|
---|
| 166 | */
|
---|
| 167 | __owur static ossl_inline int PACKET_peek_net_3(const PACKET *pkt,
|
---|
| 168 | unsigned long *data)
|
---|
| 169 | {
|
---|
| 170 | if (PACKET_remaining(pkt) < 3)
|
---|
| 171 | return 0;
|
---|
| 172 |
|
---|
| 173 | *data = ((unsigned long)(*pkt->curr)) << 16;
|
---|
| 174 | *data |= ((unsigned long)(*(pkt->curr + 1))) << 8;
|
---|
| 175 | *data |= *(pkt->curr + 2);
|
---|
| 176 |
|
---|
| 177 | return 1;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | /* Equivalent of n2l3 */
|
---|
| 181 | /* Get 3 bytes in network order from |pkt| and store the value in |*data| */
|
---|
| 182 | __owur static ossl_inline int PACKET_get_net_3(PACKET *pkt, unsigned long *data)
|
---|
| 183 | {
|
---|
| 184 | if (!PACKET_peek_net_3(pkt, data))
|
---|
| 185 | return 0;
|
---|
| 186 |
|
---|
| 187 | packet_forward(pkt, 3);
|
---|
| 188 |
|
---|
| 189 | return 1;
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | /*
|
---|
| 193 | * Peek ahead at 4 bytes in network order from |pkt| and store the value in
|
---|
| 194 | * |*data|
|
---|
| 195 | */
|
---|
| 196 | __owur static ossl_inline int PACKET_peek_net_4(const PACKET *pkt,
|
---|
| 197 | unsigned long *data)
|
---|
| 198 | {
|
---|
| 199 | if (PACKET_remaining(pkt) < 4)
|
---|
| 200 | return 0;
|
---|
| 201 |
|
---|
| 202 | *data = ((unsigned long)(*pkt->curr)) << 24;
|
---|
| 203 | *data |= ((unsigned long)(*(pkt->curr + 1))) << 16;
|
---|
| 204 | *data |= ((unsigned long)(*(pkt->curr + 2))) << 8;
|
---|
| 205 | *data |= *(pkt->curr + 3);
|
---|
| 206 |
|
---|
| 207 | return 1;
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | /* Equivalent of n2l */
|
---|
| 211 | /* Get 4 bytes in network order from |pkt| and store the value in |*data| */
|
---|
| 212 | __owur static ossl_inline int PACKET_get_net_4(PACKET *pkt, unsigned long *data)
|
---|
| 213 | {
|
---|
| 214 | if (!PACKET_peek_net_4(pkt, data))
|
---|
| 215 | return 0;
|
---|
| 216 |
|
---|
| 217 | packet_forward(pkt, 4);
|
---|
| 218 |
|
---|
| 219 | return 1;
|
---|
| 220 | }
|
---|
| 221 |
|
---|
| 222 | /* Peek ahead at 1 byte from |pkt| and store the value in |*data| */
|
---|
| 223 | __owur static ossl_inline int PACKET_peek_1(const PACKET *pkt,
|
---|
| 224 | unsigned int *data)
|
---|
| 225 | {
|
---|
| 226 | if (!PACKET_remaining(pkt))
|
---|
| 227 | return 0;
|
---|
| 228 |
|
---|
| 229 | *data = *pkt->curr;
|
---|
| 230 |
|
---|
| 231 | return 1;
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 | /* Get 1 byte from |pkt| and store the value in |*data| */
|
---|
| 235 | __owur static ossl_inline int PACKET_get_1(PACKET *pkt, unsigned int *data)
|
---|
| 236 | {
|
---|
| 237 | if (!PACKET_peek_1(pkt, data))
|
---|
| 238 | return 0;
|
---|
| 239 |
|
---|
| 240 | packet_forward(pkt, 1);
|
---|
| 241 |
|
---|
| 242 | return 1;
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | /*
|
---|
| 246 | * Peek ahead at 4 bytes in reverse network order from |pkt| and store the value
|
---|
| 247 | * in |*data|
|
---|
| 248 | */
|
---|
| 249 | __owur static ossl_inline int PACKET_peek_4(const PACKET *pkt,
|
---|
| 250 | unsigned long *data)
|
---|
| 251 | {
|
---|
| 252 | if (PACKET_remaining(pkt) < 4)
|
---|
| 253 | return 0;
|
---|
| 254 |
|
---|
| 255 | *data = *pkt->curr;
|
---|
| 256 | *data |= ((unsigned long)(*(pkt->curr + 1))) << 8;
|
---|
| 257 | *data |= ((unsigned long)(*(pkt->curr + 2))) << 16;
|
---|
| 258 | *data |= ((unsigned long)(*(pkt->curr + 3))) << 24;
|
---|
| 259 |
|
---|
| 260 | return 1;
|
---|
| 261 | }
|
---|
| 262 |
|
---|
| 263 | /* Equivalent of c2l */
|
---|
| 264 | /*
|
---|
| 265 | * Get 4 bytes in reverse network order from |pkt| and store the value in
|
---|
| 266 | * |*data|
|
---|
| 267 | */
|
---|
| 268 | __owur static ossl_inline int PACKET_get_4(PACKET *pkt, unsigned long *data)
|
---|
| 269 | {
|
---|
| 270 | if (!PACKET_peek_4(pkt, data))
|
---|
| 271 | return 0;
|
---|
| 272 |
|
---|
| 273 | packet_forward(pkt, 4);
|
---|
| 274 |
|
---|
| 275 | return 1;
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 | /*
|
---|
| 279 | * Peek ahead at |len| bytes from the |pkt| and store a pointer to them in
|
---|
| 280 | * |*data|. This just points at the underlying buffer that |pkt| is using. The
|
---|
| 281 | * caller should not free this data directly (it will be freed when the
|
---|
| 282 | * underlying buffer gets freed
|
---|
| 283 | */
|
---|
| 284 | __owur static ossl_inline int PACKET_peek_bytes(const PACKET *pkt,
|
---|
| 285 | const unsigned char **data,
|
---|
| 286 | size_t len)
|
---|
| 287 | {
|
---|
| 288 | if (PACKET_remaining(pkt) < len)
|
---|
| 289 | return 0;
|
---|
| 290 |
|
---|
| 291 | *data = pkt->curr;
|
---|
| 292 |
|
---|
| 293 | return 1;
|
---|
| 294 | }
|
---|
| 295 |
|
---|
| 296 | /*
|
---|
| 297 | * Read |len| bytes from the |pkt| and store a pointer to them in |*data|. This
|
---|
| 298 | * just points at the underlying buffer that |pkt| is using. The caller should
|
---|
| 299 | * not free this data directly (it will be freed when the underlying buffer gets
|
---|
| 300 | * freed
|
---|
| 301 | */
|
---|
| 302 | __owur static ossl_inline int PACKET_get_bytes(PACKET *pkt,
|
---|
| 303 | const unsigned char **data,
|
---|
| 304 | size_t len)
|
---|
| 305 | {
|
---|
| 306 | if (!PACKET_peek_bytes(pkt, data, len))
|
---|
| 307 | return 0;
|
---|
| 308 |
|
---|
| 309 | packet_forward(pkt, len);
|
---|
| 310 |
|
---|
| 311 | return 1;
|
---|
| 312 | }
|
---|
| 313 |
|
---|
| 314 | /* Peek ahead at |len| bytes from |pkt| and copy them to |data| */
|
---|
| 315 | __owur static ossl_inline int PACKET_peek_copy_bytes(const PACKET *pkt,
|
---|
| 316 | unsigned char *data,
|
---|
| 317 | size_t len)
|
---|
| 318 | {
|
---|
| 319 | if (PACKET_remaining(pkt) < len)
|
---|
| 320 | return 0;
|
---|
| 321 |
|
---|
| 322 | memcpy(data, pkt->curr, len);
|
---|
| 323 |
|
---|
| 324 | return 1;
|
---|
| 325 | }
|
---|
| 326 |
|
---|
| 327 | /*
|
---|
| 328 | * Read |len| bytes from |pkt| and copy them to |data|.
|
---|
| 329 | * The caller is responsible for ensuring that |data| can hold |len| bytes.
|
---|
| 330 | */
|
---|
| 331 | __owur static ossl_inline int PACKET_copy_bytes(PACKET *pkt,
|
---|
| 332 | unsigned char *data, size_t len)
|
---|
| 333 | {
|
---|
| 334 | if (!PACKET_peek_copy_bytes(pkt, data, len))
|
---|
| 335 | return 0;
|
---|
| 336 |
|
---|
| 337 | packet_forward(pkt, len);
|
---|
| 338 |
|
---|
| 339 | return 1;
|
---|
| 340 | }
|
---|
| 341 |
|
---|
| 342 | /*
|
---|
| 343 | * Copy packet data to |dest|, and set |len| to the number of copied bytes.
|
---|
| 344 | * If the packet has more than |dest_len| bytes, nothing is copied.
|
---|
| 345 | * Returns 1 if the packet data fits in |dest_len| bytes, 0 otherwise.
|
---|
| 346 | * Does not forward PACKET position (because it is typically the last thing
|
---|
| 347 | * done with a given PACKET).
|
---|
| 348 | */
|
---|
| 349 | __owur static ossl_inline int PACKET_copy_all(const PACKET *pkt,
|
---|
| 350 | unsigned char *dest,
|
---|
| 351 | size_t dest_len, size_t *len)
|
---|
| 352 | {
|
---|
| 353 | if (PACKET_remaining(pkt) > dest_len) {
|
---|
| 354 | *len = 0;
|
---|
| 355 | return 0;
|
---|
| 356 | }
|
---|
| 357 | *len = pkt->remaining;
|
---|
| 358 | memcpy(dest, pkt->curr, pkt->remaining);
|
---|
| 359 | return 1;
|
---|
| 360 | }
|
---|
| 361 |
|
---|
| 362 | /*
|
---|
| 363 | * Copy |pkt| bytes to a newly allocated buffer and store a pointer to the
|
---|
| 364 | * result in |*data|, and the length in |len|.
|
---|
| 365 | * If |*data| is not NULL, the old data is OPENSSL_free'd.
|
---|
| 366 | * If the packet is empty, or malloc fails, |*data| will be set to NULL.
|
---|
| 367 | * Returns 1 if the malloc succeeds and 0 otherwise.
|
---|
| 368 | * Does not forward PACKET position (because it is typically the last thing
|
---|
| 369 | * done with a given PACKET).
|
---|
| 370 | */
|
---|
| 371 | __owur static ossl_inline int PACKET_memdup(const PACKET *pkt,
|
---|
| 372 | unsigned char **data, size_t *len)
|
---|
| 373 | {
|
---|
| 374 | size_t length;
|
---|
| 375 |
|
---|
| 376 | OPENSSL_free(*data);
|
---|
| 377 | *data = NULL;
|
---|
| 378 | *len = 0;
|
---|
| 379 |
|
---|
| 380 | length = PACKET_remaining(pkt);
|
---|
| 381 |
|
---|
| 382 | if (length == 0)
|
---|
| 383 | return 1;
|
---|
| 384 |
|
---|
| 385 | *data = OPENSSL_memdup(pkt->curr, length);
|
---|
| 386 | if (*data == NULL)
|
---|
| 387 | return 0;
|
---|
| 388 |
|
---|
| 389 | *len = length;
|
---|
| 390 | return 1;
|
---|
| 391 | }
|
---|
| 392 |
|
---|
| 393 | /*
|
---|
| 394 | * Read a C string from |pkt| and copy to a newly allocated, NUL-terminated
|
---|
| 395 | * buffer. Store a pointer to the result in |*data|.
|
---|
| 396 | * If |*data| is not NULL, the old data is OPENSSL_free'd.
|
---|
| 397 | * If the data in |pkt| does not contain a NUL-byte, the entire data is
|
---|
| 398 | * copied and NUL-terminated.
|
---|
| 399 | * Returns 1 if the malloc succeeds and 0 otherwise.
|
---|
| 400 | * Does not forward PACKET position (because it is typically the last thing done
|
---|
| 401 | * with a given PACKET).
|
---|
| 402 | */
|
---|
| 403 | __owur static ossl_inline int PACKET_strndup(const PACKET *pkt, char **data)
|
---|
| 404 | {
|
---|
| 405 | OPENSSL_free(*data);
|
---|
| 406 |
|
---|
| 407 | /* This will succeed on an empty packet, unless pkt->curr == NULL. */
|
---|
| 408 | *data = OPENSSL_strndup((const char *)pkt->curr, PACKET_remaining(pkt));
|
---|
| 409 | return (*data != NULL);
|
---|
| 410 | }
|
---|
| 411 |
|
---|
| 412 | /* Returns 1 if |pkt| contains at least one 0-byte, 0 otherwise. */
|
---|
| 413 | static ossl_inline int PACKET_contains_zero_byte(const PACKET *pkt)
|
---|
| 414 | {
|
---|
| 415 | return memchr(pkt->curr, 0, pkt->remaining) != NULL;
|
---|
| 416 | }
|
---|
| 417 |
|
---|
| 418 | /* Move the current reading position forward |len| bytes */
|
---|
| 419 | __owur static ossl_inline int PACKET_forward(PACKET *pkt, size_t len)
|
---|
| 420 | {
|
---|
| 421 | if (PACKET_remaining(pkt) < len)
|
---|
| 422 | return 0;
|
---|
| 423 |
|
---|
| 424 | packet_forward(pkt, len);
|
---|
| 425 |
|
---|
| 426 | return 1;
|
---|
| 427 | }
|
---|
| 428 |
|
---|
| 429 | /*
|
---|
| 430 | * Reads a variable-length vector prefixed with a one-byte length, and stores
|
---|
| 431 | * the contents in |subpkt|. |pkt| can equal |subpkt|.
|
---|
| 432 | * Data is not copied: the |subpkt| packet will share its underlying buffer with
|
---|
| 433 | * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
|
---|
| 434 | * Upon failure, the original |pkt| and |subpkt| are not modified.
|
---|
| 435 | */
|
---|
| 436 | __owur static ossl_inline int PACKET_get_length_prefixed_1(PACKET *pkt,
|
---|
| 437 | PACKET *subpkt)
|
---|
| 438 | {
|
---|
| 439 | unsigned int length;
|
---|
| 440 | const unsigned char *data;
|
---|
| 441 | PACKET tmp = *pkt;
|
---|
| 442 | if (!PACKET_get_1(&tmp, &length) ||
|
---|
| 443 | !PACKET_get_bytes(&tmp, &data, (size_t)length)) {
|
---|
| 444 | return 0;
|
---|
| 445 | }
|
---|
| 446 |
|
---|
| 447 | *pkt = tmp;
|
---|
| 448 | subpkt->curr = data;
|
---|
| 449 | subpkt->remaining = length;
|
---|
| 450 |
|
---|
| 451 | return 1;
|
---|
| 452 | }
|
---|
| 453 |
|
---|
| 454 | /*
|
---|
| 455 | * Like PACKET_get_length_prefixed_1, but additionally, fails when there are
|
---|
| 456 | * leftover bytes in |pkt|.
|
---|
| 457 | */
|
---|
| 458 | __owur static ossl_inline int PACKET_as_length_prefixed_1(PACKET *pkt,
|
---|
| 459 | PACKET *subpkt)
|
---|
| 460 | {
|
---|
| 461 | unsigned int length;
|
---|
| 462 | const unsigned char *data;
|
---|
| 463 | PACKET tmp = *pkt;
|
---|
| 464 | if (!PACKET_get_1(&tmp, &length) ||
|
---|
| 465 | !PACKET_get_bytes(&tmp, &data, (size_t)length) ||
|
---|
| 466 | PACKET_remaining(&tmp) != 0) {
|
---|
| 467 | return 0;
|
---|
| 468 | }
|
---|
| 469 |
|
---|
| 470 | *pkt = tmp;
|
---|
| 471 | subpkt->curr = data;
|
---|
| 472 | subpkt->remaining = length;
|
---|
| 473 |
|
---|
| 474 | return 1;
|
---|
| 475 | }
|
---|
| 476 |
|
---|
| 477 | /*
|
---|
| 478 | * Reads a variable-length vector prefixed with a two-byte length, and stores
|
---|
| 479 | * the contents in |subpkt|. |pkt| can equal |subpkt|.
|
---|
| 480 | * Data is not copied: the |subpkt| packet will share its underlying buffer with
|
---|
| 481 | * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
|
---|
| 482 | * Upon failure, the original |pkt| and |subpkt| are not modified.
|
---|
| 483 | */
|
---|
| 484 | __owur static ossl_inline int PACKET_get_length_prefixed_2(PACKET *pkt,
|
---|
| 485 | PACKET *subpkt)
|
---|
| 486 | {
|
---|
| 487 | unsigned int length;
|
---|
| 488 | const unsigned char *data;
|
---|
| 489 | PACKET tmp = *pkt;
|
---|
| 490 |
|
---|
| 491 | if (!PACKET_get_net_2(&tmp, &length) ||
|
---|
| 492 | !PACKET_get_bytes(&tmp, &data, (size_t)length)) {
|
---|
| 493 | return 0;
|
---|
| 494 | }
|
---|
| 495 |
|
---|
| 496 | *pkt = tmp;
|
---|
| 497 | subpkt->curr = data;
|
---|
| 498 | subpkt->remaining = length;
|
---|
| 499 |
|
---|
| 500 | return 1;
|
---|
| 501 | }
|
---|
| 502 |
|
---|
| 503 | /*
|
---|
| 504 | * Like PACKET_get_length_prefixed_2, but additionally, fails when there are
|
---|
| 505 | * leftover bytes in |pkt|.
|
---|
| 506 | */
|
---|
| 507 | __owur static ossl_inline int PACKET_as_length_prefixed_2(PACKET *pkt,
|
---|
| 508 | PACKET *subpkt)
|
---|
| 509 | {
|
---|
| 510 | unsigned int length;
|
---|
| 511 | const unsigned char *data;
|
---|
| 512 | PACKET tmp = *pkt;
|
---|
| 513 |
|
---|
| 514 | if (!PACKET_get_net_2(&tmp, &length) ||
|
---|
| 515 | !PACKET_get_bytes(&tmp, &data, (size_t)length) ||
|
---|
| 516 | PACKET_remaining(&tmp) != 0) {
|
---|
| 517 | return 0;
|
---|
| 518 | }
|
---|
| 519 |
|
---|
| 520 | *pkt = tmp;
|
---|
| 521 | subpkt->curr = data;
|
---|
| 522 | subpkt->remaining = length;
|
---|
| 523 |
|
---|
| 524 | return 1;
|
---|
| 525 | }
|
---|
| 526 |
|
---|
| 527 | /*
|
---|
| 528 | * Reads a variable-length vector prefixed with a three-byte length, and stores
|
---|
| 529 | * the contents in |subpkt|. |pkt| can equal |subpkt|.
|
---|
| 530 | * Data is not copied: the |subpkt| packet will share its underlying buffer with
|
---|
| 531 | * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
|
---|
| 532 | * Upon failure, the original |pkt| and |subpkt| are not modified.
|
---|
| 533 | */
|
---|
| 534 | __owur static ossl_inline int PACKET_get_length_prefixed_3(PACKET *pkt,
|
---|
| 535 | PACKET *subpkt)
|
---|
| 536 | {
|
---|
| 537 | unsigned long length;
|
---|
| 538 | const unsigned char *data;
|
---|
| 539 | PACKET tmp = *pkt;
|
---|
| 540 | if (!PACKET_get_net_3(&tmp, &length) ||
|
---|
| 541 | !PACKET_get_bytes(&tmp, &data, (size_t)length)) {
|
---|
| 542 | return 0;
|
---|
| 543 | }
|
---|
| 544 |
|
---|
| 545 | *pkt = tmp;
|
---|
| 546 | subpkt->curr = data;
|
---|
| 547 | subpkt->remaining = length;
|
---|
| 548 |
|
---|
| 549 | return 1;
|
---|
| 550 | }
|
---|
| 551 | # ifdef __cplusplus
|
---|
| 552 | }
|
---|
| 553 | # endif
|
---|
| 554 |
|
---|
| 555 | #endif /* HEADER_PACKET_LOCL_H */
|
---|