1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_xmlsecurity.hxx"
26 #include <sal/config.h>
27 #include <rtl/uuid.h>
28 #include "securityenvironment_nssimpl.hxx"
29 
30 #ifndef _XMLSECURITYCONTEXT_NSSIMPL_HXX_
31 #include "xmlsecuritycontext_nssimpl.hxx"
32 #endif
33 #include "xmlstreamio.hxx"
34 
35 #include <sal/types.h>
36 //For reasons that escape me, this is what xmlsec does when size_t is not 4
37 #if SAL_TYPES_SIZEOFPOINTER != 4
38 #    define XMLSEC_NO_SIZE_T
39 #endif
40 #include "xmlsec/xmlsec.h"
41 #include "xmlsec/keysmngr.h"
42 #include "xmlsec/crypto.h"
43 
44 using namespace ::com::sun::star::uno ;
45 using namespace ::com::sun::star::lang ;
46 using ::com::sun::star::lang::XMultiServiceFactory ;
47 using ::com::sun::star::lang::XSingleServiceFactory ;
48 using ::rtl::OUString ;
49 
50 using ::com::sun::star::xml::crypto::XSecurityEnvironment ;
51 using ::com::sun::star::xml::crypto::XXMLSecurityContext ;
52 
XMLSecurityContext_NssImpl(const Reference<XMultiServiceFactory> & aFactory)53 XMLSecurityContext_NssImpl :: XMLSecurityContext_NssImpl( const Reference< XMultiServiceFactory >& aFactory )
54 	://i39448 : m_pKeysMngr( NULL ) ,
55 	m_xServiceManager( aFactory ) ,
56 	m_nDefaultEnvIndex(-1)
57 	//m_xSecurityEnvironment( NULL )
58 {
59 	//Init xmlsec library
60 	if( xmlSecInit() < 0 ) {
61 		throw RuntimeException() ;
62 	}
63 
64 	//Init xmlsec crypto engine library
65 	if( xmlSecCryptoInit() < 0 ) {
66 		xmlSecShutdown() ;
67 		throw RuntimeException() ;
68 	}
69 
70 	//Enable external stream handlers
71 	if( xmlEnableStreamInputCallbacks() < 0 ) {
72 		xmlSecCryptoShutdown() ;
73 		xmlSecShutdown() ;
74 		throw RuntimeException() ;
75 	}
76 }
77 
~XMLSecurityContext_NssImpl()78 XMLSecurityContext_NssImpl :: ~XMLSecurityContext_NssImpl() {
79 #if 0 //i39448
80 	if( m_pKeysMngr != NULL ) {
81 		xmlSecKeysMngrDestroy( m_pKeysMngr ) ;
82 	}
83 #endif
84 
85 	xmlDisableStreamInputCallbacks() ;
86 	xmlSecCryptoShutdown() ;
87 	xmlSecShutdown() ;
88 }
89 
90 //i39448 : new methods
addSecurityEnvironment(const::com::sun::star::uno::Reference<::com::sun::star::xml::crypto::XSecurityEnvironment> & aSecurityEnvironment)91 sal_Int32 SAL_CALL XMLSecurityContext_NssImpl::addSecurityEnvironment(
92 	const ::com::sun::star::uno::Reference< ::com::sun::star::xml::crypto::XSecurityEnvironment >& aSecurityEnvironment)
93 	throw (::com::sun::star::security::SecurityInfrastructureException, ::com::sun::star::uno::RuntimeException)
94 {
95 	if( !aSecurityEnvironment.is() )
96 	{
97 		throw RuntimeException() ;
98 	}
99 
100 	m_vSecurityEnvironments.push_back( aSecurityEnvironment );
101 
102 	return m_vSecurityEnvironments.size() - 1 ;
103 }
104 
105 
getSecurityEnvironmentNumber()106 sal_Int32 SAL_CALL XMLSecurityContext_NssImpl::getSecurityEnvironmentNumber(  )
107 	throw (::com::sun::star::uno::RuntimeException)
108 {
109 	return m_vSecurityEnvironments.size();
110 }
111 
112 ::com::sun::star::uno::Reference< ::com::sun::star::xml::crypto::XSecurityEnvironment > SAL_CALL
getSecurityEnvironmentByIndex(sal_Int32 index)113 	XMLSecurityContext_NssImpl::getSecurityEnvironmentByIndex( sal_Int32 index )
114 	throw (::com::sun::star::uno::RuntimeException)
115 {
116 	::com::sun::star::uno::Reference< ::com::sun::star::xml::crypto::XSecurityEnvironment > xSecurityEnvironment;
117 
118 	if (index >= 0 && index < ( sal_Int32 )m_vSecurityEnvironments.size())
119 	{
120 		xSecurityEnvironment = m_vSecurityEnvironments[index];
121 	}
122 	else
123 		throw RuntimeException() ;
124 
125 	return xSecurityEnvironment;
126 }
127 
128 ::com::sun::star::uno::Reference< ::com::sun::star::xml::crypto::XSecurityEnvironment > SAL_CALL
getSecurityEnvironment()129 	XMLSecurityContext_NssImpl::getSecurityEnvironment(  )
130 	throw (::com::sun::star::uno::RuntimeException)
131 {
132 	if (m_nDefaultEnvIndex >= 0 && m_nDefaultEnvIndex < ( sal_Int32 )m_vSecurityEnvironments.size())
133 		return getSecurityEnvironmentByIndex(m_nDefaultEnvIndex);
134 	else
135 		throw RuntimeException() ;
136 }
137 
getDefaultSecurityEnvironmentIndex()138 sal_Int32 SAL_CALL XMLSecurityContext_NssImpl::getDefaultSecurityEnvironmentIndex(  )
139 	throw (::com::sun::star::uno::RuntimeException)
140 {
141 	return m_nDefaultEnvIndex ;
142 }
143 
setDefaultSecurityEnvironmentIndex(sal_Int32 nDefaultEnvIndex)144 void SAL_CALL XMLSecurityContext_NssImpl::setDefaultSecurityEnvironmentIndex( sal_Int32 nDefaultEnvIndex )
145 	throw (::com::sun::star::uno::RuntimeException)
146 {
147 	m_nDefaultEnvIndex = nDefaultEnvIndex;
148 }
149 
150 #if 0 //i39448 : old methods should be deleted
151 /* XXMLSecurityContext */
152 void SAL_CALL XMLSecurityContext_NssImpl :: setSecurityEnvironment( const Reference< XSecurityEnvironment >& aSecurityEnvironment ) throw( com::sun::star::security::SecurityInfrastructureException ) {
153 	PK11SlotInfo* slot ;
154 	CERTCertDBHandle* handler ;
155 	//xmlSecKeyPtr key ;
156 	//xmlSecKeyDataPtr keyData ;
157 	PK11SymKey* symKey ;
158 	SECKEYPublicKey* pubKey ;
159 	SECKEYPrivateKey* priKey ;
160 	unsigned int i ;
161 
162 	if( !aSecurityEnvironment.is() )
163 		throw RuntimeException() ;
164 
165 	m_xSecurityEnvironment = aSecurityEnvironment ;
166 
167 	//Clear key manager
168 	if( m_pKeysMngr != NULL ) {
169 		xmlSecKeysMngrDestroy( m_pKeysMngr ) ;
170 		m_pKeysMngr = NULL ;
171 	}
172 
173 	//Create key manager
174 	Reference< XUnoTunnel > xEnvTunnel( m_xSecurityEnvironment , UNO_QUERY ) ;
175 	if( !xEnvTunnel.is() ) {
176 		throw RuntimeException() ;^1
177 	}
178 
179 	SecurityEnvironment_NssImpl* pSecEnv = ( SecurityEnvironment_NssImpl* )xEnvTunnel->getSomething( SecurityEnvironment_NssImpl::getUnoTunnelId() ) ;
180 	if( pSecEnv == NULL )
181 		throw RuntimeException() ;
182 
183 	//todo
184 //	slot = pSecEnv->getCryptoSlot() ;
185 	handler = pSecEnv->getCertDb() ;
186 
187 	/*-
188 	 * The following lines is based on the private version of xmlSec-NSS
189 	 * crypto engine
190 	 */
191 	m_pKeysMngr = xmlSecNssAppliedKeysMngrCreate( slot , handler ) ;
192 	if( m_pKeysMngr == NULL )
193 		throw RuntimeException() ;
194 
195 	/*-
196 	 * Adopt symmetric key into keys manager
197 	 */
198 	for( i = 0 ; ( symKey = pSecEnv->getSymKey( i ) ) != NULL ; i ++ ) {
199 		if( xmlSecNssAppliedKeysMngrSymKeyLoad( m_pKeysMngr, symKey ) < 0 ) {
200 			throw RuntimeException() ;
201 		}
202 	}
203 
204 	/*-
205 	 * Adopt asymmetric public key into keys manager
206 	 */
207 	for( i = 0 ; ( pubKey = pSecEnv->getPubKey( i ) ) != NULL ; i ++ ) {
208 		if( xmlSecNssAppliedKeysMngrPubKeyLoad( m_pKeysMngr, pubKey ) < 0 ) {
209 			throw RuntimeException() ;
210 		}
211 	}
212 
213 	/*-
214 	 * Adopt asymmetric private key into keys manager
215 	 */
216 	for( i = 0 ; ( priKey = pSecEnv->getPriKey( i ) ) != NULL ; i ++ ) {
217 		if( xmlSecNssAppliedKeysMngrPriKeyLoad( m_pKeysMngr, priKey ) < 0 ) {
218 			throw RuntimeException() ;
219 		}
220 	}
221 }
222 
223 /* XXMLSecurityContext */
224 Reference< XSecurityEnvironment > SAL_CALL XMLSecurityContext_NssImpl :: getSecurityEnvironment()
225 	throw (RuntimeException)
226 {
227 	return	m_xSecurityEnvironment ;
228 }
229 #endif
230 
231 
232 /* XInitialization */
initialize(const Sequence<Any> &)233 void SAL_CALL XMLSecurityContext_NssImpl :: initialize( const Sequence< Any >& /*aArguments*/ ) throw( Exception, RuntimeException ) {
234 	// TBD
235 } ;
236 
237 /* XServiceInfo */
getImplementationName()238 OUString SAL_CALL XMLSecurityContext_NssImpl :: getImplementationName() throw( RuntimeException ) {
239 	return impl_getImplementationName() ;
240 }
241 
242 /* XServiceInfo */
supportsService(const OUString & serviceName)243 sal_Bool SAL_CALL XMLSecurityContext_NssImpl :: supportsService( const OUString& serviceName) throw( RuntimeException ) {
244 	Sequence< OUString > seqServiceNames = getSupportedServiceNames() ;
245 	const OUString* pArray = seqServiceNames.getConstArray() ;
246 	for( sal_Int32 i = 0 ; i < seqServiceNames.getLength() ; i ++ ) {
247 		if( *( pArray + i ) == serviceName )
248 			return sal_True ;
249 	}
250 	return sal_False ;
251 }
252 
253 /* XServiceInfo */
getSupportedServiceNames()254 Sequence< OUString > SAL_CALL XMLSecurityContext_NssImpl :: getSupportedServiceNames() throw( RuntimeException ) {
255 	return impl_getSupportedServiceNames() ;
256 }
257 
258 //Helper for XServiceInfo
impl_getSupportedServiceNames()259 Sequence< OUString > XMLSecurityContext_NssImpl :: impl_getSupportedServiceNames() {
260 	::osl::Guard< ::osl::Mutex > aGuard( ::osl::Mutex::getGlobalMutex() ) ;
261 	Sequence< OUString > seqServiceNames( 1 ) ;
262 	seqServiceNames.getArray()[0] = OUString::createFromAscii( "com.sun.star.xml.crypto.XMLSecurityContext" ) ;
263 	return seqServiceNames ;
264 }
265 
impl_getImplementationName()266 OUString XMLSecurityContext_NssImpl :: impl_getImplementationName() throw( RuntimeException ) {
267 	return OUString::createFromAscii( "com.sun.star.xml.security.bridge.xmlsec.XMLSecurityContext_NssImpl" ) ;
268 }
269 
270 //Helper for registry
impl_createInstance(const Reference<XMultiServiceFactory> & aServiceManager)271 Reference< XInterface > SAL_CALL XMLSecurityContext_NssImpl :: impl_createInstance( const Reference< XMultiServiceFactory >& aServiceManager ) throw( RuntimeException ) {
272 	return Reference< XInterface >( *new XMLSecurityContext_NssImpl( aServiceManager ) ) ;
273 }
274 
impl_createFactory(const Reference<XMultiServiceFactory> & aServiceManager)275 Reference< XSingleServiceFactory > XMLSecurityContext_NssImpl :: impl_createFactory( const Reference< XMultiServiceFactory >& aServiceManager ) {
276 	//Reference< XSingleServiceFactory > xFactory ;
277 	//xFactory = ::cppu::createSingleFactory( aServiceManager , impl_getImplementationName , impl_createInstance , impl_getSupportedServiceNames ) ;
278 	//return xFactory ;
279 	return ::cppu::createSingleFactory( aServiceManager , impl_getImplementationName() , impl_createInstance , impl_getSupportedServiceNames() ) ;
280 }
281 
282 #if 0 //not useful any longer
283 /* XUnoTunnel */
284 sal_Int64 SAL_CALL XMLSecurityContext_NssImpl :: getSomething( const Sequence< sal_Int8 >& aIdentifier )
285 throw (RuntimeException)
286 {
287 	if( aIdentifier.getLength() == 16 && 0 == rtl_compareMemory( getUnoTunnelId().getConstArray(), aIdentifier.getConstArray(), 16 ) ) {
288 		return ( sal_Int64 )this ;
289 	}
290 	return 0 ;
291 }
292 
293 /* XUnoTunnel extension */
294 const Sequence< sal_Int8>& XMLSecurityContext_NssImpl :: getUnoTunnelId() {
295 	static Sequence< sal_Int8 >* pSeq = 0 ;
296 	if( !pSeq ) {
297 		::osl::Guard< ::osl::Mutex > aGuard( ::osl::Mutex::getGlobalMutex() ) ;
298 		if( !pSeq ) {
299 			static Sequence< sal_Int8> aSeq( 16 ) ;
300 			rtl_createUuid( ( sal_uInt8* )aSeq.getArray() , 0 , sal_True ) ;
301 			pSeq = &aSeq ;
302 		}
303 	}
304 	return *pSeq ;
305 }
306 
307 /* XUnoTunnel extension */
308 XMLSecurityContext_NssImpl* XMLSecurityContext_NssImpl :: getImplementation( const Reference< XInterface > xObj ) {
309 	Reference< XUnoTunnel > xUT( xObj , UNO_QUERY ) ;
310 	if( xUT.is() ) {
311 		return ( XMLSecurityContext_NssImpl* )xUT->getSomething( getUnoTunnelId() ) ;
312 	} else
313 		return NULL ;
314 }
315 
316 /* Native methods */
317 xmlSecKeysMngrPtr XMLSecurityContext_NssImpl :: keysManager() throw( Exception, RuntimeException ) {
318 	return m_pKeysMngr ;
319 }
320 
321 #endif
322