1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/KeyRing.cc
14 //#include "zypp/base/Logger.h"
19 #include "zypp/ZYppFactory.h"
20 #include "zypp/ZYpp.h"
22 #include <boost/regex.hpp>
24 #include "zypp/base/String.h"
25 #include "zypp/base/IOStream.h"
26 #include "zypp/KeyRing.h"
27 #include "zypp/ExternalProgram.h"
28 #include "zypp/TmpPath.h"
31 using namespace boost;
32 using namespace zypp::filesystem;
35 ///////////////////////////////////////////////////////////////////
37 { /////////////////////////////////////////////////////////////////
39 IMPL_PTR_TYPE(KeyRing);
41 static void dumpRegexpResults( const boost::smatch &what )
43 for ( unsigned int k=0; k < what.size(); k++)
45 XXX << "[match "<< k << "] [" << what[k] << "]" << std::endl;
49 static bool printLine( const std::string &line )
51 MIL << line << std::endl;
54 static void dumpFile(const Pathname &file)
56 std::ifstream is(file.asString().c_str());
57 iostr::forEachLine( is, printLine);
60 bool KeyRingReport::askUserToAcceptUnsignedFile( const Pathname &file )
63 bool KeyRingReport::askUserToAcceptUnknownKey( const Pathname &file, const std::string &keyid, const std::string &keyname )
66 bool KeyRingReport::askUserToTrustKey( const std::string &keyid, const std::string &keyname, const std::string &keydetails )
69 bool KeyRingReport::askUserToAcceptVerificationFailed( const Pathname &file, const std::string &keyid, const std::string &keyname )
72 ///////////////////////////////////////////////////////////////////
74 // CLASS NAME : KeyRing::Impl
76 /** KeyRing implementation. */
81 _general_kr = _general_tmp_dir.path();
82 _trusted_kr = _trusted_tmp_dir.path();
86 Impl( const Pathname &general_kr, const Pathname &trusted_kr )
88 filesystem::assert_dir(general_kr);
89 filesystem::assert_dir(trusted_kr);
91 _general_kr = general_kr;
92 _trusted_kr = trusted_kr;
96 void importKey( const Pathname &keyfile, bool trusted = false);
97 PublicKey readPublicKey( const Pathname &keyfile );
98 std::string readSignatureKeyId( const Pathname &data, const Pathname &keyfile );
100 void deleteKey( const std::string &id, bool trusted );
101 std::list<PublicKey> trustedPublicKeys();
102 std::list<PublicKey> publicKeys();
104 void dumpPublicKey( const std::string &id, bool trusted, std::ostream &stream );
106 bool verifyFileSignatureWorkflow( const Pathname &file, const Pathname &signature);
108 bool verifyFileSignature( const Pathname &file, const Pathname &signature);
109 bool verifyFileTrustedSignature( const Pathname &file, const Pathname &signature);
111 //mutable std::map<Locale, std::string> translations;
112 bool verifyFile( const Pathname &file, const Pathname &signature, const Pathname &keyring);
113 void importKey( const Pathname &keyfile, const Pathname &keyring);
115 void exportKey( std::string id, const Pathname &keyfile, bool trusted);
117 void deleteKey( const std::string &id, const Pathname &keyring );
118 std::list<PublicKey> publicKeys(const Pathname &keyring);
120 bool publicKeyExists( std::string id, const Pathname &keyring);
122 Pathname _general_kr;
123 Pathname _trusted_kr;
125 // Used for trusted and untrusted keyrings
126 TmpDir _trusted_tmp_dir;
127 TmpDir _general_tmp_dir;
129 /** Offer default Impl. */
130 static shared_ptr<Impl> nullimpl()
132 static shared_ptr<Impl> _nullimpl( new Impl );
137 friend Impl * rwcowClone<Impl>( const Impl * rhs );
138 /** clone for RWCOW_pointer */
140 { return new Impl( *this ); }
143 void KeyRing::Impl::importKey( const Pathname &keyfile, bool trusted)
145 importKey( keyfile, trusted ? _trusted_kr : _general_kr );
148 void KeyRing::Impl::deleteKey( const std::string &id, bool trusted)
150 deleteKey( id, trusted ? _trusted_kr : _general_kr );
153 std::list<PublicKey> KeyRing::Impl::publicKeys()
155 return publicKeys( _general_kr );
158 std::list<PublicKey> KeyRing::Impl::trustedPublicKeys()
160 return publicKeys( _trusted_kr );
163 bool KeyRing::Impl::verifyFileTrustedSignature( const Pathname &file, const Pathname &signature)
165 return verifyFile( file, signature, _trusted_kr );
168 bool KeyRing::Impl::verifyFileSignature( const Pathname &file, const Pathname &signature)
170 return verifyFile( file, signature, _general_kr );
173 bool KeyRing::Impl::publicKeyExists( std::string id, const Pathname &keyring)
175 std::list<PublicKey> keys = publicKeys(keyring);
176 for (std::list<PublicKey>::const_iterator it = keys.begin(); it != keys.end(); it++)
178 if ( id == (*it).id )
184 void KeyRing::Impl::exportKey( std::string id, const Pathname &keyfile, bool trusted)
187 std::ofstream os(keyfile.asString().c_str());
188 dumpPublicKey( id, trusted, os );
191 catch (std::exception &e)
193 ERR << "Cannot export key " << id << " from " << (trusted ? "trusted" : "untrusted ") << " keyring to file " << keyfile << std::endl;
197 void KeyRing::Impl::dumpPublicKey( const std::string &id, bool trusted, std::ostream &stream )
199 Pathname keyring = trusted ? _trusted_kr : _general_kr;
206 "--no-permission-warning",
209 keyring.asString().c_str(),
215 ExternalProgram prog(argv,ExternalProgram::Discard_Stderr, false, -1, true);
218 for(line = prog.receiveLine(), count=0; !line.empty(); line = prog.receiveLine(), count++ )
226 bool KeyRing::Impl::verifyFileSignatureWorkflow( const Pathname &file, const Pathname &signature)
228 if ( ! getenv("ZYPP_CHECKSIG") )
230 MIL << "Digital signature check disabled. Accepting source." << std::endl;
234 callback::SendReport<KeyRingReport> report;
235 callback::SendReport<KeyRingSignals> emitSignal;
236 MIL << "Going to verify signature for " << file << " with " << signature << std::endl;
238 // if signature does not exists, ask user if he wants to accept unsigned file.
239 if( signature.empty() || (!PathInfo(signature).isExist()) )
241 bool res = report->askUserToAcceptUnsignedFile( file );
242 MIL << "User decision on unsigned file: " << res << endl;
246 // get the id of the signature
247 std::string id = readSignatureKeyId(file, signature);
249 // doeskey exists in trusted keyring
250 if ( publicKeyExists( id, _trusted_kr ) )
253 exportKey( id, trustedKey.path(), true);
254 PublicKey key = readPublicKey(trustedKey.path());
255 MIL << "Key " << id << " " << key.name << " is trusted" << std::endl;
256 // it exists, is trusted, does it validates?
257 if ( verifyFile( file, signature, _trusted_kr ) )
260 return report->askUserToAcceptVerificationFailed( file, key.id, key.name );
264 if ( publicKeyExists( id, _general_kr ) )
267 exportKey( id, unKey.path(), false);
268 MIL << "Exported key " << id << " to " << unKey << std::endl;
270 PublicKey key = readPublicKey(unKey.path());
271 MIL << "Key " << id << " " << key.name << " is not trusted" << std::endl;
272 // ok the key is not trusted, ask the user to trust it or not
273 #warning We need the key details passed to the callback
274 if ( report->askUserToTrustKey(key.id, key.name, "") )
276 MIL << "User wants to trust key " << id << " " << key.name << std::endl;
277 //dumpFile(unKey.path());
279 importKey( unKey.path(), _trusted_kr );
280 emitSignal->trustedKeyAdded( (const KeyRing &)(*this), id, key.name );
283 if ( verifyFile( file, signature, _trusted_kr ) )
285 MIL << "File signature is verified" << std::endl;
290 MIL << "File signature check fails" << std::endl;
291 if ( report->askUserToAcceptVerificationFailed( file, key.id, key.name ) )
293 MIL << "User continues anyway." << std::endl;
298 MIL << "User does not want to continue" << std::endl;
305 MIL << "User does not want to trust key " << id << " " << key.name << std::endl;
312 if ( report->askUserToAcceptUnknownKey( file, id, "Unknown Key" ) )
314 MIL << "User wants to accept unknown key " << id << std::endl;
319 MIL << "User does not want to accept unknown key " << id << std::endl;
328 PublicKey KeyRing::Impl::readPublicKey( const Pathname &keyfile )
333 "--with-fingerprint",
341 keyfile.asString().c_str(),
345 ExternalProgram prog(argv,ExternalProgram::Discard_Stderr, false, -1, true);
350 boost::regex rxColons("^([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):\n$");
352 // pub:-:1024:17:A84EDAE89C800ACA:2000-10-19:2008-06-21::-:SuSE Package Signing Key <build@suse.de>:
355 for(line = prog.receiveLine(), count=0; !line.empty(); line = prog.receiveLine(), count++ )
357 //MIL << "[" << line << "]" << std::endl;
359 if(boost::regex_match(line, what, rxColons, boost::match_extra))
361 if ( what[1] == "pub" )
367 //dumpRegexpResults(what);
374 std::list<PublicKey> KeyRing::Impl::publicKeys(const Pathname &keyring)
380 "--list-public-keys",
382 "--with-fingerprint",
384 keyring.asString().c_str(),
387 std::list<PublicKey> keys;
389 ExternalProgram prog(argv,ExternalProgram::Discard_Stderr, false, -1, true);
393 boost::regex rxColons("^([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):\n$");
394 for(line = prog.receiveLine(), count=0; !line.empty(); line = prog.receiveLine(), count++ )
396 //MIL << line << std::endl;
398 if(boost::regex_match(line, what, rxColons, boost::match_extra))
400 if ( what[1] == "pub" )
405 MIL << "Found key " << key.id << " [" << key.name << "]" << std::endl;
408 dumpRegexpResults(what);
415 void KeyRing::Impl::importKey( const Pathname &keyfile, const Pathname &keyring)
423 "--no-permission-warning",
427 keyring.asString().c_str(),
429 keyfile.asString().c_str(),
434 ExternalProgram prog(argv,ExternalProgram::Discard_Stderr, false, -1, true);
438 // ZYPP_THROW(Exception("failed to import key"));
441 void KeyRing::Impl::deleteKey( const std::string &id, const Pathname &keyring )
453 keyring.asString().c_str(),
459 ExternalProgram prog(argv,ExternalProgram::Discard_Stderr, false, -1, true);
461 int code = prog.close();
463 ZYPP_THROW(Exception("Failed to delete key."));
465 MIL << "Deleted key " << id << " from keyring " << keyring << std::endl;
469 std::string KeyRing::Impl::readSignatureKeyId( const Pathname &data, const Pathname &keyfile )
471 // HACK create a tmp keyring with no keys
484 dir.path().asString().c_str(),
486 keyfile.asString().c_str(),
487 data.asString().c_str(),
491 ExternalProgram prog(argv,ExternalProgram::Discard_Stderr, false, -1, true);
496 boost::regex rxNoKey("^\\[GNUPG:\\] NO_PUBKEY (.+)\n$");
498 for(line = prog.receiveLine(), count=0; !line.empty(); line = prog.receiveLine(), count++ )
500 //MIL << "[" << line << "]" << std::endl;
502 if(boost::regex_match(line, what, rxNoKey, boost::match_extra))
504 if ( what.size() > 1 )
506 //dumpRegexpResults(what);
513 bool KeyRing::Impl::verifyFile( const Pathname &file, const Pathname &signature, const Pathname &keyring)
525 keyring.asString().c_str(),
527 signature.asString().c_str(),
528 file.asString().c_str(),
532 // no need to parse output for now
533 // [GNUPG:] SIG_ID yCc4u223XRJnLnVAIllvYbUd8mQ 2006-03-29 1143618744
534 // [GNUPG:] GOODSIG A84EDAE89C800ACA SuSE Package Signing Key <build@suse.de>
535 // gpg: Good signature from "SuSE Package Signing Key <build@suse.de>"
536 // [GNUPG:] VALIDSIG 79C179B2E1C820C1890F9994A84EDAE89C800ACA 2006-03-29 1143618744 0 3 0 17 2 00 79C179B2E1C820C1890F9994A84EDAE89C800ACA
537 // [GNUPG:] TRUST_UNDEFINED
539 // [GNUPG:] ERRSIG A84EDAE89C800ACA 17 2 00 1143618744 9
540 // [GNUPG:] NO_PUBKEY A84EDAE89C800ACA
542 ExternalProgram prog(argv,ExternalProgram::Discard_Stderr, false, -1, true);
544 return (prog.close() == 0) ? true : false;
547 ///////////////////////////////////////////////////////////////////
549 ///////////////////////////////////////////////////////////////////
551 // CLASS NAME : KeyRing
553 ///////////////////////////////////////////////////////////////////
555 ///////////////////////////////////////////////////////////////////
557 // METHOD NAME : KeyRing::KeyRing
558 // METHOD TYPE : Ctor
561 : _pimpl( new Impl() )
564 ///////////////////////////////////////////////////////////////////
566 // METHOD NAME : KeyRing::KeyRing
567 // METHOD TYPE : Ctor
569 //KeyRing::KeyRing( const Pathname &general_kr, const Pathname &trusted_kr )
570 //: _pimpl( new Impl(general_kr, trusted_kr) )
573 ///////////////////////////////////////////////////////////////////
575 // METHOD NAME : KeyRing::~KeyRing
576 // METHOD TYPE : Dtor
581 ///////////////////////////////////////////////////////////////////
583 // Forward to implementation:
585 ///////////////////////////////////////////////////////////////////
587 void KeyRing::importKey( const Pathname &keyfile, bool trusted)
589 _pimpl->importKey(keyfile, trusted);
592 PublicKey KeyRing::readPublicKey( const Pathname &keyfile )
594 return _pimpl->readPublicKey(keyfile);
597 std::string KeyRing::readSignatureKeyId( const Pathname &data, const Pathname &keyfile )
599 return _pimpl->readSignatureKeyId(data, keyfile);
602 void KeyRing::deleteKey( const std::string &id, bool trusted )
604 _pimpl->deleteKey(id, trusted);
607 std::list<PublicKey> KeyRing::publicKeys()
609 return _pimpl->publicKeys();
612 std::list<PublicKey> KeyRing::trustedPublicKeys()
614 return _pimpl->trustedPublicKeys();
617 bool KeyRing::verifyFileSignatureWorkflow( const Pathname &file, const Pathname &signature)
619 return _pimpl->verifyFileSignatureWorkflow(file, signature);
622 bool KeyRing::verifyFileSignature( const Pathname &file, const Pathname &signature)
624 return _pimpl->verifyFileSignature(file, signature);
627 bool KeyRing::verifyFileTrustedSignature( const Pathname &file, const Pathname &signature)
629 return _pimpl->verifyFileTrustedSignature(file, signature);
632 void KeyRing::dumpPublicKey( const std::string &id, bool trusted, std::ostream &stream )
634 _pimpl->dumpPublicKey( id, trusted, stream);
637 /////////////////////////////////////////////////////////////////
639 ///////////////////////////////////////////////////////////////////