Skip to content

Commit

Permalink
Loading, storing, and generating DNSSEC keys (#406)
Browse files Browse the repository at this point in the history
* [sign] Define 'KeyPair' and impl key export

A private key converted into a 'KeyPair' can be exported in the
conventional DNS format.  This is an important step in implementing
'ldns-keygen' using 'domain'.  It is up to the implementation modules
to provide conversion to and from 'KeyPair'; some impls (e.g. for HSMs)
won't support it at all.

* [sign] Define trait 'Sign'

'Sign' is a more generic version of 'sign::key::SigningKey' that does
not provide public key information.  It does not try to abstract over
all the functionality of a keypair, since that can depend on the
underlying cryptographic implementation.

* [sign] Implement parsing from the DNS format

There are probably lots of bugs in this implementation, I'll add some
tests soon.

* [sign] Provide some error information

Also fixes 'cargo clippy' issues, particularly with the MSRV.

* [sign] Move 'KeyPair' to 'generic::SecretKey'

I'm going to add a corresponding 'PublicKey' type, at which point it
becomes important to differentiate from the generic representations and
actual cryptographic implementations.

* [sign/generic] Add 'PublicKey'

* [sign] Rewrite the 'ring' module to use the 'Sign' trait

Key generation, for now, will only be provided by the OpenSSL backend
(coming soon).  However, generic keys (for RSA/SHA-256 or Ed25519) can
be imported into the Ring backend and used freely.

* Implement DNSSEC signing with OpenSSL

The OpenSSL backend supports import from and export to generic secret
keys, making the formatting and parsing machinery for them usable.  The
next step is to implement generation of keys.

* [sign/openssl] Implement key generation

* [sign/openssl] Test key generation and import/export

* [sign/openssl] Add support for ECDSA

* [sign/openssl] satisfy clippy

* [sign/openssl] Implement the 'Sign' trait

* Install OpenSSL in CI builds

* Ensure 'openssl' dep supports 3.x.x

* [workflows/ci] Use 'vcpkg' instead of vendoring OpenSSL

* Ensure 'openssl' dep exposes necessary interfaces

* [workflows/ci] Record location of 'vcpkg'

* [workflows/ci] Use a YAML def for 'VCPKG_ROOT'

* [workflows/ci] Fix a vcpkg triplet to use

* Upgrade openssl to 0.10.57 for bitflags 2.x

* [workflows/ci] Use dynamic linking for vcpkg openssl

* [workflows/ci] Correctly annotate 'vcpkg'

* [sign/openssl] Implement exporting public keys

* [sign/ring] Implement exporting public keys

* [sign/generic] Test (de)serialization for generic secret keys

There were bugs in the Base64 encoding/decoding that are not worth
trying to debug; there's a perfectly usable Base64 implementation in
the crate already.

* [sign] Thoroughly test import/export in both backends

I had to swap out the RSA key since 'ring' found it to be too small.

* [sign] Remove debugging code and satisfy clippy

* [sign] Account for CR LF in tests

* [sign/openssl] Fix bugs in the signing procedure

- RSA signatures were being made with an unspecified padding scheme.
- ECDSA signatures were being output in ASN.1 DER format, instead of
  the fixed-size format required by DNSSEC (and output by 'ring').
- Tests for signature failures are now added for both backends.

* Refactor the 'sign' module

Most functions have been renamed.  The public key types have been moved
to the 'validate' module (which 'sign' now depends on), and they have
been outfitted with conversions (e.g. to and from DNSKEY records).

Importing a generic key into an OpenSSL or Ring key now requires the
public key to also be available.  In both implementations, the pair are
checked for consistency -- this ensures that both are uncorrupted and
that keys have not been mixed up.  This also allows the Ring backend to
support ECDSA keys (although key generation is still difficult).

The 'PublicKey' and 'PrivateKey' enums now store their array data in
'Box'.  This has two benefits: it is easier to securely manage memory
on the heap (since the compiler will not copy it around the stack); and
the smaller sizes of the types is beneficial (although negligibly) to
performance.

* Move 'sign' and 'validate' to unstable feature gates

* [workflows/ci] Document the vcpkg env vars

* Rename public/secret key interfaces to '*Raw*'

This makes space for higher-level interfaces which track DNSKEY flags
information (and possibly key rollover information).

* [sign/ring] Store the RNG in an 'Arc'

* [validate] Enhance 'Signature' API

* [validate] Add high-level 'Key' type

* [sign/openssl] Pad ECDSA keys when exporting

Tests would spuriously fail when generated keys were only 31 bytes in
size.

* [validate] Implement 'Key::key_tag()'

This is more efficient than allocating a DNSKEY record and computing
the key tag there.

* [validate] Correct bit offsets for flags

* [validate] Implement support for digests

The test keys have been rotated and replaced with KSKs since they have
associated DS records I can verify digests against.  I also expanded
Ring's testing to include ECDSA keys.  The validate module tests SHA-1
keys as well, which aren't supported by 'sign'.

* [validate] Enhance BIND format conversion for 'Key'

Public keys in the BIND format can now have multiple lines (even with
comments).  Keys can also be directly written into the BIND format and
round-trips to and from the BIND format are now tested.

* [sign] Introduce 'SigningKey'

* [sign] Handle errors more responsibly

The 'openssl' and 'ring' modules should now follow the contributing
guidelines regarding module layout and formatting.

* [sign] correct doc link

* [sign/openssl] Replace panics with results

* remove 'sign/key'

* [sign] Introduce 'common' for abstracting backends

This is useful for abstracting over OpenSSL and Ring, so that Ring can
be used whenever possible while OpenSSL is used as a fallback.  This is
useful for clients that just wish to support everything.

* [sign/generic] add top-level doc comment

* [validate] debug bind format errors

* [validate] more debug statements

* [validate] format DNSKEYs using 'ZonefileFmt'

The 'Dnskey' impl of 'fmt::Display' was no longer accurate to the zone
file format because 'SecAlg' now prints '<code>(<mnemonic>)'.

* Reorganize crate features in 'Cargo.toml'

* [sign] Add key generation support for Ring

It's a bit hacky because it relies on specific byte indices within the
generated PKCS8 documents (internally, Ring basically just concatenates
bytes to form the documents, and we use the same indices).  However,
any change to the document format should be caught by the tests here.

* [sign] Make OpenSSL support optional

Now that Ring and OpenSSL support all mandatory algorithms, OpenSSL is
no longer required in order to provide signing functionality.

* [sign] Rename 'generic::SecretKey' to 'KeyBytes'

* [sign] Rename 'SecretKey' to 'KeyPair' in all impls

* [sign] Rename 'KeyBytes' to 'SecretKeyBytes'

For consistency with the upcoming 'PublicKeyBytes'.

* [validate] Rename 'RawPublicKey' to 'PublicKeyBytes'

* [sign/ring] Remove redundant imports

* [sign,validate] Add 'display_as_bind()' to key bytes types

* [sign,validate] remove unused imports

* [sign] Document everything

* [lib] Rewrite feature flag documentation

* [workflows/ci] Use 'apt-get' instead of 'apt'

* [sign] Clarify documentation as per @ximon18

* [sign] Use 'secrecy' to protect private keys

* [sign] Improve documentation and examples

---------

Co-authored-by: arya dradjica <arya@nlnetlabs.nl>
  • Loading branch information
bal-e and arya dradjica authored Nov 5, 2024
1 parent 24ecebe commit 8e8d616
Show file tree
Hide file tree
Showing 32 changed files with 3,313 additions and 257 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,28 @@ jobs:
rust: [1.76.0, stable, beta, nightly]
env:
RUSTFLAGS: "-D warnings"
# We use 'vcpkg' to install OpenSSL on Windows.
VCPKG_ROOT: "${{ github.workspace }}\\vcpkg"
VCPKGRS_TRIPLET: x64-windows-release
# Ensure that OpenSSL is dynamically linked.
VCPKGRS_DYNAMIC: 1
steps:
- name: Checkout repository
uses: actions/checkout@v1
- name: Install Rust
uses: hecrj/setup-rust-action@v2
with:
rust-version: ${{ matrix.rust }}
- if: matrix.os == 'ubuntu-latest'
run: sudo apt-get install -y libssl-dev
- if: matrix.os == 'windows-latest'
id: vcpkg
uses: johnwason/vcpkg-action@v6
with:
pkgs: openssl
triplet: ${{ env.VCPKGRS_TRIPLET }}
token: ${{ github.token }}
github-binarycache: true
- if: matrix.rust == 'stable'
run: rustup component add clippy
- if: matrix.rust == 'stable'
Expand All @@ -37,6 +52,8 @@ jobs:
uses: hecrj/setup-rust-action@v2
with:
rust-version: "1.68.2"
- name: Install OpenSSL
run: sudo apt-get install -y libssl-dev
- name: Install nightly Rust
run: rustup install nightly
- name: Check with minimal-versions
Expand Down
76 changes: 76 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 15 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ heapless = { version = "0.8", optional = true }
libc = { version = "0.2.153", default-features = false, optional = true } # 0.2.79 is the first version that has IP_PMTUDISC_OMIT
parking_lot = { version = "0.12", optional = true }
moka = { version = "0.12.3", optional = true, features = ["future"] }
openssl = { version = "0.10.57", optional = true } # 0.10.57 upgrades to 'bitflags' 2.x
proc-macro2 = { version = "1.0.69", optional = true } # Force proc-macro2 to at least 1.0.69 for minimal-version build
ring = { version = "0.17", optional = true }
rustversion = { version = "1", optional = true }
secrecy = { version = "0.10", optional = true }
serde = { version = "1.0.130", optional = true, features = ["derive"] }
siphasher = { version = "1", optional = true }
smallvec = { version = "1.3", optional = true }
Expand All @@ -47,24 +49,32 @@ tracing-subscriber = { version = "0.3.18", optional = true, features = ["env-fil

[features]
default = ["std", "rand"]

# Support for libraries
bytes = ["dep:bytes", "octseq/bytes"]
heapless = ["dep:heapless", "octseq/heapless"]
resolv = ["net", "smallvec", "unstable-client-transport"]
resolv-sync = ["resolv", "tokio/rt"]
serde = ["dep:serde", "octseq/serde"]
sign = ["std"]
smallvec = ["dep:smallvec", "octseq/smallvec"]
std = ["dep:hashbrown", "bytes?/std", "octseq/std", "time/std"]

# Cryptographic backends
ring = ["dep:ring"]
openssl = ["dep:openssl"]

# Crate features
net = ["bytes", "futures-util", "rand", "std", "tokio"]
resolv = ["net", "smallvec", "unstable-client-transport"]
resolv-sync = ["resolv", "tokio/rt"]
tsig = ["bytes", "ring", "smallvec"]
validate = ["bytes", "std", "ring"]
zonefile = ["bytes", "serde", "std"]

# Unstable features
unstable-client-transport = ["moka", "net", "tracing"]
unstable-server-transport = ["arc-swap", "chrono/clock", "libc", "net", "siphasher", "tracing"]
unstable-sign = ["std", "dep:secrecy", "unstable-validate"]
unstable-stelline = ["tokio/test-util", "tracing", "tracing-subscriber", "tsig", "unstable-client-transport", "unstable-server-transport", "zonefile"]
unstable-validator = ["validate", "zonefile", "unstable-client-transport"]
unstable-validate = ["bytes", "std", "ring"]
unstable-validator = ["unstable-validate", "zonefile", "unstable-client-transport"]
unstable-xfr = ["net"]
unstable-zonetree = ["futures-util", "parking_lot", "rustversion", "serde", "std", "tokio", "tracing", "unstable-xfr", "zonefile"]

Expand Down
102 changes: 65 additions & 37 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@
#![cfg_attr(not(feature = "resolv"), doc = "* resolv:")]
//! An asynchronous DNS resolver based on the
//! [Tokio](https://tokio.rs/) async runtime.
#![cfg_attr(feature = "sign", doc = "* [sign]:")]
#![cfg_attr(not(feature = "sign"), doc = "* sign:")]
#![cfg_attr(feature = "unstable-sign", doc = "* [sign]:")]
#![cfg_attr(not(feature = "unstable-sign"), doc = "* sign:")]
//! Experimental support for DNSSEC signing.
#![cfg_attr(feature = "tsig", doc = "* [tsig]:")]
#![cfg_attr(not(feature = "tsig"), doc = "* tsig:")]
//! Support for securing DNS transactions with TSIG records.
#![cfg_attr(feature = "validate", doc = "* [validate]:")]
#![cfg_attr(not(feature = "validate"), doc = "* validate:")]
#![cfg_attr(feature = "unstable-validate", doc = "* [validate]:")]
#![cfg_attr(not(feature = "unstable-validate"), doc = "* validate:")]
//! Experimental support for DNSSEC validation.
#![cfg_attr(feature = "unstable-validator", doc = "* [validator]:")]
#![cfg_attr(not(feature = "unstable-validator"), doc = "* validator:")]
Expand All @@ -61,61 +61,79 @@
//!
//! # Reference of feature flags
//!
//! The following is the complete list of the feature flags with the
//! exception of unstable features which are described below.
//! Several feature flags simply enable support for other crates, e.g. by
//! adding `impl`s for their types. They are optional and do not introduce
//! new functionality into this crate.
//!
//! * `bytes`: Enables using the types `Bytes` and `BytesMut` from the
//! [bytes](https://github.com/tokio-rs/bytes) crate as octet sequences.
//! * `chrono`: Adds the [chrono](https://github.com/chronotope/chrono)
//! crate as a dependency. This adds support for generating serial numbers
//! from time stamps.
//!
//! * `heapless`: enables the use of the `Vec` type from the
//! [heapless](https://github.com/japaric/heapless) crate as octet
//! sequences.
//! * `interop`: Activate interoperability tests that rely on other software
//! to be installed in the system (currently NSD and dig) and will fail if
//! it isn’t. This feature is not meaningful for users of the crate.
//!
//! * `smallvec`: enables the use of the `Smallvec` type from the
//! [smallvec](https://github.com/servo/rust-smallvec) crate as octet
//! sequences.
//!
//! Some flags enable support for specific kinds of operations that are not
//! otherwise possible. They are gated as they may not always be necessary
//! and they may introduce new dependencies.
//!
//! * `chrono`: Adds the [chrono](https://github.com/chronotope/chrono)
//! crate as a dependency. This adds support for generating serial numbers
//! from time stamps.
//!
//! * `rand`: Enables a number of methods that rely on a random number
//! generator being available in the system.
//! * `resolv`: Enables the asynchronous stub resolver via the
#![cfg_attr(feature = "resolv", doc = " [resolv]")]
#![cfg_attr(not(feature = "resolv"), doc = " resolv")]
//! module.
//! * `resolv-sync`: Enables the synchronous version of the stub resolver.
//! * `ring`: Enables crypto functionality via the
//! [ring](https://github.com/briansmith/ring) crate.
//!
//! * `serde`: Enables serde serialization for a number of basic types.
//! * `sign`: basic DNSSEC signing support. This will enable the
#![cfg_attr(feature = "sign", doc = " [sign]")]
#![cfg_attr(not(feature = "sign"), doc = " sign")]
//! module and requires the `std` feature. Note that this will not directly
//! enable actual signing. For that you will also need to pick a crypto
//! module via an additional feature. Currently we only support the `ring`
//! module, but support for OpenSSL is coming soon.
//!
//! * `siphasher`: enables the dependency on the
//! [siphasher](https://github.com/jedisct1/rust-siphash) crate which allows
//! generating and checking hashes in [standard server
//! cookies][crate::base::opt::cookie::StandardServerCookie].
//! * `smallvec`: enables the use of the `Smallvec` type from the
//! [smallvec](https://github.com/servo/rust-smallvec) crate as octet
//! sequences.
//!
//! * `std`: support for the Rust std library. This feature is enabled by
//! default.
//!
//! A special case here is cryptographic backends. Certain modules (e.g. for
//! DNSSEC signing and validation) require a backend to provide cryptography.
//! At least one such module should be enabled.
//!
//! * `openssl`: Enables crypto functionality via OpenSSL through the
//! [rust-openssl](https://github.com/sfackler/rust-openssl) crate.
//!
//! * `ring`: Enables crypto functionality via the
//! [ring](https://github.com/briansmith/ring) crate.
//!
//! Some flags represent entire categories of functionality within this crate.
//! Each flag is associated with a particular module. Note that some of these
//! modules are under heavy development, and so have unstable feature flags
//! which are categorized separately.
//!
//! * `net`: Enables sending and receiving DNS messages via the
#![cfg_attr(feature = "net", doc = " [net]")]
#![cfg_attr(not(feature = "net"), doc = " net")]
//! module.
//!
//! * `resolv`: Enables the asynchronous stub resolver via the
#![cfg_attr(feature = "resolv", doc = " [resolv]")]
#![cfg_attr(not(feature = "resolv"), doc = " resolv")]
//! module.
//!
//! * `resolv-sync`: Enables the synchronous version of the stub resolver.
//!
//! * `tsig`: support for signing and validating message exchanges via TSIG
//! signatures. This enables the
#![cfg_attr(feature = "tsig", doc = " [tsig]")]
#![cfg_attr(not(feature = "tsig"), doc = " tsig")]
//! module and currently pulls in the
//! `bytes`, `ring`, and `smallvec` features.
//! * `validate`: basic DNSSEC validation support. This feature enables the
#![cfg_attr(feature = "validate", doc = " [validate]")]
#![cfg_attr(not(feature = "validate"), doc = " validate")]
//! module and currently also enables the `std` and `ring`
//! features.
//! module and currently enables `bytes`, `ring`, and `smallvec`.
//!
//! * `zonefile`: reading and writing of zonefiles. This feature enables the
#![cfg_attr(feature = "zonefile", doc = " [zonefile]")]
#![cfg_attr(not(feature = "zonefile"), doc = " zonefile")]
//! module and currently also enables the `bytes` and `std` features.
//! module and currently also enables `bytes`, `serde`, and `std`.
//!
//! # Unstable features
//!
Expand All @@ -137,6 +155,16 @@
//! a client perspective; primarily the `net::client` module.
//! * `unstable-server-transport`: receiving and sending DNS messages from
//! a server perspective; primarily the `net::server` module.
//! * `unstable-sign`: basic DNSSEC signing support. This will enable the
#![cfg_attr(feature = "unstable-sign", doc = " [sign]")]
#![cfg_attr(not(feature = "unstable-sign"), doc = " sign")]
//! module and requires the `std` feature. In order to actually perform any
//! signing, also enable one or more cryptographic backend modules (`ring`
//! and `openssl`).
//! * `unstable-validate`: basic DNSSEC validation support. This enables the
#![cfg_attr(feature = "unstable-validate", doc = " [validate]")]
#![cfg_attr(not(feature = "unstable-validate"), doc = " validate")]
//! module and currently also enables the `std` and `ring` features.
//! * `unstable-validator`: a DNSSEC validator, primarily the `validator`
//! and the `net::client::validator` modules.
//! * `unstable-xfr`: zone transfer related functionality..
Expand Down
Loading

0 comments on commit 8e8d616

Please sign in to comment.