Debian Codegraph @

xmlsecurity/source/xmlsec/mscrypt/xmlsignature_mscryptimpl.cxx

raw
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 <sal/config.h>
21 #include <sal/log.hxx>
22 #include <rtl/uuid.h>
23
24 #include <xmlsec/xmldsig.h>
25 #include <xmlsec/xmltree.h> // this MUST precede the mscng/x509.h for some reason
26 #include <xmlsec/mscng/x509.h>
27
28 #include <com/sun/star/xml/crypto/SecurityOperationStatus.hpp>
29 #include <com/sun/star/xml/crypto/XXMLSignature.hpp>
30
31 #include "securityenvironment_mscryptimpl.hxx"
32
33 #include <xmlsec/xmldocumentwrapper_xmlsecimpl.hxx>
34 #include <xmlelementwrapper_xmlsecimpl.hxx>
35 #include <xmlsec/xmlstreamio.hxx>
36 #include <xmlsec/errorcallback.hxx>
37 #include <cppuhelper/supportsservice.hxx>
38
39 using namespace ::com::sun::star;
40 using namespace ::com::sun::star::uno ;
41 using namespace ::com::sun::star::lang ;
42
43 using ::com::sun::star::xml::wrapper::XXMLElementWrapper ;
44 using ::com::sun::star::xml::crypto::XSecurityEnvironment ;
45 using ::com::sun::star::xml::crypto::XXMLSignature ;
46 using ::com::sun::star::xml::crypto::XXMLSignatureTemplate ;
47 using ::com::sun::star::xml::crypto::XXMLSecurityContext ;
48 using ::com::sun::star::xml::crypto::XUriBinding ;
49
50 namespace {
51
52 class XMLSignature_MSCryptImpl : public ::cppu::WeakImplHelper<
53 css::xml::crypto::XXMLSignature ,
54 css::lang::XServiceInfo >
55 {
56 public:
57 explicit XMLSignature_MSCryptImpl();
58
59 //Methods from XXMLSignature
60 virtual css::uno::Reference< css::xml::crypto::XXMLSignatureTemplate > SAL_CALL generate(
61 const css::uno::Reference< css::xml::crypto::XXMLSignatureTemplate >& aTemplate ,
62 const css::uno::Reference< css::xml::crypto::XSecurityEnvironment >& aEnvironment
63 ) override;
64
65 virtual css::uno::Reference< css::xml::crypto::XXMLSignatureTemplate > SAL_CALL validate(
66 const css::uno::Reference< css::xml::crypto::XXMLSignatureTemplate >& aTemplate ,
67 const css::uno::Reference< css::xml::crypto::XXMLSecurityContext >& aContext
68 ) override;
69
70 //Methods from XServiceInfo
71 virtual OUString SAL_CALL getImplementationName() override;
72
73 virtual sal_Bool SAL_CALL supportsService(
74 const OUString& ServiceName
75 ) override;
76
77 virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
78 } ;
79
80 }
81
82 XMLSignature_MSCryptImpl::XMLSignature_MSCryptImpl() {
83 }
84
85 /* XXMLSignature */
86 Reference< XXMLSignatureTemplate >
87 SAL_CALL XMLSignature_MSCryptImpl::generate(
88 const Reference< XXMLSignatureTemplate >& aTemplate ,
89 const Reference< XSecurityEnvironment >& aEnvironment
90 )
91 {
92 xmlSecKeysMngrPtr pMngr = nullptr ;
93 xmlSecDSigCtxPtr pDsigCtx = nullptr ;
94 xmlNodePtr pNode = nullptr ;
95
96 if( !aTemplate.is() )
97 throw RuntimeException() ;
98
99 if( !aEnvironment.is() )
100 throw RuntimeException() ;
101
102 //Get Keys Manager
103 SecurityEnvironment_MSCryptImpl* pSecEnv = dynamic_cast<SecurityEnvironment_MSCryptImpl*>(aEnvironment.get());
104 if( pSecEnv == nullptr )
105 throw RuntimeException() ;
106
107 //Get the xml node
108 Reference< XXMLElementWrapper > xElement = aTemplate->getTemplate() ;
109 if( !xElement.is() ) {
110 throw RuntimeException() ;
111 }
112
113 XMLElementWrapper_XmlSecImpl* pElement = dynamic_cast<XMLElementWrapper_XmlSecImpl*>(xElement.get());
114 if( pElement == nullptr ) {
115 throw RuntimeException() ;
116 }
117
118 pNode = pElement->getNativeElement() ;
119
120 //Get the stream/URI binding
121 Reference< XUriBinding > xUriBinding = aTemplate->getBinding() ;
122 if( xUriBinding.is() ) {
123 //Register the stream input callbacks into libxml2
124 if( xmlRegisterStreamInputCallbacks( xUriBinding ) < 0 )
125 throw RuntimeException() ;
126 }
127
128 setErrorRecorder( );
129
130 pMngr = pSecEnv->createKeysManager();
131 if( !pMngr ) {
132 throw RuntimeException() ;
133 }
134
135 //Create Signature context
136 pDsigCtx = xmlSecDSigCtxCreate( pMngr ) ;
137 if( pDsigCtx == nullptr )
138 {
139 //throw XMLSignatureException() ;
140 SecurityEnvironment_MSCryptImpl::destroyKeysManager( pMngr );
141 clearErrorRecorder();
142 return aTemplate;
143 }
144
145 //Sign the template
146 pDsigCtx->keyInfoReadCtx.flags |= XMLSEC_KEYINFO_FLAGS_X509DATA_DONT_VERIFY_CERTS;
147 if( xmlSecDSigCtxSign( pDsigCtx , pNode ) == 0 )
148 {
149 if (pDsigCtx->status == xmlSecDSigStatusSucceeded)
150 aTemplate->setStatus(css::xml::crypto::SecurityOperationStatus_OPERATION_SUCCEEDED);
151 else
152 aTemplate->setStatus(css::xml::crypto::SecurityOperationStatus_UNKNOWN);
153 }
154 else
155 {
156 aTemplate->setStatus(css::xml::crypto::SecurityOperationStatus_UNKNOWN);
157 }
158
159
160 xmlSecDSigCtxDestroy( pDsigCtx ) ;
161 SecurityEnvironment_MSCryptImpl::destroyKeysManager( pMngr );
162
163 //Unregistered the stream/URI binding
164 if( xUriBinding.is() )
165 xmlUnregisterStreamInputCallbacks() ;
166
167 clearErrorRecorder();
168 return aTemplate ;
169 }
170
171 /* XXMLSignature */
172 Reference< XXMLSignatureTemplate >
173 SAL_CALL XMLSignature_MSCryptImpl::validate(
174 const Reference< XXMLSignatureTemplate >& aTemplate ,
175 const Reference< XXMLSecurityContext >& aSecurityCtx
176 ) {
177 xmlSecKeysMngrPtr pMngr = nullptr ;
178 xmlSecDSigCtxPtr pDsigCtx = nullptr ;
179 xmlNodePtr pNode = nullptr ;
180
181 if( !aTemplate.is() )
182 throw RuntimeException() ;
183
184 if( !aSecurityCtx.is() )
185 throw RuntimeException() ;
186
187 //Get Keys Manager
188 Reference< XSecurityEnvironment > xSecEnv
189 = aSecurityCtx->getSecurityEnvironmentByIndex(
190 aSecurityCtx->getDefaultSecurityEnvironmentIndex());
191 SecurityEnvironment_MSCryptImpl* pSecEnv = dynamic_cast<SecurityEnvironment_MSCryptImpl*>(xSecEnv.get());
192 if( pSecEnv == nullptr )
193 throw RuntimeException() ;
194
195 //Get the xml node
196 Reference< XXMLElementWrapper > xElement = aTemplate->getTemplate() ;
197 if( !xElement.is() )
198 throw RuntimeException() ;
199
200 XMLElementWrapper_XmlSecImpl* pElement = dynamic_cast<XMLElementWrapper_XmlSecImpl*>(xElement.get());
201 if( pElement == nullptr )
202 throw RuntimeException() ;
203
204 pNode = pElement->getNativeElement() ;
205
206 //Get the stream/URI binding
207 Reference< XUriBinding > xUriBinding = aTemplate->getBinding() ;
208 if( xUriBinding.is() ) {
209 //Register the stream input callbacks into libxml2
210 if( xmlRegisterStreamInputCallbacks( xUriBinding ) < 0 )
211 throw RuntimeException() ;
212 }
213
214 setErrorRecorder( );
215
216 pMngr = pSecEnv->createKeysManager();
217 if( !pMngr ) {
218 throw RuntimeException() ;
219 }
220
221 //Create Signature context
222 pDsigCtx = xmlSecDSigCtxCreate( pMngr ) ;
223 if( pDsigCtx == nullptr )
224 {
225 SecurityEnvironment_MSCryptImpl::destroyKeysManager( pMngr );
226 clearErrorRecorder();
227 return aTemplate;
228 }
229
230 // We do certificate verification ourselves.
231 pDsigCtx->keyInfoReadCtx.flags |= XMLSEC_KEYINFO_FLAGS_X509DATA_DONT_VERIFY_CERTS;
232
233 // limit possible key data to valid X509 certificates only, no KeyValues
234 if (xmlSecPtrListAdd(&(pDsigCtx->keyInfoReadCtx.enabledKeyData), BAD_CAST xmlSecMSCngKeyDataX509GetKlass()) < 0)
235 throw RuntimeException("failed to limit allowed key data");
236
237 //Verify signature
238 //The documentation says that the signature is only valid if the return value is 0 (that is, not < 0)
239 //AND pDsigCtx->status == xmlSecDSigStatusSucceeded. That is, we must not make any assumptions, if
240 //the return value is < 0. Then we must regard the signature as INVALID. We cannot use the
241 //error recorder feature to get the ONE error that made the verification fail, because there is no
242 //documentation/specification as to how to interpret the number of recorded errors and what is the initial
243 //error.
244 int rs = xmlSecDSigCtxVerify(pDsigCtx , pNode);
245
246 // Also verify manifest: this is empty for ODF, but contains everything (except signature metadata) for OOXML.
247 xmlSecSize nReferenceCount = xmlSecPtrListGetSize(&pDsigCtx->manifestReferences);
248 // Require that all manifest references are also good.
249 xmlSecSize nReferenceGood = 0;
250 for (xmlSecSize nReference = 0; nReference < nReferenceCount; ++nReference)
251 {
252 xmlSecDSigReferenceCtxPtr pReference = static_cast<xmlSecDSigReferenceCtxPtr>(xmlSecPtrListGetItem(&pDsigCtx->manifestReferences, nReference));
253 if (pReference)
254 {
255 if (pReference->status == xmlSecDSigStatusSucceeded)
256 ++nReferenceGood;
257 }
258 }
259 SAL_INFO("xmlsecurity.xmlsec", "xmlSecDSigCtxVerify status " << pDsigCtx->status << ", references good " << nReferenceGood << " of " << nReferenceCount);
260
261 if (rs == 0 && nReferenceCount == nReferenceGood)
262 {
263 if (pDsigCtx->status == xmlSecDSigStatusSucceeded)
264 aTemplate->setStatus(css::xml::crypto::SecurityOperationStatus_OPERATION_SUCCEEDED);
265 else
266 aTemplate->setStatus(css::xml::crypto::SecurityOperationStatus_UNKNOWN);
267 }
268 else
269 {
270 aTemplate->setStatus(css::xml::crypto::SecurityOperationStatus_UNKNOWN);
271 }
272
273 xmlSecDSigCtxDestroy( pDsigCtx ) ;
274 SecurityEnvironment_MSCryptImpl::destroyKeysManager( pMngr );
275
276 //Unregistered the stream/URI binding
277 if( xUriBinding.is() )
278 xmlUnregisterStreamInputCallbacks() ;
279
280
281 clearErrorRecorder();
282 return aTemplate;
283 }
284
285 /* XServiceInfo */
286 OUString SAL_CALL XMLSignature_MSCryptImpl::getImplementationName() {
287 return "com.sun.star.xml.crypto.XMLSignature";
288 }
289
290 /* XServiceInfo */
291 sal_Bool SAL_CALL XMLSignature_MSCryptImpl::supportsService( const OUString& serviceName) {
292 return cppu::supportsService(this, serviceName);
293 }
294
295 /* XServiceInfo */
296 Sequence< OUString > SAL_CALL XMLSignature_MSCryptImpl::getSupportedServiceNames() {
297 return { "com.sun.star.xml.crypto.XMLSignature" };
298 }
299
300 extern "C" SAL_DLLPUBLIC_EXPORT uno::XInterface*
301 com_sun_star_xml_crypto_XMLSignature_get_implementation(uno::XComponentContext* /*pCtx*/,
302 uno::Sequence<uno::Any> const& /*rSeq*/)
303 {
304 return cppu::acquire(new XMLSignature_MSCryptImpl);
305 }
306
307 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */