]>
git.saurik.com Git - apt.git/blob - methods/gpgv.cc
de9dfea1eb943150f89923444cac34ce78e5e523
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::array
<string
, 1> WeakDigests
{
48 class GPGVMethod
: public aptMethod
51 string
VerifyGetSigners(const char *file
, const char *outfile
,
52 std::string
const &key
,
53 vector
<string
> &GoodSigners
,
54 vector
<string
> &BadSigners
,
55 vector
<string
> &WorthlessSigners
,
56 vector
<string
> &NoPubKeySigners
);
59 virtual bool URIAcquire(std::string
const &Message
, FetchItem
*Itm
) APT_OVERRIDE
;
62 GPGVMethod() : aptMethod("gpgv","1.0",SingleInstance
| SendConfig
) {};
65 string
GPGVMethod::VerifyGetSigners(const char *file
, const char *outfile
,
66 std::string
const &key
,
67 vector
<string
> &GoodSigners
,
68 vector
<string
> &BadSigners
,
69 vector
<string
> &WorthlessSigners
,
70 vector
<string
> &NoPubKeySigners
)
72 bool const Debug
= _config
->FindB("Debug::Acquire::gpgv", false);
75 std::clog
<< "inside VerifyGetSigners" << std::endl
;
78 bool const keyIsID
= (key
.empty() == false && key
[0] != '/');
81 return "Couldn't create pipe";
85 return string("Couldn't spawn new process") + strerror(errno
);
87 ExecGPGV(outfile
, file
, 3, fd
, (keyIsID
? "" : key
));
90 FILE *pipein
= fdopen(fd
[0], "r");
92 // Loop over the output of apt-key (which really is gnupg), and check the signatures.
93 std::vector
<std::string
> ValidSigners
;
94 size_t buffersize
= 0;
98 if (getline(&buffer
, &buffersize
, pipein
) == -1)
101 std::clog
<< "Read: " << buffer
<< std::endl
;
103 // Push the data into three separate vectors, which
104 // we later concatenate. They're kept separate so
105 // if we improve the apt method communication stuff later
106 // it will be better.
107 if (strncmp(buffer
, GNUPGBADSIG
, sizeof(GNUPGBADSIG
)-1) == 0)
110 std::clog
<< "Got BADSIG! " << std::endl
;
111 BadSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
113 else if (strncmp(buffer
, GNUPGNOPUBKEY
, sizeof(GNUPGNOPUBKEY
)-1) == 0)
116 std::clog
<< "Got NO_PUBKEY " << std::endl
;
117 NoPubKeySigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
119 else if (strncmp(buffer
, GNUPGNODATA
, sizeof(GNUPGBADSIG
)-1) == 0)
122 std::clog
<< "Got NODATA! " << std::endl
;
123 BadSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
125 else if (strncmp(buffer
, GNUPGKEYEXPIRED
, sizeof(GNUPGKEYEXPIRED
)-1) == 0)
128 std::clog
<< "Got KEYEXPIRED! " << std::endl
;
129 WorthlessSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
131 else if (strncmp(buffer
, GNUPGREVKEYSIG
, sizeof(GNUPGREVKEYSIG
)-1) == 0)
134 std::clog
<< "Got REVKEYSIG! " << std::endl
;
135 WorthlessSigners
.push_back(string(buffer
+sizeof(GNUPGPREFIX
)));
137 else if (strncmp(buffer
, GNUPGGOODSIG
, sizeof(GNUPGGOODSIG
)-1) == 0)
139 char *sig
= buffer
+ sizeof(GNUPGPREFIX
);
140 char *p
= sig
+ sizeof("GOODSIG");
141 while (*p
&& isxdigit(*p
))
145 std::clog
<< "Got GOODSIG, key ID:" << sig
<< std::endl
;
146 GoodSigners
.push_back(string(sig
));
148 else if (strncmp(buffer
, GNUPGVALIDSIG
, sizeof(GNUPGVALIDSIG
)-1) == 0)
150 char *sig
= buffer
+ sizeof(GNUPGVALIDSIG
);
151 std::istringstream
iss((string(sig
)));
152 vector
<string
> tokens
{std::istream_iterator
<string
>{iss
},
153 std::istream_iterator
<string
>{}};
155 while (*p
&& isxdigit(*p
))
159 std::clog
<< "Got VALIDSIG, key ID: " << sig
<< std::endl
;
160 // Reject weak digest algorithms
161 if (std::find(WeakDigests
.begin(), WeakDigests
.end(), tokens
[7]) != WeakDigests
.end())
163 // Treat them like an expired key: For that a message about expiry
164 // is emitted, a VALIDSIG, but no GOODSIG.
165 WorthlessSigners
.push_back("WEAKDIGEST " + string(sig
));
166 GoodSigners
.erase(std::remove(GoodSigners
.begin(), GoodSigners
.end(), string(sig
)));
169 ValidSigners
.push_back(string(sig
));
175 // apt-key has a --keyid parameter, but this requires gpg, so we call it without it
176 // and instead check after the fact which keyids where used for verification
180 std::clog
<< "GoodSigs needs to be limited to keyid " << key
<< std::endl
;
181 std::vector
<std::string
>::iterator
const foundItr
= std::find(ValidSigners
.begin(), ValidSigners
.end(), key
);
182 bool const found
= (foundItr
!= ValidSigners
.end());
183 std::copy(GoodSigners
.begin(), GoodSigners
.end(), std::back_insert_iterator
<std::vector
<std::string
> >(NoPubKeySigners
));
186 // we look for GOODSIG here as well as an expired sig is a valid sig as well (but not a good one)
187 std::string
const goodlongkeyid
= "GOODSIG " + key
.substr(24, 16);
188 bool const foundGood
= std::find(GoodSigners
.begin(), GoodSigners
.end(), goodlongkeyid
) != GoodSigners
.end();
190 std::clog
<< "Key " << key
<< " is valid sig, is " << goodlongkeyid
<< " also a good one? " << (foundGood
? "yes" : "no") << std::endl
;
194 GoodSigners
.push_back(goodlongkeyid
);
195 NoPubKeySigners
.erase(std::remove(NoPubKeySigners
.begin(), NoPubKeySigners
.end(), goodlongkeyid
), NoPubKeySigners
.end());
203 waitpid(pid
, &status
, 0);
206 ioprintf(std::clog
, "gpgv exited with status %i\n", WEXITSTATUS(status
));
209 if (WEXITSTATUS(status
) == 0)
213 // gpgv will report success, but we want to enforce a certain keyring
214 // so if we haven't found the key the valid we found is in fact invalid
215 if (GoodSigners
.empty())
216 return _("At least one invalid signature was encountered.");
220 if (GoodSigners
.empty())
221 return _("Internal error: Good signature, but could not determine key fingerprint?!");
225 else if (WEXITSTATUS(status
) == 1)
226 return _("At least one invalid signature was encountered.");
227 else if (WEXITSTATUS(status
) == 111)
228 return _("Could not execute 'apt-key' to verify signature (is gnupg installed?)");
229 else if (WEXITSTATUS(status
) == 112)
231 // acquire system checks for "NODATA" to generate GPG errors (the others are only warnings)
233 //TRANSLATORS: %s is a single techy word like 'NODATA'
234 strprintf(errmsg
, _("Clearsigned file isn't valid, got '%s' (does the network require authentication?)"), "NODATA");
238 return _("Unknown error executing apt-key");
241 bool GPGVMethod::URIAcquire(std::string
const &Message
, FetchItem
*Itm
)
243 URI
const Get
= Itm
->Uri
;
244 string
const Path
= Get
.Host
+ Get
.Path
; // To account for relative paths
245 std::string
const key
= LookupTag(Message
, "Signed-By");
246 vector
<string
> GoodSigners
;
247 vector
<string
> BadSigners
;
248 // a worthless signature is a expired or revoked one
249 vector
<string
> WorthlessSigners
;
250 vector
<string
> NoPubKeySigners
;
253 Res
.Filename
= Itm
->DestFile
;
256 // Run apt-key on file, extract contents and get the key ID of the signer
257 string msg
= VerifyGetSigners(Path
.c_str(), Itm
->DestFile
.c_str(), key
,
258 GoodSigners
, BadSigners
, WorthlessSigners
,
260 if (GoodSigners
.empty() || !BadSigners
.empty() || !NoPubKeySigners
.empty())
263 // In this case, something bad probably happened, so we just go
264 // with what the other method gave us for an error message.
265 if (BadSigners
.empty() && WorthlessSigners
.empty() && NoPubKeySigners
.empty())
269 if (!BadSigners
.empty())
271 errmsg
+= _("The following signatures were invalid:\n");
272 for (vector
<string
>::iterator I
= BadSigners
.begin();
273 I
!= BadSigners
.end(); ++I
)
274 errmsg
+= (*I
+ "\n");
276 if (!WorthlessSigners
.empty())
278 errmsg
+= _("The following signatures were invalid:\n");
279 for (vector
<string
>::iterator I
= WorthlessSigners
.begin();
280 I
!= WorthlessSigners
.end(); ++I
)
281 errmsg
+= (*I
+ "\n");
283 if (!NoPubKeySigners
.empty())
285 errmsg
+= _("The following signatures couldn't be verified because the public key is not available:\n");
286 for (vector
<string
>::iterator I
= NoPubKeySigners
.begin();
287 I
!= NoPubKeySigners
.end(); ++I
)
288 errmsg
+= (*I
+ "\n");
291 // this is only fatal if we have no good sigs or if we have at
292 // least one bad signature. good signatures and NoPubKey signatures
293 // happen easily when a file is signed with multiple signatures
294 if(GoodSigners
.empty() or !BadSigners
.empty())
295 return _error
->Error("%s", errmsg
.c_str());
298 // Just pass the raw output up, because passing it as a real data
299 // structure is too difficult with the method stuff. We keep it
300 // as three separate vectors for future extensibility.
301 Res
.GPGVOutput
= GoodSigners
;
302 Res
.GPGVOutput
.insert(Res
.GPGVOutput
.end(),BadSigners
.begin(),BadSigners
.end());
303 Res
.GPGVOutput
.insert(Res
.GPGVOutput
.end(),NoPubKeySigners
.begin(),NoPubKeySigners
.end());
306 if (_config
->FindB("Debug::Acquire::gpgv", false))
308 std::clog
<< "apt-key succeeded\n";
317 setlocale(LC_ALL
, "");