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 <xmlsecurity/certificatechooser.hxx>
22 : #include <xmlsecurity/certificateviewer.hxx>
23 : #include <xmlsecurity/biginteger.hxx>
24 : #include <com/sun/star/xml/crypto/XSecurityEnvironment.hpp>
25 : #include <comphelper/sequence.hxx>
26 : #include <comphelper/processfactory.hxx>
27 :
28 : #include <com/sun/star/security/NoPasswordException.hpp>
29 : #include <com/sun/star/security/CertificateCharacters.hpp>
30 : #include <com/sun/star/security/SerialNumberAdapter.hpp>
31 :
32 : #include <dialogs.hrc>
33 : #include <resourcemanager.hxx>
34 : #include <vcl/msgbox.hxx>
35 : #include "svtools/treelistentry.hxx"
36 :
37 : /* HACK: disable some warnings for MS-C */
38 : #ifdef _MSC_VER
39 : #pragma warning (disable : 4355) // 4355: this used in initializer-list
40 : #endif
41 :
42 : using namespace ::com::sun::star;
43 :
44 : #define INVAL_SEL 0xFFFF
45 :
46 0 : sal_uInt16 CertificateChooser::GetSelectedEntryPos( void ) const
47 : {
48 0 : sal_uInt16 nSel = INVAL_SEL;
49 :
50 0 : SvTreeListEntry* pSel = maCertLB.FirstSelected();
51 0 : if( pSel )
52 0 : nSel = (sal_uInt16) ( sal_uIntPtr ) pSel->GetUserData();
53 :
54 0 : return (sal_uInt16) nSel;
55 : }
56 :
57 0 : CertificateChooser::CertificateChooser( Window* _pParent, uno::Reference< uno::XComponentContext>& _rxCtx, uno::Reference< dcss::xml::crypto::XSecurityEnvironment >& _rxSecurityEnvironment, const SignatureInformations& _rCertsToIgnore )
58 0 : :ModalDialog ( _pParent, XMLSEC_RES( RID_XMLSECDLG_CERTCHOOSER ) )
59 : ,maCertsToIgnore( _rCertsToIgnore )
60 0 : ,maHintFT ( this, XMLSEC_RES( FT_HINT_SELECT ) )
61 0 : ,m_aCertLBContainer(this, XMLSEC_RES(LB_SIGNATURES))
62 : ,maCertLB(m_aCertLBContainer)
63 0 : ,maViewBtn ( this, XMLSEC_RES( BTN_VIEWCERT ) )
64 0 : ,maBottomSepFL ( this, XMLSEC_RES( FL_BOTTOM_SEP ) )
65 0 : ,maOKBtn ( this, XMLSEC_RES( BTN_OK ) )
66 0 : ,maCancelBtn ( this, XMLSEC_RES( BTN_CANCEL ) )
67 0 : ,maHelpBtn ( this, XMLSEC_RES( BTN_HELP ) )
68 : {
69 : static long nTabs[] = { 3, 0, 30*CS_LB_WIDTH/100, 60*CS_LB_WIDTH/100 };
70 0 : maCertLB.SetTabs( &nTabs[0] );
71 0 : maCertLB.InsertHeaderEntry( XMLSEC_RES( STR_HEADERBAR ) );
72 0 : maCertLB.SetSelectHdl( LINK( this, CertificateChooser, CertificateHighlightHdl ) );
73 0 : maCertLB.SetDoubleClickHdl( LINK( this, CertificateChooser, CertificateSelectHdl ) );
74 0 : maViewBtn.SetClickHdl( LINK( this, CertificateChooser, ViewButtonHdl ) );
75 :
76 0 : FreeResource();
77 :
78 0 : mxCtx = _rxCtx;
79 0 : mxSecurityEnvironment = _rxSecurityEnvironment;
80 0 : mbInitialized = false;
81 :
82 : // disable buttons
83 0 : CertificateHighlightHdl( NULL );
84 0 : }
85 :
86 0 : CertificateChooser::~CertificateChooser()
87 : {
88 0 : }
89 :
90 0 : short CertificateChooser::Execute()
91 : {
92 : // #i48432#
93 : // We can't check for personal certificates before raising this dialog,
94 : // because the mozilla implementation throws a NoPassword exception,
95 : // if the user pressed cancel, and also if the database does not exist!
96 : // But in the later case, the is no password query, and the user is confused
97 : // that nothing happens when pressing "Add..." in the SignatureDialog.
98 :
99 : // PostUserEvent( LINK( this, CertificateChooser, Initialize ) );
100 :
101 : // PostUserLink behavior is to slow, so do it directly before Execute().
102 : // Problem: This Dialog should be visible right now, and the parent should not be accessible.
103 : // Show, Update, DIsableInput...
104 :
105 0 : Window* pMe = this;
106 0 : Window* pParent = GetParent();
107 0 : if ( pParent )
108 0 : pParent->EnableInput( sal_False );
109 0 : pMe->Show();
110 0 : pMe->Update();
111 0 : ImplInitialize();
112 0 : if ( pParent )
113 0 : pParent->EnableInput( sal_True );
114 0 : return ModalDialog::Execute();
115 : }
116 :
117 : // IMPL_LINK_NOARG(CertificateChooser, Initialize)
118 0 : void CertificateChooser::ImplInitialize()
119 : {
120 0 : if ( !mbInitialized )
121 : {
122 : try
123 : {
124 0 : maCerts = mxSecurityEnvironment->getPersonalCertificates();
125 : }
126 0 : catch (security::NoPasswordException&)
127 : {
128 : }
129 :
130 : uno::Reference< dcss::security::XSerialNumberAdapter> xSerialNumberAdapter =
131 0 : ::com::sun::star::security::SerialNumberAdapter::create(mxCtx);
132 :
133 0 : sal_Int32 nCertificates = maCerts.getLength();
134 0 : sal_Int32 nCertificatesToIgnore = maCertsToIgnore.size();
135 0 : for( sal_Int32 nCert = nCertificates; nCert; )
136 : {
137 0 : uno::Reference< security::XCertificate > xCert = maCerts[ --nCert ];
138 0 : sal_Bool bIgnoreThis = false;
139 :
140 : // Do we already use that?
141 0 : if( nCertificatesToIgnore )
142 : {
143 0 : rtl::OUString aIssuerName = xCert->getIssuerName();
144 0 : for( sal_Int32 nSig = 0; nSig < nCertificatesToIgnore; ++nSig )
145 : {
146 0 : const SignatureInformation& rInf = maCertsToIgnore[ nSig ];
147 0 : if ( ( aIssuerName == rInf.ouX509IssuerName ) &&
148 0 : ( xSerialNumberAdapter->toString( xCert->getSerialNumber() ) == rInf.ouX509SerialNumber ) )
149 : {
150 0 : bIgnoreThis = true;
151 0 : break;
152 : }
153 0 : }
154 : }
155 :
156 0 : if ( !bIgnoreThis )
157 : {
158 : // Check if we have a private key for this...
159 0 : long nCertificateCharacters = mxSecurityEnvironment->getCertificateCharacters( xCert );
160 :
161 0 : if ( !( nCertificateCharacters & security::CertificateCharacters::HAS_PRIVATE_KEY ) )
162 0 : bIgnoreThis = true;
163 :
164 : }
165 :
166 0 : if ( bIgnoreThis )
167 : {
168 0 : ::comphelper::removeElementAt( maCerts, nCert );
169 0 : nCertificates = maCerts.getLength();
170 : }
171 0 : }
172 :
173 : // fill list of certificates; the first entry will be selected
174 0 : for ( sal_Int32 nC = 0; nC < nCertificates; ++nC )
175 : {
176 0 : OUStringBuffer sEntry( XmlSec::GetContentPart( maCerts[ nC ]->getSubjectName() ) );
177 0 : sEntry.append( '\t' );
178 0 : sEntry.append( XmlSec::GetContentPart( maCerts[ nC ]->getIssuerName() ) );
179 0 : sEntry.append( '\t' );
180 0 : sEntry.append( XmlSec::GetDateString( maCerts[ nC ]->getNotValidAfter() ) );
181 0 : SvTreeListEntry* pEntry = maCertLB.InsertEntry( sEntry.makeStringAndClear() );
182 0 : pEntry->SetUserData( ( void* )(sal_IntPtr)nC ); // missuse user data as index
183 0 : }
184 :
185 : // enable/disable buttons
186 0 : CertificateHighlightHdl( NULL );
187 0 : mbInitialized = true;
188 : }
189 0 : }
190 :
191 :
192 0 : uno::Reference< dcss::security::XCertificate > CertificateChooser::GetSelectedCertificate()
193 : {
194 0 : uno::Reference< dcss::security::XCertificate > xCert;
195 0 : sal_uInt16 nSelected = GetSelectedEntryPos();
196 0 : if ( nSelected < maCerts.getLength() )
197 0 : xCert = maCerts[ nSelected ];
198 0 : return xCert;
199 : }
200 :
201 0 : IMPL_LINK_NOARG(CertificateChooser, CertificateHighlightHdl)
202 : {
203 0 : sal_Bool bEnable = GetSelectedCertificate().is();
204 0 : maViewBtn.Enable( bEnable );
205 0 : maOKBtn.Enable( bEnable );
206 0 : return 0;
207 : }
208 :
209 0 : IMPL_LINK_NOARG(CertificateChooser, CertificateSelectHdl)
210 : {
211 0 : EndDialog( RET_OK );
212 0 : return 0;
213 : }
214 :
215 0 : IMPL_LINK_NOARG(CertificateChooser, ViewButtonHdl)
216 : {
217 0 : ImplShowCertificateDetails();
218 0 : return 0;
219 : }
220 :
221 0 : void CertificateChooser::ImplShowCertificateDetails()
222 : {
223 0 : uno::Reference< dcss::security::XCertificate > xCert = GetSelectedCertificate();
224 0 : if( xCert.is() )
225 : {
226 0 : CertificateViewer aViewer( this, mxSecurityEnvironment, xCert, sal_True );
227 0 : aViewer.Execute();
228 0 : }
229 0 : }
230 :
231 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|