| 1 | // Modified by 42wim |
| 2 | // |
| 3 | // Copyright 2021 The Sigstore Authors. |
| 4 | // |
| 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
| 16 | |
| 17 | package sshsig |
| 18 | |
| 19 | import ( |
| 20 | "bytes" |
| 21 | "io/ioutil" |
| 22 | "os/exec" |
| 23 | "path/filepath" |
| 24 | "strings" |
| 25 | "testing" |
| 26 | ) |
| 27 | |
| 28 | var ( |
| 29 | // Generated with "ssh-keygen -C [email protected] -f id_rsa" |
| 30 | sshPrivateKey = `-----BEGIN OPENSSH PRIVATE KEY----- |
| 31 | b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAABlwAAAAdzc2gtcn |
| 32 | NhAAAAAwEAAQAAAYEA16H5ImoRO7mr41r8Z8JFBdu6jIM+6XU8M0r9F81RuhLYqzr9zw1n |
| 33 | LeGCqFxPXNBKm8ZyH2BCsBHsbXbwe85IMHM3SUh8X/9fI0Lpi5/xbqAproFUpNR+UJYv6s |
| 34 | 8AaWk5zpN1rmpBrqGFJfGQKJCioDiiwNGmSdVkUNmQmYIANxJMDWYmNe8vUOh6nYEHB+lz |
| 35 | fGgDAAzVSXTACW994UkSY47AD05swU4rIT/JWA6BkUrEhO//F0QQhFeROCPJiPRhJXGcFf |
| 36 | 9SicffJqR/ELzM1zNYnRXMD0bbdTUwDrIcIFFNBbtcfJVOUUCGumSlt+qjUC7y8cvwbHAu |
| 37 | wf5nS6baA7P6LfTYplF2XIAkdWtkN6O1ouoyIHICXMldd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GZBHdSx3gk4TzCsEDOP7ZZPLq42lpKMK10zFPTMd0maXtJDYKU/b4 |
| 66 | gAATvvPoylyYUAAAAOdGVzdEByZWtvci5kZXYBAgMEBQ== |
| 67 | -----END OPENSSH PRIVATE KEY----- |
| 68 | ` |
| 69 | sshPublicKey = `ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDXofkiahE7uavjWvxnwkUF27qMgz7pdTwzSv0XzVG6EtirOv3PDWct4YKoXE9c0EqbxnIfYEKwEextdvB7zkgwczdJSHxf/18jQumLn/FuoCmugVSk1H5Qli/qzwBpaTnOk3WuakGuoYUl8ZAokKKgOKLA0aZJ1WRQ2ZCZggA3EkwNZiY17y9Q6HqdgQcH6XN8aAMADNVJdMAJb33hSRJjjsAPTmzBTishP8lYDoGRSsSE7/8XRBCEV5E4I8mI9GElcZwV/1KJx98mpH8QvMzXM1idFcwPRtt1NTAOshwgUU0Fu1x8lU5RQIa6ZKW36qNQLvLxy/BscC7B/mdLptoDs/ot9NimUXZcgCR1a2Q3o7Wi6jIgcgJcyV10Nba81ol4RdN4qPHnVZIzuo+dBkqwG3CMtB4Rj84+Qi+7zyU01hIPreoxQDXaayiGPBUUIiAlW9gsiuRWJzNnu3cvuWDLVfQIkjh7Wug58z+v2NOJ7IMdyERillhzDcvVHaq14+U= [email protected] |
| 70 | ` |
| 71 | // Generated with "ssh-keygen -C [email protected] -f id_rsa" |
| 72 | otherSSHPrivateKey = `-----BEGIN OPENSSH PRIVATE KEY----- |
| 73 | b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAABlwAAAAdzc2gtcn |
| 74 | NhAAAAAwEAAQAAAYEAw/WCSWC9TEvCQOwO+T68EvNa3OSIv1Y0+sT8uSvyjPyEO0+p0t8C |
| 75 | g/zy67vOxiQpU5jN6MItjXAjMmeCm8GKMt6gk+cDoaAev/ZfjuzSL7RayExpmhBleh2X3G |
| 76 | KLkkXF9ABFNchlTqSLOZiEjDoNpbF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os4EGLnkwAAAMEAy/AuLtPKA2u9oCA8e18ZnuQRAi27FBVU |
| 105 | rU2D7bMg1eS0IakG8v0gE9K6WdYzyArY1RoKB3ZklK5VmJ1cOcWc2x3Ejc5jcJgc8cC6lZ |
| 106 | wwjpE8HfWL1kIIYgPdcexqFc+l6MdgH6QMKU3nLg1LsM4v5FEldtk/2dmnw620xnFfstpF |
| 107 | VxSZNdKrYfM/v9o6sRaDRqSfH1dG8BvkUxPznTAF+JDxBENcKXYECcq9f6dcl1w5IEnNTD |
| 108 | Wry/EKQvgvOUjbAAAAFG90aGVyLXRlc3RAcmVrb3IuZGV2AQIDBAUG |
| 109 | -----END OPENSSH PRIVATE KEY----- |
| 110 | ` |
| 111 | otherSSHPublicKey = `ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDD9YJJYL1MS8JA7A75PrwS81rc5Ii/VjT6xPy5K/KM/IQ7T6nS3wKD/PLru87GJClTmM3owi2NcCMyZ4KbwYoy3qCT5wOhoB6/9l+O7NIvtFrITGmaEGV6HZfcYouSRcX0AEU1yGVOpIs5mISMOg2lsW/XopPWwToKpbwOPFdCRCT0krrmEsf4HF5Yw0IQlVoKZrhfThomYLvMkCLnIZ55PRIpWoyiFq8X3Q7peJgUJAe7Bc8/Id+hyqC52ZeejPP7oPprEkpkzBCw2ndYq6Y6OXNafEEIIHWXaM3pFqDxonbbvuIwVdHCNMv/yNoSxbgqTKwN/QaNXb+KpuvSrlvRqsNhu/sKsYFH64fTAbP9miDXHmJkA05uFlQukOstUmJ0QxzbsdcFvs8yw0PLsEZhEHXJzR3TLzenyZSq86VZICvGfVacBk7TikCBOtyWuESHhlc6SfZKfzZ67cOlDyKeSiVjgh+eEh9s4h56ahQ2rW05Sq6GjD0YtEzog2J4csE= [email protected] |
| 112 | ` |
| 113 | |
| 114 | // Generated with ssh-keygen -C [email protected] -t ed25519 -f id_ed25519 |
| 115 | ed25519PrivateKey = `-----BEGIN OPENSSH PRIVATE KEY----- |
| 116 | b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW |
| 117 | QyNTUxOQAAACBB45zRHxPPFtabwS3Vd6Lb9vMe+tIHZj2qN5VQ+bgLfQAAAJgyRa3cMkWt |
| 118 | 3AAAAAtzc2gtZWQyNTUxOQAAACBB45zRHxPPFtabwS3Vd6Lb9vMe+tIHZj2qN5VQ+bgLfQ |
| 119 | AAAED7y4N/DsVnRQiBZNxEWdsJ9RmbranvtQ3X9jnb6gFed0HjnNEfE88W1pvBLdV3otv2 |
| 120 | 8x760gdmPao3lVD5uAt9AAAADnRlc3RAcmVrb3IuZGV2AQIDBAUGBw== |
| 121 | -----END OPENSSH PRIVATE KEY----- |
| 122 | ` |
| 123 | ed25519PublicKey = `ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEHjnNEfE88W1pvBLdV3otv28x760gdmPao3lVD5uAt9 [email protected] |
| 124 | ` |
| 125 | ) |
| 126 | |
| 127 | func TestFromOpenSSH(t *testing.T) { |
| 128 | for _, tt := range []struct { |
| 129 | name string |
| 130 | pub string |
| 131 | priv string |
| 132 | }{ |
| 133 | { |
| 134 | name: "rsa", |
| 135 | pub: sshPublicKey, |
| 136 | priv: sshPrivateKey, |
| 137 | }, |
| 138 | { |
| 139 | name: "ed25519", |
| 140 | pub: ed25519PublicKey, |
| 141 | priv: ed25519PrivateKey, |
| 142 | }, |
| 143 | } { |
| 144 | if _, err := exec.LookPath("ssh-keygen"); err != nil { |
| 145 | t.Skip("skip TestFromOpenSSH: missing ssh-keygen in PATH") |
| 146 | } |
| 147 | t.Run(tt.name, func(t *testing.T) { |
| 148 | tt := tt |
| 149 | |
| 150 | // Test that a signature from the cli can validate here. |
| 151 | td := t.TempDir() |
| 152 | |
| 153 | data := []byte("hello, ssh world") |
| 154 | dataPath := write(t, []byte(data), td, "data") |
| 155 | |
| 156 | privPath := write(t, []byte(tt.priv), td, "id") |
| 157 | write(t, []byte(tt.pub), td, "id.pub") |
| 158 | |
| 159 | sigPath := dataPath + ".sig" |
| 160 | run(t, nil, "ssh-keygen", "-Y", "sign", "-n", "file", "-f", privPath, dataPath) |
| 161 | |
| 162 | sigBytes, err := ioutil.ReadFile(sigPath) |
| 163 | if err != nil { |
| 164 | t.Fatal(err) |
| 165 | } |
| 166 | if err := Verify(bytes.NewReader(data), sigBytes, []byte(tt.pub), "file"); err != nil { |
| 167 | t.Error(err) |
| 168 | } |
| 169 | |
| 170 | // It should not verify if we check against another public key |
| 171 | if err := Verify(bytes.NewReader(data), sigBytes, []byte(otherSSHPublicKey), "file"); err == nil { |
| 172 | t.Error("expected error with incorrect key") |
| 173 | } |
| 174 | |
| 175 | // It should not verify if the data is tampered |
| 176 | if err := Verify(strings.NewReader("bad data"), sigBytes, []byte(sshPublicKey), "file"); err == nil { |
| 177 | t.Error("expected error with incorrect data") |
| 178 | } |
| 179 | }) |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | func TestToOpenSSH(t *testing.T) { |
| 184 | for _, tt := range []struct { |
| 185 | name string |
| 186 | pub string |
| 187 | priv string |
| 188 | }{ |
| 189 | { |
| 190 | name: "rsa", |
| 191 | pub: sshPublicKey, |
| 192 | priv: sshPrivateKey, |
| 193 | }, |
| 194 | { |
| 195 | name: "ed25519", |
| 196 | pub: ed25519PublicKey, |
| 197 | priv: ed25519PrivateKey, |
| 198 | }, |
| 199 | } { |
| 200 | if _, err := exec.LookPath("ssh-keygen"); err != nil { |
| 201 | t.Skip("skip TestToOpenSSH: missing ssh-keygen in PATH") |
| 202 | } |
| 203 | t.Run(tt.name, func(t *testing.T) { |
| 204 | tt := tt |
| 205 | // Test that a signature from here can validate in the CLI. |
| 206 | td := t.TempDir() |
| 207 | |
| 208 | data := []byte("hello, ssh world") |
| 209 | write(t, []byte(data), td, "data") |
| 210 | |
| 211 | armored, err := Sign([]byte(tt.priv), bytes.NewReader(data), "file") |
| 212 | if err != nil { |
| 213 | t.Fatal(err) |
| 214 | } |
| 215 | |
| 216 | sigPath := write(t, []byte(armored), td, "oursig") |
| 217 | |
| 218 | // Create an allowed_signers file with two keys to check against. |
| 219 | allowedSigner := "[email protected] " + tt.pub + "\n" |
| 220 | allowedSigner += "[email protected] " + otherSSHPublicKey + "\n" |
| 221 | allowedSigners := write(t, []byte(allowedSigner), td, "allowed_signer") |
| 222 | |
| 223 | // We use the correct principal here so it should work. |
| 224 | run(t, data, "ssh-keygen", "-Y", "verify", "-f", allowedSigners, |
| 225 | "-I", "[email protected]", "-n", "file", "-s", sigPath) |
| 226 | |
| 227 | // Just to be sure, check against the other public key as well. |
| 228 | runErr(t, data, "ssh-keygen", "-Y", "verify", "-f", allowedSigners, |
| 229 | "-I", "[email protected]", "-n", "file", "-s", sigPath) |
| 230 | |
| 231 | // It should error if we run it against other data |
| 232 | data = []byte("other data!") |
| 233 | runErr(t, data, "ssh-keygen", "-Y", "check-novalidate", "-n", "file", "-s", sigPath) |
| 234 | }) |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | func TestRoundTrip(t *testing.T) { |
| 239 | data := []byte("my good data to be signed!") |
| 240 | |
| 241 | // Create one extra signature for all the tests. |
| 242 | otherSig, err := Sign([]byte(otherSSHPrivateKey), bytes.NewReader(data), "file") |
| 243 | if err != nil { |
| 244 | t.Fatal(err) |
| 245 | } |
| 246 | |
| 247 | for _, tt := range []struct { |
| 248 | name string |
| 249 | pub string |
| 250 | priv string |
| 251 | }{ |
| 252 | { |
| 253 | name: "rsa", |
| 254 | pub: sshPublicKey, |
| 255 | priv: sshPrivateKey, |
| 256 | }, |
| 257 | { |
| 258 | name: "ed25519", |
| 259 | pub: ed25519PublicKey, |
| 260 | priv: ed25519PrivateKey, |
| 261 | }, |
| 262 | } { |
| 263 | t.Run(tt.name, func(t *testing.T) { |
| 264 | tt := tt |
| 265 | sig, err := Sign([]byte(tt.priv), bytes.NewReader(data), "file") |
| 266 | if err != nil { |
| 267 | t.Fatal(err) |
| 268 | } |
| 269 | |
| 270 | // Check the signature against that data and public key |
| 271 | if err := Verify(bytes.NewReader(data), sig, []byte(tt.pub), "file"); err != nil { |
| 272 | t.Error(err) |
| 273 | } |
| 274 | |
| 275 | // Now check it against invalid data. |
| 276 | if err := Verify(strings.NewReader("invalid data!"), sig, []byte(tt.pub), "file"); err == nil { |
| 277 | t.Error("expected error!") |
| 278 | } |
| 279 | |
| 280 | // Now check it against the wrong key. |
| 281 | if err := Verify(bytes.NewReader(data), sig, []byte(otherSSHPublicKey), "file"); err == nil { |
| 282 | t.Error("expected error!") |
| 283 | } |
| 284 | |
| 285 | // Now check it against an invalid signature data. |
| 286 | if err := Verify(bytes.NewReader(data), []byte("invalid signature!"), []byte(tt.pub), "file"); err == nil { |
| 287 | t.Error("expected error!") |
| 288 | } |
| 289 | |
| 290 | // Once more, use the wrong signature and check it against the original (wrong public key) |
| 291 | if err := Verify(bytes.NewReader(data), otherSig, []byte(tt.pub), "file"); err == nil { |
| 292 | t.Error("expected error!") |
| 293 | } |
| 294 | // It should work against the correct public key. |
| 295 | if err := Verify(bytes.NewReader(data), otherSig, []byte(otherSSHPublicKey), "file"); err != nil { |
| 296 | t.Error(err) |
| 297 | } |
| 298 | }) |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | func write(t *testing.T, d []byte, fp ...string) string { |
| 303 | p := filepath.Join(fp...) |
| 304 | if err := ioutil.WriteFile(p, d, 0o600); err != nil { |
| 305 | t.Fatal(err) |
| 306 | } |
| 307 | return p |
| 308 | } |
| 309 | |
| 310 | func run(t *testing.T, stdin []byte, args ...string) { |
| 311 | t.Helper() |
| 312 | /* #nosec */ |
| 313 | cmd := exec.Command(args[0], args[1:]...) |
| 314 | cmd.Stdin = bytes.NewReader(stdin) |
| 315 | out, err := cmd.CombinedOutput() |
| 316 | t.Logf("cmd %v: %s", cmd, string(out)) |
| 317 | if err != nil { |
| 318 | t.Fatal(err) |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | func runErr(t *testing.T, stdin []byte, args ...string) { |
| 323 | t.Helper() |
| 324 | /* #nosec */ |
| 325 | cmd := exec.Command(args[0], args[1:]...) |
| 326 | cmd.Stdin = bytes.NewReader(stdin) |
| 327 | out, err := cmd.CombinedOutput() |
| 328 | t.Logf("cmd %v: %s", cmd, string(out)) |
| 329 | if err == nil { |
| 330 | t.Fatal("expected error") |
| 331 | } |
| 332 | } |