keys_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package ssh
  2. import (
  3. "crypto/dsa"
  4. "crypto/ecdsa"
  5. "crypto/elliptic"
  6. "crypto/rand"
  7. "crypto/rsa"
  8. "reflect"
  9. "strings"
  10. "testing"
  11. )
  12. var ecdsaKey Signer
  13. func rawKey(pub PublicKey) interface{} {
  14. switch k := pub.(type) {
  15. case *rsaPublicKey:
  16. return (*rsa.PublicKey)(k)
  17. case *dsaPublicKey:
  18. return (*dsa.PublicKey)(k)
  19. case *ecdsaPublicKey:
  20. return (*ecdsa.PublicKey)(k)
  21. }
  22. panic("unknown key type")
  23. }
  24. func TestKeyMarshalParse(t *testing.T) {
  25. keys := []Signer{rsaKey, dsaKey, ecdsaKey}
  26. for _, priv := range keys {
  27. pub := priv.PublicKey()
  28. roundtrip, rest, ok := ParsePublicKey(MarshalPublicKey(pub))
  29. if !ok {
  30. t.Errorf("ParsePublicKey(%T) failed", pub)
  31. }
  32. if len(rest) > 0 {
  33. t.Errorf("ParsePublicKey(%T): trailing junk", pub)
  34. }
  35. k1 := rawKey(pub)
  36. k2 := rawKey(roundtrip)
  37. if !reflect.DeepEqual(k1, k2) {
  38. t.Errorf("got %#v in roundtrip, want %#v", k2, k1)
  39. }
  40. }
  41. }
  42. func TestUnsupportedCurves(t *testing.T) {
  43. raw, err := ecdsa.GenerateKey(elliptic.P224(), rand.Reader)
  44. if err != nil {
  45. t.Fatalf("GenerateKey: %v", err)
  46. }
  47. if _, err = NewSignerFromKey(raw); err == nil || !strings.Contains(err.Error(), "only P256") {
  48. t.Fatalf("NewPrivateKey should not succeed with P224, got: %v", err)
  49. }
  50. if _, err = NewPublicKey(&raw.PublicKey); err == nil || !strings.Contains(err.Error(), "only P256") {
  51. t.Fatalf("NewPublicKey should not succeed with P224, got: %v", err)
  52. }
  53. }
  54. func TestNewPublicKey(t *testing.T) {
  55. keys := []Signer{rsaKey, dsaKey, ecdsaKey}
  56. for _, k := range keys {
  57. raw := rawKey(k.PublicKey())
  58. pub, err := NewPublicKey(raw)
  59. if err != nil {
  60. t.Errorf("NewPublicKey(%#v): %v", raw, err)
  61. }
  62. if !reflect.DeepEqual(k.PublicKey(), pub) {
  63. t.Errorf("NewPublicKey(%#v) = %#v, want %#v", raw, pub, k.PublicKey())
  64. }
  65. }
  66. }
  67. func TestKeySignVerify(t *testing.T) {
  68. keys := []Signer{rsaKey, dsaKey, ecdsaKey}
  69. for _, priv := range keys {
  70. pub := priv.PublicKey()
  71. data := []byte("sign me")
  72. sig, err := priv.Sign(rand.Reader, data)
  73. if err != nil {
  74. t.Fatalf("Sign(%T): %v", priv, err)
  75. }
  76. if !pub.Verify(data, sig) {
  77. t.Errorf("publicKey.Verify(%T) failed", priv)
  78. }
  79. }
  80. }
  81. func TestParseRSAPrivateKey(t *testing.T) {
  82. key, err := ParsePrivateKey([]byte(testServerPrivateKey))
  83. if err != nil {
  84. t.Fatalf("ParsePrivateKey: %v", err)
  85. }
  86. rsa, ok := key.(*rsaPrivateKey)
  87. if !ok {
  88. t.Fatalf("got %T, want *rsa.PrivateKey", rsa)
  89. }
  90. if err := rsa.Validate(); err != nil {
  91. t.Errorf("Validate: %v", err)
  92. }
  93. }
  94. func TestParseECPrivateKey(t *testing.T) {
  95. // Taken from the data in test/ .
  96. pem := []byte(`-----BEGIN EC PRIVATE KEY-----
  97. MHcCAQEEINGWx0zo6fhJ/0EAfrPzVFyFC9s18lBt3cRoEDhS3ARooAoGCCqGSM49
  98. AwEHoUQDQgAEi9Hdw6KvZcWxfg2IDhA7UkpDtzzt6ZqJXSsFdLd+Kx4S3Sx4cVO+
  99. 6/ZOXRnPmNAlLUqjShUsUBBngG0u2fqEqA==
  100. -----END EC PRIVATE KEY-----`)
  101. key, err := ParsePrivateKey(pem)
  102. if err != nil {
  103. t.Fatalf("ParsePrivateKey: %v", err)
  104. }
  105. ecKey, ok := key.(*ecdsaPrivateKey)
  106. if !ok {
  107. t.Fatalf("got %T, want *ecdsaPrivateKey", ecKey)
  108. }
  109. if !validateECPublicKey(ecKey.Curve, ecKey.X, ecKey.Y) {
  110. t.Fatalf("public key does not validate.")
  111. }
  112. }
  113. func init() {
  114. raw, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
  115. ecdsaKey, _ = NewSignerFromKey(raw)
  116. }