aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/http/fetch_gpl/sockhandler.h
blob: 91d4f67a0656536db322e32d448ec87f26d77221 (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
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
#pragma once

#include <library/cpp/http/fetch/sockhandler.h>
#include <contrib/libs/matrixssl/matrixSsl.h>

class TSslSocketBase {
public:
    static bool StaticInit(const char* caFile = nullptr);
    static bool StaticInit(unsigned char* caBuff, int caLen);
    static void StaticTerm();
    static int CertChecker(sslCertInfo_t* cert, void* arg);
    enum ECertErrors {
        SSL_CERT_UNTRUSTED = 0x01,
        SSL_CERT_BAD_CHAIN = 0x02,
        SSL_CERT_HOSTNAME_MISMATCH = 0x04,
        SSL_CERT_EXPIRED = 0x08
    };

    struct TSessIdDestructor {
        static void Destroy(sslSessionId_t* id) {
            matrixSslFreeSessionId(id);
        }
    };

protected:
    enum ESslStatus {
        SSLSOCKET_EOF = 0x1,
        SSLSOCKET_CLOSE_NOTIFY = 0x2
    };
    struct TSocketCtx {
        ui16 SslError;
        ui16 CertErrors;
        const char* Host;
        size_t HostLen;
        TSocketCtx()
            : SslError(0)
            , CertErrors(0)
            , Host(nullptr)
            , HostLen(0)
        {
        }
    };

protected:
    class TBufferAllocator {
        class TChunk;
        typedef TIntrusiveSListItem<TChunk> TChunkBase;

        class TChunk: public TChunkBase {
        public:
            inline unsigned char* ToPointer() {
                //shut up clang warning
                (void)Buf;

                return (unsigned char*)this;

                static_assert(sizeof(TChunk) >= SSL_MAX_BUF_SIZE, "expect sizeof(TChunk) >= SSL_MAX_BUF_SIZE");
            }
            static inline TChunk* FromPointer(unsigned char* ptr) {
                return (TChunk*)ptr;
            }

        private:
            unsigned char Buf[SSL_MAX_BUF_SIZE - sizeof(TChunkBase)];
        };

    public:
        TBufferAllocator()
            : NFree(0)
            , NAllocated(0)
        {
            static_assert(InitialItems > 0 && A1 > 0 && A2 > 0 && A1 >= A2, "expect InitialItems > 0 && A1 > 0 && A2 > 0 && A1 >= A2");
            ResizeList(InitialItems);
        }

        ~TBufferAllocator() {
            Y_VERIFY(!NAllocated, "Ssl bufferAllocator: %" PRISZT " blocks lost!", NAllocated);
            ResizeList(0);
        }

        unsigned char* Alloc() {
            TGuard<TMutex> guard(Lock);
            if (Free_.Empty())
                ResizeList(A2 * NAllocated);

            NAllocated++;
            NFree--;
            return Free_.PopFront()->ToPointer();
        }

        void Free(unsigned char* p) {
            if (!p)
                return;
            TGuard<TMutex> guard(Lock);
            Y_VERIFY(NAllocated, "Ssl bufferAllocator: multiple frees?");
            TChunk* ch = TChunk::FromPointer(p);
            Free_.PushFront(ch);
            NFree++;
            NAllocated--;

            // destroy some items if NFree/NAllocated increased too much
            size_t newSize = A2 * NAllocated;
            if (NAllocated + newSize >= InitialItems && NFree >= A1 * NAllocated)
                ResizeList(newSize);
        }

    private:
        inline void ResizeList(size_t newSize) {
            while (NFree < newSize) {
                Free_.PushFront(new TChunk);
                NFree++;
            }
            while (NFree > newSize) {
                TChunk* ch = Free_.PopFront();
                Y_VERIFY(ch, "Ssl bufferAllocator: internal error");
                delete ch;
                NFree--;
            }
        }

        static const size_t InitialItems = 100;
        static const unsigned A1 = 3; // maximum reserved/allocated ratio
        static const unsigned A2 = 1; // if ratio A1 is reached, decrease by A2

        TIntrusiveSList<TChunk> Free_;
        size_t NFree;
        size_t NAllocated;
        TMutex Lock;
    };

    static bool Initialized;
    static sslKeys_t* Keys;
    static THolder<TBufferAllocator> BufAlloc;

public:
    class TFakeLogger {
    public:
        static void Write(const char* /*format*/, ...) {
        }
    };
};

template <class TSocketHandler = TSimpleSocketHandler, class TErrorLogger = TSslSocketBase::TFakeLogger>
class TSslSocketHandler: public TSslSocketBase, protected TSocketHandler, TNonCopyable {
public:
    struct TSocketCtx: public TSslSocketBase::TSocketCtx {
        sslBuf_t InBuf;
        sslBuf_t InSock;
        THolder<sslSessionId_t, TSessIdDestructor> CachedSession;
        TSocketCtx() {
            Zero(InBuf);
            Zero(InSock);
        }
        void AllocBuffers() {
            Y_ASSERT(InBuf.size == 0);
            InBuf.size = SSL_MAX_BUF_SIZE;
            InBuf.start = InBuf.end = InBuf.buf = BufAlloc->Alloc();

            Y_ASSERT(InSock.size == 0);
            InSock.size = SSL_MAX_BUF_SIZE;
            InSock.start = InSock.end = InSock.buf = BufAlloc->Alloc();
        }
        void FreeBuffers() {
            if (InBuf.buf) {
                if (InBuf.end - InBuf.start) {
                    // We had some data read and decrypted. Too sad, nobody needs it now :(
                    TErrorLogger::Write("TSocketCtx::FreeBuffers: %i bytes of data lost in InBuf (%s)\n", (int)(InBuf.end - InBuf.start), TString(Host, HostLen).data());
                }
                BufAlloc->Free(InBuf.buf);
                Zero(InBuf);
            }
            if (InSock.buf) {
                if (InSock.end - InSock.start) {
                    // We had some data read and waiting for decryption. Most likely we disconnected before server's "bye".
                    TErrorLogger::Write("TSocketCtx::FreeBuffers: %i bytes of data lost in InSock (%s)\n", (int)(InSock.end - InSock.start), TString(Host, HostLen).data());
                }
                BufAlloc->Free(InSock.buf);
                Zero(InSock);
            }
        }
        void ResetBuffers() {
            InBuf.start = InBuf.end = InBuf.buf;
            InSock.start = InSock.end = InSock.buf;
        }
    };

    TSslSocketHandler()
        : TSocketHandler()
        , Ssl(nullptr)
    {
        Y_VERIFY(TSslSocketBase::Initialized, "Ssl library isn't initialized. Call TSslSocketBase::StaticInit() first");
    }

    virtual ~TSslSocketHandler() {
        Y_ASSERT(Initialized);
        Disconnect();
    }

    int Good() const {
        return TSocketHandler::Good();
    }
    bool HasSsl() const {
        return Ssl;
    }

    // set reconnect "true" to try to recover from cached session id
    int Connect(TSocketCtx* ctx, const TAddrList& addrs, TDuration timeout, bool isHttps, bool reconnect = false);

    // for debug "file" socket
    bool open(const char* name) {
        Y_ASSERT(Initialized);
        Disconnect();
        return TSocketHandler::open(name);
    }

    void Disconnect(TSocketCtx* ctx = nullptr) { // if ctx is non-NULL, cache session id in it.
        Y_ASSERT(Initialized);
        if (Ssl) {
            if (ctx) {
                sslSessionId_t* cs;
                if (matrixSslGetSessionId(Ssl, &cs) < 0) {
                    cs = nullptr;
                    TErrorLogger::Write("TSslSocketHandler::Disconnect: failed to create session id for host %s\n", TString(ctx->Host, ctx->HostLen).data());
                }
                ctx->CachedSession.Reset(cs);
            }
            matrixSslDeleteSession(Ssl);
            Ssl = nullptr;
        }
        TSocketHandler::Disconnect();
    }

    void shutdown() {
        TSocketHandler::shutdown();
    }

    ssize_t send(TSocketCtx* ctx, const void* message, size_t messlen) {
        Y_ASSERT(TSocketHandler::Good());
        if (!Ssl)
            return TSocketHandler::send(message, messlen);
        int status;
        int rc = SslWrite(ctx, static_cast<const char*>(message), (int)messlen, &status);
        if (rc < 0) {
            errno = status;
            ctx->ResetBuffers();
            Disconnect();
            return false;
        }
        Y_ASSERT((size_t)rc == messlen);
        return true;
    }

    bool peek(TSocketCtx* ctx) {
        if (!Ssl)
            return TSocketHandler::peek();
        int rc;
        int status;
        while (true) {
            rc = SslRead(ctx, nullptr, 0, &status);
            if (rc < 0) {
                errno = status;
                ctx->ResetBuffers();
                Disconnect();
                return false;
            } else if (rc > 0) {
                return true;
            }
            // else if (rc == 0)
            if (status) {
                errno = status;
                ctx->ResetBuffers();
                Disconnect();
                return false;
            }
        }
    }

    ssize_t read(TSocketCtx* ctx, void* message, size_t messlen) {
        if (!Ssl)
            return TSocketHandler::read(message, messlen);
        int rc;
        int status;
        if (!messlen)
            return 0;
        while (true) {
            rc = SslRead(ctx, static_cast<char*>(message), (int)messlen, &status);
            if (rc < 0) {
                errno = status;
                ctx->ResetBuffers();
                Disconnect();
                return rc;
            } else if (rc > 0)
                return rc;
            // else if (rc == 0)
            if (status) {
                errno = status;
                ctx->ResetBuffers();
                Disconnect();
                return 0;
            }
        }
    }

private:
    int SslRead(TSocketCtx* ctx, char* buf, int buflen, int* status);
    int SslWrite(TSocketCtx* ctx, const char* buf, int len, int* status);

    ssl_t* Ssl;
};

template <typename TSocketHandler, typename TErrorLogger>
int TSslSocketHandler<TSocketHandler, TErrorLogger>::Connect(TSocketCtx* ctx, const TAddrList& addrs, TDuration timeout, bool isHttps, bool reconnect) {
    Y_ASSERT(Initialized);
    ctx->SslError = 0;
    ctx->ResetBuffers();
    Disconnect();
    int res = TSocketHandler::Connect(addrs, timeout);
    if (!isHttps || res != 0) {
        ctx->CachedSession.Destroy();
        return res;
    }

    // create ssl session
    if ((res = matrixSslNewSession(&Ssl, Keys, reconnect ? ctx->CachedSession.Get() : nullptr, 0)) < 0) {
        ctx->SslError = 1;
        ctx->ResetBuffers();
        Disconnect();
        return res;
    }
    ctx->CachedSession.Destroy();

    matrixSslSetCertValidator(Ssl, CertChecker, ctx);

    // now it's time to perform handshake
    sslBuf_t outsock;
    outsock.buf = outsock.start = outsock.end = BufAlloc->Alloc();
    outsock.size = SSL_MAX_BUF_SIZE;

    res = matrixSslEncodeClientHello(Ssl, &outsock, 0);
    if (res) {
        TErrorLogger::Write("TSslSocketHandler::Connect: internal error %i\n", res);
        BufAlloc->Free(outsock.buf);
        ctx->SslError = 1;
        ctx->ResetBuffers();
        Disconnect();
        return -1;
    }

    if (!TSocketHandler::send(outsock.start, outsock.end - outsock.start)) {
        BufAlloc->Free(outsock.buf);
        ctx->SslError = 1;
        ctx->ResetBuffers();
        Disconnect();
        return -1;
    }
    BufAlloc->Free(outsock.buf);

    // SslRead will handle handshake and is suppozed to return 0
    int status, rc;
    int ncalls = 10; // FIXME: maybe it's better to check time
    while (true) {
        rc = SslRead(ctx, nullptr, 0, &status);
        if (rc == 0) {
            if (status == SSLSOCKET_EOF || status == SSLSOCKET_CLOSE_NOTIFY) {
                ctx->SslError = 1;
                ctx->ResetBuffers();
                Disconnect();
                return -1;
            }
            if (matrixSslHandshakeIsComplete(Ssl))
                break;
            if (--ncalls <= 0) {
                TErrorLogger::Write("TSslSocketHandler::Connect: handshake too long (server wants multiple handshakes maybe)\n");
                ctx->SslError = 1;
                ctx->ResetBuffers();
                Disconnect();
                return -1;
            }
            continue;
        } else if (rc > 0) {
            TErrorLogger::Write("TSslSocketHandler::Connect: server sent data instead of a handshake\n");
            ctx->SslError = 1;
            ctx->ResetBuffers();
            Disconnect();
            return -1;
        } else { // rc < 0
            //this is an error
            ctx->SslError = 1;
            ctx->ResetBuffers();
            Disconnect();
            return -1;
        }
    }

    return 0;
}

template <typename TSocketHandler, typename TErrorLogger>
int TSslSocketHandler<TSocketHandler, TErrorLogger>::SslRead(TSocketCtx* ctx, char* buf, int buflen, int* status) {
    Y_ASSERT(Initialized);
    int remaining, bytes, rc;

    *status = 0;
    if (Ssl == nullptr || buflen < 0)
        return -1;

    // Return data if we still have cached
    if (ctx->InBuf.start < ctx->InBuf.end) {
        remaining = (int)(ctx->InBuf.end - ctx->InBuf.start);
        if (!buflen) // polling
            return remaining;
        bytes = Min(buflen, remaining);
        memcpy(buf, ctx->InBuf.start, bytes);
        ctx->InBuf.start += bytes;
        return bytes;
    }

    // Pack buffered socket data (if any) so that start is at zero.
    if (ctx->InSock.buf < ctx->InSock.start) {
        if (ctx->InSock.start == ctx->InSock.end) {
            ctx->InSock.start = ctx->InSock.end = ctx->InSock.buf;
        } else {
            memmove(ctx->InSock.buf, ctx->InSock.start, ctx->InSock.end - ctx->InSock.start);
            ctx->InSock.end -= (ctx->InSock.start - ctx->InSock.buf);
            ctx->InSock.start = ctx->InSock.buf;
        }
    }

    bool performRead = false;
    bool dontPerformRead = false;
    unsigned char error;
    unsigned char alertLevel;
    unsigned char alertDescription;

    while (true) {
        // Read data from socket
        if (!dontPerformRead && (performRead || ctx->InSock.end == ctx->InSock.start)) {
            performRead = true;
            bytes = TSocketHandler::read((void*)ctx->InSock.end, (ctx->InSock.buf + ctx->InSock.size) - ctx->InSock.end);
            if (bytes == SOCKET_ERROR) {
                *status = errno;
                return -1;
            }
            if (bytes == 0) {
                *status = SSLSOCKET_EOF;
                return 0;
            }
            ctx->InSock.end += bytes;
        }
        dontPerformRead = false;

        error = 0;
        alertLevel = 0;
        alertDescription = 0;

        ctx->InBuf.start = ctx->InBuf.end = ctx->InBuf.buf;
        rc = matrixSslDecode(Ssl, &ctx->InSock, &ctx->InBuf, &error, &alertLevel, &alertDescription);

        switch (rc) {
            // Successfully decoded a record that did not return data or require a response.
            case SSL_SUCCESS:
                return 0;

            case SSL_PROCESS_DATA:
                rc = (int)(ctx->InBuf.end - ctx->InBuf.start);
                if (!buflen)
                    return rc;
                rc = Min(rc, buflen);
                memcpy(buf, ctx->InBuf.start, rc);
                ctx->InBuf.start += rc;
                return rc;

            case SSL_SEND_RESPONSE:
                if (!TSocketHandler::send(ctx->InBuf.start, ctx->InBuf.end - ctx->InBuf.start)) {
                    *status = errno;
                    return -1;
                }
                ctx->InBuf.start = ctx->InBuf.end = ctx->InBuf.buf;
                return 0;

            case SSL_ERROR:
                if (ctx->InBuf.start < ctx->InBuf.end)
                    TSocketHandler::send(ctx->InBuf.start, ctx->InBuf.end - ctx->InBuf.start);
                ctx->InBuf.start = ctx->InBuf.end = ctx->InBuf.buf;
                ctx->SslError = 1;
                return -1;

            case SSL_ALERT:
                if (alertDescription == SSL_ALERT_CLOSE_NOTIFY) {
                    *status = SSLSOCKET_CLOSE_NOTIFY;
                    ctx->InBuf.start = ctx->InBuf.end = ctx->InBuf.buf;
                    return 0;
                }
                ctx->InBuf.start = ctx->InBuf.end = ctx->InBuf.buf;
                ctx->SslError = 1;
                return -1;

            case SSL_PARTIAL:
                if (ctx->InSock.start == ctx->InSock.buf && ctx->InSock.end == (ctx->InSock.buf + ctx->InSock.size)) {
                    ctx->InSock.start = ctx->InSock.end = ctx->InSock.buf;
                    ctx->SslError = 1;
                    return -1;
                }
                if (!performRead) {
                    performRead = 1;
                    ctx->InBuf.start = ctx->InBuf.end = ctx->InBuf.buf;
                    continue;
                } else {
                    ctx->InBuf.start = ctx->InBuf.end = ctx->InBuf.buf;
                    return 0;
                }

            case SSL_FULL:
                ctx->InBuf.start = ctx->InBuf.end = ctx->InBuf.buf;
                ctx->SslError = 1;
                return -1;
        }
    }

    return 0;
}

template <typename TSocketHandler, typename TErrorLogger>
int TSslSocketHandler<TSocketHandler, TErrorLogger>::SslWrite(TSocketCtx* ctx, const char* buf, int len, int* status) {
    Y_ASSERT(Initialized);
    if (len <= 0)
        return len ? -1 : 0;
    int rc;
    *status = 0;

    sslBuf_t outsock;
    outsock.size = SSL_MAX_BUF_SIZE;
    outsock.start = outsock.end = outsock.buf = BufAlloc->Alloc();

    size_t remaining = len;
    while (remaining) {
        size_t l = Min<size_t>(remaining, SSL_MAX_PLAINTEXT_LEN);
        rc = matrixSslEncode(Ssl, (const unsigned char*)buf, l, &outsock);
        if (rc <= 0) {
            TErrorLogger::Write("TSslSocketHandler::SslWrite: internal error: %u\n", rc);
            BufAlloc->Free(outsock.buf);
            ctx->SslError = 1;
            return -1;
        }
        rc = TSocketHandler::send(outsock.start, outsock.end - outsock.start);
        if (!rc) {
            *status = errno;
            BufAlloc->Free(outsock.buf);
            return -1;
        }
        remaining -= l;
        buf += l;
        outsock.start = outsock.end = outsock.buf;
    }
    BufAlloc->Free(outsock.buf);
    return len;
}