Skip to content

Commit 51ceefd

Browse files
committed
adds 'SignatureTypeEmpty' check and improves description of 'GetSignatureType'
Signed-off-by: Ricardo Bartels <ricardo.bartels@telekom.de>
1 parent ab54ec2 commit 51ceefd

4 files changed

Lines changed: 19 additions & 6 deletions

File tree

git/git_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ func TestSignatureType(t *testing.T) {
550550
name: "unsigned",
551551
commit: &Commit{},
552552
tag: &Tag{},
553-
want: "unknown",
553+
want: "empty",
554554
},
555555
{
556556
name: "unknown signature type",

git/signatures/signature.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ const (
3232
SignatureTypeX509 SignatureType = "x509"
3333
// SignatureTypeUnknown represents an unknown signature type.
3434
SignatureTypeUnknown SignatureType = "unknown"
35+
// SignatureTypeEmpty represents an empty signature.
36+
SignatureTypeEmpty SignatureType = "empty"
3537
)
3638

3739
// IsX509Signature is the prefix used by Git to identify x509 signatures.
@@ -72,9 +74,16 @@ func IsX509Signature(signature string) bool {
7274
return startsWithStrings(signature, X509SignaturePrefix)
7375
}
7476

77+
// IsEmptySignature tests if the given signature string is empty.
78+
// It returns true if the signature string has a length of 0.
79+
func IsEmptySignature(signature string) bool {
80+
return len(signature) == 0
81+
}
82+
7583
// GetSignatureType returns the type of the signature as a string.
7684
// It returns "pgp" for PGP signatures, "ssh" for SSH signatures,
77-
// and "unknown" for unrecognized or empty signatures.
85+
// "x509" for S/MIME signatures, "empty" for an empty signature
86+
// and "unknown" for unrecognized signatures.
7887
func GetSignatureType(signature string) string {
7988
if IsPGPSignature(signature) {
8089
return string(SignatureTypePGP)
@@ -85,5 +94,8 @@ func GetSignatureType(signature string) string {
8594
if IsX509Signature(signature) {
8695
return string(SignatureTypeX509)
8796
}
97+
if IsEmptySignature(signature) {
98+
return string(SignatureTypeEmpty)
99+
}
88100
return string(SignatureTypeUnknown)
89101
}

git/signatures/signature_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ func TestGetSignatureType(t *testing.T) {
217217
{
218218
name: "empty signature",
219219
signature: "",
220-
want: string(SignatureTypeUnknown),
220+
want: string(SignatureTypeEmpty),
221221
},
222222
{
223223
name: "unknown signature",

git/signatures/ssh_signature.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import (
2727
gossh "golang.org/x/crypto/ssh"
2828
)
2929

30+
const SSHSignatureNamespace = "git"
31+
3032
// SSHSignaturePrefix is the prefix used by Git to identify SSH signatures.
3133
// https://github.com/git/git/blob/7b2bccb0d58d4f24705bf985de1f4612e4cf06e5/gpg-interface.c#L71
3234
var SSHSignaturePrefix = []string{"-----BEGIN SSH SIGNATURE-----"}
@@ -56,7 +58,7 @@ func ParseAuthorizedKeys(authorizedKeys string) ([]gossh.PublicKey, error) {
5658
return publicKeys, nil
5759
}
5860

59-
// verifySSHSignature verifies the SSH signature against the payload using
61+
// VerifySSHSignature verifies the SSH signature against the payload using
6062
// the provided authorized keys. It returns the fingerprint of the key that
6163
// successfully verified the signature, or an error.
6264
func VerifySSHSignature(signature string, payload []byte, authorizedKeys ...string) (string, error) {
@@ -88,8 +90,7 @@ func VerifySSHSignature(signature string, payload []byte, authorizedKeys ...stri
8890
// Try to verify with each public key
8991
for _, pubKey := range publicKeys {
9092
// Verify the signature using sshsig library
91-
// The namespace for Git is "git"
92-
err := sshsig.Verify(bytes.NewReader(payload), sig, pubKey, sig.HashAlgorithm, "git")
93+
err := sshsig.Verify(bytes.NewReader(payload), sig, pubKey, sig.HashAlgorithm, SSHSignatureNamespace)
9394
if err == nil {
9495
// Signature verified successfully
9596
return getPublicKeyFingerprint(pubKey), nil

0 commit comments

Comments
 (0)