1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_package.hxx"
26 
27 #include <rtl/cipher.h>
28 #include <rtl/ref.hxx>
29 
30 #include "blowfishcontext.hxx"
31 
32 using namespace ::com::sun::star;
33 
34 // static
Create(const uno::Sequence<sal_Int8> & aDerivedKey,const uno::Sequence<sal_Int8> & aInitVector,bool bEncrypt)35 uno::Reference< xml::crypto::XCipherContext > BlowfishCFB8CipherContext::Create( const uno::Sequence< sal_Int8 >& aDerivedKey, const uno::Sequence< sal_Int8 >& aInitVector, bool bEncrypt )
36 {
37     ::rtl::Reference< BlowfishCFB8CipherContext > xResult = new BlowfishCFB8CipherContext();
38     xResult->m_pCipher = rtl_cipher_create( rtl_Cipher_AlgorithmBF, rtl_Cipher_ModeStream );
39     if ( !xResult->m_pCipher )
40         throw uno::RuntimeException( ::rtl::OUString::createFromAscii( "Can not create cipher!\n" ),
41                                      uno::Reference< XInterface >() );
42 
43     if ( rtl_Cipher_E_None != rtl_cipher_init(
44                                 xResult->m_pCipher,
45                                 bEncrypt ? rtl_Cipher_DirectionEncode : rtl_Cipher_DirectionDecode,
46                                 reinterpret_cast< const sal_uInt8* >( aDerivedKey.getConstArray() ),
47                                 aDerivedKey.getLength(),
48                                 reinterpret_cast< const sal_uInt8* >( aInitVector.getConstArray() ),
49                                 aInitVector.getLength() ) )
50     {
51         throw uno::RuntimeException( ::rtl::OUString::createFromAscii( "Can not initialize cipher!\n" ),
52                                      uno::Reference< XInterface >() );
53     }
54 
55     xResult->m_bEncrypt = bEncrypt;
56 
57     return uno::Reference< xml::crypto::XCipherContext >( xResult.get() );
58 }
59 
~BlowfishCFB8CipherContext()60 BlowfishCFB8CipherContext::~BlowfishCFB8CipherContext()
61 {
62     if ( m_pCipher )
63     {
64 		rtl_cipher_destroy ( m_pCipher );
65         m_pCipher = NULL;
66     }
67 }
68 
convertWithCipherContext(const uno::Sequence<::sal_Int8> & aData)69 uno::Sequence< sal_Int8 > SAL_CALL BlowfishCFB8CipherContext::convertWithCipherContext( const uno::Sequence< ::sal_Int8 >& aData )
70     throw( lang::IllegalArgumentException, lang::DisposedException, uno::RuntimeException )
71 {
72     ::osl::MutexGuard aGuard( m_aMutex );
73     if ( !m_pCipher )
74         throw lang::DisposedException();
75 
76     uno::Sequence< sal_Int8 > aResult( aData.getLength() );
77     rtlCipherError nError = rtl_Cipher_E_None;
78 
79     if ( m_bEncrypt )
80     {
81         rtl_cipher_encode( m_pCipher,
82                           aData.getConstArray(),
83                           aData.getLength(),
84                           reinterpret_cast< sal_uInt8* >( aResult.getArray() ),
85                           aResult.getLength() );
86     }
87     else
88     {
89         rtl_cipher_decode( m_pCipher,
90                           aData.getConstArray(),
91                           aData.getLength(),
92                           reinterpret_cast< sal_uInt8* >( aResult.getArray() ),
93                           aResult.getLength() );
94     }
95 
96     if ( rtl_Cipher_E_None != nError )
97     {
98         throw uno::RuntimeException( ::rtl::OUString::createFromAscii( "Can not decrypt/encrypt with cipher!\n" ),
99                                      uno::Reference< uno::XInterface >() );
100     }
101 
102     return aResult;
103 }
104 
finalizeCipherContextAndDispose()105 uno::Sequence< ::sal_Int8 > SAL_CALL BlowfishCFB8CipherContext::finalizeCipherContextAndDispose()
106     throw( lang::DisposedException, uno::RuntimeException )
107 {
108     ::osl::MutexGuard aGuard( m_aMutex );
109     if ( !m_pCipher )
110         throw lang::DisposedException();
111 
112     rtl_cipher_destroy ( m_pCipher );
113     m_pCipher = NULL;
114 
115     return uno::Sequence< sal_Int8 >();
116 }
117 
118 
119