Debian Codegraph @

xmlsecurity/source/xmlsec/nss/xmlsignature_nssimpl.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
22 #include <xmlsec/nss/x509.h>
23
24 #include <xmlelementwrapper_xmlsecimpl.hxx>
25 #include <xmlsec/xmlstreamio.hxx>
26 #include <xmlsec/errorcallback.hxx>
27
28 #include "securityenvironment_nssimpl.hxx"
29
30 #include <xmlsec/xmldsig.h>
31 #include <sal/log.hxx>
32
33 #include <com/sun/star/xml/crypto/XXMLSignature.hpp>
34 #include <memory>
35
36 namespace com::sun::star::uno { class XComponentContext; }
37
38 using namespace ::com::sun::star;
39 using namespace ::com::sun::star::uno ;
40 using namespace ::com::sun::star::lang ;
41
42 using ::com::sun::star::xml::wrapper::XXMLElementWrapper ;
43 using ::com::sun::star::xml::crypto::XSecurityEnvironment ;
44 using ::com::sun::star::xml::crypto::XXMLSignature ;
45 using ::com::sun::star::xml::crypto::XXMLSignatureTemplate ;
46 using ::com::sun::star::xml::crypto::XXMLSecurityContext ;
47 using ::com::sun::star::xml::crypto::XUriBinding ;
48
49 namespace std
50 {
51 template <> struct default_delete<xmlSecKeysMngr>
52 {
53 void operator()(xmlSecKeysMngrPtr ptr) { SecurityEnvironment_NssImpl::destroyKeysManager(ptr); }
54 };
55 template <> struct default_delete<xmlSecDSigCtx>
56 {
57 void operator()(xmlSecDSigCtxPtr ptr) { xmlSecDSigCtxDestroy(ptr); }
58 };
59 }
60
61 namespace {
62
63 class XMLSignature_NssImpl
64 : public ::cppu::WeakImplHelper<xml::crypto::XXMLSignature, lang::XServiceInfo>
65 {
66 public:
67 explicit XMLSignature_NssImpl();
68
69 //Methods from XXMLSignature
70 virtual uno::Reference<xml::crypto::XXMLSignatureTemplate> SAL_CALL
71 generate(const uno::Reference<xml::crypto::XXMLSignatureTemplate>& aTemplate,
72 const uno::Reference<xml::crypto::XSecurityEnvironment>& aEnvironment) override;
73
74 virtual uno::Reference<xml::crypto::XXMLSignatureTemplate> SAL_CALL
75 validate(const uno::Reference<xml::crypto::XXMLSignatureTemplate>& aTemplate,
76 const uno::Reference<xml::crypto::XXMLSecurityContext>& aContext) override;
77
78 //Methods from XServiceInfo
79 virtual OUString SAL_CALL getImplementationName() override;
80
81 virtual sal_Bool SAL_CALL supportsService(const OUString& ServiceName) override;
82
83 virtual uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override;
84 };
85
86 }
87
88 XMLSignature_NssImpl::XMLSignature_NssImpl() {
89 }
90
91 /* XXMLSignature */
92 Reference< XXMLSignatureTemplate >
93 SAL_CALL XMLSignature_NssImpl::generate(
94 const Reference< XXMLSignatureTemplate >& aTemplate ,
95 const Reference< XSecurityEnvironment >& aEnvironment
96 )
97 {
98 xmlNodePtr pNode = nullptr ;
99
100 if( !aTemplate.is() )
101 throw RuntimeException() ;
102
103 if( !aEnvironment.is() )
104 throw RuntimeException() ;
105
106 //Get the xml node
107 Reference< XXMLElementWrapper > xElement = aTemplate->getTemplate() ;
108 if( !xElement.is() ) {
109 throw RuntimeException() ;
110 }
111
112 XMLElementWrapper_XmlSecImpl* pElement
113 = 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 //Get Keys Manager
129
130 // the key manager should be retrieved from SecurityEnvironment, instead of SecurityContext
131 SecurityEnvironment_NssImpl* pSecEnv
132 = dynamic_cast<SecurityEnvironment_NssImpl*>(aEnvironment.get());
133 if( pSecEnv == nullptr )
134 throw RuntimeException() ;
135
136 setErrorRecorder();
137
138 std::unique_ptr<xmlSecKeysMngr> pMngr(pSecEnv->createKeysManager());
139 if( !pMngr ) {
140 throw RuntimeException() ;
141 }
142
143 //Create Signature context
144 std::unique_ptr<xmlSecDSigCtx> pDsigCtx(xmlSecDSigCtxCreate(pMngr.get()));
145 if( pDsigCtx == nullptr )
146 {
147 //throw XMLSignatureException() ;
148 clearErrorRecorder();
149 return aTemplate;
150 }
151
152 //Sign the template
153 pDsigCtx->keyInfoReadCtx.flags |= XMLSEC_KEYINFO_FLAGS_X509DATA_DONT_VERIFY_CERTS;
154 if( xmlSecDSigCtxSign( pDsigCtx.get() , pNode ) == 0 )
155 {
156 if (pDsigCtx->status == xmlSecDSigStatusSucceeded)
157 aTemplate->setStatus(css::xml::crypto::SecurityOperationStatus_OPERATION_SUCCEEDED);
158 else
159 aTemplate->setStatus(css::xml::crypto::SecurityOperationStatus_UNKNOWN);
160 }
161 else
162 {
163 aTemplate->setStatus(css::xml::crypto::SecurityOperationStatus_UNKNOWN);
164 }
165
166 //Unregistered the stream/URI binding
167 if( xUriBinding.is() )
168 xmlUnregisterStreamInputCallbacks() ;
169
170 clearErrorRecorder();
171 return aTemplate ;
172 }
173
174 /* XXMLSignature */
175 Reference< XXMLSignatureTemplate >
176 SAL_CALL XMLSignature_NssImpl::validate(
177 const Reference< XXMLSignatureTemplate >& aTemplate ,
178 const Reference< XXMLSecurityContext >& aSecurityCtx
179 ) {
180 xmlNodePtr pNode = nullptr ;
181 //sal_Bool valid ;
182
183 if( !aTemplate.is() )
184 throw RuntimeException() ;
185
186 if( !aSecurityCtx.is() )
187 throw RuntimeException() ;
188
189 //Get the xml node
190 Reference< XXMLElementWrapper > xElement = aTemplate->getTemplate() ;
191 if( !xElement.is() )
192 throw RuntimeException() ;
193
194 XMLElementWrapper_XmlSecImpl* pElement
195 = dynamic_cast<XMLElementWrapper_XmlSecImpl*>(xElement.get());
196 if( pElement == nullptr )
197 throw RuntimeException() ;
198
199 pNode = pElement->getNativeElement() ;
200
201 //Get the stream/URI binding
202 Reference< XUriBinding > xUriBinding = aTemplate->getBinding() ;
203 if( xUriBinding.is() ) {
204 //Register the stream input callbacks into libxml2
205 if( xmlRegisterStreamInputCallbacks( xUriBinding ) < 0 )
206 throw RuntimeException() ;
207 }
208
209 setErrorRecorder();
210
211 sal_Int32 nSecurityEnvironment = aSecurityCtx->getSecurityEnvironmentNumber();
212 sal_Int32 i;
213
214 for (i=0; i<nSecurityEnvironment; ++i)
215 {
216 Reference< XSecurityEnvironment > aEnvironment = aSecurityCtx->getSecurityEnvironmentByIndex(i);
217
218 //Get Keys Manager
219 SecurityEnvironment_NssImpl* pSecEnv
220 = dynamic_cast<SecurityEnvironment_NssImpl*>(aEnvironment.get());
221 if( pSecEnv == nullptr )
222 throw RuntimeException() ;
223
224 std::unique_ptr<xmlSecKeysMngr> pMngr(pSecEnv->createKeysManager());
225 if( !pMngr ) {
226 throw RuntimeException() ;
227 }
228
229 //Create Signature context
230 std::unique_ptr<xmlSecDSigCtx> pDsigCtx(xmlSecDSigCtxCreate(pMngr.get()));
231 if( pDsigCtx == nullptr )
232 {
233 clearErrorRecorder();
234 return aTemplate;
235 }
236
237 // We do certificate verification ourselves.
238 pDsigCtx->keyInfoReadCtx.flags |= XMLSEC_KEYINFO_FLAGS_X509DATA_DONT_VERIFY_CERTS;
239
240 // limit possible key data to valid X509 certificates only, no KeyValues
241 if (xmlSecPtrListAdd(&(pDsigCtx->keyInfoReadCtx.enabledKeyData), BAD_CAST xmlSecNssKeyDataX509GetKlass()) < 0)
242 throw RuntimeException(u"failed to limit allowed key data"_ustr);
243
244 xmlBufferPtr pBuf = xmlBufferCreate();
245 xmlNodeDump(pBuf, nullptr, pNode, 0, 0);
246 SAL_INFO("xmlsecurity.xmlsec", "xmlSecDSigCtxVerify input XML node is '"
247 << reinterpret_cast<const char*>(xmlBufferContent(pBuf))
248 << "'");
249 xmlBufferFree(pBuf);
250
251 //Verify signature
252 int rs = xmlSecDSigCtxVerify( pDsigCtx.get() , pNode );
253
254 // Also verify manifest: this is empty for ODF, but contains everything (except signature metadata) for OOXML.
255 xmlSecSize nReferenceCount = xmlSecPtrListGetSize(&pDsigCtx->manifestReferences);
256 // Require that all manifest references are also good.
257 xmlSecSize nReferenceGood = 0;
258 for (xmlSecSize nReference = 0; nReference < nReferenceCount; ++nReference)
259 {
260 xmlSecDSigReferenceCtxPtr pReference = static_cast<xmlSecDSigReferenceCtxPtr>(xmlSecPtrListGetItem(&pDsigCtx->manifestReferences, nReference));
261 if (pReference)
262 {
263 if (pReference->status == xmlSecDSigStatusSucceeded)
264 ++nReferenceGood;
265 }
266 }
267 SAL_INFO("xmlsecurity.xmlsec", "xmlSecDSigCtxVerify status " << pDsigCtx->status << ", references good " << nReferenceGood << " of " << nReferenceCount);
268
269 if (rs == 0 && pDsigCtx->status == xmlSecDSigStatusSucceeded && nReferenceCount == nReferenceGood)
270 {
271 aTemplate->setStatus(css::xml::crypto::SecurityOperationStatus_OPERATION_SUCCEEDED);
272 break;
273 }
274 else
275 {
276 aTemplate->setStatus(css::xml::crypto::SecurityOperationStatus_UNKNOWN);
277 }
278 }
279
280
281 //Unregistered the stream/URI binding
282 if( xUriBinding.is() )
283 xmlUnregisterStreamInputCallbacks() ;
284
285 //return valid ;
286 clearErrorRecorder();
287 return aTemplate;
288 }
289
290 /* XServiceInfo */
291 OUString SAL_CALL XMLSignature_NssImpl::getImplementationName()
292 {
293 return u"com.sun.star.xml.crypto.XMLSignature"_ustr;
294 }
295
296 /* XServiceInfo */
297 sal_Bool SAL_CALL XMLSignature_NssImpl::supportsService(const OUString& rServiceName)
298 {
299 const css::uno::Sequence<OUString> aServiceNames = getSupportedServiceNames();
300 for (OUString const & rCurrentServiceName : aServiceNames)
301 {
302 if (rCurrentServiceName == rServiceName)
303 return true;
304 }
305 return false;
306 }
307
308 /* XServiceInfo */
309 Sequence<OUString> SAL_CALL XMLSignature_NssImpl::getSupportedServiceNames()
310 {
311 return { u"com.sun.star.xml.crypto.XMLSignature"_ustr };
312 }
313
314 extern "C" SAL_DLLPUBLIC_EXPORT uno::XInterface*
315 com_sun_star_xml_crypto_XMLSignature_get_implementation(uno::XComponentContext* /*pCtx*/,
316 uno::Sequence<uno::Any> const& /*rSeq*/)
317 {
318 return cppu::acquire(new XMLSignature_NssImpl);
319 }
320
321 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */