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

Implement TryFrom for a variety of types #139

Merged
merged 2 commits into from
Jan 12, 2021
Merged
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
42 changes: 22 additions & 20 deletions src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,22 @@
pub struct Time(u64);

impl Time {
/// Create a `webpki::Time` from a `std::time::SystemTime`.
/// Create a `webpki::Time` from a unix timestamp.
///
/// This will be replaced with a real `TryFrom<std::time::SystemTime>`
/// implementation when `TryFrom` is added to Rust Stable.
/// It is usually better to use the less error-prone
/// `webpki::Time::try_from(time: std::time::SystemTime)` instead when
/// `std::time::SystemTime` is available (when `#![no_std]` isn't being
/// used).
pub fn from_seconds_since_unix_epoch(secs: u64) -> Time {
Time(secs)
}
}

#[cfg(feature = "std")]
impl core::convert::TryFrom<std::time::SystemTime> for Time {
type Error = ring::error::Unspecified;
briansmith marked this conversation as resolved.
Show resolved Hide resolved

/// Create a `webpki::Time` from a `std::time::SystemTime`.
///
/// # Example:
///
Expand All @@ -36,28 +48,18 @@ impl Time {
/// # extern crate ring;
/// # extern crate webpki;
/// #
/// #[cfg(feature = "std")]
/// #![cfg(feature = "std")]
/// use std::{convert::TryFrom, time::SystemTime};
///
/// # fn foo() -> Result<(), ring::error::Unspecified> {
/// let time = webpki::Time::try_from(std::time::SystemTime::now())?;
/// let time = webpki::Time::try_from(SystemTime::now())?;
/// # Ok(())
/// # }
/// ```
///
/// Requires the `std` feature.
#[cfg(feature = "std")]
pub fn try_from(time: std::time::SystemTime) -> Result<Time, ring::error::Unspecified> {
time.duration_since(std::time::UNIX_EPOCH)
fn try_from(value: std::time::SystemTime) -> Result<Self, Self::Error> {
value
.duration_since(std::time::UNIX_EPOCH)
.map(|d| Time::from_seconds_since_unix_epoch(d.as_secs()))
.map_err(|_| ring::error::Unspecified)
}

/// Create a `webpki::Time` from a unix timestamp.
///
/// It is usually better to use the less error-prone
/// `webpki::Time::try_from(time: &std::time::SystemTime)` instead when
/// `std::time::SystemTime` is available (when `#![no_std]` isn't being
/// used).
pub fn from_seconds_since_unix_epoch(secs: u64) -> Time {
Time(secs)
}
}
8 changes: 6 additions & 2 deletions src/webpki.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,22 @@ pub struct EndEntityCert<'a> {
inner: cert::Cert<'a>,
}

impl<'a> EndEntityCert<'a> {
impl<'a> core::convert::TryFrom<&'a [u8]> for EndEntityCert<'a> {
type Error = Error;

/// Parse the ASN.1 DER-encoded X.509 encoding of the certificate
/// `cert_der`.
pub fn from(cert_der: &'a [u8]) -> Result<Self, Error> {
fn try_from(cert_der: &'a [u8]) -> Result<Self, Self::Error> {
Ok(Self {
inner: cert::parse_cert(
untrusted::Input::from(cert_der),
cert::EndEntityOrCA::EndEntity,
)?,
})
}
}

impl<'a> EndEntityCert<'a> {
/// Verifies that the end-entity certificate is valid for use by a TLS
/// server.
///
Expand Down
9 changes: 5 additions & 4 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

use core::convert::TryFrom;
extern crate webpki;

static ALL_SIGALGS: &[&webpki::SignatureAlgorithm] = &[
Expand All @@ -35,7 +36,7 @@ static ALL_SIGALGS: &[&webpki::SignatureAlgorithm] = &[
#[cfg(feature = "alloc")]
#[test]
pub fn netflix() {
let ee = include_bytes!("netflix/ee.der");
let ee: &[u8] = include_bytes!("netflix/ee.der");
let inter = include_bytes!("netflix/inter.der");
let ca = include_bytes!("netflix/ca.der");

Expand All @@ -45,7 +46,7 @@ pub fn netflix() {
#[allow(clippy::unreadable_literal)] // TODO: Make this clear.
let time = webpki::Time::from_seconds_since_unix_epoch(1492441716);

let cert = webpki::EndEntityCert::from(ee).unwrap();
let cert = webpki::EndEntityCert::try_from(ee).unwrap();
assert_eq!(
Ok(()),
cert.verify_is_valid_tls_server_cert(ALL_SIGALGS, &anchors, &[inter], time)
Expand All @@ -54,7 +55,7 @@ pub fn netflix() {

#[test]
pub fn ed25519() {
let ee = include_bytes!("ed25519/ee.der");
let ee: &[u8] = include_bytes!("ed25519/ee.der");
let ca = include_bytes!("ed25519/ca.der");

let anchors = vec![webpki::trust_anchor_util::cert_der_as_trust_anchor(ca).unwrap()];
Expand All @@ -63,7 +64,7 @@ pub fn ed25519() {
#[allow(clippy::unreadable_literal)] // TODO: Make this clear.
let time = webpki::Time::from_seconds_since_unix_epoch(1547363522);

let cert = webpki::EndEntityCert::from(ee).unwrap();
let cert = webpki::EndEntityCert::try_from(ee).unwrap();
assert_eq!(
Ok(()),
cert.verify_is_valid_tls_server_cert(ALL_SIGALGS, &anchors, &[], time)
Expand Down