29 template <
class Executor,
class OpType>
48 using zyppng::operators::operator|;
64 if (
_repo.gpgKeyUrlsEmpty() ) {
71 if ( !res )
return false;
81 zypp::PublicKeyData keyData (
_context->keyRing()->publicKeyData(
_keyId) );
87 key = zypp::PublicKey(
_context->keyRing()->exportPublicKey( keyData ) );
93 if ( !key.isValid() ) {
94 ERR <<
"Key [" <<
_keyId <<
"] from known keyring is not valid" << std::endl;
98 MIL <<
"Key [" <<
_keyId <<
"] " << key.name() <<
" loaded from cache" << std::endl;
106 MIL <<
"User wants to import key [" <<
_keyId <<
"] " << key.name() <<
" from cache" << std::endl;
108 _context->keyRing()->importKey( key,
true );
109 }
catch (
const zypp::KeyRingException &e ) {
138 template <
class Executor,
class OpType>
139 struct VerifyFileSignatureLogic :
public LogicBase<Executor, OpType>
147 : _zyppContext(
std::move(zyppContext) )
148 , _keyringReport( _zyppContext )
149 , _keyRing(
std::move(keyRing) )
150 , _verifyContext(
std::move(ctx) )
153 struct FoundKeyData {
154 zypp::PublicKeyData _foundKey;
156 bool trusted =
false;
159 MaybeAsyncRef<FoundKeyData> findKey (
const std::string &
id ) {
161 using zyppng::operators::operator|;
164 return makeReadyResult(FoundKeyData{zypp::PublicKeyData(), zypp::Pathname()});
167 zypp::PublicKeyData trustedKeyData( _keyRing->pimpl().publicKeyExists(
id, _keyRing->pimpl().trustedKeyRing() ) );
168 if ( trustedKeyData )
170 MIL <<
"Key is trusted: " << trustedKeyData << std::endl;
174 zypp::PublicKeyData generalKeyData( _keyRing->pimpl().publicKeyExists(
id, _keyRing->pimpl().generalKeyRing() ) );
175 if ( generalKeyData )
185 if ( trustedKeyData.fingerprint() == generalKeyData.fingerprint()
186 && trustedKeyData.created() < generalKeyData.created() )
188 MIL <<
"Key was updated. Saving new version into trusted keyring: " << generalKeyData << std::endl;
189 _keyRing->importKey( _keyRing->pimpl().exportKey( generalKeyData, _keyRing->pimpl().generalKeyRing() ),
true );
190 trustedKeyData = _keyRing->pimpl().publicKeyExists(
id, _keyRing->pimpl().trustedKeyRing() );
194 return makeReadyResult( FoundKeyData{ trustedKeyData, _keyRing->pimpl().trustedKeyRing(),
true } );
198 zypp::PublicKeyData generalKeyData( _keyRing->pimpl().publicKeyExists(
id, _keyRing->pimpl().generalKeyRing() ) );
199 if ( generalKeyData )
201 zypp::PublicKey key( _keyRing->pimpl().exportKey( generalKeyData, _keyRing->pimpl().generalKeyRing() ) );
202 MIL <<
"Key [" <<
id <<
"] " << key.name() <<
" is not trusted" << std::endl;
209 zypp::Pathname whichKeyring;
211 MIL <<
"User wants to trust key [" <<
id <<
"] " << key.name() << std::endl;
215 MIL <<
"User wants to import key [" <<
id <<
"] " << key.name() << std::endl;
216 _keyRing->importKey( key,
true );
217 whichKeyring = _keyRing->pimpl().trustedKeyRing();
220 whichKeyring = _keyRing->pimpl().generalKeyRing();
222 return makeReadyResult(FoundKeyData { std::move(generalKeyData), std::move(whichKeyring),
true });
226 MIL <<
"User does not want to trust key [" <<
id <<
"] " << key.name() << std::endl;
227 return makeReadyResult(FoundKeyData { std::move(generalKeyData), _keyRing->pimpl().generalKeyRing(),
false });
230 else if ( ! _verifyContext.keyContext().empty() )
234 | [
this, id](
bool success ) {
236 return FoundKeyData{ zypp::PublicKeyData(), zypp::Pathname() };
238 return FoundKeyData{ _keyRing->pimpl().publicKeyExists(
id, _keyRing->pimpl().trustedKeyRing() ), _keyRing->pimpl().trustedKeyRing(),
true };
242 return makeReadyResult(FoundKeyData{ zypp::PublicKeyData(), zypp::Pathname() });
248 _verifyContext.resetResults();
249 const zypp::Pathname & file { _verifyContext.file() };
250 const zypp::Pathname & signature { _verifyContext.signature() };
251 const std::string & filedesc { _verifyContext.shortFile() };
253 MIL <<
"Going to verify signature for " << filedesc <<
" ( " << file <<
" ) with " << signature << std::endl;
256 if( signature.
empty() || (!zypp::PathInfo( signature ).isExist()) )
258 bool res = _keyringReport.askUserToAcceptUnsignedFile( filedesc, _verifyContext.keyContext() );
259 MIL <<
"askUserToAcceptUnsignedFile: " << res << std::endl;
265 _verifyContext.signatureId( _keyRing->readSignatureKeyId( signature ) );
266 }
catch (
const zypp::Exception &e ) {
267 MIL <<
"Failed to read the signature from " << signature << std::endl;
272 const std::string &
id = _verifyContext.signatureId();
275 std::list<zypp::PublicKeyData> buddies;
276 for (
const auto & sid : _verifyContext.buddyKeys() ) {
277 if ( not zypp::PublicKeyData::isSafeKeyId( sid ) ) {
278 WAR <<
"buddy " << sid <<
": key id is too short to safely identify a gpg key. Skipping it." << std::endl;
281 if ( _keyRing->pimpl().trustedPublicKeyExists( sid ) ) {
282 MIL <<
"buddy " << sid <<
": already in trusted key ring. Not needed." << std::endl;
285 auto pk = _keyRing->pimpl().publicKeyExists( sid );
287 WAR <<
"buddy " << sid <<
": not available in the public key ring. Skipping it." << std::endl;
290 if ( pk.providesKey(
id) ) {
291 MIL <<
"buddy " << sid <<
": is the signing key. Handled separately." << std::endl;
294 MIL <<
"buddy " << sid <<
": candidate for auto import. Remeber it." << std::endl;
295 buddies.push_back( pk );
298 using zyppng::operators::operator|;
299 return findKey(
id ) | [
this, id, buddies=std::move(buddies)]( FoundKeyData res ) {
301 const zypp::Pathname & file { _verifyContext.file() };
302 const zypp::KeyContext & keyContext { _verifyContext.keyContext() };
303 const zypp::Pathname & signature { _verifyContext.signature() };
304 const std::string & filedesc { _verifyContext.shortFile() };
306 if ( res._foundKey ) {
310 return makeReturn(
false);
313 _verifyContext.signatureIdTrusted( res._whichKeyRing == _keyRing->pimpl().trustedKeyRing() );
314 _keyringReport.infoVerify( filedesc, res._foundKey, keyContext );
315 if ( _keyRing->pimpl().verifyFile( file, signature, res._whichKeyRing ) )
317 _verifyContext.fileValidated(
true );
318 if ( _verifyContext.signatureIdTrusted() && not buddies.empty() ) {
320 MIL <<
"Validated with trusted key: importing buddy list..." << std::endl;
321 _keyringReport.reportAutoImportKey( buddies, res._foundKey, keyContext );
322 for (
const auto & kd : buddies ) {
323 _keyRing->importKey( _keyRing->pimpl().exportKey( kd, _keyRing->pimpl().generalKeyRing() ),
true );
326 return makeReturn(_verifyContext.fileValidated());
330 bool userAnswer = _keyringReport.askUserToAcceptVerificationFailed( filedesc, _keyRing->pimpl().exportKey( res._foundKey, res._whichKeyRing ), keyContext );
331 MIL <<
"askUserToAcceptVerificationFailed: " << userAnswer << std::endl;
332 return makeReturn(userAnswer);
336 MIL <<
"File [" << file <<
"] ( " << filedesc <<
" ) signed with unknown key [" <<
id <<
"]" << std::endl;
337 bool res = _keyringReport.askUserToAcceptUnknownKey( filedesc,
id, _verifyContext.keyContext() );
338 MIL <<
"askUserToAcceptUnknownKey: " << res << std::endl;
339 return makeReturn(res);
342 return makeReturn(
false);
347 ZyppContextRefType _zyppContext;
348 KeyRingReportHelper<ZyppContextRefType> _keyringReport;
350 zypp::keyring::VerifyFileContext _verifyContext;
353 inline std::pair<bool, zypp::keyring::VerifyFileContext> makeReturn(
bool res ){
354 _verifyContext.fileAccepted( res );
355 return std::make_pair( res, std::move(_verifyContext) ) ;
360 std::pair<bool,zypp::keyring::VerifyFileContext>
363 auto kr = zyppContext->keyRing();
369 auto kr = zyppContext->keyRing();