1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_xmlsecurity.hxx"
26 #include <xmlsecurity/certificateviewer.hxx>
27 #include <com/sun/star/security/XCertificate.hpp>
28 
29 #include <com/sun/star/security/CertificateCharacters.hpp>
30 #include <com/sun/star/xml/crypto/XSecurityEnvironment.hpp>
31 #include <com/sun/star/security/CertificateValidity.hpp>
32 
33 #include <unotools/localedatawrapper.hxx>
34 #include <unotools/datetime.hxx>
35 
36 #include "dialogs.hrc"
37 #include "resourcemanager.hxx"
38 
39 /* HACK: disable some warnings for MS-C */
40 #ifdef _MSC_VER
41 #pragma warning (disable : 4355)	// 4355: this used in initializer-list
42 #endif
43 
44 using namespace ::com::sun::star;
45 using namespace ::com::sun::star::uno;
46 namespace css = ::com::sun::star;
47 
48 
49 namespace
50 {
51 	void ShrinkToFit( FixedImage& _rImage );
52 	void AdjustPosAndSize( Control& _rCtrl, Point& _rStartIn_EndOut, long _nXOffset = 0 );
53 
ShrinkToFit(FixedImage & _rImg)54 	void ShrinkToFit( FixedImage& _rImg )
55 	{
56 		_rImg.SetSizePixel( _rImg.GetImage().GetSizePixel() );
57 	}
58 
AdjustPosAndSize(Control & _rCtrl,Point & _rStartIn_EndOut,long _nOffs)59 	void AdjustPosAndSize( Control& _rCtrl, Point& _rStartIn_EndOut, long _nOffs )
60 	{
61 		_rCtrl.SetPosPixel( _rStartIn_EndOut );
62 		_rStartIn_EndOut.X() += XmlSec::ShrinkToFitWidth( _rCtrl, _nOffs );
63 	}
64 }
65 
CertificateViewer(Window * _pParent,const cssu::Reference<dcss::xml::crypto::XSecurityEnvironment> & _rxSecurityEnvironment,const cssu::Reference<dcss::security::XCertificate> & _rXCert,sal_Bool bCheckForPrivateKey)66 CertificateViewer::CertificateViewer(
67 		Window* _pParent,
68 		const cssu::Reference< dcss::xml::crypto::XSecurityEnvironment >& _rxSecurityEnvironment,
69 		const cssu::Reference< dcss::security::XCertificate >& _rXCert, sal_Bool bCheckForPrivateKey )
70 	:TabDialog		( _pParent, XMLSEC_RES( RID_XMLSECDLG_CERTVIEWER ) )
71 	,maTabCtrl		( this, XMLSEC_RES( 1 ) )
72 	,maOkBtn		( this, XMLSEC_RES( BTN_OK ) )
73 	,maHelpBtn		( this, XMLSEC_RES( BTN_HELP ) )
74 {
75 	FreeResource();
76 
77 	mbCheckForPrivateKey = bCheckForPrivateKey;
78 
79 	mxSecurityEnvironment = _rxSecurityEnvironment;
80 	mxCert = _rXCert;
81 
82 	maTabCtrl.SetTabPage( RID_XMLSECTP_GENERAL, new CertificateViewerGeneralTP( &maTabCtrl, this ) );
83 	maTabCtrl.SetTabPage( RID_XMLSECTP_DETAILS, new CertificateViewerDetailsTP( &maTabCtrl, this ) );
84 	maTabCtrl.SetTabPage( RID_XMLSECTP_CERTPATH, new CertificateViewerCertPathTP( &maTabCtrl, this ) );
85 	maTabCtrl.SetCurPageId( RID_XMLSECTP_GENERAL );
86 }
87 
~CertificateViewer()88 CertificateViewer::~CertificateViewer()
89 {
90 	delete maTabCtrl.GetTabPage( RID_XMLSECTP_CERTPATH );
91 	delete maTabCtrl.GetTabPage( RID_XMLSECTP_DETAILS );
92 	delete maTabCtrl.GetTabPage( RID_XMLSECTP_GENERAL );
93 }
94 
CertificateViewerTP(Window * _pParent,const ResId & _rResId,CertificateViewer * _pDlg)95 CertificateViewerTP::CertificateViewerTP( Window* _pParent, const ResId& _rResId, CertificateViewer* _pDlg )
96 	:TabPage		( _pParent, _rResId )
97 	,mpDlg			( _pDlg )
98 {
99 }
100 
101 
CertificateViewerGeneralTP(Window * _pParent,CertificateViewer * _pDlg)102 CertificateViewerGeneralTP::CertificateViewerGeneralTP( Window* _pParent, CertificateViewer* _pDlg )
103 	:CertificateViewerTP	( _pParent, XMLSEC_RES( RID_XMLSECTP_GENERAL ), _pDlg )
104 	,maFrameWin				( this, XMLSEC_RES( WIN_FRAME ) )
105 	,maCertImg				( this, XMLSEC_RES( IMG_CERT ) )
106 	,maCertInfoFI			( this, XMLSEC_RES( FI_CERTINFO ) )
107 	,maSep1FL				( this, XMLSEC_RES( FL_SEP1 ) )
108 	,maHintNotTrustedFI		( this, XMLSEC_RES( FI_HINTNOTTRUST ) )
109 	,maSep2FL				( this, XMLSEC_RES( FL_SEP2 ) )
110 	,maIssuedToLabelFI		( this, XMLSEC_RES( FI_ISSTOLABEL ) )
111 	,maIssuedToFI			( this, XMLSEC_RES( FI_ISSTO ) )
112 	,maIssuedByLabelFI		( this, XMLSEC_RES( FI_ISSBYLABEL ) )
113 	,maIssuedByFI			( this, XMLSEC_RES( FI_ISSBY ) )
114     ,maValidDateFI          ( this, XMLSEC_RES( FI_VALIDDATE ) )
115 	,maKeyImg				( this, XMLSEC_RES( IMG_KEY ) )
116 	,maHintCorrespPrivKeyFI	( this, XMLSEC_RES( FI_CORRPRIVKEY ) )
117 {
118     if ( GetSettings().GetStyleSettings().GetHighContrastMode() )
119         maKeyImg.SetImage( Image( XMLSEC_RES( IMG_KEY_HC ) ) );
120 
121     //Verify the certificate
122 	sal_Int32 certStatus = mpDlg->mxSecurityEnvironment->verifyCertificate(mpDlg->mxCert,
123          Sequence<Reference<css::security::XCertificate> >());
124 
125 	bool bCertValid = certStatus == css::security::CertificateValidity::VALID ?  true : false;
126 
127     bool bHC = GetSettings().GetStyleSettings().GetHighContrastMode();
128     if ( !bCertValid )
129     {
130         maCertImg.SetImage(
131             Image( XMLSEC_RES( bHC ? IMG_STATE_NOT_VALIDATED_HC : IMG_STATE_NOT_VALIDATED ) ) );
132         maHintNotTrustedFI.SetText( String( XMLSEC_RES( STR_CERTIFICATE_NOT_VALIDATED ) ) );
133     }
134     else if ( bHC )
135         maCertImg.SetImage( Image( XMLSEC_RES( IMG_STATE_CERIFICATED_HC ) ) );
136 
137     FreeResource();
138 
139     Wallpaper aBack( GetSettings().GetStyleSettings().GetWindowColor() );
140 	maFrameWin.SetBackground( aBack );
141 	maCertImg.SetBackground( aBack );
142 	maCertInfoFI.SetBackground( aBack );
143 	maSep1FL.SetBackground( aBack );
144 	maHintNotTrustedFI.SetBackground( aBack );
145 	maSep2FL.SetBackground( aBack );
146 	maIssuedToLabelFI.SetBackground( aBack );
147 	maIssuedToFI.SetBackground( aBack );
148 	maIssuedByLabelFI.SetBackground( aBack );
149 	maIssuedByFI.SetBackground( aBack );
150     maValidDateFI.SetBackground( aBack );
151 	maKeyImg.SetBackground( aBack );
152 	maHintCorrespPrivKeyFI.SetBackground( aBack );
153 
154 	// make some bold
155 	Font	aFnt( maCertInfoFI.GetFont() );
156 	aFnt.SetWeight( WEIGHT_BOLD );
157 	maCertInfoFI.SetFont( aFnt );
158 	maHintNotTrustedFI.SetFont( aFnt );
159 	maIssuedToLabelFI.SetFont( aFnt );
160 	maIssuedByLabelFI.SetFont( aFnt );
161     maValidDateFI.SetFont( aFnt );
162 
163 	// insert data
164 	cssu::Reference< dcss::security::XCertificate >	xCert = mpDlg->mxCert;
165 
166     maIssuedToFI.SetText( XmlSec::GetContentPart( xCert->getSubjectName() ) );
167     maIssuedByFI.SetText( XmlSec::GetContentPart( xCert->getIssuerName() ) );
168 
169     // dynamic length because of the different languages
170     long nWidth1 = maIssuedToLabelFI.GetTextWidth( maIssuedToLabelFI.GetText() );
171     long nWidth2 = maIssuedByLabelFI.GetTextWidth( maIssuedByLabelFI.GetText() );
172     long nNewWidth = Max( nWidth1, nWidth2 ) + 5;
173     Size aNewSize = maIssuedToLabelFI.GetSizePixel();
174     aNewSize.Width() = nNewWidth;
175     maIssuedToLabelFI.SetSizePixel( aNewSize );
176     maIssuedByLabelFI.SetSizePixel( aNewSize );
177     long nNewX = maIssuedToLabelFI.GetPosPixel().X() + nNewWidth + 1;
178     Point aNewPos = maIssuedToFI.GetPosPixel();
179     aNewPos.X() = nNewX;
180     maIssuedToFI.SetPosPixel( aNewPos );
181     aNewPos = maIssuedByFI.GetPosPixel();
182     aNewPos.X() = nNewX;
183     maIssuedByFI.SetPosPixel( aNewPos );
184     nNewWidth = maValidDateFI.GetSizePixel().Width() - nNewX;
185     aNewSize = maIssuedToFI.GetSizePixel();
186     aNewSize.Width() = nNewWidth;
187     maIssuedToFI.SetSizePixel( aNewSize );
188     maIssuedByFI.SetSizePixel( aNewSize );
189 
190     DateTime aDateTimeStart;
191     DateTime aDateTimeEnd;
192     utl::typeConvert( xCert->getNotValidBefore(), aDateTimeStart );
193     utl::typeConvert( xCert->getNotValidAfter(), aDateTimeEnd );
194     String sText = maValidDateFI.GetText();
195     sText.SearchAndReplace( String::CreateFromAscii( "%SDATE%" ),
196                             GetSettings().GetUILocaleDataWrapper().getDate( aDateTimeStart.GetDate() ) );
197     sText.SearchAndReplace( String::CreateFromAscii( "%EDATE%" ),
198                             GetSettings().GetUILocaleDataWrapper().getDate( aDateTimeEnd.GetDate() ) );
199     maValidDateFI.SetText( sText );
200 
201 	// adjust position of fixed text depending on image sizes
202 	ShrinkToFit( maCertImg );
203 	ShrinkToFit( maKeyImg );
204 	XmlSec::AlignAfterImage( maCertImg, maCertInfoFI, 12 );
205 	XmlSec::AlignAfterImage( maKeyImg, maHintCorrespPrivKeyFI, 12 );
206 
207     // Check if we have the private key...
208 	sal_Bool bHasPrivateKey = sal_False;
209 	// #i41270# Check only if we have that certificate in our security environment
210 	if ( _pDlg->mbCheckForPrivateKey )
211 	{
212 		long nCertificateCharacters = _pDlg->mxSecurityEnvironment->getCertificateCharacters( xCert );
213 		bHasPrivateKey = ( nCertificateCharacters & security::CertificateCharacters::HAS_PRIVATE_KEY ) ? sal_True : sal_False;
214 	}
215     if ( !bHasPrivateKey )
216     {
217 	    maKeyImg.Hide();
218 	    maHintCorrespPrivKeyFI.Hide();
219     }
220 }
221 
ActivatePage()222 void CertificateViewerGeneralTP::ActivatePage()
223 {
224 
225 }
226 
227 
228 struct Details_UserDatat
229 {
230 	String			maTxt;
231 	bool			mbFixedWidthFont;
232 
233 	inline			Details_UserDatat( const String& _rTxt, bool _bFixedWidthFont );
234 };
235 
Details_UserDatat(const String & _rTxt,bool _bFixedWidthFont)236 inline Details_UserDatat::Details_UserDatat( const String& _rTxt, bool _bFixedWidthFont )
237 	:maTxt				( _rTxt )
238 	,mbFixedWidthFont	( _bFixedWidthFont )
239 {
240 }
241 
242 
Clear(void)243 void CertificateViewerDetailsTP::Clear( void )
244 {
245 	maElementML.SetText( String() );
246 	sal_uLong			i = 0;
247 	SvLBoxEntry*	pEntry = maElementsLB.GetEntry( i );
248 	while( pEntry )
249 	{
250 		delete ( Details_UserDatat* ) pEntry->GetUserData();
251 		++i;
252 		pEntry = maElementsLB.GetEntry( i );
253 	}
254 
255 	maElementsLB.Clear();
256 }
257 
InsertElement(const String & _rField,const String & _rValue,const String & _rDetails,bool _bFixedWidthFont)258 void CertificateViewerDetailsTP::InsertElement( const String& _rField, const String& _rValue,
259 												const String& _rDetails, bool _bFixedWidthFont )
260 {
261 	SvLBoxEntry*	pEntry = maElementsLB.InsertEntry( _rField );
262 	maElementsLB.SetEntryText( _rValue, pEntry, 1 );
263 	pEntry->SetUserData( ( void* ) new Details_UserDatat( _rDetails, _bFixedWidthFont ) );
264 }
265 
CertificateViewerDetailsTP(Window * _pParent,CertificateViewer * _pDlg)266 CertificateViewerDetailsTP::CertificateViewerDetailsTP( Window* _pParent, CertificateViewer* _pDlg )
267 	:CertificateViewerTP	( _pParent, XMLSEC_RES( RID_XMLSECTP_DETAILS ), _pDlg  )
268 	,maElementsLB			( this, XMLSEC_RES( LB_ELEMENTS ) )
269 	,maElementML			( this, XMLSEC_RES( ML_ELEMENT ) )
270 	,maStdFont				( maElementML.GetControlFont() )
271 	,maFixedWidthFont		( OutputDevice::GetDefaultFont( DEFAULTFONT_UI_FIXED, LANGUAGE_DONTKNOW, DEFAULTFONT_FLAGS_ONLYONE, this ) )
272 {
273     WinBits nStyle = maElementsLB.GetStyle();
274     nStyle &= ~WB_HSCROLL;
275     maElementsLB.SetStyle( nStyle );
276 
277 	maFixedWidthFont.SetHeight( maStdFont.GetHeight() );
278 
279 	static long	nTabs[] = { 2, 0, 30*CS_LB_WIDTH/100 };
280 	maElementsLB.SetTabs( &nTabs[ 0 ] );
281 	maElementsLB.InsertHeaderEntry( String( XMLSEC_RES( STR_HEADERBAR ) ) );
282 
283 	// fill list box
284 	Reference< security::XCertificate > xCert = mpDlg->mxCert;
285 	sal_uInt16					nLineBreak = 16;
286 	const char*				pHexSep = " ";
287 	String					aLBEntry;
288 	String					aDetails;
289     // --> PB 2004-10-11 #i35107# - 0 == "V1", 1 == "V2", ..., n = "V(n+1)"
290 	aLBEntry = String::CreateFromAscii( "V" );
291     aLBEntry += String::CreateFromInt32( xCert->getVersion() + 1 );
292     // <--
293 	InsertElement( String( XMLSEC_RES( STR_VERSION ) ), aLBEntry, aLBEntry );
294 	Sequence< sal_Int8 >	aSeq = xCert->getSerialNumber();
295 	aLBEntry = XmlSec::GetHexString( aSeq, pHexSep );
296 	aDetails = XmlSec::GetHexString( aSeq, pHexSep, nLineBreak );
297 	InsertElement( String( XMLSEC_RES( STR_SERIALNUM ) ), aLBEntry, aDetails, true );
298 
299     std::pair< ::rtl::OUString, ::rtl::OUString> pairIssuer =
300         XmlSec::GetDNForCertDetailsView(xCert->getIssuerName());
301 	aLBEntry = pairIssuer.first;
302 	aDetails = pairIssuer.second;
303 	InsertElement( String( XMLSEC_RES( STR_ISSUER ) ), aLBEntry, aDetails );
304     /*
305 	aSeq = xCert->getIssuerUniqueID();
306 	aLBEntry = XmlSec::GetHexString( aSeq, pHexSep );
307 	aDetails = XmlSec::GetHexString( aSeq, pHexSep, nLineBreak );
308 	InsertElement( String( XMLSEC_RES( STR_ISSUER_ID ) ), aLBEntry, aDetails, true );
309     */
310 
311     DateTime aDateTime;
312     utl::typeConvert( xCert->getNotValidBefore(), aDateTime );
313 	aLBEntry = GetSettings().GetUILocaleDataWrapper().getDate( aDateTime.GetDate() );
314     aLBEntry += String::CreateFromAscii( " " );
315 	aLBEntry += GetSettings().GetUILocaleDataWrapper().getTime( aDateTime.GetTime() );
316 	InsertElement( String( XMLSEC_RES( STR_VALIDFROM ) ), aLBEntry, aLBEntry  );
317     utl::typeConvert( xCert->getNotValidAfter(), aDateTime );
318 	aLBEntry = GetSettings().GetUILocaleDataWrapper().getDate( aDateTime.GetDate() );
319     aLBEntry += String::CreateFromAscii( " " );
320 	aLBEntry += GetSettings().GetUILocaleDataWrapper().getTime( aDateTime.GetTime() );
321 	InsertElement( String( XMLSEC_RES( STR_VALIDTO ) ), aLBEntry, aLBEntry );
322 
323     std::pair< ::rtl::OUString, ::rtl::OUString > pairSubject =
324         XmlSec::GetDNForCertDetailsView(xCert->getSubjectName());
325 	aLBEntry = pairSubject.first;
326 	aDetails = pairSubject.second;
327 	InsertElement( String( XMLSEC_RES( STR_SUBJECT ) ), aLBEntry, aDetails );
328     /*
329 	aSeq = xCert->getSubjectUniqueID();
330 	aLBEntry = XmlSec::GetHexString( aSeq, pHexSep );
331 	aDetails = XmlSec::GetHexString( aSeq, pHexSep, nLineBreak );
332 	InsertElement( String( XMLSEC_RES( STR_SUBJECT_ID ) ), aLBEntry, aDetails, true );
333     */
334 	aLBEntry = aDetails = xCert->getSubjectPublicKeyAlgorithm();
335 	InsertElement( String( XMLSEC_RES( STR_SUBJECT_PUBKEY_ALGO ) ), aLBEntry, aDetails );
336 	aSeq = xCert->getSubjectPublicKeyValue();
337 	aLBEntry = XmlSec::GetHexString( aSeq, pHexSep );
338 	aDetails = XmlSec::GetHexString( aSeq, pHexSep, nLineBreak );
339 	InsertElement( String( XMLSEC_RES( STR_SUBJECT_PUBKEY_VAL ) ), aLBEntry, aDetails, true );
340 
341 	aLBEntry = aDetails = xCert->getSignatureAlgorithm();
342 	InsertElement( String( XMLSEC_RES( STR_SIGNATURE_ALGO ) ), aLBEntry, aDetails );
343 
344     aSeq = xCert->getSHA1Thumbprint();
345     aLBEntry = XmlSec::GetHexString( aSeq, pHexSep );
346 	aDetails = XmlSec::GetHexString( aSeq, pHexSep, nLineBreak );
347 	InsertElement( String( XMLSEC_RES( STR_THUMBPRINT_SHA1 ) ), aLBEntry, aDetails, true );
348 
349     aSeq = xCert->getMD5Thumbprint();
350     aLBEntry = XmlSec::GetHexString( aSeq, pHexSep );
351 	aDetails = XmlSec::GetHexString( aSeq, pHexSep, nLineBreak );
352 	InsertElement( String( XMLSEC_RES( STR_THUMBPRINT_MD5 ) ), aLBEntry, aDetails, true );
353 
354 	FreeResource();
355 
356 	maElementsLB.SetSelectHdl( LINK( this, CertificateViewerDetailsTP, ElementSelectHdl ) );
357 }
358 
~CertificateViewerDetailsTP()359 CertificateViewerDetailsTP::~CertificateViewerDetailsTP()
360 {
361 	Clear();
362 }
363 
ActivatePage()364 void CertificateViewerDetailsTP::ActivatePage()
365 {
366 }
367 
IMPL_LINK(CertificateViewerDetailsTP,ElementSelectHdl,void *,EMPTYARG)368 IMPL_LINK( CertificateViewerDetailsTP, ElementSelectHdl, void*, EMPTYARG )
369 {
370 	SvLBoxEntry*	pEntry = maElementsLB.FirstSelected();
371 	String			aElementText;
372 	bool			bFixedWidthFont;
373 	if( pEntry )
374 	{
375 		const Details_UserDatat*	p = ( Details_UserDatat* ) pEntry->GetUserData();
376 		aElementText = p->maTxt;
377 		bFixedWidthFont = p->mbFixedWidthFont;
378 	}
379 	else
380 		bFixedWidthFont = false;
381 
382 	maElementML.SetFont( bFixedWidthFont? maFixedWidthFont : maStdFont );
383 	maElementML.SetControlFont( bFixedWidthFont? maFixedWidthFont : maStdFont );
384 	maElementML.SetText( aElementText );
385 
386     return 0;
387 }
388 
389 struct CertPath_UserData
390 {
391     cssu::Reference< dcss::security::XCertificate > mxCert;
392 	String			                                maStatus;
393 	bool mbValid;
394 
CertPath_UserDataCertPath_UserData395 	CertPath_UserData( cssu::Reference< dcss::security::XCertificate > xCert, bool bValid):
396 		mxCert(xCert),
397 		mbValid(bValid)
398 	{
399 	}
400 };
401 
402 
CertificateViewerCertPathTP(Window * _pParent,CertificateViewer * _pDlg)403 CertificateViewerCertPathTP::CertificateViewerCertPathTP( Window* _pParent, CertificateViewer* _pDlg )
404 	:CertificateViewerTP	( _pParent, XMLSEC_RES( RID_XMLSECTP_CERTPATH ), _pDlg  )
405 	,maCertPathFT			( this, XMLSEC_RES( FT_CERTPATH ) )
406 	,maCertPathLB			( this, XMLSEC_RES( LB_SIGNATURES ) )
407     ,maViewCertPB           ( this, XMLSEC_RES( BTN_VIEWCERT ) )
408 	,maCertStatusFT			( this, XMLSEC_RES( FT_CERTSTATUS ) )
409 	,maCertStatusML			( this, XMLSEC_RES( ML_CERTSTATUS ) )
410     ,mpParent               ( _pDlg )
411     ,mbFirstActivateDone    ( false )
412     ,maCertImage            ( XMLSEC_RES( IMG_CERT_SMALL ) )
413     ,maCertNotValidatedImage( XMLSEC_RES( IMG_CERT_NOTVALIDATED_SMALL ) )
414     ,msCertOK               ( XMLSEC_RES( STR_PATH_CERT_OK ) )
415     ,msCertNotValidated     ( XMLSEC_RES( STR_PATH_CERT_NOT_VALIDATED ) )
416 
417 {
418     if ( GetSettings().GetStyleSettings().GetHighContrastMode() )
419     {
420         maCertImage = Image( XMLSEC_RES( IMG_CERT_SMALL_HC ) );
421         maCertNotValidatedImage = Image( XMLSEC_RES( IMG_CERT_NOTVALIDATED_SMALL_HC ) );
422     }
423 
424 	FreeResource();
425 
426     maCertPathLB.SetNodeDefaultImages();
427     maCertPathLB.SetSublistOpenWithLeftRight();
428     maCertPathLB.SetSelectHdl( LINK( this, CertificateViewerCertPathTP, CertSelectHdl ) );
429     maViewCertPB.SetClickHdl( LINK( this, CertificateViewerCertPathTP, ViewCertHdl ) );
430 
431     // check if buttontext is to wide
432     const long nOffset = 10;
433     String sText = maViewCertPB.GetText();
434     long nTxtW = maViewCertPB.GetTextWidth( sText );
435     if ( sText.Search( '~' ) == STRING_NOTFOUND )
436         nTxtW += nOffset;
437     long nBtnW = maViewCertPB.GetSizePixel().Width();
438     if ( nTxtW > nBtnW )
439     {
440         // broaden the button
441         long nDelta = nTxtW - nBtnW;
442         Size aNewSize = maViewCertPB.GetSizePixel();
443         aNewSize.Width() += nDelta;
444         maViewCertPB.SetSizePixel( aNewSize );
445         // and give it a new position
446         Point aNewPos = maViewCertPB.GetPosPixel();
447         aNewPos.X() -= nDelta;
448         maViewCertPB.SetPosPixel( aNewPos );
449     }
450 }
451 
~CertificateViewerCertPathTP()452 CertificateViewerCertPathTP::~CertificateViewerCertPathTP()
453 {
454 	Clear();
455 }
456 
ActivatePage()457 void CertificateViewerCertPathTP::ActivatePage()
458 {
459     if ( !mbFirstActivateDone )
460     {
461         mbFirstActivateDone = true;
462         Sequence< Reference< security::XCertificate > > aCertPath =
463             mpParent->mxSecurityEnvironment->buildCertificatePath( mpParent->mxCert );
464         const Reference< security::XCertificate >* pCertPath = aCertPath.getConstArray();
465 
466         String aState;
467         sal_Int32 i, nCnt = aCertPath.getLength();
468         SvLBoxEntry* pParent = NULL;
469         for( i = nCnt; i; )
470         {
471             const Reference< security::XCertificate > rCert = pCertPath[ --i ];
472             String sName = XmlSec::GetContentPart( rCert->getSubjectName() );
473 			//Verify the certificate
474 			sal_Int32 certStatus = mpDlg->mxSecurityEnvironment->verifyCertificate(rCert,
475                  Sequence<Reference<css::security::XCertificate> >());
476 			bool bCertValid = certStatus == css::security::CertificateValidity::VALID ? true : false;
477             pParent = InsertCert( pParent, sName, rCert, bCertValid);
478         }
479 
480         maCertPathLB.Select( pParent );
481         maViewCertPB.Disable(); // Own certificate selected
482 
483         while( pParent )
484         {
485             maCertPathLB.Expand( pParent );
486             pParent = maCertPathLB.GetParent( pParent );
487         }
488 
489         CertSelectHdl( NULL );
490     }
491 }
492 
IMPL_LINK(CertificateViewerCertPathTP,ViewCertHdl,void *,EMPTYARG)493 IMPL_LINK( CertificateViewerCertPathTP, ViewCertHdl, void*, EMPTYARG )
494 {
495 	SvLBoxEntry* pEntry = maCertPathLB.FirstSelected();
496 	if( pEntry )
497     {
498 		CertificateViewer aViewer( this, mpDlg->mxSecurityEnvironment, ((CertPath_UserData*)pEntry->GetUserData())->mxCert, sal_False );
499 		aViewer.Execute();
500     }
501 
502     return 0;
503 }
504 
IMPL_LINK(CertificateViewerCertPathTP,CertSelectHdl,void *,EMPTYARG)505 IMPL_LINK( CertificateViewerCertPathTP, CertSelectHdl, void*, EMPTYARG )
506 {
507     String sStatus;
508 	SvLBoxEntry* pEntry = maCertPathLB.FirstSelected();
509     if( pEntry )
510     {
511         CertPath_UserData* pData = (CertPath_UserData*) pEntry->GetUserData();
512         if ( pData )
513             sStatus = pData->mbValid ? msCertOK : msCertNotValidated;
514     }
515 
516     maCertStatusML.SetText( sStatus );
517     maViewCertPB.Enable( pEntry && ( pEntry != maCertPathLB.Last() ) );
518     return 0;
519 }
520 
Clear(void)521 void CertificateViewerCertPathTP::Clear( void )
522 {
523 	maCertStatusML.SetText( String() );
524 	sal_uLong			i = 0;
525 	SvLBoxEntry*	pEntry = maCertPathLB.GetEntry( i );
526 	while( pEntry )
527 	{
528 		delete ( CertPath_UserData* ) pEntry->GetUserData();
529 		++i;
530 		pEntry = maCertPathLB.GetEntry( i );
531 	}
532 
533 	maCertPathLB.Clear();
534 }
535 
InsertCert(SvLBoxEntry * _pParent,const String & _rName,cssu::Reference<dcss::security::XCertificate> rxCert,bool bValid)536 SvLBoxEntry* CertificateViewerCertPathTP::InsertCert(
537 	SvLBoxEntry* _pParent, const String& _rName, cssu::Reference< dcss::security::XCertificate > rxCert,
538 	bool bValid)
539 {
540     Image aImage = bValid ? maCertImage : maCertNotValidatedImage;
541     SvLBoxEntry* pEntry = maCertPathLB.InsertEntry( _rName, aImage, aImage, _pParent );
542 	pEntry->SetUserData( ( void* ) new CertPath_UserData( rxCert, bValid ) );
543 
544 	return pEntry;
545 }
546 
547