]>
git.saurik.com Git - apt.git/blob - methods/gpgv.cc
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"
42 static const std::vector
<string
> WeakDigests
{
48 static const std::vector
<string
> DeprecatedDigests
{
53 class GPGVMethod
: public aptMethod
56 string
VerifyGetSigners(const char *file
, const char *outfile
,
57 std::string
const &key
,
58 vector
<string
> &GoodSigners
,
59 vector
<string
> &BadSigners
,
60 vector
<string
> &WorthlessSigners
,
61 vector
<string
> &SoonWorthlessSigners
,
62 vector
<string
> &NoPubKeySigners
);
64 virtual bool URIAcquire(std::string
const &Message
, FetchItem
*Itm
) APT_OVERRIDE
;
67 GPGVMethod() : aptMethod("gpgv","1.0",SingleInstance
| SendConfig
) {};
70 string
GPGVMethod::VerifyGetSigners(const char *file
, const char *outfile
,
71 std::string
const &key
,
72 vector
<string
> &GoodSigners
,
73 vector
<string
> &BadSigners
,
74 vector
<string
> &WorthlessSigners
,
75 vector
<string
> &SoonWorthlessSigners
,
76 vector
<string
> &NoPubKeySigners
)
78 bool const Debug
= _config
->FindB("Debug::Acquire::gpgv", false);
81 std::clog
<< "inside VerifyGetSigners" << std::endl
;
84 bool const keyIsID
= (key
.empty() == false && key
[0] != '/');
87 return "Couldn't create pipe";
91 return string("Couldn't spawn new process") + strerror(errno
);
93 ExecGPGV(outfile
, file
, 3, fd
, (keyIsID
? "" : key
));
96 FILE *pipein
= fdopen(fd
[0], "r");
98 // Loop over the output of apt-key (which really is gnupg), and check the signatures.
99 std::vector
<std::string
> ValidSigners
;
100 size_t buffersize
= 0;
104 if (getline(&buffer
, &buffersize
, pipein
) == -1)
107 std::clog
<< "Read: " << buffer
<< std::endl
;
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)
116 std::clog
<< "Got BADSIG! " << std::endl
;
117 BadSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
119 else if (strncmp(buffer
, GNUPGNOPUBKEY
, sizeof(GNUPGNOPUBKEY
)-1) == 0)
122 std::clog
<< "Got NO_PUBKEY " << std::endl
;
123 NoPubKeySigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
125 else if (strncmp(buffer
, GNUPGNODATA
, sizeof(GNUPGBADSIG
)-1) == 0)
128 std::clog
<< "Got NODATA! " << std::endl
;
129 BadSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
131 else if (strncmp(buffer
, GNUPGKEYEXPIRED
, sizeof(GNUPGKEYEXPIRED
)-1) == 0)
134 std::clog
<< "Got KEYEXPIRED! " << std::endl
;
135 WorthlessSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
137 else if (strncmp(buffer
, GNUPGREVKEYSIG
, sizeof(GNUPGREVKEYSIG
)-1) == 0)
140 std::clog
<< "Got REVKEYSIG! " << std::endl
;
141 WorthlessSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
143 else if (strncmp(buffer
, GNUPGGOODSIG
, sizeof(GNUPGGOODSIG
)-1) == 0)
145 char *sig
= buffer
+ sizeof(GNUPGPREFIX
);
146 char *p
= sig
+ sizeof("GOODSIG");
147 while (*p
&& isxdigit(*p
))
151 std::clog
<< "Got GOODSIG, key ID:" << sig
<< std::endl
;
152 GoodSigners
.push_back(string(sig
));
154 else if (strncmp(buffer
, GNUPGVALIDSIG
, sizeof(GNUPGVALIDSIG
)-1) == 0)
156 char *sig
= buffer
+ sizeof(GNUPGVALIDSIG
);
157 std::istringstream
iss((string(sig
)));
158 vector
<string
> tokens
{std::istream_iterator
<string
>{iss
},
159 std::istream_iterator
<string
>{}};
161 while (*p
&& isxdigit(*p
))
165 std::clog
<< "Got VALIDSIG, key ID: " << sig
<< std::endl
;
166 // Reject weak digest algorithms
167 if (std::find(DeprecatedDigests
.begin(), DeprecatedDigests
.end(), tokens
[7]) != DeprecatedDigests
.end())
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
));
173 if (std::find(WeakDigests
.begin(), WeakDigests
.end(), tokens
[7]) != WeakDigests
.end())
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
)));
181 ValidSigners
.push_back(string(sig
));
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
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
));
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();
202 std::clog
<< "Key " << key
<< " is valid sig, is " << goodlongkeyid
<< " also a good one? " << (foundGood
? "yes" : "no") << std::endl
;
206 GoodSigners
.push_back(goodlongkeyid
);
207 NoPubKeySigners
.erase(std::remove(NoPubKeySigners
.begin(), NoPubKeySigners
.end(), goodlongkeyid
), NoPubKeySigners
.end());
215 waitpid(pid
, &status
, 0);
218 ioprintf(std::clog
, "gpgv exited with status %i\n", WEXITSTATUS(status
));
221 if (WEXITSTATUS(status
) == 0)
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.");
232 if (GoodSigners
.empty())
233 return _("Internal error: Good signature, but could not determine key fingerprint?!");
237 else if (WEXITSTATUS(status
) == 1)
238 return _("At least one invalid signature was encountered.");
239 else if (WEXITSTATUS(status
) == 111)
240 return _("Could not execute 'apt-key' to verify signature (is gnupg installed?)");
241 else if (WEXITSTATUS(status
) == 112)
243 // acquire system checks for "NODATA" to generate GPG errors (the others are only warnings)
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");
250 return _("Unknown error executing apt-key");
253 bool GPGVMethod::URIAcquire(std::string
const &Message
, FetchItem
*Itm
)
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");
258 vector
<string
> GoodSigners
;
259 vector
<string
> BadSigners
;
260 // a worthless signature is a expired or revoked one
261 vector
<string
> WorthlessSigners
;
262 vector
<string
> SoonWorthlessSigners
;
263 vector
<string
> NoPubKeySigners
;
266 Res
.Filename
= Itm
->DestFile
;
269 // Run apt-key on file, extract contents and get the key ID of the signer
270 string msg
= VerifyGetSigners(Path
.c_str(), Itm
->DestFile
.c_str(), key
,
271 GoodSigners
, BadSigners
, WorthlessSigners
,
272 SoonWorthlessSigners
, NoPubKeySigners
);
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");
286 if (GoodSigners
.empty() || !BadSigners
.empty() || !NoPubKeySigners
.empty())
289 // In this case, something bad probably happened, so we just go
290 // with what the other method gave us for an error message.
291 if (BadSigners
.empty() && WorthlessSigners
.empty() && NoPubKeySigners
.empty())
295 if (!BadSigners
.empty())
297 errmsg
+= _("The following signatures were invalid:\n");
298 for (vector
<string
>::iterator I
= BadSigners
.begin();
299 I
!= BadSigners
.end(); ++I
)
300 errmsg
+= (*I
+ "\n");
302 if (!WorthlessSigners
.empty())
304 errmsg
+= _("The following signatures were invalid:\n");
305 for (vector
<string
>::iterator I
= WorthlessSigners
.begin();
306 I
!= WorthlessSigners
.end(); ++I
)
307 errmsg
+= (*I
+ "\n");
309 if (!NoPubKeySigners
.empty())
311 errmsg
+= _("The following signatures couldn't be verified because the public key is not available:\n");
312 for (vector
<string
>::iterator I
= NoPubKeySigners
.begin();
313 I
!= NoPubKeySigners
.end(); ++I
)
314 errmsg
+= (*I
+ "\n");
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())
321 return _error
->Error("%s", errmsg
.c_str());
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());
332 if (_config
->FindB("Debug::Acquire::gpgv", false))
334 std::clog
<< "apt-key succeeded\n";
343 setlocale(LC_ALL
, "");