summaryrefslogtreecommitdiffstats
blob: 968ef8fa0ab0c3d6af004325f341432ecc8950a0 (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
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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
/*
 * Copyright (c) 2010-2019 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <Swiften/Base/Platform.h>

#ifdef SWIFTEN_PLATFORM_WINDOWS
#include <windows.h>
#include <wincrypt.h>
#endif

#include <cassert>
#include <memory>
#include <vector>


#include <openssl/bio.h>
#include <openssl/err.h>
#include <openssl/pkcs12.h>

#if defined(SWIFTEN_PLATFORM_MACOSX)
#include <Security/Security.h>
#endif

#include <Swiften/Base/Log.h>
#include <Swiften/Base/Algorithm.h>
#include <Swiften/TLS/OpenSSL/OpenSSLContext.h>
#include <Swiften/TLS/OpenSSL/OpenSSLCertificate.h>
#include <Swiften/TLS/CertificateWithKey.h>
#include <Swiften/TLS/PKCS12Certificate.h>

#pragma GCC diagnostic ignored "-Wold-style-cast"
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#pragma clang diagnostic ignored "-Wshorten-64-to-32"
#pragma clang diagnostic ignored "-Wcast-align"
#pragma clang diagnostic ignored "-Wsign-conversion"

namespace Swift {

static const int MAX_FINISHED_SIZE = 4096;
static const int SSL_READ_BUFFERSIZE = 8192;

static void freeX509Stack(STACK_OF(X509)* stack) {
    sk_X509_free(stack);
}

namespace {
    class OpenSSLInitializerFinalizer {
        public:
            OpenSSLInitializerFinalizer() {
                SSL_load_error_strings();
                SSL_library_init();
                OpenSSL_add_all_algorithms();

                // Disable compression
                /*
                STACK_OF(SSL_COMP)* compressionMethods = SSL_COMP_get_compression_methods();
                sk_SSL_COMP_zero(compressionMethods);*/
            }

            ~OpenSSLInitializerFinalizer() {
                EVP_cleanup();
            }

            OpenSSLInitializerFinalizer(const OpenSSLInitializerFinalizer &) = delete;
    };

    std::unique_ptr<SSL_CTX> createSSL_CTX(OpenSSLContext::Mode mode) {
        std::unique_ptr<SSL_CTX> sslCtx;
        switch (mode) {
            case OpenSSLContext::Mode::Client:
                sslCtx = std::unique_ptr<SSL_CTX>(SSL_CTX_new(SSLv23_client_method()));
                break;
            case OpenSSLContext::Mode::Server:
                sslCtx = std::unique_ptr<SSL_CTX>(SSL_CTX_new(SSLv23_server_method()));
                break;
        }
        return sslCtx;
    }

    std::string openSSLInternalErrorToString() {
        auto bio = std::shared_ptr<BIO>(BIO_new(BIO_s_mem()), BIO_free);
        ERR_print_errors(bio.get());
        std::string errorString;
        errorString.resize(BIO_pending(bio.get()));
        BIO_read(bio.get(), (void*)errorString.data(), errorString.size());
        return errorString;
    }
 }

OpenSSLContext::OpenSSLContext(Mode mode) : mode_(mode), state_(State::Start) {
    ensureLibraryInitialized();
    context_ = createSSL_CTX(mode_);
    SSL_CTX_set_options(context_.get(), SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3);

    if (mode_ == Mode::Server) {
#if OPENSSL_VERSION_NUMBER < 0x1010
        // Automatically select highest preference curve used for ECDH temporary keys used during
        // key exchange if possible.
        // Since version 1.1.0, this option is always enabled.
        SSL_CTX_set_ecdh_auto(context_.get(), 1);
#endif

        SSL_CTX_set_tlsext_servername_arg(context_.get(), this);
        SSL_CTX_set_tlsext_servername_callback(context_.get(), OpenSSLContext::handleServerNameCallback);
    }

    // TODO: implement CRL checking
    // TODO: download CRL (HTTP transport)
    // TODO: cache CRL downloads for configurable time period

    // TODO: implement OCSP support
    // TODO: handle OCSP stapling see https://www.rfc-editor.org/rfc/rfc4366.txt
    // Load system certs
#if defined(SWIFTEN_PLATFORM_WINDOWS)
    X509_STORE* store = SSL_CTX_get_cert_store(context_.get());
    HCERTSTORE systemStore = CertOpenSystemStore(0, "ROOT");
    if (systemStore) {
        PCCERT_CONTEXT certContext = NULL;
        while (true) {
            certContext = CertFindCertificateInStore(systemStore, X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, 0, CERT_FIND_ANY, NULL, certContext);
            if (!certContext) {
                break;
            }
            OpenSSLCertificate cert(createByteArray(certContext->pbCertEncoded, certContext->cbCertEncoded));
            if (store && cert.getInternalX509()) {
                X509_STORE_add_cert(store, cert.getInternalX509().get());
            }
        }
    }
#elif !defined(SWIFTEN_PLATFORM_MACOSX)
    SSL_CTX_set_default_verify_paths(context_.get());
#elif defined(SWIFTEN_PLATFORM_MACOSX) && !defined(SWIFTEN_PLATFORM_IPHONE)
    // On Mac OS X 10.5 (OpenSSL < 0.9.8), OpenSSL does not automatically look in the system store.
    // On Mac OS X 10.6 (OpenSSL >= 0.9.8), OpenSSL *does* look in the system store to determine trust.
    // However, if there is a certificate error, it will always emit the "Invalid CA" error if we didn't add
    // the certificates first. See
    //        http://opensource.apple.com/source/OpenSSL098/OpenSSL098-27/src/crypto/x509/x509_vfy_apple.c
    // to understand why. We therefore add all certs from the system store ourselves.
    X509_STORE* store = SSL_CTX_get_cert_store(context_.get());
    CFArrayRef anchorCertificates;
    if (SecTrustCopyAnchorCertificates(&anchorCertificates) == 0) {
        for (int i = 0; i < CFArrayGetCount(anchorCertificates); ++i) {
            SecCertificateRef cert = reinterpret_cast<SecCertificateRef>(const_cast<void*>(CFArrayGetValueAtIndex(anchorCertificates, i)));
            CSSM_DATA certCSSMData;
            if (SecCertificateGetData(cert, &certCSSMData) != 0 || certCSSMData.Length == 0) {
                continue;
            }
            std::vector<unsigned char> certData;
            certData.resize(certCSSMData.Length);
            memcpy(&certData[0], certCSSMData.Data, certCSSMData.Length);
            OpenSSLCertificate certificate(certData);
            if (store && certificate.getInternalX509()) {
                X509_STORE_add_cert(store, certificate.getInternalX509().get());
            }
        }
        CFRelease(anchorCertificates);
    }
#endif
}

OpenSSLContext::~OpenSSLContext() {
}

void OpenSSLContext::ensureLibraryInitialized() {
    static OpenSSLInitializerFinalizer openSSLInit;
}

void OpenSSLContext::initAndSetBIOs() {
    // Ownership of BIOs is transferred
    readBIO_ = BIO_new(BIO_s_mem());
    writeBIO_ = BIO_new(BIO_s_mem());
    SSL_set_bio(handle_.get(), readBIO_, writeBIO_);
}

void OpenSSLContext::accept() {
    assert(mode_ == Mode::Server);
    handle_ = std::unique_ptr<SSL>(SSL_new(context_.get()));
    if (!handle_) {
        state_ = State::Error;
        onError(std::make_shared<TLSError>(TLSError::AcceptFailed, openSSLInternalErrorToString()));
        return;
    }

    initAndSetBIOs();

    state_ = State::Accepting;
    doAccept();
}

void OpenSSLContext::connect() {
    connect(std::string());
}

void OpenSSLContext::connect(const std::string& requestedServerName) {
    assert(mode_ == Mode::Client);
    handle_ = std::unique_ptr<SSL>(SSL_new(context_.get()));
    if (!handle_) {
        state_ = State::Error;
        onError(std::make_shared<TLSError>(TLSError::ConnectFailed, openSSLInternalErrorToString()));
        return;
    }

    if (!requestedServerName.empty()) {
        if (SSL_set_tlsext_host_name(handle_.get(), const_cast<char*>(requestedServerName.c_str())) != 1) {
            onError(std::make_shared<TLSError>(TLSError::ConnectFailed, "Failed to set Server Name Indication: " + openSSLInternalErrorToString()));\
            return;
        }
    }

    // Ownership of BIOs is transferred to the SSL_CTX instance in handle_.
    initAndSetBIOs();

    state_ = State::Connecting;
    doConnect();
}

void OpenSSLContext::doAccept() {
    auto acceptResult = SSL_accept(handle_.get());
    auto error = SSL_get_error(handle_.get(), acceptResult);
    switch (error) {
        case SSL_ERROR_NONE: {
            state_ = State::Connected;
            //std::cout << x->name << std::endl;
            //const char* comp = SSL_get_current_compression(handle_.get());
            //std::cout << "Compression: " << SSL_COMP_get_name(comp) << std::endl;
            onConnected();
            // The following call is important so the client knowns the handshake is finished.
            sendPendingDataToNetwork();
            break;
        }
        case SSL_ERROR_WANT_READ:
            sendPendingDataToNetwork();
            break;
        case SSL_ERROR_WANT_WRITE:
            sendPendingDataToNetwork();
            break;
        default:
            state_ = State::Error;
            onError(std::make_shared<TLSError>(TLSError::AcceptFailed, openSSLInternalErrorToString()));
            sendPendingDataToNetwork();
    }
}

void OpenSSLContext::doConnect() {
    int connectResult = SSL_connect(handle_.get());
    int error = SSL_get_error(handle_.get(), connectResult);
    switch (error) {
        case SSL_ERROR_NONE: {
            state_ = State::Connected;
            //std::cout << x->name << std::endl;
            //const char* comp = SSL_get_current_compression(handle_.get());
            //std::cout << "Compression: " << SSL_COMP_get_name(comp) << std::endl;
            onConnected();
            break;
        }
        case SSL_ERROR_WANT_READ:
            sendPendingDataToNetwork();
            break;
        default:
            state_ = State::Error;
            onError(std::make_shared<TLSError>());
            onError(std::make_shared<TLSError>(TLSError::ConnectFailed, openSSLInternalErrorToString()));
    }
}

int OpenSSLContext::handleServerNameCallback(SSL* ssl, int*, void* arg) {
    if (ssl == nullptr)
        return SSL_TLSEXT_ERR_NOACK;

    const char* servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
    if (servername) {
        auto serverNameString = std::string(servername);
        auto context = reinterpret_cast<OpenSSLContext*>(arg);
        context->onServerNameRequested(serverNameString);

        if (context->abortTLSHandshake_) {
            context->abortTLSHandshake_ = false;
            return SSL_TLSEXT_ERR_ALERT_FATAL;
        }
    }
    return SSL_TLSEXT_ERR_OK;
}

void OpenSSLContext::sendPendingDataToNetwork() {
    int size = BIO_pending(writeBIO_);
    if (size > 0) {
        SafeByteArray data;
        data.resize(size);
        BIO_read(writeBIO_, vecptr(data), size);
        onDataForNetwork(data);
    }
}

void OpenSSLContext::handleDataFromNetwork(const SafeByteArray& data) {
    BIO_write(readBIO_, vecptr(data), data.size());
    switch (state_) {
        case State::Accepting:
            doAccept();
            break;
        case State::Connecting:
            doConnect();
            break;
        case State::Connected:
            sendPendingDataToApplication();
            break;
        case State::Start: assert(false); break;
        case State::Error: /*assert(false);*/ break;
    }
}

void OpenSSLContext::handleDataFromApplication(const SafeByteArray& data) {
    auto ret = SSL_write(handle_.get(), vecptr(data), data.size());
    if (ret > 0 || SSL_get_error(handle_.get(), ret) == SSL_ERROR_WANT_READ) {
            sendPendingDataToNetwork();
    }
    else {
        state_ = State::Error;
        onError(std::make_shared<TLSError>(TLSError::UnknownError, openSSLInternalErrorToString()));
    }
}

void OpenSSLContext::sendPendingDataToApplication() {
    SafeByteArray data;
    data.resize(SSL_READ_BUFFERSIZE);
    int ret = SSL_read(handle_.get(), vecptr(data), data.size());
    while (ret > 0) {
        data.resize(ret);
        onDataForApplication(data);
        data.resize(SSL_READ_BUFFERSIZE);
        ret = SSL_read(handle_.get(), vecptr(data), data.size());
    }
    if (ret < 0 && SSL_get_error(handle_.get(), ret) != SSL_ERROR_WANT_READ) {
        state_ = State::Error;
        onError(std::make_shared<TLSError>(TLSError::UnknownError, openSSLInternalErrorToString()));
    }
}

bool OpenSSLContext::setCertificateChain(const std::vector<Certificate::ref>& certificateChain) {
    if (certificateChain.size() == 0) {
        SWIFT_LOG(warning) << "Trying to load empty certificate chain." << std::endl;
        return false;
    }

    // load endpoint certificate
    auto openSSLCert = std::dynamic_pointer_cast<OpenSSLCertificate>(certificateChain[0]);
    if (!openSSLCert) {
        return false;
    }

    if (SSL_CTX_use_certificate(context_.get(), openSSLCert->getInternalX509().get()) != 1) {
        return false;
    }

    if (certificateChain.size() > 1) {
        for (auto certificate : range(certificateChain.begin() + 1, certificateChain.end())) {
            auto openSSLCert = std::dynamic_pointer_cast<OpenSSLCertificate>(certificate);
            if (!openSSLCert) {
                return false;
            }
            if (SSL_CTX_add_extra_chain_cert(context_.get(), openSSLCert->getInternalX509().get()) != 1) {
                SWIFT_LOG(warning) << "Trying to load empty certificate chain." << std::endl;
                return false;
            }
        }
    }

    if (handle_) {
        // This workaround is needed as OpenSSL has a shortcut to not do anything
        // if you set the SSL_CTX to the existing SSL_CTX and not reloading the
        // certificates from the SSL_CTX.
        auto dummyContext = createSSL_CTX(mode_);
        SSL_set_SSL_CTX(handle_.get(), dummyContext.get());
        SSL_set_SSL_CTX(handle_.get(), context_.get());
    }

    return true;
}

int empty_or_preset_password_cb(char* buf, int max_len, int flag, void* password);

int empty_or_preset_password_cb(char* buf, int max_len, int /* flag */, void* password) {
    char* charPassword = (char*)password;
    if (charPassword == nullptr) {
        return 0;
    }
    int len = strlen(charPassword);
    if(len > max_len) {
        return 0;
    }
    memcpy(buf, charPassword, len);
    return len;
}

bool OpenSSLContext::setPrivateKey(const PrivateKey::ref& privateKey) {
    if (privateKey->getData().size() > std::numeric_limits<int>::max()) {
        return false;
    }

    auto bio = std::shared_ptr<BIO>(BIO_new(BIO_s_mem()), BIO_free);
    BIO_write(bio.get(), vecptr(privateKey->getData()), int(privateKey->getData().size()));

    SafeByteArray safePassword;
    void* password = nullptr;
    if (privateKey->getPassword()) {
        safePassword = privateKey->getPassword().get();
        safePassword.push_back(0);
        password = safePassword.data();
    }
    auto resultKey = PEM_read_bio_PrivateKey(bio.get(), nullptr, empty_or_preset_password_cb, password);
    if (resultKey) {
        if (handle_) {
            auto result = SSL_use_PrivateKey(handle_.get(), resultKey);;
            if (result != 1) {
                return false;
            }
        }
        else {
            auto result = SSL_CTX_use_PrivateKey(context_.get(), resultKey);
            if (result != 1) {
                return false;
            }
        }
    }
    else {
        return false;
    }
    return true;
}

void OpenSSLContext::setAbortTLSHandshake(bool abort) {
    abortTLSHandshake_ = abort;
}

bool OpenSSLContext::setClientCertificate(CertificateWithKey::ref certificate) {
    std::shared_ptr<PKCS12Certificate> pkcs12Certificate = std::dynamic_pointer_cast<PKCS12Certificate>(certificate);
    if (!pkcs12Certificate || pkcs12Certificate->isNull()) {
        return false;
    }

    // Create a PKCS12 structure
    BIO* bio = BIO_new(BIO_s_mem());
    BIO_write(bio, vecptr(pkcs12Certificate->getData()), pkcs12Certificate->getData().size());
    std::shared_ptr<PKCS12> pkcs12(d2i_PKCS12_bio(bio, nullptr), PKCS12_free);
    BIO_free(bio);
    if (!pkcs12) {
        return false;
    }

    // Parse PKCS12
    X509 *certPtr = nullptr;
    EVP_PKEY* privateKeyPtr = nullptr;
    STACK_OF(X509)* caCertsPtr = nullptr;
    SafeByteArray password(pkcs12Certificate->getPassword());
    password.push_back(0);
    int result = PKCS12_parse(pkcs12.get(), reinterpret_cast<const char*>(vecptr(password)), &privateKeyPtr, &certPtr, &caCertsPtr);
    if (result != 1) {
        return false;
    }
    std::shared_ptr<X509> cert(certPtr, X509_free);
    std::shared_ptr<EVP_PKEY> privateKey(privateKeyPtr, EVP_PKEY_free);
    std::shared_ptr<STACK_OF(X509)> caCerts(caCertsPtr, freeX509Stack);

    // Use the key & certificates
    if (SSL_CTX_use_certificate(context_.get(), cert.get()) != 1) {
        return false;
    }
    if (SSL_CTX_use_PrivateKey(context_.get(), privateKey.get()) != 1) {
        return false;
    }
    for (int i = 0;  i < sk_X509_num(caCerts.get()); ++i) {
        SSL_CTX_add_extra_chain_cert(context_.get(), sk_X509_value(caCerts.get(), i));
    }
    return true;
}

bool OpenSSLContext::setDiffieHellmanParameters(const ByteArray& parametersInOpenSslDer) {
    auto bio = std::unique_ptr<BIO, decltype(&BIO_free)>(BIO_new(BIO_s_mem()), BIO_free);
    if (bio) {
        BIO_write(bio.get(), vecptr(parametersInOpenSslDer), parametersInOpenSslDer.size());
        auto result = 0L;
        if (auto dhparams = d2i_DHparams_bio(bio.get(), NULL)) {
            if (handle_) {
                result = SSL_set_tmp_dh(handle_.get(), dhparams);
            }
            else {
                result = SSL_CTX_set_tmp_dh(context_.get(), dhparams);
            }
            DH_free(dhparams);
        }
        return result == 1;
    }
    return false;
}

std::vector<Certificate::ref> OpenSSLContext::getPeerCertificateChain() const {
    std::vector<Certificate::ref> result;
    STACK_OF(X509)* chain = SSL_get_peer_cert_chain(handle_.get());
    for (int i = 0; i < sk_X509_num(chain); ++i) {
        std::shared_ptr<X509> x509Cert(X509_dup(sk_X509_value(chain, i)), X509_free);

        Certificate::ref cert = std::make_shared<OpenSSLCertificate>(x509Cert);
        result.push_back(cert);
    }
    return result;
}

std::shared_ptr<CertificateVerificationError> OpenSSLContext::getPeerCertificateVerificationError() const {
    int verifyResult = SSL_get_verify_result(handle_.get());
    if (verifyResult != X509_V_OK) {
        return std::make_shared<CertificateVerificationError>(getVerificationErrorTypeForResult(verifyResult));
    }
    else {
        return std::shared_ptr<CertificateVerificationError>();
    }
}

ByteArray OpenSSLContext::getFinishMessage() const {
    ByteArray data;
    data.resize(MAX_FINISHED_SIZE);
    auto size = SSL_get_finished(handle_.get(), vecptr(data), data.size());
    data.resize(size);
    return data;
}

ByteArray OpenSSLContext::getPeerFinishMessage() const {
    ByteArray data;
    data.resize(MAX_FINISHED_SIZE);
    auto size = SSL_get_peer_finished(handle_.get(), vecptr(data), data.size());
    data.resize(size);
    return data;
 }

CertificateVerificationError::Type OpenSSLContext::getVerificationErrorTypeForResult(int result) {
    assert(result != 0);
    switch (result) {
        case X509_V_ERR_CERT_NOT_YET_VALID:
        case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
            return CertificateVerificationError::NotYetValid;

        case X509_V_ERR_CERT_HAS_EXPIRED:
        case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
            return CertificateVerificationError::Expired;

        case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
        case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
            return CertificateVerificationError::SelfSigned;

        case X509_V_ERR_CERT_UNTRUSTED:
            return CertificateVerificationError::Untrusted;

        case X509_V_ERR_CERT_REJECTED:
            return CertificateVerificationError::Rejected;

        case X509_V_ERR_INVALID_PURPOSE:
            return CertificateVerificationError::InvalidPurpose;

        case X509_V_ERR_PATH_LENGTH_EXCEEDED:
            return CertificateVerificationError::PathLengthExceeded;

        case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE:
        case X509_V_ERR_CERT_SIGNATURE_FAILURE:
        case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
            return CertificateVerificationError::InvalidSignature;

        case X509_V_ERR_INVALID_CA:
        case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
        case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY:
        case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
            return CertificateVerificationError::InvalidCA;

        case X509_V_ERR_SUBJECT_ISSUER_MISMATCH:
        case X509_V_ERR_AKID_SKID_MISMATCH:
        case X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH:
        case X509_V_ERR_KEYUSAGE_NO_CERTSIGN:
            return CertificateVerificationError::UnknownError;

        // Unused / should not happen
        case X509_V_ERR_CERT_REVOKED:
        case X509_V_ERR_OUT_OF_MEM:
        case X509_V_ERR_UNABLE_TO_GET_CRL:
        case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE:
        case X509_V_ERR_CRL_SIGNATURE_FAILURE:
        case X509_V_ERR_CRL_NOT_YET_VALID:
        case X509_V_ERR_CRL_HAS_EXPIRED:
        case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD:
        case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD:
        case X509_V_ERR_CERT_CHAIN_TOO_LONG:
        case X509_V_ERR_APPLICATION_VERIFICATION:
        default:
            return CertificateVerificationError::UnknownError;
    }
}

}