summaryrefslogtreecommitdiffstats
path: root/crypto/bio/ossl_core_bio.c
blob: 328302ea34e61d614e416da7f45eff1f58f7c9f9 (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
/*
 * Copyright 2021 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 <openssl/core.h>
#include "bio_local.h"

/*-
 * Core BIO structure
 * This is distinct from a BIO to prevent casting between the two which could
 * lead to versioning problems.
 */
struct ossl_core_bio_st {
    CRYPTO_REF_COUNT ref_cnt;
    CRYPTO_RWLOCK *ref_lock;
    BIO *bio;
};

static OSSL_CORE_BIO *core_bio_new(void)
{
    OSSL_CORE_BIO *cb = OPENSSL_malloc(sizeof(*cb));

    if (cb == NULL || (cb->ref_lock = CRYPTO_THREAD_lock_new()) == NULL) {
        OPENSSL_free(cb);
        return NULL;
    }
    cb->ref_cnt = 1;
    return cb;
}

int ossl_core_bio_up_ref(OSSL_CORE_BIO *cb)
{
    int ref = 0;

    return CRYPTO_UP_REF(&cb->ref_cnt, &ref, cb->ref_lock);
}

int ossl_core_bio_free(OSSL_CORE_BIO *cb)
{
    int ref = 0, res = 1;

    if (cb != NULL) {
        CRYPTO_DOWN_REF(&cb->ref_cnt, &ref, cb->ref_lock);
        if (ref <= 0) {
            res = BIO_free(cb->bio);
            CRYPTO_THREAD_lock_free(cb->ref_lock);
            OPENSSL_free(cb);
        }
    }
    return res;
}

OSSL_CORE_BIO *ossl_core_bio_new_from_bio(BIO *bio)
{
    OSSL_CORE_BIO *cb = core_bio_new();

    if (cb == NULL || !BIO_up_ref(bio)) {
        ossl_core_bio_free(cb);
        return NULL;
    }
    cb->bio = bio;
    return cb;
}

static OSSL_CORE_BIO *core_bio_new_from_new_bio(BIO *bio)
{
    OSSL_CORE_BIO *cb = NULL;

    if (bio == NULL)
        return NULL;
    if ((cb = core_bio_new()) == NULL) {
        BIO_free(bio);
        return NULL;
    }
    cb->bio = bio;
    return cb;
}

OSSL_CORE_BIO *ossl_core_bio_new_file(const char *filename, const char *mode)
{
    return core_bio_new_from_new_bio(BIO_new_file(filename, mode));
}

OSSL_CORE_BIO *ossl_core_bio_new_mem_buf(const void *buf, int len)
{
    return core_bio_new_from_new_bio(BIO_new_mem_buf(buf, len));
}

int ossl_core_bio_read_ex(OSSL_CORE_BIO *cb, void *data, size_t dlen,
                          size_t *readbytes)
{
    return BIO_read_ex(cb->bio, data, dlen, readbytes);
}

int ossl_core_bio_write_ex(OSSL_CORE_BIO *cb, const void *data, size_t dlen,
                           size_t *written)
{
    return BIO_write_ex(cb->bio, data, dlen, written);
}

int ossl_core_bio_gets(OSSL_CORE_BIO *cb, char *buf, int size)
{
    return BIO_gets(cb->bio, buf, size);
}

int ossl_core_bio_puts(OSSL_CORE_BIO *cb, const char *buf)
{
    return BIO_puts(cb->bio, buf);
}

long ossl_core_bio_ctrl(OSSL_CORE_BIO *cb, int cmd, long larg, void *parg)
{
    return BIO_ctrl(cb->bio, cmd, larg, parg);
}

int ossl_core_bio_vprintf(OSSL_CORE_BIO *cb, const char *format, va_list args)
{
    return BIO_vprintf(cb->bio, format, args);
}