]>
git.saurik.com Git - apt.git/blob - methods/gpgv.cc
f355aaac345911ec257287d4131dff9ff131fa77
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 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"
51 static constexpr Digest Digests
[] = {
52 {Digest::State::Untrusted
, "Invalid digest"},
53 {Digest::State::Untrusted
, "MD5"},
54 {Digest::State::Weak
, "SHA1"},
55 {Digest::State::Weak
, "RIPE-MD/160"},
56 {Digest::State::Trusted
, "Reserved digest"},
57 {Digest::State::Trusted
, "Reserved digest"},
58 {Digest::State::Trusted
, "Reserved digest"},
59 {Digest::State::Trusted
, "Reserved digest"},
60 {Digest::State::Trusted
, "SHA256"},
61 {Digest::State::Trusted
, "SHA384"},
62 {Digest::State::Trusted
, "SHA512"},
63 {Digest::State::Trusted
, "SHA224"},
66 static Digest
FindDigest(std::string
const & Digest
)
68 int id
= atoi(Digest
.c_str());
69 if (id
>= 0 && static_cast<unsigned>(id
) < _count(Digests
)) {
81 class GPGVMethod
: public aptMethod
84 string
VerifyGetSigners(const char *file
, const char *outfile
,
85 std::string
const &key
,
86 vector
<string
> &GoodSigners
,
87 vector
<string
> &BadSigners
,
88 vector
<string
> &WorthlessSigners
,
89 vector
<Signer
> &SoonWorthlessSigners
,
90 vector
<string
> &NoPubKeySigners
);
92 virtual bool URIAcquire(std::string
const &Message
, FetchItem
*Itm
) APT_OVERRIDE
;
95 GPGVMethod() : aptMethod("gpgv","1.0",SingleInstance
| SendConfig
) {};
98 string
GPGVMethod::VerifyGetSigners(const char *file
, const char *outfile
,
99 std::string
const &key
,
100 vector
<string
> &GoodSigners
,
101 vector
<string
> &BadSigners
,
102 vector
<string
> &WorthlessSigners
,
103 vector
<Signer
> &SoonWorthlessSigners
,
104 vector
<string
> &NoPubKeySigners
)
106 bool const Debug
= _config
->FindB("Debug::Acquire::gpgv", false);
109 std::clog
<< "inside VerifyGetSigners" << std::endl
;
112 bool const keyIsID
= (key
.empty() == false && key
[0] != '/');
115 return "Couldn't create pipe";
119 return string("Couldn't spawn new process") + strerror(errno
);
121 ExecGPGV(outfile
, file
, 3, fd
, (keyIsID
? "" : key
));
124 FILE *pipein
= fdopen(fd
[0], "r");
126 // Loop over the output of apt-key (which really is gnupg), and check the signatures.
127 std::vector
<std::string
> ValidSigners
;
128 size_t buffersize
= 0;
132 if (getline(&buffer
, &buffersize
, pipein
) == -1)
135 std::clog
<< "Read: " << buffer
<< std::endl
;
137 // Push the data into three separate vectors, which
138 // we later concatenate. They're kept separate so
139 // if we improve the apt method communication stuff later
140 // it will be better.
141 if (strncmp(buffer
, GNUPGBADSIG
, sizeof(GNUPGBADSIG
)-1) == 0)
144 std::clog
<< "Got BADSIG! " << std::endl
;
145 BadSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
147 else if (strncmp(buffer
, GNUPGNOPUBKEY
, sizeof(GNUPGNOPUBKEY
)-1) == 0)
150 std::clog
<< "Got NO_PUBKEY " << std::endl
;
151 NoPubKeySigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
153 else if (strncmp(buffer
, GNUPGNODATA
, sizeof(GNUPGBADSIG
)-1) == 0)
156 std::clog
<< "Got NODATA! " << std::endl
;
157 BadSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
159 else if (strncmp(buffer
, GNUPGKEYEXPIRED
, sizeof(GNUPGKEYEXPIRED
)-1) == 0)
162 std::clog
<< "Got KEYEXPIRED! " << std::endl
;
163 WorthlessSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
165 else if (strncmp(buffer
, GNUPGREVKEYSIG
, sizeof(GNUPGREVKEYSIG
)-1) == 0)
168 std::clog
<< "Got REVKEYSIG! " << std::endl
;
169 WorthlessSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
171 else if (strncmp(buffer
, GNUPGGOODSIG
, sizeof(GNUPGGOODSIG
)-1) == 0)
173 char *sig
= buffer
+ sizeof(GNUPGPREFIX
);
174 char *p
= sig
+ sizeof("GOODSIG");
175 while (*p
&& isxdigit(*p
))
179 std::clog
<< "Got GOODSIG, key ID:" << sig
<< std::endl
;
180 GoodSigners
.push_back(string(sig
));
182 else if (strncmp(buffer
, GNUPGVALIDSIG
, sizeof(GNUPGVALIDSIG
)-1) == 0)
184 char *sig
= buffer
+ sizeof(GNUPGVALIDSIG
);
185 std::istringstream
iss((string(sig
)));
186 vector
<string
> tokens
{std::istream_iterator
<string
>{iss
},
187 std::istream_iterator
<string
>{}};
189 while (*p
&& isxdigit(*p
))
193 std::clog
<< "Got VALIDSIG, key ID: " << sig
<< std::endl
;
194 // Reject weak digest algorithms
195 Digest digest
= FindDigest(tokens
[7]);
196 switch (digest
.state
) {
197 case Digest::State::Weak
:
198 // Treat them like an expired key: For that a message about expiry
199 // is emitted, a VALIDSIG, but no GOODSIG.
200 SoonWorthlessSigners
.push_back({string(sig
), digest
.name
});
202 case Digest::State::Untrusted
:
203 // Treat them like an expired key: For that a message about expiry
204 // is emitted, a VALIDSIG, but no GOODSIG.
205 WorthlessSigners
.push_back(string(sig
));
206 GoodSigners
.erase(std::remove(GoodSigners
.begin(), GoodSigners
.end(), string(sig
)));
208 case Digest::State::Trusted
:
212 ValidSigners
.push_back(string(sig
));
218 // apt-key has a --keyid parameter, but this requires gpg, so we call it without it
219 // and instead check after the fact which keyids where used for verification
223 std::clog
<< "GoodSigs needs to be limited to keyid " << key
<< std::endl
;
224 std::vector
<std::string
>::iterator
const foundItr
= std::find(ValidSigners
.begin(), ValidSigners
.end(), key
);
225 bool const found
= (foundItr
!= ValidSigners
.end());
226 std::copy(GoodSigners
.begin(), GoodSigners
.end(), std::back_insert_iterator
<std::vector
<std::string
> >(NoPubKeySigners
));
229 // we look for GOODSIG here as well as an expired sig is a valid sig as well (but not a good one)
230 std::string
const goodlongkeyid
= "GOODSIG " + key
.substr(24, 16);
231 bool const foundGood
= std::find(GoodSigners
.begin(), GoodSigners
.end(), goodlongkeyid
) != GoodSigners
.end();
233 std::clog
<< "Key " << key
<< " is valid sig, is " << goodlongkeyid
<< " also a good one? " << (foundGood
? "yes" : "no") << std::endl
;
237 GoodSigners
.push_back(goodlongkeyid
);
238 NoPubKeySigners
.erase(std::remove(NoPubKeySigners
.begin(), NoPubKeySigners
.end(), goodlongkeyid
), NoPubKeySigners
.end());
246 waitpid(pid
, &status
, 0);
249 ioprintf(std::clog
, "gpgv exited with status %i\n", WEXITSTATUS(status
));
252 if (WEXITSTATUS(status
) == 0)
256 // gpgv will report success, but we want to enforce a certain keyring
257 // so if we haven't found the key the valid we found is in fact invalid
258 if (GoodSigners
.empty())
259 return _("At least one invalid signature was encountered.");
263 if (GoodSigners
.empty())
264 return _("Internal error: Good signature, but could not determine key fingerprint?!");
268 else if (WEXITSTATUS(status
) == 1)
269 return _("At least one invalid signature was encountered.");
270 else if (WEXITSTATUS(status
) == 111)
271 return _("Could not execute 'apt-key' to verify signature (is gnupg installed?)");
272 else if (WEXITSTATUS(status
) == 112)
274 // acquire system checks for "NODATA" to generate GPG errors (the others are only warnings)
276 //TRANSLATORS: %s is a single techy word like 'NODATA'
277 strprintf(errmsg
, _("Clearsigned file isn't valid, got '%s' (does the network require authentication?)"), "NODATA");
281 return _("Unknown error executing apt-key");
284 bool GPGVMethod::URIAcquire(std::string
const &Message
, FetchItem
*Itm
)
286 URI
const Get
= Itm
->Uri
;
287 string
const Path
= Get
.Host
+ Get
.Path
; // To account for relative paths
288 std::string
const key
= LookupTag(Message
, "Signed-By");
289 vector
<string
> GoodSigners
;
290 vector
<string
> BadSigners
;
291 // a worthless signature is a expired or revoked one
292 vector
<string
> WorthlessSigners
;
293 vector
<Signer
> SoonWorthlessSigners
;
294 vector
<string
> NoPubKeySigners
;
297 Res
.Filename
= Itm
->DestFile
;
300 // Run apt-key on file, extract contents and get the key ID of the signer
301 string msg
= VerifyGetSigners(Path
.c_str(), Itm
->DestFile
.c_str(), key
,
302 GoodSigners
, BadSigners
, WorthlessSigners
,
303 SoonWorthlessSigners
, NoPubKeySigners
);
306 // Check if there are any good signers that are not soon worthless
307 std::vector
<std::string
> NotWarnAboutSigners(GoodSigners
);
308 for (auto const & Signer
: SoonWorthlessSigners
)
309 NotWarnAboutSigners
.erase(std::remove(NotWarnAboutSigners
.begin(), NotWarnAboutSigners
.end(), "GOODSIG " + Signer
.key
));
310 // If all signers are soon worthless, report them.
311 if (NotWarnAboutSigners
.empty()) {
312 for (auto const & Signer
: SoonWorthlessSigners
)
313 // TRANSLATORS: The second %s is the reason and is untranslated for repository owners.
314 Warning(_("Weak signature from %s (%s)"), Signer
.key
.c_str(), Signer
.note
.c_str());
317 if (GoodSigners
.empty() || !BadSigners
.empty() || !NoPubKeySigners
.empty())
320 // In this case, something bad probably happened, so we just go
321 // with what the other method gave us for an error message.
322 if (BadSigners
.empty() && WorthlessSigners
.empty() && NoPubKeySigners
.empty())
326 if (!BadSigners
.empty())
328 errmsg
+= _("The following signatures were invalid:\n");
329 for (vector
<string
>::iterator I
= BadSigners
.begin();
330 I
!= BadSigners
.end(); ++I
)
331 errmsg
+= (*I
+ "\n");
333 if (!WorthlessSigners
.empty())
335 errmsg
+= _("The following signatures were invalid:\n");
336 for (vector
<string
>::iterator I
= WorthlessSigners
.begin();
337 I
!= WorthlessSigners
.end(); ++I
)
338 errmsg
+= (*I
+ "\n");
340 if (!NoPubKeySigners
.empty())
342 errmsg
+= _("The following signatures couldn't be verified because the public key is not available:\n");
343 for (vector
<string
>::iterator I
= NoPubKeySigners
.begin();
344 I
!= NoPubKeySigners
.end(); ++I
)
345 errmsg
+= (*I
+ "\n");
348 // this is only fatal if we have no good sigs or if we have at
349 // least one bad signature. good signatures and NoPubKey signatures
350 // happen easily when a file is signed with multiple signatures
351 if(GoodSigners
.empty() or !BadSigners
.empty())
352 return _error
->Error("%s", errmsg
.c_str());
355 // Just pass the raw output up, because passing it as a real data
356 // structure is too difficult with the method stuff. We keep it
357 // as three separate vectors for future extensibility.
358 Res
.GPGVOutput
= GoodSigners
;
359 Res
.GPGVOutput
.insert(Res
.GPGVOutput
.end(),BadSigners
.begin(),BadSigners
.end());
360 Res
.GPGVOutput
.insert(Res
.GPGVOutput
.end(),NoPubKeySigners
.begin(),NoPubKeySigners
.end());
363 if (_config
->FindB("Debug::Acquire::gpgv", false))
365 std::clog
<< "apt-key succeeded\n";
374 setlocale(LC_ALL
, "");