/src/openssl/crypto/packet.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2015-2026 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 | | #include "internal/cryptlib.h" |
11 | | #include <stdint.h> |
12 | | #include "internal/packet.h" |
13 | | #include "internal/packet_inline.h" |
14 | | #if !defined OPENSSL_NO_QUIC && !defined FIPS_MODULE |
15 | | #include "internal/packet_quic.h" |
16 | | #endif |
17 | | #include <openssl/err.h> |
18 | | |
19 | | #ifdef OPENSSL_SMALL_FOOTPRINT |
20 | | int WPACKET_allocate_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes) |
21 | | { |
22 | | return WPACKET_allocate_bytes_int(pkt, len, allocbytes); |
23 | | } |
24 | | |
25 | | int WPACKET_sub_allocate_bytes__(WPACKET *pkt, size_t len, |
26 | | unsigned char **allocbytes, size_t lenbytes) |
27 | | { |
28 | | return WPACKET_sub_allocate_bytes___int(pkt, len, allocbytes, lenbytes); |
29 | | } |
30 | | |
31 | | int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes) |
32 | | { |
33 | | return WPACKET_reserve_bytes_int(pkt, len, allocbytes); |
34 | | } |
35 | | |
36 | | int WPACKET_sub_reserve_bytes__(WPACKET *pkt, size_t len, |
37 | | unsigned char **allocbytes, size_t lenbytes) |
38 | | { |
39 | | return WPACKET_sub_reserve_bytes___int(pkt, len, allocbytes, lenbytes); |
40 | | } |
41 | | |
42 | | int WPACKET_init_static_len(WPACKET *pkt, unsigned char *buf, size_t len, |
43 | | size_t lenbytes) |
44 | | { |
45 | | return WPACKET_init_static_len_int(pkt, buf, len, lenbytes); |
46 | | } |
47 | | |
48 | | int WPACKET_init_der(WPACKET *pkt, unsigned char *buf, size_t len) |
49 | | { |
50 | | return WPACKET_init_der_int(pkt, buf, len); |
51 | | } |
52 | | |
53 | | int WPACKET_init_len(WPACKET *pkt, BUF_MEM *buf, size_t lenbytes) |
54 | | { |
55 | | return WPACKET_init_len_int(pkt, buf, lenbytes); |
56 | | } |
57 | | |
58 | | int WPACKET_init(WPACKET *pkt, BUF_MEM *buf) |
59 | | { |
60 | | return WPACKET_init_int(pkt, buf); |
61 | | } |
62 | | |
63 | | int WPACKET_init_null(WPACKET *pkt, size_t lenbytes) |
64 | | { |
65 | | return WPACKET_init_null_int(pkt, lenbytes); |
66 | | } |
67 | | |
68 | | int WPACKET_init_null_der(WPACKET *pkt) |
69 | | { |
70 | | return WPACKET_init_null_der_int(pkt); |
71 | | } |
72 | | |
73 | | int WPACKET_set_flags(WPACKET *pkt, unsigned int flags) |
74 | | { |
75 | | return WPACKET_set_flags_int(pkt, flags); |
76 | | } |
77 | | |
78 | | int WPACKET_fill_lengths(WPACKET *pkt) |
79 | | { |
80 | | return WPACKET_fill_lengths_int(pkt); |
81 | | } |
82 | | |
83 | | int WPACKET_close(WPACKET *pkt) |
84 | | { |
85 | | return WPACKET_close_int(pkt); |
86 | | } |
87 | | |
88 | | int WPACKET_finish(WPACKET *pkt) |
89 | | { |
90 | | return WPACKET_finish_int(pkt); |
91 | | } |
92 | | |
93 | | int WPACKET_start_sub_packet_len__(WPACKET *pkt, size_t lenbytes) |
94 | | { |
95 | | return WPACKET_start_sub_packet_len___int(pkt, lenbytes); |
96 | | } |
97 | | |
98 | | int WPACKET_start_sub_packet_at_offset_len__(WPACKET *pkt, size_t lenbytes, |
99 | | size_t offset) |
100 | | { |
101 | | return WPACKET_start_sub_packet_at_offset_len___int(pkt, lenbytes, offset); |
102 | | } |
103 | | |
104 | | int WPACKET_start_sub_packet(WPACKET *pkt) |
105 | | { |
106 | | return WPACKET_start_sub_packet_int(pkt); |
107 | | } |
108 | | |
109 | | int WPACKET_put_bytes__(WPACKET *pkt, uint64_t val, size_t size) |
110 | | { |
111 | | return WPACKET_put_bytes___int(pkt, val, size); |
112 | | } |
113 | | |
114 | | int WPACKET_set_max_size(WPACKET *pkt, size_t maxsize) |
115 | | { |
116 | | return WPACKET_set_max_size_int(pkt, maxsize); |
117 | | } |
118 | | |
119 | | int WPACKET_memset(WPACKET *pkt, int ch, size_t len) |
120 | | { |
121 | | return WPACKET_memset_int(pkt, ch, len); |
122 | | } |
123 | | |
124 | | int WPACKET_memcpy(WPACKET *pkt, const void *src, size_t len) |
125 | | { |
126 | | return WPACKET_memcpy_int(pkt, src, len); |
127 | | } |
128 | | |
129 | | int WPACKET_sub_memcpy__(WPACKET *pkt, const void *src, size_t len, |
130 | | size_t lenbytes) |
131 | | { |
132 | | return WPACKET_sub_memcpy___int(pkt, src, len, lenbytes); |
133 | | } |
134 | | |
135 | | int WPACKET_get_total_written(WPACKET *pkt, size_t *written) |
136 | | { |
137 | | return WPACKET_get_total_written_int(pkt, written); |
138 | | } |
139 | | |
140 | | int WPACKET_get_length(WPACKET *pkt, size_t *len) |
141 | | { |
142 | | return WPACKET_get_length_int(pkt, len); |
143 | | } |
144 | | |
145 | | unsigned char *WPACKET_get_curr(WPACKET *pkt) |
146 | | { |
147 | | return WPACKET_get_curr_int(pkt); |
148 | | } |
149 | | |
150 | | int WPACKET_is_null_buf(WPACKET *pkt) |
151 | | { |
152 | | return WPACKET_is_null_buf_int(pkt); |
153 | | } |
154 | | |
155 | | void WPACKET_cleanup(WPACKET *pkt) |
156 | | { |
157 | | WPACKET_cleanup_int(pkt); |
158 | | } |
159 | | |
160 | | #endif |
161 | | |
162 | | #if !defined OPENSSL_NO_QUIC && !defined FIPS_MODULE |
163 | | |
164 | | int WPACKET_start_quic_sub_packet_bound(WPACKET *pkt, size_t max_len) |
165 | 0 | { |
166 | 0 | size_t enclen = ossl_quic_vlint_encode_len(max_len); |
167 | |
|
168 | 0 | if (enclen == 0) |
169 | 0 | return 0; |
170 | | |
171 | 0 | if (WPACKET_start_sub_packet_len__(pkt, enclen) == 0) |
172 | 0 | return 0; |
173 | | |
174 | 0 | pkt->subs->flags |= WPACKET_FLAGS_QUIC_VLINT; |
175 | 0 | return 1; |
176 | 0 | } |
177 | | |
178 | | int WPACKET_start_quic_sub_packet(WPACKET *pkt) |
179 | 0 | { |
180 | | /* |
181 | | * Assume no (sub)packet will exceed 4GiB, thus the 8-byte encoding need not |
182 | | * be used. |
183 | | */ |
184 | 0 | return WPACKET_start_quic_sub_packet_bound(pkt, OSSL_QUIC_VLINT_4B_MIN); |
185 | 0 | } |
186 | | |
187 | | int WPACKET_quic_sub_allocate_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes) |
188 | 0 | { |
189 | 0 | if (!WPACKET_start_quic_sub_packet_bound(pkt, len) |
190 | 0 | || !WPACKET_allocate_bytes(pkt, len, allocbytes) |
191 | 0 | || !WPACKET_close(pkt)) |
192 | 0 | return 0; |
193 | | |
194 | 0 | return 1; |
195 | 0 | } |
196 | | |
197 | | /* |
198 | | * Write a QUIC variable-length integer to the packet. |
199 | | */ |
200 | | int WPACKET_quic_write_vlint(WPACKET *pkt, uint64_t v) |
201 | 0 | { |
202 | 0 | unsigned char *b = NULL; |
203 | 0 | size_t enclen = ossl_quic_vlint_encode_len(v); |
204 | |
|
205 | 0 | if (enclen == 0) |
206 | 0 | return 0; |
207 | | |
208 | 0 | if (WPACKET_allocate_bytes(pkt, enclen, &b) == 0) |
209 | 0 | return 0; |
210 | | |
211 | 0 | ossl_quic_vlint_encode(b, v); |
212 | 0 | return 1; |
213 | 0 | } |
214 | | |
215 | | #endif |