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 #include "precompiled_xmlsecurity.hxx"
25 #include <certificatecontainer.hxx>
26 
27 #include <sal/config.h>
28 
29 using namespace ::com::sun::star::uno;
30 
31 
32 sal_Bool
searchMap(const::rtl::OUString & url,const::rtl::OUString & certificate_name,Map & _certMap)33 CertificateContainer::searchMap( const ::rtl::OUString & url, const ::rtl::OUString & certificate_name, Map &_certMap )
34 {
35 	Map::iterator p = _certMap.find(url);
36 
37 	::sal_Bool ret = sal_False;
38 
39 	while( p != _certMap.end() )
40 	{
41 		ret = (sal_Bool) (*p).second.equals(certificate_name);
42 		if( ret )
43                     break;
44 		p++;
45 	}
46 
47 	return ret;
48 }
49 // -------------------------------------------------------------------
50 
51 sal_Bool
isTemporaryCertificate(const::rtl::OUString & url,const::rtl::OUString & certificate_name)52 CertificateContainer::isTemporaryCertificate ( const ::rtl::OUString & url, const ::rtl::OUString & certificate_name )
53     throw(::com::sun::star::uno::RuntimeException)
54 {
55 	return searchMap( url, certificate_name, certMap);
56 }
57 
58 // -------------------------------------------------------------------
59 
60 sal_Bool
isCertificateTrust(const::rtl::OUString & url,const::rtl::OUString & certificate_name)61 CertificateContainer::isCertificateTrust ( const ::rtl::OUString & url, const ::rtl::OUString & certificate_name )
62     throw(::com::sun::star::uno::RuntimeException)
63 {
64 	return searchMap( url, certificate_name, certTrustMap);
65 }
66 
67 // -------------------------------------------------------------------
68 sal_Bool
addCertificate(const::rtl::OUString & url,const::rtl::OUString & certificate_name,::sal_Bool trust)69 CertificateContainer::addCertificate( const ::rtl::OUString & url, const ::rtl::OUString & certificate_name, ::sal_Bool trust )
70     throw(::com::sun::star::uno::RuntimeException)
71 {
72 	certMap.insert( Map::value_type( url, certificate_name ) );
73 
74         //remember that the cert is trusted
75         if (trust)
76             certTrustMap.insert( Map::value_type( url, certificate_name ) );
77 
78         return true;
79 }
80 
81 //-------------------------------------------------------------------------
82 ::security::CertificateContainerStatus
hasCertificate(const::rtl::OUString & url,const::rtl::OUString & certificate_name)83 CertificateContainer::hasCertificate( const ::rtl::OUString & url, const ::rtl::OUString & certificate_name ) throw(::com::sun::star::uno::RuntimeException)
84 {
85 	if ( isTemporaryCertificate( url, certificate_name ) )
86 	{
87 		if ( isCertificateTrust( url, certificate_name ) )
88 			return security::CertificateContainerStatus( security::CertificateContainerStatus_TRUSTED );
89 		else
90 			return security::CertificateContainerStatus_UNTRUSTED;
91 	} else
92 	{
93 		return security::CertificateContainerStatus_NOCERT;
94 	}
95 }
96 //-------------------------------------------------------------------------
97 
98 ::rtl::OUString SAL_CALL
getImplementationName()99 CertificateContainer::getImplementationName( )
100     throw(::com::sun::star::uno::RuntimeException)
101 {
102     return impl_getStaticImplementationName();
103 }
104 
105 //-------------------------------------------------------------------------
106 
107 sal_Bool SAL_CALL
supportsService(const::rtl::OUString & ServiceName)108 CertificateContainer::supportsService( const ::rtl::OUString& ServiceName )
109     throw(::com::sun::star::uno::RuntimeException)
110 {
111     if ( ServiceName.compareToAscii("com.sun.star.security.CertificateContainer") == 0 )
112         return sal_True;
113     else
114         return sal_False;
115 }
116 
117 //-------------------------------------------------------------------------
118 
119 Sequence< ::rtl::OUString > SAL_CALL
getSupportedServiceNames()120 CertificateContainer::getSupportedServiceNames(  )
121     throw(::com::sun::star::uno::RuntimeException)
122 {
123 	return impl_getStaticSupportedServiceNames();
124 }
125 
126 //-------------------------------------------------------------------------
127 
128 Sequence< ::rtl::OUString > SAL_CALL
impl_getStaticSupportedServiceNames()129 CertificateContainer::impl_getStaticSupportedServiceNames(  )
130     throw(::com::sun::star::uno::RuntimeException)
131 {
132     Sequence< ::rtl::OUString > aRet(1);
133     *aRet.getArray() = ::rtl::OUString::createFromAscii("com.sun.star.security.CertificateContainer");
134     return aRet;
135 }
136 
137 //-------------------------------------------------------------------------
138 
139 ::rtl::OUString SAL_CALL
impl_getStaticImplementationName()140 CertificateContainer::impl_getStaticImplementationName()
141     throw(::com::sun::star::uno::RuntimeException)
142 {
143     return ::rtl::OUString::createFromAscii("com.sun.star.security.CertificateContainer");
144 }
145 
146 //-------------------------------------------------------------------------
147 
impl_createInstance(const Reference<XMultiServiceFactory> & xServiceManager)148 Reference< XInterface > SAL_CALL CertificateContainer::impl_createInstance( const Reference< XMultiServiceFactory >& xServiceManager )
149     throw( RuntimeException )
150 {
151 	return Reference< XInterface >( *new CertificateContainer( xServiceManager ) );
152 }
153 
154 //-------------------------------------------------------------------------
155 
156 Reference< XSingleServiceFactory > SAL_CALL
impl_createFactory(const Reference<XMultiServiceFactory> & ServiceManager)157 CertificateContainer::impl_createFactory( const Reference< XMultiServiceFactory >& ServiceManager )
158     throw(RuntimeException)
159 {
160 	Reference< XSingleServiceFactory > xReturn( ::cppu::createOneInstanceFactory( ServiceManager,
161 		CertificateContainer::impl_getStaticImplementationName(),
162 		CertificateContainer::impl_createInstance,
163 		CertificateContainer::impl_getStaticSupportedServiceNames()));
164 
165 	return xReturn;
166 }
167 
168