LCOV - code coverage report
Current view: top level - xmlsecurity/source/dialogs - certificatechooser.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 1 108 0.9 %
Date: 2015-06-13 12:38:46 Functions: 2 17 11.8 %
Legend: Lines: hit not hit

          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() 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 = VclPtr<SvSimpleTable>::Create(*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 :     disposeOnce();
      85           0 : }
      86             : 
      87           0 : void CertificateChooser::dispose()
      88             : {
      89           0 :     m_pCertLB.disposeAndClear();
      90           0 :     m_pViewBtn.clear();
      91           0 :     m_pOKBtn.clear();
      92           0 :     ModalDialog::dispose();
      93           0 : }
      94             : 
      95           0 : short CertificateChooser::Execute()
      96             : {
      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.
     103             : 
     104             :     // PostUserEvent( LINK( this, CertificateChooser, Initialize ) );
     105             : 
     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...
     109             : 
     110           0 :     vcl::Window* pMe = this;
     111           0 :     vcl::Window* pParent = GetParent();
     112           0 :     if ( pParent )
     113           0 :         pParent->EnableInput( false );
     114           0 :     pMe->Show();
     115           0 :     pMe->Update();
     116           0 :     ImplInitialize();
     117           0 :     if ( pParent )
     118           0 :         pParent->EnableInput( true );
     119           0 :     return ModalDialog::Execute();
     120             : }
     121             : 
     122             : // IMPL_LINK_NOARG(CertificateChooser, Initialize)
     123           0 : void CertificateChooser::ImplInitialize()
     124             : {
     125           0 :     if ( !mbInitialized )
     126             :     {
     127             :         try
     128             :         {
     129           0 :             maCerts = mxSecurityEnvironment->getPersonalCertificates();
     130             :         }
     131           0 :         catch (security::NoPasswordException&)
     132             :         {
     133             :         }
     134             : 
     135             :         uno::Reference< css::security::XSerialNumberAdapter> xSerialNumberAdapter =
     136           0 :             ::com::sun::star::security::SerialNumberAdapter::create(mxCtx);
     137             : 
     138           0 :         sal_Int32 nCertificates = maCerts.getLength();
     139           0 :         sal_Int32 nCertificatesToIgnore = maCertsToIgnore.size();
     140           0 :         for( sal_Int32 nCert = nCertificates; nCert; )
     141             :         {
     142           0 :             uno::Reference< security::XCertificate > xCert = maCerts[ --nCert ];
     143           0 :             bool bIgnoreThis = false;
     144             : 
     145             :             // Do we already use that?
     146           0 :             if( nCertificatesToIgnore )
     147             :             {
     148           0 :                 OUString aIssuerName = xCert->getIssuerName();
     149           0 :                 for( sal_Int32 nSig = 0; nSig < nCertificatesToIgnore; ++nSig )
     150             :                 {
     151           0 :                     const SignatureInformation& rInf = maCertsToIgnore[ nSig ];
     152           0 :                     if ( ( aIssuerName == rInf.ouX509IssuerName ) &&
     153           0 :                         ( xSerialNumberAdapter->toString( xCert->getSerialNumber() ) == rInf.ouX509SerialNumber ) )
     154             :                     {
     155           0 :                         bIgnoreThis = true;
     156           0 :                         break;
     157             :                     }
     158           0 :                 }
     159             :             }
     160             : 
     161           0 :             if ( !bIgnoreThis )
     162             :             {
     163             :                 // Check if we have a private key for this...
     164           0 :                 long nCertificateCharacters = mxSecurityEnvironment->getCertificateCharacters( xCert );
     165             : 
     166           0 :                 if ( !( nCertificateCharacters & security::CertificateCharacters::HAS_PRIVATE_KEY ) )
     167           0 :                     bIgnoreThis = true;
     168             : 
     169             :             }
     170             : 
     171           0 :             if ( bIgnoreThis )
     172             :             {
     173           0 :                 ::comphelper::removeElementAt( maCerts, nCert );
     174           0 :                 nCertificates = maCerts.getLength();
     175             :             }
     176           0 :         }
     177             : 
     178             :         // fill list of certificates; the first entry will be selected
     179           0 :         for ( sal_Int32 nC = 0; nC < nCertificates; ++nC )
     180             :         {
     181           0 :             SvTreeListEntry* pEntry = m_pCertLB->InsertEntry( XmlSec::GetContentPart( maCerts[ nC ]->getSubjectName() )
     182           0 :                 + "\t" + XmlSec::GetContentPart( maCerts[ nC ]->getIssuerName() )
     183           0 :                 + "\t" + XmlSec::GetDateString( maCerts[ nC ]->getNotValidAfter() ) );
     184           0 :             pEntry->SetUserData( reinterpret_cast<void*>(nC) ); // missuse user data as index
     185             :         }
     186             : 
     187             :         // enable/disable buttons
     188           0 :         CertificateHighlightHdl( NULL );
     189           0 :         mbInitialized = true;
     190             :     }
     191           0 : }
     192             : 
     193             : 
     194           0 : uno::Reference< css::security::XCertificate > CertificateChooser::GetSelectedCertificate()
     195             : {
     196           0 :     uno::Reference< css::security::XCertificate > xCert;
     197           0 :     sal_uInt16  nSelected = GetSelectedEntryPos();
     198           0 :     if ( nSelected < maCerts.getLength() )
     199           0 :         xCert = maCerts[ nSelected ];
     200           0 :     return xCert;
     201             : }
     202             : 
     203           0 : IMPL_LINK_NOARG(CertificateChooser, CertificateHighlightHdl)
     204             : {
     205           0 :     bool bEnable = GetSelectedCertificate().is();
     206           0 :     m_pViewBtn->Enable( bEnable );
     207           0 :     m_pOKBtn->Enable( bEnable );
     208           0 :     return 0;
     209             : }
     210             : 
     211           0 : IMPL_LINK_NOARG(CertificateChooser, CertificateSelectHdl)
     212             : {
     213           0 :     EndDialog( RET_OK );
     214           0 :     return 0;
     215             : }
     216             : 
     217           0 : IMPL_LINK_NOARG(CertificateChooser, ViewButtonHdl)
     218             : {
     219           0 :     ImplShowCertificateDetails();
     220           0 :     return 0;
     221             : }
     222             : 
     223           0 : void CertificateChooser::ImplShowCertificateDetails()
     224             : {
     225           0 :     uno::Reference< css::security::XCertificate > xCert = GetSelectedCertificate();
     226           0 :     if( xCert.is() )
     227             :     {
     228           0 :         ScopedVclPtrInstance< CertificateViewer > aViewer( this, mxSecurityEnvironment, xCert, true );
     229           0 :         aViewer->Execute();
     230           0 :     }
     231         114 : }
     232             : 
     233             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11