- Rust 90.1%
- Shell 5.1%
- Python 3.8%
- Makefile 1%
|
|
||
|---|---|---|
| .github/workflows | ||
| contrib | ||
| docs | ||
| examples/encrypt-demo | ||
| native-ossl | ||
| native-ossl-sys | ||
| ring-native-ossl | ||
| rustls-native-ossl | ||
| wiki | ||
| .gitignore | ||
| Cargo.lock | ||
| Cargo.toml | ||
| CHANGELOG.md | ||
| LICENSE | ||
| README.md | ||
native-ossl
Safe, idiomatic Rust wrappers for modern OpenSSL (3.0.7+, 4.x).
native-ossl gives Rust code direct access to the OpenSSL cryptographic
library already installed on the system — no reimplementation, no vendored
C code by default, no protocol logic added on top. The API covers the full
public OpenSSL stack: EVP algorithms, asymmetric keys, TLS, X.509, OCSP,
PKCS#12, BIO, and provider/library-context management.
What is provided
| Module | Types |
|---|---|
error |
ErrorStack — full OpenSSL error queue on failure |
lib_ctx |
LibCtx, Provider — isolated library contexts |
params |
ParamBuilder, Params — typed OSSL_PARAM arrays with set() method for compile-time type safety |
typed_params |
Algorithm-scoped typed parameter constants (rsa::N, ec::GROUP, sig::PAD_MODE, etc.) |
pkey_builder |
Per-algorithm key import builders (RsaPrivKeyBuilder, EcPrivKeyBuilder, RawKeyBuilder, DhPrivKeyBuilder) |
pkey_params |
OpenSSL parameter name constants and key-type identifiers |
digest |
DigestAlg, DigestCtx — including mid-stream context fork and checkpoint serialization |
cipher |
CipherAlg, CipherCtx<Dir>, AeadEncryptCtx, AeadDecryptCtx |
mac |
MacAlg, MacCtx, HmacCtx, CmacCtx |
rand |
Rand, RandAlg, RandCtx |
pkey |
Pkey<T>, KeygenCtx, Signer, Verifier, RawSigner, RawVerifier, MessageSigner, MessageVerifier, DeriveCtx, PkeyEncryptCtx, PkeyDecryptCtx, EncapCtx, DecapCtx |
kdf |
HkdfBuilder, Pbkdf2Builder, ScryptBuilder, SshkdfBuilder, KbkdfBuilder |
x509 |
X509, X509Builder, X509Store, X509StoreCtx, X509Crl, SignatureInfo |
pkcs12 |
Pkcs12 |
ocsp |
OcspCertId, OcspRequest, OcspResponse, OcspBasicResp |
ssl |
SslCtx, SslCtxBuilder<R>, Ssl, SslSession, BorrowedSslSession, HostnameFlags |
fips |
fips::is_running |
util |
SecretBuf — OPENSSL_cleanse-on-drop buffer |
Requirements
- OpenSSL 3.0.7 or later (system install; 3.2+ for KEM/message-sign APIs, 3.5+ for SSH-KDF/KBKDF)
- Rust 1.77 or later (required for
c"..."C-string literal syntax) pkg-config
# Fedora / RHEL
sudo dnf install openssl-devel
# Ubuntu / Debian
sudo apt install libssl-dev pkg-config
# macOS (Homebrew)
brew install openssl@3
export PKG_CONFIG_PATH="$(brew --prefix openssl@3)/lib/pkgconfig"
Usage
[dependencies]
native-ossl = { version = "0.2" }
SHA-256
use native_ossl::digest::DigestAlg;
let sha256 = DigestAlg::fetch(c"SHA2-256", None)?;
let mut ctx = sha256.new_context()?;
ctx.update(b"hello world")?;
let mut out = [0u8; 32];
ctx.finish(&mut out)?;
AES-256-GCM
use native_ossl::{cipher::{CipherAlg, AeadEncryptCtx, AeadDecryptCtx}, rand::Rand};
let alg = CipherAlg::fetch(c"AES-256-GCM", None)?;
let mut key = [0u8; 32];
let mut nonce = [0u8; 12];
Rand::fill(&mut key)?;
Rand::fill(&mut nonce)?;
let mut enc = AeadEncryptCtx::new(&alg, &key, &nonce, None)?;
let mut ct = vec![0u8; plaintext.len()];
enc.update(plaintext, &mut ct)?;
enc.finalize(&mut ct[n..])?;
let mut tag = [0u8; 16];
enc.tag(&mut tag)?;
HKDF
use native_ossl::{digest::DigestAlg, kdf::HkdfBuilder};
let sha256 = DigestAlg::fetch(c"SHA2-256", None)?;
let mut okm = [0u8; 32];
HkdfBuilder::new(&sha256)
.key(ikm)
.salt(salt)
.info(info)
.derive(&mut okm)?;
Ed25519 sign / verify
use native_ossl::pkey::KeygenCtx;
let key = KeygenCtx::new(c"ED25519")?.generate()?;
let sig = key.sign_oneshot(message)?;
key.verify_oneshot(&sig, message)?;
TLS client (fluent builder)
use native_ossl::ssl::{SslCtxBuilder, Client, TlsVersion, HostnameFlags};
let ctx = SslCtxBuilder::<Client>::new()?
.min_proto_version(TlsVersion::Tls12)?
.default_ca_paths()?
.verify_peer()
.verify_hostname("example.com")?
.verify_hostname_flags(HostnameFlags::NO_PARTIAL_WILDCARDS)?
.build()?;
let mut ssl = ctx.new_ssl()?;
ssl.set_connect_state();
ssl.connect()?;
Inspect certificate signature algorithm
use native_ossl::x509::X509;
let cert = X509::from_pem(&std::fs::read("cert.pem")?)?;
let info = cert.signature_info()?;
// info.md_nid, info.pk_nid, info.security_bits
println!("pk_nid={} md_nid={} bits={}", info.pk_nid, info.md_nid, info.security_bits);
Cargo features
| Feature | Default | Effect |
|---|---|---|
dynamic |
yes | Link against the system OpenSSL via pkg-config |
vendored |
no | Build a specific OpenSSL source tree (set NATIVE_OSSL_OPENSSL_SOURCES) |
fips |
no | Link libfips.a instead of libcrypto.a |
fips-provider |
no | Enable non-public provider-internal bindings (OPENSSL_SOURCE_DIR required) |
Version-gated APIs
The build system detects the installed OpenSSL version automatically and
activates the appropriate #[cfg(...)] gates. No manual flags are needed.
| cfg flag | Minimum | APIs |
|---|---|---|
ossl320 |
3.2.0 | MessageSigner, MessageVerifier, EncapCtx, DecapCtx, GlobalRandCtx |
ossl350 |
3.5.0 | SshkdfBuilder, KbkdfBuilder |
ossl_v400 |
4.0.0 | DigestCtx::serialize / deserialize |
ossl_slhdsa |
— | SLH-DSA key operations |
ossl_mldsa |
— | ML-DSA key operations |
ossl_mlkem |
— | ML-KEM encapsulate / decapsulate |
Algorithm flags (ossl_slhdsa, ossl_mldsa, ossl_mlkem) are detected from
header macros rather than the version number, so they track distribution backports
correctly.
Building and testing
cargo build
cargo test --all
cargo clippy --all-targets -- -D warnings
See docs/src/dev/building.md for FIPS mode,
fips-provider, vendored builds, and the full CI checklist.
Documentation
cargo install mdbook
cd docs && mdbook build --open
Or browse the source under docs/src/.
License
Apache-2.0. See LICENSE.