| 1 |
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 |
/* |
| 3 |
* This file is part of the LibreOffice project. |
| 4 |
* |
| 5 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 6 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 |
* |
| 9 |
* This file incorporates work covered by the following license notice: |
| 10 |
* |
| 11 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
| 12 |
* contributor license agreements. See the NOTICE file distributed |
| 13 |
* with this work for additional information regarding copyright |
| 14 |
* ownership. The ASF licenses this file to you under the Apache |
| 15 |
* License, Version 2.0 (the "License"); you may not use this file |
| 16 |
* except in compliance with the License. You may obtain a copy of |
| 17 |
* the License at http://www.apache.org/licenses/LICENSE-2.0 . |
| 18 |
*/ |
| 19 |
|
| 20 |
|
| 21 |
#include <framework/signatureengine.hxx> |
| 22 |
#include <framework/xmlsignaturetemplateimpl.hxx> |
| 23 |
#include <rtl/ref.hxx> |
| 24 |
|
| 25 |
namespace com::sun::star::xml::wrapper { class XXMLElementWrapper; } |
| 26 |
|
| 27 |
using namespace com::sun::star::uno; |
| 28 |
|
| 29 |
SignatureEngine::SignatureEngine() |
| 30 |
: m_nTotalReferenceNumber(-1) |
| 31 |
{ |
| 32 |
} |
| 33 |
|
| 34 |
bool SignatureEngine::checkReady() const |
| 35 |
/****** SignatureEngine/checkReady ******************************************* |
| 36 |
* |
| 37 |
* NAME |
| 38 |
* checkReady -- checks the conditions for the main operation. |
| 39 |
* |
| 40 |
* SYNOPSIS |
| 41 |
* bReady = checkReady( ); |
| 42 |
* |
| 43 |
* FUNCTION |
| 44 |
* checks whether all following conditions are satisfied: |
| 45 |
* 1. the main operation has't begun yet; |
| 46 |
* 2. the key material is known; |
| 47 |
* 3. the amount of reference is known; |
| 48 |
* 4. all of referenced elements, the key element and the signature |
| 49 |
* template are buffered. |
| 50 |
* |
| 51 |
* RESULT |
| 52 |
* bReady - true if all conditions are satisfied, false otherwise |
| 53 |
******************************************************************************/ |
| 54 |
{ |
| 55 |
bool rc = true; |
| 56 |
|
| 57 |
sal_Int32 nKeyInc = 0; |
| 58 |
if (m_nIdOfKeyEC != 0) |
| 59 |
{ |
| 60 |
nKeyInc = 1; |
| 61 |
} |
| 62 |
|
| 63 |
if (m_bMissionDone || |
| 64 |
m_nIdOfKeyEC == -1 || |
| 65 |
m_nTotalReferenceNumber == -1 || |
| 66 |
m_nTotalReferenceNumber+1+nKeyInc > m_nNumOfResolvedReferences) |
| 67 |
{ |
| 68 |
rc = false; |
| 69 |
} |
| 70 |
|
| 71 |
return rc; |
| 72 |
} |
| 73 |
|
| 74 |
void SignatureEngine::tryToPerform( ) |
| 75 |
/****** SignatureEngine/tryToPerform ***************************************** |
| 76 |
* |
| 77 |
* NAME |
| 78 |
* tryToPerform -- tries to perform the signature operation. |
| 79 |
* |
| 80 |
* FUNCTION |
| 81 |
* if the situation is ready, perform following operations. |
| 82 |
* 1. prepares a signature template; |
| 83 |
* 2. calls the signature bridge component; |
| 84 |
* 3. clears up all used resources; |
| 85 |
* 4. notifies the result listener; |
| 86 |
* 5. sets the "accomplishment" flag. |
| 87 |
******************************************************************************/ |
| 88 |
{ |
| 89 |
if (!checkReady()) |
| 90 |
return; |
| 91 |
|
| 92 |
rtl::Reference<XMLSignatureTemplateImpl> xSignatureTemplate = new XMLSignatureTemplateImpl(); |
| 93 |
|
| 94 |
css::uno::Reference< css::xml::wrapper::XXMLElementWrapper > |
| 95 |
xXMLElement = m_xSAXEventKeeper->getElement( m_nIdOfTemplateEC ); |
| 96 |
|
| 97 |
xSignatureTemplate->setTemplate(xXMLElement); |
| 98 |
|
| 99 |
for( const auto i : m_vReferenceIds ) |
| 100 |
{ |
| 101 |
xXMLElement = m_xSAXEventKeeper->getElement( i ); |
| 102 |
xSignatureTemplate->setTarget(xXMLElement); |
| 103 |
} |
| 104 |
|
| 105 |
/* |
| 106 |
* set the Uri binding |
| 107 |
*/ |
| 108 |
xSignatureTemplate->setBinding( this ); |
| 109 |
|
| 110 |
startEngine(xSignatureTemplate); |
| 111 |
|
| 112 |
/* |
| 113 |
* done |
| 114 |
*/ |
| 115 |
clearUp( ); |
| 116 |
|
| 117 |
notifyResultListener(); |
| 118 |
|
| 119 |
m_bMissionDone = true; |
| 120 |
} |
| 121 |
|
| 122 |
void SignatureEngine::clearUp( ) const |
| 123 |
/****** SignatureEngine/clearUp ********************************************** |
| 124 |
* |
| 125 |
* NAME |
| 126 |
* clearUp -- clear up all resources used by this operation. |
| 127 |
* |
| 128 |
* FUNCTION |
| 129 |
* cleaning resources up includes: |
| 130 |
* 1. releases the ElementCollector for the signature template element; |
| 131 |
* 2. releases ElementCollectors for referenced elements; |
| 132 |
* 3. releases the ElementCollector for the key element, if there is one. |
| 133 |
******************************************************************************/ |
| 134 |
{ |
| 135 |
css::uno::Reference < css::xml::crypto::sax::XReferenceResolvedBroadcaster > |
| 136 |
xReferenceResolvedBroadcaster( m_xSAXEventKeeper, css::uno::UNO_QUERY ); |
| 137 |
xReferenceResolvedBroadcaster->removeReferenceResolvedListener( |
| 138 |
m_nIdOfTemplateEC, |
| 139 |
static_cast<const css::uno::Reference < css::xml::crypto::sax::XReferenceResolvedListener > >(static_cast<SecurityEngine *>(const_cast<SignatureEngine *>(this)))); |
| 140 |
|
| 141 |
m_xSAXEventKeeper->removeElementCollector(m_nIdOfTemplateEC); |
| 142 |
|
| 143 |
for( const auto& i : m_vReferenceIds ) |
| 144 |
{ |
| 145 |
xReferenceResolvedBroadcaster->removeReferenceResolvedListener( |
| 146 |
i, |
| 147 |
static_cast<const css::uno::Reference < css::xml::crypto::sax::XReferenceResolvedListener > >(static_cast<SecurityEngine *>(const_cast<SignatureEngine *>(this)))); |
| 148 |
m_xSAXEventKeeper->removeElementCollector(i); |
| 149 |
} |
| 150 |
|
| 151 |
if (m_nIdOfKeyEC != 0 && m_nIdOfKeyEC != -1) |
| 152 |
{ |
| 153 |
m_xSAXEventKeeper->removeElementCollector(m_nIdOfKeyEC); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
/* XReferenceCollector */ |
| 158 |
void SAL_CALL SignatureEngine::setReferenceCount( sal_Int32 count ) |
| 159 |
{ |
| 160 |
m_nTotalReferenceNumber = count; |
| 161 |
tryToPerform(); |
| 162 |
} |
| 163 |
|
| 164 |
void SAL_CALL SignatureEngine::setReferenceId( sal_Int32 id ) |
| 165 |
{ |
| 166 |
m_vReferenceIds.push_back( id ); |
| 167 |
} |
| 168 |
|
| 169 |
/* XUriBinding */ |
| 170 |
void SAL_CALL SignatureEngine::setUriBinding( |
| 171 |
const OUString& uri, |
| 172 |
const css::uno::Reference< css::io::XInputStream >& aInputStream ) |
| 173 |
{ |
| 174 |
m_vUris.push_back(uri); |
| 175 |
m_vXInputStreams.push_back(aInputStream); |
| 176 |
} |
| 177 |
|
| 178 |
css::uno::Reference< css::io::XInputStream > SAL_CALL SignatureEngine::getUriBinding( const OUString& uri ) |
| 179 |
{ |
| 180 |
css::uno::Reference< css::io::XInputStream > xInputStream; |
| 181 |
|
| 182 |
int size = m_vUris.size(); |
| 183 |
|
| 184 |
for( int i=0; i<size; ++i) |
| 185 |
{ |
| 186 |
if (m_vUris[i] == uri) |
| 187 |
{ |
| 188 |
xInputStream = m_vXInputStreams[i]; |
| 189 |
break; |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
return xInputStream; |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |