-
Notifications
You must be signed in to change notification settings - Fork 14
[PM-23653] Add support for ssh keys #375
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
Open
Hinton
wants to merge
2
commits into
main
Choose a base branch
from
cxf/ssh
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -16,4 +16,5 @@ mod api_key; | |
mod card; | ||
mod editable_field; | ||
mod login; | ||
mod ssh; | ||
mod wifi; |
This file contains hidden or 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,99 @@ | ||
use bitwarden_ssh::{error::SshKeyImportError, import::import_der_key}; | ||
use bitwarden_vault::FieldType; | ||
use credential_exchange_format::SshKeyCredential; | ||
|
||
use crate::{cxf::editable_field::create_field, Field, SshKey}; | ||
|
||
/// Convert SSH key credentials to SshKey and custom fields | ||
pub(super) fn to_ssh( | ||
credential: &SshKeyCredential, | ||
) -> Result<(SshKey, Vec<Field>), SshKeyImportError> { | ||
// Convert to OpenSSH format | ||
let encoded_key: Vec<u8> = credential.private_key.as_ref().into(); | ||
let encoded_key = import_der_key(&encoded_key)?; | ||
|
||
let ssh = SshKey { | ||
private_key: encoded_key.private_key, | ||
public_key: encoded_key.public_key, | ||
fingerprint: encoded_key.fingerprint, | ||
}; | ||
|
||
let fields = [ | ||
credential.key_comment.as_ref().map(|comment| Field { | ||
name: Some("Key Comment".into()), | ||
value: Some(comment.into()), | ||
r#type: FieldType::Text as u8, | ||
linked_id: None, | ||
}), | ||
credential | ||
.creation_date | ||
.as_ref() | ||
.map(|date| create_field("Creation Date", date)), | ||
credential | ||
.expiry_date | ||
.as_ref() | ||
.map(|date| create_field("Expiry Date", date)), | ||
credential | ||
.key_generation_source | ||
.as_ref() | ||
.map(|source| create_field("Key Generation Source", source)), | ||
] | ||
.into_iter() | ||
.flatten() | ||
.collect(); | ||
|
||
Ok((ssh, fields)) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use bitwarden_vault::FieldType; | ||
use chrono::NaiveDate; | ||
use credential_exchange_format::EditableFieldDate; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn test_to_ssh() { | ||
let credential = SshKeyCredential { | ||
key_type: "ssh-ed25519".into(), | ||
private_key: "MC4CAQAwBQYDK2VwBCIEID-U9VakauO4Fsv4b_znpDHcdYg74U68siZjnWLPn7Q1" | ||
.try_into() | ||
.unwrap(), | ||
key_comment: Some("Work SSH Key".into()), | ||
creation_date: Some( | ||
EditableFieldDate(NaiveDate::from_ymd_opt(2023, 1, 1).unwrap()).into(), | ||
), | ||
expiry_date: Some( | ||
EditableFieldDate(NaiveDate::from_ymd_opt(2025, 1, 1).unwrap()).into(), | ||
), | ||
key_generation_source: Some("Generated using OpenSSH".to_owned().into()), | ||
}; | ||
|
||
let (ssh, fields) = to_ssh(&credential).unwrap(); | ||
|
||
assert_eq!(ssh.private_key, "-----BEGIN OPENSSH PRIVATE KEY-----\nb3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW\nQyNTUxOQAAACDQiCIk4t4YPC6bOSb7CLzac/vC+ZudqhYqY00cxqr8zAAAAIilFVdupRVX\nbgAAAAtzc2gtZWQyNTUxOQAAACDQiCIk4t4YPC6bOSb7CLzac/vC+ZudqhYqY00cxqr8zA\nAAAEA/lPVWpGrjuBbL+G/856Qx3HWIO+FOvLImY51iz5+0NdCIIiTi3hg8Lps5JvsIvNpz\n+8L5m52qFipjTRzGqvzMAAAAAAECAwQF\n-----END OPENSSH PRIVATE KEY-----\n"); | ||
assert_eq!( | ||
ssh.public_key, | ||
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAINCIIiTi3hg8Lps5JvsIvNpz+8L5m52qFipjTRzGqvzM" | ||
); | ||
assert_eq!( | ||
ssh.fingerprint, | ||
"SHA256:mZ0BOhUVicE81yPEpFJrv1rEXB2R3Y3t5nh/riicTvs" | ||
); | ||
|
||
assert_eq!(fields.len(), 4); | ||
assert_eq!( | ||
fields[0], | ||
Field { | ||
name: Some("Key Comment".to_string()), | ||
value: Some("Work SSH Key".to_string()), | ||
r#type: FieldType::Text as u8, | ||
linked_id: None, | ||
} | ||
); | ||
assert_eq!(fields[1].value.as_deref(), Some("2023-01-01")); | ||
assert_eq!(fields[2].value.as_deref(), Some("2025-01-01")); | ||
assert_eq!(fields[3].value.as_deref(), Some("Generated using OpenSSH")); | ||
} | ||
} |
This file contains hidden or 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 hidden or 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
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.
Note (non-actionable): In the agent, we treat the name of the cipher as the comment, and advertise it as such. We may want to research (not here) how other clients treat comment vs name, to not disrupt user experience. If someone comes over from another product which uses a separate key comment field, suddenly all their ciphers may be named something different.
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.
I think we will have a lot of follow up work as other credential managers implements credential exchange and we discover edge cases ๐ฐ