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