Skip to content

Commit e020c8d

Browse files
authored
Add fips method to crypto provider (#14)
1 parent 6e79d9e commit e020c8d

File tree

17 files changed

+295
-243
lines changed

17 files changed

+295
-243
lines changed

.github/workflows/ci.yml

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ jobs:
2424
- name: Cache build artifacts
2525
uses: Swatinem/rust-cache@v2
2626
- name: cargo test
27-
run: cargo test --all-features
27+
run: cargo test
2828
# https://github.com/rust-lang/cargo/issues/6669
2929
- name: cargo test --doc
30-
run: cargo test --doc --all-features
30+
run: cargo test --doc
3131
lint:
3232
name: Lint
3333
runs-on: ubuntu-latest
@@ -44,4 +44,23 @@ jobs:
4444
- name: cargo fmt (check)
4545
run: cargo fmt -- --check -l
4646
- name: cargo clippy (warnings)
47-
run: cargo clippy --all-targets --all-features -- -D warnings
47+
run: cargo clippy --all-targets -- -D warnings
48+
49+
test-fips:
50+
name: Test using FIPS openssl
51+
runs-on: ubuntu-latest
52+
container:
53+
image: registry.access.redhat.com/ubi8/ubi:latest
54+
steps:
55+
- name: Install dependencies
56+
run: dnf install -y gcc openssl-devel openssl
57+
- name: Check out repository
58+
uses: actions/checkout@v4
59+
- name: Install toolchain
60+
uses: dtolnay/rust-toolchain@v1
61+
with:
62+
toolchain: stable
63+
- name: Cache build artifacts
64+
uses: Swatinem/rust-cache@v2
65+
- name: Run cargo test --features fips
66+
run: cargo test --features fips

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ rustls = { version = "0.23.0", default-features = false }
1717
rustls-webpki = { version = "0.102.2", default-features = false }
1818

1919
[features]
20-
default = ["tls12", "x25519"]
21-
x25519 = []
20+
default = ["tls12"]
21+
fips = []
2222
tls12 = ["rustls/tls12", "foreign-types-shared"]
2323

2424
[dev-dependencies]

README.md

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,7 @@
11
# rustls-openssl
22
A [rustls Crypto Provider](https://docs.rs/rustls/latest/rustls/crypto/struct.CryptoProvider.html) that uses OpenSSL for cryptographic operations.
33

4-
## Status
5-
**Early in development.**
6-
7-
## Usage
8-
The main entry points are the `rustls_openssl::default_provider` and `rustls_openssl::custom_provider` functions.
9-
See the [rustls documentation]((https://docs.rs/rustls/latest/rustls/crypto/struct.CryptoProvider.html)) for how to use them.
10-
11-
## Supported Ciphers
12-
13-
Supported cipher suites are listed below, in descending order of preference.
14-
If OpenSSL is compiled with the `OPENSSL_NO_CHACHA` option, the ChaCha20-Poly1305 ciphers will not be available.
15-
16-
### TLS 1.3
17-
18-
The following cipher suites are supported for TLS 1.3. These support QUIC.
19-
20-
```
21-
TLS13_AES_256_GCM_SHA384
22-
TLS13_AES_128_GCM_SHA256
23-
TLS13_CHACHA20_POLY1305_SHA256
24-
```
25-
26-
### TLS 1.2
27-
*Requires the `tls12` feature, which is a default feature.*
28-
29-
```
30-
TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384
31-
TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
32-
TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256
33-
TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
34-
TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
35-
TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256
36-
```
37-
## Supported Key Exchanges
38-
39-
Key exchanges, in descending order ofpreference:
40-
41-
```
42-
SECP384R1
43-
SECP256R1
44-
X25519 // Requires the `x25519` feature
45-
```
4+
[Documentation](https://docs.rs/rustls-openssl)
465

6+
## Status
7+
**In development.**

build.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const OPENSSL_NO_CHACHA: &str = "OPENSSL_NO_CHACHA";
66

77
fn main() {
88
println!("cargo:rustc-check-cfg=cfg(chacha)");
9+
println!("cargo:rustc-check-cfg=cfg(fips_module)");
910
// Determine whether to work around https://github.com/openssl/openssl/issues/23448
1011
// according to the OpenSSL version
1112
println!("cargo:rustc-check-cfg=cfg(bugged_add_hkdf_info)");
@@ -15,6 +16,10 @@ fn main() {
1516
if (0x3_00_00_00_0..0x3_04_00_00_0).contains(&version) {
1617
println!("cargo:rustc-cfg=bugged_add_hkdf_info");
1718
}
19+
20+
if version < 0x3_00_00_00_0 {
21+
println!("cargo:rustc-cfg=fips_module");
22+
}
1823
}
1924

2025
// Enable the `chacha` cfg if the `OPENSSL_NO_CHACHA` OpenSSL config is not set.

src/aead.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustls::Error;
77
pub(crate) enum Algorithm {
88
Aes128Gcm,
99
Aes256Gcm,
10-
#[cfg(chacha)]
10+
#[cfg(all(chacha, not(feature = "fips")))]
1111
ChaCha20Poly1305,
1212
}
1313

@@ -19,7 +19,7 @@ impl Algorithm {
1919
match self {
2020
Self::Aes128Gcm => Cipher::aes_128_gcm(),
2121
Self::Aes256Gcm => Cipher::aes_256_gcm(),
22-
#[cfg(chacha)]
22+
#[cfg(all(chacha, not(feature = "fips")))]
2323
Self::ChaCha20Poly1305 => Cipher::chacha20_poly1305(),
2424
}
2525
}

src/hash.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ impl rustls::crypto::hash::Hash for Algorithm {
6262
Algorithm::SHA384 => rustls::crypto::hash::HashAlgorithm::SHA384,
6363
}
6464
}
65+
66+
fn fips(&self) -> bool {
67+
crate::fips()
68+
}
6569
}
6670

6771
impl Context {

src/hkdf.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ impl RustlsHkdf for Hkdf {
7171
fn hmac_sign(&self, key: &OkmBlock, message: &[u8]) -> Tag {
7272
Hmac(self.0).with_key(key.as_ref()).sign(&[message])
7373
}
74+
75+
fn fips(&self) -> bool {
76+
crate::fips()
77+
}
7478
}
7579

7680
impl RustlsHkdfExpander for HkdfExpander {

src/hmac.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ impl rustls::crypto::hmac::Hmac for Hmac {
2121
fn hash_output_len(&self) -> usize {
2222
self.0.output_len()
2323
}
24+
25+
fn fips(&self) -> bool {
26+
crate::fips()
27+
}
2428
}
2529

2630
impl Key for HmacKey {

src/kx.rs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@ use openssl::derive::Deriver;
33
use openssl::ec::{EcGroup, EcKey, EcPoint, PointConversionForm};
44
use openssl::error::ErrorStack;
55
use openssl::nid::Nid;
6-
#[cfg(feature = "x25519")]
6+
#[cfg(not(feature = "fips"))]
77
use openssl::pkey::Id;
88
use openssl::pkey::{PKey, Private, Public};
99
use rustls::crypto::{ActiveKeyExchange, SharedSecret, SupportedKxGroup};
1010
use rustls::{Error, NamedGroup};
1111

12-
/// Supported `KeyExchange` groups.
13-
/// ```ignore
14-
/// SECP384R1
15-
/// SECP256R1
16-
/// X25519 // Enabled with the `x25519` feature
17-
/// ```
12+
/// [Supported KeyExchange groups](SupportedKxGroup).
13+
/// * [SECP384R1]
14+
/// * [SECP256R1]
15+
/// * [X25519]
16+
///
17+
/// If the `fips` feature is enabled, only [SECP384R1] and [SECP256R1] are available.
1818
pub const ALL_KX_GROUPS: &[&dyn SupportedKxGroup] = &[
1919
SECP256R1,
2020
SECP384R1,
21-
#[cfg(feature = "x25519")]
21+
#[cfg(not(feature = "fips"))]
2222
X25519,
2323
];
2424

@@ -36,26 +36,27 @@ struct EcKeyExchange {
3636
pub_key: Vec<u8>,
3737
}
3838

39-
#[cfg(feature = "x25519")]
39+
#[cfg(not(feature = "fips"))]
4040
/// KXGroup for X25519
4141
#[derive(Debug)]
4242
struct X25519KxGroup {}
4343

44-
#[cfg(feature = "x25519")]
44+
#[cfg(not(feature = "fips"))]
4545
#[derive(Debug)]
4646
struct X25519KeyExchange {
4747
private_key: PKey<Private>,
4848
public_key: Vec<u8>,
4949
}
5050

51-
#[cfg(feature = "x25519")]
51+
#[cfg(not(feature = "fips"))]
52+
/// X25519 key exchange group as registered with [IANA](https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8).
5253
pub const X25519: &dyn SupportedKxGroup = &X25519KxGroup {};
53-
54+
/// secp256r1 key exchange group as registered with [IANA](https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8)
5455
pub const SECP256R1: &dyn SupportedKxGroup = &EcKxGroup {
5556
name: NamedGroup::secp256r1,
5657
nid: Nid::X9_62_PRIME256V1,
5758
};
58-
59+
/// secp384r1 key exchange group as registered with [IANA](https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8)
5960
pub const SECP384R1: &dyn SupportedKxGroup = &EcKxGroup {
6061
name: NamedGroup::secp384r1,
6162
nid: Nid::SECP384R1,
@@ -85,6 +86,10 @@ impl SupportedKxGroup for EcKxGroup {
8586
fn name(&self) -> NamedGroup {
8687
self.name
8788
}
89+
90+
fn fips(&self) -> bool {
91+
crate::fips()
92+
}
8893
}
8994

9095
impl EcKeyExchange {
@@ -120,7 +125,7 @@ impl ActiveKeyExchange for EcKeyExchange {
120125
}
121126
}
122127

123-
#[cfg(feature = "x25519")]
128+
#[cfg(not(feature = "fips"))]
124129
impl SupportedKxGroup for X25519KxGroup {
125130
fn start(&self) -> Result<Box<dyn ActiveKeyExchange>, Error> {
126131
PKey::generate_x25519()
@@ -139,7 +144,7 @@ impl SupportedKxGroup for X25519KxGroup {
139144
}
140145
}
141146

142-
#[cfg(feature = "x25519")]
147+
#[cfg(not(feature = "fips"))]
143148
impl ActiveKeyExchange for X25519KeyExchange {
144149
fn complete(self: Box<Self>, peer_pub_key: &[u8]) -> Result<SharedSecret, Error> {
145150
PKey::public_key_from_raw_bytes(peer_pub_key, Id::X25519)

0 commit comments

Comments
 (0)