Skip to content

Commit 553f90e

Browse files
author
Xynnn007
committed
refactor: remove ring dependency
- replace ring crate with RustCrypto - refactor CosignVerificationKey implementation and interface - fix the code which references CosignVerificationKey Signed-off-by: Xynnn007 <[email protected]>
1 parent 7805f14 commit 553f90e

File tree

17 files changed

+239
-290
lines changed

17 files changed

+239
-290
lines changed

Cargo.toml

+1-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ openidconnect = { version = "2.3", default-features = false, features = [ "reqwe
2626
pem = "1.0.2"
2727
picky = { version = "7.0.0-rc.3", default-features = false, features = [ "x509", "ec" ] }
2828
regex = "1.5.5"
29-
ring = "0.16.20"
3029
serde_json = "1.0.79"
3130
serde = { version = "1.0.136", features = ["derive"] }
3231
sha2 = "0.10.2"
@@ -44,7 +43,7 @@ pkcs8 = { version = "0.9.0", features = ["pem", "alloc", "pkcs5", "encryption"]
4443
elliptic-curve = { version = "0.12.2", features = [ "arithmetic", "pem" ] }
4544
p256 = "0.11.1"
4645
p384 = "0.11.1"
47-
ecdsa = { version = "0.14.3", features = [ "pkcs8", "digest" ] }
46+
ecdsa = { version = "0.14.3", features = [ "pkcs8", "digest", "der" ] }
4847
digest = "0.10.3"
4948
signature = { version = "1.5.0", features = [ "digest-preview" ] }
5049
ed25519 = { version = "1", features = [ "alloc" ] }

examples/cosign/verify/main.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use sigstore::cosign::verification_constraint::{
1919
VerificationConstraintVec,
2020
};
2121
use sigstore::cosign::{CosignCapabilities, SignatureLayer};
22-
use sigstore::crypto::SignatureDigestAlgorithm;
22+
use sigstore::crypto::SigningScheme;
2323
use sigstore::errors::SigstoreVerifyConstraintsError;
2424
use sigstore::tuf::SigstoreRepository;
2525
use std::boxed::Box;
@@ -47,9 +47,9 @@ struct Cli {
4747
#[clap(short, long, required(false))]
4848
key: Option<String>,
4949

50-
/// Digest algorithm to use when processing a signature
51-
#[clap(long, default_value = "sha256")]
52-
signature_digest_algorithm: String,
50+
/// Signing scheme when signing and verifying
51+
#[clap(long, default_value = "ECDSA_P256_SHA256_ASN1")]
52+
signing_scheme: String,
5353

5454
/// Fetch Rekor and Fulcio data from Sigstore's TUF repository"
5555
#[clap(long)]
@@ -156,10 +156,9 @@ async fn run_app(
156156
}
157157
if let Some(path_to_key) = cli.key.as_ref() {
158158
let key = fs::read(path_to_key).map_err(|e| anyhow!("Cannot read key: {:?}", e))?;
159-
let signature_digest_algorithm =
160-
SignatureDigestAlgorithm::try_from(cli.signature_digest_algorithm.as_str())
161-
.map_err(anyhow::Error::msg)?;
162-
let verifier = PublicKeyVerifier::new(&key, signature_digest_algorithm)
159+
let signing_scheme =
160+
SigningScheme::try_from(cli.signing_scheme.as_str()).map_err(anyhow::Error::msg)?;
161+
let verifier = PublicKeyVerifier::new(&key, &signing_scheme)
163162
.map_err(|e| anyhow!("Cannot create public key verifier: {}", e))?;
164163
verification_constraints.push(Box::new(verifier));
165164
}

examples/key_interface/key_pair_gen_and_export/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// limitations under the License.
1515

1616
use anyhow::Result;
17-
use sigstore::crypto::signing_key::SigningScheme;
17+
use sigstore::crypto::SigningScheme;
1818

1919
const PASSWORD: &str = "example password";
2020

examples/key_interface/key_pair_gen_sign_verify/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// limitations under the License.
1515

1616
use anyhow::{anyhow, Result};
17-
use sigstore::crypto::{signing_key::SigningScheme, Signature};
17+
use sigstore::crypto::{Signature, SigningScheme};
1818

1919
const DATA_TO_BE_SIGNED: &str = "this is an example data to be signed";
2020

examples/key_interface/key_pair_import/main.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@
1414
// limitations under the License.
1515

1616
use anyhow::{bail, Result};
17-
use ring::signature::ECDSA_P256_SHA256_ASN1;
1817
use sigstore::crypto::{
1918
signing_key::{ecdsa::ECDSAKeys, SigStoreKeyPair},
20-
CosignVerificationKey, SignatureDigestAlgorithm,
19+
CosignVerificationKey, SigningScheme,
2120
};
2221

2322
const PASSWORD: &str = "password";
@@ -30,13 +29,10 @@ const ECDSA_P256_ASN1_ENCRYPTED_PRIVATE_PEM: &[u8] =
3029
include_bytes!("./ECDSA_P256_ASN1_ENCRYPTED_PRIVATE_PEM.key");
3130

3231
fn main() -> Result<()> {
33-
let _ = CosignVerificationKey::from_pem(
34-
ECDSA_P256_ASN1_PUBLIC_PEM,
35-
SignatureDigestAlgorithm::Sha256,
36-
)?;
32+
let _ = CosignVerificationKey::from_pem(ECDSA_P256_ASN1_PUBLIC_PEM, &SigningScheme::default())?;
3733
println!("Imported PEM encoded public key as CosignVerificationKey.");
3834

39-
let _ = CosignVerificationKey::from_der(ECDSA_P256_ASN1_PUBLIC_DER, &ECDSA_P256_SHA256_ASN1)?;
35+
let _ = CosignVerificationKey::from_der(ECDSA_P256_ASN1_PUBLIC_DER, &SigningScheme::default())?;
4036
println!("Imported DER encoded public key as CosignVerificationKey.");
4137

4238
let _ = SigStoreKeyPair::from_pem(ECDSA_P256_ASN1_PRIVATE_PEM)?;

src/cosign/bundle.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ mod tests {
7070
use serde_json::json;
7171

7272
use crate::cosign::tests::get_rekor_public_key;
73-
use crate::crypto::SignatureDigestAlgorithm;
73+
use crate::crypto::SigningScheme;
7474

7575
fn build_correct_bundle() -> String {
7676
let bundle_json = json!({
@@ -101,11 +101,9 @@ mod tests {
101101
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAENptdY/l3nB0yqkXLBWkZWQwo6+cu
102102
OSWS1X9vPavpiQOoTTGC0xX57OojUadxF1cdQmrsiReWg2Wn4FneJfa8xw==
103103
-----END PUBLIC KEY-----"#;
104-
let not_rekor_pub_key = CosignVerificationKey::from_pem(
105-
public_key.as_bytes(),
106-
SignatureDigestAlgorithm::default(),
107-
)
108-
.expect("Cannot create CosignVerificationKey");
104+
let not_rekor_pub_key =
105+
CosignVerificationKey::from_pem(public_key.as_bytes(), &SigningScheme::default())
106+
.expect("Cannot create CosignVerificationKey");
109107

110108
let bundle_json = build_correct_bundle();
111109
let bundle = Bundle::new_verified(&bundle_json, &not_rekor_pub_key);

src/cosign/client.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,13 @@ impl Client {
137137
mod tests {
138138
use super::*;
139139
use crate::cosign::tests::{get_fulcio_cert_pool, REKOR_PUB_KEY};
140-
use crate::{crypto::SignatureDigestAlgorithm, mock_client::test::MockOciClient};
140+
use crate::crypto::SigningScheme;
141+
use crate::mock_client::test::MockOciClient;
141142

142143
fn build_test_client(mock_client: MockOciClient) -> Client {
143-
let rekor_pub_key = CosignVerificationKey::from_pem(
144-
REKOR_PUB_KEY.as_bytes(),
145-
SignatureDigestAlgorithm::default(),
146-
)
147-
.expect("Cannot create CosignVerificationKey");
144+
let rekor_pub_key =
145+
CosignVerificationKey::from_pem(REKOR_PUB_KEY.as_bytes(), &SigningScheme::default())
146+
.expect("Cannot create CosignVerificationKey");
148147

149148
Client {
150149
registry_client: Box::new(mock_client),

src/cosign/client_builder.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@
1616
use tracing::info;
1717

1818
use super::client::Client;
19-
use crate::crypto::{
20-
certificate_pool::CertificatePool, CosignVerificationKey, SignatureDigestAlgorithm,
21-
};
19+
use crate::crypto::SigningScheme;
20+
use crate::crypto::{certificate_pool::CertificatePool, CosignVerificationKey};
2221
use crate::errors::Result;
2322
use crate::registry::{Certificate, ClientConfig};
2423

@@ -125,7 +124,7 @@ impl ClientBuilder {
125124
}
126125
Some(data) => Some(CosignVerificationKey::from_pem(
127126
data.as_bytes(),
128-
SignatureDigestAlgorithm::default(),
127+
&SigningScheme::default(),
129128
)?),
130129
};
131130

src/cosign/mod.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ mod tests {
150150
AnnotationVerifier, CertSubjectEmailVerifier, VerificationConstraintVec,
151151
};
152152
use crate::crypto::certificate_pool::CertificatePool;
153-
use crate::crypto::{CosignVerificationKey, SignatureDigestAlgorithm};
153+
use crate::crypto::{CosignVerificationKey, SigningScheme};
154154
use crate::simple_signing::Optional;
155155

156156
pub(crate) const REKOR_PUB_KEY: &str = r#"-----BEGIN PUBLIC KEY-----
@@ -201,11 +201,8 @@ TNMea7Ix/stJ5TfcLLeABLE4BNJOsQ4vnBHJ
201201
}
202202

203203
pub(crate) fn get_rekor_public_key() -> CosignVerificationKey {
204-
CosignVerificationKey::from_pem(
205-
REKOR_PUB_KEY.as_bytes(),
206-
SignatureDigestAlgorithm::default(),
207-
)
208-
.expect("Cannot create test REKOR_PUB_KEY")
204+
CosignVerificationKey::from_pem(REKOR_PUB_KEY.as_bytes(), &SigningScheme::default())
205+
.expect("Cannot create test REKOR_PUB_KEY")
209206
}
210207

211208
#[test]

src/cosign/signature_layers.rs

+8-15
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ use super::constants::{
3131
};
3232
use crate::crypto::certificate_pool::CertificatePool;
3333
use crate::{
34-
crypto::{
35-
self, CosignVerificationKey, Signature, SIGSTORE_DEFAULT_SIGNATURE_VERIFICATION_ALGORITHM,
36-
},
34+
crypto::{self, CosignVerificationKey, Signature, SigningScheme},
3735
errors::{Result, SigstoreError},
3836
simple_signing::SimpleSigning,
3937
};
@@ -366,10 +364,8 @@ impl CertificateSignature {
366364
crypto::certificate::is_trusted(&cert, integrated_time)?;
367365

368366
let subject = CertificateSubject::from_certificate(&cert)?;
369-
let verification_key = CosignVerificationKey::from_der(
370-
cert.public_key().raw,
371-
SIGSTORE_DEFAULT_SIGNATURE_VERIFICATION_ALGORITHM,
372-
)?;
367+
let verification_key =
368+
CosignVerificationKey::from_der(cert.public_key().raw, &SigningScheme::default())?;
373369

374370
let issuer = get_cert_extension_by_oid(&cert, SIGSTORE_ISSUER_OID, "Issuer")?;
375371

@@ -464,7 +460,6 @@ pub(crate) mod tests {
464460
use std::convert::TryFrom;
465461

466462
use crate::cosign::tests::{get_fulcio_cert_pool, get_rekor_public_key};
467-
use crate::crypto::SignatureDigestAlgorithm;
468463

469464
pub(crate) fn build_correct_signature_layer_without_bundle(
470465
) -> (SignatureLayer, CosignVerificationKey) {
@@ -474,11 +469,9 @@ OSWS1X9vPavpiQOoTTGC0xX57OojUadxF1cdQmrsiReWg2Wn4FneJfa8xw==
474469
-----END PUBLIC KEY-----"#;
475470

476471
let signature = String::from("MEUCIQD6q/COgzOyW0YH1Dk+CCYSt4uAhm3FDHUwvPI55zwnlwIgE0ZK58ZOWpZw8YVmBapJhBqCfdPekIknimuO0xH8Jh8=");
477-
let verification_key = CosignVerificationKey::from_pem(
478-
public_key.as_bytes(),
479-
SignatureDigestAlgorithm::default(),
480-
)
481-
.expect("Cannot create CosignVerificationKey");
472+
let verification_key =
473+
CosignVerificationKey::from_pem(public_key.as_bytes(), &SigningScheme::default())
474+
.expect("Cannot create CosignVerificationKey");
482475
let ss_value = json!({
483476
"critical": {
484477
"identity": {
@@ -576,7 +569,7 @@ MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAETJP9cqpUQsn2ggmJniWGjHdlsHzD
576569
JsB89BPhZYch0U0hKANx5TY+ncrm0s8bfJxxHoenAEFhwhuXeb4PqIrtoQ==
577570
-----END PUBLIC KEY-----"#
578571
.as_bytes(),
579-
SignatureDigestAlgorithm::default(),
572+
&SigningScheme::default(),
580573
)
581574
.expect("Cannot create CosignVerificationKey");
582575

@@ -789,7 +782,7 @@ MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAETJP9cqpUQsn2ggmJniWGjHdlsHzD
789782
JsB89BPhZYch0U0hKANx5TY+ncrm0s8bfJxxHoenAEFhwhuXeb4PqIrtoQ==
790783
-----END PUBLIC KEY-----"#
791784
.as_bytes(),
792-
SignatureDigestAlgorithm::default(),
785+
&SigningScheme::default(),
793786
)
794787
.expect("Cannot create CosignVerificationKey");
795788
assert!(!sl.is_signed_by_key(&verification_key));

src/cosign/verification_constraint.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
use std::collections::HashMap;
3232

3333
use super::signature_layers::{CertificateSubject, SignatureLayer};
34-
use crate::crypto::{CosignVerificationKey, SignatureDigestAlgorithm};
34+
use crate::crypto::{CosignVerificationKey, SigningScheme};
3535
use crate::errors::Result;
3636

3737
/// A list of objects implementing the [`VerificationConstraint`] trait
@@ -79,11 +79,8 @@ impl PublicKeyVerifier {
7979
/// Create a new instance of `PublicKeyVerifier`.
8080
/// The `key_raw` variable holds a PEM encoded rapresentation of the
8181
/// public key to be used at verification time.
82-
pub fn new(
83-
key_raw: &[u8],
84-
signature_digest_algorithm: SignatureDigestAlgorithm,
85-
) -> Result<Self> {
86-
let key = CosignVerificationKey::from_pem(key_raw, signature_digest_algorithm)?;
82+
pub fn new(key_raw: &[u8], signing_scheme: &SigningScheme) -> Result<Self> {
83+
let key = CosignVerificationKey::from_pem(key_raw, signing_scheme)?;
8784
Ok(PublicKeyVerifier { key })
8885
}
8986
}

src/crypto/mod.rs

+56-26
Original file line numberDiff line numberDiff line change
@@ -15,44 +15,70 @@
1515

1616
//! Structures and constants required to perform cryptographic operations.
1717
18-
use ring::signature;
18+
use sha2::{Sha256, Sha384};
1919
use std::convert::TryFrom;
2020

21-
/// The default signature verification algorithm used by Sigstore.
22-
/// Sigstore relies on NIST P-256
23-
/// NIST P-256 is a Weierstrass curve specified in [FIPS 186-4: Digital Signature Standard (DSS)](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf).
24-
/// Also known as prime256v1 (ANSI X9.62) and secp256r1 (SECG)
25-
pub static SIGSTORE_DEFAULT_SIGNATURE_VERIFICATION_ALGORITHM:
26-
&signature::EcdsaVerificationAlgorithm = &signature::ECDSA_P256_SHA256_ASN1;
27-
28-
/// Describes the signature digest algorithms supported.
29-
/// The default one is sha256.
30-
#[derive(Debug, Clone)]
31-
pub enum SignatureDigestAlgorithm {
32-
Sha256,
33-
Sha384,
34-
Sha512,
21+
use crate::errors::*;
22+
23+
pub use signing_key::SigStoreSigner;
24+
pub use verification_key::CosignVerificationKey;
25+
26+
/// Different digital signature algorithms.
27+
/// * `ECDSA_P256_SHA256_ASN1`: ASN.1 DER-encoded ECDSA
28+
/// signatures using the P-256 curve and SHA-256. It
29+
/// is the default signing scheme.
30+
/// * `ECDSA_P384_SHA384_ASN1`: ASN.1 DER-encoded ECDSA
31+
/// signatures using the P-384 curve and SHA-384.
32+
/// * `ED25519`: ECDSA signature using SHA2-512
33+
/// as the digest function and curve edwards25519. The
34+
/// signature format please refer
35+
/// to [RFC 8032](https://www.rfc-editor.org/rfc/rfc8032.html#section-5.1.6).
36+
#[allow(non_camel_case_types)]
37+
#[derive(Debug, Clone, Copy)]
38+
pub enum SigningScheme {
39+
// TODO: Support RSA
40+
ECDSA_P256_SHA256_ASN1,
41+
ECDSA_P384_SHA384_ASN1,
42+
ED25519,
3543
}
3644

37-
impl TryFrom<&str> for SignatureDigestAlgorithm {
45+
impl TryFrom<&str> for SigningScheme {
3846
type Error = String;
3947

4048
fn try_from(value: &str) -> std::result::Result<Self, Self::Error> {
4149
match value {
42-
"sha256" => Ok(Self::Sha256),
43-
"sha384" => Ok(Self::Sha384),
44-
"sha512" => Ok(Self::Sha512),
45-
unknown => Err(format!(
46-
"Unsupported signature digest algorithm: {}",
47-
unknown
48-
)),
50+
"ECDSA_P256_SHA256_ASN1" => Ok(Self::ECDSA_P256_SHA256_ASN1),
51+
"ECDSA_P384_SHA384_ASN1" => Ok(Self::ECDSA_P384_SHA384_ASN1),
52+
"ED25519" => Ok(Self::ED25519),
53+
unknown => Err(format!("Unsupported signing algorithm: {}", unknown)),
4954
}
5055
}
5156
}
5257

53-
impl Default for SignatureDigestAlgorithm {
58+
impl SigningScheme {
59+
/// Create a key-pair due to the given signing scheme.
60+
pub fn create_signer(&self) -> Result<SigStoreSigner> {
61+
Ok(match self {
62+
SigningScheme::ECDSA_P256_SHA256_ASN1 => SigStoreSigner::ECDSA_P256_SHA256_ASN1(
63+
EcdsaSigner::<_, Sha256>::from_ecdsa_keys(&EcdsaKeys::<p256::NistP256>::new()?)?,
64+
),
65+
SigningScheme::ECDSA_P384_SHA384_ASN1 => SigStoreSigner::ECDSA_P384_SHA384_ASN1(
66+
EcdsaSigner::<_, Sha384>::from_ecdsa_keys(&EcdsaKeys::<p384::NistP384>::new()?)?,
67+
),
68+
SigningScheme::ED25519 => {
69+
SigStoreSigner::ED25519(Ed25519Signer::from_ed25519_keys(&Ed25519Keys::new()?)?)
70+
}
71+
})
72+
}
73+
}
74+
75+
/// The default signature verification algorithm used by Sigstore.
76+
/// Sigstore relies on NIST P-256
77+
/// NIST P-256 is a Weierstrass curve specified in [FIPS 186-4: Digital Signature Standard (DSS)](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf).
78+
/// Also known as prime256v1 (ANSI X9.62) and secp256r1 (SECG)
79+
impl Default for SigningScheme {
5480
fn default() -> Self {
55-
Self::Sha256
81+
SigningScheme::ECDSA_P256_SHA256_ASN1
5682
}
5783
}
5884

@@ -68,7 +94,11 @@ pub(crate) mod certificate;
6894
pub(crate) mod certificate_pool;
6995

7096
pub mod verification_key;
71-
pub use verification_key::CosignVerificationKey;
97+
98+
use self::signing_key::{
99+
ecdsa::ec::{EcdsaKeys, EcdsaSigner},
100+
ed25519::{Ed25519Keys, Ed25519Signer},
101+
};
72102

73103
pub mod signing_key;
74104

0 commit comments

Comments
 (0)