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
;
39 class CertificateContainer
40 : public ::cppu::WeakImplHelper
<css::lang::XServiceInfo
, css::security::XCertificateContainer
>
43 typedef std::map
<OUString
, OUString
> Map
;
47 static bool searchMap(const OUString
& url
, std::u16string_view certificate_name
, Map
& _certMap
);
48 /// @throws css::uno::RuntimeException
49 bool isTemporaryCertificate(const OUString
& url
, std::u16string_view certificate_name
);
50 /// @throws css::uno::RuntimeException
51 bool isCertificateTrust(const OUString
& url
, std::u16string_view certificate_name
);
54 explicit CertificateContainer(const uno::Reference
<uno::XComponentContext
>&) {}
55 virtual sal_Bool SAL_CALL
addCertificate(const OUString
& url
, const OUString
& certificate_name
,
56 sal_Bool trust
) override
;
57 virtual css::security::CertificateContainerStatus SAL_CALL
58 hasCertificate(const OUString
& url
, const OUString
& certificate_name
) override
;
61 virtual OUString SAL_CALL
getImplementationName() override
;
62 virtual sal_Bool SAL_CALL
supportsService(const OUString
& ServiceName
) override
;
64 virtual css::uno::Sequence
<OUString
> SAL_CALL
getSupportedServiceNames() override
;
70 CertificateContainer::searchMap( const OUString
& url
, std::u16string_view certificate_name
, Map
&_certMap
)
72 Map::iterator p
= _certMap
.find(url
);
76 while( p
!= _certMap
.end() )
78 ret
= (*p
).second
== certificate_name
;
88 CertificateContainer::isTemporaryCertificate ( const OUString
& url
, std::u16string_view certificate_name
)
90 return searchMap( url
, certificate_name
, certMap
);
94 CertificateContainer::isCertificateTrust ( const OUString
& url
, std::u16string_view certificate_name
)
96 return searchMap( url
, certificate_name
, certTrustMap
);
100 CertificateContainer::addCertificate( const OUString
& url
, const OUString
& certificate_name
, sal_Bool trust
)
102 certMap
.emplace( url
, certificate_name
);
104 //remember that the cert is trusted
106 certTrustMap
.emplace( url
, certificate_name
);
111 ::security::CertificateContainerStatus
112 CertificateContainer::hasCertificate( const OUString
& url
, const OUString
& certificate_name
)
114 if ( isTemporaryCertificate( url
, certificate_name
) )
116 if ( isCertificateTrust( url
, certificate_name
) )
117 return security::CertificateContainerStatus_TRUSTED
;
119 return security::CertificateContainerStatus_UNTRUSTED
;
122 return security::CertificateContainerStatus_NOCERT
;
127 CertificateContainer::getImplementationName( )
129 return u
"com.sun.star.security.CertificateContainer"_ustr
;
133 CertificateContainer::supportsService( const OUString
& ServiceName
)
135 return cppu::supportsService( this, ServiceName
);
138 Sequence
< OUString
> SAL_CALL
139 CertificateContainer::getSupportedServiceNames( )
141 return { u
"com.sun.star.security.CertificateContainer"_ustr
};
144 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
145 com_sun_star_security_CertificateContainer_get_implementation(
146 css::uno::XComponentContext
* context
, css::uno::Sequence
<css::uno::Any
> const&)
148 static rtl::Reference
<CertificateContainer
> gContainer
= new CertificateContainer(context
);
149 return cppu::acquire(gContainer
.get());
152 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */