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 {
94 if( !aSecurityEnvironment.is() )
95 {
96 throw RuntimeException() ;
97 }
98
99 m_vSecurityEnvironments.push_back( aSecurityEnvironment );
100
101 return m_vSecurityEnvironments.size() - 1 ;
102 }
103
104
getSecurityEnvironmentNumber()105 sal_Int32 SAL_CALL XMLSecurityContext_NssImpl::getSecurityEnvironmentNumber( )
106 {
107 return m_vSecurityEnvironments.size();
108 }
109
110 ::com::sun::star::uno::Reference< ::com::sun::star::xml::crypto::XSecurityEnvironment > SAL_CALL
getSecurityEnvironmentByIndex(sal_Int32 index)111 XMLSecurityContext_NssImpl::getSecurityEnvironmentByIndex( sal_Int32 index )
112 {
113 ::com::sun::star::uno::Reference< ::com::sun::star::xml::crypto::XSecurityEnvironment > xSecurityEnvironment;
114
115 if (index >= 0 && index < ( sal_Int32 )m_vSecurityEnvironments.size())
116 {
117 xSecurityEnvironment = m_vSecurityEnvironments[index];
118 }
119 else
120 throw RuntimeException() ;
121
122 return xSecurityEnvironment;
123 }
124
125 ::com::sun::star::uno::Reference< ::com::sun::star::xml::crypto::XSecurityEnvironment > SAL_CALL
getSecurityEnvironment()126 XMLSecurityContext_NssImpl::getSecurityEnvironment( )
127 {
128 if (m_nDefaultEnvIndex >= 0 && m_nDefaultEnvIndex < ( sal_Int32 )m_vSecurityEnvironments.size())
129 return getSecurityEnvironmentByIndex(m_nDefaultEnvIndex);
130 else
131 throw RuntimeException() ;
132 }
133
getDefaultSecurityEnvironmentIndex()134 sal_Int32 SAL_CALL XMLSecurityContext_NssImpl::getDefaultSecurityEnvironmentIndex( )
135 {
136 return m_nDefaultEnvIndex ;
137 }
138
setDefaultSecurityEnvironmentIndex(sal_Int32 nDefaultEnvIndex)139 void SAL_CALL XMLSecurityContext_NssImpl::setDefaultSecurityEnvironmentIndex( sal_Int32 nDefaultEnvIndex )
140 {
141 m_nDefaultEnvIndex = nDefaultEnvIndex;
142 }
143
144 #if 0 //i39448 : old methods should be deleted
145 /* XXMLSecurityContext */
146 void SAL_CALL XMLSecurityContext_NssImpl :: setSecurityEnvironment( const Reference< XSecurityEnvironment >& aSecurityEnvironment ) {
147 PK11SlotInfo* slot ;
148 CERTCertDBHandle* handler ;
149 //xmlSecKeyPtr key ;
150 //xmlSecKeyDataPtr keyData ;
151 PK11SymKey* symKey ;
152 SECKEYPublicKey* pubKey ;
153 SECKEYPrivateKey* priKey ;
154 unsigned int i ;
155
156 if( !aSecurityEnvironment.is() )
157 throw RuntimeException() ;
158
159 m_xSecurityEnvironment = aSecurityEnvironment ;
160
161 //Clear key manager
162 if( m_pKeysMngr != NULL ) {
163 xmlSecKeysMngrDestroy( m_pKeysMngr ) ;
164 m_pKeysMngr = NULL ;
165 }
166
167 //Create key manager
168 Reference< XUnoTunnel > xEnvTunnel( m_xSecurityEnvironment , UNO_QUERY ) ;
169 if( !xEnvTunnel.is() ) {
170 throw RuntimeException() ;^1
171 }
172
173 SecurityEnvironment_NssImpl* pSecEnv = ( SecurityEnvironment_NssImpl* )xEnvTunnel->getSomething( SecurityEnvironment_NssImpl::getUnoTunnelId() ) ;
174 if( pSecEnv == NULL )
175 throw RuntimeException() ;
176
177 //todo
178 // slot = pSecEnv->getCryptoSlot() ;
179 handler = pSecEnv->getCertDb() ;
180
181 /*-
182 * The following lines is based on the private version of xmlSec-NSS
183 * crypto engine
184 */
185 m_pKeysMngr = xmlSecNssAppliedKeysMngrCreate( slot , handler ) ;
186 if( m_pKeysMngr == NULL )
187 throw RuntimeException() ;
188
189 /*-
190 * Adopt symmetric key into keys manager
191 */
192 for( i = 0 ; ( symKey = pSecEnv->getSymKey( i ) ) != NULL ; i ++ ) {
193 if( xmlSecNssAppliedKeysMngrSymKeyLoad( m_pKeysMngr, symKey ) < 0 ) {
194 throw RuntimeException() ;
195 }
196 }
197
198 /*-
199 * Adopt asymmetric public key into keys manager
200 */
201 for( i = 0 ; ( pubKey = pSecEnv->getPubKey( i ) ) != NULL ; i ++ ) {
202 if( xmlSecNssAppliedKeysMngrPubKeyLoad( m_pKeysMngr, pubKey ) < 0 ) {
203 throw RuntimeException() ;
204 }
205 }
206
207 /*-
208 * Adopt asymmetric private key into keys manager
209 */
210 for( i = 0 ; ( priKey = pSecEnv->getPriKey( i ) ) != NULL ; i ++ ) {
211 if( xmlSecNssAppliedKeysMngrPriKeyLoad( m_pKeysMngr, priKey ) < 0 ) {
212 throw RuntimeException() ;
213 }
214 }
215 }
216
217 /* XXMLSecurityContext */
218 Reference< XSecurityEnvironment > SAL_CALL XMLSecurityContext_NssImpl :: getSecurityEnvironment()
219 {
220 return m_xSecurityEnvironment ;
221 }
222 #endif
223
224
225 /* XInitialization */
initialize(const Sequence<Any> &)226 void SAL_CALL XMLSecurityContext_NssImpl :: initialize( const Sequence< Any >& /*aArguments*/ ) {
227 // TBD
228 } ;
229
230 /* XServiceInfo */
getImplementationName()231 OUString SAL_CALL XMLSecurityContext_NssImpl :: getImplementationName() {
232 return impl_getImplementationName() ;
233 }
234
235 /* XServiceInfo */
supportsService(const OUString & serviceName)236 sal_Bool SAL_CALL XMLSecurityContext_NssImpl :: supportsService( const OUString& serviceName) {
237 Sequence< OUString > seqServiceNames = getSupportedServiceNames() ;
238 const OUString* pArray = seqServiceNames.getConstArray() ;
239 for( sal_Int32 i = 0 ; i < seqServiceNames.getLength() ; i ++ ) {
240 if( *( pArray + i ) == serviceName )
241 return sal_True ;
242 }
243 return sal_False ;
244 }
245
246 /* XServiceInfo */
getSupportedServiceNames()247 Sequence< OUString > SAL_CALL XMLSecurityContext_NssImpl :: getSupportedServiceNames() {
248 return impl_getSupportedServiceNames() ;
249 }
250
251 //Helper for XServiceInfo
impl_getSupportedServiceNames()252 Sequence< OUString > XMLSecurityContext_NssImpl :: impl_getSupportedServiceNames() {
253 ::osl::Guard< ::osl::Mutex > aGuard( ::osl::Mutex::getGlobalMutex() ) ;
254 Sequence< OUString > seqServiceNames( 1 ) ;
255 seqServiceNames.getArray()[0] = OUString::createFromAscii( "com.sun.star.xml.crypto.XMLSecurityContext" ) ;
256 return seqServiceNames ;
257 }
258
impl_getImplementationName()259 OUString XMLSecurityContext_NssImpl :: impl_getImplementationName() {
260 return OUString::createFromAscii( "com.sun.star.xml.security.bridge.xmlsec.XMLSecurityContext_NssImpl" ) ;
261 }
262
263 //Helper for registry
impl_createInstance(const Reference<XMultiServiceFactory> & aServiceManager)264 Reference< XInterface > SAL_CALL XMLSecurityContext_NssImpl :: impl_createInstance( const Reference< XMultiServiceFactory >& aServiceManager ) {
265 return Reference< XInterface >( *new XMLSecurityContext_NssImpl( aServiceManager ) ) ;
266 }
267
impl_createFactory(const Reference<XMultiServiceFactory> & aServiceManager)268 Reference< XSingleServiceFactory > XMLSecurityContext_NssImpl :: impl_createFactory( const Reference< XMultiServiceFactory >& aServiceManager ) {
269 //Reference< XSingleServiceFactory > xFactory ;
270 //xFactory = ::cppu::createSingleFactory( aServiceManager , impl_getImplementationName , impl_createInstance , impl_getSupportedServiceNames ) ;
271 //return xFactory ;
272 return ::cppu::createSingleFactory( aServiceManager , impl_getImplementationName() , impl_createInstance , impl_getSupportedServiceNames() ) ;
273 }
274
275 #if 0 //not useful any longer
276 /* XUnoTunnel */
277 sal_Int64 SAL_CALL XMLSecurityContext_NssImpl :: getSomething( const Sequence< sal_Int8 >& aIdentifier )
278 {
279 if( aIdentifier.getLength() == 16 && 0 == rtl_compareMemory( getUnoTunnelId().getConstArray(), aIdentifier.getConstArray(), 16 ) ) {
280 return ( sal_Int64 )this ;
281 }
282 return 0 ;
283 }
284
285 /* XUnoTunnel extension */
286 const Sequence< sal_Int8>& XMLSecurityContext_NssImpl :: getUnoTunnelId() {
287 static Sequence< sal_Int8 >* pSeq = 0 ;
288 if( !pSeq ) {
289 ::osl::Guard< ::osl::Mutex > aGuard( ::osl::Mutex::getGlobalMutex() ) ;
290 if( !pSeq ) {
291 static Sequence< sal_Int8> aSeq( 16 ) ;
292 rtl_createUuid( ( sal_uInt8* )aSeq.getArray() , 0 , sal_True ) ;
293 pSeq = &aSeq ;
294 }
295 }
296 return *pSeq ;
297 }
298
299 /* XUnoTunnel extension */
300 XMLSecurityContext_NssImpl* XMLSecurityContext_NssImpl :: getImplementation( const Reference< XInterface > xObj ) {
301 Reference< XUnoTunnel > xUT( xObj , UNO_QUERY ) ;
302 if( xUT.is() ) {
303 return ( XMLSecurityContext_NssImpl* )xUT->getSomething( getUnoTunnelId() ) ;
304 } else
305 return NULL ;
306 }
307
308 /* Native methods */
309 xmlSecKeysMngrPtr XMLSecurityContext_NssImpl :: keysManager() {
310 return m_pKeysMngr ;
311 }
312
313 #endif
314