1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
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
;
54 using namespace ::com::sun::star
;
58 #define TEXTFIELDWIDTH 80
59 #define TEXTFIELDSTARTX 10
64 #define FIXEDLINEHEIGHT 15
66 #define BUTTONWIDTH 50
67 #define BUTTONHEIGHT 22
68 #define BUTTONSPACE 20
70 // -----------------------------------------------------------------------
74 uno::Reference
< lang::XMultiServiceFactory
> xMSF
;
77 uno::Reference
< uno::XComponentContext
> xCtx( cppu::defaultBootstrap_InitialComponentContext() );
80 OSL_FAIL( "Error creating initial component context!" );
84 xMSF
= uno::Reference
< lang::XMultiServiceFactory
>(xCtx
->getServiceManager(), uno::UNO_QUERY
);
88 OSL_FAIL( "No service manager!" );
92 catch ( uno::Exception
const & )
94 OSL_FAIL( "Exception during creation of initial component context!" );
97 comphelper::setProcessServiceFactory( xMSF
);
106 // -----------------------------------------------------------------------
108 class MyWin
: public WorkWindow
111 FixedLine maTokenLine
;
112 CheckBox maCryptoCheckBox
;
113 FixedText maFixedTextTokenName
;
114 FileControl maEditTokenName
;
115 FixedLine maTest1Line
;
116 FixedText maFixedTextXMLFileName
;
117 FileControl maEditXMLFileName
;
118 FixedText maFixedTextBINFileName
;
119 FileControl maEditBINFileName
;
120 FixedText maFixedTextSIGFileName
;
121 FileControl maEditSIGFileName
;
122 PushButton maSignButton
;
123 PushButton maVerifyButton
;
124 FixedLine maTest2Line
;
125 FixedText maFixedTextDOCFileName
;
126 FileControl maEditDOCFileName
;
127 PushButton maDigitalSignaturesButton
;
128 PushButton maVerifyDigitalSignaturesButton
;
129 FixedLine maHintLine
;
130 FixedText maHintText
;
132 DECL_LINK( CryptoCheckBoxHdl
, CheckBox
* );
133 DECL_LINK( SignButtonHdl
, Button
* );
134 DECL_LINK( VerifyButtonHdl
, Button
* );
135 DECL_LINK( DigitalSignaturesWithServiceHdl
, Button
* );
136 DECL_LINK( VerifyDigitalSignaturesHdl
, Button
* );
137 DECL_LINK( DigitalSignaturesWithTokenHdl
, Button
* );
138 DECL_LINK( StartVerifySignatureHdl
, void* );
141 MyWin( Window
* pParent
, WinBits nWinStyle
);
145 // -----------------------------------------------------------------------
149 MyWin
aMainWin( NULL
, WB_APP
| WB_STDWORK
| WB_3DLOOK
);
152 Application::Execute();
155 // -----------------------------------------------------------------------
157 MyWin::MyWin( Window
* pParent
, WinBits nWinStyle
) :
158 WorkWindow( pParent
, nWinStyle
),
163 maFixedTextXMLFileName( this ),
164 maEditXMLFileName( this, WB_BORDER
),
165 maFixedTextBINFileName( this ),
166 maEditBINFileName( this, WB_BORDER
),
167 maFixedTextSIGFileName( this ),
168 maEditSIGFileName( this, WB_BORDER
),
169 maFixedTextTokenName( this ),
170 maEditTokenName( this, WB_BORDER
),
171 maFixedTextDOCFileName( this ),
172 maEditDOCFileName( this, WB_BORDER
),
173 maSignButton( this ),
174 maVerifyButton( this ),
175 maDigitalSignaturesButton( this ),
176 maVerifyDigitalSignaturesButton( this ),
177 maHintText( this, WB_WORDBREAK
),
178 maCryptoCheckBox( this )
181 Size
aOutputSize( 400, 400 );
182 SetOutputSizePixel( aOutputSize
);
183 SetText( OUString("XML Signature Test") );
187 maTokenLine
.SetPosSizePixel( TEXTFIELDSTARTX
, nY
, aOutputSize
.Width()-2*TEXTFIELDSTARTX
, FIXEDLINEHEIGHT
);
188 maTokenLine
.SetText( OUString("Crypto Settings") );
191 nY
+= EDITHEIGHT
*3/2;
193 maCryptoCheckBox
.SetPosSizePixel( TEXTFIELDSTARTX
, nY
, aOutputSize
.Width()-2*TEXTFIELDSTARTX
, FIXEDLINEHEIGHT
);
194 maCryptoCheckBox
.SetText( OUString("Use Default Token (NSS option only)") );
195 maCryptoCheckBox
.Check( sal_True
);
196 maEditTokenName
.Disable();
197 maFixedTextTokenName
.Disable();
198 maCryptoCheckBox
.SetClickHdl( LINK( this, MyWin
, CryptoCheckBoxHdl
) );
199 maCryptoCheckBox
.Show();
203 maFixedTextTokenName
.SetPosSizePixel( TEXTFIELDSTARTX
, nY
, TEXTFIELDWIDTH
, EDITHEIGHT
);
204 maFixedTextTokenName
.SetText( OUString("Crypto Token:") );
205 maFixedTextTokenName
.Show();
207 maEditTokenName
.SetPosSizePixel( TEXTFIELDSTARTX
+TEXTFIELDWIDTH
, nY
, EDITWIDTH
, EDITHEIGHT
);
208 maEditTokenName
.Show();
212 maTest2Line
.SetPosSizePixel( TEXTFIELDSTARTX
, nY
, aOutputSize
.Width()-2*TEXTFIELDSTARTX
, FIXEDLINEHEIGHT
);
213 maTest2Line
.SetText( OUString("Test Office Document") );
216 nY
+= EDITHEIGHT
*3/2;
219 maFixedTextDOCFileName
.SetPosSizePixel( TEXTFIELDSTARTX
, nY
, TEXTFIELDWIDTH
, EDITHEIGHT
);
220 maFixedTextDOCFileName
.SetText( OUString("Office File:") );
221 maFixedTextDOCFileName
.Show();
223 maEditDOCFileName
.SetPosSizePixel( TEXTFIELDSTARTX
+TEXTFIELDWIDTH
, nY
, EDITWIDTH
, EDITHEIGHT
);
224 maEditDOCFileName
.Show();
228 maDigitalSignaturesButton
.SetPosSizePixel( TEXTFIELDSTARTX
, nY
, BUTTONWIDTH
*2, BUTTONHEIGHT
);
229 maDigitalSignaturesButton
.SetText( OUString("Digital Signatures...") );
230 maDigitalSignaturesButton
.SetClickHdl( LINK( this, MyWin
, DigitalSignaturesWithServiceHdl
) );
231 maDigitalSignaturesButton
.Show();
233 maVerifyDigitalSignaturesButton
.SetPosSizePixel( TEXTFIELDSTARTX
+BUTTONWIDTH
*2+BUTTONSPACE
, nY
, BUTTONWIDTH
*2, BUTTONHEIGHT
);
234 maVerifyDigitalSignaturesButton
.SetText( OUString("Verify Signatures") );
235 maVerifyDigitalSignaturesButton
.SetClickHdl( LINK( this, MyWin
, VerifyDigitalSignaturesHdl
) );
236 maVerifyDigitalSignaturesButton
.Show();
240 maHintLine
.SetPosSizePixel( TEXTFIELDSTARTX
, nY
, aOutputSize
.Width()-2*TEXTFIELDSTARTX
, FIXEDLINEHEIGHT
);
245 maHintText
.SetPosSizePixel( TEXTFIELDSTARTX
, nY
, aOutputSize
.Width()-2*TEXTFIELDSTARTX
, aOutputSize
.Height()-nY
);
246 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.") );
249 // Help the user with some default values
250 ::rtl::OUString aTempDirURL
;
251 ::osl::File::getTempDirURL( aTempDirURL
);
252 INetURLObject
aURLObj( aTempDirURL
);
253 aURLObj
.insertName( "nss", true );
254 ::rtl::OUString aNSSFolder
= aURLObj
.getFSysPath( INetURLObject::FSYS_DETECT
);
255 maEditXMLFileName
.SetText( aNSSFolder
+ "demo-sample.xml" );
256 maEditBINFileName
.SetText( aNSSFolder
+ "demo-sample.gif" );
257 maEditDOCFileName
.SetText( aNSSFolder
+ "demo-sample.sxw" );
258 maEditSIGFileName
.SetText( aNSSFolder
+ "demo-result.xml" );
259 maEditTokenName
.SetText( aNSSFolder
);
262 maEditTokenName
.SetText( OUString() );
263 maEditTokenName
.Disable();
264 maCryptoCheckBox
.Disable();
269 IMPL_LINK_NOARG(MyWin
, CryptoCheckBoxHdl
)
271 if ( maCryptoCheckBox
.IsChecked() )
273 maEditTokenName
.Disable();
274 maFixedTextTokenName
.Disable();
278 maEditTokenName
.Enable();
279 maFixedTextTokenName
.Enable();
284 IMPL_LINK_NOARG(MyWin
, DigitalSignaturesWithServiceHdl
)
286 rtl::OUString aDocFileName
= maEditDOCFileName
.GetText();
287 uno::Reference
< embed::XStorage
> xStore
= ::comphelper::OStorageHelper::GetStorageFromURL(
288 aDocFileName
, embed::ElementModes::READWRITE
, comphelper::getProcessServiceFactory() );
290 uno::Reference
< security::XDocumentDigitalSignatures
> xD(
291 security::DocumentDigitalSignatures::createDefault(comphelper::getProcessComponentContext() );
292 xD
->signDocumentContent( xStore
, NULL
);
298 IMPL_LINK_NOARG(MyWin
, VerifyDigitalSignaturesHdl
)
300 rtl::OUString aDocFileName
= maEditDOCFileName
.GetText();
301 uno::Reference
< embed::XStorage
> xStore
= ::comphelper::OStorageHelper::GetStorageFromURL(
302 aDocFileName
, embed::ElementModes::READWRITE
, comphelper::getProcessServiceFactory() );
304 uno::Reference
< security::XDocumentDigitalSignatures
> xD(
305 security::DocumentDigitalSignatures::createDefault(comphelper::getProcessComponentContext()) );
306 uno::Sequence
< security::DocumentSignatureInformation
> aInfos
= xD
->verifyDocumentContentSignatures( xStore
, NULL
);
307 int nInfos
= aInfos
.getLength();
308 for ( int n
= 0; n
< nInfos
; n
++ )
310 security::DocumentSignatureInformation
& rInf
= aInfos
[n
];
311 OUStringBuffer
aText( "The document is signed by\n\n " );
312 aText
.append( rInf
.Signer
->getSubjectName() );
313 aText
.append( "\n\n The signature is " );
314 if ( !rInf
.SignatureIsValid
)
315 aText
.append( "NOT " );
316 aText
.append( "valid" );
317 InfoBox( this, aText
.makeStringAndClear() ).Execute();
323 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */