19#include <boost/thread/once.hpp>
20#include <boost/interprocess/smart_ptr/scoped_ptr.hpp>
26#undef ZYPP_BASE_LOGGER_LOGGROUP
27#define ZYPP_BASE_LOGGER_LOGGROUP "zypp::gpg"
39 boost::once_flag gpgme_init_once = BOOST_ONCE_INIT;
43 const char *version = gpgme_check_version(NULL);
46 MIL <<
"Initialized libgpgme version: " << version << endl;
50 MIL <<
"Initialized libgpgme with unknown version" << endl;
55 using GpgmeDataPtr = boost::interprocess::scoped_ptr<gpgme_data, boost::function<void (gpgme_data_t)>>;
56 using GpgmeKeyPtr = boost::interprocess::scoped_ptr<_gpgme_key, boost::function<void (gpgme_key_t)>>;
57 using FILEPtr = boost::interprocess::scoped_ptr<FILE, boost::function<int (FILE *)>>;
61 GpgmeErr( gpgme_error_t err_r = GPG_ERR_NO_ERROR )
64 operator gpgme_error_t()
const {
return _err; }
69 std::ostream &
operator<<( std::ostream &
str,
const GpgmeErr & obj )
70 {
return str <<
"<" << gpgme_strsource(obj) <<
"> " << gpgme_strerror(obj); }
73 [[maybe_unused]] std::ostream &
operator<<( std::ostream &
str,
const _gpgme_op_import_result & obj )
75 str <<
"gpgme_op_import_result {" << endl;
76 str <<
" " << obj.considered <<
" The total number of considered keys." << endl;
77 str <<
" " << obj.no_user_id <<
" The number of keys without user ID." << endl;
78 str <<
" " << obj.imported <<
" The total number of imported keys." << endl;
79 str <<
" " << obj.imported_rsa <<
" imported RSA keys." << endl;
80 str <<
" " << obj.unchanged <<
" unchanged keys." << endl;
81 str <<
" " << obj.new_user_ids <<
" new user IDs." << endl;
82 str <<
" " << obj.new_sub_keys <<
" new sub keys." << endl;
83 str <<
" " << obj.new_signatures <<
" new signatures." << endl;
84 str <<
" " << obj.new_revocations <<
" new revocations." << endl;
85 str <<
" " << obj.secret_read <<
" secret keys read." << endl;
86 str <<
" " << obj.secret_imported <<
" imported secret keys." << endl;
87 str <<
" " << obj.secret_unchanged <<
" unchanged secret keys." << endl;
88 str <<
" " << obj.not_imported <<
" keys not imported." << endl;
89 for ( gpgme_import_status_t p = obj.imports; p; p = p->next )
91 str <<
" - " << p->fpr <<
": " << p->result << endl;
97 [[maybe_unused]] std::ostream &
operator<<( std::ostream &
str,
const gpgme_sigsum_t & obj )
99 str << ((int)obj&(int)0xffff) <<
":";
100#define OSC(V) if ( V & (unsigned)obj ) str << " " << #V;
101 OSC(GPGME_SIGSUM_VALID );
102 OSC(GPGME_SIGSUM_GREEN );
103 OSC(GPGME_SIGSUM_RED );
104 OSC(GPGME_SIGSUM_KEY_REVOKED );
105 OSC(GPGME_SIGSUM_KEY_EXPIRED );
106 OSC(GPGME_SIGSUM_SIG_EXPIRED );
107 OSC(GPGME_SIGSUM_KEY_MISSING );
108 OSC(GPGME_SIGSUM_CRL_MISSING );
109 OSC(GPGME_SIGSUM_CRL_TOO_OLD );
110 OSC(GPGME_SIGSUM_BAD_POLICY );
111 OSC(GPGME_SIGSUM_SYS_ERROR );
112 OSC(GPGME_SIGSUM_TOFU_CONFLICT);
117 [[maybe_unused]] std::ostream &
operator<<( std::ostream &
str,
const gpgme_signature_t & obj )
119 str <<
"gpgme_signature_t " << (
void *)obj <<
" {" << endl;
120 str <<
" next: " << (
void *)obj->next << endl;
121 str <<
" summary: " << obj->summary << endl;
122 str <<
" fpr: " << obj->fpr << endl;
123 str <<
" status: " << obj->status <<
" " << GpgmeErr(obj->status) << endl;
124 str <<
" timestamp: " << obj->timestamp << endl;
125 str <<
" exp_timestamp: " << obj->exp_timestamp << endl;
126 str <<
" wrong_key_usage: " << obj->wrong_key_usage << endl;
127 str <<
" pka_trust: " << obj->pka_trust << endl;
128 str <<
" chain_model: " << obj->chain_model << endl;
129 str <<
" is_de_vs: " << obj->is_de_vs << endl;
130 str <<
" validity: " << obj->validity << endl;
131 str <<
" validity_reason: " << obj->validity_reason <<
" " << GpgmeErr(obj->validity_reason) << endl;
132 str <<
" pubkey_algo: " << obj->pubkey_algo << endl;
133 str <<
" hash_algo: " << obj->hash_algo << endl;
134 str <<
" pka_address: " << (obj->pka_address ? obj->pka_address :
"") << endl;
152 { boost::call_once( gpgme_init_once, initGpgme ); }
180 template<
typename Callback >
181 bool importKey(GpgmeDataPtr &data, Callback &&calcDataSize );
203 if (!
PathInfo( signature_r ).isExist())
204 return std::list<std::string>();
206 FILEPtr sigFile(fopen(signature_r.
c_str(),
"rb"), fclose);
208 ERR <<
"Unable to open signature file '" << signature_r <<
"'" <<endl;
209 return std::list<std::string>();
212 GpgmeDataPtr sigData(
nullptr, gpgme_data_release);
213 GpgmeErr err = gpgme_data_new_from_stream (&sigData.get(), sigFile.get());
216 return std::list<std::string>();
228 GpgmeDataPtr sigData(
nullptr, gpgme_data_release);
229 GpgmeErr err = gpgme_data_new_from_mem(&sigData.get(), keyData_r.data(), keyData_r.size(), 1 );
232 return std::list<std::string>();
244 FILEPtr dataFile(fopen(file_r.
c_str(),
"rb"), fclose);
246 return std::list<std::string>();
248 GpgmeDataPtr fileData(
nullptr, gpgme_data_release);
249 GpgmeErr err = gpgme_data_new_from_stream (&fileData.get(), dataFile.get());
252 return std::list<std::string>();
255 err = gpgme_op_verify(
_ctx, sigData.get(), fileData.get(), NULL);
256 if (err != GPG_ERR_NO_ERROR) {
258 return std::list<std::string>();
261 gpgme_verify_result_t res = gpgme_op_verify_result(
_ctx);
262 if (!res || !res->signatures) {
263 ERR <<
"Unable to read signature fingerprints" <<endl;
264 return std::list<std::string>();
267 bool foundBadSignature =
false;
268 bool foundGoodSignature =
false;
269 std::list<std::string> signatures;
270 for ( gpgme_signature_t sig = res->signatures; sig; sig = sig->next ) {
278 std::string id( sig->fpr );
279 if (
id.size() > 16 )
280 id =
id.substr(
id.size()-16 );
282 DBG <<
"Found signature with ID: " <<
id <<
" in " << file_r << std::endl;
283 signatures.push_back( std::move(
id) );
286 if ( verify_r && sig->status != GPG_ERR_NO_ERROR ) {
287 const auto status = gpgme_err_code(sig->status);
293 if ( status != GPG_ERR_KEY_EXPIRED && status != GPG_ERR_NO_PUBKEY )
295 WAR <<
"Failed signature check: " << file_r <<
" " << GpgmeErr(sig->status) << endl;
296 if ( !foundBadSignature )
297 foundBadSignature =
true;
301 WAR <<
"Legacy: Ignore expired or unknown key: " << file_r <<
" " << GpgmeErr(sig->status) << endl;
303 if ( status == GPG_ERR_KEY_EXPIRED )
304 foundGoodSignature =
true;
307 foundGoodSignature =
true;
312 *verify_r = (!foundBadSignature) && foundGoodSignature;
332 DBG <<
"createForOpenPGP(" << keyring_r <<
")" << endl;
338 GpgmeErr err = gpgme_new( &ctx );
339 if ( err != GPG_ERR_NO_ERROR )
343 err = gpgme_set_protocol( ctx, GPGME_PROTOCOL_OpenPGP );
344 if ( err != GPG_ERR_NO_ERROR )
347 if ( !keyring_r.
empty() ) {
349 gpgme_engine_info_t enginfo = gpgme_ctx_get_engine_info( ctx );
353 err = gpgme_ctx_set_engine_info( ctx, GPGME_PROTOCOL_OpenPGP, enginfo->file_name, keyring_r.
c_str() );
354 if ( err != GPG_ERR_NO_ERROR )
358 DBG <<
"createForOpenPGP {" << endl;
359 for (
const auto & key : ret.
listKeys() ) {
360 DBG <<
" " << key << endl;
370 if ( gpgme_engine_info_t enginfo = gpgme_ctx_get_engine_info(
_pimpl->_ctx ) )
371 ret = enginfo->home_dir;
377 std::list<PublicKeyData> ret;
378 GpgmeErr err = GPG_ERR_NO_ERROR;
383 if ( (err = gpgme_set_keylist_mode(
_pimpl->_ctx, GPGME_KEYLIST_MODE_LOCAL | GPGME_KEYLIST_MODE_SIGS )) != GPG_ERR_NO_ERROR ) {
384 ERR <<
"gpgme_set_keylist_mode: " << err << endl;
388 if ( (err = gpgme_op_keylist_start(
_pimpl->_ctx, NULL, 0 )) != GPG_ERR_NO_ERROR ) {
389 ERR <<
"gpgme_op_keylist_start: " << err << endl;
396 for ( ; gpgme_op_keylist_next(
_pimpl->_ctx, &(*key) ) == GPG_ERR_NO_ERROR; key.
getDispose()( key ) ) {
399 ret.push_back( data );
413 std::list<PublicKeyData> ret;
415 if (
_pimpl->_volatile ) {
430 return _pimpl->verifySignaturesFprs(file, signature);
435 GpgmeErr err = GPG_ERR_NO_ERROR;
437 GpgmeKeyPtr foundKey;
440 gpgme_key_t key =
nullptr;
441 gpgme_op_keylist_start(
_pimpl->_ctx, NULL, 0);
442 while (!(err = gpgme_op_keylist_next(
_pimpl->_ctx, &key))) {
443 if (key->subkeys &&
id ==
str::asString(key->subkeys->keyid)) {
444 GpgmeKeyPtr(key, gpgme_key_release).swap(foundKey);
447 gpgme_key_release(key);
449 gpgme_op_keylist_end(
_pimpl->_ctx);
452 WAR <<
"Key " <<
id <<
"not found" << endl;
457 gpgme_key_t keyarray[2];
458 keyarray[0] = foundKey.get();
461 GpgmeDataPtr out(
nullptr, gpgme_data_release);
462 err = gpgme_data_new (&out.get());
469 gpgme_set_armor (
_pimpl->_ctx, 1);
474 err = gpgme_op_export_keys (
_pimpl->_ctx, keyarray, GPGME_EXPORT_MODE_MINIMAL, out.get());
476 int ret = gpgme_data_seek (out.get(), 0, SEEK_SET);
478 ERR <<
"Unable to seek in exported key data" << endl;
482 const int bufsize = 512;
483 char buf[bufsize + 1];
484 while ((ret = gpgme_data_read(out.get(), buf, bufsize)) > 0) {
485 stream.write(buf, ret);
490 ERR <<
"Unable to read exported key data" << endl;
494 ERR <<
"Error exporting key: "<< err << endl;
504 if ( !
PathInfo( keyfile ).isExist() ) {
505 ERR <<
"Keyfile '" << keyfile <<
"' does not exist.";
509 GpgmeDataPtr data(
nullptr, gpgme_data_release);
512 err = gpgme_data_new_from_file(&data.get(), keyfile.
c_str(), 1);
514 ERR <<
"Error importing key: "<< err << endl;
523 GpgmeDataPtr data(
nullptr, gpgme_data_release);
526 err = gpgme_data_new_from_mem( &data.get(), keydata.data(), keydata.size(), 1);
528 ERR <<
"Error importing key: "<< err << endl;
532 return _pimpl->importKey( data, [&](){
return keydata.size(); } );
535template<
typename Callback>
539 err = gpgme_op_import(
_ctx, data.get() );
541 ERR <<
"Error importing key: "<< err << endl;
547 if ( gpgme_import_result_t res = gpgme_op_import_result(
_ctx) )
549 if ( ! res->considered && std::forward<Callback>(calcDataSize)() )
552 ERR <<
"Error importing key: No keys considered (bsc#1127220, [libgpgme] signal received?)" << endl;
557 return (err == GPG_ERR_NO_ERROR);
562 gpgme_key_t key =
nullptr;
563 GpgmeErr err = GPG_ERR_NO_ERROR;
565 gpgme_op_keylist_start(
_pimpl->_ctx, NULL, 0);
567 while (!(err = gpgme_op_keylist_next(
_pimpl->_ctx, &key))) {
568 if (key->subkeys &&
id ==
str::asString(key->subkeys->keyid)) {
569 err = gpgme_op_delete(
_pimpl->_ctx, key, 0);
571 gpgme_key_release(key);
572 gpgme_op_keylist_end(
_pimpl->_ctx);
575 ERR <<
"Error deleting key: "<< err << endl;
580 gpgme_key_release(key);
583 gpgme_op_keylist_end(
_pimpl->_ctx);
584 WAR <<
"Key: '"<<
id <<
"' not found." << endl;
589{
return _pimpl->readSignaturesFprs(signature); }
592{
return _pimpl->readSignaturesFprs(keyData); }
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Reference counted access to a Tp object calling a custom Dispose function when the last AutoDispose h...
const Dispose & getDispose() const
Return the current dispose function.
Impl & operator=(const Impl &)=delete
std::list< std::string > readSignaturesFprs(const Pathname &signature_r)
Return all fingerprints found in signature_r.
Impl(const Impl &)=delete
bool _volatile
readKeyFromFile workaround bsc#1140670
Impl & operator=(Impl &&)=delete
std::list< std::string > readSignaturesFprs(const ByteArray &signature_r)
Return all fingerprints found in signature_r.
std::list< std::string > readSignaturesFprsOptVerify(const Pathname &signature_r, const Pathname &file_r="/dev/null", bool *verify_r=nullptr)
Return all fingerprints found in signature_r and optionally verify the file_r on the fly.
bool verifySignaturesFprs(const Pathname &file_r, const Pathname &signature_r)
Tries to verify the file_r using signature_r.
bool importKey(GpgmeDataPtr &data, Callback &&calcDataSize)
bool exportKey(const std::string &id, std::ostream &stream)
Exports the key with id into the given stream, returns true on success.
std::list< PublicKeyData > listKeys()
Returns a list of all public keys found in the current keyring.
bool verify(const Pathname &file, const Pathname &signature)
Tries to verify file using signature, returns true on success.
static KeyManagerCtx createForOpenPGP()
Creates a new KeyManagerCtx for PGP using a volatile temp.
std::list< std::string > readSignatureFingerprints(const Pathname &signature)
Reads all fingerprints from the signature file , returns a list of all found fingerprints.
std::list< PublicKeyData > readKeyFromFile(const Pathname &file)
Returns a list of all PublicKeyData found in file.
RW_pointer< Impl > _pimpl
Pointer to implementation.
bool deleteKey(const std::string &id)
Tries to delete a key specified by id, returns true on success.
Pathname homedir() const
Return the homedir/keyring.
bool importKey(const Pathname &keyfile)
Tries to import a key from keyfile, returns true on success.
KeyRingException()
Ctor taking message.
Class representing one GPG Public Keys data.
static PublicKeyData fromGpgmeKey(_gpgme_key *data)
Wrapper class for stat/lstat.
const char * c_str() const
String representation.
bool empty() const
Test for an empty path.
String related utilities and Regular expression matching.
int clean_dir(const Pathname &path)
Like 'rm -r DIR/ *'.
int assert_dir(const Pathname &path, unsigned mode)
Like 'mkdir -p'.
const std::string & asString(const std::string &t)
Global asString() that works with std::string too.
Easy-to use interface to the ZYPP dependency resolver.
Pathname myTmpDir()
Global access to the zypp.TMPDIR (created on demand, deleted when libzypp is unloaded).
std::ostream & operator<<(std::ostream &str, const SerialNumber &obj)
GpgmeException(const std::string &in_r, const GpgmeErr &err_r)