1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
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
>
41 typedef std::map
<OUString
, OUString
> Map
;
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
);
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
;
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
;
66 CertificateContainer::searchMap( const OUString
& url
, const OUString
& certificate_name
, Map
&_certMap
)
68 Map::iterator p
= _certMap
.find(url
);
72 while( p
!= _certMap
.end() )
74 ret
= (*p
).second
== certificate_name
;
84 CertificateContainer::isTemporaryCertificate ( const OUString
& url
, const OUString
& certificate_name
)
86 return searchMap( url
, certificate_name
, certMap
);
90 CertificateContainer::isCertificateTrust ( const OUString
& url
, const OUString
& certificate_name
)
92 return searchMap( url
, certificate_name
, certTrustMap
);
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
102 certTrustMap
.emplace( url
, certificate_name
);
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
;
115 return security::CertificateContainerStatus_UNTRUSTED
;
118 return security::CertificateContainerStatus_NOCERT
;
123 CertificateContainer::getImplementationName( )
125 return "com.sun.star.security.CertificateContainer";
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" };
145 explicit Instance(css::uno::Reference
<css::uno::XComponentContext
> const& context
)
146 : instance(new CertificateContainer(context
))
150 rtl::Reference
<CertificateContainer
> instance
;
154 : public rtl::StaticWithArg
<Instance
, css::uno::Reference
<css::uno::XComponentContext
>,
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: */