Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a single TLS implementation #308

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ async-trait = "0.1"
connection-string = "0.2"
num-traits = "0.2"
uuid = "1.0"
cfg-if = "1.0"

[target.'cfg(windows)'.dependencies]
winauth = { version = "0.0.4", optional = true }
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ A native Microsoft SQL Server (TDS) client for Rust.
| `sql-browser-smol` | SQL Browser implementation for the `TcpStream` of smol. | `disabled` |
| `integrated-auth-gssapi` | Support for using Integrated Auth via GSSAPI | `disabled` |

#### TLS feature flags

There are three TLS feature flags: `native-tls` which uses system libraries; `rustls` which is a pure rust solution; and `vendored-openssl` which uses a prepackaged binary. By default this library will use `native-tls`. Because of the way default features
currently work with cargo, if you select another TLS implementation, you will get that implementation *and* the `native-tls` implementation.

To avoid duplication of implementations, this library will use any specified TLS feature in preference of `native-tls`. This means that by default the library will use `native-tls`, but in the case of either `rustls` or `vendored-openssl` being supplied, those choices will take preference.

If you experience issues with TLS handshake, simply add `tiberius = {version = "*", features = ["rustls"]}` to your cargo file.

### Supported protocols

Tiberius does not rely on any protocol when connecting to an SQL Server instance. Instead the `Client` takes a socket that implements the `AsyncRead` and `AsyncWrite` traits from the [futures-rs](https://crates.io/crates/futures) crate.
Expand Down
75 changes: 35 additions & 40 deletions src/client/tls_stream.rs
Original file line number Diff line number Diff line change
@@ -1,44 +1,39 @@
use crate::Config;
use futures_util::io::{AsyncRead, AsyncWrite};

#[cfg(feature = "native-tls")]
mod native_tls_stream;

#[cfg(feature = "rustls")]
mod rustls_tls_stream;

#[cfg(feature = "vendored-openssl")]
mod opentls_tls_stream;

#[cfg(feature = "native-tls")]
pub(crate) use native_tls_stream::TlsStream;

#[cfg(feature = "rustls")]
pub(crate) use rustls_tls_stream::TlsStream;

#[cfg(feature = "vendored-openssl")]
pub(crate) use opentls_tls_stream::TlsStream;

#[cfg(feature = "rustls")]
pub(crate) async fn create_tls_stream<S: AsyncRead + AsyncWrite + Unpin + Send>(
config: &Config,
stream: S,
) -> crate::Result<TlsStream<S>> {
TlsStream::new(config, stream).await
}

#[cfg(feature = "native-tls")]
pub(crate) async fn create_tls_stream<S: AsyncRead + AsyncWrite + Unpin + Send>(
config: &Config,
stream: S,
) -> crate::Result<TlsStream<S>> {
native_tls_stream::create_tls_stream(config, stream).await
}

#[cfg(feature = "vendored-openssl")]
pub(crate) async fn create_tls_stream<S: AsyncRead + AsyncWrite + Unpin + Send>(
config: &Config,
stream: S,
) -> crate::Result<TlsStream<S>> {
opentls_tls_stream::create_tls_stream(config, stream).await
cfg_if::cfg_if! {
if #[cfg(feature = "rustls")] {
mod rustls_tls_stream;

pub(crate) use rustls_tls_stream::TlsStream;

pub(crate) async fn create_tls_stream<S: AsyncRead + AsyncWrite + Unpin + Send>(
config: &Config,
stream: S,
) -> crate::Result<TlsStream<S>> {
TlsStream::new(config, stream).await
}
} else if #[cfg(feature = "vendored-openssl")] {
mod opentls_tls_stream;

pub(crate) use opentls_tls_stream::TlsStream;

pub(crate) async fn create_tls_stream<S: AsyncRead + AsyncWrite + Unpin + Send>(
config: &Config,
stream: S,
) -> crate::Result<TlsStream<S>> {
opentls_tls_stream::create_tls_stream(config, stream).await
}
} else if #[cfg(feature = "native-tls")] {
mod native_tls_stream;

pub(crate) use native_tls_stream::TlsStream;

pub(crate) async fn create_tls_stream<S: AsyncRead + AsyncWrite + Unpin + Send>(
config: &Config,
stream: S,
) -> crate::Result<TlsStream<S>> {
native_tls_stream::create_tls_stream(config, stream).await
}
}
}
4 changes: 2 additions & 2 deletions src/tds/time/chrono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ from_sql!(
let offset = chrono::Duration::minutes(dto.offset as i64);
let naive = NaiveDateTime::new(date, time).sub(offset);

chrono::DateTime::from_utc(naive, Utc)
chrono::DateTime::from_naive_utc_and_offset(naive, Utc)
});
chrono::DateTime<FixedOffset>: ColumnData::DateTimeOffset(ref dto) => dto.map(|dto| {
let date = from_days(dto.datetime2.date.days() as i64, 1);
Expand All @@ -91,7 +91,7 @@ from_sql!(
let offset = FixedOffset::east_opt((dto.offset as i32) * 60).unwrap();
let naive = NaiveDateTime::new(date, time);

chrono::DateTime::from_utc(naive, offset)
chrono::DateTime::from_naive_utc_and_offset(naive, offset)
})
);

Expand Down
6 changes: 3 additions & 3 deletions tests/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2237,7 +2237,7 @@ where
.unwrap()
.and_hms_opt(16, 20, 0)
.unwrap();
let dt: DateTime<Utc> = DateTime::from_utc(naive, Utc);
let dt: DateTime<Utc> = DateTime::from_naive_utc_and_offset(naive, Utc);

let row = conn
.query("SELECT @P1", &[&dt])
Expand Down Expand Up @@ -2276,7 +2276,7 @@ where
.unwrap();

let fixed = FixedOffset::east_opt(3600 * 3).unwrap();
let dt: DateTime<FixedOffset> = DateTime::from_utc(naive, fixed);
let dt: DateTime<FixedOffset> = DateTime::from_naive_utc_and_offset(naive, fixed);

let row = conn
.query("SELECT @P1", &[&dt])
Expand Down Expand Up @@ -2314,7 +2314,7 @@ where
.and_hms_opt(16, 20, 0)
.unwrap();
let fixed = FixedOffset::east_opt(3600 * 3).unwrap();
let dt: DateTime<FixedOffset> = DateTime::from_utc(naive, fixed);
let dt: DateTime<FixedOffset> = DateTime::from_naive_utc_and_offset(naive, fixed);

let row = conn
.query(format!("SELECT CAST('{}' AS datetimeoffset(7))", dt), &[])
Expand Down