Skip to content

Commit 3daaaab

Browse files
mgornygitster
authored andcommitted
gpg-interface.c: support getting key fingerprint via %GF format
Support processing VALIDSIG status that provides additional information for valid signatures. Use this information to propagate signing key fingerprint and expose it via %GF pretty format. This format can be used to build safer key verification systems that verify the key via complete fingerprint rather than short/long identifier provided by %GK. Signed-off-by: Michał Górny <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0b11a84 commit 3daaaab

File tree

5 files changed

+31
-7
lines changed

5 files changed

+31
-7
lines changed

Documentation/pretty-formats.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ endif::git-rev-list[]
153153
and "N" for no signature
154154
- '%GS': show the name of the signer for a signed commit
155155
- '%GK': show the key used to sign a signed commit
156+
- '%GF': show the fingerprint of the key used to sign a signed commit
156157
- '%gD': reflog selector, e.g., `refs/stash@{1}` or
157158
`refs/stash@{2 minutes ago`}; the format follows the rules described
158159
for the `-g` option. The portion before the `@` is the refname as

gpg-interface.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ void signature_check_clear(struct signature_check *sigc)
7373
FREE_AND_NULL(sigc->gpg_status);
7474
FREE_AND_NULL(sigc->signer);
7575
FREE_AND_NULL(sigc->key);
76+
FREE_AND_NULL(sigc->fingerprint);
7677
}
7778

7879
/* An exclusive status -- only one of them can appear in output */
@@ -81,6 +82,8 @@ void signature_check_clear(struct signature_check *sigc)
8182
#define GPG_STATUS_KEYID (1<<1)
8283
/* The status includes user identifier */
8384
#define GPG_STATUS_UID (1<<2)
85+
/* The status includes key fingerprints */
86+
#define GPG_STATUS_FINGERPRINT (1<<3)
8487

8588
/* Short-hand for standard exclusive *SIG status with keyid & UID */
8689
#define GPG_STATUS_STDSIG (GPG_STATUS_EXCLUSIVE|GPG_STATUS_KEYID|GPG_STATUS_UID)
@@ -98,6 +101,7 @@ static struct {
98101
{ 'X', "EXPSIG ", GPG_STATUS_STDSIG },
99102
{ 'Y', "EXPKEYSIG ", GPG_STATUS_STDSIG },
100103
{ 'R', "REVKEYSIG ", GPG_STATUS_STDSIG },
104+
{ 0, "VALIDSIG ", GPG_STATUS_FINGERPRINT },
101105
};
102106

103107
static void parse_gpg_output(struct signature_check *sigc)
@@ -123,7 +127,8 @@ static void parse_gpg_output(struct signature_check *sigc)
123127
goto found_duplicate_status;
124128
}
125129

126-
sigc->result = sigcheck_gpg_status[i].result;
130+
if (sigcheck_gpg_status[i].result)
131+
sigc->result = sigcheck_gpg_status[i].result;
127132
/* Do we have key information? */
128133
if (sigcheck_gpg_status[i].flags & GPG_STATUS_KEYID) {
129134
next = strchrnul(line, ' ');
@@ -137,6 +142,12 @@ static void parse_gpg_output(struct signature_check *sigc)
137142
sigc->signer = xmemdupz(line, next - line);
138143
}
139144
}
145+
/* Do we have fingerprint? */
146+
if (sigcheck_gpg_status[i].flags & GPG_STATUS_FINGERPRINT) {
147+
next = strchrnul(line, ' ');
148+
free(sigc->fingerprint);
149+
sigc->fingerprint = xmemdupz(line, next - line);
150+
}
140151

141152
break;
142153
}
@@ -154,6 +165,7 @@ static void parse_gpg_output(struct signature_check *sigc)
154165
*/
155166
sigc->result = 'E';
156167
/* Clear partial data to avoid confusion */
168+
FREE_AND_NULL(sigc->fingerprint);
157169
FREE_AND_NULL(sigc->signer);
158170
FREE_AND_NULL(sigc->key);
159171
}

gpg-interface.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ struct signature_check {
2323
char result;
2424
char *signer;
2525
char *key;
26+
char *fingerprint;
2627
};
2728

2829
void signature_check_clear(struct signature_check *sigc);

pretty.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,6 +1256,10 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
12561256
if (c->signature_check.key)
12571257
strbuf_addstr(sb, c->signature_check.key);
12581258
break;
1259+
case 'F':
1260+
if (c->signature_check.fingerprint)
1261+
strbuf_addstr(sb, c->signature_check.fingerprint);
1262+
break;
12591263
default:
12601264
return 0;
12611265
}

t/t7510-signed-commit.sh

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,9 @@ test_expect_success GPG 'show good signature with custom format' '
175175
G
176176
13B6F51ECDDE430D
177177
C O Mitter <[email protected]>
178+
73D758744BE721698EC54E8713B6F51ECDDE430D
178179
EOF
179-
git log -1 --format="%G?%n%GK%n%GS" sixth-signed >actual &&
180+
git log -1 --format="%G?%n%GK%n%GS%n%GF" sixth-signed >actual &&
180181
test_cmp expect actual
181182
'
182183

@@ -185,8 +186,9 @@ test_expect_success GPG 'show bad signature with custom format' '
185186
B
186187
13B6F51ECDDE430D
187188
C O Mitter <[email protected]>
189+
188190
EOF
189-
git log -1 --format="%G?%n%GK%n%GS" $(cat forged1.commit) >actual &&
191+
git log -1 --format="%G?%n%GK%n%GS%n%GF" $(cat forged1.commit) >actual &&
190192
test_cmp expect actual
191193
'
192194

@@ -195,8 +197,9 @@ test_expect_success GPG 'show untrusted signature with custom format' '
195197
U
196198
61092E85B7227189
197199
Eris Discordia <[email protected]>
200+
D4BE22311AD3131E5EDA29A461092E85B7227189
198201
EOF
199-
git log -1 --format="%G?%n%GK%n%GS" eighth-signed-alt >actual &&
202+
git log -1 --format="%G?%n%GK%n%GS%n%GF" eighth-signed-alt >actual &&
200203
test_cmp expect actual
201204
'
202205

@@ -205,8 +208,9 @@ test_expect_success GPG 'show unknown signature with custom format' '
205208
E
206209
61092E85B7227189
207210
211+
208212
EOF
209-
GNUPGHOME="$GNUPGHOME_NOT_USED" git log -1 --format="%G?%n%GK%n%GS" eighth-signed-alt >actual &&
213+
GNUPGHOME="$GNUPGHOME_NOT_USED" git log -1 --format="%G?%n%GK%n%GS%n%GF" eighth-signed-alt >actual &&
210214
test_cmp expect actual
211215
'
212216

@@ -215,8 +219,9 @@ test_expect_success GPG 'show lack of signature with custom format' '
215219
N
216220
217221
222+
218223
EOF
219-
git log -1 --format="%G?%n%GK%n%GS" seventh-unsigned >actual &&
224+
git log -1 --format="%G?%n%GK%n%GS%n%GF" seventh-unsigned >actual &&
220225
test_cmp expect actual
221226
'
222227

@@ -255,8 +260,9 @@ test_expect_success GPG 'show double signature with custom format' '
255260
E
256261
257262
263+
258264
EOF
259-
git log -1 --format="%G?%n%GK%n%GS" $(cat double-commit.commit) >actual &&
265+
git log -1 --format="%G?%n%GK%n%GS%n%GF" $(cat double-commit.commit) >actual &&
260266
test_cmp expect actual
261267
'
262268

0 commit comments

Comments
 (0)