2 #include "run-command.h"
 
   4 #include "gpg-interface.h"
 
   7 static char *configured_signing_key;
 
   8 static const char *gpg_program = "gpg";
 
  10 void set_signing_key(const char *key)
 
  12         free(configured_signing_key);
 
  13         configured_signing_key = xstrdup(key);
 
  16 int git_gpg_config(const char *var, const char *value, void *cb)
 
  18         if (!strcmp(var, "user.signingkey")) {
 
  19                 set_signing_key(value);
 
  21         if (!strcmp(var, "gpg.program")) {
 
  23                         return config_error_nonbool(var);
 
  24                 gpg_program = xstrdup(value);
 
  29 const char *get_signing_key(void)
 
  31         if (configured_signing_key)
 
  32                 return configured_signing_key;
 
  33         return git_committer_info(IDENT_ERROR_ON_NO_NAME|IDENT_NO_DATE);
 
  37  * Create a detached signature for the contents of "buffer" and append
 
  38  * it after "signature"; "buffer" and "signature" can be the same
 
  39  * strbuf instance, which would cause the detached signature appended
 
  42 int sign_buffer(struct strbuf *buffer, struct strbuf *signature, const char *signing_key)
 
  44         struct child_process gpg;
 
  49         memset(&gpg, 0, sizeof(gpg));
 
  53         args[0] = gpg_program;
 
  55         args[2] = signing_key;
 
  58         if (start_command(&gpg))
 
  59                 return error(_("could not run gpg."));
 
  62          * When the username signingkey is bad, program could be terminated
 
  63          * because gpg exits without reading and then write gets SIGPIPE.
 
  65         sigchain_push(SIGPIPE, SIG_IGN);
 
  67         if (write_in_full(gpg.in, buffer->buf, buffer->len) != buffer->len) {
 
  71                 return error(_("gpg did not accept the data"));
 
  75         bottom = signature->len;
 
  76         len = strbuf_read(signature, gpg.out, 1024);
 
  79         sigchain_pop(SIGPIPE);
 
  81         if (finish_command(&gpg) || !len || len < 0)
 
  82                 return error(_("gpg failed to sign the data"));
 
  84         /* Strip CR from the line endings, in case we are on Windows. */
 
  85         for (i = j = bottom; i < signature->len; i++)
 
  86                 if (signature->buf[i] != '\r') {
 
  88                                 signature->buf[j] = signature->buf[i];
 
  91         strbuf_setlen(signature, j);
 
  97  * Run "gpg" to see if the payload matches the detached signature.
 
  98  * gpg_output, when set, receives the diagnostic output from GPG.
 
 100 int verify_signed_buffer(const char *payload, size_t payload_size,
 
 101                          const char *signature, size_t signature_size,
 
 102                          struct strbuf *gpg_output)
 
 104         struct child_process gpg;
 
 105         const char *args_gpg[] = {NULL, "--verify", "FILE", "-", NULL};
 
 109         args_gpg[0] = gpg_program;
 
 110         fd = git_mkstemp(path, PATH_MAX, ".git_vtag_tmpXXXXXX");
 
 112                 return error("could not create temporary file '%s': %s",
 
 113                              path, strerror(errno));
 
 114         if (write_in_full(fd, signature, signature_size) < 0)
 
 115                 return error("failed writing detached signature to '%s': %s",
 
 116                              path, strerror(errno));
 
 119         memset(&gpg, 0, sizeof(gpg));
 
 125         if (start_command(&gpg)) {
 
 127                 return error("could not run gpg.");
 
 130         write_in_full(gpg.in, payload, payload_size);
 
 134                 strbuf_read(gpg_output, gpg.err, 0);
 
 135         ret = finish_command(&gpg);
 
 137         unlink_or_warn(path);