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