update credits
[LibreOffice.git] / xmlsecurity / source / dialogs / certificatechooser.cxx
blob28613293f4b2f7151b1276abc1584474533db5c8
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( void ) const
42 sal_uInt16 nSel = INVAL_SEL;
44 SvTreeListEntry* pSel = m_pCertLB->FirstSelected();
45 if( pSel )
46 nSel = (sal_uInt16) ( sal_uIntPtr ) pSel->GetUserData();
48 return (sal_uInt16) nSel;
51 CertificateChooser::CertificateChooser( 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 SvxSimpleTableContainer *pSignatures = get<SvxSimpleTableContainer>("signatures");
62 pSignatures->set_width_request(aControlSize.Width());
63 pSignatures->set_height_request(aControlSize.Height());
65 m_pCertLB = new SvxSimpleTable(*pSignatures);
66 static long nTabs[] = { 3, 0, 30*nControlWidth/100, 60*nControlWidth/100 };
67 m_pCertLB->SetTabs( &nTabs[0] );
68 OUStringBuffer sHeader;
69 sHeader.append(get<FixedText>("issuedto")->GetText())
70 .append("\t").append(get<FixedText>("issuedby")->GetText())
71 .append("\t").append(get<FixedText>("expiration")->GetText());
72 m_pCertLB->InsertHeaderEntry(sHeader.makeStringAndClear());
74 m_pCertLB->SetSelectHdl( LINK( this, CertificateChooser, CertificateHighlightHdl ) );
75 m_pCertLB->SetDoubleClickHdl( LINK( this, CertificateChooser, CertificateSelectHdl ) );
76 m_pViewBtn->SetClickHdl( LINK( this, CertificateChooser, ViewButtonHdl ) );
78 mxCtx = _rxCtx;
79 mxSecurityEnvironment = _rxSecurityEnvironment;
80 mbInitialized = false;
82 // disable buttons
83 CertificateHighlightHdl( NULL );
86 CertificateChooser::~CertificateChooser()
88 delete m_pCertLB;
91 short CertificateChooser::Execute()
93 // #i48432#
94 // We can't check for personal certificates before raising this dialog,
95 // because the mozilla implementation throws a NoPassword exception,
96 // if the user pressed cancel, and also if the database does not exist!
97 // But in the later case, the is no password query, and the user is confused
98 // that nothing happens when pressing "Add..." in the SignatureDialog.
100 // PostUserEvent( LINK( this, CertificateChooser, Initialize ) );
102 // PostUserLink behavior is to slow, so do it directly before Execute().
103 // Problem: This Dialog should be visible right now, and the parent should not be accessible.
104 // Show, Update, DIsableInput...
106 Window* pMe = this;
107 Window* pParent = GetParent();
108 if ( pParent )
109 pParent->EnableInput( sal_False );
110 pMe->Show();
111 pMe->Update();
112 ImplInitialize();
113 if ( pParent )
114 pParent->EnableInput( sal_True );
115 return ModalDialog::Execute();
118 // IMPL_LINK_NOARG(CertificateChooser, Initialize)
119 void CertificateChooser::ImplInitialize()
121 if ( !mbInitialized )
125 maCerts = mxSecurityEnvironment->getPersonalCertificates();
127 catch (security::NoPasswordException&)
131 uno::Reference< css::security::XSerialNumberAdapter> xSerialNumberAdapter =
132 ::com::sun::star::security::SerialNumberAdapter::create(mxCtx);
134 sal_Int32 nCertificates = maCerts.getLength();
135 sal_Int32 nCertificatesToIgnore = maCertsToIgnore.size();
136 for( sal_Int32 nCert = nCertificates; nCert; )
138 uno::Reference< security::XCertificate > xCert = maCerts[ --nCert ];
139 sal_Bool bIgnoreThis = false;
141 // Do we already use that?
142 if( nCertificatesToIgnore )
144 OUString aIssuerName = xCert->getIssuerName();
145 for( sal_Int32 nSig = 0; nSig < nCertificatesToIgnore; ++nSig )
147 const SignatureInformation& rInf = maCertsToIgnore[ nSig ];
148 if ( ( aIssuerName == rInf.ouX509IssuerName ) &&
149 ( xSerialNumberAdapter->toString( xCert->getSerialNumber() ) == rInf.ouX509SerialNumber ) )
151 bIgnoreThis = true;
152 break;
157 if ( !bIgnoreThis )
159 // Check if we have a private key for this...
160 long nCertificateCharacters = mxSecurityEnvironment->getCertificateCharacters( xCert );
162 if ( !( nCertificateCharacters & security::CertificateCharacters::HAS_PRIVATE_KEY ) )
163 bIgnoreThis = true;
167 if ( bIgnoreThis )
169 ::comphelper::removeElementAt( maCerts, nCert );
170 nCertificates = maCerts.getLength();
174 // fill list of certificates; the first entry will be selected
175 for ( sal_Int32 nC = 0; nC < nCertificates; ++nC )
177 OUStringBuffer sEntry( XmlSec::GetContentPart( maCerts[ nC ]->getSubjectName() ) );
178 sEntry.append( '\t' );
179 sEntry.append( XmlSec::GetContentPart( maCerts[ nC ]->getIssuerName() ) );
180 sEntry.append( '\t' );
181 sEntry.append( XmlSec::GetDateString( maCerts[ nC ]->getNotValidAfter() ) );
182 SvTreeListEntry* pEntry = m_pCertLB->InsertEntry( sEntry.makeStringAndClear() );
183 pEntry->SetUserData( ( void* )(sal_IntPtr)nC ); // missuse user data as index
186 // enable/disable buttons
187 CertificateHighlightHdl( NULL );
188 mbInitialized = true;
193 uno::Reference< css::security::XCertificate > CertificateChooser::GetSelectedCertificate()
195 uno::Reference< css::security::XCertificate > xCert;
196 sal_uInt16 nSelected = GetSelectedEntryPos();
197 if ( nSelected < maCerts.getLength() )
198 xCert = maCerts[ nSelected ];
199 return xCert;
202 IMPL_LINK_NOARG(CertificateChooser, CertificateHighlightHdl)
204 sal_Bool bEnable = GetSelectedCertificate().is();
205 m_pViewBtn->Enable( bEnable );
206 m_pOKBtn->Enable( bEnable );
207 return 0;
210 IMPL_LINK_NOARG(CertificateChooser, CertificateSelectHdl)
212 EndDialog( RET_OK );
213 return 0;
216 IMPL_LINK_NOARG(CertificateChooser, ViewButtonHdl)
218 ImplShowCertificateDetails();
219 return 0;
222 void CertificateChooser::ImplShowCertificateDetails()
224 uno::Reference< css::security::XCertificate > xCert = GetSelectedCertificate();
225 if( xCert.is() )
227 CertificateViewer aViewer( this, mxSecurityEnvironment, xCert, sal_True );
228 aViewer.Execute();
232 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */