gpg-interface: provide clear helper for struct signature_check
[git] / gpg-interface.c
1 #include "cache.h"
2 #include "run-command.h"
3 #include "strbuf.h"
4 #include "gpg-interface.h"
5 #include "sigchain.h"
6
7 static char *configured_signing_key;
8 static const char *gpg_program = "gpg";
9
10 void signature_check_clear(struct signature_check *sigc)
11 {
12         free(sigc->gpg_output);
13         free(sigc->gpg_status);
14         free(sigc->signer);
15         free(sigc->key);
16         sigc->gpg_output = NULL;
17         sigc->gpg_status = NULL;
18         sigc->signer = NULL;
19         sigc->key = NULL;
20 }
21
22 void set_signing_key(const char *key)
23 {
24         free(configured_signing_key);
25         configured_signing_key = xstrdup(key);
26 }
27
28 int git_gpg_config(const char *var, const char *value, void *cb)
29 {
30         if (!strcmp(var, "user.signingkey")) {
31                 set_signing_key(value);
32         }
33         if (!strcmp(var, "gpg.program")) {
34                 if (!value)
35                         return config_error_nonbool(var);
36                 gpg_program = xstrdup(value);
37         }
38         return 0;
39 }
40
41 const char *get_signing_key(void)
42 {
43         if (configured_signing_key)
44                 return configured_signing_key;
45         return git_committer_info(IDENT_STRICT|IDENT_NO_DATE);
46 }
47
48 /*
49  * Create a detached signature for the contents of "buffer" and append
50  * it after "signature"; "buffer" and "signature" can be the same
51  * strbuf instance, which would cause the detached signature appended
52  * at the end.
53  */
54 int sign_buffer(struct strbuf *buffer, struct strbuf *signature, const char *signing_key)
55 {
56         struct child_process gpg;
57         const char *args[4];
58         ssize_t len;
59         size_t i, j, bottom;
60
61         memset(&gpg, 0, sizeof(gpg));
62         gpg.argv = args;
63         gpg.in = -1;
64         gpg.out = -1;
65         args[0] = gpg_program;
66         args[1] = "-bsau";
67         args[2] = signing_key;
68         args[3] = NULL;
69
70         if (start_command(&gpg))
71                 return error(_("could not run gpg."));
72
73         /*
74          * When the username signingkey is bad, program could be terminated
75          * because gpg exits without reading and then write gets SIGPIPE.
76          */
77         sigchain_push(SIGPIPE, SIG_IGN);
78
79         if (write_in_full(gpg.in, buffer->buf, buffer->len) != buffer->len) {
80                 close(gpg.in);
81                 close(gpg.out);
82                 finish_command(&gpg);
83                 return error(_("gpg did not accept the data"));
84         }
85         close(gpg.in);
86
87         bottom = signature->len;
88         len = strbuf_read(signature, gpg.out, 1024);
89         close(gpg.out);
90
91         sigchain_pop(SIGPIPE);
92
93         if (finish_command(&gpg) || !len || len < 0)
94                 return error(_("gpg failed to sign the data"));
95
96         /* Strip CR from the line endings, in case we are on Windows. */
97         for (i = j = bottom; i < signature->len; i++)
98                 if (signature->buf[i] != '\r') {
99                         if (i != j)
100                                 signature->buf[j] = signature->buf[i];
101                         j++;
102                 }
103         strbuf_setlen(signature, j);
104
105         return 0;
106 }
107
108 /*
109  * Run "gpg" to see if the payload matches the detached signature.
110  * gpg_output, when set, receives the diagnostic output from GPG.
111  * gpg_status, when set, receives the status output from GPG.
112  */
113 int verify_signed_buffer(const char *payload, size_t payload_size,
114                          const char *signature, size_t signature_size,
115                          struct strbuf *gpg_output, struct strbuf *gpg_status)
116 {
117         struct child_process gpg;
118         const char *args_gpg[] = {NULL, "--status-fd=1", "--verify", "FILE", "-", NULL};
119         char path[PATH_MAX];
120         int fd, ret;
121         struct strbuf buf = STRBUF_INIT;
122         struct strbuf *pbuf = &buf;
123
124         args_gpg[0] = gpg_program;
125         fd = git_mkstemp(path, PATH_MAX, ".git_vtag_tmpXXXXXX");
126         if (fd < 0)
127                 return error(_("could not create temporary file '%s': %s"),
128                              path, strerror(errno));
129         if (write_in_full(fd, signature, signature_size) < 0)
130                 return error(_("failed writing detached signature to '%s': %s"),
131                              path, strerror(errno));
132         close(fd);
133
134         memset(&gpg, 0, sizeof(gpg));
135         gpg.argv = args_gpg;
136         gpg.in = -1;
137         gpg.out = -1;
138         if (gpg_output)
139                 gpg.err = -1;
140         args_gpg[3] = path;
141         if (start_command(&gpg)) {
142                 unlink(path);
143                 return error(_("could not run gpg."));
144         }
145
146         write_in_full(gpg.in, payload, payload_size);
147         close(gpg.in);
148
149         if (gpg_output) {
150                 strbuf_read(gpg_output, gpg.err, 0);
151                 close(gpg.err);
152         }
153         if (gpg_status)
154                 pbuf = gpg_status;
155         strbuf_read(pbuf, gpg.out, 0);
156         close(gpg.out);
157
158         ret = finish_command(&gpg);
159
160         unlink_or_warn(path);
161
162         ret |= !strstr(pbuf->buf, "\n[GNUPG:] GOODSIG ");
163         strbuf_release(&buf); /* no matter it was used or not */
164
165         return ret;
166 }