Line data Source code
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 :
21 : #include <com/sun/star/security/CertificateValidity.hpp>
22 : #include <com/sun/star/security/XCertificateExtension.hpp>
23 : #include <com/sun/star/security/XSanExtension.hpp>
24 : #include <com/sun/star/security/ExtAltNameType.hpp>
25 : #include <com/sun/star/task/XInteractionAbort.hpp>
26 : #include <com/sun/star/task/XInteractionApprove.hpp>
27 : #include <com/sun/star/task/XInteractionRequest.hpp>
28 : #include <com/sun/star/ucb/CertificateValidationRequest.hpp>
29 : #include <com/sun/star/uno/Reference.hxx>
30 :
31 : #include <osl/mutex.hxx>
32 : #include <com/sun/star/uno/Sequence.hxx>
33 : #include <svl/zforlist.hxx>
34 : #include <vcl/svapp.hxx>
35 : #include <vcl/settings.hxx>
36 :
37 : #include "ids.hrc"
38 : #include "getcontinuations.hxx"
39 : #include "sslwarndlg.hxx"
40 : #include "unknownauthdlg.hxx"
41 :
42 : #include "iahndl.hxx"
43 :
44 : #include <boost/scoped_ptr.hpp>
45 :
46 : #define DESCRIPTION_1 1
47 : #define TITLE 3
48 :
49 : #define OID_SUBJECT_ALTERNATIVE_NAME "2.5.29.17"
50 :
51 :
52 : using namespace com::sun::star;
53 :
54 : namespace {
55 :
56 : OUString
57 0 : getContentPart( const OUString& _rRawString )
58 : {
59 : // search over some parts to find a string
60 : static char const * aIDs[] = { "CN=", "OU=", "O=", "E=", NULL };
61 0 : OUString sPart;
62 0 : int i = 0;
63 0 : while ( aIDs[i] )
64 : {
65 0 : OUString sPartId = OUString::createFromAscii( aIDs[i++] );
66 0 : sal_Int32 nContStart = _rRawString.indexOf( sPartId );
67 0 : if ( nContStart != -1 )
68 : {
69 0 : nContStart += sPartId.getLength();
70 0 : sal_Int32 nContEnd = _rRawString.indexOf( ',', nContStart );
71 0 : if ( nContEnd != -1 )
72 0 : sPart = _rRawString.copy( nContStart, nContEnd - nContStart );
73 : else
74 0 : sPart = _rRawString.copy( nContStart );
75 0 : break;
76 : }
77 0 : }
78 0 : return sPart;
79 : }
80 :
81 : bool
82 0 : isDomainMatch(
83 : const OUString& hostName, const uno::Sequence< OUString >& certHostNames)
84 : {
85 0 : for ( int i = 0; i < certHostNames.getLength(); i++){
86 0 : OUString element = certHostNames[i];
87 :
88 0 : if (element.isEmpty())
89 0 : continue;
90 :
91 0 : if (hostName.equalsIgnoreAsciiCase( element ))
92 0 : return true;
93 :
94 0 : if (element.startsWith("*") &&
95 0 : hostName.getLength() >= element.getLength() )
96 : {
97 0 : OUString cmpStr = element.copy( 1 );
98 0 : if ( hostName.matchIgnoreAsciiCase(
99 0 : cmpStr, hostName.getLength() - cmpStr.getLength()) )
100 0 : return true;
101 : }
102 0 : }
103 :
104 0 : return false;
105 : }
106 :
107 : OUString
108 0 : getLocalizedDatTimeStr(
109 : uno::Reference< uno::XComponentContext> const & xContext,
110 : util::DateTime const & rDateTime )
111 : {
112 0 : OUString aDateTimeStr;
113 0 : Date aDate( Date::EMPTY );
114 0 : tools::Time aTime( tools::Time::EMPTY );
115 :
116 0 : aDate = Date( rDateTime.Day, rDateTime.Month, rDateTime.Year );
117 0 : aTime = tools::Time( rDateTime.Hours, rDateTime.Minutes, rDateTime.Seconds );
118 :
119 0 : LanguageType eUILang = Application::GetSettings().GetUILanguageTag().getLanguageType();
120 0 : SvNumberFormatter *pNumberFormatter = new SvNumberFormatter( xContext, eUILang );
121 0 : OUString aTmpStr;
122 0 : Color* pColor = NULL;
123 0 : Date* pNullDate = pNumberFormatter->GetNullDate();
124 : sal_uInt32 nFormat
125 0 : = pNumberFormatter->GetStandardFormat( css::util::NumberFormat::DATE, eUILang );
126 :
127 0 : pNumberFormatter->GetOutputString( aDate - *pNullDate, nFormat, aTmpStr, &pColor );
128 0 : aDateTimeStr = aTmpStr + " ";
129 :
130 0 : nFormat = pNumberFormatter->GetStandardFormat( css::util::NumberFormat::TIME, eUILang );
131 : pNumberFormatter->GetOutputString(
132 0 : aTime.GetTimeInDays(), nFormat, aTmpStr, &pColor );
133 0 : aDateTimeStr += aTmpStr;
134 :
135 0 : return aDateTimeStr;
136 : }
137 :
138 : bool
139 0 : executeUnknownAuthDialog(
140 : vcl::Window * pParent,
141 : uno::Reference< uno::XComponentContext > const & xContext,
142 : const uno::Reference< security::XCertificate >& rXCert)
143 : {
144 : try
145 : {
146 0 : SolarMutexGuard aGuard;
147 :
148 0 : ScopedVclPtrInstance< UnknownAuthDialog > xDialog(pParent, rXCert, xContext);
149 :
150 : // Get correct resource string
151 0 : OUString aMessage;
152 :
153 0 : std::vector< OUString > aArguments;
154 0 : aArguments.push_back( getContentPart( rXCert->getSubjectName()) );
155 :
156 0 : boost::scoped_ptr< ResMgr > xManager(ResMgr::CreateResMgr("uui"));
157 0 : if (xManager.get())
158 : {
159 0 : ResId aResId(RID_UUI_ERRHDL, *xManager.get());
160 0 : if (ErrorResource(aResId).getString(
161 0 : ERRCODE_UUI_UNKNOWNAUTH_UNTRUSTED, aMessage))
162 : {
163 0 : aMessage = UUIInteractionHelper::replaceMessageWithArguments(
164 0 : aMessage, aArguments );
165 0 : xDialog->setDescriptionText( aMessage );
166 : }
167 : }
168 :
169 0 : return static_cast<bool>(xDialog->Execute());
170 : }
171 0 : catch (std::bad_alloc const &)
172 : {
173 0 : throw uno::RuntimeException("out of memory");
174 : }
175 : }
176 :
177 : bool
178 0 : executeSSLWarnDialog(
179 : vcl::Window * pParent,
180 : uno::Reference< uno::XComponentContext > const & xContext,
181 : const uno::Reference< security::XCertificate >& rXCert,
182 : sal_Int32 const & failure,
183 : const OUString & hostName )
184 : {
185 : try
186 : {
187 0 : SolarMutexGuard aGuard;
188 :
189 0 : ScopedVclPtrInstance< SSLWarnDialog > xDialog(pParent, rXCert, xContext);
190 :
191 : // Get correct resource string
192 0 : OUString aMessage_1;
193 0 : std::vector< OUString > aArguments_1;
194 :
195 0 : switch( failure )
196 : {
197 : case SSLWARN_TYPE_DOMAINMISMATCH:
198 0 : aArguments_1.push_back( hostName );
199 : aArguments_1.push_back(
200 0 : getContentPart( rXCert->getSubjectName()) );
201 0 : aArguments_1.push_back( hostName );
202 0 : break;
203 : case SSLWARN_TYPE_EXPIRED:
204 : aArguments_1.push_back(
205 0 : getContentPart( rXCert->getSubjectName()) );
206 : aArguments_1.push_back(
207 : getLocalizedDatTimeStr( xContext,
208 0 : rXCert->getNotValidAfter() ) );
209 : aArguments_1.push_back(
210 : getLocalizedDatTimeStr( xContext,
211 0 : rXCert->getNotValidAfter() ) );
212 0 : break;
213 : case SSLWARN_TYPE_INVALID:
214 0 : break;
215 : }
216 :
217 0 : boost::scoped_ptr< ResMgr > xManager(ResMgr::CreateResMgr("uui"));
218 :
219 0 : if (xManager.get())
220 : {
221 0 : ResId aResId(RID_UUI_ERRHDL, *xManager.get());
222 0 : if (ErrorResource(aResId).getString(
223 0 : ERRCODE_AREA_UUI_UNKNOWNAUTH + failure + DESCRIPTION_1,
224 0 : aMessage_1))
225 : {
226 0 : aMessage_1 = UUIInteractionHelper::replaceMessageWithArguments(
227 0 : aMessage_1, aArguments_1 );
228 0 : xDialog->setDescription1Text( aMessage_1 );
229 : }
230 :
231 0 : OUString aTitle;
232 0 : if (ErrorResource(aResId).getString(
233 0 : ERRCODE_AREA_UUI_UNKNOWNAUTH + failure + TITLE, aTitle))
234 : {
235 0 : xDialog->SetText(aTitle);
236 0 : }
237 : }
238 :
239 0 : return static_cast<bool>(xDialog->Execute());
240 : }
241 0 : catch (std::bad_alloc const &)
242 : {
243 0 : throw uno::RuntimeException("out of memory");
244 : }
245 : }
246 :
247 : void
248 0 : handleCertificateValidationRequest_(
249 : vcl::Window * pParent,
250 : uno::Reference< uno::XComponentContext > const & xContext,
251 : ucb::CertificateValidationRequest const & rRequest,
252 : uno::Sequence< uno::Reference< task::XInteractionContinuation > > const &
253 : rContinuations)
254 : {
255 0 : uno::Reference< task::XInteractionApprove > xApprove;
256 0 : uno::Reference< task::XInteractionAbort > xAbort;
257 0 : getContinuations(rContinuations, &xApprove, &xAbort);
258 :
259 0 : sal_Int32 failures = rRequest.CertificateValidity;
260 0 : bool trustCert = true;
261 :
262 0 : if ( ((failures & security::CertificateValidity::UNTRUSTED)
263 0 : == security::CertificateValidity::UNTRUSTED ) ||
264 0 : ((failures & security::CertificateValidity::ISSUER_UNTRUSTED)
265 0 : == security::CertificateValidity::ISSUER_UNTRUSTED) ||
266 0 : ((failures & security::CertificateValidity::ROOT_UNTRUSTED)
267 : == security::CertificateValidity::ROOT_UNTRUSTED) )
268 : {
269 : trustCert = executeUnknownAuthDialog( pParent,
270 : xContext,
271 0 : rRequest.Certificate );
272 : }
273 :
274 0 : uno::Sequence< uno::Reference< security::XCertificateExtension > > extensions = rRequest.Certificate->getExtensions();
275 0 : uno::Sequence< security::CertAltNameEntry > altNames;
276 0 : for (sal_Int32 i = 0 ; i < extensions.getLength(); i++){
277 0 : uno::Reference< security::XCertificateExtension >element = extensions[i];
278 :
279 0 : OString aId ( reinterpret_cast<const char *>(element->getExtensionId().getConstArray()), element->getExtensionId().getLength());
280 0 : if (aId.equals(OID_SUBJECT_ALTERNATIVE_NAME))
281 : {
282 0 : uno::Reference< security::XSanExtension > sanExtension ( element, uno::UNO_QUERY );
283 0 : altNames = sanExtension->getAlternativeNames();
284 0 : break;
285 : }
286 0 : }
287 :
288 0 : OUString certHostName = getContentPart( rRequest.Certificate->getSubjectName() );
289 0 : uno::Sequence< OUString > certHostNames(altNames.getLength() + 1);
290 :
291 0 : certHostNames[0] = certHostName;
292 :
293 0 : for(int n = 0; n < altNames.getLength(); ++n)
294 : {
295 0 : if (altNames[n].Type == security::ExtAltNameType_DNS_NAME){
296 0 : altNames[n].Value >>= certHostNames[n+1];
297 : }
298 : }
299 :
300 0 : if ( (!isDomainMatch(
301 : rRequest.HostName,
302 0 : certHostNames )) &&
303 : trustCert )
304 : {
305 : trustCert = executeSSLWarnDialog( pParent,
306 : xContext,
307 : rRequest.Certificate,
308 : SSLWARN_TYPE_DOMAINMISMATCH,
309 0 : rRequest.HostName );
310 : }
311 :
312 0 : else if ( (((failures & security::CertificateValidity::TIME_INVALID)
313 0 : == security::CertificateValidity::TIME_INVALID) ||
314 0 : ((failures & security::CertificateValidity::NOT_TIME_NESTED)
315 0 : == security::CertificateValidity::NOT_TIME_NESTED)) &&
316 : trustCert )
317 : {
318 : trustCert = executeSSLWarnDialog( pParent,
319 : xContext,
320 : rRequest.Certificate,
321 : SSLWARN_TYPE_EXPIRED,
322 0 : rRequest.HostName );
323 : }
324 :
325 0 : else if ( (((failures & security::CertificateValidity::REVOKED)
326 0 : == security::CertificateValidity::REVOKED) ||
327 0 : ((failures & security::CertificateValidity::SIGNATURE_INVALID)
328 0 : == security::CertificateValidity::SIGNATURE_INVALID) ||
329 0 : ((failures & security::CertificateValidity::EXTENSION_INVALID)
330 0 : == security::CertificateValidity::EXTENSION_INVALID) ||
331 0 : ((failures & security::CertificateValidity::INVALID)
332 0 : == security::CertificateValidity::INVALID)) &&
333 : trustCert )
334 : {
335 : trustCert = executeSSLWarnDialog( pParent,
336 : xContext,
337 : rRequest.Certificate,
338 : SSLWARN_TYPE_INVALID,
339 0 : rRequest.HostName );
340 : }
341 :
342 0 : if ( trustCert )
343 : {
344 0 : if (xApprove.is())
345 0 : xApprove->select();
346 : }
347 : else
348 : {
349 0 : if (xAbort.is())
350 0 : xAbort->select();
351 0 : }
352 0 : }
353 :
354 : } // namespace
355 :
356 : bool
357 0 : UUIInteractionHelper::handleCertificateValidationRequest(
358 : uno::Reference< task::XInteractionRequest > const & rRequest)
359 : {
360 0 : uno::Any aAnyRequest(rRequest->getRequest());
361 :
362 0 : ucb::CertificateValidationRequest aCertificateValidationRequest;
363 0 : if (aAnyRequest >>= aCertificateValidationRequest)
364 : {
365 : handleCertificateValidationRequest_(getParentProperty(),
366 : m_xContext,
367 : aCertificateValidationRequest,
368 0 : rRequest->getContinuations());
369 0 : return true;
370 : }
371 :
372 0 : return false;
373 159 : }
374 :
375 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|