Line data Source code
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_XMLSECURITY_XMLSIGNATUREHELPER_HXX
21 : #define INCLUDED_XMLSECURITY_INC_XMLSECURITY_XMLSIGNATUREHELPER_HXX
22 :
23 : #include <vector>
24 :
25 : #include <tools/link.hxx>
26 : #include <rtl/ustring.hxx>
27 : #include <xmlsecurity/sigstruct.hxx>
28 :
29 : #include <com/sun/star/uno/XComponentContext.hpp>
30 : #include <com/sun/star/xml/sax/XWriter.hpp>
31 : #include <com/sun/star/xml/crypto/XUriBinding.hpp>
32 : #include <com/sun/star/xml/crypto/XSEInitializer.hpp>
33 : #include <com/sun/star/xml/crypto/sax/XSecurityController.hpp>
34 : #include <com/sun/star/xml/crypto/sax/XSignatureCreationResultListener.hpp>
35 : #include <com/sun/star/xml/crypto/sax/XSignatureVerifyResultListener.hpp>
36 :
37 : class XSecController;
38 : class Date;
39 : namespace tools { class Time; }
40 :
41 : namespace com {
42 : namespace sun {
43 : namespace star {
44 : namespace io {
45 : class XOutputStream;
46 : class XInputStream; }
47 : namespace embed {
48 : class XStorage; }
49 : }}}
50 :
51 : struct XMLSignatureCreationResult
52 : {
53 : sal_Int32 nSecurityId;
54 : com::sun::star::xml::crypto::SecurityOperationStatus nSignatureCreationResult;
55 :
56 0 : XMLSignatureCreationResult( sal_Int32 nId, com::sun::star::xml::crypto::SecurityOperationStatus nResult )
57 : {
58 0 : nSecurityId = nId;
59 0 : nSignatureCreationResult = nResult;
60 0 : }
61 : };
62 :
63 : struct XMLSignatureVerifyResult
64 : {
65 : sal_Int32 nSecurityId;
66 : com::sun::star::xml::crypto::SecurityOperationStatus nSignatureVerifyResult;
67 :
68 0 : XMLSignatureVerifyResult( sal_Int32 nId, com::sun::star::xml::crypto::SecurityOperationStatus nResult )
69 : {
70 0 : nSecurityId = nId;
71 0 : nSignatureVerifyResult = nResult;
72 0 : }
73 : };
74 :
75 : typedef ::std::vector<XMLSignatureCreationResult> XMLSignatureCreationResults;
76 : typedef ::std::vector<XMLSignatureVerifyResult> XMLSignatureVerifyResults;
77 :
78 :
79 :
80 : /**********************************************************
81 : XMLSignatureHelper
82 :
83 : Helper class for the XML Security framework
84 :
85 : Functions:
86 : 1. help to create a security context;
87 : 2. help to listen signature creation result;
88 : 3. help to listen signature verify result;
89 : 4. help to indicate which signature to verify.
90 :
91 : **********************************************************/
92 :
93 : class XMLSignatureHelper
94 : {
95 : private:
96 : ::com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext > mxCtx;
97 : ::com::sun::star::uno::Reference< com::sun::star::xml::crypto::sax::XSecurityController > mxSecurityController;
98 : ::com::sun::star::uno::Reference< com::sun::star::xml::crypto::XUriBinding > mxUriBinding;
99 :
100 : ::com::sun::star::uno::Reference< com::sun::star::xml::crypto::XSEInitializer > mxSEInitializer;
101 : ::com::sun::star::uno::Reference< com::sun::star::xml::crypto::XXMLSecurityContext > mxSecurityContext;
102 :
103 : XMLSignatureCreationResults maCreationResults;
104 : XMLSignatureVerifyResults maVerifyResults;
105 : XSecController* mpXSecController;
106 : bool mbError;
107 : bool mbODFPre1_2;
108 : Link<> maStartVerifySignatureHdl;
109 :
110 : private:
111 : DECL_LINK( SignatureCreationResultListener, XMLSignatureCreationResult*);
112 : DECL_LINK( SignatureVerifyResultListener, XMLSignatureVerifyResult* );
113 : DECL_LINK( StartVerifySignatureElement, const com::sun::star::uno::Reference< com::sun::star::xml::sax::XAttributeList >* );
114 :
115 : XMLSignatureHelper(const XMLSignatureHelper&) SAL_DELETED_FUNCTION;
116 :
117 : public:
118 : XMLSignatureHelper(const com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext >& mrCtx );
119 : ~XMLSignatureHelper();
120 :
121 : // Initialize the security context with default crypto token.
122 : // Returns true for success.
123 : bool Init();
124 :
125 : // Set the storage which should be used by the default UriBinding
126 : // Must be set before StatrtMission().
127 : //sODFVersion indicates the ODF version
128 : void SetStorage( const com::sun::star::uno::Reference < com::sun::star::embed::XStorage >& rxStorage, const OUString& sODFVersion );
129 :
130 : // Argument for the Link is a uno::Reference< xml::sax::XAttributeList >*
131 : // Return 1 to verify, 0 to skip.
132 : // Default handler will verify all.
133 : void SetStartVerifySignatureHdl( const Link<>& rLink );
134 :
135 : // Get the security environment
136 : ::com::sun::star::uno::Reference< ::com::sun::star::xml::crypto::XSecurityEnvironment > GetSecurityEnvironment();
137 :
138 : // After signing/veryfieng, get information about signatures
139 : SignatureInformation GetSignatureInformation( sal_Int32 nSecurityId ) const;
140 : SignatureInformations GetSignatureInformations() const;
141 :
142 : // See XSecController for documentation
143 : void StartMission();
144 : void EndMission();
145 : sal_Int32 GetNewSecurityId();
146 : /** sets data that describes the certificate.
147 :
148 : It is absolutely necessary that the parameter ouX509IssuerName is set. It contains
149 : the base64 encoded certificate, which is DER encoded. The XMLSec needs it to find
150 : the private key. Although issuer name and certificate should be sufficient to identify
151 : the certificate the implementation in XMLSec is broken, both for Windows and mozilla.
152 : The reason is that they use functions to find the certificate which take as parameter
153 : the DER encoded ASN.1 issuer name. The issuer name is a DName, where most attributes
154 : are of type DirectoryName, which is a choice of 5 string types. This information is
155 : not contained in the issuer string and while it is converted to the ASN.1 name the
156 : conversion function must assume a particular type, which is often wrong. For example,
157 : the Windows function CertStrToName will use a T.61 string if the string does not contain
158 : special characters. So if the certificate uses simple characters but encodes the
159 : issuer attributes in Utf8, then CertStrToName will use T.61. The resulting DER encoded
160 : ASN.1 name now contains different bytes which indicate the string type. The functions
161 : for finding the certificate apparently use memcmp - hence they fail to find the
162 : certificate.
163 : */
164 : void SetX509Certificate(sal_Int32 nSecurityId, const OUString& ouX509IssuerName,
165 : const OUString& ouX509SerialNumber, const OUString& ouX509Cert);
166 :
167 : void SetDateTime( sal_Int32 nSecurityId, const Date& rDate, const tools::Time& rTime );
168 :
169 : void AddForSigning( sal_Int32 securityId, const OUString& uri, const OUString& objectURL, bool bBinary );
170 : bool CreateAndWriteSignature( const com::sun::star::uno::Reference< com::sun::star::xml::sax::XDocumentHandler >& xDocumentHandler );
171 : bool ReadAndVerifySignature( const com::sun::star::uno::Reference< com::sun::star::io::XInputStream >& xInputStream );
172 :
173 : // MT: ??? I think only for adding/removing, not for new signatures...
174 : // MM: Yes, but if you want to insert a new signature into an existing signature file, those function
175 : // will be very useful, see Mission 3 in the new "multisigdemo" program :-)
176 : ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XWriter> CreateDocumentHandlerWithHeader( const com::sun::star::uno::Reference< com::sun::star::io::XOutputStream >& xOutputStream );
177 : static void CloseDocumentHandler( const ::com::sun::star::uno::Reference< com::sun::star::xml::sax::XDocumentHandler>& xDocumentHandler );
178 : static void ExportSignature( const com::sun::star::uno::Reference< com::sun::star::xml::sax::XDocumentHandler >& xDocumentHandler, const SignatureInformation& signatureInfo );
179 : };
180 :
181 : #endif // INCLUDED_XMLSECURITY_INC_XMLSECURITY_XMLSIGNATUREHELPER_HXX
182 :
183 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|