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_mscryptimpl.hxx"
29
30 #ifndef _XMLSECURITYCONTEXT_MSCRYPTIMPL_HXX_
31 #include "xmlsecuritycontext_mscryptimpl.hxx"
32 #endif
33 #include "xmlstreamio.hxx"
34
35 #include "xmlsec/xmlsec.h"
36 #include "xmlsec/keysmngr.h"
37 #include "xmlsec/crypto.h"
38 #include "xmlsec/mscrypto/akmngr.h"
39
40 using namespace ::com::sun::star::uno ;
41 using namespace ::com::sun::star::lang ;
42 using ::com::sun::star::lang::XMultiServiceFactory ;
43 using ::com::sun::star::lang::XSingleServiceFactory ;
44 using ::rtl::OUString ;
45
46 using ::com::sun::star::xml::crypto::XSecurityEnvironment ;
47 using ::com::sun::star::xml::crypto::XXMLSecurityContext ;
48
XMLSecurityContext_MSCryptImpl(const Reference<XMultiServiceFactory> & aFactory)49 XMLSecurityContext_MSCryptImpl :: XMLSecurityContext_MSCryptImpl( const Reference< XMultiServiceFactory >& aFactory )
50 ://m_pKeysMngr( NULL ) ,
51 m_xServiceManager( aFactory ),
52 m_xSecurityEnvironment( NULL )
53 {
54 //Init xmlsec library
55 if( xmlSecInit() < 0 ) {
56 throw RuntimeException() ;
57 }
58
59 //Init xmlsec crypto engine library
60 if( xmlSecCryptoInit() < 0 ) {
61 xmlSecShutdown() ;
62 throw RuntimeException() ;
63 }
64
65 //Enable external stream handlers
66 if( xmlEnableStreamInputCallbacks() < 0 ) {
67 xmlSecCryptoShutdown() ;
68 xmlSecShutdown() ;
69 throw RuntimeException() ;
70 }
71 }
72
~XMLSecurityContext_MSCryptImpl()73 XMLSecurityContext_MSCryptImpl :: ~XMLSecurityContext_MSCryptImpl() {
74 xmlDisableStreamInputCallbacks() ;
75 xmlSecCryptoShutdown() ;
76 xmlSecShutdown() ;
77 }
78
79 //i39448 : new methods
addSecurityEnvironment(const::com::sun::star::uno::Reference<::com::sun::star::xml::crypto::XSecurityEnvironment> & aSecurityEnvironment)80 sal_Int32 SAL_CALL XMLSecurityContext_MSCryptImpl::addSecurityEnvironment(
81 const ::com::sun::star::uno::Reference< ::com::sun::star::xml::crypto::XSecurityEnvironment >& aSecurityEnvironment)
82 {
83 if( !aSecurityEnvironment.is() )
84 {
85 throw RuntimeException() ;
86 }
87
88 m_xSecurityEnvironment = aSecurityEnvironment;
89
90 return 0;
91 }
92
93
getSecurityEnvironmentNumber()94 sal_Int32 SAL_CALL XMLSecurityContext_MSCryptImpl::getSecurityEnvironmentNumber( )
95 {
96 return 1;
97 }
98
99 ::com::sun::star::uno::Reference< ::com::sun::star::xml::crypto::XSecurityEnvironment > SAL_CALL
getSecurityEnvironmentByIndex(sal_Int32 index)100 XMLSecurityContext_MSCryptImpl::getSecurityEnvironmentByIndex( sal_Int32 index )
101 {
102 if (index == 0)
103 {
104 return m_xSecurityEnvironment;
105 }
106 else
107 throw RuntimeException() ;
108 }
109
110 ::com::sun::star::uno::Reference< ::com::sun::star::xml::crypto::XSecurityEnvironment > SAL_CALL
getSecurityEnvironment()111 XMLSecurityContext_MSCryptImpl::getSecurityEnvironment( )
112 {
113 return m_xSecurityEnvironment;
114 }
115
getDefaultSecurityEnvironmentIndex()116 sal_Int32 SAL_CALL XMLSecurityContext_MSCryptImpl::getDefaultSecurityEnvironmentIndex( )
117 {
118 return 0;
119 }
120
setDefaultSecurityEnvironmentIndex(sal_Int32)121 void SAL_CALL XMLSecurityContext_MSCryptImpl::setDefaultSecurityEnvironmentIndex( sal_Int32 /*nDefaultEnvIndex*/ )
122 {
123 //dummy
124 }
125
126 #if 0
127 /* XXMLSecurityContext */
128 void SAL_CALL XMLSecurityContext_MSCryptImpl :: setSecurityEnvironment( const Reference< XSecurityEnvironment >& aSecurityEnvironment ) {
129 HCERTSTORE hkeyStore ;
130 HCERTSTORE hCertStore ;
131 HCRYPTKEY symKey ;
132 HCRYPTKEY pubKey ;
133 HCRYPTKEY priKey ;
134 unsigned int i ;
135
136 if( !aSecurityEnvironment.is() )
137 throw RuntimeException() ;
138
139 m_xSecurityEnvironment = aSecurityEnvironment ;
140
141 //Clear key manager
142 if( m_pKeysMngr != NULL ) {
143 xmlSecKeysMngrDestroy( m_pKeysMngr ) ;
144 m_pKeysMngr = NULL ;
145 }
146
147 //Create key manager
148 Reference< XUnoTunnel > xEnvTunnel( m_xSecurityEnvironment , UNO_QUERY ) ;
149 if( !xEnvTunnel.is() ) {
150 throw RuntimeException() ;
151 }
152
153 SecurityEnvironment_MSCryptImpl* pSecEnv = ( SecurityEnvironment_MSCryptImpl* )xEnvTunnel->getSomething( SecurityEnvironment_MSCryptImpl::getUnoTunnelId() ) ;
154 if( pSecEnv == NULL )
155 throw RuntimeException() ;
156
157 hkeyStore = pSecEnv->getCryptoSlot() ;
158 hCertStore = pSecEnv->getCertDb() ;
159
160 /*-
161 * The following lines is based on the of xmlsec-mscrypto crypto engine
162 */
163 m_pKeysMngr = xmlSecMSCryptoAppliedKeysMngrCreate( hkeyStore , hCertStore ) ;
164 if( m_pKeysMngr == NULL )
165 throw RuntimeException() ;
166
167 /*-
168 * Adopt symmetric key into keys manager
169 */
170 for( i = 0 ; ( symKey = pSecEnv->getSymKey( i ) ) != NULL ; i ++ ) {
171 if( xmlSecMSCryptoAppliedKeysMngrSymKeyLoad( m_pKeysMngr, symKey ) < 0 ) {
172 throw RuntimeException() ;
173 }
174 }
175
176 /*-
177 * Adopt asymmetric public key into keys manager
178 */
179 for( i = 0 ; ( pubKey = pSecEnv->getPubKey( i ) ) != NULL ; i ++ ) {
180 if( xmlSecMSCryptoAppliedKeysMngrPubKeyLoad( m_pKeysMngr, pubKey ) < 0 ) {
181 throw RuntimeException() ;
182 }
183 }
184
185 /*-
186 * Adopt asymmetric private key into keys manager
187 */
188 for( i = 0 ; ( priKey = pSecEnv->getPriKey( i ) ) != NULL ; i ++ ) {
189 if( xmlSecMSCryptoAppliedKeysMngrPriKeyLoad( m_pKeysMngr, priKey ) < 0 ) {
190 throw RuntimeException() ;
191 }
192 }
193
194 /*-
195 * Adopt system default certificate store.
196 */
197 if( pSecEnv->defaultEnabled() ) {
198 HCERTSTORE hSystemStore ;
199
200 //Add system key store into the keys manager.
201 hSystemStore = CertOpenSystemStore( 0, "MY" ) ;
202 if( hSystemStore != NULL ) {
203 if( xmlSecMSCryptoAppliedKeysMngrAdoptKeyStore( m_pKeysMngr, hSystemStore ) < 0 ) {
204 CertCloseStore( hSystemStore, CERT_CLOSE_STORE_CHECK_FLAG ) ;
205 throw RuntimeException() ;
206 }
207 }
208
209 //Add system root store into the keys manager.
210 hSystemStore = CertOpenSystemStore( 0, "Root" ) ;
211 if( hSystemStore != NULL ) {
212 if( xmlSecMSCryptoAppliedKeysMngrAdoptTrustedStore( m_pKeysMngr, hSystemStore ) < 0 ) {
213 CertCloseStore( hSystemStore, CERT_CLOSE_STORE_CHECK_FLAG ) ;
214 throw RuntimeException() ;
215 }
216 }
217
218 //Add system trusted store into the keys manager.
219 hSystemStore = CertOpenSystemStore( 0, "Trust" ) ;
220 if( hSystemStore != NULL ) {
221 if( xmlSecMSCryptoAppliedKeysMngrAdoptUntrustedStore( m_pKeysMngr, hSystemStore ) < 0 ) {
222 CertCloseStore( hSystemStore, CERT_CLOSE_STORE_CHECK_FLAG ) ;
223 throw RuntimeException() ;
224 }
225 }
226
227 //Add system CA store into the keys manager.
228 hSystemStore = CertOpenSystemStore( 0, "CA" ) ;
229 if( hSystemStore != NULL ) {
230 if( xmlSecMSCryptoAppliedKeysMngrAdoptUntrustedStore( m_pKeysMngr, hSystemStore ) < 0 ) {
231 CertCloseStore( hSystemStore, CERT_CLOSE_STORE_CHECK_FLAG ) ;
232 throw RuntimeException() ;
233 }
234 }
235 }
236 }
237
238 /* XXMLSecurityContext */
239 Reference< XSecurityEnvironment > SAL_CALL XMLSecurityContext_MSCryptImpl :: getSecurityEnvironment()
240 {
241 return m_xSecurityEnvironment ;
242 }
243 #endif
244
245 /* XInitialization */
initialize(const Sequence<Any> &)246 void SAL_CALL XMLSecurityContext_MSCryptImpl :: initialize( const Sequence< Any >& /*aArguments*/ ) {
247 // TBD
248 } ;
249
250 /* XServiceInfo */
getImplementationName()251 OUString SAL_CALL XMLSecurityContext_MSCryptImpl :: getImplementationName() {
252 return impl_getImplementationName() ;
253 }
254
255 /* XServiceInfo */
supportsService(const OUString & serviceName)256 sal_Bool SAL_CALL XMLSecurityContext_MSCryptImpl :: supportsService( const OUString& serviceName) {
257 Sequence< OUString > seqServiceNames = getSupportedServiceNames() ;
258 const OUString* pArray = seqServiceNames.getConstArray() ;
259 for( sal_Int32 i = 0 ; i < seqServiceNames.getLength() ; i ++ ) {
260 if( *( pArray + i ) == serviceName )
261 return sal_True ;
262 }
263 return sal_False ;
264 }
265
266 /* XServiceInfo */
getSupportedServiceNames()267 Sequence< OUString > SAL_CALL XMLSecurityContext_MSCryptImpl :: getSupportedServiceNames() {
268 return impl_getSupportedServiceNames() ;
269 }
270
271 //Helper for XServiceInfo
impl_getSupportedServiceNames()272 Sequence< OUString > XMLSecurityContext_MSCryptImpl :: impl_getSupportedServiceNames() {
273 ::osl::Guard< ::osl::Mutex > aGuard( ::osl::Mutex::getGlobalMutex() ) ;
274 Sequence< OUString > seqServiceNames( 1 ) ;
275 seqServiceNames.getArray()[0] = OUString::createFromAscii( "com.sun.star.xml.crypto.XMLSecurityContext" ) ;
276 return seqServiceNames ;
277 }
278
impl_getImplementationName()279 OUString XMLSecurityContext_MSCryptImpl :: impl_getImplementationName() {
280 return OUString::createFromAscii( "com.sun.star.xml.security.bridge.xmlsec.XMLSecurityContext_MSCryptImpl" ) ;
281 }
282
283 //Helper for registry
impl_createInstance(const Reference<XMultiServiceFactory> & aServiceManager)284 Reference< XInterface > SAL_CALL XMLSecurityContext_MSCryptImpl :: impl_createInstance( const Reference< XMultiServiceFactory >& aServiceManager ) {
285 return Reference< XInterface >( *new XMLSecurityContext_MSCryptImpl( aServiceManager ) ) ;
286 }
287
impl_createFactory(const Reference<XMultiServiceFactory> & aServiceManager)288 Reference< XSingleServiceFactory > XMLSecurityContext_MSCryptImpl :: impl_createFactory( const Reference< XMultiServiceFactory >& aServiceManager ) {
289 //Reference< XSingleServiceFactory > xFactory ;
290 //xFactory = ::cppu::createSingleFactory( aServiceManager , impl_getImplementationName , impl_createInstance , impl_getSupportedServiceNames ) ;
291 //return xFactory ;
292 return ::cppu::createSingleFactory( aServiceManager , impl_getImplementationName() , impl_createInstance , impl_getSupportedServiceNames() ) ;
293 }
294
295 #if 0
296 /* XUnoTunnel */
297 sal_Int64 SAL_CALL XMLSecurityContext_MSCryptImpl :: getSomething( const Sequence< sal_Int8 >& aIdentifier )
298 {
299 if( aIdentifier.getLength() == 16 && 0 == rtl_compareMemory( getUnoTunnelId().getConstArray(), aIdentifier.getConstArray(), 16 ) ) {
300 return ( sal_Int64 )this ;
301 }
302 return 0 ;
303 }
304
305 /* XUnoTunnel extension */
306 const Sequence< sal_Int8>& XMLSecurityContext_MSCryptImpl :: getUnoTunnelId() {
307 static Sequence< sal_Int8 >* pSeq = 0 ;
308 if( !pSeq ) {
309 ::osl::Guard< ::osl::Mutex > aGuard( ::osl::Mutex::getGlobalMutex() ) ;
310 if( !pSeq ) {
311 static Sequence< sal_Int8> aSeq( 16 ) ;
312 rtl_createUuid( ( sal_uInt8* )aSeq.getArray() , 0 , sal_True ) ;
313 pSeq = &aSeq ;
314 }
315 }
316 return *pSeq ;
317 }
318
319 /* XUnoTunnel extension */
320 XMLSecurityContext_MSCryptImpl* XMLSecurityContext_MSCryptImpl :: getImplementation( const Reference< XInterface > xObj ) {
321 Reference< XUnoTunnel > xUT( xObj , UNO_QUERY ) ;
322 if( xUT.is() ) {
323 return ( XMLSecurityContext_MSCryptImpl* )xUT->getSomething( getUnoTunnelId() ) ;
324 } else
325 return NULL ;
326 }
327
328 /* Native methods */
329 xmlSecKeysMngrPtr XMLSecurityContext_MSCryptImpl :: keysManager() {
330 return m_pKeysMngr ;
331 }
332 #endif
333