-
Notifications
You must be signed in to change notification settings - Fork 58
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
feat(crypto): Add rsa
crate support to rust_native_crypto
feature
#853
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
e378942
feat(crypto): Add `rsa` crate support to `rust_native_crypto` feature
scouten-adobe 913633f
Clippy
scouten-adobe 7a93ce2
Clean up debugging / Q&D error handling code
scouten-adobe 36e48bb
cargo fmt
scouten-adobe 0bef13b
Update reference to `async_validator_for_signing_alg`
scouten-adobe 86960bd
Remove `RsaWasmSigner` from c2pa-rs
scouten-adobe 922c72a
Check for proper private key OIDs
scouten-adobe c6e5269
cargo fmt
scouten-adobe 87f96bb
Fix problems verifying time stamps on WASM builds
scouten-adobe f4d0c88
Merge branch 'main' into crypto+native-rsa-signer
scouten-adobe a7ab334
cargo fmt
scouten-adobe ef14425
Remove tests that are no longer needed
scouten-adobe 650cda8
Update time_stamp::verify to use synchronous validators when possible
scouten-adobe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
181 changes: 181 additions & 0 deletions
181
internal/crypto/src/raw_signature/rust_native/signers/rsa_signer.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
// Copyright 2024 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. | ||
|
||
use const_oid::ObjectIdentifier; | ||
use der::{pem::PemLabel, SecretDocument}; | ||
use num_bigint_dig::BigUint; | ||
use rsa::{ | ||
pkcs8::PrivateKeyInfo, | ||
pss::SigningKey, | ||
sha2::{Sha256, Sha384, Sha512}, | ||
signature::{RandomizedSigner, SignatureEncoding}, | ||
RsaPrivateKey, | ||
}; | ||
use x509_parser::{error::PEMError, pem::Pem}; | ||
|
||
use crate::{ | ||
raw_signature::{RawSigner, RawSignerError, SigningAlg}, | ||
time_stamp::TimeStampProvider, | ||
}; | ||
|
||
enum RsaSigningAlg { | ||
Ps256, | ||
Ps384, | ||
Ps512, | ||
} | ||
|
||
/// Implements [`RawSigner`] trait using `rsa` crate's implementation of SHA256 | ||
/// + RSA encryption. | ||
pub(crate) struct RsaSigner { | ||
alg: RsaSigningAlg, | ||
|
||
cert_chain: Vec<Vec<u8>>, | ||
cert_chain_len: usize, | ||
|
||
private_key: RsaPrivateKey, | ||
|
||
time_stamp_service_url: Option<String>, | ||
time_stamp_size: usize, | ||
} | ||
|
||
// Can't use the OIDs defined at certificate_profile.rs because they're | ||
// different underlying types. (Sigh.) | ||
const RSA_OID: ObjectIdentifier = ObjectIdentifier::new_unwrap("1.2.840.113549.1.1.1"); | ||
const RSA_PSS_OID: ObjectIdentifier = ObjectIdentifier::new_unwrap("1.2.840.113549.1.1.10"); | ||
|
||
impl RsaSigner { | ||
pub(crate) fn from_cert_chain_and_private_key( | ||
cert_chain: &[u8], | ||
private_key: &[u8], | ||
alg: SigningAlg, | ||
time_stamp_service_url: Option<String>, | ||
) -> Result<Self, RawSignerError> { | ||
let cert_chain = Pem::iter_from_buffer(cert_chain) | ||
.map(|r| match r { | ||
Ok(pem) => Ok(pem.contents), | ||
Err(e) => Err(e), | ||
}) | ||
.collect::<Result<Vec<Vec<u8>>, PEMError>>() | ||
.map_err(|e| RawSignerError::InvalidSigningCredentials(e.to_string()))?; | ||
|
||
// TO DO: check_chain_order(&cert_chain).await?; | ||
|
||
let cert_chain_len = cert_chain.len(); | ||
|
||
let pem_str = std::str::from_utf8(private_key) | ||
.map_err(|e| RawSignerError::InvalidSigningCredentials(e.to_string()))?; | ||
|
||
let (label, private_key_der) = SecretDocument::from_pem(pem_str) | ||
.map_err(|e| RawSignerError::InvalidSigningCredentials(e.to_string()))?; | ||
|
||
PrivateKeyInfo::validate_pem_label(label) | ||
.map_err(|e| RawSignerError::InvalidSigningCredentials(e.to_string()))?; | ||
|
||
let pki = PrivateKeyInfo::try_from(private_key_der.as_bytes()) | ||
.map_err(|e| RawSignerError::InvalidSigningCredentials(e.to_string()))?; | ||
|
||
let oid = &pki.algorithm.oid; | ||
if !(oid == &RSA_OID || oid == &RSA_PSS_OID) { | ||
return Err(RawSignerError::InvalidSigningCredentials(format!( | ||
"unsupported private key algorithm ({oid})" | ||
))); | ||
} | ||
|
||
let pkcs1_key = pkcs1::RsaPrivateKey::try_from(pki.private_key) | ||
.map_err(|e| RawSignerError::InvalidSigningCredentials(e.to_string()))?; | ||
|
||
if pkcs1_key.version() != pkcs1::Version::TwoPrime { | ||
return Err(RawSignerError::InvalidSigningCredentials( | ||
"multi-prime RSA keys not supported".to_string(), | ||
)); | ||
} | ||
|
||
let n = BigUint::from_bytes_be(pkcs1_key.modulus.as_bytes()); | ||
let e = BigUint::from_bytes_be(pkcs1_key.public_exponent.as_bytes()); | ||
let d = BigUint::from_bytes_be(pkcs1_key.private_exponent.as_bytes()); | ||
let prime1 = BigUint::from_bytes_be(pkcs1_key.prime1.as_bytes()); | ||
let prime2 = BigUint::from_bytes_be(pkcs1_key.prime2.as_bytes()); | ||
let primes = vec![prime1, prime2]; | ||
let private_key = RsaPrivateKey::from_components(n, e, d, primes) | ||
.map_err(|e| RawSignerError::InvalidSigningCredentials(e.to_string()))?; | ||
|
||
let alg: RsaSigningAlg = match alg { | ||
SigningAlg::Ps256 => RsaSigningAlg::Ps256, | ||
SigningAlg::Ps384 => RsaSigningAlg::Ps384, | ||
SigningAlg::Ps512 => RsaSigningAlg::Ps512, | ||
_ => { | ||
return Err(RawSignerError::InternalError( | ||
"RsaSigner should be used only for SigningAlg::Ps***".to_string(), | ||
)); | ||
} | ||
}; | ||
|
||
Ok(RsaSigner { | ||
alg, | ||
cert_chain, | ||
private_key, | ||
cert_chain_len, | ||
time_stamp_service_url, | ||
time_stamp_size: 10000, | ||
// TO DO: Call out to time stamp service to get actual time stamp and use that size? | ||
}) | ||
} | ||
} | ||
|
||
impl RawSigner for RsaSigner { | ||
fn sign(&self, data: &[u8]) -> Result<Vec<u8>, RawSignerError> { | ||
let mut rng = rand::thread_rng(); | ||
|
||
match self.alg { | ||
RsaSigningAlg::Ps256 => { | ||
let s = SigningKey::<Sha256>::new(self.private_key.clone()); | ||
let sig = s.sign_with_rng(&mut rng, data); | ||
Ok(sig.to_bytes().to_vec()) | ||
} | ||
|
||
RsaSigningAlg::Ps384 => { | ||
let s = SigningKey::<Sha384>::new(self.private_key.clone()); | ||
let sig = s.sign_with_rng(&mut rng, data); | ||
Ok(sig.to_bytes().to_vec()) | ||
} | ||
|
||
RsaSigningAlg::Ps512 => { | ||
let s = SigningKey::<Sha512>::new(self.private_key.clone()); | ||
let sig = s.sign_with_rng(&mut rng, data); | ||
Ok(sig.to_bytes().to_vec()) | ||
} | ||
} | ||
} | ||
|
||
fn reserve_size(&self) -> usize { | ||
1024 + self.cert_chain_len + self.time_stamp_size | ||
} | ||
|
||
fn cert_chain(&self) -> Result<Vec<Vec<u8>>, RawSignerError> { | ||
Ok(self.cert_chain.clone()) | ||
} | ||
|
||
fn alg(&self) -> SigningAlg { | ||
match self.alg { | ||
RsaSigningAlg::Ps256 => SigningAlg::Ps256, | ||
RsaSigningAlg::Ps384 => SigningAlg::Ps384, | ||
RsaSigningAlg::Ps512 => SigningAlg::Ps512, | ||
} | ||
} | ||
} | ||
|
||
impl TimeStampProvider for RsaSigner { | ||
fn time_stamp_service_url(&self) -> Option<String> { | ||
self.time_stamp_service_url.clone() | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should probably include OCSP size if part of this signer
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mauricefisher64 do we have a way to estimate OCSP size ahead of time?