1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_XMLSECURITY_INC_XMLSIGNATUREHELPER_HXX
21 #define INCLUDED_XMLSECURITY_INC_XMLSIGNATUREHELPER_HXX
25 #include <tools/link.hxx>
26 #include <rtl/ustring.hxx>
27 #include <rtl/ref.hxx>
28 #include <sigstruct.hxx>
29 #include <xsecctl.hxx>
30 #include <xmlsecurity/xmlsecuritydllapi.h>
32 #include <com/sun/star/uno/XComponentContext.hpp>
33 #include <com/sun/star/xml/sax/XWriter.hpp>
34 #include <com/sun/star/xml/crypto/XUriBinding.hpp>
35 #include <com/sun/star/xml/crypto/XSEInitializer.hpp>
36 #include <com/sun/star/xml/crypto/sax/XSignatureCreationResultListener.hpp>
37 #include <com/sun/star/xml/crypto/sax/XSignatureVerifyResultListener.hpp>
40 namespace tools
{ class Time
; }
52 struct XMLSignatureCreationResult
54 sal_Int32 nSecurityId
;
55 css::xml::crypto::SecurityOperationStatus nSignatureCreationResult
;
57 XMLSignatureCreationResult( sal_Int32 nId
, css::xml::crypto::SecurityOperationStatus nResult
)
60 nSignatureCreationResult
= nResult
;
64 struct XMLSignatureVerifyResult
66 sal_Int32 nSecurityId
;
67 css::xml::crypto::SecurityOperationStatus nSignatureVerifyResult
;
69 XMLSignatureVerifyResult( sal_Int32 nId
, css::xml::crypto::SecurityOperationStatus nResult
)
72 nSignatureVerifyResult
= nResult
;
77 /**********************************************************
80 Helper class for the XML Security framework
83 1. help to create a security context;
84 2. help to listen signature creation result;
85 3. help to listen signature verify result;
86 4. help to indicate which signature to verify.
88 **********************************************************/
90 class XMLSECURITY_DLLPUBLIC XMLSignatureHelper
93 css::uno::Reference
< css::uno::XComponentContext
> mxCtx
;
94 css::uno::Reference
< css::xml::crypto::XUriBinding
> mxUriBinding
;
96 std::vector
<XMLSignatureCreationResult
>
98 std::vector
<XMLSignatureVerifyResult
>
100 rtl::Reference
<XSecController
> mpXSecController
;
103 Link
<LinkParamNone
*,bool> maStartVerifySignatureHdl
;
106 XMLSignatureHelper(const XMLSignatureHelper
&) = delete;
109 XMLSignatureHelper(const css::uno::Reference
< css::uno::XComponentContext
>& mrCtx
);
110 ~XMLSignatureHelper();
112 void SignatureCreationResultListener(XMLSignatureCreationResult
& rResult
);
113 void SignatureVerifyResultListener(XMLSignatureVerifyResult
& rResult
);
114 void StartVerifySignatureElement();
116 // Set the storage which should be used by the default UriBinding
117 // Must be set before StartMission().
118 //sODFVersion indicates the ODF version
119 void SetStorage( const css::uno::Reference
< css::embed::XStorage
>& rxStorage
, const OUString
& sODFVersion
);
121 // Argument for the Link is a uno::Reference< xml::sax::XAttributeList >*
122 // Return 1 to verify, 0 to skip.
123 // Default handler will verify all.
124 void SetStartVerifySignatureHdl( const Link
<LinkParamNone
*,bool>& rLink
);
126 // After signing/verifying, get information about signatures
127 SignatureInformation
GetSignatureInformation( sal_Int32 nSecurityId
) const;
128 SignatureInformations
GetSignatureInformations() const;
130 // See XSecController for documentation
131 void StartMission(const css::uno::Reference
<css::xml::crypto::XXMLSecurityContext
>& xSecurityContext
);
133 sal_Int32
GetNewSecurityId();
134 /** sets data that describes the certificate.
136 It is absolutely necessary that the parameter ouX509IssuerName is set. It contains
137 the base64 encoded certificate, which is DER encoded. The XMLSec needs it to find
138 the private key. Although issuer name and certificate should be sufficient to identify
139 the certificate the implementation in XMLSec is broken, both for Windows and mozilla.
140 The reason is that they use functions to find the certificate which take as parameter
141 the DER encoded ASN.1 issuer name. The issuer name is a DName, where most attributes
142 are of type DirectoryName, which is a choice of 5 string types. This information is
143 not contained in the issuer string and while it is converted to the ASN.1 name the
144 conversion function must assume a particular type, which is often wrong. For example,
145 the Windows function CertStrToName will use a T.61 string if the string does not contain
146 special characters. So if the certificate uses simple characters but encodes the
147 issuer attributes in Utf8, then CertStrToName will use T.61. The resulting DER encoded
148 ASN.1 name now contains different bytes which indicate the string type. The functions
149 for finding the certificate apparently use memcmp - hence they fail to find the
152 void SetX509Certificate(sal_Int32 nSecurityId
, const OUString
& ouX509IssuerName
,
153 const OUString
& ouX509SerialNumber
, const OUString
& ouX509Cert
, const OUString
& ouX509CertDigest
);
155 void AddEncapsulatedX509Certificate(const OUString
& ouEncapsulatedX509Certificate
);
157 void SetGpgCertificate(sal_Int32 nSecurityId
, const OUString
& ouGpgCertDigest
,
158 const OUString
& ouGpgCert
, const OUString
& ouGpgOwner
);
160 void SetDateTime( sal_Int32 nSecurityId
, const Date
& rDate
, const tools::Time
& rTime
);
161 void SetDescription(sal_Int32 nSecurityId
, const OUString
& rDescription
);
163 void AddForSigning( sal_Int32 securityId
, const OUString
& uri
, bool bBinary
, bool bXAdESCompliantIfODF
);
164 bool CreateAndWriteSignature( const css::uno::Reference
< css::xml::sax::XDocumentHandler
>& xDocumentHandler
, bool bXAdESCompliantIfODF
);
165 bool ReadAndVerifySignature( const css::uno::Reference
< css::io::XInputStream
>& xInputStream
);
167 // MT: ??? I think only for adding/removing, not for new signatures...
168 // MM: Yes, but if you want to insert a new signature into an existing signature file, those function
169 // will be very useful, see Mission 3 in the new "multisigdemo" program :-)
170 css::uno::Reference
< css::xml::sax::XWriter
> CreateDocumentHandlerWithHeader( const css::uno::Reference
< css::io::XOutputStream
>& xOutputStream
);
171 static void CloseDocumentHandler( const css::uno::Reference
< css::xml::sax::XDocumentHandler
>& xDocumentHandler
);
172 static void ExportSignature(
173 const css::uno::Reference
< css::xml::sax::XDocumentHandler
>& xDocumentHandler
,
174 const SignatureInformation
& signatureInfo
,
175 bool bXAdESCompliantIfODF
);
177 /// Read and verify OOXML signatures.
178 bool ReadAndVerifySignatureStorage(const css::uno::Reference
<css::embed::XStorage
>& xStorage
, bool bCacheLastSignature
= true);
179 /// Read and verify a single OOXML signature.
180 bool ReadAndVerifySignatureStorageStream(const css::uno::Reference
<css::io::XInputStream
>& xInputStream
);
181 /// Adds or removes an OOXML digital signature relation to _rels/.rels if there wasn't any before.
182 void EnsureSignaturesRelation(const css::uno::Reference
<css::embed::XStorage
>& xStorage
, bool bAdd
);
183 /// Given that xStorage is an OOXML _xmlsignatures storage, create origin.sigs and its relations.
184 void ExportSignatureRelations(const css::uno::Reference
<css::embed::XStorage
>& xStorage
, int nSignatureCount
);
185 /// Given that xSignatureStorage is an OOXML _xmlsignatures storage, create and write a new signature.
186 bool CreateAndWriteOOXMLSignature(const css::uno::Reference
<css::embed::XStorage
>& xRootStorage
, const css::uno::Reference
<css::embed::XStorage
>& xSignatureStorage
, int nSignatureIndex
);
187 /// Similar to CreateAndWriteOOXMLSignature(), but used to write the signature to the persistent storage, not the temporary one.
188 void ExportOOXMLSignature(const css::uno::Reference
<css::embed::XStorage
>& xRootStorage
, const css::uno::Reference
<css::embed::XStorage
>& xSignatureStorage
, const SignatureInformation
& rInformation
, int nSignatureIndex
);
189 /// Given that xStorage is an OOXML root storage, advertise signatures in its [Content_Types].xml stream.
190 void ExportSignatureContentTypes(const css::uno::Reference
<css::embed::XStorage
>& xStorage
, int nSignatureCount
);
193 #endif // INCLUDED_XMLSECURITY_INC_XMLSIGNATUREHELPER_HXX
195 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */