This repository has been archived by the owner on Jun 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
yubihsm: Support for exporting/importing wrapped (encrypted) keys #197
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 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,74 @@ | ||
use super::*; | ||
use abscissa::Callable; | ||
use std::{fs::OpenOptions, io::Write, os::unix::fs::OpenOptionsExt, path::PathBuf, process}; | ||
use subtle_encoding::base64; | ||
|
||
/// The `yubihsm keys export` subcommand: create encrypted backups of keys | ||
#[derive(Debug, Default, Options)] | ||
pub struct ExportCommand { | ||
/// Path to configuration file | ||
#[options(short = "c", long = "config")] | ||
pub config: Option<String>, | ||
|
||
/// ID of the key to export | ||
#[options(short = "i", long = "id")] | ||
pub key_id: u16, | ||
|
||
/// ID of the wrap key to encrypt the exported key under | ||
#[options(short = "w", long = "wrapkey")] | ||
pub wrap_key_id: Option<u16>, | ||
|
||
/// Path to write the resulting file to | ||
#[options(free)] | ||
pub path: PathBuf, | ||
} | ||
|
||
impl Callable for ExportCommand { | ||
fn call(&self) { | ||
let wrap_key_id = self.wrap_key_id.unwrap_or(DEFAULT_WRAP_KEY); | ||
|
||
let wrapped_bytes = crate::yubihsm::client() | ||
.export_wrapped( | ||
wrap_key_id, | ||
yubihsm::object::Type::AsymmetricKey, | ||
self.key_id, | ||
) | ||
.unwrap_or_else(|e| { | ||
status_err!( | ||
"couldn't export key {} under wrap key {}: {}", | ||
self.key_id, | ||
wrap_key_id, | ||
e | ||
); | ||
process::exit(1); | ||
}); | ||
|
||
let mut export_file = OpenOptions::new() | ||
.create(true) | ||
.write(true) | ||
.truncate(true) | ||
.mode(0o600) | ||
.open(&self.path) | ||
.unwrap_or_else(|e| { | ||
status_err!("couldn't export to {} ({})", &self.path.display(), e); | ||
process::exit(1); | ||
}); | ||
|
||
export_file | ||
.write_all(&base64::encode(&wrapped_bytes.into_vec())) | ||
.unwrap_or_else(|e| { | ||
status_err!("error exporting {}: {}", &self.path.display(), e); | ||
process::exit(1); | ||
}); | ||
|
||
status_ok!( | ||
"Exported", | ||
"key 0x{:04x} (encrypted under wrap key 0x{:04x}) to {}", | ||
self.key_id, | ||
wrap_key_id, | ||
self.path.display() | ||
); | ||
} | ||
} | ||
|
||
impl_command!(ExportCommand); |
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 |
---|---|---|
|
@@ -119,6 +119,7 @@ impl Callable for GenerateCommand { | |
impl_command!(GenerateCommand); | ||
|
||
/// Create an encrypted backup of this key under the given wrap key ID | ||
// TODO(tarcieri): unify this with the similar code in export? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would definitely be nice to do this before landing this PR as it's otherwise code duplication. |
||
fn create_encrypted_backup( | ||
hsm: &yubihsm::Client, | ||
key_id: yubihsm::object::Id, | ||
|
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.
Bumped this to the final release (no changes from v0.22.0-alpha2 other than the version bump):
tendermint/yubihsm-rs#194