]> git.saurik.com Git - apt.git/blob - methods/gpgv.cc
de9dfea1eb943150f89923444cac34ce78e5e523
[apt.git] / methods / gpgv.cc
1 #include <config.h>
2
3 #include <apt-pkg/acquire-method.h>
4 #include <apt-pkg/configuration.h>
5 #include <apt-pkg/error.h>
6 #include <apt-pkg/gpgv.h>
7 #include <apt-pkg/strutl.h>
8 #include <apt-pkg/fileutl.h>
9 #include "aptmethod.h"
10
11 #include <ctype.h>
12 #include <errno.h>
13 #include <stddef.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <sys/wait.h>
18 #include <unistd.h>
19
20 #include <array>
21 #include <algorithm>
22 #include <sstream>
23 #include <iterator>
24 #include <iostream>
25 #include <string>
26 #include <vector>
27
28 #include <apti18n.h>
29
30 using std::string;
31 using std::vector;
32
33 #define GNUPGPREFIX "[GNUPG:]"
34 #define GNUPGBADSIG "[GNUPG:] BADSIG"
35 #define GNUPGNOPUBKEY "[GNUPG:] NO_PUBKEY"
36 #define GNUPGVALIDSIG "[GNUPG:] VALIDSIG"
37 #define GNUPGGOODSIG "[GNUPG:] GOODSIG"
38 #define GNUPGKEYEXPIRED "[GNUPG:] KEYEXPIRED"
39 #define GNUPGREVKEYSIG "[GNUPG:] REVKEYSIG"
40 #define GNUPGNODATA "[GNUPG:] NODATA"
41
42 static const std::array<string, 1> WeakDigests {
43 "1", // MD5
44 // "2", // SHA1
45 // "3", // RIPEMD-160
46 };
47
48 class GPGVMethod : public aptMethod
49 {
50 private:
51 string VerifyGetSigners(const char *file, const char *outfile,
52 std::string const &key,
53 vector<string> &GoodSigners,
54 vector<string> &BadSigners,
55 vector<string> &WorthlessSigners,
56 vector<string> &NoPubKeySigners);
57
58 protected:
59 virtual bool URIAcquire(std::string const &Message, FetchItem *Itm) APT_OVERRIDE;
60 public:
61
62 GPGVMethod() : aptMethod("gpgv","1.0",SingleInstance | SendConfig) {};
63 };
64
65 string GPGVMethod::VerifyGetSigners(const char *file, const char *outfile,
66 std::string const &key,
67 vector<string> &GoodSigners,
68 vector<string> &BadSigners,
69 vector<string> &WorthlessSigners,
70 vector<string> &NoPubKeySigners)
71 {
72 bool const Debug = _config->FindB("Debug::Acquire::gpgv", false);
73
74 if (Debug == true)
75 std::clog << "inside VerifyGetSigners" << std::endl;
76
77 int fd[2];
78 bool const keyIsID = (key.empty() == false && key[0] != '/');
79
80 if (pipe(fd) < 0)
81 return "Couldn't create pipe";
82
83 pid_t pid = fork();
84 if (pid < 0)
85 return string("Couldn't spawn new process") + strerror(errno);
86 else if (pid == 0)
87 ExecGPGV(outfile, file, 3, fd, (keyIsID ? "" : key));
88 close(fd[1]);
89
90 FILE *pipein = fdopen(fd[0], "r");
91
92 // Loop over the output of apt-key (which really is gnupg), and check the signatures.
93 std::vector<std::string> ValidSigners;
94 size_t buffersize = 0;
95 char *buffer = NULL;
96 while (1)
97 {
98 if (getline(&buffer, &buffersize, pipein) == -1)
99 break;
100 if (Debug == true)
101 std::clog << "Read: " << buffer << std::endl;
102
103 // Push the data into three separate vectors, which
104 // we later concatenate. They're kept separate so
105 // if we improve the apt method communication stuff later
106 // it will be better.
107 if (strncmp(buffer, GNUPGBADSIG, sizeof(GNUPGBADSIG)-1) == 0)
108 {
109 if (Debug == true)
110 std::clog << "Got BADSIG! " << std::endl;
111 BadSigners.push_back(string(buffer+sizeof(GNUPGPREFIX)));
112 }
113 else if (strncmp(buffer, GNUPGNOPUBKEY, sizeof(GNUPGNOPUBKEY)-1) == 0)
114 {
115 if (Debug == true)
116 std::clog << "Got NO_PUBKEY " << std::endl;
117 NoPubKeySigners.push_back(string(buffer+sizeof(GNUPGPREFIX)));
118 }
119 else if (strncmp(buffer, GNUPGNODATA, sizeof(GNUPGBADSIG)-1) == 0)
120 {
121 if (Debug == true)
122 std::clog << "Got NODATA! " << std::endl;
123 BadSigners.push_back(string(buffer+sizeof(GNUPGPREFIX)));
124 }
125 else if (strncmp(buffer, GNUPGKEYEXPIRED, sizeof(GNUPGKEYEXPIRED)-1) == 0)
126 {
127 if (Debug == true)
128 std::clog << "Got KEYEXPIRED! " << std::endl;
129 WorthlessSigners.push_back(string(buffer+sizeof(GNUPGPREFIX)));
130 }
131 else if (strncmp(buffer, GNUPGREVKEYSIG, sizeof(GNUPGREVKEYSIG)-1) == 0)
132 {
133 if (Debug == true)
134 std::clog << "Got REVKEYSIG! " << std::endl;
135 WorthlessSigners.push_back(string(buffer+sizeof(GNUPGPREFIX)));
136 }
137 else if (strncmp(buffer, GNUPGGOODSIG, sizeof(GNUPGGOODSIG)-1) == 0)
138 {
139 char *sig = buffer + sizeof(GNUPGPREFIX);
140 char *p = sig + sizeof("GOODSIG");
141 while (*p && isxdigit(*p))
142 p++;
143 *p = 0;
144 if (Debug == true)
145 std::clog << "Got GOODSIG, key ID:" << sig << std::endl;
146 GoodSigners.push_back(string(sig));
147 }
148 else if (strncmp(buffer, GNUPGVALIDSIG, sizeof(GNUPGVALIDSIG)-1) == 0)
149 {
150 char *sig = buffer + sizeof(GNUPGVALIDSIG);
151 std::istringstream iss((string(sig)));
152 vector<string> tokens{std::istream_iterator<string>{iss},
153 std::istream_iterator<string>{}};
154 char *p = sig;
155 while (*p && isxdigit(*p))
156 p++;
157 *p = 0;
158 if (Debug == true)
159 std::clog << "Got VALIDSIG, key ID: " << sig << std::endl;
160 // Reject weak digest algorithms
161 if (std::find(WeakDigests.begin(), WeakDigests.end(), tokens[7]) != WeakDigests.end())
162 {
163 // Treat them like an expired key: For that a message about expiry
164 // is emitted, a VALIDSIG, but no GOODSIG.
165 WorthlessSigners.push_back("WEAKDIGEST " + string(sig));
166 GoodSigners.erase(std::remove(GoodSigners.begin(), GoodSigners.end(), string(sig)));
167 }
168
169 ValidSigners.push_back(string(sig));
170 }
171 }
172 fclose(pipein);
173 free(buffer);
174
175 // apt-key has a --keyid parameter, but this requires gpg, so we call it without it
176 // and instead check after the fact which keyids where used for verification
177 if (keyIsID == true)
178 {
179 if (Debug == true)
180 std::clog << "GoodSigs needs to be limited to keyid " << key << std::endl;
181 std::vector<std::string>::iterator const foundItr = std::find(ValidSigners.begin(), ValidSigners.end(), key);
182 bool const found = (foundItr != ValidSigners.end());
183 std::copy(GoodSigners.begin(), GoodSigners.end(), std::back_insert_iterator<std::vector<std::string> >(NoPubKeySigners));
184 if (found)
185 {
186 // we look for GOODSIG here as well as an expired sig is a valid sig as well (but not a good one)
187 std::string const goodlongkeyid = "GOODSIG " + key.substr(24, 16);
188 bool const foundGood = std::find(GoodSigners.begin(), GoodSigners.end(), goodlongkeyid) != GoodSigners.end();
189 if (Debug == true)
190 std::clog << "Key " << key << " is valid sig, is " << goodlongkeyid << " also a good one? " << (foundGood ? "yes" : "no") << std::endl;
191 GoodSigners.clear();
192 if (foundGood)
193 {
194 GoodSigners.push_back(goodlongkeyid);
195 NoPubKeySigners.erase(std::remove(NoPubKeySigners.begin(), NoPubKeySigners.end(), goodlongkeyid), NoPubKeySigners.end());
196 }
197 }
198 else
199 GoodSigners.clear();
200 }
201
202 int status;
203 waitpid(pid, &status, 0);
204 if (Debug == true)
205 {
206 ioprintf(std::clog, "gpgv exited with status %i\n", WEXITSTATUS(status));
207 }
208
209 if (WEXITSTATUS(status) == 0)
210 {
211 if (keyIsID)
212 {
213 // gpgv will report success, but we want to enforce a certain keyring
214 // so if we haven't found the key the valid we found is in fact invalid
215 if (GoodSigners.empty())
216 return _("At least one invalid signature was encountered.");
217 }
218 else
219 {
220 if (GoodSigners.empty())
221 return _("Internal error: Good signature, but could not determine key fingerprint?!");
222 }
223 return "";
224 }
225 else if (WEXITSTATUS(status) == 1)
226 return _("At least one invalid signature was encountered.");
227 else if (WEXITSTATUS(status) == 111)
228 return _("Could not execute 'apt-key' to verify signature (is gnupg installed?)");
229 else if (WEXITSTATUS(status) == 112)
230 {
231 // acquire system checks for "NODATA" to generate GPG errors (the others are only warnings)
232 std::string errmsg;
233 //TRANSLATORS: %s is a single techy word like 'NODATA'
234 strprintf(errmsg, _("Clearsigned file isn't valid, got '%s' (does the network require authentication?)"), "NODATA");
235 return errmsg;
236 }
237 else
238 return _("Unknown error executing apt-key");
239 }
240
241 bool GPGVMethod::URIAcquire(std::string const &Message, FetchItem *Itm)
242 {
243 URI const Get = Itm->Uri;
244 string const Path = Get.Host + Get.Path; // To account for relative paths
245 std::string const key = LookupTag(Message, "Signed-By");
246 vector<string> GoodSigners;
247 vector<string> BadSigners;
248 // a worthless signature is a expired or revoked one
249 vector<string> WorthlessSigners;
250 vector<string> NoPubKeySigners;
251
252 FetchResult Res;
253 Res.Filename = Itm->DestFile;
254 URIStart(Res);
255
256 // Run apt-key on file, extract contents and get the key ID of the signer
257 string msg = VerifyGetSigners(Path.c_str(), Itm->DestFile.c_str(), key,
258 GoodSigners, BadSigners, WorthlessSigners,
259 NoPubKeySigners);
260 if (GoodSigners.empty() || !BadSigners.empty() || !NoPubKeySigners.empty())
261 {
262 string errmsg;
263 // In this case, something bad probably happened, so we just go
264 // with what the other method gave us for an error message.
265 if (BadSigners.empty() && WorthlessSigners.empty() && NoPubKeySigners.empty())
266 errmsg = msg;
267 else
268 {
269 if (!BadSigners.empty())
270 {
271 errmsg += _("The following signatures were invalid:\n");
272 for (vector<string>::iterator I = BadSigners.begin();
273 I != BadSigners.end(); ++I)
274 errmsg += (*I + "\n");
275 }
276 if (!WorthlessSigners.empty())
277 {
278 errmsg += _("The following signatures were invalid:\n");
279 for (vector<string>::iterator I = WorthlessSigners.begin();
280 I != WorthlessSigners.end(); ++I)
281 errmsg += (*I + "\n");
282 }
283 if (!NoPubKeySigners.empty())
284 {
285 errmsg += _("The following signatures couldn't be verified because the public key is not available:\n");
286 for (vector<string>::iterator I = NoPubKeySigners.begin();
287 I != NoPubKeySigners.end(); ++I)
288 errmsg += (*I + "\n");
289 }
290 }
291 // this is only fatal if we have no good sigs or if we have at
292 // least one bad signature. good signatures and NoPubKey signatures
293 // happen easily when a file is signed with multiple signatures
294 if(GoodSigners.empty() or !BadSigners.empty())
295 return _error->Error("%s", errmsg.c_str());
296 }
297
298 // Just pass the raw output up, because passing it as a real data
299 // structure is too difficult with the method stuff. We keep it
300 // as three separate vectors for future extensibility.
301 Res.GPGVOutput = GoodSigners;
302 Res.GPGVOutput.insert(Res.GPGVOutput.end(),BadSigners.begin(),BadSigners.end());
303 Res.GPGVOutput.insert(Res.GPGVOutput.end(),NoPubKeySigners.begin(),NoPubKeySigners.end());
304 URIDone(Res);
305
306 if (_config->FindB("Debug::Acquire::gpgv", false))
307 {
308 std::clog << "apt-key succeeded\n";
309 }
310
311 return true;
312 }
313
314
315 int main()
316 {
317 setlocale(LC_ALL, "");
318
319 GPGVMethod Mth;
320
321 return Mth.Run();
322 }