@@ -34,7 +34,31 @@ pub fn import_key(
34
34
35
35
/// Import a DER encoded private key, and returns a decoded [SshKeyView]. This is primarily used for
36
36
/// importing SSH keys from other Credential Managers through Credential Exchange.
37
- pub fn import_der_key ( encoded_key : & [ u8 ] ) -> Result < SshKeyView , SshKeyImportError > {
37
+ pub fn import_pkcs8_der ( encoded_key : & [ u8 ] ) -> Result < SshKeyView , SshKeyImportError > {
38
+ import_der_key ( encoded_key)
39
+ }
40
+
41
+ fn import_pkcs8_key (
42
+ encoded_key : String ,
43
+ password : Option < String > ,
44
+ ) -> Result < SshKeyView , SshKeyImportError > {
45
+ let doc = if let Some ( password) = password {
46
+ SecretDocument :: from_pkcs8_encrypted_pem ( & encoded_key, password. as_bytes ( ) ) . map_err (
47
+ |err| match err {
48
+ pkcs8:: Error :: EncryptedPrivateKey ( pkcs5:: Error :: DecryptFailed ) => {
49
+ SshKeyImportError :: WrongPassword
50
+ }
51
+ _ => SshKeyImportError :: ParsingError ,
52
+ } ,
53
+ ) ?
54
+ } else {
55
+ SecretDocument :: from_pkcs8_pem ( & encoded_key) . map_err ( |_| SshKeyImportError :: ParsingError ) ?
56
+ } ;
57
+
58
+ import_der_key ( doc. as_bytes ( ) )
59
+ }
60
+
61
+ fn import_der_key ( encoded_key : & [ u8 ] ) -> Result < SshKeyView , SshKeyImportError > {
38
62
let private_key_info =
39
63
PrivateKeyInfo :: from_der ( encoded_key) . map_err ( |_| SshKeyImportError :: ParsingError ) ?;
40
64
@@ -61,26 +85,6 @@ pub fn import_der_key(encoded_key: &[u8]) -> Result<SshKeyView, SshKeyImportErro
61
85
ssh_private_key_to_view ( private_key) . map_err ( |_| SshKeyImportError :: ParsingError )
62
86
}
63
87
64
- fn import_pkcs8_key (
65
- encoded_key : String ,
66
- password : Option < String > ,
67
- ) -> Result < SshKeyView , SshKeyImportError > {
68
- let doc = if let Some ( password) = password {
69
- SecretDocument :: from_pkcs8_encrypted_pem ( & encoded_key, password. as_bytes ( ) ) . map_err (
70
- |err| match err {
71
- pkcs8:: Error :: EncryptedPrivateKey ( pkcs5:: Error :: DecryptFailed ) => {
72
- SshKeyImportError :: WrongPassword
73
- }
74
- _ => SshKeyImportError :: ParsingError ,
75
- } ,
76
- ) ?
77
- } else {
78
- SecretDocument :: from_pkcs8_pem ( & encoded_key) . map_err ( |_| SshKeyImportError :: ParsingError ) ?
79
- } ;
80
-
81
- import_der_key ( doc. as_bytes ( ) )
82
- }
83
-
84
88
fn import_openssh_key (
85
89
encoded_key : String ,
86
90
password : Option < String > ,
0 commit comments