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