OSDN Git Service

okular: add support for more decryption algorithms, key and checksum types
authorIvailo Monev <xakepa10@gmail.com>
Sat, 27 Mar 2021 14:54:00 +0000 (16:54 +0200)
committerIvailo Monev <xakepa10@gmail.com>
Sat, 27 Mar 2021 15:01:51 +0000 (17:01 +0200)
Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
okular/generators/ooo/manifest.cpp
okular/generators/ooo/manifest.h

index 512eb39..8a6c60f 100644 (file)
@@ -39,7 +39,7 @@ void ManifestEntry::setMimeType( const QString &mimeType )
 
 void ManifestEntry::setSize( const QString &size )
 {
-  m_size = size;
+  m_size = size.toInt();
 }
 
 QString ManifestEntry::fileName() const
@@ -52,7 +52,7 @@ QString ManifestEntry::mimeType() const
   return m_mimeType;
 }
 
-QString ManifestEntry::size() const
+int ManifestEntry::size() const
 {
   return m_size;
 }
@@ -278,10 +278,14 @@ void Manifest::checkPassword( ManifestEntry *entry, const QByteArray &fileData,
   const int keygenerationhashindex = keygenerationname.indexOf('#');
   keygenerationname = keygenerationname.mid(keygenerationhashindex + 1);
   int gcryptkeyalgorithm = GCRY_MD_NONE;
+  // libreoffice suports SHA1 and SHA512, for reference:
+  // libreoffice/oox/source/crypto/AgileEngine.cxx
   if ( keygenerationname == "sha1" ) {
     gcryptkeyalgorithm = GCRY_MD_SHA1;
   } else if ( keygenerationname == "sha256" ) {
     gcryptkeyalgorithm = GCRY_MD_SHA256;
+  } else if ( keygenerationname == "sha512" ) {
+    gcryptkeyalgorithm = GCRY_MD_SHA512;
   } else {
     kWarning(OooDebug) << "unknown key generation name: " << entry->keyGenerationName();
     // we can only assume it will be OK.
@@ -309,9 +313,15 @@ void Manifest::checkPassword( ManifestEntry *entry, const QByteArray &fileData,
   algorithm = algorithm.mid(algorithmhashindex + 1);
   int gcryptalgorithm = GCRY_CIPHER_NONE;
   int gcryptmode = GCRY_CIPHER_MODE_NONE;
-  // TODO: what other algorithms are used by applications?
-  // default of libreoffice
-  if ( algorithm == "aes256-cbc" ) {
+  // libreoffice suports AES128-ECB, AES128-CBC and AES256-CBC, for reference:
+  // libreoffice/oox/source/crypto/CryptTools.cxx
+  if ( algorithm == "aes128-ecb" ) {
+    gcryptalgorithm = GCRY_CIPHER_AES128;
+    gcryptmode = GCRY_CIPHER_MODE_ECB;
+  } else if ( algorithm == "aes128-cbc" ) {
+    gcryptalgorithm = GCRY_CIPHER_AES128;
+    gcryptmode = GCRY_CIPHER_MODE_CBC;
+  } else if ( algorithm == "aes256-cbc" ) {
     gcryptalgorithm = GCRY_CIPHER_AES256;
     gcryptmode = GCRY_CIPHER_MODE_CBC;
   // according to the spec "blowfish" is Blowfish CFB but just in case match "-cfb" suffixed one too
@@ -365,13 +375,14 @@ void Manifest::checkPassword( ManifestEntry *entry, const QByteArray &fileData,
   }
   gcry_cipher_close( dec );
 
-  const int size = entry->size().toInt();
-  *decryptedData = QByteArray( reinterpret_cast<char*>(decbuff), size );
+  *decryptedData = QByteArray( reinterpret_cast<char*>(decbuff), entry->size() );
 
   QByteArray csum;
   QString checksumtype = entry->checksumType().toLower();
   const int checksumhashindex = checksumtype.indexOf('#');
   checksumtype = checksumtype.mid(checksumhashindex + 1);
+  // libreoffice suports SHA1, SHA256 and SHA512, for reference:
+  // libreoffice/oox/source/crypto/CryptTools.cxx
   if ( checksumtype == "sha1-1k" ) {
     csum = QCryptographicHash::hash( decryptedData->left(1024), QCryptographicHash::Sha1 );
   } else if ( checksumtype == "sha1" ) {
@@ -380,6 +391,10 @@ void Manifest::checkPassword( ManifestEntry *entry, const QByteArray &fileData,
     csum = QCryptographicHash::hash( decryptedData->left(1024), QCryptographicHash::Sha256 );
   } else if ( checksumtype == "sha256") {
     csum = QCryptographicHash::hash( *decryptedData, QCryptographicHash::Sha256 );
+  } else if ( checksumtype == "sha512-1k") {
+    csum = QCryptographicHash::hash( decryptedData->left(1024), QCryptographicHash::Sha512 );
+  } else if ( checksumtype == "sha512") {
+    csum = QCryptographicHash::hash( *decryptedData, QCryptographicHash::Sha512 );
   } else {
     kWarning(OooDebug) << "unknown checksum type: " << entry->checksumType();
     // we can only assume it will be OK.
index a44b59e..9b40ddf 100644 (file)
@@ -66,7 +66,7 @@ class ManifestEntry
 
        This is only meaningful for files with encryption
     */
-    QString size() const;
+    int size() const;
 
     /**
        Set the checksum type.
@@ -207,7 +207,7 @@ class ManifestEntry
  private:
    const QString m_fileName;
    QString m_mimeType;
-   QString m_size;
+   int m_size;
    QString m_checksumType;
    QByteArray m_checksum;
    QString m_algorithm;