Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
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/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
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 .
18 : : */
19 : :
20 : : #include <com/sun/star/xml/crypto/XSecurityEnvironment.hpp>
21 : : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
22 : : #include <com/sun/star/security/XDocumentDigitalSignatures.hpp>
23 : : #include <comphelper/sequence.hxx>
24 : : #include "comphelper/documentconstants.hxx"
25 : : #include <comphelper/processfactory.hxx>
26 : : #include <sal/macros.h>
27 : :
28 : : #include <vcl/msgbox.hxx>
29 : : #include <com/sun/star/security/NoPasswordException.hpp>
30 : :
31 : : using namespace ::com::sun::star::security;
32 : :
33 : : #include "ids.hrc"
34 : : #include "secmacrowarnings.hxx"
35 : : #include "secmacrowarnings.hrc"
36 : :
37 : : #ifdef _MSC_VER
38 : : #pragma warning (disable : 4355) // 4355: this used in initializer-list
39 : : #endif
40 : :
41 : : using namespace ::com::sun::star;
42 : : using namespace ::com::sun::star;
43 : :
44 : :
45 : : // HACK!!! copied from xmlsecurity/source/dialog/resourcemanager.cxx
46 : :
47 : : namespace
48 : : {
49 : 0 : String GetContentPart( const String& _rRawString, const String& _rPartId )
50 : : {
51 : 0 : String s;
52 : :
53 [ # # ]: 0 : xub_StrLen nContStart = _rRawString.Search( _rPartId );
54 [ # # ]: 0 : if( nContStart != STRING_NOTFOUND )
55 : : {
56 : 0 : nContStart = nContStart + _rPartId.Len();
57 : 0 : ++nContStart; // now it's start of content, directly after Id
58 : :
59 [ # # ]: 0 : xub_StrLen nContEnd = _rRawString.Search( sal_Unicode( ',' ), nContStart );
60 : :
61 [ # # ][ # # ]: 0 : s = String( _rRawString, nContStart, nContEnd - nContStart );
[ # # ]
62 : : }
63 : :
64 : 0 : return s;
65 : : }
66 : : }
67 : :
68 : :
69 : 0 : MacroWarning::MacroWarning( Window* _pParent, bool _bWithSignatures, ResMgr& rResMgr )
70 : : :ModalDialog ( _pParent, ResId( RID_XMLSECDLG_MACROWARN, rResMgr ) )
71 : : ,mpInfos ( NULL )
72 : : ,maSymbolImg ( this, ResId( IMG_SYMBOL, rResMgr ) )
73 : : ,maDocNameFI ( this, ResId( FI_DOCNAME, rResMgr ) )
74 : : ,maDescr1aFI ( this, ResId( FI_DESCR1A, rResMgr ) )
75 : : ,maDescr1bFI ( this, ResId( FI_DESCR1B, rResMgr ) )
76 : : ,maSignsFI ( this, ResId( FI_SIGNS, rResMgr ) )
77 : : ,maViewSignsBtn ( this, ResId( PB_VIEWSIGNS, rResMgr ) )
78 : : ,maDescr2FI ( this, ResId( FI_DESCR2, rResMgr ) )
79 : : ,maAlwaysTrustCB ( this, ResId( CB_ALWAYSTRUST, rResMgr ) )
80 : : ,maBottomSepFL ( this, ResId( FL_BOTTOM_SEP, rResMgr ) )
81 : : ,maEnableBtn ( this, ResId( PB_ENABLE, rResMgr ) )
82 : : ,maDisableBtn ( this, ResId( PB_DISABLE, rResMgr ) )
83 : : ,maHelpBtn ( this, ResId( BTN_HELP, rResMgr ) )
84 : : ,mbSignedMode ( true )
85 : : ,mbShowSignatures ( _bWithSignatures )
86 [ # # ][ # # ]: 0 : ,mnActSecLevel ( 0 )
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
87 : : {
88 [ # # ]: 0 : FreeResource();
89 : :
90 [ # # ]: 0 : InitControls();
91 : :
92 [ # # ]: 0 : maDisableBtn.SetClickHdl( LINK( this, MacroWarning, DisableBtnHdl ) );
93 [ # # ]: 0 : maEnableBtn.SetClickHdl( LINK( this, MacroWarning, EnableBtnHdl ) );
94 [ # # ]: 0 : maDisableBtn.GrabFocus(); // Default button, but focus is on view button
95 : 0 : }
96 : :
97 [ # # ][ # # ]: 0 : MacroWarning::~MacroWarning()
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
98 : : {
99 [ # # ]: 0 : }
100 : :
101 : 0 : short MacroWarning::Execute()
102 : : {
103 : 0 : FitControls();
104 : 0 : return ModalDialog::Execute();
105 : : }
106 : :
107 : 0 : void MacroWarning::SetDocumentURL( const String& rDocURL )
108 : : {
109 : 0 : maDocNameFI.SetText( rDocURL );
110 : 0 : }
111 : :
112 : 0 : IMPL_LINK_NOARG(MacroWarning, ViewSignsBtnHdl)
113 : : {
114 : : DBG_ASSERT( mxCert.is(), "*MacroWarning::ViewSignsBtnHdl(): no certificate set!" );
115 : :
116 [ # # ]: 0 : uno::Sequence< uno::Any > aArgs( 1 );
117 [ # # ][ # # ]: 0 : aArgs[0] = uno::makeAny( maODFVersion );
118 : : uno::Reference< security::XDocumentDigitalSignatures > xD(
119 [ # # ][ # # ]: 0 : comphelper::getProcessServiceFactory()->createInstanceWithArguments( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.security.DocumentDigitalSignatures" ) ), aArgs ), uno::UNO_QUERY );
[ # # ][ # # ]
[ # # ]
120 [ # # ]: 0 : if( xD.is() )
121 : : {
122 [ # # ]: 0 : if( mxCert.is() )
123 [ # # ][ # # ]: 0 : xD->showCertificate( mxCert );
124 [ # # ]: 0 : else if( mxStore.is() )
125 [ # # ][ # # ]: 0 : xD->showScriptingContentSignatures( mxStore, uno::Reference< io::XInputStream >() );
126 : : }
127 : :
128 [ # # ]: 0 : return 0;
129 : : }
130 : :
131 : 0 : IMPL_LINK_NOARG(MacroWarning, EnableBtnHdl)
132 : : {
133 [ # # ][ # # ]: 0 : if( mbSignedMode && maAlwaysTrustCB.IsChecked() )
[ # # ]
134 : : { // insert path into trusted path list
135 [ # # ]: 0 : uno::Sequence< uno::Any > aArgs( 1 );
136 [ # # ][ # # ]: 0 : aArgs[0] = uno::makeAny( maODFVersion );
137 : : uno::Reference< security::XDocumentDigitalSignatures > xD(
138 [ # # ][ # # ]: 0 : comphelper::getProcessServiceFactory()->createInstanceWithArguments( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.security.DocumentDigitalSignatures" ) ), aArgs ), uno::UNO_QUERY );
[ # # ][ # # ]
[ # # ]
139 [ # # ]: 0 : if( xD.is() )
140 : : {
141 [ # # ]: 0 : if( mxCert.is() )
142 [ # # ][ # # ]: 0 : xD->addAuthorToTrustedSources( mxCert );
143 [ # # ]: 0 : else if( mxStore.is() )
144 : : {
145 : : DBG_ASSERT( mpInfos, "-MacroWarning::EnableBtnHdl(): no infos, search in nirvana..." );
146 : :
147 : 0 : sal_Int32 nCnt = mpInfos->getLength();
148 [ # # ]: 0 : for( sal_Int32 i = 0 ; i < nCnt ; ++i )
149 [ # # ][ # # ]: 0 : xD->addAuthorToTrustedSources( (*mpInfos)[ i ].Signer );
150 : : }
151 [ # # ]: 0 : }
152 : : }
153 : :
154 : 0 : EndDialog( RET_OK );
155 : 0 : return 0;
156 : : }
157 : :
158 : 0 : IMPL_LINK_NOARG(MacroWarning, DisableBtnHdl)
159 : : {
160 : 0 : EndDialog( RET_CANCEL );
161 : 0 : return 0;
162 : : }
163 : :
164 : 0 : IMPL_LINK_NOARG(MacroWarning, AlwaysTrustCheckHdl)
165 : : {
166 [ # # ][ # # ]: 0 : bool bEnable = ( mnActSecLevel < 2 || maAlwaysTrustCB.IsChecked() );
167 : 0 : maEnableBtn.Enable( bEnable );
168 : 0 : maDisableBtn.Enable( !maAlwaysTrustCB.IsChecked() );
169 : :
170 : 0 : return 0;
171 : : }
172 : :
173 : 0 : void MacroWarning::InitControls()
174 : : {
175 : : // set warning image
176 [ # # ]: 0 : Image aImg( WarningBox::GetStandardImage() );
177 [ # # ]: 0 : maSymbolImg.SetImage( aImg );
178 [ # # ][ # # ]: 0 : maSymbolImg.SetSizePixel( aImg.GetSizePixel() );
179 : : // set bold font and path ellipsis for docname fixedtext
180 [ # # ]: 0 : Font aTmpFont = maDocNameFI.GetControlFont();
181 [ # # ]: 0 : aTmpFont.SetWeight( WEIGHT_BOLD );
182 [ # # ]: 0 : maDocNameFI.SetControlFont( aTmpFont );
183 [ # # ]: 0 : WinBits nStyle = maDocNameFI.GetStyle();
184 : 0 : nStyle |= WB_PATHELLIPSIS;
185 [ # # ]: 0 : maDocNameFI.SetStyle( nStyle );
186 : : // show signature controls?
187 [ # # ]: 0 : if( mbShowSignatures )
188 : : {
189 [ # # ]: 0 : maDescr1bFI.Hide();
190 [ # # ]: 0 : maViewSignsBtn.SetClickHdl( LINK( this, MacroWarning, ViewSignsBtnHdl ) );
191 [ # # ]: 0 : maViewSignsBtn.Disable(); // default
192 [ # # ]: 0 : maAlwaysTrustCB.SetClickHdl( LINK( this, MacroWarning, AlwaysTrustCheckHdl ) );
193 : :
194 [ # # ][ # # ]: 0 : mnActSecLevel = SvtSecurityOptions().GetMacroSecurityLevel();
[ # # ]
195 [ # # ]: 0 : if ( mnActSecLevel >= 2 )
196 [ # # ]: 0 : maEnableBtn.Disable();
197 : : }
198 : : else
199 : : {
200 [ # # ]: 0 : maDescr1aFI.Hide();
201 [ # # ]: 0 : maSignsFI.Hide();
202 [ # # ]: 0 : maViewSignsBtn.Hide();
203 [ # # ]: 0 : maAlwaysTrustCB.Hide();
204 : :
205 : : // move hint up to position of signer list
206 [ # # ][ # # ]: 0 : maDescr2FI.SetPosPixel( maSignsFI.GetPosPixel() );
207 : : }
208 : : // without signature controls could be smaller
209 [ # # ]: 0 : if ( !mbShowSignatures )
210 : : {
211 [ # # ]: 0 : Point aPos = maDescr2FI.GetPosPixel();
212 [ # # ]: 0 : aPos.Y() += maDescr2FI.GetSizePixel().Height();
213 [ # # ]: 0 : aPos.Y() += LogicToPixel( Size( 3, 3 ) ).Height();
214 [ # # ]: 0 : long nDelta = maBottomSepFL.GetPosPixel().Y() - aPos.Y();
215 : : Window* pWins[] =
216 : : {
217 : : &maBottomSepFL, &maEnableBtn, &maDisableBtn, &maHelpBtn
218 : 0 : };
219 : 0 : Window** pCurrent = pWins;
220 [ # # ]: 0 : for ( sal_uInt32 i = 0; i < sizeof( pWins ) / sizeof( pWins[ 0 ] ); ++i, ++pCurrent )
221 : : {
222 [ # # ]: 0 : Point aNewPos = (*pCurrent)->GetPosPixel();
223 : 0 : aNewPos.Y() -= nDelta;
224 [ # # ]: 0 : (*pCurrent)->SetPosPixel( aNewPos );
225 : : }
226 : :
227 [ # # ]: 0 : Size aDlgSz = GetSizePixel();
228 : 0 : aDlgSz.Height() -= nDelta;
229 [ # # ]: 0 : SetSizePixel( aDlgSz );
230 : : }
231 : :
232 : : // check if some buttontexts are to wide
233 [ # # ]: 0 : String sText = maViewSignsBtn.GetText();
234 [ # # ]: 0 : long nTxtW = maViewSignsBtn.GetTextWidth( sText );
235 : 0 : const long nOffset = 12;
236 [ # # ][ # # ]: 0 : if ( sText.Search( '~' ) == STRING_NOTFOUND )
237 : 0 : nTxtW += nOffset;
238 [ # # ]: 0 : long nBtnW = maViewSignsBtn.GetSizePixel().Width();
239 [ # # ]: 0 : if ( nTxtW >= nBtnW )
240 : : {
241 : : // broaden the button
242 : 0 : long nDelta = Max( nTxtW - nBtnW, nOffset/3 );
243 [ # # ]: 0 : Size aNewSize = maViewSignsBtn.GetSizePixel();
244 : 0 : aNewSize.Width() += nDelta;
245 [ # # ]: 0 : maViewSignsBtn.SetSizePixel( aNewSize );
246 : : // and give it a new position
247 [ # # ]: 0 : Point aNewPos = maViewSignsBtn.GetPosPixel();
248 : 0 : aNewPos.X() -= nDelta;
249 [ # # ]: 0 : maViewSignsBtn.SetPosPixel( aNewPos );
250 : : // the the left fixedtext must be smaller
251 [ # # ]: 0 : aNewSize = maSignsFI.GetSizePixel();
252 : 0 : aNewSize.Width() -= nDelta;
253 [ # # ]: 0 : maSignsFI.SetSizePixel( aNewSize );
254 : : }
255 : : // if the button text (we compare with the longest of both) is too wide, then broaden the buttons
256 [ # # ]: 0 : String sText1 = maEnableBtn.GetText();
257 [ # # ]: 0 : long nTxtW1 = maEnableBtn.GetTextWidth( sText1 );
258 [ # # ][ # # ]: 0 : if ( sText1.Search( '~' ) == STRING_NOTFOUND )
259 : 0 : nTxtW1 += nOffset;
260 [ # # ]: 0 : String sText2 = maDisableBtn.GetText();
261 [ # # ]: 0 : long nTxtW2 = maDisableBtn.GetTextWidth( sText2 );
262 [ # # ][ # # ]: 0 : if ( sText2.Search( '~' ) == STRING_NOTFOUND )
263 : 0 : nTxtW2 += nOffset;
264 : 0 : nTxtW = Max( nTxtW1, nTxtW2 );
265 [ # # ]: 0 : nBtnW = maEnableBtn.GetSizePixel().Width();
266 [ # # ]: 0 : if ( nTxtW > nBtnW )
267 : : {
268 : : // broaden both buttons
269 : 0 : long nDelta = nTxtW - nBtnW;
270 [ # # ]: 0 : Size aNewSize = maEnableBtn.GetSizePixel();
271 : 0 : aNewSize.Width() += nDelta;
272 [ # # ]: 0 : maEnableBtn.SetSizePixel( aNewSize );
273 [ # # ]: 0 : maDisableBtn.SetSizePixel( aNewSize );
274 : : // and give them a new position
275 [ # # ]: 0 : Point aNewPos = maEnableBtn.GetPosPixel();
276 : 0 : aNewPos.X() -= (2*nDelta);
277 [ # # ]: 0 : maEnableBtn.SetPosPixel( aNewPos );
278 [ # # ]: 0 : aNewPos = maDisableBtn.GetPosPixel();
279 : 0 : aNewPos.X() -= nDelta;
280 [ # # ]: 0 : maDisableBtn.SetPosPixel( aNewPos );
281 [ # # ][ # # ]: 0 : }
[ # # ][ # # ]
[ # # ]
282 : 0 : }
283 : :
284 : 0 : void MacroWarning::FitControls()
285 : : {
286 [ # # ][ # # ]: 0 : Size a3Size = LogicToPixel( Size( 3, 3 ), MAP_APPFONT );
[ # # ]
287 : 0 : Size aNewSize, aMinSize;
288 : 0 : long nTxtH = 0;
289 : 0 : long nCtrlH = 0;
290 : 0 : long nDelta = 0;
291 : :
292 [ # # ]: 0 : if ( mbShowSignatures )
293 : : {
294 [ # # ][ # # ]: 0 : aMinSize = maSignsFI.CalcMinimumSize( maSignsFI.GetSizePixel().Width() );
295 [ # # ]: 0 : nTxtH = Max( aMinSize.Height(), maViewSignsBtn.GetSizePixel().Height() );
296 : 0 : nTxtH += a3Size.Height() / 2;
297 [ # # ]: 0 : nCtrlH = maSignsFI.GetSizePixel().Height();
298 : 0 : nDelta = Max( nCtrlH - nTxtH, static_cast< long >( -100 ) ); // not too large
299 [ # # ]: 0 : aNewSize = maSignsFI.GetSizePixel();
300 : 0 : aNewSize.Height() -= nDelta;
301 [ # # ]: 0 : maSignsFI.SetSizePixel( aNewSize );
302 : : }
303 : :
304 [ # # ][ # # ]: 0 : aMinSize = maDescr2FI.CalcMinimumSize( maDescr2FI.GetSizePixel().Width() );
305 : 0 : nTxtH = aMinSize.Height();
306 [ # # ]: 0 : nCtrlH = maDescr2FI.GetSizePixel().Height();
307 : 0 : long nDelta2 = ( nCtrlH - nTxtH );
308 [ # # ]: 0 : aNewSize = maDescr2FI.GetSizePixel();
309 : 0 : aNewSize.Height() -= nDelta2;
310 [ # # ]: 0 : maDescr2FI.SetSizePixel( aNewSize );
311 : :
312 : : // new position for the succeeding windows
313 : : Window* pWins[] =
314 : : {
315 : : &maDescr2FI, &maAlwaysTrustCB, &maBottomSepFL, &maEnableBtn, &maDisableBtn, &maHelpBtn
316 : 0 : };
317 : 0 : Window** pCurrent = pWins;
318 [ # # ]: 0 : for ( sal_uInt32 i = 0; i < sizeof( pWins ) / sizeof( pWins[ 0 ] ); ++i, ++pCurrent )
319 : : {
320 [ # # ]: 0 : Point aNewPos = (*pCurrent)->GetPosPixel();
321 : 0 : aNewPos.Y() -= nDelta;
322 [ # # ]: 0 : (*pCurrent)->SetPosPixel( aNewPos );
323 : :
324 [ # # ]: 0 : if ( *pCurrent == &maDescr2FI )
325 : 0 : nDelta += nDelta2;
326 : : }
327 : :
328 : : // new size of the dialog
329 [ # # ]: 0 : aNewSize = GetSizePixel();
330 : 0 : aNewSize.Height() -= nDelta;
331 [ # # ]: 0 : SetSizePixel( aNewSize );
332 : 0 : }
333 : :
334 : 0 : void MacroWarning::SetStorage( const cssu::Reference < css::embed::XStorage >& rxStore,
335 : : const ::rtl::OUString& aODFVersion,
336 : : const cssu::Sequence< security::DocumentSignatureInformation >& rInfos )
337 : : {
338 : 0 : mxStore = rxStore;
339 : 0 : maODFVersion = aODFVersion;
340 : 0 : sal_Int32 nCnt = rInfos.getLength();
341 [ # # ][ # # ]: 0 : if( mxStore.is() && nCnt > 0 )
[ # # ]
342 : : {
343 : 0 : mpInfos = &rInfos;
344 : 0 : rtl::OUString aCN_Id("CN");
345 [ # # ]: 0 : String s;
346 [ # # ][ # # ]: 0 : s = GetContentPart( rInfos[ 0 ].Signer->getSubjectName(), aCN_Id );
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
347 : :
348 [ # # ]: 0 : for( sal_Int32 i = 1 ; i < nCnt ; ++i )
349 : : {
350 [ # # ]: 0 : s.AppendAscii( "\n" );
351 [ # # ][ # # ]: 0 : s += GetContentPart( rInfos[ i ].Signer->getSubjectName(), aCN_Id );
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
352 : : }
353 : :
354 [ # # ]: 0 : maSignsFI.SetText( s );
355 [ # # ][ # # ]: 0 : maViewSignsBtn.Enable();
356 : : }
357 : 0 : }
358 : :
359 : 0 : void MacroWarning::SetCertificate( const cssu::Reference< css::security::XCertificate >& _rxCert )
360 : : {
361 : 0 : mxCert = _rxCert;
362 [ # # ]: 0 : if( mxCert.is() )
363 : : {
364 : 0 : rtl::OUString aCN_Id("CN");
365 [ # # ]: 0 : String s;
366 [ # # ][ # # ]: 0 : s = GetContentPart( mxCert->getSubjectName(), aCN_Id );
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
367 [ # # ]: 0 : maSignsFI.SetText( s );
368 [ # # ][ # # ]: 0 : maViewSignsBtn.Enable();
369 : : }
370 : 0 : }
371 : :
372 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|