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