Skip to content

Commit

Permalink
Handle ASiC-S XAdES signatures (#1203)
Browse files Browse the repository at this point in the history
IB-7593

Signed-off-by: Raul Metsma <raul@metsma.ee>
  • Loading branch information
metsma authored Sep 15, 2023
1 parent 3f5a925 commit a7a80bd
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 24 deletions.
27 changes: 17 additions & 10 deletions client/DigiDoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,19 +335,17 @@ void SDocumentModel::open(int row)
{
if(row >= rowCount())
return;

QString path = FileDialog::tempPath(FileDialog::safeName(from(doc->b->dataFiles().at(size_t(row))->fileName())));
if(!verifyFile(path))
return;
QFileInfo f(save(row, path));
if( !f.exists() )
if(!QFileInfo::exists(save(row, path)))
return;
doc->m_tempFiles.append(f.absoluteFilePath());
FileDialog::setReadOnly(f.absoluteFilePath());
if(!doc->fileName().endsWith(QStringLiteral(".pdf"), Qt::CaseInsensitive) && FileDialog::isSignedPDF(f.absoluteFilePath()))
qApp->showClient({ f.absoluteFilePath() }, false, false, true);
doc->m_tempFiles.append(path);
FileDialog::setReadOnly(path);
if(!doc->fileName().endsWith(QLatin1String(".pdf"), Qt::CaseInsensitive) && FileDialog::isSignedPDF(path))
qApp->showClient({ path }, false, false, true);
else
QDesktopServices::openUrl(QUrl::fromLocalFile(f.absoluteFilePath()));
QDesktopServices::openUrl(QUrl::fromLocalFile(path));
}

bool SDocumentModel::removeRow(int row)
Expand Down Expand Up @@ -447,11 +445,20 @@ bool DigiDoc::isError(bool failure, const QString &msg) const
return !b || failure;
}

bool DigiDoc::isAsicS() const
{
return b && b->mediaType() == "application/vnd.etsi.asic-s+zip" &&
std::any_of(m_signatures.cbegin(), m_signatures.cend(), [](const DigiDocSignature &s) {
return s.profile().contains(QLatin1String("BES"), Qt::CaseInsensitive);
});
}

bool DigiDoc::isPDF() const
{
return b && b->mediaType() == "application/pdf";
}
bool DigiDoc::isModified() const { return modified; }

bool DigiDoc::isSupported() const
{
return b && b->mediaType() == "application/vnd.etsi.asic-e+zip";
Expand Down Expand Up @@ -493,8 +500,8 @@ bool DigiDoc::open( const QString &file )
dlg->addButton(WarningDialog::YES, ContainerSave);
return dlg->exec() == ContainerSave;
};
if((file.endsWith(QStringLiteral(".pdf"), Qt::CaseInsensitive) ||
file.endsWith(QStringLiteral(".ddoc"), Qt::CaseInsensitive)) && !serviceConfirmation())
if((file.endsWith(QLatin1String(".pdf"), Qt::CaseInsensitive) ||
file.endsWith(QLatin1String(".ddoc"), Qt::CaseInsensitive)) && !serviceConfirmation())
return false;

try {
Expand Down
1 change: 1 addition & 0 deletions client/DigiDoc.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ class DigiDoc: public QObject
void clear();
DocumentModel *documentModel() const;
QString fileName() const;
bool isAsicS() const;
bool isPDF() const;
bool isModified() const;
bool isSupported() const;
Expand Down
1 change: 1 addition & 0 deletions client/common_enums.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ enum WarningType {
InvalidTimestampWarning,
UnknownSignatureWarning,
UnknownTimestampWarning,
UnsupportedAsicSWarning,
UnsupportedDDocWarning,
EmptyFileWarning,
};
Expand Down
25 changes: 11 additions & 14 deletions client/dialogs/FileDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#ifdef Q_OS_WIN
#include <ShObjIdl.h>
#include <ShlGuid.h>
#include <fileapi.h>

template <class T>
class CPtr
Expand Down Expand Up @@ -125,19 +126,14 @@ int FileDialog::fileZone(const QString &path)

bool FileDialog::isSignedPDF(const QString &path)
{
const QFileInfo f(path);
if(f.suffix().compare(QStringLiteral("pdf"), Qt::CaseInsensitive))
if(!path.endsWith(QLatin1String("pdf"), Qt::CaseInsensitive))
return false;
QFile file(path);
if(!file.open(QIODevice::ReadOnly))
return false;
QByteArray blob = file.readAll();
for(const QByteArray &token: {"adbe.pkcs7.detached", "adbe.pkcs7.sha1", "adbe.x509.rsa_sha1", "ETSI.CAdES.detached"})
{
if(blob.indexOf(token) > 0)
return true;
}
return false;
static const auto list = {"adbe.pkcs7.detached", "adbe.pkcs7.sha1", "adbe.x509.rsa_sha1", "ETSI.CAdES.detached"};
return std::any_of(list.begin(), list.end(), [&blob](const char *token) { return blob.indexOf(token) > 0; });
}

void FileDialog::setFileZone(const QString &path, int zone)
Expand Down Expand Up @@ -290,7 +286,7 @@ QString FileDialog::getSaveFileName( QWidget *parent, const QString &caption,

QString FileDialog::normalized(const QString &data)
{
static constexpr std::array<const unsigned char[3],5> list = {{
static constexpr std::array<const unsigned char[3],5> list {{
{0xE2, 0x80, 0x8E}, // \u200E LEFT-TO-RIGHT MARK
{0xE2, 0x80, 0x8F}, // \u200F RIGHT-TO-LEFT MARK
{0xE2, 0x80, 0xAA}, // \u202A LEFT-TO-RIGHT EMBEDDING
Expand Down Expand Up @@ -340,15 +336,16 @@ QString FileDialog::safeName(const QString &file)
QString filename = info.fileName();
#if defined(Q_OS_WIN)
static const QStringList disabled { "CON", "PRN", "AUX", "NUL",
"COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9",
"LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9" };
"COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9",
"LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9" };
if(disabled.contains(info.baseName(), Qt::CaseInsensitive))
filename = QStringLiteral("___.") + info.suffix();
filename.replace(QRegularExpression(QStringLiteral("[\\\\/*:?\"<>|]")), QStringLiteral("_"));
static const QRegularExpression replace(QStringLiteral("[\\\\/*:?\"<>|]"));
#elif defined(Q_OS_MAC)
filename.replace(QRegularExpression(QStringLiteral("[\\\\/:]")), QStringLiteral("_"));
static const QRegularExpression replace(QStringLiteral("[\\\\/:]"));
#else
filename.replace(QRegularExpression(QStringLiteral("[\\\\/]")), QStringLiteral("_"));
static const QRegularExpression replace(QStringLiteral("[\\\\/]"));
#endif
filename.replace(replace, QStringLiteral("_"));
return filename;
}
4 changes: 4 additions & 0 deletions client/translations/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3288,6 +3288,10 @@ Additional licenses and components</translation>
<source>Certificates expire soon! </source>
<translation>Certificates expire soon! </translation>
</message>
<message>
<source>This ASiC-S container contains XAdES signature. You are not allowed to add or remove signatures to this container.</source>
<translation>This ASiC-S container contains XAdES signature. You are not allowed to add or remove signatures to this container.</translation>
</message>
</context>
<context>
<name>WarningRibbon</name>
Expand Down
4 changes: 4 additions & 0 deletions client/translations/et.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3288,6 +3288,10 @@ Täiendavad litsentsid ja komponendid</translation>
<source>Certificates have expired! </source>
<translation>Sertifikaadid on aegunud! </translation>
</message>
<message>
<source>This ASiC-S container contains XAdES signature. You are not allowed to add or remove signatures to this container.</source>
<translation>Tegemist on XAdES allkirja sisalduva ASiC-S ümbrikuga. Sellele ümbrikule ei saa allkirja lisada ega eemaldada.</translation>
</message>
</context>
<context>
<name>WarningRibbon</name>
Expand Down
4 changes: 4 additions & 0 deletions client/translations/ru.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3293,6 +3293,10 @@ Additional licenses and components</source>
<source>Certificates expire soon! </source>
<translation>Срок действия сертификатов скоро истекает! </translation>
</message>
<message>
<source>This ASiC-S container contains XAdES signature. You are not allowed to add or remove signatures to this container.</source>
<translation>Речь идет о конверте ASiC-S, содержащемся в подписи XAdES. К данному контейнеру нельзя добавить или удалить из него подпись.</translation>
</message>
</context>
<context>
<name>WarningRibbon</name>
Expand Down
2 changes: 2 additions & 0 deletions client/widgets/ContainerPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,8 @@ void ContainerPage::transition(DigiDoc* container)
emit warning(WarningText(i.key(), i.value()));
if(container->fileName().endsWith(QStringLiteral("ddoc"), Qt::CaseInsensitive))
emit warning(UnsupportedDDocWarning);
if(container->isAsicS())
emit warning(UnsupportedAsicSWarning);

hasEmptyFile = false;
for (auto i = 0; i < container->documentModel()->rowCount(); i++)
Expand Down
7 changes: 7 additions & 0 deletions client/widgets/WarningItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,13 @@ void WarningItem::lookupWarning()
warnText.details = tr("More information");
warnText.page = ria::qdigidoc4::SignDetails;
break;
case ria::qdigidoc4::UnsupportedAsicSWarning:
warnText.text = tr("This ASiC-S container contains XAdES signature. "
"You are not allowed to add or remove signatures to this container.");
warnText.url = tr("https://www.id.ee/en/article/digidoc-container-format-life-cycle-2/");
warnText.details = tr("More information");
warnText.page = ria::qdigidoc4::SignDetails;
break;
case ria::qdigidoc4::UnsupportedDDocWarning:
warnText.text = tr("The current file is a DigiDoc container that is not supported officially any longer. You are not allowed to add or remove signatures to this container.");
warnText.url = tr("https://www.id.ee/en/article/digidoc-container-format-life-cycle-2/");
Expand Down

0 comments on commit a7a80bd

Please sign in to comment.