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 #ifndef _XSECURITYENVIRONMENT_MSCRYPTIMPL_HXX_
25 #define _XSECURITYENVIRONMENT_MSCRYPTIMPL_HXX_
26 
27 #ifdef _MSC_VER
28 #pragma warning(push,1)
29 #endif
30 #include <windows.h>
31 #include <wincrypt.h>
32 #ifdef _MSC_VER
33 #pragma warning(pop)
34 #endif
35 #include <sal/config.h>
36 #include <rtl/ustring.hxx>
37 #include <cppuhelper/factory.hxx>
38 #include <cppuhelper/implbase4.hxx>
39 #include <com/sun/star/uno/Exception.hpp>
40 
41 #ifndef _COM_SUN_STAR_UNO_REFERENCE_HPP_
42 #include <com/sun/star/uno/Reference.hxx>
43 #endif
44 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
45 
46 #ifndef _COM_SUN_STAR_LANG_XSECVICEINFO_HPP_
47 #include <com/sun/star/lang/XServiceInfo.hpp>
48 #endif
49 #include <com/sun/star/lang/XInitialization.hpp>
50 #include <com/sun/star/xml/crypto/XSecurityEnvironment.hpp>
51 #include <com/sun/star/security/XCertificate.hpp>
52 #include <com/sun/star/security/CertificateCharacters.hpp>
53 #include <com/sun/star/security/CertificateValidity.hpp>
54 #include <com/sun/star/lang/XUnoTunnel.hpp>
55 
56 #include <list>
57 #include "xmlsec/xmlsec.h"
58 
59 #include "sal/types.h"
60 
61 
62 class SecurityEnvironment_MSCryptImpl : public ::cppu::WeakImplHelper4<
63 	::com::sun::star::xml::crypto::XSecurityEnvironment ,
64 	::com::sun::star::lang::XInitialization ,
65 	::com::sun::star::lang::XServiceInfo ,
66 	::com::sun::star::lang::XUnoTunnel >
67 {
68 	private :
69 		//cyrpto provider and key container
70 		HCRYPTPROV							m_hProv ;
71 		LPCTSTR								m_pszContainer ;
72 
73 		//Key store
74 		HCERTSTORE							m_hKeyStore ;
75 
76 		//Certiticate store
77 		HCERTSTORE							m_hCertStore ;
78 
79 		//Enable default system cryptography setting
80 		sal_Bool							m_bEnableDefault ;
81 
82 		//External keys
83 		std::list< HCRYPTKEY >				m_tSymKeyList ;
84 		std::list< HCRYPTKEY >				m_tPubKeyList ;
85 		std::list< HCRYPTKEY >				m_tPriKeyList ;
86 
87 		//Service manager
88 		::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > m_xServiceManager ;
89 
90 	public :
91 		SecurityEnvironment_MSCryptImpl( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& aFactory ) ;
92 		virtual ~SecurityEnvironment_MSCryptImpl() ;
93 
94 		//Methods from XSecurityEnvironment
95 		virtual ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Reference< ::com::sun::star::security::XCertificate > > SAL_CALL getPersonalCertificates() throw(  ::com::sun::star::uno::SecurityException , ::com::sun::star::uno::RuntimeException ) ;
96 
97 		virtual ::com::sun::star::uno::Reference< ::com::sun::star::security::XCertificate > SAL_CALL getCertificate( const ::rtl::OUString& issuerName, const ::com::sun::star::uno::Sequence< sal_Int8 >& serialNumber ) throw( ::com::sun::star::uno::SecurityException , ::com::sun::star::uno::RuntimeException ) ;
98 
99 		virtual ::com::sun::star::uno::Reference< ::com::sun::star::security::XCertificate > SAL_CALL getCertificate( const ::rtl::OUString& issuerName, const ::rtl::OUString& serialNumber ) throw( ::com::sun::star::uno::SecurityException , ::com::sun::star::uno::RuntimeException ) ;
100 
101 		virtual ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Reference< ::com::sun::star::security::XCertificate > > SAL_CALL buildCertificatePath( const ::com::sun::star::uno::Reference< ::com::sun::star::security::XCertificate >& beginCert ) throw(  ::com::sun::star::uno::SecurityException , ::com::sun::star::uno::RuntimeException ) ;
102 
103 		virtual ::com::sun::star::uno::Reference< ::com::sun::star::security::XCertificate > SAL_CALL createCertificateFromRaw( const ::com::sun::star::uno::Sequence< sal_Int8 >& rawCertificate ) throw( ::com::sun::star::uno::SecurityException , ::com::sun::star::uno::RuntimeException ) ;
104 
105 		virtual ::com::sun::star::uno::Reference< ::com::sun::star::security::XCertificate > SAL_CALL createCertificateFromAscii( const ::rtl::OUString& asciiCertificate ) throw( ::com::sun::star::uno::SecurityException , ::com::sun::star::uno::RuntimeException ) ;
106 
107 		virtual ::sal_Int32 SAL_CALL verifyCertificate(
108             const ::com::sun::star::uno::Reference< ::com::sun::star::security::XCertificate >& xCert,
109             const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Reference<
110             ::com::sun::star::security::XCertificate > >& intermediateCertificates)
111             throw (::com::sun::star::uno::SecurityException, ::com::sun::star::uno::RuntimeException) ;
112 	virtual ::sal_Int32 SAL_CALL getCertificateCharacters( const ::com::sun::star::uno::Reference< ::com::sun::star::security::XCertificate >& xCert ) throw (::com::sun::star::uno::SecurityException, ::com::sun::star::uno::RuntimeException) ;
113 
114 		virtual ::rtl::OUString SAL_CALL getSecurityEnvironmentInformation(  ) throw (::com::sun::star::uno::RuntimeException);
115 
116 
117 		//Methods from XInitialization
118 		virtual void SAL_CALL initialize(
119 			const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& aArguments
120 		) throw( ::com::sun::star::uno::Exception , ::com::sun::star::uno::RuntimeException ) ;
121 
122 		//Methods from XServiceInfo
123 		virtual ::rtl::OUString SAL_CALL getImplementationName() throw( ::com::sun::star::uno::RuntimeException ) ;
124 
125 		virtual sal_Bool SAL_CALL supportsService(
126 			const ::rtl::OUString& ServiceName
127 		) throw( ::com::sun::star::uno::RuntimeException ) ;
128 
129 		virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames() throw( ::com::sun::star::uno::RuntimeException ) ;
130 
131 		//Helper for XServiceInfo
132 		static ::com::sun::star::uno::Sequence< ::rtl::OUString > impl_getSupportedServiceNames() ;
133 
134 		static ::rtl::OUString impl_getImplementationName() throw( ::com::sun::star::uno::RuntimeException ) ;
135 
136 		//Helper for registry
137 		static ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > SAL_CALL impl_createInstance( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& aServiceManager ) throw( ::com::sun::star::uno::RuntimeException ) ;
138 
139 		static ::com::sun::star::uno::Reference< ::com::sun::star::lang::XSingleServiceFactory > impl_createFactory( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& aServiceManager ) ;
140 
141 		//Methods from XUnoTunnel
142 		virtual sal_Int64 SAL_CALL getSomething( const ::com::sun::star::uno::Sequence< sal_Int8 >& aIdentifier )
143 			throw (::com::sun::star::uno::RuntimeException);
144 
145 		static const ::com::sun::star::uno::Sequence< sal_Int8 >& getUnoTunnelId() ;
146 		static SecurityEnvironment_MSCryptImpl* getImplementation( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > xObj ) ;
147 
148 		//Native mehtods
149 		virtual HCRYPTPROV getCryptoProvider() throw( ::com::sun::star::uno::Exception , ::com::sun::star::uno::RuntimeException ) ;
150 
151 		virtual void setCryptoProvider( HCRYPTPROV aProv ) throw( ::com::sun::star::uno::Exception , ::com::sun::star::uno::RuntimeException ) ;
152 
153 		virtual LPCTSTR getKeyContainer() throw( ::com::sun::star::uno::Exception , ::com::sun::star::uno::RuntimeException ) ;
154 
155 		virtual void setKeyContainer( LPCTSTR aKeyContainer ) throw( ::com::sun::star::uno::Exception , ::com::sun::star::uno::RuntimeException ) ;
156 
157 		virtual HCERTSTORE getCryptoSlot() throw( ::com::sun::star::uno::Exception , ::com::sun::star::uno::RuntimeException ) ;
158 
159 		virtual void setCryptoSlot( HCERTSTORE aKeyStore ) throw( ::com::sun::star::uno::Exception , ::com::sun::star::uno::RuntimeException ) ;
160 
161 		virtual HCERTSTORE getCertDb() throw( ::com::sun::star::uno::Exception , ::com::sun::star::uno::RuntimeException ) ;
162 
163 		virtual void setCertDb( HCERTSTORE aCertDb ) throw( ::com::sun::star::uno::Exception , ::com::sun::star::uno::RuntimeException ) ;
164 
165 		virtual void adoptSymKey( HCRYPTKEY aSymKey ) throw( ::com::sun::star::uno::Exception , ::com::sun::star::uno::RuntimeException ) ;
166 
167 		virtual void rejectSymKey( HCRYPTKEY aSymKey ) throw( ::com::sun::star::uno::Exception , ::com::sun::star::uno::RuntimeException ) ;
168 
169 		virtual HCRYPTKEY getSymKey( unsigned int position ) throw( ::com::sun::star::uno::Exception , ::com::sun::star::uno::RuntimeException ) ;
170 
171 		virtual void adoptPubKey( HCRYPTKEY aPubKey ) throw( ::com::sun::star::uno::Exception , ::com::sun::star::uno::RuntimeException ) ;
172 
173 		virtual void rejectPubKey( HCRYPTKEY aPubKey ) throw( ::com::sun::star::uno::Exception , ::com::sun::star::uno::RuntimeException ) ;
174 
175 		virtual HCRYPTKEY getPubKey( unsigned int position ) throw( ::com::sun::star::uno::Exception , ::com::sun::star::uno::RuntimeException ) ;
176 
177 		virtual void adoptPriKey( HCRYPTKEY aPriKey ) throw( ::com::sun::star::uno::Exception , ::com::sun::star::uno::RuntimeException ) ;
178 
179 		virtual void rejectPriKey( HCRYPTKEY aPriKey ) throw( ::com::sun::star::uno::Exception , ::com::sun::star::uno::RuntimeException ) ;
180 
181 		virtual HCRYPTKEY getPriKey( unsigned int position ) throw( ::com::sun::star::uno::Exception , ::com::sun::star::uno::RuntimeException ) ;
182 
183 		virtual void enableDefaultCrypt( sal_Bool enable ) throw( ::com::sun::star::uno::Exception , ::com::sun::star::uno::RuntimeException ) ;
184 
185 		virtual sal_Bool defaultEnabled() throw( ::com::sun::star::uno::Exception , ::com::sun::star::uno::RuntimeException ) ;
186 
187 		//Native mehtods
188 		virtual xmlSecKeysMngrPtr createKeysManager() throw( ::com::sun::star::uno::Exception , ::com::sun::star::uno::RuntimeException ) ;
189 
190 		virtual void destroyKeysManager(xmlSecKeysMngrPtr pKeysMngr) throw( ::com::sun::star::uno::Exception , ::com::sun::star::uno::RuntimeException ) ;
191 } ;
192 
193 #endif	// _XSECURITYENVIRONMENT_MSCRYPTIMPL_HXX_
194 
195