| 1 |
/* poppler-form.h: qt interface to poppler |
| 2 |
* Copyright (C) 2007-2008, 2011, Pino Toscano <[email protected]> |
| 3 |
* Copyright (C) 2008, 2011, 2012, 2015-2025 Albert Astals Cid <[email protected]> |
| 4 |
* Copyright (C) 2011 Carlos Garcia Campos <[email protected]> |
| 5 |
* Copyright (C) 2012, Adam Reichold <[email protected]> |
| 6 |
* Copyright (C) 2016, Hanno Meyer-Thurow <[email protected]> |
| 7 |
* Copyright (C) 2017, Hans-Ulrich Jüttner <[email protected]> |
| 8 |
* Copyright (C) 2018, Andre Heinecke <[email protected]> |
| 9 |
* Copyright (C) 2018 Klarälvdalens Datakonsult AB, a KDAB Group company, <[email protected]>. Work sponsored by the LiMux project of the city of Munich |
| 10 |
* Copyright (C) 2018 Chinmoy Ranjan Pradhan <[email protected]> |
| 11 |
* Copyright (C) 2018, 2020, 2021 Oliver Sander <[email protected]> |
| 12 |
* Copyright (C) 2019 João Netto <[email protected]> |
| 13 |
* Copyright (C) 2020 David García Garzón <[email protected]> |
| 14 |
* Copyright (C) 2020 Thorsten Behrens <[email protected]> |
| 15 |
* Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, <[email protected]>. Work sponsored by Technische Universität Dresden |
| 16 |
* Copyright (C) 2021 Georgiy Sgibnev <[email protected]>. Work sponsored by lab50.net. |
| 17 |
* Copyright (C) 2021 Theofilos Intzoglou <[email protected]> |
| 18 |
* Copyright (C) 2022 Alexander Sulfrian <[email protected]> |
| 19 |
* Copyright (C) 2023-2025 g10 Code GmbH, Author: Sune Stolborg Vuorela <[email protected]> |
| 20 |
* Copyright (C) 2024 Pratham Gandhi <[email protected]> |
| 21 |
* Copyright (C) 2024 Stefan Brüns <[email protected]> |
| 22 |
* Copyright (C) 2025 Blair Bonnett <[email protected]> |
| 23 |
* |
| 24 |
* This program is free software; you can redistribute it and/or modify |
| 25 |
* it under the terms of the GNU General Public License as published by |
| 26 |
* the Free Software Foundation; either version 2, or (at your option) |
| 27 |
* any later version. |
| 28 |
* |
| 29 |
* This program is distributed in the hope that it will be useful, |
| 30 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 31 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 32 |
* GNU General Public License for more details. |
| 33 |
* |
| 34 |
* You should have received a copy of the GNU General Public License |
| 35 |
* along with this program; if not, write to the Free Software |
| 36 |
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. |
| 37 |
*/ |
| 38 |
|
| 39 |
#include "poppler-form.h" |
| 40 |
#include "poppler-converter.h" |
| 41 |
|
| 42 |
#include <config.h> |
| 43 |
|
| 44 |
#include <QtCore/QSizeF> |
| 45 |
#include <QTimeZone> |
| 46 |
#include <QUrl> |
| 47 |
|
| 48 |
#include <Form.h> |
| 49 |
#include <Object.h> |
| 50 |
#include <Link.h> |
| 51 |
#include <SignatureInfo.h> |
| 52 |
#include <CertificateInfo.h> |
| 53 |
#include <CryptoSignBackend.h> |
| 54 |
#if ENABLE_NSS3 |
| 55 |
# include <NSSCryptoSignBackend.h> |
| 56 |
#endif |
| 57 |
#if ENABLE_GPGME |
| 58 |
# include <GPGMECryptoSignBackendConfiguration.h> |
| 59 |
#endif |
| 60 |
|
| 61 |
#include "poppler-page-private.h" |
| 62 |
#include "poppler-private.h" |
| 63 |
#include "poppler-annotation-helper.h" |
| 64 |
|
| 65 |
namespace { |
| 66 |
|
| 67 |
Qt::Alignment formTextAlignment(::FormWidget *fm) |
| 68 |
{ |
| 69 |
Qt::Alignment qtalign = Qt::AlignLeft; |
| 70 |
switch (fm->getField()->getTextQuadding()) { |
| 71 |
case VariableTextQuadding::centered: |
| 72 |
qtalign = Qt::AlignHCenter; |
| 73 |
break; |
| 74 |
case VariableTextQuadding::rightJustified: |
| 75 |
qtalign = Qt::AlignRight; |
| 76 |
break; |
| 77 |
case VariableTextQuadding::leftJustified: |
| 78 |
qtalign = Qt::AlignLeft; |
| 79 |
} |
| 80 |
return qtalign; |
| 81 |
} |
| 82 |
|
| 83 |
} |
| 84 |
|
| 85 |
namespace Poppler { |
| 86 |
|
| 87 |
FormFieldIcon::FormFieldIcon(FormFieldIconData *data) : d_ptr(data) { } |
| 88 |
|
| 89 |
FormFieldIcon::FormFieldIcon(const FormFieldIcon &ffIcon) |
| 90 |
{ |
| 91 |
d_ptr = new FormFieldIconData; |
| 92 |
d_ptr->icon = ffIcon.d_ptr->icon; |
| 93 |
} |
| 94 |
|
| 95 |
FormFieldIcon &FormFieldIcon::operator=(const FormFieldIcon &ffIcon) |
| 96 |
{ |
| 97 |
if (this != &ffIcon) { |
| 98 |
delete d_ptr; |
| 99 |
d_ptr = nullptr; |
| 100 |
|
| 101 |
d_ptr = new FormFieldIconData; |
| 102 |
*d_ptr = *ffIcon.d_ptr; |
| 103 |
} |
| 104 |
|
| 105 |
return *this; |
| 106 |
} |
| 107 |
|
| 108 |
FormFieldIcon::~FormFieldIcon() |
| 109 |
{ |
| 110 |
delete d_ptr; |
| 111 |
} |
| 112 |
|
| 113 |
FormField::FormField(std::unique_ptr<FormFieldData> dd) : m_formData(std::move(dd)) |
| 114 |
{ |
| 115 |
if (m_formData->page) { |
| 116 |
const int rotation = m_formData->page->getRotate(); |
| 117 |
// reading the coords |
| 118 |
double left, top, right, bottom; |
| 119 |
m_formData->fm->getRect(&left, &bottom, &right, &top); |
| 120 |
// build a normalized transform matrix for this page at 100% scale |
| 121 |
GfxState gfxState(72.0, 72.0, m_formData->page->getCropBox(), rotation, true); |
| 122 |
const double *gfxCTM = gfxState.getCTM(); |
| 123 |
double MTX[6]; |
| 124 |
double pageWidth = m_formData->page->getCropWidth(); |
| 125 |
double pageHeight = m_formData->page->getCropHeight(); |
| 126 |
// landscape and seascape page rotation: be sure to use the correct (== rotated) page size |
| 127 |
if (((rotation / 90) % 2) == 1) { |
| 128 |
qSwap(pageWidth, pageHeight); |
| 129 |
} |
| 130 |
for (int i = 0; i < 6; i += 2) { |
| 131 |
MTX[i] = gfxCTM[i] / pageWidth; |
| 132 |
MTX[i + 1] = gfxCTM[i + 1] / pageHeight; |
| 133 |
} |
| 134 |
QPointF topLeft; |
| 135 |
XPDFReader::transform(MTX, qMin(left, right), qMax(top, bottom), topLeft); |
| 136 |
QPointF bottomRight; |
| 137 |
XPDFReader::transform(MTX, qMax(left, right), qMin(top, bottom), bottomRight); |
| 138 |
m_formData->box = QRectF(topLeft, QSizeF(bottomRight.x() - topLeft.x(), bottomRight.y() - topLeft.y())); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
FormField::~FormField() = default; |
| 143 |
|
| 144 |
QRectF FormField::rect() const |
| 145 |
{ |
| 146 |
return m_formData->box; |
| 147 |
} |
| 148 |
|
| 149 |
int FormField::id() const |
| 150 |
{ |
| 151 |
return m_formData->fm->getID(); |
| 152 |
} |
| 153 |
|
| 154 |
QString FormField::name() const |
| 155 |
{ |
| 156 |
QString name; |
| 157 |
if (const GooString *goo = m_formData->fm->getPartialName()) { |
| 158 |
name = UnicodeParsedString(goo); |
| 159 |
} |
| 160 |
return name; |
| 161 |
} |
| 162 |
|
| 163 |
void FormField::setName(const QString &name) const |
| 164 |
{ |
| 165 |
const std::unique_ptr<GooString> goo = QStringToGooString(name); |
| 166 |
m_formData->fm->setPartialName(*goo); |
| 167 |
} |
| 168 |
|
| 169 |
QString FormField::fullyQualifiedName() const |
| 170 |
{ |
| 171 |
QString name; |
| 172 |
if (const GooString *goo = m_formData->fm->getFullyQualifiedName()) { |
| 173 |
name = UnicodeParsedString(goo); |
| 174 |
} |
| 175 |
return name; |
| 176 |
} |
| 177 |
|
| 178 |
QString FormField::uiName() const |
| 179 |
{ |
| 180 |
QString name; |
| 181 |
if (const GooString *goo = m_formData->fm->getAlternateUiName()) { |
| 182 |
name = UnicodeParsedString(goo); |
| 183 |
} |
| 184 |
return name; |
| 185 |
} |
| 186 |
|
| 187 |
bool FormField::isReadOnly() const |
| 188 |
{ |
| 189 |
return m_formData->fm->isReadOnly(); |
| 190 |
} |
| 191 |
|
| 192 |
void FormField::setReadOnly(bool value) |
| 193 |
{ |
| 194 |
m_formData->fm->setReadOnly(value); |
| 195 |
} |
| 196 |
|
| 197 |
bool FormField::isVisible() const |
| 198 |
{ |
| 199 |
const unsigned int flags = m_formData->fm->getWidgetAnnotation()->getFlags(); |
| 200 |
if (flags & Annot::flagHidden) { |
| 201 |
return false; |
| 202 |
} |
| 203 |
if (flags & Annot::flagNoView) { |
| 204 |
return false; |
| 205 |
} |
| 206 |
return true; |
| 207 |
} |
| 208 |
|
| 209 |
void FormField::setVisible(bool value) |
| 210 |
{ |
| 211 |
unsigned int flags = m_formData->fm->getWidgetAnnotation()->getFlags(); |
| 212 |
if (value) { |
| 213 |
flags &= ~Annot::flagHidden; |
| 214 |
flags &= ~Annot::flagNoView; |
| 215 |
} else { |
| 216 |
flags |= Annot::flagHidden; |
| 217 |
} |
| 218 |
m_formData->fm->getWidgetAnnotation()->setFlags(flags); |
| 219 |
} |
| 220 |
|
| 221 |
bool FormField::isPrintable() const |
| 222 |
{ |
| 223 |
return (m_formData->fm->getWidgetAnnotation()->getFlags() & Annot::flagPrint); |
| 224 |
} |
| 225 |
|
| 226 |
void FormField::setPrintable(bool value) |
| 227 |
{ |
| 228 |
unsigned int flags = m_formData->fm->getWidgetAnnotation()->getFlags(); |
| 229 |
if (value) { |
| 230 |
flags |= Annot::flagPrint; |
| 231 |
} else { |
| 232 |
flags &= ~Annot::flagPrint; |
| 233 |
} |
| 234 |
m_formData->fm->getWidgetAnnotation()->setFlags(flags); |
| 235 |
} |
| 236 |
|
| 237 |
std::unique_ptr<Link> FormField::activationAction() const |
| 238 |
{ |
| 239 |
if (::LinkAction *act = m_formData->fm->getActivationAction()) { |
| 240 |
return PageData::convertLinkActionToLink(act, m_formData->doc, QRectF()); |
| 241 |
} |
| 242 |
|
| 243 |
return {}; |
| 244 |
} |
| 245 |
|
| 246 |
std::unique_ptr<Link> FormField::additionalAction(AdditionalActionType type) const |
| 247 |
{ |
| 248 |
Annot::FormAdditionalActionsType actionType = Annot::actionFieldModified; |
| 249 |
switch (type) { |
| 250 |
case FieldModified: |
| 251 |
actionType = Annot::actionFieldModified; |
| 252 |
break; |
| 253 |
case FormatField: |
| 254 |
actionType = Annot::actionFormatField; |
| 255 |
break; |
| 256 |
case ValidateField: |
| 257 |
actionType = Annot::actionValidateField; |
| 258 |
break; |
| 259 |
case CalculateField: |
| 260 |
actionType = Annot::actionCalculateField; |
| 261 |
break; |
| 262 |
} |
| 263 |
|
| 264 |
if (std::unique_ptr<::LinkAction> act = m_formData->fm->getAdditionalAction(actionType)) { |
| 265 |
return PageData::convertLinkActionToLink(act.get(), m_formData->doc, QRectF()); |
| 266 |
} |
| 267 |
|
| 268 |
return {}; |
| 269 |
} |
| 270 |
|
| 271 |
std::unique_ptr<Link> FormField::additionalAction(Annotation::AdditionalActionType type) const |
| 272 |
{ |
| 273 |
::AnnotWidget *w = m_formData->fm->getWidgetAnnotation().get(); |
| 274 |
if (!w) { |
| 275 |
return {}; |
| 276 |
} |
| 277 |
|
| 278 |
const Annot::AdditionalActionsType actionType = toPopplerAdditionalActionType(type); |
| 279 |
|
| 280 |
if (std::unique_ptr<::LinkAction> act = w->getAdditionalAction(actionType)) { |
| 281 |
return PageData::convertLinkActionToLink(act.get(), m_formData->doc, QRectF()); |
| 282 |
} |
| 283 |
|
| 284 |
return {}; |
| 285 |
} |
| 286 |
|
| 287 |
FormFieldButton::FormFieldButton(DocumentData *doc, ::Page *p, ::FormWidgetButton *w) : FormField(std::make_unique<FormFieldData>(doc, p, w)) { } |
| 288 |
|
| 289 |
FormFieldButton::~FormFieldButton() = default; |
| 290 |
|
| 291 |
FormFieldButton::FormType FormFieldButton::type() const |
| 292 |
{ |
| 293 |
return FormField::FormButton; |
| 294 |
} |
| 295 |
|
| 296 |
FormFieldButton::ButtonType FormFieldButton::buttonType() const |
| 297 |
{ |
| 298 |
FormWidgetButton *fwb = static_cast<FormWidgetButton *>(m_formData->fm); |
| 299 |
switch (fwb->getButtonType()) { |
| 300 |
case formButtonCheck: |
| 301 |
return FormFieldButton::CheckBox; |
| 302 |
break; |
| 303 |
case formButtonPush: |
| 304 |
return FormFieldButton::Push; |
| 305 |
break; |
| 306 |
case formButtonRadio: |
| 307 |
return FormFieldButton::Radio; |
| 308 |
break; |
| 309 |
} |
| 310 |
return FormFieldButton::CheckBox; |
| 311 |
} |
| 312 |
|
| 313 |
QString FormFieldButton::caption() const |
| 314 |
{ |
| 315 |
FormWidgetButton *fwb = static_cast<FormWidgetButton *>(m_formData->fm); |
| 316 |
QString ret; |
| 317 |
if (fwb->getButtonType() == formButtonPush) { |
| 318 |
Dict *dict = m_formData->fm->getObj()->getDict(); |
| 319 |
Object obj1 = dict->lookup("MK"); |
| 320 |
if (obj1.isDict()) { |
| 321 |
AnnotAppearanceCharacs appearCharacs(obj1.getDict()); |
| 322 |
if (appearCharacs.getNormalCaption()) { |
| 323 |
ret = UnicodeParsedString(appearCharacs.getNormalCaption()); |
| 324 |
} |
| 325 |
} |
| 326 |
} else { |
| 327 |
if (const char *goo = fwb->getOnStr()) { |
| 328 |
ret = QString::fromUtf8(goo); |
| 329 |
} |
| 330 |
} |
| 331 |
return ret; |
| 332 |
} |
| 333 |
|
| 334 |
FormFieldIcon FormFieldButton::icon() const |
| 335 |
{ |
| 336 |
FormWidgetButton *fwb = static_cast<FormWidgetButton *>(m_formData->fm); |
| 337 |
if (fwb->getButtonType() == formButtonPush) { |
| 338 |
Dict *dict = m_formData->fm->getObj()->getDict(); |
| 339 |
FormFieldIconData *data = new FormFieldIconData; |
| 340 |
data->icon = dict; |
| 341 |
return FormFieldIcon(data); |
| 342 |
} |
| 343 |
return FormFieldIcon(nullptr); |
| 344 |
} |
| 345 |
|
| 346 |
void FormFieldButton::setIcon(const FormFieldIcon &icon) |
| 347 |
{ |
| 348 |
if (FormFieldIconData::getData(icon) == nullptr) { |
| 349 |
return; |
| 350 |
} |
| 351 |
|
| 352 |
FormWidgetButton *fwb = static_cast<FormWidgetButton *>(m_formData->fm); |
| 353 |
if (fwb->getButtonType() == formButtonPush) { |
| 354 |
std::shared_ptr<::AnnotWidget> w = m_formData->fm->getWidgetAnnotation(); |
| 355 |
FormFieldIconData *data = FormFieldIconData::getData(icon); |
| 356 |
if (data->icon != nullptr) { |
| 357 |
w->setNewAppearance(data->icon->lookup("AP")); |
| 358 |
} |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
bool FormFieldButton::state() const |
| 363 |
{ |
| 364 |
FormWidgetButton *fwb = static_cast<FormWidgetButton *>(m_formData->fm); |
| 365 |
return fwb->getState(); |
| 366 |
} |
| 367 |
|
| 368 |
void FormFieldButton::setState(bool state) |
| 369 |
{ |
| 370 |
FormWidgetButton *fwb = static_cast<FormWidgetButton *>(m_formData->fm); |
| 371 |
fwb->setState((bool)state); |
| 372 |
} |
| 373 |
|
| 374 |
QList<int> FormFieldButton::siblings() const |
| 375 |
{ |
| 376 |
FormWidgetButton *fwb = static_cast<FormWidgetButton *>(m_formData->fm); |
| 377 |
::FormFieldButton *ffb = static_cast<::FormFieldButton *>(fwb->getField()); |
| 378 |
if (fwb->getButtonType() == formButtonPush) { |
| 379 |
return QList<int>(); |
| 380 |
} |
| 381 |
|
| 382 |
QList<int> ret; |
| 383 |
for (int i = 0; i < ffb->getNumSiblings(); ++i) { |
| 384 |
::FormFieldButton *sibling = static_cast<::FormFieldButton *>(ffb->getSibling(i)); |
| 385 |
for (int j = 0; j < sibling->getNumWidgets(); ++j) { |
| 386 |
FormWidget *w = sibling->getWidget(j); |
| 387 |
if (w) { |
| 388 |
ret.append(w->getID()); |
| 389 |
} |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
return ret; |
| 394 |
} |
| 395 |
|
| 396 |
FormFieldText::FormFieldText(DocumentData *doc, ::Page *p, ::FormWidgetText *w) : FormField(std::make_unique<FormFieldData>(doc, p, w)) { } |
| 397 |
|
| 398 |
FormFieldText::~FormFieldText() = default; |
| 399 |
|
| 400 |
FormField::FormType FormFieldText::type() const |
| 401 |
{ |
| 402 |
return FormField::FormText; |
| 403 |
} |
| 404 |
|
| 405 |
FormFieldText::TextType FormFieldText::textType() const |
| 406 |
{ |
| 407 |
FormWidgetText *fwt = static_cast<FormWidgetText *>(m_formData->fm); |
| 408 |
if (fwt->isFileSelect()) { |
| 409 |
return FormFieldText::FileSelect; |
| 410 |
} else if (fwt->isMultiline()) { |
| 411 |
return FormFieldText::Multiline; |
| 412 |
} |
| 413 |
return FormFieldText::Normal; |
| 414 |
} |
| 415 |
|
| 416 |
QString FormFieldText::text() const |
| 417 |
{ |
| 418 |
const GooString *goo = static_cast<FormWidgetText *>(m_formData->fm)->getContent(); |
| 419 |
return UnicodeParsedString(goo); |
| 420 |
} |
| 421 |
|
| 422 |
void FormFieldText::setText(const QString &text) |
| 423 |
{ |
| 424 |
FormWidgetText *fwt = static_cast<FormWidgetText *>(m_formData->fm); |
| 425 |
std::unique_ptr<GooString> goo = QStringToUnicodeGooString(text); |
| 426 |
fwt->setContent(std::move(goo)); |
| 427 |
} |
| 428 |
|
| 429 |
void FormFieldText::setAppearanceText(const QString &text) |
| 430 |
{ |
| 431 |
FormWidgetText *fwt = static_cast<FormWidgetText *>(m_formData->fm); |
| 432 |
std::unique_ptr<GooString> goo = QStringToUnicodeGooString(text); |
| 433 |
fwt->setAppearanceContent(std::move(goo)); |
| 434 |
} |
| 435 |
|
| 436 |
bool FormFieldText::isPassword() const |
| 437 |
{ |
| 438 |
FormWidgetText *fwt = static_cast<FormWidgetText *>(m_formData->fm); |
| 439 |
return fwt->isPassword(); |
| 440 |
} |
| 441 |
|
| 442 |
bool FormFieldText::isRichText() const |
| 443 |
{ |
| 444 |
FormWidgetText *fwt = static_cast<FormWidgetText *>(m_formData->fm); |
| 445 |
return fwt->isRichText(); |
| 446 |
} |
| 447 |
|
| 448 |
int FormFieldText::maximumLength() const |
| 449 |
{ |
| 450 |
FormWidgetText *fwt = static_cast<FormWidgetText *>(m_formData->fm); |
| 451 |
const int maxlen = fwt->getMaxLen(); |
| 452 |
return maxlen > 0 ? maxlen : -1; |
| 453 |
} |
| 454 |
|
| 455 |
Qt::Alignment FormFieldText::textAlignment() const |
| 456 |
{ |
| 457 |
return formTextAlignment(m_formData->fm); |
| 458 |
} |
| 459 |
|
| 460 |
bool FormFieldText::canBeSpellChecked() const |
| 461 |
{ |
| 462 |
FormWidgetText *fwt = static_cast<FormWidgetText *>(m_formData->fm); |
| 463 |
return !fwt->noSpellCheck(); |
| 464 |
} |
| 465 |
|
| 466 |
double FormFieldText::getFontSize() const |
| 467 |
{ |
| 468 |
FormWidgetText *fwt = static_cast<FormWidgetText *>(m_formData->fm); |
| 469 |
return fwt->getTextFontSize(); |
| 470 |
} |
| 471 |
|
| 472 |
void FormFieldText::setFontSize(int fontSize) |
| 473 |
{ |
| 474 |
FormWidgetText *fwt = static_cast<FormWidgetText *>(m_formData->fm); |
| 475 |
fwt->setTextFontSize(fontSize); |
| 476 |
} |
| 477 |
|
| 478 |
FormFieldChoice::FormFieldChoice(DocumentData *doc, ::Page *p, ::FormWidgetChoice *w) : FormField(std::make_unique<FormFieldData>(doc, p, w)) { } |
| 479 |
|
| 480 |
FormFieldChoice::~FormFieldChoice() = default; |
| 481 |
|
| 482 |
FormFieldChoice::FormType FormFieldChoice::type() const |
| 483 |
{ |
| 484 |
return FormField::FormChoice; |
| 485 |
} |
| 486 |
|
| 487 |
FormFieldChoice::ChoiceType FormFieldChoice::choiceType() const |
| 488 |
{ |
| 489 |
FormWidgetChoice *fwc = static_cast<FormWidgetChoice *>(m_formData->fm); |
| 490 |
if (fwc->isCombo()) { |
| 491 |
return FormFieldChoice::ComboBox; |
| 492 |
} |
| 493 |
return FormFieldChoice::ListBox; |
| 494 |
} |
| 495 |
|
| 496 |
QStringList FormFieldChoice::choices() const |
| 497 |
{ |
| 498 |
FormWidgetChoice *fwc = static_cast<FormWidgetChoice *>(m_formData->fm); |
| 499 |
QStringList ret; |
| 500 |
int num = fwc->getNumChoices(); |
| 501 |
ret.reserve(num); |
| 502 |
for (int i = 0; i < num; ++i) { |
| 503 |
ret.append(UnicodeParsedString(fwc->getChoice(i))); |
| 504 |
} |
| 505 |
return ret; |
| 506 |
} |
| 507 |
|
| 508 |
QVector<QPair<QString, QString>> FormFieldChoice::choicesWithExportValues() const |
| 509 |
{ |
| 510 |
FormWidgetChoice *fwc = static_cast<FormWidgetChoice *>(m_formData->fm); |
| 511 |
QVector<QPair<QString, QString>> ret; |
| 512 |
const int num = fwc->getNumChoices(); |
| 513 |
ret.reserve(num); |
| 514 |
for (int i = 0; i < num; ++i) { |
| 515 |
const QString display = UnicodeParsedString(fwc->getChoice(i)); |
| 516 |
const GooString *exportValueG = fwc->getExportVal(i); |
| 517 |
const QString exportValue = exportValueG ? UnicodeParsedString(exportValueG) : display; |
| 518 |
ret.append({ display, exportValue }); |
| 519 |
} |
| 520 |
return ret; |
| 521 |
} |
| 522 |
|
| 523 |
bool FormFieldChoice::isEditable() const |
| 524 |
{ |
| 525 |
FormWidgetChoice *fwc = static_cast<FormWidgetChoice *>(m_formData->fm); |
| 526 |
return fwc->isCombo() ? fwc->hasEdit() : false; |
| 527 |
} |
| 528 |
|
| 529 |
bool FormFieldChoice::multiSelect() const |
| 530 |
{ |
| 531 |
FormWidgetChoice *fwc = static_cast<FormWidgetChoice *>(m_formData->fm); |
| 532 |
return !fwc->isCombo() ? fwc->isMultiSelect() : false; |
| 533 |
} |
| 534 |
|
| 535 |
QList<int> FormFieldChoice::currentChoices() const |
| 536 |
{ |
| 537 |
FormWidgetChoice *fwc = static_cast<FormWidgetChoice *>(m_formData->fm); |
| 538 |
int num = fwc->getNumChoices(); |
| 539 |
QList<int> choices; |
| 540 |
for (int i = 0; i < num; ++i) { |
| 541 |
if (fwc->isSelected(i)) { |
| 542 |
choices.append(i); |
| 543 |
} |
| 544 |
} |
| 545 |
return choices; |
| 546 |
} |
| 547 |
|
| 548 |
void FormFieldChoice::setCurrentChoices(const QList<int> &choice) |
| 549 |
{ |
| 550 |
FormWidgetChoice *fwc = static_cast<FormWidgetChoice *>(m_formData->fm); |
| 551 |
fwc->deselectAll(); |
| 552 |
for (int i = 0; i < choice.count(); ++i) { |
| 553 |
fwc->select(choice.at(i)); |
| 554 |
} |
| 555 |
} |
| 556 |
|
| 557 |
QString FormFieldChoice::editChoice() const |
| 558 |
{ |
| 559 |
FormWidgetChoice *fwc = static_cast<FormWidgetChoice *>(m_formData->fm); |
| 560 |
|
| 561 |
if (fwc->isCombo() && fwc->hasEdit()) { |
| 562 |
return UnicodeParsedString(fwc->getEditChoice()); |
| 563 |
} else { |
| 564 |
return QString(); |
| 565 |
} |
| 566 |
} |
| 567 |
|
| 568 |
void FormFieldChoice::setEditChoice(const QString &text) |
| 569 |
{ |
| 570 |
FormWidgetChoice *fwc = static_cast<FormWidgetChoice *>(m_formData->fm); |
| 571 |
|
| 572 |
if (fwc->isCombo() && fwc->hasEdit()) { |
| 573 |
std::unique_ptr<GooString> goo = QStringToUnicodeGooString(text); |
| 574 |
fwc->setEditChoice(std::move(goo)); |
| 575 |
} |
| 576 |
} |
| 577 |
|
| 578 |
Qt::Alignment FormFieldChoice::textAlignment() const |
| 579 |
{ |
| 580 |
return formTextAlignment(m_formData->fm); |
| 581 |
} |
| 582 |
|
| 583 |
bool FormFieldChoice::canBeSpellChecked() const |
| 584 |
{ |
| 585 |
FormWidgetChoice *fwc = static_cast<FormWidgetChoice *>(m_formData->fm); |
| 586 |
return !fwc->noSpellCheck(); |
| 587 |
} |
| 588 |
|
| 589 |
void FormFieldChoice::setAppearanceChoiceText(const QString &text) |
| 590 |
{ |
| 591 |
FormWidgetChoice *fwc = static_cast<FormWidgetChoice *>(m_formData->fm); |
| 592 |
std::unique_ptr<GooString> goo = QStringToUnicodeGooString(text); |
| 593 |
fwc->setAppearanceChoiceContent(std::move(goo)); |
| 594 |
} |
| 595 |
|
| 596 |
class CertificateInfoPrivate |
| 597 |
{ |
| 598 |
public: |
| 599 |
struct EntityInfo |
| 600 |
{ |
| 601 |
QString common_name; |
| 602 |
QString email_address; |
| 603 |
QString org_name; |
| 604 |
QString distinguished_name; |
| 605 |
}; |
| 606 |
|
| 607 |
EntityInfo issuer_info; |
| 608 |
EntityInfo subject_info; |
| 609 |
QString nick_name; |
| 610 |
QByteArray certificate_der; |
| 611 |
QByteArray serial_number; |
| 612 |
QByteArray public_key; |
| 613 |
QDateTime validity_start; |
| 614 |
QDateTime validity_end; |
| 615 |
int public_key_type; |
| 616 |
int public_key_strength; |
| 617 |
int ku_extensions; |
| 618 |
int version; |
| 619 |
bool is_self_signed; |
| 620 |
bool is_null; |
| 621 |
bool is_qualified; |
| 622 |
CertificateInfo::CertificateType certificateType; |
| 623 |
CertificateInfo::KeyLocation keyLocation; |
| 624 |
}; |
| 625 |
|
| 626 |
CertificateInfo::CertificateInfo() : d_ptr(new CertificateInfoPrivate()) |
| 627 |
{ |
| 628 |
d_ptr->is_null = true; |
| 629 |
} |
| 630 |
|
| 631 |
CertificateInfo::CertificateInfo(CertificateInfoPrivate *priv) : d_ptr(priv) { } |
| 632 |
|
| 633 |
CertificateInfo::CertificateInfo(const CertificateInfo &other) = default; |
| 634 |
|
| 635 |
CertificateInfo::~CertificateInfo() = default; |
| 636 |
|
| 637 |
CertificateInfo &CertificateInfo::operator=(const CertificateInfo &other) |
| 638 |
{ |
| 639 |
if (this != &other) { |
| 640 |
d_ptr = other.d_ptr; |
| 641 |
} |
| 642 |
|
| 643 |
return *this; |
| 644 |
} |
| 645 |
|
| 646 |
bool CertificateInfo::isNull() const |
| 647 |
{ |
| 648 |
Q_D(const CertificateInfo); |
| 649 |
return d->is_null; |
| 650 |
} |
| 651 |
|
| 652 |
int CertificateInfo::version() const |
| 653 |
{ |
| 654 |
Q_D(const CertificateInfo); |
| 655 |
return d->version; |
| 656 |
} |
| 657 |
|
| 658 |
QByteArray CertificateInfo::serialNumber() const |
| 659 |
{ |
| 660 |
Q_D(const CertificateInfo); |
| 661 |
return d->serial_number; |
| 662 |
} |
| 663 |
|
| 664 |
bool CertificateInfo::isQualified() const |
| 665 |
{ |
| 666 |
Q_D(const CertificateInfo); |
| 667 |
return d->is_qualified; |
| 668 |
} |
| 669 |
|
| 670 |
CertificateInfo::CertificateType CertificateInfo::certificateType() const |
| 671 |
{ |
| 672 |
Q_D(const CertificateInfo); |
| 673 |
return d->certificateType; |
| 674 |
} |
| 675 |
|
| 676 |
QString CertificateInfo::issuerInfo(EntityInfoKey key) const |
| 677 |
{ |
| 678 |
Q_D(const CertificateInfo); |
| 679 |
switch (key) { |
| 680 |
case CommonName: |
| 681 |
return d->issuer_info.common_name; |
| 682 |
case DistinguishedName: |
| 683 |
return d->issuer_info.distinguished_name; |
| 684 |
case EmailAddress: |
| 685 |
return d->issuer_info.email_address; |
| 686 |
case Organization: |
| 687 |
return d->issuer_info.org_name; |
| 688 |
default: |
| 689 |
return QString(); |
| 690 |
} |
| 691 |
} |
| 692 |
|
| 693 |
QString CertificateInfo::subjectInfo(EntityInfoKey key) const |
| 694 |
{ |
| 695 |
Q_D(const CertificateInfo); |
| 696 |
switch (key) { |
| 697 |
case CommonName: |
| 698 |
return d->subject_info.common_name; |
| 699 |
case DistinguishedName: |
| 700 |
return d->subject_info.distinguished_name; |
| 701 |
case EmailAddress: |
| 702 |
return d->subject_info.email_address; |
| 703 |
case Organization: |
| 704 |
return d->subject_info.org_name; |
| 705 |
default: |
| 706 |
return QString(); |
| 707 |
} |
| 708 |
} |
| 709 |
|
| 710 |
QString CertificateInfo::nickName() const |
| 711 |
{ |
| 712 |
Q_D(const CertificateInfo); |
| 713 |
return d->nick_name; |
| 714 |
} |
| 715 |
|
| 716 |
QDateTime CertificateInfo::validityStart() const |
| 717 |
{ |
| 718 |
Q_D(const CertificateInfo); |
| 719 |
return d->validity_start; |
| 720 |
} |
| 721 |
|
| 722 |
QDateTime CertificateInfo::validityEnd() const |
| 723 |
{ |
| 724 |
Q_D(const CertificateInfo); |
| 725 |
return d->validity_end; |
| 726 |
} |
| 727 |
|
| 728 |
CertificateInfo::KeyUsageExtensions CertificateInfo::keyUsageExtensions() const |
| 729 |
{ |
| 730 |
Q_D(const CertificateInfo); |
| 731 |
|
| 732 |
KeyUsageExtensions kuExtensions = KuNone; |
| 733 |
if (d->ku_extensions & KU_DIGITAL_SIGNATURE) { |
| 734 |
kuExtensions |= KuDigitalSignature; |
| 735 |
} |
| 736 |
if (d->ku_extensions & KU_NON_REPUDIATION) { |
| 737 |
kuExtensions |= KuNonRepudiation; |
| 738 |
} |
| 739 |
if (d->ku_extensions & KU_KEY_ENCIPHERMENT) { |
| 740 |
kuExtensions |= KuKeyEncipherment; |
| 741 |
} |
| 742 |
if (d->ku_extensions & KU_DATA_ENCIPHERMENT) { |
| 743 |
kuExtensions |= KuDataEncipherment; |
| 744 |
} |
| 745 |
if (d->ku_extensions & KU_KEY_AGREEMENT) { |
| 746 |
kuExtensions |= KuKeyAgreement; |
| 747 |
} |
| 748 |
if (d->ku_extensions & KU_KEY_CERT_SIGN) { |
| 749 |
kuExtensions |= KuKeyCertSign; |
| 750 |
} |
| 751 |
if (d->ku_extensions & KU_CRL_SIGN) { |
| 752 |
kuExtensions |= KuClrSign; |
| 753 |
} |
| 754 |
if (d->ku_extensions & KU_ENCIPHER_ONLY) { |
| 755 |
kuExtensions |= KuEncipherOnly; |
| 756 |
} |
| 757 |
|
| 758 |
return kuExtensions; |
| 759 |
} |
| 760 |
|
| 761 |
CertificateInfo::KeyLocation CertificateInfo::keyLocation() const |
| 762 |
{ |
| 763 |
Q_D(const CertificateInfo); |
| 764 |
return d->keyLocation; |
| 765 |
} |
| 766 |
|
| 767 |
QByteArray CertificateInfo::publicKey() const |
| 768 |
{ |
| 769 |
Q_D(const CertificateInfo); |
| 770 |
return d->public_key; |
| 771 |
} |
| 772 |
|
| 773 |
CertificateInfo::PublicKeyType CertificateInfo::publicKeyType() const |
| 774 |
{ |
| 775 |
Q_D(const CertificateInfo); |
| 776 |
switch (d->public_key_type) { |
| 777 |
case RSAKEY: |
| 778 |
return RsaKey; |
| 779 |
case DSAKEY: |
| 780 |
return DsaKey; |
| 781 |
case ECKEY: |
| 782 |
return EcKey; |
| 783 |
default: |
| 784 |
return OtherKey; |
| 785 |
} |
| 786 |
} |
| 787 |
|
| 788 |
int CertificateInfo::publicKeyStrength() const |
| 789 |
{ |
| 790 |
Q_D(const CertificateInfo); |
| 791 |
return d->public_key_strength; |
| 792 |
} |
| 793 |
|
| 794 |
bool CertificateInfo::isSelfSigned() const |
| 795 |
{ |
| 796 |
Q_D(const CertificateInfo); |
| 797 |
return d->is_self_signed; |
| 798 |
} |
| 799 |
|
| 800 |
QByteArray CertificateInfo::certificateData() const |
| 801 |
{ |
| 802 |
Q_D(const CertificateInfo); |
| 803 |
return d->certificate_der; |
| 804 |
} |
| 805 |
|
| 806 |
bool CertificateInfo::checkPassword(const QString &password) const |
| 807 |
{ |
| 808 |
#if ENABLE_SIGNATURES |
| 809 |
auto backend = CryptoSign::Factory::createActive(); |
| 810 |
if (!backend) { |
| 811 |
return false; |
| 812 |
} |
| 813 |
Q_D(const CertificateInfo); |
| 814 |
auto sigHandler = backend->createSigningHandler(d->nick_name.toStdString(), HashAlgorithm::Sha256); |
| 815 |
unsigned char buffer[5]; |
| 816 |
memcpy(buffer, "test", 5); |
| 817 |
sigHandler->addData(buffer, 5); |
| 818 |
std::variant<std::vector<unsigned char>, CryptoSign::SigningErrorMessage> tmpSignature = sigHandler->signDetached(password.toStdString()); |
| 819 |
return std::holds_alternative<std::vector<unsigned char>>(tmpSignature); |
| 820 |
#else |
| 821 |
(void)password; |
| 822 |
return false; |
| 823 |
#endif |
| 824 |
} |
| 825 |
|
| 826 |
class SignatureValidationInfoPrivate |
| 827 |
{ |
| 828 |
public: |
| 829 |
explicit SignatureValidationInfoPrivate(CertificateInfo &&ci) : cert_info(ci) { } |
| 830 |
|
| 831 |
SignatureValidationInfo::SignatureStatus signature_status; |
| 832 |
SignatureValidationInfo::CertificateStatus certificate_status; |
| 833 |
CertificateInfo cert_info; |
| 834 |
|
| 835 |
QByteArray signature; |
| 836 |
QString signer_name; |
| 837 |
QString signer_subject_dn; |
| 838 |
QString location; |
| 839 |
QString reason; |
| 840 |
HashAlgorithm hash_algorithm; |
| 841 |
time_t signing_time; |
| 842 |
QList<qint64> range_bounds; |
| 843 |
qint64 docLength; |
| 844 |
}; |
| 845 |
|
| 846 |
SignatureValidationInfo::SignatureValidationInfo(SignatureValidationInfoPrivate *priv) : d_ptr(priv) { } |
| 847 |
|
| 848 |
SignatureValidationInfo::SignatureValidationInfo(const SignatureValidationInfo &other) = default; |
| 849 |
|
| 850 |
SignatureValidationInfo::~SignatureValidationInfo() = default; |
| 851 |
|
| 852 |
SignatureValidationInfo::SignatureStatus SignatureValidationInfo::signatureStatus() const |
| 853 |
{ |
| 854 |
Q_D(const SignatureValidationInfo); |
| 855 |
return d->signature_status; |
| 856 |
} |
| 857 |
|
| 858 |
SignatureValidationInfo::CertificateStatus SignatureValidationInfo::certificateStatus() const |
| 859 |
{ |
| 860 |
Q_D(const SignatureValidationInfo); |
| 861 |
return d->certificate_status; |
| 862 |
} |
| 863 |
|
| 864 |
QString SignatureValidationInfo::signerName() const |
| 865 |
{ |
| 866 |
Q_D(const SignatureValidationInfo); |
| 867 |
return d->signer_name; |
| 868 |
} |
| 869 |
|
| 870 |
QString SignatureValidationInfo::signerSubjectDN() const |
| 871 |
{ |
| 872 |
Q_D(const SignatureValidationInfo); |
| 873 |
return d->signer_subject_dn; |
| 874 |
} |
| 875 |
|
| 876 |
QString SignatureValidationInfo::location() const |
| 877 |
{ |
| 878 |
Q_D(const SignatureValidationInfo); |
| 879 |
return d->location; |
| 880 |
} |
| 881 |
|
| 882 |
QString SignatureValidationInfo::reason() const |
| 883 |
{ |
| 884 |
Q_D(const SignatureValidationInfo); |
| 885 |
return d->reason; |
| 886 |
} |
| 887 |
|
| 888 |
SignatureValidationInfo::HashAlgorithm SignatureValidationInfo::hashAlgorithm() const |
| 889 |
{ |
| 890 |
#if ENABLE_SIGNATURES |
| 891 |
Q_D(const SignatureValidationInfo); |
| 892 |
|
| 893 |
switch (d->hash_algorithm) { |
| 894 |
case ::HashAlgorithm::Md2: |
| 895 |
return HashAlgorithmMd2; |
| 896 |
case ::HashAlgorithm::Md5: |
| 897 |
return HashAlgorithmMd5; |
| 898 |
case ::HashAlgorithm::Sha1: |
| 899 |
return HashAlgorithmSha1; |
| 900 |
case ::HashAlgorithm::Sha256: |
| 901 |
return HashAlgorithmSha256; |
| 902 |
case ::HashAlgorithm::Sha384: |
| 903 |
return HashAlgorithmSha384; |
| 904 |
case ::HashAlgorithm::Sha512: |
| 905 |
return HashAlgorithmSha512; |
| 906 |
case ::HashAlgorithm::Sha224: |
| 907 |
return HashAlgorithmSha224; |
| 908 |
case ::HashAlgorithm::Unknown: |
| 909 |
return HashAlgorithmUnknown; |
| 910 |
} |
| 911 |
#endif |
| 912 |
return HashAlgorithmUnknown; |
| 913 |
} |
| 914 |
|
| 915 |
time_t SignatureValidationInfo::signingTime() const |
| 916 |
{ |
| 917 |
Q_D(const SignatureValidationInfo); |
| 918 |
return d->signing_time; |
| 919 |
} |
| 920 |
|
| 921 |
QByteArray SignatureValidationInfo::signature() const |
| 922 |
{ |
| 923 |
Q_D(const SignatureValidationInfo); |
| 924 |
return d->signature; |
| 925 |
} |
| 926 |
|
| 927 |
QList<qint64> SignatureValidationInfo::signedRangeBounds() const |
| 928 |
{ |
| 929 |
Q_D(const SignatureValidationInfo); |
| 930 |
return d->range_bounds; |
| 931 |
} |
| 932 |
|
| 933 |
bool SignatureValidationInfo::signsTotalDocument() const |
| 934 |
{ |
| 935 |
Q_D(const SignatureValidationInfo); |
| 936 |
if (d->range_bounds.size() == 4 && d->range_bounds.value(0) == 0 && d->range_bounds.value(1) >= 0 && d->range_bounds.value(2) > d->range_bounds.value(1) && d->range_bounds.value(3) >= d->range_bounds.value(2)) { |
| 937 |
// The range from d->range_bounds.value(1) to d->range_bounds.value(2) is |
| 938 |
// not authenticated by the signature and should only contain the signature |
| 939 |
// itself padded with 0 bytes. This has been checked in readSignature(). |
| 940 |
// If it failed, d->signature is empty. |
| 941 |
// A potential range after d->range_bounds.value(3) would be also not |
| 942 |
// authenticated. Therefore d->range_bounds.value(3) should coincide with |
| 943 |
// the end of the document. |
| 944 |
if (d->docLength == d->range_bounds.value(3) && !d->signature.isEmpty()) { |
| 945 |
return true; |
| 946 |
} |
| 947 |
} |
| 948 |
return false; |
| 949 |
} |
| 950 |
|
| 951 |
CertificateInfo SignatureValidationInfo::certificateInfo() const |
| 952 |
{ |
| 953 |
Q_D(const SignatureValidationInfo); |
| 954 |
return d->cert_info; |
| 955 |
} |
| 956 |
|
| 957 |
SignatureValidationInfo &SignatureValidationInfo::operator=(const SignatureValidationInfo &other) |
| 958 |
{ |
| 959 |
if (this != &other) { |
| 960 |
d_ptr = other.d_ptr; |
| 961 |
} |
| 962 |
|
| 963 |
return *this; |
| 964 |
} |
| 965 |
|
| 966 |
FormFieldSignature::FormFieldSignature(DocumentData *doc, ::Page *p, ::FormWidgetSignature *w) : FormField(std::make_unique<FormFieldData>(doc, p, w)) { } |
| 967 |
|
| 968 |
FormFieldSignature::~FormFieldSignature() = default; |
| 969 |
|
| 970 |
FormField::FormType FormFieldSignature::type() const |
| 971 |
{ |
| 972 |
return FormField::FormSignature; |
| 973 |
} |
| 974 |
|
| 975 |
FormFieldSignature::SignatureType FormFieldSignature::signatureType() const |
| 976 |
{ |
| 977 |
SignatureType sigType = AdbePkcs7detached; |
| 978 |
FormWidgetSignature *fws = static_cast<FormWidgetSignature *>(m_formData->fm); |
| 979 |
switch (fws->signatureType()) { |
| 980 |
case CryptoSign::SignatureType::adbe_pkcs7_sha1: |
| 981 |
sigType = AdbePkcs7sha1; |
| 982 |
break; |
| 983 |
case CryptoSign::SignatureType::adbe_pkcs7_detached: |
| 984 |
sigType = AdbePkcs7detached; |
| 985 |
break; |
| 986 |
case CryptoSign::SignatureType::ETSI_CAdES_detached: |
| 987 |
sigType = EtsiCAdESdetached; |
| 988 |
break; |
| 989 |
case CryptoSign::SignatureType::unknown_signature_type: |
| 990 |
sigType = UnknownSignatureType; |
| 991 |
break; |
| 992 |
case CryptoSign::SignatureType::g10c_pgp_signature_detached: |
| 993 |
sigType = G10cPgpSignatureDetached; |
| 994 |
break; |
| 995 |
case CryptoSign::SignatureType::unsigned_signature_field: |
| 996 |
sigType = UnsignedSignature; |
| 997 |
break; |
| 998 |
} |
| 999 |
return sigType; |
| 1000 |
} |
| 1001 |
|
| 1002 |
SignatureValidationInfo FormFieldSignature::validate(ValidateOptions opt) const |
| 1003 |
{ |
| 1004 |
auto tempResult = validateAsync(opt); |
| 1005 |
tempResult.first.d_ptr->certificate_status = validateResult(); |
| 1006 |
return tempResult.first; |
| 1007 |
} |
| 1008 |
|
| 1009 |
static CertificateInfo::CertificateType fromPopplerCore(CertificateType type) |
| 1010 |
{ |
| 1011 |
switch (type) { |
| 1012 |
case CertificateType::PGP: |
| 1013 |
return CertificateInfo::CertificateType::PGP; |
| 1014 |
case CertificateType::X509: |
| 1015 |
return CertificateInfo::CertificateType::X509; |
| 1016 |
} |
| 1017 |
return CertificateInfo::CertificateType::X509; // fallback |
| 1018 |
} |
| 1019 |
|
| 1020 |
static CertificateInfo::KeyLocation fromPopplerCore(KeyLocation location) |
| 1021 |
{ |
| 1022 |
switch (location) { |
| 1023 |
case KeyLocation::Computer: |
| 1024 |
return CertificateInfo::KeyLocation::Computer; |
| 1025 |
case KeyLocation::Other: |
| 1026 |
return CertificateInfo::KeyLocation::Other; |
| 1027 |
case KeyLocation::Unknown: |
| 1028 |
return CertificateInfo::KeyLocation::Unknown; |
| 1029 |
case KeyLocation::HardwareToken: |
| 1030 |
return CertificateInfo::KeyLocation::HardwareToken; |
| 1031 |
} |
| 1032 |
return CertificateInfo::KeyLocation::Unknown; |
| 1033 |
} |
| 1034 |
|
| 1035 |
static CertificateInfoPrivate *createCertificateInfoPrivate(const X509CertificateInfo *ci) |
| 1036 |
{ |
| 1037 |
CertificateInfoPrivate *certPriv = new CertificateInfoPrivate; |
| 1038 |
certPriv->is_null = true; |
| 1039 |
if (ci) { |
| 1040 |
certPriv->version = ci->getVersion(); |
| 1041 |
certPriv->ku_extensions = ci->getKeyUsageExtensions(); |
| 1042 |
certPriv->keyLocation = fromPopplerCore(ci->getKeyLocation()); |
| 1043 |
certPriv->certificateType = fromPopplerCore(ci->getCertificateType()); |
| 1044 |
|
| 1045 |
const GooString &certSerial = ci->getSerialNumber(); |
| 1046 |
certPriv->serial_number = QByteArray(certSerial.c_str(), certSerial.size()); |
| 1047 |
|
| 1048 |
const X509CertificateInfo::EntityInfo &issuerInfo = ci->getIssuerInfo(); |
| 1049 |
certPriv->issuer_info.common_name = QString::fromStdString(issuerInfo.commonName); |
| 1050 |
certPriv->issuer_info.distinguished_name = QString::fromStdString(issuerInfo.distinguishedName); |
| 1051 |
certPriv->issuer_info.email_address = QString::fromStdString(issuerInfo.email); |
| 1052 |
certPriv->issuer_info.org_name = QString::fromStdString(issuerInfo.organization); |
| 1053 |
|
| 1054 |
const X509CertificateInfo::EntityInfo &subjectInfo = ci->getSubjectInfo(); |
| 1055 |
certPriv->subject_info.common_name = QString::fromStdString(subjectInfo.commonName); |
| 1056 |
certPriv->subject_info.distinguished_name = QString::fromStdString(subjectInfo.distinguishedName); |
| 1057 |
certPriv->subject_info.email_address = QString::fromStdString(subjectInfo.email); |
| 1058 |
certPriv->subject_info.org_name = QString::fromStdString(subjectInfo.organization); |
| 1059 |
|
| 1060 |
certPriv->nick_name = QString::fromStdString(ci->getNickName().toStr()); |
| 1061 |
|
| 1062 |
X509CertificateInfo::Validity certValidity = ci->getValidity(); |
| 1063 |
certPriv->validity_start = QDateTime::fromSecsSinceEpoch(certValidity.notBefore, QTimeZone::utc()); |
| 1064 |
certPriv->validity_end = QDateTime::fromSecsSinceEpoch(certValidity.notAfter, QTimeZone::utc()); |
| 1065 |
|
| 1066 |
const X509CertificateInfo::PublicKeyInfo &pkInfo = ci->getPublicKeyInfo(); |
| 1067 |
certPriv->public_key = QByteArray(pkInfo.publicKey.c_str(), pkInfo.publicKey.size()); |
| 1068 |
certPriv->public_key_type = static_cast<int>(pkInfo.publicKeyType); |
| 1069 |
certPriv->public_key_strength = pkInfo.publicKeyStrength; |
| 1070 |
|
| 1071 |
const GooString &certDer = ci->getCertificateDER(); |
| 1072 |
certPriv->certificate_der = QByteArray(certDer.c_str(), certDer.size()); |
| 1073 |
|
| 1074 |
certPriv->is_null = false; |
| 1075 |
certPriv->is_qualified = ci->isQualified(); |
| 1076 |
} |
| 1077 |
|
| 1078 |
return certPriv; |
| 1079 |
} |
| 1080 |
|
| 1081 |
static SignatureValidationInfo::CertificateStatus fromInternal(CertificateValidationStatus status) |
| 1082 |
{ |
| 1083 |
switch (status) { |
| 1084 |
case CERTIFICATE_TRUSTED: |
| 1085 |
return SignatureValidationInfo::CertificateTrusted; |
| 1086 |
case CERTIFICATE_UNTRUSTED_ISSUER: |
| 1087 |
return SignatureValidationInfo::CertificateUntrustedIssuer; |
| 1088 |
case CERTIFICATE_UNKNOWN_ISSUER: |
| 1089 |
return SignatureValidationInfo::CertificateUnknownIssuer; |
| 1090 |
case CERTIFICATE_REVOKED: |
| 1091 |
return SignatureValidationInfo::CertificateRevoked; |
| 1092 |
case CERTIFICATE_EXPIRED: |
| 1093 |
return SignatureValidationInfo::CertificateExpired; |
| 1094 |
default: |
| 1095 |
case CERTIFICATE_GENERIC_ERROR: |
| 1096 |
return SignatureValidationInfo::CertificateGenericError; |
| 1097 |
case CERTIFICATE_NOT_VERIFIED: |
| 1098 |
return SignatureValidationInfo::CertificateNotVerified; |
| 1099 |
} |
| 1100 |
} |
| 1101 |
|
| 1102 |
static SignatureValidationInfo fromInternal(SignatureInfo *si, FormWidgetSignature *fws) |
| 1103 |
{ |
| 1104 |
// get certificate info |
| 1105 |
const X509CertificateInfo *ci = si->getCertificateInfo(); |
| 1106 |
CertificateInfoPrivate *certPriv = createCertificateInfoPrivate(ci); |
| 1107 |
|
| 1108 |
SignatureValidationInfoPrivate *priv = new SignatureValidationInfoPrivate(CertificateInfo(certPriv)); |
| 1109 |
switch (si->getSignatureValStatus()) { |
| 1110 |
case SIGNATURE_VALID: |
| 1111 |
priv->signature_status = SignatureValidationInfo::SignatureValid; |
| 1112 |
break; |
| 1113 |
case SIGNATURE_INVALID: |
| 1114 |
priv->signature_status = SignatureValidationInfo::SignatureInvalid; |
| 1115 |
break; |
| 1116 |
case SIGNATURE_DIGEST_MISMATCH: |
| 1117 |
priv->signature_status = SignatureValidationInfo::SignatureDigestMismatch; |
| 1118 |
break; |
| 1119 |
case SIGNATURE_DECODING_ERROR: |
| 1120 |
priv->signature_status = SignatureValidationInfo::SignatureDecodingError; |
| 1121 |
break; |
| 1122 |
default: |
| 1123 |
case SIGNATURE_GENERIC_ERROR: |
| 1124 |
priv->signature_status = SignatureValidationInfo::SignatureGenericError; |
| 1125 |
break; |
| 1126 |
case SIGNATURE_NOT_FOUND: |
| 1127 |
priv->signature_status = SignatureValidationInfo::SignatureNotFound; |
| 1128 |
break; |
| 1129 |
case SIGNATURE_NOT_VERIFIED: |
| 1130 |
priv->signature_status = SignatureValidationInfo::SignatureNotVerified; |
| 1131 |
break; |
| 1132 |
} |
| 1133 |
priv->certificate_status = SignatureValidationInfo::CertificateVerificationInProgress; |
| 1134 |
priv->signer_name = QString::fromStdString(si->getSignerName()); |
| 1135 |
priv->signer_subject_dn = QString::fromStdString(si->getSubjectDN()); |
| 1136 |
priv->hash_algorithm = si->getHashAlgorithm(); |
| 1137 |
priv->location = UnicodeParsedString(si->getLocation().toStr()); |
| 1138 |
priv->reason = UnicodeParsedString(si->getReason().toStr()); |
| 1139 |
|
| 1140 |
priv->signing_time = si->getSigningTime(); |
| 1141 |
const std::vector<Goffset> ranges = fws->getSignedRangeBounds(); |
| 1142 |
if (!ranges.empty()) { |
| 1143 |
for (Goffset bound : ranges) { |
| 1144 |
priv->range_bounds.append(bound); |
| 1145 |
} |
| 1146 |
} |
| 1147 |
std::optional<std::vector<unsigned char>> checkedSignature; |
| 1148 |
std::tie(checkedSignature, priv->docLength) = fws->getCheckedSignature(); |
| 1149 |
if (priv->range_bounds.size() == 4 && checkedSignature) { |
| 1150 |
priv->signature = QByteArray::fromRawData(reinterpret_cast<const char *>(checkedSignature->data()), checkedSignature->size()); |
| 1151 |
} |
| 1152 |
|
| 1153 |
return SignatureValidationInfo(priv); |
| 1154 |
} |
| 1155 |
|
| 1156 |
SignatureValidationInfo FormFieldSignature::validate(int opt, const QDateTime &validationTime) const |
| 1157 |
{ |
| 1158 |
auto tempResult = validateAsync(static_cast<ValidateOptions>(opt), validationTime); |
| 1159 |
tempResult.first.d_ptr->certificate_status = validateResult(); |
| 1160 |
return tempResult.first; |
| 1161 |
} |
| 1162 |
|
| 1163 |
class AsyncObjectPrivate |
| 1164 |
{ /*Currently unused. Created for abi future proofing*/ |
| 1165 |
}; |
| 1166 |
|
| 1167 |
AsyncObject::AsyncObject() : QObject(nullptr) { } |
| 1168 |
|
| 1169 |
AsyncObject::~AsyncObject() = default; |
| 1170 |
|
| 1171 |
std::pair<SignatureValidationInfo, std::shared_ptr<Poppler::AsyncObject>> FormFieldSignature::validateAsync(ValidateOptions opt, const QDateTime &validationTime) const |
| 1172 |
{ |
| 1173 |
auto object = std::make_shared<AsyncObject>(); |
| 1174 |
FormWidgetSignature *fws = static_cast<FormWidgetSignature *>(m_formData->fm); |
| 1175 |
const time_t validationTimeT = validationTime.isValid() ? validationTime.toSecsSinceEpoch() : -1; |
| 1176 |
SignatureInfo *si = fws->validateSignatureAsync(opt & ValidateVerifyCertificate, opt & ValidateForceRevalidation, validationTimeT, !(opt & ValidateWithoutOCSPRevocationCheck), opt & ValidateUseAIACertFetch, |
| 1177 |
[obj = std::weak_ptr<AsyncObject>(object)]() { |
| 1178 |
if (auto l = obj.lock()) { |
| 1179 |
// We need to roundtrip over the eventloop |
| 1180 |
// to ensure callers have a chance of connecting to AsyncObject::done |
| 1181 |
QMetaObject::invokeMethod( |
| 1182 |
l.get(), |
| 1183 |
[innerObj = std::weak_ptr<AsyncObject>(l)]() { |
| 1184 |
if (auto innerLocked = innerObj.lock()) { |
| 1185 |
Q_EMIT innerLocked->done(); |
| 1186 |
} |
| 1187 |
}, |
| 1188 |
Qt::QueuedConnection); |
| 1189 |
} |
| 1190 |
}); |
| 1191 |
|
| 1192 |
return { fromInternal(si, fws), object }; |
| 1193 |
} |
| 1194 |
|
| 1195 |
SignatureValidationInfo::CertificateStatus FormFieldSignature::validateResult() const |
| 1196 |
{ |
| 1197 |
return fromInternal(static_cast<FormWidgetSignature *>(m_formData->fm)->validateSignatureResult()); |
| 1198 |
} |
| 1199 |
|
| 1200 |
FormFieldSignature::SigningResult FormFieldSignature::sign(const QString &outputFileName, const PDFConverter::NewSignatureData &data) const |
| 1201 |
{ |
| 1202 |
FormWidgetSignature *fws = static_cast<FormWidgetSignature *>(m_formData->fm); |
| 1203 |
if (fws->signatureType() != CryptoSign::SignatureType::unsigned_signature_field) { |
| 1204 |
return FieldAlreadySigned; |
| 1205 |
} |
| 1206 |
|
| 1207 |
const auto [sig, file_size] = fws->getCheckedSignature(); |
| 1208 |
if (sig) { |
| 1209 |
// the above unsigned_signature_field check |
| 1210 |
// should already catch this, but double check |
| 1211 |
return FieldAlreadySigned; |
| 1212 |
} |
| 1213 |
const auto reason = std::unique_ptr<GooString>(data.reason().isEmpty() ? nullptr : QStringToUnicodeGooString(data.reason())); |
| 1214 |
const auto location = std::unique_ptr<GooString>(data.location().isEmpty() ? nullptr : QStringToUnicodeGooString(data.location())); |
| 1215 |
const auto ownerPwd = std::optional<GooString>(data.documentOwnerPassword().constData()); |
| 1216 |
const auto userPwd = std::optional<GooString>(data.documentUserPassword().constData()); |
| 1217 |
const auto gSignatureText = std::unique_ptr<GooString>(QStringToUnicodeGooString(data.signatureText())); |
| 1218 |
const auto gSignatureLeftText = std::unique_ptr<GooString>(QStringToUnicodeGooString(data.signatureLeftText())); |
| 1219 |
|
| 1220 |
const auto failure = fws->signDocumentWithAppearance(outputFileName.toStdString(), data.certNickname().toStdString(), data.password().toStdString(), reason.get(), location.get(), ownerPwd, userPwd, *gSignatureText, *gSignatureLeftText, |
| 1221 |
data.fontSize(), data.leftFontSize(), convertQColor(data.fontColor()), data.borderWidth(), convertQColor(data.borderColor()), convertQColor(data.backgroundColor())); |
| 1222 |
if (failure) { |
| 1223 |
m_formData->lastSigningErrorDetails = fromPopplerCore(failure.value().message); |
| 1224 |
switch (failure.value().type) { |
| 1225 |
case CryptoSign::SigningError::GenericError: |
| 1226 |
return GenericSigningError; |
| 1227 |
case CryptoSign::SigningError::InternalError: |
| 1228 |
return InternalError; |
| 1229 |
case CryptoSign::SigningError::KeyMissing: |
| 1230 |
return KeyMissing; |
| 1231 |
case CryptoSign::SigningError::UserCancelled: |
| 1232 |
return UserCancelled; |
| 1233 |
case CryptoSign::SigningError::WriteFailed: |
| 1234 |
return WriteFailed; |
| 1235 |
case CryptoSign::SigningError::BadPassphrase: |
| 1236 |
return BadPassphrase; |
| 1237 |
} |
| 1238 |
return GenericSigningError; |
| 1239 |
} |
| 1240 |
m_formData->lastSigningErrorDetails = {}; |
| 1241 |
return SigningSuccess; |
| 1242 |
} |
| 1243 |
|
| 1244 |
ErrorString FormFieldSignature::lastSigningErrorDetails() const |
| 1245 |
{ |
| 1246 |
return m_formData->lastSigningErrorDetails; |
| 1247 |
} |
| 1248 |
|
| 1249 |
bool hasNSSSupport() |
| 1250 |
{ |
| 1251 |
#if ENABLE_NSS3 |
| 1252 |
return true; |
| 1253 |
#else |
| 1254 |
return false; |
| 1255 |
#endif |
| 1256 |
} |
| 1257 |
|
| 1258 |
QVector<CertificateInfo> getAvailableSigningCertificates() |
| 1259 |
{ |
| 1260 |
auto backend = CryptoSign::Factory::createActive(); |
| 1261 |
if (!backend) { |
| 1262 |
return {}; |
| 1263 |
} |
| 1264 |
QVector<CertificateInfo> vReturnCerts; |
| 1265 |
std::vector<std::unique_ptr<X509CertificateInfo>> vCerts = backend->getAvailableSigningCertificates(); |
| 1266 |
|
| 1267 |
for (auto &cert : vCerts) { |
| 1268 |
CertificateInfoPrivate *certPriv = createCertificateInfoPrivate(cert.get()); |
| 1269 |
vReturnCerts.append(CertificateInfo(certPriv)); |
| 1270 |
} |
| 1271 |
|
| 1272 |
return vReturnCerts; |
| 1273 |
} |
| 1274 |
|
| 1275 |
static std::optional<CryptoSignBackend> convertToFrontend(std::optional<CryptoSign::Backend::Type> type) |
| 1276 |
{ |
| 1277 |
if (!type) { |
| 1278 |
return std::nullopt; |
| 1279 |
} |
| 1280 |
switch (type.value()) { |
| 1281 |
case CryptoSign::Backend::Type::NSS3: |
| 1282 |
return CryptoSignBackend::NSS; |
| 1283 |
case CryptoSign::Backend::Type::GPGME: |
| 1284 |
return CryptoSignBackend::GPG; |
| 1285 |
} |
| 1286 |
return std::nullopt; |
| 1287 |
} |
| 1288 |
|
| 1289 |
static std::optional<CryptoSign::Backend::Type> convertToBackend(std::optional<CryptoSignBackend> backend) |
| 1290 |
{ |
| 1291 |
if (!backend) { |
| 1292 |
return std::nullopt; |
| 1293 |
} |
| 1294 |
|
| 1295 |
switch (backend.value()) { |
| 1296 |
case CryptoSignBackend::NSS: |
| 1297 |
return CryptoSign::Backend::Type::NSS3; |
| 1298 |
case CryptoSignBackend::GPG: |
| 1299 |
return CryptoSign::Backend::Type::GPGME; |
| 1300 |
} |
| 1301 |
return std::nullopt; |
| 1302 |
} |
| 1303 |
|
| 1304 |
QVector<CryptoSignBackend> availableCryptoSignBackends() |
| 1305 |
{ |
| 1306 |
QVector<CryptoSignBackend> backends; |
| 1307 |
for (auto &backend : CryptoSign::Factory::getAvailable()) { |
| 1308 |
auto converted = convertToFrontend(backend); |
| 1309 |
if (converted) { |
| 1310 |
backends.push_back(converted.value()); |
| 1311 |
} |
| 1312 |
} |
| 1313 |
return backends; |
| 1314 |
} |
| 1315 |
|
| 1316 |
std::optional<CryptoSignBackend> activeCryptoSignBackend() |
| 1317 |
{ |
| 1318 |
return convertToFrontend(CryptoSign::Factory::getActive()); |
| 1319 |
} |
| 1320 |
|
| 1321 |
bool setActiveCryptoSignBackend(CryptoSignBackend backend) |
| 1322 |
{ |
| 1323 |
auto available = availableCryptoSignBackends(); |
| 1324 |
if (!available.contains(backend)) { |
| 1325 |
return false; |
| 1326 |
} |
| 1327 |
auto converted = convertToBackend(backend); |
| 1328 |
if (!converted) { |
| 1329 |
return false; |
| 1330 |
} |
| 1331 |
CryptoSign::Factory::setPreferredBackend(converted.value()); |
| 1332 |
return activeCryptoSignBackend() == backend; |
| 1333 |
} |
| 1334 |
|
| 1335 |
static bool hasNSSBackendFeature(CryptoSignBackendFeature feature) |
| 1336 |
{ |
| 1337 |
switch (feature) { |
| 1338 |
case CryptoSignBackendFeature::BackendAsksPassphrase: |
| 1339 |
return false; |
| 1340 |
} |
| 1341 |
return false; |
| 1342 |
} |
| 1343 |
|
| 1344 |
static bool hasGPGBackendFeature(CryptoSignBackendFeature feature) |
| 1345 |
{ |
| 1346 |
switch (feature) { |
| 1347 |
case CryptoSignBackendFeature::BackendAsksPassphrase: |
| 1348 |
return true; |
| 1349 |
} |
| 1350 |
return false; |
| 1351 |
} |
| 1352 |
|
| 1353 |
bool hasCryptoSignBackendFeature(CryptoSignBackend backend, CryptoSignBackendFeature feature) |
| 1354 |
{ |
| 1355 |
switch (backend) { |
| 1356 |
case CryptoSignBackend::NSS: |
| 1357 |
return hasNSSBackendFeature(feature); |
| 1358 |
case CryptoSignBackend::GPG: |
| 1359 |
return hasGPGBackendFeature(feature); |
| 1360 |
} |
| 1361 |
return false; |
| 1362 |
} |
| 1363 |
|
| 1364 |
QString POPPLER_QT6_EXPORT getNSSDir() |
| 1365 |
{ |
| 1366 |
#if ENABLE_NSS3 |
| 1367 |
return QString::fromLocal8Bit(NSSSignatureConfiguration::getNSSDir().c_str()); |
| 1368 |
#else |
| 1369 |
return QString(); |
| 1370 |
#endif |
| 1371 |
} |
| 1372 |
|
| 1373 |
void setNSSDir(const QString &path) |
| 1374 |
{ |
| 1375 |
#if ENABLE_NSS3 |
| 1376 |
if (path.isEmpty()) { |
| 1377 |
return; |
| 1378 |
} |
| 1379 |
|
| 1380 |
const std::unique_ptr<GooString> goo = QStringToGooString(path); |
| 1381 |
NSSSignatureConfiguration::setNSSDir(*goo); |
| 1382 |
#else |
| 1383 |
(void)path; |
| 1384 |
#endif |
| 1385 |
} |
| 1386 |
|
| 1387 |
namespace { |
| 1388 |
std::function<QString(const QString &)> nssPasswordCall; |
| 1389 |
} |
| 1390 |
|
| 1391 |
void setNSSPasswordCallback(const std::function<char *(const char *)> &f) |
| 1392 |
{ |
| 1393 |
#if ENABLE_NSS3 |
| 1394 |
NSSSignatureConfiguration::setNSSPasswordCallback(f); |
| 1395 |
#else |
| 1396 |
qWarning() << "setNSSPasswordCallback called but this poppler is built without NSS support"; |
| 1397 |
(void)f; |
| 1398 |
#endif |
| 1399 |
} |
| 1400 |
|
| 1401 |
void setPgpSignaturesAllowed(bool allowed) |
| 1402 |
{ |
| 1403 |
#if ENABLE_GPGME |
| 1404 |
GpgSignatureConfiguration::setPgpSignaturesAllowed(allowed); |
| 1405 |
#else |
| 1406 |
qWarning() << "Trying to enable pgp signatures, but pgp not enabled in this build"; |
| 1407 |
(void)allowed; |
| 1408 |
#endif |
| 1409 |
} |
| 1410 |
|
| 1411 |
bool arePgpSignaturesAllowed() |
| 1412 |
{ |
| 1413 |
#if ENABLE_GPGME |
| 1414 |
return GpgSignatureConfiguration::arePgpSignaturesAllowed(); |
| 1415 |
#else |
| 1416 |
return false; |
| 1417 |
#endif |
| 1418 |
} |
| 1419 |
} |