------------------------------------------------------------------------
r1594 | pzent | 2014-04-06 14:09:02 +0200 (dom, 06 abr 2014) | 1 line

Fixed compilation on Apple platforms by requiring that they use a regular OpenSSL library instead of Apple's Common Crypto


Modified by Antonio Larrosa <alarrosa@suse.com>
Modified first chunk of PdfEncrypt.cpp

Index: a/podofo/trunk/cmake/modules/FindLIBCRYPTO.cmake
===================================================================
--- a/podofo/trunk/cmake/modules/FindLIBCRYPTO.cmake	(revision 1593)
+++ b/podofo/trunk/cmake/modules/FindLIBCRYPTO.cmake	(revision 1594)
@@ -11,17 +11,18 @@
   SET(LIBCRYPTO_FIND_QUIETLY TRUE)
 ENDIF (LIBCRYPTO_INCLUDE_DIR)
 
-IF(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
-	# MacOSX has deprecated the use of openssl crypto functions
-	# and replaced it with API-compatible CommonCrypto
-	FIND_PATH(LIBCRYPTO_INCLUDE_DIR CommonCrypto/CommonDigest.h)
-	SET(LIBCRYPTO_LIBRARY_NAMES_RELEASE ${LIBCRYPTO_LIBRARY_NAMES_RELEASE} ${LIBCRYPTO_LIBRARY_NAMES} ssl)
-	SET(LIBCRYPTO_LIBRARY_NAMES_DEBUG ${LIBCRYPTO_LIBRARY_NAMES_DEBUG} ssld)
-ELSE(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") 
-	FIND_PATH(LIBCRYPTO_INCLUDE_DIR openssl/sha.h)
-	SET(LIBCRYPTO_LIBRARY_NAMES_RELEASE ${LIBCRYPTO_LIBRARY_NAMES_RELEASE} ${LIBCRYPTO_LIBRARY_NAMES} crypto)
-	SET(LIBCRYPTO_LIBRARY_NAMES_DEBUG ${LIBCRYPTO_LIBRARY_NAMES_DEBUG} cryptod)
-ENDIF(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") 
+# Require a regular OpenSSL even on OSX/iOS
+# IF(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
+#   # MacOSX has deprecated the use of openssl crypto functions
+#   # and replaced it with API-compatible CommonCrypto
+#   FIND_PATH(LIBCRYPTO_INCLUDE_DIR CommonCrypto/CommonDigest.h)
+#   SET(LIBCRYPTO_LIBRARY_NAMES_RELEASE ${LIBCRYPTO_LIBRARY_NAMES_RELEASE} ${LIBCRYPTO_LIBRARY_NAMES} ssl)
+#   SET(LIBCRYPTO_LIBRARY_NAMES_DEBUG ${LIBCRYPTO_LIBRARY_NAMES_DEBUG} ssld)
+# ELSE(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") 
+  FIND_PATH(LIBCRYPTO_INCLUDE_DIR openssl/sha.h)
+  SET(LIBCRYPTO_LIBRARY_NAMES_RELEASE ${LIBCRYPTO_LIBRARY_NAMES_RELEASE} ${LIBCRYPTO_LIBRARY_NAMES} crypto)
+  SET(LIBCRYPTO_LIBRARY_NAMES_DEBUG ${LIBCRYPTO_LIBRARY_NAMES_DEBUG} cryptod)
+# ENDIF(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") 
 
 FIND_LIBRARY(LIBCRYPTO_LIBRARY_RELEASE NAMES ${LIBCRYPTO_LIBRARY_NAMES_RELEASE} )
 
Index: a/podofo/trunk/src/base/PdfEncrypt.cpp
===================================================================
--- a/podofo/trunk/src/base/PdfEncrypt.cpp	(revision 1593)
+++ b/podofo/trunk/src/base/PdfEncrypt.cpp	(revision 1594)
@@ -38,18 +38,11 @@
 // AES-256 dependencies :
 // SASL
 #include <stringprep.h>
-#ifndef __APPLE__
 #include <openssl/sha.h>
-#endif // !__APPLE__
 #endif // PODOFO_HAVE_LIBIDN
 
-#ifdef __APPLE__
-#define COMMON_DIGEST_FOR_OPENSSL
-#include <CommonCrypto/CommonCrypto.h>
-#else // __APPLE__
 #include <openssl/md5.h>
 #include <openssl/evp.h>
-#endif // __APPLE__
 
 namespace
 {
@@ -79,41 +71,24 @@
 #define AES_IV_LENGTH 16
 
 // A class that holds the AES Crypto object
-// Either CCCrpytor or EVP_CIPHER_CTX
 class AESCryptoEngine {
     public:
     
         AESCryptoEngine()
         {
-#ifdef __APPLE__
-            aes = NULL;
-#else
             EVP_CIPHER_CTX_init(&aes);
-#endif // __APPLE__
         }
     
-#ifdef __APPLE__
-        CCCryptorRef* getEngine() {return &aes;}
-#else // __APPLE__
         EVP_CIPHER_CTX* getEngine() {return &aes;}
-#endif // __APPLE__
     
         ~AESCryptoEngine()
         {
-#ifdef __APPLE__
-            CCCryptorRelease(aes);
-#else // __APPLE__
             EVP_CIPHER_CTX_cleanup(&aes);
-#endif // __APPLE__
         }
     
     private:
     
-#ifdef __APPLE__
-        CCCryptorRef aes;
-#else // __APPLE__
         EVP_CIPHER_CTX aes;
-#endif // __APPLE__
 };
 
 // A class that holds the RC4 Crypto object
@@ -123,35 +98,19 @@
     
     RC4CryptoEngine()
     {
-#ifdef __APPLE__
-        rc4 = NULL;
-#else
         EVP_CIPHER_CTX_init(&rc4);
-#endif // __APPLE__
     }
     
-#ifdef __APPLE__
-    CCCryptorRef* getEngine() {return &rc4;}
-#else // __APPLE__
     EVP_CIPHER_CTX* getEngine() {return &rc4;}
-#endif // __APPLE__
     
     ~RC4CryptoEngine()
     {
-#ifdef __APPLE__
-        CCCryptorRelease(rc4);
-#else // __APPLE__
         EVP_CIPHER_CTX_cleanup(&rc4);
-#endif // __APPLE__
     }
     
 private:
     
-#ifdef __APPLE__
-    CCCryptorRef rc4;
-#else // __APPLE__
     EVP_CIPHER_CTX rc4;
-#endif // __APPLE__
 };
     
 /** A class that can encrypt/decrpyt streamed data block wise
@@ -941,39 +900,11 @@
  * RC4 is the standard encryption algorithm used in PDF format
  */
 
-#ifdef __APPLE__
 void
 PdfEncryptRC4Base::RC4(const unsigned char* key, int keylen,
                        const unsigned char* textin, pdf_long textlen,
                        unsigned char* textout, pdf_long textoutlen)
 {
-    CCCryptorRef* rc4 = m_rc4->getEngine();
-    
-    CCCryptorStatus status = CCCryptorCreate(kCCEncrypt, kCCAlgorithmRC4, 0, key, keylen, NULL, rc4);
-    if(status != kCCSuccess)
-        PODOFO_RAISE_ERROR_INFO( ePdfError_InternalLogic, "Error initializing RC4 encryption engine" );
-    
-    pdf_long requiredOutputLength = CCCryptorGetOutputLength(*rc4, textlen, true);
-    if(requiredOutputLength != textoutlen)
-        PODOFO_RAISE_ERROR_INFO( ePdfError_InternalLogic, "Error RC4-encrypting data" );
-    
-    size_t dataOutMoved;
-    status = CCCryptorUpdate(*rc4, textin, textlen, textout, textoutlen, &dataOutMoved);
-    if(status != kCCSuccess)
-        PODOFO_RAISE_ERROR_INFO( ePdfError_InternalLogic, "Error RC4-encrypting data" );
-    
-    status = CCCryptorFinal(*rc4, &textout[dataOutMoved], textoutlen-dataOutMoved, &dataOutMoved);
-    if(status != kCCSuccess)
-        PODOFO_RAISE_ERROR_INFO( ePdfError_InternalLogic, "Error RC4-encrypting data" );
-}
-    
-#else // __APPLE__
-    
-void
-PdfEncryptRC4Base::RC4(const unsigned char* key, int keylen,
-                       const unsigned char* textin, pdf_long textlen,
-                       unsigned char* textout, pdf_long textoutlen)
-{
     EVP_CIPHER_CTX* rc4 = m_rc4->getEngine();
     
     if(textlen != textoutlen)
@@ -1002,7 +933,6 @@
     if(status != 1)
         PODOFO_RAISE_ERROR_INFO( ePdfError_InternalLogic, "Error RC4-encrypting data" );
 }
-#endif // __APPLE__
         
 void
 PdfEncryptMD5Base::GetMD5Binary(const unsigned char* data, int length, unsigned char* digest)
#@@ -1252,34 +1182,6 @@
# {
#     delete m_aes;
# }
#-    
#-#ifdef __APPLE__
#-    void
#-PdfEncryptAESBase::Encrypt(const unsigned char* key, int keyLen, const unsigned char* iv,
#-                       const unsigned char* textin, pdf_long textlen,
#-                       unsigned char* textout, pdf_long textoutlen)
#-{   
#-    CCCryptorRef* aes = m_aes->getEngine();
#-    
#-    CCCryptorStatus status = CCCryptorCreate(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding, key, keyLen, iv, aes);
#-    if(status != kCCSuccess)
#-        PODOFO_RAISE_ERROR_INFO( ePdfError_InternalLogic, "Error initializing AES encryption engine" );
#-    
#-    pdf_long requiredOutputLength = CCCryptorGetOutputLength(*aes, textlen, true);
#-    if(requiredOutputLength != textoutlen)
#-        PODOFO_RAISE_ERROR_INFO( ePdfError_InternalLogic, "Error AES-encrypting data" );
#-    
#-    size_t dataOutMoved;
#-    status = CCCryptorUpdate(*aes, textin, textlen, textout, textoutlen, &dataOutMoved);
#-    if(status != kCCSuccess)
#-        PODOFO_RAISE_ERROR_INFO( ePdfError_InternalLogic, "Error AES-encrypting data" );
#-    
#-    status = CCCryptorFinal(*aes, &textout[dataOutMoved], textoutlen-dataOutMoved, &dataOutMoved);
#-    if(status != kCCSuccess)
#-        PODOFO_RAISE_ERROR_INFO( ePdfError_InternalLogic, "Error AES-encrypting data" );
#-}
#-    
#-#else // __APPLE__
# 
# void
# PdfEncryptAESBase::BaseDecrypt(const unsigned char* key, int keyLen, const unsigned char* iv,
#@@ -1343,7 +1245,6 @@
#     if(status != 1)
#         PODOFO_RAISE_ERROR_INFO( ePdfError_InternalLogic, "Error AES-encrypting data" );
# }
#-#endif // __APPLE__
#     
# void
# PdfEncryptAESV2::GenerateEncryptionKey(const PdfString & documentId)
@@ -1557,16 +1458,7 @@
     
     // UE = AES-256 encoded file encryption key with key=hash
     // CBC mode, no padding, init vector=0
-#ifdef __APPLE__
     
-    size_t dataOutMoved;
-    CCCryptorStatus status = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, 0, hashValue, 32, NULL, m_encryptionKey, m_keyLength, m_ueValue, 32, &dataOutMoved);
-    if(status != kCCSuccess) {
-        PODOFO_RAISE_ERROR_INFO( ePdfError_InternalLogic, "Error initializing AES encryption engine" );
-    }
-    
-#else // __APPLE__
-    
     EVP_CIPHER_CTX aes;
     EVP_CIPHER_CTX_init(&aes);
     
@@ -1585,8 +1477,6 @@
         PODOFO_RAISE_ERROR_INFO( ePdfError_InternalLogic, "Error AES-encrypting data" );
     
     EVP_CIPHER_CTX_cleanup(&aes);
-    
-#endif // __APPLE__
 }
 
 void PdfEncryptSHABase::ComputeOwnerKey(const unsigned char * ownerpswd, int len)
@@ -1624,15 +1514,7 @@
     
     // OE = AES-256 encoded file encryption key with key=hash
     // CBC mode, no padding, init vector=0
-#ifdef __APPLE__
     
-    size_t dataOutMoved;
-    CCCryptorStatus status = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, 0, hashValue, 32, NULL, m_encryptionKey, m_keyLength, m_oeValue, 32, &dataOutMoved);
-    if(status != kCCSuccess)
-        PODOFO_RAISE_ERROR_INFO( ePdfError_InternalLogic, "Error initializing AES encryption engine" );
-    
-#else // __APPLE__
-    
     EVP_CIPHER_CTX aes;
     EVP_CIPHER_CTX_init(&aes);
     
@@ -1651,8 +1533,6 @@
         PODOFO_RAISE_ERROR_INFO( ePdfError_InternalLogic, "Error AES-encrypting data" );
     
     EVP_CIPHER_CTX_cleanup(&aes);
-    
-#endif // __APPLE__
 }
 
 void PdfEncryptSHABase::PreprocessPassword( const std::string &password, unsigned char* outBuf, int &len)
@@ -1782,15 +1662,7 @@
     perms[15] = 0;
     
     // Encrypt Perms value
-#ifdef __APPLE__
     
-    size_t dataOutMoved;
-    CCCryptorStatus status = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionECBMode, m_encryptionKey, 32, NULL, perms, 16, m_permsValue, 16, &dataOutMoved);
-    if(status != kCCSuccess)
-        PODOFO_RAISE_ERROR_INFO( ePdfError_InternalLogic, "Error initializing AES encryption engine" );
-    
-#else // __APPLE__
-    
     EVP_CIPHER_CTX aes;
     EVP_CIPHER_CTX_init(&aes);
     
@@ -1809,8 +1681,6 @@
         PODOFO_RAISE_ERROR_INFO( ePdfError_InternalLogic, "Error AES-encrypting data" );
     
     EVP_CIPHER_CTX_cleanup(&aes);
-    
-#endif // __APPLE__
 }
 
 bool PdfEncryptAESV3::Authenticate( const std::string & password, const PdfString & )

------------------------------------------------------------------------
