gpg-interface: fix const-correctness of "eol" pointer
[git] / gpg-interface.c
1 #include "cache.h"
2 #include "config.h"
3 #include "run-command.h"
4 #include "strbuf.h"
5 #include "gpg-interface.h"
6 #include "sigchain.h"
7 #include "tempfile.h"
8
9 static char *configured_signing_key;
10 static const char *gpg_program = "gpg";
11
12 #define PGP_SIGNATURE "-----BEGIN PGP SIGNATURE-----"
13 #define PGP_MESSAGE "-----BEGIN PGP MESSAGE-----"
14
15 void signature_check_clear(struct signature_check *sigc)
16 {
17         FREE_AND_NULL(sigc->payload);
18         FREE_AND_NULL(sigc->gpg_output);
19         FREE_AND_NULL(sigc->gpg_status);
20         FREE_AND_NULL(sigc->signer);
21         FREE_AND_NULL(sigc->key);
22 }
23
24 static struct {
25         char result;
26         const char *check;
27 } sigcheck_gpg_status[] = {
28         { 'G', "\n[GNUPG:] GOODSIG " },
29         { 'B', "\n[GNUPG:] BADSIG " },
30         { 'U', "\n[GNUPG:] TRUST_NEVER" },
31         { 'U', "\n[GNUPG:] TRUST_UNDEFINED" },
32         { 'E', "\n[GNUPG:] ERRSIG "},
33         { 'X', "\n[GNUPG:] EXPSIG "},
34         { 'Y', "\n[GNUPG:] EXPKEYSIG "},
35         { 'R', "\n[GNUPG:] REVKEYSIG "},
36 };
37
38 void parse_gpg_output(struct signature_check *sigc)
39 {
40         const char *buf = sigc->gpg_status;
41         int i;
42
43         /* Iterate over all search strings */
44         for (i = 0; i < ARRAY_SIZE(sigcheck_gpg_status); i++) {
45                 const char *found, *next;
46
47                 if (!skip_prefix(buf, sigcheck_gpg_status[i].check + 1, &found)) {
48                         found = strstr(buf, sigcheck_gpg_status[i].check);
49                         if (!found)
50                                 continue;
51                         found += strlen(sigcheck_gpg_status[i].check);
52                 }
53                 sigc->result = sigcheck_gpg_status[i].result;
54                 /* The trust messages are not followed by key/signer information */
55                 if (sigc->result != 'U') {
56                         sigc->key = xmemdupz(found, 16);
57                         /* The ERRSIG message is not followed by signer information */
58                         if (sigc-> result != 'E') {
59                                 found += 17;
60                                 next = strchrnul(found, '\n');
61                                 sigc->signer = xmemdupz(found, next - found);
62                         }
63                 }
64         }
65 }
66
67 int check_signature(const char *payload, size_t plen, const char *signature,
68         size_t slen, struct signature_check *sigc)
69 {
70         struct strbuf gpg_output = STRBUF_INIT;
71         struct strbuf gpg_status = STRBUF_INIT;
72         int status;
73
74         sigc->result = 'N';
75
76         status = verify_signed_buffer(payload, plen, signature, slen,
77                                       &gpg_output, &gpg_status);
78         if (status && !gpg_output.len)
79                 goto out;
80         sigc->payload = xmemdupz(payload, plen);
81         sigc->gpg_output = strbuf_detach(&gpg_output, NULL);
82         sigc->gpg_status = strbuf_detach(&gpg_status, NULL);
83         parse_gpg_output(sigc);
84
85  out:
86         strbuf_release(&gpg_status);
87         strbuf_release(&gpg_output);
88
89         return sigc->result != 'G' && sigc->result != 'U';
90 }
91
92 void print_signature_buffer(const struct signature_check *sigc, unsigned flags)
93 {
94         const char *output = flags & GPG_VERIFY_RAW ?
95                 sigc->gpg_status : sigc->gpg_output;
96
97         if (flags & GPG_VERIFY_VERBOSE && sigc->payload)
98                 fputs(sigc->payload, stdout);
99
100         if (output)
101                 fputs(output, stderr);
102 }
103
104 size_t parse_signature(const char *buf, size_t size)
105 {
106         size_t len = 0;
107         while (len < size && !starts_with(buf + len, PGP_SIGNATURE) &&
108                         !starts_with(buf + len, PGP_MESSAGE)) {
109                 const char *eol = memchr(buf + len, '\n', size - len);
110                 len += eol ? eol - (buf + len) + 1 : size - len;
111         }
112         return len;
113 }
114
115 void set_signing_key(const char *key)
116 {
117         free(configured_signing_key);
118         configured_signing_key = xstrdup(key);
119 }
120
121 int git_gpg_config(const char *var, const char *value, void *cb)
122 {
123         if (!strcmp(var, "user.signingkey")) {
124                 if (!value)
125                         return config_error_nonbool(var);
126                 set_signing_key(value);
127                 return 0;
128         }
129
130         if (!strcmp(var, "gpg.program")) {
131                 if (!value)
132                         return config_error_nonbool(var);
133                 gpg_program = xstrdup(value);
134                 return 0;
135         }
136
137         return 0;
138 }
139
140 const char *get_signing_key(void)
141 {
142         if (configured_signing_key)
143                 return configured_signing_key;
144         return git_committer_info(IDENT_STRICT|IDENT_NO_DATE);
145 }
146
147 int sign_buffer(struct strbuf *buffer, struct strbuf *signature, const char *signing_key)
148 {
149         struct child_process gpg = CHILD_PROCESS_INIT;
150         int ret;
151         size_t i, j, bottom;
152         struct strbuf gpg_status = STRBUF_INIT;
153
154         argv_array_pushl(&gpg.args,
155                          gpg_program,
156                          "--status-fd=2",
157                          "-bsau", signing_key,
158                          NULL);
159
160         bottom = signature->len;
161
162         /*
163          * When the username signingkey is bad, program could be terminated
164          * because gpg exits without reading and then write gets SIGPIPE.
165          */
166         sigchain_push(SIGPIPE, SIG_IGN);
167         ret = pipe_command(&gpg, buffer->buf, buffer->len,
168                            signature, 1024, &gpg_status, 0);
169         sigchain_pop(SIGPIPE);
170
171         ret |= !strstr(gpg_status.buf, "\n[GNUPG:] SIG_CREATED ");
172         strbuf_release(&gpg_status);
173         if (ret)
174                 return error(_("gpg failed to sign the data"));
175
176         /* Strip CR from the line endings, in case we are on Windows. */
177         for (i = j = bottom; i < signature->len; i++)
178                 if (signature->buf[i] != '\r') {
179                         if (i != j)
180                                 signature->buf[j] = signature->buf[i];
181                         j++;
182                 }
183         strbuf_setlen(signature, j);
184
185         return 0;
186 }
187
188 int verify_signed_buffer(const char *payload, size_t payload_size,
189                          const char *signature, size_t signature_size,
190                          struct strbuf *gpg_output, struct strbuf *gpg_status)
191 {
192         struct child_process gpg = CHILD_PROCESS_INIT;
193         struct tempfile *temp;
194         int ret;
195         struct strbuf buf = STRBUF_INIT;
196
197         temp = mks_tempfile_t(".git_vtag_tmpXXXXXX");
198         if (!temp)
199                 return error_errno(_("could not create temporary file"));
200         if (write_in_full(temp->fd, signature, signature_size) < 0 ||
201             close_tempfile_gently(temp) < 0) {
202                 error_errno(_("failed writing detached signature to '%s'"),
203                             temp->filename.buf);
204                 delete_tempfile(&temp);
205                 return -1;
206         }
207
208         argv_array_pushl(&gpg.args,
209                          gpg_program,
210                          "--status-fd=1",
211                          "--keyid-format=long",
212                          "--verify", temp->filename.buf, "-",
213                          NULL);
214
215         if (!gpg_status)
216                 gpg_status = &buf;
217
218         sigchain_push(SIGPIPE, SIG_IGN);
219         ret = pipe_command(&gpg, payload, payload_size,
220                            gpg_status, 0, gpg_output, 0);
221         sigchain_pop(SIGPIPE);
222
223         delete_tempfile(&temp);
224
225         ret |= !strstr(gpg_status->buf, "\n[GNUPG:] GOODSIG ");
226         strbuf_release(&buf); /* no matter it was used or not */
227
228         return ret;
229 }