]>
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>
29 #define GNUPGPREFIX "[GNUPG:]"
30 #define GNUPGBADSIG "[GNUPG:] BADSIG"
31 #define GNUPGNOPUBKEY "[GNUPG:] NO_PUBKEY"
32 #define GNUPGVALIDSIG "[GNUPG:] VALIDSIG"
33 #define GNUPGGOODSIG "[GNUPG:] GOODSIG"
34 #define GNUPGKEYEXPIRED "[GNUPG:] KEYEXPIRED"
35 #define GNUPGREVKEYSIG "[GNUPG:] REVKEYSIG"
36 #define GNUPGNODATA "[GNUPG:] NODATA"
38 class GPGVMethod
: public pkgAcqMethod
41 string
VerifyGetSigners(const char *file
, const char *outfile
,
42 std::string
const &key
,
43 vector
<string
> &GoodSigners
,
44 vector
<string
> &BadSigners
,
45 vector
<string
> &WorthlessSigners
,
46 vector
<string
> &NoPubKeySigners
);
49 virtual bool URIAcquire(std::string
const &Message
, FetchItem
*Itm
) APT_OVERRIDE
;
50 virtual bool Configuration(string Message
) APT_OVERRIDE
;
53 GPGVMethod() : pkgAcqMethod("1.0",SingleInstance
| SendConfig
) {};
56 bool GPGVMethod::Configuration(string Message
)
58 if (pkgAcqMethod::Configuration(Message
) == false)
66 string
GPGVMethod::VerifyGetSigners(const char *file
, const char *outfile
,
67 std::string
const &key
,
68 vector
<string
> &GoodSigners
,
69 vector
<string
> &BadSigners
,
70 vector
<string
> &WorthlessSigners
,
71 vector
<string
> &NoPubKeySigners
)
73 bool const Debug
= _config
->FindB("Debug::Acquire::gpgv", false);
76 std::clog
<< "inside VerifyGetSigners" << std::endl
;
79 bool const keyIsID
= (key
.empty() == false && key
[0] != '/');
82 return "Couldn't create pipe";
86 return string("Couldn't spawn new process") + strerror(errno
);
88 ExecGPGV(outfile
, file
, 3, fd
, (keyIsID
? "" : key
));
91 FILE *pipein
= fdopen(fd
[0], "r");
93 // Loop over the output of apt-key (which really is gnupg), and check the signatures.
94 std::vector
<std::string
> ValidSigners
;
95 size_t buffersize
= 0;
99 if (getline(&buffer
, &buffersize
, pipein
) == -1)
102 std::clog
<< "Read: " << buffer
<< std::endl
;
104 // Push the data into three separate vectors, which
105 // we later concatenate. They're kept separate so
106 // if we improve the apt method communication stuff later
107 // it will be better.
108 if (strncmp(buffer
, GNUPGBADSIG
, sizeof(GNUPGBADSIG
)-1) == 0)
111 std::clog
<< "Got BADSIG! " << std::endl
;
112 BadSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
114 else if (strncmp(buffer
, GNUPGNOPUBKEY
, sizeof(GNUPGNOPUBKEY
)-1) == 0)
117 std::clog
<< "Got NO_PUBKEY " << std::endl
;
118 NoPubKeySigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
120 else if (strncmp(buffer
, GNUPGNODATA
, sizeof(GNUPGBADSIG
)-1) == 0)
123 std::clog
<< "Got NODATA! " << std::endl
;
124 BadSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
126 else if (strncmp(buffer
, GNUPGKEYEXPIRED
, sizeof(GNUPGKEYEXPIRED
)-1) == 0)
129 std::clog
<< "Got KEYEXPIRED! " << std::endl
;
130 WorthlessSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
132 else if (strncmp(buffer
, GNUPGREVKEYSIG
, sizeof(GNUPGREVKEYSIG
)-1) == 0)
135 std::clog
<< "Got REVKEYSIG! " << std::endl
;
136 WorthlessSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
138 else if (strncmp(buffer
, GNUPGGOODSIG
, sizeof(GNUPGGOODSIG
)-1) == 0)
140 char *sig
= buffer
+ sizeof(GNUPGPREFIX
);
141 char *p
= sig
+ sizeof("GOODSIG");
142 while (*p
&& isxdigit(*p
))
146 std::clog
<< "Got GOODSIG, key ID:" << sig
<< std::endl
;
147 GoodSigners
.push_back(string(sig
));
149 else if (strncmp(buffer
, GNUPGVALIDSIG
, sizeof(GNUPGVALIDSIG
)-1) == 0)
151 char *sig
= buffer
+ sizeof(GNUPGVALIDSIG
);
153 while (*p
&& isxdigit(*p
))
157 std::clog
<< "Got VALIDSIG, key ID: " << sig
<< std::endl
;
158 ValidSigners
.push_back(string(sig
));
164 // apt-key has a --keyid parameter, but this requires gpg, so we call it without it
165 // and instead check after the fact which keyids where used for verification
169 std::clog
<< "GoodSigs needs to be limited to keyid " << key
<< std::endl
;
170 std::vector
<std::string
>::iterator
const foundItr
= std::find(ValidSigners
.begin(), ValidSigners
.end(), key
);
171 bool const found
= (foundItr
!= ValidSigners
.end());
172 std::copy(GoodSigners
.begin(), GoodSigners
.end(), std::back_insert_iterator
<std::vector
<std::string
> >(NoPubKeySigners
));
175 // we look for GOODSIG here as well as an expired sig is a valid sig as well (but not a good one)
176 std::string
const goodlongkeyid
= "GOODSIG " + key
.substr(24, 16);
177 bool const foundGood
= std::find(GoodSigners
.begin(), GoodSigners
.end(), goodlongkeyid
) != GoodSigners
.end();
179 std::clog
<< "Key " << key
<< " is valid sig, is " << goodlongkeyid
<< " also a good one? " << (foundGood
? "yes" : "no") << std::endl
;
183 GoodSigners
.push_back(goodlongkeyid
);
184 NoPubKeySigners
.erase(std::remove(NoPubKeySigners
.begin(), NoPubKeySigners
.end(), goodlongkeyid
), NoPubKeySigners
.end());
192 waitpid(pid
, &status
, 0);
195 ioprintf(std::clog
, "gpgv exited with status %i\n", WEXITSTATUS(status
));
198 if (WEXITSTATUS(status
) == 0)
202 // gpgv will report success, but we want to enforce a certain keyring
203 // so if we haven't found the key the valid we found is in fact invalid
204 if (GoodSigners
.empty())
205 return _("At least one invalid signature was encountered.");
209 if (GoodSigners
.empty())
210 return _("Internal error: Good signature, but could not determine key fingerprint?!");
214 else if (WEXITSTATUS(status
) == 1)
215 return _("At least one invalid signature was encountered.");
216 else if (WEXITSTATUS(status
) == 111)
217 return _("Could not execute 'apt-key' to verify signature (is gnupg installed?)");
218 else if (WEXITSTATUS(status
) == 112)
220 // acquire system checks for "NODATA" to generate GPG errors (the others are only warnings)
222 //TRANSLATORS: %s is a single techy word like 'NODATA'
223 strprintf(errmsg
, _("Clearsigned file isn't valid, got '%s' (does the network require authentication?)"), "NODATA");
227 return _("Unknown error executing apt-key");
230 bool GPGVMethod::URIAcquire(std::string
const &Message
, FetchItem
*Itm
)
232 URI
const Get
= Itm
->Uri
;
233 string
const Path
= Get
.Host
+ Get
.Path
; // To account for relative paths
234 std::string
const key
= LookupTag(Message
, "Signed-By");
235 vector
<string
> GoodSigners
;
236 vector
<string
> BadSigners
;
237 // a worthless signature is a expired or revoked one
238 vector
<string
> WorthlessSigners
;
239 vector
<string
> NoPubKeySigners
;
242 Res
.Filename
= Itm
->DestFile
;
245 // Run apt-key on file, extract contents and get the key ID of the signer
246 string msg
= VerifyGetSigners(Path
.c_str(), Itm
->DestFile
.c_str(), key
,
247 GoodSigners
, BadSigners
, WorthlessSigners
,
249 if (GoodSigners
.empty() || !BadSigners
.empty() || !NoPubKeySigners
.empty())
252 // In this case, something bad probably happened, so we just go
253 // with what the other method gave us for an error message.
254 if (BadSigners
.empty() && WorthlessSigners
.empty() && NoPubKeySigners
.empty())
258 if (!BadSigners
.empty())
260 errmsg
+= _("The following signatures were invalid:\n");
261 for (vector
<string
>::iterator I
= BadSigners
.begin();
262 I
!= BadSigners
.end(); ++I
)
263 errmsg
+= (*I
+ "\n");
265 if (!WorthlessSigners
.empty())
267 errmsg
+= _("The following signatures were invalid:\n");
268 for (vector
<string
>::iterator I
= WorthlessSigners
.begin();
269 I
!= WorthlessSigners
.end(); ++I
)
270 errmsg
+= (*I
+ "\n");
272 if (!NoPubKeySigners
.empty())
274 errmsg
+= _("The following signatures couldn't be verified because the public key is not available:\n");
275 for (vector
<string
>::iterator I
= NoPubKeySigners
.begin();
276 I
!= NoPubKeySigners
.end(); ++I
)
277 errmsg
+= (*I
+ "\n");
280 // this is only fatal if we have no good sigs or if we have at
281 // least one bad signature. good signatures and NoPubKey signatures
282 // happen easily when a file is signed with multiple signatures
283 if(GoodSigners
.empty() or !BadSigners
.empty())
284 return _error
->Error("%s", errmsg
.c_str());
287 // Just pass the raw output up, because passing it as a real data
288 // structure is too difficult with the method stuff. We keep it
289 // as three separate vectors for future extensibility.
290 Res
.GPGVOutput
= GoodSigners
;
291 Res
.GPGVOutput
.insert(Res
.GPGVOutput
.end(),BadSigners
.begin(),BadSigners
.end());
292 Res
.GPGVOutput
.insert(Res
.GPGVOutput
.end(),NoPubKeySigners
.begin(),NoPubKeySigners
.end());
295 if (_config
->FindB("Debug::Acquire::gpgv", false))
297 std::clog
<< "apt-key succeeded\n";
306 setlocale(LC_ALL
, "");