3 #include "run-command.h"
5 #include "gpg-interface.h"
9 static char *configured_signing_key
;
13 const char **verify_args
;
17 static const char *openpgp_verify_args
[] = {
18 "--keyid-format=long",
21 static const char *openpgp_sigs
[] = {
22 "-----BEGIN PGP SIGNATURE-----",
23 "-----BEGIN PGP MESSAGE-----",
27 static const char *x509_verify_args
[] = {
30 static const char *x509_sigs
[] = {
31 "-----BEGIN SIGNED MESSAGE-----",
35 static struct gpg_format gpg_format
[] = {
36 { .name
= "openpgp", .program
= "gpg",
37 .verify_args
= openpgp_verify_args
,
40 { .name
= "x509", .program
= "gpgsm",
41 .verify_args
= x509_verify_args
,
46 static struct gpg_format
*use_format
= &gpg_format
[0];
48 static struct gpg_format
*get_format_by_name(const char *str
)
52 for (i
= 0; i
< ARRAY_SIZE(gpg_format
); i
++)
53 if (!strcmp(gpg_format
[i
].name
, str
))
54 return gpg_format
+ i
;
58 static struct gpg_format
*get_format_by_sig(const char *sig
)
62 for (i
= 0; i
< ARRAY_SIZE(gpg_format
); i
++)
63 for (j
= 0; gpg_format
[i
].sigs
[j
]; j
++)
64 if (starts_with(sig
, gpg_format
[i
].sigs
[j
]))
65 return gpg_format
+ i
;
69 void signature_check_clear(struct signature_check
*sigc
)
71 FREE_AND_NULL(sigc
->payload
);
72 FREE_AND_NULL(sigc
->gpg_output
);
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 FREE_AND_NULL(sigc
->primary_key_fingerprint
);
80 /* An exclusive status -- only one of them can appear in output */
81 #define GPG_STATUS_EXCLUSIVE (1<<0)
82 /* The status includes key identifier */
83 #define GPG_STATUS_KEYID (1<<1)
84 /* The status includes user identifier */
85 #define GPG_STATUS_UID (1<<2)
86 /* The status includes key fingerprints */
87 #define GPG_STATUS_FINGERPRINT (1<<3)
89 /* Short-hand for standard exclusive *SIG status with keyid & UID */
90 #define GPG_STATUS_STDSIG (GPG_STATUS_EXCLUSIVE|GPG_STATUS_KEYID|GPG_STATUS_UID)
96 } sigcheck_gpg_status
[] = {
97 { 'G', "GOODSIG ", GPG_STATUS_STDSIG
},
98 { 'B', "BADSIG ", GPG_STATUS_STDSIG
},
99 { 'U', "TRUST_NEVER", 0 },
100 { 'U', "TRUST_UNDEFINED", 0 },
101 { 'E', "ERRSIG ", GPG_STATUS_EXCLUSIVE
|GPG_STATUS_KEYID
},
102 { 'X', "EXPSIG ", GPG_STATUS_STDSIG
},
103 { 'Y', "EXPKEYSIG ", GPG_STATUS_STDSIG
},
104 { 'R', "REVKEYSIG ", GPG_STATUS_STDSIG
},
105 { 0, "VALIDSIG ", GPG_STATUS_FINGERPRINT
},
108 static void parse_gpg_output(struct signature_check
*sigc
)
110 const char *buf
= sigc
->gpg_status
;
111 const char *line
, *next
;
113 int seen_exclusive_status
= 0;
115 /* Iterate over all lines */
116 for (line
= buf
; *line
; line
= strchrnul(line
+1, '\n')) {
117 while (*line
== '\n')
122 /* Skip lines that don't start with GNUPG status */
123 if (!skip_prefix(line
, "[GNUPG:] ", &line
))
126 /* Iterate over all search strings */
127 for (i
= 0; i
< ARRAY_SIZE(sigcheck_gpg_status
); i
++) {
128 if (skip_prefix(line
, sigcheck_gpg_status
[i
].check
, &line
)) {
129 if (sigcheck_gpg_status
[i
].flags
& GPG_STATUS_EXCLUSIVE
) {
130 if (seen_exclusive_status
++)
131 goto found_duplicate_status
;
134 if (sigcheck_gpg_status
[i
].result
)
135 sigc
->result
= sigcheck_gpg_status
[i
].result
;
136 /* Do we have key information? */
137 if (sigcheck_gpg_status
[i
].flags
& GPG_STATUS_KEYID
) {
138 next
= strchrnul(line
, ' ');
140 sigc
->key
= xmemdupz(line
, next
- line
);
141 /* Do we have signer information? */
142 if (*next
&& (sigcheck_gpg_status
[i
].flags
& GPG_STATUS_UID
)) {
144 next
= strchrnul(line
, '\n');
146 sigc
->signer
= xmemdupz(line
, next
- line
);
149 /* Do we have fingerprint? */
150 if (sigcheck_gpg_status
[i
].flags
& GPG_STATUS_FINGERPRINT
) {
151 next
= strchrnul(line
, ' ');
152 free(sigc
->fingerprint
);
153 sigc
->fingerprint
= xmemdupz(line
, next
- line
);
155 /* Skip interim fields */
156 for (j
= 9; j
> 0; j
--) {
160 next
= strchrnul(line
, ' ');
163 next
= strchrnul(line
, '\n');
164 free(sigc
->primary_key_fingerprint
);
165 sigc
->primary_key_fingerprint
= xmemdupz(line
, next
- line
);
174 found_duplicate_status
:
176 * GOODSIG, BADSIG etc. can occur only once for each signature.
177 * Therefore, if we had more than one then we're dealing with multiple
178 * signatures. We don't support them currently, and they're rather
179 * hard to create, so something is likely fishy and we should reject
183 /* Clear partial data to avoid confusion */
184 FREE_AND_NULL(sigc
->primary_key_fingerprint
);
185 FREE_AND_NULL(sigc
->fingerprint
);
186 FREE_AND_NULL(sigc
->signer
);
187 FREE_AND_NULL(sigc
->key
);
190 int check_signature(const char *payload
, size_t plen
, const char *signature
,
191 size_t slen
, struct signature_check
*sigc
)
193 struct strbuf gpg_output
= STRBUF_INIT
;
194 struct strbuf gpg_status
= STRBUF_INIT
;
199 status
= verify_signed_buffer(payload
, plen
, signature
, slen
,
200 &gpg_output
, &gpg_status
);
201 if (status
&& !gpg_output
.len
)
203 sigc
->payload
= xmemdupz(payload
, plen
);
204 sigc
->gpg_output
= strbuf_detach(&gpg_output
, NULL
);
205 sigc
->gpg_status
= strbuf_detach(&gpg_status
, NULL
);
206 parse_gpg_output(sigc
);
207 status
|= sigc
->result
!= 'G' && sigc
->result
!= 'U';
210 strbuf_release(&gpg_status
);
211 strbuf_release(&gpg_output
);
216 void print_signature_buffer(const struct signature_check
*sigc
, unsigned flags
)
218 const char *output
= flags
& GPG_VERIFY_RAW
?
219 sigc
->gpg_status
: sigc
->gpg_output
;
221 if (flags
& GPG_VERIFY_VERBOSE
&& sigc
->payload
)
222 fputs(sigc
->payload
, stdout
);
225 fputs(output
, stderr
);
228 size_t parse_signature(const char *buf
, size_t size
)
235 if (get_format_by_sig(buf
+ len
))
238 eol
= memchr(buf
+ len
, '\n', size
- len
);
239 len
+= eol
? eol
- (buf
+ len
) + 1 : size
- len
;
244 void set_signing_key(const char *key
)
246 free(configured_signing_key
);
247 configured_signing_key
= xstrdup(key
);
250 int git_gpg_config(const char *var
, const char *value
, void *cb
)
252 struct gpg_format
*fmt
= NULL
;
253 char *fmtname
= NULL
;
255 if (!strcmp(var
, "user.signingkey")) {
257 return config_error_nonbool(var
);
258 set_signing_key(value
);
262 if (!strcmp(var
, "gpg.format")) {
264 return config_error_nonbool(var
);
265 fmt
= get_format_by_name(value
);
267 return error("unsupported value for %s: %s",
273 if (!strcmp(var
, "gpg.program") || !strcmp(var
, "gpg.openpgp.program"))
276 if (!strcmp(var
, "gpg.x509.program"))
280 fmt
= get_format_by_name(fmtname
);
281 return git_config_string(&fmt
->program
, var
, value
);
287 const char *get_signing_key(void)
289 if (configured_signing_key
)
290 return configured_signing_key
;
291 return git_committer_info(IDENT_STRICT
|IDENT_NO_DATE
);
294 int sign_buffer(struct strbuf
*buffer
, struct strbuf
*signature
, const char *signing_key
)
296 struct child_process gpg
= CHILD_PROCESS_INIT
;
299 struct strbuf gpg_status
= STRBUF_INIT
;
301 argv_array_pushl(&gpg
.args
,
304 "-bsau", signing_key
,
307 bottom
= signature
->len
;
310 * When the username signingkey is bad, program could be terminated
311 * because gpg exits without reading and then write gets SIGPIPE.
313 sigchain_push(SIGPIPE
, SIG_IGN
);
314 ret
= pipe_command(&gpg
, buffer
->buf
, buffer
->len
,
315 signature
, 1024, &gpg_status
, 0);
316 sigchain_pop(SIGPIPE
);
318 ret
|= !strstr(gpg_status
.buf
, "\n[GNUPG:] SIG_CREATED ");
319 strbuf_release(&gpg_status
);
321 return error(_("gpg failed to sign the data"));
323 /* Strip CR from the line endings, in case we are on Windows. */
324 for (i
= j
= bottom
; i
< signature
->len
; i
++)
325 if (signature
->buf
[i
] != '\r') {
327 signature
->buf
[j
] = signature
->buf
[i
];
330 strbuf_setlen(signature
, j
);
335 int verify_signed_buffer(const char *payload
, size_t payload_size
,
336 const char *signature
, size_t signature_size
,
337 struct strbuf
*gpg_output
, struct strbuf
*gpg_status
)
339 struct child_process gpg
= CHILD_PROCESS_INIT
;
340 struct gpg_format
*fmt
;
341 struct tempfile
*temp
;
343 struct strbuf buf
= STRBUF_INIT
;
345 temp
= mks_tempfile_t(".git_vtag_tmpXXXXXX");
347 return error_errno(_("could not create temporary file"));
348 if (write_in_full(temp
->fd
, signature
, signature_size
) < 0 ||
349 close_tempfile_gently(temp
) < 0) {
350 error_errno(_("failed writing detached signature to '%s'"),
352 delete_tempfile(&temp
);
356 fmt
= get_format_by_sig(signature
);
358 BUG("bad signature '%s'", signature
);
360 argv_array_push(&gpg
.args
, fmt
->program
);
361 argv_array_pushv(&gpg
.args
, fmt
->verify_args
);
362 argv_array_pushl(&gpg
.args
,
364 "--verify", temp
->filename
.buf
, "-",
370 sigchain_push(SIGPIPE
, SIG_IGN
);
371 ret
= pipe_command(&gpg
, payload
, payload_size
,
372 gpg_status
, 0, gpg_output
, 0);
373 sigchain_pop(SIGPIPE
);
375 delete_tempfile(&temp
);
377 ret
|= !strstr(gpg_status
->buf
, "\n[GNUPG:] GOODSIG ");
378 strbuf_release(&buf
); /* no matter it was used or not */