]>
git.saurik.com Git - apt.git/blob - methods/gpgv.cc
53c3ff80e2c59287be8c082e9794bac51e6cfd36
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>
33 #define GNUPGPREFIX "[GNUPG:]"
34 #define GNUPGBADSIG "[GNUPG:] BADSIG"
35 #define GNUPGERRSIG "[GNUPG:] ERRSIG"
36 #define GNUPGNOPUBKEY "[GNUPG:] NO_PUBKEY"
37 #define GNUPGVALIDSIG "[GNUPG:] VALIDSIG"
38 #define GNUPGGOODSIG "[GNUPG:] GOODSIG"
39 #define GNUPGEXPKEYSIG "[GNUPG:] EXPKEYSIG"
40 #define GNUPGEXPSIG "[GNUPG:] EXPSIG"
41 #define GNUPGREVKEYSIG "[GNUPG:] REVKEYSIG"
42 #define GNUPGNODATA "[GNUPG:] NODATA"
52 State
getState() const {
53 std::string optionUntrusted
;
54 std::string optionWeak
;
55 strprintf(optionUntrusted
, "APT::Hashes::%s::Untrusted", name
);
56 strprintf(optionWeak
, "APT::Hashes::%s::Weak", name
);
57 if (_config
->FindB(optionUntrusted
, state
== State::Untrusted
) == true)
58 return State::Untrusted
;
59 if (_config
->FindB(optionWeak
, state
== State::Weak
) == true)
66 static constexpr Digest Digests
[] = {
67 {Digest::State::Untrusted
, "Invalid digest"},
68 {Digest::State::Untrusted
, "MD5"},
69 {Digest::State::Weak
, "SHA1"},
70 {Digest::State::Weak
, "RIPE-MD/160"},
71 {Digest::State::Trusted
, "Reserved digest"},
72 {Digest::State::Trusted
, "Reserved digest"},
73 {Digest::State::Trusted
, "Reserved digest"},
74 {Digest::State::Trusted
, "Reserved digest"},
75 {Digest::State::Trusted
, "SHA256"},
76 {Digest::State::Trusted
, "SHA384"},
77 {Digest::State::Trusted
, "SHA512"},
78 {Digest::State::Trusted
, "SHA224"},
81 static Digest
FindDigest(std::string
const & Digest
)
83 int id
= atoi(Digest
.c_str());
84 if (id
>= 0 && static_cast<unsigned>(id
) < _count(Digests
)) {
95 static bool IsTheSameKey(std::string
const &validsig
, std::string
const &goodsig
) {
96 // VALIDSIG reports a keyid (40 = 24 + 16), GOODSIG is a longid (16) only
97 return validsig
.compare(24, 16, goodsig
, strlen("GOODSIG "), 16) == 0;
100 class GPGVMethod
: public aptMethod
103 string
VerifyGetSigners(const char *file
, const char *outfile
,
104 std::string
const &key
,
105 vector
<string
> &GoodSigners
,
106 vector
<string
> &BadSigners
,
107 vector
<string
> &WorthlessSigners
,
108 vector
<Signer
> &SoonWorthlessSigners
,
109 vector
<string
> &NoPubKeySigners
);
111 virtual bool URIAcquire(std::string
const &Message
, FetchItem
*Itm
) APT_OVERRIDE
;
113 GPGVMethod() : aptMethod("gpgv","1.0",SingleInstance
| SendConfig
) {};
116 string
GPGVMethod::VerifyGetSigners(const char *file
, const char *outfile
,
117 std::string
const &key
,
118 vector
<string
> &GoodSigners
,
119 vector
<string
> &BadSigners
,
120 vector
<string
> &WorthlessSigners
,
121 vector
<Signer
> &SoonWorthlessSigners
,
122 vector
<string
> &NoPubKeySigners
)
124 bool const Debug
= _config
->FindB("Debug::Acquire::gpgv", false);
127 std::clog
<< "inside VerifyGetSigners" << std::endl
;
130 bool const keyIsID
= (key
.empty() == false && key
[0] != '/');
133 return "Couldn't create pipe";
137 return string("Couldn't spawn new process") + strerror(errno
);
139 ExecGPGV(outfile
, file
, 3, fd
, (keyIsID
? "" : key
));
142 FILE *pipein
= fdopen(fd
[0], "r");
144 // Loop over the output of apt-key (which really is gnupg), and check the signatures.
145 std::vector
<std::string
> ValidSigners
;
146 std::vector
<std::string
> ErrSigners
;
147 size_t buffersize
= 0;
151 if (getline(&buffer
, &buffersize
, pipein
) == -1)
154 std::clog
<< "Read: " << buffer
<< std::endl
;
156 // Push the data into three separate vectors, which
157 // we later concatenate. They're kept separate so
158 // if we improve the apt method communication stuff later
159 // it will be better.
160 if (strncmp(buffer
, GNUPGBADSIG
, sizeof(GNUPGBADSIG
)-1) == 0)
163 std::clog
<< "Got BADSIG! " << std::endl
;
164 BadSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
166 else if (strncmp(buffer
, GNUPGERRSIG
, sizeof(GNUPGERRSIG
)-1) == 0)
169 std::clog
<< "Got ERRSIG " << std::endl
;
170 ErrSigners
.push_back(string(buffer
, strlen(GNUPGPREFIX
), strlen("ERRSIG ") + 16));
172 else if (strncmp(buffer
, GNUPGNOPUBKEY
, sizeof(GNUPGNOPUBKEY
)-1) == 0)
175 std::clog
<< "Got NO_PUBKEY " << std::endl
;
176 NoPubKeySigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
177 ErrSigners
.erase(std::remove_if(ErrSigners
.begin(), ErrSigners
.end(), [&](std::string
const &errsig
) {
178 return errsig
.compare(strlen("ERRSIG "), 16, buffer
, strlen(GNUPGNOPUBKEY
), 16) == 0; }), ErrSigners
.end());
180 else if (strncmp(buffer
, GNUPGNODATA
, sizeof(GNUPGBADSIG
)-1) == 0)
183 std::clog
<< "Got NODATA! " << std::endl
;
184 BadSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
186 else if (strncmp(buffer
, GNUPGEXPKEYSIG
, sizeof(GNUPGEXPKEYSIG
)-1) == 0)
189 std::clog
<< "Got EXPKEYSIG! " << std::endl
;
190 WorthlessSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
192 else if (strncmp(buffer
, GNUPGEXPSIG
, sizeof(GNUPGEXPSIG
)-1) == 0)
195 std::clog
<< "Got EXPSIG!" << std::endl
;
196 WorthlessSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
198 else if (strncmp(buffer
, GNUPGREVKEYSIG
, sizeof(GNUPGREVKEYSIG
)-1) == 0)
201 std::clog
<< "Got REVKEYSIG! " << std::endl
;
202 WorthlessSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
204 else if (strncmp(buffer
, GNUPGGOODSIG
, sizeof(GNUPGGOODSIG
)-1) == 0)
206 char *sig
= buffer
+ sizeof(GNUPGPREFIX
);
207 char *p
= sig
+ sizeof("GOODSIG");
208 while (*p
&& isxdigit(*p
))
212 std::clog
<< "Got GOODSIG, key ID:" << sig
<< std::endl
;
213 GoodSigners
.push_back(string(sig
));
215 else if (strncmp(buffer
, GNUPGVALIDSIG
, sizeof(GNUPGVALIDSIG
)-1) == 0)
217 char *sig
= buffer
+ sizeof(GNUPGVALIDSIG
);
218 std::istringstream
iss((string(sig
)));
219 vector
<string
> tokens
{std::istream_iterator
<string
>{iss
},
220 std::istream_iterator
<string
>{}};
222 while (*p
&& isxdigit(*p
))
225 // Reject weak digest algorithms
226 Digest digest
= FindDigest(tokens
[7]);
227 switch (digest
.getState()) {
228 case Digest::State::Weak
:
229 // Treat them like an expired key: For that a message about expiry
230 // is emitted, a VALIDSIG, but no GOODSIG.
231 SoonWorthlessSigners
.push_back({string(sig
), digest
.name
});
233 std::clog
<< "Got weak VALIDSIG, key ID: " << sig
<< std::endl
;
235 case Digest::State::Untrusted
:
236 // Treat them like an expired key: For that a message about expiry
237 // is emitted, a VALIDSIG, but no GOODSIG.
238 WorthlessSigners
.push_back(string(sig
));
239 GoodSigners
.erase(std::remove_if(GoodSigners
.begin(), GoodSigners
.end(), [&](std::string
const &goodsig
) {
240 return IsTheSameKey(string(sig
), goodsig
); }), GoodSigners
.end());
242 std::clog
<< "Got untrusted VALIDSIG, key ID: " << sig
<< std::endl
;
245 case Digest::State::Trusted
:
247 std::clog
<< "Got trusted VALIDSIG, key ID: " << sig
<< std::endl
;
251 ValidSigners
.push_back(string(sig
));
256 std::move(ErrSigners
.begin(), ErrSigners
.end(), std::back_inserter(WorthlessSigners
));
258 // apt-key has a --keyid parameter, but this requires gpg, so we call it without it
259 // and instead check after the fact which keyids where used for verification
263 std::clog
<< "GoodSigs needs to be limited to keyid " << key
<< std::endl
;
264 std::vector
<std::string
>::iterator
const foundItr
= std::find(ValidSigners
.begin(), ValidSigners
.end(), key
);
265 bool const found
= (foundItr
!= ValidSigners
.end());
266 std::copy(GoodSigners
.begin(), GoodSigners
.end(), std::back_insert_iterator
<std::vector
<std::string
> >(NoPubKeySigners
));
269 // we look for GOODSIG here as well as an expired sig is a valid sig as well (but not a good one)
270 std::string
const goodlongkeyid
= "GOODSIG " + key
.substr(24, 16);
271 bool const foundGood
= std::find(GoodSigners
.begin(), GoodSigners
.end(), goodlongkeyid
) != GoodSigners
.end();
273 std::clog
<< "Key " << key
<< " is valid sig, is " << goodlongkeyid
<< " also a good one? " << (foundGood
? "yes" : "no") << std::endl
;
277 GoodSigners
.push_back(goodlongkeyid
);
278 NoPubKeySigners
.erase(std::remove(NoPubKeySigners
.begin(), NoPubKeySigners
.end(), goodlongkeyid
), NoPubKeySigners
.end());
286 waitpid(pid
, &status
, 0);
289 ioprintf(std::clog
, "gpgv exited with status %i\n", WEXITSTATUS(status
));
294 std::cerr
<< "Summary:" << std::endl
<< " Good: ";
295 std::copy(GoodSigners
.begin(), GoodSigners
.end(), std::ostream_iterator
<std::string
>(std::cerr
, ", "));
296 std::cerr
<< std::endl
<< " Bad: ";
297 std::copy(BadSigners
.begin(), BadSigners
.end(), std::ostream_iterator
<std::string
>(std::cerr
, ", "));
298 std::cerr
<< std::endl
<< " Worthless: ";
299 std::copy(WorthlessSigners
.begin(), WorthlessSigners
.end(), std::ostream_iterator
<std::string
>(std::cerr
, ", "));
300 std::cerr
<< std::endl
<< " SoonWorthless: ";
301 std::for_each(SoonWorthlessSigners
.begin(), SoonWorthlessSigners
.end(), [](Signer
const &sig
) { std::cerr
<< sig
.key
<< ", "; });
302 std::cerr
<< std::endl
<< " NoPubKey: ";
303 std::copy(NoPubKeySigners
.begin(), NoPubKeySigners
.end(), std::ostream_iterator
<std::string
>(std::cerr
, ", "));
304 std::cerr
<< std::endl
;
307 if (WEXITSTATUS(status
) == 0)
311 // gpgv will report success, but we want to enforce a certain keyring
312 // so if we haven't found the key the valid we found is in fact invalid
313 if (GoodSigners
.empty())
314 return _("At least one invalid signature was encountered.");
318 if (GoodSigners
.empty())
319 return _("Internal error: Good signature, but could not determine key fingerprint?!");
323 else if (WEXITSTATUS(status
) == 1)
324 return _("At least one invalid signature was encountered.");
325 else if (WEXITSTATUS(status
) == 111)
326 return _("Could not execute 'apt-key' to verify signature (is gnupg installed?)");
327 else if (WEXITSTATUS(status
) == 112)
329 // acquire system checks for "NODATA" to generate GPG errors (the others are only warnings)
331 //TRANSLATORS: %s is a single techy word like 'NODATA'
332 strprintf(errmsg
, _("Clearsigned file isn't valid, got '%s' (does the network require authentication?)"), "NODATA");
336 return _("Unknown error executing apt-key");
339 bool GPGVMethod::URIAcquire(std::string
const &Message
, FetchItem
*Itm
)
341 URI
const Get
= Itm
->Uri
;
342 string
const Path
= Get
.Host
+ Get
.Path
; // To account for relative paths
343 std::string
const key
= LookupTag(Message
, "Signed-By");
344 vector
<string
> GoodSigners
;
345 vector
<string
> BadSigners
;
346 // a worthless signature is a expired or revoked one
347 vector
<string
> WorthlessSigners
;
348 vector
<Signer
> SoonWorthlessSigners
;
349 vector
<string
> NoPubKeySigners
;
352 Res
.Filename
= Itm
->DestFile
;
355 // Run apt-key on file, extract contents and get the key ID of the signer
356 string msg
= VerifyGetSigners(Path
.c_str(), Itm
->DestFile
.c_str(), key
,
357 GoodSigners
, BadSigners
, WorthlessSigners
,
358 SoonWorthlessSigners
, NoPubKeySigners
);
360 // Check if all good signers are soon worthless and warn in that case
361 if (std::all_of(GoodSigners
.begin(), GoodSigners
.end(), [&](std::string
const &good
) {
362 return std::any_of(SoonWorthlessSigners
.begin(), SoonWorthlessSigners
.end(), [&](Signer
const &weak
) {
363 return IsTheSameKey(weak
.key
, good
);
367 for (auto const & Signer
: SoonWorthlessSigners
)
368 // TRANSLATORS: The second %s is the reason and is untranslated for repository owners.
369 Warning(_("Signature by key %s uses weak digest algorithm (%s)"), Signer
.key
.c_str(), Signer
.note
.c_str());
372 if (GoodSigners
.empty() || !BadSigners
.empty() || !NoPubKeySigners
.empty())
375 // In this case, something bad probably happened, so we just go
376 // with what the other method gave us for an error message.
377 if (BadSigners
.empty() && WorthlessSigners
.empty() && NoPubKeySigners
.empty())
381 if (!BadSigners
.empty())
383 errmsg
+= _("The following signatures were invalid:\n");
384 for (vector
<string
>::iterator I
= BadSigners
.begin();
385 I
!= BadSigners
.end(); ++I
)
386 errmsg
+= (*I
+ "\n");
388 if (!WorthlessSigners
.empty())
390 errmsg
+= _("The following signatures were invalid:\n");
391 for (vector
<string
>::iterator I
= WorthlessSigners
.begin();
392 I
!= WorthlessSigners
.end(); ++I
)
393 errmsg
+= (*I
+ "\n");
395 if (!NoPubKeySigners
.empty())
397 errmsg
+= _("The following signatures couldn't be verified because the public key is not available:\n");
398 for (vector
<string
>::iterator I
= NoPubKeySigners
.begin();
399 I
!= NoPubKeySigners
.end(); ++I
)
400 errmsg
+= (*I
+ "\n");
403 // this is only fatal if we have no good sigs or if we have at
404 // least one bad signature. good signatures and NoPubKey signatures
405 // happen easily when a file is signed with multiple signatures
406 if(GoodSigners
.empty() or !BadSigners
.empty())
407 return _error
->Error("%s", errmsg
.c_str());
410 // Just pass the raw output up, because passing it as a real data
411 // structure is too difficult with the method stuff. We keep it
412 // as three separate vectors for future extensibility.
413 Res
.GPGVOutput
= GoodSigners
;
414 Res
.GPGVOutput
.insert(Res
.GPGVOutput
.end(),BadSigners
.begin(),BadSigners
.end());
415 Res
.GPGVOutput
.insert(Res
.GPGVOutput
.end(),NoPubKeySigners
.begin(),NoPubKeySigners
.end());
418 if (_config
->FindB("Debug::Acquire::gpgv", false))
420 std::clog
<< "apt-key succeeded\n";
429 setlocale(LC_ALL
, "");