| 1 | package main |
| 2 | |
| 3 | // The envelope: a detached SSHSIG signature over the statement's canonical |
| 4 | // bytes, under a namespace of this project's own. No git object is signed |
| 5 | // (ida-51605e8) — the signature covers the statement and nothing else, so |
| 6 | // it survives a rebase exactly as the subject digest does. |
| 7 | // |
| 8 | // ssh-keygen is the implementation. It is the reference SSHSIG signer, it |
| 9 | // is already on any machine that pushes over ssh, and using it keeps this |
| 10 | // program's dependencies to the Go standard library. |
| 11 | |
| 12 | import ( |
| 13 | "bytes" |
| 14 | "encoding/base64" |
| 15 | "encoding/hex" |
| 16 | "fmt" |
| 17 | "os" |
| 18 | "os/exec" |
| 19 | "strings" |
| 20 | ) |
| 21 | |
| 22 | // signDetached signs data with the key at keyPath and returns the armored |
| 23 | // signature. |
| 24 | func signDetached(keyPath, namespace string, data []byte) ([]byte, error) { |
| 25 | if keyPath == "" { |
| 26 | return nil, fmt.Errorf("no signing key: pass --key. The host signs; an unsigned statement is not an attestation") |
| 27 | } |
| 28 | if _, err := os.Stat(keyPath); err != nil { |
| 29 | return nil, fmt.Errorf("signing key %s: %w", keyPath, err) |
| 30 | } |
| 31 | cmd := exec.Command("ssh-keygen", "-Y", "sign", "-f", keyPath, "-n", namespace, "-") |
| 32 | cmd.Stdin = bytes.NewReader(data) |
| 33 | var out, errb bytes.Buffer |
| 34 | cmd.Stdout = &out |
| 35 | cmd.Stderr = &errb |
| 36 | if err := cmd.Run(); err != nil { |
| 37 | return nil, fmt.Errorf("ssh-keygen -Y sign: %w: %s", err, strings.TrimSpace(errb.String())) |
| 38 | } |
| 39 | return out.Bytes(), nil |
| 40 | } |
| 41 | |
| 42 | // keyFingerprint returns the signer's key fingerprint twice: as hex, which |
| 43 | // names the ref, and in OpenSSH's own SHA256:base64 rendering, which is |
| 44 | // what a reader sees everywhere else. Both are SHA-256 of the same public |
| 45 | // key blob; base64 cannot name a ref because it contains '/'. |
| 46 | func keyFingerprint(keyPath string) (hexPrint, sshPrint string, err error) { |
| 47 | cmd := exec.Command("ssh-keygen", "-l", "-f", keyPath) |
| 48 | var out, errb bytes.Buffer |
| 49 | cmd.Stdout = &out |
| 50 | cmd.Stderr = &errb |
| 51 | if err := cmd.Run(); err != nil { |
| 52 | return "", "", fmt.Errorf("ssh-keygen -l -f %s: %w: %s", keyPath, err, strings.TrimSpace(errb.String())) |
| 53 | } |
| 54 | fields := strings.Fields(out.String()) |
| 55 | if len(fields) < 2 || !strings.HasPrefix(fields[1], "SHA256:") { |
| 56 | return "", "", fmt.Errorf("ssh-keygen -l -f %s: unexpected output %q", keyPath, strings.TrimSpace(out.String())) |
| 57 | } |
| 58 | sshPrint = fields[1] |
| 59 | raw, err := base64.RawStdEncoding.DecodeString(strings.TrimPrefix(sshPrint, "SHA256:")) |
| 60 | if err != nil { |
| 61 | return "", "", fmt.Errorf("fingerprint %s: %w", sshPrint, err) |
| 62 | } |
| 63 | return hex.EncodeToString(raw), sshPrint, nil |
| 64 | } |
| 65 | |
| 66 | // findPrincipal asks the allowed-signers file who signed. A signature by a |
| 67 | // key the file does not list has no principal, which is the failure a |
| 68 | // verifier wants: not "bad signature" but "not a signer we accept". |
| 69 | func findPrincipal(allowedSigners, sigPath string) (string, error) { |
| 70 | cmd := exec.Command("ssh-keygen", "-Y", "find-principals", "-s", sigPath, "-f", allowedSigners) |
| 71 | var out, errb bytes.Buffer |
| 72 | cmd.Stdout = &out |
| 73 | cmd.Stderr = &errb |
| 74 | if err := cmd.Run(); err != nil { |
| 75 | return "", fmt.Errorf("no principal in %s signed this statement: %s", allowedSigners, strings.TrimSpace(errb.String())) |
| 76 | } |
| 77 | principals := strings.Fields(out.String()) |
| 78 | if len(principals) == 0 { |
| 79 | return "", fmt.Errorf("no principal in %s signed this statement", allowedSigners) |
| 80 | } |
| 81 | return principals[0], nil |
| 82 | } |
| 83 | |
| 84 | // verifyDetached checks a signature over data against an allowed-signers |
| 85 | // file, and returns what ssh-keygen said about it. |
| 86 | func verifyDetached(allowedSigners, principal, namespace, sigPath string, data []byte) (string, error) { |
| 87 | cmd := exec.Command("ssh-keygen", "-Y", "verify", |
| 88 | "-f", allowedSigners, "-I", principal, "-n", namespace, "-s", sigPath) |
| 89 | cmd.Stdin = bytes.NewReader(data) |
| 90 | var out, errb bytes.Buffer |
| 91 | cmd.Stdout = &out |
| 92 | cmd.Stderr = &errb |
| 93 | if err := cmd.Run(); err != nil { |
| 94 | return "", fmt.Errorf("%s", strings.TrimSpace(errb.String()+out.String())) |
| 95 | } |
| 96 | return strings.TrimSpace(errb.String() + out.String()), nil |
| 97 | } |