You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
3.2 KiB

  1. // Copyright 2011 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"
  8. "crypto/aes"
  9. "crypto/rand"
  10. "testing"
  11. )
  12. func TestDefaultCiphersExist(t *testing.T) {
  13. for _, cipherAlgo := range supportedCiphers {
  14. if _, ok := cipherModes[cipherAlgo]; !ok {
  15. t.Errorf("default cipher %q is unknown", cipherAlgo)
  16. }
  17. }
  18. }
  19. func TestPacketCiphers(t *testing.T) {
  20. // Still test aes128cbc cipher althought it's commented out.
  21. cipherModes[aes128cbcID] = &streamCipherMode{16, aes.BlockSize, 0, nil}
  22. defer delete(cipherModes, aes128cbcID)
  23. for cipher := range cipherModes {
  24. kr := &kexResult{Hash: crypto.SHA1}
  25. algs := directionAlgorithms{
  26. Cipher: cipher,
  27. MAC: "hmac-sha1",
  28. Compression: "none",
  29. }
  30. client, err := newPacketCipher(clientKeys, algs, kr)
  31. if err != nil {
  32. t.Errorf("newPacketCipher(client, %q): %v", cipher, err)
  33. continue
  34. }
  35. server, err := newPacketCipher(clientKeys, algs, kr)
  36. if err != nil {
  37. t.Errorf("newPacketCipher(client, %q): %v", cipher, err)
  38. continue
  39. }
  40. want := "bla bla"
  41. input := []byte(want)
  42. buf := &bytes.Buffer{}
  43. if err := client.writePacket(0, buf, rand.Reader, input); err != nil {
  44. t.Errorf("writePacket(%q): %v", cipher, err)
  45. continue
  46. }
  47. packet, err := server.readPacket(0, buf)
  48. if err != nil {
  49. t.Errorf("readPacket(%q): %v", cipher, err)
  50. continue
  51. }
  52. if string(packet) != want {
  53. t.Errorf("roundtrip(%q): got %q, want %q", cipher, packet, want)
  54. }
  55. }
  56. }
  57. func TestCBCOracleCounterMeasure(t *testing.T) {
  58. cipherModes[aes128cbcID] = &streamCipherMode{16, aes.BlockSize, 0, nil}
  59. defer delete(cipherModes, aes128cbcID)
  60. kr := &kexResult{Hash: crypto.SHA1}
  61. algs := directionAlgorithms{
  62. Cipher: aes128cbcID,
  63. MAC: "hmac-sha1",
  64. Compression: "none",
  65. }
  66. client, err := newPacketCipher(clientKeys, algs, kr)
  67. if err != nil {
  68. t.Fatalf("newPacketCipher(client): %v", err)
  69. }
  70. want := "bla bla"
  71. input := []byte(want)
  72. buf := &bytes.Buffer{}
  73. if err := client.writePacket(0, buf, rand.Reader, input); err != nil {
  74. t.Errorf("writePacket: %v", err)
  75. }
  76. packetSize := buf.Len()
  77. buf.Write(make([]byte, 2*maxPacket))
  78. // We corrupt each byte, but this usually will only test the
  79. // 'packet too large' or 'MAC failure' cases.
  80. lastRead := -1
  81. for i := 0; i < packetSize; i++ {
  82. server, err := newPacketCipher(clientKeys, algs, kr)
  83. if err != nil {
  84. t.Fatalf("newPacketCipher(client): %v", err)
  85. }
  86. fresh := &bytes.Buffer{}
  87. fresh.Write(buf.Bytes())
  88. fresh.Bytes()[i] ^= 0x01
  89. before := fresh.Len()
  90. _, err = server.readPacket(0, fresh)
  91. if err == nil {
  92. t.Errorf("corrupt byte %d: readPacket succeeded ", i)
  93. continue
  94. }
  95. if _, ok := err.(cbcError); !ok {
  96. t.Errorf("corrupt byte %d: got %v (%T), want cbcError", i, err, err)
  97. continue
  98. }
  99. after := fresh.Len()
  100. bytesRead := before - after
  101. if bytesRead < maxPacket {
  102. t.Errorf("corrupt byte %d: read %d bytes, want more than %d", i, bytesRead, maxPacket)
  103. continue
  104. }
  105. if i > 0 && bytesRead != lastRead {
  106. t.Errorf("corrupt byte %d: read %d bytes, want %d bytes read", i, bytesRead, lastRead)
  107. }
  108. lastRead = bytesRead
  109. }
  110. }