|
0 |
/*
|
|
1 |
* Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
|
|
2 |
*
|
|
3 |
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
4 |
* this file except in compliance with the License. You can obtain a copy
|
|
5 |
* in the file LICENSE in the source distribution or at
|
|
6 |
* https://www.openssl.org/source/license.html
|
|
7 |
*/
|
|
8 |
|
|
9 |
#include <string.h>
|
|
10 |
#include <openssl/safestack.h>
|
|
11 |
|
|
12 |
#include "ssltestlib.h"
|
|
13 |
|
|
14 |
#define SSL_IS_DTLS(s) (s->method->version == DTLS_ANY_VERSION \
|
|
15 |
|| s->method->version == DTLS1_2_VERSION \
|
|
16 |
|| s->method->version == DTLS1_VERSION)
|
|
17 |
|
|
18 |
static int tls_dump_new(BIO *bi);
|
|
19 |
static int tls_dump_free(BIO *a);
|
|
20 |
static int tls_dump_read(BIO *b, char *out, int outl);
|
|
21 |
static int tls_dump_write(BIO *b, const char *in, int inl);
|
|
22 |
static long tls_dump_ctrl(BIO *b, int cmd, long num, void *ptr);
|
|
23 |
static int tls_dump_gets(BIO *bp, char *buf, int size);
|
|
24 |
static int tls_dump_puts(BIO *bp, const char *str);
|
|
25 |
|
|
26 |
/* Choose a sufficiently large type likely to be unused for this custom BIO */
|
|
27 |
# define BIO_TYPE_TLS_DUMP_FILTER (0x80 | BIO_TYPE_FILTER)
|
|
28 |
|
|
29 |
# define BIO_TYPE_MEMPACKET_TEST 0x81
|
|
30 |
|
|
31 |
static BIO_METHOD method_tls_dump = {
|
|
32 |
BIO_TYPE_TLS_DUMP_FILTER,
|
|
33 |
"TLS dump filter",
|
|
34 |
tls_dump_write,
|
|
35 |
tls_dump_read,
|
|
36 |
tls_dump_puts,
|
|
37 |
tls_dump_gets,
|
|
38 |
tls_dump_ctrl,
|
|
39 |
tls_dump_new,
|
|
40 |
tls_dump_free
|
|
41 |
};
|
|
42 |
|
|
43 |
BIO_METHOD *bio_f_tls_dump_filter(void)
|
|
44 |
{
|
|
45 |
return &method_tls_dump;
|
|
46 |
}
|
|
47 |
|
|
48 |
static int tls_dump_new(BIO *bio)
|
|
49 |
{
|
|
50 |
bio->init = 1;
|
|
51 |
return 1;
|
|
52 |
}
|
|
53 |
|
|
54 |
static int tls_dump_free(BIO *bio)
|
|
55 |
{
|
|
56 |
bio->init = 0;
|
|
57 |
|
|
58 |
return 1;
|
|
59 |
}
|
|
60 |
|
|
61 |
static void copy_flags(BIO *bio)
|
|
62 |
{
|
|
63 |
int flags;
|
|
64 |
BIO *next = BIO_next(bio);
|
|
65 |
|
|
66 |
flags = BIO_test_flags(next, BIO_FLAGS_SHOULD_RETRY | BIO_FLAGS_RWS);
|
|
67 |
BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY | BIO_FLAGS_RWS);
|
|
68 |
BIO_set_flags(bio, flags);
|
|
69 |
}
|
|
70 |
|
|
71 |
#define RECORD_CONTENT_TYPE 0
|
|
72 |
#define RECORD_VERSION_HI 1
|
|
73 |
#define RECORD_VERSION_LO 2
|
|
74 |
#define RECORD_EPOCH_HI 3
|
|
75 |
#define RECORD_EPOCH_LO 4
|
|
76 |
#define RECORD_SEQUENCE_START 5
|
|
77 |
#define RECORD_SEQUENCE_END 10
|
|
78 |
#define RECORD_LEN_HI 11
|
|
79 |
#define RECORD_LEN_LO 12
|
|
80 |
|
|
81 |
#define MSG_TYPE 0
|
|
82 |
#define MSG_LEN_HI 1
|
|
83 |
#define MSG_LEN_MID 2
|
|
84 |
#define MSG_LEN_LO 3
|
|
85 |
#define MSG_SEQ_HI 4
|
|
86 |
#define MSG_SEQ_LO 5
|
|
87 |
#define MSG_FRAG_OFF_HI 6
|
|
88 |
#define MSG_FRAG_OFF_MID 7
|
|
89 |
#define MSG_FRAG_OFF_LO 8
|
|
90 |
#define MSG_FRAG_LEN_HI 9
|
|
91 |
#define MSG_FRAG_LEN_MID 10
|
|
92 |
#define MSG_FRAG_LEN_LO 11
|
|
93 |
|
|
94 |
|
|
95 |
static void dump_data(const char *data, int len)
|
|
96 |
{
|
|
97 |
int rem, i, content, reclen, msglen, fragoff, fraglen, epoch;
|
|
98 |
unsigned char *rec;
|
|
99 |
|
|
100 |
printf("---- START OF PACKET ----\n");
|
|
101 |
|
|
102 |
rem = len;
|
|
103 |
rec = (unsigned char *)data;
|
|
104 |
|
|
105 |
while (rem > 0) {
|
|
106 |
if (rem != len)
|
|
107 |
printf("*\n");
|
|
108 |
printf("*---- START OF RECORD ----\n");
|
|
109 |
if (rem < DTLS1_RT_HEADER_LENGTH) {
|
|
110 |
printf("*---- RECORD TRUNCATED ----\n");
|
|
111 |
break;
|
|
112 |
}
|
|
113 |
content = rec[RECORD_CONTENT_TYPE];
|
|
114 |
printf("** Record Content-type: %d\n", content);
|
|
115 |
printf("** Record Version: %02x%02x\n",
|
|
116 |
rec[RECORD_VERSION_HI], rec[RECORD_VERSION_LO]);
|
|
117 |
epoch = (rec[RECORD_EPOCH_HI] << 8) | rec[RECORD_EPOCH_LO];
|
|
118 |
printf("** Record Epoch: %d\n", epoch);
|
|
119 |
printf("** Record Sequence: ");
|
|
120 |
for (i = RECORD_SEQUENCE_START; i <= RECORD_SEQUENCE_END; i++)
|
|
121 |
printf("%02x", rec[i]);
|
|
122 |
reclen = (rec[RECORD_LEN_HI] << 8) | rec[RECORD_LEN_LO];
|
|
123 |
printf("\n** Record Length: %d\n", reclen);
|
|
124 |
|
|
125 |
/* Now look at message */
|
|
126 |
rec += DTLS1_RT_HEADER_LENGTH;
|
|
127 |
rem -= DTLS1_RT_HEADER_LENGTH;
|
|
128 |
if (content == SSL3_RT_HANDSHAKE) {
|
|
129 |
printf("**---- START OF HANDSHAKE MESSAGE FRAGMENT ----\n");
|
|
130 |
if (epoch > 0) {
|
|
131 |
printf("**---- HANDSHAKE MESSAGE FRAGMENT ENCRYPTED ----\n");
|
|
132 |
} else if (rem < DTLS1_HM_HEADER_LENGTH
|
|
133 |
|| reclen < DTLS1_HM_HEADER_LENGTH) {
|
|
134 |
printf("**---- HANDSHAKE MESSAGE FRAGMENT TRUNCATED ----\n");
|
|
135 |
} else {
|
|
136 |
printf("*** Message Type: %d\n", rec[MSG_TYPE]);
|
|
137 |
msglen = (rec[MSG_LEN_HI] << 16) | (rec[MSG_LEN_MID] << 8)
|
|
138 |
| rec[MSG_LEN_LO];
|
|
139 |
printf("*** Message Length: %d\n", msglen);
|
|
140 |
printf("*** Message sequence: %d\n",
|
|
141 |
(rec[MSG_SEQ_HI] << 8) | rec[MSG_SEQ_LO]);
|
|
142 |
fragoff = (rec[MSG_FRAG_OFF_HI] << 16)
|
|
143 |
| (rec[MSG_FRAG_OFF_MID] << 8)
|
|
144 |
| rec[MSG_FRAG_OFF_LO];
|
|
145 |
printf("*** Message Fragment offset: %d\n", fragoff);
|
|
146 |
fraglen = (rec[MSG_FRAG_LEN_HI] << 16)
|
|
147 |
| (rec[MSG_FRAG_LEN_MID] << 8)
|
|
148 |
| rec[MSG_FRAG_LEN_LO];
|
|
149 |
printf("*** Message Fragment len: %d\n", fraglen);
|
|
150 |
if (fragoff + fraglen > msglen)
|
|
151 |
printf("***---- HANDSHAKE MESSAGE FRAGMENT INVALID ----\n");
|
|
152 |
else if(reclen < fraglen)
|
|
153 |
printf("**---- HANDSHAKE MESSAGE FRAGMENT TRUNCATED ----\n");
|
|
154 |
else
|
|
155 |
printf("**---- END OF HANDSHAKE MESSAGE FRAGMENT ----\n");
|
|
156 |
}
|
|
157 |
}
|
|
158 |
if (rem < reclen) {
|
|
159 |
printf("*---- RECORD TRUNCATED ----\n");
|
|
160 |
rem = 0;
|
|
161 |
} else {
|
|
162 |
rec += reclen;
|
|
163 |
rem -= reclen;
|
|
164 |
printf("*---- END OF RECORD ----\n");
|
|
165 |
}
|
|
166 |
}
|
|
167 |
printf("---- END OF PACKET ----\n\n");
|
|
168 |
fflush(stdout);
|
|
169 |
}
|
|
170 |
|
|
171 |
static int tls_dump_read(BIO *bio, char *out, int outl)
|
|
172 |
{
|
|
173 |
int ret;
|
|
174 |
BIO *next = BIO_next(bio);
|
|
175 |
|
|
176 |
ret = BIO_read(next, out, outl);
|
|
177 |
copy_flags(bio);
|
|
178 |
|
|
179 |
if (ret > 0) {
|
|
180 |
dump_data(out, ret);
|
|
181 |
}
|
|
182 |
|
|
183 |
return ret;
|
|
184 |
}
|
|
185 |
|
|
186 |
static int tls_dump_write(BIO *bio, const char *in, int inl)
|
|
187 |
{
|
|
188 |
int ret;
|
|
189 |
BIO *next = BIO_next(bio);
|
|
190 |
|
|
191 |
ret = BIO_write(next, in, inl);
|
|
192 |
copy_flags(bio);
|
|
193 |
|
|
194 |
return ret;
|
|
195 |
}
|
|
196 |
|
|
197 |
static long tls_dump_ctrl(BIO *bio, int cmd, long num, void *ptr)
|
|
198 |
{
|
|
199 |
long ret;
|
|
200 |
BIO *next = BIO_next(bio);
|
|
201 |
|
|
202 |
if (next == NULL)
|
|
203 |
return 0;
|
|
204 |
|
|
205 |
switch (cmd) {
|
|
206 |
case BIO_CTRL_DUP:
|
|
207 |
ret = 0L;
|
|
208 |
break;
|
|
209 |
default:
|
|
210 |
ret = BIO_ctrl(next, cmd, num, ptr);
|
|
211 |
break;
|
|
212 |
}
|
|
213 |
return ret;
|
|
214 |
}
|
|
215 |
|
|
216 |
static int tls_dump_gets(BIO *bio, char *buf, int size)
|
|
217 |
{
|
|
218 |
/* We don't support this - not needed anyway */
|
|
219 |
return -1;
|
|
220 |
}
|
|
221 |
|
|
222 |
static int tls_dump_puts(BIO *bio, const char *str)
|
|
223 |
{
|
|
224 |
return tls_dump_write(bio, str, strlen(str));
|
|
225 |
}
|
|
226 |
|
|
227 |
|
|
228 |
typedef struct mempacket_st {
|
|
229 |
unsigned char *data;
|
|
230 |
int len;
|
|
231 |
unsigned int num;
|
|
232 |
unsigned int type;
|
|
233 |
} MEMPACKET;
|
|
234 |
|
|
235 |
/*
|
|
236 |
* These defines would normally be auto-generated and in safestack.h...but this
|
|
237 |
* is just for tests so its probably not an appropriate place
|
|
238 |
*/
|
|
239 |
# define sk_MEMPACKET_new(cmp) SKM_sk_new(MEMPACKET, (cmp))
|
|
240 |
# define sk_MEMPACKET_new_null() SKM_sk_new_null(MEMPACKET)
|
|
241 |
# define sk_MEMPACKET_free(st) SKM_sk_free(MEMPACKET, (st))
|
|
242 |
# define sk_MEMPACKET_num(st) SKM_sk_num(MEMPACKET, (st))
|
|
243 |
# define sk_MEMPACKET_value(st, i) SKM_sk_value(MEMPACKET, (st), (i))
|
|
244 |
# define sk_MEMPACKET_set(st, i, val) SKM_sk_set(MEMPACKET, (st), (i), (val))
|
|
245 |
# define sk_MEMPACKET_zero(st) SKM_sk_zero(MEMPACKET, (st))
|
|
246 |
# define sk_MEMPACKET_push(st, val) SKM_sk_push(MEMPACKET, (st), (val))
|
|
247 |
# define sk_MEMPACKET_unshift(st, val) SKM_sk_unshift(MEMPACKET, (st), (val))
|
|
248 |
# define sk_MEMPACKET_find(st, val) SKM_sk_find(MEMPACKET, (st), (val))
|
|
249 |
# define sk_MEMPACKET_find_ex(st, val) SKM_sk_find_ex(MEMPACKET, (st), (val))
|
|
250 |
# define sk_MEMPACKET_delete(st, i) SKM_sk_delete(MEMPACKET, (st), (i))
|
|
251 |
# define sk_MEMPACKET_delete_ptr(st, ptr) SKM_sk_delete_ptr(MEMPACKET, (st), (ptr))
|
|
252 |
# define sk_MEMPACKET_insert(st, val, i) SKM_sk_insert(MEMPACKET, (st), (val), (i))
|
|
253 |
# define sk_MEMPACKET_set_cmp_func(st, cmp) SKM_sk_set_cmp_func(MEMPACKET, (st), (cmp))
|
|
254 |
# define sk_MEMPACKET_dup(st) SKM_sk_dup(MEMPACKET, st)
|
|
255 |
# define sk_MEMPACKET_pop_free(st, free_func) SKM_sk_pop_free(MEMPACKET, (st), (free_func))
|
|
256 |
# define sk_MEMPACKET_deep_copy(st, copy_func, free_func) SKM_sk_deep_copy(MEMPACKET, (st), (copy_func), (free_func))
|
|
257 |
# define sk_MEMPACKET_shift(st) SKM_sk_shift(MEMPACKET, (st))
|
|
258 |
# define sk_MEMPACKET_pop(st) SKM_sk_pop(MEMPACKET, (st))
|
|
259 |
# define sk_MEMPACKET_sort(st) SKM_sk_sort(MEMPACKET, (st))
|
|
260 |
# define sk_MEMPACKET_is_sorted(st) SKM_sk_is_sorted(MEMPACKET, (st))
|
|
261 |
|
|
262 |
static void mempacket_free(MEMPACKET *pkt)
|
|
263 |
{
|
|
264 |
if (pkt->data != NULL)
|
|
265 |
OPENSSL_free(pkt->data);
|
|
266 |
OPENSSL_free(pkt);
|
|
267 |
}
|
|
268 |
|
|
269 |
typedef struct mempacket_test_ctx_st {
|
|
270 |
STACK_OF(MEMPACKET) *pkts;
|
|
271 |
unsigned int epoch;
|
|
272 |
unsigned int currrec;
|
|
273 |
unsigned int currpkt;
|
|
274 |
unsigned int lastpkt;
|
|
275 |
unsigned int noinject;
|
|
276 |
} MEMPACKET_TEST_CTX;
|
|
277 |
|
|
278 |
static int mempacket_test_new(BIO *bi);
|
|
279 |
static int mempacket_test_free(BIO *a);
|
|
280 |
static int mempacket_test_read(BIO *b, char *out, int outl);
|
|
281 |
static int mempacket_test_write(BIO *b, const char *in, int inl);
|
|
282 |
static long mempacket_test_ctrl(BIO *b, int cmd, long num, void *ptr);
|
|
283 |
static int mempacket_test_gets(BIO *bp, char *buf, int size);
|
|
284 |
static int mempacket_test_puts(BIO *bp, const char *str);
|
|
285 |
|
|
286 |
static BIO_METHOD method_mempacket_test = {
|
|
287 |
BIO_TYPE_MEMPACKET_TEST,
|
|
288 |
"Mem Packet Test",
|
|
289 |
mempacket_test_write,
|
|
290 |
mempacket_test_read,
|
|
291 |
mempacket_test_puts,
|
|
292 |
mempacket_test_gets,
|
|
293 |
mempacket_test_ctrl,
|
|
294 |
mempacket_test_new,
|
|
295 |
mempacket_test_free
|
|
296 |
};
|
|
297 |
|
|
298 |
BIO_METHOD *bio_s_mempacket_test(void)
|
|
299 |
{
|
|
300 |
return &method_mempacket_test;
|
|
301 |
}
|
|
302 |
|
|
303 |
static int mempacket_test_new(BIO *bio)
|
|
304 |
{
|
|
305 |
MEMPACKET_TEST_CTX *ctx = OPENSSL_malloc(sizeof(*ctx));
|
|
306 |
if (ctx == NULL)
|
|
307 |
return 0;
|
|
308 |
memset(ctx, 0, sizeof(*ctx));
|
|
309 |
|
|
310 |
ctx->pkts = sk_MEMPACKET_new_null();
|
|
311 |
if (ctx->pkts == NULL) {
|
|
312 |
OPENSSL_free(ctx);
|
|
313 |
return 0;
|
|
314 |
}
|
|
315 |
bio->init = 1;
|
|
316 |
bio->ptr = ctx;
|
|
317 |
return 1;
|
|
318 |
}
|
|
319 |
|
|
320 |
static int mempacket_test_free(BIO *bio)
|
|
321 |
{
|
|
322 |
MEMPACKET_TEST_CTX *ctx = bio->ptr;
|
|
323 |
|
|
324 |
sk_MEMPACKET_pop_free(ctx->pkts, mempacket_free);
|
|
325 |
OPENSSL_free(ctx);
|
|
326 |
bio->ptr = NULL;
|
|
327 |
bio->init = 0;
|
|
328 |
|
|
329 |
return 1;
|
|
330 |
}
|
|
331 |
|
|
332 |
/* Record Header values */
|
|
333 |
#define EPOCH_HI 4
|
|
334 |
#define EPOCH_LO 5
|
|
335 |
#define RECORD_SEQUENCE 10
|
|
336 |
#define RECORD_LEN_HI 11
|
|
337 |
#define RECORD_LEN_LO 12
|
|
338 |
|
|
339 |
#define STANDARD_PACKET 0
|
|
340 |
|
|
341 |
static int mempacket_test_read(BIO *bio, char *out, int outl)
|
|
342 |
{
|
|
343 |
MEMPACKET_TEST_CTX *ctx = bio->ptr;
|
|
344 |
MEMPACKET *thispkt;
|
|
345 |
unsigned char *rec;
|
|
346 |
int rem;
|
|
347 |
unsigned int seq, offset, len, epoch;
|
|
348 |
|
|
349 |
BIO_clear_retry_flags(bio);
|
|
350 |
|
|
351 |
thispkt = sk_MEMPACKET_value(ctx->pkts, 0);
|
|
352 |
if (thispkt == NULL || thispkt->num != ctx->currpkt) {
|
|
353 |
/* Probably run out of data */
|
|
354 |
BIO_set_retry_read(bio);
|
|
355 |
return -1;
|
|
356 |
}
|
|
357 |
sk_MEMPACKET_shift(ctx->pkts);
|
|
358 |
ctx->currpkt++;
|
|
359 |
|
|
360 |
if (outl > thispkt->len)
|
|
361 |
outl = thispkt->len;
|
|
362 |
|
|
363 |
if (thispkt->type != INJECT_PACKET_IGNORE_REC_SEQ) {
|
|
364 |
/*
|
|
365 |
* Overwrite the record sequence number. We strictly number them in
|
|
366 |
* the order received. Since we are actually a reliable transport
|
|
367 |
* we know that there won't be any re-ordering. We overwrite to deal
|
|
368 |
* with any packets that have been injected
|
|
369 |
*/
|
|
370 |
rem = thispkt->len;
|
|
371 |
rec = thispkt->data;
|
|
372 |
while (rem > 0) {
|
|
373 |
if (rem < DTLS1_RT_HEADER_LENGTH) {
|
|
374 |
return -1;
|
|
375 |
}
|
|
376 |
epoch = (rec[EPOCH_HI] << 8) | rec[EPOCH_LO];
|
|
377 |
if (epoch != ctx->epoch) {
|
|
378 |
ctx->epoch = epoch;
|
|
379 |
ctx->currrec = 0;
|
|
380 |
}
|
|
381 |
seq = ctx->currrec;
|
|
382 |
offset = 0;
|
|
383 |
do {
|
|
384 |
rec[RECORD_SEQUENCE - offset] = seq & 0xFF;
|
|
385 |
seq >>= 8;
|
|
386 |
offset++;
|
|
387 |
} while (seq > 0);
|
|
388 |
ctx->currrec++;
|
|
389 |
|
|
390 |
len = ((rec[RECORD_LEN_HI] << 8) | rec[RECORD_LEN_LO])
|
|
391 |
+ DTLS1_RT_HEADER_LENGTH;
|
|
392 |
|
|
393 |
rec += len;
|
|
394 |
rem -= len;
|
|
395 |
}
|
|
396 |
}
|
|
397 |
|
|
398 |
memcpy(out, thispkt->data, outl);
|
|
399 |
|
|
400 |
mempacket_free(thispkt);
|
|
401 |
|
|
402 |
return outl;
|
|
403 |
}
|
|
404 |
|
|
405 |
int mempacket_test_inject(BIO *bio, const char *in, int inl, int pktnum,
|
|
406 |
int type)
|
|
407 |
{
|
|
408 |
MEMPACKET_TEST_CTX *ctx = bio->ptr;
|
|
409 |
MEMPACKET *thispkt, *looppkt, *nextpkt;
|
|
410 |
int i;
|
|
411 |
|
|
412 |
if (ctx == NULL)
|
|
413 |
return -1;
|
|
414 |
|
|
415 |
/* We only allow injection before we've started writing any data */
|
|
416 |
if (pktnum >= 0) {
|
|
417 |
if (ctx->noinject)
|
|
418 |
return -1;
|
|
419 |
} else {
|
|
420 |
ctx->noinject = 1;
|
|
421 |
}
|
|
422 |
|
|
423 |
thispkt = OPENSSL_malloc(sizeof(MEMPACKET));
|
|
424 |
if (thispkt == NULL)
|
|
425 |
return -1;
|
|
426 |
|
|
427 |
thispkt->data = OPENSSL_malloc(inl);
|
|
428 |
if (thispkt->data == NULL) {
|
|
429 |
mempacket_free(thispkt);
|
|
430 |
return -1;
|
|
431 |
}
|
|
432 |
|
|
433 |
memcpy(thispkt->data, in, inl);
|
|
434 |
thispkt->len = inl;
|
|
435 |
thispkt->num = (pktnum >= 0) ? (unsigned int)pktnum : ctx->lastpkt;
|
|
436 |
thispkt->type = type;
|
|
437 |
|
|
438 |
for(i = 0; (looppkt = sk_MEMPACKET_value(ctx->pkts, i)) != NULL; i++) {
|
|
439 |
/* Check if we found the right place to insert this packet */
|
|
440 |
if (looppkt->num > thispkt->num) {
|
|
441 |
if (sk_MEMPACKET_insert(ctx->pkts, thispkt, i) == 0) {
|
|
442 |
mempacket_free(thispkt);
|
|
443 |
return -1;
|
|
444 |
}
|
|
445 |
/* If we're doing up front injection then we're done */
|
|
446 |
if (pktnum >= 0)
|
|
447 |
return inl;
|
|
448 |
/*
|
|
449 |
* We need to do some accounting on lastpkt. We increment it first,
|
|
450 |
* but it might now equal the value of injected packets, so we need
|
|
451 |
* to skip over those
|
|
452 |
*/
|
|
453 |
ctx->lastpkt++;
|
|
454 |
do {
|
|
455 |
i++;
|
|
456 |
nextpkt = sk_MEMPACKET_value(ctx->pkts, i);
|
|
457 |
if (nextpkt != NULL && nextpkt->num == ctx->lastpkt)
|
|
458 |
ctx->lastpkt++;
|
|
459 |
else
|
|
460 |
return inl;
|
|
461 |
} while(1);
|
|
462 |
} else if(looppkt->num == thispkt->num) {
|
|
463 |
if (!ctx->noinject) {
|
|
464 |
/* We injected two packets with the same packet number! */
|
|
465 |
return -1;
|
|
466 |
}
|
|
467 |
ctx->lastpkt++;
|
|
468 |
thispkt->num++;
|
|
469 |
}
|
|
470 |
}
|
|
471 |
/*
|
|
472 |
* We didn't find any packets with a packet number equal to or greater than
|
|
473 |
* this one, so we just add it onto the end
|
|
474 |
*/
|
|
475 |
if (!sk_MEMPACKET_push(ctx->pkts, thispkt)) {
|
|
476 |
mempacket_free(thispkt);
|
|
477 |
return -1;
|
|
478 |
}
|
|
479 |
|
|
480 |
if (pktnum < 0)
|
|
481 |
ctx->lastpkt++;
|
|
482 |
|
|
483 |
return inl;
|
|
484 |
}
|
|
485 |
|
|
486 |
static int mempacket_test_write(BIO *bio, const char *in, int inl)
|
|
487 |
{
|
|
488 |
return mempacket_test_inject(bio, in, inl, -1, STANDARD_PACKET);
|
|
489 |
}
|
|
490 |
|
|
491 |
static long mempacket_test_ctrl(BIO *bio, int cmd, long num, void *ptr)
|
|
492 |
{
|
|
493 |
long ret = 1;
|
|
494 |
MEMPACKET_TEST_CTX *ctx = bio->ptr;
|
|
495 |
MEMPACKET *thispkt;
|
|
496 |
|
|
497 |
switch (cmd) {
|
|
498 |
case BIO_CTRL_EOF:
|
|
499 |
ret = (long)(sk_MEMPACKET_num(ctx->pkts) == 0);
|
|
500 |
break;
|
|
501 |
case BIO_CTRL_GET_CLOSE:
|
|
502 |
ret = bio->shutdown;
|
|
503 |
break;
|
|
504 |
case BIO_CTRL_SET_CLOSE:
|
|
505 |
bio->shutdown = (int)num;
|
|
506 |
break;
|
|
507 |
case BIO_CTRL_WPENDING:
|
|
508 |
ret = 0L;
|
|
509 |
break;
|
|
510 |
case BIO_CTRL_PENDING:
|
|
511 |
thispkt = sk_MEMPACKET_value(ctx->pkts, 0);
|
|
512 |
if (thispkt == NULL)
|
|
513 |
ret = 0;
|
|
514 |
else
|
|
515 |
ret = thispkt->len;
|
|
516 |
break;
|
|
517 |
case BIO_CTRL_FLUSH:
|
|
518 |
ret = 1;
|
|
519 |
break;
|
|
520 |
case BIO_CTRL_RESET:
|
|
521 |
case BIO_CTRL_DUP:
|
|
522 |
case BIO_CTRL_PUSH:
|
|
523 |
case BIO_CTRL_POP:
|
|
524 |
default:
|
|
525 |
ret = 0;
|
|
526 |
break;
|
|
527 |
}
|
|
528 |
return ret;
|
|
529 |
}
|
|
530 |
|
|
531 |
static int mempacket_test_gets(BIO *bio, char *buf, int size)
|
|
532 |
{
|
|
533 |
/* We don't support this - not needed anyway */
|
|
534 |
return -1;
|
|
535 |
}
|
|
536 |
|
|
537 |
static int mempacket_test_puts(BIO *bio, const char *str)
|
|
538 |
{
|
|
539 |
return mempacket_test_write(bio, str, strlen(str));
|
|
540 |
}
|
|
541 |
|
|
542 |
int create_ssl_ctx_pair(const SSL_METHOD *sm, const SSL_METHOD *cm,
|
|
543 |
SSL_CTX **sctx, SSL_CTX **cctx, char *certfile,
|
|
544 |
char *privkeyfile)
|
|
545 |
{
|
|
546 |
SSL_CTX *serverctx = NULL;
|
|
547 |
SSL_CTX *clientctx = NULL;
|
|
548 |
|
|
549 |
serverctx = SSL_CTX_new(sm);
|
|
550 |
clientctx = SSL_CTX_new(cm);
|
|
551 |
if (serverctx == NULL || clientctx == NULL) {
|
|
552 |
printf("Failed to create SSL_CTX\n");
|
|
553 |
goto err;
|
|
554 |
}
|
|
555 |
|
|
556 |
if (SSL_CTX_use_certificate_file(serverctx, certfile,
|
|
557 |
SSL_FILETYPE_PEM) <= 0) {
|
|
558 |
printf("Failed to load server certificate\n");
|
|
559 |
goto err;
|
|
560 |
}
|
|
561 |
if (SSL_CTX_use_PrivateKey_file(serverctx, privkeyfile,
|
|
562 |
SSL_FILETYPE_PEM) <= 0) {
|
|
563 |
printf("Failed to load server private key\n");
|
|
564 |
}
|
|
565 |
if (SSL_CTX_check_private_key(serverctx) <= 0) {
|
|
566 |
printf("Failed to check private key\n");
|
|
567 |
goto err;
|
|
568 |
}
|
|
569 |
|
|
570 |
*sctx = serverctx;
|
|
571 |
*cctx = clientctx;
|
|
572 |
|
|
573 |
return 1;
|
|
574 |
err:
|
|
575 |
SSL_CTX_free(serverctx);
|
|
576 |
SSL_CTX_free(clientctx);
|
|
577 |
return 0;
|
|
578 |
}
|
|
579 |
|
|
580 |
#define MAXLOOPS 100000
|
|
581 |
|
|
582 |
/*
|
|
583 |
* NOTE: Transfers control of the BIOs - this function will free them on error
|
|
584 |
*/
|
|
585 |
int create_ssl_objects(SSL_CTX *serverctx, SSL_CTX *clientctx, SSL **sssl,
|
|
586 |
SSL **cssl, BIO *s_to_c_fbio, BIO *c_to_s_fbio)
|
|
587 |
{
|
|
588 |
SSL *serverssl, *clientssl;
|
|
589 |
BIO *s_to_c_bio = NULL, *c_to_s_bio = NULL;
|
|
590 |
|
|
591 |
serverssl = SSL_new(serverctx);
|
|
592 |
clientssl = SSL_new(clientctx);
|
|
593 |
|
|
594 |
if (serverssl == NULL || clientssl == NULL) {
|
|
595 |
printf("Failed to create SSL object\n");
|
|
596 |
goto error;
|
|
597 |
}
|
|
598 |
|
|
599 |
if (SSL_IS_DTLS(clientssl)) {
|
|
600 |
s_to_c_bio = BIO_new(bio_s_mempacket_test());
|
|
601 |
c_to_s_bio = BIO_new(bio_s_mempacket_test());;
|
|
602 |
} else {
|
|
603 |
s_to_c_bio = BIO_new(BIO_s_mem());
|
|
604 |
c_to_s_bio = BIO_new(BIO_s_mem());
|
|
605 |
}
|
|
606 |
if (s_to_c_bio == NULL || c_to_s_bio == NULL) {
|
|
607 |
printf("Failed to create mem BIOs\n");
|
|
608 |
goto error;
|
|
609 |
}
|
|
610 |
|
|
611 |
if (s_to_c_fbio != NULL)
|
|
612 |
s_to_c_bio = BIO_push(s_to_c_fbio, s_to_c_bio);
|
|
613 |
if (c_to_s_fbio != NULL)
|
|
614 |
c_to_s_bio = BIO_push(c_to_s_fbio, c_to_s_bio);
|
|
615 |
if (s_to_c_bio == NULL || c_to_s_bio == NULL) {
|
|
616 |
printf("Failed to create chained BIOs\n");
|
|
617 |
goto error;
|
|
618 |
}
|
|
619 |
|
|
620 |
/* Set Non-blocking IO behaviour */
|
|
621 |
BIO_set_mem_eof_return(s_to_c_bio, -1);
|
|
622 |
BIO_set_mem_eof_return(c_to_s_bio, -1);
|
|
623 |
|
|
624 |
/* Up ref these as we are passing them to two SSL objects */
|
|
625 |
CRYPTO_add(&s_to_c_bio->references, 1, CRYPTO_LOCK_BIO);
|
|
626 |
CRYPTO_add(&c_to_s_bio->references, 1, CRYPTO_LOCK_BIO);
|
|
627 |
|
|
628 |
SSL_set_bio(serverssl, c_to_s_bio, s_to_c_bio);
|
|
629 |
SSL_set_bio(clientssl, s_to_c_bio, c_to_s_bio);
|
|
630 |
|
|
631 |
/* BIOs will now be freed when SSL objects are freed */
|
|
632 |
s_to_c_bio = c_to_s_bio = NULL;
|
|
633 |
s_to_c_fbio = c_to_s_fbio = NULL;
|
|
634 |
|
|
635 |
*sssl = serverssl;
|
|
636 |
*cssl = clientssl;
|
|
637 |
|
|
638 |
return 1;
|
|
639 |
|
|
640 |
error:
|
|
641 |
SSL_free(serverssl);
|
|
642 |
SSL_free(clientssl);
|
|
643 |
BIO_free(s_to_c_bio);
|
|
644 |
BIO_free(c_to_s_bio);
|
|
645 |
BIO_free(s_to_c_fbio);
|
|
646 |
BIO_free(c_to_s_fbio);
|
|
647 |
|
|
648 |
return 0;
|
|
649 |
}
|
|
650 |
|
|
651 |
int create_ssl_connection(SSL *serverssl, SSL *clientssl)
|
|
652 |
{
|
|
653 |
int retc = -1, rets = -1, err, abortctr = 0;
|
|
654 |
|
|
655 |
do {
|
|
656 |
err = SSL_ERROR_WANT_WRITE;
|
|
657 |
while (retc <= 0 && err == SSL_ERROR_WANT_WRITE) {
|
|
658 |
retc = SSL_connect(clientssl);
|
|
659 |
if (retc <= 0)
|
|
660 |
err = SSL_get_error(clientssl, retc);
|
|
661 |
}
|
|
662 |
|
|
663 |
if (retc <= 0 && err != SSL_ERROR_WANT_READ) {
|
|
664 |
printf("SSL_connect() failed %d, %d\n", retc, err);
|
|
665 |
return 0;
|
|
666 |
}
|
|
667 |
|
|
668 |
err = SSL_ERROR_WANT_WRITE;
|
|
669 |
while (rets <= 0 && err == SSL_ERROR_WANT_WRITE) {
|
|
670 |
rets = SSL_accept(serverssl);
|
|
671 |
if (rets <= 0)
|
|
672 |
err = SSL_get_error(serverssl, rets);
|
|
673 |
}
|
|
674 |
|
|
675 |
if (rets <= 0 && err != SSL_ERROR_WANT_READ) {
|
|
676 |
printf("SSL_accept() failed %d, %d\n", retc, err);
|
|
677 |
return 0;
|
|
678 |
}
|
|
679 |
if (++abortctr == MAXLOOPS) {
|
|
680 |
printf("No progress made\n");
|
|
681 |
return 0;
|
|
682 |
}
|
|
683 |
} while (retc <=0 || rets <= 0);
|
|
684 |
|
|
685 |
return 1;
|
|
686 |
}
|