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')
119 /* Skip lines that don't start with GNUPG status */
120 if (!skip_prefix(line
, "[GNUPG:] ", &line
))
123 /* Iterate over all search strings */
124 for (i
= 0; i
< ARRAY_SIZE(sigcheck_gpg_status
); i
++) {
125 if (skip_prefix(line
, sigcheck_gpg_status
[i
].check
, &line
)) {
126 if (sigcheck_gpg_status
[i
].flags
& GPG_STATUS_EXCLUSIVE
) {
127 if (seen_exclusive_status
++)
128 goto found_duplicate_status
;
131 if (sigcheck_gpg_status
[i
].result
)
132 sigc
->result
= sigcheck_gpg_status
[i
].result
;
133 /* Do we have key information? */
134 if (sigcheck_gpg_status
[i
].flags
& GPG_STATUS_KEYID
) {
135 next
= strchrnul(line
, ' ');
137 sigc
->key
= xmemdupz(line
, next
- line
);
138 /* Do we have signer information? */
139 if (*next
&& (sigcheck_gpg_status
[i
].flags
& GPG_STATUS_UID
)) {
141 next
= strchrnul(line
, '\n');
143 sigc
->signer
= xmemdupz(line
, next
- line
);
146 /* Do we have fingerprint? */
147 if (sigcheck_gpg_status
[i
].flags
& GPG_STATUS_FINGERPRINT
) {
148 next
= strchrnul(line
, ' ');
149 free(sigc
->fingerprint
);
150 sigc
->fingerprint
= xmemdupz(line
, next
- line
);
152 /* Skip interim fields */
153 for (j
= 9; j
> 0; j
--) {
157 next
= strchrnul(line
, ' ');
160 next
= strchrnul(line
, '\n');
161 free(sigc
->primary_key_fingerprint
);
162 sigc
->primary_key_fingerprint
= xmemdupz(line
, next
- line
);
171 found_duplicate_status
:
173 * GOODSIG, BADSIG etc. can occur only once for each signature.
174 * Therefore, if we had more than one then we're dealing with multiple
175 * signatures. We don't support them currently, and they're rather
176 * hard to create, so something is likely fishy and we should reject
180 /* Clear partial data to avoid confusion */
181 FREE_AND_NULL(sigc
->primary_key_fingerprint
);
182 FREE_AND_NULL(sigc
->fingerprint
);
183 FREE_AND_NULL(sigc
->signer
);
184 FREE_AND_NULL(sigc
->key
);
187 int check_signature(const char *payload
, size_t plen
, const char *signature
,
188 size_t slen
, struct signature_check
*sigc
)
190 struct strbuf gpg_output
= STRBUF_INIT
;
191 struct strbuf gpg_status
= STRBUF_INIT
;
196 status
= verify_signed_buffer(payload
, plen
, signature
, slen
,
197 &gpg_output
, &gpg_status
);
198 if (status
&& !gpg_output
.len
)
200 sigc
->payload
= xmemdupz(payload
, plen
);
201 sigc
->gpg_output
= strbuf_detach(&gpg_output
, NULL
);
202 sigc
->gpg_status
= strbuf_detach(&gpg_status
, NULL
);
203 parse_gpg_output(sigc
);
204 status
|= sigc
->result
!= 'G' && sigc
->result
!= 'U';
207 strbuf_release(&gpg_status
);
208 strbuf_release(&gpg_output
);
213 void print_signature_buffer(const struct signature_check
*sigc
, unsigned flags
)
215 const char *output
= flags
& GPG_VERIFY_RAW
?
216 sigc
->gpg_status
: sigc
->gpg_output
;
218 if (flags
& GPG_VERIFY_VERBOSE
&& sigc
->payload
)
219 fputs(sigc
->payload
, stdout
);
222 fputs(output
, stderr
);
225 size_t parse_signature(const char *buf
, size_t size
)
232 if (get_format_by_sig(buf
+ len
))
235 eol
= memchr(buf
+ len
, '\n', size
- len
);
236 len
+= eol
? eol
- (buf
+ len
) + 1 : size
- len
;
241 void set_signing_key(const char *key
)
243 free(configured_signing_key
);
244 configured_signing_key
= xstrdup(key
);
247 int git_gpg_config(const char *var
, const char *value
, void *cb
)
249 struct gpg_format
*fmt
= NULL
;
250 char *fmtname
= NULL
;
252 if (!strcmp(var
, "user.signingkey")) {
254 return config_error_nonbool(var
);
255 set_signing_key(value
);
259 if (!strcmp(var
, "gpg.format")) {
261 return config_error_nonbool(var
);
262 fmt
= get_format_by_name(value
);
264 return error("unsupported value for %s: %s",
270 if (!strcmp(var
, "gpg.program") || !strcmp(var
, "gpg.openpgp.program"))
273 if (!strcmp(var
, "gpg.x509.program"))
277 fmt
= get_format_by_name(fmtname
);
278 return git_config_string(&fmt
->program
, var
, value
);
284 const char *get_signing_key(void)
286 if (configured_signing_key
)
287 return configured_signing_key
;
288 return git_committer_info(IDENT_STRICT
|IDENT_NO_DATE
);
291 int sign_buffer(struct strbuf
*buffer
, struct strbuf
*signature
, const char *signing_key
)
293 struct child_process gpg
= CHILD_PROCESS_INIT
;
296 struct strbuf gpg_status
= STRBUF_INIT
;
298 argv_array_pushl(&gpg
.args
,
301 "-bsau", signing_key
,
304 bottom
= signature
->len
;
307 * When the username signingkey is bad, program could be terminated
308 * because gpg exits without reading and then write gets SIGPIPE.
310 sigchain_push(SIGPIPE
, SIG_IGN
);
311 ret
= pipe_command(&gpg
, buffer
->buf
, buffer
->len
,
312 signature
, 1024, &gpg_status
, 0);
313 sigchain_pop(SIGPIPE
);
315 ret
|= !strstr(gpg_status
.buf
, "\n[GNUPG:] SIG_CREATED ");
316 strbuf_release(&gpg_status
);
318 return error(_("gpg failed to sign the data"));
320 /* Strip CR from the line endings, in case we are on Windows. */
321 for (i
= j
= bottom
; i
< signature
->len
; i
++)
322 if (signature
->buf
[i
] != '\r') {
324 signature
->buf
[j
] = signature
->buf
[i
];
327 strbuf_setlen(signature
, j
);
332 int verify_signed_buffer(const char *payload
, size_t payload_size
,
333 const char *signature
, size_t signature_size
,
334 struct strbuf
*gpg_output
, struct strbuf
*gpg_status
)
336 struct child_process gpg
= CHILD_PROCESS_INIT
;
337 struct gpg_format
*fmt
;
338 struct tempfile
*temp
;
340 struct strbuf buf
= STRBUF_INIT
;
342 temp
= mks_tempfile_t(".git_vtag_tmpXXXXXX");
344 return error_errno(_("could not create temporary file"));
345 if (write_in_full(temp
->fd
, signature
, signature_size
) < 0 ||
346 close_tempfile_gently(temp
) < 0) {
347 error_errno(_("failed writing detached signature to '%s'"),
349 delete_tempfile(&temp
);
353 fmt
= get_format_by_sig(signature
);
355 BUG("bad signature '%s'", signature
);
357 argv_array_push(&gpg
.args
, fmt
->program
);
358 argv_array_pushv(&gpg
.args
, fmt
->verify_args
);
359 argv_array_pushl(&gpg
.args
,
361 "--verify", temp
->filename
.buf
, "-",
367 sigchain_push(SIGPIPE
, SIG_IGN
);
368 ret
= pipe_command(&gpg
, payload
, payload_size
,
369 gpg_status
, 0, gpg_output
, 0);
370 sigchain_pop(SIGPIPE
);
372 delete_tempfile(&temp
);
374 ret
|= !strstr(gpg_status
->buf
, "\n[GNUPG:] GOODSIG ");
375 strbuf_release(&buf
); /* no matter it was used or not */