Bump version to 5.0-14
[LibreOffice.git] / xmlsecurity / source / dialogs / certificatechooser.cxx
blob9c5e28e94b674657cfcbbc97a8150f0bbfc0932d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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>
28 #include <com/sun/star/security/NoPasswordException.hpp>
29 #include <com/sun/star/security/CertificateCharacters.hpp>
30 #include <com/sun/star/security/SerialNumberAdapter.hpp>
32 #include "resourcemanager.hxx"
33 #include <vcl/msgbox.hxx>
34 #include <svtools/treelistentry.hxx>
36 using namespace ::com::sun::star;
38 #define INVAL_SEL 0xFFFF
40 sal_uInt16 CertificateChooser::GetSelectedEntryPos() const
42 sal_uInt16 nSel = INVAL_SEL;
44 SvTreeListEntry* pSel = m_pCertLB->FirstSelected();
45 if( pSel )
46 nSel = (sal_uInt16) reinterpret_cast<sal_uIntPtr>( pSel->GetUserData() );
48 return (sal_uInt16) nSel;
51 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 , maCertsToIgnore( _rCertsToIgnore )
55 get(m_pOKBtn, "ok");
56 get(m_pViewBtn, "viewcert");
58 Size aControlSize(275, 122);
59 const long nControlWidth = aControlSize.Width();
60 aControlSize = LogicToPixel(aControlSize, MAP_APPFONT);
61 SvSimpleTableContainer *pSignatures = get<SvSimpleTableContainer>("signatures");
62 pSignatures->set_width_request(aControlSize.Width());
63 pSignatures->set_height_request(aControlSize.Height());
65 m_pCertLB = VclPtr<SvSimpleTable>::Create(*pSignatures);
66 static long nTabs[] = { 3, 0, 30*nControlWidth/100, 60*nControlWidth/100 };
67 m_pCertLB->SetTabs( &nTabs[0] );
68 m_pCertLB->InsertHeaderEntry(get<FixedText>("issuedto")->GetText() + "\t" + get<FixedText>("issuedby")->GetText()
69 + "\t" + get<FixedText>("expiration")->GetText());
70 m_pCertLB->SetSelectHdl( LINK( this, CertificateChooser, CertificateHighlightHdl ) );
71 m_pCertLB->SetDoubleClickHdl( LINK( this, CertificateChooser, CertificateSelectHdl ) );
72 m_pViewBtn->SetClickHdl( LINK( this, CertificateChooser, ViewButtonHdl ) );
74 mxCtx = _rxCtx;
75 mxSecurityEnvironment = _rxSecurityEnvironment;
76 mbInitialized = false;
78 // disable buttons
79 CertificateHighlightHdl( NULL );
82 CertificateChooser::~CertificateChooser()
84 disposeOnce();
87 void CertificateChooser::dispose()
89 m_pCertLB.disposeAndClear();
90 m_pViewBtn.clear();
91 m_pOKBtn.clear();
92 ModalDialog::dispose();
95 short CertificateChooser::Execute()
97 // #i48432#
98 // We can't check for personal certificates before raising this dialog,
99 // because the mozilla implementation throws a NoPassword exception,
100 // if the user pressed cancel, and also if the database does not exist!
101 // But in the later case, the is no password query, and the user is confused
102 // that nothing happens when pressing "Add..." in the SignatureDialog.
104 // PostUserEvent( LINK( this, CertificateChooser, Initialize ) );
106 // PostUserLink behavior is to slow, so do it directly before Execute().
107 // Problem: This Dialog should be visible right now, and the parent should not be accessible.
108 // Show, Update, DIsableInput...
110 vcl::Window* pMe = this;
111 vcl::Window* pParent = GetParent();
112 if ( pParent )
113 pParent->EnableInput( false );
114 pMe->Show();
115 pMe->Update();
116 ImplInitialize();
117 if ( pParent )
118 pParent->EnableInput( true );
119 return ModalDialog::Execute();
122 // IMPL_LINK_NOARG(CertificateChooser, Initialize)
123 void CertificateChooser::ImplInitialize()
125 if ( !mbInitialized )
129 maCerts = mxSecurityEnvironment->getPersonalCertificates();
131 catch (security::NoPasswordException&)
135 uno::Reference< css::security::XSerialNumberAdapter> xSerialNumberAdapter =
136 ::com::sun::star::security::SerialNumberAdapter::create(mxCtx);
138 sal_Int32 nCertificates = maCerts.getLength();
139 sal_Int32 nCertificatesToIgnore = maCertsToIgnore.size();
140 for( sal_Int32 nCert = nCertificates; nCert; )
142 uno::Reference< security::XCertificate > xCert = maCerts[ --nCert ];
143 bool bIgnoreThis = false;
145 // Do we already use that?
146 if( nCertificatesToIgnore )
148 OUString aIssuerName = xCert->getIssuerName();
149 for( sal_Int32 nSig = 0; nSig < nCertificatesToIgnore; ++nSig )
151 const SignatureInformation& rInf = maCertsToIgnore[ nSig ];
152 if ( ( aIssuerName == rInf.ouX509IssuerName ) &&
153 ( xSerialNumberAdapter->toString( xCert->getSerialNumber() ) == rInf.ouX509SerialNumber ) )
155 bIgnoreThis = true;
156 break;
161 if ( !bIgnoreThis )
163 // Check if we have a private key for this...
164 long nCertificateCharacters = mxSecurityEnvironment->getCertificateCharacters( xCert );
166 if ( !( nCertificateCharacters & security::CertificateCharacters::HAS_PRIVATE_KEY ) )
167 bIgnoreThis = true;
171 if ( bIgnoreThis )
173 ::comphelper::removeElementAt( maCerts, nCert );
174 nCertificates = maCerts.getLength();
178 // fill list of certificates; the first entry will be selected
179 for ( sal_Int32 nC = 0; nC < nCertificates; ++nC )
181 SvTreeListEntry* pEntry = m_pCertLB->InsertEntry( XmlSec::GetContentPart( maCerts[ nC ]->getSubjectName() )
182 + "\t" + XmlSec::GetContentPart( maCerts[ nC ]->getIssuerName() )
183 + "\t" + XmlSec::GetDateString( maCerts[ nC ]->getNotValidAfter() ) );
184 pEntry->SetUserData( reinterpret_cast<void*>(nC) ); // missuse user data as index
187 // enable/disable buttons
188 CertificateHighlightHdl( NULL );
189 mbInitialized = true;
194 uno::Reference< css::security::XCertificate > CertificateChooser::GetSelectedCertificate()
196 uno::Reference< css::security::XCertificate > xCert;
197 sal_uInt16 nSelected = GetSelectedEntryPos();
198 if ( nSelected < maCerts.getLength() )
199 xCert = maCerts[ nSelected ];
200 return xCert;
203 IMPL_LINK_NOARG(CertificateChooser, CertificateHighlightHdl)
205 bool bEnable = GetSelectedCertificate().is();
206 m_pViewBtn->Enable( bEnable );
207 m_pOKBtn->Enable( bEnable );
208 return 0;
211 IMPL_LINK_NOARG(CertificateChooser, CertificateSelectHdl)
213 EndDialog( RET_OK );
214 return 0;
217 IMPL_LINK_NOARG(CertificateChooser, ViewButtonHdl)
219 ImplShowCertificateDetails();
220 return 0;
223 void CertificateChooser::ImplShowCertificateDetails()
225 uno::Reference< css::security::XCertificate > xCert = GetSelectedCertificate();
226 if( xCert.is() )
228 ScopedVclPtrInstance< CertificateViewer > aViewer( this, mxSecurityEnvironment, xCert, true );
229 aViewer->Execute();
233 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */