2 #include "run-command.h"
4 #include "gpg-interface.h"
7 static char *configured_signing_key
;
9 void set_signing_key(const char *key
)
11 free(configured_signing_key
);
12 configured_signing_key
= xstrdup(key
);
15 int git_gpg_config(const char *var
, const char *value
, void *cb
)
17 if (!strcmp(var
, "user.signingkey")) {
19 return config_error_nonbool(var
);
20 set_signing_key(value
);
25 const char *get_signing_key(void)
27 if (configured_signing_key
)
28 return configured_signing_key
;
29 return git_committer_info(IDENT_ERROR_ON_NO_NAME
|IDENT_NO_DATE
);
33 * Create a detached signature for the contents of "buffer" and append
34 * it after "signature"; "buffer" and "signature" can be the same
35 * strbuf instance, which would cause the detached signature appended
38 int sign_buffer(struct strbuf
*buffer
, struct strbuf
*signature
, const char *signing_key
)
40 struct child_process gpg
;
45 memset(&gpg
, 0, sizeof(gpg
));
51 args
[2] = signing_key
;
54 if (start_command(&gpg
))
55 return error(_("could not run gpg."));
58 * When the username signingkey is bad, program could be terminated
59 * because gpg exits without reading and then write gets SIGPIPE.
61 sigchain_push(SIGPIPE
, SIG_IGN
);
63 if (write_in_full(gpg
.in
, buffer
->buf
, buffer
->len
) != buffer
->len
) {
67 return error(_("gpg did not accept the data"));
71 bottom
= signature
->len
;
72 len
= strbuf_read(signature
, gpg
.out
, 1024);
75 sigchain_pop(SIGPIPE
);
77 if (finish_command(&gpg
) || !len
|| len
< 0)
78 return error(_("gpg failed to sign the data"));
80 /* Strip CR from the line endings, in case we are on Windows. */
81 for (i
= j
= bottom
; i
< signature
->len
; i
++)
82 if (signature
->buf
[i
] != '\r') {
84 signature
->buf
[j
] = signature
->buf
[i
];
87 strbuf_setlen(signature
, j
);
93 * Run "gpg" to see if the payload matches the detached signature.
94 * gpg_output_to tells where the output from "gpg" should go:
96 * = 0: standard error of the calling process
97 * > 0: the specified file descriptor
99 int verify_signed_buffer(const char *payload
, size_t payload_size
,
100 const char *signature
, size_t signature_size
,
101 struct strbuf
*gpg_output
)
103 struct child_process gpg
;
104 const char *args_gpg
[] = {"gpg", "--verify", "FILE", "-", NULL
};
108 fd
= git_mkstemp(path
, PATH_MAX
, ".git_vtag_tmpXXXXXX");
110 return error("could not create temporary file '%s': %s",
111 path
, strerror(errno
));
112 if (write_in_full(fd
, signature
, signature_size
) < 0)
113 return error("failed writing detached signature to '%s': %s",
114 path
, strerror(errno
));
117 memset(&gpg
, 0, sizeof(gpg
));
123 if (start_command(&gpg
)) {
125 return error("could not run gpg.");
128 write_in_full(gpg
.in
, payload
, payload_size
);
132 strbuf_read(gpg_output
, gpg
.err
, 0);
133 ret
= finish_command(&gpg
);
135 unlink_or_warn(path
);