Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / xmlsecurity / source / component / certificatecontainer.cxx
blob648d72c13c203e12a5637b306f3f395d01d1ed17
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <map>
22 #include <cppuhelper/implbase.hxx>
23 #include <com/sun/star/security/XCertificateContainer.hpp>
24 #include <com/sun/star/lang/XServiceInfo.hpp>
26 #include <cppuhelper/supportsservice.hxx>
27 #include <rtl/ref.hxx>
29 #include <sal/config.h>
31 namespace com::sun::star::uno { class XComponentContext; }
33 using namespace ::com::sun::star;
34 using namespace ::com::sun::star::lang;
35 using namespace ::com::sun::star::uno;
37 class CertificateContainer
38 : public ::cppu::WeakImplHelper<css::lang::XServiceInfo, css::security::XCertificateContainer>
40 private:
41 typedef std::map<OUString, OUString> Map;
42 Map certMap;
43 Map certTrustMap;
45 static bool searchMap(const OUString& url, const OUString& certificate_name, Map& _certMap);
46 /// @throws css::uno::RuntimeException
47 bool isTemporaryCertificate(const OUString& url, const OUString& certificate_name);
48 /// @throws css::uno::RuntimeException
49 bool isCertificateTrust(const OUString& url, const OUString& certificate_name);
51 public:
52 explicit CertificateContainer(const uno::Reference<uno::XComponentContext>&) {}
53 virtual sal_Bool SAL_CALL addCertificate(const OUString& url, const OUString& certificate_name,
54 sal_Bool trust) override;
55 virtual css::security::CertificateContainerStatus SAL_CALL
56 hasCertificate(const OUString& url, const OUString& certificate_name) override;
58 // XServiceInfo
59 virtual OUString SAL_CALL getImplementationName() override;
60 virtual sal_Bool SAL_CALL supportsService(const OUString& ServiceName) override;
62 virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override;
65 bool
66 CertificateContainer::searchMap( const OUString & url, const OUString & certificate_name, Map &_certMap )
68 Map::iterator p = _certMap.find(url);
70 bool ret = false;
72 while( p != _certMap.end() )
74 ret = (*p).second == certificate_name;
75 if( ret )
76 break;
77 ++p;
80 return ret;
83 bool
84 CertificateContainer::isTemporaryCertificate ( const OUString & url, const OUString & certificate_name )
86 return searchMap( url, certificate_name, certMap);
89 bool
90 CertificateContainer::isCertificateTrust ( const OUString & url, const OUString & certificate_name )
92 return searchMap( url, certificate_name, certTrustMap);
95 sal_Bool
96 CertificateContainer::addCertificate( const OUString & url, const OUString & certificate_name, sal_Bool trust )
98 certMap.emplace( url, certificate_name );
100 //remember that the cert is trusted
101 if (trust)
102 certTrustMap.emplace( url, certificate_name );
104 return true;
107 ::security::CertificateContainerStatus
108 CertificateContainer::hasCertificate( const OUString & url, const OUString & certificate_name )
110 if ( isTemporaryCertificate( url, certificate_name ) )
112 if ( isCertificateTrust( url, certificate_name ) )
113 return security::CertificateContainerStatus_TRUSTED;
114 else
115 return security::CertificateContainerStatus_UNTRUSTED;
116 } else
118 return security::CertificateContainerStatus_NOCERT;
122 OUString SAL_CALL
123 CertificateContainer::getImplementationName( )
125 return "com.sun.star.security.CertificateContainer";
128 sal_Bool SAL_CALL
129 CertificateContainer::supportsService( const OUString& ServiceName )
131 return cppu::supportsService( this, ServiceName );
134 Sequence< OUString > SAL_CALL
135 CertificateContainer::getSupportedServiceNames( )
137 Sequence< OUString > aRet { "com.sun.star.security.CertificateContainer" };
138 return aRet;
141 namespace
143 struct Instance
145 explicit Instance(css::uno::Reference<css::uno::XComponentContext> const& context)
146 : instance(new CertificateContainer(context))
150 rtl::Reference<CertificateContainer> instance;
153 struct Singleton
154 : public rtl::StaticWithArg<Instance, css::uno::Reference<css::uno::XComponentContext>,
155 Singleton>
160 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
161 com_sun_star_security_CertificateContainer_get_implementation(
162 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
164 return cppu::acquire(Singleton::get(context).instance.get());
167 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */