Last active
July 12, 2019 05:07
-
-
Save takuan-osho/4df2db2760d4f870363b4a4e1898122e to your computer and use it in GitHub Desktop.
ssh.ParsePrivateKeyWithPassphrase (https://godoc.org/golang.org/x/crypto/ssh#ParsePrivateKeyWithPassphrase) の動作確認用スクリプト
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
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
"log" | |
"os" | |
"golang.org/x/crypto/ssh" | |
) | |
func main() { | |
sshPrivateKeyPath := os.Args[1] | |
keyData, err := ioutil.ReadFile(sshPrivateKeyPath) | |
if err != nil { | |
log.Fatal(err) | |
} | |
signer, err := ssh.ParsePrivateKey([]byte(keyData)) | |
if err == nil { | |
fmt.Println(signer) | |
os.Exit(0) | |
} | |
passPhrase := os.Getenv("PASSPHRASE") | |
if passPhrase == "" { | |
fmt.Println("Please configure `PASSPHRASE` environment variable for your passphrase") | |
os.Exit(1) | |
} | |
signer, err = ssh.ParsePrivateKeyWithPassphrase(keyData, []byte(passPhrase)) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Println("Parsed private key with passphrase!!") | |
fmt.Println(signer) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
delveを使ってデバッグしてみたところ、パスフレーズがあるときはここでエラーが出ていた
https://github.com/golang/crypto/blob/4def268fd1a49955bfb3dda92fe3db4f924f2285/ssh/keys.go#L990-L992
w.CipherName
がaes256-ctr
,w.KdfName
がbcrypt
という値になっていてエラーになっていた。ここで上記の値になっているのか真っ当な状態なのかどうか、それともgolang/x/crypto/ssh パッケージの上記の処理がよろしくないのかの判別が付かない。
delveをCLIで起動してエラー処理直前の時に
w.CipherName
がaes256-ctr
,w.KdfName
がbcrypt
という値になっている状況を示したのが以下のデバッグ時のコード。