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