16 #include <boost/thread/once.hpp> 17 #include <boost/interprocess/smart_ptr/scoped_ptr.hpp> 22 #undef ZYPP_BASE_LOGGER_LOGGROUP 23 #define ZYPP_BASE_LOGGER_LOGGROUP "zypp::gpg" 33 typedef boost::interprocess::scoped_ptr<gpgme_data, boost::function<void (gpgme_data_t)>>
GpgmeDataPtr;
34 typedef boost::interprocess::scoped_ptr<_gpgme_key, boost::function<void (gpgme_key_t)>>
GpgmeKeyPtr;
35 typedef boost::interprocess::scoped_ptr<FILE, boost::function<int (FILE *)>>
FILEPtr;
39 GpgmeErr( gpgme_error_t err_r = GPG_ERR_NO_ERROR )
42 operator gpgme_error_t()
const {
return _err; }
47 {
return str <<
"<" << gpgme_strsource(obj) <<
"> " << gpgme_strerror(obj); }
51 gpgme_check_version(NULL);
90 if (!
PathInfo( signature ).isExist())
91 return std::list<std::string>();
95 return std::list<std::string>();
98 GpgmeErr err = gpgme_data_new_from_stream (&fileData.get(), dataFile.get());
101 return std::list<std::string>();
104 FILEPtr sigFile(fopen(signature.
c_str(),
"rb"), fclose);
106 ERR <<
"Unable to open signature file '" << signature <<
"'" <<endl;
107 return std::list<std::string>();
111 err = gpgme_data_new_from_stream (&sigData.get(), sigFile.get());
114 return std::list<std::string>();
117 err = gpgme_op_verify(
_ctx, sigData.get(), fileData.get(), NULL);
118 if (err != GPG_ERR_NO_ERROR) {
120 return std::list<std::string>();
123 gpgme_verify_result_t res = gpgme_op_verify_result(
_ctx);
124 if (!res || !res->signatures) {
125 ERR <<
"Unable to read signature fingerprints" <<endl;
126 return std::list<std::string>();
129 bool foundBadSignature =
false;
130 std::list<std::string> signatures;
131 gpgme_signature_t sig = res->signatures;
134 if (!foundBadSignature)
135 foundBadSignature = (sig->status != GPG_ERR_NO_ERROR);
144 verifed = (!foundBadSignature);
160 if (err != GPG_ERR_NO_ERROR) {
162 return shared_ptr<KeyManagerCtx>();
166 err = gpgme_set_protocol(ctx, GPGME_PROTOCOL_OpenPGP);
167 if (err != GPG_ERR_NO_ERROR) {
170 return shared_ptr<KeyManagerCtx>();
174 me->_pimpl->_ctx = ctx;
182 gpgme_engine_info_t enginfo = gpgme_ctx_get_engine_info(
_pimpl->
_ctx);
186 GpgmeErr err = gpgme_ctx_set_engine_info(
188 GPGME_PROTOCOL_OpenPGP,
192 if (err != GPG_ERR_NO_ERROR) {
193 ERR <<
"Unable to set homedir " << err << endl;
202 gpgme_engine_info_t enginfo = gpgme_ctx_get_engine_info(
_pimpl->
_ctx);
211 std::list<PublicKeyData> keys;
215 gpgme_keylist_mode_t mode = GPGME_KEYLIST_MODE_LOCAL | GPGME_KEYLIST_MODE_SIGS;
217 gpgme_op_keylist_start (
_pimpl->
_ctx, NULL, 0);
219 while (!(err = gpgme_op_keylist_next(
_pimpl->
_ctx, &key))) {
222 keys.push_back(data);
224 gpgme_key_release(key);
239 return std::list<PublicKeyData>();
243 return std::list<PublicKeyData>();
246 std::list<PublicKeyData> keys =
listKeys();
256 bool verified =
false;
269 gpgme_op_keylist_start(
_pimpl->
_ctx, NULL, 0);
270 while (!(err = gpgme_op_keylist_next(
_pimpl->
_ctx, &key))) {
271 if (key->subkeys &&
id ==
str::asString(key->subkeys->keyid)) {
272 GpgmeKeyPtr(key, gpgme_key_release).swap(foundKey);
275 gpgme_key_release(key);
280 WAR <<
"Key " <<
id <<
"not found" << endl;
285 gpgme_key_t keyarray[2];
286 keyarray[0] = foundKey.get();
290 err = gpgme_data_new (&out.get());
298 err = gpgme_op_export_keys (
_pimpl->
_ctx, keyarray, 0, out.get());
300 int ret = gpgme_data_seek (out.get(), 0, SEEK_SET);
302 ERR <<
"Unable to seek in exported key data" << endl;
306 const int bufsize = 512;
307 char buf[bufsize + 1];
308 while ((ret = gpgme_data_read(out.get(), buf, bufsize)) > 0) {
309 stream.write(buf, ret);
314 ERR <<
"Unable to read exported key data" << endl;
318 ERR <<
"Error exporting key: "<< err << endl;
328 if ( !
PathInfo( keyfile ).isExist() ) {
329 ERR <<
"Keyfile '" << keyfile <<
"' does not exist.";
336 err = gpgme_data_new_from_file(&data.get(), keyfile.
c_str(), 1);
338 ERR <<
"Error importing key: "<< err << endl;
342 err = gpgme_op_import(
_pimpl->
_ctx, data.get());
344 ERR <<
"Error importing key: "<< err << endl;
346 return (err == GPG_ERR_NO_ERROR);
354 gpgme_op_keylist_start(
_pimpl->
_ctx, NULL, 0);
356 while (!(err = gpgme_op_keylist_next(
_pimpl->
_ctx, &key))) {
357 if (key->subkeys &&
id ==
str::asString(key->subkeys->keyid)) {
360 gpgme_key_release(key);
364 ERR <<
"Error deleting key: "<< err << endl;
369 gpgme_key_release(key);
373 WAR <<
"Key: '"<<
id <<
"' not found." << endl;
383 bool verified =
false;
static Ptr createForOpenPGP()
Creates a new KeyManagerCtx for PGP.
std::list< PublicKeyData > readKeyFromFile(const Pathname &file)
Returns a list of all.
GpgmeErr(gpgme_error_t err_r=GPG_ERR_NO_ERROR)
std::list< std::string > verifyAndReadSignaturesFprs(const Pathname &file, const Pathname &signature, bool &verifed)
Class representing one GPG Public Keys data.
const char * c_str() const
String representation.
String related utilities and Regular expression matching.
const std::string & asString(const std::string &t)
Global asString() that works with std::string too.
bool verify(const Pathname &file, const Pathname &signature)
Tries to verify file using signature, returns true on success.
Provide a new empty temporary file and delete it when no longer needed.
boost::interprocess::scoped_ptr< FILE, boost::function< int(FILE *)> > FILEPtr
bool exportKey(const std::string &id, std::ostream &stream)
Exports the key with id into the given stream, returns true on success.
RW_pointer< Impl > _pimpl
Pointer to implementation.
boost::interprocess::scoped_ptr< gpgme_data, boost::function< void(gpgme_data_t)> > GpgmeDataPtr
bool importKey(const Pathname &keyfile)
Tries to import a key from keyfile, returns true on success.
Provide a new empty temporary directory and recursively delete it when no longer needed.
std::ostream & operator<<(std::ostream &str, const Arch::CompatEntry &obj)
bool deleteKey(const std::string &id)
Tries to delete a key specified by id, returns true on success.
std::list< PublicKeyData > listKeys()
Returns a list of all public keys found in the current keyring.
bool setHomedir(const Pathname &keyring_r)
Changes the keyring directory.
boost::interprocess::scoped_ptr< _gpgme_key, boost::function< void(gpgme_key_t)> > GpgmeKeyPtr
shared_ptr< KeyManagerCtx > Ptr
Wrapper class for ::stat/::lstat.
std::list< std::string > readSignatureFingerprints(const Pathname &signature)
Reads all fingerprints from the signature file , returns a list of all found fingerprints.
static PublicKeyData fromGpgmeKey(_gpgme_key *data)
Easy-to use interface to the ZYPP dependency resolver.
boost::once_flag gpgme_init_once