Skip to content

Commit 67a6ea6

Browse files
illikainengitster
authored andcommitted
gpg-interface: limit search for primary key fingerprint
The VALIDSIG status line from GnuPG with --status-fd is documented to have 9 required and 1 optional fields [1]. The final, and optional, field is used to specify the fingerprint of the primary key that made the signature in case it was made by a subkey. However, this field is only available for OpenPGP signatures; not for CMS/X.509. If the VALIDSIG status line does not have the optional 10th field, the current code will continue reading onto the next status line. And this is the case for non-OpenPGP signatures [1]. The consequence is that a subsequent status line may be considered as the "primary key" for signatures that does not have an actual primary key. Limit the search of these 9 or 10 fields to the single line to avoid this problem. If the 10th field is missing, report that there is no primary key fingerprint. [Reference] [1] GnuPG Details, General status codes https://git.gnupg.org/cgi-bin/gitweb.cgi?p=gnupg.git;a=blob;f=doc/DETAILS;h=6ce340e8c04794add995e84308bb3091450bd28f;hb=HEAD#l483 The documentation says: VALIDSIG <args> The args are: - <fingerprint_in_hex> - <sig_creation_date> - <sig-timestamp> - <expire-timestamp> - <sig-version> - <reserved> - <pubkey-algo> - <hash-algo> - <sig-class> - [ <primary-key-fpr> ] This status indicates that the signature is cryptographically valid. [...] PRIMARY-KEY-FPR is the fingerprint of the primary key or identical to the first argument. The primary-key-fpr parameter is used for OpenPGP and not available for CMS signatures. [...] Signed-off-by: Hans Jerry Illikainen <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 392b862 commit 67a6ea6

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

gpg-interface.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,21 +156,33 @@ static void parse_gpg_output(struct signature_check *sigc)
156156
}
157157
/* Do we have fingerprint? */
158158
if (sigcheck_gpg_status[i].flags & GPG_STATUS_FINGERPRINT) {
159+
const char *limit;
160+
char **field;
161+
159162
next = strchrnul(line, ' ');
160163
replace_cstring(&sigc->fingerprint, line, next);
161164

162-
/* Skip interim fields */
165+
/*
166+
* Skip interim fields. The search is
167+
* limited to the same line since only
168+
* OpenPGP signatures has a field with
169+
* the primary fingerprint.
170+
*/
171+
limit = strchrnul(line, '\n');
163172
for (j = 9; j > 0; j--) {
164-
if (!*next)
173+
if (!*next || limit <= next)
165174
break;
166175
line = next + 1;
167176
next = strchrnul(line, ' ');
168177
}
169178

170-
next = strchrnul(line, '\n');
171-
free(sigc->primary_key_fingerprint);
172-
replace_cstring(&sigc->primary_key_fingerprint,
173-
line, next);
179+
field = &sigc->primary_key_fingerprint;
180+
if (!j) {
181+
next = strchrnul(line, '\n');
182+
replace_cstring(field, line, next);
183+
} else {
184+
replace_cstring(field, NULL, NULL);
185+
}
174186
}
175187

176188
break;

t/t4202-log.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1555,6 +1555,14 @@ test_expect_success GPG 'setup signed branch' '
15551555
git commit -S -m signed_commit
15561556
'
15571557

1558+
test_expect_success GPG 'setup signed branch with subkey' '
1559+
test_when_finished "git reset --hard && git checkout master" &&
1560+
git checkout -b signed-subkey master &&
1561+
echo foo >foo &&
1562+
git add foo &&
1563+
git commit -SB7227189 -m signed_commit
1564+
'
1565+
15581566
test_expect_success GPGSM 'setup signed branch x509' '
15591567
test_when_finished "git reset --hard && git checkout master" &&
15601568
git checkout -b signed-x509 master &&
@@ -1565,6 +1573,18 @@ test_expect_success GPGSM 'setup signed branch x509' '
15651573
git commit -S -m signed_commit
15661574
'
15671575

1576+
test_expect_success GPGSM 'log x509 fingerprint' '
1577+
echo "F8BF62E0693D0694816377099909C779FA23FD65 | " >expect &&
1578+
git log -n1 --format="%GF | %GP" signed-x509 >actual &&
1579+
test_cmp expect actual
1580+
'
1581+
1582+
test_expect_success GPGSM 'log OpenPGP fingerprint' '
1583+
echo "D4BE22311AD3131E5EDA29A461092E85B7227189" > expect &&
1584+
git log -n1 --format="%GP" signed-subkey >actual &&
1585+
test_cmp expect actual
1586+
'
1587+
15681588
test_expect_success GPG 'log --graph --show-signature' '
15691589
git log --graph --show-signature -n1 signed >actual &&
15701590
grep "^| gpg: Signature made" actual &&

0 commit comments

Comments
 (0)