-
Notifications
You must be signed in to change notification settings - Fork 42
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
Adding functions and types to calculate AUTH and ID blocks #139
Merged
+1,158
−47
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
//! Functions to use to calculate the ID-BLOCK and the AUTH-BLOCK. | ||
|
||
use bincode; | ||
use openssl::{ec::EcKey, pkey::Private, sha::sha384}; | ||
use std::{ | ||
convert::{TryFrom, TryInto}, | ||
fs::File, | ||
io::Read, | ||
path::PathBuf, | ||
}; | ||
|
||
use crate::{ | ||
error::IdBlockError, | ||
measurement::idblock_types::{ | ||
FamilyId, IdAuth, IdBlock, IdBlockLaunchDigest, IdMeasurements, ImageId, SevEcdsaPubKey, | ||
SevEcdsaSig, CURVE_P384_NID, | ||
}, | ||
}; | ||
|
||
/// Generate an AUTH-BLOCK using 2 EC P-384 keys and an already calculated ID-BlOCK | ||
pub fn gen_id_auth_block( | ||
id_block: &IdBlock, | ||
id_key_file: PathBuf, | ||
author_key_file: PathBuf, | ||
) -> Result<IdAuth, IdBlockError> { | ||
let id_ec_priv_key = load_priv_key(id_key_file)?; | ||
let id_ec_pub_key = SevEcdsaPubKey::try_from(&id_ec_priv_key)?; | ||
let id_sig = SevEcdsaSig::try_from(( | ||
id_ec_priv_key, | ||
bincode::serialize(id_block) | ||
.map_err(|e| IdBlockError::BincodeError(*e))? | ||
.as_slice(), | ||
))?; | ||
|
||
let author_ec_priv_key = load_priv_key(author_key_file)?; | ||
let author_pub_key = SevEcdsaPubKey::try_from(&author_ec_priv_key)?; | ||
let author_sig = SevEcdsaSig::try_from(( | ||
author_ec_priv_key, | ||
bincode::serialize(&id_ec_pub_key) | ||
.map_err(|e| IdBlockError::BincodeError(*e))? | ||
.as_slice(), | ||
))?; | ||
|
||
Ok(IdAuth::new( | ||
None, | ||
None, | ||
id_sig, | ||
id_ec_pub_key, | ||
author_sig, | ||
author_pub_key, | ||
)) | ||
} | ||
|
||
enum KeyFormat { | ||
Pem, | ||
Der, | ||
} | ||
|
||
/// Identifies the format of a key based upon the first twenty-seven | ||
/// bytes of a byte stream. A non-PEM format assumes DER format. | ||
fn identify_priv_key_format(bytes: &[u8]) -> KeyFormat { | ||
const PEM_START: &[u8] = b"-----BEGIN PRIVATE KEY-----"; | ||
match &bytes[0..27] { | ||
PEM_START => KeyFormat::Pem, | ||
_ => KeyFormat::Der, | ||
} | ||
} | ||
///Read a key file and return a private EcKey. | ||
/// Key has to be an EC P-384 key. | ||
pub fn load_priv_key(path: PathBuf) -> Result<EcKey<Private>, IdBlockError> { | ||
let mut key_data = Vec::new(); | ||
let mut file = match File::open(path) { | ||
Ok(file) => file, | ||
Err(e) => return Err(IdBlockError::FileError(e)), | ||
}; | ||
|
||
file.read_to_end(&mut key_data) | ||
.map_err(IdBlockError::FileError)?; | ||
|
||
let pkey = match identify_priv_key_format(&key_data) { | ||
KeyFormat::Pem => { | ||
EcKey::private_key_from_pem(&key_data).map_err(IdBlockError::CryptoErrorStack)? | ||
} | ||
KeyFormat::Der => { | ||
EcKey::private_key_from_der(&key_data).map_err(IdBlockError::CryptoErrorStack)? | ||
} | ||
}; | ||
|
||
pkey.check_key().map_err(IdBlockError::CryptoErrorStack)?; | ||
|
||
if let Some(name) = pkey.group().curve_name() { | ||
if name != CURVE_P384_NID { | ||
return Err(IdBlockError::SevCurveError()); | ||
}; | ||
}; | ||
|
||
Ok(pkey) | ||
} | ||
|
||
/// Generate the sha384 digest of the provided pem key | ||
pub fn generate_key_digest(key_path: PathBuf) -> Result<IdBlockLaunchDigest, IdBlockError> { | ||
let ec_key = load_priv_key(key_path)?; | ||
|
||
let pub_key = SevEcdsaPubKey::try_from(&ec_key)?; | ||
|
||
Ok(IdBlockLaunchDigest::new( | ||
sha384( | ||
bincode::serialize(&pub_key) | ||
.map_err(|e| IdBlockError::BincodeError(*e))? | ||
.as_slice(), | ||
) | ||
.try_into()?, | ||
)) | ||
} | ||
|
||
/// Calculate the different pieces needed for a complete pre-attestation. | ||
/// ID-BLOCK, AUTH-BLOCK, id-key digest and auth-key digest. | ||
pub fn snp_calculate_id( | ||
ld: Option<IdBlockLaunchDigest>, | ||
family_id: Option<FamilyId>, | ||
image_id: Option<ImageId>, | ||
svn: Option<u32>, | ||
policy: Option<u64>, | ||
id_key_file: PathBuf, | ||
auth_key_file: PathBuf, | ||
) -> Result<IdMeasurements, IdBlockError> { | ||
let id_block = IdBlock::new(ld, family_id, image_id, svn, policy)?; | ||
|
||
Ok(IdMeasurements { | ||
id_block, | ||
id_auth: gen_id_auth_block(&id_block, id_key_file.clone(), auth_key_file.clone())?, | ||
|
||
id_key_digest: generate_key_digest(id_key_file)?, | ||
|
||
auth_key_digest: generate_key_digest(auth_key_file)?, | ||
}) | ||
} |
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.
Can't these be converted by
map_err()
? Is theFrom
implementation necessary?