1
0
mirror of https://github.com/mariadb-corporation/mariadb-connector-c.git synced 2025-08-08 14:02:17 +03:00
Commit Graph

52 Commits

Author SHA1 Message Date
Vladislav Vaintroub
8804593283 CONC-767 Improve SSL verification performance on Windows
Fixes slow SSL handshakes in network-restricted environments. On Windows,
the verification process uses the CertGetCertificateChain API, which
may attempt to refresh the CA list or fetch CRLs/OCSP data from the
network. This can trigger slow network lookups when no CA or CRL is
explicitly specified.

This patch disables these unnecessary network calls by using flags
like CERT_CHAIN_CACHE_ONLY_URL_RETRIEVAL to prevent external requests
during certificate chain validation. Additionally, it applies
Microsoft-recommended optimizations to speed up certificate handling
and avoid delays in SSL handshakes.

Also, unless explicitly requested(via ca_cert or crl_file or similar),
do not bother to verify name, CA or CRL for local connections. It saves
time. The failures in verification were previously discarded anyway.
2025-04-17 19:55:17 +02:00
Georg Richter
4a157ffbb5 Merge branch '3.3' into 3.4 2024-08-31 07:37:31 +02:00
Georg Richter
312b7eab4d Folow up of CONC-567 Schannel:
Added TLSv1.3 in ma_tls_get_protocol_version
2024-08-06 13:48:14 +02:00
Sergei Golubchik
0f3a41ec77 TLS post-fixes
* fix comments
* reorder errors to put hard errors on top
* report errors from openssl
* don't overwrite errors in C/C
* pass correct flags to gnutls_x509_crt_check_hostname2()
* use the same define name everywhere consistently
* don't recalculate fingerprint in openssl unnecessary
* misc
2024-08-03 16:37:57 +02:00
Vladislav Vaintroub
1e8e1f4f38 Fix "set but not used" warnings. 2024-07-31 20:48:44 +02:00
Vladislav Vaintroub
d15c73859c CONC-567 Schannel - handle SEC_I_RENEGOTIATE, prepare for TLSv1.3
There is no real renegotiation in TLSv1.3 protocol, so it is
some internal schannel thing, that makes DecryptMessage() to return
SEC_I_RENEGOTIATE, to replay a handshake step.

This pops up when TLSv1.3 is enabled.
2024-07-31 20:48:08 +02:00
Georg Richter
c5d2a0ebb3 TLS (schannel) fixes:
- don't verify fingerprint twice
- pci->dwVersion (certificate version) needs to be increased by 1
- use MARIADB_TLS_VERIFY_UNKNOWN for unknown tls verification errors
2024-07-18 05:44:50 +02:00
Georg Richter
1287c901dc TLS/SSL changes (major rework)
Peer certificate validation:

Since version 3.4 peer certificate verification is enabled by default.
It can be disabled via `mysql_optionsv`, using option
MYSQL_OPT_SSL_VERIFY_SERVER_CERT:

    my_bool verify= 0;
    mysql_options(mariadb, MYSQL_OPT_SSL_VERIFY_SERVER_CERT, &verify);

Self signed certificates

If the client obtained a self signed peer certificate from MariaDB server
the verification will fail, with the following exceptions:

* If the connection between client and server is considered to be secure:, e.g.
  * a unix_socket is used for client server communication
  * hostname is localhost (Windows operating system), 127.0.0.1 or ::1
* a specified fingerprint matches the fingerprint of the peer certificate (see below)
* a client can verify the certificate using account password, it's possible if
  * account has a password
  * authentication plugin is "secure without TLS", that is, one of
    mysql_native_password, ed25519 or parsec.

Fingerprint verification of the peer certificate

A fingerprint is a cryptographic hash (SHA-256, SHA-384 or SHA-512) of the peer
certificate's binary data. Even if the fingerprint matches, an expired or
revoked certificate will not be accepted.

For security reasons support for MD5 and SHA1 has been removed.

Technical details:
==================

- Peer certificate verification call was removed from ma_tls_connect, instead it
  will be called directly after the handshake succeeded (my_auth.c)

- mysql->net.tls_self_signed_error was replaced by mysql->net.tls_verify_status which
  contains the result of the peer certfificate verification:

  The verification status can be obtained with mariadb_get_infov using new parameter
  MARIADB_TLS_VERIFY_STATUS.

  unsigned int tls_verify_status;
  mariadb_get_infov(mysql, MARIADB_TLS_VERIFY_STATUS, &tls_verify_status);

  The result is a combination of the following flags:

  MARIADB_TLS_VERIFY_OK                  0
  MARIADB_TLS_VERIFY_TRUST               1
  MARIADB_TLS_VERIFY_HOST                2
  MARIADB_TLS_VERIFY_PERIOD              4
  MARIADB_TLS_VERIFY_FINGERPRINT         8
  MARIADB_TLS_VERIFY_REVOKED            16
  MARIADB_TLS_VERIFY_UNKNOWN            32

- GnuTLS peer certificate verification callback was removed and replaced by
  gnutls_verify_peers2() api function, so the peer certificate validation
  will happen after handshake.

- OpenSSL implementation will no longer use SSL_verify_result to check the
  validity of the peer certificate. Instead a callback function will be called
  during the handshake, which collects all certificate validation errors.

- If the peer certificate is not trusted, hostname verification will be
  skipped.

- Testing
  Added new test tls, which implements a python based dummy server, which allows
  to set different certificates and TLS options. Please note. that tests are
  expected to fail, since the server doesn't support further steps like user
  authentication etc. after the handshake. Prerequisite for running the tls test
  is Python3.
2024-07-16 13:12:26 +02:00
Sergei Golubchik
f6e99af056 Revert "self-signed certificate verification", it's 3.4 feature
This reverts 395641549ac7..536d9e2b9e5b, in particular:

8dffd56936 MDEV-31857 enable MYSQL_OPT_SSL_VERIFY_SERVER_CERT by default
a99570c118 MDEV-31855 SSL cert validation protocol extension
9aa15e72a7 TLS fingerprint

and related commits
2024-02-19 11:16:26 +01:00
Sergei Golubchik
a99570c118 MDEV-31855 SSL cert validation protocol extension
* extend the client auth plugin API with a new callback
* relax the plugin version check to allow load a plugin with the
  same major version, even if the minor versions differ
* implement the protocol extension:
  - don't abort at once if the certificate is self signed and
    no CA was explicitly specified
  - allow it if it passes fingerprint check
  - allow it if plugin has hash_password_bin callback, password was
    non-empty and the control hash matches server's
2024-02-04 22:17:25 +01:00
Georg Richter
12722e3131 Error message fix:
Since TLS errors might happen not only when connecting and SSL protocol
is not longer used, errormessage for CR_SSL_CONNECTION_ERROR was replaced
by TLS/SSL error.
2022-07-27 14:52:20 +02:00
Vladislav Vaintroub
15c7004022 Fix clang-cl warnings "variable initialized but unused"
Fix some comments.
2021-10-11 20:06:35 +02:00
Vladislav Vaintroub
cc56a1ced5 Fix MSVC/ASAN error
Apparently, it complains about wild pointer freed in when using
ASAN_OPTIONS= windows_hook_rtl_allocators=true

while it seems like a false positive, take it as a good opportunity to
remove allocators from Win3.1 times, and replace with simple malloc/free
2021-08-13 08:15:40 +02:00
Oleksandr Byelkin
895dcb61e3 C preprocessor defines fix 2020-06-04 16:49:21 +02:00
Vladislav Vaintroub
63df45ce3d CONC-447 ERROR 2026 (HY000): SSL connection error: Certificate signature check failed
Implement proper verification for server certificate chain,
with refactoring of the certificate stuff.

If custom CA and CRL certs are given, load them into in-memory store, and
use CertVerifyCertificateChainPolicy() to verify the certificate chain.

There are minor errors fixed, such as
- now there is a support for private keys encoded as BEGIN/END PRIVATE KEY
in PEM, instead of only BEGIN/END RSA PRIVATE KEY
- memory leak around CryptAcquireContext() is fixed i.e when client loads
private key, it previously did never released it, not even when connection
ended.

The handling of certificates moved into schannel_certs.c from various places
2019-12-08 18:07:48 +01:00
Vladislav Vaintroub
c8833751cf CONC-446 For Schannel errors, provide better errors
Print error symbol (e.g S"EC_E_ILLEGAL_MESSAGE") in the error message
for some schannel errors, in addition to error message

Print error code for all errors coming from schannel.

Fix some whitespace.
2019-11-29 11:19:48 +01:00
Vladislav Vaintroub
f035fc5f7f MDEV-13492 : SEC_E_INVALID_TOKEN when server sends large message during
SSL handshake

Bug:
The SECBUFFER_EXTRA returned by Schannel was incorrectly handled,
and unprocessed bytes were overriden by the new pvio read,
instead of being passed to InitializeSecurityContext().

Side note:
The code for ma_schannel_handshake_loop() was "inspired" (copied from)
http://www.coastrd.com/c-schannel-smtp did not have the bug,
it was introduced somehow by own modifications.
2019-11-28 22:45:55 +01:00
Georg Richter
9ba8e32f6d Fix for CONC-418:
For unknown/not handled schannel error codes we now use the
FormatMessage function instead of returning "Unknown error" message.
2019-09-21 09:17:36 +02:00
Georg Richter
de04c2e01f Workaround for CONC-417, MDEV-13492
At irregular intervals older windows versions (prior Windows 10) fail to establish a secure (TLS)
connection and return errors SEC_E_INVALID_TOKEN, SEC_E_BUFFER_TOO_SMALL or SEC_E_MESSAGE_ALTERED.
This is a bug in windows schannel library and was only fixed in recent versions, also OpenSSL provided
a workaround (see https://github.com/openssl/openssl/pull/1350).
Since we are unable to fix this, we introduced a workaround for this problem. In case of an error
during TLS handshake we check the errorcode and try to reconnect up to three times if the error code
was SEC_E_INVALID_TOKEN, SEC_E_BUFFER_TOO_SMALL or SEC_E_MESSAGE_ALTERED.
2019-09-19 08:50:55 +02:00
Georg Richter
b0411b731f CONC-386:
Added support for pem files which contain certificate and private key.
In case the file will contain more than one certificate or key, the first
certificate or key found will be used.
2019-06-02 13:39:27 +02:00
luz.paz
b2e6ed0295 Fixes misc. typos
found via `codespell -q 3`
2018-04-13 07:30:23 +02:00
Vladislav Vaintroub
db1a1a1d31 more clang fixes 2018-02-12 09:29:27 +00:00
Vladislav Vaintroub
aed8005e2e Fix clang on Windows warnings
- STDCALL is ignored for variable argument functions.
- __attribute__ does work for Clang (also if that pretends to be MSVC)
- remove unused function
- simplify ma_getopt, fixes some strange compile error in clang (about
SSE intrinsics)

- fix some clang warnings
2018-02-09 19:10:50 +01:00
Vladislav Vaintroub
02d8cc08cd MDEV-11546 main.ssl_7937 failed with timeout in buildbot on Windows
fix ma_schannel_read_decrypt w.r.t handling of SECBUFFER_EXTRA.

Do not do pvio_read, if last call to DecryptMessage() returned SECBUFFER_EXTRA.
2017-11-12 21:29:10 +00:00
Georg Richter
b241f8995f MDEV-14101: tls-version
Client part of MDEV-14101: Add support for tls-version, via
mysql_options(mysql, MARIADB_OPT_TLS_VERSION, value)
Accepted values are "TLSv1.1", "TLSv1.2" and "TLSv1.3".

Fixed testcase openssl_1 for schannel
2017-10-23 11:09:54 +02:00
Georg Richter
02f57a9c14 Fix for CONC-276: client library crashes on Windows after TLS reconnect:
The connection pointer mysql is now no longer part (and doesn't need to be updated) of schannel security context, since it can be obtained directly from tls container.
2017-08-24 18:09:50 +02:00
Georg Richter
b4681a2258 Removed dump information, which was previously added by mistake 2017-05-02 11:58:50 +02:00
Georg Richter
bde93e828d Merge branch 'master' of https://github.com/MariaDB/mariadb-connector-c 2017-04-03 18:12:13 +02:00
Georg Richter
4974bac88b Schannel fixes
To obtain the correct cipher suite name, we use the (undocumented) flag
  SECPKG_ATTR_CIPHER_INFO, which delivers cipher suite id and IANA cipher suite name.
  Added more cipher suites and mappings between IANA and OpenSSL cipher suite names
2017-03-23 17:04:33 +01:00
Georg Richter
9a865bc88c Fix for CONC-243:
ABI breakage: Revert parameter length from size_t to unsigned long.
  (affects mysql_stmt_prepare, mysql_real_query, mysql_send_query)
2017-03-14 16:11:04 +01:00
Vladislav Vaintroub
93af3ae693 Simplify and fix ma_schannel_read_decrypt() to cache state
between the calls.

State can be unread buffer  from DecryptMessage (SECBUFFER_EXTRA)
or decrypted data that did not fit into callers buffer

- Fix error handling - SEC_I_RENEGOTIATE is handled as error,
we're not doing it yet. Stop reading at SEC_I_CONTEXT_EXPIRED.

- Fix buffer sizes  pased to SSPI ( so that large buffers can be read or written
"SELECT REPEAT('a', 20000)"

- Fix unchecked memcpy into the output buffer (size of the output buffer
was not checked, so it is a potential memory overrun)
2016-10-14 17:49:30 +00:00
Vladislav Vaintroub
629ec64630 Fix PVIO to return number of bytes read/written as "signed" integer
since there is a lot of checks for return code being < 0 or -1.
2016-10-13 17:42:03 +00:00
Vladislav Vaintroub
2157642849 Cleanup/fix schannel TLS implementation
- remove global variables
- remove in memory certificate stores that cache all CRL and all CA
- verify certificate against ssl_ca and ssl_crl  specified in
connection options (not against all CRL/CA in store)
2016-09-09 20:17:30 +00:00
Georg Richter
07877e61cf Fix for CONC-180
In case handshake ended with Errorcode SEC_E_INTERNAL_ERROR we check
LastErrorCode (if it was set) and return system errormessage. For
timeout during SSL handshake we return the following error message:

ERROR 2026 (HY000): SSL connection error: A connection attempt failed
because the connected party did not properly respond after a period of time, or
established connection failed because connected host has failed to respond.
2016-05-15 15:41:45 +02:00
vvaintroub
c84de83ba3 Start all SSL bugs with 'SSL connection error' for common messaging across TLS implementation, and to pass the openssl_1 test cross-plattform 2016-05-11 17:11:09 +00:00
vvaintroub
49527f7590 Fix errors in openssl_1 test suite.
Provide mapping between openssl and schannel test suite ids.
This mapping is currently incomplete
2016-05-11 12:41:57 +00:00
buildbot
6190f608f9 Fix schannel problems that popup on Win2012 R2 buildbot
- Do not acquire a named context, because this might run
into permissions problem.
- Avoid sending TLS1.2 version by default. Yassl wrongfully rejects it
with a bad handshake (it should consider that 1.1 and 1.0 are supported too
but it does not)
2016-04-05 19:39:42 +00:00
buildbot
ec878da68f Fix duplicate CertFreeCertificateContext()
in case ma_schannel_load_private_key() fails

Also fix error checking for CryptAcquireContext
2016-04-05 16:08:36 +00:00
Georg Richter
4b1e94bccc Since we use TLS and not SSL functions and structures were renamed
from SSL to TLS
2016-03-16 18:20:08 +01:00
Georg Richter
bb365dd794 SSL fixes:
- wrong incude directory for OpenSSL
- added errormessage for SEC_E_ILLEGAL_MESSAGE
2016-03-14 12:11:36 +01:00
Georg Richter
d303cf76a7 More 10.2-integ fixes:
- renamed my_net functions (ma_net)
- fixed wrong types in ma_schannel.c
- fixed wrong parameter in client_plugin when building load string
2016-02-17 10:00:53 +01:00
Georg Richter
85525c2ba7 Merge remote-tracking branch 'origin/3.1' 2016-02-16 17:40:03 +01:00
Georg Richter
509b948e7d SSL fixes:
- added MARIADB_OPT_SSL_CIPHER_STRENGTH (value uint) for Schannel
- fixed mutes in all ssl variants
2016-02-16 13:04:16 +01:00
Georg Richter
b5cf443681 Windows fixes for remote_io plugin 2016-01-04 10:02:10 +01:00
Georg Richter
a3bb1d2009 merge from 3.0.0 fixes 2015-12-29 21:06:23 +01:00
Georg Richter
d73e4c23a2 Added new API function mysql_get_info/mysql_get_infov which retrieves
global or connection dependent information:

mysql_get_infov(MYSQL *mysql, enum mariadb_value value, void *arg, ...)

the following value types are supported:
    MARIADB_CHARSET_ID (requires numeric 4th parameter)
    MARIADB_CHARSET_INFO (requires string 4th parameter)
    MARIADB_CHARSET_NAME
    MARIADB_CLIENT_ERRORS
    MARIADB_CLIENT_VERSION
    MARIADB_CLIENT_VERSION_ID
    MARIADB_CONNECTION_ASYNC_TIMEOUT
    MARIADB_CONNECTION_ASYNC_TIMEOUT_MS
    MARIADB_CONNECTION_HOST
    MARIADB_CONNECTION_INFO
    MARIADB_CONNECTION_PORT
    MARIADB_CONNECTION_PROTOCOL_VERSION_ID
    MARIADB_CONNECTION_PVIO_TYPE
    MARIADB_CONNECTION_SCHEMA
    MARIADB_CONNECTION_SERVER_TYPE
    MARIADB_CONNECTION_SERVER_VERSION
    MARIADB_CONNECTION_SERVER_VERSION_ID
    MARIADB_CONNECTION_SOCKET
    MARIADB_CONNECTION_SSL_CIPHER
    MARIADB_CONNECTION_SSL_VERSION
    MARIADB_CONNECTION_SSL_VERSION_ID
    MARIADB_CONNECTION_TYPE
    MARIADB_CONNECTION_UNIX_SOCKET
    MARIADB_CONNECTION_USER
    MARIADB_MAX_ALLOWED_PACKET
    MARIADB_NET_BUFFER_LENGTH

MARIADB_CONNECTION prefix indicates that a valid connection handle has
to be passed as first parameter.
2015-12-28 07:32:53 +01:00
Georg Richter
d68c7dae95 Fixed warning on Windows 64-bit build 2015-12-19 17:07:10 +01:00
Georg Richter
72f7c4abf5 Windows 64-bit fixes:
changed type of length parameter in mysql_stmt_prepare,
mysql_real_query, mysql_stmt_send_long_data (incl. async _start
functions) from unsigned long to size_t.
Fixed warnings
2015-11-05 11:52:50 +01:00
Georg Richter
7500f37150 - Renamed cio to pvio (pluggable virtual IO)
- minor fixes in windows schannel
2015-10-27 10:24:48 +01:00
Georg Richter
05659c872e More plugin configuration changes (last commit was incomplete) 2015-10-02 10:09:41 +02:00