aboutsummaryrefslogtreecommitdiff
path: root/object/signed/tag/integration_test.go
blob: af32aa0231d9387b453b0639f85d3d98edb668d6 (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package signedtag_test

import (
	"bytes"
	"os"
	"os/exec"
	"path/filepath"
	"testing"

	"codeberg.org/lindenii/furgit/internal/testgit"
	objectid "codeberg.org/lindenii/furgit/object/id"
	signedtag "codeberg.org/lindenii/furgit/object/signed/tag"
)

func setupSSHSignedTag(
	t *testing.T,
	algo objectid.Algorithm,
) (payload []byte, allowedSignersPath string, signaturePath string) {
	t.Helper()

	testRepo := testgit.NewRepo(t, testgit.RepoOptions{ObjectFormat: algo})

	signDir := t.TempDir()

	signRoot, err := os.OpenRoot(signDir)
	if err != nil {
		t.Fatalf("os.OpenRoot(%q): %v", signDir, err)
	}

	t.Cleanup(func() { _ = signRoot.Close() })

	privateKeyPath := filepath.Join(signDir, "signing_key")
	allowedSignersPath = filepath.Join(signDir, "allowed_signers")
	signaturePath = filepath.Join(signDir, "tag.sig")

	cmd := exec.Command( //nolint:noctx
		"ssh-keygen",
		"-q",
		"-t", "ed25519",
		"-N", "",
		"-C", "runxiyu@umich.edu",
		"-f", privateKeyPath,
	) //#nosec G204

	out, err := cmd.CombinedOutput()
	if err != nil {
		t.Fatalf("ssh-keygen generate failed: %v\n%s", err, out)
	}

	publicKey, err := signRoot.ReadFile("signing_key.pub")
	if err != nil {
		t.Fatalf("ReadFile(signing_key.pub): %v", err)
	}

	err = signRoot.WriteFile(
		"allowed_signers",
		append([]byte("runxiyu@umich.edu "), publicKey...),
		0o600,
	)
	if err != nil {
		t.Fatalf("WriteFile(allowed_signers): %v", err)
	}

	testRepo.Run(t, "config", "gpg.format", "ssh")
	testRepo.Run(t, "config", "user.signingkey", privateKeyPath)

	testRepo.WriteFile(t, "file.txt", []byte("signed\n"), 0o644)
	testRepo.Run(t, "add", "file.txt")
	testRepo.Run(t, "commit", "-m", "base commit")
	testRepo.Run(t, "tag", "-s", "-m", "signed tag", "signed-tag")

	tagID := testRepo.RevParse(t, "signed-tag^{tag}")
	body := testRepo.CatFile(t, "tag", tagID)

	tag, err := signedtag.Parse(body, algo)
	if err != nil {
		t.Fatalf("Parse: %v", err)
	}

	signature, ok := tag.AppendSignature(nil, algo)
	if !ok {
		t.Fatal("missing signature")
	}

	err = signRoot.WriteFile("tag.sig", signature, 0o600)
	if err != nil {
		t.Fatalf("WriteFile(tag.sig): %v", err)
	}

	return tag.AppendPayload(nil), allowedSignersPath, signaturePath
}

func TestSSHSignedTagIntegration(t *testing.T) {
	t.Parallel()

	testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) { //nolint:thelper
		payload, allowedSignersPath, signaturePath := setupSSHSignedTag(t, algo)

		cmd := exec.Command( //nolint:noctx
			"ssh-keygen",
			"-Y", "verify",
			"-n", "git",
			"-f", allowedSignersPath,
			"-I", "runxiyu@umich.edu",
			"-s", signaturePath,
		) //#nosec G204
		cmd.Stdin = bytes.NewReader(payload)

		out, err := cmd.CombinedOutput()
		if err != nil {
			t.Fatalf("ssh-keygen verify failed: %v\n%s", err, out)
		}
	})
}

func TestSSHSignedTagIntegrationRejectsTamperedPayload(t *testing.T) {
	t.Parallel()

	testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) { //nolint:thelper
		payload, allowedSignersPath, signaturePath := setupSSHSignedTag(t, algo)
		payload = append([]byte(nil), payload...)
		payload[len(payload)-2] ^= 1

		cmd := exec.Command( //nolint:noctx
			"ssh-keygen",
			"-Y", "verify",
			"-n", "git",
			"-f", allowedSignersPath,
			"-I", "runxiyu@umich.edu",
			"-s", signaturePath,
		) //#nosec G204
		cmd.Stdin = bytes.NewReader(payload)

		out, err := cmd.CombinedOutput()
		if err == nil {
			t.Fatalf("ssh-keygen verify unexpectedly succeeded:\n%s", out)
		}
	})
}