1*06b3ce53SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*06b3ce53SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*06b3ce53SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*06b3ce53SAndrew Rist  * distributed with this work for additional information
6*06b3ce53SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*06b3ce53SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*06b3ce53SAndrew Rist  * "License"); you may not use this file except in compliance
9*06b3ce53SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*06b3ce53SAndrew Rist  *
11*06b3ce53SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*06b3ce53SAndrew Rist  *
13*06b3ce53SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*06b3ce53SAndrew Rist  * software distributed under the License is distributed on an
15*06b3ce53SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*06b3ce53SAndrew Rist  * KIND, either express or implied.  See the License for the
17*06b3ce53SAndrew Rist  * specific language governing permissions and limitations
18*06b3ce53SAndrew Rist  * under the License.
19*06b3ce53SAndrew Rist  *
20*06b3ce53SAndrew Rist  *************************************************************/
21*06b3ce53SAndrew Rist 
22*06b3ce53SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_xmlsecurity.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <sal/main.h>
28cdf0e10cSrcweir #include <vcl/event.hxx>
29cdf0e10cSrcweir #include <vcl/svapp.hxx>
30cdf0e10cSrcweir #include <vcl/wrkwin.hxx>
31cdf0e10cSrcweir #include <vcl/msgbox.hxx>
32cdf0e10cSrcweir #include <vcl/fixed.hxx>
33cdf0e10cSrcweir #include <vcl/edit.hxx>
34cdf0e10cSrcweir #include <vcl/button.hxx>
35cdf0e10cSrcweir #include <vcl/lstbox.hxx>
36cdf0e10cSrcweir #include <svtools/filectrl.hxx>
37cdf0e10cSrcweir #include <tools/urlobj.hxx>
38cdf0e10cSrcweir #include <osl/file.hxx>
39cdf0e10cSrcweir 
40cdf0e10cSrcweir #include <svtools/docpasswdrequest.hxx>
41cdf0e10cSrcweir 
42cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
43cdf0e10cSrcweir #include <cppuhelper/servicefactory.hxx>
44cdf0e10cSrcweir #include <cppuhelper/bootstrap.hxx>
45cdf0e10cSrcweir #include <unotools/streamhelper.hxx>
46cdf0e10cSrcweir 
47cdf0e10cSrcweir #include <ucbhelper/contentbroker.hxx>
48cdf0e10cSrcweir #include <ucbhelper/configurationkeys.hxx>
49cdf0e10cSrcweir 
50cdf0e10cSrcweir // Will be in comphelper if CWS MAV09 is integrated
51cdf0e10cSrcweir #include <comphelper/storagehelper.hxx>
52cdf0e10cSrcweir 
53cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
54cdf0e10cSrcweir 
55cdf0e10cSrcweir #include <xmlsecurity/xmlsignaturehelper.hxx>
56cdf0e10cSrcweir #include <xmlsecurity/digitalsignaturesdialog.hxx>
57cdf0e10cSrcweir #include <xmlsecurity/certificatechooser.hxx>
58cdf0e10cSrcweir #include <xmlsecurity/biginteger.hxx>
59cdf0e10cSrcweir 
60cdf0e10cSrcweir #include <com/sun/star/security/XDocumentDigitalSignatures.hpp>
61cdf0e10cSrcweir 
62cdf0e10cSrcweir using namespace ::com::sun::star;
63cdf0e10cSrcweir using namespace ::com::sun::star;
64cdf0e10cSrcweir 
65cdf0e10cSrcweir void Main();
66cdf0e10cSrcweir 
67cdf0e10cSrcweir #define TEXTFIELDWIDTH	80
68cdf0e10cSrcweir #define TEXTFIELDSTARTX	10
69cdf0e10cSrcweir 
70cdf0e10cSrcweir #define EDITWIDTH		200
71cdf0e10cSrcweir #define EDITHEIGHT		20
72cdf0e10cSrcweir 
73cdf0e10cSrcweir #define FIXEDLINEHEIGHT	15
74cdf0e10cSrcweir 
75cdf0e10cSrcweir #define BUTTONWIDTH		50
76cdf0e10cSrcweir #define BUTTONHEIGHT	22
77cdf0e10cSrcweir #define BUTTONSPACE     20
78cdf0e10cSrcweir 
79cdf0e10cSrcweir #define LISTBOXHEIGHT   120
80cdf0e10cSrcweir 
81cdf0e10cSrcweir // #define TEST_IMPLEMENTATION_DIRECTLY
82cdf0e10cSrcweir 
83cdf0e10cSrcweir 
84cdf0e10cSrcweir // -----------------------------------------------------------------------
85cdf0e10cSrcweir 
SAL_IMPLEMENT_MAIN()86cdf0e10cSrcweir     SAL_IMPLEMENT_MAIN()
87cdf0e10cSrcweir {
88cdf0e10cSrcweir     uno::Reference< lang::XMultiServiceFactory > xMSF;
89cdf0e10cSrcweir 	try
90cdf0e10cSrcweir 	{
91cdf0e10cSrcweir         uno::Reference< uno::XComponentContext > xCtx( cppu::defaultBootstrap_InitialComponentContext() );
92cdf0e10cSrcweir         if ( !xCtx.is() )
93cdf0e10cSrcweir         {
94cdf0e10cSrcweir             DBG_ERROR( "Error creating initial component context!" );
95cdf0e10cSrcweir             return -1;
96cdf0e10cSrcweir         }
97cdf0e10cSrcweir 
98cdf0e10cSrcweir         xMSF = uno::Reference< lang::XMultiServiceFactory >(xCtx->getServiceManager(), uno::UNO_QUERY );
99cdf0e10cSrcweir 
100cdf0e10cSrcweir         if ( !xMSF.is() )
101cdf0e10cSrcweir         {
102cdf0e10cSrcweir             DBG_ERROR( "No service manager!" );
103cdf0e10cSrcweir             return -1;
104cdf0e10cSrcweir         }
105cdf0e10cSrcweir 
106cdf0e10cSrcweir         // Init USB
107cdf0e10cSrcweir         uno::Sequence< uno::Any > aArgs( 2 );
108cdf0e10cSrcweir         aArgs[ 0 ] <<= rtl::OUString::createFromAscii( UCB_CONFIGURATION_KEY1_LOCAL );
109cdf0e10cSrcweir 	    aArgs[ 1 ] <<= rtl::OUString::createFromAscii( UCB_CONFIGURATION_KEY2_OFFICE );
110cdf0e10cSrcweir 	    sal_Bool bSuccess = ::ucb::ContentBroker::initialize( xMSF, aArgs );
111cdf0e10cSrcweir 	    if ( !bSuccess )
112cdf0e10cSrcweir 	    {
113cdf0e10cSrcweir 		    DBG_ERROR( "Error creating UCB!" );
114cdf0e10cSrcweir 		    return -1;
115cdf0e10cSrcweir 	    }
116cdf0e10cSrcweir 
117cdf0e10cSrcweir 	}
118cdf0e10cSrcweir     catch ( uno::Exception const & )
119cdf0e10cSrcweir 	{
120cdf0e10cSrcweir         DBG_ERROR( "Exception during creation of initial component context!" );
121cdf0e10cSrcweir 		return -1;
122cdf0e10cSrcweir 	}
123cdf0e10cSrcweir 	comphelper::setProcessServiceFactory( xMSF );
124cdf0e10cSrcweir 
125cdf0e10cSrcweir     InitVCL( xMSF );
126cdf0e10cSrcweir     ::Main();
127cdf0e10cSrcweir     DeInitVCL();
128cdf0e10cSrcweir 
129cdf0e10cSrcweir     return 0;
130cdf0e10cSrcweir }
131cdf0e10cSrcweir 
132cdf0e10cSrcweir // -----------------------------------------------------------------------
133cdf0e10cSrcweir 
134cdf0e10cSrcweir class MyWin : public WorkWindow
135cdf0e10cSrcweir {
136cdf0e10cSrcweir private:
137cdf0e10cSrcweir     FixedLine   maTokenLine;
138cdf0e10cSrcweir     CheckBox    maCryptoCheckBox;
139cdf0e10cSrcweir 	FixedText	maFixedTextTokenName;
140cdf0e10cSrcweir 	FileControl	maEditTokenName;
141cdf0e10cSrcweir     FixedLine   maTest1Line;
142cdf0e10cSrcweir 	FixedText	maFixedTextXMLFileName;
143cdf0e10cSrcweir 	FileControl	maEditXMLFileName;
144cdf0e10cSrcweir 	FixedText	maFixedTextBINFileName;
145cdf0e10cSrcweir 	FileControl	maEditBINFileName;
146cdf0e10cSrcweir 	FixedText	maFixedTextSIGFileName;
147cdf0e10cSrcweir 	FileControl	maEditSIGFileName;
148cdf0e10cSrcweir 	PushButton	maSignButton;
149cdf0e10cSrcweir 	PushButton	maVerifyButton;
150cdf0e10cSrcweir     FixedLine   maTest2Line;
151cdf0e10cSrcweir 	FixedText	maFixedTextDOCFileName;
152cdf0e10cSrcweir 	FileControl	maEditDOCFileName;
153cdf0e10cSrcweir 	PushButton	maDigitalSignaturesButton;
154cdf0e10cSrcweir 	PushButton	maVerifyDigitalSignaturesButton;
155cdf0e10cSrcweir     FixedLine   maHintLine;
156cdf0e10cSrcweir 	FixedText	maHintText;
157cdf0e10cSrcweir 
158cdf0e10cSrcweir 	DECL_LINK(	CryptoCheckBoxHdl, CheckBox* );
159cdf0e10cSrcweir 	DECL_LINK(	SignButtonHdl, Button* );
160cdf0e10cSrcweir 	DECL_LINK(	VerifyButtonHdl, Button* );
161cdf0e10cSrcweir 	DECL_LINK(	DigitalSignaturesWithServiceHdl, Button* );
162cdf0e10cSrcweir 	DECL_LINK(	VerifyDigitalSignaturesHdl, Button* );
163cdf0e10cSrcweir 	DECL_LINK(	DigitalSignaturesWithTokenHdl, Button* );
164cdf0e10cSrcweir 	DECL_LINK(	StartVerifySignatureHdl, void* );
165cdf0e10cSrcweir 
166cdf0e10cSrcweir public:
167cdf0e10cSrcweir 				MyWin( Window* pParent, WinBits nWinStyle );
168cdf0e10cSrcweir 
169cdf0e10cSrcweir };
170cdf0e10cSrcweir 
171cdf0e10cSrcweir // -----------------------------------------------------------------------
172cdf0e10cSrcweir 
Main()173cdf0e10cSrcweir void Main()
174cdf0e10cSrcweir {
175cdf0e10cSrcweir 	MyWin aMainWin( NULL, WB_APP | WB_STDWORK | WB_3DLOOK);
176cdf0e10cSrcweir 	aMainWin.Show();
177cdf0e10cSrcweir 
178cdf0e10cSrcweir 	Application::Execute();
179cdf0e10cSrcweir }
180cdf0e10cSrcweir 
181cdf0e10cSrcweir // -----------------------------------------------------------------------
182cdf0e10cSrcweir 
MyWin(Window * pParent,WinBits nWinStyle)183cdf0e10cSrcweir MyWin::MyWin( Window* pParent, WinBits nWinStyle ) :
184cdf0e10cSrcweir 	WorkWindow( pParent, nWinStyle ),
185cdf0e10cSrcweir     maTokenLine( this ),
186cdf0e10cSrcweir     maTest1Line( this ),
187cdf0e10cSrcweir     maTest2Line( this ),
188cdf0e10cSrcweir     maHintLine( this ),
189cdf0e10cSrcweir 	maFixedTextXMLFileName( this ),
190cdf0e10cSrcweir 	maEditXMLFileName( this, WB_BORDER ),
191cdf0e10cSrcweir 	maFixedTextBINFileName( this ),
192cdf0e10cSrcweir 	maEditBINFileName( this, WB_BORDER ),
193cdf0e10cSrcweir 	maFixedTextSIGFileName( this ),
194cdf0e10cSrcweir 	maEditSIGFileName( this, WB_BORDER ),
195cdf0e10cSrcweir 	maFixedTextTokenName( this ),
196cdf0e10cSrcweir 	maEditTokenName( this, WB_BORDER ),
197cdf0e10cSrcweir 	maFixedTextDOCFileName( this ),
198cdf0e10cSrcweir 	maEditDOCFileName( this, WB_BORDER ),
199cdf0e10cSrcweir 	maSignButton( this ),
200cdf0e10cSrcweir 	maVerifyButton( this ),
201cdf0e10cSrcweir 	maDigitalSignaturesButton( this ),
202cdf0e10cSrcweir 	maVerifyDigitalSignaturesButton( this ),
203cdf0e10cSrcweir 	maHintText( this, WB_WORDBREAK ),
204cdf0e10cSrcweir 	maCryptoCheckBox( this )
205cdf0e10cSrcweir 
206cdf0e10cSrcweir {
207cdf0e10cSrcweir #ifdef TEST_IMPLEMENTATION_DIRECTLY
208cdf0e10cSrcweir     Size aOutputSize( 400, 600 );
209cdf0e10cSrcweir #else
210cdf0e10cSrcweir     Size aOutputSize( 400, 400 );
211cdf0e10cSrcweir #endif
212cdf0e10cSrcweir 	SetOutputSizePixel( aOutputSize );
213cdf0e10cSrcweir 	SetText( String( RTL_CONSTASCII_USTRINGPARAM( "XML Signature Test" ) ) );
214cdf0e10cSrcweir 
215cdf0e10cSrcweir 	long nY = 15;
216cdf0e10cSrcweir 
217cdf0e10cSrcweir     maTokenLine.SetPosSizePixel( TEXTFIELDSTARTX, nY, aOutputSize.Width()-2*TEXTFIELDSTARTX, FIXEDLINEHEIGHT );
218cdf0e10cSrcweir 	maTokenLine.SetText( String( RTL_CONSTASCII_USTRINGPARAM( "Crypto Settings" ) ) );
219cdf0e10cSrcweir 	maTokenLine.Show();
220cdf0e10cSrcweir 
221cdf0e10cSrcweir 	nY += EDITHEIGHT*3/2;
222cdf0e10cSrcweir 
223cdf0e10cSrcweir 	maCryptoCheckBox.SetPosSizePixel( TEXTFIELDSTARTX, nY, aOutputSize.Width()-2*TEXTFIELDSTARTX, FIXEDLINEHEIGHT );
224cdf0e10cSrcweir 	maCryptoCheckBox.SetText( String( RTL_CONSTASCII_USTRINGPARAM( "Use Default Token (NSS option only)" ) ) );
225cdf0e10cSrcweir 	maCryptoCheckBox.Check( sal_True );
226cdf0e10cSrcweir 	maEditTokenName.Disable();
227cdf0e10cSrcweir 	maFixedTextTokenName.Disable();
228cdf0e10cSrcweir     maCryptoCheckBox.SetClickHdl( LINK( this, MyWin, CryptoCheckBoxHdl ) );
229cdf0e10cSrcweir 	maCryptoCheckBox.Show();
230cdf0e10cSrcweir 
231cdf0e10cSrcweir 	nY += EDITHEIGHT;
232cdf0e10cSrcweir 
233cdf0e10cSrcweir 	maFixedTextTokenName.SetPosSizePixel( TEXTFIELDSTARTX, nY, TEXTFIELDWIDTH, EDITHEIGHT );
234cdf0e10cSrcweir 	maFixedTextTokenName.SetText( String( RTL_CONSTASCII_USTRINGPARAM( "Crypto Token:" ) ) );
235cdf0e10cSrcweir 	maFixedTextTokenName.Show();
236cdf0e10cSrcweir 
237cdf0e10cSrcweir 	maEditTokenName.SetPosSizePixel( TEXTFIELDSTARTX+TEXTFIELDWIDTH, nY, EDITWIDTH, EDITHEIGHT );
238cdf0e10cSrcweir 	maEditTokenName.Show();
239cdf0e10cSrcweir 
240cdf0e10cSrcweir 	nY += EDITHEIGHT*3;
241cdf0e10cSrcweir 
242cdf0e10cSrcweir #ifdef TEST_IMPLEMENTATION_DIRECTLY
243cdf0e10cSrcweir 
244cdf0e10cSrcweir     maTest1Line.SetPosSizePixel( TEXTFIELDSTARTX, nY, aOutputSize.Width()-2*TEXTFIELDSTARTX, FIXEDLINEHEIGHT );
245cdf0e10cSrcweir 	maTest1Line.SetText( String( RTL_CONSTASCII_USTRINGPARAM( "Test simple files" ) ) );
246cdf0e10cSrcweir 	maTest1Line.Show();
247cdf0e10cSrcweir 
248cdf0e10cSrcweir 	nY += EDITHEIGHT*3/2;
249cdf0e10cSrcweir 
250cdf0e10cSrcweir     maFixedTextXMLFileName.SetPosSizePixel( TEXTFIELDSTARTX, nY, TEXTFIELDWIDTH, EDITHEIGHT );
251cdf0e10cSrcweir 	maFixedTextXMLFileName.SetText( String( RTL_CONSTASCII_USTRINGPARAM( "XML File:" ) ) );
252cdf0e10cSrcweir 	maFixedTextXMLFileName.Show();
253cdf0e10cSrcweir 
254cdf0e10cSrcweir 	maEditXMLFileName.SetPosSizePixel( TEXTFIELDSTARTX+TEXTFIELDWIDTH, nY, EDITWIDTH, EDITHEIGHT );
255cdf0e10cSrcweir 	maEditXMLFileName.Show();
256cdf0e10cSrcweir 
257cdf0e10cSrcweir 	nY += EDITHEIGHT*3/2;
258cdf0e10cSrcweir 
259cdf0e10cSrcweir 	maFixedTextBINFileName.SetPosSizePixel( TEXTFIELDSTARTX, nY, TEXTFIELDWIDTH, EDITHEIGHT );
260cdf0e10cSrcweir 	maFixedTextBINFileName.SetText( String( RTL_CONSTASCII_USTRINGPARAM( "Binary File:" ) ) );
261cdf0e10cSrcweir 	maFixedTextBINFileName.Show();
262cdf0e10cSrcweir 
263cdf0e10cSrcweir 	maEditBINFileName.SetPosSizePixel( TEXTFIELDSTARTX+TEXTFIELDWIDTH, nY, EDITWIDTH, EDITHEIGHT );
264cdf0e10cSrcweir 	maEditBINFileName.Show();
265cdf0e10cSrcweir 
266cdf0e10cSrcweir 	nY += EDITHEIGHT*3/2;
267cdf0e10cSrcweir 
268cdf0e10cSrcweir 	maFixedTextSIGFileName.SetPosSizePixel( TEXTFIELDSTARTX, nY, TEXTFIELDWIDTH, EDITHEIGHT );
269cdf0e10cSrcweir 	maFixedTextSIGFileName.SetText( String( RTL_CONSTASCII_USTRINGPARAM( "Signature File:" ) ) );
270cdf0e10cSrcweir 	maFixedTextSIGFileName.Show();
271cdf0e10cSrcweir 
272cdf0e10cSrcweir 	maEditSIGFileName.SetPosSizePixel( TEXTFIELDSTARTX+TEXTFIELDWIDTH, nY, EDITWIDTH, EDITHEIGHT );
273cdf0e10cSrcweir 	maEditSIGFileName.Show();
274cdf0e10cSrcweir 
275cdf0e10cSrcweir 	nY += EDITHEIGHT*2;
276cdf0e10cSrcweir 
277cdf0e10cSrcweir 	maSignButton.SetPosSizePixel( TEXTFIELDSTARTX, nY, BUTTONWIDTH, BUTTONHEIGHT );
278cdf0e10cSrcweir 	maSignButton.SetText( String( RTL_CONSTASCII_USTRINGPARAM( "Sign" ) ) );
279cdf0e10cSrcweir 	maSignButton.SetClickHdl( LINK( this, MyWin, SignButtonHdl ) );
280cdf0e10cSrcweir 	maSignButton.Show();
281cdf0e10cSrcweir 
282cdf0e10cSrcweir 	maVerifyButton.SetPosSizePixel( TEXTFIELDSTARTX+BUTTONWIDTH+BUTTONSPACE, nY, BUTTONWIDTH, BUTTONHEIGHT );
283cdf0e10cSrcweir 	maVerifyButton.SetText( String( RTL_CONSTASCII_USTRINGPARAM( "Verify" ) ) );
284cdf0e10cSrcweir 	maVerifyButton.SetClickHdl( LINK( this, MyWin, VerifyButtonHdl ) );
285cdf0e10cSrcweir 	maVerifyButton.Show();
286cdf0e10cSrcweir 
287cdf0e10cSrcweir 	nY += EDITHEIGHT*3;
288cdf0e10cSrcweir 
289cdf0e10cSrcweir #endif // TEST_IMPLEMENTATION_DIRECTLY
290cdf0e10cSrcweir 
291cdf0e10cSrcweir     maTest2Line.SetPosSizePixel( TEXTFIELDSTARTX, nY, aOutputSize.Width()-2*TEXTFIELDSTARTX, FIXEDLINEHEIGHT );
292cdf0e10cSrcweir 	maTest2Line.SetText( String( RTL_CONSTASCII_USTRINGPARAM( "Test Office Document" ) ) );
293cdf0e10cSrcweir 	maTest2Line.Show();
294cdf0e10cSrcweir 
295cdf0e10cSrcweir 	nY += EDITHEIGHT*3/2;
296cdf0e10cSrcweir 
297cdf0e10cSrcweir 
298cdf0e10cSrcweir 	maFixedTextDOCFileName.SetPosSizePixel( TEXTFIELDSTARTX, nY, TEXTFIELDWIDTH, EDITHEIGHT );
299cdf0e10cSrcweir 	maFixedTextDOCFileName.SetText( String( RTL_CONSTASCII_USTRINGPARAM( "Office File:" ) ) );
300cdf0e10cSrcweir 	maFixedTextDOCFileName.Show();
301cdf0e10cSrcweir 
302cdf0e10cSrcweir 	maEditDOCFileName.SetPosSizePixel( TEXTFIELDSTARTX+TEXTFIELDWIDTH, nY, EDITWIDTH, EDITHEIGHT );
303cdf0e10cSrcweir 	maEditDOCFileName.Show();
304cdf0e10cSrcweir 
305cdf0e10cSrcweir 	nY += EDITHEIGHT*2;
306cdf0e10cSrcweir 
307cdf0e10cSrcweir 	maDigitalSignaturesButton.SetPosSizePixel( TEXTFIELDSTARTX, nY, BUTTONWIDTH*2, BUTTONHEIGHT );
308cdf0e10cSrcweir 	maDigitalSignaturesButton.SetText( String( RTL_CONSTASCII_USTRINGPARAM( "Digital Signatures..." ) ) );
309cdf0e10cSrcweir 	maDigitalSignaturesButton.SetClickHdl( LINK( this, MyWin, DigitalSignaturesWithServiceHdl ) );
310cdf0e10cSrcweir 	maDigitalSignaturesButton.Show();
311cdf0e10cSrcweir 
312cdf0e10cSrcweir 	maVerifyDigitalSignaturesButton.SetPosSizePixel( TEXTFIELDSTARTX+BUTTONWIDTH*2+BUTTONSPACE, nY, BUTTONWIDTH*2, BUTTONHEIGHT );
313cdf0e10cSrcweir 	maVerifyDigitalSignaturesButton.SetText( String( RTL_CONSTASCII_USTRINGPARAM( "Verify Signatures" ) ) );
314cdf0e10cSrcweir 	maVerifyDigitalSignaturesButton.SetClickHdl( LINK( this, MyWin, VerifyDigitalSignaturesHdl ) );
315cdf0e10cSrcweir 	maVerifyDigitalSignaturesButton.Show();
316cdf0e10cSrcweir 
317cdf0e10cSrcweir     nY += EDITHEIGHT*2;
318cdf0e10cSrcweir 
319cdf0e10cSrcweir     maHintLine.SetPosSizePixel( TEXTFIELDSTARTX, nY, aOutputSize.Width()-2*TEXTFIELDSTARTX, FIXEDLINEHEIGHT );
320cdf0e10cSrcweir 	maHintLine.Show();
321cdf0e10cSrcweir 
322cdf0e10cSrcweir     nY += EDITHEIGHT*2;
323cdf0e10cSrcweir 
324cdf0e10cSrcweir 	maHintText.SetPosSizePixel( TEXTFIELDSTARTX, nY, aOutputSize.Width()-2*TEXTFIELDSTARTX, aOutputSize.Height()-nY );
325cdf0e10cSrcweir 	maHintText.SetText( String( RTL_CONSTASCII_USTRINGPARAM( "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." ) ) );
326cdf0e10cSrcweir 	maHintText.Show();
327cdf0e10cSrcweir 
328cdf0e10cSrcweir     // Help the user with some default values
329cdf0e10cSrcweir     ::rtl::OUString aTempDirURL;
330cdf0e10cSrcweir     ::osl::File::getTempDirURL( aTempDirURL );
331cdf0e10cSrcweir     INetURLObject aURLObj( aTempDirURL );
332cdf0e10cSrcweir     aURLObj.insertName( String( RTL_CONSTASCII_USTRINGPARAM( "nss" ) ), true );
333cdf0e10cSrcweir     ::rtl::OUString aNSSFolder = aURLObj.getFSysPath( INetURLObject::FSYS_DETECT );
334cdf0e10cSrcweir     String aDefaultXMLFileName( aNSSFolder );
335cdf0e10cSrcweir     maEditXMLFileName.SetText( aNSSFolder + String( RTL_CONSTASCII_USTRINGPARAM( "demo-sample.xml" ) ) );
336cdf0e10cSrcweir     maEditBINFileName.SetText( aNSSFolder + String( RTL_CONSTASCII_USTRINGPARAM( "demo-sample.gif" ) ) );
337cdf0e10cSrcweir     maEditDOCFileName.SetText( aNSSFolder + String( RTL_CONSTASCII_USTRINGPARAM( "demo-sample.sxw" ) ) );
338cdf0e10cSrcweir     maEditSIGFileName.SetText( aNSSFolder + String( RTL_CONSTASCII_USTRINGPARAM( "demo-result.xml" ) ) );
339cdf0e10cSrcweir     maEditTokenName.SetText( aNSSFolder );
340cdf0e10cSrcweir 
341cdf0e10cSrcweir #ifdef WNT
342cdf0e10cSrcweir 	maEditTokenName.SetText( String() );
343cdf0e10cSrcweir 	maEditTokenName.Disable();
344cdf0e10cSrcweir 	maCryptoCheckBox.Disable();
345cdf0e10cSrcweir #endif
346cdf0e10cSrcweir 
347cdf0e10cSrcweir }
348cdf0e10cSrcweir 
IMPL_LINK(MyWin,CryptoCheckBoxHdl,CheckBox *,EMPTYARG)349cdf0e10cSrcweir IMPL_LINK( MyWin, CryptoCheckBoxHdl, CheckBox*, EMPTYARG )
350cdf0e10cSrcweir {
351cdf0e10cSrcweir     if ( maCryptoCheckBox.IsChecked() )
352cdf0e10cSrcweir     {
353cdf0e10cSrcweir         maEditTokenName.Disable();
354cdf0e10cSrcweir         maFixedTextTokenName.Disable();
355cdf0e10cSrcweir     }
356cdf0e10cSrcweir     else
357cdf0e10cSrcweir     {
358cdf0e10cSrcweir         maEditTokenName.Enable();
359cdf0e10cSrcweir         maFixedTextTokenName.Enable();
360cdf0e10cSrcweir     }
361cdf0e10cSrcweir     return 1;
362cdf0e10cSrcweir }
363cdf0e10cSrcweir 
IMPL_LINK(MyWin,DigitalSignaturesWithServiceHdl,Button *,EMPTYARG)364cdf0e10cSrcweir IMPL_LINK( MyWin, DigitalSignaturesWithServiceHdl, Button*, EMPTYARG )
365cdf0e10cSrcweir {
366cdf0e10cSrcweir     rtl::OUString aDocFileName = maEditDOCFileName.GetText();
367cdf0e10cSrcweir     uno::Reference < embed::XStorage > xStore = ::comphelper::OStorageHelper::GetStorageFromURL(
368cdf0e10cSrcweir             aDocFileName, embed::ElementModes::READWRITE, comphelper::getProcessServiceFactory() );
369cdf0e10cSrcweir 
370cdf0e10cSrcweir     uno::Reference< security::XDocumentDigitalSignatures > xD(
371cdf0e10cSrcweir         comphelper::getProcessServiceFactory()->createInstance( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.security.DocumentDigitalSignatures" ) ) ), uno::UNO_QUERY );
372cdf0e10cSrcweir     if ( xD.is() )
373cdf0e10cSrcweir         xD->signDocumentContent( xStore, NULL );
374cdf0e10cSrcweir 
375cdf0e10cSrcweir 
376cdf0e10cSrcweir     return 0;
377cdf0e10cSrcweir }
378cdf0e10cSrcweir 
IMPL_LINK(MyWin,VerifyDigitalSignaturesHdl,Button *,EMPTYARG)379cdf0e10cSrcweir IMPL_LINK( MyWin, VerifyDigitalSignaturesHdl, Button*, EMPTYARG )
380cdf0e10cSrcweir {
381cdf0e10cSrcweir     rtl::OUString aDocFileName = maEditDOCFileName.GetText();
382cdf0e10cSrcweir     uno::Reference < embed::XStorage > xStore = ::comphelper::OStorageHelper::GetStorageFromURL(
383cdf0e10cSrcweir             aDocFileName, embed::ElementModes::READWRITE, comphelper::getProcessServiceFactory() );
384cdf0e10cSrcweir 
385cdf0e10cSrcweir     uno::Reference< security::XDocumentDigitalSignatures > xD(
386cdf0e10cSrcweir         comphelper::getProcessServiceFactory()->createInstance( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.security.DocumentDigitalSignatures" ) ) ), uno::UNO_QUERY );
387cdf0e10cSrcweir     if ( xD.is() )
388cdf0e10cSrcweir     {
389cdf0e10cSrcweir         uno::Sequence< security::DocumentSignatureInformation > aInfos = xD->verifyDocumentContentSignatures( xStore, NULL );
390cdf0e10cSrcweir         int nInfos = aInfos.getLength();
391cdf0e10cSrcweir         for ( int n = 0; n < nInfos; n++ )
392cdf0e10cSrcweir         {
393cdf0e10cSrcweir             security::DocumentSignatureInformation& rInf = aInfos[n];
394cdf0e10cSrcweir             String aText( RTL_CONSTASCII_USTRINGPARAM( "The document is signed by\n\n  " ) );
395cdf0e10cSrcweir             aText += String( rInf.Signer->getSubjectName() );
396cdf0e10cSrcweir             aText += String( RTL_CONSTASCII_USTRINGPARAM( "\n\n The signature is " ) );
397cdf0e10cSrcweir             if ( !rInf.SignatureIsValid )
398cdf0e10cSrcweir                 aText += String( RTL_CONSTASCII_USTRINGPARAM( "NOT " ) );
399cdf0e10cSrcweir             aText += String( RTL_CONSTASCII_USTRINGPARAM( "valid" ) );
400cdf0e10cSrcweir             InfoBox( this, aText ).Execute();
401cdf0e10cSrcweir         }
402cdf0e10cSrcweir 
403cdf0e10cSrcweir     }
404cdf0e10cSrcweir 
405cdf0e10cSrcweir 
406cdf0e10cSrcweir     return 0;
407cdf0e10cSrcweir }
408cdf0e10cSrcweir 
409cdf0e10cSrcweir 
410cdf0e10cSrcweir #ifdef TEST_IMPLEMENTATION_DIRECTLY
411cdf0e10cSrcweir 
IMPL_LINK(MyWin,DigitalSignaturesWithTokenHdl,Button *,EMPTYARG)412cdf0e10cSrcweir IMPL_LINK( MyWin, DigitalSignaturesWithTokenHdl, Button*, EMPTYARG )
413cdf0e10cSrcweir {
414cdf0e10cSrcweir     String aDocFileName = maEditDOCFileName.GetText();
415cdf0e10cSrcweir     String aTokenFileName = maEditTokenName.GetText();
416cdf0e10cSrcweir 
417cdf0e10cSrcweir     DigitalSignaturesDialog aSignaturesDialog( this, comphelper::getProcessServiceFactory(), SignatureModeDocumentContent, false );
418cdf0e10cSrcweir 
419cdf0e10cSrcweir     bool bInit = aSignaturesDialog.Init( aTokenFileName );
420cdf0e10cSrcweir     if ( !bInit )
421cdf0e10cSrcweir     {
422cdf0e10cSrcweir         ErrorBox( this, WB_OK, String( RTL_CONSTASCII_USTRINGPARAM( "Error initializing security context!" ) ) ).Execute();
423cdf0e10cSrcweir         return 0;
424cdf0e10cSrcweir     }
425cdf0e10cSrcweir 
426cdf0e10cSrcweir     uno::Reference < embed::XStorage > xStore = ::comphelper::OStorageHelper::GetStorageFromURL(
427cdf0e10cSrcweir             aDocFileName, embed::ElementModes::READWRITE, comphelper::getProcessServiceFactory() );
428cdf0e10cSrcweir 
429cdf0e10cSrcweir     aSignaturesDialog.SetStorage( xStore );
430cdf0e10cSrcweir 
431cdf0e10cSrcweir     aSignaturesDialog.Execute();
432cdf0e10cSrcweir 
433cdf0e10cSrcweir     return 0;
434cdf0e10cSrcweir }
435cdf0e10cSrcweir 
IMPL_LINK(MyWin,SignButtonHdl,Button *,EMPTYARG)436cdf0e10cSrcweir IMPL_LINK( MyWin, SignButtonHdl, Button*, EMPTYARG )
437cdf0e10cSrcweir {
438cdf0e10cSrcweir 	String aXMLFileName = maEditXMLFileName.GetText();
439cdf0e10cSrcweir 	String aBINFileName = maEditBINFileName.GetText();
440cdf0e10cSrcweir 	String aSIGFileName = maEditSIGFileName.GetText();
441cdf0e10cSrcweir 
442cdf0e10cSrcweir 	String aTokenFileName;
443cdf0e10cSrcweir 	if ( !maCryptoCheckBox.IsChecked() )
444cdf0e10cSrcweir 	    aTokenFileName = maEditTokenName.GetText();
445cdf0e10cSrcweir 
446cdf0e10cSrcweir     XMLSignatureHelper aSignatureHelper( comphelper::getProcessServiceFactory() );
447cdf0e10cSrcweir     bool bInit = aSignatureHelper.Init( aTokenFileName );
448cdf0e10cSrcweir 
449cdf0e10cSrcweir     if ( !bInit )
450cdf0e10cSrcweir     {
451cdf0e10cSrcweir         ErrorBox( this, WB_OK, String( RTL_CONSTASCII_USTRINGPARAM( "Error initializing security context!" ) ) ).Execute();
452cdf0e10cSrcweir         return 0;
453cdf0e10cSrcweir     }
454cdf0e10cSrcweir 
455cdf0e10cSrcweir     uno::Reference< ::com::sun::star::security::XCertificate > xCertToUse;
456cdf0e10cSrcweir     CertificateChooser aChooser( this, aSignatureHelper.GetSecurityEnvironment(), SignatureInformations() );
457cdf0e10cSrcweir     if ( aChooser.Execute() )
458cdf0e10cSrcweir         xCertToUse = aChooser.GetSelectedCertificate();
459cdf0e10cSrcweir 
460cdf0e10cSrcweir     if ( !xCertToUse.is() )
461cdf0e10cSrcweir         return 0;
462cdf0e10cSrcweir 
463cdf0e10cSrcweir 
464cdf0e10cSrcweir     aSignatureHelper.StartMission();
465cdf0e10cSrcweir 
466cdf0e10cSrcweir     sal_Int32 nSecurityId = aSignatureHelper.GetNewSecurityId();
467cdf0e10cSrcweir 
468cdf0e10cSrcweir     aSignatureHelper.SetX509Certificate( nSecurityId, xCertToUse->getIssuerName(), bigIntegerToNumericString( xCertToUse->getSerialNumber() ) );
469cdf0e10cSrcweir 
470cdf0e10cSrcweir     aSignatureHelper.AddForSigning( nSecurityId, aXMLFileName, aXMLFileName, sal_False );
471cdf0e10cSrcweir 	aSignatureHelper.AddForSigning( nSecurityId, aBINFileName, aBINFileName, sal_True );
472cdf0e10cSrcweir 
473cdf0e10cSrcweir     SvFileStream* pStream = new SvFileStream( aSIGFileName, STREAM_WRITE );
474cdf0e10cSrcweir     SvLockBytesRef xLockBytes = new SvLockBytes( pStream, sal_True );
475cdf0e10cSrcweir  	uno::Reference< io::XOutputStream > xOutputStream = new utl::OOutputStreamHelper( xLockBytes );
476cdf0e10cSrcweir 	bool bDone = aSignatureHelper.CreateAndWriteSignature( xOutputStream );
477cdf0e10cSrcweir 
478cdf0e10cSrcweir 	aSignatureHelper.EndMission();
479cdf0e10cSrcweir 
480cdf0e10cSrcweir     if ( !bDone )
481cdf0e10cSrcweir     {
482cdf0e10cSrcweir         ErrorBox( this, WB_OK, String( RTL_CONSTASCII_USTRINGPARAM( "Error creating Signature!" ) ) ).Execute();
483cdf0e10cSrcweir     }
484cdf0e10cSrcweir     else
485cdf0e10cSrcweir     {
486cdf0e10cSrcweir         rtl::OUString aInfo( String( RTL_CONSTASCII_USTRINGPARAM( "Signature successfully created!\n\n" ) ) );
487cdf0e10cSrcweir         // aInfo += getSignatureInformationmations( aSignatureHelper.getAllSignatureInformation(), aSignatureHelper.GetSecurityEnvironment() );
488cdf0e10cSrcweir 
489cdf0e10cSrcweir 
490cdf0e10cSrcweir         InfoBox( this, aInfo ).Execute();
491cdf0e10cSrcweir     }
492cdf0e10cSrcweir 
493cdf0e10cSrcweir     // Check for more detailed results...
494cdf0e10cSrcweir 
495cdf0e10cSrcweir     return 0;
496cdf0e10cSrcweir }
497cdf0e10cSrcweir 
IMPL_LINK(MyWin,VerifyButtonHdl,Button *,EMPTYARG)498cdf0e10cSrcweir IMPL_LINK( MyWin, VerifyButtonHdl, Button*, EMPTYARG )
499cdf0e10cSrcweir {
500cdf0e10cSrcweir     String aXMLFileName = maEditXMLFileName.GetText();
501cdf0e10cSrcweir     String aBINFileName = maEditBINFileName.GetText();
502cdf0e10cSrcweir     String aSIGFileName = maEditSIGFileName.GetText();
503cdf0e10cSrcweir 
504cdf0e10cSrcweir 	String aTokenFileName;
505cdf0e10cSrcweir 	if ( !maCryptoCheckBox.IsChecked() )
506cdf0e10cSrcweir 	    aTokenFileName = maEditTokenName.GetText();
507cdf0e10cSrcweir 
508cdf0e10cSrcweir     XMLSignatureHelper aSignatureHelper( comphelper::getProcessServiceFactory() );
509cdf0e10cSrcweir     bool bInit = aSignatureHelper.Init( aTokenFileName );
510cdf0e10cSrcweir 
511cdf0e10cSrcweir     if ( !bInit )
512cdf0e10cSrcweir     {
513cdf0e10cSrcweir         ErrorBox( this, WB_OK, String( RTL_CONSTASCII_USTRINGPARAM( "Error initializing security context!" ) ) ).Execute();
514cdf0e10cSrcweir         return 0;
515cdf0e10cSrcweir     }
516cdf0e10cSrcweir 
517cdf0e10cSrcweir     aSignatureHelper.SetStartVerifySignatureHdl( LINK( this, MyWin, StartVerifySignatureHdl ) );
518cdf0e10cSrcweir 
519cdf0e10cSrcweir     aSignatureHelper.StartMission();
520cdf0e10cSrcweir 
521cdf0e10cSrcweir     SvFileStream* pStream = new SvFileStream( aSIGFileName, STREAM_READ );
522cdf0e10cSrcweir     pStream->Seek( STREAM_SEEK_TO_END );
523cdf0e10cSrcweir     sal_uLong nBytes = pStream->Tell();
524cdf0e10cSrcweir     pStream->Seek( STREAM_SEEK_TO_BEGIN );
525cdf0e10cSrcweir     SvLockBytesRef xLockBytes = new SvLockBytes( pStream, sal_True );
526cdf0e10cSrcweir  	uno::Reference< io::XInputStream > xInputStream = new utl::OInputStreamHelper( xLockBytes, nBytes );
527cdf0e10cSrcweir 	bool bDone = aSignatureHelper.ReadAndVerifySignature( xInputStream );
528cdf0e10cSrcweir 	xInputStream->closeInput();
529cdf0e10cSrcweir 
530cdf0e10cSrcweir 	aSignatureHelper.EndMission();
531cdf0e10cSrcweir 
532cdf0e10cSrcweir     if ( !bDone )
533cdf0e10cSrcweir         ErrorBox( this, WB_OK, String( RTL_CONSTASCII_USTRINGPARAM( "Error in Signature!" ) ) ).Execute();
534cdf0e10cSrcweir     else
535cdf0e10cSrcweir         InfoBox( this, String( RTL_CONSTASCII_USTRINGPARAM( "Signatures verified without any problems!" ) ) ).Execute();
536cdf0e10cSrcweir 
537cdf0e10cSrcweir     return 0;
538cdf0e10cSrcweir }
539cdf0e10cSrcweir 
IMPL_LINK(MyWin,StartVerifySignatureHdl,void *,EMPTYARG)540cdf0e10cSrcweir IMPL_LINK( MyWin, StartVerifySignatureHdl, void*, EMPTYARG )
541cdf0e10cSrcweir {
542cdf0e10cSrcweir     QueryBox aQueryBox( this, WB_YES_NO|WB_DEF_YES, String( RTL_CONSTASCII_USTRINGPARAM( "Found Signature - Verify?" ) ) );
543cdf0e10cSrcweir     return ( aQueryBox.Execute() == RET_YES ) ? 1 : 0;
544cdf0e10cSrcweir }
545cdf0e10cSrcweir 
546cdf0e10cSrcweir 
547cdf0e10cSrcweir #endif // #ifdef TEST_IMPLEMENTATION_DIRECTLY
548