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
);
81 } sigcheck_gpg_status
[] = {
82 { 'G', "\n[GNUPG:] GOODSIG " },
83 { 'B', "\n[GNUPG:] BADSIG " },
84 { 'U', "\n[GNUPG:] TRUST_NEVER" },
85 { 'U', "\n[GNUPG:] TRUST_UNDEFINED" },
86 { 'E', "\n[GNUPG:] ERRSIG "},
87 { 'X', "\n[GNUPG:] EXPSIG "},
88 { 'Y', "\n[GNUPG:] EXPKEYSIG "},
89 { 'R', "\n[GNUPG:] REVKEYSIG "},
92 static void parse_gpg_output(struct signature_check
*sigc
)
94 const char *buf
= sigc
->gpg_status
;
97 /* Iterate over all search strings */
98 for (i
= 0; i
< ARRAY_SIZE(sigcheck_gpg_status
); i
++) {
99 const char *found
, *next
;
101 if (!skip_prefix(buf
, sigcheck_gpg_status
[i
].check
+ 1, &found
)) {
102 found
= strstr(buf
, sigcheck_gpg_status
[i
].check
);
105 found
+= strlen(sigcheck_gpg_status
[i
].check
);
107 sigc
->result
= sigcheck_gpg_status
[i
].result
;
108 /* The trust messages are not followed by key/signer information */
109 if (sigc
->result
!= 'U') {
110 next
= strchrnul(found
, ' ');
111 sigc
->key
= xmemdupz(found
, next
- found
);
112 /* The ERRSIG message is not followed by signer information */
113 if (*next
&& sigc
-> result
!= 'E') {
115 next
= strchrnul(found
, '\n');
116 sigc
->signer
= xmemdupz(found
, next
- found
);
122 int check_signature(const char *payload
, size_t plen
, const char *signature
,
123 size_t slen
, struct signature_check
*sigc
)
125 struct strbuf gpg_output
= STRBUF_INIT
;
126 struct strbuf gpg_status
= STRBUF_INIT
;
131 status
= verify_signed_buffer(payload
, plen
, signature
, slen
,
132 &gpg_output
, &gpg_status
);
133 if (status
&& !gpg_output
.len
)
135 sigc
->payload
= xmemdupz(payload
, plen
);
136 sigc
->gpg_output
= strbuf_detach(&gpg_output
, NULL
);
137 sigc
->gpg_status
= strbuf_detach(&gpg_status
, NULL
);
138 parse_gpg_output(sigc
);
141 strbuf_release(&gpg_status
);
142 strbuf_release(&gpg_output
);
144 return sigc
->result
!= 'G' && sigc
->result
!= 'U';
147 void print_signature_buffer(const struct signature_check
*sigc
, unsigned flags
)
149 const char *output
= flags
& GPG_VERIFY_RAW
?
150 sigc
->gpg_status
: sigc
->gpg_output
;
152 if (flags
& GPG_VERIFY_VERBOSE
&& sigc
->payload
)
153 fputs(sigc
->payload
, stdout
);
156 fputs(output
, stderr
);
159 size_t parse_signature(const char *buf
, size_t size
)
166 if (get_format_by_sig(buf
+ len
))
169 eol
= memchr(buf
+ len
, '\n', size
- len
);
170 len
+= eol
? eol
- (buf
+ len
) + 1 : size
- len
;
175 void set_signing_key(const char *key
)
177 free(configured_signing_key
);
178 configured_signing_key
= xstrdup(key
);
181 int git_gpg_config(const char *var
, const char *value
, void *cb
)
183 struct gpg_format
*fmt
= NULL
;
184 char *fmtname
= NULL
;
186 if (!strcmp(var
, "user.signingkey")) {
188 return config_error_nonbool(var
);
189 set_signing_key(value
);
193 if (!strcmp(var
, "gpg.format")) {
195 return config_error_nonbool(var
);
196 fmt
= get_format_by_name(value
);
198 return error("unsupported value for %s: %s",
204 if (!strcmp(var
, "gpg.program") || !strcmp(var
, "gpg.openpgp.program"))
207 if (!strcmp(var
, "gpg.x509.program"))
211 fmt
= get_format_by_name(fmtname
);
212 return git_config_string(&fmt
->program
, var
, value
);
218 const char *get_signing_key(void)
220 if (configured_signing_key
)
221 return configured_signing_key
;
222 return git_committer_info(IDENT_STRICT
|IDENT_NO_DATE
);
225 int sign_buffer(struct strbuf
*buffer
, struct strbuf
*signature
, const char *signing_key
)
227 struct child_process gpg
= CHILD_PROCESS_INIT
;
230 struct strbuf gpg_status
= STRBUF_INIT
;
232 argv_array_pushl(&gpg
.args
,
235 "-bsau", signing_key
,
238 bottom
= signature
->len
;
241 * When the username signingkey is bad, program could be terminated
242 * because gpg exits without reading and then write gets SIGPIPE.
244 sigchain_push(SIGPIPE
, SIG_IGN
);
245 ret
= pipe_command(&gpg
, buffer
->buf
, buffer
->len
,
246 signature
, 1024, &gpg_status
, 0);
247 sigchain_pop(SIGPIPE
);
249 ret
|= !strstr(gpg_status
.buf
, "\n[GNUPG:] SIG_CREATED ");
250 strbuf_release(&gpg_status
);
252 return error(_("gpg failed to sign the data"));
254 /* Strip CR from the line endings, in case we are on Windows. */
255 for (i
= j
= bottom
; i
< signature
->len
; i
++)
256 if (signature
->buf
[i
] != '\r') {
258 signature
->buf
[j
] = signature
->buf
[i
];
261 strbuf_setlen(signature
, j
);
266 int verify_signed_buffer(const char *payload
, size_t payload_size
,
267 const char *signature
, size_t signature_size
,
268 struct strbuf
*gpg_output
, struct strbuf
*gpg_status
)
270 struct child_process gpg
= CHILD_PROCESS_INIT
;
271 struct gpg_format
*fmt
;
272 struct tempfile
*temp
;
274 struct strbuf buf
= STRBUF_INIT
;
276 temp
= mks_tempfile_t(".git_vtag_tmpXXXXXX");
278 return error_errno(_("could not create temporary file"));
279 if (write_in_full(temp
->fd
, signature
, signature_size
) < 0 ||
280 close_tempfile_gently(temp
) < 0) {
281 error_errno(_("failed writing detached signature to '%s'"),
283 delete_tempfile(&temp
);
287 fmt
= get_format_by_sig(signature
);
289 BUG("bad signature '%s'", signature
);
291 argv_array_push(&gpg
.args
, fmt
->program
);
292 argv_array_pushv(&gpg
.args
, fmt
->verify_args
);
293 argv_array_pushl(&gpg
.args
,
295 "--verify", temp
->filename
.buf
, "-",
301 sigchain_push(SIGPIPE
, SIG_IGN
);
302 ret
= pipe_command(&gpg
, payload
, payload_size
,
303 gpg_status
, 0, gpg_output
, 0);
304 sigchain_pop(SIGPIPE
);
306 delete_tempfile(&temp
);
308 ret
|= !strstr(gpg_status
->buf
, "\n[GNUPG:] GOODSIG ");
309 strbuf_release(&buf
); /* no matter it was used or not */