Commit ed7661b9 authored by Oleg Korshul's avatar Oleg Korshul

ooxmlsignature library & test (full work version)

parent f582a9b6
DEFINES += HAVE_VA_COPY DEFINES += HAVE_VA_COPY
core_static_link_xml_full {
DEFINES += \
LIBXML_READER_ENABLED \
LIBXML_PUSH_ENABLED \
LIBXML_HTML_ENABLED \
LIBXML_XPATH_ENABLED \
LIBXML_OUTPUT_ENABLED \
LIBXML_C14N_ENABLED \
LIBXML_SAX1_ENABLED \
LIBXML_TREE_ENABLED \
LIBXML_XPTR_ENABLED \
IN_LIBXML \
LIBXML_STATIC
}
INCLUDEPATH += \ INCLUDEPATH += \
$$PWD/../../libxml2/include \ $$PWD/../../libxml2/include \
$$PWD/../../libxml2/include/libxml \ $$PWD/../../libxml2/include/libxml \
......
#ifndef _XMLSIGNER_CERTIFICATE_H_
#define _XMLSIGNER_CERTIFICATE_H_
#ifdef WIN32
#include "XmlSigner_mscrypto.h"
#define CCertificate CCertificate_mscrypto
#endif
#if defined(_LINUX) && !defined(_MAC)
#endif
#ifdef _MAC
#endif
#endif // _XMLSIGNER_CERTIFICATE_H_
#ifndef _XML_OOXMLSIGNER_H_
#define _XML_OOXMLSIGNER_H_
#include "./XmlCertificate.h"
class COOXMLSigner_private;
class Q_DECL_EXPORT COOXMLSigner
{
public:
COOXMLSigner(const std::wstring& sFolder, ICertificate* pContext);
~COOXMLSigner();
void SetGuid(const std::wstring& guid);
void SetImageValid(const std::wstring& file);
void SetImageInvalid(const std::wstring& file);
void Sign();
private:
COOXMLSigner_private* m_internal;
};
#endif //_XML_OOXMLSIGNER_H_
#ifndef _XML_OOXMLVERIFIER_H_
#define _XML_OOXMLVERIFIER_H_
#include "./XmlCertificate.h"
#define OOXML_SIGNATURE_VALID 0
#define OOXML_SIGNATURE_INVALID 1
#define OOXML_SIGNATURE_NOTSUPPORTED 2
#define OOXML_SIGNATURE_BAD 3
class COOXMLSignature_private;
class Q_DECL_EXPORT COOXMLSignature
{
public:
COOXMLSignature();
~COOXMLSignature();
public:
int GetValid();
std::string GetGuid();
ICertificate* GetCertificate();
std::string GetImageValidBase64();
std::string GetImageInvalidBase64();
public:
void Check();
friend class COOXMLVerifier_private;
friend class COOXMLVerifier;
private:
COOXMLSignature_private* m_internal;
};
class COOXMLVerifier_private;
class Q_DECL_EXPORT COOXMLVerifier
{
public:
COOXMLVerifier(const std::wstring& sFolder);
~COOXMLVerifier();
int GetSignatureCount();
COOXMLSignature* GetSignature(const int& index);
private:
COOXMLVerifier_private* m_internal;
};
#endif //_XML_OOXMLVERIFIER_H_
#ifndef _XMLSIGNER_BASE_H_ #ifndef _XML_SERTIFICATE_BASE_H_
#define _XMLSIGNER_BASE_H_ #define _XML_SERTIFICATE_BASE_H_
#include "../../common/File.h"
#include "../../common/BigInteger.h"
#include <string> #include <string>
#include <vector> #include <vector>
#include <map> #include "../../../common/base_export.h"
#define OOXML_HASH_ALG_SHA1 0 #define OOXML_HASH_ALG_SHA1 0
#define OOXML_HASH_ALG_INVALID 1 #define OOXML_HASH_ALG_INVALID 1
class ICertificate class Q_DECL_EXPORT ICertificate
{ {
public: public:
ICertificate() ICertificate()
...@@ -36,20 +33,15 @@ public: ...@@ -36,20 +33,15 @@ public:
virtual std::string GetHash(std::wstring& sXmlFile, int nAlg) = 0; virtual std::string GetHash(std::wstring& sXmlFile, int nAlg) = 0;
virtual bool Verify(std::string& sXml, std::string& sXmlSignature, int nAlg) = 0; virtual bool Verify(std::string& sXml, std::string& sXmlSignature, int nAlg) = 0;
virtual bool LoadFromBase64Data(const std::string& data) = 0; virtual bool LoadFromBase64Data(const std::string& data) = 0;
virtual int ShowCertificate() = 0;
public: public:
virtual bool ShowSelectDialog() = 0; virtual bool ShowSelectDialog() = 0;
virtual int ShowCertificate() = 0;
static int GetOOXMLHashAlg(const std::string& sAlg) public:
{ static int GetOOXMLHashAlg(const std::string& sAlg);
if ("http://www.w3.org/2000/09/xmldsig#rsa-sha1" == sAlg || static ICertificate* CreateInstance();
"http://www.w3.org/2000/09/xmldsig#sha1" == sAlg)
return OOXML_HASH_ALG_SHA1;
return OOXML_HASH_ALG_INVALID;
}
}; };
#endif // _XMLSIGNER_BASE_H_ #endif // _XML_SERTIFICATE_BASE_H_
#ifndef _XML_OOXMLSIGNER_H_ #include "./../include/OOXMLSigner.h"
#define _XML_OOXMLSIGNER_H_ #include "./../src/XmlTransform.h"
#include "./XmlCanonicalizator.h" class COOXMLSigner_private
#include "./XmlSignerBase.h"
#include "./XmlTransform.h"
class COOXMLSigner
{ {
public: public:
ICertificate* m_certificate; ICertificate* m_certificate;
...@@ -25,7 +21,7 @@ public: ...@@ -25,7 +21,7 @@ public:
std::wstring m_guid; std::wstring m_guid;
public: public:
COOXMLSigner(const std::wstring& sFolder, ICertificate* pContext) COOXMLSigner_private(const std::wstring& sFolder, ICertificate* pContext)
{ {
m_sFolder = sFolder; m_sFolder = sFolder;
m_certificate = pContext; m_certificate = pContext;
...@@ -35,7 +31,7 @@ public: ...@@ -35,7 +31,7 @@ public:
m_signed_info.WriteString("<CanonicalizationMethod Algorithm=\"http://www.w3.org/TR/2001/REC-xml-c14n-20010315\"/>"); m_signed_info.WriteString("<CanonicalizationMethod Algorithm=\"http://www.w3.org/TR/2001/REC-xml-c14n-20010315\"/>");
m_signed_info.WriteString("<SignatureMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#rsa-sha1\"/>"); m_signed_info.WriteString("<SignatureMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#rsa-sha1\"/>");
} }
~COOXMLSigner() ~COOXMLSigner_private()
{ {
} }
...@@ -560,4 +556,32 @@ Type=\"http://schemas.openxmlformats.org/package/2006/relationships/digital-sign ...@@ -560,4 +556,32 @@ Type=\"http://schemas.openxmlformats.org/package/2006/relationships/digital-sign
} }
}; };
#endif //_XML_OOXMLSIGNER_H_ COOXMLSigner::COOXMLSigner(const std::wstring& sFolder, ICertificate* pContext)
{
m_internal = new COOXMLSigner_private(sFolder, pContext);
}
COOXMLSigner::~COOXMLSigner()
{
RELEASEOBJECT(m_internal);
}
void COOXMLSigner::SetGuid(const std::wstring& guid)
{
m_internal->SetGuid(guid);
}
void COOXMLSigner::SetImageValid(const std::wstring& file)
{
m_internal->SetImageValid(file);
}
void COOXMLSigner::SetImageInvalid(const std::wstring& file)
{
m_internal->SetImageInvalid(file);
}
void COOXMLSigner::Sign()
{
m_internal->Sign();
}
#ifndef _XML_OOXMLVERIFIER_H_
#define _XML_OOXMLVERIFIER_H_
#include "./XmlCanonicalizator.h" #include "./XmlCanonicalizator.h"
#include "./XmlTransform.h" #include "./XmlTransform.h"
#include "./XmlCertificate.h" #include "./../include/OOXMLVerifier.h"
#define OOXML_SIGNATURE_VALID 0
#define OOXML_SIGNATURE_INVALID 1
#define OOXML_SIGNATURE_NOTSUPPORTED 2
#define OOXML_SIGNATURE_BAD 3
class COOXMLSignature class COOXMLSignature_private
{ {
private: public:
int m_valid; int m_valid;
std::string m_guid; std::string m_guid;
ICertificate* m_cert; ICertificate* m_cert;
...@@ -22,7 +14,6 @@ private: ...@@ -22,7 +14,6 @@ private:
std::wstring m_sFolder; std::wstring m_sFolder;
private:
XmlUtils::CXmlNode m_node; // signature file XmlUtils::CXmlNode m_node; // signature file
class CXmlStackNamespaces class CXmlStackNamespaces
...@@ -133,13 +124,13 @@ private: ...@@ -133,13 +124,13 @@ private:
}; };
public: public:
COOXMLSignature() COOXMLSignature_private()
{ {
m_valid = OOXML_SIGNATURE_INVALID; m_valid = OOXML_SIGNATURE_INVALID;
m_guid = ""; m_guid = "";
m_cert = NULL; m_cert = NULL;
} }
~COOXMLSignature() ~COOXMLSignature_private()
{ {
RELEASEOBJECT(m_cert); RELEASEOBJECT(m_cert);
} }
...@@ -176,7 +167,7 @@ public: ...@@ -176,7 +167,7 @@ public:
m_valid = OOXML_SIGNATURE_NOTSUPPORTED; m_valid = OOXML_SIGNATURE_NOTSUPPORTED;
return; return;
} }
m_cert = new CCertificate(); m_cert = ICertificate::CreateInstance();
if (!m_cert->LoadFromBase64Data(U_TO_UTF8(oNodeCert.GetText()))) if (!m_cert->LoadFromBase64Data(U_TO_UTF8(oNodeCert.GetText())))
{ {
m_valid = OOXML_SIGNATURE_NOTSUPPORTED; m_valid = OOXML_SIGNATURE_NOTSUPPORTED;
...@@ -273,7 +264,7 @@ public: ...@@ -273,7 +264,7 @@ public:
friend class COOXMLVerifier; friend class COOXMLVerifier;
private: public:
int CheckManifestReference(XmlUtils::CXmlNode& node) int CheckManifestReference(XmlUtils::CXmlNode& node)
{ {
...@@ -391,14 +382,54 @@ private: ...@@ -391,14 +382,54 @@ private:
} }
}; };
class COOXMLVerifier COOXMLSignature::COOXMLSignature()
{
m_internal = new COOXMLSignature_private();
}
COOXMLSignature::~COOXMLSignature()
{
RELEASEOBJECT(m_internal);
}
int COOXMLSignature::GetValid()
{
return m_internal->GetValid();
}
std::string COOXMLSignature::GetGuid()
{
return m_internal->GetGuid();
}
ICertificate* COOXMLSignature::GetCertificate()
{
return m_internal->GetCertificate();
}
std::string COOXMLSignature::GetImageValidBase64()
{
return m_internal->GetImageValidBase64();
}
std::string COOXMLSignature::GetImageInvalidBase64()
{
return m_internal->GetImageInvalidBase64();
}
void COOXMLSignature::Check()
{
m_internal->Check();
}
class COOXMLVerifier_private
{ {
public: public:
std::wstring m_sFolder; std::wstring m_sFolder;
std::vector<COOXMLSignature*> m_arSignatures; std::vector<COOXMLSignature*> m_arSignatures;
public: public:
COOXMLVerifier(const std::wstring& sFolder) COOXMLVerifier_private(const std::wstring& sFolder)
{ {
m_sFolder = sFolder; m_sFolder = sFolder;
...@@ -429,14 +460,14 @@ public: ...@@ -429,14 +460,14 @@ public:
continue; continue;
COOXMLSignature* pSignature = new COOXMLSignature(); COOXMLSignature* pSignature = new COOXMLSignature();
pSignature->m_node = nodeSig; pSignature->m_internal->m_node = nodeSig;
pSignature->m_sFolder = m_sFolder; pSignature->m_internal->m_sFolder = m_sFolder;
pSignature->Check(); pSignature->Check();
m_arSignatures.push_back(pSignature); m_arSignatures.push_back(pSignature);
} }
} }
~COOXMLVerifier() ~COOXMLVerifier_private()
{ {
for (std::vector<COOXMLSignature*>::iterator i = m_arSignatures.begin(); i != m_arSignatures.end(); i++) for (std::vector<COOXMLSignature*>::iterator i = m_arSignatures.begin(); i != m_arSignatures.end(); i++)
{ {
...@@ -447,4 +478,22 @@ public: ...@@ -447,4 +478,22 @@ public:
} }
}; };
#endif //_XML_OOXMLVERIFIER_H_ COOXMLVerifier::COOXMLVerifier(const std::wstring& sFolder)
{
m_internal = new COOXMLVerifier_private(sFolder);
}
COOXMLVerifier::~COOXMLVerifier()
{
RELEASEOBJECT(m_internal);
}
int COOXMLVerifier::GetSignatureCount()
{
return (int)m_internal->m_arSignatures.size();
}
COOXMLSignature* COOXMLVerifier::GetSignature(const int& index)
{
return m_internal->m_arSignatures[index];
}
#ifndef _XML_CANONICALIZATOR_H_ #ifndef _XML_CANONICALIZATOR_H_
#define _XML_CANONICALIZATOR_H_ #define _XML_CANONICALIZATOR_H_
#include "../../common/File.h" #include "../../../common/File.h"
#include "../../common/Directory.h" #include "../../../common/Directory.h"
#include "../../common/StringBuilder.h" #include "../../../common/StringBuilder.h"
#include "../../xml/include/xmlutils.h" #include "../../../xml/include/xmlutils.h"
#include "../../xml/libxml2/include/libxml/c14n.h" #include "../../../xml/libxml2/include/libxml/c14n.h"
#ifndef XML_UNUSED #ifndef XML_UNUSED
#define XML_UNUSED( arg ) ( (arg) = (arg) ) #define XML_UNUSED( arg ) ( (arg) = (arg) )
......
#ifdef WIN32
#include "./XmlSigner_mscrypto.h"
#define CCertificate CCertificate_mscrypto
#endif
#if defined(_LINUX) && !defined(_MAC)
#endif
#ifdef _MAC
#endif
int ICertificate::GetOOXMLHashAlg(const std::string& sAlg)
{
if ("http://www.w3.org/2000/09/xmldsig#rsa-sha1" == sAlg ||
"http://www.w3.org/2000/09/xmldsig#sha1" == sAlg)
return OOXML_HASH_ALG_SHA1;
return OOXML_HASH_ALG_INVALID;
}
ICertificate* ICertificate::CreateInstance()
{
return new CCertificate();
}
#ifndef _XMLSIGNER_MSCRYPTO_H_ #ifndef _XMLSIGNER_MSCRYPTO_H_
#define _XMLSIGNER_MSCRYPTO_H_ #define _XMLSIGNER_MSCRYPTO_H_
#include "./XmlSignerBase.h" #include "./include/XmlCertificate.h"
#include <stdio.h> #include <stdio.h>
#include <windows.h> #include <windows.h>
#include <wincrypt.h> #include <wincrypt.h>
#include <cryptuiapi.h> #include <cryptuiapi.h>
#include "../../../common/File.h"
#include "../../../common/BigInteger.h"
class CCertificate_mscrypto : public ICertificate class CCertificate_mscrypto : public ICertificate
{ {
public: public:
......
#include "./XmlTransform.h"
IXmlTransform* IXmlTransform::GetFromType(const std::string& alg)
{
if (true)
{
CXmlTransformRelationship* transform = new CXmlTransformRelationship();
if (transform->m_algorithm == alg)
return transform;
RELEASEOBJECT(transform);
}
if (true)
{
CXmlTransformC14N* transform = new CXmlTransformC14N();
if (transform->CheckC14NTransform(alg))
return transform;
RELEASEOBJECT(transform);
}
return NULL;
}
...@@ -122,26 +122,6 @@ public: ...@@ -122,26 +122,6 @@ public:
} }
}; };
IXmlTransform* IXmlTransform::GetFromType(const std::string& alg)
{
if (true)
{
CXmlTransformRelationship* transform = new CXmlTransformRelationship();
if (transform->m_algorithm == alg)
return transform;
RELEASEOBJECT(transform);
}
if (true)
{
CXmlTransformC14N* transform = new CXmlTransformC14N();
if (transform->CheckC14NTransform(alg))
return transform;
RELEASEOBJECT(transform);
}
return NULL;
}
class CXmlTransforms class CXmlTransforms
{ {
protected: protected:
......
#include "../../src/XmlCertificate.h" #include "../../src/include/XmlCertificate.h"
#include "../../src/OOXMLSigner.h" #include "../../src/include/OOXMLSigner.h"
#include "../../src/OOXMLVerifier.h" #include "../../src/include/OOXMLVerifier.h"
#pragma comment (lib, "crypt32.lib") #include "../../../common/File.h"
#pragma comment (lib, "cryptui.lib")
#pragma comment (lib, "Advapi32.lib")
void main(void) void main(void)
{ {
...@@ -17,31 +15,34 @@ void main(void) ...@@ -17,31 +15,34 @@ void main(void)
{ {
std::wstring sSignId = L"{9792D33F-AB37-4E5B-A465-481B9465818B}"; std::wstring sSignId = L"{9792D33F-AB37-4E5B-A465-481B9465818B}";
CCertificate oCertificate; ICertificate* pCertificate = ICertificate::CreateInstance();
if (!oCertificate.ShowSelectDialog()) if (!pCertificate->ShowSelectDialog())
{
RELEASEOBJECT(pCertificate);
return; return;
}
COOXMLSigner oOOXMLSigner(sFolderOOXML, &oCertificate); COOXMLSigner oOOXMLSigner(sFolderOOXML, pCertificate);
oOOXMLSigner.SetGuid(sSignId); oOOXMLSigner.SetGuid(sSignId);
oOOXMLSigner.SetImageValid(NSFile::GetProcessDirectory() + L"/../../../resources/valid.png"); oOOXMLSigner.SetImageValid(NSFile::GetProcessDirectory() + L"/../../../resources/valid.png");
oOOXMLSigner.SetImageInvalid(NSFile::GetProcessDirectory() + L"/../../../resources/invalid.png"); oOOXMLSigner.SetImageInvalid(NSFile::GetProcessDirectory() + L"/../../../resources/invalid.png");
oOOXMLSigner.Sign(); oOOXMLSigner.Sign();
RELEASEOBJECT(pCertificate);
} }
else else
{ {
COOXMLVerifier oVerifier(sFolderOOXML); COOXMLVerifier oVerifier(sFolderOOXML);
size_t nCount = oVerifier.m_arSignatures.size(); int nCount = oVerifier.GetSignatureCount();
for (std::vector<COOXMLSignature*>::iterator i = oVerifier.m_arSignatures.begin(); i != oVerifier.m_arSignatures.end(); i++) for (int i = 0; i < nCount; i++)
{ {
COOXMLSignature* pSign = *i; COOXMLSignature* pSign = oVerifier.GetSignature(i);
int nRes = pSign->GetValid(); int nRes = pSign->GetValid();
XML_UNUSED(pSign); pSign = pSign;
XML_UNUSED(nRes); nRes = nRes;
} }
XML_UNUSED(nCount);
} }
} }
...@@ -10,26 +10,8 @@ CORE_ROOT_DIR = $$PWD/../../../../ ...@@ -10,26 +10,8 @@ CORE_ROOT_DIR = $$PWD/../../../../
PWD_ROOT_DIR = $$PWD PWD_ROOT_DIR = $$PWD
include($$CORE_ROOT_DIR/Common/base.pri) include($$CORE_ROOT_DIR/Common/base.pri)
DEFINES -= UNICODE
DEFINES += \
LIBXML_READER_ENABLED \
LIBXML_PUSH_ENABLED \
LIBXML_HTML_ENABLED \
LIBXML_XPATH_ENABLED \
LIBXML_OUTPUT_ENABLED \
LIBXML_C14N_ENABLED \
LIBXML_SAX1_ENABLED \
LIBXML_TREE_ENABLED \
LIBXML_XPTR_ENABLED \
LIBXML_STATIC
SOURCES += main.cpp SOURCES += main.cpp
LIBS += -L$$CORE_BUILDS_LIBRARIES_PATH -llibxml LIBS += -L$$CORE_BUILDS_LIBRARIES_PATH -looxmlsignature
INCLUDEPATH += \
$$CORE_ROOT_DIR/DesktopEditor/xml/libxml2/include \
$$CORE_ROOT_DIR/DesktopEditor/xml/libxml2/include/libxml
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment