bump product version to 4.1.6.2
[LibreOffice.git] / xmlsecurity / workben / signaturetest.cxx
blob676357fac2499dbe576493fb177f11dd26a6741f
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <sal/main.h>
22 #include <vcl/event.hxx>
23 #include <vcl/svapp.hxx>
24 #include <vcl/wrkwin.hxx>
25 #include <vcl/msgbox.hxx>
26 #include <vcl/fixed.hxx>
27 #include <vcl/edit.hxx>
28 #include <vcl/button.hxx>
29 #include <vcl/lstbox.hxx>
30 #include <svtools/filectrl.hxx>
31 #include <tools/urlobj.hxx>
32 #include <osl/file.hxx>
34 #include <svtools/docpasswdrequest.hxx>
36 #include <comphelper/processfactory.hxx>
37 #include <cppuhelper/servicefactory.hxx>
38 #include <cppuhelper/bootstrap.hxx>
39 #include <unotools/streamhelper.hxx>
41 // Will be in comphelper if CWS MAV09 is integrated
42 #include <comphelper/storagehelper.hxx>
44 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
46 #include <xmlsecurity/xmlsignaturehelper.hxx>
47 #include <xmlsecurity/digitalsignaturesdialog.hxx>
48 #include <xmlsecurity/certificatechooser.hxx>
49 #include <xmlsecurity/biginteger.hxx>
51 #include <com/sun/star/security/DocumentDigitalSignatures.hpp>
53 using namespace ::com::sun::star;
55 void Main();
57 #define TEXTFIELDWIDTH 80
58 #define TEXTFIELDSTARTX 10
60 #define EDITWIDTH 200
61 #define EDITHEIGHT 20
63 #define FIXEDLINEHEIGHT 15
65 #define BUTTONWIDTH 50
66 #define BUTTONHEIGHT 22
67 #define BUTTONSPACE 20
69 // -----------------------------------------------------------------------
71 SAL_IMPLEMENT_MAIN()
73 uno::Reference< lang::XMultiServiceFactory > xMSF;
74 try
76 uno::Reference< uno::XComponentContext > xCtx( cppu::defaultBootstrap_InitialComponentContext() );
77 if ( !xCtx.is() )
79 OSL_FAIL( "Error creating initial component context!" );
80 return -1;
83 xMSF = uno::Reference< lang::XMultiServiceFactory >(xCtx->getServiceManager(), uno::UNO_QUERY );
85 if ( !xMSF.is() )
87 OSL_FAIL( "No service manager!" );
88 return -1;
91 catch ( uno::Exception const & )
93 OSL_FAIL( "Exception during creation of initial component context!" );
94 return -1;
96 comphelper::setProcessServiceFactory( xMSF );
98 InitVCL();
99 ::Main();
100 DeInitVCL();
102 return 0;
105 // -----------------------------------------------------------------------
107 class MyWin : public WorkWindow
109 private:
110 FixedLine maTokenLine;
111 CheckBox maCryptoCheckBox;
112 FixedText maFixedTextTokenName;
113 FileControl maEditTokenName;
114 FixedLine maTest1Line;
115 FixedText maFixedTextXMLFileName;
116 FileControl maEditXMLFileName;
117 FixedText maFixedTextBINFileName;
118 FileControl maEditBINFileName;
119 FixedText maFixedTextSIGFileName;
120 FileControl maEditSIGFileName;
121 PushButton maSignButton;
122 PushButton maVerifyButton;
123 FixedLine maTest2Line;
124 FixedText maFixedTextDOCFileName;
125 FileControl maEditDOCFileName;
126 PushButton maDigitalSignaturesButton;
127 PushButton maVerifyDigitalSignaturesButton;
128 FixedLine maHintLine;
129 FixedText maHintText;
131 DECL_LINK( CryptoCheckBoxHdl, CheckBox* );
132 DECL_LINK( SignButtonHdl, Button* );
133 DECL_LINK( VerifyButtonHdl, Button* );
134 DECL_LINK( DigitalSignaturesWithServiceHdl, Button* );
135 DECL_LINK( VerifyDigitalSignaturesHdl, Button* );
136 DECL_LINK( DigitalSignaturesWithTokenHdl, Button* );
137 DECL_LINK( StartVerifySignatureHdl, void* );
139 public:
140 MyWin( Window* pParent, WinBits nWinStyle );
144 // -----------------------------------------------------------------------
146 void Main()
148 MyWin aMainWin( NULL, WB_APP | WB_STDWORK | WB_3DLOOK);
149 aMainWin.Show();
151 Application::Execute();
154 // -----------------------------------------------------------------------
156 MyWin::MyWin( Window* pParent, WinBits nWinStyle ) :
157 WorkWindow( pParent, nWinStyle ),
158 maTokenLine( this ),
159 maTest1Line( this ),
160 maTest2Line( this ),
161 maHintLine( this ),
162 maFixedTextXMLFileName( this ),
163 maEditXMLFileName( this, WB_BORDER ),
164 maFixedTextBINFileName( this ),
165 maEditBINFileName( this, WB_BORDER ),
166 maFixedTextSIGFileName( this ),
167 maEditSIGFileName( this, WB_BORDER ),
168 maFixedTextTokenName( this ),
169 maEditTokenName( this, WB_BORDER ),
170 maFixedTextDOCFileName( this ),
171 maEditDOCFileName( this, WB_BORDER ),
172 maSignButton( this ),
173 maVerifyButton( this ),
174 maDigitalSignaturesButton( this ),
175 maVerifyDigitalSignaturesButton( this ),
176 maHintText( this, WB_WORDBREAK ),
177 maCryptoCheckBox( this )
180 Size aOutputSize( 400, 400 );
181 SetOutputSizePixel( aOutputSize );
182 SetText( OUString("XML Signature Test") );
184 long nY = 15;
186 maTokenLine.SetPosSizePixel( TEXTFIELDSTARTX, nY, aOutputSize.Width()-2*TEXTFIELDSTARTX, FIXEDLINEHEIGHT );
187 maTokenLine.SetText( OUString("Crypto Settings") );
188 maTokenLine.Show();
190 nY += EDITHEIGHT*3/2;
192 maCryptoCheckBox.SetPosSizePixel( TEXTFIELDSTARTX, nY, aOutputSize.Width()-2*TEXTFIELDSTARTX, FIXEDLINEHEIGHT );
193 maCryptoCheckBox.SetText( OUString("Use Default Token (NSS option only)") );
194 maCryptoCheckBox.Check( sal_True );
195 maEditTokenName.Disable();
196 maFixedTextTokenName.Disable();
197 maCryptoCheckBox.SetClickHdl( LINK( this, MyWin, CryptoCheckBoxHdl ) );
198 maCryptoCheckBox.Show();
200 nY += EDITHEIGHT;
202 maFixedTextTokenName.SetPosSizePixel( TEXTFIELDSTARTX, nY, TEXTFIELDWIDTH, EDITHEIGHT );
203 maFixedTextTokenName.SetText( OUString("Crypto Token:") );
204 maFixedTextTokenName.Show();
206 maEditTokenName.SetPosSizePixel( TEXTFIELDSTARTX+TEXTFIELDWIDTH, nY, EDITWIDTH, EDITHEIGHT );
207 maEditTokenName.Show();
209 nY += EDITHEIGHT*3;
211 maTest2Line.SetPosSizePixel( TEXTFIELDSTARTX, nY, aOutputSize.Width()-2*TEXTFIELDSTARTX, FIXEDLINEHEIGHT );
212 maTest2Line.SetText( OUString("Test Office Document") );
213 maTest2Line.Show();
215 nY += EDITHEIGHT*3/2;
218 maFixedTextDOCFileName.SetPosSizePixel( TEXTFIELDSTARTX, nY, TEXTFIELDWIDTH, EDITHEIGHT );
219 maFixedTextDOCFileName.SetText( OUString("Office File:") );
220 maFixedTextDOCFileName.Show();
222 maEditDOCFileName.SetPosSizePixel( TEXTFIELDSTARTX+TEXTFIELDWIDTH, nY, EDITWIDTH, EDITHEIGHT );
223 maEditDOCFileName.Show();
225 nY += EDITHEIGHT*2;
227 maDigitalSignaturesButton.SetPosSizePixel( TEXTFIELDSTARTX, nY, BUTTONWIDTH*2, BUTTONHEIGHT );
228 maDigitalSignaturesButton.SetText( OUString("Digital Signatures...") );
229 maDigitalSignaturesButton.SetClickHdl( LINK( this, MyWin, DigitalSignaturesWithServiceHdl ) );
230 maDigitalSignaturesButton.Show();
232 maVerifyDigitalSignaturesButton.SetPosSizePixel( TEXTFIELDSTARTX+BUTTONWIDTH*2+BUTTONSPACE, nY, BUTTONWIDTH*2, BUTTONHEIGHT );
233 maVerifyDigitalSignaturesButton.SetText( OUString("Verify Signatures") );
234 maVerifyDigitalSignaturesButton.SetClickHdl( LINK( this, MyWin, VerifyDigitalSignaturesHdl ) );
235 maVerifyDigitalSignaturesButton.Show();
237 nY += EDITHEIGHT*2;
239 maHintLine.SetPosSizePixel( TEXTFIELDSTARTX, nY, aOutputSize.Width()-2*TEXTFIELDSTARTX, FIXEDLINEHEIGHT );
240 maHintLine.Show();
242 nY += EDITHEIGHT*2;
244 maHintText.SetPosSizePixel( TEXTFIELDSTARTX, nY, aOutputSize.Width()-2*TEXTFIELDSTARTX, aOutputSize.Height()-nY );
245 maHintText.SetText( OUString("Hint: Copy crypto files from xmlsecurity/tools/cryptoken/nss and sample files from xmlsecurity/tools/examples to <temp>/nss.\nThis location will be used from the demo as the default location.") );
246 maHintText.Show();
248 // Help the user with some default values
249 OUString aTempDirURL;
250 ::osl::File::getTempDirURL( aTempDirURL );
251 INetURLObject aURLObj( aTempDirURL );
252 aURLObj.insertName( "nss", true );
253 OUString aNSSFolder = aURLObj.getFSysPath( INetURLObject::FSYS_DETECT );
254 maEditXMLFileName.SetText( aNSSFolder + "demo-sample.xml" );
255 maEditBINFileName.SetText( aNSSFolder + "demo-sample.gif" );
256 maEditDOCFileName.SetText( aNSSFolder + "demo-sample.sxw" );
257 maEditSIGFileName.SetText( aNSSFolder + "demo-result.xml" );
258 maEditTokenName.SetText( aNSSFolder );
260 #ifdef WNT
261 maEditTokenName.SetText( OUString() );
262 maEditTokenName.Disable();
263 maCryptoCheckBox.Disable();
264 #endif
268 IMPL_LINK_NOARG(MyWin, CryptoCheckBoxHdl)
270 if ( maCryptoCheckBox.IsChecked() )
272 maEditTokenName.Disable();
273 maFixedTextTokenName.Disable();
275 else
277 maEditTokenName.Enable();
278 maFixedTextTokenName.Enable();
280 return 1;
283 IMPL_LINK_NOARG(MyWin, DigitalSignaturesWithServiceHdl)
285 OUString aDocFileName = maEditDOCFileName.GetText();
286 uno::Reference < embed::XStorage > xStore = ::comphelper::OStorageHelper::GetStorageFromURL(
287 aDocFileName, embed::ElementModes::READWRITE, comphelper::getProcessComponentContext() );
289 uno::Reference< security::XDocumentDigitalSignatures > xD(
290 security::DocumentDigitalSignatures::createDefault(comphelper::getProcessComponentContext()) );
291 xD->signDocumentContent( xStore, NULL );
294 return 0;
297 IMPL_LINK_NOARG(MyWin, VerifyDigitalSignaturesHdl)
299 OUString aDocFileName = maEditDOCFileName.GetText();
300 uno::Reference < embed::XStorage > xStore = ::comphelper::OStorageHelper::GetStorageFromURL(
301 aDocFileName, embed::ElementModes::READWRITE, comphelper::getProcessServiceFactory() );
303 uno::Reference< security::XDocumentDigitalSignatures > xD(
304 security::DocumentDigitalSignatures::createDefault(comphelper::getProcessComponentContext()) );
305 uno::Sequence< security::DocumentSignatureInformation > aInfos = xD->verifyDocumentContentSignatures( xStore, NULL );
306 int nInfos = aInfos.getLength();
307 for ( int n = 0; n < nInfos; n++ )
309 security::DocumentSignatureInformation& rInf = aInfos[n];
310 OUStringBuffer aText( "The document is signed by\n\n " );
311 aText.append( rInf.Signer->getSubjectName() );
312 aText.append( "\n\n The signature is " );
313 if ( !rInf.SignatureIsValid )
314 aText.append( "NOT " );
315 aText.append( "valid" );
316 InfoBox( this, aText.makeStringAndClear() ).Execute();
319 return 0;
322 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */