LCOV - code coverage report
Current view: top level - xmlsecurity/source/dialogs - certificatechooser.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 102 0.0 %
Date: 2012-08-25 Functions: 0 14 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 222 0.0 %

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

Generated by: LCOV version 1.10