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:51:56 +00:00
2004-07-12 12:15:31 +00:00
# include <sal/main.h>
# include <vcl/event.hxx>
# include <vcl/svapp.hxx>
# include <vcl/wrkwin.hxx>
# include <vcl/msgbox.hxx>
# include <vcl/fixed.hxx>
# include <vcl/edit.hxx>
# include <vcl/button.hxx>
# include <vcl/lstbox.hxx>
# include <svtools/filectrl.hxx>
# include <tools/urlobj.hxx>
# include <osl/file.hxx>
# include <svtools/docpasswdrequest.hxx>
# include <comphelper/processfactory.hxx>
# include <cppuhelper/servicefactory.hxx>
# include <cppuhelper/bootstrap.hxx>
# include <unotools/streamhelper.hxx>
// Will be in comphelper if CWS MAV09 is integrated
2004-11-26 14:02:16 +00:00
# include <comphelper/storagehelper.hxx>
2004-07-12 12:15:31 +00:00
# include <com/sun/star/lang/XMultiServiceFactory.hpp>
# include <xmlsecurity/xmlsignaturehelper.hxx>
# include <xmlsecurity/digitalsignaturesdialog.hxx>
# include <xmlsecurity/certificatechooser.hxx>
2004-07-26 06:30:29 +00:00
# include <xmlsecurity/biginteger.hxx>
2004-07-12 12:15:31 +00:00
2012-09-04 11:42:03 +02:00
# include <com/sun/star/security/DocumentDigitalSignatures.hpp>
2004-07-12 12:15:31 +00:00
using namespace : : com : : sun : : star ;
using namespace : : com : : sun : : star ;
void Main ( ) ;
# define TEXTFIELDWIDTH 80
# define TEXTFIELDSTARTX 10
# define EDITWIDTH 200
# define EDITHEIGHT 20
# define FIXEDLINEHEIGHT 15
# define BUTTONWIDTH 50
# define BUTTONHEIGHT 22
# define BUTTONSPACE 20
// -----------------------------------------------------------------------
SAL_IMPLEMENT_MAIN ( )
{
uno : : Reference < lang : : XMultiServiceFactory > xMSF ;
try
{
uno : : Reference < uno : : XComponentContext > xCtx ( cppu : : defaultBootstrap_InitialComponentContext ( ) ) ;
if ( ! xCtx . is ( ) )
{
2011-03-01 17:55:09 +01:00
OSL_FAIL ( " Error creating initial component context! " ) ;
2004-07-12 12:15:31 +00:00
return - 1 ;
}
xMSF = uno : : Reference < lang : : XMultiServiceFactory > ( xCtx - > getServiceManager ( ) , uno : : UNO_QUERY ) ;
if ( ! xMSF . is ( ) )
{
2011-03-01 17:55:09 +01:00
OSL_FAIL ( " No service manager! " ) ;
2004-07-12 12:15:31 +00:00
return - 1 ;
}
}
catch ( uno : : Exception const & )
{
2011-03-01 17:55:09 +01:00
OSL_FAIL ( " Exception during creation of initial component context! " ) ;
2004-07-12 12:15:31 +00:00
return - 1 ;
}
comphelper : : setProcessServiceFactory ( xMSF ) ;
InitVCL ( xMSF ) ;
: : Main ( ) ;
DeInitVCL ( ) ;
return 0 ;
}
// -----------------------------------------------------------------------
class MyWin : public WorkWindow
{
private :
FixedLine maTokenLine ;
CheckBox maCryptoCheckBox ;
FixedText maFixedTextTokenName ;
FileControl maEditTokenName ;
FixedLine maTest1Line ;
FixedText maFixedTextXMLFileName ;
FileControl maEditXMLFileName ;
FixedText maFixedTextBINFileName ;
FileControl maEditBINFileName ;
FixedText maFixedTextSIGFileName ;
FileControl maEditSIGFileName ;
PushButton maSignButton ;
PushButton maVerifyButton ;
FixedLine maTest2Line ;
FixedText maFixedTextDOCFileName ;
FileControl maEditDOCFileName ;
PushButton maDigitalSignaturesButton ;
2004-07-14 10:06:31 +00:00
PushButton maVerifyDigitalSignaturesButton ;
2004-07-12 12:15:31 +00:00
FixedLine maHintLine ;
FixedText maHintText ;
DECL_LINK ( CryptoCheckBoxHdl , CheckBox * ) ;
DECL_LINK ( SignButtonHdl , Button * ) ;
DECL_LINK ( VerifyButtonHdl , Button * ) ;
DECL_LINK ( DigitalSignaturesWithServiceHdl , Button * ) ;
2004-07-14 10:06:31 +00:00
DECL_LINK ( VerifyDigitalSignaturesHdl , Button * ) ;
2004-07-12 12:15:31 +00:00
DECL_LINK ( DigitalSignaturesWithTokenHdl , Button * ) ;
DECL_LINK ( StartVerifySignatureHdl , void * ) ;
public :
MyWin ( Window * pParent , WinBits nWinStyle ) ;
} ;
// -----------------------------------------------------------------------
void Main ( )
{
MyWin aMainWin ( NULL , WB_APP | WB_STDWORK | WB_3DLOOK ) ;
aMainWin . Show ( ) ;
Application : : Execute ( ) ;
}
// -----------------------------------------------------------------------
MyWin : : MyWin ( Window * pParent , WinBits nWinStyle ) :
WorkWindow ( pParent , nWinStyle ) ,
maTokenLine ( this ) ,
maTest1Line ( this ) ,
maTest2Line ( this ) ,
maHintLine ( this ) ,
maFixedTextXMLFileName ( this ) ,
maEditXMLFileName ( this , WB_BORDER ) ,
maFixedTextBINFileName ( this ) ,
maEditBINFileName ( this , WB_BORDER ) ,
maFixedTextSIGFileName ( this ) ,
maEditSIGFileName ( this , WB_BORDER ) ,
maFixedTextTokenName ( this ) ,
maEditTokenName ( this , WB_BORDER ) ,
maFixedTextDOCFileName ( this ) ,
maEditDOCFileName ( this , WB_BORDER ) ,
maSignButton ( this ) ,
maVerifyButton ( this ) ,
maDigitalSignaturesButton ( this ) ,
2004-07-14 10:06:31 +00:00
maVerifyDigitalSignaturesButton ( this ) ,
2004-07-12 12:15:31 +00:00
maHintText ( this , WB_WORDBREAK ) ,
maCryptoCheckBox ( this )
{
2004-07-14 10:06:31 +00:00
Size aOutputSize ( 400 , 400 ) ;
2004-07-12 12:15:31 +00:00
SetOutputSizePixel ( aOutputSize ) ;
2012-09-25 10:31:20 +04:00
SetText ( OUString ( " XML Signature Test " ) ) ;
2004-07-12 12:15:31 +00:00
long nY = 15 ;
maTokenLine . SetPosSizePixel ( TEXTFIELDSTARTX , nY , aOutputSize . Width ( ) - 2 * TEXTFIELDSTARTX , FIXEDLINEHEIGHT ) ;
2012-09-25 10:31:20 +04:00
maTokenLine . SetText ( OUString ( " Crypto Settings " ) ) ;
2004-07-12 12:15:31 +00:00
maTokenLine . Show ( ) ;
nY + = EDITHEIGHT * 3 / 2 ;
maCryptoCheckBox . SetPosSizePixel ( TEXTFIELDSTARTX , nY , aOutputSize . Width ( ) - 2 * TEXTFIELDSTARTX , FIXEDLINEHEIGHT ) ;
2012-09-25 10:31:20 +04:00
maCryptoCheckBox . SetText ( OUString ( " Use Default Token (NSS option only) " ) ) ;
2011-01-14 14:16:44 +01:00
maCryptoCheckBox . Check ( sal_True ) ;
2004-07-12 12:15:31 +00:00
maEditTokenName . Disable ( ) ;
maFixedTextTokenName . Disable ( ) ;
maCryptoCheckBox . SetClickHdl ( LINK ( this , MyWin , CryptoCheckBoxHdl ) ) ;
maCryptoCheckBox . Show ( ) ;
nY + = EDITHEIGHT ;
maFixedTextTokenName . SetPosSizePixel ( TEXTFIELDSTARTX , nY , TEXTFIELDWIDTH , EDITHEIGHT ) ;
2012-09-25 10:31:20 +04:00
maFixedTextTokenName . SetText ( OUString ( " Crypto Token: " ) ) ;
2004-07-12 12:15:31 +00:00
maFixedTextTokenName . Show ( ) ;
maEditTokenName . SetPosSizePixel ( TEXTFIELDSTARTX + TEXTFIELDWIDTH , nY , EDITWIDTH , EDITHEIGHT ) ;
maEditTokenName . Show ( ) ;
nY + = EDITHEIGHT * 3 ;
maTest2Line . SetPosSizePixel ( TEXTFIELDSTARTX , nY , aOutputSize . Width ( ) - 2 * TEXTFIELDSTARTX , FIXEDLINEHEIGHT ) ;
2012-09-25 10:31:20 +04:00
maTest2Line . SetText ( OUString ( " Test Office Document " ) ) ;
2004-07-12 12:15:31 +00:00
maTest2Line . Show ( ) ;
nY + = EDITHEIGHT * 3 / 2 ;
2004-07-14 10:06:31 +00:00
2004-07-12 12:15:31 +00:00
maFixedTextDOCFileName . SetPosSizePixel ( TEXTFIELDSTARTX , nY , TEXTFIELDWIDTH , EDITHEIGHT ) ;
2012-09-25 10:31:20 +04:00
maFixedTextDOCFileName . SetText ( OUString ( " Office File: " ) ) ;
2004-07-12 12:15:31 +00:00
maFixedTextDOCFileName . Show ( ) ;
maEditDOCFileName . SetPosSizePixel ( TEXTFIELDSTARTX + TEXTFIELDWIDTH , nY , EDITWIDTH , EDITHEIGHT ) ;
maEditDOCFileName . Show ( ) ;
nY + = EDITHEIGHT * 2 ;
maDigitalSignaturesButton . SetPosSizePixel ( TEXTFIELDSTARTX , nY , BUTTONWIDTH * 2 , BUTTONHEIGHT ) ;
2012-09-25 10:31:20 +04:00
maDigitalSignaturesButton . SetText ( OUString ( " Digital Signatures... " ) ) ;
2004-07-12 12:15:31 +00:00
maDigitalSignaturesButton . SetClickHdl ( LINK ( this , MyWin , DigitalSignaturesWithServiceHdl ) ) ;
maDigitalSignaturesButton . Show ( ) ;
2004-07-14 10:06:31 +00:00
maVerifyDigitalSignaturesButton . SetPosSizePixel ( TEXTFIELDSTARTX + BUTTONWIDTH * 2 + BUTTONSPACE , nY , BUTTONWIDTH * 2 , BUTTONHEIGHT ) ;
2012-09-25 10:31:20 +04:00
maVerifyDigitalSignaturesButton . SetText ( OUString ( " Verify Signatures " ) ) ;
2004-07-14 10:06:31 +00:00
maVerifyDigitalSignaturesButton . SetClickHdl ( LINK ( this , MyWin , VerifyDigitalSignaturesHdl ) ) ;
maVerifyDigitalSignaturesButton . Show ( ) ;
nY + = EDITHEIGHT * 2 ;
2004-07-12 12:15:31 +00:00
maHintLine . SetPosSizePixel ( TEXTFIELDSTARTX , nY , aOutputSize . Width ( ) - 2 * TEXTFIELDSTARTX , FIXEDLINEHEIGHT ) ;
maHintLine . Show ( ) ;
nY + = EDITHEIGHT * 2 ;
maHintText . SetPosSizePixel ( TEXTFIELDSTARTX , nY , aOutputSize . Width ( ) - 2 * TEXTFIELDSTARTX , aOutputSize . Height ( ) - nY ) ;
2012-09-25 10:31:20 +04:00
maHintText . SetText ( OUString ( " Hint: Copy crypto files from xmlsecurity/tools/cryptoken/nss and sample files from xmlsecurity/tools/examples to <temp>/nss. \n This location will be used from the demo as the default location. " ) ) ;
2004-07-12 12:15:31 +00:00
maHintText . Show ( ) ;
// Help the user with some default values
: : rtl : : OUString aTempDirURL ;
: : osl : : File : : getTempDirURL ( aTempDirURL ) ;
INetURLObject aURLObj ( aTempDirURL ) ;
2012-09-25 10:31:20 +04:00
aURLObj . insertName ( " nss " , true ) ;
2004-07-12 12:15:31 +00:00
: : rtl : : OUString aNSSFolder = aURLObj . getFSysPath ( INetURLObject : : FSYS_DETECT ) ;
2012-09-25 10:31:20 +04:00
maEditXMLFileName . SetText ( aNSSFolder + " demo-sample.xml " ) ;
maEditBINFileName . SetText ( aNSSFolder + " demo-sample.gif " ) ;
maEditDOCFileName . SetText ( aNSSFolder + " demo-sample.sxw " ) ;
maEditSIGFileName . SetText ( aNSSFolder + " demo-result.xml " ) ;
2004-07-12 12:15:31 +00:00
maEditTokenName . SetText ( aNSSFolder ) ;
2004-07-14 10:06:31 +00:00
# ifdef WNT
2012-09-25 10:31:20 +04:00
maEditTokenName . SetText ( OUString ( ) ) ;
2004-07-14 10:06:31 +00:00
maEditTokenName . Disable ( ) ;
maCryptoCheckBox . Disable ( ) ;
# endif
2004-07-12 12:15:31 +00:00
}
2012-03-01 18:00:32 +01:00
IMPL_LINK_NOARG ( MyWin , CryptoCheckBoxHdl )
2004-07-12 12:15:31 +00:00
{
if ( maCryptoCheckBox . IsChecked ( ) )
{
maEditTokenName . Disable ( ) ;
maFixedTextTokenName . Disable ( ) ;
}
else
{
maEditTokenName . Enable ( ) ;
maFixedTextTokenName . Enable ( ) ;
}
return 1 ;
}
2012-03-01 18:00:32 +01:00
IMPL_LINK_NOARG ( MyWin , DigitalSignaturesWithServiceHdl )
2004-07-14 10:06:31 +00:00
{
rtl : : OUString aDocFileName = maEditDOCFileName . GetText ( ) ;
uno : : Reference < embed : : XStorage > xStore = : : comphelper : : OStorageHelper : : GetStorageFromURL (
aDocFileName , embed : : ElementModes : : READWRITE , comphelper : : getProcessServiceFactory ( ) ) ;
uno : : Reference < security : : XDocumentDigitalSignatures > xD (
2012-09-04 17:23:18 +02:00
security : : DocumentDigitalSignatures : : createDefault ( comphelper : : getProcessComponentContext ( ) ) ;
2012-09-04 11:42:03 +02:00
xD - > signDocumentContent ( xStore , NULL ) ;
2004-07-14 10:06:31 +00:00
return 0 ;
}
2012-03-01 18:00:32 +01:00
IMPL_LINK_NOARG ( MyWin , VerifyDigitalSignaturesHdl )
2004-07-14 10:06:31 +00:00
{
rtl : : OUString aDocFileName = maEditDOCFileName . GetText ( ) ;
uno : : Reference < embed : : XStorage > xStore = : : comphelper : : OStorageHelper : : GetStorageFromURL (
aDocFileName , embed : : ElementModes : : READWRITE , comphelper : : getProcessServiceFactory ( ) ) ;
uno : : Reference < security : : XDocumentDigitalSignatures > xD (
2012-09-04 17:23:18 +02:00
security : : DocumentDigitalSignatures : : createDefault ( comphelper : : getProcessComponentContext ( ) ) ) ;
2012-09-04 11:42:03 +02:00
uno : : Sequence < security : : DocumentSignatureInformation > aInfos = xD - > verifyDocumentContentSignatures ( xStore , NULL ) ;
int nInfos = aInfos . getLength ( ) ;
for ( int n = 0 ; n < nInfos ; n + + )
2004-07-14 10:06:31 +00:00
{
2012-09-04 11:42:03 +02:00
security : : DocumentSignatureInformation & rInf = aInfos [ n ] ;
2012-09-25 10:31:20 +04:00
OUStringBuffer aText ( " The document is signed by \n \n " ) ;
aText . append ( rInf . Signer - > getSubjectName ( ) ) ;
aText . append ( " \n \n The signature is " ) ;
2012-09-04 11:42:03 +02:00
if ( ! rInf . SignatureIsValid )
2012-09-25 10:31:20 +04:00
aText . append ( " NOT " ) ;
aText . append ( " valid " ) ;
InfoBox ( this , aText . makeStringAndClear ( ) ) . Execute ( ) ;
2004-07-14 10:06:31 +00:00
}
return 0 ;
}
2010-10-12 15:57:08 +02:00
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */