1*d0626817SAndrew Rist /**************************************************************
2*d0626817SAndrew Rist  *
3*d0626817SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*d0626817SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*d0626817SAndrew Rist  * distributed with this work for additional information
6*d0626817SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*d0626817SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*d0626817SAndrew Rist  * "License"); you may not use this file except in compliance
9*d0626817SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*d0626817SAndrew Rist  *
11*d0626817SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*d0626817SAndrew Rist  *
13*d0626817SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*d0626817SAndrew Rist  * software distributed under the License is distributed on an
15*d0626817SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*d0626817SAndrew Rist  * KIND, either express or implied.  See the License for the
17*d0626817SAndrew Rist  * specific language governing permissions and limitations
18*d0626817SAndrew Rist  * under the License.
19*d0626817SAndrew Rist  *
20*d0626817SAndrew Rist  *************************************************************/
21*d0626817SAndrew Rist 
22*d0626817SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include <precompiled_xmlsecurity.hxx>
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include <pk11pub.h>
27cdf0e10cSrcweir #include "digestcontext.hxx"
28cdf0e10cSrcweir 
29cdf0e10cSrcweir using namespace ::com::sun::star;
30cdf0e10cSrcweir 
~ODigestContext()31cdf0e10cSrcweir ODigestContext::~ODigestContext()
32cdf0e10cSrcweir {
33cdf0e10cSrcweir     if ( m_pContext )
34cdf0e10cSrcweir     {
35cdf0e10cSrcweir         PK11_DestroyContext( m_pContext, PR_TRUE );
36cdf0e10cSrcweir         m_pContext = NULL;
37cdf0e10cSrcweir     }
38cdf0e10cSrcweir }
39cdf0e10cSrcweir 
updateDigest(const uno::Sequence<::sal_Int8> & aData)40cdf0e10cSrcweir void SAL_CALL ODigestContext::updateDigest( const uno::Sequence< ::sal_Int8 >& aData )
41cdf0e10cSrcweir     throw (lang::DisposedException, uno::RuntimeException)
42cdf0e10cSrcweir {
43cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
44cdf0e10cSrcweir 
45cdf0e10cSrcweir     if ( m_bBroken )
46cdf0e10cSrcweir         throw uno::RuntimeException();
47cdf0e10cSrcweir 
48cdf0e10cSrcweir     if ( m_bDisposed )
49cdf0e10cSrcweir         throw lang::DisposedException();
50cdf0e10cSrcweir 
51cdf0e10cSrcweir     if ( !m_b1KData || m_nDigested < 1024 )
52cdf0e10cSrcweir     {
53cdf0e10cSrcweir         uno::Sequence< sal_Int8 > aToDigest = aData;
54cdf0e10cSrcweir         if ( m_b1KData && m_nDigested + aData.getLength() > 1024 )
55cdf0e10cSrcweir             aToDigest.realloc( 1024 - m_nDigested );
56cdf0e10cSrcweir 
57cdf0e10cSrcweir         if ( PK11_DigestOp( m_pContext, reinterpret_cast< const unsigned char* >( aToDigest.getConstArray() ), aToDigest.getLength() ) != SECSuccess )
58cdf0e10cSrcweir         {
59cdf0e10cSrcweir             PK11_DestroyContext( m_pContext, PR_TRUE );
60cdf0e10cSrcweir             m_pContext = NULL;
61cdf0e10cSrcweir             m_bBroken = true;
62cdf0e10cSrcweir             throw uno::RuntimeException();
63cdf0e10cSrcweir         }
64cdf0e10cSrcweir 
65cdf0e10cSrcweir         m_nDigested += aToDigest.getLength();
66cdf0e10cSrcweir     }
67cdf0e10cSrcweir }
68cdf0e10cSrcweir 
finalizeDigestAndDispose()69cdf0e10cSrcweir uno::Sequence< ::sal_Int8 > SAL_CALL ODigestContext::finalizeDigestAndDispose()
70cdf0e10cSrcweir     throw (lang::DisposedException, uno::RuntimeException)
71cdf0e10cSrcweir {
72cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
73cdf0e10cSrcweir 
74cdf0e10cSrcweir     if ( m_bBroken )
75cdf0e10cSrcweir         throw uno::RuntimeException();
76cdf0e10cSrcweir 
77cdf0e10cSrcweir     if ( m_bDisposed )
78cdf0e10cSrcweir         throw lang::DisposedException();
79cdf0e10cSrcweir 
80cdf0e10cSrcweir     uno::Sequence< sal_Int8 > aResult( m_nDigestLength );
81cdf0e10cSrcweir     unsigned int nResultLen = 0;
82cdf0e10cSrcweir     if ( PK11_DigestFinal( m_pContext, reinterpret_cast< unsigned char* >( aResult.getArray() ), &nResultLen, aResult.getLength() ) != SECSuccess )
83cdf0e10cSrcweir     {
84cdf0e10cSrcweir         PK11_DestroyContext( m_pContext, PR_TRUE );
85cdf0e10cSrcweir         m_pContext = NULL;
86cdf0e10cSrcweir         m_bBroken = true;
87cdf0e10cSrcweir         throw uno::RuntimeException();
88cdf0e10cSrcweir     }
89cdf0e10cSrcweir 
90cdf0e10cSrcweir     PK11_DestroyContext( m_pContext, PR_TRUE );
91cdf0e10cSrcweir     m_pContext = NULL;
92cdf0e10cSrcweir     m_bDisposed = true;
93cdf0e10cSrcweir 
94cdf0e10cSrcweir     aResult.realloc( nResultLen );
95cdf0e10cSrcweir     return aResult;
96cdf0e10cSrcweir }
97cdf0e10cSrcweir 
98