| 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 |
#include <resourcemanager.hxx> |
| 21 |
|
| 22 |
#include <certificate.hxx> |
| 23 |
#include <certificatechooser.hxx> |
| 24 |
#include <certificateviewer.hxx> |
| 25 |
#include <digitalsignaturesdialog.hxx> |
| 26 |
#include <macrosecurity.hxx> |
| 27 |
#include <biginteger.hxx> |
| 28 |
#include <strings.hrc> |
| 29 |
#include <pdfsignaturehelper.hxx> |
| 30 |
|
| 31 |
#include <com/sun/star/embed/XStorage.hpp> |
| 32 |
#include <com/sun/star/embed/StorageFormats.hpp> |
| 33 |
#include <com/sun/star/embed/XTransactedObject.hpp> |
| 34 |
#include <com/sun/star/embed/ElementModes.hpp> |
| 35 |
#include <com/sun/star/lang/XInitialization.hpp> |
| 36 |
#include <com/sun/star/lang/XServiceInfo.hpp> |
| 37 |
#include <com/sun/star/uno/SecurityException.hpp> |
| 38 |
#include <vcl/svapp.hxx> |
| 39 |
#include <tools/time.hxx> |
| 40 |
#include <unotools/securityoptions.hxx> |
| 41 |
#include <com/sun/star/security/CertificateValidity.hpp> |
| 42 |
#include <com/sun/star/security/CertificateKind.hpp> |
| 43 |
#include <comphelper/base64.hxx> |
| 44 |
#include <comphelper/documentconstants.hxx> |
| 45 |
#include <comphelper/propertyvalue.hxx> |
| 46 |
#include <comphelper/sequence.hxx> |
| 47 |
#include <cppuhelper/implbase.hxx> |
| 48 |
#include <comphelper/xmlsechelper.hxx> |
| 49 |
#include <cppuhelper/supportsservice.hxx> |
| 50 |
#include <sal/log.hxx> |
| 51 |
#include <com/sun/star/lang/IllegalArgumentException.hpp> |
| 52 |
#include <com/sun/star/security/XDocumentDigitalSignatures.hpp> |
| 53 |
#include <com/sun/star/xml/crypto/XXMLSecurityContext.hpp> |
| 54 |
#include <sfx2/digitalsignatures.hxx> |
| 55 |
#include <svl/cryptosign.hxx> |
| 56 |
|
| 57 |
#include <map> |
| 58 |
|
| 59 |
using namespace css; |
| 60 |
using namespace css::uno; |
| 61 |
using namespace css::lang; |
| 62 |
using namespace css::security; |
| 63 |
using namespace css::xml::crypto; |
| 64 |
|
| 65 |
namespace { |
| 66 |
class DocumentDigitalSignatures |
| 67 |
: public cppu::WeakImplHelper<css::security::XDocumentDigitalSignatures, |
| 68 |
css::lang::XInitialization, css::lang::XServiceInfo>, |
| 69 |
public sfx2::DigitalSignatures |
| 70 |
{ |
| 71 |
private: |
| 72 |
css::uno::Reference<css::uno::XComponentContext> mxCtx; |
| 73 |
css::uno::Reference<css::awt::XWindow> mxParentWindow; |
| 74 |
uno::Reference<io::XStream> mxScriptingSignStream; |
| 75 |
|
| 76 |
/// will be set by XInitialization. If not we assume true. false means an earlier version (whatever that means, |
| 77 |
/// this is a string, not a boolean). |
| 78 |
/// Note that the code talks about "ODF version" even if this class is also used to sign OOXML. |
| 79 |
OUString m_sODFVersion; |
| 80 |
/// The number of arguments which were passed in XInitialization::initialize |
| 81 |
int m_nArgumentsCount; |
| 82 |
/// Indicates if the document already contains a document signature |
| 83 |
bool m_bHasDocumentSignature; |
| 84 |
|
| 85 |
/// @throws css::uno::RuntimeException |
| 86 |
void ImplViewSignatures(const css::uno::Reference<css::embed::XStorage>& rxStorage, |
| 87 |
const css::uno::Reference<css::io::XStream>& xSignStream, |
| 88 |
DocumentSignatureMode eMode, bool bReadOnly, |
| 89 |
SfxViewShell* pViewShell, |
| 90 |
const std::function<void(bool)>& rCallback); |
| 91 |
/// @throws css::uno::RuntimeException |
| 92 |
void ImplViewSignatures(const css::uno::Reference<css::embed::XStorage>& rxStorage, |
| 93 |
const css::uno::Reference<css::io::XInputStream>& xSignStream, |
| 94 |
DocumentSignatureMode eMode, bool bReadOnly); |
| 95 |
/// @throws css::uno::RuntimeException |
| 96 |
css::uno::Sequence<css::security::DocumentSignatureInformation> |
| 97 |
ImplVerifySignatures(const css::uno::Reference<css::embed::XStorage>& rxStorage, |
| 98 |
const css::uno::Reference<css::io::XInputStream>& xSignStream, |
| 99 |
DocumentSignatureMode eMode); |
| 100 |
|
| 101 |
css::uno::Sequence<css::uno::Reference<css::security::XCertificate>> |
| 102 |
chooseCertificatesImpl(SfxViewShell* pViewShell, std::map<OUString, OUString>& rProperties, const CertificateChooserUserAction eAction, |
| 103 |
const CertificateKind certificateKind=CertificateKind_NONE); |
| 104 |
|
| 105 |
bool |
| 106 |
signWithCertificateImpl(const uno::Reference<frame::XModel>& /*xModel*/, |
| 107 |
svl::crypto::SigningContext& rSigningContext, |
| 108 |
css::uno::Reference<css::embed::XStorage> const& xStorage, |
| 109 |
css::uno::Reference<css::io::XStream> const& xStream, |
| 110 |
DocumentSignatureMode eMode); |
| 111 |
|
| 112 |
public: |
| 113 |
explicit DocumentDigitalSignatures( |
| 114 |
const css::uno::Reference<css::uno::XComponentContext>& rxCtx); |
| 115 |
|
| 116 |
//XInitialization |
| 117 |
void SAL_CALL initialize(const css::uno::Sequence<css::uno::Any>& aArguments) override; |
| 118 |
|
| 119 |
OUString SAL_CALL getImplementationName() override; |
| 120 |
|
| 121 |
sal_Bool SAL_CALL supportsService(OUString const& ServiceName) override; |
| 122 |
|
| 123 |
css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override; |
| 124 |
|
| 125 |
// XDocumentDigitalSignatures |
| 126 |
sal_Bool SAL_CALL signSignatureLine( |
| 127 |
const css::uno::Reference<css::embed::XStorage>& Storage, |
| 128 |
const css::uno::Reference<css::io::XStream>& xSignStream, const OUString& aSignatureLineId, |
| 129 |
const Reference<css::security::XCertificate>& xCertificate, |
| 130 |
const Reference<css::graphic::XGraphic>& xValidGraphic, |
| 131 |
const Reference<css::graphic::XGraphic>& xInvalidGraphic, |
| 132 |
const OUString& aComment) override; |
| 133 |
css::uno::Sequence<css::security::DocumentSignatureInformation> |
| 134 |
SAL_CALL verifyDocumentContentSignatures( |
| 135 |
const css::uno::Reference<css::embed::XStorage>& xStorage, |
| 136 |
const css::uno::Reference<css::io::XInputStream>& xSignInStream) override; |
| 137 |
void SAL_CALL showDocumentContentSignatures( |
| 138 |
const css::uno::Reference<css::embed::XStorage>& xStorage, |
| 139 |
const css::uno::Reference<css::io::XInputStream>& xSignInStream) override; |
| 140 |
OUString SAL_CALL getDocumentContentSignatureDefaultStreamName() override; |
| 141 |
css::uno::Sequence<css::security::DocumentSignatureInformation> |
| 142 |
SAL_CALL verifyScriptingContentSignatures( |
| 143 |
const css::uno::Reference<css::embed::XStorage>& xStorage, |
| 144 |
const css::uno::Reference<css::io::XInputStream>& xSignInStream) override; |
| 145 |
void SAL_CALL showScriptingContentSignatures( |
| 146 |
const css::uno::Reference<css::embed::XStorage>& xStorage, |
| 147 |
const css::uno::Reference<css::io::XInputStream>& xSignInStream) override; |
| 148 |
OUString SAL_CALL getScriptingContentSignatureDefaultStreamName() override; |
| 149 |
void SAL_CALL |
| 150 |
showCertificate(const css::uno::Reference<css::security::XCertificate>& Certificate) override; |
| 151 |
void SAL_CALL manageTrustedSources() override; |
| 152 |
sal_Bool SAL_CALL |
| 153 |
isAuthorTrusted(const css::uno::Reference<css::security::XCertificate>& Author) override; |
| 154 |
sal_Bool SAL_CALL isLocationTrusted(const OUString& Location) override; |
| 155 |
void SAL_CALL addAuthorToTrustedSources( |
| 156 |
const css::uno::Reference<css::security::XCertificate>& Author) override; |
| 157 |
void SAL_CALL addLocationToTrustedSources(const OUString& Location) override; |
| 158 |
|
| 159 |
css::uno::Reference<css::security::XCertificate> |
| 160 |
SAL_CALL chooseCertificate(OUString& rDescription) override; |
| 161 |
css::uno::Reference<css::security::XCertificate> |
| 162 |
SAL_CALL chooseSigningCertificate(OUString& rDescription) override; |
| 163 |
css::uno::Reference<css::security::XCertificate> |
| 164 |
SAL_CALL selectSigningCertificate(OUString& rDescription) override; |
| 165 |
css::uno::Reference<css::security::XCertificate> |
| 166 |
SAL_CALL selectSigningCertificateWithType(const CertificateKind certificateKind, |
| 167 |
OUString& rDescription) override; |
| 168 |
css::uno::Sequence<css::uno::Reference<css::security::XCertificate>> |
| 169 |
SAL_CALL chooseEncryptionCertificate(const CertificateKind certificateKind) override; |
| 170 |
css::uno::Reference<css::security::XCertificate> SAL_CALL chooseCertificateWithProps( |
| 171 |
css::uno::Sequence<css::beans::PropertyValue>& Properties) override; |
| 172 |
|
| 173 |
sal_Bool SAL_CALL signDocumentWithCertificate( |
| 174 |
css::uno::Reference<css::security::XCertificate> const & xCertificate, |
| 175 |
css::uno::Reference<css::embed::XStorage> const & xStoragexStorage, |
| 176 |
css::uno::Reference<css::io::XStream> const & xStream) override; |
| 177 |
|
| 178 |
sal_Bool SAL_CALL trustUntrustedCertificate( |
| 179 |
css::uno::Reference<css::security::XCertificate> const& xCertificate) override; |
| 180 |
|
| 181 |
sal_Bool SAL_CALL signScriptingContentWithCertificate( |
| 182 |
css::uno::Reference<css::security::XCertificate> const& xCertificate, |
| 183 |
css::uno::Reference<css::embed::XStorage> const& xStoragexStorage, |
| 184 |
css::uno::Reference<css::io::XStream> const& xStream) override; |
| 185 |
|
| 186 |
void SAL_CALL setParentWindow(const css::uno::Reference<css::awt::XWindow>& rParentwindow) override |
| 187 |
{ |
| 188 |
mxParentWindow = rParentwindow; |
| 189 |
} |
| 190 |
|
| 191 |
/// See sfx2::DigitalSignatures::SignModelWithCertificate(). |
| 192 |
bool |
| 193 |
SignModelWithCertificate(const css::uno::Reference<css::frame::XModel>& xModel, |
| 194 |
svl::crypto::SigningContext& rSigningContext, |
| 195 |
const css::uno::Reference<css::embed::XStorage>& xStorage, |
| 196 |
const css::uno::Reference<css::io::XStream>& xStream) override; |
| 197 |
/// See sfx2::DigitalSignatures::SignDocumentContentAsync(). |
| 198 |
void SignDocumentContentAsync(const css::uno::Reference<css::embed::XStorage>& xStorage, |
| 199 |
const css::uno::Reference<css::io::XStream>& xSignStream, |
| 200 |
SfxViewShell* pViewShell, |
| 201 |
const std::function<void(bool)>& rCallback) override; |
| 202 |
/// See sfx2::DigitalSignatures::SignScriptingContentAsync(). |
| 203 |
void SignScriptingContentAsync(const css::uno::Reference<css::embed::XStorage>& xStorage, |
| 204 |
const css::uno::Reference<css::io::XStream>& xSignStream, |
| 205 |
const std::function<void(bool)>& rCallback) override; |
| 206 |
|
| 207 |
/// See sfx2::DigitalSignatures::SetSignScriptingContent(). |
| 208 |
void SetSignScriptingContent( |
| 209 |
const css::uno::Reference<css::io::XStream>& xScriptingSignStream) override; |
| 210 |
|
| 211 |
/// See sfx2::DigitalSignatures::SelectSigningCertificateWithType(). |
| 212 |
css::uno::Reference<css::security::XCertificate> |
| 213 |
SelectSigningCertificateWithType(SfxViewShell* pViewShell, |
| 214 |
const css::security::CertificateKind certificateKind, |
| 215 |
OUString& rDescription) override; |
| 216 |
}; |
| 217 |
|
| 218 |
} |
| 219 |
|
| 220 |
DocumentDigitalSignatures::DocumentDigitalSignatures( const Reference< XComponentContext >& rxCtx ): |
| 221 |
mxCtx(rxCtx), |
| 222 |
m_sODFVersion(ODFVER_014_TEXT), |
| 223 |
m_nArgumentsCount(0), |
| 224 |
m_bHasDocumentSignature(false) |
| 225 |
{ |
| 226 |
} |
| 227 |
|
| 228 |
void DocumentDigitalSignatures::initialize( const Sequence< Any >& aArguments) |
| 229 |
{ |
| 230 |
if (aArguments.getLength() > 2) |
| 231 |
throw css::lang::IllegalArgumentException( |
| 232 |
u"DocumentDigitalSignatures::initialize requires zero, one, or two arguments"_ustr, |
| 233 |
static_cast<XInitialization*>(this), 0); |
| 234 |
|
| 235 |
m_nArgumentsCount = aArguments.getLength(); |
| 236 |
|
| 237 |
if (!aArguments.hasElements()) |
| 238 |
return; |
| 239 |
|
| 240 |
if (!(aArguments[0] >>= m_sODFVersion)) |
| 241 |
throw css::lang::IllegalArgumentException( |
| 242 |
u"DocumentDigitalSignatures::initialize: the first arguments must be a string"_ustr, |
| 243 |
static_cast<XInitialization*>(this), 0); |
| 244 |
|
| 245 |
if (aArguments.getLength() == 2 |
| 246 |
&& !(aArguments[1] >>= m_bHasDocumentSignature)) |
| 247 |
throw css::lang::IllegalArgumentException( |
| 248 |
u"DocumentDigitalSignatures::initialize: the second arguments must be a bool"_ustr, |
| 249 |
static_cast<XInitialization*>(this), 1); |
| 250 |
|
| 251 |
//the Version is supported as of ODF1.2, so for and 1.1 document or older we will receive the |
| 252 |
//an empty string. In this case we set it to ODFVER_010_TEXT. Then we can later check easily |
| 253 |
//if initialize was called. Only then m_sODFVersion.getLength() is greater than 0 |
| 254 |
if (m_sODFVersion.isEmpty()) |
| 255 |
m_sODFVersion = ODFVER_010_TEXT; |
| 256 |
} |
| 257 |
|
| 258 |
OUString DocumentDigitalSignatures::getImplementationName() |
| 259 |
{ |
| 260 |
return u"com.sun.star.security.DocumentDigitalSignatures"_ustr; |
| 261 |
} |
| 262 |
|
| 263 |
sal_Bool DocumentDigitalSignatures::supportsService( |
| 264 |
OUString const & ServiceName) |
| 265 |
{ |
| 266 |
return cppu::supportsService(this, ServiceName); |
| 267 |
} |
| 268 |
|
| 269 |
css::uno::Sequence<OUString> |
| 270 |
DocumentDigitalSignatures::getSupportedServiceNames() |
| 271 |
{ |
| 272 |
Sequence<OUString> aRet{ u"com.sun.star.security.DocumentDigitalSignatures"_ustr }; |
| 273 |
return aRet; |
| 274 |
} |
| 275 |
|
| 276 |
sal_Bool DocumentDigitalSignatures::signSignatureLine( |
| 277 |
const Reference<css::embed::XStorage>& rxStorage, |
| 278 |
const Reference<css::io::XStream>& xSignStream, |
| 279 |
const OUString& aSignatureLineId, |
| 280 |
const Reference<css::security::XCertificate>& xCertificate, |
| 281 |
const Reference<css::graphic::XGraphic>& xValidGraphic, |
| 282 |
const Reference<css::graphic::XGraphic>& xInvalidGraphic, |
| 283 |
const OUString& aComment) |
| 284 |
{ |
| 285 |
OSL_ENSURE(!m_sODFVersion.isEmpty(), |
| 286 |
"DocumentDigitalSignatures: ODF Version not set, assuming minimum 1.2"); |
| 287 |
|
| 288 |
DocumentSignatureManager aSignatureManager(mxCtx, DocumentSignatureMode::Content); |
| 289 |
|
| 290 |
if (!aSignatureManager.init()) |
| 291 |
return false; |
| 292 |
|
| 293 |
aSignatureManager.setStore(rxStorage); |
| 294 |
aSignatureManager.getSignatureHelper().SetStorage(rxStorage, m_sODFVersion); |
| 295 |
aSignatureManager.setSignatureStream(xSignStream); |
| 296 |
|
| 297 |
Reference<XXMLSecurityContext> xSecurityContext; |
| 298 |
Reference<XServiceInfo> xServiceInfo(xCertificate, UNO_QUERY); |
| 299 |
if (xServiceInfo->getImplementationName() |
| 300 |
== "com.sun.star.xml.security.gpg.XCertificate_GpgImpl") |
| 301 |
xSecurityContext = aSignatureManager.getGpgSecurityContext(); |
| 302 |
else |
| 303 |
xSecurityContext = aSignatureManager.getSecurityContext(); |
| 304 |
|
| 305 |
sal_Int32 nSecurityId; |
| 306 |
svl::crypto::SigningContext aSigningContext; |
| 307 |
aSigningContext.m_xCertificate = xCertificate; |
| 308 |
bool bSuccess = aSignatureManager.add(aSigningContext, xSecurityContext, aComment, nSecurityId, |
| 309 |
true, aSignatureLineId, xValidGraphic, xInvalidGraphic); |
| 310 |
if (!bSuccess) |
| 311 |
return false; |
| 312 |
|
| 313 |
// Need to have this to verify the signature |
| 314 |
aSignatureManager.read(/*bUseTempStream=*/true, /*bCacheLastSignature=*/false); |
| 315 |
aSignatureManager.write(true); |
| 316 |
|
| 317 |
if (rxStorage.is() && !xSignStream.is()) |
| 318 |
{ |
| 319 |
uno::Reference<embed::XTransactedObject> xTrans(rxStorage, uno::UNO_QUERY); |
| 320 |
xTrans->commit(); |
| 321 |
} |
| 322 |
|
| 323 |
return true; |
| 324 |
} |
| 325 |
|
| 326 |
Sequence< css::security::DocumentSignatureInformation > |
| 327 |
DocumentDigitalSignatures::verifyDocumentContentSignatures( |
| 328 |
const Reference< css::embed::XStorage >& rxStorage, |
| 329 |
const Reference< css::io::XInputStream >& xSignInStream ) |
| 330 |
{ |
| 331 |
OSL_ENSURE(!m_sODFVersion.isEmpty(),"DocumentDigitalSignatures: ODF Version not set, assuming minimum 1.2"); |
| 332 |
return ImplVerifySignatures( rxStorage, xSignInStream, DocumentSignatureMode::Content ); |
| 333 |
} |
| 334 |
|
| 335 |
void DocumentDigitalSignatures::showDocumentContentSignatures( |
| 336 |
const Reference< css::embed::XStorage >& rxStorage, |
| 337 |
const Reference< css::io::XInputStream >& xSignInStream ) |
| 338 |
{ |
| 339 |
OSL_ENSURE(!m_sODFVersion.isEmpty(),"DocumentDigitalSignatures: ODF Version not set, assuming minimum 1.2"); |
| 340 |
ImplViewSignatures( rxStorage, xSignInStream, DocumentSignatureMode::Content, true ); |
| 341 |
} |
| 342 |
|
| 343 |
OUString DocumentDigitalSignatures::getDocumentContentSignatureDefaultStreamName() |
| 344 |
{ |
| 345 |
return DocumentSignatureHelper::GetDocumentContentSignatureDefaultStreamName(); |
| 346 |
} |
| 347 |
|
| 348 |
Sequence< css::security::DocumentSignatureInformation > |
| 349 |
DocumentDigitalSignatures::verifyScriptingContentSignatures( |
| 350 |
const Reference< css::embed::XStorage >& rxStorage, |
| 351 |
const Reference< css::io::XInputStream >& xSignInStream ) |
| 352 |
{ |
| 353 |
OSL_ENSURE(!m_sODFVersion.isEmpty(),"DocumentDigitalSignatures: ODF Version not set, assuming minimum 1.2"); |
| 354 |
return ImplVerifySignatures( rxStorage, xSignInStream, DocumentSignatureMode::Macros ); |
| 355 |
} |
| 356 |
|
| 357 |
void DocumentDigitalSignatures::showScriptingContentSignatures( |
| 358 |
const Reference< css::embed::XStorage >& rxStorage, |
| 359 |
const Reference< css::io::XInputStream >& xSignInStream ) |
| 360 |
{ |
| 361 |
OSL_ENSURE(!m_sODFVersion.isEmpty(),"DocumentDigitalSignatures: ODF Version not set, assuming minimum 1.2"); |
| 362 |
ImplViewSignatures( rxStorage, xSignInStream, DocumentSignatureMode::Macros, true ); |
| 363 |
} |
| 364 |
|
| 365 |
OUString DocumentDigitalSignatures::getScriptingContentSignatureDefaultStreamName() |
| 366 |
{ |
| 367 |
return DocumentSignatureHelper::GetScriptingContentSignatureDefaultStreamName(); |
| 368 |
} |
| 369 |
|
| 370 |
void DocumentDigitalSignatures::ImplViewSignatures( |
| 371 |
const Reference< css::embed::XStorage >& rxStorage, |
| 372 |
const Reference< css::io::XInputStream >& xSignStream, |
| 373 |
DocumentSignatureMode eMode, bool bReadOnly ) |
| 374 |
{ |
| 375 |
Reference< io::XStream > xStream; |
| 376 |
if ( xSignStream.is() ) |
| 377 |
xStream.set( xSignStream, UNO_QUERY ); |
| 378 |
ImplViewSignatures( rxStorage, xStream, eMode, bReadOnly, nullptr, [](bool /*bSuccess*/){} ); |
| 379 |
} |
| 380 |
|
| 381 |
void DocumentDigitalSignatures::ImplViewSignatures( |
| 382 |
const Reference< css::embed::XStorage >& rxStorage, const Reference< css::io::XStream >& xSignStream, |
| 383 |
DocumentSignatureMode eMode, bool bReadOnly, SfxViewShell* pViewShell, const std::function<void(bool)>& rCallback ) |
| 384 |
{ |
| 385 |
bool bChanges = false; |
| 386 |
auto xSignaturesDialog = std::make_shared<DigitalSignaturesDialog>( |
| 387 |
Application::GetFrameWeld(mxParentWindow), mxCtx, eMode, bReadOnly, m_sODFVersion, |
| 388 |
m_bHasDocumentSignature, pViewShell); |
| 389 |
bool bInit = xSignaturesDialog->Init(); |
| 390 |
SAL_WARN_IF( !bInit, "xmlsecurity.comp", "Error initializing security context!" ); |
| 391 |
if ( bInit ) |
| 392 |
{ |
| 393 |
xSignaturesDialog->SetStorage(rxStorage); |
| 394 |
|
| 395 |
xSignaturesDialog->SetSignatureStream( xSignStream ); |
| 396 |
xSignaturesDialog->SetScriptingSignatureStream( mxScriptingSignStream ); |
| 397 |
|
| 398 |
xSignaturesDialog->beforeRun(); |
| 399 |
weld::DialogController::runAsync(xSignaturesDialog, [xSignaturesDialog, rxStorage, xSignStream, rCallback] (sal_Int32 nRet) { |
| 400 |
if (nRet == RET_OK) |
| 401 |
{ |
| 402 |
bool bChanged = false; |
| 403 |
if (xSignaturesDialog->SignaturesChanged()) |
| 404 |
{ |
| 405 |
bChanged = true; |
| 406 |
// If we have a storage and no stream, we are responsible for commit |
| 407 |
if ( rxStorage.is() && !xSignStream.is() ) |
| 408 |
{ |
| 409 |
uno::Reference< embed::XTransactedObject > xTrans( rxStorage, uno::UNO_QUERY ); |
| 410 |
xTrans->commit(); |
| 411 |
} |
| 412 |
} |
| 413 |
rCallback(bChanged); |
| 414 |
} |
| 415 |
}); |
| 416 |
return; |
| 417 |
} |
| 418 |
else |
| 419 |
{ |
| 420 |
std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(Application::GetFrameWeld(mxParentWindow), |
| 421 |
VclMessageType::Warning, VclButtonsType::Ok, |
| 422 |
XsResId(RID_XMLSECWB_NO_MOZILLA_PROFILE))); |
| 423 |
xBox->run(); |
| 424 |
} |
| 425 |
|
| 426 |
rCallback(bChanges); |
| 427 |
} |
| 428 |
|
| 429 |
Sequence< css::security::DocumentSignatureInformation > |
| 430 |
DocumentDigitalSignatures::ImplVerifySignatures( |
| 431 |
const Reference< css::embed::XStorage >& rxStorage, |
| 432 |
const Reference< css::io::XInputStream >& xSignStream, DocumentSignatureMode eMode ) |
| 433 |
{ |
| 434 |
DocumentSignatureManager aSignatureManager(mxCtx, eMode); |
| 435 |
|
| 436 |
bool bInit = aSignatureManager.init(); |
| 437 |
|
| 438 |
SAL_WARN_IF(!bInit, "xmlsecurity.comp", "Error initializing security context!"); |
| 439 |
|
| 440 |
if (!bInit) |
| 441 |
return {}; |
| 442 |
|
| 443 |
if (!rxStorage.is()) |
| 444 |
{ |
| 445 |
if (xSignStream.is()) |
| 446 |
{ |
| 447 |
// Something not ZIP-based, try PDF. |
| 448 |
PDFSignatureHelper& rSignatureHelper = aSignatureManager.getPDFSignatureHelper(); |
| 449 |
if (rSignatureHelper.ReadAndVerifySignature(xSignStream)) |
| 450 |
return rSignatureHelper.GetDocumentSignatureInformations(aSignatureManager.getSecurityEnvironment()); |
| 451 |
} |
| 452 |
|
| 453 |
SAL_WARN( "xmlsecurity.comp", "Error, no XStorage provided"); |
| 454 |
return Sequence<css::security::DocumentSignatureInformation>(); |
| 455 |
} |
| 456 |
// First check for the InputStream, to avoid unnecessary initialization of the security environment... |
| 457 |
SignatureStreamHelper aStreamHelper; |
| 458 |
Reference< io::XInputStream > xInputStream = xSignStream; |
| 459 |
|
| 460 |
if ( !xInputStream.is() ) |
| 461 |
{ |
| 462 |
aStreamHelper = DocumentSignatureHelper::OpenSignatureStream( rxStorage, embed::ElementModes::READ, eMode ); |
| 463 |
if ( aStreamHelper.xSignatureStream.is() ) |
| 464 |
xInputStream.set( aStreamHelper.xSignatureStream, UNO_QUERY ); |
| 465 |
} |
| 466 |
|
| 467 |
if (!xInputStream.is() && aStreamHelper.nStorageFormat != embed::StorageFormats::OFOPXML) |
| 468 |
return {}; |
| 469 |
|
| 470 |
|
| 471 |
XMLSignatureHelper& rSignatureHelper = aSignatureManager.getSignatureHelper(); |
| 472 |
rSignatureHelper.SetStorage(rxStorage, m_sODFVersion); |
| 473 |
|
| 474 |
rSignatureHelper.StartMission(aSignatureManager.getSecurityContext()); |
| 475 |
|
| 476 |
if (xInputStream.is()) |
| 477 |
rSignatureHelper.ReadAndVerifySignature(xInputStream); |
| 478 |
else if (aStreamHelper.nStorageFormat == embed::StorageFormats::OFOPXML) |
| 479 |
rSignatureHelper.ReadAndVerifySignatureStorage(aStreamHelper.xSignatureStorage); |
| 480 |
|
| 481 |
rSignatureHelper.EndMission(); |
| 482 |
|
| 483 |
uno::Reference<xml::crypto::XSecurityEnvironment> xSecEnv = aSignatureManager.getSecurityEnvironment(); |
| 484 |
uno::Reference<xml::crypto::XSecurityEnvironment> xGpgSecEnv = aSignatureManager.getGpgSecurityEnvironment(); |
| 485 |
|
| 486 |
SignatureInformations aSignInfos = rSignatureHelper.GetSignatureInformations(); |
| 487 |
int nInfos = aSignInfos.size(); |
| 488 |
Sequence< css::security::DocumentSignatureInformation > aInfos(nInfos); |
| 489 |
css::security::DocumentSignatureInformation* arInfos = aInfos.getArray(); |
| 490 |
|
| 491 |
for (int n = 0; n < nInfos; ++n) |
| 492 |
{ |
| 493 |
DocumentSignatureAlgorithm mode |
| 494 |
= DocumentSignatureHelper::getDocumentAlgorithm(m_sODFVersion, aSignInfos[n]); |
| 495 |
const std::vector<OUString> aElementsToBeVerified |
| 496 |
= DocumentSignatureHelper::CreateElementList(rxStorage, eMode, mode); |
| 497 |
|
| 498 |
const SignatureInformation& rInfo = aSignInfos[n]; |
| 499 |
css::security::DocumentSignatureInformation& rSigInfo = arInfos[n]; |
| 500 |
|
| 501 |
if (!rInfo.X509Datas.empty()) // X.509 |
| 502 |
{ |
| 503 |
std::vector<uno::Reference<XCertificate>> certs( |
| 504 |
rSignatureHelper.CheckAndUpdateSignatureInformation( |
| 505 |
xSecEnv, rInfo)); |
| 506 |
if (certs.empty()) |
| 507 |
{ |
| 508 |
rSigInfo.CertificateStatus = css::security::CertificateValidity::INVALID; |
| 509 |
} |
| 510 |
else |
| 511 |
{ |
| 512 |
rSigInfo.Signer = certs.back(); |
| 513 |
// get only intermediates |
| 514 |
certs.pop_back(); |
| 515 |
// On Windows checking the certificate path is buggy. It does name matching (issuer, subject name) |
| 516 |
// to find the parent certificate. It does not take into account that there can be several certificates |
| 517 |
// with the same subject name. |
| 518 |
try |
| 519 |
{ |
| 520 |
rSigInfo.CertificateStatus = xSecEnv->verifyCertificate( |
| 521 |
rSigInfo.Signer, comphelper::containerToSequence(certs)); |
| 522 |
} |
| 523 |
catch (SecurityException&) |
| 524 |
{ |
| 525 |
SAL_WARN("xmlsecurity.comp", "Verification of certificate failed"); |
| 526 |
rSigInfo.CertificateStatus = css::security::CertificateValidity::INVALID; |
| 527 |
} |
| 528 |
} |
| 529 |
} |
| 530 |
else if (!rInfo.ouGpgCertificate.isEmpty() && xGpgSecEnv.is()) // GPG |
| 531 |
{ |
| 532 |
// TODO not ideal to retrieve cert by keyID, might |
| 533 |
// collide, or PGPKeyID format might change - can't we |
| 534 |
// keep the xCert itself in rInfo? |
| 535 |
rSigInfo.Signer = xGpgSecEnv->getCertificate( |
| 536 |
rInfo.ouGpgKeyID, xmlsecurity::numericStringToBigInteger(u"")); |
| 537 |
rSigInfo.CertificateStatus = xGpgSecEnv->verifyCertificate( |
| 538 |
rSigInfo.Signer, Sequence<Reference<css::security::XCertificate>>()); |
| 539 |
} |
| 540 |
|
| 541 |
// Time support again (#i38744#) |
| 542 |
Date aDate(rInfo.stDateTime.Day, rInfo.stDateTime.Month, rInfo.stDateTime.Year); |
| 543 |
tools::Time aTime(rInfo.stDateTime.Hours, rInfo.stDateTime.Minutes, |
| 544 |
rInfo.stDateTime.Seconds, rInfo.stDateTime.NanoSeconds); |
| 545 |
rSigInfo.SignatureDate = aDate.GetDate(); |
| 546 |
rSigInfo.SignatureTime = aTime.GetTime() / tools::Time::nanoPerCenti; |
| 547 |
|
| 548 |
rSigInfo.SignatureIsValid |
| 549 |
= (rInfo.nStatus == css::xml::crypto::SecurityOperationStatus_OPERATION_SUCCEEDED); |
| 550 |
|
| 551 |
// Signature line info (ID + Images) |
| 552 |
if (!rInfo.ouSignatureLineId.isEmpty()) |
| 553 |
rSigInfo.SignatureLineId = rInfo.ouSignatureLineId; |
| 554 |
|
| 555 |
if (rInfo.aValidSignatureImage.is()) |
| 556 |
rSigInfo.ValidSignatureLineImage = rInfo.aValidSignatureImage; |
| 557 |
|
| 558 |
if (rInfo.aInvalidSignatureImage.is()) |
| 559 |
rSigInfo.InvalidSignatureLineImage = rInfo.aInvalidSignatureImage; |
| 560 |
|
| 561 |
// OOXML intentionally doesn't sign metadata. |
| 562 |
if (rSigInfo.SignatureIsValid |
| 563 |
&& aStreamHelper.nStorageFormat != embed::StorageFormats::OFOPXML) |
| 564 |
{ |
| 565 |
rSigInfo.SignatureIsValid = DocumentSignatureHelper::checkIfAllFilesAreSigned( |
| 566 |
aElementsToBeVerified, rInfo, mode); |
| 567 |
} |
| 568 |
if (eMode == DocumentSignatureMode::Content) |
| 569 |
{ |
| 570 |
if (aStreamHelper.nStorageFormat == embed::StorageFormats::OFOPXML) |
| 571 |
rSigInfo.PartialDocumentSignature = true; |
| 572 |
else |
| 573 |
rSigInfo.PartialDocumentSignature |
| 574 |
= !DocumentSignatureHelper::isOOo3_2_Signature(aSignInfos[n]); |
| 575 |
} |
| 576 |
} |
| 577 |
|
| 578 |
return aInfos; |
| 579 |
|
| 580 |
} |
| 581 |
|
| 582 |
void DocumentDigitalSignatures::manageTrustedSources( ) |
| 583 |
{ |
| 584 |
// MT: i45295 |
| 585 |
// SecEnv is only needed to display certificate information from trusted sources. |
| 586 |
// Macro Security also has some options where no security environment is needed, so raise dialog anyway. |
| 587 |
// Later I should change the code so the Dialog creates the SecEnv on demand... |
| 588 |
|
| 589 |
Reference< css::xml::crypto::XSecurityEnvironment > xSecEnv; |
| 590 |
|
| 591 |
DocumentSignatureManager aSignatureManager(mxCtx, {}); |
| 592 |
if (aSignatureManager.init()) |
| 593 |
xSecEnv = aSignatureManager.getSecurityEnvironment(); |
| 594 |
|
| 595 |
MacroSecurity aDlg(Application::GetFrameWeld(mxParentWindow), xSecEnv); |
| 596 |
aDlg.run(); |
| 597 |
} |
| 598 |
|
| 599 |
void DocumentDigitalSignatures::showCertificate( |
| 600 |
const Reference< css::security::XCertificate >& Certificate ) |
| 601 |
{ |
| 602 |
DocumentSignatureManager aSignatureManager(mxCtx, {}); |
| 603 |
|
| 604 |
bool bInit = aSignatureManager.init(); |
| 605 |
|
| 606 |
SAL_WARN_IF( !bInit, "xmlsecurity.comp", "Error initializing security context!" ); |
| 607 |
|
| 608 |
if ( bInit ) |
| 609 |
{ |
| 610 |
CertificateViewer aViewer(Application::GetFrameWeld(mxParentWindow), aSignatureManager.getSecurityEnvironment(), Certificate, false, nullptr); |
| 611 |
aViewer.run(); |
| 612 |
} |
| 613 |
} |
| 614 |
|
| 615 |
sal_Bool DocumentDigitalSignatures::isAuthorTrusted( |
| 616 |
const Reference<css::security::XCertificate>& xAuthor) |
| 617 |
{ |
| 618 |
if (!xAuthor.is()) |
| 619 |
{ |
| 620 |
return false; |
| 621 |
} |
| 622 |
OUString sSerialNum = xmlsecurity::bigIntegerToNumericString(xAuthor->getSerialNumber()); |
| 623 |
|
| 624 |
std::vector< SvtSecurityOptions::Certificate > aTrustedAuthors = SvtSecurityOptions::GetTrustedAuthors(); |
| 625 |
|
| 626 |
return std::any_of(aTrustedAuthors.begin(), aTrustedAuthors.end(), |
| 627 |
[this, &xAuthor, &sSerialNum](const SvtSecurityOptions::Certificate& rAuthor) { |
| 628 |
if (!xmlsecurity::EqualDistinguishedNames(rAuthor.SubjectName, xAuthor->getIssuerName(), xmlsecurity::NOCOMPAT)) |
| 629 |
return false; |
| 630 |
if (rAuthor.SerialNumber != sSerialNum) |
| 631 |
return false; |
| 632 |
|
| 633 |
DocumentSignatureManager aSignatureManager(mxCtx, {}); |
| 634 |
if (!aSignatureManager.init()) |
| 635 |
return false; |
| 636 |
uno::Reference<css::security::XCertificate> xCert = aSignatureManager.getSecurityEnvironment()->createCertificateFromAscii(rAuthor.RawData); |
| 637 |
|
| 638 |
auto pAuthor = dynamic_cast<xmlsecurity::Certificate*>(xAuthor.get()); |
| 639 |
auto pCert = dynamic_cast<xmlsecurity::Certificate*>(xCert.get()); |
| 640 |
if (pAuthor && pCert) |
| 641 |
return pCert->getSHA256Thumbprint() == pAuthor->getSHA256Thumbprint(); |
| 642 |
|
| 643 |
return xCert->getSHA1Thumbprint() == xAuthor->getSHA1Thumbprint(); |
| 644 |
}); |
| 645 |
} |
| 646 |
|
| 647 |
uno::Sequence<Reference<css::security::XCertificate>> |
| 648 |
DocumentDigitalSignatures::chooseCertificatesImpl(SfxViewShell* pViewShell, |
| 649 |
std::map<OUString, OUString>& rProperties, |
| 650 |
const CertificateChooserUserAction eAction, |
| 651 |
const CertificateKind certificateKind) |
| 652 |
{ |
| 653 |
std::vector< Reference< css::xml::crypto::XXMLSecurityContext > > xSecContexts; |
| 654 |
|
| 655 |
DocumentSignatureManager aSignatureManager(mxCtx, {}); |
| 656 |
if (aSignatureManager.init()) { |
| 657 |
// Include OpenPGP and / or X.509 as requested. |
| 658 |
if (certificateKind == CertificateKind_NONE || certificateKind == CertificateKind_X509) |
| 659 |
xSecContexts.push_back(aSignatureManager.getSecurityContext()); |
| 660 |
if (certificateKind == CertificateKind_NONE || certificateKind == CertificateKind_OPENPGP) |
| 661 |
xSecContexts.push_back(aSignatureManager.getGpgSecurityContext()); |
| 662 |
} |
| 663 |
|
| 664 |
std::shared_ptr<CertificateChooser> aChooser = CertificateChooser::getInstance(Application::GetFrameWeld(mxParentWindow), pViewShell, std::move(xSecContexts), eAction); |
| 665 |
|
| 666 |
if (aChooser->run() != RET_OK) |
| 667 |
return { Reference< css::security::XCertificate >(nullptr) }; |
| 668 |
|
| 669 |
uno::Sequence< Reference< css::security::XCertificate > > xCerts = aChooser->GetSelectedCertificates(); |
| 670 |
rProperties[u"Description"_ustr] = aChooser->GetDescription(); |
| 671 |
rProperties[u"Usage"_ustr] = aChooser->GetUsageText(); |
| 672 |
|
| 673 |
return xCerts; |
| 674 |
} |
| 675 |
|
| 676 |
Reference< css::security::XCertificate > DocumentDigitalSignatures::chooseCertificate(OUString& rDescription) |
| 677 |
{ |
| 678 |
return chooseSigningCertificate( rDescription ); |
| 679 |
} |
| 680 |
|
| 681 |
Reference< css::security::XCertificate > DocumentDigitalSignatures::chooseSigningCertificate(OUString& rDescription) |
| 682 |
{ |
| 683 |
std::map<OUString, OUString> aProperties; |
| 684 |
Reference< css::security::XCertificate > xCert = chooseCertificatesImpl( nullptr, aProperties, CertificateChooserUserAction::Sign )[0]; |
| 685 |
rDescription = aProperties[u"Description"_ustr]; |
| 686 |
return xCert; |
| 687 |
} |
| 688 |
|
| 689 |
Reference< css::security::XCertificate > DocumentDigitalSignatures::selectSigningCertificate(OUString& rDescription) |
| 690 |
{ |
| 691 |
std::map<OUString, OUString> aProperties; |
| 692 |
Reference< css::security::XCertificate > xCert = chooseCertificatesImpl( nullptr, aProperties, CertificateChooserUserAction::SelectSign )[0]; |
| 693 |
rDescription = aProperties[u"Description"_ustr]; |
| 694 |
return xCert; |
| 695 |
} |
| 696 |
|
| 697 |
Reference<css::security::XCertificate> |
| 698 |
DocumentDigitalSignatures::selectSigningCertificateWithType(const CertificateKind certificateKind, |
| 699 |
OUString& rDescription) |
| 700 |
{ |
| 701 |
return SelectSigningCertificateWithType(nullptr, certificateKind, rDescription); |
| 702 |
} |
| 703 |
|
| 704 |
Reference<css::security::XCertificate> |
| 705 |
DocumentDigitalSignatures::SelectSigningCertificateWithType(SfxViewShell* pViewShell, const CertificateKind certificateKind, OUString& rDescription) |
| 706 |
{ |
| 707 |
std::map<OUString, OUString> aProperties; |
| 708 |
Reference<css::security::XCertificate> xCert |
| 709 |
= chooseCertificatesImpl(pViewShell, aProperties, CertificateChooserUserAction::SelectSign, certificateKind)[0]; |
| 710 |
rDescription = aProperties[u"Description"_ustr]; |
| 711 |
return xCert; |
| 712 |
} |
| 713 |
|
| 714 |
css::uno::Sequence<Reference<css::security::XCertificate>> |
| 715 |
DocumentDigitalSignatures::chooseEncryptionCertificate(const CertificateKind certificateKind) |
| 716 |
{ |
| 717 |
std::map<OUString, OUString> aProperties; |
| 718 |
uno::Sequence< Reference< css::security::XCertificate > > aCerts= |
| 719 |
chooseCertificatesImpl( nullptr, aProperties, CertificateChooserUserAction::Encrypt , certificateKind ); |
| 720 |
if (aCerts.getLength() == 1 && !aCerts[0].is()) |
| 721 |
// our error case contract is: empty sequence, so map that! |
| 722 |
return uno::Sequence< Reference< css::security::XCertificate > >(); |
| 723 |
else |
| 724 |
return aCerts; |
| 725 |
} |
| 726 |
|
| 727 |
css::uno::Reference< css::security::XCertificate > DocumentDigitalSignatures::chooseCertificateWithProps(Sequence<css::beans::PropertyValue>& rProperties) |
| 728 |
{ |
| 729 |
std::map<OUString, OUString> aProperties; |
| 730 |
auto xCert = chooseCertificatesImpl( nullptr, aProperties, CertificateChooserUserAction::Sign )[0]; |
| 731 |
|
| 732 |
std::vector<css::beans::PropertyValue> vec; |
| 733 |
vec.reserve(aProperties.size()); |
| 734 |
for (const auto& pair : aProperties) |
| 735 |
{ |
| 736 |
vec.emplace_back(comphelper::makePropertyValue(pair.first, pair.second)); |
| 737 |
} |
| 738 |
|
| 739 |
rProperties = comphelper::containerToSequence(vec); |
| 740 |
return xCert; |
| 741 |
} |
| 742 |
|
| 743 |
sal_Bool DocumentDigitalSignatures::isLocationTrusted( const OUString& Location ) |
| 744 |
{ |
| 745 |
return SvtSecurityOptions::isTrustedLocationUri(Location); |
| 746 |
} |
| 747 |
|
| 748 |
void DocumentDigitalSignatures::addAuthorToTrustedSources( |
| 749 |
const Reference< css::security::XCertificate >& Author ) |
| 750 |
{ |
| 751 |
SvtSecurityOptions::Certificate aNewCert; |
| 752 |
aNewCert.SubjectName = Author->getIssuerName(); |
| 753 |
aNewCert.SerialNumber = xmlsecurity::bigIntegerToNumericString( Author->getSerialNumber() ); |
| 754 |
|
| 755 |
OUStringBuffer aStrBuffer; |
| 756 |
::comphelper::Base64::encode(aStrBuffer, Author->getEncoded()); |
| 757 |
aNewCert.RawData = aStrBuffer.makeStringAndClear(); |
| 758 |
|
| 759 |
std::vector< SvtSecurityOptions::Certificate > aTrustedAuthors = SvtSecurityOptions::GetTrustedAuthors(); |
| 760 |
aTrustedAuthors.push_back( aNewCert ); |
| 761 |
SvtSecurityOptions::SetTrustedAuthors( aTrustedAuthors ); |
| 762 |
} |
| 763 |
|
| 764 |
void DocumentDigitalSignatures::addLocationToTrustedSources( const OUString& Location ) |
| 765 |
{ |
| 766 |
std::vector< OUString > aSecURLs = SvtSecurityOptions::GetSecureURLs(); |
| 767 |
aSecURLs.push_back(Location); |
| 768 |
|
| 769 |
SvtSecurityOptions::SetSecureURLs( std::move(aSecURLs) ); |
| 770 |
} |
| 771 |
|
| 772 |
sal_Bool DocumentDigitalSignatures::signDocumentWithCertificate( |
| 773 |
css::uno::Reference<css::security::XCertificate> const & xCertificate, |
| 774 |
css::uno::Reference<css::embed::XStorage> const & xStorage, |
| 775 |
css::uno::Reference<css::io::XStream> const & xStream) |
| 776 |
{ |
| 777 |
uno::Reference<frame::XModel> xModel; |
| 778 |
svl::crypto::SigningContext aSigningContext; |
| 779 |
aSigningContext.m_xCertificate = xCertificate; |
| 780 |
return signWithCertificateImpl(xModel, aSigningContext, xStorage, xStream, |
| 781 |
DocumentSignatureMode::Content); |
| 782 |
} |
| 783 |
|
| 784 |
bool DocumentDigitalSignatures::SignModelWithCertificate( |
| 785 |
const uno::Reference<frame::XModel>& xModel, |
| 786 |
svl::crypto::SigningContext& rSigningContext, |
| 787 |
const css::uno::Reference<css::embed::XStorage>& xStorage, |
| 788 |
const css::uno::Reference<css::io::XStream>& xStream) |
| 789 |
{ |
| 790 |
return signWithCertificateImpl(xModel, rSigningContext, xStorage, xStream, |
| 791 |
DocumentSignatureMode::Content); |
| 792 |
} |
| 793 |
|
| 794 |
void DocumentDigitalSignatures::SignDocumentContentAsync(const css::uno::Reference<css::embed::XStorage>& rxStorage, |
| 795 |
const css::uno::Reference<css::io::XStream>& xSignStream, |
| 796 |
SfxViewShell* pViewShell, |
| 797 |
const std::function<void(bool)>& rCallback) |
| 798 |
{ |
| 799 |
OSL_ENSURE(!m_sODFVersion.isEmpty(), "DocumentDigitalSignatures: ODF Version not set, assuming minimum 1.2"); |
| 800 |
ImplViewSignatures( rxStorage, xSignStream, DocumentSignatureMode::Content, false, pViewShell, rCallback ); |
| 801 |
} |
| 802 |
|
| 803 |
void DocumentDigitalSignatures::SignScriptingContentAsync( |
| 804 |
const Reference<css::embed::XStorage>& rxStorage, |
| 805 |
const Reference<css::io::XStream>& xSignStream, const std::function<void(bool)>& rCallback) |
| 806 |
{ |
| 807 |
OSL_ENSURE(!m_sODFVersion.isEmpty(),"DocumentDigitalSignatures: ODF Version not set, assuming minimum 1.2"); |
| 808 |
OSL_ENSURE(m_nArgumentsCount == 2, "DocumentDigitalSignatures: Service was not initialized properly"); |
| 809 |
ImplViewSignatures( rxStorage, xSignStream, DocumentSignatureMode::Macros, false, nullptr, rCallback ); |
| 810 |
} |
| 811 |
|
| 812 |
void DocumentDigitalSignatures::SetSignScriptingContent( |
| 813 |
const css::uno::Reference<css::io::XStream>& xScriptingSignStream) |
| 814 |
{ |
| 815 |
mxScriptingSignStream = xScriptingSignStream; |
| 816 |
} |
| 817 |
|
| 818 |
sal_Bool DocumentDigitalSignatures::trustUntrustedCertificate( |
| 819 |
css::uno::Reference<css::security::XCertificate> const& xCertificate) |
| 820 |
{ |
| 821 |
OUString aSubjectName(comphelper::xmlsec::GetContentPart(xCertificate->getSubjectName(), xCertificate->getCertificateKind())); |
| 822 |
OUString aMsg(XsResId(STR_TRUST_UNTRUSTED_PUBKEY)); |
| 823 |
aMsg = aMsg.replaceFirst("%{data}", aSubjectName); |
| 824 |
std::unique_ptr<weld::MessageDialog> m_xQueryBox(Application::CreateMessageDialog(nullptr, VclMessageType::Warning, VclButtonsType::YesNo, aMsg)); |
| 825 |
m_xQueryBox->set_default_response(RET_NO); |
| 826 |
return m_xQueryBox->run() == RET_YES; |
| 827 |
} |
| 828 |
|
| 829 |
sal_Bool DocumentDigitalSignatures::signScriptingContentWithCertificate( |
| 830 |
css::uno::Reference<css::security::XCertificate> const& xCertificate, |
| 831 |
css::uno::Reference<css::embed::XStorage> const& xStorage, |
| 832 |
css::uno::Reference<css::io::XStream> const& xStream) |
| 833 |
{ |
| 834 |
uno::Reference<frame::XModel> xModel; |
| 835 |
svl::crypto::SigningContext aSigningContext; |
| 836 |
aSigningContext.m_xCertificate = xCertificate; |
| 837 |
return signWithCertificateImpl(xModel, aSigningContext, xStorage, xStream, |
| 838 |
DocumentSignatureMode::Macros); |
| 839 |
} |
| 840 |
|
| 841 |
bool DocumentDigitalSignatures::signWithCertificateImpl( |
| 842 |
const uno::Reference<frame::XModel>& xModel, |
| 843 |
svl::crypto::SigningContext& rSigningContext, |
| 844 |
css::uno::Reference<css::embed::XStorage> const& xStorage, |
| 845 |
css::uno::Reference<css::io::XStream> const& xStream, DocumentSignatureMode eMode) |
| 846 |
{ |
| 847 |
OSL_ENSURE(!m_sODFVersion.isEmpty(), |
| 848 |
"DocumentDigitalSignatures: ODF Version not set, assuming minimum 1.2"); |
| 849 |
|
| 850 |
DocumentSignatureManager aSignatureManager(mxCtx, eMode); |
| 851 |
|
| 852 |
if (!aSignatureManager.init()) |
| 853 |
return false; |
| 854 |
|
| 855 |
aSignatureManager.setStore(xStorage); |
| 856 |
aSignatureManager.getSignatureHelper().SetStorage(xStorage, m_sODFVersion); |
| 857 |
aSignatureManager.setSignatureStream(xStream); |
| 858 |
aSignatureManager.setModel(xModel); |
| 859 |
|
| 860 |
Reference<XXMLSecurityContext> xSecurityContext; |
| 861 |
Reference<XServiceInfo> xServiceInfo(rSigningContext.m_xCertificate, UNO_QUERY); |
| 862 |
if (xServiceInfo.is() && xServiceInfo->getImplementationName() |
| 863 |
== "com.sun.star.xml.security.gpg.XCertificate_GpgImpl") |
| 864 |
xSecurityContext = aSignatureManager.getGpgSecurityContext(); |
| 865 |
else |
| 866 |
xSecurityContext = aSignatureManager.getSecurityContext(); |
| 867 |
|
| 868 |
sal_Int32 nSecurityId; |
| 869 |
|
| 870 |
bool bSuccess = aSignatureManager.add(rSigningContext, xSecurityContext, u""_ustr, nSecurityId, true); |
| 871 |
if (!bSuccess) |
| 872 |
return false; |
| 873 |
|
| 874 |
aSignatureManager.read(/*bUseTempStream=*/true, /*bCacheLastSignature=*/false); |
| 875 |
aSignatureManager.write(true); |
| 876 |
|
| 877 |
if (xStorage.is() && !xStream.is()) |
| 878 |
{ |
| 879 |
uno::Reference<embed::XTransactedObject> xTransaction(xStorage, uno::UNO_QUERY); |
| 880 |
xTransaction->commit(); |
| 881 |
} |
| 882 |
|
| 883 |
return true; |
| 884 |
} |
| 885 |
|
| 886 |
extern "C" SAL_DLLPUBLIC_EXPORT uno::XInterface* |
| 887 |
com_sun_star_security_DocumentDigitalSignatures_get_implementation( |
| 888 |
uno::XComponentContext* pCtx, uno::Sequence<uno::Any> const& /*rSeq*/) |
| 889 |
{ |
| 890 |
return cppu::acquire( |
| 891 |
new DocumentDigitalSignatures(uno::Reference<uno::XComponentContext>(pCtx))); |
| 892 |
} |
| 893 |
|
| 894 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |