1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef _XSECURITYENVIRONMENT_NSSIMPL_HXX_
29 #define _XSECURITYENVIRONMENT_NSSIMPL_HXX_
30 
31 #include <sal/config.h>
32 #include <rtl/ustring.hxx>
33 #include <cppuhelper/factory.hxx>
34 #include <cppuhelper/implbase4.hxx>
35 #include <com/sun/star/uno/Exception.hpp>
36 
37 #ifndef _COM_SUN_STAR_UNO_REFERENCE_HPP_
38 #include <com/sun/star/uno/Reference.hxx>
39 #endif
40 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
41 
42 #ifndef _COM_SUN_STAR_LANG_XSECVICEINFO_HPP_
43 #include <com/sun/star/lang/XServiceInfo.hpp>
44 #endif
45 #include <com/sun/star/lang/XInitialization.hpp>
46 #include <com/sun/star/xml/crypto/XSecurityEnvironment.hpp>
47 #include <com/sun/star/security/XCertificate.hpp>
48 #include <com/sun/star/security/CertificateCharacters.hpp>
49 #include <com/sun/star/security/CertificateValidity.hpp>
50 #include <com/sun/star/lang/XUnoTunnel.hpp>
51 
52 #include "osl/mutex.hxx"
53 
54 #include "pk11func.h"
55 #include "keyhi.h"
56 #include "certdb.h"
57 #include "list"
58 
59 #include <sal/types.h>
60 //For reasons that escape me, this is what xmlsec does when size_t is not 4
61 #if SAL_TYPES_SIZEOFPOINTER != 4
62 #    define XMLSEC_NO_SIZE_T
63 #endif
64 #include "xmlsec/xmlsec.h"
65 
66 class SecurityEnvironment_NssImpl : public ::cppu::WeakImplHelper4<
67 	::com::sun::star::xml::crypto::XSecurityEnvironment ,
68 	::com::sun::star::lang::XInitialization ,
69 	::com::sun::star::lang::XServiceInfo ,
70 	::com::sun::star::lang::XUnoTunnel >
71 {
72 private :
73 
74     std::list< PK11SlotInfo* > m_Slots;
75     typedef std::list< PK11SlotInfo* >::const_iterator CIT_SLOTS;
76 
77     osl::Mutex m_mutex;
78 
79 		CERTCertDBHandle*					m_pHandler ;
80 		std::list< PK11SymKey* >			m_tSymKeyList ;
81 		std::list< SECKEYPublicKey* >		m_tPubKeyList ;
82 		std::list< SECKEYPrivateKey* >		m_tPriKeyList ;
83 		::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > m_xServiceManager ;
84 
85 	public :
86 		SecurityEnvironment_NssImpl( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& aFactory ) ;
87 		virtual ~SecurityEnvironment_NssImpl() ;
88 
89 		//Methods from XSecurityEnvironment
90 
91 		//Methods from XInitialization
92 		virtual void SAL_CALL initialize(
93 			const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& aArguments
94 		) throw( ::com::sun::star::uno::Exception , ::com::sun::star::uno::RuntimeException ) ;
95 
96 		//Methods from XServiceInfo
97 		virtual ::rtl::OUString SAL_CALL getImplementationName() throw( ::com::sun::star::uno::RuntimeException ) ;
98 
99 		virtual sal_Bool SAL_CALL supportsService(
100 			const ::rtl::OUString& ServiceName
101 		) throw( ::com::sun::star::uno::RuntimeException ) ;
102 
103 		virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames() throw( ::com::sun::star::uno::RuntimeException ) ;
104 
105 		//Helper for XServiceInfo
106 		static ::com::sun::star::uno::Sequence< ::rtl::OUString > impl_getSupportedServiceNames() ;
107 
108 		static ::rtl::OUString impl_getImplementationName() throw( ::com::sun::star::uno::RuntimeException ) ;
109 
110 		//Helper for registry
111 		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 ) ;
112 
113 		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 ) ;
114 
115 		virtual ::sal_Int32 SAL_CALL verifyCertificate(
116             const ::com::sun::star::uno::Reference<
117             ::com::sun::star::security::XCertificate >& xCert,
118             const ::com::sun::star::uno::Sequence<
119             ::com::sun::star::uno::Reference< ::com::sun::star::security::XCertificate > > &
120             intermediateCerts)
121             throw (::com::sun::star::uno::SecurityException, ::com::sun::star::uno::RuntimeException) ;
122 
123 		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) ;
124 
125 		virtual ::rtl::OUString SAL_CALL getSecurityEnvironmentInformation(  ) throw (::com::sun::star::uno::RuntimeException);
126 
127 		//Methods from XUnoTunnel
128 		virtual sal_Int64 SAL_CALL getSomething( const ::com::sun::star::uno::Sequence< sal_Int8 >& aIdentifier )
129 			throw (::com::sun::star::uno::RuntimeException);
130 
131 		static const ::com::sun::star::uno::Sequence< sal_Int8 >& getUnoTunnelId() ;
132 		static SecurityEnvironment_NssImpl* getImplementation( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > xObj ) ;
133 
134 		//Native mehtods
135 		virtual CERTCertDBHandle* getCertDb() throw( ::com::sun::star::uno::Exception , ::com::sun::star::uno::RuntimeException ) ;
136 
137 		virtual void setCertDb( CERTCertDBHandle* aCertDb ) throw( ::com::sun::star::uno::Exception , ::com::sun::star::uno::RuntimeException ) ;
138 
139 		virtual void adoptSymKey( PK11SymKey* aSymKey ) throw( ::com::sun::star::uno::Exception , ::com::sun::star::uno::RuntimeException ) ;
140 
141 		virtual void rejectSymKey( PK11SymKey* aSymKey ) throw( ::com::sun::star::uno::Exception , ::com::sun::star::uno::RuntimeException ) ;
142 
143 		virtual PK11SymKey* getSymKey( unsigned int position ) throw( ::com::sun::star::uno::Exception , ::com::sun::star::uno::RuntimeException ) ;
144 
145 		virtual void adoptPubKey( SECKEYPublicKey* aPubKey ) throw( ::com::sun::star::uno::Exception , ::com::sun::star::uno::RuntimeException ) ;
146 
147 		virtual void rejectPubKey( SECKEYPublicKey* aPubKey ) throw( ::com::sun::star::uno::Exception , ::com::sun::star::uno::RuntimeException ) ;
148 
149 		virtual SECKEYPublicKey* getPubKey( unsigned int position ) throw( ::com::sun::star::uno::Exception , ::com::sun::star::uno::RuntimeException ) ;
150 
151 		virtual void adoptPriKey( SECKEYPrivateKey* aPriKey ) throw( ::com::sun::star::uno::Exception , ::com::sun::star::uno::RuntimeException ) ;
152 
153 		virtual void rejectPriKey( SECKEYPrivateKey* aPriKey ) throw( ::com::sun::star::uno::Exception , ::com::sun::star::uno::RuntimeException ) ;
154 
155 		virtual SECKEYPrivateKey* getPriKey( unsigned int position ) throw( ::com::sun::star::uno::Exception , ::com::sun::star::uno::RuntimeException ) ;
156 
157 		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 ) ;
158 
159 		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 ) ;
160 
161 		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 ) ;
162 
163 		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 ) ;
164 
165 		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 ) ;
166 
167 		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 ) ;
168 
169 
170 		//Native mehtods
171 		virtual xmlSecKeysMngrPtr createKeysManager() throw( ::com::sun::star::uno::Exception , ::com::sun::star::uno::RuntimeException ) ;
172 		virtual void destroyKeysManager(xmlSecKeysMngrPtr pKeysMngr) throw( ::com::sun::star::uno::Exception , ::com::sun::star::uno::RuntimeException ) ;
173 
174 private:
175         void updateSlots();
176 
177   		virtual void addCryptoSlot( PK11SlotInfo* aSlot ) throw( ::com::sun::star::uno::Exception , ::com::sun::star::uno::RuntimeException ) ;
178 
179 
180 } ;
181 
182 #endif	// _XSECURITYENVIRONMENT_NSSIMPL_HXX_
183 
184