aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/openssl/io/stream.cpp
blob: eb6f9a9e5fcf7c5b8419463c26b4dcf97ad098a5 (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
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
#include "stream.h" 
 
#include <util/generic/deque.h>
#include <util/generic/singleton.h> 
#include <util/generic/yexception.h> 
 
#include <library/cpp/openssl/init/init.h>
#include <library/cpp/openssl/method/io.h>
#include <library/cpp/resource/resource.h>
 
#include <openssl/bio.h> 
#include <openssl/ssl.h> 
#include <openssl/err.h> 
#include <openssl/tls1.h>
#include <openssl/x509v3.h>
 
using TOptions = TOpenSslClientIO::TOptions; 
 
namespace { 
    struct TSslIO; 
 
    struct TSslInitOnDemand { 
        inline TSslInitOnDemand() { 
            InitOpenSSL(); 
        } 
    }; 
 
    int GetLastSslError() noexcept {
        return ERR_peek_last_error(); 
    } 
 
    const char* SslErrorText(int error) noexcept {
        return ERR_error_string(error, nullptr);
    } 
 
    inline TStringBuf SslLastError() noexcept {
        return SslErrorText(GetLastSslError()); 
    } 
 
    struct TSslError: public yexception { 
        inline TSslError() { 
            *this << SslLastError(); 
        } 
    }; 
 
    struct TSslDestroy { 
        static inline void Destroy(ssl_ctx_st* ctx) noexcept {
            SSL_CTX_free(ctx); 
        } 
 
        static inline void Destroy(ssl_st* ssl) noexcept {
            SSL_free(ssl); 
        } 
 
        static inline void Destroy(bio_st* bio) noexcept {
            BIO_free(bio); 
        } 

        static inline void Destroy(x509_st* x509) noexcept {
            X509_free(x509);
        }
    }; 
 
    template <class T> 
    using TSslHolderPtr = THolder<T, TSslDestroy>;
 
    using TSslContextPtr = TSslHolderPtr<ssl_ctx_st>;
    using TSslPtr = TSslHolderPtr<ssl_st>;
    using TBioPtr = TSslHolderPtr<bio_st>;
    using TX509Ptr = TSslHolderPtr<x509_st>;
 
    inline TSslContextPtr CreateSslCtx(const ssl_method_st* method) {
        TSslContextPtr ctx(SSL_CTX_new(method)); 
 
        if (!ctx) { 
            ythrow TSslError() << "SSL_CTX_new"; 
        } 
 
        SSL_CTX_set_options(ctx.Get(), SSL_OP_NO_SSLv2); 
        SSL_CTX_set_options(ctx.Get(), SSL_OP_NO_SSLv3);
        SSL_CTX_set_options(ctx.Get(), SSL_OP_MICROSOFT_SESS_ID_BUG); 
        SSL_CTX_set_options(ctx.Get(), SSL_OP_NETSCAPE_CHALLENGE_BUG); 
 
        return ctx; 
    } 
 
    struct TStreamIO : public NOpenSSL::TAbstractIO {
        inline TStreamIO(IInputStream* in, IOutputStream* out)
            : In(in)
            , Out(out)
        {
        }
 
        int Write(const char* data, size_t dlen, size_t* written) override {
            Out->Write(data, dlen);

            *written = dlen;
            return 1;
        } 
 
        int Read(char* data, size_t dlen, size_t* readbytes) override {
            *readbytes = In->Read(data, dlen);
            return 1;
        } 
 
        int Puts(const char* buf) override {
            Y_UNUSED(buf);
            return -1;
        } 
 
        int Gets(char* buf, int size) override {
            Y_UNUSED(buf);
            Y_UNUSED(size);
            return -1;
        } 
 
        void Flush() override {
        } 
 
        IInputStream* In;
        IOutputStream* Out;
    }; 
 
    struct TSslIO: public TSslInitOnDemand, public TOptions { 
        inline TSslIO(IInputStream* in, IOutputStream* out, const TOptions& opts)
            : TOptions(opts) 
            , Io(in, out)
            , Ctx(CreateClientContext()) 
            , Ssl(ConstructSsl()) 
        { 
            Connect(); 
        } 
 
        inline TSslContextPtr CreateClientContext() {
            TSslContextPtr ctx = CreateSslCtx(SSLv23_client_method());
            if (ClientCert_) {
                if (!ClientCert_->CertificateFile_ || !ClientCert_->PrivateKeyFile_) {
                    ythrow yexception() << "both client certificate and private key are required";
                }
                if (ClientCert_->PrivateKeyPassword_) {
                    SSL_CTX_set_default_passwd_cb(ctx.Get(), [](char* buf, int size, int rwflag, void* userData) -> int {
                        Y_UNUSED(rwflag);
                        auto io = static_cast<TSslIO*>(userData);
                        if (!io) {
                            return -1;
                        }
                        if (size < static_cast<int>(io->ClientCert_->PrivateKeyPassword_.size())) {
                            return -1;
                        }
                        return io->ClientCert_->PrivateKeyPassword_.copy(buf, size, 0);
                    });
                    SSL_CTX_set_default_passwd_cb_userdata(ctx.Get(), this);
                }
                if (1 != SSL_CTX_use_certificate_chain_file(ctx.Get(), ClientCert_->CertificateFile_.c_str())) {
                    ythrow TSslError() << "SSL_CTX_use_certificate_chain_file";
                }
                if (1 != SSL_CTX_use_PrivateKey_file(ctx.Get(), ClientCert_->PrivateKeyFile_.c_str(), SSL_FILETYPE_PEM)) {
                    ythrow TSslError() << "SSL_CTX_use_PrivateKey_file";
                }
                if (1 != SSL_CTX_check_private_key(ctx.Get())) {
                    ythrow TSslError() << "SSL_CTX_check_private_key (client)";
                }
            }
            return ctx;
        }

        inline TSslPtr ConstructSsl() { 
            TSslPtr ssl(SSL_new(Ctx.Get())); 
 
            if (!ssl) { 
                ythrow TSslError() << "SSL_new"; 
            } 
 
            if (VerifyCert_) {
                InitVerification(ssl.Get());
            }

            BIO_up_ref(Io); // SSL_set_bio consumes only one reference if rbio and wbio are the same
            SSL_set_bio(ssl.Get(), Io, Io);
 
            return ssl; 
        } 
 
        inline void InitVerification(ssl_st* ssl) {
            X509_VERIFY_PARAM* param = SSL_get0_param(ssl);
            X509_VERIFY_PARAM_set_hostflags(param, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
            Y_ENSURE(X509_VERIFY_PARAM_set1_host(param, VerifyCert_->Hostname_.data(), VerifyCert_->Hostname_.size()));
            SSL_set_tlsext_host_name(ssl, VerifyCert_->Hostname_.data()); // TLS extenstion: SNI

            SSL_CTX_set_cert_store(Ctx.Get(), GetBuiltinOpenSslX509Store().Release());

            Y_ENSURE_EX(1 == SSL_CTX_set_default_verify_paths(Ctx.Get()),
                        TSslError());
            // it is OK to ignore result of SSL_CTX_load_verify_locations():
            // Dir "/etc/ssl/certs/" may be missing
            SSL_CTX_load_verify_locations(Ctx.Get(),
                                          "/etc/ssl/certs/ca-certificates.crt",
                                          "/etc/ssl/certs/");

            SSL_set_verify(ssl, SSL_VERIFY_PEER, nullptr);
        }

        inline void Connect() { 
            if (SSL_connect(Ssl.Get()) != 1) { 
                ythrow TSslError() << "SSL_connect"; 
            } 
        } 
 
        inline void Finish() const { 
            SSL_shutdown(Ssl.Get()); 
        } 
 
        inline size_t Read(void* buf, size_t len) { 
            const int ret = SSL_read(Ssl.Get(), buf, len); 
 
            if (ret < 0) { 
                ythrow TSslError() << "SSL_read"; 
            } 
 
            return ret; 
        } 
 
        inline void Write(const char* buf, size_t len) { 
            while (len) { 
                const int ret = SSL_write(Ssl.Get(), buf, len); 
 
                if (ret < 0) { 
                    ythrow TSslError() << "SSL_write"; 
                } 
 
                buf += (size_t)ret; 
                len -= (size_t)ret; 
            } 
        } 
 
        TStreamIO Io;
        TSslContextPtr Ctx; 
        TSslPtr Ssl; 
    }; 
} 
 
struct TOpenSslClientIO::TImpl: public TSslIO { 
    inline TImpl(IInputStream* in, IOutputStream* out, const TOptions& opts)
        : TSslIO(in, out, opts) 
    { 
    } 
}; 
 
TOpenSslClientIO::TOpenSslClientIO(IInputStream* in, IOutputStream* out)
    : Impl_(new TImpl(in, out, TOptions())) 
{ 
} 
 
TOpenSslClientIO::TOpenSslClientIO(IInputStream* in, IOutputStream* out, const TOptions& options)
    : Impl_(new TImpl(in, out, options)) 
{ 
} 
 
TOpenSslClientIO::~TOpenSslClientIO() { 
    try { 
        Impl_->Finish(); 
    } catch (...) { 
    } 
} 
 
void TOpenSslClientIO::DoWrite(const void* buf, size_t len) { 
    Impl_->Write((const char*)buf, len); 
} 
 
size_t TOpenSslClientIO::DoRead(void* buf, size_t len) { 
    return Impl_->Read(buf, len); 
} 

namespace NPrivate {
    void TSslDestroy::Destroy(x509_store_st* x509) noexcept {
        X509_STORE_free(x509);
    }
}

class TBuiltinCerts {
public:
    TBuiltinCerts() {
        TString c = NResource::Find("/builtin/cacert");

        TBioPtr cbio(BIO_new_mem_buf(c.data(), c.size()));
        Y_ENSURE_EX(cbio, TSslError() << "BIO_new_mem_buf");

        while (true) {
            TX509Ptr cert(PEM_read_bio_X509(cbio.Get(), nullptr, nullptr, nullptr));
            if (!cert) {
                break;
            }
            Certs.push_back(std::move(cert));
        }

        int err = GetLastSslError();
        if (!Certs.empty() && ERR_GET_LIB(err) == ERR_LIB_PEM && ERR_GET_REASON(err) == PEM_R_NO_START_LINE) {
            ERR_clear_error();
        } else {
            ythrow TSslError() << "can't load provided bundle: " << ERR_reason_error_string(err);
        }

        Y_ENSURE_EX(!Certs.empty(), TSslError());
    }

    TOpenSslX509StorePtr GetX509Store() const {
        TOpenSslX509StorePtr store(X509_STORE_new());

        for (const TX509Ptr& c : Certs) {
            if (0 == X509_STORE_add_cert(store.Get(), c.Get())) {
                int err = GetLastSslError();
                if (ERR_GET_LIB(err) == ERR_LIB_X509 && ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE) {
                    ERR_clear_error();
                } else {
                    ythrow TSslError() << "can't load provided bundle: " << ERR_reason_error_string(err);
                }
            }
        }

        return store;
    }

private:
    TDeque<TX509Ptr> Certs;
};

TOpenSslX509StorePtr GetBuiltinOpenSslX509Store() {
    return Singleton<TBuiltinCerts>()->GetX509Store();
}