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, 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 : Time aTime( Time::EMPTY );
115 :
116 0 : aDate = Date( rDateTime.Day, rDateTime.Month, rDateTime.Year );
117 0 : aTime = 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( NUMBERFORMAT_DATE, eUILang );
126 :
127 0 : pNumberFormatter->GetOutputString( aDate - *pNullDate, nFormat, aTmpStr, &pColor );
128 0 : aDateTimeStr = aTmpStr + " ";
129 :
130 0 : nFormat = pNumberFormatter->GetStandardFormat( 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 : sal_Bool
139 0 : executeUnknownAuthDialog(
140 : Window * pParent,
141 : uno::Reference< uno::XComponentContext > const & xContext,
142 : const uno::Reference< security::XCertificate >& rXCert)
143 : SAL_THROW((uno::RuntimeException))
144 : {
145 : try
146 : {
147 0 : SolarMutexGuard aGuard;
148 :
149 : boost::scoped_ptr< UnknownAuthDialog > xDialog(
150 0 : new UnknownAuthDialog(pParent, rXCert, xContext));
151 :
152 : // Get correct resource string
153 0 : OUString aMessage;
154 :
155 0 : std::vector< OUString > aArguments;
156 0 : aArguments.push_back( getContentPart( rXCert->getSubjectName()) );
157 :
158 0 : boost::scoped_ptr< ResMgr > xManager(ResMgr::CreateResMgr("uui"));
159 0 : if (xManager.get())
160 : {
161 0 : ResId aResId(RID_UUI_ERRHDL, *xManager.get());
162 0 : if (ErrorResource(aResId).getString(
163 0 : ERRCODE_UUI_UNKNOWNAUTH_UNTRUSTED, aMessage))
164 : {
165 0 : aMessage = UUIInteractionHelper::replaceMessageWithArguments(
166 0 : aMessage, aArguments );
167 0 : xDialog->setDescriptionText( aMessage );
168 : }
169 : }
170 :
171 0 : return static_cast<sal_Bool> (xDialog->Execute());
172 : }
173 0 : catch (std::bad_alloc const &)
174 : {
175 : throw uno::RuntimeException(
176 : OUString("out of memory"),
177 0 : uno::Reference< uno::XInterface >());
178 : }
179 : }
180 :
181 : sal_Bool
182 0 : executeSSLWarnDialog(
183 : Window * pParent,
184 : uno::Reference< uno::XComponentContext > const & xContext,
185 : const uno::Reference< security::XCertificate >& rXCert,
186 : sal_Int32 const & failure,
187 : const OUString & hostName )
188 : SAL_THROW((uno::RuntimeException))
189 : {
190 : try
191 : {
192 0 : SolarMutexGuard aGuard;
193 :
194 : boost::scoped_ptr< SSLWarnDialog > xDialog(
195 0 : new SSLWarnDialog(pParent, rXCert, xContext));
196 :
197 : // Get correct resource string
198 0 : OUString aMessage_1;
199 0 : std::vector< OUString > aArguments_1;
200 :
201 0 : switch( failure )
202 : {
203 : case SSLWARN_TYPE_DOMAINMISMATCH:
204 0 : aArguments_1.push_back( hostName );
205 : aArguments_1.push_back(
206 0 : getContentPart( rXCert->getSubjectName()) );
207 0 : aArguments_1.push_back( hostName );
208 0 : break;
209 : case SSLWARN_TYPE_EXPIRED:
210 : aArguments_1.push_back(
211 0 : getContentPart( rXCert->getSubjectName()) );
212 : aArguments_1.push_back(
213 : getLocalizedDatTimeStr( xContext,
214 0 : rXCert->getNotValidAfter() ) );
215 : aArguments_1.push_back(
216 : getLocalizedDatTimeStr( xContext,
217 0 : rXCert->getNotValidAfter() ) );
218 0 : break;
219 : case SSLWARN_TYPE_INVALID:
220 0 : break;
221 : }
222 :
223 0 : boost::scoped_ptr< ResMgr > xManager(ResMgr::CreateResMgr("uui"));
224 :
225 0 : if (xManager.get())
226 : {
227 0 : ResId aResId(RID_UUI_ERRHDL, *xManager.get());
228 0 : if (ErrorResource(aResId).getString(
229 0 : ERRCODE_AREA_UUI_UNKNOWNAUTH + failure + DESCRIPTION_1,
230 0 : aMessage_1))
231 : {
232 0 : aMessage_1 = UUIInteractionHelper::replaceMessageWithArguments(
233 0 : aMessage_1, aArguments_1 );
234 0 : xDialog->setDescription1Text( aMessage_1 );
235 : }
236 :
237 0 : OUString aTitle;
238 : ErrorResource(aResId).getString(
239 0 : ERRCODE_AREA_UUI_UNKNOWNAUTH + failure + TITLE, aTitle);
240 0 : xDialog->SetText( aTitle );
241 : }
242 :
243 0 : return static_cast<sal_Bool> (xDialog->Execute());
244 : }
245 0 : catch (std::bad_alloc const &)
246 : {
247 : throw uno::RuntimeException(
248 : OUString("out of memory"),
249 0 : uno::Reference< uno::XInterface >());
250 : }
251 : }
252 :
253 : void
254 0 : handleCertificateValidationRequest_(
255 : Window * pParent,
256 : uno::Reference< uno::XComponentContext > const & xContext,
257 : ucb::CertificateValidationRequest const & rRequest,
258 : uno::Sequence< uno::Reference< task::XInteractionContinuation > > const &
259 : rContinuations)
260 : SAL_THROW((uno::RuntimeException))
261 : {
262 0 : uno::Reference< task::XInteractionApprove > xApprove;
263 0 : uno::Reference< task::XInteractionAbort > xAbort;
264 0 : getContinuations(rContinuations, &xApprove, &xAbort);
265 :
266 0 : sal_Int32 failures = rRequest.CertificateValidity;
267 0 : sal_Bool trustCert = sal_True;
268 :
269 0 : if ( ((failures & security::CertificateValidity::UNTRUSTED)
270 0 : == security::CertificateValidity::UNTRUSTED ) ||
271 0 : ((failures & security::CertificateValidity::ISSUER_UNTRUSTED)
272 0 : == security::CertificateValidity::ISSUER_UNTRUSTED) ||
273 0 : ((failures & security::CertificateValidity::ROOT_UNTRUSTED)
274 : == security::CertificateValidity::ROOT_UNTRUSTED) )
275 : {
276 : trustCert = executeUnknownAuthDialog( pParent,
277 : xContext,
278 0 : rRequest.Certificate );
279 : }
280 :
281 0 : uno::Sequence< uno::Reference< security::XCertificateExtension > > extensions = rRequest.Certificate->getExtensions();
282 0 : uno::Sequence< security::CertAltNameEntry > altNames;
283 0 : for (sal_Int32 i = 0 ; i < extensions.getLength(); i++){
284 0 : uno::Reference< security::XCertificateExtension >element = extensions[i];
285 :
286 0 : OString aId ( (const sal_Char *)element->getExtensionId().getArray(), element->getExtensionId().getLength());
287 0 : if (aId.equals(OID_SUBJECT_ALTERNATIVE_NAME))
288 : {
289 0 : uno::Reference< security::XSanExtension > sanExtension ( element, uno::UNO_QUERY );
290 0 : altNames = sanExtension->getAlternativeNames();
291 0 : break;
292 : }
293 0 : }
294 :
295 0 : OUString certHostName = getContentPart( rRequest.Certificate->getSubjectName() );
296 0 : uno::Sequence< OUString > certHostNames(altNames.getLength() + 1);
297 :
298 0 : certHostNames[0] = certHostName;
299 :
300 0 : for(int n = 0; n < altNames.getLength(); ++n)
301 : {
302 0 : if (altNames[n].Type == security::ExtAltNameType_DNS_NAME){
303 0 : altNames[n].Value >>= certHostNames[n+1];
304 : }
305 : }
306 :
307 0 : if ( (!isDomainMatch(
308 : rRequest.HostName,
309 0 : certHostNames )) &&
310 : trustCert )
311 : {
312 : trustCert = executeSSLWarnDialog( pParent,
313 : xContext,
314 : rRequest.Certificate,
315 : SSLWARN_TYPE_DOMAINMISMATCH,
316 0 : rRequest.HostName );
317 : }
318 :
319 0 : else if ( (((failures & security::CertificateValidity::TIME_INVALID)
320 0 : == security::CertificateValidity::TIME_INVALID) ||
321 0 : ((failures & security::CertificateValidity::NOT_TIME_NESTED)
322 0 : == security::CertificateValidity::NOT_TIME_NESTED)) &&
323 : trustCert )
324 : {
325 : trustCert = executeSSLWarnDialog( pParent,
326 : xContext,
327 : rRequest.Certificate,
328 : SSLWARN_TYPE_EXPIRED,
329 0 : rRequest.HostName );
330 : }
331 :
332 0 : else if ( (((failures & security::CertificateValidity::REVOKED)
333 0 : == security::CertificateValidity::REVOKED) ||
334 0 : ((failures & security::CertificateValidity::SIGNATURE_INVALID)
335 0 : == security::CertificateValidity::SIGNATURE_INVALID) ||
336 0 : ((failures & security::CertificateValidity::EXTENSION_INVALID)
337 0 : == security::CertificateValidity::EXTENSION_INVALID) ||
338 0 : ((failures & security::CertificateValidity::INVALID)
339 0 : == security::CertificateValidity::INVALID)) &&
340 : trustCert )
341 : {
342 : trustCert = executeSSLWarnDialog( pParent,
343 : xContext,
344 : rRequest.Certificate,
345 : SSLWARN_TYPE_INVALID,
346 0 : rRequest.HostName );
347 : }
348 :
349 0 : if ( trustCert )
350 : {
351 0 : if (xApprove.is())
352 0 : xApprove->select();
353 : }
354 : else
355 : {
356 0 : if (xAbort.is())
357 0 : xAbort->select();
358 0 : }
359 0 : }
360 :
361 : } // namespace
362 :
363 : bool
364 0 : UUIInteractionHelper::handleCertificateValidationRequest(
365 : uno::Reference< task::XInteractionRequest > const & rRequest)
366 : SAL_THROW((uno::RuntimeException))
367 : {
368 0 : uno::Any aAnyRequest(rRequest->getRequest());
369 :
370 0 : ucb::CertificateValidationRequest aCertificateValidationRequest;
371 0 : if (aAnyRequest >>= aCertificateValidationRequest)
372 : {
373 : handleCertificateValidationRequest_(getParentProperty(),
374 : m_xContext,
375 : aCertificateValidationRequest,
376 0 : rRequest->getContinuations());
377 0 : return true;
378 : }
379 :
380 0 : return false;
381 0 : }
382 :
383 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|