Commit | Line | Data |
---|---|---|
2f47eae2 JH |
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; | |
0c5e70f0 | 8 | static const char *gpg_program = "gpg"; |
2f47eae2 | 9 | |
01e57b5d MG |
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 | ||
2f47eae2 JH |
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")) { | |
0c5e70f0 JH |
31 | set_signing_key(value); |
32 | } | |
33 | if (!strcmp(var, "gpg.program")) { | |
2f47eae2 JH |
34 | if (!value) |
35 | return config_error_nonbool(var); | |
0c5e70f0 | 36 | gpg_program = xstrdup(value); |
2f47eae2 JH |
37 | } |
38 | return 0; | |
39 | } | |
40 | ||
41 | const char *get_signing_key(void) | |
42 | { | |
43 | if (configured_signing_key) | |
44 | return configured_signing_key; | |
f9bc573f | 45 | return git_committer_info(IDENT_STRICT|IDENT_NO_DATE); |
2f47eae2 JH |
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; | |
0c5e70f0 | 65 | args[0] = gpg_program; |
2f47eae2 JH |
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. | |
e3f55e07 | 110 | * gpg_output, when set, receives the diagnostic output from GPG. |
b60b7566 | 111 | * gpg_status, when set, receives the status output from GPG. |
2f47eae2 JH |
112 | */ |
113 | int verify_signed_buffer(const char *payload, size_t payload_size, | |
114 | const char *signature, size_t signature_size, | |
9cc4ac8f | 115 | struct strbuf *gpg_output, struct strbuf *gpg_status) |
2f47eae2 JH |
116 | { |
117 | struct child_process gpg; | |
b60b7566 | 118 | const char *args_gpg[] = {NULL, "--status-fd=1", "--verify", "FILE", "-", NULL}; |
2f47eae2 JH |
119 | char path[PATH_MAX]; |
120 | int fd, ret; | |
b60b7566 | 121 | struct strbuf buf = STRBUF_INIT; |
9cc4ac8f | 122 | struct strbuf *pbuf = &buf; |
2f47eae2 | 123 | |
0c5e70f0 | 124 | args_gpg[0] = gpg_program; |
2f47eae2 JH |
125 | fd = git_mkstemp(path, PATH_MAX, ".git_vtag_tmpXXXXXX"); |
126 | if (fd < 0) | |
4c9a4182 | 127 | return error(_("could not create temporary file '%s': %s"), |
2f47eae2 JH |
128 | path, strerror(errno)); |
129 | if (write_in_full(fd, signature, signature_size) < 0) | |
4c9a4182 | 130 | return error(_("failed writing detached signature to '%s': %s"), |
2f47eae2 JH |
131 | path, strerror(errno)); |
132 | close(fd); | |
133 | ||
134 | memset(&gpg, 0, sizeof(gpg)); | |
135 | gpg.argv = args_gpg; | |
136 | gpg.in = -1; | |
b60b7566 | 137 | gpg.out = -1; |
2f47eae2 JH |
138 | if (gpg_output) |
139 | gpg.err = -1; | |
b60b7566 | 140 | args_gpg[3] = path; |
2f47eae2 JH |
141 | if (start_command(&gpg)) { |
142 | unlink(path); | |
4c9a4182 | 143 | return error(_("could not run gpg.")); |
2f47eae2 JH |
144 | } |
145 | ||
146 | write_in_full(gpg.in, payload, payload_size); | |
147 | close(gpg.in); | |
148 | ||
7dac3f83 | 149 | if (gpg_output) { |
2f47eae2 | 150 | strbuf_read(gpg_output, gpg.err, 0); |
7dac3f83 SB |
151 | close(gpg.err); |
152 | } | |
9cc4ac8f MG |
153 | if (gpg_status) |
154 | pbuf = gpg_status; | |
155 | strbuf_read(pbuf, gpg.out, 0); | |
b60b7566 MG |
156 | close(gpg.out); |
157 | ||
2f47eae2 JH |
158 | ret = finish_command(&gpg); |
159 | ||
160 | unlink_or_warn(path); | |
161 | ||
9cc4ac8f MG |
162 | ret |= !strstr(pbuf->buf, "\n[GNUPG:] GOODSIG "); |
163 | strbuf_release(&buf); /* no matter it was used or not */ | |
b60b7566 | 164 | |
2f47eae2 JH |
165 | return ret; |
166 | } |