41 using zyppng::operators::operator|;
45 return makeReadyTask(
false);
49 return makeReadyTask(
true);
57 if (
_repo.gpgKeyUrlsEmpty() ) {
64 if ( !res )
return false;
87 ERR <<
"Key [" <<
_keyId <<
"] from known keyring is not valid" << std::endl;
91 MIL <<
"Key [" <<
_keyId <<
"] " << key.
name() <<
" loaded from cache" << std::endl;
99 MIL <<
"User wants to import key [" <<
_keyId <<
"] " << key.
name() <<
" from cache" << std::endl;
101 _context->keyRing()->importKey( key,
true );
118 zypp_co_return zypp_co_await( impl.
execute () );
128 struct VerifyFileSignatureLogic
130 using ZyppContextRefType = ContextRef;
134 : _zyppContext(
std::move(zyppContext) )
135 , _keyringReport( _zyppContext )
136 , _keyRing(
std::move(keyRing) )
137 , _verifyContext(
std::move(ctx) )
140 struct FoundKeyData {
142 Ring _whichKeyRing = Ring::Trusted;
143 bool trusted =
false;
146 MaybeAwaitable<FoundKeyData> findKey (
const std::string &
id ) {
148 using zyppng::operators::operator|;
151 return makeReadyTask(FoundKeyData());
154 zypp::PublicKeyData trustedKeyData { _keyRing->pimpl().publicKeyData(
id, Ring::Trusted ) };
155 if ( trustedKeyData )
157 MIL <<
"Key is trusted: " << trustedKeyData << std::endl;
158 return makeReadyTask( FoundKeyData{ trustedKeyData, Ring::Trusted,
true } );
162 zypp::PublicKeyData generalKeyData { _keyRing->pimpl().publicKeyData(
id, Ring::General ) };
163 if ( generalKeyData )
165 zypp::PublicKey key( _keyRing->pimpl().exportKey( generalKeyData, Ring::General ) );
166 MIL <<
"Key [" <<
id <<
"] " << key.name() <<
" is not trusted" << std::endl;
175 MIL <<
"User wants to trust key [" <<
id <<
"] " << key.name() << std::endl;
179 MIL <<
"User wants to import key [" <<
id <<
"] " << key.name() << std::endl;
180 _keyRing->importKey( key,
true );
181 whichKeyring = Ring::Trusted;
184 whichKeyring = Ring::General;
186 return makeReadyTask(FoundKeyData { std::move(generalKeyData), std::move(whichKeyring),
true });
190 MIL <<
"User does not want to trust key [" <<
id <<
"] " << key.name() << std::endl;
191 return makeReadyTask(FoundKeyData { std::move(generalKeyData), Ring::General,
false });
194 else if ( ! _verifyContext.keyContext().empty() )
198 | [
this, id](
bool success ) {
200 return FoundKeyData();
202 return FoundKeyData{ _keyRing->pimpl().publicKeyData(
id, Ring::Trusted ), Ring::Trusted,
true };
206 return makeReadyTask(FoundKeyData());
212 _verifyContext.resetResults();
213 const zypp::Pathname & file { _verifyContext.file() };
214 const zypp::Pathname & signature { _verifyContext.signature() };
215 const std::string & filedesc { _verifyContext.shortFile() };
217 MIL <<
"Going to verify signature for " << filedesc <<
" ( " << file <<
" ) with " << signature << std::endl;
220 if( signature.
empty() || (!zypp::PathInfo( signature ).isExist()) )
222 bool res = _keyringReport.askUserToAcceptUnsignedFile( filedesc, _verifyContext.keyContext() );
223 MIL <<
"askUserToAcceptUnsignedFile: " << res << std::endl;
224 return makeReadyTask( makeReturn(res) );
229 _verifyContext.signatureId( _keyRing->readSignatureKeyId( signature ) );
230 }
catch (
const zypp::Exception &e ) {
231 MIL <<
"Failed to read the signature from " << signature << std::endl;
233 return makeReadyTask( makeReturn(
false) );
236 const std::string &
id = _verifyContext.signatureId();
239 std::list<zypp::PublicKeyData> buddies;
240 for (
const auto & sid : _verifyContext.buddyKeys() ) {
242 WAR <<
"buddy " << sid <<
": key id is too short to safely identify a gpg key. Skipping it." << std::endl;
245 if ( _keyRing->pimpl().publicKeyData( sid, Ring::Trusted ) ) {
246 MIL <<
"buddy " << sid <<
": already in trusted key ring. Not needed." << std::endl;
249 auto pk = _keyRing->pimpl().publicKeyData( sid, Ring::General );
251 WAR <<
"buddy " << sid <<
": not available in the public key ring. Skipping it." << std::endl;
254 if ( pk.providesKey(
id) ) {
255 MIL <<
"buddy " << sid <<
": is the signing key. Handled separately." << std::endl;
258 MIL <<
"buddy " << sid <<
": candidate for auto import. Remeber it." << std::endl;
259 buddies.push_back( pk );
262 using zyppng::operators::operator|;
263 return findKey(
id ) | [
this, id, buddies=std::move(buddies)]( FoundKeyData res ) {
265 const zypp::Pathname & file { _verifyContext.file() };
266 const zypp::KeyContext & keyContext { _verifyContext.keyContext() };
267 const zypp::Pathname & signature { _verifyContext.signature() };
268 const std::string & filedesc { _verifyContext.shortFile() };
270 if ( res._foundKey ) {
274 return makeReturn(
false);
277 _verifyContext.signatureIdTrusted( res._whichKeyRing == Ring::Trusted );
278 _keyringReport.infoVerify( filedesc, res._foundKey, keyContext );
279 if ( _keyRing->pimpl().verifyFile( file, signature, res._whichKeyRing ) )
281 _verifyContext.fileValidated(
true );
282 if ( _verifyContext.signatureIdTrusted() && not buddies.empty() ) {
284 MIL <<
"Validated with trusted key: importing buddy list..." << std::endl;
285 _keyringReport.reportAutoImportKey( buddies, res._foundKey, keyContext );
286 for (
const auto & kd : buddies ) {
287 _keyRing->importKey( _keyRing->pimpl().exportKey( kd, Ring::General ),
true );
290 return makeReturn(_verifyContext.fileValidated());
294 bool userAnswer = _keyringReport.askUserToAcceptVerificationFailed( filedesc, _keyRing->pimpl().exportKey( res._foundKey, res._whichKeyRing ), keyContext );
295 MIL <<
"askUserToAcceptVerificationFailed: " << userAnswer << std::endl;
296 return makeReturn(userAnswer);
300 MIL <<
"File [" << file <<
"] ( " << filedesc <<
" ) signed with unknown key [" <<
id <<
"]" << std::endl;
301 bool res = _keyringReport.askUserToAcceptUnknownKey( filedesc,
id, _verifyContext.keyContext() );
302 MIL <<
"askUserToAcceptUnknownKey: " << res << std::endl;
303 return makeReturn(res);
306 return makeReturn(
false);
311 ZyppContextRefType _zyppContext;
312 KeyRingReportHelper _keyringReport;
314 zypp::keyring::VerifyFileContext _verifyContext;
317 inline std::pair<bool, zypp::keyring::VerifyFileContext> makeReturn(
bool res ){
318 _verifyContext.fileAccepted( res );
319 return std::make_pair( res, std::move(_verifyContext) ) ;
326 auto kr = zyppContext->keyRing();
327 VerifyFileSignatureLogic impl( std::move(zyppContext), std::move(kr), std::move(context_r) );
328 zypp_co_return zypp_co_await( impl.execute () );
333 VerifyFileSignatureLogic impl( std::move(zyppContext), std::move(keyRing), std::move(context_r) );
334 zypp_co_return zypp_co_await( impl.execute () );