xref: /trunk/main/xmlsecurity/source/xmlsec/nss/digestcontext.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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 {
42     ::osl::MutexGuard aGuard( m_aMutex );
43 
44     if ( m_bBroken )
45         throw uno::RuntimeException();
46 
47     if ( m_bDisposed )
48         throw lang::DisposedException();
49 
50     if ( !m_b1KData || m_nDigested < 1024 )
51     {
52         uno::Sequence< sal_Int8 > aToDigest = aData;
53         if ( m_b1KData && m_nDigested + aData.getLength() > 1024 )
54             aToDigest.realloc( 1024 - m_nDigested );
55 
56         if ( PK11_DigestOp( m_pContext, reinterpret_cast< const unsigned char* >( aToDigest.getConstArray() ), aToDigest.getLength() ) != SECSuccess )
57         {
58             PK11_DestroyContext( m_pContext, PR_TRUE );
59             m_pContext = NULL;
60             m_bBroken = true;
61             throw uno::RuntimeException();
62         }
63 
64         m_nDigested += aToDigest.getLength();
65     }
66 }
67 
finalizeDigestAndDispose()68 uno::Sequence< ::sal_Int8 > SAL_CALL ODigestContext::finalizeDigestAndDispose()
69 {
70     ::osl::MutexGuard aGuard( m_aMutex );
71 
72     if ( m_bBroken )
73         throw uno::RuntimeException();
74 
75     if ( m_bDisposed )
76         throw lang::DisposedException();
77 
78     uno::Sequence< sal_Int8 > aResult( m_nDigestLength );
79     unsigned int nResultLen = 0;
80     if ( PK11_DigestFinal( m_pContext, reinterpret_cast< unsigned char* >( aResult.getArray() ), &nResultLen, aResult.getLength() ) != SECSuccess )
81     {
82         PK11_DestroyContext( m_pContext, PR_TRUE );
83         m_pContext = NULL;
84         m_bBroken = true;
85         throw uno::RuntimeException();
86     }
87 
88     PK11_DestroyContext( m_pContext, PR_TRUE );
89     m_pContext = NULL;
90     m_bDisposed = true;
91 
92     aResult.realloc( nResultLen );
93     return aResult;
94 }
95