LibreOffice Module xmlsecurity (master) 1
certificatecontainer.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <map>
21
23#include <com/sun/star/security/XCertificateContainer.hpp>
24#include <com/sun/star/lang/XServiceInfo.hpp>
25
27#include <rtl/ref.hxx>
28
29#include <sal/config.h>
30
31namespace com::sun::star::uno { class XComponentContext; }
32
33using namespace ::com::sun::star;
34using namespace ::com::sun::star::lang;
35using namespace ::com::sun::star::uno;
36
37namespace {
38
39class CertificateContainer
40 : public ::cppu::WeakImplHelper<css::lang::XServiceInfo, css::security::XCertificateContainer>
41{
42private:
43 typedef std::map<OUString, OUString> Map;
44 Map certMap;
45 Map certTrustMap;
46
47 static bool searchMap(const OUString& url, std::u16string_view certificate_name, Map& _certMap);
49 bool isTemporaryCertificate(const OUString& url, std::u16string_view certificate_name);
51 bool isCertificateTrust(const OUString& url, std::u16string_view certificate_name);
52
53public:
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;
59
60 // XServiceInfo
61 virtual OUString SAL_CALL getImplementationName() override;
62 virtual sal_Bool SAL_CALL supportsService(const OUString& ServiceName) override;
63
64 virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override;
65};
66
67}
68
69bool
70CertificateContainer::searchMap( const OUString & url, std::u16string_view certificate_name, Map &_certMap )
71{
72 Map::iterator p = _certMap.find(url);
73
74 bool ret = false;
75
76 while( p != _certMap.end() )
77 {
78 ret = (*p).second == certificate_name;
79 if( ret )
80 break;
81 ++p;
82 }
83
84 return ret;
85}
86
87bool
88CertificateContainer::isTemporaryCertificate ( const OUString & url, std::u16string_view certificate_name )
89{
90 return searchMap( url, certificate_name, certMap);
91}
92
93bool
94CertificateContainer::isCertificateTrust ( const OUString & url, std::u16string_view certificate_name )
95{
96 return searchMap( url, certificate_name, certTrustMap);
97}
98
100CertificateContainer::addCertificate( const OUString & url, const OUString & certificate_name, sal_Bool trust )
101{
102 certMap.emplace( url, certificate_name );
103
104 //remember that the cert is trusted
105 if (trust)
106 certTrustMap.emplace( url, certificate_name );
107
108 return true;
109}
110
111::security::CertificateContainerStatus
112CertificateContainer::hasCertificate( const OUString & url, const OUString & certificate_name )
113{
114 if ( isTemporaryCertificate( url, certificate_name ) )
115 {
116 if ( isCertificateTrust( url, certificate_name ) )
117 return security::CertificateContainerStatus_TRUSTED;
118 else
119 return security::CertificateContainerStatus_UNTRUSTED;
120 } else
121 {
122 return security::CertificateContainerStatus_NOCERT;
123 }
124}
125
126OUString SAL_CALL
127CertificateContainer::getImplementationName( )
128{
129 return "com.sun.star.security.CertificateContainer";
130}
131
132sal_Bool SAL_CALL
133CertificateContainer::supportsService( const OUString& ServiceName )
134{
135 return cppu::supportsService( this, ServiceName );
136}
137
138Sequence< OUString > SAL_CALL
139CertificateContainer::getSupportedServiceNames( )
140{
141 return { "com.sun.star.security.CertificateContainer" };
142}
143
144extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
146 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
147{
148 static rtl::Reference<CertificateContainer> gContainer = new CertificateContainer(context);
149 return cppu::acquire(gContainer.get());
150}
151
152/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_security_CertificateContainer_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
void * p
css::uno::Sequence< OUString > getSupportedServiceNames()
OUString getImplementationName()
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
unsigned char sal_Bool