Skip to content
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

A simple way to decode an SSH key from a seed and its type #37

Merged
merged 2 commits into from
Nov 15, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/keys.ml
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,16 @@ let of_seed typ seed =
Log.info (fun m -> m "using ssh-ed25519 %s"
(Cstruct.to_string pubkey |> Base64.encode_string));
Hostkey.Ed25519_priv priv

let of_string str =
match String.split_on_char ':' str with
| [ typ; data; ] ->
( match typ_of_string typ, Base64.decode data with
| Ok `Rsa, Ok seed -> Ok (of_seed `Rsa seed)
| Ok `Ed25519, Ok key ->
( match Mirage_crypto_ec.Ed25519.priv_of_cstruct (Cstruct.of_string key) with
| Ok key -> Ok (Hostkey.Ed25519_priv key)
| Error err -> Error (`Msg (Fmt.str "%a" Mirage_crypto_ec.pp_error err)) )
| Error _, _ -> Error (`Msg "Invalid type of SSH key")
| _, Error _ -> Error (`Msg "Invalid b64 key seed") )
| _ -> Error (`Msg "Invalid SSH key format (type:b64-seed)")