Imported Upstream version 15.0.0
[platform/upstream/libzypp.git] / zypp / FileChecker.cc
index 5368eb1..f6b7abc 100644 (file)
@@ -29,9 +29,9 @@ namespace zypp
 
   void ChecksumFileChecker::operator()( const Pathname &file ) const
   {
+      //MIL << "checking " << file << " file against checksum '" << _checksum << "'" << endl;
     callback::SendReport<DigestReport> report;
-    CheckSum real_checksum( _checksum.type(), filesystem::checksum( file, _checksum.type() ));
-    
+
     if ( _checksum.empty() )
     {
       MIL << "File " <<  file << " has no checksum available." << std::endl;
@@ -42,11 +42,12 @@ namespace zypp
       }
       else
       {
-        ZYPP_THROW(FileCheckException("No checksum available"));
+        ZYPP_THROW( FileCheckException( file.basename() + " has no checksum" ) );
       }
     }
     else
     {
+      CheckSum real_checksum( _checksum.type(), filesystem::checksum( file, _checksum.type() ));
       if ( (real_checksum != _checksum) )
       {
         if ( report->askUserToAcceptWrongDigest( file, _checksum.checksum(), real_checksum.checksum() ) )
@@ -56,7 +57,7 @@ namespace zypp
         }
         else
         {
-          ZYPP_THROW(FileCheckException("Wrong checksum"));
+          ZYPP_THROW( FileCheckException( file.basename() + " has wrong checksum" ) );
         }
       }
     }
@@ -64,49 +65,72 @@ namespace zypp
 
   void NullFileChecker::operator()(const Pathname &file ) const
   {
+    MIL << "+ null check on " << file << endl;
     return;
   }
 
   void CompositeFileChecker::operator()(const Pathname &file ) const
   {
-    MIL << _checkers.size() << " checkers" << endl;
+    //MIL << _checkers.size() << " checkers" << endl;
     for ( list<FileChecker>::const_iterator it = _checkers.begin(); it != _checkers.end(); ++it )
-    { 
-      MIL << "checking..." << endl;
-      (*it)(file);
+    {
+      if ( *it )
+      {
+        //MIL << "+ chk" << endl;
+        (*it)(file);
+      }
+      else
+      {
+        ERR << "Invalid checker" << endl;
+      }
     }
   }
-  
+
   void CompositeFileChecker::add( const FileChecker &checker )
   {
     //MIL << "||# " << _checkers.size() << endl;
     _checkers.push_back(checker);
     //MIL << "||* " << _checkers.size() << endl;
-    
+
   }
 
-  SignatureFileChecker::SignatureFileChecker( const Pathname &signature )
-    : _signature(signature)
+   SignatureFileChecker::SignatureFileChecker( const Pathname &signature )
+       : _signature(signature)
   {
+
   }
-  
+
+
   SignatureFileChecker::SignatureFileChecker()
   {
   }
-  
-  void SignatureFileChecker::addPublicKey( const Pathname &publickey )
+
+  void SignatureFileChecker::setKeyContext(const KeyContext & keycontext)
+  { _context = keycontext; }
+
+  void SignatureFileChecker::addPublicKey( const Pathname & publickey, const KeyContext & keycontext )
+  { addPublicKey( PublicKey(publickey), keycontext ); }
+
+  void SignatureFileChecker::addPublicKey( const PublicKey & publickey, const KeyContext & keycontext )
   {
-    ZYpp::Ptr z = getZYpp();
-    z->keyRing()->importKey(publickey, false);
+    getZYpp()->keyRing()->importKey(publickey, false);
+    _context = keycontext;
   }
-  
+
   void SignatureFileChecker::operator()(const Pathname &file ) const
   {
     ZYpp::Ptr z = getZYpp();
+
+    if ( (! PathInfo(_signature).isExist()) && (!_signature.empty()))
+    {
+      ZYPP_THROW(FileCheckException("Signature " + _signature.asString() + " not found."));
+    }
+
     MIL << "checking " << file << " file validity using digital signature.." << endl;
-    bool valid = z->keyRing()->verifyFileSignatureWorkflow( file, string(), _signature);
+    bool valid = z->keyRing()->verifyFileSignatureWorkflow( file, file.basename(), _signature, _context);
+
     if (!valid)
-      ZYPP_THROW(FileCheckException("Signature verification failed"));
+      ZYPP_THROW( FileCheckException( "Signature verification failed for "  + file.basename() ) );
   }
 
   /******************************************************************