Coverage Report

Created: 2026-05-16 09:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/uui/source/secmacrowarnings.cxx
Line
Count
Source
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/security/DocumentDigitalSignatures.hpp>
22
#include <comphelper/processfactory.hxx>
23
#include <comphelper/xmlsechelper.hxx>
24
#include <vcl/svapp.hxx>
25
#include <vcl/vclenum.hxx>
26
#include <vcl/weld/Builder.hxx>
27
#include <vcl/weld/MessageDialog.hxx>
28
#include <osl/file.hxx>
29
#include <rtl/ustrbuf.hxx>
30
#include <tools/datetime.hxx>
31
#include <tools/debug.hxx>
32
#include <unotools/datetime.hxx>
33
#include <unotools/resmgr.hxx>
34
#include <unotools/securityoptions.hxx>
35
#include <tools/urlobj.hxx>
36
37
#include "secmacrowarnings.hxx"
38
39
#include <strings.hrc>
40
41
using namespace ::com::sun::star::security;
42
using namespace ::com::sun::star;
43
using namespace comphelper;
44
45
MacroWarning::MacroWarning(weld::Window* pParent, bool _bWithSignatures)
46
0
    : MessageDialogController(pParent, u"uui/ui/macrowarnmedium.ui"_ustr, u"MacroWarnMedium"_ustr, u"grid"_ustr)
47
0
    , mxGrid(m_xBuilder->weld_widget(u"grid"_ustr))
48
0
    , mxSignsFI(m_xBuilder->weld_label(u"signsLabel"_ustr))
49
0
    , mxNotYetValid(m_xBuilder->weld_label(u"certNotYetValidLabel"_ustr))
50
0
    , mxNoLongerValid(m_xBuilder->weld_label(u"certNoLongerValidLabel"_ustr))
51
0
    , mxViewSignsBtn(m_xBuilder->weld_button(u"viewSignsButton"_ustr))
52
0
    , mxViewCertBtn(m_xBuilder->weld_button(u"viewCertButton"_ustr))
53
0
    , mxAlwaysTrustCB(m_xBuilder->weld_check_button(u"alwaysTrustCheckbutton"_ustr))
54
0
    , mxEnableBtn(m_xBuilder->weld_button(u"ok"_ustr))
55
0
    , mxDisableBtn(m_xBuilder->weld_button(u"cancel"_ustr))
56
0
    , mpInfos                ( nullptr )
57
0
    , mbShowSignatures       ( _bWithSignatures )
58
0
    , mnActSecLevel          ( 0 )
59
0
{
60
0
    InitControls();
61
62
0
    mxEnableBtn->connect_clicked(LINK(this, MacroWarning, EnableBtnHdl));
63
0
    mxDisableBtn->connect_clicked(LINK(this, MacroWarning, DisableBtnHdl));
64
0
    mxDisableBtn->grab_focus(); // Default button, but focus is on view button
65
0
    m_xDialog->SetInstallLOKNotifierHdl(LINK(this, MacroWarning, InstallLOKNotifierHdl));
66
0
}
67
68
IMPL_STATIC_LINK_NOARG(MacroWarning, InstallLOKNotifierHdl, void*, vcl::ILibreOfficeKitNotifier*)
69
0
{
70
0
    return GetpApp();
71
0
}
72
73
void MacroWarning::SetDocumentURL( const OUString& rDocURL )
74
0
{
75
0
    OUString aPath;
76
77
0
    osl::FileBase::getFileURLFromSystemPath(rDocURL, aPath);
78
0
    aPath = INetURLObject(aPath).GetLastName(INetURLObject::DecodeMechanism::Unambiguous);
79
0
    m_xDialog->set_primary_text(aPath);
80
0
}
81
82
IMPL_LINK_NOARG(MacroWarning, ViewSignsBtnHdl, weld::Button&, void)
83
0
{
84
0
    DBG_ASSERT( mxCert.is(), "*MacroWarning::ViewSignsBtnHdl(): no certificate set!" );
85
86
0
    uno::Reference< security::XDocumentDigitalSignatures > xD(
87
0
        security::DocumentDigitalSignatures::createWithVersion(comphelper::getProcessComponentContext(), maODFVersion));
88
0
    if( !xD.is() )
89
0
        return;
90
91
0
    xD->setParentWindow(m_xDialog->GetXWindow());
92
0
    if( mxCert.is() )
93
0
        xD->showCertificate( mxCert );
94
0
    else if( mxStore.is() )
95
0
        xD->showScriptingContentSignatures( mxStore, uno::Reference< io::XInputStream >() );
96
0
    else
97
0
        return;
98
99
0
    mxAlwaysTrustCB->set_sensitive(true);
100
0
    EnableOkBtn(true);
101
0
}
102
103
IMPL_LINK_NOARG(MacroWarning, EnableBtnHdl, weld::Button&, void)
104
0
{
105
0
    if (mxAlwaysTrustCB->get_active())
106
0
    {
107
0
        uno::Reference< security::XDocumentDigitalSignatures > xD(
108
0
            security::DocumentDigitalSignatures::createWithVersion(comphelper::getProcessComponentContext(), maODFVersion));
109
0
        xD->setParentWindow(m_xDialog->GetXWindow());
110
0
        if( mxCert.is() )
111
0
            xD->addAuthorToTrustedSources( mxCert );
112
0
        else if( mxStore.is() )
113
0
        {
114
0
            assert(mpInfos && "-MacroWarning::EnableBtnHdl(): no infos, search in nirvana...");
115
116
0
            sal_Int32   nCnt = mpInfos->getLength();
117
0
            for( sal_Int32 i = 0 ; i < nCnt ; ++i )
118
0
                xD->addAuthorToTrustedSources( (*mpInfos)[ i ].Signer );
119
0
        }
120
0
    }
121
0
    m_xDialog->response(RET_OK);
122
0
}
123
124
IMPL_LINK_NOARG(MacroWarning, DisableBtnHdl, weld::Button&, void)
125
0
{
126
0
    m_xDialog->response(RET_CANCEL);
127
0
}
128
129
IMPL_LINK_NOARG(MacroWarning, AlwaysTrustCheckHdl, weld::Toggleable&, void)
130
0
{
131
0
    const bool bEnable = (mnActSecLevel < 3 || mxAlwaysTrustCB->get_active());
132
0
    EnableOkBtn(bEnable);
133
0
    mxDisableBtn->set_sensitive(!mxAlwaysTrustCB->get_active());
134
0
}
135
136
void MacroWarning::InitControls()
137
0
{
138
    // show signature controls?
139
0
    if (mbShowSignatures)
140
0
    {
141
0
        mxAlwaysTrustCB->connect_toggled(LINK(this, MacroWarning, AlwaysTrustCheckHdl));
142
0
        mxAlwaysTrustCB->set_sensitive(false);
143
0
        mxViewSignsBtn->connect_clicked(LINK(this, MacroWarning, ViewSignsBtnHdl));
144
0
        mxViewSignsBtn->set_visible(false);
145
0
        mxViewCertBtn->connect_clicked(LINK(this, MacroWarning, ViewSignsBtnHdl));
146
0
        mxViewCertBtn->set_visible(false);
147
148
0
        mnActSecLevel = SvtSecurityOptions::GetMacroSecurityLevel();
149
0
        if ( mnActSecLevel >= 2 )
150
0
            EnableOkBtn(false);
151
0
    }
152
0
    else
153
0
    {
154
0
        mxGrid->hide();
155
0
    }
156
0
}
157
158
void MacroWarning::EnableOkBtn(bool bEnable)
159
0
{
160
0
    mxEnableBtn->set_sensitive(bEnable);
161
0
    std::locale aResLocale(Translate::Create("uui"));
162
0
    mxEnableBtn->set_tooltip_text(bEnable ? u""_ustr : Translate::get(STR_VERIFY_CERT, aResLocale));
163
0
}
164
165
void MacroWarning::SetStorage( const css::uno::Reference < css::embed::XStorage >& rxStore,
166
                               const OUString& aODFVersion,
167
                               const css::uno::Sequence< security::DocumentSignatureInformation >& rInfos )
168
0
{
169
0
    mxStore = rxStore;
170
0
    maODFVersion = aODFVersion;
171
0
    sal_Int32   nCnt = rInfos.getLength();
172
0
    if( !(mxStore.is() && nCnt > 0) )
173
0
        return;
174
175
0
    mpInfos = &rInfos;
176
0
    OUStringBuffer s(xmlsec::GetContentPart(rInfos[0].Signer->getSubjectName(),
177
0
                                            rInfos[0].Signer->getCertificateKind()));
178
179
0
    for( sal_Int32 i = 1 ; i < nCnt ; ++i )
180
0
    {
181
0
        s.append(OUString::Concat("\n")
182
0
                 + xmlsec::GetContentPart(rInfos[i].Signer->getSubjectName(),
183
0
                                          rInfos[0].Signer->getCertificateKind()));
184
0
    }
185
186
0
    mxSignsFI->set_label(s.makeStringAndClear());
187
0
    mxViewSignsBtn->set_visible(true);
188
0
    mxViewCertBtn->set_visible(false);
189
0
}
190
191
void MacroWarning::SetCertificate( const css::uno::Reference< css::security::XCertificate >& _rxCert )
192
0
{
193
0
    mxCert = _rxCert;
194
0
    if( mxCert.is() )
195
0
    {
196
0
        OUString s(xmlsec::GetContentPart(mxCert->getSubjectName(), mxCert->getCertificateKind()));
197
0
        mxSignsFI->set_label(s);
198
199
0
        ::DateTime now( ::DateTime::SYSTEM );
200
0
        DateTime aDateTimeStart( DateTime::EMPTY );
201
0
        DateTime aDateTimeEnd( DateTime::EMPTY );
202
0
        utl::typeConvert( mxCert->getNotValidBefore(), aDateTimeStart );
203
0
        utl::typeConvert( mxCert->getNotValidAfter(), aDateTimeEnd );
204
0
        mxNotYetValid->set_visible(now < aDateTimeStart);
205
0
        mxNoLongerValid->set_visible(now > aDateTimeEnd);
206
0
        mxViewSignsBtn->set_visible(false);
207
0
        mxViewCertBtn->set_visible(true);
208
0
    }
209
0
}
210
211
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */