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

241 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
fb0eea663a Merge branch '3.3' into 3.4 2024-12-08 12:01:20 +01:00
Georg Richter
6d28fe89af Merge branch '3.1' into 3.3 2024-12-08 11:50:43 +01:00
Georg Richter
af44fc5c5e CONC-748: Allow to set TLSv1.3 ciphers in GnuTLS
- Added TLSv1.3 ciphers to cipher map.
- Fixed retrieval of cipher suite: Since gnutls_cipher_suite_get_name
  supports only TLSv1.2 (and lower) cipher suites, we have to call
  gnutls_ciphersuite_get() (requires GnuTLS 3.7.4 or newer).
2024-12-08 11:27:32 +01:00
Vladislav Vaintroub
73823d9b56 Merge branch '3.3' into 3.4 2024-11-07 08:47:37 +01:00
Vladislav Vaintroub
76564675fc Merge 3.1 into 3.3 2024-11-07 08:47:12 +01:00
Vladislav Vaintroub
b1f1267805 CONC-527 post-fix.
Add fallback if CryptAcquireContext fails with ERROR_ACCESS_DENIED,
as seen in Jenkins CI.

The fallback, also suggested by https://stackoverflow.com/a/14053718/547065,
is to retry with machine-wide key container, if user-specific fails.
2024-11-07 08:30:59 +01:00
Vladislav Vaintroub
58d773da30 Schannel - parse TLSv1.0, but ignore attempt to use it
This is apparently what server test suite wants in tls_version.test
2024-10-30 08:39:57 +01:00
Sergei Golubchik
4aebdb600b fix certificate verification for GnuTLS
* don't verify trust unless requested
* don't error about host of untrusted certificates
* auto-verification replaces both TRUST and HOST
2024-10-28 15:06:31 +01:00
Georg Richter
7cb4b05d99 Fix for CONC-732: Always set verification callback (OpenSSL)
If peer certificate verification was disabled, we also need
to set the verification callback function to avoid output
from OpenSSL's internal fallback function.
2024-09-25 08:08:39 +02:00
Georg Richter
968b5f0aa2 Fix for CONC-731: wrong error message (incorrect fp)
- moved fingerprint verification to ma_tls.c
- don't verify cert again if fingerprint check
  succeeded.
- Disable self signed check in fingerprint tests
  (Schannel only).
2024-09-24 12:08:42 +02:00
Georg Richter
a6fd09f1df OpenSSL timeout fix:
Since timeout was already set via setsockopt, we call wait_io_or_timeout()
with a very small timeout (5ms) to get a more precise errno, which is used
by OpenSSL's error function.
2024-09-17 10:58:25 +02:00
Georg Richter
21b12feaad Revert "Fix usage of wait_io_or_timeout from TLS layers" 2024-09-15 07:18:44 +02:00
Georg Richter
b45eaa7c9a Merge pull request #250 from azat-ch/wait_io_or_timeout-fixes
Fix usage of wait_io_or_timeout from TLS layers
2024-09-12 19:40:45 +02:00
Georg Richter
375720dc1c GnuTLS fixes:
To behave like OpenSSL and Schannel, we need to check the hostname,
even if other checks failed before.
2024-09-06 10:58:57 +02:00
Georg Richter
57f38cf87f Save the result of peer certificate verification
Since the MARIADB_TLS_VERIFY_TRUST flag might be cleared in my_auth,
we store the original result of peer certificate verification in
mysql->extension->tls_validation.
This value can be obtained via mariadb_get_infov API function
using option MARIADB_TLS_VERIFY_STATUS.
2024-08-31 07:53:46 +02:00
Georg Richter
4a157ffbb5 Merge branch '3.3' into 3.4 2024-08-31 07:37:31 +02:00
Georg Richter
c5ce23d340 Fix compiler warnings (schannel.c) 2024-08-30 07:39:46 +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
3ceb310ebe CONC-567 Schannel : support TLSv1.3
Use SCH_CREDENTIAL structure, to support TLSv1.3
Allow TLSv1.3 starting with Windows 11 / Server 2022, which are
first Windows releases to officially support latest TLS version
2024-07-31 20:48:34 +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
Vladislav Vaintroub
72116a30ab Merge branch '3.1' into 3.3 2024-07-29 11:53:15 +02:00
Vladislav Vaintroub
6a67a34f47 CONC-527 "SEC_E_ALGORITHM_MISMATCH" connecting Windows client to Ubuntu
The bug happens only when connecting with SSL with client certificates.

Apparently if client certificates are used in TLS handshake,
private keys for cert should be loaded into named persistent
container.This is because AcquireCredentialsHandle is done partically
out-of-process in lsass.exe, and lsass wants to read private keys from disk

See discussion in https://github.com/dotnet/runtime/issues/23749

Schannel has legacy behavior for ephemeral keys, not involving lsass,
and this is why it worked for us so far, however there are limitations.

It appears to only use rsa_sha1 for signature verification, and newer
OpenSSL no longer allows SHA1 for it, and this ends up in
"algorithm mismatch" message from schannel.

The above is just my understanding of how it works, because there is no
real documentation, the conclusion is based on discussion in
https://github.com/dotnet/runtime/issues/23749

The fix:
So storing the key in persistent named container evidently fixes it,
and this is what is done in this patch. Care is takes to destroy
key container after key is no longer needed, to
avoid filling  %AppData%\Roaming\Microsoft\Crypto\RSA with tiny encrypted
key files. Thus the "persistency window" of the key in container on disk
is only for duration of AcquireCredentialsHandle
2024-07-28 03:46:50 +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
Azat Khuzhin
b8a93c4519 Fix usage of wait_io_or_timeout from TLS layers
wait_io_or_timeout() accepts milliseconds, while options has seconds,
since this is just plain MYSQL_OPT_READ_TIMEOUT/...

The one that has milliseconds are pvio->timeout[PVIO_*_TIMEOUT], so use
them.

Usually this is not a problem, but, in case of interrupt (i.e. signal -
EINTR) SSL_read() will return SSL_ERROR_WANT_READ/SSL_ERROR_WANT_WRITE,
and then wait_io_or_timeout() will be called, and timeout will be wrong,
may cause a failure.
2024-07-01 15:36:52 +02:00
Georg Richter
7498d30a07 CONC-698: certificate info is read on every connect
Read and store peer certificate information only if
mariadb_get_options was called.
2024-06-21 16:14:36 +02:00
Georg Richter
71fa44cff0 CONC-698: certificate info is read on every connect
Part 1: Fix for OpenSSL and Schannel
2024-06-20 08:34:19 +02:00
Sergei Golubchik
cc985fab30 fix the memory leak with gnutls (~11K per connection) 2024-05-26 15:45:54 +02:00
Sergei Golubchik
875ef5ad64 fix the memory leak with openssl (~8K per connection) 2024-05-26 15:45:33 +02:00
Sergei Golubchik
d9a50aceac ASN1_TIME_to_tm was added in OpenSSL 1.1.1 2024-05-16 13:53:14 +02:00
Sergei Golubchik
deb38a3e1c fix the code for OpenSSL 1.0 2024-05-16 11:47:00 +02:00
Sergei Golubchik
e52197420e TLS fingerprint is returned in hex! 2024-05-15 16:39:18 +02:00
Georg Richter
4d46ae76bc Merge branch '3.1' into 3.3 2024-05-08 11:43:18 +02:00
Georg Richter
3228ed2ea3 Fix copy/paste error 2024-05-07 15:13:15 +02:00
Georg Richter
dc1606781f Fix build for OpenSSL < 1.1 2024-05-07 11:40:29 +02:00
Georg Richter
f7eab7d2e3 Merge branch '3.1' into 3.4 2024-04-29 14:18:54 +02:00
Josh Hunt
4c1c7f37d6 Fix SSL_read/write return value checking in ma_tls_async_check_result
SSL_{read,write}'s return values == 0 signify the operation was
unsuccessful, but here it's being treated as success. Other calls of
these functions already properly checks the return value.

Signed-off-by: Josh Hunt <johunt@akamai.com>
2024-04-29 13:47:36 +02:00
Georg Richter
19dffea4dc CONC-692: Provide X509 peer certificate information
Added a new structure MARIADB_X509_INFO, which
contains information about servers certificate.
The information can be obtained via mysql_get_infov API
function:

MARIADB_X509_INFO *info;
mariadb_get_infov(mysql, MARIADB_TLS_PEER_CERT_INFO, &info);
2024-04-24 11:21:28 +02:00
Georg Richter
fe411bf336 CONC-403:
Remove support of TLSv1.0 protocol
2024-02-20 09:52:07 +01: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
8dffd56936 MDEV-31857 enable MYSQL_OPT_SSL_VERIFY_SERVER_CERT by default
because the default value of every option is 0
(option and option.extension are bzero-ed to reset),
tls_verify_server_cert was renamed to tls_allow_invalid_server_cert
with the default value of 0, "do not allow".

API didn't change, it's still MYSQL_OPT_SSL_VERIFY_SERVER_CERT
2024-02-04 22:17:25 +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
9aa15e72a7 TLS fingerprint
Beside SHA1 fingerprint hash, Connector/C now also supports
SHA224 (OpenSSL and GnuTLS only), SHA256, SHA384 and SHA512
fingerprint hashes.
2024-02-04 22:17:22 +01:00
Vladislav Vaintroub
4692e9cec1 CONC-645 : fix build with clang (v16), clang-cl(v16), and mingw-gcc(v12).
schannel_certs.c - conflicting headers, include winsock2.h before windows.h,

strerror_r is now defined also with mingw

do not build mariadb_config on Windows, getopt.h is missing
2023-09-22 00:59:48 +02:00
Vladislav Vaintroub
463a50e973 Merge remote-tracking branch 'origin/3.1' into 3.3
# Conflicts:
#	include/ma_crypt.h
#	libmariadb/mariadb_lib.c
2023-09-21 23:45:35 +02:00
Vladislav Vaintroub
1b3cf6b441 CONC-669 Cache bcrypt algorithm providers in win_crypt.c
CryptOpenAlgorithmProvider() documentation states:
"We recommend that you cache any algorithm provider handles that you will
use more than once, rather than opening and closing the algorithm providers
over and over."

This patch implements algorithm handle caching,
and also simplifies how hashes are handled in win_crypt.c

- MA_HASH_CTX* is now just BCrypt hash handle.
- We no longer allocate or free memory any memory outselves.

There are small change in the ma_hash API:
ma_hash_new() lost the second parameter, only used on Windows previously
2023-09-21 18:06:01 +02:00
Georg Richter
4e3905c20a Fix for bcrypt hash functions:
Using a global crypt provider is not thread safe, so we need to load
provider via BCryptOpenProvider in ma_hash_new().
2023-08-23 16:18:50 +02:00