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 #ifndef INCLUDED_XMLSECURITY_INC_DOCUMENTSIGNATUREMANAGER_HXX
21 #define INCLUDED_XMLSECURITY_INC_DOCUMENTSIGNATUREMANAGER_HXX
22 
23 #include "xmlsecuritydllapi.h"
24 
25 #include <memory>
26 
27 #include <svl/sigstruct.hxx>
28 #include "xmlsignaturehelper.hxx"
29 #include "documentsignaturehelper.hxx"
30 
31 #include <com/sun/star/xml/crypto/XSEInitializer.hpp>
32 
33 namespace com
34 {
35 namespace sun
36 {
37 namespace star
38 {
39 namespace beans
40 {
41 struct PropertyValue;
42 }
43 namespace embed
44 {
45 class XStorage;
46 }
47 namespace graphic
48 {
49 class XGraphic;
50 }
51 namespace uno
52 {
53 class XComponentContext;
54 }
55 }
56 }
57 }
58 class PDFSignatureHelper;
59 
60 /// Manages signatures (addition, removal), used by DigitalSignaturesDialog.
61 class XMLSECURITY_DLLPUBLIC DocumentSignatureManager
62 {
63 private:
64     css::uno::Reference<css::uno::XComponentContext> mxContext;
65     css::uno::Reference<css::embed::XStorage> mxStore;
66     XMLSignatureHelper maSignatureHelper;
67     std::unique_ptr<PDFSignatureHelper> mpPDFSignatureHelper;
68     SignatureInformations maCurrentSignatureInformations;
69     DocumentSignatureMode const meSignatureMode;
70     css::uno::Sequence<css::uno::Sequence<css::beans::PropertyValue>> m_manifest;
71     css::uno::Reference<css::io::XStream> mxSignatureStream;
72     css::uno::Reference<css::io::XStream> mxTempSignatureStream;
73     /// Storage containing all OOXML signatures, unused for ODF.
74     css::uno::Reference<css::embed::XStorage> mxTempSignatureStorage;
75     css::uno::Reference<css::xml::crypto::XSEInitializer> mxSEInitializer;
76     css::uno::Reference<css::xml::crypto::XXMLSecurityContext> mxSecurityContext;
77     css::uno::Reference<css::xml::crypto::XSEInitializer> mxGpgSEInitializer;
78     css::uno::Reference<css::xml::crypto::XXMLSecurityContext> mxGpgSecurityContext;
79 
80 public:
81     DocumentSignatureManager(const css::uno::Reference<css::uno::XComponentContext>& xContext,
82                              DocumentSignatureMode eMode);
83     ~DocumentSignatureManager();
84 
85     /**
86      * Checks if a particular stream is a valid xml stream. Those are treated
87      * differently when they are signed (c14n transformation)
88      */
89     bool isXML(const OUString& rURI);
90     bool readManifest();
91 
92     SignatureStreamHelper ImplOpenSignatureStream(sal_Int32 nStreamOpenMode, bool bTempStream);
93     /// Add a new signature, using xCert as a signing certificate, and rDescription as description.
94     bool add(const css::uno::Reference<css::security::XCertificate>& xCert,
95              const css::uno::Reference<css::xml::crypto::XXMLSecurityContext>& xSecurityContext,
96              const OUString& rDescription, sal_Int32& nSecurityId, bool bAdESCompliant,
97              const OUString& rSignatureLineId = OUString(),
98              const css::uno::Reference<css::graphic::XGraphic>& xValidGraphic
99              = css::uno::Reference<css::graphic::XGraphic>(),
100              const css::uno::Reference<css::graphic::XGraphic>& xInvalidGraphic
101              = css::uno::Reference<css::graphic::XGraphic>());
102     /// Remove signature at nPosition.
103     void remove(sal_uInt16 nPosition);
104     /// Read signatures from either a temp stream or the real storage.
105     void read(bool bUseTempStream, bool bCacheLastSignature = true);
106     /// Write signatures back to the persistent storage.
107     void write(bool bXAdESCompliantIfODF);
108     /// Lazy creation of PDF helper.
109     PDFSignatureHelper& getPDFSignatureHelper();
110 #if 0
111     // Checks if the document is a kind where it is relevant to distinguish between using XAdES or not
112     bool IsXAdESRelevant();
113 #endif
114     /// Attempts to initialize the platform-specific crypto.
115     bool init();
116     /// Get the security environment.
117     css::uno::Reference<css::xml::crypto::XSecurityEnvironment> getSecurityEnvironment();
118     css::uno::Reference<css::xml::crypto::XSecurityEnvironment> getGpgSecurityEnvironment();
119     css::uno::Reference<css::xml::crypto::XXMLSecurityContext> const& getSecurityContext() const;
120     css::uno::Reference<css::xml::crypto::XXMLSecurityContext> const& getGpgSecurityContext() const;
setStore(const css::uno::Reference<css::embed::XStorage> & xStore)121     void setStore(const css::uno::Reference<css::embed::XStorage>& xStore) { mxStore = xStore; }
getSignatureHelper()122     XMLSignatureHelper& getSignatureHelper() { return maSignatureHelper; }
hasPDFSignatureHelper() const123     bool hasPDFSignatureHelper() const { return mpPDFSignatureHelper.get(); }
setSignatureStream(const css::uno::Reference<css::io::XStream> & xSignatureStream)124     void setSignatureStream(const css::uno::Reference<css::io::XStream>& xSignatureStream)
125     {
126         mxSignatureStream = xSignatureStream;
127     }
getStore() const128     const css::uno::Reference<css::embed::XStorage>& getStore() const { return mxStore; }
getSignatureMode() const129     DocumentSignatureMode getSignatureMode() const { return meSignatureMode; }
getCurrentSignatureInformations()130     SignatureInformations& getCurrentSignatureInformations()
131     {
132         return maCurrentSignatureInformations;
133     }
134 };
135 
136 #endif // INCLUDED_XMLSECURITY_INC_DOCUMENTSIGNATUREMANAGER_HXX
137 
138 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
139