Created
July 25, 2012 04:11
-
-
Save mogigoma/3174341 to your computer and use it in GitHub Desktop.
Parse the binary portion of an SSH that's already decoded from Base64.
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
(require 'bindat) | |
(require 'format-spec) | |
(defvar dsa-pubkey-format-spec | |
"Parameter P: %p\nParameter Q: %q\nParameter G: %g\nParameter Y: %y\n" | |
"Output format for DSA public keys.") | |
(defvar dsa-pubkey-bindat-spec | |
'((:p-len u32) | |
(:p vec (:p-len)) | |
(:q-len u32) | |
(:q vec (:q-len)) | |
(:g-len u32) | |
(:g vec (:g-len)) | |
(:y-len u32) | |
(:y vec (:y-len))) | |
"Description of a DSA public key's format.") | |
(defvar rsa-pubkey-format-spec | |
"Exponent: %e\nModulus: %n\n" | |
"Output format for RSA public keys.") | |
(defvar rsa-pubkey-bindat-spec | |
'((:e-len u32) | |
(:e vec (:e-len)) | |
(:n-len u32) | |
(:n vec (:n-len))) | |
"Description of an RSA public key's format.") | |
(defvar openssh-pubkey-bindat-spec | |
'((:alg-name-len u32) | |
(:alg-name str (:alg-name-len)) | |
(union (:alg-name) | |
("dsa" (struct dsa-pubkey-bindat-spec)) | |
("ssh-dsa" (struct dsa-pubkey-bindat-spec)) | |
("rsa" (struct rsa-pubkey-bindat-spec)) | |
("ssh-rsa" (struct rsa-pubkey-bindat-spec)))) | |
"Description of an OpenSSH public key's format.") | |
(defun parse-openssh-pubkey (file) | |
(interactive "fPublic key file: ") | |
(let* ((key (with-temp-buffer | |
(set-buffer-multibyte nil) | |
(insert-file-contents-literally file) | |
(bindat-unpack openssh-pubkey-bindat-spec (buffer-string))))) | |
(switch-to-buffer | |
(get-buffer-create | |
(format "*OpenSSH Public Key: %s*" (file-name-nondirectory file)))) | |
(erase-buffer) | |
(let ((alg (bindat-get-field key :alg-name))) | |
(insert (format "Algorithm: %s\n" alg)) | |
(insert (apply 'format-spec (cond | |
((member alg '("dsa" "ssh-dsa")) | |
(list dsa-pubkey-format-spec | |
(format-spec-make | |
?p (bindat-get-field key :p) | |
?q (bindat-get-field key :q) | |
?g (bindat-get-field key :g) | |
?y (bindat-get-field key :y)))) | |
((member alg '("rsa" "ssh-rsa")) | |
(list rsa-pubkey-format-spec | |
(format-spec-make | |
?e (bindat-get-field key :e) | |
?n (bindat-get-field key :n)))) | |
(t | |
(list "Unknown key format '%f'." | |
(format-spec-make ?f alg))))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment