| 1 | // Copyright 2019 The age 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 | |
| 5 | package agessh_test |
| 6 | |
| 7 | import ( |
| 8 | "bytes" |
| 9 | "crypto/ed25519" |
| 10 | "crypto/rand" |
| 11 | "crypto/rsa" |
| 12 | "encoding/pem" |
| 13 | "math/big" |
| 14 | "reflect" |
| 15 | "testing" |
| 16 | |
| 17 | "filippo.io/age/agessh" |
| 18 | "golang.org/x/crypto/ssh" |
| 19 | ) |
| 20 | |
| 21 | func TestSSHRSAKeySize(t *testing.T) { |
| 22 | for _, bits := range []int{2041, 2047, 2048} { |
| 23 | n := new(big.Int).Lsh(big.NewInt(1), uint(bits-1)) |
| 24 | n.SetBit(n, 0, 1) |
| 25 | pub, err := ssh.NewPublicKey(&rsa.PublicKey{N: n, E: 65537}) |
| 26 | if err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | _, err = agessh.NewRSARecipient(pub) |
| 30 | if rejected, want := err != nil, bits < 2048; rejected != want { |
| 31 | t.Errorf("%d-bit key: rejected = %v, want %v", bits, rejected, want) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | key, err := rsa.GenerateKey(rand.Reader, 1024) |
| 36 | if err != nil { |
| 37 | t.Fatal(err) |
| 38 | } |
| 39 | id, err := agessh.NewRSAIdentity(key) |
| 40 | if err != nil { |
| 41 | t.Fatal(err) |
| 42 | } |
| 43 | if _, err := id.Recipient().Wrap(make([]byte, 16)); err == nil { |
| 44 | t.Error("RSAIdentity.Recipient accepted a 1024-bit key") |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | func TestSSHRSARoundTrip(t *testing.T) { |
| 49 | pk, err := rsa.GenerateKey(rand.Reader, 2048) |
| 50 | if err != nil { |
| 51 | t.Fatal(err) |
| 52 | } |
| 53 | pub, err := ssh.NewPublicKey(&pk.PublicKey) |
| 54 | if err != nil { |
| 55 | t.Fatal(err) |
| 56 | } |
| 57 | |
| 58 | r, err := agessh.NewRSARecipient(pub) |
| 59 | if err != nil { |
| 60 | t.Fatal(err) |
| 61 | } |
| 62 | i, err := agessh.NewRSAIdentity(pk) |
| 63 | if err != nil { |
| 64 | t.Fatal(err) |
| 65 | } |
| 66 | |
| 67 | // TODO: replace this with (and go-diff) with go-cmp. |
| 68 | if !reflect.DeepEqual(r, i.Recipient()) { |
| 69 | t.Fatalf("i.Recipient is different from r") |
| 70 | } |
| 71 | |
| 72 | fileKey := make([]byte, 16) |
| 73 | if _, err := rand.Read(fileKey); err != nil { |
| 74 | t.Fatal(err) |
| 75 | } |
| 76 | stanzas, err := r.Wrap(fileKey) |
| 77 | if err != nil { |
| 78 | t.Fatal(err) |
| 79 | } |
| 80 | |
| 81 | out, err := i.Unwrap(stanzas) |
| 82 | if err != nil { |
| 83 | t.Fatal(err) |
| 84 | } |
| 85 | |
| 86 | if !bytes.Equal(fileKey, out) { |
| 87 | t.Errorf("invalid output: %x, expected %x", out, fileKey) |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | func TestSSHRSAFingerprintCollision(t *testing.T) { |
| 92 | targetKey, err := rsa.GenerateKey(rand.Reader, 2048) |
| 93 | if err != nil { |
| 94 | t.Fatal(err) |
| 95 | } |
| 96 | targetIdentity, err := agessh.NewRSAIdentity(targetKey) |
| 97 | if err != nil { |
| 98 | t.Fatal(err) |
| 99 | } |
| 100 | |
| 101 | otherKey, err := rsa.GenerateKey(rand.Reader, 2048) |
| 102 | if err != nil { |
| 103 | t.Fatal(err) |
| 104 | } |
| 105 | otherIdentity, err := agessh.NewRSAIdentity(otherKey) |
| 106 | if err != nil { |
| 107 | t.Fatal(err) |
| 108 | } |
| 109 | |
| 110 | fileKey := make([]byte, 16) |
| 111 | if _, err := rand.Read(fileKey); err != nil { |
| 112 | t.Fatal(err) |
| 113 | } |
| 114 | stanzas, err := targetIdentity.Recipient().Wrap(fileKey) |
| 115 | if err != nil { |
| 116 | t.Fatal(err) |
| 117 | } |
| 118 | collision, err := otherIdentity.Recipient().Wrap(make([]byte, 16)) |
| 119 | if err != nil { |
| 120 | t.Fatal(err) |
| 121 | } |
| 122 | collision[0].Args[0] = stanzas[0].Args[0] |
| 123 | |
| 124 | out, err := targetIdentity.Unwrap(append(collision, stanzas...)) |
| 125 | if err != nil { |
| 126 | t.Fatal(err) |
| 127 | } |
| 128 | if !bytes.Equal(fileKey, out) { |
| 129 | t.Errorf("invalid output: %x, expected %x", out, fileKey) |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | func TestSSHEd25519RoundTrip(t *testing.T) { |
| 134 | pub, priv, err := ed25519.GenerateKey(rand.Reader) |
| 135 | if err != nil { |
| 136 | t.Fatal(err) |
| 137 | } |
| 138 | sshPubKey, err := ssh.NewPublicKey(pub) |
| 139 | if err != nil { |
| 140 | t.Fatal(err) |
| 141 | } |
| 142 | |
| 143 | r, err := agessh.NewEd25519Recipient(sshPubKey) |
| 144 | if err != nil { |
| 145 | t.Fatal(err) |
| 146 | } |
| 147 | i, err := agessh.NewEd25519Identity(priv) |
| 148 | if err != nil { |
| 149 | t.Fatal(err) |
| 150 | } |
| 151 | |
| 152 | // TODO: replace this with (and go-diff) with go-cmp. |
| 153 | if !reflect.DeepEqual(r, i.Recipient()) { |
| 154 | t.Fatalf("i.Recipient is different from r") |
| 155 | } |
| 156 | |
| 157 | fileKey := make([]byte, 16) |
| 158 | if _, err := rand.Read(fileKey); err != nil { |
| 159 | t.Fatal(err) |
| 160 | } |
| 161 | stanzas, err := r.Wrap(fileKey) |
| 162 | if err != nil { |
| 163 | t.Fatal(err) |
| 164 | } |
| 165 | |
| 166 | out, err := i.Unwrap(stanzas) |
| 167 | if err != nil { |
| 168 | t.Fatal(err) |
| 169 | } |
| 170 | |
| 171 | if !bytes.Equal(fileKey, out) { |
| 172 | t.Errorf("invalid output: %x, expected %x", out, fileKey) |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | func TestSSHEd25519FingerprintCollision(t *testing.T) { |
| 177 | _, targetKey, err := ed25519.GenerateKey(rand.Reader) |
| 178 | if err != nil { |
| 179 | t.Fatal(err) |
| 180 | } |
| 181 | targetIdentity, err := agessh.NewEd25519Identity(targetKey) |
| 182 | if err != nil { |
| 183 | t.Fatal(err) |
| 184 | } |
| 185 | |
| 186 | _, otherKey, err := ed25519.GenerateKey(rand.Reader) |
| 187 | if err != nil { |
| 188 | t.Fatal(err) |
| 189 | } |
| 190 | otherIdentity, err := agessh.NewEd25519Identity(otherKey) |
| 191 | if err != nil { |
| 192 | t.Fatal(err) |
| 193 | } |
| 194 | |
| 195 | fileKey := make([]byte, 16) |
| 196 | if _, err := rand.Read(fileKey); err != nil { |
| 197 | t.Fatal(err) |
| 198 | } |
| 199 | stanzas, err := targetIdentity.Recipient().Wrap(fileKey) |
| 200 | if err != nil { |
| 201 | t.Fatal(err) |
| 202 | } |
| 203 | collision, err := otherIdentity.Recipient().Wrap(make([]byte, 16)) |
| 204 | if err != nil { |
| 205 | t.Fatal(err) |
| 206 | } |
| 207 | collision[0].Args[0] = stanzas[0].Args[0] |
| 208 | |
| 209 | out, err := targetIdentity.Unwrap(append(collision, stanzas...)) |
| 210 | if err != nil { |
| 211 | t.Fatal(err) |
| 212 | } |
| 213 | if !bytes.Equal(fileKey, out) { |
| 214 | t.Errorf("invalid output: %x, expected %x", out, fileKey) |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | func TestEncryptedSSHIdentityMismatch(t *testing.T) { |
| 219 | announcedPub, _, err := ed25519.GenerateKey(rand.Reader) |
| 220 | if err != nil { |
| 221 | t.Fatal(err) |
| 222 | } |
| 223 | embeddedPub, embeddedPriv, err := ed25519.GenerateKey(rand.Reader) |
| 224 | if err != nil { |
| 225 | t.Fatal(err) |
| 226 | } |
| 227 | announcedKey, err := ssh.NewPublicKey(announcedPub) |
| 228 | if err != nil { |
| 229 | t.Fatal(err) |
| 230 | } |
| 231 | embeddedKey, err := ssh.NewPublicKey(embeddedPub) |
| 232 | if err != nil { |
| 233 | t.Fatal(err) |
| 234 | } |
| 235 | |
| 236 | passphrase := []byte("passphrase") |
| 237 | block, err := ssh.MarshalPrivateKeyWithPassphrase(embeddedPriv, "", passphrase) |
| 238 | if err != nil { |
| 239 | t.Fatal(err) |
| 240 | } |
| 241 | prompts := 0 |
| 242 | i, err := agessh.NewEncryptedSSHIdentity(announcedKey, pem.EncodeToMemory(block), func() ([]byte, error) { |
| 243 | prompts++ |
| 244 | return passphrase, nil |
| 245 | }) |
| 246 | if err != nil { |
| 247 | t.Fatal(err) |
| 248 | } |
| 249 | |
| 250 | announcedRecipient, err := agessh.NewEd25519Recipient(announcedKey) |
| 251 | if err != nil { |
| 252 | t.Fatal(err) |
| 253 | } |
| 254 | embeddedRecipient, err := agessh.NewEd25519Recipient(embeddedKey) |
| 255 | if err != nil { |
| 256 | t.Fatal(err) |
| 257 | } |
| 258 | fileKey := make([]byte, 16) |
| 259 | if _, err := rand.Read(fileKey); err != nil { |
| 260 | t.Fatal(err) |
| 261 | } |
| 262 | stanzas, err := announcedRecipient.Wrap(fileKey) |
| 263 | if err != nil { |
| 264 | t.Fatal(err) |
| 265 | } |
| 266 | embeddedStanzas, err := embeddedRecipient.Wrap(fileKey) |
| 267 | if err != nil { |
| 268 | t.Fatal(err) |
| 269 | } |
| 270 | stanzas = append(stanzas, embeddedStanzas...) |
| 271 | |
| 272 | for attempt := 1; attempt <= 2; attempt++ { |
| 273 | _, err := i.Unwrap(stanzas) |
| 274 | if err == nil || err.Error() != "mismatched private and public SSH key" { |
| 275 | t.Fatalf("attempt %d: unexpected error: %v", attempt, err) |
| 276 | } |
| 277 | if prompts != attempt { |
| 278 | t.Fatalf("attempt %d: passphrase callback invoked %d times", attempt, prompts) |
| 279 | } |
| 280 | } |
| 281 | } |