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 #include <precompiled_xmlsecurity.hxx>
25 
26 #include <pk11pub.h>
27 #include "digestcontext.hxx"
28 
29 using namespace ::com::sun::star;
30 
~ODigestContext()31 ODigestContext::~ODigestContext()
32 {
33     if ( m_pContext )
34     {
35         PK11_DestroyContext( m_pContext, PR_TRUE );
36         m_pContext = NULL;
37     }
38 }
39 
updateDigest(const uno::Sequence<::sal_Int8> & aData)40 void SAL_CALL ODigestContext::updateDigest( const uno::Sequence< ::sal_Int8 >& aData )
41     throw (lang::DisposedException, uno::RuntimeException)
42 {
43     ::osl::MutexGuard aGuard( m_aMutex );
44 
45     if ( m_bBroken )
46         throw uno::RuntimeException();
47 
48     if ( m_bDisposed )
49         throw lang::DisposedException();
50 
51     if ( !m_b1KData || m_nDigested < 1024 )
52     {
53         uno::Sequence< sal_Int8 > aToDigest = aData;
54         if ( m_b1KData && m_nDigested + aData.getLength() > 1024 )
55             aToDigest.realloc( 1024 - m_nDigested );
56 
57         if ( PK11_DigestOp( m_pContext, reinterpret_cast< const unsigned char* >( aToDigest.getConstArray() ), aToDigest.getLength() ) != SECSuccess )
58         {
59             PK11_DestroyContext( m_pContext, PR_TRUE );
60             m_pContext = NULL;
61             m_bBroken = true;
62             throw uno::RuntimeException();
63         }
64 
65         m_nDigested += aToDigest.getLength();
66     }
67 }
68 
finalizeDigestAndDispose()69 uno::Sequence< ::sal_Int8 > SAL_CALL ODigestContext::finalizeDigestAndDispose()
70     throw (lang::DisposedException, uno::RuntimeException)
71 {
72     ::osl::MutexGuard aGuard( m_aMutex );
73 
74     if ( m_bBroken )
75         throw uno::RuntimeException();
76 
77     if ( m_bDisposed )
78         throw lang::DisposedException();
79 
80     uno::Sequence< sal_Int8 > aResult( m_nDigestLength );
81     unsigned int nResultLen = 0;
82     if ( PK11_DigestFinal( m_pContext, reinterpret_cast< unsigned char* >( aResult.getArray() ), &nResultLen, aResult.getLength() ) != SECSuccess )
83     {
84         PK11_DestroyContext( m_pContext, PR_TRUE );
85         m_pContext = NULL;
86         m_bBroken = true;
87         throw uno::RuntimeException();
88     }
89 
90     PK11_DestroyContext( m_pContext, PR_TRUE );
91     m_pContext = NULL;
92     m_bDisposed = true;
93 
94     aResult.realloc( nResultLen );
95     return aResult;
96 }
97 
98