diff options
| author | Adam Langley <agl@golang.org> | 2018-08-31 15:38:59 -0700 |
|---|---|---|
| committer | Adam Langley <agl@golang.org> | 2018-09-04 16:38:35 +0000 |
| commit | 0709b304e793a5edb4a2c0145f281ecdc20838a4 (patch) | |
| tree | 9f46c5cf38a843e891d9c715145d1e64091dbcea /ssh/keys_test.go | |
| parent | 182538f80094b6a8efaade63a8fd8e0d9d5843dd (diff) | |
| download | go-x-crypto-0709b304e793a5edb4a2c0145f281ecdc20838a4.tar.xz | |
ssh: don't panic if a key is too short.
Change-Id: I810eb1c5d4cacc710a427e2ce031db1e9c292454
Reviewed-on: https://go-review.googlesource.com/132656
Run-TryBot: Adam Langley <agl@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Filippo Valsorda <filippo@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Diffstat (limited to 'ssh/keys_test.go')
| -rw-r--r-- | ssh/keys_test.go | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/ssh/keys_test.go b/ssh/keys_test.go index 9a90abc..f28725f 100644 --- a/ssh/keys_test.go +++ b/ssh/keys_test.go @@ -13,7 +13,9 @@ import ( "crypto/rsa" "crypto/x509" "encoding/base64" + "encoding/pem" "fmt" + "io" "reflect" "strings" "testing" @@ -498,3 +500,32 @@ func TestFingerprintSHA256(t *testing.T) { t.Errorf("got fingerprint %q want %q", fingerprint, want) } } + +func TestInvalidKeys(t *testing.T) { + keyTypes := []string{ + "RSA PRIVATE KEY", + "PRIVATE KEY", + "EC PRIVATE KEY", + "DSA PRIVATE KEY", + "OPENSSH PRIVATE KEY", + } + + for _, keyType := range keyTypes { + for _, dataLen := range []int{0, 1, 2, 5, 10, 20} { + data := make([]byte, dataLen) + if _, err := io.ReadFull(rand.Reader, data); err != nil { + t.Fatal(err) + } + + var buf bytes.Buffer + pem.Encode(&buf, &pem.Block{ + Type: keyType, + Bytes: data, + }) + + // This test is just to ensure that the function + // doesn't panic so the return value is ignored. + ParseRawPrivateKey(buf.Bytes()) + } + } +} |
