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 <mgorny@gentoo.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michał Górny committed Oct 22, 2018 at 18:38 UTC 3daaaabe7ed22c17bff04d19c711be427bd2e225
5 files changed +31 -7
Documentation/pretty-formats.txt
+1
@@ -153,6 +153,7 @@ endif::git-rev-list[]
153 and "N" for no signature
154 - '%GS': show the name of the signer for a signed commit
155 - '%GK': show the key used to sign a signed commit
156 +- '%GF': show the fingerprint of the key used to sign a signed commit
157 - '%gD': reflog selector, e.g., `refs/stash@{1}` or
158 `refs/stash@{2 minutes ago`}; the format follows the rules described
159 for the `-g` option. The portion before the `@` is the refname as
gpg-interface.c
+13 -1
@@ -73,6 +73,7 @@ void signature_check_clear(struct signature_check *sigc)
73 FREE_AND_NULL(sigc->gpg_status);
74 FREE_AND_NULL(sigc->signer);
75 FREE_AND_NULL(sigc->key);
76 + FREE_AND_NULL(sigc->fingerprint);
77 }
78
79 /* An exclusive status -- only one of them can appear in output */
@@ -81,6 +82,8 @@ void signature_check_clear(struct signature_check *sigc)
82 #define GPG_STATUS_KEYID (1<<1)
83 /* The status includes user identifier */
84 #define GPG_STATUS_UID (1<<2)
85 +/* The status includes key fingerprints */
86 +#define GPG_STATUS_FINGERPRINT (1<<3)
87
88 /* Short-hand for standard exclusive *SIG status with keyid & UID */
89 #define GPG_STATUS_STDSIG (GPG_STATUS_EXCLUSIVE|GPG_STATUS_KEYID|GPG_STATUS_UID)
@@ -98,6 +101,7 @@ static struct {
101 { 'X', "EXPSIG ", GPG_STATUS_STDSIG },
102 { 'Y', "EXPKEYSIG ", GPG_STATUS_STDSIG },
103 { 'R', "REVKEYSIG ", GPG_STATUS_STDSIG },
104 + { 0, "VALIDSIG ", GPG_STATUS_FINGERPRINT },
105 };
106
107 static void parse_gpg_output(struct signature_check *sigc)
@@ -123,7 +127,8 @@ static void parse_gpg_output(struct signature_check *sigc)
127 goto found_duplicate_status;
128 }
129
126 - sigc->result = sigcheck_gpg_status[i].result;
130 + if (sigcheck_gpg_status[i].result)
131 + sigc->result = sigcheck_gpg_status[i].result;
132 /* Do we have key information? */
133 if (sigcheck_gpg_status[i].flags & GPG_STATUS_KEYID) {
134 next = strchrnul(line, ' ');
@@ -137,6 +142,12 @@ static void parse_gpg_output(struct signature_check *sigc)
142 sigc->signer = xmemdupz(line, next - line);
143 }
144 }
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 + }
151
152 break;
153 }
@@ -154,6 +165,7 @@ found_duplicate_status:
165 */
166 sigc->result = 'E';
167 /* Clear partial data to avoid confusion */
168 + FREE_AND_NULL(sigc->fingerprint);
169 FREE_AND_NULL(sigc->signer);
170 FREE_AND_NULL(sigc->key);
171 }
gpg-interface.h
+1
@@ -23,6 +23,7 @@ struct signature_check {
23 char result;
24 char *signer;
25 char *key;
26 + char *fingerprint;
27 };
28
29 void signature_check_clear(struct signature_check *sigc);
pretty.c
+4
@@ -1256,6 +1256,10 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
1256 if (c->signature_check.key)
1257 strbuf_addstr(sb, c->signature_check.key);
1258 break;
1259 + case 'F':
1260 + if (c->signature_check.fingerprint)
1261 + strbuf_addstr(sb, c->signature_check.fingerprint);
1262 + break;
1263 default:
1264 return 0;
1265 }
t/t7510-signed-commit.sh
+12 -6
@@ -175,8 +175,9 @@ test_expect_success GPG 'show good signature with custom format' '
175 G
176 13B6F51ECDDE430D
177 C O Mitter <committer@example.com>
178 + 73D758744BE721698EC54E8713B6F51ECDDE430D
179 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 &&
181 test_cmp expect actual
182 '
183
@@ -185,8 +186,9 @@ test_expect_success GPG 'show bad signature with custom format' '
186 B
187 13B6F51ECDDE430D
188 C O Mitter <committer@example.com>
189 +
190 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 &&
192 test_cmp expect actual
193 '
194
@@ -195,8 +197,9 @@ test_expect_success GPG 'show untrusted signature with custom format' '
197 U
198 61092E85B7227189
199 Eris Discordia <discord@example.net>
200 + D4BE22311AD3131E5EDA29A461092E85B7227189
201 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 &&
203 test_cmp expect actual
204 '
205
@@ -205,8 +208,9 @@ test_expect_success GPG 'show unknown signature with custom format' '
208 E
209 61092E85B7227189
210
211 +
212 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 &&
214 test_cmp expect actual
215 '
216
@@ -215,8 +219,9 @@ test_expect_success GPG 'show lack of signature with custom format' '
219 N
220
221
222 +
223 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 &&
225 test_cmp expect actual
226 '
227
@@ -255,8 +260,9 @@ test_expect_success GPG 'show double signature with custom format' '
260 E
261
262
263 +
264 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 &&
266 test_cmp expect actual
267 '
268