2010-10-12 15:57:08 +02:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-07-16 17:32:30 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*
|
|
|
|
* This file incorporates work covered by the following license notice:
|
|
|
|
*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed
|
|
|
|
* with this work for additional information regarding copyright
|
|
|
|
* ownership. The ASF licenses this file to you under the Apache
|
|
|
|
* License, Version 2.0 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
|
|
|
|
*/
|
2004-07-12 12:15:31 +00:00
|
|
|
|
2006-09-16 13:35:04 +00:00
|
|
|
|
2016-05-27 10:56:17 +03:00
|
|
|
#include <certificatechooser.hxx>
|
|
|
|
#include <certificateviewer.hxx>
|
|
|
|
#include <biginteger.hxx>
|
2004-07-12 12:15:31 +00:00
|
|
|
#include <com/sun/star/xml/crypto/XSecurityEnvironment.hpp>
|
|
|
|
#include <comphelper/sequence.hxx>
|
2008-08-18 12:13:05 +00:00
|
|
|
#include <comphelper/processfactory.hxx>
|
2004-07-12 12:15:31 +00:00
|
|
|
|
|
|
|
#include <com/sun/star/security/NoPasswordException.hpp>
|
2004-07-22 14:37:38 +00:00
|
|
|
#include <com/sun/star/security/CertificateCharacters.hpp>
|
2008-08-18 12:13:05 +00:00
|
|
|
#include <com/sun/star/security/SerialNumberAdapter.hpp>
|
2004-07-12 12:15:31 +00:00
|
|
|
|
2014-05-19 11:57:34 +02:00
|
|
|
#include "resourcemanager.hxx"
|
2005-03-10 17:04:16 +00:00
|
|
|
#include <vcl/msgbox.hxx>
|
2014-05-19 11:57:34 +02:00
|
|
|
#include <svtools/treelistentry.hxx>
|
2005-03-10 17:04:16 +00:00
|
|
|
|
2004-07-12 12:15:31 +00:00
|
|
|
using namespace ::com::sun::star;
|
|
|
|
|
|
|
|
#define INVAL_SEL 0xFFFF
|
|
|
|
|
2015-04-14 12:44:47 +02:00
|
|
|
sal_uInt16 CertificateChooser::GetSelectedEntryPos() const
|
2004-07-12 12:15:31 +00:00
|
|
|
{
|
2011-01-14 14:16:44 +01:00
|
|
|
sal_uInt16 nSel = INVAL_SEL;
|
2004-07-12 12:15:31 +00:00
|
|
|
|
2012-12-29 18:11:33 +00:00
|
|
|
SvTreeListEntry* pSel = m_pCertLB->FirstSelected();
|
2004-07-12 12:15:31 +00:00
|
|
|
if( pSel )
|
2014-10-02 15:36:13 +02:00
|
|
|
nSel = (sal_uInt16) reinterpret_cast<sal_uIntPtr>( pSel->GetUserData() );
|
2004-07-12 12:15:31 +00:00
|
|
|
|
2011-01-14 14:16:44 +01:00
|
|
|
return (sal_uInt16) nSel;
|
2004-07-12 12:15:31 +00:00
|
|
|
}
|
|
|
|
|
2016-01-06 09:21:22 +01:00
|
|
|
CertificateChooser::CertificateChooser(vcl::Window* _pParent, uno::Reference<uno::XComponentContext>& _rxCtx, uno::Reference<xml::crypto::XSecurityEnvironment>& _rxSecurityEnvironment)
|
2012-12-29 18:11:33 +00:00
|
|
|
: ModalDialog(_pParent, "SelectCertificateDialog", "xmlsec/ui/selectcertificatedialog.ui")
|
2004-07-12 12:15:31 +00:00
|
|
|
{
|
2012-12-29 18:11:33 +00:00
|
|
|
get(m_pOKBtn, "ok");
|
|
|
|
get(m_pViewBtn, "viewcert");
|
2016-01-06 09:07:46 +01:00
|
|
|
get(m_pDescriptionED, "description");
|
2012-12-29 18:11:33 +00:00
|
|
|
|
2016-05-19 20:27:55 +03:00
|
|
|
Size aControlSize(475, 122);
|
2012-12-29 18:11:33 +00:00
|
|
|
const long nControlWidth = aControlSize.Width();
|
2016-09-20 16:41:39 +02:00
|
|
|
aControlSize = LogicToPixel(aControlSize, MapUnit::MapAppFont);
|
2013-07-16 09:12:42 +01:00
|
|
|
SvSimpleTableContainer *pSignatures = get<SvSimpleTableContainer>("signatures");
|
2012-12-29 18:11:33 +00:00
|
|
|
pSignatures->set_width_request(aControlSize.Width());
|
|
|
|
pSignatures->set_height_request(aControlSize.Height());
|
|
|
|
|
2015-04-16 22:18:45 +01:00
|
|
|
m_pCertLB = VclPtr<SvSimpleTable>::Create(*pSignatures);
|
2016-05-19 20:27:55 +03:00
|
|
|
static long nTabs[] = { 4, 0, 20*nControlWidth/100, 40*nControlWidth/100, 80*nControlWidth/100 };
|
2012-12-29 18:11:33 +00:00
|
|
|
m_pCertLB->SetTabs( &nTabs[0] );
|
2013-08-17 01:11:11 +02:00
|
|
|
m_pCertLB->InsertHeaderEntry(get<FixedText>("issuedto")->GetText() + "\t" + get<FixedText>("issuedby")->GetText()
|
2016-05-19 20:27:55 +03:00
|
|
|
+ "\t" + get<FixedText>("usage")->GetText() + "\t" + get<FixedText>("expiration")->GetText());
|
2012-12-29 18:11:33 +00:00
|
|
|
m_pCertLB->SetSelectHdl( LINK( this, CertificateChooser, CertificateHighlightHdl ) );
|
|
|
|
m_pCertLB->SetDoubleClickHdl( LINK( this, CertificateChooser, CertificateSelectHdl ) );
|
|
|
|
m_pViewBtn->SetClickHdl( LINK( this, CertificateChooser, ViewButtonHdl ) );
|
2004-07-12 12:15:31 +00:00
|
|
|
|
2008-08-18 12:13:05 +00:00
|
|
|
mxCtx = _rxCtx;
|
2004-07-12 12:15:31 +00:00
|
|
|
mxSecurityEnvironment = _rxSecurityEnvironment;
|
2012-09-25 17:28:22 +02:00
|
|
|
mbInitialized = false;
|
2004-07-12 12:15:31 +00:00
|
|
|
|
2005-05-18 08:57:17 +00:00
|
|
|
// disable buttons
|
2015-11-10 10:29:43 +01:00
|
|
|
CertificateHighlightHdl( nullptr );
|
2005-05-18 08:57:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CertificateChooser::~CertificateChooser()
|
|
|
|
{
|
2015-03-10 09:07:06 +02:00
|
|
|
disposeOnce();
|
2015-01-26 13:25:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CertificateChooser::dispose()
|
2005-05-18 08:57:17 +00:00
|
|
|
{
|
2015-04-10 10:44:13 +01:00
|
|
|
m_pCertLB.disposeAndClear();
|
2015-03-09 14:29:30 +02:00
|
|
|
m_pViewBtn.clear();
|
|
|
|
m_pOKBtn.clear();
|
2016-06-30 14:09:31 +02:00
|
|
|
m_pDescriptionED.clear();
|
2015-01-26 13:25:18 +02:00
|
|
|
ModalDialog::dispose();
|
2005-05-18 08:57:17 +00:00
|
|
|
}
|
2004-07-12 12:15:31 +00:00
|
|
|
|
2005-05-18 08:57:17 +00:00
|
|
|
short CertificateChooser::Execute()
|
|
|
|
{
|
|
|
|
// #i48432#
|
|
|
|
// We can't check for personal certificates before raising this dialog,
|
|
|
|
// because the mozilla implementation throws a NoPassword exception,
|
|
|
|
// if the user pressed cancel, and also if the database does not exist!
|
|
|
|
// But in the later case, the is no password query, and the user is confused
|
|
|
|
// that nothing happens when pressing "Add..." in the SignatureDialog.
|
|
|
|
|
|
|
|
// PostUserEvent( LINK( this, CertificateChooser, Initialize ) );
|
|
|
|
|
|
|
|
// PostUserLink behavior is to slow, so do it directly before Execute().
|
|
|
|
// Problem: This Dialog should be visible right now, and the parent should not be accessible.
|
|
|
|
// Show, Update, DIsableInput...
|
|
|
|
|
2014-09-23 11:20:40 +02:00
|
|
|
vcl::Window* pMe = this;
|
|
|
|
vcl::Window* pParent = GetParent();
|
2005-05-18 08:57:17 +00:00
|
|
|
if ( pParent )
|
2014-01-28 19:58:53 +01:00
|
|
|
pParent->EnableInput( false );
|
2005-05-18 08:57:17 +00:00
|
|
|
pMe->Show();
|
|
|
|
pMe->Update();
|
|
|
|
ImplInitialize();
|
|
|
|
if ( pParent )
|
2015-10-19 17:52:21 +02:00
|
|
|
pParent->EnableInput();
|
2005-05-18 08:57:17 +00:00
|
|
|
return ModalDialog::Execute();
|
|
|
|
}
|
|
|
|
|
2016-05-19 20:27:55 +03:00
|
|
|
void CertificateChooser::HandleOneUsageBit(OUString& string, int& bits, int bit, const char *name)
|
|
|
|
{
|
|
|
|
if (bits & bit)
|
|
|
|
{
|
|
|
|
if (!string.isEmpty())
|
|
|
|
string += ", ";
|
|
|
|
string += get<FixedText>(OString("STR_") + name)->GetText();
|
|
|
|
bits &= ~bit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
OUString CertificateChooser::UsageInClearText(int bits)
|
|
|
|
{
|
|
|
|
OUString result;
|
|
|
|
|
|
|
|
HandleOneUsageBit(result, bits, 0x80, "DIGITAL_SIGNATURE");
|
|
|
|
HandleOneUsageBit(result, bits, 0x40, "NON_REPUDIATION");
|
|
|
|
HandleOneUsageBit(result, bits, 0x20, "KEY_ENCIPHERMENT");
|
|
|
|
HandleOneUsageBit(result, bits, 0x10, "DATA_ENCIPHERMENT");
|
|
|
|
HandleOneUsageBit(result, bits, 0x08, "KEY_AGREEMENT");
|
|
|
|
HandleOneUsageBit(result, bits, 0x04, "KEY_CERT_SIGN");
|
|
|
|
HandleOneUsageBit(result, bits, 0x02, "CRL_SIGN");
|
|
|
|
HandleOneUsageBit(result, bits, 0x01, "ENCIPHER_ONLY");
|
|
|
|
|
|
|
|
// Check for mystery leftover bits
|
|
|
|
if (bits != 0)
|
|
|
|
{
|
|
|
|
if (!result.isEmpty())
|
|
|
|
result += ", ";
|
2016-05-19 21:55:23 +03:00
|
|
|
result += "0x" + OUString::number(bits, 16);
|
2016-05-19 20:27:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2005-05-18 08:57:17 +00:00
|
|
|
void CertificateChooser::ImplInitialize()
|
|
|
|
{
|
|
|
|
if ( !mbInitialized )
|
2004-07-12 12:15:31 +00:00
|
|
|
{
|
2005-05-18 08:57:17 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
maCerts = mxSecurityEnvironment->getPersonalCertificates();
|
|
|
|
}
|
|
|
|
catch (security::NoPasswordException&)
|
|
|
|
{
|
|
|
|
}
|
2004-07-22 14:37:38 +00:00
|
|
|
|
2013-03-30 00:32:52 +01:00
|
|
|
uno::Reference< css::security::XSerialNumberAdapter> xSerialNumberAdapter =
|
2015-11-26 09:49:00 +02:00
|
|
|
css::security::SerialNumberAdapter::create(mxCtx);
|
2008-08-18 12:13:05 +00:00
|
|
|
|
2005-05-18 08:57:17 +00:00
|
|
|
sal_Int32 nCertificates = maCerts.getLength();
|
|
|
|
for( sal_Int32 nCert = nCertificates; nCert; )
|
2004-07-12 12:15:31 +00:00
|
|
|
{
|
2005-05-18 08:57:17 +00:00
|
|
|
uno::Reference< security::XCertificate > xCert = maCerts[ --nCert ];
|
2016-01-06 09:21:22 +01:00
|
|
|
// Check if we have a private key for this...
|
|
|
|
long nCertificateCharacters = mxSecurityEnvironment->getCertificateCharacters(xCert);
|
2005-05-18 08:57:17 +00:00
|
|
|
|
2016-01-06 09:21:22 +01:00
|
|
|
if (!(nCertificateCharacters & security::CertificateCharacters::HAS_PRIVATE_KEY))
|
2005-05-18 08:57:17 +00:00
|
|
|
{
|
|
|
|
::comphelper::removeElementAt( maCerts, nCert );
|
|
|
|
nCertificates = maCerts.getLength();
|
|
|
|
}
|
2004-07-22 14:37:38 +00:00
|
|
|
}
|
|
|
|
|
2005-05-18 08:57:17 +00:00
|
|
|
// fill list of certificates; the first entry will be selected
|
|
|
|
for ( sal_Int32 nC = 0; nC < nCertificates; ++nC )
|
2004-07-22 14:37:38 +00:00
|
|
|
{
|
2013-08-17 01:11:11 +02:00
|
|
|
SvTreeListEntry* pEntry = m_pCertLB->InsertEntry( XmlSec::GetContentPart( maCerts[ nC ]->getSubjectName() )
|
|
|
|
+ "\t" + XmlSec::GetContentPart( maCerts[ nC ]->getIssuerName() )
|
2016-05-19 20:27:55 +03:00
|
|
|
+ "\t" + UsageInClearText( maCerts[ nC ]->getCertificateUsage() )
|
2013-08-17 01:11:11 +02:00
|
|
|
+ "\t" + XmlSec::GetDateString( maCerts[ nC ]->getNotValidAfter() ) );
|
2014-10-02 15:36:13 +02:00
|
|
|
pEntry->SetUserData( reinterpret_cast<void*>(nC) ); // missuse user data as index
|
2004-07-22 14:37:38 +00:00
|
|
|
}
|
2004-07-12 12:15:31 +00:00
|
|
|
|
2005-05-18 08:57:17 +00:00
|
|
|
// enable/disable buttons
|
2015-11-10 10:29:43 +01:00
|
|
|
CertificateHighlightHdl( nullptr );
|
2012-09-25 17:28:22 +02:00
|
|
|
mbInitialized = true;
|
2004-07-12 12:15:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-30 00:32:52 +01:00
|
|
|
uno::Reference< css::security::XCertificate > CertificateChooser::GetSelectedCertificate()
|
2004-07-12 12:15:31 +00:00
|
|
|
{
|
2013-03-30 00:32:52 +01:00
|
|
|
uno::Reference< css::security::XCertificate > xCert;
|
2011-01-14 14:16:44 +01:00
|
|
|
sal_uInt16 nSelected = GetSelectedEntryPos();
|
2005-03-10 17:04:16 +00:00
|
|
|
if ( nSelected < maCerts.getLength() )
|
2004-07-12 12:15:31 +00:00
|
|
|
xCert = maCerts[ nSelected ];
|
|
|
|
return xCert;
|
|
|
|
}
|
|
|
|
|
2016-01-06 09:07:46 +01:00
|
|
|
OUString CertificateChooser::GetDescription()
|
|
|
|
{
|
|
|
|
return m_pDescriptionED->GetText();
|
|
|
|
}
|
|
|
|
|
2016-10-05 07:56:12 +02:00
|
|
|
IMPL_LINK_NOARG(CertificateChooser, CertificateHighlightHdl, SvTreeListBox*, void)
|
2004-07-12 12:15:31 +00:00
|
|
|
{
|
2014-04-23 13:44:07 +02:00
|
|
|
bool bEnable = GetSelectedCertificate().is();
|
2012-12-29 18:11:33 +00:00
|
|
|
m_pViewBtn->Enable( bEnable );
|
|
|
|
m_pOKBtn->Enable( bEnable );
|
2016-01-06 09:07:46 +01:00
|
|
|
m_pDescriptionED->Enable(bEnable);
|
2004-07-12 12:15:31 +00:00
|
|
|
}
|
|
|
|
|
2016-10-05 07:56:12 +02:00
|
|
|
IMPL_LINK_NOARG(CertificateChooser, CertificateSelectHdl, SvTreeListBox*, bool)
|
2004-07-12 12:15:31 +00:00
|
|
|
{
|
2005-03-10 17:04:16 +00:00
|
|
|
EndDialog( RET_OK );
|
2015-09-10 14:39:17 +02:00
|
|
|
return false;
|
2004-07-12 12:15:31 +00:00
|
|
|
}
|
|
|
|
|
2016-10-05 07:56:12 +02:00
|
|
|
IMPL_LINK_NOARG(CertificateChooser, ViewButtonHdl, Button*, void)
|
2004-07-12 12:15:31 +00:00
|
|
|
{
|
|
|
|
ImplShowCertificateDetails();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CertificateChooser::ImplShowCertificateDetails()
|
|
|
|
{
|
2013-03-30 00:32:52 +01:00
|
|
|
uno::Reference< css::security::XCertificate > xCert = GetSelectedCertificate();
|
2004-07-12 12:15:31 +00:00
|
|
|
if( xCert.is() )
|
|
|
|
{
|
2015-03-31 23:04:14 +01:00
|
|
|
ScopedVclPtrInstance< CertificateViewer > aViewer( this, mxSecurityEnvironment, xCert, true );
|
2015-02-11 14:42:23 +02:00
|
|
|
aViewer->Execute();
|
2004-07-12 12:15:31 +00:00
|
|
|
}
|
2005-03-10 17:04:16 +00:00
|
|
|
}
|
|
|
|
|
2010-10-12 15:57:08 +02:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|