1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/KeyRing.cc
18 #include "zypp/ZYppFactory.h"
19 #include "zypp/ZYpp.h"
21 #include "zypp/base/Logger.h"
22 #include "zypp/base/IOStream.h"
23 #include "zypp/base/String.h"
24 #include "zypp/Pathname.h"
25 #include "zypp/KeyRing.h"
26 #include "zypp/ExternalProgram.h"
27 #include "zypp/TmpPath.h"
30 using namespace zypp::filesystem;
33 #undef ZYPP_BASE_LOGGER_LOGGROUP
34 #define ZYPP_BASE_LOGGER_LOGGROUP "zypp::KeyRing"
36 ///////////////////////////////////////////////////////////////////
38 { /////////////////////////////////////////////////////////////////
40 IMPL_PTR_TYPE(KeyRing);
42 static void dumpRegexpResults( const str::smatch &what )
44 for ( unsigned int k=0; k < what.size(); k++)
46 XXX << "[match "<< k << "] [" << what[k] << "]" << std::endl;
50 static bool printLine( const std::string &line )
52 MIL << line << std::endl;
55 static void dumpFile(const Pathname &file)
57 std::ifstream is(file.asString().c_str());
58 iostr::forEachLine( is, printLine);
63 bool _keyRingDefaultAccept( getenv("ZYPP_KEYRING_DEFAULT_ACCEPT_ALL") );
66 bool KeyRingReport::askUserToAcceptUnsignedFile( const std::string &file )
67 { return _keyRingDefaultAccept; }
69 bool KeyRingReport::askUserToAcceptUnknownKey( const std::string &file, const std::string &keyid, const std::string &keyname, const std::string &fingerprint )
70 { return _keyRingDefaultAccept; }
72 bool KeyRingReport::askUserToTrustKey( const std::string &keyid, const std::string &keyname, const std::string &fingerprint )
73 { return _keyRingDefaultAccept; }
75 bool KeyRingReport::askUserToAcceptVerificationFailed( const std::string &file, const std::string &keyid, const std::string &keyname, const std::string &fingerprint )
76 { return _keyRingDefaultAccept; }
78 ///////////////////////////////////////////////////////////////////
80 // CLASS NAME : KeyRing::Impl
82 /** KeyRing implementation. */
87 _general_kr = _general_tmp_dir.path();
88 _trusted_kr = _trusted_tmp_dir.path();
92 Impl( const Pathname &general_kr, const Pathname &trusted_kr )
94 filesystem::assert_dir(general_kr);
95 filesystem::assert_dir(trusted_kr);
97 _general_kr = general_kr;
98 _trusted_kr = trusted_kr;
102 void importKey( const Pathname &keyfile, bool trusted = false);
103 PublicKey readPublicKey( const Pathname &keyfile );
104 std::string readSignatureKeyId( const Pathname &data, const Pathname &keyfile );
106 void deleteKey( const std::string &id, bool trusted );
107 std::list<PublicKey> trustedPublicKeys();
108 std::list<PublicKey> publicKeys();
110 void dumpPublicKey( const std::string &id, bool trusted, std::ostream &stream );
112 bool verifyFileSignatureWorkflow( const Pathname &file, const std::string filedesc, const Pathname &signature);
114 bool verifyFileSignature( const Pathname &file, const Pathname &signature);
115 bool verifyFileTrustedSignature( const Pathname &file, const Pathname &signature);
117 //mutable std::map<Locale, std::string> translations;
118 bool verifyFile( const Pathname &file, const Pathname &signature, const Pathname &keyring);
119 void importKey( const Pathname &keyfile, const Pathname &keyring);
121 void exportKey( std::string id, const Pathname &keyfile, bool trusted);
123 void deleteKey( const std::string &id, const Pathname &keyring );
124 std::list<PublicKey> publicKeys(const Pathname &keyring);
126 bool publicKeyExists( std::string id, const Pathname &keyring);
128 Pathname _general_kr;
129 Pathname _trusted_kr;
131 // Used for trusted and untrusted keyrings
132 TmpDir _trusted_tmp_dir;
133 TmpDir _general_tmp_dir;
135 /** Offer default Impl. */
136 static shared_ptr<Impl> nullimpl()
138 static shared_ptr<Impl> _nullimpl( new Impl );
143 friend Impl * rwcowClone<Impl>( const Impl * rhs );
144 /** clone for RWCOW_pointer */
146 { return new Impl( *this ); }
149 void KeyRing::Impl::importKey( const Pathname &keyfile, bool trusted)
151 importKey( keyfile, trusted ? _trusted_kr : _general_kr );
154 void KeyRing::Impl::deleteKey( const std::string &id, bool trusted)
156 deleteKey( id, trusted ? _trusted_kr : _general_kr );
159 std::list<PublicKey> KeyRing::Impl::publicKeys()
161 return publicKeys( _general_kr );
164 std::list<PublicKey> KeyRing::Impl::trustedPublicKeys()
166 return publicKeys( _trusted_kr );
169 bool KeyRing::Impl::verifyFileTrustedSignature( const Pathname &file, const Pathname &signature)
171 return verifyFile( file, signature, _trusted_kr );
174 bool KeyRing::Impl::verifyFileSignature( const Pathname &file, const Pathname &signature)
176 return verifyFile( file, signature, _general_kr );
179 bool KeyRing::Impl::publicKeyExists( std::string id, const Pathname &keyring)
181 std::list<PublicKey> keys = publicKeys(keyring);
182 for (std::list<PublicKey>::const_iterator it = keys.begin(); it != keys.end(); it++)
184 if ( id == (*it).id )
190 void KeyRing::Impl::exportKey( std::string id, const Pathname &keyfile, bool trusted)
193 std::ofstream os(keyfile.asString().c_str());
194 dumpPublicKey( id, trusted, os );
197 catch (std::exception &e)
199 ERR << "Cannot export key " << id << " from " << (trusted ? "trusted" : "untrusted ") << " keyring to file " << keyfile << std::endl;
203 void KeyRing::Impl::dumpPublicKey( const std::string &id, bool trusted, std::ostream &stream )
205 Pathname keyring = trusted ? _trusted_kr : _general_kr;
212 "--no-permission-warning",
215 keyring.asString().c_str(),
221 ExternalProgram prog(argv,ExternalProgram::Discard_Stderr, false, -1, true);
224 for(line = prog.receiveLine(), count=0; !line.empty(); line = prog.receiveLine(), count++ )
232 bool KeyRing::Impl::verifyFileSignatureWorkflow( const Pathname &file, const std::string filedesc, const Pathname &signature)
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( filedesc );
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( filedesc, key.id, key.name, key.fingerprint );
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, key.fingerprint) )
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, key.fingerprint );
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( filedesc, key.id, key.name, key.fingerprint ) )
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( filedesc, id, "", "" ) )
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 str::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(str::regex_match(line, what, rxColons, str::match_extra))
361 if ( what[1] == "pub" )
367 else if ( what[1] == "fpr" )
369 key.fingerprint = what[10];
371 //dumpRegexpResults(what);
378 std::list<PublicKey> KeyRing::Impl::publicKeys(const Pathname &keyring)
384 "--list-public-keys",
386 "--with-fingerprint",
393 keyring.asString().c_str(),
396 std::list<PublicKey> keys;
398 ExternalProgram prog(argv,ExternalProgram::Discard_Stderr, false, -1, true);
402 str::regex rxColons("^([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):\n$");
403 str::regex rxColonsFpr("^([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):\n$");
405 for(line = prog.receiveLine(), count=0; !line.empty(); line = prog.receiveLine(), count++ )
407 //MIL << line << std::endl;
409 if(str::regex_match(line, what, rxColons, str::match_extra))
412 if ( what[1] == "pub" )
418 for(line2 = prog.receiveLine(); !line2.empty(); line2 = prog.receiveLine(), count++ )
421 if (str::regex_match(line2, what2, rxColonsFpr, str::match_extra))
423 if ( (what2[1] == "fpr") && (what2[1] != "pub") && (what2[1] !="sub"))
425 key.fingerprint = what2[10];
431 MIL << "Found key " << "[" << key.id << "]" << " [" << key.name << "]" << " [" << key.fingerprint << "]" << std::endl;
433 //dumpRegexpResults(what);
440 void KeyRing::Impl::importKey( const Pathname &keyfile, const Pathname &keyring)
448 "--no-permission-warning",
452 keyring.asString().c_str(),
454 keyfile.asString().c_str(),
459 ExternalProgram prog(argv,ExternalProgram::Discard_Stderr, false, -1, true);
463 // ZYPP_THROW(Exception("failed to import key"));
466 void KeyRing::Impl::deleteKey( const std::string &id, const Pathname &keyring )
478 keyring.asString().c_str(),
484 ExternalProgram prog(argv,ExternalProgram::Discard_Stderr, false, -1, true);
486 int code = prog.close();
488 ZYPP_THROW(Exception("Failed to delete key."));
490 MIL << "Deleted key " << id << " from keyring " << keyring << std::endl;
494 std::string KeyRing::Impl::readSignatureKeyId( const Pathname &data, const Pathname &keyfile )
496 // HACK create a tmp keyring with no keys
509 dir.path().asString().c_str(),
511 keyfile.asString().c_str(),
512 data.asString().c_str(),
516 ExternalProgram prog(argv,ExternalProgram::Discard_Stderr, false, -1, true);
521 str::regex rxNoKey("^\\[GNUPG:\\] NO_PUBKEY (.+)\n$");
523 for(line = prog.receiveLine(), count=0; !line.empty(); line = prog.receiveLine(), count++ )
525 //MIL << "[" << line << "]" << std::endl;
527 if(str::regex_match(line, what, rxNoKey, str::match_extra))
529 if ( what.size() > 1 )
531 //dumpRegexpResults(what);
538 bool KeyRing::Impl::verifyFile( const Pathname &file, const Pathname &signature, const Pathname &keyring)
550 keyring.asString().c_str(),
552 signature.asString().c_str(),
553 file.asString().c_str(),
557 // no need to parse output for now
558 // [GNUPG:] SIG_ID yCc4u223XRJnLnVAIllvYbUd8mQ 2006-03-29 1143618744
559 // [GNUPG:] GOODSIG A84EDAE89C800ACA SuSE Package Signing Key <build@suse.de>
560 // gpg: Good signature from "SuSE Package Signing Key <build@suse.de>"
561 // [GNUPG:] VALIDSIG 79C179B2E1C820C1890F9994A84EDAE89C800ACA 2006-03-29 1143618744 0 3 0 17 2 00 79C179B2E1C820C1890F9994A84EDAE89C800ACA
562 // [GNUPG:] TRUST_UNDEFINED
564 // [GNUPG:] ERRSIG A84EDAE89C800ACA 17 2 00 1143618744 9
565 // [GNUPG:] NO_PUBKEY A84EDAE89C800ACA
567 ExternalProgram prog(argv,ExternalProgram::Discard_Stderr, false, -1, true);
569 return (prog.close() == 0) ? true : false;
572 ///////////////////////////////////////////////////////////////////
574 ///////////////////////////////////////////////////////////////////
576 // CLASS NAME : KeyRing
578 ///////////////////////////////////////////////////////////////////
580 ///////////////////////////////////////////////////////////////////
582 // METHOD NAME : KeyRing::KeyRing
583 // METHOD TYPE : Ctor
586 : _pimpl( new Impl() )
589 ///////////////////////////////////////////////////////////////////
591 // METHOD NAME : KeyRing::KeyRing
592 // METHOD TYPE : Ctor
594 //KeyRing::KeyRing( const Pathname &general_kr, const Pathname &trusted_kr )
595 //: _pimpl( new Impl(general_kr, trusted_kr) )
598 ///////////////////////////////////////////////////////////////////
600 // METHOD NAME : KeyRing::~KeyRing
601 // METHOD TYPE : Dtor
606 ///////////////////////////////////////////////////////////////////
608 // Forward to implementation:
610 ///////////////////////////////////////////////////////////////////
612 void KeyRing::importKey( const Pathname &keyfile, bool trusted)
614 _pimpl->importKey(keyfile, trusted);
617 PublicKey KeyRing::readPublicKey( const Pathname &keyfile )
619 return _pimpl->readPublicKey(keyfile);
622 std::string KeyRing::readSignatureKeyId( const Pathname &data, const Pathname &keyfile )
624 return _pimpl->readSignatureKeyId(data, keyfile);
627 void KeyRing::deleteKey( const std::string &id, bool trusted )
629 _pimpl->deleteKey(id, trusted);
632 std::list<PublicKey> KeyRing::publicKeys()
634 return _pimpl->publicKeys();
637 std::list<PublicKey> KeyRing::trustedPublicKeys()
639 return _pimpl->trustedPublicKeys();
642 bool KeyRing::verifyFileSignatureWorkflow( const Pathname &file, const std::string filedesc, const Pathname &signature)
644 return _pimpl->verifyFileSignatureWorkflow(file, filedesc, signature);
647 bool KeyRing::verifyFileSignature( const Pathname &file, const Pathname &signature)
649 return _pimpl->verifyFileSignature(file, signature);
652 bool KeyRing::verifyFileTrustedSignature( const Pathname &file, const Pathname &signature)
654 return _pimpl->verifyFileTrustedSignature(file, signature);
657 void KeyRing::dumpPublicKey( const std::string &id, bool trusted, std::ostream &stream )
659 _pimpl->dumpPublicKey( id, trusted, stream);
662 /////////////////////////////////////////////////////////////////
664 ///////////////////////////////////////////////////////////////////