Welcome to mirror list, hosted at ThFree Co, Russian Federation.

toy.c « toy « crypto - github.com/openssl/experimental.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: dc53d3ac843bbc5026409af519364909f79334dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/*
 * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
 *
 * Licensed under the Apache License 2.0 (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
 */

#include <string.h>
#include <openssl/crypto.h>
#include <openssl/rand.h>
#include <openssl/bio.h>
#include <openssl/toy.h>
#include <time.h>
#include <sys/time.h>

struct ossl_toy_packet_st {
    /* The connection id */
    uint32_t connid;
    /* The stream id within the connection */
    uint32_t streamid;
    /* The packet id within the stream */
    uint32_t packetid;

    /* Start of the packet data (including the header) */
    unsigned char *data;
    /* Start of the application data within the packet */
    unsigned char *appdata;
    /* Length of the application data */
    size_t length;
    /* Size of the data buffer */
    size_t datasize;
};

struct ossl_toy_stream_st {
    /* The parent connection */
    OSSL_TOY_CONN *conn;
    /* The stream id */
    uint32_t id;
    /* Id of the next packet we expect to read from this stream */
    uint32_t rnxtpkt;
    /* Id of the next packet we will write to this stream */
    uint32_t wnxtpkt;
    OSSL_TOY_PACKET *packets[OSSL_TOY_MAX_PACKETS];
};

struct ossl_toy_conn_st {
    /* The parent context */
    OSSL_TOY_CTX *ctx;
    /* Unique id for this connection */
    uint32_t id;
    /* Address of peer */
    BIO_ADDR *peer;
    /* All of our child streams */
    OSSL_TOY_STREAM *streams[OSSL_TOY_MAX_TOY_STREAMS];
};

struct ossl_toy_ctx_st {
    OSSL_LIB_CTX *libctx;
    /* Whether we are a server or a client */
    int isserver;
    /* Currently active connections */
    OSSL_TOY_CONN *conns[OSSL_TOY_MAX_TOY_CONNECTIONS];
    /* Read BIO */
    BIO *rbio;
    /* Write BIO */
    BIO *wbio;
};

static void ossl_toy_conn_free(OSSL_TOY_CONN *conn);

static OSSL_TOY_STREAM *ossl_toy_stream_new(OSSL_TOY_CONN *conn,
                                            uint32_t streamid);
static void ossl_toy_stream_free(OSSL_TOY_STREAM *stream);
static int ossl_toy_stream_add0_packet(OSSL_TOY_STREAM *stream,
                                       OSSL_TOY_PACKET *packet);

static OSSL_TOY_PACKET *ossl_toy_packet_new(void);
static void ossl_toy_packet_free(OSSL_TOY_PACKET *packet);

OSSL_TOY_CTX *OSSL_TOY_CTX_new(OSSL_LIB_CTX *libctx, int isserver)
{
    OSSL_TOY_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));

    ctx->libctx = libctx;
    ctx->isserver = isserver;

    return ctx;
}

void OSSL_TOY_CTX_free(OSSL_TOY_CTX *ctx)
{
    size_t i;

    for (i = 0; i < OSSL_TOY_MAX_TOY_CONNECTIONS; i++) {
        if (ctx->conns[i] != NULL) {
            ossl_toy_conn_free(ctx->conns[i]);
            ctx->conns[i] = NULL;
        }
    }

    BIO_free(ctx->rbio);
    BIO_free(ctx->wbio);
    OPENSSL_free(ctx);
}

BIO *OSSL_TOY_CTX_get0_rbio(OSSL_TOY_CTX *ctx)
{
    return ctx->rbio;
}

BIO *OSSL_TOY_CTX_get0_wbio(OSSL_TOY_CTX *ctx)
{
    return ctx->wbio;
}

int OSSL_TOY_CTX_set0_rbio(OSSL_TOY_CTX *ctx, BIO *rbio)
{
    BIO_free(ctx->rbio);
    ctx->rbio = rbio;

    return 1;
}

int OSSL_TOY_CTX_set0_wbio(OSSL_TOY_CTX *ctx, BIO *wbio)
{
    BIO_free(ctx->wbio);
    ctx->wbio = wbio;

    return 1;
}


OSSL_TOY_CONN *OSSL_TOY_CTX_get0_connection(OSSL_TOY_CTX *ctx, uint32_t id)
{
    OSSL_TOY_CONN *conn;
    size_t i;

    if (id == OSSL_TOY_NULL_CONNECTION_ID) {
        if (RAND_bytes_ex(ctx->libctx, (unsigned char *)&id, sizeof(id), 0) <= 0
                || id == OSSL_TOY_NULL_CONNECTION_ID)
            return NULL;
    }

    conn = OPENSSL_zalloc(sizeof(*conn));
    if (conn == NULL)
        return NULL;

    conn->ctx = ctx;
    conn->id = id;

    for (i = 0; i < OSSL_TOY_MAX_TOY_CONNECTIONS; i++) {
        if (ctx->conns[i] == NULL) {
            ctx->conns[i] = conn;
            return conn;
        }
    }

    /* It is an error if we get this far */
    ossl_toy_conn_free(conn);
    return NULL;
}

int OSSL_TOY_CTX_process_packet(OSSL_TOY_CTX *ctx, OSSL_TOY_CONN **conn,
                                OSSL_TOY_STREAM **stream, int *isnew)
{
    int ret;
    OSSL_TOY_PACKET *packet = ossl_toy_packet_new();
    uint32_t *uiptr;
    size_t i;

    if (packet == NULL)
        return -1;

    ret = BIO_read(ctx->rbio, packet->data, packet->datasize);
    if (ret <= 0)
        goto err;

    if (ret < OSSL_TOY_PACKET_HEADER_SIZE) {
        ret = -1;
        goto err;
    }

    uiptr = (uint32_t *)packet->data;
    packet->connid = *uiptr++;
    packet->streamid = *uiptr++;
    packet->packetid = *uiptr++;
    packet->appdata = packet->data + OSSL_TOY_PACKET_HEADER_SIZE;
    packet->length = ret - OSSL_TOY_PACKET_HEADER_SIZE;

    *isnew = 0;
    *conn = NULL;
    /* Find the connection */
    for (i = 0; i < OSSL_TOY_MAX_TOY_CONNECTIONS; i++) {
        if (ctx->conns[i] != NULL && ctx->conns[i]->id == packet->connid) {
            *conn = ctx->conns[i];
            break;
        }
    }
    if (*conn == NULL) {
        /* New connection */
        if (!ctx->isserver) {
            /* Only servers can receive new connection requests */
            ret = -1;
            goto err;
        }
        *conn = OSSL_TOY_CTX_get0_connection(ctx, packet->connid);
        if (*conn == NULL) {
            ret = -1;
            goto err;
        }
        *isnew = 1;
    }

    if (ctx->isserver && (*conn)->peer == NULL) {
        if (((*conn)->peer = BIO_ADDR_new()) == NULL) {
            ret = -1;
            goto err;
        }

        if (BIO_dgram_get_peer(ctx->rbio, (*conn)->peer) <= 0) {
            ret = -1;
            goto err;
        }
    }

    *stream = OSSL_TOY_CONN_get0_stream(*conn, packet->streamid);
    if (*stream == NULL) {
        ret = -1;
        goto err;
    }

    if (!ossl_toy_stream_add0_packet(*stream, packet)) {
        ret = -1;
        goto err;
    }
    /* Packet has been assigned to the stream, so don't free it below */
    packet = NULL;
    ret = 1;
 err:
    ossl_toy_packet_free(packet);
    return ret;
}

int OSSL_TOY_CTX_handle_timeout(struct timeval *nxttimeout, int *havenewtimeout)
{
    struct timeval timenow;


    *havenewtimeout = 1;

    /* Get current time */
    gettimeofday(&timenow, NULL);
    timenow.tv_sec += 2;
    *nxttimeout = timenow;

    return 1;
}

int OSSL_TOY_CTX_is_server(OSSL_TOY_CTX *ctx)
{
    return ctx->isserver;
}

void ossl_toy_conn_free(OSSL_TOY_CONN *conn)
{
    size_t i;
    if (conn == NULL)
        return;

    BIO_ADDR_free(conn->peer);

    /* Remove any associated streams */
    for (i = 0; i < OSSL_TOY_MAX_TOY_STREAMS; i++)
        ossl_toy_stream_free(conn->streams[i]);

    /* Remove the connection from the context's connection list */
    if (conn->ctx != NULL) {
        for (i = 0; i < OSSL_TOY_MAX_TOY_CONNECTIONS; i++) {
            if (conn->ctx->conns[i] == conn) {
                conn->ctx->conns[i] = NULL;
                break;
            }
        }
    }

    OPENSSL_free(conn);
}

OSSL_TOY_STREAM *OSSL_TOY_CONN_get0_stream(OSSL_TOY_CONN *conn, uint32_t streamid)
{
    if (conn == NULL || streamid >= OSSL_TOY_MAX_TOY_STREAMS)
        return NULL;

    if (conn->streams[streamid] == NULL)
        conn->streams[streamid] = ossl_toy_stream_new(conn, streamid);

    return conn->streams[streamid];
}

uint32_t OSSL_TOY_CONN_get_id(OSSL_TOY_CONN *conn)
{
    return conn->id;
}

void OSSL_TOY_CONN_set0_peer(OSSL_TOY_CONN *conn, BIO_ADDR *peer)
{
    BIO_ADDR_free(conn->peer);
    conn->peer = peer;
}

static OSSL_TOY_STREAM *ossl_toy_stream_new(OSSL_TOY_CONN *conn,
                                            uint32_t streamid)
{
    OSSL_TOY_STREAM *stream = OPENSSL_zalloc(sizeof(*stream));

    if (stream == NULL)
        return NULL;

    stream->conn = conn;
    stream->id = streamid;

    return stream;
}

static void ossl_toy_stream_free(OSSL_TOY_STREAM *stream)
{
    size_t i;

    if (stream == NULL)
        return;

    /* Remove the stream from the connection */
    stream->conn->streams[stream->id] = NULL;

    /* Free any received packets */
    for (i = 0; i < OSSL_TOY_MAX_PACKETS; i++)
        ossl_toy_packet_free(stream->packets[i]);

    OPENSSL_free(stream);
}

static int ossl_toy_stream_add0_packet(OSSL_TOY_STREAM *stream,
                                       OSSL_TOY_PACKET *packet)
{
    if (stream == NULL
            || packet == NULL
            || packet->packetid >= OSSL_TOY_MAX_PACKETS
            || stream->packets[packet->packetid] != NULL)
        return 0;

    stream->packets[packet->packetid] = packet;

    return 1;
}

int OSSL_TOY_STREAM_read(OSSL_TOY_STREAM *stream, unsigned char *buf,
                         size_t bufsize, size_t *bytesread)
{
    if (bytesread == NULL)
        return 0;
    *bytesread = 0;
    if (stream == NULL)
        return 0;
    if (bufsize == 0)
        return 1;
    if (buf == NULL)
        return 0;

    for (; stream->rnxtpkt < OSSL_TOY_MAX_PACKETS
           && stream->packets[stream->rnxtpkt] != NULL; stream->rnxtpkt++) {
        OSSL_TOY_PACKET *packet = stream->packets[stream->rnxtpkt];

        if (packet->length > 0) {
            if (bufsize > packet->length) {
                /* Read all the available data from the packet */
                memcpy(buf, packet->appdata, packet->length);
                *bytesread += packet->length;
                packet->appdata += packet->length;
                packet->length = 0;
            } else {
                /* Partial read of data from the packet */
                memcpy(buf, packet->appdata, bufsize);
                *bytesread += bufsize;
                packet->appdata += bufsize;
                packet->length -= bufsize;

                /* We've consumed the whole buffer, so just return */
                return 1;
            }
        }
    }
    if (*bytesread > 0)
        return 1;

    /* Nothing available to read */
    return 0;
}

int OSSL_TOY_STREAM_write(OSSL_TOY_STREAM *stream, const unsigned char *buf,
                          size_t bufsize, size_t *byteswritten)
{
    if (byteswritten == NULL)
        return 0;
    *byteswritten = 0;
    if (stream == NULL)
        return 0;
    if (bufsize == 0)
        return 1;
    if (buf == NULL)
        return 0;

    do {
        size_t towrite, written;
        unsigned char packetdata[OSSL_TOY_PACKET_HEADER_SIZE
                                 + OSSL_TOY_MAX_APP_DATA_SIZE];
        uint32_t *uiptr = (uint32_t *)packetdata;

        if (bufsize > OSSL_TOY_MAX_APP_DATA_SIZE)
            towrite = OSSL_TOY_MAX_APP_DATA_SIZE;
        else
            towrite = bufsize;

        /* Header data */
        /* Connection id */
        *uiptr++ = stream->conn->id;
        /* Stream id */
        *uiptr++ = stream->id;
        /* Packet id */
        *uiptr++ = stream->wnxtpkt;

        /* App data */
        memcpy(packetdata + OSSL_TOY_PACKET_HEADER_SIZE, buf, towrite);

        if (stream->conn->ctx->isserver) {
            if (stream->conn->peer == NULL)
                return 0;
            if (BIO_dgram_set_peer(stream->conn->ctx->wbio,
                                   stream->conn->peer) <= 0) {
                return 0;
            }
        }
        if (!BIO_write_ex(stream->conn->ctx->wbio, packetdata,
                          OSSL_TOY_PACKET_HEADER_SIZE + towrite, &written)) {
            /* We might have written partial data!! */
            break;
        }
        if (written != OSSL_TOY_PACKET_HEADER_SIZE + towrite) {
            /* Should not happen!! */
            return 0;
        }
        *byteswritten += towrite;

        stream->wnxtpkt++;
        buf += towrite;
        bufsize -= towrite;
    } while (bufsize > 0);

    if (*byteswritten > 0)
        return 1;

    /* We failed to write anything */
    return 0;
}

uint32_t OSSL_TOY_STREAM_get_id(OSSL_TOY_STREAM *stream)
{
    return stream->id;
}

static OSSL_TOY_PACKET *ossl_toy_packet_new(void)
{
    OSSL_TOY_PACKET *packet = OPENSSL_zalloc(sizeof(*packet));

    packet->datasize = OSSL_TOY_PACKET_HEADER_SIZE + OSSL_TOY_MAX_APP_DATA_SIZE;
    packet->data = OPENSSL_malloc(packet->datasize);

    if (packet->data == NULL) {
        OPENSSL_free(packet);
        return NULL;
    }

    return packet;
}

static void ossl_toy_packet_free(OSSL_TOY_PACKET *packet)
{
    if (packet == NULL)
        return;
    OPENSSL_free(packet->data);
    OPENSSL_free(packet);
}