Update ooo320-m1
[ooovba.git] / xmlsecurity / source / dialogs / certificatechooser.cxx
blob131f3de372cc477edb3a719bf65ff80fb886571b
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: certificatechooser.cxx,v $
10 * $Revision: 1.13 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_xmlsecurity.hxx"
34 #include <xmlsecurity/certificatechooser.hxx>
35 #include <xmlsecurity/certificateviewer.hxx>
36 #include <xmlsecurity/biginteger.hxx>
37 #include <com/sun/star/xml/crypto/XSecurityEnvironment.hpp>
38 #include <comphelper/sequence.hxx>
39 #include <comphelper/processfactory.hxx>
41 #include <com/sun/star/security/NoPasswordException.hpp>
42 #include <com/sun/star/security/CertificateCharacters.hpp>
43 #include <com/sun/star/security/SerialNumberAdapter.hpp>
45 #include <dialogs.hrc>
46 #include <resourcemanager.hxx>
47 #include <vcl/msgbox.hxx>
49 /* HACK: disable some warnings for MS-C */
50 #ifdef _MSC_VER
51 #pragma warning (disable : 4355) // 4355: this used in initializer-list
52 #endif
54 using namespace ::com::sun::star;
56 #define INVAL_SEL 0xFFFF
58 USHORT CertificateChooser::GetSelectedEntryPos( void ) const
60 USHORT nSel = INVAL_SEL;
62 SvLBoxEntry* pSel = maCertLB.FirstSelected();
63 if( pSel )
64 nSel = (USHORT) ( sal_uIntPtr ) pSel->GetUserData();
66 return (USHORT) nSel;
69 CertificateChooser::CertificateChooser( Window* _pParent, uno::Reference< uno::XComponentContext>& _rxCtx, uno::Reference< dcss::xml::crypto::XSecurityEnvironment >& _rxSecurityEnvironment, const SignatureInformations& _rCertsToIgnore )
70 :ModalDialog ( _pParent, XMLSEC_RES( RID_XMLSECDLG_CERTCHOOSER ) )
71 ,maCertsToIgnore( _rCertsToIgnore )
72 ,maHintFT ( this, XMLSEC_RES( FT_HINT_SELECT ) )
73 ,maCertLB ( this, XMLSEC_RES( LB_SIGNATURES ) )
74 ,maViewBtn ( this, XMLSEC_RES( BTN_VIEWCERT ) )
75 ,maBottomSepFL ( this, XMLSEC_RES( FL_BOTTOM_SEP ) )
76 ,maOKBtn ( this, XMLSEC_RES( BTN_OK ) )
77 ,maCancelBtn ( this, XMLSEC_RES( BTN_CANCEL ) )
78 ,maHelpBtn ( this, XMLSEC_RES( BTN_HELP ) )
80 static long nTabs[] = { 3, 0, 30*CS_LB_WIDTH/100, 60*CS_LB_WIDTH/100 };
81 maCertLB.SetTabs( &nTabs[0] );
82 maCertLB.InsertHeaderEntry( String( XMLSEC_RES( STR_HEADERBAR ) ) );
83 maCertLB.SetSelectHdl( LINK( this, CertificateChooser, CertificateHighlightHdl ) );
84 maCertLB.SetDoubleClickHdl( LINK( this, CertificateChooser, CertificateSelectHdl ) );
85 maViewBtn.SetClickHdl( LINK( this, CertificateChooser, ViewButtonHdl ) );
87 FreeResource();
89 mxCtx = _rxCtx;
90 mxSecurityEnvironment = _rxSecurityEnvironment;
91 mbInitialized = FALSE;
93 // disable buttons
94 CertificateHighlightHdl( NULL );
97 CertificateChooser::~CertificateChooser()
101 short CertificateChooser::Execute()
103 // #i48432#
104 // We can't check for personal certificates before raising this dialog,
105 // because the mozilla implementation throws a NoPassword exception,
106 // if the user pressed cancel, and also if the database does not exist!
107 // But in the later case, the is no password query, and the user is confused
108 // that nothing happens when pressing "Add..." in the SignatureDialog.
110 // PostUserEvent( LINK( this, CertificateChooser, Initialize ) );
112 // PostUserLink behavior is to slow, so do it directly before Execute().
113 // Problem: This Dialog should be visible right now, and the parent should not be accessible.
114 // Show, Update, DIsableInput...
116 Window* pMe = this;
117 Window* pParent = GetParent();
118 if ( pParent )
119 pParent->EnableInput( FALSE );
120 pMe->Show();
121 pMe->Update();
122 ImplInitialize();
123 if ( pParent )
124 pParent->EnableInput( TRUE );
125 return ModalDialog::Execute();
128 // IMPL_LINK( CertificateChooser, Initialize, void*, EMPTYARG )
129 void CertificateChooser::ImplInitialize()
131 if ( !mbInitialized )
135 maCerts = mxSecurityEnvironment->getPersonalCertificates();
137 catch (security::NoPasswordException&)
141 uno::Reference< dcss::security::XSerialNumberAdapter> xSerialNumberAdapter =
142 ::com::sun::star::security::SerialNumberAdapter::create(mxCtx);
144 sal_Int32 nCertificates = maCerts.getLength();
145 sal_Int32 nCertificatesToIgnore = maCertsToIgnore.size();
146 for( sal_Int32 nCert = nCertificates; nCert; )
148 uno::Reference< security::XCertificate > xCert = maCerts[ --nCert ];
149 sal_Bool bIgnoreThis = false;
151 // Do we already use that?
152 if( nCertificatesToIgnore )
154 rtl::OUString aIssuerName = xCert->getIssuerName();
155 for( sal_Int32 nSig = 0; nSig < nCertificatesToIgnore; ++nSig )
157 const SignatureInformation& rInf = maCertsToIgnore[ nSig ];
158 if ( ( aIssuerName == rInf.ouX509IssuerName ) &&
159 ( xSerialNumberAdapter->toString( xCert->getSerialNumber() ) == rInf.ouX509SerialNumber ) )
161 bIgnoreThis = true;
162 break;
167 if ( !bIgnoreThis )
169 // Check if we have a private key for this...
170 long nCertificateCharacters = mxSecurityEnvironment->getCertificateCharacters( xCert );
172 if ( !( nCertificateCharacters & security::CertificateCharacters::HAS_PRIVATE_KEY ) )
173 bIgnoreThis = true;
177 if ( bIgnoreThis )
179 ::comphelper::removeElementAt( maCerts, nCert );
180 nCertificates = maCerts.getLength();
184 // fill list of certificates; the first entry will be selected
185 for ( sal_Int32 nC = 0; nC < nCertificates; ++nC )
187 String sEntry( XmlSec::GetContentPart( maCerts[ nC ]->getSubjectName() ) );
188 sEntry += '\t';
189 sEntry += XmlSec::GetContentPart( maCerts[ nC ]->getIssuerName() );
190 sEntry += '\t';
191 sEntry += XmlSec::GetDateString( maCerts[ nC ]->getNotValidAfter() );
192 SvLBoxEntry* pEntry = maCertLB.InsertEntry( sEntry );
193 pEntry->SetUserData( ( void* )nC ); // missuse user data as index
196 // enable/disable buttons
197 CertificateHighlightHdl( NULL );
198 mbInitialized = TRUE;
203 uno::Reference< dcss::security::XCertificate > CertificateChooser::GetSelectedCertificate()
205 uno::Reference< dcss::security::XCertificate > xCert;
206 USHORT nSelected = GetSelectedEntryPos();
207 if ( nSelected < maCerts.getLength() )
208 xCert = maCerts[ nSelected ];
209 return xCert;
212 IMPL_LINK( CertificateChooser, CertificateHighlightHdl, void*, EMPTYARG )
214 sal_Bool bEnable = GetSelectedCertificate().is();
215 maViewBtn.Enable( bEnable );
216 maOKBtn.Enable( bEnable );
217 return 0;
220 IMPL_LINK( CertificateChooser, CertificateSelectHdl, void*, EMPTYARG )
222 EndDialog( RET_OK );
223 return 0;
226 IMPL_LINK( CertificateChooser, ViewButtonHdl, Button*, EMPTYARG )
228 ImplShowCertificateDetails();
229 return 0;
232 void CertificateChooser::ImplShowCertificateDetails()
234 uno::Reference< dcss::security::XCertificate > xCert = GetSelectedCertificate();
235 if( xCert.is() )
237 CertificateViewer aViewer( this, mxSecurityEnvironment, xCert, TRUE );
238 aViewer.Execute();