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 .
21 #include <vcl/event.hxx>
22 #include <vcl/svapp.hxx>
23 #include <vcl/wrkwin.hxx>
24 #include <vcl/msgbox.hxx>
25 #include <vcl/fixed.hxx>
26 #include <vcl/edit.hxx>
27 #include <vcl/button.hxx>
28 #include <vcl/lstbox.hxx>
29 #include <svtools/filectrl.hxx>
30 #include <tools/urlobj.hxx>
31 #include <osl/file.hxx>
33 #include <svtools/docpasswdrequest.hxx>
35 #include <comphelper/processfactory.hxx>
36 #include <cppuhelper/servicefactory.hxx>
37 #include <cppuhelper/bootstrap.hxx>
38 #include <unotools/streamhelper.hxx>
40 // Will be in comphelper if CWS MAV09 is integrated
41 #include <comphelper/storagehelper.hxx>
43 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
45 #include <xmlsecurity/xmlsignaturehelper.hxx>
46 #include <xmlsecurity/digitalsignaturesdialog.hxx>
47 #include <xmlsecurity/certificatechooser.hxx>
48 #include <xmlsecurity/biginteger.hxx>
50 #include <com/sun/star/security/DocumentDigitalSignatures.hpp>
56 #define TEXTFIELDWIDTH 80
57 #define TEXTFIELDSTARTX 10
62 #define FIXEDLINEHEIGHT 15
64 #define BUTTONWIDTH 50
65 #define BUTTONHEIGHT 22
66 #define BUTTONSPACE 20
70 uno::Reference
< lang::XMultiServiceFactory
> xMSF
;
73 uno::Reference
< uno::XComponentContext
> xCtx( cppu::defaultBootstrap_InitialComponentContext() );
76 OSL_FAIL( "Error creating initial component context!" );
80 xMSF
= uno::Reference
< lang::XMultiServiceFactory
>(xCtx
->getServiceManager(), uno::UNO_QUERY
);
84 OSL_FAIL( "No service manager!" );
88 catch ( uno::Exception
const & )
90 OSL_FAIL( "Exception during creation of initial component context!" );
93 comphelper::setProcessServiceFactory( xMSF
);
102 class MyWin
: public WorkWindow
105 FixedLine maTokenLine
;
106 CheckBox maCryptoCheckBox
;
107 FixedText maFixedTextTokenName
;
108 FileControl maEditTokenName
;
109 FixedLine maTest1Line
;
110 FixedText maFixedTextXMLFileName
;
111 FileControl maEditXMLFileName
;
112 FixedText maFixedTextBINFileName
;
113 FileControl maEditBINFileName
;
114 FixedText maFixedTextSIGFileName
;
115 FileControl maEditSIGFileName
;
116 PushButton maSignButton
;
117 PushButton maVerifyButton
;
118 FixedLine maTest2Line
;
119 FixedText maFixedTextDOCFileName
;
120 FileControl maEditDOCFileName
;
121 PushButton maDigitalSignaturesButton
;
122 PushButton maVerifyDigitalSignaturesButton
;
123 FixedLine maHintLine
;
124 FixedText maHintText
;
126 DECL_LINK( CryptoCheckBoxHdl
, CheckBox
* );
127 DECL_LINK( SignButtonHdl
, Button
* );
128 DECL_LINK( VerifyButtonHdl
, Button
* );
129 DECL_LINK( DigitalSignaturesWithServiceHdl
, Button
* );
130 DECL_LINK( VerifyDigitalSignaturesHdl
, Button
* );
131 DECL_LINK( DigitalSignaturesWithTokenHdl
, Button
* );
132 DECL_LINK( StartVerifySignatureHdl
, void* );
135 MyWin( vcl::Window
* pParent
, WinBits nWinStyle
);
141 MyWin
aMainWin( NULL
, WB_APP
| WB_STDWORK
| WB_3DLOOK
);
144 Application::Execute();
147 MyWin::MyWin( vcl::Window
* pParent
, WinBits nWinStyle
) :
148 WorkWindow( pParent
, nWinStyle
),
153 maFixedTextXMLFileName( this ),
154 maEditXMLFileName( this, WB_BORDER
),
155 maFixedTextBINFileName( this ),
156 maEditBINFileName( this, WB_BORDER
),
157 maFixedTextSIGFileName( this ),
158 maEditSIGFileName( this, WB_BORDER
),
159 maFixedTextTokenName( this ),
160 maEditTokenName( this, WB_BORDER
),
161 maFixedTextDOCFileName( this ),
162 maEditDOCFileName( this, WB_BORDER
),
163 maSignButton( this ),
164 maVerifyButton( this ),
165 maDigitalSignaturesButton( this ),
166 maVerifyDigitalSignaturesButton( this ),
167 maHintText( this, WB_WORDBREAK
),
168 maCryptoCheckBox( this )
171 Size
aOutputSize( 400, 400 );
172 SetOutputSizePixel( aOutputSize
);
173 SetText( OUString("XML Signature Test") );
177 maTokenLine
.SetPosSizePixel( TEXTFIELDSTARTX
, nY
, aOutputSize
.Width()-2*TEXTFIELDSTARTX
, FIXEDLINEHEIGHT
);
178 maTokenLine
.SetText( OUString("Crypto Settings") );
181 nY
+= EDITHEIGHT
*3/2;
183 maCryptoCheckBox
.SetPosSizePixel( TEXTFIELDSTARTX
, nY
, aOutputSize
.Width()-2*TEXTFIELDSTARTX
, FIXEDLINEHEIGHT
);
184 maCryptoCheckBox
.SetText( OUString("Use Default Token (NSS option only)") );
185 maCryptoCheckBox
.Check( sal_True
);
186 maEditTokenName
.Disable();
187 maFixedTextTokenName
.Disable();
188 maCryptoCheckBox
.SetClickHdl( LINK( this, MyWin
, CryptoCheckBoxHdl
) );
189 maCryptoCheckBox
.Show();
193 maFixedTextTokenName
.SetPosSizePixel( TEXTFIELDSTARTX
, nY
, TEXTFIELDWIDTH
, EDITHEIGHT
);
194 maFixedTextTokenName
.SetText( OUString("Crypto Token:") );
195 maFixedTextTokenName
.Show();
197 maEditTokenName
.SetPosSizePixel( TEXTFIELDSTARTX
+TEXTFIELDWIDTH
, nY
, EDITWIDTH
, EDITHEIGHT
);
198 maEditTokenName
.Show();
202 maTest2Line
.SetPosSizePixel( TEXTFIELDSTARTX
, nY
, aOutputSize
.Width()-2*TEXTFIELDSTARTX
, FIXEDLINEHEIGHT
);
203 maTest2Line
.SetText( OUString("Test Office Document") );
206 nY
+= EDITHEIGHT
*3/2;
208 maFixedTextDOCFileName
.SetPosSizePixel( TEXTFIELDSTARTX
, nY
, TEXTFIELDWIDTH
, EDITHEIGHT
);
209 maFixedTextDOCFileName
.SetText( OUString("Office File:") );
210 maFixedTextDOCFileName
.Show();
212 maEditDOCFileName
.SetPosSizePixel( TEXTFIELDSTARTX
+TEXTFIELDWIDTH
, nY
, EDITWIDTH
, EDITHEIGHT
);
213 maEditDOCFileName
.Show();
217 maDigitalSignaturesButton
.SetPosSizePixel( TEXTFIELDSTARTX
, nY
, BUTTONWIDTH
*2, BUTTONHEIGHT
);
218 maDigitalSignaturesButton
.SetText( OUString("Digital Signatures...") );
219 maDigitalSignaturesButton
.SetClickHdl( LINK( this, MyWin
, DigitalSignaturesWithServiceHdl
) );
220 maDigitalSignaturesButton
.Show();
222 maVerifyDigitalSignaturesButton
.SetPosSizePixel( TEXTFIELDSTARTX
+BUTTONWIDTH
*2+BUTTONSPACE
, nY
, BUTTONWIDTH
*2, BUTTONHEIGHT
);
223 maVerifyDigitalSignaturesButton
.SetText( OUString("Verify Signatures") );
224 maVerifyDigitalSignaturesButton
.SetClickHdl( LINK( this, MyWin
, VerifyDigitalSignaturesHdl
) );
225 maVerifyDigitalSignaturesButton
.Show();
229 maHintLine
.SetPosSizePixel( TEXTFIELDSTARTX
, nY
, aOutputSize
.Width()-2*TEXTFIELDSTARTX
, FIXEDLINEHEIGHT
);
234 maHintText
.SetPosSizePixel( TEXTFIELDSTARTX
, nY
, aOutputSize
.Width()-2*TEXTFIELDSTARTX
, aOutputSize
.Height()-nY
);
235 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.") );
238 // Help the user with some default values
239 OUString aTempDirURL
;
240 ::osl::File::getTempDirURL( aTempDirURL
);
241 INetURLObject
aURLObj( aTempDirURL
);
242 aURLObj
.insertName( "nss", true );
243 OUString aNSSFolder
= aURLObj
.getFSysPath( INetURLObject::FSYS_DETECT
);
244 maEditXMLFileName
.SetText( aNSSFolder
+ "demo-sample.xml" );
245 maEditBINFileName
.SetText( aNSSFolder
+ "demo-sample.gif" );
246 maEditDOCFileName
.SetText( aNSSFolder
+ "demo-sample.sxw" );
247 maEditSIGFileName
.SetText( aNSSFolder
+ "demo-result.xml" );
248 maEditTokenName
.SetText( aNSSFolder
);
251 maEditTokenName
.SetText( OUString() );
252 maEditTokenName
.Disable();
253 maCryptoCheckBox
.Disable();
258 IMPL_LINK_NOARG(MyWin
, CryptoCheckBoxHdl
)
260 if ( maCryptoCheckBox
.IsChecked() )
262 maEditTokenName
.Disable();
263 maFixedTextTokenName
.Disable();
267 maEditTokenName
.Enable();
268 maFixedTextTokenName
.Enable();
273 IMPL_LINK_NOARG(MyWin
, DigitalSignaturesWithServiceHdl
)
275 OUString aDocFileName
= maEditDOCFileName
.GetText();
276 uno::Reference
< embed::XStorage
> xStore
= ::comphelper::OStorageHelper::GetStorageFromURL(
277 aDocFileName
, embed::ElementModes::READWRITE
, comphelper::getProcessComponentContext() );
279 uno::Reference
< security::XDocumentDigitalSignatures
> xD(
280 security::DocumentDigitalSignatures::createDefault(comphelper::getProcessComponentContext()) );
281 xD
->signDocumentContent( xStore
, NULL
);
286 IMPL_LINK_NOARG(MyWin
, VerifyDigitalSignaturesHdl
)
288 OUString aDocFileName
= maEditDOCFileName
.GetText();
289 uno::Reference
< embed::XStorage
> xStore
= ::comphelper::OStorageHelper::GetStorageFromURL(
290 aDocFileName
, embed::ElementModes::READWRITE
, comphelper::getProcessServiceFactory() );
292 uno::Reference
< security::XDocumentDigitalSignatures
> xD(
293 security::DocumentDigitalSignatures::createDefault(comphelper::getProcessComponentContext()) );
294 uno::Sequence
< security::DocumentSignatureInformation
> aInfos
= xD
->verifyDocumentContentSignatures( xStore
, NULL
);
295 int nInfos
= aInfos
.getLength();
296 for ( int n
= 0; n
< nInfos
; n
++ )
298 security::DocumentSignatureInformation
& rInf
= aInfos
[n
];
299 OUString aText
= "The document is signed by\n\n " + rInf
.Signer
->getSubjectName()
300 + "\n\n The signature is ";
301 if ( !rInf
.SignatureIsValid
)
302 aText
.append( "NOT " );
303 aText
.append( "valid" );
304 ScopedVclPtr
<InfoBox
>::Create( this, aText
)->Execute();
310 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */