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

Remove need for using OpenSSL to generate certs by using pre-generated certs #41

Merged
merged 6 commits into from
Jun 13, 2022
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
7 changes: 1 addition & 6 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,5 @@ Cargo.lock
.DS_Store
.idea

.x509
.ec
.rsa
.ed
.es509

.vscode

1 change: 0 additions & 1 deletion make_test_images/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ anyhow = "1.0"
c2pa = { path="../sdk", features = ["file_io"] }
env_logger = "0.9"
log = "0.4"
tempfile = "3.3"
image = "0.23.10"
nom = "7.1.1"
regex = "1.5.6"
Expand Down
12 changes: 9 additions & 3 deletions make_test_images/src/make_test_images.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,18 @@ use std::{
fs,
path::{Path, PathBuf},
};
use tempfile::tempdir;
use twoway::find_bytes;

const IMAGE_WIDTH: u32 = 2048;
const IMAGE_HEIGHT: u32 = 1365;

// returns a path to a file in the fixtures folder
fn fixture_path(file_name: &str) -> PathBuf {
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
path.push("../sdk/tests/fixtures");
path.push(file_name);
path
}
/// Defines an operation for creating a test image
#[derive(Debug, Deserialize)]
pub struct Recipe {
Expand Down Expand Up @@ -258,9 +264,9 @@ impl MakeTestImages {
manifest.add_assertion(&actions)?; // extra get required here, since actions is an array

// now create store; sign claim and embed in target
let temp_dir = tempdir()?;
let cert_dir: PathBuf = fixture_path("certs");
let (signer, _) =
get_temp_signer_by_alg(&temp_dir.path(), &self.config.alg, self.config.ta.clone());
get_temp_signer_by_alg(&cert_dir, &self.config.alg, self.config.ta.clone());

manifest.embed(&dst_path, &dst_path, signer.as_ref())?;

Expand Down
12 changes: 9 additions & 3 deletions sdk/examples/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ use c2pa::{
get_temp_signer, Ingredient, Manifest, ManifestStore,
};
use std::path::PathBuf;
use tempfile::tempdir;
// returns a path to a file in the fixtures folder
fn fixture_path(file_name: &str) -> PathBuf {
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
path.push("../sdk/tests/fixtures");
path.push(file_name);
path
}

const GENERATOR: &str = "test_app/0.1";
const CREATIVE_WORK_URL: &str = r#"{"@type":"CreativeWork","@context":"https://schema.org","url":"http://contentauthenticity.org"}"#;
Expand Down Expand Up @@ -109,8 +115,8 @@ pub fn main() -> Result<()> {
manifest.add_assertion(&creative_work)?;

// sign and embed into the target file
let temp_dir = tempdir()?;
let (signer, _) = get_temp_signer(&temp_dir.path());
let cert_dir = fixture_path("certs");
let (signer, _) = get_temp_signer(&cert_dir);
manifest.embed(&source, &dest, &signer)?;

let manifest_store = ManifestStore::from_file(&dest)?;
Expand Down
3 changes: 2 additions & 1 deletion sdk/src/asset_handlers/c2pa_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ pub mod tests {
let store = Store::load_from_asset(&temp_path, false, &mut OneShotStatusTracker::new())
.expect("loading store");

let (signer, _) = get_temp_signer(&temp_dir.path());
let cert_dir = fixture_path("certs");
let (signer, _) = get_temp_signer(&cert_dir);

let manifest2 = store.to_jumbf(&signer).expect("to_jumbf");
assert_eq!(&manifest, &manifest2);
Expand Down
11 changes: 5 additions & 6 deletions sdk/src/cose_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1060,23 +1060,22 @@ pub mod tests {
#[test]
#[cfg(feature = "file_io")]
fn test_cert_algorithms() {
use tempfile::tempdir;
let cert_dir = crate::utils::test::fixture_path("certs");

use crate::openssl::temp_signer;

let mut validation_log = DetailedStatusTracker::new();

let temp_dir = tempdir().unwrap();
let (_, cert_path) = temp_signer::get_ec_signer(&temp_dir.path(), "es256", None);
let (_, cert_path) = temp_signer::get_ec_signer(&cert_dir, "es256", None);
let es256_cert = std::fs::read(&cert_path).unwrap();

let (_, cert_path) = temp_signer::get_ec_signer(&temp_dir.path(), "es384", None);
let (_, cert_path) = temp_signer::get_ec_signer(&cert_dir, "es384", None);
let es384_cert = std::fs::read(&cert_path).unwrap();

let (_, cert_path) = temp_signer::get_ec_signer(&temp_dir.path(), "es512", None);
let (_, cert_path) = temp_signer::get_ec_signer(&cert_dir, "es512", None);
let es512_cert = std::fs::read(&cert_path).unwrap();

let (_, cert_path) = temp_signer::get_rsa_signer(&temp_dir.path(), "ps256", None);
let (_, cert_path) = temp_signer::get_rsa_signer(&cert_dir, "ps256", None);
let rsa_pss256_cert = std::fs::read(&cert_path).unwrap();

if let Ok(signcert) = openssl::x509::X509::from_pem(&es256_cert) {
Expand Down
3 changes: 2 additions & 1 deletion sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@
//! let dir = tempdir()?;
//! let dest = dir.path().join("test_file.jpg");
//!
//! let (signer, _) = get_temp_signer(&dir.path());
//! let cert_dir = PathBuf::from("tests/fixtures/certs");
//! let (signer, _) = get_temp_signer(&cert_dir);
//! manifest.embed(&source, &dest, &signer)?;
//! # Ok(())
//! # }
Expand Down
10 changes: 6 additions & 4 deletions sdk/src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,8 @@ pub(crate) mod tests {
let test_output = dir.path().join("wc_embed_test.jpg");

//embed a claim generated from this manifest
let (signer, _) = get_temp_signer(&dir.path());
let cert_dir = fixture_path("certs");
mauricefisher64 marked this conversation as resolved.
Show resolved Hide resolved
let (signer, _) = get_temp_signer(&cert_dir);

let _store = manifest
.embed(&source_path, &test_output, &signer)
Expand Down Expand Up @@ -796,7 +797,8 @@ pub(crate) mod tests {
)
.expect("add_assertion");

let (signer, _) = get_temp_signer(&temp_dir.path());
let cert_dir = fixture_path("certs");
let (signer, _) = get_temp_signer(&cert_dir);

let store1 = manifest.embed(&output, &output, &signer).expect("embed");
let claim1_label = store1.provenance_label().unwrap();
Expand All @@ -818,10 +820,10 @@ pub(crate) mod tests {
manifest2
.add_redaction(ASSERTION_LABEL)
.expect("add_redaction");
let temp_dir = tempdir().expect("temp dir");

//embed a claim in output2
let (signer, _) = get_temp_signer(&temp_dir.path());
let cert_dir = fixture_path("certs");
let (signer, _) = get_temp_signer(&cert_dir);

let _store2 = manifest2.embed(&output2, &output2, &signer).expect("embed");

Expand Down
17 changes: 7 additions & 10 deletions sdk/src/openssl/ec_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,15 +208,12 @@ mod tests {

use super::*;

use tempfile::tempdir;

use crate::openssl::temp_signer;

use crate::{openssl::temp_signer, utils::test::fixture_path};
#[test]
fn es256_signer() {
let temp_dir = tempdir().unwrap();
let cert_dir = fixture_path("certs");

let (signer, _) = temp_signer::get_ec_signer(&temp_dir.path(), "es256", None);
let (signer, _) = temp_signer::get_ec_signer(&cert_dir, "es256", None);

let data = b"some sample content to sign";
println!("data len = {}", data.len());
Expand All @@ -229,9 +226,9 @@ mod tests {

#[test]
fn es384_signer() {
let temp_dir = tempdir().unwrap();
let cert_dir = fixture_path("certs");

let (signer, _) = temp_signer::get_ec_signer(&temp_dir.path(), "es384", None);
let (signer, _) = temp_signer::get_ec_signer(&cert_dir, "es384", None);

let data = b"some sample content to sign";
println!("data len = {}", data.len());
Expand All @@ -244,9 +241,9 @@ mod tests {

#[test]
fn es512_signer() {
let temp_dir = tempdir().unwrap();
let cert_dir = fixture_path("certs");

let (signer, _) = temp_signer::get_ec_signer(&temp_dir.path(), "es512", None);
let (signer, _) = temp_signer::get_ec_signer(&cert_dir, "es512", None);

let data = b"some sample content to sign";
println!("data len = {}", data.len());
Expand Down
53 changes: 11 additions & 42 deletions sdk/src/openssl/ec_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,13 @@ mod tests {
#![allow(clippy::unwrap_used)]
use super::*;

use tempfile::tempdir;

use crate::{
openssl::{ec_signer::EcSigner, temp_signer},
signer::ConfigurableSigner,
utils::test::fixture_path,
Signer,
};
use crate::{openssl::temp_signer, utils::test::fixture_path, Signer};

#[test]
fn sign_and_validate_es256() {
let temp_dir = tempdir().unwrap();
let cert_dir = fixture_path("certs");

let (signer, cert_path) = temp_signer::get_ec_signer(&temp_dir.path(), "es256", None);
let (signer, cert_path) = temp_signer::get_ec_signer(&cert_dir, "es256", None);

let data = b"some sample content to sign";
println!("data len = {}", data.len());
Expand All @@ -113,9 +106,9 @@ mod tests {

#[test]
fn sign_and_validate_es384() {
let temp_dir = tempdir().unwrap();
let cert_dir = fixture_path("certs");

let (signer, cert_path) = temp_signer::get_ec_signer(&temp_dir.path(), "es384", None);
let (signer, cert_path) = temp_signer::get_ec_signer(&cert_dir, "es384", None);

let data = b"some sample content to sign";
println!("data len = {}", data.len());
Expand All @@ -136,9 +129,9 @@ mod tests {

#[test]
fn sign_and_validate_es512() {
let temp_dir = tempdir().unwrap();
let cert_dir = fixture_path("certs");

let (signer, cert_path) = temp_signer::get_ec_signer(&temp_dir.path(), "es512", None);
let (signer, cert_path) = temp_signer::get_ec_signer(&cert_dir, "es512", None);

let data = b"some sample content to sign";
println!("data len = {}", data.len());
Expand All @@ -159,9 +152,9 @@ mod tests {

#[test]
fn bad_sig_es256() {
let temp_dir = tempdir().unwrap();
let cert_dir = fixture_path("certs");

let (signer, cert_path) = temp_signer::get_ec_signer(&temp_dir.path(), "es256", None);
let (signer, cert_path) = temp_signer::get_ec_signer(&cert_dir, "es256", None);

let data = b"some sample content to sign";
println!("data len = {}", data.len());
Expand All @@ -180,9 +173,9 @@ mod tests {

#[test]
fn bad_data_es256() {
let temp_dir = tempdir().unwrap();
let cert_dir = fixture_path("certs");

let (signer, cert_path) = temp_signer::get_ec_signer(&temp_dir.path(), "es256", None);
let (signer, cert_path) = temp_signer::get_ec_signer(&cert_dir, "es256", None);

let mut data = b"some sample content to sign".to_vec();
println!("data len = {}", data.len());
Expand All @@ -198,28 +191,4 @@ mod tests {
let validator = EcValidator::new("es256");
assert!(!validator.validate(&signature, &data, &pub_key).unwrap());
}

#[test]
mauricefisher64 marked this conversation as resolved.
Show resolved Hide resolved
fn sign_and_validate_with_chain() {
let pkey_path = fixture_path("bob.key");
let cert_path = fixture_path("bob.pem");

let signer =
EcSigner::from_files(&cert_path, &pkey_path, "es256".to_string(), None).unwrap();

let data = b"some sample content to sign";
println!("data len = {}", data.len());

let signature = signer.sign(data).unwrap();
println!("signature.len = {}", signature.len());
assert!(signature.len() >= 64);
assert!(signature.len() <= signer.reserve_size());

let cert_bytes = &signer.certs().unwrap()[0];
let signcert = openssl::x509::X509::from_der(cert_bytes).unwrap();

let pub_key = signcert.public_key().unwrap().public_key_to_der().unwrap();
let validator = EcValidator::new("es256");
assert!(validator.validate(&signature, data, &pub_key).unwrap());
}
}
8 changes: 3 additions & 5 deletions sdk/src/openssl/ed_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,13 @@ mod tests {
#![allow(clippy::unwrap_used)]
use super::*;

use tempfile::tempdir;

use crate::openssl::temp_signer;
use crate::{openssl::temp_signer, utils::test::fixture_path};

#[test]
fn ed25519_signer() {
let temp_dir = tempdir().unwrap();
let cert_dir = fixture_path("certs");

let (signer, _) = temp_signer::get_ed_signer(&temp_dir.path(), "ed25519", None);
let (signer, _) = temp_signer::get_ed_signer(&cert_dir, "ed25519", None);

let data = b"some sample content to sign";
println!("data len = {}", data.len());
Expand Down
12 changes: 5 additions & 7 deletions sdk/src/openssl/ed_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,13 @@ mod tests {

use super::*;

use tempfile::tempdir;

use crate::{openssl::temp_signer, Signer};
use crate::{openssl::temp_signer, utils::test::fixture_path, Signer};

#[test]
fn sign_and_validate() {
let temp_dir = tempdir().unwrap();
let cert_dir = fixture_path("certs");

let (signer, cert_path) = temp_signer::get_ed_signer(&temp_dir.path(), "ed25519", None);
let (signer, cert_path) = temp_signer::get_ed_signer(&cert_dir, "ed25519", None);

let data = b"some sample content to sign";
println!("data len = {}", data.len());
Expand All @@ -74,9 +72,9 @@ mod tests {

#[test]
fn bad_data() {
let temp_dir = tempdir().unwrap();
let cert_dir = fixture_path("certs");

let (signer, cert_path) = temp_signer::get_ed_signer(&temp_dir.path(), "ed25519", None);
let (signer, cert_path) = temp_signer::get_ed_signer(&cert_dir, "ed25519", None);

let mut data = b"some sample content to sign".to_vec();
println!("data len = {}", data.len());
Expand Down
8 changes: 3 additions & 5 deletions sdk/src/openssl/rsa_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,15 +212,13 @@ mod tests {

use super::*;

use tempfile::tempdir;

use crate::{openssl::temp_signer::get_temp_signer, Signer};
use crate::{openssl::temp_signer::get_temp_signer, utils::test::fixture_path, Signer};

#[test]
fn signer_from_files() {
let temp_dir = tempdir().unwrap();
let cert_dir = fixture_path("certs");

let (signer, _) = get_temp_signer(&temp_dir.path());
let (signer, _) = get_temp_signer(&cert_dir);
let data = b"some sample content to sign";

let signature = signer.sign(data).unwrap();
Expand Down
Loading