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. */
85 Impl(const Pathname &baseTmpDir) : _general_tmp_dir(baseTmpDir)
86 , _trusted_tmp_dir(baseTmpDir)
89 _base_dir = baseTmpDir;
93 Impl( const Pathname &general_kr, const Pathname &trusted_kr )
95 filesystem::assert_dir(general_kr);
96 filesystem::assert_dir(trusted_kr);
98 generalKeyRing() = general_kr;
99 trustedKeyRing() = trusted_kr;
103 void importKey( const Pathname &keyfile, bool trusted = false);
104 PublicKey readPublicKey( const Pathname &keyfile );
105 std::string readSignatureKeyId( const Pathname &signature );
107 void deleteKey( const std::string &id, bool trusted );
108 std::list<PublicKey> trustedPublicKeys();
109 std::list<PublicKey> publicKeys();
111 void dumpPublicKey( const std::string &id, bool trusted, std::ostream &stream );
113 bool verifyFileSignatureWorkflow( const Pathname &file, const std::string filedesc, const Pathname &signature);
115 bool verifyFileSignature( const Pathname &file, const Pathname &signature);
116 bool verifyFileTrustedSignature( const Pathname &file, const Pathname &signature);
118 //mutable std::map<Locale, std::string> translations;
119 bool verifyFile( const Pathname &file, const Pathname &signature, const Pathname &keyring);
120 void importKey( const Pathname &keyfile, const Pathname &keyring);
122 void exportKey( std::string id, const Pathname &keyfile, bool trusted);
124 void deleteKey( const std::string &id, const Pathname &keyring );
125 std::list<PublicKey> publicKeys(const Pathname &keyring);
127 bool publicKeyExists( std::string id, const Pathname &keyring);
129 const Pathname generalKeyRing() const;
130 const Pathname trustedKeyRing() const;
132 // Used for trusted and untrusted keyrings
133 TmpDir _trusted_tmp_dir;
134 TmpDir _general_tmp_dir;
137 /** Offer default Impl. */
138 static shared_ptr<Impl> nullimpl()
140 static shared_ptr<Impl> _nullimpl( new Impl( Pathname("/var/tmp") ) );
145 friend Impl * rwcowClone<Impl>( const Impl * rhs );
146 /** clone for RWCOW_pointer */
148 { return new Impl( *this ); }
152 const Pathname KeyRing::Impl::generalKeyRing() const
154 return _general_tmp_dir.path();
157 const Pathname KeyRing::Impl::trustedKeyRing() const
159 return _trusted_tmp_dir.path();
162 void KeyRing::Impl::importKey( const Pathname &keyfile, bool trusted)
164 importKey( keyfile, trusted ? trustedKeyRing() : generalKeyRing() );
167 void KeyRing::Impl::deleteKey( const std::string &id, bool trusted)
169 deleteKey( id, trusted ? trustedKeyRing() : generalKeyRing() );
172 std::list<PublicKey> KeyRing::Impl::publicKeys()
174 return publicKeys( generalKeyRing() );
177 std::list<PublicKey> KeyRing::Impl::trustedPublicKeys()
179 return publicKeys( trustedKeyRing() );
182 bool KeyRing::Impl::verifyFileTrustedSignature( const Pathname &file, const Pathname &signature)
184 return verifyFile( file, signature, trustedKeyRing() );
187 bool KeyRing::Impl::verifyFileSignature( const Pathname &file, const Pathname &signature)
189 return verifyFile( file, signature, generalKeyRing() );
192 bool KeyRing::Impl::publicKeyExists( std::string id, const Pathname &keyring)
194 MIL << "Searching key [" << id << "] in keyring " << keyring << std::endl;
195 std::list<PublicKey> keys = publicKeys(keyring);
196 for (std::list<PublicKey>::const_iterator it = keys.begin(); it != keys.end(); it++)
198 if ( id == (*it).id )
204 void KeyRing::Impl::exportKey( std::string id, const Pathname &keyfile, bool trusted)
207 std::ofstream os(keyfile.asString().c_str());
208 dumpPublicKey( id, trusted, os );
211 catch (std::exception &e)
213 ERR << "Cannot export key " << id << " from " << (trusted ? "trusted" : "untrusted ") << " keyring to file " << keyfile << std::endl;
217 void KeyRing::Impl::dumpPublicKey( const std::string &id, bool trusted, std::ostream &stream )
219 Pathname keyring = trusted ? trustedKeyRing() : generalKeyRing();
223 "--no-default-keyring",
227 "--no-permission-warning",
230 keyring.asString().c_str(),
236 ExternalProgram prog(argv,ExternalProgram::Discard_Stderr, false, -1, true);
239 for(line = prog.receiveLine(), count=0; !line.empty(); line = prog.receiveLine(), count++ )
247 bool KeyRing::Impl::verifyFileSignatureWorkflow( const Pathname &file, const std::string filedesc, const Pathname &signature)
249 callback::SendReport<KeyRingReport> report;
250 callback::SendReport<KeyRingSignals> emitSignal;
251 MIL << "Going to verify signature for " << file << " with " << signature << std::endl;
253 // if signature does not exists, ask user if he wants to accept unsigned file.
254 if( signature.empty() || (!PathInfo(signature).isExist()) )
256 bool res = report->askUserToAcceptUnsignedFile( filedesc );
257 MIL << "User decision on unsigned file: " << res << endl;
261 // get the id of the signature
262 std::string id = readSignatureKeyId(signature);
264 // doeskey exists in trusted keyring
265 if ( publicKeyExists( id, trustedKeyRing() ) )
267 TmpFile trustedKey(_base_dir);
268 exportKey( id, trustedKey.path(), true);
269 PublicKey key = readPublicKey(trustedKey.path());
270 MIL << "Key " << id << " " << key.name << " is trusted" << std::endl;
271 // it exists, is trusted, does it validates?
272 if ( verifyFile( file, signature, trustedKeyRing() ) )
275 return report->askUserToAcceptVerificationFailed( filedesc, key.id, key.name, key.fingerprint );
279 if ( publicKeyExists( id, generalKeyRing() ) )
281 TmpFile unKey(_base_dir);
282 exportKey( id, unKey.path(), false);
283 MIL << "Exported key " << id << " to " << unKey << std::endl;
285 PublicKey key = readPublicKey(unKey.path());
286 MIL << "Key " << id << " " << key.name << " is not trusted" << std::endl;
287 // ok the key is not trusted, ask the user to trust it or not
288 #warning We need the key details passed to the callback
289 if ( report->askUserToTrustKey(key.id, key.name, key.fingerprint) )
291 MIL << "User wants to trust key " << id << " " << key.name << std::endl;
292 //dumpFile(unKey.path());
294 importKey( unKey.path(), trustedKeyRing() );
295 emitSignal->trustedKeyAdded( (const KeyRing &)(*this), id, key.name, key.fingerprint );
298 if ( verifyFile( file, signature, trustedKeyRing() ) )
300 MIL << "File signature is verified" << std::endl;
305 MIL << "File signature check fails" << std::endl;
306 if ( report->askUserToAcceptVerificationFailed( filedesc, key.id, key.name, key.fingerprint ) )
308 MIL << "User continues anyway." << std::endl;
313 MIL << "User does not want to continue" << std::endl;
320 MIL << "User does not want to trust key " << id << " " << key.name << std::endl;
327 MIL << "File [" << file << "] ( " << filedesc << " ) signed with unknown key [" << id << "]" << std::endl;
328 if ( report->askUserToAcceptUnknownKey( filedesc, id, "", "" ) )
330 MIL << "User wants to accept unknown key " << id << std::endl;
335 MIL << "User does not want to accept unknown key " << id << std::endl;
344 PublicKey KeyRing::Impl::readPublicKey( const Pathname &keyfile )
346 TmpDir dir(_base_dir);
351 "--no-default-keyring",
353 dir.path().asString().c_str(),
354 "--with-fingerprint",
362 keyfile.asString().c_str(),
366 ExternalProgram prog(argv,ExternalProgram::Discard_Stderr, false, -1, true);
371 str::regex rxColons("^([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):\n$");
373 // pub:-:1024:17:A84EDAE89C800ACA:2000-10-19:2008-06-21::-:SuSE Package Signing Key <build@suse.de>:
376 for(line = prog.receiveLine(), count=0; !line.empty(); line = prog.receiveLine(), count++ )
378 //MIL << "[" << line << "]" << std::endl;
380 if(str::regex_match(line, what, rxColons, str::match_extra))
382 if ( what[1] == "pub" )
388 else if ( what[1] == "fpr" )
390 key.fingerprint = what[10];
392 //dumpRegexpResults(what);
399 std::list<PublicKey> KeyRing::Impl::publicKeys(const Pathname &keyring)
404 "--no-default-keyring",
406 "--list-public-keys",
408 "--with-fingerprint",
415 keyring.asString().c_str(),
418 std::list<PublicKey> keys;
420 ExternalProgram prog(argv,ExternalProgram::Discard_Stderr, false, -1, true);
424 str::regex rxColons("^([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):\n$");
425 str::regex rxColonsFpr("^([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):\n$");
427 for(line = prog.receiveLine(), count=0; !line.empty(); line = prog.receiveLine(), count++ )
429 //MIL << line << std::endl;
431 if(str::regex_match(line, what, rxColons, str::match_extra))
434 if ( what[1] == "pub" )
440 for(line2 = prog.receiveLine(); !line2.empty(); line2 = prog.receiveLine(), count++ )
443 if (str::regex_match(line2, what2, rxColonsFpr, str::match_extra))
445 if ( (what2[1] == "fpr") && (what2[1] != "pub") && (what2[1] !="sub"))
447 key.fingerprint = what2[10];
453 MIL << "Found key " << "[" << key.id << "]" << " [" << key.name << "]" << " [" << key.fingerprint << "]" << std::endl;
455 //dumpRegexpResults(what);
462 void KeyRing::Impl::importKey( const Pathname &keyfile, const Pathname &keyring)
467 "--no-default-keyring",
471 "--no-permission-warning",
475 keyring.asString().c_str(),
477 keyfile.asString().c_str(),
482 ExternalProgram prog(argv,ExternalProgram::Discard_Stderr, false, -1, true);
486 // ZYPP_THROW(Exception("failed to import key"));
489 void KeyRing::Impl::deleteKey( const std::string &id, const Pathname &keyring )
494 "--no-default-keyring",
502 keyring.asString().c_str(),
508 ExternalProgram prog(argv,ExternalProgram::Discard_Stderr, false, -1, true);
510 int code = prog.close();
512 ZYPP_THROW(Exception("Failed to delete key."));
514 MIL << "Deleted key " << id << " from keyring " << keyring << std::endl;
518 std::string KeyRing::Impl::readSignatureKeyId(const Pathname &signature )
520 MIL << "Deetermining key id if signature " << signature << std::endl;
521 // HACK create a tmp keyring with no keys
522 TmpDir dir(_base_dir);
523 TmpFile fakeData(_base_dir);
528 "--no-default-keyring",
536 dir.path().asString().c_str(),
538 signature.asString().c_str(),
539 fakeData.path().asString().c_str(),
543 ExternalProgram prog(argv,ExternalProgram::Discard_Stderr, false, -1, true);
548 str::regex rxNoKey("^\\[GNUPG:\\] NO_PUBKEY (.+)\n$");
550 for(line = prog.receiveLine(), count=0; !line.empty(); line = prog.receiveLine(), count++ )
552 //MIL << "[" << line << "]" << std::endl;
554 if(str::regex_match(line, what, rxNoKey, str::match_extra))
556 if ( what.size() > 1 )
558 //dumpRegexpResults(what);
561 MIL << "Determined key id [" << id << "] for signature " << signature << std::endl;
566 bool KeyRing::Impl::verifyFile( const Pathname &file, const Pathname &signature, const Pathname &keyring)
571 "--no-default-keyring",
579 keyring.asString().c_str(),
581 signature.asString().c_str(),
582 file.asString().c_str(),
586 // no need to parse output for now
587 // [GNUPG:] SIG_ID yCc4u223XRJnLnVAIllvYbUd8mQ 2006-03-29 1143618744
588 // [GNUPG:] GOODSIG A84EDAE89C800ACA SuSE Package Signing Key <build@suse.de>
589 // gpg: Good signature from "SuSE Package Signing Key <build@suse.de>"
590 // [GNUPG:] VALIDSIG 79C179B2E1C820C1890F9994A84EDAE89C800ACA 2006-03-29 1143618744 0 3 0 17 2 00 79C179B2E1C820C1890F9994A84EDAE89C800ACA
591 // [GNUPG:] TRUST_UNDEFINED
593 // [GNUPG:] ERRSIG A84EDAE89C800ACA 17 2 00 1143618744 9
594 // [GNUPG:] NO_PUBKEY A84EDAE89C800ACA
596 ExternalProgram prog(argv,ExternalProgram::Discard_Stderr, false, -1, true);
598 return (prog.close() == 0) ? true : false;
601 ///////////////////////////////////////////////////////////////////
603 ///////////////////////////////////////////////////////////////////
605 // CLASS NAME : KeyRing
607 ///////////////////////////////////////////////////////////////////
609 ///////////////////////////////////////////////////////////////////
611 // METHOD NAME : KeyRing::KeyRing
612 // METHOD TYPE : Ctor
614 KeyRing::KeyRing(const Pathname &baseTmpDir)
615 : _pimpl( new Impl(baseTmpDir) )
618 ///////////////////////////////////////////////////////////////////
620 // METHOD NAME : KeyRing::KeyRing
621 // METHOD TYPE : Ctor
623 //KeyRing::KeyRing( const Pathname &general_kr, const Pathname &trusted_kr )
624 //: _pimpl( new Impl(general_kr, trusted_kr) )
627 ///////////////////////////////////////////////////////////////////
629 // METHOD NAME : KeyRing::~KeyRing
630 // METHOD TYPE : Dtor
635 ///////////////////////////////////////////////////////////////////
637 // Forward to implementation:
639 ///////////////////////////////////////////////////////////////////
641 void KeyRing::importKey( const Pathname &keyfile, bool trusted)
643 _pimpl->importKey(keyfile, trusted);
646 PublicKey KeyRing::readPublicKey( const Pathname &keyfile )
648 return _pimpl->readPublicKey(keyfile);
651 std::string KeyRing::readSignatureKeyId( const Pathname &signature )
653 return _pimpl->readSignatureKeyId(signature);
656 void KeyRing::deleteKey( const std::string &id, bool trusted )
658 _pimpl->deleteKey(id, trusted);
661 std::list<PublicKey> KeyRing::publicKeys()
663 return _pimpl->publicKeys();
666 std::list<PublicKey> KeyRing::trustedPublicKeys()
668 return _pimpl->trustedPublicKeys();
671 bool KeyRing::verifyFileSignatureWorkflow( const Pathname &file, const std::string filedesc, const Pathname &signature)
673 return _pimpl->verifyFileSignatureWorkflow(file, filedesc, signature);
676 bool KeyRing::verifyFileSignature( const Pathname &file, const Pathname &signature)
678 return _pimpl->verifyFileSignature(file, signature);
681 bool KeyRing::verifyFileTrustedSignature( const Pathname &file, const Pathname &signature)
683 return _pimpl->verifyFileTrustedSignature(file, signature);
686 void KeyRing::dumpPublicKey( const std::string &id, bool trusted, std::ostream &stream )
688 _pimpl->dumpPublicKey( id, trusted, stream);
691 /////////////////////////////////////////////////////////////////
693 ///////////////////////////////////////////////////////////////////