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_NSSIMPL_HXX_
25 #define _XSECURITYENVIRONMENT_NSSIMPL_HXX_
26 
27 #include <sal/config.h>
28 #include <rtl/ustring.hxx>
29 #include <cppuhelper/factory.hxx>
30 #include <cppuhelper/implbase4.hxx>
31 #include <com/sun/star/uno/Exception.hpp>
32 
33 #ifndef _COM_SUN_STAR_UNO_REFERENCE_HPP_
34 #include <com/sun/star/uno/Reference.hxx>
35 #endif
36 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
37 
38 #ifndef _COM_SUN_STAR_LANG_XSECVICEINFO_HPP_
39 #include <com/sun/star/lang/XServiceInfo.hpp>
40 #endif
41 #include <com/sun/star/lang/XInitialization.hpp>
42 #include <com/sun/star/xml/crypto/XSecurityEnvironment.hpp>
43 #include <com/sun/star/security/XCertificate.hpp>
44 #include <com/sun/star/security/CertificateCharacters.hpp>
45 #include <com/sun/star/security/CertificateValidity.hpp>
46 #include <com/sun/star/lang/XUnoTunnel.hpp>
47 
48 #include "osl/mutex.hxx"
49 
50 #include "pk11func.h"
51 #include "keyhi.h"
52 #include "certdb.h"
53 #include "list"
54 
55 #include <sal/types.h>
56 //For reasons that escape me, this is what xmlsec does when size_t is not 4
57 #if SAL_TYPES_SIZEOFPOINTER != 4
58 #    define XMLSEC_NO_SIZE_T
59 #endif
60 #include "xmlsec/xmlsec.h"
61 
62 class SecurityEnvironment_NssImpl : 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 
70     std::list< PK11SlotInfo* > m_Slots;
71     typedef std::list< PK11SlotInfo* >::const_iterator CIT_SLOTS;
72 
73     osl::Mutex m_mutex;
74 
75 		CERTCertDBHandle*					m_pHandler ;
76 		std::list< PK11SymKey* >			m_tSymKeyList ;
77 		std::list< SECKEYPublicKey* >		m_tPubKeyList ;
78 		std::list< SECKEYPrivateKey* >		m_tPriKeyList ;
79 		::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > m_xServiceManager ;
80 
81 	public :
82 		SecurityEnvironment_NssImpl( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& aFactory ) ;
83 		virtual ~SecurityEnvironment_NssImpl() ;
84 
85 		//Methods from XSecurityEnvironment
86 
87 		//Methods from XInitialization
88 		virtual void SAL_CALL initialize(
89 			const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& aArguments
90 		) throw( ::com::sun::star::uno::Exception , ::com::sun::star::uno::RuntimeException ) ;
91 
92 		//Methods from XServiceInfo
93 		virtual ::rtl::OUString SAL_CALL getImplementationName() throw( ::com::sun::star::uno::RuntimeException ) ;
94 
95 		virtual sal_Bool SAL_CALL supportsService(
96 			const ::rtl::OUString& ServiceName
97 		) throw( ::com::sun::star::uno::RuntimeException ) ;
98 
99 		virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames() throw( ::com::sun::star::uno::RuntimeException ) ;
100 
101 		//Helper for XServiceInfo
102 		static ::com::sun::star::uno::Sequence< ::rtl::OUString > impl_getSupportedServiceNames() ;
103 
104 		static ::rtl::OUString impl_getImplementationName() throw( ::com::sun::star::uno::RuntimeException ) ;
105 
106 		//Helper for registry
107 		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 ) ;
108 
109 		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 ) ;
110 
111 		virtual ::sal_Int32 SAL_CALL verifyCertificate(
112             const ::com::sun::star::uno::Reference<
113             ::com::sun::star::security::XCertificate >& xCert,
114             const ::com::sun::star::uno::Sequence<
115             ::com::sun::star::uno::Reference< ::com::sun::star::security::XCertificate > > &
116             intermediateCerts)
117             throw (::com::sun::star::uno::SecurityException, ::com::sun::star::uno::RuntimeException) ;
118 
119 		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) ;
120 
121 		virtual ::rtl::OUString SAL_CALL getSecurityEnvironmentInformation(  ) throw (::com::sun::star::uno::RuntimeException);
122 
123 		//Methods from XUnoTunnel
124 		virtual sal_Int64 SAL_CALL getSomething( const ::com::sun::star::uno::Sequence< sal_Int8 >& aIdentifier )
125 			throw (::com::sun::star::uno::RuntimeException);
126 
127 		static const ::com::sun::star::uno::Sequence< sal_Int8 >& getUnoTunnelId() ;
128 		static SecurityEnvironment_NssImpl* getImplementation( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > xObj ) ;
129 
130 		//Native mehtods
131 		virtual CERTCertDBHandle* getCertDb() throw( ::com::sun::star::uno::Exception , ::com::sun::star::uno::RuntimeException ) ;
132 
133 		virtual void setCertDb( CERTCertDBHandle* aCertDb ) throw( ::com::sun::star::uno::Exception , ::com::sun::star::uno::RuntimeException ) ;
134 
135 		virtual void adoptSymKey( PK11SymKey* aSymKey ) throw( ::com::sun::star::uno::Exception , ::com::sun::star::uno::RuntimeException ) ;
136 
137 		virtual void rejectSymKey( PK11SymKey* aSymKey ) throw( ::com::sun::star::uno::Exception , ::com::sun::star::uno::RuntimeException ) ;
138 
139 		virtual PK11SymKey* getSymKey( unsigned int position ) throw( ::com::sun::star::uno::Exception , ::com::sun::star::uno::RuntimeException ) ;
140 
141 		virtual void adoptPubKey( SECKEYPublicKey* aPubKey ) throw( ::com::sun::star::uno::Exception , ::com::sun::star::uno::RuntimeException ) ;
142 
143 		virtual void rejectPubKey( SECKEYPublicKey* aPubKey ) throw( ::com::sun::star::uno::Exception , ::com::sun::star::uno::RuntimeException ) ;
144 
145 		virtual SECKEYPublicKey* getPubKey( unsigned int position ) throw( ::com::sun::star::uno::Exception , ::com::sun::star::uno::RuntimeException ) ;
146 
147 		virtual void adoptPriKey( SECKEYPrivateKey* aPriKey ) throw( ::com::sun::star::uno::Exception , ::com::sun::star::uno::RuntimeException ) ;
148 
149 		virtual void rejectPriKey( SECKEYPrivateKey* aPriKey ) throw( ::com::sun::star::uno::Exception , ::com::sun::star::uno::RuntimeException ) ;
150 
151 		virtual SECKEYPrivateKey* getPriKey( unsigned int position ) throw( ::com::sun::star::uno::Exception , ::com::sun::star::uno::RuntimeException ) ;
152 
153 		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 ) ;
154 
155 		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 ) ;
156 
157 		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 ) ;
158 
159 		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 ) ;
160 
161 		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 ) ;
162 
163 		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 ) ;
164 
165 
166 		//Native mehtods
167 		virtual xmlSecKeysMngrPtr createKeysManager() throw( ::com::sun::star::uno::Exception , ::com::sun::star::uno::RuntimeException ) ;
168 		virtual void destroyKeysManager(xmlSecKeysMngrPtr pKeysMngr) throw( ::com::sun::star::uno::Exception , ::com::sun::star::uno::RuntimeException ) ;
169 
170 private:
171         void updateSlots();
172 
173   		virtual void addCryptoSlot( PK11SlotInfo* aSlot ) throw( ::com::sun::star::uno::Exception , ::com::sun::star::uno::RuntimeException ) ;
174 
175 
176 } ;
177 
178 #endif	// _XSECURITYENVIRONMENT_NSSIMPL_HXX_
179 
180