Skip to content

Commit

Permalink
Merge 36f3ba7 into 48abe33
Browse files Browse the repository at this point in the history
  • Loading branch information
jondot committed May 28, 2019
2 parents 48abe33 + 36f3ba7 commit f37ec83
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 28 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Expand Up @@ -12,11 +12,11 @@ readme = "README.md"
chrono = {version = "0.4.2", features = ['serde']}
failure = "0.1.1"
log = "0.4.2"
ring = "0.12.1"
ring = "*"
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
untrusted = "0.5.1"
untrusted = "*"

[[bin]]
name = "gen_license"
Expand Down
19 changes: 13 additions & 6 deletions bin/gen_keys.rs
@@ -1,6 +1,7 @@
extern crate ring;
extern crate untrusted;

use ring::signature::KeyPair;
use std::env;
use std::fs;
use std::process::exit;
Expand All @@ -20,16 +21,22 @@ fn main() -> Result<(), String> {
};
// Generate a key pair in PKCS#8 (v2) format.
let rng = rand::SystemRandom::new();
let pkcs8_bytes = signature::Ed25519KeyPair::generate_pkcs8(&rng).map_err(|e| format!("Error: {:?}", e))?;
let pkcs8_bytes =
signature::Ed25519KeyPair::generate_pkcs8(&rng).map_err(|e| format!("Error: {:?}", e))?;

// Normally the application would store the PKCS#8 file persistently. Later
// it would read the PKCS#8 file from persistent storage to use it.

let key_pair =
signature::Ed25519KeyPair::from_pkcs8(
untrusted::Input::from(&pkcs8_bytes)).map_err(|e| format!("Error: {:?}", e))?;
fs::write(&format!("{}/private.pks", key_path), pkcs8_bytes.as_ref()).map_err(|e| format!("FS Error: {:?}", e))?;
signature::Ed25519KeyPair::from_pkcs8(untrusted::Input::from(pkcs8_bytes.as_ref()))
.map_err(|e| format!("Error: {:?}", e))?;
fs::write(&format!("{}/private.pks", key_path), pkcs8_bytes.as_ref())
.map_err(|e| format!("FS Error: {:?}", e))?;

fs::write(&format!("{}/public.pks", key_path), &key_pair.public_key_bytes()).map_err(|e| format!("FS Error: {:?}", e))?;
fs::write(
&format!("{}/public.pks", key_path),
&key_pair.public_key().as_ref(),
)
.map_err(|e| format!("FS Error: {:?}", e))?;
Ok(())
}
}
56 changes: 36 additions & 20 deletions src/lib.rs
@@ -1,10 +1,13 @@
extern crate chrono;
#[macro_use] extern crate failure;
#[macro_use] extern crate log;
#[macro_use]
extern crate failure;
#[macro_use]
extern crate log;
extern crate serde;
#[macro_use] extern crate serde_derive;
extern crate serde_json;
#[macro_use]
extern crate serde_derive;
extern crate ring;
extern crate serde_json;
extern crate untrusted;

use std::str;
Expand All @@ -21,7 +24,10 @@ mod tests {
let license = License::new(b"test").with_public_key(b"a").build();
match license {
Ok(_) => unreachable!(),
Err(e) => assert_eq!(e.downcast::<LicenseError>().unwrap(), LicenseError::MissingSignature)
Err(e) => assert_eq!(
e.downcast::<LicenseError>().unwrap(),
LicenseError::MissingSignature
),
}
}

Expand All @@ -30,16 +36,24 @@ mod tests {
let license = License::new(b"test").build();
match license {
Ok(_) => unreachable!(),
Err(e) => assert_eq!(e.downcast::<LicenseError>().unwrap(), LicenseError::MissingPublicKey)
Err(e) => assert_eq!(
e.downcast::<LicenseError>().unwrap(),
LicenseError::MissingPublicKey
),
}
}

#[test]
fn it_fails_with_missing_text() {
let builder = LicenseBuilder::default().with_public_key(&[0x08]).with_signature(&[0x08]);
let builder = LicenseBuilder::default()
.with_public_key(&[0x08])
.with_signature(&[0x08]);
match builder.build() {
Ok(_) => unreachable!(),
Err(e) => assert_eq!(e.downcast::<LicenseError>().unwrap(), LicenseError::MissingLicenseText)
Err(e) => assert_eq!(
e.downcast::<LicenseError>().unwrap(),
LicenseError::MissingLicenseText
),
}
}

Expand All @@ -48,7 +62,10 @@ mod tests {
let license = include_bytes!("../examples/license");
let public_key = include_bytes!("../examples/public.pks");

let license = License::new(license).with_public_key(public_key).build().unwrap();
let license = License::new(license)
.with_public_key(public_key)
.build()
.unwrap();
assert!(license.valid());
}
}
Expand Down Expand Up @@ -77,7 +94,6 @@ pub struct LicenseBuilder<'a> {
signature: Option<&'a [u8]>,
}


impl<'a> LicenseBuilder<'a> {
pub fn with_signature(mut self, signature: &'a [u8]) -> Self {
self.signature = Some(signature);
Expand Down Expand Up @@ -110,9 +126,7 @@ impl<'a> LicenseBuilder<'a> {
let pub_key = untrusted::Input::from(public_key);

let valid_signature = match signature::verify(&signature::ED25519, pub_key, msg, sig) {
Ok(_) => {
true
},
Ok(_) => true,
Err(e) => {
debug!("Erorr validating: {:?}", e);
false
Expand Down Expand Up @@ -144,12 +158,14 @@ impl<'a> LicenseBuilder<'a> {
pub struct License {
features: Vec<String>,
expires: Option<DateTime<Utc>>,
#[serde(default="false_f")]
#[serde(default = "false_f")]
signature_valid: bool,
}

#[inline(always)]
fn false_f() -> bool { false }
fn false_f() -> bool {
false
}

impl License {
/// Creates a new builder for the license, helping to construct and
Expand All @@ -175,7 +191,7 @@ impl License {
lb.text = Some(text);
}
lb.signature = Some(&sig[1..]);
},
}
None => {}
}

Expand All @@ -197,15 +213,15 @@ impl License {
/// # }
/// ```
pub fn valid(&self) -> bool {
if ! self.signature_valid {
if !self.signature_valid {
return false;
}
if let Some(expires) = self.expires {
if expires > Utc::now() {
if expires < Utc::now() {
return false;
}
}
return true
return true;
}

/// Does this license have the specified feature?
Expand All @@ -226,4 +242,4 @@ impl License {
let feat = feature.as_ref();
self.features.iter().position(|f| f == feat).is_some()
}
}
}

0 comments on commit f37ec83

Please sign in to comment.