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 _XMLSECURITY_XMLSIGNATUREHELPER_HXX
21 : #define _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 : 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 : // Not allowed:
116 : XMLSignatureHelper(const XMLSignatureHelper&);
117 :
118 : public:
119 : XMLSignatureHelper(const com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext >& mrCtx );
120 : ~XMLSignatureHelper();
121 :
122 : // Initialize the security context with default crypto token.
123 : // Returns true for success.
124 : bool Init();
125 :
126 : // Set the storage which should be used by the default UriBinding
127 : // Must be set before StatrtMission().
128 : //sODFVersion indicates the ODF version
129 : void SetStorage( const com::sun::star::uno::Reference < com::sun::star::embed::XStorage >& rxStorage, const OUString& sODFVersion );
130 :
131 : // Argument for the Link is a uno::Reference< xml::sax::XAttributeList >*
132 : // Return 1 to verify, 0 to skip.
133 : // Default handler will verify all.
134 : void SetStartVerifySignatureHdl( const Link& rLink );
135 :
136 : // Get the security environment
137 : ::com::sun::star::uno::Reference< ::com::sun::star::xml::crypto::XSecurityEnvironment > GetSecurityEnvironment();
138 :
139 : // After signing/veryfieng, get information about signatures
140 : SignatureInformation GetSignatureInformation( sal_Int32 nSecurityId ) const;
141 : SignatureInformations GetSignatureInformations() const;
142 :
143 : // See XSecController for documentation
144 : void StartMission();
145 : void EndMission();
146 : sal_Int32 GetNewSecurityId();
147 : /** sets data that describes the certificate.
148 :
149 : It is absolutely necessary that the parameter ouX509IssuerName is set. It contains
150 : the base64 encoded certificate, which is DER encoded. The XMLSec needs it to find
151 : the private key. Although issuer name and certificate should be sufficient to identify
152 : the certificate the implementation in XMLSec is broken, both for Windows and mozilla.
153 : The reason is that they use functions to find the certificate which take as parameter
154 : the DER encoded ASN.1 issuer name. The issuer name is a DName, where most attributes
155 : are of type DirectoryName, which is a choice of 5 string types. This information is
156 : not contained in the issuer string and while it is converted to the ASN.1 name the
157 : conversion function must assume a particular type, which is often wrong. For example,
158 : the Windows function CertStrToName will use a T.61 string if the string does not contain
159 : special characters. So if the certificate uses simple characters but encodes the
160 : issuer attributes in Utf8, then CertStrToName will use T.61. The resulting DER encoded
161 : ASN.1 name now contains different bytes which indicate the string type. The functions
162 : for finding the certificate apparently use memcmp - hence they fail to find the
163 : certificate.
164 : */
165 : void SetX509Certificate(sal_Int32 nSecurityId, const OUString& ouX509IssuerName,
166 : const OUString& ouX509SerialNumber, const OUString& ouX509Cert);
167 :
168 : void SetDateTime( sal_Int32 nSecurityId, const Date& rDate, const Time& rTime );
169 :
170 : void AddForSigning( sal_Int32 securityId, const OUString& uri, const OUString& objectURL, sal_Bool bBinary );
171 : bool CreateAndWriteSignature( const com::sun::star::uno::Reference< com::sun::star::xml::sax::XDocumentHandler >& xDocumentHandler );
172 : bool ReadAndVerifySignature( const com::sun::star::uno::Reference< com::sun::star::io::XInputStream >& xInputStream );
173 :
174 : // MT: ??? I think only for adding/removing, not for new signatures...
175 : // MM: Yes, but if you want to insert a new signature into an existing signature file, those function
176 : // will be very useful, see Mission 3 in the new "multisigdemo" program :-)
177 : ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XWriter> CreateDocumentHandlerWithHeader( const com::sun::star::uno::Reference< com::sun::star::io::XOutputStream >& xOutputStream );
178 : void CloseDocumentHandler( const ::com::sun::star::uno::Reference< com::sun::star::xml::sax::XDocumentHandler>& xDocumentHandler );
179 : void ExportSignature( const com::sun::star::uno::Reference< com::sun::star::xml::sax::XDocumentHandler >& xDocumentHandler, const SignatureInformation& signatureInfo );
180 : };
181 :
182 : #endif // _XMLSECURITY_XMLSIGNATUREHELPER_HXX
183 :
184 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|