Debian Codegraph @

xmlsecurity/source/dialogs/certificateviewer.cxx

raw
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 <sal/config.h>
21
22 #include <string_view>
23
24 #include <certificatechooser.hxx>
25 #include <certificateviewer.hxx>
26 #include <com/sun/star/security/XCertificate.hpp>
27
28 #include <com/sun/star/security/CertificateCharacters.hpp>
29 #include <com/sun/star/xml/crypto/XSecurityEnvironment.hpp>
30 #include <com/sun/star/security/CertificateValidity.hpp>
31
32 #include <unotools/localedatawrapper.hxx>
33 #include <unotools/datetime.hxx>
34
35 #include <strings.hrc>
36 #include <resourcemanager.hxx>
37 #include <comphelper/xmlsechelper.hxx>
38 #include <tools/datetime.hxx>
39 #include <bitmaps.hlst>
40
41 #include <vcl/settings.hxx>
42 #include <vcl/svapp.hxx>
43
44 using namespace comphelper;
45 using namespace ::com::sun::star;
46 using namespace ::com::sun::star::uno;
47
48 CertificateViewer::CertificateViewer(weld::Window* _pParent,
49 const css::uno::Reference< css::xml::crypto::XSecurityEnvironment >& _rxSecurityEnvironment,
50 const css::uno::Reference< css::security::XCertificate >& _rXCert, bool bCheckForPrivateKey,
51 CertificateChooser* pParentChooser)
52 : GenericDialogController(_pParent, u"xmlsec/ui/viewcertdialog.ui"_ustr, u"ViewCertDialog"_ustr)
53 , mbCheckForPrivateKey(bCheckForPrivateKey)
54 , mpParentChooser(pParentChooser)
55 , mxTabCtrl(m_xBuilder->weld_notebook(u"tabcontrol"_ustr))
56 {
57 mxTabCtrl->connect_enter_page(LINK(this, CertificateViewer, ActivatePageHdl));
58
59 mxSecurityEnvironment = _rxSecurityEnvironment;
60 mxCert = _rXCert;
61
62 mxGeneralPage.reset(new CertificateViewerGeneralTP(mxTabCtrl->get_page(u"general"_ustr), this));
63 mxDetailsPage.reset(new CertificateViewerDetailsTP(mxTabCtrl->get_page(u"details"_ustr), this));
64 if (!mxSecurityEnvironment->buildCertificatePath(mxCert).hasElements())
65 mxTabCtrl->remove_page(u"path"_ustr);
66 else
67 mxPathId.reset(new CertificateViewerCertPathTP(mxTabCtrl->get_page(u"path"_ustr), this));
68 mxTabCtrl->set_current_page(u"general"_ustr);
69 }
70
71 IMPL_LINK(CertificateViewer, ActivatePageHdl, const OUString&, rPage, void)
72 {
73 if (rPage == "path")
74 mxPathId->ActivatePage();
75 }
76
77 CertificateViewerTP::CertificateViewerTP(weld::Container* pParent, const OUString& rUIXMLDescription,
78 const OUString& rID, CertificateViewer* pDlg)
79 : mxBuilder(Application::CreateBuilder(pParent, rUIXMLDescription))
80 , mxContainer(mxBuilder->weld_container(rID))
81 , mpDlg(pDlg)
82 {
83 }
84
85 CertificateViewerGeneralTP::CertificateViewerGeneralTP(weld::Container* pParent, CertificateViewer* pDlg)
86 : CertificateViewerTP(pParent, u"xmlsec/ui/certgeneral.ui"_ustr, u"CertGeneral"_ustr, pDlg)
87 , m_xCertImg(mxBuilder->weld_image(u"certimage"_ustr))
88 , m_xHintNotTrustedFT(mxBuilder->weld_label(u"hintnotrust"_ustr))
89 , m_xIssuedToLabelFT(mxBuilder->weld_label(u"issued_to"_ustr))
90 , m_xIssuedToFT(mxBuilder->weld_label(u"issued_to_value"_ustr))
91 , m_xIssuedByLabelFT(mxBuilder->weld_label(u"issued_by"_ustr))
92 , m_xIssuedByFT(mxBuilder->weld_label(u"issued_by_value"_ustr))
93 , m_xValidFromDateFT(mxBuilder->weld_label(u"valid_from_value"_ustr))
94 , m_xValidToDateFT(mxBuilder->weld_label(u"valid_to_value"_ustr))
95 , m_xKeyTypeFT(mxBuilder->weld_label(u"key_type_value"_ustr))
96 , m_xKeyImg(mxBuilder->weld_image(u"keyimage"_ustr))
97 , m_xHintCorrespPrivKeyFT(mxBuilder->weld_label(u"privatekey"_ustr))
98 {
99 //Verify the certificate
100 sal_Int32 certStatus = mpDlg->mxSecurityEnvironment->verifyCertificate(mpDlg->mxCert,
101 Sequence<Reference<css::security::XCertificate> >());
102
103 bool bCertValid = certStatus == css::security::CertificateValidity::VALID;
104
105 if ( !bCertValid )
106 {
107 m_xCertImg->set_from_icon_name(BMP_STATE_NOT_VALIDATED);
108 m_xHintNotTrustedFT->set_label(XsResId(STR_CERTIFICATE_NOT_VALIDATED));
109 }
110
111 // insert data
112 css::uno::Reference< css::security::XCertificate > xCert = mpDlg->mxCert;
113
114 OUString sSubjectName(xmlsec::GetContentPart(xCert->getSubjectName(), xCert->getCertificateKind()));
115 if (!sSubjectName.isEmpty())
116 m_xIssuedToFT->set_label(sSubjectName);
117 else
118 m_xIssuedToLabelFT->hide();
119 OUString sIssuerName(xmlsec::GetContentPart(xCert->getIssuerName(), xCert->getCertificateKind()));
120 if (!sIssuerName.isEmpty())
121 m_xIssuedByFT->set_label(sIssuerName);
122 else
123 m_xIssuedByLabelFT->hide();
124
125 DateTime aDateTimeStart( DateTime::EMPTY );
126 DateTime aDateTimeEnd( DateTime::EMPTY );
127 utl::typeConvert( xCert->getNotValidBefore(), aDateTimeStart );
128 utl::typeConvert( xCert->getNotValidAfter(), aDateTimeEnd );
129
130 OUString sValidFromDate = Application::GetSettings().GetUILocaleDataWrapper().getDate(Date(aDateTimeStart.GetDate()));
131 OUString sValidToDate = Application::GetSettings().GetUILocaleDataWrapper().getDate(Date(aDateTimeEnd.GetDate()));
132
133 m_xValidFromDateFT->set_label(sValidFromDate);
134 m_xValidToDateFT->set_label(sValidToDate);
135 m_xKeyTypeFT->set_label(xmlsec::GetCertificateKind(xCert->getCertificateKind()));
136
137 // Check if we have the private key...
138 bool bHasPrivateKey = false;
139 // #i41270# Check only if we have that certificate in our security environment
140 if (pDlg->mbCheckForPrivateKey)
141 {
142 tools::Long nCertificateCharacters = pDlg->mxSecurityEnvironment->getCertificateCharacters(xCert);
143 bHasPrivateKey = (nCertificateCharacters & security::CertificateCharacters::HAS_PRIVATE_KEY);
144 }
145 if (!bHasPrivateKey)
146 {
147 m_xKeyImg->hide();
148 m_xHintCorrespPrivKeyFT->hide();
149 }
150 }
151
152 void CertificateViewerDetailsTP::InsertElement(const OUString& rField, const OUString& rValue,
153 const OUString& rDetails, bool bFixedWidthFont)
154 {
155 m_aUserData.emplace_back(std::make_unique<Details_UserDatat>(rDetails, bFixedWidthFont));
156 OUString sId(weld::toId(m_aUserData.back().get()));
157 m_xElementsLB->append(sId, rField);
158 m_xElementsLB->set_text(m_xElementsLB->n_children() -1, rValue, 1);
159 }
160
161 // X.509 + GPG
162 CertificateViewerDetailsTP::CertificateViewerDetailsTP(weld::Container* pParent, CertificateViewer* pDlg)
163 : CertificateViewerTP(pParent, u"xmlsec/ui/certdetails.ui"_ustr, u"CertDetails"_ustr, pDlg)
164 , m_xElementsLB(mxBuilder->weld_tree_view(u"tablecontainer"_ustr))
165 , m_xValueDetails(mxBuilder->weld_text_view(u"valuedetails"_ustr))
166 {
167 const int nWidth = m_xElementsLB->get_approximate_digit_width() * 100;
168 const int nHeight = m_xElementsLB->get_height_rows(14);
169 m_xElementsLB->set_size_request(nWidth, nHeight);
170 m_xValueDetails->set_size_request(nWidth, nHeight);
171 m_xValueDetails->set_editable(false);
172 m_xElementsLB->set_column_fixed_widths( { nWidth / 2 } );
173
174 // fill list box
175 Reference< security::XCertificate > xCert = mpDlg->mxCert;
176 sal_uInt16 nLineBreak = 16;
177 const char* const pHexSep = " ";
178 OUString aLBEntry;
179 OUString aDetails;
180 // Certificate Versions are reported wrong (#i35107#) - 0 == "V1", 1 == "V2", ..., n = "V(n+1)"
181 aLBEntry = "V" + OUString::number( xCert->getVersion() + 1 );
182 InsertElement( XsResId( STR_VERSION ), aLBEntry, aLBEntry );
183 Sequence< sal_Int8 > aSeq = xCert->getSerialNumber();
184 aLBEntry = xmlsec::GetHexString( aSeq, pHexSep );
185 aDetails = xmlsec::GetHexString( aSeq, pHexSep, nLineBreak );
186 InsertElement( XsResId( STR_SERIALNUM ), aLBEntry, aDetails, true );
187
188 std::pair< OUString, OUString> pairIssuer =
189 xmlsec::GetDNForCertDetailsView(xCert->getIssuerName());
190 aLBEntry = pairIssuer.first;
191 aDetails = pairIssuer.second;
192 InsertElement( XsResId( STR_ISSUER ), aLBEntry, aDetails );
193
194 DateTime aDateTime( DateTime::EMPTY );
195 utl::typeConvert( xCert->getNotValidBefore(), aDateTime );
196 aLBEntry = Application::GetSettings().GetUILocaleDataWrapper().getDate(aDateTime) + " ";
197 aLBEntry += Application::GetSettings().GetUILocaleDataWrapper().getTime(aDateTime);
198 InsertElement( XsResId( STR_VALIDFROM ), aLBEntry, aLBEntry );
199 utl::typeConvert( xCert->getNotValidAfter(), aDateTime );
200 aLBEntry = Application::GetSettings().GetUILocaleDataWrapper().getDate(aDateTime) + " ";
201 aLBEntry += Application::GetSettings().GetUILocaleDataWrapper().getTime(aDateTime);
202 InsertElement( XsResId( STR_VALIDTO ), aLBEntry, aLBEntry );
203
204 std::pair< OUString, OUString > pairSubject =
205 xmlsec::GetDNForCertDetailsView(xCert->getSubjectName());
206 aLBEntry = pairSubject.first;
207 aDetails = pairSubject.second;
208 InsertElement( XsResId( STR_SUBJECT ), aLBEntry, aDetails );
209
210 aLBEntry = aDetails = xCert->getSubjectPublicKeyAlgorithm();
211 InsertElement( XsResId( STR_SUBJECT_PUBKEY_ALGO ), aLBEntry, aDetails );
212 aSeq = xCert->getSubjectPublicKeyValue();
213 aLBEntry = xmlsec::GetHexString( aSeq, pHexSep );
214 aDetails = xmlsec::GetHexString( aSeq, pHexSep, nLineBreak );
215 InsertElement( XsResId( STR_SUBJECT_PUBKEY_VAL ), aLBEntry, aDetails, true );
216
217 aLBEntry = aDetails = xCert->getSignatureAlgorithm();
218 InsertElement( XsResId( STR_SIGNATURE_ALGO ), aLBEntry, aDetails );
219
220 CertificateChooser* pChooser = mpDlg->GetParentChooser();
221 if (pChooser)
222 {
223 aLBEntry = CertificateChooser::UsageInClearText( mpDlg->mxCert->getCertificateUsage() );
224 InsertElement( XsResId( STR_USE ), aLBEntry, aLBEntry );
225 }
226
227 aSeq = xCert->getSHA1Thumbprint();
228 aLBEntry = xmlsec::GetHexString( aSeq, pHexSep );
229 aDetails = xmlsec::GetHexString( aSeq, pHexSep, nLineBreak );
230 InsertElement( XsResId( STR_THUMBPRINT_SHA1 ), aLBEntry, aDetails, true );
231
232 aSeq = xCert->getMD5Thumbprint();
233 aLBEntry = xmlsec::GetHexString( aSeq, pHexSep );
234 aDetails = xmlsec::GetHexString( aSeq, pHexSep, nLineBreak );
235 InsertElement( XsResId( STR_THUMBPRINT_MD5 ), aLBEntry, aDetails, true );
236
237 m_xElementsLB->columns_autosize();
238 m_xElementsLB->connect_selection_changed(
239 LINK(this, CertificateViewerDetailsTP, ElementSelectHdl));
240 }
241
242 IMPL_LINK_NOARG(CertificateViewerDetailsTP, ElementSelectHdl, weld::TreeView&, void)
243 {
244 int nEntry = m_xElementsLB->get_selected_index();
245 OUString aElementText;
246 bool bFixedWidthFont;
247 if (nEntry != -1)
248 {
249 const Details_UserDatat* p = weld::fromId<Details_UserDatat*>(m_xElementsLB->get_id(nEntry));
250 aElementText = p->maTxt;
251 bFixedWidthFont = p->mbFixedWidthFont;
252 }
253 else
254 bFixedWidthFont = false;
255
256 m_xValueDetails->set_monospace(bFixedWidthFont);
257 m_xValueDetails->set_text(aElementText);
258 }
259
260 // X.509
261 CertificateViewerCertPathTP::CertificateViewerCertPathTP(weld::Container* pParent, CertificateViewer* pDlg)
262 : CertificateViewerTP(pParent, u"xmlsec/ui/certpage.ui"_ustr, u"CertPage"_ustr, pDlg)
263 , mpParent(pDlg)
264 , mbFirstActivateDone(false)
265 , mxCertPathLB(mxBuilder->weld_tree_view(u"signatures"_ustr))
266 , mxScratchIter(mxCertPathLB->make_iterator())
267 , mxViewCertPB(mxBuilder->weld_button(u"viewcert"_ustr))
268 , mxCertStatusML(mxBuilder->weld_text_view(u"status"_ustr))
269 , mxCertOK(mxBuilder->weld_label(u"certok"_ustr))
270 , mxCertNotValidated(mxBuilder->weld_label(u"certnotok"_ustr))
271 {
272 const int nWidth = mxCertPathLB->get_approximate_digit_width() * 100;
273 const int nHeight = mxCertPathLB->get_height_rows(6);
274 mxCertPathLB->set_size_request(nWidth, nHeight);
275 mxCertStatusML->set_size_request(nWidth, nHeight);
276 mxCertStatusML->set_editable(false);
277
278 mxCertPathLB->columns_autosize();
279 mxCertPathLB->connect_selection_changed(LINK(this, CertificateViewerCertPathTP, CertSelectHdl));
280 mxViewCertPB->connect_clicked( LINK( this, CertificateViewerCertPathTP, ViewCertHdl ) );
281 }
282
283 CertificateViewerCertPathTP::~CertificateViewerCertPathTP()
284 {
285 if (mxCertificateViewer)
286 mxCertificateViewer->response(RET_OK);
287 }
288
289 void CertificateViewerCertPathTP::ActivatePage()
290 {
291 if ( mbFirstActivateDone )
292 return;
293
294 mbFirstActivateDone = true;
295 Sequence< Reference< security::XCertificate > > aCertPath =
296 mpParent->mxSecurityEnvironment->buildCertificatePath( mpParent->mxCert );
297 const Reference< security::XCertificate >* pCertPath = aCertPath.getConstArray();
298
299 sal_Int32 i, nCnt = aCertPath.getLength();
300 std::unique_ptr<weld::TreeIter> xParent;
301 for (i = nCnt-1; i >= 0; i--)
302 {
303 const Reference< security::XCertificate > rCert = pCertPath[ i ];
304 OUString sName = xmlsec::GetContentPart( rCert->getSubjectName(), rCert->getCertificateKind() );
305 //Verify the certificate
306 sal_Int32 certStatus = mpDlg->mxSecurityEnvironment->verifyCertificate(rCert,
307 Sequence<Reference<css::security::XCertificate> >());
308 bool bCertValid = certStatus == css::security::CertificateValidity::VALID;
309 InsertCert(xParent.get(), sName, rCert, bCertValid);
310 if (!xParent)
311 {
312 xParent = mxCertPathLB->make_iterator();
313 (void)mxCertPathLB->get_iter_first(*xParent);
314 }
315 else
316 {
317 (void)mxCertPathLB->iter_children(*xParent);
318 }
319 }
320
321 if (xParent)
322 mxCertPathLB->select(*xParent);
323 mxViewCertPB->set_sensitive(false); // Own certificate selected
324
325 while (xParent)
326 {
327 mxCertPathLB->expand_row(*xParent);
328 if (!mxCertPathLB->iter_parent(*xParent))
329 xParent.reset();
330 }
331
332 CertSelectHdl(*mxCertPathLB);
333 }
334
335 IMPL_LINK_NOARG(CertificateViewerCertPathTP, ViewCertHdl, weld::Button&, void)
336 {
337 std::unique_ptr<weld::TreeIter> xIter = mxCertPathLB->make_iterator();
338 if (mxCertPathLB->get_selected(xIter.get()))
339 {
340 if (mxCertificateViewer)
341 mxCertificateViewer->response(RET_OK);
342
343 CertPath_UserData* pData = weld::fromId<CertPath_UserData*>(mxCertPathLB->get_id(*xIter));
344 mxCertificateViewer = std::make_shared<CertificateViewer>(mpDlg->getDialog(), mpDlg->mxSecurityEnvironment,
345 pData->mxCert, false, nullptr);
346 weld::DialogController::runAsync(mxCertificateViewer, [this] (sal_Int32) { mxCertificateViewer = nullptr; });
347 }
348 }
349
350 IMPL_LINK_NOARG(CertificateViewerCertPathTP, CertSelectHdl, weld::TreeView&, void)
351 {
352 OUString sStatus;
353
354 std::unique_ptr<weld::TreeIter> xIter = mxCertPathLB->make_iterator();
355 bool bEntry = mxCertPathLB->get_selected(xIter.get());
356 if (bEntry)
357 {
358 CertPath_UserData* pData = weld::fromId<CertPath_UserData*>(mxCertPathLB->get_id(*xIter));
359 if (pData)
360 sStatus = pData->mbValid ? mxCertOK->get_label() : mxCertNotValidated->get_label();
361 }
362
363 mxCertStatusML->set_text(sStatus);
364
365 bool bSensitive = false;
366 if (bEntry)
367 {
368 // if has children, so not the last one in the chain
369 if (mxCertPathLB->iter_children(*xIter))
370 bSensitive = true;
371 }
372 mxViewCertPB->set_sensitive(bSensitive);
373 }
374
375 void CertificateViewerCertPathTP::InsertCert(const weld::TreeIter* pParent, const OUString& rName,
376 const css::uno::Reference< css::security::XCertificate >& rxCert,
377 bool bValid)
378 {
379 auto const sImage = bValid ? std::u16string_view(u"" BMP_CERT_OK) : std::u16string_view(u"" BMP_CERT_NOT_OK);
380 maUserData.emplace_back(std::make_unique<CertPath_UserData>(rxCert, bValid));
381 OUString sId(weld::toId(maUserData.back().get()));
382 mxCertPathLB->insert(pParent, -1, &rName, &sId, nullptr, nullptr, false, mxScratchIter.get());
383 mxCertPathLB->set_image(*mxScratchIter, OUString(sImage));
384 }
385
386 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */