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

1079 Commits

Author SHA1 Message Date
Georg Richter
662a966056 CONC-702: Fix statement status
If a resultset in binary protpcpl doesn't contain data
(fields equal zero) and there are no more pending
result sets we need to change the statement status to
FETCH_DONE, otherwise it will be impossible to execute
other commands until the statement was closed.
2024-11-18 07:15:42 +01:00
Georg Richter
55e3b63c34 CONPY-739: prepared statement support AUTO_SEC_PART_DIGITS
FROM_UNIXTIME() function always returns AUTO_SEC_PART_DIGITS
(value=39). In case the microsecond value was set in MYSQL_TIME,
the decimal part should be SEC_PART_DIGITS (=6).
2024-11-15 17:41:23 +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
Markus Mäkelä
8dce8dba85 CONC-741: Check that MYSQL_STMT is not null
The code now again allows null MYSQL_STMT handles to be passed to
mysql_stmt_close(). Added a sanity check test case that should guard
against this happening in the future.
2024-11-07 07:34:12 +02:00
Markus Mäkelä
a2213b89d4 Add MYSQL_OPT_ZSTD_COMPRESSION_LEVEL
The compression level was always hard-coded to 3.
2024-11-07 06:51:57 +02:00
Georg Richter
000ed6281f Fix for CONC-740:
Moved the initialization of server verification callback from
mysql_init to mysql_real_connect to avoid memory leak in case
of a reconnect.
2024-11-05 12:51:51 +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
53243b8a7a Follow up of d4a8ca9209
Last commit was incomplete
2024-10-23 13:08:42 +02:00
Georg Richter
d4a8ca9209 CONC-690: Remove OFF option for WITH_SSL
Due to required cryptographic functions which are part of the TLS
libraries it is no longer possible to build MariaDB Connector/C
without crypto libraries.
2024-10-23 08:04:21 +02:00
Georg Richter
a66f3fc302 Merge branch '3.3' into 3.4 2024-10-22 13:50:24 +02:00
Georg Richter
6635e4bdd6 Fix for CONC-735
If a reconnect occurs, we need to check if mysql->options.host was
already set and pass NULL instead of the previous host name.
2024-10-22 13:26:50 +02:00
Kristian Nielsen
3b29ff9c91 MDEV-34859: Failed to initialise non-blocking API on OpenBSD arm64
Follow-up patch to fix copy-paste error that causes incorrect restore of
registers in my_context_continue which can cause crashes on arm64.

Signed-off-by: Kristian Nielsen <knielsen@knielsen-hq.org>
2024-10-20 17:11:23 +02:00
Sergei Golubchik
6c0e755edf fix compilation errors with -flto
declare plugin structures using their correct type.
Fixes errors like

./libmariadb/libmariadb/ma_client_plugin.c:86:39: error: type of ‘pvio_socket_client_plugin’ does not match original declaration [-Werror=lto-type-mismatch]
./libmariadb/plugins/pvio/pvio_socket.c:141:21: note: type ‘struct MARIADB_PVIO_PLUGIN’ should match type ‘struct st_mysql_client_plugin’

Approved by: Georg Richter <georg@mariadb.com>
2024-10-17 10:02:07 +02:00
Sergei Golubchik
8ace383fa0 fix meaningless code in mariadb_time_to_string()
which was apparently supposed to print microseconds
2024-10-17 10:00:31 +02:00
Kristian Nielsen
da0a01367c Implement boost::context as a fallback for non-blocking API support
The non-blocking API has native (assembler) implementations for x86_64,
i386, and (with recent patch) aarch64; these implementations are the most
efficient. For other architectures, a fallback to ucontext is supported.

But ucontext is not the most efficient, and it is not available on all
platforms (it has been deprecated in POSIX). The boost::context library
provides an alternative fallback that is available on more architectures and
should be more efficient than ucontext (if still not quite as fast as the
native support).

This patch adds a CMake option -DWITH_BOOST_CONTEXT=ON that adds
boost::context as a dependency of libmariadb to provide a fallback on
non-natively supported architectures. Boost::context is preferred over
ucontext when both are available.

The option is off by default and must be explicitly enabled by the
user. This avoids introducing a C++ dependency (including dependency
on a C++ compiler and on libstdc++) unless explicitly requested by the
user (libmariadb is otherwise C-only).

Tested-by: Brad Smith <brad@comstyle.com>
Signed-off-by: Kristian Nielsen <knielsen@knielsen-hq.org>
2024-09-27 15:33:43 +02:00
Kristian Nielsen
d2285fb830 MDEV-34859: Failed to initialise non-blocking API on OpenBSD arm64
Implement native my_context for arm64 (aarch64). This is more
efficient than ucontext, and also makes the non-blocking API available
on arm64 platforms that do not have ucontext such as OpenBSD.

Tested-by: Brad Smith <brad@comstyle.com>
Signed-off-by: Kristian Nielsen <knielsen@knielsen-hq.org>
2024-09-27 15:32:43 +02: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
39f2e12f9a TLS verification: Added support for envvar MARIADB_TLS_DISABLE_PEER_VERIFICATION
If the environment variable MARIADB_TLS_DISABLE_PEER_VERIFICATION is
set, we will skip verification of the peer certificate.
2024-09-21 17:47:44 +02:00
Georg Richter
b465742c38 Merge branch '3.3' into 3.4 2024-09-21 15:34:26 +02:00
Georg Richter
ae3854159a Merge branch '3.1' into 3.3 2024-09-21 15:33:49 +02:00
Georg Richter
9a40079349 Fix possible crash, if no default plugin was loaded. 2024-09-20 16:52:46 +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
e7b6adfbf9 Simplify tls_verification_callback 2024-09-10 07:19:12 +02:00
Georg Richter
b481c0a494 CONC-724: Added TLS verification callback support
For testing purposes (the python3 dummy server can't handle
further communication after TLS handshake succeeded) support
for verification callback was added.

my_bool callback(MYSQL *mysql, unsigned int *flags, my_bool verified)

Parameter:
  - mysql     connection handle for current connection
  - flags     verification flags
  - verified  true if callback was called after verification,
              otherwise false

Return value:
  - False (0) to continue
  - True  (1) to abort tls connection

The callback function can be registered via
mysql_optionsv(mysql, MARIADB_OPT_TLS_VERIFICATION_CALLBACK, callback);
2024-09-09 10:36: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
dd8df0c8ec Fix for CONC-719 and CONC-720
- Fixed parsec plugin declaration (CONC-719)
- When using gnutls crypto, also link against libnettle and
  libhogweed to avoid build error due to unresolved externals.
2024-08-28 15:04:15 +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
5386f1a3f2 Merge remote-tracking branch 'origin/3.3' into 3.4-tls 2024-06-25 11:57:27 +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
Georg Richter
486ce75d64 CONPY-704: parse_connection_string ignores empty string in last parameter
1) Fix check if end was reached (<= instead of <), so last parameter will
not be ignored in case it is an empty string.

2) Empty strings will be passed as NULL`in _mariadb_set_conf_option.
2024-06-11 16:00:22 +02:00
Sergei Golubchik
cc985fab30 fix the memory leak with gnutls (~11K per connection) 2024-05-26 15:45:54 +02:00