LCOV - code coverage report
Current view: top level - comphelper/source/misc - docpasswordhelper.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 0 171 0.0 %
Date: 2014-11-03 Functions: 0 13 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include <algorithm>
      21             : 
      22             : #include <comphelper/docpasswordhelper.hxx>
      23             : #include <com/sun/star/beans/PropertyValue.hpp>
      24             : #include <com/sun/star/task/XInteractionHandler.hpp>
      25             : 
      26             : #include <osl/time.h>
      27             : #include <rtl/digest.h>
      28             : #include <rtl/random.h>
      29             : #include <string.h>
      30             : 
      31             : using ::com::sun::star::uno::Sequence;
      32             : using ::com::sun::star::uno::Exception;
      33             : using ::com::sun::star::uno::Reference;
      34             : using ::com::sun::star::uno::UNO_SET_THROW;
      35             : using ::com::sun::star::task::PasswordRequestMode;
      36             : using ::com::sun::star::task::PasswordRequestMode_PASSWORD_ENTER;
      37             : using ::com::sun::star::task::PasswordRequestMode_PASSWORD_REENTER;
      38             : using ::com::sun::star::task::XInteractionHandler;
      39             : using ::com::sun::star::task::XInteractionRequest;
      40             : 
      41             : using namespace ::com::sun::star;
      42             : 
      43             : namespace comphelper {
      44             : 
      45             : 
      46             : 
      47           0 : static uno::Sequence< sal_Int8 > GeneratePBKDF2Hash( const OUString& aPassword, const uno::Sequence< sal_Int8 >& aSalt, sal_Int32 nCount, sal_Int32 nHashLength )
      48             : {
      49           0 :     uno::Sequence< sal_Int8 > aResult;
      50             : 
      51           0 :     if ( !aPassword.isEmpty() && aSalt.getLength() && nCount && nHashLength )
      52             :     {
      53           0 :         OString aBytePass = OUStringToOString( aPassword, RTL_TEXTENCODING_UTF8 );
      54           0 :         aResult.realloc( 16 );
      55           0 :         rtl_digest_PBKDF2( reinterpret_cast < sal_uInt8 * > ( aResult.getArray() ),
      56           0 :                            aResult.getLength(),
      57           0 :                            reinterpret_cast < const sal_uInt8 * > ( aBytePass.getStr() ),
      58           0 :                            aBytePass.getLength(),
      59           0 :                            reinterpret_cast < const sal_uInt8 * > ( aSalt.getConstArray() ),
      60           0 :                            aSalt.getLength(),
      61           0 :                            nCount );
      62             :     }
      63             : 
      64           0 :     return aResult;
      65             : }
      66             : 
      67             : 
      68             : 
      69           0 : IDocPasswordVerifier::~IDocPasswordVerifier()
      70             : {
      71           0 : }
      72             : 
      73             : 
      74           0 : uno::Sequence< beans::PropertyValue > DocPasswordHelper::GenerateNewModifyPasswordInfo( const OUString& aPassword )
      75             : {
      76           0 :     uno::Sequence< beans::PropertyValue > aResult;
      77             : 
      78           0 :     uno::Sequence< sal_Int8 > aSalt = GenerateRandomByteSequence( 16 );
      79           0 :     sal_Int32 nCount = 1024;
      80             : 
      81           0 :     uno::Sequence< sal_Int8 > aNewHash = GeneratePBKDF2Hash( aPassword, aSalt, nCount, 16 );
      82           0 :     if ( aNewHash.getLength() )
      83             :     {
      84           0 :         aResult.realloc( 4 );
      85           0 :         aResult[0].Name = "algorithm-name";
      86           0 :         aResult[0].Value <<= OUString( "PBKDF2" );
      87           0 :         aResult[1].Name = "salt";
      88           0 :         aResult[1].Value <<= aSalt;
      89           0 :         aResult[2].Name = "iteration-count";
      90           0 :         aResult[2].Value <<= nCount;
      91           0 :         aResult[3].Name = "hash";
      92           0 :         aResult[3].Value <<= aNewHash;
      93             :     }
      94             : 
      95           0 :     return aResult;
      96             : }
      97             : 
      98             : 
      99           0 : bool DocPasswordHelper::IsModifyPasswordCorrect( const OUString& aPassword, const uno::Sequence< beans::PropertyValue >& aInfo )
     100             : {
     101           0 :     bool bResult = false;
     102           0 :     if ( !aPassword.isEmpty() && aInfo.getLength() )
     103             :     {
     104           0 :         OUString sAlgorithm;
     105           0 :         uno::Sequence< sal_Int8 > aSalt;
     106           0 :         uno::Sequence< sal_Int8 > aHash;
     107           0 :         sal_Int32 nCount = 0;
     108             : 
     109           0 :         for ( sal_Int32 nInd = 0; nInd < aInfo.getLength(); nInd++ )
     110             :         {
     111           0 :             if ( aInfo[nInd].Name == "algorithm-name" )
     112           0 :                 aInfo[nInd].Value >>= sAlgorithm;
     113           0 :             else if ( aInfo[nInd].Name == "salt" )
     114           0 :                 aInfo[nInd].Value >>= aSalt;
     115           0 :             else if ( aInfo[nInd].Name == "iteration-count" )
     116           0 :                 aInfo[nInd].Value >>= nCount;
     117           0 :             else if ( aInfo[nInd].Name == "hash" )
     118           0 :                 aInfo[nInd].Value >>= aHash;
     119             :         }
     120             : 
     121           0 :         if ( sAlgorithm == "PBKDF2" && aSalt.getLength() && nCount > 0 && aHash.getLength() )
     122             :         {
     123           0 :             uno::Sequence< sal_Int8 > aNewHash = GeneratePBKDF2Hash( aPassword, aSalt, nCount, aHash.getLength() );
     124           0 :             for ( sal_Int32 nInd = 0; nInd < aNewHash.getLength() && nInd < aHash.getLength() && aNewHash[nInd] == aHash[nInd]; nInd ++ )
     125             :             {
     126           0 :                 if ( nInd == aNewHash.getLength() - 1 && nInd == aHash.getLength() - 1 )
     127           0 :                     bResult = true;
     128           0 :             }
     129           0 :         }
     130             :     }
     131             : 
     132           0 :     return bResult;
     133             : }
     134             : 
     135             : 
     136           0 : sal_uInt32 DocPasswordHelper::GetWordHashAsUINT32(
     137             :                 const OUString& aUString )
     138             : {
     139             :     static const sal_uInt16 pInitialCode[] = {
     140             :         0xE1F0, // 1
     141             :         0x1D0F, // 2
     142             :         0xCC9C, // 3
     143             :         0x84C0, // 4
     144             :         0x110C, // 5
     145             :         0x0E10, // 6
     146             :         0xF1CE, // 7
     147             :         0x313E, // 8
     148             :         0x1872, // 9
     149             :         0xE139, // 10
     150             :         0xD40F, // 11
     151             :         0x84F9, // 12
     152             :         0x280C, // 13
     153             :         0xA96A, // 14
     154             :         0x4EC3  // 15
     155             :     };
     156             : 
     157             :     static const sal_uInt16 pEncryptionMatrix[15][7] = {
     158             :         { 0xAEFC, 0x4DD9, 0x9BB2, 0x2745, 0x4E8A, 0x9D14, 0x2A09}, // last-14
     159             :         { 0x7B61, 0xF6C2, 0xFDA5, 0xEB6B, 0xC6F7, 0x9DCF, 0x2BBF}, // last-13
     160             :         { 0x4563, 0x8AC6, 0x05AD, 0x0B5A, 0x16B4, 0x2D68, 0x5AD0}, // last-12
     161             :         { 0x0375, 0x06EA, 0x0DD4, 0x1BA8, 0x3750, 0x6EA0, 0xDD40}, // last-11
     162             :         { 0xD849, 0xA0B3, 0x5147, 0xA28E, 0x553D, 0xAA7A, 0x44D5}, // last-10
     163             :         { 0x6F45, 0xDE8A, 0xAD35, 0x4A4B, 0x9496, 0x390D, 0x721A}, // last-9
     164             :         { 0xEB23, 0xC667, 0x9CEF, 0x29FF, 0x53FE, 0xA7FC, 0x5FD9}, // last-8
     165             :         { 0x47D3, 0x8FA6, 0x8FA6, 0x1EDA, 0x3DB4, 0x7B68, 0xF6D0}, // last-7
     166             :         { 0xB861, 0x60E3, 0xC1C6, 0x93AD, 0x377B, 0x6EF6, 0xDDEC}, // last-6
     167             :         { 0x45A0, 0x8B40, 0x06A1, 0x0D42, 0x1A84, 0x3508, 0x6A10}, // last-5
     168             :         { 0xAA51, 0x4483, 0x8906, 0x022D, 0x045A, 0x08B4, 0x1168}, // last-4
     169             :         { 0x76B4, 0xED68, 0xCAF1, 0x85C3, 0x1BA7, 0x374E, 0x6E9C}, // last-3
     170             :         { 0x3730, 0x6E60, 0xDCC0, 0xA9A1, 0x4363, 0x86C6, 0x1DAD}, // last-2
     171             :         { 0x3331, 0x6662, 0xCCC4, 0x89A9, 0x0373, 0x06E6, 0x0DCC}, // last-1
     172             :         { 0x1021, 0x2042, 0x4084, 0x8108, 0x1231, 0x2462, 0x48C4}  // last
     173             :     };
     174             : 
     175           0 :     sal_uInt32 nResult = 0;
     176           0 :     sal_uInt32 nLen = aUString.getLength();
     177             : 
     178           0 :     if ( nLen )
     179             :     {
     180           0 :         if ( nLen > 15 )
     181           0 :             nLen = 15;
     182             : 
     183           0 :         sal_uInt16 nHighResult = pInitialCode[nLen - 1];
     184           0 :         sal_uInt16 nLowResult = 0;
     185             : 
     186           0 :         const sal_Unicode* pStr = aUString.getStr();
     187           0 :         for ( sal_uInt32 nInd = 0; nInd < nLen; nInd++ )
     188             :         {
     189             :             // NO Encoding during conversion!
     190             :             // The specification says that the low byte should be used in case it is not NULL
     191           0 :             char nHighChar = (char)( pStr[nInd] >> 8 );
     192           0 :             char nLowChar = (char)( pStr[nInd] & 0xFF );
     193           0 :             char nChar = nLowChar ? nLowChar : nHighChar;
     194             : 
     195           0 :             for ( int nMatrixInd = 0; nMatrixInd < 7; ++nMatrixInd )
     196             :             {
     197           0 :                 if ( ( nChar & ( 1 << nMatrixInd ) ) != 0 )
     198           0 :                     nHighResult = nHighResult ^ pEncryptionMatrix[15 - nLen + nInd][nMatrixInd];
     199             :             }
     200             : 
     201           0 :             nLowResult = ( ( ( nLowResult >> 14 ) & 0x0001 ) | ( ( nLowResult << 1 ) & 0x7FFF ) ) ^ nChar;
     202             :         }
     203             : 
     204           0 :         nLowResult = (sal_uInt16)( ( ( ( nLowResult >> 14 ) & 0x001 ) | ( ( nLowResult << 1 ) & 0x7FF ) ) ^ nLen ^ 0xCE4B );
     205             : 
     206           0 :         nResult = ( nHighResult << 16 ) | nLowResult;
     207             :     }
     208             : 
     209           0 :     return nResult;
     210             : }
     211             : 
     212             : 
     213           0 : sal_uInt16 DocPasswordHelper::GetXLHashAsUINT16(
     214             :                 const OUString& aUString,
     215             :                 rtl_TextEncoding nEnc )
     216             : {
     217           0 :     sal_uInt16 nResult = 0;
     218             : 
     219           0 :     OString aString = OUStringToOString( aUString, nEnc );
     220             : 
     221           0 :     if ( !aString.isEmpty() && aString.getLength() <= SAL_MAX_UINT16 )
     222             :     {
     223           0 :         for ( sal_Int32 nInd = aString.getLength() - 1; nInd >= 0; nInd-- )
     224             :         {
     225           0 :             nResult = ( ( nResult >> 14 ) & 0x01 ) | ( ( nResult << 1 ) & 0x7FFF );
     226           0 :             nResult ^= aString[nInd];
     227             :         }
     228             : 
     229           0 :         nResult = ( ( nResult >> 14 ) & 0x01 ) | ( ( nResult << 1 ) & 0x7FFF );
     230           0 :         nResult ^= ( 0x8000 | ( 'N' << 8 ) | 'K' );
     231           0 :         nResult ^= aString.getLength();
     232             :     }
     233             : 
     234           0 :     return nResult;
     235             : }
     236             : 
     237             : 
     238           0 : Sequence< sal_Int8 > DocPasswordHelper::GetXLHashAsSequence(
     239             :                 const OUString& aUString,
     240             :                 rtl_TextEncoding nEnc )
     241             : {
     242           0 :     sal_uInt16 nHash = GetXLHashAsUINT16( aUString, nEnc );
     243           0 :     Sequence< sal_Int8 > aResult( 2 );
     244           0 :     aResult[0] = ( nHash >> 8 );
     245           0 :     aResult[1] = ( nHash & 0xFF );
     246             : 
     247           0 :     return aResult;
     248             : }
     249             : 
     250             : 
     251           0 : /*static*/ uno::Sequence< sal_Int8 > DocPasswordHelper::GenerateRandomByteSequence( sal_Int32 nLength )
     252             : {
     253           0 :     uno::Sequence< sal_Int8 > aResult( nLength );
     254             : 
     255             :     TimeValue aTime;
     256           0 :     osl_getSystemTime( &aTime );
     257           0 :     rtlRandomPool aRandomPool = rtl_random_createPool ();
     258           0 :     rtl_random_addBytes ( aRandomPool, &aTime, 8 );
     259           0 :     rtl_random_getBytes ( aRandomPool, aResult.getArray(), nLength );
     260           0 :     rtl_random_destroyPool ( aRandomPool );
     261             : 
     262           0 :     return aResult;
     263             : }
     264             : 
     265             : 
     266             : 
     267           0 : /*static*/ uno::Sequence< sal_Int8 > DocPasswordHelper::GenerateStd97Key( const OUString& aPassword, const uno::Sequence< sal_Int8 >& aDocId )
     268             : {
     269           0 :     uno::Sequence< sal_Int8 > aResultKey;
     270           0 :     if ( !aPassword.isEmpty() && aDocId.getLength() == 16 )
     271             :     {
     272             :         sal_uInt16 pPassData[16];
     273           0 :         memset( pPassData, 0, sizeof(pPassData) );
     274             : 
     275           0 :         sal_Int32 nPassLen = ::std::min< sal_Int32 >( aPassword.getLength(), 15 );
     276           0 :         memcpy( pPassData, aPassword.getStr(), nPassLen * sizeof(pPassData[0]) );
     277             : 
     278           0 :         aResultKey = GenerateStd97Key( pPassData, aDocId );
     279             :     }
     280             : 
     281           0 :     return aResultKey;
     282             : }
     283             : 
     284             : 
     285           0 : /*static*/ uno::Sequence< sal_Int8 > DocPasswordHelper::GenerateStd97Key( const sal_uInt16 pPassData[16], const uno::Sequence< sal_Int8 >& aDocId )
     286             : {
     287           0 :     uno::Sequence< sal_Int8 > aResultKey;
     288             : 
     289           0 :     if ( aDocId.getLength() == 16 )
     290           0 :         aResultKey = GenerateStd97Key(pPassData, (const sal_uInt8*)aDocId.getConstArray());
     291             : 
     292           0 :     return aResultKey;
     293             : }
     294             : 
     295             : 
     296           0 : /*static*/ uno::Sequence< sal_Int8 > DocPasswordHelper::GenerateStd97Key( const sal_uInt16 pPassData[16], const sal_uInt8 pDocId[16] )
     297             : {
     298           0 :     uno::Sequence< sal_Int8 > aResultKey;
     299           0 :     if ( pPassData[0] )
     300             :     {
     301             :         sal_uInt8 pKeyData[64];
     302           0 :         memset( pKeyData, 0, sizeof(pKeyData) );
     303             : 
     304           0 :         sal_Int32 nInd = 0;
     305             : 
     306             :         // Fill PassData into KeyData.
     307           0 :         for ( nInd = 0; nInd < 16 && pPassData[nInd]; nInd++)
     308             :         {
     309           0 :             pKeyData[2*nInd] = sal::static_int_cast< sal_uInt8 >( (pPassData[nInd] >> 0) & 0xff );
     310           0 :             pKeyData[2*nInd + 1] = sal::static_int_cast< sal_uInt8 >( (pPassData[nInd] >> 8) & 0xff );
     311             :         }
     312             : 
     313           0 :         pKeyData[2*nInd] = 0x80;
     314           0 :         pKeyData[56] = sal::static_int_cast< sal_uInt8 >( nInd << 4 );
     315             : 
     316             :         // Fill raw digest of KeyData into KeyData.
     317           0 :         rtlDigest hDigest = rtl_digest_create ( rtl_Digest_AlgorithmMD5 );
     318             :         (void)rtl_digest_updateMD5 (
     319           0 :             hDigest, pKeyData, sizeof(pKeyData));
     320             :         (void)rtl_digest_rawMD5 (
     321           0 :             hDigest, pKeyData, RTL_DIGEST_LENGTH_MD5);
     322             : 
     323             :         // Update digest with KeyData and Unique.
     324           0 :         for ( nInd = 0; nInd < 16; nInd++ )
     325             :         {
     326           0 :             rtl_digest_updateMD5( hDigest, pKeyData, 5 );
     327           0 :             rtl_digest_updateMD5( hDigest, pDocId, 16 );
     328             :         }
     329             : 
     330             :         // Update digest with padding.
     331           0 :         pKeyData[16] = 0x80;
     332           0 :         memset( pKeyData + 17, 0, sizeof(pKeyData) - 17 );
     333           0 :         pKeyData[56] = 0x80;
     334           0 :         pKeyData[57] = 0x0a;
     335             : 
     336           0 :         rtl_digest_updateMD5( hDigest, &(pKeyData[16]), sizeof(pKeyData) - 16 );
     337             : 
     338             :         // Fill raw digest of above updates
     339           0 :         aResultKey.realloc( RTL_DIGEST_LENGTH_MD5 );
     340           0 :         rtl_digest_rawMD5 ( hDigest, (sal_uInt8*)aResultKey.getArray(), aResultKey.getLength() );
     341             : 
     342             :         // Erase KeyData array and leave.
     343           0 :         memset( pKeyData, 0, sizeof(pKeyData) );
     344             :     }
     345             : 
     346           0 :     return aResultKey;
     347             : }
     348             : 
     349             : 
     350             : 
     351             : 
     352           0 : /*static*/ ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue > DocPasswordHelper::requestAndVerifyDocPassword(
     353             :         IDocPasswordVerifier& rVerifier,
     354             :         const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue >& rMediaEncData,
     355             :         const OUString& rMediaPassword,
     356             :         const Reference< XInteractionHandler >& rxInteractHandler,
     357             :         const OUString& rDocumentName,
     358             :         DocPasswordRequestType eRequestType,
     359             :         const ::std::vector< OUString >* pDefaultPasswords,
     360             :         bool* pbIsDefaultPassword )
     361             : {
     362           0 :     ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue > aEncData;
     363           0 :     DocPasswordVerifierResult eResult = DocPasswordVerifierResult_WRONG_PASSWORD;
     364             : 
     365             :     // first, try provided default passwords
     366           0 :     if( pbIsDefaultPassword )
     367           0 :         *pbIsDefaultPassword = false;
     368           0 :     if( pDefaultPasswords )
     369             :     {
     370           0 :         for( ::std::vector< OUString >::const_iterator aIt = pDefaultPasswords->begin(), aEnd = pDefaultPasswords->end(); (eResult == DocPasswordVerifierResult_WRONG_PASSWORD) && (aIt != aEnd); ++aIt )
     371             :         {
     372             :             OSL_ENSURE( !aIt->isEmpty(), "DocPasswordHelper::requestAndVerifyDocPassword - unexpected empty default password" );
     373           0 :             if( !aIt->isEmpty() )
     374             :             {
     375           0 :                 eResult = rVerifier.verifyPassword( *aIt, aEncData );
     376           0 :                 if( pbIsDefaultPassword )
     377           0 :                     *pbIsDefaultPassword = eResult == DocPasswordVerifierResult_OK;
     378             :             }
     379             :         }
     380             :     }
     381             : 
     382             :     // try media encryption data (skip, if result is OK or ABORT)
     383           0 :     if( eResult == DocPasswordVerifierResult_WRONG_PASSWORD )
     384             :     {
     385           0 :         if( rMediaEncData.getLength() > 0 )
     386             :         {
     387           0 :             eResult = rVerifier.verifyEncryptionData( rMediaEncData );
     388           0 :             if( eResult == DocPasswordVerifierResult_OK )
     389           0 :                 aEncData = rMediaEncData;
     390             :         }
     391             :     }
     392             : 
     393             :     // try media password (skip, if result is OK or ABORT)
     394           0 :     if( eResult == DocPasswordVerifierResult_WRONG_PASSWORD )
     395             :     {
     396           0 :         if( !rMediaPassword.isEmpty() )
     397           0 :             eResult = rVerifier.verifyPassword( rMediaPassword, aEncData );
     398             :     }
     399             : 
     400             :     // request a password (skip, if result is OK or ABORT)
     401           0 :     if( (eResult == DocPasswordVerifierResult_WRONG_PASSWORD) && rxInteractHandler.is() ) try
     402             :     {
     403           0 :         PasswordRequestMode eRequestMode = PasswordRequestMode_PASSWORD_ENTER;
     404           0 :         while( eResult == DocPasswordVerifierResult_WRONG_PASSWORD )
     405             :         {
     406           0 :             DocPasswordRequest* pRequest = new DocPasswordRequest( eRequestType, eRequestMode, rDocumentName );
     407           0 :             Reference< XInteractionRequest > xRequest( pRequest );
     408           0 :             rxInteractHandler->handle( xRequest );
     409           0 :             if( pRequest->isPassword() )
     410             :             {
     411           0 :                 if( !pRequest->getPassword().isEmpty() )
     412           0 :                     eResult = rVerifier.verifyPassword( pRequest->getPassword(), aEncData );
     413             :             }
     414             :             else
     415             :             {
     416           0 :                 eResult = DocPasswordVerifierResult_ABORT;
     417             :             }
     418           0 :             eRequestMode = PasswordRequestMode_PASSWORD_REENTER;
     419           0 :         }
     420             :     }
     421           0 :     catch( Exception& )
     422             :     {
     423             :     }
     424             : 
     425           0 :     return (eResult == DocPasswordVerifierResult_OK) ? aEncData : uno::Sequence< beans::NamedValue >();
     426             : }
     427             : 
     428             : } // namespace comphelper
     429             : 
     430             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10