Update ooo320-m1
[ooovba.git] / xmlsecurity / source / xmlsec / xsec_xmlsec.cxx
blobf767435239cf82a795b6c88a5ebc116c184e12e6
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: xsec_xmlsec.cxx,v $
10 * $Revision: 1.6 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_xmlsecurity.hxx"
34 #include <sal/config.h>
35 #include <stdio.h>
37 #include <osl/mutex.hxx>
38 #include <osl/thread.h>
39 #include <cppuhelper/factory.hxx>
40 #include <cppuhelper/implbase1.hxx>
41 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
42 #include <com/sun/star/security/XSerialNumberAdapter.hpp>
44 #include "xmlelementwrapper_xmlsecimpl.hxx"
45 #include "xmldocumentwrapper_xmlsecimpl.hxx"
46 #include "xmlsecurity/biginteger.hxx"
48 using namespace ::rtl;
49 using namespace ::cppu;
50 using namespace ::com::sun::star::uno;
51 using namespace ::com::sun::star::lang;
52 using namespace ::com::sun::star::registry;
54 namespace
56 class SerialNumberAdapterImpl : public WeakImplHelper1<
57 ::com::sun::star::security::XSerialNumberAdapter >
59 virtual OUString SAL_CALL toString( const Sequence< sal_Int8 >& rSerialNumber )
60 throw (RuntimeException)
62 return bigIntegerToNumericString(rSerialNumber);
64 virtual Sequence< sal_Int8 > SAL_CALL toSequence( const OUString& rSerialNumber )
65 throw (RuntimeException)
67 return numericStringToBigInteger(rSerialNumber);
71 OUString SerialNumberAdapterImpl_getImplementationName()
72 throw (RuntimeException)
74 return OUString(RTL_CONSTASCII_USTRINGPARAM(
75 "com.sun.star.security.SerialNumberAdapter"));
78 Sequence< OUString > SerialNumberAdapterImpl_getSupportedServiceNames()
79 throw (RuntimeException)
81 Sequence < OUString > aRet(1);
82 OUString* pArray = aRet.getArray();
83 pArray[0] = OUString(RTL_CONSTASCII_USTRINGPARAM(
84 "com.sun.star.security.SerialNumberAdapter" ) );
85 return aRet;
88 Reference< XInterface > SerialNumberAdapterImpl_createInstance(
89 const Reference< XComponentContext > &) throw( Exception )
91 return Reference< XInterface >( *new SerialNumberAdapterImpl() );
96 extern "C"
99 #if defined( XMLSEC_CRYPTO_NSS )
100 extern sal_Bool nss_component_writeInfo( void*, void* );
101 extern void* nss_component_getFactory( const sal_Char*, void*, void* );
102 #endif
104 #if defined( XMLSEC_CRYPTO_MSCRYPTO )
105 extern sal_Bool mscrypt_component_writeInfo( void*, void* );
106 extern void* mscrypt_component_getFactory( const sal_Char*, void*, void* );
107 #endif
109 void SAL_CALL component_getImplementationEnvironment(
110 const sal_Char ** ppEnvTypeName, uno_Environment **)
112 *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
116 sal_Bool SAL_CALL component_writeInfo( void* pServiceManager , void* pRegistryKey )
118 sal_Bool result = sal_False;
119 sal_Int32 i ;
120 OUString sKeyName ;
121 Reference< XRegistryKey > xNewKey ;
122 Sequence< OUString > seqServices ;
123 Reference< XRegistryKey > xKey( reinterpret_cast< XRegistryKey* >( pRegistryKey ) ) ;
125 if( xKey.is() ) {
126 // try {
127 // XMLElementWrapper_XmlSecImpl
128 sKeyName = OUString( RTL_CONSTASCII_USTRINGPARAM( "/" ) ) ;
129 sKeyName += XMLElementWrapper_XmlSecImpl_getImplementationName() ;
130 sKeyName += OUString::createFromAscii( "/UNO/SERVICES" ) ;
132 xNewKey = xKey->createKey( sKeyName ) ;
133 if( xNewKey.is() ) {
134 seqServices = XMLElementWrapper_XmlSecImpl_getSupportedServiceNames() ;
135 for( i = seqServices.getLength() ; i -- ; )
136 xNewKey->createKey( seqServices.getConstArray()[i] ) ;
139 // XMLDocumentWrapper_XmlSecImpl
140 sKeyName = OUString( RTL_CONSTASCII_USTRINGPARAM( "/" ) ) ;
141 sKeyName += XMLDocumentWrapper_XmlSecImpl_getImplementationName() ;
142 sKeyName += OUString::createFromAscii( "/UNO/SERVICES" ) ;
144 xNewKey = xKey->createKey( sKeyName ) ;
145 if( xNewKey.is() ) {
146 seqServices = XMLDocumentWrapper_XmlSecImpl_getSupportedServiceNames() ;
147 for( i = seqServices.getLength() ; i -- ; )
148 xNewKey->createKey( seqServices.getConstArray()[i] ) ;
151 // SerialNumberAdapterImpl
152 sKeyName = OUString( RTL_CONSTASCII_USTRINGPARAM( "/" ) ) ;
153 sKeyName += SerialNumberAdapterImpl_getImplementationName() ;
154 sKeyName += OUString::createFromAscii( "/UNO/SERVICES" ) ;
156 xNewKey = xKey->createKey( sKeyName ) ;
157 if( xNewKey.is() ) {
158 seqServices = SerialNumberAdapterImpl_getSupportedServiceNames() ;
159 for( i = seqServices.getLength() ; i -- ; )
160 xNewKey->createKey( seqServices.getConstArray()[i] ) ;
163 #if defined( XMLSEC_CRYPTO_NSS )
164 result = nss_component_writeInfo( pServiceManager, pRegistryKey ) ;
165 if( !result )
166 return sal_False ;
167 #endif
169 #if defined( XMLSEC_CRYPTO_MSCRYPTO )
170 result = mscrypt_component_writeInfo( pServiceManager, pRegistryKey ) ;
171 if( !result )
172 return sal_False ;
173 #endif
175 //} catch( InvalidRegistryException & ) {
176 // //we should not ignore exceptions
177 // return sal_False ;
181 return result;
184 void* SAL_CALL component_getFactory( const sal_Char* pImplName , void* pServiceManager , void* pRegistryKey )
186 void* pRet = 0;
187 Reference< XInterface > xFactory ;
189 if( pImplName != NULL && pServiceManager != NULL ) {
190 if( XMLElementWrapper_XmlSecImpl_getImplementationName().equals( OUString::createFromAscii( pImplName ) ) )
192 xFactory = Reference< XSingleServiceFactory >( createSingleFactory(
193 reinterpret_cast< XMultiServiceFactory * >( pServiceManager ),
194 OUString::createFromAscii( pImplName ),
195 XMLElementWrapper_XmlSecImpl_createInstance, XMLElementWrapper_XmlSecImpl_getSupportedServiceNames() ) );
197 else if( XMLDocumentWrapper_XmlSecImpl_getImplementationName().equals( OUString::createFromAscii( pImplName ) ) )
199 xFactory = Reference< XSingleServiceFactory >( createSingleFactory(
200 reinterpret_cast< XMultiServiceFactory * >( pServiceManager ),
201 OUString::createFromAscii( pImplName ),
202 XMLDocumentWrapper_XmlSecImpl_createInstance, XMLDocumentWrapper_XmlSecImpl_getSupportedServiceNames() ) );
204 else if( SerialNumberAdapterImpl_getImplementationName().equals( OUString::createFromAscii( pImplName ) ) )
206 xFactory = ::cppu::createSingleComponentFactory(
207 SerialNumberAdapterImpl_createInstance,
208 OUString::createFromAscii( pImplName ),
209 SerialNumberAdapterImpl_getSupportedServiceNames() );
213 if( xFactory.is() ) {
214 xFactory->acquire() ;
215 pRet = xFactory.get() ;
216 } else {
217 #if defined( XMLSEC_CRYPTO_NSS )
218 pRet = nss_component_getFactory( pImplName, pServiceManager, pRegistryKey ) ;
219 if( pRet != NULL )
220 return pRet ;
221 #endif
223 #if defined( XMLSEC_CRYPTO_MSCRYPTO )
224 pRet = mscrypt_component_getFactory( pImplName, pServiceManager, pRegistryKey ) ;
225 if( pRet != NULL )
226 return pRet ;
227 #endif
230 return pRet ;