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 : #include <xmlsecurity/certificateviewer.hxx>
21 : #include <com/sun/star/security/XCertificate.hpp>
22 :
23 : #include <com/sun/star/security/CertificateCharacters.hpp>
24 : #include <com/sun/star/xml/crypto/XSecurityEnvironment.hpp>
25 : #include <com/sun/star/security/CertificateValidity.hpp>
26 :
27 : #include <unotools/localedatawrapper.hxx>
28 : #include <unotools/datetime.hxx>
29 : #include <svtools/treelistentry.hxx>
30 :
31 : #include "dialogs.hrc"
32 : #include "resourcemanager.hxx"
33 :
34 : #include <vcl/settings.hxx>
35 :
36 : using namespace ::com::sun::star;
37 : using namespace ::com::sun::star::uno;
38 :
39 0 : CertificateViewer::CertificateViewer(
40 : vcl::Window* _pParent,
41 : const css::uno::Reference< css::xml::crypto::XSecurityEnvironment >& _rxSecurityEnvironment,
42 : const css::uno::Reference< css::security::XCertificate >& _rXCert, bool bCheckForPrivateKey )
43 0 : : TabDialog(_pParent, "ViewCertDialog", "xmlsec/ui/viewcertdialog.ui" )
44 : {
45 0 : get(mpTabCtrl, "tabcontrol");
46 :
47 0 : mbCheckForPrivateKey = bCheckForPrivateKey;
48 :
49 0 : mxSecurityEnvironment = _rxSecurityEnvironment;
50 0 : mxCert = _rXCert;
51 :
52 0 : mnGeneralId = mpTabCtrl->GetPageId("general");
53 0 : mnDetailsId = mpTabCtrl->GetPageId("details");
54 0 : mnPathId = mpTabCtrl->GetPageId("path");
55 :
56 0 : mpTabCtrl->SetTabPage(mnGeneralId, new CertificateViewerGeneralTP( mpTabCtrl, this));
57 0 : mpTabCtrl->SetTabPage(mnDetailsId, new CertificateViewerDetailsTP( mpTabCtrl, this));
58 0 : mpTabCtrl->SetTabPage(mnPathId, new CertificateViewerCertPathTP( mpTabCtrl, this));
59 0 : mpTabCtrl->SetCurPageId(mnGeneralId);
60 0 : }
61 :
62 0 : CertificateViewer::~CertificateViewer()
63 : {
64 0 : delete mpTabCtrl->GetTabPage(mnGeneralId);
65 0 : delete mpTabCtrl->GetTabPage(mnDetailsId);
66 0 : delete mpTabCtrl->GetTabPage(mnPathId);
67 0 : }
68 :
69 0 : CertificateViewerTP::CertificateViewerTP( vcl::Window* _pParent, const OString& rID,
70 : const OUString& rUIXMLDescription, CertificateViewer* _pDlg )
71 : : TabPage(_pParent, rID, rUIXMLDescription)
72 0 : , mpDlg(_pDlg)
73 : {
74 0 : }
75 :
76 0 : CertificateViewerGeneralTP::CertificateViewerGeneralTP( vcl::Window* _pParent, CertificateViewer* _pDlg )
77 0 : :CertificateViewerTP ( _pParent, "CertGeneral", "xmlsec/ui/certgeneral.ui", _pDlg )
78 : {
79 0 : get( m_pCertImg, "certimage" );
80 0 : get( m_pHintNotTrustedFI, "hintnotrust" );
81 0 : get( m_pIssuedToFI, "issued_to_value" );
82 0 : get( m_pIssuedByFI, "issued_by_value" );
83 0 : get( m_pValidFromDateFI, "valid_from_value" );
84 0 : get( m_pValidToDateFI, "valid_to_value" );
85 0 : get( m_pKeyImg, "keyimage" );
86 0 : get( m_pHintCorrespPrivKeyFI, "privatekey" );
87 :
88 : //Verify the certificate
89 0 : sal_Int32 certStatus = mpDlg->mxSecurityEnvironment->verifyCertificate(mpDlg->mxCert,
90 0 : Sequence<Reference<css::security::XCertificate> >());
91 :
92 0 : bool bCertValid = certStatus == css::security::CertificateValidity::VALID;
93 :
94 0 : if ( !bCertValid )
95 : {
96 : m_pCertImg->SetImage(
97 0 : Image( XMLSEC_RES( IMG_STATE_NOT_VALIDATED ) ) );
98 0 : m_pHintNotTrustedFI->SetText( XMLSEC_RES( STR_CERTIFICATE_NOT_VALIDATED ) );
99 : }
100 :
101 : // insert data
102 0 : css::uno::Reference< css::security::XCertificate > xCert = mpDlg->mxCert;
103 :
104 0 : m_pIssuedToFI->SetText( XmlSec::GetContentPart( xCert->getSubjectName() ) );
105 0 : m_pIssuedByFI->SetText( XmlSec::GetContentPart( xCert->getIssuerName() ) );
106 :
107 0 : DateTime aDateTimeStart( DateTime::EMPTY );
108 0 : DateTime aDateTimeEnd( DateTime::EMPTY );
109 0 : utl::typeConvert( xCert->getNotValidBefore(), aDateTimeStart );
110 0 : utl::typeConvert( xCert->getNotValidAfter(), aDateTimeEnd );
111 :
112 0 : OUString sValidFromDate = GetSettings().GetUILocaleDataWrapper().getDate( aDateTimeStart.GetDate() );
113 0 : OUString sValidToDate = GetSettings().GetUILocaleDataWrapper().getDate( aDateTimeEnd.GetDate() );
114 :
115 0 : m_pValidFromDateFI->SetText(sValidFromDate);
116 0 : m_pValidToDateFI->SetText(sValidToDate);
117 :
118 : // Check if we have the private key...
119 0 : bool bHasPrivateKey = false;
120 : // #i41270# Check only if we have that certificate in our security environment
121 0 : if ( _pDlg->mbCheckForPrivateKey )
122 : {
123 0 : long nCertificateCharacters = _pDlg->mxSecurityEnvironment->getCertificateCharacters( xCert );
124 0 : bHasPrivateKey = ( nCertificateCharacters & security::CertificateCharacters::HAS_PRIVATE_KEY );
125 : }
126 0 : if ( !bHasPrivateKey )
127 : {
128 0 : m_pKeyImg->Hide();
129 0 : m_pHintCorrespPrivKeyFI->Hide();
130 0 : }
131 0 : }
132 :
133 0 : void CertificateViewerGeneralTP::ActivatePage()
134 : {
135 :
136 0 : }
137 :
138 :
139 0 : struct Details_UserDatat
140 : {
141 : OUString maTxt;
142 : bool mbFixedWidthFont;
143 :
144 : inline Details_UserDatat( const OUString& _rTxt, bool _bFixedWidthFont );
145 : };
146 :
147 0 : inline Details_UserDatat::Details_UserDatat( const OUString& _rTxt, bool _bFixedWidthFont )
148 : :maTxt ( _rTxt )
149 0 : ,mbFixedWidthFont ( _bFixedWidthFont )
150 : {
151 0 : }
152 :
153 :
154 0 : void CertificateViewerDetailsTP::Clear( void )
155 : {
156 0 : m_pValueDetails->SetText( OUString() );
157 0 : sal_uLong i = 0;
158 0 : SvTreeListEntry* pEntry = m_pElementsLB->GetEntry( i );
159 0 : while( pEntry )
160 : {
161 0 : delete ( Details_UserDatat* ) pEntry->GetUserData();
162 0 : ++i;
163 0 : pEntry = m_pElementsLB->GetEntry( i );
164 : }
165 :
166 0 : m_pElementsLB->Clear();
167 0 : }
168 :
169 0 : void CertificateViewerDetailsTP::InsertElement( const OUString& _rField, const OUString& _rValue,
170 : const OUString& _rDetails, bool _bFixedWidthFont )
171 : {
172 0 : SvTreeListEntry* pEntry = m_pElementsLB->InsertEntry( _rField );
173 0 : m_pElementsLB->SetEntryText( _rValue, pEntry, 1 );
174 0 : pEntry->SetUserData( ( void* ) new Details_UserDatat( _rDetails, _bFixedWidthFont ) );
175 0 : }
176 :
177 0 : CertificateViewerDetailsTP::CertificateViewerDetailsTP( vcl::Window* _pParent, CertificateViewer* _pDlg )
178 : :CertificateViewerTP ( _pParent, "CertDetails", "xmlsec/ui/certdetails.ui", _pDlg )
179 0 : ,m_aFixedWidthFont( OutputDevice::GetDefaultFont( DEFAULTFONT_UI_FIXED, LANGUAGE_DONTKNOW, DEFAULTFONT_FLAGS_ONLYONE, this ) )
180 : {
181 0 : get( m_pValueDetails, "valuedetails" );
182 0 : get( m_pElementsLBContainer, "tablecontainer" );
183 0 : m_pElementsLB = new SvSimpleTable( *m_pElementsLBContainer );
184 :
185 0 : m_aStdFont = m_pValueDetails->GetControlFont();
186 0 : WinBits nStyle = m_pElementsLB->GetStyle();
187 0 : nStyle &= ~WB_HSCROLL;
188 0 : m_pElementsLB->SetStyle( nStyle );
189 :
190 0 : m_aFixedWidthFont.SetHeight( m_aStdFont.GetHeight() );
191 :
192 : static long nTabs[] = { 2, 0, 30*CS_LB_WIDTH/100 };
193 0 : m_pElementsLB->SetTabs( &nTabs[ 0 ] );
194 0 : m_pElementsLB->InsertHeaderEntry( XMLSEC_RES( STR_HEADERBAR ) );
195 :
196 : // fill list box
197 0 : Reference< security::XCertificate > xCert = mpDlg->mxCert;
198 0 : sal_uInt16 nLineBreak = 16;
199 0 : const char* pHexSep = " ";
200 0 : OUString aLBEntry;
201 0 : OUString aDetails;
202 : // Certificate Versions are reported wrong (#i35107#) - 0 == "V1", 1 == "V2", ..., n = "V(n+1)"
203 0 : aLBEntry = "V" + OUString::number( xCert->getVersion() + 1 );
204 0 : InsertElement( XMLSEC_RES( STR_VERSION ), aLBEntry, aLBEntry );
205 0 : Sequence< sal_Int8 > aSeq = xCert->getSerialNumber();
206 0 : aLBEntry = XmlSec::GetHexString( aSeq, pHexSep );
207 0 : aDetails = XmlSec::GetHexString( aSeq, pHexSep, nLineBreak );
208 0 : InsertElement( XMLSEC_RES( STR_SERIALNUM ), aLBEntry, aDetails, true );
209 :
210 : std::pair< OUString, OUString> pairIssuer =
211 0 : XmlSec::GetDNForCertDetailsView(xCert->getIssuerName());
212 0 : aLBEntry = pairIssuer.first;
213 0 : aDetails = pairIssuer.second;
214 0 : InsertElement( XMLSEC_RES( STR_ISSUER ), aLBEntry, aDetails );
215 :
216 0 : DateTime aDateTime( DateTime::EMPTY );
217 0 : utl::typeConvert( xCert->getNotValidBefore(), aDateTime );
218 0 : aLBEntry = GetSettings().GetUILocaleDataWrapper().getDate( aDateTime.GetDate() );
219 0 : aLBEntry += " ";
220 0 : aLBEntry += GetSettings().GetUILocaleDataWrapper().getTime( aDateTime.GetTime() );
221 0 : InsertElement( XMLSEC_RES( STR_VALIDFROM ), aLBEntry, aLBEntry );
222 0 : utl::typeConvert( xCert->getNotValidAfter(), aDateTime );
223 0 : aLBEntry = GetSettings().GetUILocaleDataWrapper().getDate( aDateTime.GetDate() );
224 0 : aLBEntry += " ";
225 0 : aLBEntry += GetSettings().GetUILocaleDataWrapper().getTime( aDateTime.GetTime() );
226 0 : InsertElement( XMLSEC_RES( STR_VALIDTO ), aLBEntry, aLBEntry );
227 :
228 : std::pair< OUString, OUString > pairSubject =
229 0 : XmlSec::GetDNForCertDetailsView(xCert->getSubjectName());
230 0 : aLBEntry = pairSubject.first;
231 0 : aDetails = pairSubject.second;
232 0 : InsertElement( XMLSEC_RES( STR_SUBJECT ), aLBEntry, aDetails );
233 :
234 0 : aLBEntry = aDetails = xCert->getSubjectPublicKeyAlgorithm();
235 0 : InsertElement( XMLSEC_RES( STR_SUBJECT_PUBKEY_ALGO ), aLBEntry, aDetails );
236 0 : aSeq = xCert->getSubjectPublicKeyValue();
237 0 : aLBEntry = XmlSec::GetHexString( aSeq, pHexSep );
238 0 : aDetails = XmlSec::GetHexString( aSeq, pHexSep, nLineBreak );
239 0 : InsertElement( XMLSEC_RES( STR_SUBJECT_PUBKEY_VAL ), aLBEntry, aDetails, true );
240 :
241 0 : aLBEntry = aDetails = xCert->getSignatureAlgorithm();
242 0 : InsertElement( XMLSEC_RES( STR_SIGNATURE_ALGO ), aLBEntry, aDetails );
243 :
244 0 : aSeq = xCert->getSHA1Thumbprint();
245 0 : aLBEntry = XmlSec::GetHexString( aSeq, pHexSep );
246 0 : aDetails = XmlSec::GetHexString( aSeq, pHexSep, nLineBreak );
247 0 : InsertElement( XMLSEC_RES( STR_THUMBPRINT_SHA1 ), aLBEntry, aDetails, true );
248 :
249 0 : aSeq = xCert->getMD5Thumbprint();
250 0 : aLBEntry = XmlSec::GetHexString( aSeq, pHexSep );
251 0 : aDetails = XmlSec::GetHexString( aSeq, pHexSep, nLineBreak );
252 0 : InsertElement( XMLSEC_RES( STR_THUMBPRINT_MD5 ), aLBEntry, aDetails, true );
253 :
254 0 : m_pElementsLB->SetSelectHdl( LINK( this, CertificateViewerDetailsTP, ElementSelectHdl ) );
255 0 : }
256 :
257 0 : CertificateViewerDetailsTP::~CertificateViewerDetailsTP()
258 : {
259 0 : Clear();
260 0 : delete m_pElementsLB;
261 0 : }
262 :
263 0 : void CertificateViewerDetailsTP::ActivatePage()
264 : {
265 0 : }
266 :
267 0 : IMPL_LINK_NOARG(CertificateViewerDetailsTP, ElementSelectHdl)
268 : {
269 0 : SvTreeListEntry* pEntry = m_pElementsLB->FirstSelected();
270 0 : OUString aElementText;
271 : bool bFixedWidthFont;
272 0 : if( pEntry )
273 : {
274 0 : const Details_UserDatat* p = ( Details_UserDatat* ) pEntry->GetUserData();
275 0 : aElementText = p->maTxt;
276 0 : bFixedWidthFont = p->mbFixedWidthFont;
277 : }
278 : else
279 0 : bFixedWidthFont = false;
280 :
281 0 : m_pValueDetails->SetFont( bFixedWidthFont? m_aFixedWidthFont : m_aStdFont );
282 0 : m_pValueDetails->SetControlFont( bFixedWidthFont? m_aFixedWidthFont : m_aStdFont );
283 0 : m_pValueDetails->SetText( aElementText );
284 :
285 0 : return 0;
286 : }
287 :
288 0 : struct CertPath_UserData
289 : {
290 : css::uno::Reference< css::security::XCertificate > mxCert;
291 : OUString maStatus;
292 : bool mbValid;
293 :
294 0 : CertPath_UserData( css::uno::Reference< css::security::XCertificate > xCert, bool bValid):
295 : mxCert(xCert),
296 0 : mbValid(bValid)
297 : {
298 0 : }
299 : };
300 :
301 :
302 0 : CertificateViewerCertPathTP::CertificateViewerCertPathTP( vcl::Window* _pParent, CertificateViewer* _pDlg )
303 : : CertificateViewerTP(_pParent, "CertPage", "xmlsec/ui/certpage.ui", _pDlg)
304 : , mpParent(_pDlg)
305 0 : , mbFirstActivateDone(false)
306 : {
307 0 : get(mpCertPathLB, "signatures");
308 0 : get(mpViewCertPB, "viewcert");
309 0 : get(mpCertStatusML, "status");
310 :
311 0 : msCertOK = get<FixedText>("certok")->GetText();
312 0 : msCertNotValidated = get<FixedText>("certnotok")->GetText();
313 0 : maCertImage = get<FixedImage>("imgok")->GetImage();
314 0 : maCertNotValidatedImage = get<FixedImage>("imgnotok")->GetImage();
315 :
316 0 : Size aControlSize(LogicToPixel(Size(251, 45), MAP_APPFONT));
317 0 : mpCertPathLB->set_width_request(aControlSize.Width());
318 0 : mpCertPathLB->set_height_request(aControlSize.Height());
319 0 : mpCertStatusML->set_width_request(aControlSize.Width());
320 0 : mpCertStatusML->set_height_request(aControlSize.Height());
321 :
322 0 : mpCertPathLB->SetNodeDefaultImages();
323 0 : mpCertPathLB->SetSublistOpenWithLeftRight();
324 0 : mpCertPathLB->SetSelectHdl( LINK( this, CertificateViewerCertPathTP, CertSelectHdl ) );
325 0 : mpViewCertPB->SetClickHdl( LINK( this, CertificateViewerCertPathTP, ViewCertHdl ) );
326 0 : }
327 :
328 0 : CertificateViewerCertPathTP::~CertificateViewerCertPathTP()
329 : {
330 0 : Clear();
331 0 : }
332 :
333 0 : void CertificateViewerCertPathTP::ActivatePage()
334 : {
335 0 : if ( !mbFirstActivateDone )
336 : {
337 0 : mbFirstActivateDone = true;
338 : Sequence< Reference< security::XCertificate > > aCertPath =
339 0 : mpParent->mxSecurityEnvironment->buildCertificatePath( mpParent->mxCert );
340 0 : const Reference< security::XCertificate >* pCertPath = aCertPath.getConstArray();
341 :
342 0 : sal_Int32 i, nCnt = aCertPath.getLength();
343 0 : SvTreeListEntry* pParent = NULL;
344 0 : for( i = nCnt; i; )
345 : {
346 0 : const Reference< security::XCertificate > rCert = pCertPath[ --i ];
347 0 : OUString sName = XmlSec::GetContentPart( rCert->getSubjectName() );
348 : //Verify the certificate
349 0 : sal_Int32 certStatus = mpDlg->mxSecurityEnvironment->verifyCertificate(rCert,
350 0 : Sequence<Reference<css::security::XCertificate> >());
351 0 : bool bCertValid = certStatus == css::security::CertificateValidity::VALID;
352 0 : pParent = InsertCert( pParent, sName, rCert, bCertValid);
353 0 : }
354 :
355 0 : mpCertPathLB->Select( pParent );
356 0 : mpViewCertPB->Disable(); // Own certificate selected
357 :
358 0 : while( pParent )
359 : {
360 0 : mpCertPathLB->Expand( pParent );
361 0 : pParent = mpCertPathLB->GetParent( pParent );
362 : }
363 :
364 0 : CertSelectHdl( NULL );
365 : }
366 0 : }
367 :
368 0 : IMPL_LINK_NOARG(CertificateViewerCertPathTP, ViewCertHdl)
369 : {
370 0 : SvTreeListEntry* pEntry = mpCertPathLB->FirstSelected();
371 0 : if( pEntry )
372 : {
373 0 : CertificateViewer aViewer( this, mpDlg->mxSecurityEnvironment, ((CertPath_UserData*)pEntry->GetUserData())->mxCert, false );
374 0 : aViewer.Execute();
375 : }
376 :
377 0 : return 0;
378 : }
379 :
380 0 : IMPL_LINK_NOARG(CertificateViewerCertPathTP, CertSelectHdl)
381 : {
382 0 : OUString sStatus;
383 0 : SvTreeListEntry* pEntry = mpCertPathLB->FirstSelected();
384 0 : if( pEntry )
385 : {
386 0 : CertPath_UserData* pData = (CertPath_UserData*) pEntry->GetUserData();
387 0 : if ( pData )
388 0 : sStatus = pData->mbValid ? msCertOK : msCertNotValidated;
389 : }
390 :
391 0 : mpCertStatusML->SetText( sStatus );
392 0 : mpViewCertPB->Enable( pEntry && ( pEntry != mpCertPathLB->Last() ) );
393 0 : return 0;
394 : }
395 :
396 0 : void CertificateViewerCertPathTP::Clear( void )
397 : {
398 0 : mpCertStatusML->SetText( OUString() );
399 0 : sal_uLong i = 0;
400 0 : SvTreeListEntry* pEntry = mpCertPathLB->GetEntry( i );
401 0 : while( pEntry )
402 : {
403 0 : delete ( CertPath_UserData* ) pEntry->GetUserData();
404 0 : ++i;
405 0 : pEntry = mpCertPathLB->GetEntry( i );
406 : }
407 :
408 0 : mpCertPathLB->Clear();
409 0 : }
410 :
411 0 : SvTreeListEntry* CertificateViewerCertPathTP::InsertCert(
412 : SvTreeListEntry* _pParent, const OUString& _rName, css::uno::Reference< css::security::XCertificate > rxCert,
413 : bool bValid)
414 : {
415 0 : Image aImage = bValid ? maCertImage : maCertNotValidatedImage;
416 0 : SvTreeListEntry* pEntry = mpCertPathLB->InsertEntry( _rName, aImage, aImage, _pParent );
417 0 : pEntry->SetUserData( ( void* ) new CertPath_UserData( rxCert, bValid ) );
418 :
419 0 : return pEntry;
420 162 : }
421 :
422 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|