keys_test.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. // Copyright 2014 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package ssh
  5. import (
  6. "bytes"
  7. "crypto/dsa"
  8. "crypto/ecdsa"
  9. "crypto/elliptic"
  10. "crypto/rand"
  11. "crypto/rsa"
  12. "crypto/x509"
  13. "encoding/base64"
  14. "encoding/hex"
  15. "encoding/pem"
  16. "fmt"
  17. "io"
  18. "reflect"
  19. "strings"
  20. "testing"
  21. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/crypto/ssh/testdata"
  22. "golang.org/x/crypto/ed25519"
  23. )
  24. func rawKey(pub PublicKey) interface{} {
  25. switch k := pub.(type) {
  26. case *rsaPublicKey:
  27. return (*rsa.PublicKey)(k)
  28. case *dsaPublicKey:
  29. return (*dsa.PublicKey)(k)
  30. case *ecdsaPublicKey:
  31. return (*ecdsa.PublicKey)(k)
  32. case ed25519PublicKey:
  33. return (ed25519.PublicKey)(k)
  34. case *Certificate:
  35. return k
  36. }
  37. panic("unknown key type")
  38. }
  39. func TestKeyMarshalParse(t *testing.T) {
  40. for _, priv := range testSigners {
  41. pub := priv.PublicKey()
  42. roundtrip, err := ParsePublicKey(pub.Marshal())
  43. if err != nil {
  44. t.Errorf("ParsePublicKey(%T): %v", pub, err)
  45. }
  46. k1 := rawKey(pub)
  47. k2 := rawKey(roundtrip)
  48. if !reflect.DeepEqual(k1, k2) {
  49. t.Errorf("got %#v in roundtrip, want %#v", k2, k1)
  50. }
  51. }
  52. }
  53. func TestUnsupportedCurves(t *testing.T) {
  54. raw, err := ecdsa.GenerateKey(elliptic.P224(), rand.Reader)
  55. if err != nil {
  56. t.Fatalf("GenerateKey: %v", err)
  57. }
  58. if _, err = NewSignerFromKey(raw); err == nil || !strings.Contains(err.Error(), "only P-256") {
  59. t.Fatalf("NewPrivateKey should not succeed with P-224, got: %v", err)
  60. }
  61. if _, err = NewPublicKey(&raw.PublicKey); err == nil || !strings.Contains(err.Error(), "only P-256") {
  62. t.Fatalf("NewPublicKey should not succeed with P-224, got: %v", err)
  63. }
  64. }
  65. func TestNewPublicKey(t *testing.T) {
  66. for _, k := range testSigners {
  67. raw := rawKey(k.PublicKey())
  68. // Skip certificates, as NewPublicKey does not support them.
  69. if _, ok := raw.(*Certificate); ok {
  70. continue
  71. }
  72. pub, err := NewPublicKey(raw)
  73. if err != nil {
  74. t.Errorf("NewPublicKey(%#v): %v", raw, err)
  75. }
  76. if !reflect.DeepEqual(k.PublicKey(), pub) {
  77. t.Errorf("NewPublicKey(%#v) = %#v, want %#v", raw, pub, k.PublicKey())
  78. }
  79. }
  80. }
  81. func TestKeySignVerify(t *testing.T) {
  82. for _, priv := range testSigners {
  83. pub := priv.PublicKey()
  84. data := []byte("sign me")
  85. sig, err := priv.Sign(rand.Reader, data)
  86. if err != nil {
  87. t.Fatalf("Sign(%T): %v", priv, err)
  88. }
  89. if err := pub.Verify(data, sig); err != nil {
  90. t.Errorf("publicKey.Verify(%T): %v", priv, err)
  91. }
  92. sig.Blob[5]++
  93. if err := pub.Verify(data, sig); err == nil {
  94. t.Errorf("publicKey.Verify on broken sig did not fail")
  95. }
  96. }
  97. }
  98. func TestKeySignWithAlgorithmVerify(t *testing.T) {
  99. for _, priv := range testSigners {
  100. if algorithmSigner, ok := priv.(AlgorithmSigner); !ok {
  101. t.Errorf("Signers constructed by ssh package should always implement the AlgorithmSigner interface: %T", priv)
  102. } else {
  103. pub := priv.PublicKey()
  104. data := []byte("sign me")
  105. signWithAlgTestCase := func(algorithm string, expectedAlg string) {
  106. sig, err := algorithmSigner.SignWithAlgorithm(rand.Reader, data, algorithm)
  107. if err != nil {
  108. t.Fatalf("Sign(%T): %v", priv, err)
  109. }
  110. if sig.Format != expectedAlg {
  111. t.Errorf("signature format did not match requested signature algorithm: %s != %s", sig.Format, expectedAlg)
  112. }
  113. if err := pub.Verify(data, sig); err != nil {
  114. t.Errorf("publicKey.Verify(%T): %v", priv, err)
  115. }
  116. sig.Blob[5]++
  117. if err := pub.Verify(data, sig); err == nil {
  118. t.Errorf("publicKey.Verify on broken sig did not fail")
  119. }
  120. }
  121. // Using the empty string as the algorithm name should result in the same signature format as the algorithm-free Sign method.
  122. defaultSig, err := priv.Sign(rand.Reader, data)
  123. if err != nil {
  124. t.Fatalf("Sign(%T): %v", priv, err)
  125. }
  126. signWithAlgTestCase("", defaultSig.Format)
  127. // RSA keys are the only ones which currently support more than one signing algorithm
  128. if pub.Type() == KeyAlgoRSA {
  129. for _, algorithm := range []string{SigAlgoRSA, SigAlgoRSASHA2256, SigAlgoRSASHA2512} {
  130. signWithAlgTestCase(algorithm, algorithm)
  131. }
  132. }
  133. }
  134. }
  135. }
  136. func TestParseRSAPrivateKey(t *testing.T) {
  137. key := testPrivateKeys["rsa"]
  138. rsa, ok := key.(*rsa.PrivateKey)
  139. if !ok {
  140. t.Fatalf("got %T, want *rsa.PrivateKey", rsa)
  141. }
  142. if err := rsa.Validate(); err != nil {
  143. t.Errorf("Validate: %v", err)
  144. }
  145. }
  146. func TestParseECPrivateKey(t *testing.T) {
  147. key := testPrivateKeys["ecdsa"]
  148. ecKey, ok := key.(*ecdsa.PrivateKey)
  149. if !ok {
  150. t.Fatalf("got %T, want *ecdsa.PrivateKey", ecKey)
  151. }
  152. if !validateECPublicKey(ecKey.Curve, ecKey.X, ecKey.Y) {
  153. t.Fatalf("public key does not validate.")
  154. }
  155. }
  156. func TestParseEncryptedPrivateKeysWithPassphrase(t *testing.T) {
  157. data := []byte("sign me")
  158. for _, tt := range testdata.PEMEncryptedKeys {
  159. t.Run(tt.Name, func(t *testing.T) {
  160. _, err := ParsePrivateKeyWithPassphrase(tt.PEMBytes, []byte("incorrect"))
  161. if err != x509.IncorrectPasswordError {
  162. t.Errorf("got %v want IncorrectPasswordError", err)
  163. }
  164. s, err := ParsePrivateKeyWithPassphrase(tt.PEMBytes, []byte(tt.EncryptionKey))
  165. if err != nil {
  166. t.Fatalf("ParsePrivateKeyWithPassphrase returned error: %s", err)
  167. }
  168. sig, err := s.Sign(rand.Reader, data)
  169. if err != nil {
  170. t.Fatalf("Signer.Sign: %v", err)
  171. }
  172. if err := s.PublicKey().Verify(data, sig); err != nil {
  173. t.Errorf("Verify failed: %v", err)
  174. }
  175. _, err = ParsePrivateKey(tt.PEMBytes)
  176. if err == nil {
  177. t.Fatalf("ParsePrivateKey succeeded, expected an error")
  178. }
  179. if err, ok := err.(*PassphraseMissingError); !ok {
  180. t.Errorf("got error %q, want PassphraseMissingError", err)
  181. } else if tt.IncludesPublicKey {
  182. if err.PublicKey == nil {
  183. t.Fatalf("expected PassphraseMissingError.PublicKey not to be nil")
  184. }
  185. got, want := err.PublicKey.Marshal(), s.PublicKey().Marshal()
  186. if !bytes.Equal(got, want) {
  187. t.Errorf("error field %q doesn't match signer public key %q", got, want)
  188. }
  189. }
  190. })
  191. }
  192. }
  193. func TestParseDSA(t *testing.T) {
  194. // We actually exercise the ParsePrivateKey codepath here, as opposed to
  195. // using the ParseRawPrivateKey+NewSignerFromKey path that testdata_test.go
  196. // uses.
  197. s, err := ParsePrivateKey(testdata.PEMBytes["dsa"])
  198. if err != nil {
  199. t.Fatalf("ParsePrivateKey returned error: %s", err)
  200. }
  201. data := []byte("sign me")
  202. sig, err := s.Sign(rand.Reader, data)
  203. if err != nil {
  204. t.Fatalf("dsa.Sign: %v", err)
  205. }
  206. if err := s.PublicKey().Verify(data, sig); err != nil {
  207. t.Errorf("Verify failed: %v", err)
  208. }
  209. }
  210. // Tests for authorized_keys parsing.
  211. // getTestKey returns a public key, and its base64 encoding.
  212. func getTestKey() (PublicKey, string) {
  213. k := testPublicKeys["rsa"]
  214. b := &bytes.Buffer{}
  215. e := base64.NewEncoder(base64.StdEncoding, b)
  216. e.Write(k.Marshal())
  217. e.Close()
  218. return k, b.String()
  219. }
  220. func TestMarshalParsePublicKey(t *testing.T) {
  221. pub, pubSerialized := getTestKey()
  222. line := fmt.Sprintf("%s %s user@host", pub.Type(), pubSerialized)
  223. authKeys := MarshalAuthorizedKey(pub)
  224. actualFields := strings.Fields(string(authKeys))
  225. if len(actualFields) == 0 {
  226. t.Fatalf("failed authKeys: %v", authKeys)
  227. }
  228. // drop the comment
  229. expectedFields := strings.Fields(line)[0:2]
  230. if !reflect.DeepEqual(actualFields, expectedFields) {
  231. t.Errorf("got %v, expected %v", actualFields, expectedFields)
  232. }
  233. actPub, _, _, _, err := ParseAuthorizedKey([]byte(line))
  234. if err != nil {
  235. t.Fatalf("cannot parse %v: %v", line, err)
  236. }
  237. if !reflect.DeepEqual(actPub, pub) {
  238. t.Errorf("got %v, expected %v", actPub, pub)
  239. }
  240. }
  241. type testAuthResult struct {
  242. pubKey PublicKey
  243. options []string
  244. comments string
  245. rest string
  246. ok bool
  247. }
  248. func testAuthorizedKeys(t *testing.T, authKeys []byte, expected []testAuthResult) {
  249. rest := authKeys
  250. var values []testAuthResult
  251. for len(rest) > 0 {
  252. var r testAuthResult
  253. var err error
  254. r.pubKey, r.comments, r.options, rest, err = ParseAuthorizedKey(rest)
  255. r.ok = (err == nil)
  256. t.Log(err)
  257. r.rest = string(rest)
  258. values = append(values, r)
  259. }
  260. if !reflect.DeepEqual(values, expected) {
  261. t.Errorf("got %#v, expected %#v", values, expected)
  262. }
  263. }
  264. func TestAuthorizedKeyBasic(t *testing.T) {
  265. pub, pubSerialized := getTestKey()
  266. line := "ssh-rsa " + pubSerialized + " user@host"
  267. testAuthorizedKeys(t, []byte(line),
  268. []testAuthResult{
  269. {pub, nil, "user@host", "", true},
  270. })
  271. }
  272. func TestAuth(t *testing.T) {
  273. pub, pubSerialized := getTestKey()
  274. authWithOptions := []string{
  275. `# comments to ignore before any keys...`,
  276. ``,
  277. `env="HOME=/home/root",no-port-forwarding ssh-rsa ` + pubSerialized + ` user@host`,
  278. `# comments to ignore, along with a blank line`,
  279. ``,
  280. `env="HOME=/home/root2" ssh-rsa ` + pubSerialized + ` user2@host2`,
  281. ``,
  282. `# more comments, plus a invalid entry`,
  283. `ssh-rsa data-that-will-not-parse user@host3`,
  284. }
  285. for _, eol := range []string{"\n", "\r\n"} {
  286. authOptions := strings.Join(authWithOptions, eol)
  287. rest2 := strings.Join(authWithOptions[3:], eol)
  288. rest3 := strings.Join(authWithOptions[6:], eol)
  289. testAuthorizedKeys(t, []byte(authOptions), []testAuthResult{
  290. {pub, []string{`env="HOME=/home/root"`, "no-port-forwarding"}, "user@host", rest2, true},
  291. {pub, []string{`env="HOME=/home/root2"`}, "user2@host2", rest3, true},
  292. {nil, nil, "", "", false},
  293. })
  294. }
  295. }
  296. func TestAuthWithQuotedSpaceInEnv(t *testing.T) {
  297. pub, pubSerialized := getTestKey()
  298. authWithQuotedSpaceInEnv := []byte(`env="HOME=/home/root dir",no-port-forwarding ssh-rsa ` + pubSerialized + ` user@host`)
  299. testAuthorizedKeys(t, []byte(authWithQuotedSpaceInEnv), []testAuthResult{
  300. {pub, []string{`env="HOME=/home/root dir"`, "no-port-forwarding"}, "user@host", "", true},
  301. })
  302. }
  303. func TestAuthWithQuotedCommaInEnv(t *testing.T) {
  304. pub, pubSerialized := getTestKey()
  305. authWithQuotedCommaInEnv := []byte(`env="HOME=/home/root,dir",no-port-forwarding ssh-rsa ` + pubSerialized + ` user@host`)
  306. testAuthorizedKeys(t, []byte(authWithQuotedCommaInEnv), []testAuthResult{
  307. {pub, []string{`env="HOME=/home/root,dir"`, "no-port-forwarding"}, "user@host", "", true},
  308. })
  309. }
  310. func TestAuthWithQuotedQuoteInEnv(t *testing.T) {
  311. pub, pubSerialized := getTestKey()
  312. authWithQuotedQuoteInEnv := []byte(`env="HOME=/home/\"root dir",no-port-forwarding` + "\t" + `ssh-rsa` + "\t" + pubSerialized + ` user@host`)
  313. authWithDoubleQuotedQuote := []byte(`no-port-forwarding,env="HOME=/home/ \"root dir\"" ssh-rsa ` + pubSerialized + "\t" + `user@host`)
  314. testAuthorizedKeys(t, []byte(authWithQuotedQuoteInEnv), []testAuthResult{
  315. {pub, []string{`env="HOME=/home/\"root dir"`, "no-port-forwarding"}, "user@host", "", true},
  316. })
  317. testAuthorizedKeys(t, []byte(authWithDoubleQuotedQuote), []testAuthResult{
  318. {pub, []string{"no-port-forwarding", `env="HOME=/home/ \"root dir\""`}, "user@host", "", true},
  319. })
  320. }
  321. func TestAuthWithInvalidSpace(t *testing.T) {
  322. _, pubSerialized := getTestKey()
  323. authWithInvalidSpace := []byte(`env="HOME=/home/root dir", no-port-forwarding ssh-rsa ` + pubSerialized + ` user@host
  324. #more to follow but still no valid keys`)
  325. testAuthorizedKeys(t, []byte(authWithInvalidSpace), []testAuthResult{
  326. {nil, nil, "", "", false},
  327. })
  328. }
  329. func TestAuthWithMissingQuote(t *testing.T) {
  330. pub, pubSerialized := getTestKey()
  331. authWithMissingQuote := []byte(`env="HOME=/home/root,no-port-forwarding ssh-rsa ` + pubSerialized + ` user@host
  332. env="HOME=/home/root",shared-control ssh-rsa ` + pubSerialized + ` user@host`)
  333. testAuthorizedKeys(t, []byte(authWithMissingQuote), []testAuthResult{
  334. {pub, []string{`env="HOME=/home/root"`, `shared-control`}, "user@host", "", true},
  335. })
  336. }
  337. func TestInvalidEntry(t *testing.T) {
  338. authInvalid := []byte(`ssh-rsa`)
  339. _, _, _, _, err := ParseAuthorizedKey(authInvalid)
  340. if err == nil {
  341. t.Errorf("got valid entry for %q", authInvalid)
  342. }
  343. }
  344. var knownHostsParseTests = []struct {
  345. input string
  346. err string
  347. marker string
  348. comment string
  349. hosts []string
  350. rest string
  351. }{
  352. {
  353. "",
  354. "EOF",
  355. "", "", nil, "",
  356. },
  357. {
  358. "# Just a comment",
  359. "EOF",
  360. "", "", nil, "",
  361. },
  362. {
  363. " \t ",
  364. "EOF",
  365. "", "", nil, "",
  366. },
  367. {
  368. "localhost ssh-rsa {RSAPUB}",
  369. "",
  370. "", "", []string{"localhost"}, "",
  371. },
  372. {
  373. "localhost\tssh-rsa {RSAPUB}",
  374. "",
  375. "", "", []string{"localhost"}, "",
  376. },
  377. {
  378. "localhost\tssh-rsa {RSAPUB}\tcomment comment",
  379. "",
  380. "", "comment comment", []string{"localhost"}, "",
  381. },
  382. {
  383. "localhost\tssh-rsa {RSAPUB}\tcomment comment\n",
  384. "",
  385. "", "comment comment", []string{"localhost"}, "",
  386. },
  387. {
  388. "localhost\tssh-rsa {RSAPUB}\tcomment comment\r\n",
  389. "",
  390. "", "comment comment", []string{"localhost"}, "",
  391. },
  392. {
  393. "localhost\tssh-rsa {RSAPUB}\tcomment comment\r\nnext line",
  394. "",
  395. "", "comment comment", []string{"localhost"}, "next line",
  396. },
  397. {
  398. "localhost,[host2:123]\tssh-rsa {RSAPUB}\tcomment comment",
  399. "",
  400. "", "comment comment", []string{"localhost", "[host2:123]"}, "",
  401. },
  402. {
  403. "@marker \tlocalhost,[host2:123]\tssh-rsa {RSAPUB}",
  404. "",
  405. "marker", "", []string{"localhost", "[host2:123]"}, "",
  406. },
  407. {
  408. "@marker \tlocalhost,[host2:123]\tssh-rsa aabbccdd",
  409. "short read",
  410. "", "", nil, "",
  411. },
  412. }
  413. func TestKnownHostsParsing(t *testing.T) {
  414. rsaPub, rsaPubSerialized := getTestKey()
  415. for i, test := range knownHostsParseTests {
  416. var expectedKey PublicKey
  417. const rsaKeyToken = "{RSAPUB}"
  418. input := test.input
  419. if strings.Contains(input, rsaKeyToken) {
  420. expectedKey = rsaPub
  421. input = strings.Replace(test.input, rsaKeyToken, rsaPubSerialized, -1)
  422. }
  423. marker, hosts, pubKey, comment, rest, err := ParseKnownHosts([]byte(input))
  424. if err != nil {
  425. if len(test.err) == 0 {
  426. t.Errorf("#%d: unexpectedly failed with %q", i, err)
  427. } else if !strings.Contains(err.Error(), test.err) {
  428. t.Errorf("#%d: expected error containing %q, but got %q", i, test.err, err)
  429. }
  430. continue
  431. } else if len(test.err) != 0 {
  432. t.Errorf("#%d: succeeded but expected error including %q", i, test.err)
  433. continue
  434. }
  435. if !reflect.DeepEqual(expectedKey, pubKey) {
  436. t.Errorf("#%d: expected key %#v, but got %#v", i, expectedKey, pubKey)
  437. }
  438. if marker != test.marker {
  439. t.Errorf("#%d: expected marker %q, but got %q", i, test.marker, marker)
  440. }
  441. if comment != test.comment {
  442. t.Errorf("#%d: expected comment %q, but got %q", i, test.comment, comment)
  443. }
  444. if !reflect.DeepEqual(test.hosts, hosts) {
  445. t.Errorf("#%d: expected hosts %#v, but got %#v", i, test.hosts, hosts)
  446. }
  447. if rest := string(rest); rest != test.rest {
  448. t.Errorf("#%d: expected remaining input to be %q, but got %q", i, test.rest, rest)
  449. }
  450. }
  451. }
  452. func TestFingerprintLegacyMD5(t *testing.T) {
  453. pub, _ := getTestKey()
  454. fingerprint := FingerprintLegacyMD5(pub)
  455. want := "fb:61:6d:1a:e3:f0:95:45:3c:a0:79:be:4a:93:63:66" // ssh-keygen -lf -E md5 rsa
  456. if fingerprint != want {
  457. t.Errorf("got fingerprint %q want %q", fingerprint, want)
  458. }
  459. }
  460. func TestFingerprintSHA256(t *testing.T) {
  461. pub, _ := getTestKey()
  462. fingerprint := FingerprintSHA256(pub)
  463. want := "SHA256:Anr3LjZK8YVpjrxu79myrW9Hrb/wpcMNpVvTq/RcBm8" // ssh-keygen -lf rsa
  464. if fingerprint != want {
  465. t.Errorf("got fingerprint %q want %q", fingerprint, want)
  466. }
  467. }
  468. func TestInvalidKeys(t *testing.T) {
  469. keyTypes := []string{
  470. "RSA PRIVATE KEY",
  471. "PRIVATE KEY",
  472. "EC PRIVATE KEY",
  473. "DSA PRIVATE KEY",
  474. "OPENSSH PRIVATE KEY",
  475. }
  476. for _, keyType := range keyTypes {
  477. for _, dataLen := range []int{0, 1, 2, 5, 10, 20} {
  478. data := make([]byte, dataLen)
  479. if _, err := io.ReadFull(rand.Reader, data); err != nil {
  480. t.Fatal(err)
  481. }
  482. var buf bytes.Buffer
  483. pem.Encode(&buf, &pem.Block{
  484. Type: keyType,
  485. Bytes: data,
  486. })
  487. // This test is just to ensure that the function
  488. // doesn't panic so the return value is ignored.
  489. ParseRawPrivateKey(buf.Bytes())
  490. }
  491. }
  492. }
  493. func TestSKKeys(t *testing.T) {
  494. for _, d := range testdata.SKData {
  495. pk, _, _, _, err := ParseAuthorizedKey(d.PubKey)
  496. if err != nil {
  497. t.Fatalf("parseAuthorizedKey returned error: %v", err)
  498. }
  499. sigBuf := make([]byte, hex.DecodedLen(len(d.HexSignature)))
  500. if _, err := hex.Decode(sigBuf, d.HexSignature); err != nil {
  501. t.Fatalf("hex.Decode() failed: %v", err)
  502. }
  503. dataBuf := make([]byte, hex.DecodedLen(len(d.HexData)))
  504. if _, err := hex.Decode(dataBuf, d.HexData); err != nil {
  505. t.Fatalf("hex.Decode() failed: %v", err)
  506. }
  507. sig, _, ok := parseSignature(sigBuf)
  508. if !ok {
  509. t.Fatalf("parseSignature(%v) failed", sigBuf)
  510. }
  511. // Test that good data and signature pass verification
  512. if err := pk.Verify(dataBuf, sig); err != nil {
  513. t.Errorf("%s: PublicKey.Verify(%v, %v) failed: %v", d.Name, dataBuf, sig, err)
  514. }
  515. // Invalid data being passed in
  516. invalidData := []byte("INVALID DATA")
  517. if err := pk.Verify(invalidData, sig); err == nil {
  518. t.Errorf("%s with invalid data: PublicKey.Verify(%v, %v) passed unexpectedly", d.Name, invalidData, sig)
  519. }
  520. // Change byte in blob to corrup signature
  521. sig.Blob[5] = byte('A')
  522. // Corrupted data being passed in
  523. if err := pk.Verify(dataBuf, sig); err == nil {
  524. t.Errorf("%s with corrupted signature: PublicKey.Verify(%v, %v) passed unexpectedly", d.Name, dataBuf, sig)
  525. }
  526. }
  527. }