1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: certificatechooser.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_xmlsecurity.hxx"
34 #include <xmlsecurity/certificatechooser.hxx>
35 #include <xmlsecurity/certificateviewer.hxx>
36 #include <xmlsecurity/biginteger.hxx>
37 #include <com/sun/star/xml/crypto/XSecurityEnvironment.hpp>
38 #include <comphelper/sequence.hxx>
39 #include <comphelper/processfactory.hxx>
41 #include <com/sun/star/security/NoPasswordException.hpp>
42 #include <com/sun/star/security/CertificateCharacters.hpp>
43 #include <com/sun/star/security/SerialNumberAdapter.hpp>
45 #include <dialogs.hrc>
46 #include <resourcemanager.hxx>
47 #include <vcl/msgbox.hxx>
49 /* HACK: disable some warnings for MS-C */
51 #pragma warning (disable : 4355) // 4355: this used in initializer-list
54 using namespace ::com::sun::star
;
56 #define INVAL_SEL 0xFFFF
58 USHORT
CertificateChooser::GetSelectedEntryPos( void ) const
60 USHORT nSel
= INVAL_SEL
;
62 SvLBoxEntry
* pSel
= maCertLB
.FirstSelected();
64 nSel
= (USHORT
) ( sal_uIntPtr
) pSel
->GetUserData();
69 CertificateChooser::CertificateChooser( Window
* _pParent
, uno::Reference
< uno::XComponentContext
>& _rxCtx
, uno::Reference
< dcss::xml::crypto::XSecurityEnvironment
>& _rxSecurityEnvironment
, const SignatureInformations
& _rCertsToIgnore
)
70 :ModalDialog ( _pParent
, XMLSEC_RES( RID_XMLSECDLG_CERTCHOOSER
) )
71 ,maCertsToIgnore( _rCertsToIgnore
)
72 ,maHintFT ( this, XMLSEC_RES( FT_HINT_SELECT
) )
73 ,maCertLB ( this, XMLSEC_RES( LB_SIGNATURES
) )
74 ,maViewBtn ( this, XMLSEC_RES( BTN_VIEWCERT
) )
75 ,maBottomSepFL ( this, XMLSEC_RES( FL_BOTTOM_SEP
) )
76 ,maOKBtn ( this, XMLSEC_RES( BTN_OK
) )
77 ,maCancelBtn ( this, XMLSEC_RES( BTN_CANCEL
) )
78 ,maHelpBtn ( this, XMLSEC_RES( BTN_HELP
) )
80 static long nTabs
[] = { 3, 0, 30*CS_LB_WIDTH
/100, 60*CS_LB_WIDTH
/100 };
81 maCertLB
.SetTabs( &nTabs
[0] );
82 maCertLB
.InsertHeaderEntry( String( XMLSEC_RES( STR_HEADERBAR
) ) );
83 maCertLB
.SetSelectHdl( LINK( this, CertificateChooser
, CertificateHighlightHdl
) );
84 maCertLB
.SetDoubleClickHdl( LINK( this, CertificateChooser
, CertificateSelectHdl
) );
85 maViewBtn
.SetClickHdl( LINK( this, CertificateChooser
, ViewButtonHdl
) );
90 mxSecurityEnvironment
= _rxSecurityEnvironment
;
91 mbInitialized
= FALSE
;
94 CertificateHighlightHdl( NULL
);
97 CertificateChooser::~CertificateChooser()
101 short CertificateChooser::Execute()
104 // We can't check for personal certificates before raising this dialog,
105 // because the mozilla implementation throws a NoPassword exception,
106 // if the user pressed cancel, and also if the database does not exist!
107 // But in the later case, the is no password query, and the user is confused
108 // that nothing happens when pressing "Add..." in the SignatureDialog.
110 // PostUserEvent( LINK( this, CertificateChooser, Initialize ) );
112 // PostUserLink behavior is to slow, so do it directly before Execute().
113 // Problem: This Dialog should be visible right now, and the parent should not be accessible.
114 // Show, Update, DIsableInput...
117 Window
* pParent
= GetParent();
119 pParent
->EnableInput( FALSE
);
124 pParent
->EnableInput( TRUE
);
125 return ModalDialog::Execute();
128 // IMPL_LINK( CertificateChooser, Initialize, void*, EMPTYARG )
129 void CertificateChooser::ImplInitialize()
131 if ( !mbInitialized
)
135 maCerts
= mxSecurityEnvironment
->getPersonalCertificates();
137 catch (security::NoPasswordException
&)
141 uno::Reference
< dcss::security::XSerialNumberAdapter
> xSerialNumberAdapter
=
142 ::com::sun::star::security::SerialNumberAdapter::create(mxCtx
);
144 sal_Int32 nCertificates
= maCerts
.getLength();
145 sal_Int32 nCertificatesToIgnore
= maCertsToIgnore
.size();
146 for( sal_Int32 nCert
= nCertificates
; nCert
; )
148 uno::Reference
< security::XCertificate
> xCert
= maCerts
[ --nCert
];
149 sal_Bool bIgnoreThis
= false;
151 // Do we already use that?
152 if( nCertificatesToIgnore
)
154 rtl::OUString aIssuerName
= xCert
->getIssuerName();
155 for( sal_Int32 nSig
= 0; nSig
< nCertificatesToIgnore
; ++nSig
)
157 const SignatureInformation
& rInf
= maCertsToIgnore
[ nSig
];
158 if ( ( aIssuerName
== rInf
.ouX509IssuerName
) &&
159 ( xSerialNumberAdapter
->toString( xCert
->getSerialNumber() ) == rInf
.ouX509SerialNumber
) )
169 // Check if we have a private key for this...
170 long nCertificateCharacters
= mxSecurityEnvironment
->getCertificateCharacters( xCert
);
172 if ( !( nCertificateCharacters
& security::CertificateCharacters::HAS_PRIVATE_KEY
) )
179 ::comphelper::removeElementAt( maCerts
, nCert
);
180 nCertificates
= maCerts
.getLength();
184 // fill list of certificates; the first entry will be selected
185 for ( sal_Int32 nC
= 0; nC
< nCertificates
; ++nC
)
187 String
sEntry( XmlSec::GetContentPart( maCerts
[ nC
]->getSubjectName() ) );
189 sEntry
+= XmlSec::GetContentPart( maCerts
[ nC
]->getIssuerName() );
191 sEntry
+= XmlSec::GetDateString( maCerts
[ nC
]->getNotValidAfter() );
192 SvLBoxEntry
* pEntry
= maCertLB
.InsertEntry( sEntry
);
193 pEntry
->SetUserData( ( void* )nC
); // missuse user data as index
196 // enable/disable buttons
197 CertificateHighlightHdl( NULL
);
198 mbInitialized
= TRUE
;
203 uno::Reference
< dcss::security::XCertificate
> CertificateChooser::GetSelectedCertificate()
205 uno::Reference
< dcss::security::XCertificate
> xCert
;
206 USHORT nSelected
= GetSelectedEntryPos();
207 if ( nSelected
< maCerts
.getLength() )
208 xCert
= maCerts
[ nSelected
];
212 IMPL_LINK( CertificateChooser
, CertificateHighlightHdl
, void*, EMPTYARG
)
214 sal_Bool bEnable
= GetSelectedCertificate().is();
215 maViewBtn
.Enable( bEnable
);
216 maOKBtn
.Enable( bEnable
);
220 IMPL_LINK( CertificateChooser
, CertificateSelectHdl
, void*, EMPTYARG
)
226 IMPL_LINK( CertificateChooser
, ViewButtonHdl
, Button
*, EMPTYARG
)
228 ImplShowCertificateDetails();
232 void CertificateChooser::ImplShowCertificateDetails()
234 uno::Reference
< dcss::security::XCertificate
> xCert
= GetSelectedCertificate();
237 CertificateViewer
aViewer( this, mxSecurityEnvironment
, xCert
, TRUE
);