1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 // MARKER(update_precomp.py): autogen include statement, do not remove 29 #include "precompiled_package.hxx" 30 31 #include <rtl/cipher.h> 32 #include <rtl/ref.hxx> 33 34 #include "blowfishcontext.hxx" 35 36 using namespace ::com::sun::star; 37 38 // static 39 uno::Reference< xml::crypto::XCipherContext > BlowfishCFB8CipherContext::Create( const uno::Sequence< sal_Int8 >& aDerivedKey, const uno::Sequence< sal_Int8 >& aInitVector, bool bEncrypt ) 40 { 41 ::rtl::Reference< BlowfishCFB8CipherContext > xResult = new BlowfishCFB8CipherContext(); 42 xResult->m_pCipher = rtl_cipher_create( rtl_Cipher_AlgorithmBF, rtl_Cipher_ModeStream ); 43 if ( !xResult->m_pCipher ) 44 throw uno::RuntimeException( ::rtl::OUString::createFromAscii( "Can not create cipher!\n" ), 45 uno::Reference< XInterface >() ); 46 47 if ( rtl_Cipher_E_None != rtl_cipher_init( 48 xResult->m_pCipher, 49 bEncrypt ? rtl_Cipher_DirectionEncode : rtl_Cipher_DirectionDecode, 50 reinterpret_cast< const sal_uInt8* >( aDerivedKey.getConstArray() ), 51 aDerivedKey.getLength(), 52 reinterpret_cast< const sal_uInt8* >( aInitVector.getConstArray() ), 53 aInitVector.getLength() ) ) 54 { 55 throw uno::RuntimeException( ::rtl::OUString::createFromAscii( "Can not initialize cipher!\n" ), 56 uno::Reference< XInterface >() ); 57 } 58 59 xResult->m_bEncrypt = bEncrypt; 60 61 return uno::Reference< xml::crypto::XCipherContext >( xResult.get() ); 62 } 63 64 BlowfishCFB8CipherContext::~BlowfishCFB8CipherContext() 65 { 66 if ( m_pCipher ) 67 { 68 rtl_cipher_destroy ( m_pCipher ); 69 m_pCipher = NULL; 70 } 71 } 72 73 uno::Sequence< sal_Int8 > SAL_CALL BlowfishCFB8CipherContext::convertWithCipherContext( const uno::Sequence< ::sal_Int8 >& aData ) 74 throw( lang::IllegalArgumentException, lang::DisposedException, uno::RuntimeException ) 75 { 76 ::osl::MutexGuard aGuard( m_aMutex ); 77 if ( !m_pCipher ) 78 throw lang::DisposedException(); 79 80 uno::Sequence< sal_Int8 > aResult( aData.getLength() ); 81 rtlCipherError nError = rtl_Cipher_E_None; 82 83 if ( m_bEncrypt ) 84 { 85 rtl_cipher_encode( m_pCipher, 86 aData.getConstArray(), 87 aData.getLength(), 88 reinterpret_cast< sal_uInt8* >( aResult.getArray() ), 89 aResult.getLength() ); 90 } 91 else 92 { 93 rtl_cipher_decode( m_pCipher, 94 aData.getConstArray(), 95 aData.getLength(), 96 reinterpret_cast< sal_uInt8* >( aResult.getArray() ), 97 aResult.getLength() ); 98 } 99 100 if ( rtl_Cipher_E_None != nError ) 101 { 102 throw uno::RuntimeException( ::rtl::OUString::createFromAscii( "Can not decrypt/encrypt with cipher!\n" ), 103 uno::Reference< uno::XInterface >() ); 104 } 105 106 return aResult; 107 } 108 109 uno::Sequence< ::sal_Int8 > SAL_CALL BlowfishCFB8CipherContext::finalizeCipherContextAndDispose() 110 throw( lang::DisposedException, uno::RuntimeException ) 111 { 112 ::osl::MutexGuard aGuard( m_aMutex ); 113 if ( !m_pCipher ) 114 throw lang::DisposedException(); 115 116 rtl_cipher_destroy ( m_pCipher ); 117 m_pCipher = NULL; 118 119 return uno::Sequence< sal_Int8 >(); 120 } 121 122 123