Skip to content

Instantly share code, notes, and snippets.

@mogigoma
Created July 25, 2012 04:11
Show Gist options
  • Save mogigoma/3174341 to your computer and use it in GitHub Desktop.
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.
(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