aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/openssl/method/io.cpp
blob: d184b6456c7d302c8f53f29623d10e9920620f26 (plain) (blame)
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
#include "io.h"

#include <util/generic/singleton.h>
#include <util/generic/yexception.h>
#include <util/system/compiler.h>
#include <util/system/yassert.h>

namespace {
    using NOpenSSL::TAbstractIO;

    TAbstractIO* IO(BIO* bio) noexcept {
        void* ptr = BIO_get_data(bio);
        Y_VERIFY(ptr);
        return static_cast<TAbstractIO*>(ptr);
    }

    template<class T, class Callable, class... Args>
    T ExceptionBoundary(BIO* bio, Callable&& f, T err, Args&&... args) noexcept {
        try {
            return (IO(bio)->*f)(args...);
        } catch (...) {
            return err;
        }
    }

    int Write(BIO* bio, const char* data, int dlen) noexcept {
        return ExceptionBoundary(bio, &TAbstractIO::WriteOld, -1, data, dlen);
    }

    int Read(BIO* bio, char* data, int dlen) noexcept {
        return ExceptionBoundary(bio, &TAbstractIO::ReadOld, -1, data, dlen);
    }

    int Puts(BIO* bio, const char* buf) noexcept {
        return ExceptionBoundary(bio, &TAbstractIO::Puts, -1, buf);
    }

    int Gets(BIO* bio, char* buf, int size) noexcept {
        return ExceptionBoundary(bio, &TAbstractIO::Gets, -1, buf, size);
    }

    long Ctrl(BIO* bio, int cmd, long larg, void* parg) noexcept {
        return ExceptionBoundary(bio, &TAbstractIO::Ctrl, -1, cmd, larg, parg);
    }

    int Create(BIO* bio) noexcept {
        BIO_set_data(bio, nullptr);
        BIO_set_init(bio, 1);
        return 1;
    }

    int Destroy(BIO* bio) noexcept {
        BIO_set_data(bio, nullptr);
        BIO_set_init(bio, 0);
        return 1;
    }

    NOpenSSL::TBioMethod* Method() {
        return SingletonWithPriority<NOpenSSL::TBioMethod, 32768>(
            BIO_get_new_index() | BIO_TYPE_SOURCE_SINK,
            "AbstractIO",
            Write,
            Read,
            Puts,
            Gets,
            Ctrl,
            Create,
            Destroy,
            nullptr
        );
    }
}

namespace NOpenSSL {

    TAbstractIO::TAbstractIO()
        : Bio(BIO_new(*Method())) {
        if (Y_UNLIKELY(!Bio)) {
            throw std::bad_alloc();
        }
        BIO_set_data(Bio, this);
    }

    TAbstractIO::~TAbstractIO() {
        BIO_free(Bio);
    }

    int TAbstractIO::WriteOld(const char* data, int dlen) {
        size_t written = 0;

        int ret = Write(data, dlen, &written);
        if (ret <= 0) {
            return ret;
        }

        return written;
    }

    int TAbstractIO::ReadOld(char* data, int dlen) {
        size_t readbytes = 0;

        int ret = Read(data, dlen, &readbytes);
        if (ret <= 0) {
            return ret;
        }

        return readbytes;
    }

    long TAbstractIO::Ctrl(int cmd, long larg, void* parg) {
        Y_UNUSED(larg);
        Y_UNUSED(parg);

        if (cmd == BIO_CTRL_FLUSH) {
            Flush();
            return 1;
        }

        return 0;
    }

} // namespace NOpenSSL