diff --git a/CMakeLists.txt b/CMakeLists.txt --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -195,6 +195,7 @@ core/utils.cpp core/view.cpp core/fileprinter.cpp + core/signatureutils.cpp core/script/event.cpp core/synctex/synctex_parser.c core/synctex/synctex_parser_utils.c diff --git a/core/form.h b/core/form.h --- a/core/form.h +++ b/core/form.h @@ -13,7 +13,7 @@ #include "okularcore_export.h" #include "area.h" #include "annotations.h" - +#include "signatureutils.h" #include namespace Okular { @@ -25,6 +25,7 @@ class FormFieldButtonPrivate; class FormFieldTextPrivate; class FormFieldChoicePrivate; +class FormFieldSignaturePrivate; /** * @short The base interface of a form field. @@ -387,6 +388,46 @@ Q_DISABLE_COPY( FormFieldChoice ) }; +/** + * @short Interface of a signature form field. + * + * This is the base interface to reimplement to represent a signature field. + */ +class OKULARCORE_EXPORT FormFieldSignature : public FormField +{ + public: + /** + * The types of signature. + */ + enum SignatureType { + AdbePkcs7sha1, + AdbePkcs7detached, + EtsiCAdESdetached, + UnknownType + }; + + ~FormFieldSignature(); + + /** + * The signature type + */ + virtual SignatureType signatureType() const = 0; + + /** + * Validate the signature with 'now' as validation time. + * + */ + virtual SignatureInfo *validate() const = 0; + + + protected: + FormFieldSignature(); + + private: + Q_DECLARE_PRIVATE( FormFieldSignature ) + Q_DISABLE_COPY( FormFieldSignature ) +}; + } #endif diff --git a/core/form.cpp b/core/form.cpp --- a/core/form.cpp +++ b/core/form.cpp @@ -294,3 +294,32 @@ return false; } +class Okular::FormFieldSignaturePrivate : public Okular::FormFieldPrivate +{ + public: + FormFieldSignaturePrivate() + : FormFieldPrivate( FormField::FormSignature ) + { + } + + Q_DECLARE_PUBLIC( FormFieldSignature ) + + void setValue( const QString& v ) override + { + Q_UNUSED( v ) + } + + QString value() const override + { + return QString(); + } +}; + +FormFieldSignature::FormFieldSignature() + : FormField( *new FormFieldSignaturePrivate() ) +{ +} + +FormFieldSignature::~FormFieldSignature() +{ +} diff --git a/core/signatureutils.h b/core/signatureutils.h new file mode 100644 --- /dev/null +++ b/core/signatureutils.h @@ -0,0 +1,276 @@ +/*************************************************************************** + * Copyright (C) 2018 by Chinmoy Ranjan Pradhan * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + ***************************************************************************/ + +#ifndef OKULAR_SIGNATUREINFO_H +#define OKULAR_SIGNATUREINFO_H + +#include "okularcore_export.h" + +#include +#include +#include +#include +#include + +namespace Okular { + +class CertificateInfoPrivate; +class SignatureInfoPrivate; + +/** + * @short A helper class to store information about x509 certificate + */ +class OKULARCORE_EXPORT CertificateInfo +{ + public: + + /** + * The algorithm of public key. + */ + enum PublicKeyType + { + RsaKey, + DsaKey, + EcKey, + OtherKey + }; + + /** + * Certificate key usage extensions. + */ + enum KeyUsageExtension + { + KuDigitalSignature = 0x80, + KuNonRepudiation = 0x40, + KuKeyEncipherment = 0x20, + KuDataEncipherment = 0x10, + KuKeyAgreement = 0x08, + KuKeyCertSign = 0x04, + KuClrSign = 0x02, + KuEncipherOnly = 0x01, + KuNone = 0x00 + }; + Q_DECLARE_FLAGS( KeyUsageExtensions, KeyUsageExtension ) + + /** + * Predefined keys for elements in an entity's distinguished name. + */ + enum EntityInfoKey + { + CommonName, + DistinguishedName, + EmailAddress, + Organization, + }; + + /** + * Destructor + */ + virtual ~CertificateInfo(); + + /** + * Returns true if certificate has no contents; otherwise returns false. + */ + virtual bool isNull() const; + + /** + * The certificate version string. + */ + virtual int version() const; + + /** + * The certificate serial number. + */ + virtual QByteArray serialNumber() const; + + /** + * Information about the issuer. + */ + virtual QString issuerInfo(EntityInfoKey key) const; + + /** + * Information about the subject + */ + virtual QString subjectInfo(EntityInfoKey key) const; + + /** + * The date-time when certificate becomes valid. + */ + virtual QDateTime validityStart() const; + + /** + * The date-time when certificate expires. + */ + virtual QDateTime validityEnd() const; + + /** + * The uses allowed for the certificate. + */ + virtual KeyUsageExtensions keyUsageExtensions() const; + + /** + * The public key value. + */ + virtual QByteArray publicKey() const; + + /** + * The public key type. + */ + virtual PublicKeyType publicKeyType() const; + + /** + * The strength of public key in bits. + */ + virtual int publicKeyStrength() const; + + /** + * Returns true if certificate is self-signed otherwise returns false. + */ + virtual bool isSelfSigned() const; + + /** + * The DER encoded certificate. + */ + virtual QByteArray certificateData() const; + + protected: + CertificateInfo(); + + private: + Q_DISABLE_COPY( CertificateInfo ) +}; + +/** + * @short A helper class to store information about digital signature + */ +class OKULARCORE_EXPORT SignatureInfo +{ + public: + + /** + * The verfication result of the signature. + */ + enum SignatureStatus + { + SignatureStatusUnknown, ///< The signature status is unknown for some reason. + SignatureValid, ///< The signature is cryptographically valid. + SignatureInvalid, ///< The signature is cryptographically invalid. + SignatureDigestMismatch, ///< The document content was changed after the signature was applied. + SignatureDecodingError, ///< The signature CMS/PKCS7 structure is malformed. + SignatureGenericError, ///< The signature could not be verified. + SignatureNotFound, ///< The requested signature is not present in the document. + SignatureNotVerified ///< The signature is not yet verified. + }; + + /** + * The verification result of the certificate. + */ + enum CertificateStatus + { + CertificateStatusUnknown, ///< The certificate status is unknown for some reason. + CertificateTrusted, ///< The certificate is considered trusted. + CertificateUntrustedIssuer, ///< The issuer of this certificate has been marked as untrusted by the user. + CertificateUnknownIssuer, ///< The certificate trust chain has not finished in a trusted root certificate. + CertificateRevoked, ///< The certificate was revoked by the issuing certificate authority. + CertificateExpired, ///< The signing time is outside the validity bounds of this certificate. + CertificateGenericError, ///< The certificate could not be verified. + CertificateNotVerified ///< The certificate is not yet verified. + }; + + /** + * The hash algorithm of the signature + */ + enum HashAlgorithm + { + HashAlgorithmUnknown, + HashAlgorithmMd2, + HashAlgorithmMd5, + HashAlgorithmSha1, + HashAlgorithmSha256, + HashAlgorithmSha384, + HashAlgorithmSha512, + HashAlgorithmSha224 + }; + + /** + * Destructor. + */ + virtual ~SignatureInfo(); + + /** + * The signature status of the signature. + */ + virtual SignatureStatus signatureStatus() const; + + /** + * The certificate status of the signature. + */ + virtual CertificateStatus certificateStatus() const; + + /** + * The signer subject common name associated with the signature. + */ + virtual QString signerName() const; + + /** + * The signer subject distinguished name associated with the signature. + */ + virtual QString signerSubjectDN() const; + + /** + * Get signing location. + */ + virtual QString location() const; + + /** + * Get signing reason. + */ + virtual QString reason() const; + + /** + * The the hash algorithm used for the signature. + */ + virtual HashAlgorithm hashAlgorithm() const; + + /** + * The signing time associated with the signature. + */ + virtual QDateTime signingTime() const; + + /** + * Get the signature binary data. + */ + virtual QByteArray signature() const; + + /** + * Get the bounds of the ranges of the document which are signed. + */ + virtual QList signedRangeBounds() const; + + /** + * Checks whether the signature authenticates the total document + * except for the signature itself. + */ + virtual bool signsTotalDocument() const; + + /** + * Get certificate details. + */ + virtual CertificateInfo *certificateInfo() const; + + protected: + SignatureInfo(); + + private: + Q_DISABLE_COPY( SignatureInfo ) +}; + +} + +#endif diff --git a/core/signatureutils.cpp b/core/signatureutils.cpp new file mode 100644 --- /dev/null +++ b/core/signatureutils.cpp @@ -0,0 +1,157 @@ +/*************************************************************************** + * Copyright (C) 2018 by Chinmoy Ranjan Pradhan * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + ***************************************************************************/ + +#include "signatureutils.h" + +using namespace Okular; + +CertificateInfo::CertificateInfo() +{ +} + +CertificateInfo::~CertificateInfo() +{ +} + +Q_DECLARE_OPERATORS_FOR_FLAGS( CertificateInfo::KeyUsageExtensions ) + +bool CertificateInfo::isNull() const +{ + return true; +} + +int CertificateInfo::version() const +{ + return -1; +} + +QByteArray CertificateInfo::serialNumber() const +{ + return QByteArray(); +} + +QString CertificateInfo::issuerInfo(EntityInfoKey) const +{ + return QString(); +} + +QString CertificateInfo::subjectInfo(EntityInfoKey) const +{ + return QString(); +} + +QDateTime CertificateInfo::validityStart() const +{ + return QDateTime(); +} + +QDateTime CertificateInfo::validityEnd() const +{ + return QDateTime(); +} + +CertificateInfo::KeyUsageExtensions CertificateInfo::keyUsageExtensions() const +{ + return KuNone; +} + +QByteArray CertificateInfo::publicKey() const +{ + return QByteArray(); +} + +CertificateInfo::PublicKeyType CertificateInfo::publicKeyType() const +{ + return OtherKey; +} + +int CertificateInfo::publicKeyStrength() const +{ + return -1; +} + +bool CertificateInfo::isSelfSigned() const +{ + return false; +} + +QByteArray CertificateInfo::certificateData() const +{ + return QByteArray(); +} + + +SignatureInfo::SignatureInfo() +{ +} + +SignatureInfo::~SignatureInfo() +{ +} + +SignatureInfo::SignatureStatus SignatureInfo::signatureStatus() const +{ + return SignatureStatusUnknown; +} + +SignatureInfo::CertificateStatus SignatureInfo::certificateStatus() const +{ + return CertificateStatusUnknown; + +} + +SignatureInfo::HashAlgorithm SignatureInfo::hashAlgorithm() const +{ + return HashAlgorithmUnknown; +} + +QString SignatureInfo::signerName() const +{ + return QString(); +} + +QString SignatureInfo::signerSubjectDN() const +{ + return QString(); +} + +QString SignatureInfo::location() const +{ + return QString(); +} + +QString SignatureInfo::reason() const +{ + return QString(); +} + +QDateTime SignatureInfo::signingTime() const +{ + return QDateTime(); +} + +QByteArray SignatureInfo::signature() const +{ + return QByteArray(); +} + +QList SignatureInfo::signedRangeBounds() const +{ + return QList(); +} + +bool SignatureInfo::signsTotalDocument() const +{ + return false; +} + +CertificateInfo *SignatureInfo::certificateInfo() const +{ + return nullptr; +}