Debian Codegraph @

xmlsecurity/source/component/certificatecontainer.cxx

raw
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
22 #include <cppuhelper/implbase.hxx>
23 #include <com/sun/star/security/XCertificateContainer.hpp>
24 #include <com/sun/star/lang/XServiceInfo.hpp>
25
26 #include <cppuhelper/supportsservice.hxx>
27 #include <rtl/ref.hxx>
28
29 #include <sal/config.h>
30
31 namespace com::sun::star::uno { class XComponentContext; }
32
33 using namespace ::com::sun::star;
34 using namespace ::com::sun::star::lang;
35 using namespace ::com::sun::star::uno;
36
37 namespace {
38
39 class CertificateContainer
40 : public ::cppu::WeakImplHelper<css::lang::XServiceInfo, css::security::XCertificateContainer>
41 {
42 private:
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);
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);
52
53 public:
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
69 bool
70 CertificateContainer::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
87 bool
88 CertificateContainer::isTemporaryCertificate ( const OUString & url, std::u16string_view certificate_name )
89 {
90 return searchMap( url, certificate_name, certMap);
91 }
92
93 bool
94 CertificateContainer::isCertificateTrust ( const OUString & url, std::u16string_view certificate_name )
95 {
96 return searchMap( url, certificate_name, certTrustMap);
97 }
98
99 sal_Bool
100 CertificateContainer::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
112 CertificateContainer::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
126 OUString SAL_CALL
127 CertificateContainer::getImplementationName( )
128 {
129 return u"com.sun.star.security.CertificateContainer"_ustr;
130 }
131
132 sal_Bool SAL_CALL
133 CertificateContainer::supportsService( const OUString& ServiceName )
134 {
135 return cppu::supportsService( this, ServiceName );
136 }
137
138 Sequence< OUString > SAL_CALL
139 CertificateContainer::getSupportedServiceNames( )
140 {
141 return { u"com.sun.star.security.CertificateContainer"_ustr };
142 }
143
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&)
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: */