11#include <zypp/PathInfo.h>
12#include <zypp/base/Logger.h>
13#include <zypp/TmpPath.h>
14#include <zypp/base/String.h>
15#include <zypp/AutoDispose.h>
17#include <boost/thread/once.hpp>
18#include <boost/interprocess/smart_ptr/scoped_ptr.hpp>
24#undef ZYPP_BASE_LOGGER_LOGGROUP
25#define ZYPP_BASE_LOGGER_LOGGROUP "zypp::gpg"
37 boost::once_flag gpgme_init_once = BOOST_ONCE_INIT;
41 const char *version = gpgme_check_version(NULL);
44 MIL <<
"Initialized libgpgme version: " << version << endl;
48 MIL <<
"Initialized libgpgme with unknown version" << endl;
53 typedef boost::interprocess::scoped_ptr<gpgme_data, boost::function<void (gpgme_data_t)>> GpgmeDataPtr;
54 typedef boost::interprocess::scoped_ptr<_gpgme_key, boost::function<void (gpgme_key_t)>> GpgmeKeyPtr;
55 typedef boost::interprocess::scoped_ptr<FILE, boost::function<int (FILE *)>> FILEPtr;
59 GpgmeErr( gpgme_error_t err_r = GPG_ERR_NO_ERROR )
62 operator gpgme_error_t()
const {
return _err; }
67 std::ostream &
operator<<( std::ostream &
str,
const GpgmeErr & obj )
68 {
return str <<
"<" << gpgme_strsource(obj) <<
"> " << gpgme_strerror(obj); }
71 std::ostream &
operator<<( std::ostream &
str,
const _gpgme_op_import_result & obj )
73 str <<
"gpgme_op_import_result {" << endl;
74 str <<
" " << obj.considered <<
" The total number of considered keys." << endl;
75 str <<
" " << obj.no_user_id <<
" The number of keys without user ID." << endl;
76 str <<
" " << obj.imported <<
" The total number of imported keys." << endl;
77 str <<
" " << obj.imported_rsa <<
" imported RSA keys." << endl;
78 str <<
" " << obj.unchanged <<
" unchanged keys." << endl;
79 str <<
" " << obj.new_user_ids <<
" new user IDs." << endl;
80 str <<
" " << obj.new_sub_keys <<
" new sub keys." << endl;
81 str <<
" " << obj.new_signatures <<
" new signatures." << endl;
82 str <<
" " << obj.new_revocations <<
" new revocations." << endl;
83 str <<
" " << obj.secret_read <<
" secret keys read." << endl;
84 str <<
" " << obj.secret_imported <<
" imported secret keys." << endl;
85 str <<
" " << obj.secret_unchanged <<
" unchanged secret keys." << endl;
86 str <<
" " << obj.not_imported <<
" keys not imported." << endl;
87 for ( gpgme_import_status_t p = obj.imports; p; p = p->next )
89 str <<
" - " << p->fpr <<
": " << p->result << endl;
108 { boost::call_once( gpgme_init_once, initGpgme ); }
111 {
if (
_ctx ) gpgme_release(
_ctx ); }
130 template<
typename Callback >
131 bool importKey(GpgmeDataPtr &data, Callback &&calcDataSize );
153 if (!
PathInfo( signature_r ).isExist())
154 return std::list<std::string>();
156 FILEPtr sigFile(fopen(signature_r.
c_str(),
"rb"), fclose);
158 ERR <<
"Unable to open signature file '" << signature_r <<
"'" <<endl;
159 return std::list<std::string>();
162 GpgmeDataPtr sigData(
nullptr, gpgme_data_release);
163 GpgmeErr err = gpgme_data_new_from_stream (&sigData.get(), sigFile.get());
166 return std::list<std::string>();
178 GpgmeDataPtr sigData(
nullptr, gpgme_data_release);
179 GpgmeErr err = gpgme_data_new_from_mem(&sigData.get(), keyData_r.data(), keyData_r.size(), 1 );
182 return std::list<std::string>();
185 return readSignaturesFprsOptVerify( sigData, file_r, verify_r );
194 FILEPtr dataFile(fopen(file_r.
c_str(),
"rb"), fclose);
196 return std::list<std::string>();
198 GpgmeDataPtr fileData(
nullptr, gpgme_data_release);
199 GpgmeErr err = gpgme_data_new_from_stream (&fileData.get(), dataFile.get());
202 return std::list<std::string>();
205 err = gpgme_op_verify(_ctx, sigData.get(), fileData.get(), NULL);
206 if (err != GPG_ERR_NO_ERROR) {
208 return std::list<std::string>();
211 gpgme_verify_result_t res = gpgme_op_verify_result(_ctx);
212 if (!res || !res->signatures) {
213 ERR <<
"Unable to read signature fingerprints" <<endl;
214 return std::list<std::string>();
217 bool foundBadSignature =
false;
218 bool foundGoodSignature =
false;
219 std::list<std::string> signatures;
220 for ( gpgme_signature_t sig = res->signatures; sig; sig = sig->next ) {
228 std::string id( sig->fpr );
229 if (
id.size() > 16 )
230 id =
id.substr(
id.size()-16 );
232 DBG <<
"Found signature with ID: " <<
id <<
" in " << file_r << std::endl;
233 signatures.push_back( std::move(
id) );
236 if ( verify_r && sig->status != GPG_ERR_NO_ERROR ) {
237 const auto status = gpgme_err_code(sig->status);
243 if ( status != GPG_ERR_KEY_EXPIRED && status != GPG_ERR_NO_PUBKEY )
245 WAR <<
"Failed signature check: " << file_r <<
" " << GpgmeErr(sig->status) << endl;
246 if ( !foundBadSignature )
247 foundBadSignature =
true;
251 WAR <<
"Legacy: Ignore expired or unknown key: " << file_r <<
" " << GpgmeErr(sig->status) << endl;
253 if ( status == GPG_ERR_KEY_EXPIRED )
254 foundGoodSignature =
true;
257 foundGoodSignature =
true;
262 *verify_r = (!foundBadSignature) && foundGoodSignature;
276 ret.
_pimpl->_volatile =
true;
282 DBG <<
"createForOpenPGP(" << keyring_r <<
")" << endl;
285 gpgme_ctx_t & ctx { ret.
_pimpl->_ctx };
288 GpgmeErr err = gpgme_new( &ctx );
289 if ( err != GPG_ERR_NO_ERROR )
293 err = gpgme_set_protocol( ctx, GPGME_PROTOCOL_OpenPGP );
294 if ( err != GPG_ERR_NO_ERROR )
297 if ( !keyring_r.
empty() ) {
299 gpgme_engine_info_t enginfo = gpgme_ctx_get_engine_info( ctx );
303 err = gpgme_ctx_set_engine_info( ctx, GPGME_PROTOCOL_OpenPGP, enginfo->file_name, keyring_r.
c_str() );
304 if ( err != GPG_ERR_NO_ERROR )
314 if ( gpgme_engine_info_t enginfo = gpgme_ctx_get_engine_info(
_pimpl->_ctx ) )
315 ret = enginfo->home_dir;
321 std::list<PublicKeyData> ret;
322 GpgmeErr err = GPG_ERR_NO_ERROR;
327 if ( (err = gpgme_set_keylist_mode(
_pimpl->_ctx, GPGME_KEYLIST_MODE_LOCAL | GPGME_KEYLIST_MODE_SIGS )) != GPG_ERR_NO_ERROR ) {
328 ERR <<
"gpgme_set_keylist_mode: " << err << endl;
332 if ( (err = gpgme_op_keylist_start(
_pimpl->_ctx, NULL, 0 )) != GPG_ERR_NO_ERROR ) {
333 ERR <<
"gpgme_op_keylist_start: " << err << endl;
340 for ( ; gpgme_op_keylist_next(
_pimpl->_ctx, &(*key) ) == GPG_ERR_NO_ERROR; key.getDispose()( key ) ) {
343 ret.push_back( data );
357 std::list<PublicKeyData> ret;
359 if (
_pimpl->_volatile ) {
374 return _pimpl->verifySignaturesFprs(file, signature);
379 GpgmeErr err = GPG_ERR_NO_ERROR;
381 GpgmeKeyPtr foundKey;
385 gpgme_op_keylist_start(
_pimpl->_ctx, NULL, 0);
386 while (!(err = gpgme_op_keylist_next(
_pimpl->_ctx, &key))) {
387 if (key->subkeys &&
id ==
str::asString(key->subkeys->keyid)) {
388 GpgmeKeyPtr(key, gpgme_key_release).swap(foundKey);
391 gpgme_key_release(key);
393 gpgme_op_keylist_end(
_pimpl->_ctx);
396 WAR <<
"Key " <<
id <<
"not found" << endl;
401 gpgme_key_t keyarray[2];
402 keyarray[0] = foundKey.get();
405 GpgmeDataPtr out(
nullptr, gpgme_data_release);
406 err = gpgme_data_new (&out.get());
413 gpgme_set_armor (
_pimpl->_ctx, 1);
418 err = gpgme_op_export_keys (
_pimpl->_ctx, keyarray, GPGME_EXPORT_MODE_MINIMAL, out.get());
420 int ret = gpgme_data_seek (out.get(), 0, SEEK_SET);
422 ERR <<
"Unable to seek in exported key data" << endl;
426 const int bufsize = 512;
427 char buf[bufsize + 1];
428 while ((ret = gpgme_data_read(out.get(), buf, bufsize)) > 0) {
429 stream.write(buf, ret);
434 ERR <<
"Unable to read exported key data" << endl;
438 ERR <<
"Error exporting key: "<< err << endl;
448 if ( !
PathInfo( keyfile ).isExist() ) {
449 ERR <<
"Keyfile '" << keyfile <<
"' does not exist.";
453 GpgmeDataPtr data(
nullptr, gpgme_data_release);
456 err = gpgme_data_new_from_file(&data.get(), keyfile.
c_str(), 1);
458 ERR <<
"Error importing key: "<< err << endl;
467 GpgmeDataPtr data(
nullptr, gpgme_data_release);
470 err = gpgme_data_new_from_mem( &data.get(), keydata.data(), keydata.size(), 1);
472 ERR <<
"Error importing key: "<< err << endl;
476 return _pimpl->importKey( data, [&](){
return keydata.size(); } );
479template<
typename Callback>
483 err = gpgme_op_import(
_ctx, data.get() );
485 ERR <<
"Error importing key: "<< err << endl;
491 if ( gpgme_import_result_t res = gpgme_op_import_result(
_ctx) )
493 if ( ! res->considered && std::forward<Callback>(calcDataSize)() )
496 ERR <<
"Error importing key: No keys considered (bsc#1127220, [libgpgme] signal received?)" << endl;
501 return (err == GPG_ERR_NO_ERROR);
507 GpgmeErr err = GPG_ERR_NO_ERROR;
509 gpgme_op_keylist_start(
_pimpl->_ctx, NULL, 0);
511 while (!(err = gpgme_op_keylist_next(
_pimpl->_ctx, &key))) {
512 if (key->subkeys &&
id ==
str::asString(key->subkeys->keyid)) {
513 err = gpgme_op_delete(
_pimpl->_ctx, key, 0);
515 gpgme_key_release(key);
516 gpgme_op_keylist_end(
_pimpl->_ctx);
519 ERR <<
"Error deleting key: "<< err << endl;
524 gpgme_key_release(key);
527 gpgme_op_keylist_end(
_pimpl->_ctx);
528 WAR <<
"Key: '"<<
id <<
"' not found." << endl;
533{
return _pimpl->readSignaturesFprs(signature); }
536{
return _pimpl->readSignaturesFprs(keyData); }
Reference counted access to a Tp object calling a custom Dispose function when the last AutoDispose h...
std::list< std::string > readSignaturesFprs(const Pathname &signature_r)
Return all fingerprints found in signature_r.
bool _volatile
readKeyFromFile workaround bsc#1140670
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.
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 assert_dir(const Pathname &path, unsigned mode)
Like 'mkdir -p'.
int clean_dir(const Pathname &path)
Like 'rm -r DIR/ *'.
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)
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.