commit c59dc83893f764f62e16ed6d109c2f94d49d2857
parent df9f915126ea7c38d09f7bf94560671b4c7f52c3
Author: Eric Scouten <scouten@adobe.com>
Date: Thu, 14 Jul 2022 16:21:30 -0700
(MINOR) Move crate-level functions for creating signers to new public `create_signer` mod (#72)
Diffstat:
9 files changed, 124 insertions(+), 115 deletions(-)
diff --git a/make_test_images/src/make_test_images.rs b/make_test_images/src/make_test_images.rs
@@ -12,11 +12,9 @@
// each license.
//! Constructs a set of test images using a configuration script
-//!
use c2pa::{
assertions::{c2pa_action, Action, Actions, CreativeWork, SchemaDotOrgPerson},
- get_signer_from_files, jumbf_io, Error, Ingredient, IngredientOptions, Manifest, ManifestStore,
- Signer,
+ create_signer, jumbf_io, Error, Ingredient, IngredientOptions, Manifest, ManifestStore, Signer,
};
use anyhow::{Context, Result};
@@ -38,7 +36,7 @@ fn get_signer_with_alg(alg: &str) -> c2pa::Result<Box<dyn Signer>> {
signcert_path.push(format!("../sdk/tests/fixtures/certs/{}.pub", alg));
let mut pkey_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
pkey_path.push(format!("../sdk/tests/fixtures/certs/{}.pem", alg));
- get_signer_from_files(signcert_path, pkey_path, alg, None)
+ create_signer::from_files(signcert_path, pkey_path, alg, None)
}
/// Defines an operation for creating a test image
diff --git a/sdk/examples/client/client.rs b/sdk/examples/client/client.rs
@@ -17,7 +17,7 @@ use anyhow::Result;
use c2pa::{
assertions::{c2pa_action, labels, Action, Actions, CreativeWork, SchemaDotOrgPerson},
- get_signer_from_files, Ingredient, Manifest, ManifestStore,
+ create_signer, Ingredient, Manifest, ManifestStore,
};
use std::path::PathBuf;
@@ -111,7 +111,7 @@ pub fn main() -> Result<()> {
// sign and embed into the target file
let signcert_path = "../sdk/tests/fixtures/certs.ps256.pem";
let pkey_path = "../sdk/tests/fixtures/certs.ps256.pub";
- let signer = get_signer_from_files(signcert_path, pkey_path, "ps256", None)?;
+ let signer = create_signer::from_files(signcert_path, pkey_path, "ps256", None)?;
manifest.embed(&source, &dest, &*signer)?;
diff --git a/sdk/src/create_signer.rs b/sdk/src/create_signer.rs
@@ -0,0 +1,109 @@
+// Copyright 2022 Adobe. All rights reserved.
+// This file is licensed to you under the Apache License,
+// Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
+// or the MIT license (http://opensource.org/licenses/MIT),
+// at your option.
+
+// Unless required by applicable law or agreed to in writing,
+// this software is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or
+// implied. See the LICENSE-MIT and LICENSE-APACHE files for the
+// specific language governing permissions and limitations under
+// each license.
+
+#![deny(missing_docs)]
+
+//! The `create_signer` module provides a way to obtain a [`Signer`]
+//! instance for each signing format supported by this crate.
+
+use std::path::Path;
+
+use crate::{
+ error::{Error, Result},
+ openssl::{EcSigner, EdSigner, RsaSigner},
+ signer::ConfigurableSigner,
+ Signer,
+};
+
+/// Creates a [`Signer`] instance using signing certificate and private key
+/// as byte slices.
+///
+/// The signing certificate and private key are passed to the underlying
+/// C++ code, which copies them into its own storage.
+///
+/// # Arguments
+///
+/// * `signcert` - Signing certificate
+/// * `pkey` - Private key
+/// * `alg` - Format for signing. Must be one of the supported
+/// formats (`rs256`, `rs384`, `rs512`, `ps256`, `ps384`, `ps512`,
+/// `es256`, `es384`, `es512`, or `ed25519`).
+/// * `tsa_url` - Optional URL for a timestamp authority
+pub fn from_keys(
+ signcert: &[u8],
+ pkey: &[u8],
+ alg: &str,
+ tsa_url: Option<String>,
+) -> Result<Box<dyn Signer>> {
+ Ok(match alg {
+ "ps256" | "ps384" | "ps512" => Box::new(RsaSigner::from_signcert_and_pkey(
+ signcert,
+ pkey,
+ alg.to_owned(),
+ tsa_url,
+ )?),
+ "es256" | "es384" | "es512" => Box::new(EcSigner::from_signcert_and_pkey(
+ signcert,
+ pkey,
+ alg.to_owned(),
+ tsa_url,
+ )?),
+ "ed25519" => Box::new(EdSigner::from_signcert_and_pkey(
+ signcert,
+ pkey,
+ alg.to_owned(),
+ tsa_url,
+ )?),
+ _ => return Err(Error::BadParam(alg.to_owned())),
+ })
+}
+
+/// Creates a [`Signer`] instance using signing certificate and
+/// private key files.
+///
+/// # Arguments
+///
+/// * `signcert_path` - Path to the signing certificate file
+/// * `pkey_path` - Path to the private key file
+/// * `alg` - Format for signing. Must be one of the supported
+/// formats (`rs256`, `rs384`, `rs512`, `ps256`, `ps384`, `ps512`,
+/// `es256`, `es384`, `es512`, or `ed25519`).
+/// * `tsa_url` - Optional URL for a timestamp authority
+pub fn from_files<P: AsRef<Path>>(
+ signcert_path: P,
+ pkey_path: P,
+ alg: &str,
+ tsa_url: Option<String>,
+) -> Result<Box<dyn Signer>> {
+ Ok(match alg {
+ "ps256" | "ps384" | "ps512" => Box::new(RsaSigner::from_files(
+ &signcert_path,
+ &pkey_path,
+ alg.to_owned(),
+ tsa_url,
+ )?),
+ "es256" | "es384" | "es512" => Box::new(EcSigner::from_files(
+ &signcert_path,
+ &pkey_path,
+ alg.to_owned(),
+ tsa_url,
+ )?),
+ "ed25519" => Box::new(EdSigner::from_files(
+ &signcert_path,
+ &pkey_path,
+ alg.to_owned(),
+ tsa_url,
+ )?),
+ _ => return Err(Error::BadParam(alg.to_owned())),
+ })
+}
diff --git a/sdk/src/lib.rs b/sdk/src/lib.rs
@@ -46,7 +46,7 @@
//! # use c2pa::Result;
//! use c2pa::{
//! assertions::User,
-//! get_signer_from_files,
+//! create_signer,
//! Manifest
//! };
//!
@@ -64,7 +64,7 @@
//! // Create a ps256 signer using certs and key files
//! let signcert_path = "tests/fixtures/certs/ps256.pub";
//! let pkey_path = "tests/fixtures/certs/ps256.pem";
-//! let signer = get_signer_from_files(signcert_path, pkey_path, "ps256", None)?;
+//! let signer = create_signer::from_files(signcert_path, pkey_path, "ps256", None)?;
//!
//! // embed a manifest using the signer
//! manifest.embed(&source, &dest, &*signer)?;
@@ -77,6 +77,9 @@ pub mod assertions;
mod cose_validator;
+#[cfg(feature = "file_io")]
+pub mod create_signer;
+
mod error;
pub use error::{Error, Result};
@@ -98,8 +101,6 @@ pub use manifest_store_report::ManifestStoreReport;
pub(crate) mod ocsp_utils;
#[cfg(feature = "file_io")]
mod openssl;
-#[cfg(feature = "file_io")]
-pub use crate::openssl::signer::{get_signer, get_signer_from_files};
#[cfg(feature = "file_io")]
mod signer;
diff --git a/sdk/src/manifest.rs b/sdk/src/manifest.rs
@@ -567,7 +567,7 @@ impl Manifest {
/// # use c2pa::Result;
/// use c2pa::{
/// assertions::User,
- /// get_signer_from_files,
+ /// create_signer,
/// Manifest
/// };
/// # fn main() -> Result<()> {
@@ -580,7 +580,7 @@ impl Manifest {
/// // Create a PS256 signer using certs and public key files.
/// let signcert_path = "tests/fixtures/certs/ps256.pub";
/// let pkey_path = "tests/fixtures/certs/ps256.pem";
- /// let signer = get_signer_from_files(signcert_path, pkey_path, "ps256", None)?;
+ /// let signer = create_signer::from_files(signcert_path, pkey_path, "ps256", None)?;
///
/// // Embed a manifest using the signer.
/// manifest.embed(&source, &dest, &*signer)?;
diff --git a/sdk/src/openssl/mod.rs b/sdk/src/openssl/mod.rs
@@ -29,7 +29,6 @@ pub(crate) use ed_signer::EdSigner;
mod ed_validator;
pub(crate) use ed_validator::EdValidator;
-pub mod signer;
#[cfg(test)]
pub(crate) mod temp_signer;
diff --git a/sdk/src/openssl/signer.rs b/sdk/src/openssl/signer.rs
@@ -1,98 +0,0 @@
-use std::path::Path;
-
-use crate::{
- error::{Error, Result},
- openssl::{EcSigner, EdSigner, RsaSigner},
- signer::ConfigurableSigner,
- Signer,
-};
-
-/// Creates a signer using signcert and public key
-///
-/// Can generate a [`Signer`] instance for all supported formats.
-///
-/// # Arguments
-///
-/// * `signcert` - A buffer containing a signcert
-/// * `pkey` - A buffer containing a public key file
-/// * `alg` - A format for signing. Must be one of (`rs256`, `rs384`, `rs512`,
-/// `ps256`, `ps384`, `ps512`, `es256`, `es384`, `es512`, or `ed25519`).
-/// * `tsa_url` - Optional URL for a timestamp authority.
-///
-/// # Returns
-///
-/// Returns a [`Signer`] instance or Error
-
-pub fn get_signer(
- signcert: &[u8],
- pkey: &[u8],
- alg: &str,
- tsa_url: Option<String>,
-) -> Result<Box<dyn Signer>> {
- Ok(match alg {
- "ps256" | "ps384" | "ps512" => Box::new(RsaSigner::from_signcert_and_pkey(
- signcert,
- pkey,
- alg.to_owned(),
- tsa_url,
- )?),
- "es256" | "es384" | "es512" => Box::new(EcSigner::from_signcert_and_pkey(
- signcert,
- pkey,
- alg.to_owned(),
- tsa_url,
- )?),
- "ed25519" => Box::new(EdSigner::from_signcert_and_pkey(
- signcert,
- pkey,
- alg.to_owned(),
- tsa_url,
- )?),
- _ => return Err(Error::BadParam(alg.to_owned())),
- })
-}
-
-/// Creates a signer using signcert and public key files
-///
-/// Can generate a [`Signer`] instance for all supported formats.
-///
-/// # Arguments
-///
-/// * `signcert_path` - A path to the signing cert file
-/// * `pkey_path` - A path to the public key file
-/// * `alg` - A format for signing. Must be one of (`rs256`, `rs384`, `rs512`,
-/// `ps256`, `ps384`, `ps512`, `es256`, `es384`, `es512`, or `ed25519`).
-/// * `tsa_url` - Optional URL for a timestamp authority.
-///
-/// # Returns
-///
-/// Returns a [`Signer`] instance or Error
-
-pub fn get_signer_from_files<P: AsRef<Path>>(
- signcert_path: P,
- pkey_path: P,
- alg: &str,
- tsa_url: Option<String>,
-) -> Result<Box<dyn Signer>> {
- Ok(match alg {
- "ps256" | "ps384" | "ps512" => Box::new(RsaSigner::from_files(
- &signcert_path,
- &pkey_path,
- alg.to_owned(),
- tsa_url,
- )?),
- "es256" | "es384" | "es512" => Box::new(EcSigner::from_files(
- &signcert_path,
- &pkey_path,
- alg.to_owned(),
- tsa_url,
- )?),
- "ed25519" => Box::new(EdSigner::from_files(
- &signcert_path,
- &pkey_path,
- alg.to_owned(),
- tsa_url,
- )?),
- _ => return Err(Error::BadParam(alg.to_owned())),
- })
-}
diff --git a/sdk/src/utils/test.rs b/sdk/src/utils/test.rs
@@ -23,7 +23,7 @@ use crate::{
#[cfg(feature = "file_io")]
use crate::{
- get_signer_from_files,
+ create_signer,
openssl::RsaSigner,
signer::{ConfigurableSigner, Signer},
};
@@ -234,7 +234,7 @@ pub fn temp_signer_with_alg(alg: &str) -> Box<dyn Signer> {
pem_key_path.push(alg);
pem_key_path.set_extension("pem");
- get_signer_from_files(sign_cert_path.clone(), pem_key_path, alg, None)
+ create_signer::from_files(sign_cert_path.clone(), pem_key_path, alg, None)
.expect("get_temp_signer_with_alg")
}
diff --git a/sdk/tests/integration.rs b/sdk/tests/integration.rs
@@ -18,7 +18,7 @@ mod integration_1 {
use c2pa::{
assertions::{c2pa_action, Action, Actions},
- get_signer_from_files, Ingredient, Manifest, ManifestStore, Result, Signer,
+ create_signer, Ingredient, Manifest, ManifestStore, Result, Signer,
};
use std::path::PathBuf;
use tempfile::tempdir;
@@ -31,7 +31,7 @@ mod integration_1 {
signcert_path.push("tests/fixtures/certs/ps256.pub");
let mut pkey_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
pkey_path.push("tests/fixtures/certs/ps256.pem");
- get_signer_from_files(signcert_path, pkey_path, "ps256", None)
+ create_signer::from_files(signcert_path, pkey_path, "ps256", None)
.expect("get_signer_from_files")
}