From 4251870880aab1b200781fbb1c480266c90e991c Mon Sep 17 00:00:00 2001 From: Gianluca Boiano Date: Sat, 30 Mar 2024 20:50:25 +0100 Subject: [PATCH] chore: beautify with clang-format --- libs/pkcs11/src/CSP/VerificaConCIE.h | 2 +- libs/pkcs11/src/PKCS11/CIEP11Template.h | 4 +- libs/pkcs11/src/PKCS11/CardContext.h | 2 +- libs/pkcs11/src/PKCS11/PKCS11Functions.h | 2 +- libs/pkcs11/src/PKCS11/Slot.h | 3 +- libs/pkcs11/src/PKCS11/pkcs11.h | 23 +- libs/pkcs11/src/keys.h | 1 - libs/shared/src/CSP/ExtAuthKey.cpp | 2 +- libs/shared/src/Crypto/ASNParser.cpp | 3 +- libs/shared/src/Crypto/ASNParser.h | 2 +- libs/shared/src/Crypto/DES3.cpp | 2 + libs/shared/src/Crypto/RSA.h | 2 +- libs/shared/src/PCSC/APDU.cpp | 2 +- libs/shared/src/PCSC/CardLocker.h | 4 +- libs/shared/src/PCSC/PCSC.h | 2 +- libs/shared/src/PCSC/Token.cpp | 2 +- libs/shared/src/PCSC/Token.h | 4 +- libs/shared/src/PKCS11/pkcs11.h | 23 +- libs/shared/src/Util/Array.cpp | 4 +- libs/shared/src/Util/CacheLib.cpp | 2 +- libs/shared/src/Util/CryptoppUtils.cpp | 4 +- libs/shared/src/Util/IniSettings.h | 2 +- libs/shared/src/Util/ModuleInfo.h | 1 + libs/shared/src/Util/UUCByteArray.cpp | 6 +- libs/shared/src/Util/UUCProperties.h | 2 +- libs/shared/src/Util/UUCTextFileReader.cpp | 12 +- libs/shared/src/Util/log.cpp | 36 +- libs/shared/src/Util/log.h | 3 +- libs/shared/src/Util/util.h | 2 +- libs/sign-sdk/include/Base64.h | 18 +- libs/sign-sdk/include/BaseSigner.h | 15 +- libs/sign-sdk/include/BigInteger.h | 346 +++-- libs/sign-sdk/include/BigIntegerAlgorithms.h | 6 +- libs/sign-sdk/include/BigIntegerLibrary.h | 6 +- libs/sign-sdk/include/BigIntegerUtils.h | 57 +- libs/sign-sdk/include/BigUnsigned.h | 665 +++++----- libs/sign-sdk/include/BigUnsignedInABase.h | 176 ++- libs/sign-sdk/include/CIEEngineHelper.h | 12 +- libs/sign-sdk/include/CIESigner.h | 30 +- libs/sign-sdk/include/CardMod.h | 24 +- libs/sign-sdk/include/CertStore.h | 18 +- .../include/CounterSignatureGenerator.h | 48 +- libs/sign-sdk/include/LdapCrl.h | 2 - libs/sign-sdk/include/M7MParser.h | 24 +- libs/sign-sdk/include/NumberlikeArray.h | 237 ++-- libs/sign-sdk/include/PdfSignatureGenerator.h | 5 +- libs/sign-sdk/include/PdfVerifier.h | 2 +- libs/sign-sdk/include/SignatureGenerator.h | 86 +- libs/sign-sdk/include/SignedDataGeneratorEx.h | 52 +- libs/sign-sdk/include/SignedDocument.h | 70 +- libs/sign-sdk/include/SignerInfoGenerator.h | 61 +- libs/sign-sdk/include/TSAClient.h | 28 +- libs/sign-sdk/include/UUCLogger.h | 54 +- libs/sign-sdk/include/UUCProperties.h | 54 +- libs/sign-sdk/include/UUCStringTable.h | 40 +- libs/sign-sdk/include/UUCTextFileReader.h | 22 +- libs/sign-sdk/include/UUCTextFileWriter.h | 25 +- libs/sign-sdk/include/XAdESGenerator.h | 70 +- libs/sign-sdk/include/XAdESVerifier.h | 87 +- libs/sign-sdk/include/base64-std.h | 42 +- libs/sign-sdk/include/keys.h | 1 - libs/sign-sdk/src/ASN1/ASN1BitString.cpp | 5 +- libs/sign-sdk/src/ASN1/ASN1BitString.h | 1 - libs/sign-sdk/src/ASN1/ASN1Boolean.h | 4 +- libs/sign-sdk/src/ASN1/ASN1Exception.h | 2 + libs/sign-sdk/src/ASN1/ASN1Integer.cpp | 4 - libs/sign-sdk/src/ASN1/ASN1Object.h | 4 +- libs/sign-sdk/src/ASN1/ASN1Octetstring.cpp | 3 - libs/sign-sdk/src/ASN1/ASN1OptionalField.cpp | 2 - libs/sign-sdk/src/ASN1/ASN1OptionalField.h | 18 +- libs/sign-sdk/src/ASN1/AlgorithmIdentifier.h | 2 +- libs/sign-sdk/src/ASN1/Certificate.cpp | 4 +- libs/sign-sdk/src/ASN1/Certificate.h | 6 +- libs/sign-sdk/src/ASN1/ContentType.cpp | 2 - libs/sign-sdk/src/ASN1/Crl.cpp | 2 +- libs/sign-sdk/src/ASN1/DigestInfo.h | 2 +- .../src/ASN1/IssuerAndSerialNumber.cpp | 56 +- .../sign-sdk/src/ASN1/IssuerAndSerialNumber.h | 12 +- libs/sign-sdk/src/ASN1/Name.cpp | 279 ++-- libs/sign-sdk/src/ASN1/Name.h | 13 +- libs/sign-sdk/src/ASN1/OCSPRequest.cpp | 1 - libs/sign-sdk/src/ASN1/OCSPRequest.h | 1 - libs/sign-sdk/src/ASN1/PKIStatusInfo.cpp | 17 +- libs/sign-sdk/src/ASN1/PKIStatusInfo.h | 6 +- libs/sign-sdk/src/ASN1/RSAPrivateKey.h | 2 +- libs/sign-sdk/src/ASN1/RSAPublicKey.cpp | 27 +- libs/sign-sdk/src/ASN1/RSAPublicKey.h | 20 +- .../src/ASN1/RelativeDistinguishedName.cpp | 29 +- .../src/ASN1/RelativeDistinguishedName.h | 14 +- libs/sign-sdk/src/ASN1/SignedData.cpp | 16 +- libs/sign-sdk/src/ASN1/SignedData.h | 32 +- libs/sign-sdk/src/ASN1/SignerInfo.cpp | 1160 +++++++++-------- libs/sign-sdk/src/ASN1/SignerInfo.h | 72 +- .../src/ASN1/SubjectPublicKeyInfo.cpp | 17 +- libs/sign-sdk/src/ASN1/SubjectPublicKeyInfo.h | 14 +- libs/sign-sdk/src/ASN1/TSTInfo.cpp | 48 +- libs/sign-sdk/src/ASN1/TSTInfo.h | 22 +- libs/sign-sdk/src/ASN1/TimeStampData.cpp | 58 +- libs/sign-sdk/src/ASN1/TimeStampData.h | 3 +- libs/sign-sdk/src/ASN1/TimeStampRequest.cpp | 41 +- libs/sign-sdk/src/ASN1/TimeStampRequest.h | 12 +- libs/sign-sdk/src/ASN1/TimeStampResponse.cpp | 35 +- libs/sign-sdk/src/ASN1/TimeStampResponse.h | 20 +- libs/sign-sdk/src/ASN1/TimeStampToken.cpp | 41 +- libs/sign-sdk/src/ASN1/TimeStampToken.h | 20 +- libs/sign-sdk/src/ASN1/UUCBufferedReader.cpp | 139 +- libs/sign-sdk/src/ASN1/UUCBufferedReader.h | 1 - .../src/CounterSignatureGenerator.cpp | 3 +- libs/sign-sdk/src/PCSC.cpp | 4 +- libs/sign-sdk/src/RSA/des.h | 8 +- libs/sign-sdk/src/RSA/nn.h | 2 +- libs/sign-sdk/src/RSA/rsa.h | 16 +- libs/sign-sdk/src/SignatureGenerator.cpp | 5 +- libs/sign-sdk/src/SignedDataGeneratorEx.cpp | 3 +- libs/sign-sdk/src/SignedDocument.cpp | 14 +- 115 files changed, 2377 insertions(+), 2509 deletions(-) diff --git a/libs/pkcs11/src/CSP/VerificaConCIE.h b/libs/pkcs11/src/CSP/VerificaConCIE.h index d9038018..14f26599 100644 --- a/libs/pkcs11/src/CSP/VerificaConCIE.h +++ b/libs/pkcs11/src/CSP/VerificaConCIE.h @@ -11,8 +11,8 @@ #include #include -#include "Sign/CIEVerify.h" #include "CSP/AbilitaCIE.h" +#include "Sign/CIEVerify.h" typedef CK_RV (*verificaConCIEfn)(const char* inFilePath); typedef CK_RV (*getNumberOfSignfn)(void); diff --git a/libs/pkcs11/src/PKCS11/CIEP11Template.h b/libs/pkcs11/src/PKCS11/CIEP11Template.h index 563b11d2..d10ad930 100644 --- a/libs/pkcs11/src/PKCS11/CIEP11Template.h +++ b/libs/pkcs11/src/PKCS11/CIEP11Template.h @@ -1,8 +1,8 @@ #pragma once -#include "PKCS11/CardTemplate.h" -#include "PKCS11/Slot.h" #include +#include "PKCS11/CardTemplate.h" +#include "PKCS11/Slot.h" using namespace p11; diff --git a/libs/pkcs11/src/PKCS11/CardContext.h b/libs/pkcs11/src/PKCS11/CardContext.h index 347a15df..2484e6b0 100644 --- a/libs/pkcs11/src/PKCS11/CardContext.h +++ b/libs/pkcs11/src/PKCS11/CardContext.h @@ -1,7 +1,7 @@ #pragma once -#include #include +#include class CCardContext { public: diff --git a/libs/pkcs11/src/PKCS11/PKCS11Functions.h b/libs/pkcs11/src/PKCS11/PKCS11Functions.h index b8f3340c..135278f0 100644 --- a/libs/pkcs11/src/PKCS11/PKCS11Functions.h +++ b/libs/pkcs11/src/PKCS11/PKCS11Functions.h @@ -1,7 +1,7 @@ #pragma once -#include #include +#include #include "PKCS11/cryptoki.h" diff --git a/libs/pkcs11/src/PKCS11/Slot.h b/libs/pkcs11/src/PKCS11/Slot.h index 056edd03..ac7666fd 100644 --- a/libs/pkcs11/src/PKCS11/Slot.h +++ b/libs/pkcs11/src/PKCS11/Slot.h @@ -1,7 +1,7 @@ #pragma once -#include "Util/SyncroMutex.h" #include "PKCS11/cryptoki.h" +#include "Util/SyncroMutex.h" #pragma pack() #include @@ -11,7 +11,6 @@ #include "PKCS11/CardContext.h" - namespace p11 { typedef std::map> SlotMap; diff --git a/libs/pkcs11/src/PKCS11/pkcs11.h b/libs/pkcs11/src/PKCS11/pkcs11.h index 8de7b01b..dc8937a4 100644 --- a/libs/pkcs11/src/PKCS11/pkcs11.h +++ b/libs/pkcs11/src/PKCS11/pkcs11.h @@ -220,22 +220,19 @@ extern "C" { * #endif */ - /* All the various Cryptoki types and #define'd values are in the * file pkcs11t.h. */ #include "PKCS11/pkcs11t.h" -#define __PASTE(x,y) x##y - +#define __PASTE(x, y) x##y /* ============================================================== * Define the "extern" form of all the entry points. * ============================================================== */ -#define CK_NEED_ARG_LIST 1 -#define CK_PKCS11_FUNCTION_INFO(name) \ - extern CK_DECLARE_FUNCTION(CK_RV, name) +#define CK_NEED_ARG_LIST 1 +#define CK_PKCS11_FUNCTION_INFO(name) extern CK_DECLARE_FUNCTION(CK_RV, name) /* pkcs11f.h has all the information about the Cryptoki * function prototypes. */ @@ -244,7 +241,6 @@ extern "C" { #undef CK_NEED_ARG_LIST #undef CK_PKCS11_FUNCTION_INFO - /* ============================================================== * Define the typedef form of all the entry points. That is, for * each Cryptoki function C_XXX, define a type CK_C_XXX which is @@ -252,9 +248,9 @@ extern "C" { * ============================================================== */ -#define CK_NEED_ARG_LIST 1 +#define CK_NEED_ARG_LIST 1 #define CK_PKCS11_FUNCTION_INFO(name) \ - typedef CK_DECLARE_FUNCTION_POINTER(CK_RV, __PASTE(CK_,name)) + typedef CK_DECLARE_FUNCTION_POINTER(CK_RV, __PASTE(CK_, name)) /* pkcs11f.h has all the information about the Cryptoki * function prototypes. */ @@ -263,7 +259,6 @@ extern "C" { #undef CK_NEED_ARG_LIST #undef CK_PKCS11_FUNCTION_INFO - /* ============================================================== * Define structed vector of entry points. A CK_FUNCTION_LIST * contains a CK_VERSION indicating a library's Cryptoki version @@ -273,23 +268,19 @@ extern "C" { * ============================================================== */ -#define CK_PKCS11_FUNCTION_INFO(name) \ - __PASTE(CK_,name) name; +#define CK_PKCS11_FUNCTION_INFO(name) __PASTE(CK_, name) name; struct CK_FUNCTION_LIST { - - CK_VERSION version; /* Cryptoki version */ + CK_VERSION version; /* Cryptoki version */ /* Pile all the function pointers into the CK_FUNCTION_LIST. */ /* pkcs11f.h has all the information about the Cryptoki * function prototypes. */ #include "PKCS11/pkcs11f.h" - }; #undef CK_PKCS11_FUNCTION_INFO - #undef __PASTE #ifdef __cplusplus diff --git a/libs/pkcs11/src/keys.h b/libs/pkcs11/src/keys.h index 10dedebe..f118b25d 100644 --- a/libs/pkcs11/src/keys.h +++ b/libs/pkcs11/src/keys.h @@ -11,5 +11,4 @@ #define ENCRYPTION_KEY "this is a fake key" - #endif // KEYS_H_ diff --git a/libs/shared/src/CSP/ExtAuthKey.cpp b/libs/shared/src/CSP/ExtAuthKey.cpp index 1e7b649a..c94262f1 100644 --- a/libs/shared/src/CSP/ExtAuthKey.cpp +++ b/libs/shared/src/CSP/ExtAuthKey.cpp @@ -1,5 +1,5 @@ -#include "Util/Array.h" #include "Sign/definitions.h" +#include "Util/Array.h" BYTE ExtAuth_PrivExp[] = { 0x18, 0x6B, 0x31, 0x48, 0x8C, 0x25, 0xDC, 0xF8, 0x5D, 0x95, 0x3D, 0x36, diff --git a/libs/shared/src/Crypto/ASNParser.cpp b/libs/shared/src/Crypto/ASNParser.cpp index 0c58cf3a..2cc6dbb3 100644 --- a/libs/shared/src/Crypto/ASNParser.cpp +++ b/libs/shared/src/Crypto/ASNParser.cpp @@ -1,6 +1,7 @@ -#include "Util/Array.h" #include "ASNParser.h" +#include "Util/Array.h" + extern CLog Log; #define BitValue(a, b) ((a >> b) & 1) diff --git a/libs/shared/src/Crypto/ASNParser.h b/libs/shared/src/Crypto/ASNParser.h index 1a261d7e..bebbda4f 100644 --- a/libs/shared/src/Crypto/ASNParser.h +++ b/libs/shared/src/Crypto/ASNParser.h @@ -2,7 +2,7 @@ #include #include -#include "Sign/definitions.h" +#include "Util/Array.h" size_t GetASN1DataLenght(ByteArray &data); diff --git a/libs/shared/src/Crypto/DES3.cpp b/libs/shared/src/Crypto/DES3.cpp index 70f34c6d..e13c50c8 100644 --- a/libs/shared/src/Crypto/DES3.cpp +++ b/libs/shared/src/Crypto/DES3.cpp @@ -1,6 +1,8 @@ #include "Crypto/DES3.h" #include +#include +#include extern CLog Log; diff --git a/libs/shared/src/Crypto/RSA.h b/libs/shared/src/Crypto/RSA.h index f82328ff..168d464b 100644 --- a/libs/shared/src/Crypto/RSA.h +++ b/libs/shared/src/Crypto/RSA.h @@ -3,8 +3,8 @@ #include #include -#include "Util/Array.h" #include "Sign/definitions.h" +#include "Util/Array.h" class CRSA { CryptoPP::RSA::PublicKey pubKey; diff --git a/libs/shared/src/PCSC/APDU.cpp b/libs/shared/src/PCSC/APDU.cpp index 92b50e6b..2212fc7a 100644 --- a/libs/shared/src/PCSC/APDU.cpp +++ b/libs/shared/src/PCSC/APDU.cpp @@ -2,9 +2,9 @@ #include "Crypto/DES3.h" #include "Crypto/MAC.h" +#include "PCSC/Token.h" #include "Util/TLV.h" #include "Util/util.h" -#include "PCSC/Token.h" extern CLog Log; diff --git a/libs/shared/src/PCSC/CardLocker.h b/libs/shared/src/PCSC/CardLocker.h index 35b37ebb..079152ae 100644 --- a/libs/shared/src/PCSC/CardLocker.h +++ b/libs/shared/src/PCSC/CardLocker.h @@ -1,9 +1,9 @@ #pragma once -#include #include +#include -#include "Util/SyncroMutex.h" #include "PCSC/Token.h" +#include "Util/SyncroMutex.h" class CCardLocker { SCARDHANDLE hCard; diff --git a/libs/shared/src/PCSC/PCSC.h b/libs/shared/src/PCSC/PCSC.h index e4262c62..f1382796 100644 --- a/libs/shared/src/PCSC/PCSC.h +++ b/libs/shared/src/PCSC/PCSC.h @@ -1,8 +1,8 @@ #ifndef PCSC_H #define PCSC_H -#include #include +#include #include #include diff --git a/libs/shared/src/PCSC/Token.cpp b/libs/shared/src/PCSC/Token.cpp index e4542ba4..9a2a72f1 100644 --- a/libs/shared/src/PCSC/Token.cpp +++ b/libs/shared/src/PCSC/Token.cpp @@ -4,8 +4,8 @@ #include -#include "Util/TLV.h" #include "PCSC/APDU.h" +#include "Util/TLV.h" extern CLog Log; diff --git a/libs/shared/src/PCSC/Token.h b/libs/shared/src/PCSC/Token.h index 6918da06..764c199a 100644 --- a/libs/shared/src/PCSC/Token.h +++ b/libs/shared/src/PCSC/Token.h @@ -1,10 +1,10 @@ #pragma once -#include #include +#include -#include "Util/SyncroMutex.h" #include "PCSC/APDU.h" +#include "Util/SyncroMutex.h" extern SCARDCONTEXT hContext; diff --git a/libs/shared/src/PKCS11/pkcs11.h b/libs/shared/src/PKCS11/pkcs11.h index 8de7b01b..dc8937a4 100644 --- a/libs/shared/src/PKCS11/pkcs11.h +++ b/libs/shared/src/PKCS11/pkcs11.h @@ -220,22 +220,19 @@ extern "C" { * #endif */ - /* All the various Cryptoki types and #define'd values are in the * file pkcs11t.h. */ #include "PKCS11/pkcs11t.h" -#define __PASTE(x,y) x##y - +#define __PASTE(x, y) x##y /* ============================================================== * Define the "extern" form of all the entry points. * ============================================================== */ -#define CK_NEED_ARG_LIST 1 -#define CK_PKCS11_FUNCTION_INFO(name) \ - extern CK_DECLARE_FUNCTION(CK_RV, name) +#define CK_NEED_ARG_LIST 1 +#define CK_PKCS11_FUNCTION_INFO(name) extern CK_DECLARE_FUNCTION(CK_RV, name) /* pkcs11f.h has all the information about the Cryptoki * function prototypes. */ @@ -244,7 +241,6 @@ extern "C" { #undef CK_NEED_ARG_LIST #undef CK_PKCS11_FUNCTION_INFO - /* ============================================================== * Define the typedef form of all the entry points. That is, for * each Cryptoki function C_XXX, define a type CK_C_XXX which is @@ -252,9 +248,9 @@ extern "C" { * ============================================================== */ -#define CK_NEED_ARG_LIST 1 +#define CK_NEED_ARG_LIST 1 #define CK_PKCS11_FUNCTION_INFO(name) \ - typedef CK_DECLARE_FUNCTION_POINTER(CK_RV, __PASTE(CK_,name)) + typedef CK_DECLARE_FUNCTION_POINTER(CK_RV, __PASTE(CK_, name)) /* pkcs11f.h has all the information about the Cryptoki * function prototypes. */ @@ -263,7 +259,6 @@ extern "C" { #undef CK_NEED_ARG_LIST #undef CK_PKCS11_FUNCTION_INFO - /* ============================================================== * Define structed vector of entry points. A CK_FUNCTION_LIST * contains a CK_VERSION indicating a library's Cryptoki version @@ -273,23 +268,19 @@ extern "C" { * ============================================================== */ -#define CK_PKCS11_FUNCTION_INFO(name) \ - __PASTE(CK_,name) name; +#define CK_PKCS11_FUNCTION_INFO(name) __PASTE(CK_, name) name; struct CK_FUNCTION_LIST { - - CK_VERSION version; /* Cryptoki version */ + CK_VERSION version; /* Cryptoki version */ /* Pile all the function pointers into the CK_FUNCTION_LIST. */ /* pkcs11f.h has all the information about the Cryptoki * function prototypes. */ #include "PKCS11/pkcs11f.h" - }; #undef CK_PKCS11_FUNCTION_INFO - #undef __PASTE #ifdef __cplusplus diff --git a/libs/shared/src/Util/Array.cpp b/libs/shared/src/Util/Array.cpp index ec0124ab..f34b2256 100644 --- a/libs/shared/src/Util/Array.cpp +++ b/libs/shared/src/Util/Array.cpp @@ -1,3 +1,5 @@ +#include "Util/Array.h" + #include #include #include @@ -6,8 +8,6 @@ #include -#include "Util/Array.h" - ByteArray::ByteArray() { _data = nullptr; _size = 0; diff --git a/libs/shared/src/Util/CacheLib.cpp b/libs/shared/src/Util/CacheLib.cpp index 1fe0a69a..20e0241d 100644 --- a/libs/shared/src/Util/CacheLib.cpp +++ b/libs/shared/src/Util/CacheLib.cpp @@ -15,8 +15,8 @@ #include #include -#include "keys.h" #include "Util/util.h" +#include "keys.h" using namespace CryptoPP; diff --git a/libs/shared/src/Util/CryptoppUtils.cpp b/libs/shared/src/Util/CryptoppUtils.cpp index 851ffa9d..c861977b 100644 --- a/libs/shared/src/Util/CryptoppUtils.cpp +++ b/libs/shared/src/Util/CryptoppUtils.cpp @@ -28,14 +28,14 @@ // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // POSSIBILITY OF SUCH DAMAGE. +#include "CryptoppUtils.h" + #include #include #include #include -#include "CryptoppUtils.h" - using namespace CryptoPP; namespace lcp { diff --git a/libs/shared/src/Util/IniSettings.h b/libs/shared/src/Util/IniSettings.h index 315ce89d..7bc0a666 100644 --- a/libs/shared/src/Util/IniSettings.h +++ b/libs/shared/src/Util/IniSettings.h @@ -2,8 +2,8 @@ #include #include -#include "Util/Array.h" #include "Sign/definitions.h" +#include "Util/Array.h" class IniSettings; extern std::vector _iniSettings; diff --git a/libs/shared/src/Util/ModuleInfo.h b/libs/shared/src/Util/ModuleInfo.h index 032266d5..587e418f 100644 --- a/libs/shared/src/Util/ModuleInfo.h +++ b/libs/shared/src/Util/ModuleInfo.h @@ -1,4 +1,5 @@ #include + #include "Util/util.h" class CModuleInfo { diff --git a/libs/shared/src/Util/UUCByteArray.cpp b/libs/shared/src/Util/UUCByteArray.cpp index 485f4aa5..c0fd23ec 100644 --- a/libs/shared/src/Util/UUCByteArray.cpp +++ b/libs/shared/src/Util/UUCByteArray.cpp @@ -127,7 +127,7 @@ BYTE UUCByteArray::operator[](const unsigned int index) const { // throw(long) BYTE UUCByteArray::get(const unsigned int index) const { // throw(long) if (index >= m_unLen) { // SetLastError(ERR_INDEX_OUT_OF_BOUND); - throw(long) ERR_INDEX_OUT_OF_BOUND; + throw (long)ERR_INDEX_OUT_OF_BOUND; } return m_pbtContent[index]; } @@ -136,7 +136,7 @@ void UUCByteArray::set(const unsigned int index, const BYTE btVal) { // throw(long) if (index >= m_unLen) { // SetLastError(ERR_INDEX_OUT_OF_BOUND); - throw(long) ERR_INDEX_OUT_OF_BOUND; + throw (long)ERR_INDEX_OUT_OF_BOUND; } m_pbtContent[index] = btVal; } @@ -144,7 +144,7 @@ void UUCByteArray::set(const unsigned int index, void UUCByteArray::remove(const unsigned int index) { // throw(long) if (index >= m_unLen) { // SetLastError((long)ERR_INDEX_OUT_OF_BOUND); - throw(long) ERR_INDEX_OUT_OF_BOUND; + throw (long)ERR_INDEX_OUT_OF_BOUND; } for (unsigned int i = index; i < m_unLen - 1; i++) diff --git a/libs/shared/src/Util/UUCProperties.h b/libs/shared/src/Util/UUCProperties.h index 08ed3998..09ab08e8 100644 --- a/libs/shared/src/Util/UUCProperties.h +++ b/libs/shared/src/Util/UUCProperties.h @@ -24,8 +24,8 @@ #include -#include "Util/UUCByteArray.h" #include "UUCStringTable.h" +#include "Util/UUCByteArray.h" class UUCProperties { public: diff --git a/libs/shared/src/Util/UUCTextFileReader.cpp b/libs/shared/src/Util/UUCTextFileReader.cpp index 591ac646..06f5d3a6 100644 --- a/libs/shared/src/Util/UUCTextFileReader.cpp +++ b/libs/shared/src/Util/UUCTextFileReader.cpp @@ -28,7 +28,7 @@ UUCTextFileReader::UUCTextFileReader(const char* szFilePath) { m_pf = fopen(szFilePath, "rt"); if (!m_pf) { - throw(long) ERROR_FILE_NOT_FOUND; + throw (long)ERROR_FILE_NOT_FOUND; } struct stat lstat_buf; @@ -39,26 +39,26 @@ UUCTextFileReader::UUCTextFileReader(const char* szFilePath) { /* handle the case of the lstat failing first */ if (r == -1) { fclose(m_pf); - throw(long) ERROR_FILE_NOT_FOUND; + throw (long)ERROR_FILE_NOT_FOUND; } if (S_ISLNK(lstat_buf.st_mode)) { fclose(m_pf); - throw(long) ERROR_FILE_NOT_FOUND; + throw (long)ERROR_FILE_NOT_FOUND; } /* Get the properties of the opened file descriptor */ r = stat(szFilePath, &fstat_buf); if (r == -1) { fclose(m_pf); - throw(long) ERROR_FILE_NOT_FOUND; + throw (long)ERROR_FILE_NOT_FOUND; } if (lstat_buf.st_dev != fstat_buf.st_dev || lstat_buf.st_ino != fstat_buf.st_ino || (S_IFMT & lstat_buf.st_mode) != (S_IFMT & fstat_buf.st_mode)) { fclose(m_pf); - throw(long) ERROR_FILE_NOT_FOUND; + throw (long)ERROR_FILE_NOT_FOUND; } } @@ -88,7 +88,7 @@ long UUCTextFileReader::readLine(char* szLine, while ((fread(szLine + i, 1, 1, m_pf) > 0) && (szLine[i] != '\n')) { i++; if (i == nLen) { - throw(long) ERROR_MORE_DATA; + throw (long)ERROR_MORE_DATA; } } diff --git a/libs/shared/src/Util/log.cpp b/libs/shared/src/Util/log.cpp index 527189e7..a86bcd3e 100644 --- a/libs/shared/src/Util/log.cpp +++ b/libs/shared/src/Util/log.cpp @@ -1,23 +1,20 @@ #define __STDC_WANT_LIB_EXT1__ 1 -#include -#include "Util/util.h" -#include "ModuleInfo.h" -#include -#include -#include #include "Util/log.h" -#include "Util/UtilException.h" -#include "Util/IniSettings.h" -#include + +#include #include -#include -#include "UUCProperties.h" #include -#include -#include #include -#include +#include +#include + +#include +#include +#include + +#include "ModuleInfo.h" +#include "UUCProperties.h" std::string globalLogDir; std::string globalLogName; @@ -58,7 +55,8 @@ void initLog(const char *moduleName, const char *iniFile, const char *version) { // "3 ;LM_Module_Thread // un file per modulo e per // thread\n")).GetValue((char*)iniFile); - if (LogMode == -1) { + if (LogMode != LM_Single && LogMode != LM_Module && LogMode != LM_Thread && + LogMode != LM_Module_Thread) { LogMode = LM_Single; } @@ -68,15 +66,15 @@ void initLog(const char *moduleName, const char *iniFile, const char *version) { FunctionLog = settings.getIntProperty( "FunctionLog", 1); //, "Abilitazione log delle chiamate a - //funzione")).GetValue((char*)iniFile); + // funzione")).GetValue((char*)iniFile); GlobalDepth = settings.getIntProperty( "FunctionDepth", 10); //, "Definisce la profondità massima di log delle - //funzioni\n")).GetValue((char*)iniFile); + // funzioni\n")).GetValue((char*)iniFile); globalLogParam = settings.getIntProperty( "ParamLog", 1); //, "Abilitazione log dei parametri di input delle - //funzioni")).GetValue((char*)iniFile); + // funzioni")).GetValue((char*)iniFile); globalLogName = moduleName; @@ -100,7 +98,7 @@ void initLog(const char *moduleName, const char *iniFile, const char *version) { globalLogDir = settings.getProperty( "LogDir", path.c_str()); //"Definisce il path in cui salvare il file di - //log (con / finale)")) + // log (con / finale)")) } CLog::CLog() { init(); } diff --git a/libs/shared/src/Util/log.h b/libs/shared/src/Util/log.h index 46ee76ad..90665e72 100644 --- a/libs/shared/src/Util/log.h +++ b/libs/shared/src/Util/log.h @@ -3,10 +3,9 @@ #include #include +#include #include -#include "defines.h" - #ifndef OutputDebugString #define OutputDebugString printf #endif diff --git a/libs/shared/src/Util/util.h b/libs/shared/src/Util/util.h index 97f030c6..afa1db5a 100644 --- a/libs/shared/src/Util/util.h +++ b/libs/shared/src/Util/util.h @@ -2,8 +2,8 @@ #include -#include "Util/Array.h" #include "Sign/definitions.h" +#include "Util/Array.h" #include "Util/log.h" #define ERR_BAD_POINTER "Puntatore non valido" diff --git a/libs/sign-sdk/include/Base64.h b/libs/sign-sdk/include/Base64.h index 682a8db2..34579a2e 100644 --- a/libs/sign-sdk/include/Base64.h +++ b/libs/sign-sdk/include/Base64.h @@ -32,14 +32,14 @@ #ifndef _BASE64_H #define _BASE64_H -void base64_encode_block(unsigned char out[4], const unsigned char in[3], int len); -int base64_decode_block(unsigned char out[3], const unsigned char in[4]); -int base64_encoded_size(int len); -int base64_decoded_size(int len); -void base64_encode_binary(char *out, const unsigned char *in, int len); -int base64_decode_binary(unsigned char *out, const char *in); -char * base64_encode(const char *in, int size); -char * base64_decode(const char *in); - +void base64_encode_block(unsigned char out[4], const unsigned char in[3], + int len); +int base64_decode_block(unsigned char out[3], const unsigned char in[4]); +int base64_encoded_size(int len); +int base64_decoded_size(int len); +void base64_encode_binary(char *out, const unsigned char *in, int len); +int base64_decode_binary(unsigned char *out, const char *in); +char *base64_encode(const char *in, int size); +char *base64_decode(const char *in); #endif /* ! _BASE64_H */ diff --git a/libs/sign-sdk/include/BaseSigner.h b/libs/sign-sdk/include/BaseSigner.h index 5562aaa0..a718a7d8 100644 --- a/libs/sign-sdk/include/BaseSigner.h +++ b/libs/sign-sdk/include/BaseSigner.h @@ -2,12 +2,15 @@ #include "ASN1/Certificate.h" -class CBaseSigner -{ -public: - virtual long GetCertificate(const char* alias, CCertificate** ppCertificate, UUCByteArray& id) = 0; +class CBaseSigner { + public: + virtual ~CBaseSigner(){}; // make destructor virtual - virtual long Sign(UUCByteArray& data, UUCByteArray& id, int algo, UUCByteArray& signature) = 0; + virtual long GetCertificate(const char* alias, CCertificate** ppCertificate, + UUCByteArray& id) = 0; - virtual long Close() = 0; + virtual long Sign(UUCByteArray& data, UUCByteArray& id, int algo, + UUCByteArray& signature) = 0; + + virtual long Close() = 0; }; diff --git a/libs/sign-sdk/include/BigInteger.h b/libs/sign-sdk/include/BigInteger.h index 944f501d..47e04487 100644 --- a/libs/sign-sdk/include/BigInteger.h +++ b/libs/sign-sdk/include/BigInteger.h @@ -11,169 +11,169 @@ * longer derived from BigUnsigned because that led to harmful implicit * conversions.) */ class BigInteger { - -public: - typedef BigUnsigned::Blk Blk; - typedef BigUnsigned::Index Index; - typedef BigUnsigned::CmpRes CmpRes; - static const CmpRes - less = BigUnsigned::less , - equal = BigUnsigned::equal , - greater = BigUnsigned::greater; - // Enumeration for the sign of a BigInteger. - enum Sign { negative = -1, zero = 0, positive = 1 }; - -protected: - Sign sign; - BigUnsigned mag; - -public: - // Constructs zero. - BigInteger() : sign(zero), mag() {} - - // Copy constructor - BigInteger(const BigInteger &x) : sign(x.sign), mag(x.mag) {}; - - // Assignment operator - void operator=(const BigInteger &x); - - // Constructor that copies from a given array of blocks with a sign. - BigInteger(const Blk *b, Index blen, Sign s); - - // Nonnegative constructor that copies from a given array of blocks. - BigInteger(const Blk *b, Index blen) : mag(b, blen) { - sign = mag.isZero() ? zero : positive; - } - - // Constructor from a BigUnsigned and a sign - BigInteger(const BigUnsigned &x, Sign s); - - // Nonnegative constructor from a BigUnsigned - BigInteger(const BigUnsigned &x) : mag(x) { - sign = mag.isZero() ? zero : positive; - } - - // Constructors from primitive integer types - BigInteger(unsigned long x); - BigInteger( long x); - BigInteger(unsigned int x); - BigInteger( int x); - BigInteger(unsigned short x); - BigInteger( short x); - - /* Converters to primitive integer types - * The implicit conversion operators caused trouble, so these are now - * named. */ - unsigned long toUnsignedLong () const; - long toLong () const; - unsigned int toUnsignedInt () const; - int toInt () const; - unsigned short toUnsignedShort() const; - short toShort () const; -protected: - // Helper - template X convertToUnsignedPrimitive() const; - template X convertToSignedPrimitive() const; -public: - - // ACCESSORS - Sign getSign() const { return sign; } - /* The client can't do any harm by holding a read-only reference to the - * magnitude. */ - const BigUnsigned &getMagnitude() const { return mag; } - - // Some accessors that go through to the magnitude - Index getLength() const { return mag.getLength(); } - Index getCapacity() const { return mag.getCapacity(); } - Blk getBlock(Index i) const { return mag.getBlock(i); } - bool isZero() const { return sign == zero; } // A bit special - - // COMPARISONS - - // Compares this to x like Perl's <=> - CmpRes compareTo(const BigInteger &x) const; - - // Ordinary comparison operators - bool operator ==(const BigInteger &x) const { - return sign == x.sign && mag == x.mag; - } - bool operator !=(const BigInteger &x) const { return !operator ==(x); }; - bool operator < (const BigInteger &x) const { return compareTo(x) == less ; } - bool operator <=(const BigInteger &x) const { return compareTo(x) != greater; } - bool operator >=(const BigInteger &x) const { return compareTo(x) != less ; } - bool operator > (const BigInteger &x) const { return compareTo(x) == greater; } - - // OPERATORS -- See the discussion in BigUnsigned.hh. - void add (const BigInteger &a, const BigInteger &b); - void subtract(const BigInteger &a, const BigInteger &b); - void multiply(const BigInteger &a, const BigInteger &b); - /* See the comment on BigUnsigned::divideWithRemainder. Semantics - * differ from those of primitive integers when negatives and/or zeros - * are involved. */ - void divideWithRemainder(const BigInteger &b, BigInteger &q); - void negate(const BigInteger &a); - - /* Bitwise operators are not provided for BigIntegers. Use - * getMagnitude to get the magnitude and operate on that instead. */ - - BigInteger operator +(const BigInteger &x) const; - BigInteger operator -(const BigInteger &x) const; - BigInteger operator *(const BigInteger &x) const; - BigInteger operator /(const BigInteger &x) const; - BigInteger operator %(const BigInteger &x) const; - BigInteger operator -() const; - - void operator +=(const BigInteger &x); - void operator -=(const BigInteger &x); - void operator *=(const BigInteger &x); - void operator /=(const BigInteger &x); - void operator %=(const BigInteger &x); - void flipSign(); - - // INCREMENT/DECREMENT OPERATORS - void operator ++( ); - void operator ++(int); - void operator --( ); - void operator --(int); + public: + typedef BigUnsigned::Blk Blk; + typedef BigUnsigned::Index Index; + typedef BigUnsigned::CmpRes CmpRes; + static const CmpRes less = BigUnsigned::less, equal = BigUnsigned::equal, + greater = BigUnsigned::greater; + // Enumeration for the sign of a BigInteger. + enum Sign { negative = -1, zero = 0, positive = 1 }; + + protected: + Sign sign; + BigUnsigned mag; + + public: + // Constructs zero. + BigInteger() : sign(zero), mag() {} + + // Copy constructor + BigInteger(const BigInteger &x) : sign(x.sign), mag(x.mag){}; + + // Assignment operator + void operator=(const BigInteger &x); + + // Constructor that copies from a given array of blocks with a sign. + BigInteger(const Blk *b, Index blen, Sign s); + + // Nonnegative constructor that copies from a given array of blocks. + BigInteger(const Blk *b, Index blen) : mag(b, blen) { + sign = mag.isZero() ? zero : positive; + } + + // Constructor from a BigUnsigned and a sign + BigInteger(const BigUnsigned &x, Sign s); + + // Nonnegative constructor from a BigUnsigned + BigInteger(const BigUnsigned &x) : mag(x) { + sign = mag.isZero() ? zero : positive; + } + + // Constructors from primitive integer types + BigInteger(unsigned long x); + BigInteger(long x); + BigInteger(unsigned int x); + BigInteger(int x); + BigInteger(unsigned short x); + BigInteger(short x); + + /* Converters to primitive integer types + * The implicit conversion operators caused trouble, so these are now + * named. */ + unsigned long toUnsignedLong() const; + long toLong() const; + unsigned int toUnsignedInt() const; + int toInt() const; + unsigned short toUnsignedShort() const; + short toShort() const; + + protected: + // Helper + template + X convertToUnsignedPrimitive() const; + template + X convertToSignedPrimitive() const; + + public: + // ACCESSORS + Sign getSign() const { return sign; } + /* The client can't do any harm by holding a read-only reference to the + * magnitude. */ + const BigUnsigned &getMagnitude() const { return mag; } + + // Some accessors that go through to the magnitude + Index getLength() const { return mag.getLength(); } + Index getCapacity() const { return mag.getCapacity(); } + Blk getBlock(Index i) const { return mag.getBlock(i); } + bool isZero() const { return sign == zero; } // A bit special + + // COMPARISONS + + // Compares this to x like Perl's <=> + CmpRes compareTo(const BigInteger &x) const; + + // Ordinary comparison operators + bool operator==(const BigInteger &x) const { + return sign == x.sign && mag == x.mag; + } + bool operator!=(const BigInteger &x) const { return !operator==(x); }; + bool operator<(const BigInteger &x) const { return compareTo(x) == less; } + bool operator<=(const BigInteger &x) const { return compareTo(x) != greater; } + bool operator>=(const BigInteger &x) const { return compareTo(x) != less; } + bool operator>(const BigInteger &x) const { return compareTo(x) == greater; } + + // OPERATORS -- See the discussion in BigUnsigned.hh. + void add(const BigInteger &a, const BigInteger &b); + void subtract(const BigInteger &a, const BigInteger &b); + void multiply(const BigInteger &a, const BigInteger &b); + /* See the comment on BigUnsigned::divideWithRemainder. Semantics + * differ from those of primitive integers when negatives and/or zeros + * are involved. */ + void divideWithRemainder(const BigInteger &b, BigInteger &q); + void negate(const BigInteger &a); + + /* Bitwise operators are not provided for BigIntegers. Use + * getMagnitude to get the magnitude and operate on that instead. */ + + BigInteger operator+(const BigInteger &x) const; + BigInteger operator-(const BigInteger &x) const; + BigInteger operator*(const BigInteger &x) const; + BigInteger operator/(const BigInteger &x) const; + BigInteger operator%(const BigInteger &x) const; + BigInteger operator-() const; + + void operator+=(const BigInteger &x); + void operator-=(const BigInteger &x); + void operator*=(const BigInteger &x); + void operator/=(const BigInteger &x); + void operator%=(const BigInteger &x); + void flipSign(); + + // INCREMENT/DECREMENT OPERATORS + void operator++(); + void operator++(int); + void operator--(); + void operator--(int); }; // NORMAL OPERATORS /* These create an object to hold the result and invoke * the appropriate put-here operation on it, passing * this and x. The new object is then returned. */ -inline BigInteger BigInteger::operator +(const BigInteger &x) const { - BigInteger ans; - ans.add(*this, x); - return ans; +inline BigInteger BigInteger::operator+(const BigInteger &x) const { + BigInteger ans; + ans.add(*this, x); + return ans; } -inline BigInteger BigInteger::operator -(const BigInteger &x) const { - BigInteger ans; - ans.subtract(*this, x); - return ans; +inline BigInteger BigInteger::operator-(const BigInteger &x) const { + BigInteger ans; + ans.subtract(*this, x); + return ans; } -inline BigInteger BigInteger::operator *(const BigInteger &x) const { - BigInteger ans; - ans.multiply(*this, x); - return ans; +inline BigInteger BigInteger::operator*(const BigInteger &x) const { + BigInteger ans; + ans.multiply(*this, x); + return ans; } -inline BigInteger BigInteger::operator /(const BigInteger &x) const { - if (x.isZero()) throw "BigInteger::operator /: division by zero"; - BigInteger q, r; - r = *this; - r.divideWithRemainder(x, q); - return q; +inline BigInteger BigInteger::operator/(const BigInteger &x) const { + if (x.isZero()) throw "BigInteger::operator /: division by zero"; + BigInteger q, r; + r = *this; + r.divideWithRemainder(x, q); + return q; } -inline BigInteger BigInteger::operator %(const BigInteger &x) const { - if (x.isZero()) throw "BigInteger::operator %: division by zero"; - BigInteger q, r; - r = *this; - r.divideWithRemainder(x, q); - return r; +inline BigInteger BigInteger::operator%(const BigInteger &x) const { + if (x.isZero()) throw "BigInteger::operator %: division by zero"; + BigInteger q, r; + r = *this; + r.divideWithRemainder(x, q); + return r; } -inline BigInteger BigInteger::operator -() const { - BigInteger ans; - ans.negate(*this); - return ans; +inline BigInteger BigInteger::operator-() const { + BigInteger ans; + ans.negate(*this); + return ans; } /* @@ -183,33 +183,25 @@ inline BigInteger BigInteger::operator -() const { * belongs to the put-here operations. See Assignment Operators in * BigUnsigned.hh. */ -inline void BigInteger::operator +=(const BigInteger &x) { - add(*this, x); -} -inline void BigInteger::operator -=(const BigInteger &x) { - subtract(*this, x); -} -inline void BigInteger::operator *=(const BigInteger &x) { - multiply(*this, x); +inline void BigInteger::operator+=(const BigInteger &x) { add(*this, x); } +inline void BigInteger::operator-=(const BigInteger &x) { subtract(*this, x); } +inline void BigInteger::operator*=(const BigInteger &x) { multiply(*this, x); } +inline void BigInteger::operator/=(const BigInteger &x) { + if (x.isZero()) throw "BigInteger::operator /=: division by zero"; + /* The following technique is slightly faster than copying *this first + * when x is large. */ + BigInteger q; + divideWithRemainder(x, q); + // *this contains the remainder, but we overwrite it with the quotient. + *this = q; } -inline void BigInteger::operator /=(const BigInteger &x) { - if (x.isZero()) throw "BigInteger::operator /=: division by zero"; - /* The following technique is slightly faster than copying *this first - * when x is large. */ - BigInteger q; - divideWithRemainder(x, q); - // *this contains the remainder, but we overwrite it with the quotient. - *this = q; -} -inline void BigInteger::operator %=(const BigInteger &x) { - if (x.isZero()) throw "BigInteger::operator %=: division by zero"; - BigInteger q; - // Mods *this by x. Don't care about quotient left in q. - divideWithRemainder(x, q); +inline void BigInteger::operator%=(const BigInteger &x) { + if (x.isZero()) throw "BigInteger::operator %=: division by zero"; + BigInteger q; + // Mods *this by x. Don't care about quotient left in q. + divideWithRemainder(x, q); } // This one is trivial -inline void BigInteger::flipSign() { - sign = Sign(-sign); -} +inline void BigInteger::flipSign() { sign = Sign(-sign); } #endif diff --git a/libs/sign-sdk/include/BigIntegerAlgorithms.h b/libs/sign-sdk/include/BigIntegerAlgorithms.h index 0dc8d308..302ff4fb 100644 --- a/libs/sign-sdk/include/BigIntegerAlgorithms.h +++ b/libs/sign-sdk/include/BigIntegerAlgorithms.h @@ -11,8 +11,8 @@ BigUnsigned gcd(BigUnsigned a, BigUnsigned b); /* Extended Euclidean algorithm. * Given m and n, finds gcd g and numbers r, s such that r*m + s*n == g. */ -void extendedEuclidean(BigInteger m, BigInteger n, - BigInteger &g, BigInteger &r, BigInteger &s); +void extendedEuclidean(BigInteger m, BigInteger n, BigInteger &g, BigInteger &r, + BigInteger &s); /* Returns the multiplicative inverse of x modulo n, or throws an exception if * they have a common factor. */ @@ -20,6 +20,6 @@ BigUnsigned modinv(const BigInteger &x, const BigUnsigned &n); // Returns (base ^ exponent) % modulus. BigUnsigned modexp(const BigInteger &base, const BigUnsigned &exponent, - const BigUnsigned &modulus); + const BigUnsigned &modulus); #endif diff --git a/libs/sign-sdk/include/BigIntegerLibrary.h b/libs/sign-sdk/include/BigIntegerLibrary.h index 1d89ecab..382221bf 100644 --- a/libs/sign-sdk/include/BigIntegerLibrary.h +++ b/libs/sign-sdk/include/BigIntegerLibrary.h @@ -1,8 +1,8 @@ // This header file includes all of the library header files. -#include "NumberlikeArray.h" -#include "BigUnsigned.h" #include "BigInteger.h" #include "BigIntegerAlgorithms.h" -#include "BigUnsignedInABase.h" #include "BigIntegerUtils.h" +#include "BigUnsigned.h" +#include "BigUnsignedInABase.h" +#include "NumberlikeArray.h" diff --git a/libs/sign-sdk/include/BigIntegerUtils.h b/libs/sign-sdk/include/BigIntegerUtils.h index 5eb2c27f..caf93aba 100644 --- a/libs/sign-sdk/include/BigIntegerUtils.h +++ b/libs/sign-sdk/include/BigIntegerUtils.h @@ -1,9 +1,10 @@ #ifndef BIGINTEGERUTILS_H #define BIGINTEGERUTILS_H -#include "BigInteger.h" -#include #include +#include + +#include "BigInteger.h" /* This file provides: * - Convenient std::string <-> BigUnsigned/BigInteger conversion routines @@ -17,14 +18,16 @@ BigInteger stringToBigInteger(const std::string &s); // Creates a BigInteger from data such as `char's; read below for details. template -BigInteger dataToBigInteger(const T* data, BigInteger::Index length, BigInteger::Sign sign); +BigInteger dataToBigInteger(const T *data, BigInteger::Index length, + BigInteger::Sign sign); // Outputs x to os, obeying the flags `dec', `hex', `bin', and `showbase'. -std::ostream &operator <<(std::ostream &os, const BigUnsigned &x); +std::ostream &operator<<(std::ostream &os, const BigUnsigned &x); // Outputs x to os, obeying the flags `dec', `hex', `bin', and `showbase'. -// My somewhat arbitrary policy: a negative sign comes before a base indicator (like -0xFF). -std::ostream &operator <<(std::ostream &os, const BigInteger &x); +// My somewhat arbitrary policy: a negative sign comes before a base indicator +// (like -0xFF). +std::ostream &operator<<(std::ostream &os, const BigInteger &x); // BEGIN TEMPLATE DEFINITIONS. @@ -42,31 +45,33 @@ std::ostream &operator <<(std::ostream &os, const BigInteger &x); * the result contain the desired binary data. */ template -BigInteger dataToBigInteger(const T* data, BigInteger::Index length, BigInteger::Sign sign) { - // really ceiling(numBytes / sizeof(BigInteger::Blk)) - unsigned int pieceSizeInBits = 8 * sizeof(T); - unsigned int piecesPerBlock = sizeof(BigInteger::Blk) / sizeof(T); - unsigned int numBlocks = (length + piecesPerBlock - 1) / piecesPerBlock; +BigInteger dataToBigInteger(const T *data, BigInteger::Index length, + BigInteger::Sign sign) { + // really ceiling(numBytes / sizeof(BigInteger::Blk)) + unsigned int pieceSizeInBits = 8 * sizeof(T); + unsigned int piecesPerBlock = sizeof(BigInteger::Blk) / sizeof(T); + unsigned int numBlocks = (length + piecesPerBlock - 1) / piecesPerBlock; - // Allocate our block array - BigInteger::Blk *blocks = new BigInteger::Blk[numBlocks]; + // Allocate our block array + BigInteger::Blk *blocks = new BigInteger::Blk[numBlocks]; - BigInteger::Index blockNum, pieceNum, pieceNumHere; + BigInteger::Index blockNum, pieceNum, pieceNumHere; - // Convert - for (blockNum = 0, pieceNum = 0; blockNum < numBlocks; blockNum++) { - BigInteger::Blk curBlock = 0; - for (pieceNumHere = 0; pieceNumHere < piecesPerBlock && pieceNum < length; - pieceNumHere++, pieceNum++) - curBlock |= (BigInteger::Blk(data[pieceNum]) << (pieceSizeInBits * pieceNumHere)); - blocks[blockNum] = curBlock; - } + // Convert + for (blockNum = 0, pieceNum = 0; blockNum < numBlocks; blockNum++) { + BigInteger::Blk curBlock = 0; + for (pieceNumHere = 0; pieceNumHere < piecesPerBlock && pieceNum < length; + pieceNumHere++, pieceNum++) + curBlock |= + (BigInteger::Blk(data[pieceNum]) << (pieceSizeInBits * pieceNumHere)); + blocks[blockNum] = curBlock; + } - // Create the BigInteger. - BigInteger x(blocks, numBlocks, sign); + // Create the BigInteger. + BigInteger x(blocks, numBlocks, sign); - delete [] blocks; - return x; + delete[] blocks; + return x; } #endif diff --git a/libs/sign-sdk/include/BigUnsigned.h b/libs/sign-sdk/include/BigUnsigned.h index d1e63f11..fc8552e6 100644 --- a/libs/sign-sdk/include/BigUnsigned.h +++ b/libs/sign-sdk/include/BigUnsigned.h @@ -11,333 +11,324 @@ * written in base 256^sizeof(unsigned long). The least significant block is * first, and the length is such that the most significant block is nonzero. */ class BigUnsigned : protected NumberlikeArray { - -public: - // Enumeration for the result of a comparison. - enum CmpRes { less = -1, equal = 0, greater = 1 }; - - // BigUnsigneds are built with a Blk type of unsigned long. - typedef unsigned long Blk; - - typedef NumberlikeArray::Index Index; - using NumberlikeArray::N; - -protected: - // Creates a BigUnsigned with a capacity; for internal use. - BigUnsigned(int, Index c) : NumberlikeArray(0, c) {} - - // Decreases len to eliminate any leading zero blocks. - void zapLeadingZeros() { - while (len > 0 && blk[len - 1] == 0) - len--; - } - -public: - // Constructs zero. - BigUnsigned() : NumberlikeArray() {} - - // Copy constructor - BigUnsigned(const BigUnsigned &x) : NumberlikeArray(x) {} - - // Assignment operator - void operator=(const BigUnsigned &x) { - NumberlikeArray::operator =(x); - } - - // Constructor that copies from a given array of blocks. - BigUnsigned(const Blk *b, Index blen) : NumberlikeArray(b, blen) { - // Eliminate any leading zeros we may have been passed. - zapLeadingZeros(); - } - - // Destructor. NumberlikeArray does the delete for us. - ~BigUnsigned() {} - - // Constructors from primitive integer types - BigUnsigned(unsigned long x); - BigUnsigned( long x); - BigUnsigned(unsigned int x); - BigUnsigned( int x); - BigUnsigned(unsigned short x); - BigUnsigned( short x); -protected: - // Helpers - template void initFromPrimitive (X x); - template void initFromSignedPrimitive(X x); -public: - - /* Converters to primitive integer types - * The implicit conversion operators caused trouble, so these are now - * named. */ - unsigned long toUnsignedLong () const; - long toLong () const; - unsigned int toUnsignedInt () const; - int toInt () const; - unsigned short toUnsignedShort() const; - short toShort () const; -protected: - // Helpers - template X convertToSignedPrimitive() const; - template X convertToPrimitive () const; -public: - - // BIT/BLOCK ACCESSORS - - // Expose these from NumberlikeArray directly. - using NumberlikeArray::getCapacity; - using NumberlikeArray::getLength; - - /* Returns the requested block, or 0 if it is beyond the length (as if - * the number had 0s infinitely to the left). */ - Blk getBlock(Index i) const { return i >= len ? 0 : blk[i]; } - /* Sets the requested block. The number grows or shrinks as necessary. */ - void setBlock(Index i, Blk newBlock); - - // The number is zero if and only if the canonical length is zero. - bool isZero() const { return NumberlikeArray::isEmpty(); } - - /* Returns the length of the number in bits, i.e., zero if the number - * is zero and otherwise one more than the largest value of bi for - * which getBit(bi) returns true. */ - Index bitLength() const; - /* Get the state of bit bi, which has value 2^bi. Bits beyond the - * number's length are considered to be 0. */ - bool getBit(Index bi) const { - return (getBlock(bi / N) & (Blk(1) << (bi % N))) != 0; - } - /* Sets the state of bit bi to newBit. The number grows or shrinks as - * necessary. */ - void setBit(Index bi, bool newBit); - - // COMPARISONS - - // Compares this to x like Perl's <=> - CmpRes compareTo(const BigUnsigned &x) const; - - // Ordinary comparison operators - bool operator ==(const BigUnsigned &x) const { - return NumberlikeArray::operator ==(x); - } - bool operator !=(const BigUnsigned &x) const { - return NumberlikeArray::operator !=(x); - } - bool operator < (const BigUnsigned &x) const { return compareTo(x) == less ; } - bool operator <=(const BigUnsigned &x) const { return compareTo(x) != greater; } - bool operator >=(const BigUnsigned &x) const { return compareTo(x) != less ; } - bool operator > (const BigUnsigned &x) const { return compareTo(x) == greater; } - - /* - * BigUnsigned and BigInteger both provide three kinds of operators. - * Here ``big-integer'' refers to BigInteger or BigUnsigned. - * - * (1) Overloaded ``return-by-value'' operators: - * +, -, *, /, %, unary -, &, |, ^, <<, >>. - * Big-integer code using these operators looks identical to code using - * the primitive integer types. These operators take one or two - * big-integer inputs and return a big-integer result, which can then - * be assigned to a BigInteger variable or used in an expression. - * Example: - * BigInteger a(1), b = 1; - * BigInteger c = a + b; - * - * (2) Overloaded assignment operators: - * +=, -=, *=, /=, %=, flipSign, &=, |=, ^=, <<=, >>=, ++, --. - * Again, these are used on big integers just like on ints. They take - * one writable big integer that both provides an operand and receives a - * result. Most also take a second read-only operand. - * Example: - * BigInteger a(1), b(1); - * a += b; - * - * (3) Copy-less operations: `add', `subtract', etc. - * These named methods take operands as arguments and store the result - * in the receiver (*this), avoiding unnecessary copies and allocations. - * `divideWithRemainder' is special: it both takes the dividend from and - * stores the remainder into the receiver, and it takes a separate - * object in which to store the quotient. NOTE: If you are wondering - * why these don't return a value, you probably mean to use the - * overloaded return-by-value operators instead. - * - * Examples: - * BigInteger a(43), b(7), c, d; - * - * c = a + b; // Now c == 50. - * c.add(a, b); // Same effect but without the two copies. - * - * c.divideWithRemainder(b, d); - * // 50 / 7; now d == 7 (quotient) and c == 1 (remainder). - * - * // ``Aliased'' calls now do the right thing using a temporary - * // copy, but see note on `divideWithRemainder'. - * a.add(a, b); - */ - - // COPY-LESS OPERATIONS - - // These 8: Arguments are read-only operands, result is saved in *this. - void add(const BigUnsigned &a, const BigUnsigned &b); - void subtract(const BigUnsigned &a, const BigUnsigned &b); - void multiply(const BigUnsigned &a, const BigUnsigned &b); - void bitAnd(const BigUnsigned &a, const BigUnsigned &b); - void bitOr(const BigUnsigned &a, const BigUnsigned &b); - void bitXor(const BigUnsigned &a, const BigUnsigned &b); - /* Negative shift amounts translate to opposite-direction shifts, - * except for -2^(8*sizeof(int)-1) which is unimplemented. */ - void bitShiftLeft(const BigUnsigned &a, int b); - void bitShiftRight(const BigUnsigned &a, int b); - - /* `a.divideWithRemainder(b, q)' is like `q = a / b, a %= b'. - * / and % use semantics similar to Knuth's, which differ from the - * primitive integer semantics under division by zero. See the - * implementation in BigUnsigned.cc for details. - * `a.divideWithRemainder(b, a)' throws an exception: it doesn't make - * sense to write quotient and remainder into the same variable. */ - void divideWithRemainder(const BigUnsigned &b, BigUnsigned &q); - - /* `divide' and `modulo' are no longer offered. Use - * `divideWithRemainder' instead. */ - - // OVERLOADED RETURN-BY-VALUE OPERATORS - BigUnsigned operator +(const BigUnsigned &x) const; - BigUnsigned operator -(const BigUnsigned &x) const; - BigUnsigned operator *(const BigUnsigned &x) const; - BigUnsigned operator /(const BigUnsigned &x) const; - BigUnsigned operator %(const BigUnsigned &x) const; - /* OK, maybe unary minus could succeed in one case, but it really - * shouldn't be used, so it isn't provided. */ - BigUnsigned operator &(const BigUnsigned &x) const; - BigUnsigned operator |(const BigUnsigned &x) const; - BigUnsigned operator ^(const BigUnsigned &x) const; - BigUnsigned operator <<(int b) const; - BigUnsigned operator >>(int b) const; - - // OVERLOADED ASSIGNMENT OPERATORS - void operator +=(const BigUnsigned &x); - void operator -=(const BigUnsigned &x); - void operator *=(const BigUnsigned &x); - void operator /=(const BigUnsigned &x); - void operator %=(const BigUnsigned &x); - void operator &=(const BigUnsigned &x); - void operator |=(const BigUnsigned &x); - void operator ^=(const BigUnsigned &x); - void operator <<=(int b); - void operator >>=(int b); - - /* INCREMENT/DECREMENT OPERATORS - * To discourage messy coding, these do not return *this, so prefix - * and postfix behave the same. */ - void operator ++( ); - void operator ++(int); - void operator --( ); - void operator --(int); - - // Helper function that needs access to BigUnsigned internals - friend Blk getShiftedBlock(const BigUnsigned &num, Index x, - unsigned int y); - - // See BigInteger.cc. - template - friend X convertBigUnsignedToPrimitiveAccess(const BigUnsigned &a); + public: + // Enumeration for the result of a comparison. + enum CmpRes { less = -1, equal = 0, greater = 1 }; + + // BigUnsigneds are built with a Blk type of unsigned long. + typedef unsigned long Blk; + + typedef NumberlikeArray::Index Index; + using NumberlikeArray::N; + + protected: + // Creates a BigUnsigned with a capacity; for internal use. + BigUnsigned(int, Index c) : NumberlikeArray(0, c) {} + + // Decreases len to eliminate any leading zero blocks. + void zapLeadingZeros() { + while (len > 0 && blk[len - 1] == 0) len--; + } + + public: + // Constructs zero. + BigUnsigned() : NumberlikeArray() {} + + // Copy constructor + BigUnsigned(const BigUnsigned &x) : NumberlikeArray(x) {} + + // Assignment operator + void operator=(const BigUnsigned &x) { NumberlikeArray::operator=(x); } + + // Constructor that copies from a given array of blocks. + BigUnsigned(const Blk *b, Index blen) : NumberlikeArray(b, blen) { + // Eliminate any leading zeros we may have been passed. + zapLeadingZeros(); + } + + // Destructor. NumberlikeArray does the delete for us. + ~BigUnsigned() {} + + // Constructors from primitive integer types + BigUnsigned(unsigned long x); + BigUnsigned(long x); + BigUnsigned(unsigned int x); + BigUnsigned(int x); + BigUnsigned(unsigned short x); + BigUnsigned(short x); + + protected: + // Helpers + template + void initFromPrimitive(X x); + template + void initFromSignedPrimitive(X x); + + public: + /* Converters to primitive integer types + * The implicit conversion operators caused trouble, so these are now + * named. */ + unsigned long toUnsignedLong() const; + long toLong() const; + unsigned int toUnsignedInt() const; + int toInt() const; + unsigned short toUnsignedShort() const; + short toShort() const; + + protected: + // Helpers + template + X convertToSignedPrimitive() const; + template + X convertToPrimitive() const; + + public: + // BIT/BLOCK ACCESSORS + + // Expose these from NumberlikeArray directly. + using NumberlikeArray::getCapacity; + using NumberlikeArray::getLength; + + /* Returns the requested block, or 0 if it is beyond the length (as if + * the number had 0s infinitely to the left). */ + Blk getBlock(Index i) const { return i >= len ? 0 : blk[i]; } + /* Sets the requested block. The number grows or shrinks as necessary. */ + void setBlock(Index i, Blk newBlock); + + // The number is zero if and only if the canonical length is zero. + bool isZero() const { return NumberlikeArray::isEmpty(); } + + /* Returns the length of the number in bits, i.e., zero if the number + * is zero and otherwise one more than the largest value of bi for + * which getBit(bi) returns true. */ + Index bitLength() const; + /* Get the state of bit bi, which has value 2^bi. Bits beyond the + * number's length are considered to be 0. */ + bool getBit(Index bi) const { + return (getBlock(bi / N) & (Blk(1) << (bi % N))) != 0; + } + /* Sets the state of bit bi to newBit. The number grows or shrinks as + * necessary. */ + void setBit(Index bi, bool newBit); + + // COMPARISONS + + // Compares this to x like Perl's <=> + CmpRes compareTo(const BigUnsigned &x) const; + + // Ordinary comparison operators + bool operator==(const BigUnsigned &x) const { + return NumberlikeArray::operator==(x); + } + bool operator!=(const BigUnsigned &x) const { + return NumberlikeArray::operator!=(x); + } + bool operator<(const BigUnsigned &x) const { return compareTo(x) == less; } + bool operator<=(const BigUnsigned &x) const { + return compareTo(x) != greater; + } + bool operator>=(const BigUnsigned &x) const { return compareTo(x) != less; } + bool operator>(const BigUnsigned &x) const { return compareTo(x) == greater; } + + /* + * BigUnsigned and BigInteger both provide three kinds of operators. + * Here ``big-integer'' refers to BigInteger or BigUnsigned. + * + * (1) Overloaded ``return-by-value'' operators: + * +, -, *, /, %, unary -, &, |, ^, <<, >>. + * Big-integer code using these operators looks identical to code using + * the primitive integer types. These operators take one or two + * big-integer inputs and return a big-integer result, which can then + * be assigned to a BigInteger variable or used in an expression. + * Example: + * BigInteger a(1), b = 1; + * BigInteger c = a + b; + * + * (2) Overloaded assignment operators: + * +=, -=, *=, /=, %=, flipSign, &=, |=, ^=, <<=, >>=, ++, --. + * Again, these are used on big integers just like on ints. They take + * one writable big integer that both provides an operand and receives a + * result. Most also take a second read-only operand. + * Example: + * BigInteger a(1), b(1); + * a += b; + * + * (3) Copy-less operations: `add', `subtract', etc. + * These named methods take operands as arguments and store the result + * in the receiver (*this), avoiding unnecessary copies and allocations. + * `divideWithRemainder' is special: it both takes the dividend from and + * stores the remainder into the receiver, and it takes a separate + * object in which to store the quotient. NOTE: If you are wondering + * why these don't return a value, you probably mean to use the + * overloaded return-by-value operators instead. + * + * Examples: + * BigInteger a(43), b(7), c, d; + * + * c = a + b; // Now c == 50. + * c.add(a, b); // Same effect but without the two copies. + * + * c.divideWithRemainder(b, d); + * // 50 / 7; now d == 7 (quotient) and c == 1 (remainder). + * + * // ``Aliased'' calls now do the right thing using a temporary + * // copy, but see note on `divideWithRemainder'. + * a.add(a, b); + */ + + // COPY-LESS OPERATIONS + + // These 8: Arguments are read-only operands, result is saved in *this. + void add(const BigUnsigned &a, const BigUnsigned &b); + void subtract(const BigUnsigned &a, const BigUnsigned &b); + void multiply(const BigUnsigned &a, const BigUnsigned &b); + void bitAnd(const BigUnsigned &a, const BigUnsigned &b); + void bitOr(const BigUnsigned &a, const BigUnsigned &b); + void bitXor(const BigUnsigned &a, const BigUnsigned &b); + /* Negative shift amounts translate to opposite-direction shifts, + * except for -2^(8*sizeof(int)-1) which is unimplemented. */ + void bitShiftLeft(const BigUnsigned &a, int b); + void bitShiftRight(const BigUnsigned &a, int b); + + /* `a.divideWithRemainder(b, q)' is like `q = a / b, a %= b'. + * / and % use semantics similar to Knuth's, which differ from the + * primitive integer semantics under division by zero. See the + * implementation in BigUnsigned.cc for details. + * `a.divideWithRemainder(b, a)' throws an exception: it doesn't make + * sense to write quotient and remainder into the same variable. */ + void divideWithRemainder(const BigUnsigned &b, BigUnsigned &q); + + /* `divide' and `modulo' are no longer offered. Use + * `divideWithRemainder' instead. */ + + // OVERLOADED RETURN-BY-VALUE OPERATORS + BigUnsigned operator+(const BigUnsigned &x) const; + BigUnsigned operator-(const BigUnsigned &x) const; + BigUnsigned operator*(const BigUnsigned &x) const; + BigUnsigned operator/(const BigUnsigned &x) const; + BigUnsigned operator%(const BigUnsigned &x) const; + /* OK, maybe unary minus could succeed in one case, but it really + * shouldn't be used, so it isn't provided. */ + BigUnsigned operator&(const BigUnsigned &x) const; + BigUnsigned operator|(const BigUnsigned &x) const; + BigUnsigned operator^(const BigUnsigned &x) const; + BigUnsigned operator<<(int b) const; + BigUnsigned operator>>(int b) const; + + // OVERLOADED ASSIGNMENT OPERATORS + void operator+=(const BigUnsigned &x); + void operator-=(const BigUnsigned &x); + void operator*=(const BigUnsigned &x); + void operator/=(const BigUnsigned &x); + void operator%=(const BigUnsigned &x); + void operator&=(const BigUnsigned &x); + void operator|=(const BigUnsigned &x); + void operator^=(const BigUnsigned &x); + void operator<<=(int b); + void operator>>=(int b); + + /* INCREMENT/DECREMENT OPERATORS + * To discourage messy coding, these do not return *this, so prefix + * and postfix behave the same. */ + void operator++(); + void operator++(int); + void operator--(); + void operator--(int); + + // Helper function that needs access to BigUnsigned internals + friend Blk getShiftedBlock(const BigUnsigned &num, Index x, unsigned int y); + + // See BigInteger.cc. + template + friend X convertBigUnsignedToPrimitiveAccess(const BigUnsigned &a); }; /* Implementing the return-by-value and assignment operators in terms of the * copy-less operations. The copy-less operations are responsible for making * any necessary temporary copies to work around aliasing. */ -inline BigUnsigned BigUnsigned::operator +(const BigUnsigned &x) const { - BigUnsigned ans; - ans.add(*this, x); - return ans; +inline BigUnsigned BigUnsigned::operator+(const BigUnsigned &x) const { + BigUnsigned ans; + ans.add(*this, x); + return ans; } -inline BigUnsigned BigUnsigned::operator -(const BigUnsigned &x) const { - BigUnsigned ans; - ans.subtract(*this, x); - return ans; +inline BigUnsigned BigUnsigned::operator-(const BigUnsigned &x) const { + BigUnsigned ans; + ans.subtract(*this, x); + return ans; } -inline BigUnsigned BigUnsigned::operator *(const BigUnsigned &x) const { - BigUnsigned ans; - ans.multiply(*this, x); - return ans; +inline BigUnsigned BigUnsigned::operator*(const BigUnsigned &x) const { + BigUnsigned ans; + ans.multiply(*this, x); + return ans; } -inline BigUnsigned BigUnsigned::operator /(const BigUnsigned &x) const { - if (x.isZero()) throw "BigUnsigned::operator /: division by zero"; - BigUnsigned q, r; - r = *this; - r.divideWithRemainder(x, q); - return q; +inline BigUnsigned BigUnsigned::operator/(const BigUnsigned &x) const { + if (x.isZero()) throw "BigUnsigned::operator /: division by zero"; + BigUnsigned q, r; + r = *this; + r.divideWithRemainder(x, q); + return q; } -inline BigUnsigned BigUnsigned::operator %(const BigUnsigned &x) const { - if (x.isZero()) throw "BigUnsigned::operator %: division by zero"; - BigUnsigned q, r; - r = *this; - r.divideWithRemainder(x, q); - return r; +inline BigUnsigned BigUnsigned::operator%(const BigUnsigned &x) const { + if (x.isZero()) throw "BigUnsigned::operator %: division by zero"; + BigUnsigned q, r; + r = *this; + r.divideWithRemainder(x, q); + return r; } -inline BigUnsigned BigUnsigned::operator &(const BigUnsigned &x) const { - BigUnsigned ans; - ans.bitAnd(*this, x); - return ans; +inline BigUnsigned BigUnsigned::operator&(const BigUnsigned &x) const { + BigUnsigned ans; + ans.bitAnd(*this, x); + return ans; } -inline BigUnsigned BigUnsigned::operator |(const BigUnsigned &x) const { - BigUnsigned ans; - ans.bitOr(*this, x); - return ans; +inline BigUnsigned BigUnsigned::operator|(const BigUnsigned &x) const { + BigUnsigned ans; + ans.bitOr(*this, x); + return ans; } -inline BigUnsigned BigUnsigned::operator ^(const BigUnsigned &x) const { - BigUnsigned ans; - ans.bitXor(*this, x); - return ans; +inline BigUnsigned BigUnsigned::operator^(const BigUnsigned &x) const { + BigUnsigned ans; + ans.bitXor(*this, x); + return ans; } -inline BigUnsigned BigUnsigned::operator <<(int b) const { - BigUnsigned ans; - ans.bitShiftLeft(*this, b); - return ans; +inline BigUnsigned BigUnsigned::operator<<(int b) const { + BigUnsigned ans; + ans.bitShiftLeft(*this, b); + return ans; } -inline BigUnsigned BigUnsigned::operator >>(int b) const { - BigUnsigned ans; - ans.bitShiftRight(*this, b); - return ans; +inline BigUnsigned BigUnsigned::operator>>(int b) const { + BigUnsigned ans; + ans.bitShiftRight(*this, b); + return ans; } -inline void BigUnsigned::operator +=(const BigUnsigned &x) { - add(*this, x); -} -inline void BigUnsigned::operator -=(const BigUnsigned &x) { - subtract(*this, x); -} -inline void BigUnsigned::operator *=(const BigUnsigned &x) { - multiply(*this, x); -} -inline void BigUnsigned::operator /=(const BigUnsigned &x) { - if (x.isZero()) throw "BigUnsigned::operator /=: division by zero"; - /* The following technique is slightly faster than copying *this first - * when x is large. */ - BigUnsigned q; - divideWithRemainder(x, q); - // *this contains the remainder, but we overwrite it with the quotient. - *this = q; -} -inline void BigUnsigned::operator %=(const BigUnsigned &x) { - if (x.isZero()) throw "BigUnsigned::operator %=: division by zero"; - BigUnsigned q; - // Mods *this by x. Don't care about quotient left in q. - divideWithRemainder(x, q); -} -inline void BigUnsigned::operator &=(const BigUnsigned &x) { - bitAnd(*this, x); -} -inline void BigUnsigned::operator |=(const BigUnsigned &x) { - bitOr(*this, x); +inline void BigUnsigned::operator+=(const BigUnsigned &x) { add(*this, x); } +inline void BigUnsigned::operator-=(const BigUnsigned &x) { + subtract(*this, x); } -inline void BigUnsigned::operator ^=(const BigUnsigned &x) { - bitXor(*this, x); +inline void BigUnsigned::operator*=(const BigUnsigned &x) { + multiply(*this, x); } -inline void BigUnsigned::operator <<=(int b) { - bitShiftLeft(*this, b); +inline void BigUnsigned::operator/=(const BigUnsigned &x) { + if (x.isZero()) throw "BigUnsigned::operator /=: division by zero"; + /* The following technique is slightly faster than copying *this first + * when x is large. */ + BigUnsigned q; + divideWithRemainder(x, q); + // *this contains the remainder, but we overwrite it with the quotient. + *this = q; } -inline void BigUnsigned::operator >>=(int b) { - bitShiftRight(*this, b); +inline void BigUnsigned::operator%=(const BigUnsigned &x) { + if (x.isZero()) throw "BigUnsigned::operator %=: division by zero"; + BigUnsigned q; + // Mods *this by x. Don't care about quotient left in q. + divideWithRemainder(x, q); } +inline void BigUnsigned::operator&=(const BigUnsigned &x) { bitAnd(*this, x); } +inline void BigUnsigned::operator|=(const BigUnsigned &x) { bitOr(*this, x); } +inline void BigUnsigned::operator^=(const BigUnsigned &x) { bitXor(*this, x); } +inline void BigUnsigned::operator<<=(int b) { bitShiftLeft(*this, b); } +inline void BigUnsigned::operator>>=(int b) { bitShiftRight(*this, b); } /* Templates for conversions of BigUnsigned to and from primitive integers. * BigInteger.cc needs to instantiate convertToPrimitive, and the uses in @@ -354,15 +345,15 @@ inline void BigUnsigned::operator >>=(int b) { * known to be nonnegative. */ template void BigUnsigned::initFromPrimitive(X x) { - if (x == 0) - ; // NumberlikeArray already initialized us to zero. - else { - // Create a single block. blk is NULL; no need to delete it. - cap = 1; - blk = new Blk[1]; - len = 1; - blk[0] = Blk(x); - } + if (x == 0) + ; // NumberlikeArray already initialized us to zero. + else { + // Create a single block. blk is NULL; no need to delete it. + cap = 1; + blk = new Blk[1]; + len = 1; + blk[0] = Blk(x); + } } /* Ditto, but first check that x is nonnegative. I could have put the check in @@ -371,11 +362,11 @@ void BigUnsigned::initFromPrimitive(X x) { * a condition that is constant in *any* instantiation, even if not in all. */ template void BigUnsigned::initFromSignedPrimitive(X x) { - if (x < 0) - throw "BigUnsigned constructor: " + if (x < 0) + throw "BigUnsigned constructor: " "Cannot construct a BigUnsigned from a negative number"; - else - initFromPrimitive(x); + else + initFromPrimitive(x); } // CONVERSION TO PRIMITIVE INTEGERS @@ -385,19 +376,19 @@ void BigUnsigned::initFromSignedPrimitive(X x) { * clearer, which is the library's stated goal. */ template X BigUnsigned::convertToPrimitive() const { - if (len == 0) - // The number is zero; return zero. - return 0; - else if (len == 1) { - // The single block might fit in an X. Try the conversion. - X x = X(blk[0]); - // Make sure the result accurately represents the block. - if (Blk(x) == blk[0]) - // Successful conversion. - return x; - // Otherwise fall through. - } - throw "BigUnsigned::to: " + if (len == 0) + // The number is zero; return zero. + return 0; + else if (len == 1) { + // The single block might fit in an X. Try the conversion. + X x = X(blk[0]); + // Make sure the result accurately represents the block. + if (Blk(x) == blk[0]) + // Successful conversion. + return x; + // Otherwise fall through. + } + throw "BigUnsigned::to: " "Value is too big to fit in the requested type"; } @@ -407,11 +398,11 @@ X BigUnsigned::convertToPrimitive() const { * separated to avoid a g++ warning. */ template X BigUnsigned::convertToSignedPrimitive() const { - X x = convertToPrimitive(); - if (x >= 0) - return x; - else - throw "BigUnsigned::to(Primitive): " + X x = convertToPrimitive(); + if (x >= 0) + return x; + else + throw "BigUnsigned::to(Primitive): " "Value is too big to fit in the requested type"; } diff --git a/libs/sign-sdk/include/BigUnsignedInABase.h b/libs/sign-sdk/include/BigUnsignedInABase.h index f66e01e4..58bcb92e 100644 --- a/libs/sign-sdk/include/BigUnsignedInABase.h +++ b/libs/sign-sdk/include/BigUnsignedInABase.h @@ -1,10 +1,11 @@ #ifndef BIGUNSIGNEDINABASE_H #define BIGUNSIGNEDINABASE_H -#include "NumberlikeArray.h" -#include "BigUnsigned.h" #include +#include "BigUnsigned.h" +#include "NumberlikeArray.h" + /* * A BigUnsignedInABase object represents a nonnegative integer of size limited * only by available memory, represented in a user-specified base that can fit @@ -30,93 +31,90 @@ */ class BigUnsignedInABase : protected NumberlikeArray { - -public: - // The digits of a BigUnsignedInABase are unsigned shorts. - typedef unsigned short Digit; - // That's also the type of a base. - typedef Digit Base; - -protected: - // The base in which this BigUnsignedInABase is expressed - Base base; - - // Creates a BigUnsignedInABase with a capacity; for internal use. - BigUnsignedInABase(int, Index c) : NumberlikeArray(0, c) {} - - // Decreases len to eliminate any leading zero digits. - void zapLeadingZeros() { - while (len > 0 && blk[len - 1] == 0) - len--; - } - -public: - // Constructs zero in base 2. - BigUnsignedInABase() : NumberlikeArray(), base(2) {} - - // Copy constructor - BigUnsignedInABase(const BigUnsignedInABase &x) : NumberlikeArray(x), base(x.base) {} - - // Assignment operator - void operator =(const BigUnsignedInABase &x) { - NumberlikeArray::operator =(x); - base = x.base; - } - - // Constructor that copies from a given array of digits. - BigUnsignedInABase(const Digit *d, Index l, Base base); - - // Destructor. NumberlikeArray does the delete for us. - ~BigUnsignedInABase() {} - - // LINKS TO BIGUNSIGNED - BigUnsignedInABase(const BigUnsigned &x, Base base); - operator BigUnsigned() const; - - /* LINKS TO STRINGS - * - * These use the symbols ``0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'' to - * represent digits of 0 through 35. When parsing strings, lowercase is - * also accepted. - * - * All string representations are big-endian (big-place-value digits - * first). (Computer scientists have adopted zero-based counting; why - * can't they tolerate little-endian numbers?) - * - * No string representation has a ``base indicator'' like ``0x''. - * - * An exception is made for zero: it is converted to ``0'' and not the - * empty string. - * - * If you want different conventions, write your own routines to go - * between BigUnsignedInABase and strings. It's not hard. - */ - operator std::string() const; - BigUnsignedInABase(const std::string &s, Base base); - -public: - - // ACCESSORS - Base getBase() const { return base; } - - // Expose these from NumberlikeArray directly. - using NumberlikeArray::getCapacity; - using NumberlikeArray::getLength; - - /* Returns the requested digit, or 0 if it is beyond the length (as if - * the number had 0s infinitely to the left). */ - Digit getDigit(Index i) const { return i >= len ? 0 : blk[i]; } - - // The number is zero if and only if the canonical length is zero. - bool isZero() const { return NumberlikeArray::isEmpty(); } - - /* Equality test. For the purposes of this test, two BigUnsignedInABase - * values must have the same base to be equal. */ - bool operator ==(const BigUnsignedInABase &x) const { - return base == x.base && NumberlikeArray::operator ==(x); - } - bool operator !=(const BigUnsignedInABase &x) const { return !operator ==(x); } - + public: + // The digits of a BigUnsignedInABase are unsigned shorts. + typedef unsigned short Digit; + // That's also the type of a base. + typedef Digit Base; + + protected: + // The base in which this BigUnsignedInABase is expressed + Base base; + + // Creates a BigUnsignedInABase with a capacity; for internal use. + BigUnsignedInABase(int, Index c) : NumberlikeArray(0, c) {} + + // Decreases len to eliminate any leading zero digits. + void zapLeadingZeros() { + while (len > 0 && blk[len - 1] == 0) len--; + } + + public: + // Constructs zero in base 2. + BigUnsignedInABase() : NumberlikeArray(), base(2) {} + + // Copy constructor + BigUnsignedInABase(const BigUnsignedInABase &x) + : NumberlikeArray(x), base(x.base) {} + + // Assignment operator + void operator=(const BigUnsignedInABase &x) { + NumberlikeArray::operator=(x); + base = x.base; + } + + // Constructor that copies from a given array of digits. + BigUnsignedInABase(const Digit *d, Index l, Base base); + + // Destructor. NumberlikeArray does the delete for us. + ~BigUnsignedInABase() {} + + // LINKS TO BIGUNSIGNED + BigUnsignedInABase(const BigUnsigned &x, Base base); + operator BigUnsigned() const; + + /* LINKS TO STRINGS + * + * These use the symbols ``0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'' to + * represent digits of 0 through 35. When parsing strings, lowercase is + * also accepted. + * + * All string representations are big-endian (big-place-value digits + * first). (Computer scientists have adopted zero-based counting; why + * can't they tolerate little-endian numbers?) + * + * No string representation has a ``base indicator'' like ``0x''. + * + * An exception is made for zero: it is converted to ``0'' and not the + * empty string. + * + * If you want different conventions, write your own routines to go + * between BigUnsignedInABase and strings. It's not hard. + */ + operator std::string() const; + BigUnsignedInABase(const std::string &s, Base base); + + public: + // ACCESSORS + Base getBase() const { return base; } + + // Expose these from NumberlikeArray directly. + using NumberlikeArray::getCapacity; + using NumberlikeArray::getLength; + + /* Returns the requested digit, or 0 if it is beyond the length (as if + * the number had 0s infinitely to the left). */ + Digit getDigit(Index i) const { return i >= len ? 0 : blk[i]; } + + // The number is zero if and only if the canonical length is zero. + bool isZero() const { return NumberlikeArray::isEmpty(); } + + /* Equality test. For the purposes of this test, two BigUnsignedInABase + * values must have the same base to be equal. */ + bool operator==(const BigUnsignedInABase &x) const { + return base == x.base && NumberlikeArray::operator==(x); + } + bool operator!=(const BigUnsignedInABase &x) const { return !operator==(x); } }; #endif diff --git a/libs/sign-sdk/include/CIEEngineHelper.h b/libs/sign-sdk/include/CIEEngineHelper.h index 3390d9dc..e5063e4e 100644 --- a/libs/sign-sdk/include/CIEEngineHelper.h +++ b/libs/sign-sdk/include/CIEEngineHelper.h @@ -8,9 +8,9 @@ #ifndef CIEEngineHelper_h #define CIEEngineHelper_h -#include #include #include +#include void alloc_rsa_ex_index(void); void free_rsa_ex_index(void); @@ -18,11 +18,13 @@ RSA_METHOD *RSA_meth_dup(const RSA_METHOD *meth); int RSA_meth_set1_name(RSA_METHOD *meth, const char *name); int RSA_meth_set_flags(RSA_METHOD *meth, int flags); int RSA_meth_set_priv_enc(RSA_METHOD *meth, - int (*priv_enc) (int flen, const unsigned char *from, - unsigned char *to, RSA *rsa, int padding)); + int (*priv_enc)(int flen, const unsigned char *from, + unsigned char *to, RSA *rsa, + int padding)); int RSA_meth_set_priv_dec(RSA_METHOD *meth, - int (*priv_dec) (int flen, const unsigned char *from, - unsigned char *to, RSA *rsa, int padding)); + int (*priv_dec)(int flen, const unsigned char *from, + unsigned char *to, RSA *rsa, + int padding)); int RSA_meth_set_finish(RSA_METHOD *meth, int (*finish)(RSA *rsa)); diff --git a/libs/sign-sdk/include/CIESigner.h b/libs/sign-sdk/include/CIESigner.h index 40d3ff54..9dcf82ef 100644 --- a/libs/sign-sdk/include/CIESigner.h +++ b/libs/sign-sdk/include/CIESigner.h @@ -1,27 +1,27 @@ #pragma once - #include "ASN1/Certificate.h" -#include "BaseSigner.h" #include "ASN1/RSAPrivateKey.h" +#include "BaseSigner.h" #include "CSP/IAS.h" -class CCIESigner : public CBaseSigner -{ -public: - CCIESigner(IAS* pIAS); - virtual ~CCIESigner(void); +class CCIESigner : public CBaseSigner { + public: + CCIESigner(IAS* pIAS); + virtual ~CCIESigner(void); - long Init(const char* szPIN); + long Init(const char* szPIN); - virtual long GetCertificate(const char* alias, CCertificate** ppCertificate, UUCByteArray& id); + virtual long GetCertificate(const char* alias, CCertificate** ppCertificate, + UUCByteArray& id); - virtual long Sign(UUCByteArray& data, UUCByteArray& id, int algo, UUCByteArray& signature); + virtual long Sign(UUCByteArray& data, UUCByteArray& id, int algo, + UUCByteArray& signature); - virtual long Close(); + virtual long Close(); -private: - IAS* m_pIAS; - char m_szPIN[9]; - CCertificate* m_pCertificate; + private: + IAS* m_pIAS; + char m_szPIN[9]; + CCertificate* m_pCertificate; }; diff --git a/libs/sign-sdk/include/CardMod.h b/libs/sign-sdk/include/CardMod.h index 6240e229..ce311930 100644 --- a/libs/sign-sdk/include/CardMod.h +++ b/libs/sign-sdk/include/CardMod.h @@ -35,8 +35,8 @@ #include #include #else -#include #include +#include typedef const wchar_t *LPCWSTR; #endif typedef ULONG *ULONG_PTR; @@ -1033,17 +1033,17 @@ CardSetContainerProperty(__in PCARD_DATA pCardData, __in BYTE bContainerIndex, #define CP_PARENT_WINDOW L"Parent Window" // Write only #define CP_PIN_CONTEXT_STRING L"PIN Context String" // Write only //// Gemalto Custom -//#define CP_CARD_PIN_INFO_EX L"PIN Information Extended"// Read -//only #define CP_CARD_PIN_POLICY L"PIN Policy" // -//Read/Write #define CP_CARD_PIN_CHECK L"PIN Check" // Write only -//#define CP_CARD_SERIALIZE L"Card Serialization" // -//Read/Write #define CP_CARD_DESERIALIZE L"Card Deserialization" // -//Read/Write #define CP_CARD_VERSION_INFO L"Card Version" // Read -//only #define CP_CARD_CACHE L"Card Cache" // -//Read/Write #define CP_CARD_IMPORT_ALLOWED L"Import Allowed" // -//Read/Write #define CP_CARD_IMPORT_CHANGE_ALLOWED L"Import Change Allowed" // -//Read/Write #define CP_CARD_CHANGE_PIN_FIRST L"Change PIN First" // -//Read/Write +// #define CP_CARD_PIN_INFO_EX L"PIN Information Extended"// Read +// only #define CP_CARD_PIN_POLICY L"PIN Policy" // +// Read/Write #define CP_CARD_PIN_CHECK L"PIN Check" // Write only +// #define CP_CARD_SERIALIZE L"Card Serialization" // +// Read/Write #define CP_CARD_DESERIALIZE L"Card Deserialization" // +// Read/Write #define CP_CARD_VERSION_INFO L"Card Version" // Read +// only #define CP_CARD_CACHE L"Card Cache" // +// Read/Write #define CP_CARD_IMPORT_ALLOWED L"Import Allowed" // +// Read/Write #define CP_CARD_IMPORT_CHANGE_ALLOWED L"Import Change Allowed" +// // Read/Write #define CP_CARD_CHANGE_PIN_FIRST L"Change PIN First" // +// Read/Write typedef DWORD(WINAPI *PFN_CARD_GET_PROPERTY)( __in PCARD_DATA pCardData, __in LPCWSTR wszProperty, __out_bcount_part_opt(cbData, *pdwDataLen) PBYTE pbData, __in DWORD cbData, diff --git a/libs/sign-sdk/include/CertStore.h b/libs/sign-sdk/include/CertStore.h index a0d095d8..05887619 100644 --- a/libs/sign-sdk/include/CertStore.h +++ b/libs/sign-sdk/include/CertStore.h @@ -1,21 +1,19 @@ #pragma once +#include #include "ASN1/Certificate.h" -#include using namespace std; -class CCertStore -{ -public: - - static void AddCertificate(CCertificate& caCertificate); +class CCertStore { + public: + static void AddCertificate(CCertificate& caCertificate); - static CCertificate* GetCertificate(CCertificate& certificate); + static CCertificate* GetCertificate(CCertificate& certificate); - static void CleanUp(); + static void CleanUp(); -private: - static map m_certMap; + private: + static map m_certMap; }; diff --git a/libs/sign-sdk/include/CounterSignatureGenerator.h b/libs/sign-sdk/include/CounterSignatureGenerator.h index e9ea1504..dbc98022 100644 --- a/libs/sign-sdk/include/CounterSignatureGenerator.h +++ b/libs/sign-sdk/include/CounterSignatureGenerator.h @@ -7,41 +7,41 @@ * */ -//#include "ASN1/utiltypes.h" -#include "Util/UUCByteArray.h" -#include "SignerInfoGenerator.h" +// #include "ASN1/utiltypes.h" #include "SignedDocument.h" +#include "SignerInfoGenerator.h" +#include "Util/UUCByteArray.h" -class CounterSignatureGenerator -{ -public: - CounterSignatureGenerator(CSignedDocument& signedDoc, int signerInfoIndex); +class CounterSignatureGenerator { + public: + CounterSignatureGenerator(CSignedDocument& signedDoc, int signerInfoIndex); - virtual ~CounterSignatureGenerator(); + virtual ~CounterSignatureGenerator(); - void getContent(UUCByteArray& content); + void getContent(UUCByteArray& content); - void setContentHash(const BYTE* hash, int hashlen); + void setContentHash(const BYTE* hash, int hashlen); - void setSigningCertificate(const BYTE* certificate, int certlen, const BYTE* certHash, int certHashLen); + void setSigningCertificate(const BYTE* certificate, int certlen, + const BYTE* certHash, int certHashLen); - void setSignature(const BYTE* signature, int siglen); + void setSignature(const BYTE* signature, int siglen); - void setTimestampToken(const BYTE* timestampToken, int tstlen); + void setTimestampToken(const BYTE* timestampToken, int tstlen); - void getSignedAttributes(UUCByteArray& signedAttribute); + void getSignedAttributes(UUCByteArray& signedAttribute); - void toByteArray(UUCByteArray& signedDoc); + void toByteArray(UUCByteArray& signedDoc); -private: - CSignedDocument m_signedDoc; - CSignerInfo m_signerInfo; - int m_signerInfoIndex; + private: + CSignedDocument m_signedDoc; + CSignerInfo m_signerInfo; + int m_signerInfoIndex; - UUCByteArray m_signingCertificate; - CASN1SetOf m_signerInfos;// = new DerSet(signerInfos); - CASN1SetOf m_certificates;// = new DerSet(certificates); - CASN1SetOf m_digestAlgos;// = new DerSet(certificates); + UUCByteArray m_signingCertificate; + CASN1SetOf m_signerInfos; // = new DerSet(signerInfos); + CASN1SetOf m_certificates; // = new DerSet(certificates); + CASN1SetOf m_digestAlgos; // = new DerSet(certificates); - CSignerInfoGenerator m_signerInfoGenerator; + CSignerInfoGenerator m_signerInfoGenerator; }; diff --git a/libs/sign-sdk/include/LdapCrl.h b/libs/sign-sdk/include/LdapCrl.h index 090d38fb..77a5b32c 100644 --- a/libs/sign-sdk/include/LdapCrl.h +++ b/libs/sign-sdk/include/LdapCrl.h @@ -6,6 +6,4 @@ // Copyright 2012 __MyCompanyName__. All rights reserved. // - - long getCRLFromLDAP(char* url, UUCByteArray& data); diff --git a/libs/sign-sdk/include/M7MParser.h b/libs/sign-sdk/include/M7MParser.h index a06b2ba9..9efa3815 100644 --- a/libs/sign-sdk/include/M7MParser.h +++ b/libs/sign-sdk/include/M7MParser.h @@ -11,26 +11,24 @@ #define _M7MPARSER_H_ #include + #include "Util/UUCByteArray.h" using namespace std; -class M7MParser -{ -public: - M7MParser(); +class M7MParser { + public: + M7MParser(); - int Load(const char* m7m, int m7mlen); + int Load(const char* m7m, int m7mlen); - int GetP7M(UUCByteArray& p7m); + int GetP7M(UUCByteArray& p7m); - int GetTSR(UUCByteArray& tsr); + int GetTSR(UUCByteArray& tsr); -private: - UUCByteArray m_p7m; - UUCByteArray m_tsr; + private: + UUCByteArray m_p7m; + UUCByteArray m_tsr; }; - - -#endif //_M7MPARSER_H_ +#endif //_M7MPARSER_H_ diff --git a/libs/sign-sdk/include/NumberlikeArray.h b/libs/sign-sdk/include/NumberlikeArray.h index d1676a4d..1af2327c 100644 --- a/libs/sign-sdk/include/NumberlikeArray.h +++ b/libs/sign-sdk/include/NumberlikeArray.h @@ -19,69 +19,64 @@ */ template class NumberlikeArray { -public: - - // Type for the index of a block in the array - typedef unsigned int Index; - // The number of bits in a block, defined below. - static const unsigned int N; - - // The current allocated capacity of this NumberlikeArray (in blocks) - Index cap; - // The actual length of the value stored in this NumberlikeArray (in blocks) - Index len; - // Heap-allocated array of the blocks (can be NULL if len == 0) - Blk *blk; - - // Constructs a ``zero'' NumberlikeArray with the given capacity. - NumberlikeArray(Index c) : cap(c), len(0) { - blk = (cap > 0) ? (new Blk[cap]) : NULL; - } - - /* Constructs a zero NumberlikeArray without allocating a backing array. - * A subclass that doesn't know the needed capacity at initialization - * time can use this constructor and then overwrite blk without first - * deleting it. */ - NumberlikeArray() : cap(0), len(0) { - blk = NULL; - } - - // Destructor. Note that `delete NULL' is a no-op. - ~NumberlikeArray() { - delete [] blk; - } - - /* Ensures that the array has at least the requested capacity; may - * destroy the contents. */ - void allocate(Index c); - - /* Ensures that the array has at least the requested capacity; does not - * destroy the contents. */ - void allocateAndCopy(Index c); - - // Copy constructor - NumberlikeArray(const NumberlikeArray &x); - - // Assignment operator - void operator=(const NumberlikeArray &x); - - // Constructor that copies from a given array of blocks - NumberlikeArray(const Blk *b, Index blen); - - // ACCESSORS - Index getCapacity() const { return cap; } - Index getLength() const { return len; } - Blk getBlock(Index i) const { return blk[i]; } - bool isEmpty() const { return len == 0; } - - /* Equality comparison: checks if both objects have the same length and - * equal (==) array elements to that length. Subclasses may wish to - * override. */ - bool operator ==(const NumberlikeArray &x) const; - - bool operator !=(const NumberlikeArray &x) const { - return !operator ==(x); - } + public: + // Type for the index of a block in the array + typedef unsigned int Index; + // The number of bits in a block, defined below. + static const unsigned int N; + + // The current allocated capacity of this NumberlikeArray (in blocks) + Index cap; + // The actual length of the value stored in this NumberlikeArray (in blocks) + Index len; + // Heap-allocated array of the blocks (can be NULL if len == 0) + Blk *blk; + + // Constructs a ``zero'' NumberlikeArray with the given capacity. + NumberlikeArray(Index c) : cap(c), len(0) { + blk = (cap > 0) ? (new Blk[cap]) : NULL; + } + + /* Constructs a zero NumberlikeArray without allocating a backing array. + * A subclass that doesn't know the needed capacity at initialization + * time can use this constructor and then overwrite blk without first + * deleting it. */ + NumberlikeArray() : cap(0), len(0) { blk = NULL; } + + // Destructor. Note that `delete NULL' is a no-op. + ~NumberlikeArray() { delete[] blk; } + + /* Ensures that the array has at least the requested capacity; may + * destroy the contents. */ + void allocate(Index c); + + /* Ensures that the array has at least the requested capacity; does not + * destroy the contents. */ + void allocateAndCopy(Index c); + + // Copy constructor + NumberlikeArray(const NumberlikeArray &x); + + // Assignment operator + void operator=(const NumberlikeArray &x); + + // Constructor that copies from a given array of blocks + NumberlikeArray(const Blk *b, Index blen); + + // ACCESSORS + Index getCapacity() const { return cap; } + Index getLength() const { return len; } + Blk getBlock(Index i) const { return blk[i]; } + bool isEmpty() const { return len == 0; } + + /* Equality comparison: checks if both objects have the same length and + * equal (==) array elements to that length. Subclasses may wish to + * override. */ + bool operator==(const NumberlikeArray &x) const; + + bool operator!=(const NumberlikeArray &x) const { + return !operator==(x); + } }; /* BEGIN TEMPLATE DEFINITIONS. They are present here so that source files that @@ -92,86 +87,80 @@ const unsigned int NumberlikeArray::N = 8 * sizeof(Blk); template void NumberlikeArray::allocate(Index c) { - // If the requested capacity is more than the current capacity... - if (c > cap) { - // Delete the old number array - delete [] blk; - // Allocate the new array - cap = c; - blk = new Blk[cap]; - } + // If the requested capacity is more than the current capacity... + if (c > cap) { + // Delete the old number array + delete[] blk; + // Allocate the new array + cap = c; + blk = new Blk[cap]; + } } template void NumberlikeArray::allocateAndCopy(Index c) { - // If the requested capacity is more than the current capacity... - if (c > cap) { - Blk *oldBlk = blk; - // Allocate the new number array - cap = c; - blk = new Blk[cap]; - // Copy number blocks - Index i; - for (i = 0; i < len; i++) - blk[i] = oldBlk[i]; - // Delete the old array - delete [] oldBlk; - } + // If the requested capacity is more than the current capacity... + if (c > cap) { + Blk *oldBlk = blk; + // Allocate the new number array + cap = c; + blk = new Blk[cap]; + // Copy number blocks + Index i; + for (i = 0; i < len; i++) blk[i] = oldBlk[i]; + // Delete the old array + delete[] oldBlk; + } } template NumberlikeArray::NumberlikeArray(const NumberlikeArray &x) - : len(x.len) { - // Create array - cap = len; - blk = new Blk[cap]; - // Copy blocks - Index i; - for (i = 0; i < len; i++) - blk[i] = x.blk[i]; + : len(x.len) { + // Create array + cap = len; + blk = new Blk[cap]; + // Copy blocks + Index i; + for (i = 0; i < len; i++) blk[i] = x.blk[i]; } template void NumberlikeArray::operator=(const NumberlikeArray &x) { - /* Calls like a = a have no effect; catch them before the aliasing - * causes a problem */ - if (this == &x) - return; - // Copy length - len = x.len; - // Expand array if necessary - allocate(len); - // Copy number blocks - Index i; - for (i = 0; i < len; i++) - blk[i] = x.blk[i]; + /* Calls like a = a have no effect; catch them before the aliasing + * causes a problem */ + if (this == &x) return; + // Copy length + len = x.len; + // Expand array if necessary + allocate(len); + // Copy number blocks + Index i; + for (i = 0; i < len; i++) blk[i] = x.blk[i]; } template NumberlikeArray::NumberlikeArray(const Blk *b, Index blen) - : cap(blen), len(blen) { - // Create array - blk = new Blk[cap]; - // Copy blocks - Index i; - for (i = 0; i < len; i++) - blk[i] = b[i]; + : cap(blen), len(blen) { + // Create array + blk = new Blk[cap]; + // Copy blocks + Index i; + for (i = 0; i < len; i++) blk[i] = b[i]; } template -bool NumberlikeArray::operator ==(const NumberlikeArray &x) const { - if (len != x.len) - // Definitely unequal. - return false; - else { - // Compare corresponding blocks one by one. - Index i; - for (i = 0; i < len; i++) - if (blk[i] != x.blk[i]) - return false; - // No blocks differed, so the objects are equal. - return true; - } +bool NumberlikeArray::operator==(const NumberlikeArray &x) const { + if (len != x.len) + // Definitely unequal. + return false; + else { + // Compare corresponding blocks one by one. + Index i; + for (i = 0; i < len; i++) + if (blk[i] != x.blk[i]) return false; + // No blocks differed, so the objects are equal. + return true; + } } #endif diff --git a/libs/sign-sdk/include/PdfSignatureGenerator.h b/libs/sign-sdk/include/PdfSignatureGenerator.h index 1707c640..93ab4455 100644 --- a/libs/sign-sdk/include/PdfSignatureGenerator.h +++ b/libs/sign-sdk/include/PdfSignatureGenerator.h @@ -9,11 +9,10 @@ #ifndef _PDFSIGNATUREGENERATOR_H_ #define _PDFSIGNATUREGENERATOR_H_ -#include "podofo/podofo.h" +#include "Util/UUCByteArray.h" #include "podofo/doc/PdfSignOutputDevice.h" #include "podofo/doc/PdfSignatureField.h" - -#include "Util/UUCByteArray.h" +#include "podofo/podofo.h" using namespace PoDoFo; using namespace std; diff --git a/libs/sign-sdk/include/PdfVerifier.h b/libs/sign-sdk/include/PdfVerifier.h index fe25142e..4acf9d21 100644 --- a/libs/sign-sdk/include/PdfVerifier.h +++ b/libs/sign-sdk/include/PdfVerifier.h @@ -12,8 +12,8 @@ #include -#include "Util/UUCByteArray.h" #include "Sign/disigonsdk.h" +#include "Util/UUCByteArray.h" using namespace PoDoFo; using namespace std; diff --git a/libs/sign-sdk/include/SignatureGenerator.h b/libs/sign-sdk/include/SignatureGenerator.h index b8d6a866..87c1af4f 100644 --- a/libs/sign-sdk/include/SignatureGenerator.h +++ b/libs/sign-sdk/include/SignatureGenerator.h @@ -1,71 +1,65 @@ #pragma once -#include "Sign/definitions.h" -#include "PKCS11/cryptoki.h" - #include "ASN1/SignerInfo.h" +#include "BaseSigner.h" +#include "PKCS11/cryptoki.h" +#include "Sign/definitions.h" #include "SignedDocument.h" #include "SignerInfoGenerator.h" #include "TSAClient.h" -#include "BaseSigner.h" #define ALGO_SHA1 1 #define ALGO_SHA256 2 -class CSignatureGeneratorBase -{ -protected: - CSignatureGeneratorBase(CBaseSigner* pCryptoki); - CSignatureGeneratorBase(CSignatureGeneratorBase* pGenerator); - virtual ~CSignatureGeneratorBase(void); - -public: - - virtual void SetData(const UUCByteArray& data); +class CSignatureGeneratorBase { + protected: + CSignatureGeneratorBase(CBaseSigner* pCryptoki); + CSignatureGeneratorBase(CSignatureGeneratorBase* pGenerator); + virtual ~CSignatureGeneratorBase(void); - virtual void SetAlias(char* alias); + public: + virtual void SetData(const UUCByteArray& data); - virtual void SetHashAlgo(int hashAlgo); + virtual void SetAlias(char* alias); - virtual void SetTSA(char* szUrl, char* szUsername, char* szPassword); - virtual void SetTSAUsername(char* szUsername); - virtual void SetTSAPassword(char* szPassword); + virtual void SetHashAlgo(int hashAlgo); - virtual long Generate(UUCByteArray& pkcs7SignedData, BOOL bDetached = FALSE, BOOL bVerifyRevocation = FALSE) = 0; - -protected: - CBaseSigner* m_pSigner; - UUCByteArray m_data; - int m_nHashAlgo; - char m_szAlias[MAX_PATH]; - CTSAClient* m_pTSAClient; + virtual void SetTSA(char* szUrl, char* szUsername, char* szPassword); + virtual void SetTSAUsername(char* szUsername); + virtual void SetTSAPassword(char* szPassword); + virtual long Generate(UUCByteArray& pkcs7SignedData, BOOL bDetached = FALSE, + BOOL bVerifyRevocation = FALSE) = 0; + protected: + CBaseSigner* m_pSigner; + UUCByteArray m_data; + int m_nHashAlgo; + char m_szAlias[MAX_PATH]; + CTSAClient* m_pTSAClient; }; +class CSignatureGenerator : public CSignatureGeneratorBase { + public: + CSignatureGenerator(CBaseSigner* pSigner, bool bRemote = false); + virtual ~CSignatureGenerator(void); -class CSignatureGenerator : public CSignatureGeneratorBase -{ -public: - CSignatureGenerator(CBaseSigner* pSigner, bool bRemote = false); - virtual ~CSignatureGenerator(void); - - void SetPKCS7Data(const UUCByteArray& pkcs7Data); - - void SetCAdES(bool cades); - bool GetCAdES(); + void SetPKCS7Data(const UUCByteArray& pkcs7Data); - long GetCertificate(CCertificate** ppCertificate); - virtual long Generate(UUCByteArray& pkcs7SignedData, BOOL bDetached = FALSE, BOOL bVerifyRevocation = FALSE); + void SetCAdES(bool cades); + bool GetCAdES(); -private: - bool m_bCAdES; - bool m_bRemote; + long GetCertificate(CCertificate** ppCertificate); + virtual long Generate(UUCByteArray& pkcs7SignedData, BOOL bDetached = FALSE, + BOOL bVerifyRevocation = FALSE); + private: + bool m_bCAdES; + bool m_bRemote; - CASN1SetOf m_signerInfos;// = new DerSet(signerInfos); - CASN1SetOf m_certificates;// = new DerSet(certificates); - CASN1SetOf m_digestAlgos;// = new DerSet(certificates); + CASN1SetOf m_signerInfos; // = new DerSet(signerInfos); + CASN1SetOf m_certificates; // = new DerSet(certificates); + CASN1SetOf m_digestAlgos; // = new DerSet(certificates); - CSignerInfoGenerator m_signerInfoGenerator; + CSignerInfoGenerator m_signerInfoGenerator; }; diff --git a/libs/sign-sdk/include/SignedDataGeneratorEx.h b/libs/sign-sdk/include/SignedDataGeneratorEx.h index 16b3a143..f912c2be 100644 --- a/libs/sign-sdk/include/SignedDataGeneratorEx.h +++ b/libs/sign-sdk/include/SignedDataGeneratorEx.h @@ -7,43 +7,45 @@ * */ -//#include "ASN1/utiltypes.h" -#include "Util/UUCByteArray.h" -#include "SignedDocument.h" -#include "ASN1/SignerInfo.h" -#include "ASN1/ASN1Setof.h" -#include "ASN1/TimeStampResponse.h" - +// #include "ASN1/utiltypes.h" #include -class SignedDataGeneratorEx -{ -public: - SignedDataGeneratorEx(CSignedDocument& sd); +#include "ASN1/ASN1Setof.h" +#include "ASN1/SignerInfo.h" +#include "ASN1/TimeStampResponse.h" +#include "SignedDocument.h" +#include "Util/UUCByteArray.h" - virtual ~SignedDataGeneratorEx(); +class SignedDataGeneratorEx { + public: + SignedDataGeneratorEx(CSignedDocument& sd); - bool isDetached(); + virtual ~SignedDataGeneratorEx(); - void setContent(const BYTE* content, int len); + bool isDetached(); - void addSigners(CSignedDocument& sd); + void setContent(const BYTE* content, int len); - void addCounterSignature(CSignerInfo& signerInfoRef, CSignedDocument& countersignature); + void addSigners(CSignedDocument& sd); - void addCounterSignature(CSignerInfo& signerInfoRef, CSignedDocument& counterSignature, CTimeStampResponse& tsr); + void addCounterSignature(CSignerInfo& signerInfoRef, + CSignedDocument& countersignature); - void setTimestamp(CTimeStampResponse& tsr, int signerInfoIndex); + void addCounterSignature(CSignerInfo& signerInfoRef, + CSignedDocument& counterSignature, + CTimeStampResponse& tsr); + void setTimestamp(CTimeStampResponse& tsr, int signerInfoIndex); - void toByteArray(UUCByteArray& pkcs7SignedData); + void toByteArray(UUCByteArray& pkcs7SignedData); -private: - UUCByteArray m_content; + private: + UUCByteArray m_content; - CASN1SetOf m_signerInfos; - CASN1SetOf m_certificates; - CASN1SetOf m_digestAlgos; + CASN1SetOf m_signerInfos; + CASN1SetOf m_certificates; + CASN1SetOf m_digestAlgos; - bool addCounterSignature(CSignerInfo& signerInfo, CSignerInfo& signerInfoRef, CSignerInfo& counterSignature); + bool addCounterSignature(CSignerInfo& signerInfo, CSignerInfo& signerInfoRef, + CSignerInfo& counterSignature); }; diff --git a/libs/sign-sdk/include/SignedDocument.h b/libs/sign-sdk/include/SignedDocument.h index 440bc736..5986f34e 100644 --- a/libs/sign-sdk/include/SignedDocument.h +++ b/libs/sign-sdk/include/SignedDocument.h @@ -7,56 +7,54 @@ * */ -#include "Sign/definitions.h" -#include "Util/UUCByteArray.h" +#include + #include "ASN1/ContentInfo.h" #include "ASN1/SignedData.h" #include "ASN1/SignerInfo.h" -#include +#include "Sign/definitions.h" #include "Sign/disigonsdk.h" +#include "Util/UUCByteArray.h" +class CSignedDocument { + public: + CSignedDocument(const BYTE* content, int len); + CSignedDocument(const CSignedDocument& signedDocument); -class CSignedDocument -{ -public: - CSignedDocument(const BYTE* content, int len); - CSignedDocument(const CSignedDocument& signedDocument); - - virtual ~CSignedDocument(); - - int verify(); - int verify(const char* dateTime); - int verify(int index, REVOCATION_INFO* pRevocationInfo); - int verify(int index, const char* dateTime, REVOCATION_INFO* pRevocationInfo); - - int getSignerCount(); - CASN1SetOf getSignerInfos(); - CASN1SetOf getCertificates(); - CASN1SetOf getDigestAlgos(); + virtual ~CSignedDocument(); - CSignerInfo getSignerInfo(int index); - CCertificate getSignerCertificate(int index); - void getContent(UUCByteArray& content); + int verify(); + int verify(const char* dateTime); + int verify(int index, REVOCATION_INFO* pRevocationInfo); + int verify(int index, const char* dateTime, REVOCATION_INFO* pRevocationInfo); - //static void init(map certMap); + int getSignerCount(); + CASN1SetOf getSignerInfos(); + CASN1SetOf getCertificates(); + CASN1SetOf getDigestAlgos(); - void makeDetached(); + CSignerInfo getSignerInfo(int index); + CCertificate getSignerCertificate(int index); + void getContent(UUCByteArray& content); - void toByteArray(UUCByteArray& signedData); + // static void init(map certMap); - CSignedData getSignedData(); + void makeDetached(); - bool isDetached(); - void setContent(UUCByteArray& content); + void toByteArray(UUCByteArray& signedData); + CSignedData getSignedData(); - // 0 successivo al 30 Giugno 2011, 1 successivo al 30 agosto 2010, 2 precedente al 30 agosto 2010 - static int get452009Range(char* szDateTime); + bool isDetached(); + void setContent(UUCByteArray& content); -private: - CContentInfo* m_pCMSSignedData; - CSignedData* m_pSignedData; - CASN1SetOf m_signerInfos; - CASN1SetOf m_certificates; + // 0 successivo al 30 Giugno 2011, 1 successivo al 30 agosto 2010, 2 + // precedente al 30 agosto 2010 + static int get452009Range(char* szDateTime); + private: + CContentInfo* m_pCMSSignedData; + CSignedData* m_pSignedData; + CASN1SetOf m_signerInfos; + CASN1SetOf m_certificates; }; diff --git a/libs/sign-sdk/include/SignerInfoGenerator.h b/libs/sign-sdk/include/SignerInfoGenerator.h index da68c006..0081ff87 100644 --- a/libs/sign-sdk/include/SignerInfoGenerator.h +++ b/libs/sign-sdk/include/SignerInfoGenerator.h @@ -9,49 +9,48 @@ #pragma once -#include "Sign/definitions.h" -#include "Util/UUCByteArray.h" #include "ASN1/IssuerAndSerialNumber.h" #include "ASN1/SignerInfo.h" +#include "Sign/definitions.h" +#include "Util/UUCByteArray.h" -class CSignerInfoGenerator -{ -public: - CSignerInfoGenerator(); - - virtual ~CSignerInfoGenerator(); - +class CSignerInfoGenerator { + public: + CSignerInfoGenerator(); - void setContent(const BYTE* content, int len); + virtual ~CSignerInfoGenerator(); - void setContentHash(const BYTE* hash, int hashlen); + void setContent(const BYTE* content, int len); - void setSigningCertificate(const BYTE* certificate, int certlen, const BYTE* certHash, int certHashLen); + void setContentHash(const BYTE* hash, int hashlen); - void setSignature(const BYTE* signature, int siglen); + void setSigningCertificate(const BYTE* certificate, int certlen, + const BYTE* certHash, int certHashLen); - void setTimestampToken(const BYTE* timestampToken, int tstlen); + void setSignature(const BYTE* signature, int siglen); - void setTimestampToken(const CTimeStampToken* pTimestampToken); + void setTimestampToken(const BYTE* timestampToken, int tstlen); - void getSignedAttributes(UUCByteArray& signedAttribute, bool countersignature, bool signingTime); + void setTimestampToken(const CTimeStampToken* pTimestampToken); - void toByteArray(UUCByteArray& signerInfo); + void getSignedAttributes(UUCByteArray& signedAttribute, bool countersignature, + bool signingTime); - CSignerInfo getSignerInfo(); + void toByteArray(UUCByteArray& signerInfo); -private: - UUCByteArray m_content; - UUCByteArray m_contentHash; - UUCByteArray m_signingCertificate; - UUCByteArray m_signature; - UUCByteArray m_signedAttributes; - CASN1SetOf m_unsignedAttributes; - UUCByteArray m_certificateHash; - UUCByteArray m_timeStampToken; - CASN1SetOf m_counterSignatures; - void buildUnsignedAttributes(); - CName* m_pIssuer; - CASN1Integer* m_pSerialNumber; + CSignerInfo getSignerInfo(); + private: + UUCByteArray m_content; + UUCByteArray m_contentHash; + UUCByteArray m_signingCertificate; + UUCByteArray m_signature; + UUCByteArray m_signedAttributes; + CASN1SetOf m_unsignedAttributes; + UUCByteArray m_certificateHash; + UUCByteArray m_timeStampToken; + CASN1SetOf m_counterSignatures; + void buildUnsignedAttributes(); + CName* m_pIssuer; + CASN1Integer* m_pSerialNumber; }; diff --git a/libs/sign-sdk/include/TSAClient.h b/libs/sign-sdk/include/TSAClient.h index 13966336..553122a4 100644 --- a/libs/sign-sdk/include/TSAClient.h +++ b/libs/sign-sdk/include/TSAClient.h @@ -2,20 +2,20 @@ #include "ASN1/TimeStampToken.h" -class CTSAClient -{ -public: - CTSAClient(void); - virtual ~CTSAClient(void); +class CTSAClient { + public: + CTSAClient(void); + virtual ~CTSAClient(void); - void SetTSAUrl(const char* szUrl); - void SetCredential(const char* szUsername, const char* szPassword); - void SetUsername(const char* szUsername); - void SetPassword(const char* szPassword); - long GetTimeStampToken(UUCByteArray& digest, const char* szPolicyID, CTimeStampToken** ppTimeStampToken); + void SetTSAUrl(const char* szUrl); + void SetCredential(const char* szUsername, const char* szPassword); + void SetUsername(const char* szUsername); + void SetPassword(const char* szPassword); + long GetTimeStampToken(UUCByteArray& digest, const char* szPolicyID, + CTimeStampToken** ppTimeStampToken); -private: - char m_szTSAUrl[256]; - char m_szTSAUsername[256]; - char m_szTSAPassword[256]; + private: + char m_szTSAUrl[256]; + char m_szTSAUsername[256]; + char m_szTSAPassword[256]; }; diff --git a/libs/sign-sdk/include/UUCLogger.h b/libs/sign-sdk/include/UUCLogger.h index 677ff90c..2cbaa10d 100644 --- a/libs/sign-sdk/include/UUCLogger.h +++ b/libs/sign-sdk/include/UUCLogger.h @@ -1,15 +1,15 @@ #pragma once -#include "Sign/disigonsdk.h" #include -#define MAX_LOG_SIZE 5000 +#include "Sign/disigonsdk.h" -#define LOG_TYPE_ERROR 1 -#define LOG_TYPE_WARNING 2 -#define LOG_TYPE_MESSAGE 3 -#define LOG_TYPE_DEBUG 4 +#define MAX_LOG_SIZE 5000 +#define LOG_TYPE_ERROR 1 +#define LOG_TYPE_WARNING 2 +#define LOG_TYPE_MESSAGE 3 +#define LOG_TYPE_DEBUG 4 #define DECLARE_LOG #define USE_LOG @@ -23,7 +23,6 @@ //- ------------- DA VERIFICARE, CONTIENTE MOLTI BUG - // macro #if 0 #define DECLARE_LOG UUCLogger g_log @@ -37,26 +36,31 @@ #define LOG_DBG(params) g_log.logDebug params #endif +class UUCLogger { + public: + UUCLogger(void); + UUCLogger(const char* szLogFileName); + virtual ~UUCLogger(void); -class UUCLogger -{ -public: - UUCLogger(void); - UUCLogger(const char* szLogFileName); - virtual ~UUCLogger(void); + virtual void logMessage(const unsigned int nID, const char* szModuleName, + const char* szMsg, ...); + virtual void logWarning(const unsigned int nID, const char* szModuleName, + const char* szMsg, ...); + virtual void logError(const unsigned int nID, const char* szModuleName, + const char* szMsg, ...); + virtual void logDebug(const unsigned int nID, const char* szModuleName, + const char* szMsg, ...); - virtual void logMessage(const unsigned int nID, const char *szModuleName, const char* szMsg, ...); - virtual void logWarning(const unsigned int nID, const char *szModuleName, const char* szMsg, ...); - virtual void logError(const unsigned int nID, const char *szModuleName, const char* szMsg, ...); - virtual void logDebug(const unsigned int nID, const char *szModuleName, const char* szMsg, ...); + virtual void setLogFile(const char* szLogFileName); + virtual void setLogLevel(int logLevel); - virtual void setLogFile(const char* szLogFileName); - virtual void setLogLevel(int logLevel); -private: - void log(const unsigned int nType, const char *szMsg, const unsigned int nID, const char *szModuleName); - void log(const unsigned int nType, const char* szMsg, const unsigned int nID, const char *szModuleName, va_list args); + private: + void log(const unsigned int nType, const char* szMsg, const unsigned int nID, + const char* szModuleName); + void log(const unsigned int nType, const char* szMsg, const unsigned int nID, + const char* szModuleName, va_list args); - char m_szLogFileName[MAX_PATH]; - int m_nLogLevel; - char m_szBuffer[MAX_LOG_SIZE]; + char m_szLogFileName[MAX_PATH]; + int m_nLogLevel; + char m_szBuffer[MAX_LOG_SIZE]; }; diff --git a/libs/sign-sdk/include/UUCProperties.h b/libs/sign-sdk/include/UUCProperties.h index 9458fc0b..e4756d63 100644 --- a/libs/sign-sdk/include/UUCProperties.h +++ b/libs/sign-sdk/include/UUCProperties.h @@ -1,44 +1,46 @@ // UUCProperties.h: interface for the UUCProperties class. // -#if !defined(AFX_UUCPROPERTIES_H__715BAE3B_069E_4D31_9FBF_54EA38AAEFEC__INCLUDED_) +#if !defined( \ + AFX_UUCPROPERTIES_H__715BAE3B_069E_4D31_9FBF_54EA38AAEFEC__INCLUDED_) #define AFX_UUCPROPERTIES_H__715BAE3B_069E_4D31_9FBF_54EA38AAEFEC__INCLUDED_ #if _MSC_VER > 1000 #pragma once -#endif // _MSC_VER > 1000 -#include "Util/UUCByteArray.h" -#include "UUCStringTable.h" +#endif // _MSC_VER > 1000 #include -class UUCProperties -{ -public: - UUCProperties(); - UUCProperties(const UUCProperties& defaults); +#include "UUCStringTable.h" +#include "Util/UUCByteArray.h" + +class UUCProperties { + public: + UUCProperties(); + UUCProperties(const UUCProperties& defaults); - virtual ~UUCProperties(); + virtual ~UUCProperties(); - long load(const char* szFilePath); - long load(const UUCByteArray& props); - long save(const char* szFilePath, const char* szHeader) const; - long save(UUCByteArray& props, const char* szHeader) const; - void putProperty(const char* szName, const char* szValue); - //void putProperty(char* szName, char* szValue); - const char* getProperty(const char* szName, const char* szDefaultValue = NULL) const; - void remove(const char* szName); - void removeAll(); + long load(const char* szFilePath); + long load(const UUCByteArray& props); + long save(const char* szFilePath, const char* szHeader) const; + long save(UUCByteArray& props, const char* szHeader) const; + void putProperty(const char* szName, const char* szValue); + // void putProperty(char* szName, char* szValue); + const char* getProperty(const char* szName, + const char* szDefaultValue = NULL) const; + void remove(const char* szName); + void removeAll(); - UUCStringTable* getPropertyTable() const; + UUCStringTable* getPropertyTable() const; - bool contains(const char* szName) const; + bool contains(const char* szName) const; - int size() const; + int size() const; -protected: - UUCStringTable* m_pStringTable; + protected: + UUCStringTable* m_pStringTable; - bool m_bAllocated; + bool m_bAllocated; }; -#endif // !defined(AFX_UUCPROPERTIES_H__715BAE3B_069E_4D31_9FBF_54EA38AAEFEC__INCLUDED_) +#endif // !defined(AFX_UUCPROPERTIES_H__715BAE3B_069E_4D31_9FBF_54EA38AAEFEC__INCLUDED_) diff --git a/libs/sign-sdk/include/UUCStringTable.h b/libs/sign-sdk/include/UUCStringTable.h index 64fea594..69086006 100644 --- a/libs/sign-sdk/include/UUCStringTable.h +++ b/libs/sign-sdk/include/UUCStringTable.h @@ -1,36 +1,36 @@ // UUCStringTable.h: interface for the UUCStringTable class. // -#if !defined(AFX_UUCSTRINGTABLE_H__4392B6C2_89AA_436D_8291_A3D22CFF877B__INCLUDED_) +#if !defined( \ + AFX_UUCSTRINGTABLE_H__4392B6C2_89AA_436D_8291_A3D22CFF877B__INCLUDED_) #define AFX_UUCSTRINGTABLE_H__4392B6C2_89AA_436D_8291_A3D22CFF877B__INCLUDED_ #pragma once #include "UUCHashtable.h" -class UUCStringTable : public UUCHashtable -{ -public: - void remove(); - // contructors - UUCStringTable(); - UUCStringTable(int initialCapacity, float loadFactor); - UUCStringTable(int initialCapacity); +class UUCStringTable : public UUCHashtable { + public: + void remove(); + // contructors + UUCStringTable(); + UUCStringTable(int initialCapacity, float loadFactor); + UUCStringTable(int initialCapacity); - virtual void put(char* const& szKey, char* const& szValue); - virtual bool remove(char* const& szKey); + virtual void put(char* const& szKey, char* const& szValue); + virtual bool remove(char* const& szKey); - static unsigned long getHash(const char* szKey); + static unsigned long getHash(const char* szKey); - // destructor - virtual ~UUCStringTable(); + // destructor + virtual ~UUCStringTable(); -protected: - //virtual unsigned long getHashValue(unsigned long szKey); - virtual unsigned long getHashValue(char* const& szKey) const; - virtual bool equal(char* const& szKey1, char* const& szKey2) const; + protected: + // virtual unsigned long getHashValue(unsigned long szKey); + virtual unsigned long getHashValue(char* const& szKey) const; + virtual bool equal(char* const& szKey1, char* const& szKey2) const; - //virtual UINT getHashValue(const char*& szKey); + // virtual UINT getHashValue(const char*& szKey); }; -#endif // !defined(AFX_UUCSTRINGTABLE_H__4392B6C2_89AA_436D_8291_A3D22CFF877B__INCLUDED_) +#endif // !defined(AFX_UUCSTRINGTABLE_H__4392B6C2_89AA_436D_8291_A3D22CFF877B__INCLUDED_) diff --git a/libs/sign-sdk/include/UUCTextFileReader.h b/libs/sign-sdk/include/UUCTextFileReader.h index 8fb4295b..d9d85f55 100644 --- a/libs/sign-sdk/include/UUCTextFileReader.h +++ b/libs/sign-sdk/include/UUCTextFileReader.h @@ -6,22 +6,22 @@ #if _MSC_VER > 1000 #pragma once -#endif // _MSC_VER > 1000 +#endif // _MSC_VER > 1000 #include + #include "Util/UUCByteArray.h" -class UUCTextFileReader -{ -public: - UUCTextFileReader(const char* szFilePath); - virtual ~UUCTextFileReader(); +class UUCTextFileReader { + public: + UUCTextFileReader(const char* szFilePath); + virtual ~UUCTextFileReader(); - long readLine(char* szLine, unsigned long nLen);// throw (long); - long readLine(UUCByteArray& line); -private: + long readLine(char* szLine, unsigned long nLen); // throw (long); + long readLine(UUCByteArray& line); - FILE* m_pf; + private: + FILE* m_pf; }; -#endif // !defined(AFX_UUCTEXTFILE_H__CD3660A5_B4C5_4CD4_99AC_69AC96D1460F__INCLUDED_) +#endif // !defined(AFX_UUCTEXTFILE_H__CD3660A5_B4C5_4CD4_99AC_69AC96D1460F__INCLUDED_) diff --git a/libs/sign-sdk/include/UUCTextFileWriter.h b/libs/sign-sdk/include/UUCTextFileWriter.h index e5d9cfd3..9c41845f 100644 --- a/libs/sign-sdk/include/UUCTextFileWriter.h +++ b/libs/sign-sdk/include/UUCTextFileWriter.h @@ -1,26 +1,27 @@ // UUCTextFileWriter.h: interface for the UUCTextFileWriter class. // -#if !defined(AFX_UUCTEXTFILEWRITER_H__F64974CA_4F95_4200_B7AE_4A53FB004B75__INCLUDED_) +#if !defined( \ + AFX_UUCTEXTFILEWRITER_H__F64974CA_4F95_4200_B7AE_4A53FB004B75__INCLUDED_) #define AFX_UUCTEXTFILEWRITER_H__F64974CA_4F95_4200_B7AE_4A53FB004B75__INCLUDED_ #if _MSC_VER > 1000 #pragma once -#endif // _MSC_VER > 1000 +#endif // _MSC_VER > 1000 #include + #include "Util/UUCByteArray.h" -class UUCTextFileWriter -{ -public: - UUCTextFileWriter(const char* szFilePath, bool bAppend = false); - virtual ~UUCTextFileWriter(); +class UUCTextFileWriter { + public: + UUCTextFileWriter(const char* szFilePath, bool bAppend = false); + virtual ~UUCTextFileWriter(); - long writeLine(const char* szLine); - long writeLine(const UUCByteArray& byteArray); + long writeLine(const char* szLine); + long writeLine(const UUCByteArray& byteArray); -private: - FILE* m_pf; + private: + FILE* m_pf; }; -#endif // !defined(AFX_UUCTEXTFILEWRITER_H__F64974CA_4F95_4200_B7AE_4A53FB004B75__INCLUDED_) +#endif // !defined(AFX_UUCTEXTFILEWRITER_H__F64974CA_4F95_4200_B7AE_4A53FB004B75__INCLUDED_) diff --git a/libs/sign-sdk/include/XAdESGenerator.h b/libs/sign-sdk/include/XAdESGenerator.h index 4636aa0f..afe4c19c 100644 --- a/libs/sign-sdk/include/XAdESGenerator.h +++ b/libs/sign-sdk/include/XAdESGenerator.h @@ -1,54 +1,58 @@ #pragma once -#include "SignatureGenerator.h" -#include - -#include +#include #include #include -#include +#include + +#include + +#include "SignatureGenerator.h" /* typedef struct { - int nType; - union - { - UUCByteArray content; - char* szURI; - } reference; + int nType; + union + { + UUCByteArray content; + char* szURI; + } reference; } Reference; */ -class CXAdESGenerator : public CSignatureGeneratorBase -{ -public: - CXAdESGenerator(CBaseSigner* pCryptoki); - CXAdESGenerator(CSignatureGeneratorBase* pGenerator); +class CXAdESGenerator : public CSignatureGeneratorBase { + public: + CXAdESGenerator(CBaseSigner* pCryptoki); + CXAdESGenerator(CSignatureGeneratorBase* pGenerator); - virtual ~CXAdESGenerator(void); + virtual ~CXAdESGenerator(void); - void SetXAdES(bool xades); + void SetXAdES(bool xades); - void SetFileName(char* szFileName); + void SetFileName(char* szFileName); - virtual long Generate(UUCByteArray& xadesData, BOOL bDetached, BOOL bVerifyRevocation); -/* - void AddContent(UUCByteArray& content); - - void AddReference(char* szURI); -*/ -private: - bool m_bXAdES; + virtual long Generate(UUCByteArray& xadesData, BOOL bDetached, + BOOL bVerifyRevocation); + /* + void AddContent(UUCByteArray& content); - xmlDocPtr CreateSignedInfo(xmlDocPtr pDocument, string& strQualifyingPropertiesB64Hash, bool bDetached, char* szFileName); - xmlDocPtr CreateQualifyingProperties(xmlDocPtr pDocument, CCertificate* pCertificate); + void AddReference(char* szURI); + */ + private: + bool m_bXAdES; - void CanonicalizeAndHashBase64(xmlDocPtr pDoc, string& strDocHashB64, string& strCanonical); + xmlDocPtr CreateSignedInfo(xmlDocPtr pDocument, + string& strQualifyingPropertiesB64Hash, + bool bDetached, char* szFileName); + xmlDocPtr CreateQualifyingProperties(xmlDocPtr pDocument, + CCertificate* pCertificate); -// vector m_refecenceVect; + void CanonicalizeAndHashBase64(xmlDocPtr pDoc, string& strDocHashB64, + string& strCanonical); - char m_szID[100]; - char m_szFileName[MAX_PATH]; + // vector m_refecenceVect; + char m_szID[100]; + char m_szFileName[MAX_PATH]; }; diff --git a/libs/sign-sdk/include/XAdESVerifier.h b/libs/sign-sdk/include/XAdESVerifier.h index 5b2e086e..a2d08183 100644 --- a/libs/sign-sdk/include/XAdESVerifier.h +++ b/libs/sign-sdk/include/XAdESVerifier.h @@ -1,74 +1,75 @@ #pragma once -#include "Util/UUCByteArray.h" -#include "ASN1/Certificate.h" - -#include #include #include +#include #include #include +#include "ASN1/Certificate.h" +#include "Util/UUCByteArray.h" typedef struct RefDataFile_st { - char* szId; // data file id - char* szFileName; // signed doc file name - char* szMimeType; // date file mime type - char* szContentType; // DETATCHED, EMBEDDED or EMBEDDED_BASE64 - long nSize; // file size (unencoded) - char* szDigestType; // digest type - UUCByteArray mbufDigest; // real DataFile digest value - UUCByteArray mbufDetachedDigest; // detached file digest - int nAttributes; // number of other attributes - char* szCharset; // datafile initial codepage - char** pAttNames; // other attribute names - char** pAttValues; // other attribute values + char* szId; // data file id + char* szFileName; // signed doc file name + char* szMimeType; // date file mime type + char* szContentType; // DETATCHED, EMBEDDED or EMBEDDED_BASE64 + long nSize; // file size (unencoded) + char* szDigestType; // digest type + UUCByteArray mbufDigest; // real DataFile digest value + UUCByteArray mbufDetachedDigest; // detached file digest + int nAttributes; // number of other attributes + char* szCharset; // datafile initial codepage + char** pAttNames; // other attribute names + char** pAttValues; // other attribute values UUCByteArray mbufContent; } RefDataFile; // signature info for each user signature typedef struct SignatureInfo_st { - char* szId; // signature id - int nDocs; // number of separate documents signed -// DocInfo** pDocs; // info for each signed document - char* szTimeStamp; // signature timestamp in format "YYYY-MM-DDTHH:MM:SSZ" + char* szId; // signature id + int nDocs; // number of separate documents signed + // DocInfo** pDocs; // info for each signed document + char* szTimeStamp; // signature timestamp in format "YYYY-MM-DDTHH:MM:SSZ" UUCByteArray sigPropDigest; UUCByteArray sigPropRealDigest; UUCByteArray sigInfoRealDigest; UUCByteArray sigValue; // RSA+SHA1 signature value - CCertificate* pX509Cert; // X509Cert certificate (used internally for data during loading) + CCertificate* pX509Cert; // X509Cert certificate (used internally for data + // during loading) UUCByteArray mbufOrigContent; int nDigestAlgo; bool bCAdES; } SignatureInfo; typedef struct XAdESDoc_st { - char* szFormat; // data format name - char* szFormatVer; // data format version - int nDataFiles; - RefDataFile** pRefDataFiles; - int nSignatures; - SignatureInfo** ppSignatures; + char* szFormat; // data format name + char* szFormatVer; // data format version + int nDataFiles; + RefDataFile** pRefDataFiles; + int nSignatures; + SignatureInfo** ppSignatures; } XAdESDoc; +class CXAdESVerifier { + public: + CXAdESVerifier(void); + virtual ~CXAdESVerifier(void); -class CXAdESVerifier -{ -public: - CXAdESVerifier(void); - virtual ~CXAdESVerifier(void); - - long Load(BYTE* buf, int len); - long Load(char *szFilename); + long Load(BYTE* buf, int len); + long Load(char* szFilename); - CCertificate* GetCertificate(int index); - CASN1ObjectIdentifier GetDigestAlgorithm(int index); + CCertificate* GetCertificate(int index); + CASN1ObjectIdentifier GetDigestAlgorithm(int index); - int verifySignature(int index, const char* szDateTime, REVOCATION_INFO* pRevocationInfo); + int verifySignature(int index, const char* szDateTime, + REVOCATION_INFO* pRevocationInfo); -private: - static XAdESDoc* parseXAdESFile(char *szFilename); - static void parseSignatureNode(xmlXPathContextPtr xpathCtx, xmlNodeSetPtr signatureNodes, XAdESDoc* pXAdESDoc); - static bool m_bLibXmlInitialized; - XAdESDoc* m_pXAdESDoc; + private: + static XAdESDoc* parseXAdESFile(char* szFilename); + static void parseSignatureNode(xmlXPathContextPtr xpathCtx, + xmlNodeSetPtr signatureNodes, + XAdESDoc* pXAdESDoc); + static bool m_bLibXmlInitialized; + XAdESDoc* m_pXAdESDoc; }; diff --git a/libs/sign-sdk/include/base64-std.h b/libs/sign-sdk/include/base64-std.h index d0e2705e..21b5c4f6 100644 --- a/libs/sign-sdk/include/base64-std.h +++ b/libs/sign-sdk/include/base64-std.h @@ -3,7 +3,8 @@ #include -const char kBase64Alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +const char kBase64Alphabet[] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz" "0123456789+/"; @@ -52,7 +53,8 @@ class Base64 { return (enc_len == out->size()); } - static bool Encode(const char *input, size_t input_length, char *out, size_t out_length) { + static bool Encode(const char *input, size_t input_length, char *out, + size_t out_length) { int i = 0, j = 0; char *out_begin = out; unsigned char a3[3]; @@ -112,11 +114,11 @@ class Base64 { a4[i++] = *(input++); if (i == 4) { - for (i = 0; i <4; i++) { + for (i = 0; i < 4; i++) { a4[i] = b64_lookup(a4[i]); } - a4_to_a3(a3,a4); + a4_to_a3(a3, a4); for (i = 0; i < 3; i++) { (*out)[dec_len++] = a3[i]; @@ -135,7 +137,7 @@ class Base64 { a4[j] = b64_lookup(a4[j]); } - a4_to_a3(a3,a4); + a4_to_a3(a3, a4); for (j = 0; j < i - 1; j++) { (*out)[dec_len++] = a3[j]; @@ -145,7 +147,8 @@ class Base64 { return (dec_len == out->size()); } - static bool Decode(const char *input, size_t input_length, char *out, size_t out_length) { + static bool Decode(const char *input, size_t input_length, char *out, + size_t out_length) { int i = 0, j = 0; char *out_begin = out; unsigned char a3[3]; @@ -162,11 +165,11 @@ class Base64 { a4[i++] = *(input++); if (i == 4) { - for (i = 0; i <4; i++) { + for (i = 0; i < 4; i++) { a4[i] = b64_lookup(a4[i]); } - a4_to_a3(a3,a4); + a4_to_a3(a3, a4); for (i = 0; i < 3; i++) { *out++ = a3[i]; @@ -185,7 +188,7 @@ class Base64 { a4[j] = b64_lookup(a4[j]); } - a4_to_a3(a3,a4); + a4_to_a3(a3, a4); for (j = 0; j < i - 1; j++) { *out++ = a3[j]; @@ -208,7 +211,8 @@ class Base64 { int numEq = 0; int n = in.size(); - for (std::string::const_reverse_iterator it = in.rbegin(); *it == '='; ++it) { + for (std::string::const_reverse_iterator it = in.rbegin(); *it == '='; + ++it) { ++numEq; } @@ -228,29 +232,27 @@ class Base64 { } private: - static inline void a3_to_a4(unsigned char * a4, unsigned char * a3) { + static inline void a3_to_a4(unsigned char *a4, unsigned char *a3) { a4[0] = (a3[0] & 0xfc) >> 2; a4[1] = ((a3[0] & 0x03) << 4) + ((a3[1] & 0xf0) >> 4); a4[2] = ((a3[1] & 0x0f) << 2) + ((a3[2] & 0xc0) >> 6); a4[3] = (a3[2] & 0x3f); } - static inline void a4_to_a3(unsigned char * a3, unsigned char * a4) { + static inline void a4_to_a3(unsigned char *a3, unsigned char *a4) { a3[0] = (a4[0] << 2) + ((a4[1] & 0x30) >> 4); a3[1] = ((a4[1] & 0xf) << 4) + ((a4[2] & 0x3c) >> 2); a3[2] = ((a4[2] & 0x3) << 6) + a4[3]; } static inline unsigned char b64_lookup(unsigned char c) { - if(c >='A' && c <='Z') return c - 'A'; - if(c >='a' && c <='z') return c - 71; - if(c >='0' && c <='9') return c + 4; - if(c == '+') return 62; - if(c == '/') return 63; + if (c >= 'A' && c <= 'Z') return c - 'A'; + if (c >= 'a' && c <= 'z') return c - 71; + if (c >= '0' && c <= '9') return c + 4; + if (c == '+') return 62; + if (c == '/') return 63; return 255; } }; - - -#endif // BASE64_H +#endif // BASE64_H diff --git a/libs/sign-sdk/include/keys.h b/libs/sign-sdk/include/keys.h index c303e4cf..fdb8b4eb 100644 --- a/libs/sign-sdk/include/keys.h +++ b/libs/sign-sdk/include/keys.h @@ -11,5 +11,4 @@ #define ENCRYPTION_KEY "this is a fake key" - #endif /* keys_h */ diff --git a/libs/sign-sdk/src/ASN1/ASN1BitString.cpp b/libs/sign-sdk/src/ASN1/ASN1BitString.cpp index dd744e41..a904bfd2 100644 --- a/libs/sign-sdk/src/ASN1/ASN1BitString.cpp +++ b/libs/sign-sdk/src/ASN1/ASN1BitString.cpp @@ -3,8 +3,6 @@ #include "ASN1BitString.h" -#include "ASN1Exception.h" - // Construction/Destruction const BYTE CASN1BitString::TAG = 0x03; @@ -12,7 +10,6 @@ const BYTE CASN1BitString::TAG = 0x03; CASN1BitString::~CASN1BitString() {} CASN1BitString::CASN1BitString(UUCBufferedReader& reader) - : CASN1Object(reader) { -} + : CASN1Object(reader) {} CASN1BitString::CASN1BitString(const CASN1Object& obj) : CASN1Object(obj) {} diff --git a/libs/sign-sdk/src/ASN1/ASN1BitString.h b/libs/sign-sdk/src/ASN1/ASN1BitString.h index c2fd6f6a..9f404922 100644 --- a/libs/sign-sdk/src/ASN1/ASN1BitString.h +++ b/libs/sign-sdk/src/ASN1/ASN1BitString.h @@ -5,7 +5,6 @@ AFX_ASN1BITSTRING_H__C1B0BC02_DBEF_4919_902C_5C6C91AE9D25__INCLUDED_) #define AFX_ASN1BITSTRING_H__C1B0BC02_DBEF_4919_902C_5C6C91AE9D25__INCLUDED_ -#include "ASN1/ASN1Integer.h" #include "ASN1Object.h" class CASN1BitString : public CASN1Object { diff --git a/libs/sign-sdk/src/ASN1/ASN1Boolean.h b/libs/sign-sdk/src/ASN1/ASN1Boolean.h index df1b35d9..049e5581 100644 --- a/libs/sign-sdk/src/ASN1/ASN1Boolean.h +++ b/libs/sign-sdk/src/ASN1/ASN1Boolean.h @@ -1,8 +1,8 @@ #ifndef _ASN1BOOLEAN_H #define _ASN1BOOLEAN_H -#include "ASN1/ASN1Integer.h" - +#include "ASN1/ASN1Object.h" +#include "ASN1/UUCBufferedReader.h" class CASN1Boolean : public CASN1Object { private: static const BYTE TAG; diff --git a/libs/sign-sdk/src/ASN1/ASN1Exception.h b/libs/sign-sdk/src/ASN1/ASN1Exception.h index c561f90d..e716b1fb 100644 --- a/libs/sign-sdk/src/ASN1/ASN1Exception.h +++ b/libs/sign-sdk/src/ASN1/ASN1Exception.h @@ -21,6 +21,8 @@ #define _ASN1EXCEPTION_H #include + +#include "Sign/definitions.h" class CASN1Exception { public: CASN1Exception(const char* lpszMsg) : m_lpszMsg(lpszMsg) {} diff --git a/libs/sign-sdk/src/ASN1/ASN1Integer.cpp b/libs/sign-sdk/src/ASN1/ASN1Integer.cpp index a364c984..6aa54aeb 100644 --- a/libs/sign-sdk/src/ASN1/ASN1Integer.cpp +++ b/libs/sign-sdk/src/ASN1/ASN1Integer.cpp @@ -1,9 +1,5 @@ #include "ASN1/ASN1Integer.h" -#include - -#include "ASN1Exception.h" - const BYTE CASN1Integer::TAG = 0x02; CASN1Integer::CASN1Integer(const CASN1Object& obj) : CASN1Object(obj) { diff --git a/libs/sign-sdk/src/ASN1/ASN1Object.h b/libs/sign-sdk/src/ASN1/ASN1Object.h index a862dd39..434aa3ae 100644 --- a/libs/sign-sdk/src/ASN1/ASN1Object.h +++ b/libs/sign-sdk/src/ASN1/ASN1Object.h @@ -1,11 +1,9 @@ #ifndef _ASN1OBJECT_ #define _ASN1OBJECT_ -#include - +#include "Sign/definitions.h" #include "UUCBufferedReader.h" #include "Util/UUCByteArray.h" -#include "Sign/definitions.h" class CASN1Object { public: diff --git a/libs/sign-sdk/src/ASN1/ASN1Octetstring.cpp b/libs/sign-sdk/src/ASN1/ASN1Octetstring.cpp index 862d2b0e..12b07733 100644 --- a/libs/sign-sdk/src/ASN1/ASN1Octetstring.cpp +++ b/libs/sign-sdk/src/ASN1/ASN1Octetstring.cpp @@ -1,8 +1,5 @@ #include "ASN1Octetstring.h" -#include "ASN1/ASN1Integer.h" -//#include "ASN1Exception.h" - const BYTE CASN1OctetString::TAG = 0x04; // distruttori diff --git a/libs/sign-sdk/src/ASN1/ASN1OptionalField.cpp b/libs/sign-sdk/src/ASN1/ASN1OptionalField.cpp index a8225bbb..1033a9d2 100644 --- a/libs/sign-sdk/src/ASN1/ASN1OptionalField.cpp +++ b/libs/sign-sdk/src/ASN1/ASN1OptionalField.cpp @@ -1,7 +1,5 @@ #include "ASN1OptionalField.h" -#include "ASN1Exception.h" - const BYTE CASN1OptionalField::TAG = 0xA0; CASN1OptionalField::CASN1OptionalField(const CASN1Object& asn1Obj, diff --git a/libs/sign-sdk/src/ASN1/ASN1OptionalField.h b/libs/sign-sdk/src/ASN1/ASN1OptionalField.h index 05cb5ddd..f71b531d 100644 --- a/libs/sign-sdk/src/ASN1/ASN1OptionalField.h +++ b/libs/sign-sdk/src/ASN1/ASN1OptionalField.h @@ -4,20 +4,20 @@ #include "ASN1Object.h" class CASN1OptionalField : public CASN1Object { - public: - CASN1OptionalField(UUCBufferedReader& reader); + public: + CASN1OptionalField(UUCBufferedReader& reader); - CASN1OptionalField(const CASN1Object& pAsn1Obj, const BYTE& btClass); + CASN1OptionalField(const CASN1Object& pAsn1Obj, const BYTE& btClass); - CASN1OptionalField(const CASN1Object& opt); + CASN1OptionalField(const CASN1Object& opt); - ~CASN1OptionalField(); + ~CASN1OptionalField(); - BYTE getTag() const; + BYTE getTag() const; - private: - static const BYTE TAG; - BYTE m_btClass; + private: + static const BYTE TAG; + BYTE m_btClass; }; #endif //_ASN1OPTIONALFIELD_H diff --git a/libs/sign-sdk/src/ASN1/AlgorithmIdentifier.h b/libs/sign-sdk/src/ASN1/AlgorithmIdentifier.h index 39268c2e..5535abcf 100644 --- a/libs/sign-sdk/src/ASN1/AlgorithmIdentifier.h +++ b/libs/sign-sdk/src/ASN1/AlgorithmIdentifier.h @@ -1,8 +1,8 @@ #ifndef _ALGORITHMIDENTIFIER_H #define _ALGORITHMIDENTIFIER_H -#include "ASN1ObjectIdentifier.h" #include "ASN1/ASN1Sequence.h" +#include "ASN1ObjectIdentifier.h" class CAlgorithmIdentifier : public CASN1Sequence { public: diff --git a/libs/sign-sdk/src/ASN1/Certificate.cpp b/libs/sign-sdk/src/ASN1/Certificate.cpp index f465180d..29275e67 100644 --- a/libs/sign-sdk/src/ASN1/Certificate.cpp +++ b/libs/sign-sdk/src/ASN1/Certificate.cpp @@ -8,19 +8,17 @@ #include #include +#include "ASN1/Crl.h" #include "ASN1Exception.h" #include "ASN1Octetstring.h" -#include "ASN1OptionalField.h" #include "Base64.h" #include "CertStore.h" -#include "ASN1/Crl.h" #include "DigestInfo.h" #include "LdapCrl.h" #include "OCSPRequest.h" #include "RSA/rsaeuro.h" #include "RSA/sha1.h" #include "RSA/sha2.h" -#include "RSAPublicKey.h" #include "UUCLogger.h" #define PROXY_AUTHENTICATION_REQUIRED 407 diff --git a/libs/sign-sdk/src/ASN1/Certificate.h b/libs/sign-sdk/src/ASN1/Certificate.h index f9c095b7..c1db785b 100644 --- a/libs/sign-sdk/src/ASN1/Certificate.h +++ b/libs/sign-sdk/src/ASN1/Certificate.h @@ -4,13 +4,11 @@ #if !defined(AFX_CERTIFICATE_H__2DF2B808_9398_479F_9FD2_9A229517EF9D__INCLUDED_) #define AFX_CERTIFICATE_H__2DF2B808_9398_479F_9FD2_9A229517EF9D__INCLUDED_ -#include "ASN1Octetstring.h" #include "ASN1/ASN1Sequence.h" +#include "ASN1/ASN1UTCTime.h" #include "ASN1/AlgorithmIdentifier.h" +#include "ASN1Octetstring.h" #include "CertificateInfo.h" - -#include "ASN1/ASN1UTCTime.h" -#include "ASN1/Crl.h" #include "Sign/disigonsdk.h" class CCertificate : public CASN1Sequence { diff --git a/libs/sign-sdk/src/ASN1/ContentType.cpp b/libs/sign-sdk/src/ASN1/ContentType.cpp index 8ec247f7..bc1e89a9 100644 --- a/libs/sign-sdk/src/ASN1/ContentType.cpp +++ b/libs/sign-sdk/src/ASN1/ContentType.cpp @@ -1,7 +1,5 @@ #include "ContentType.h" -#include "ASN1Exception.h" - const char* CContentType::OID_TYPE_DATA = "1.2.840.113549.1.7.1"; const char* CContentType::OID_TYPE_SIGNED = "1.2.840.113549.1.7.2"; const char* CContentType::OID_TYPE_ENVELOPED = "1.2.840.113549.1.7.3"; diff --git a/libs/sign-sdk/src/ASN1/Crl.cpp b/libs/sign-sdk/src/ASN1/Crl.cpp index afe13483..5b0dc87f 100644 --- a/libs/sign-sdk/src/ASN1/Crl.cpp +++ b/libs/sign-sdk/src/ASN1/Crl.cpp @@ -9,8 +9,8 @@ #include "ASN1/Crl.h" -#include "ASN1Octetstring.h" #include "ASN1/ASN1UTCTime.h" +#include "ASN1Octetstring.h" #include "UUCLogger.h" USE_LOG; diff --git a/libs/sign-sdk/src/ASN1/DigestInfo.h b/libs/sign-sdk/src/ASN1/DigestInfo.h index bdb1194b..7526884f 100644 --- a/libs/sign-sdk/src/ASN1/DigestInfo.h +++ b/libs/sign-sdk/src/ASN1/DigestInfo.h @@ -1,9 +1,9 @@ #ifndef _DIGESTINFO_H #define _DIGESTINFO_H -#include "ASN1Octetstring.h" #include "ASN1/ASN1Sequence.h" #include "ASN1/AlgorithmIdentifier.h" +#include "ASN1Octetstring.h" class CDigestInfo : public CASN1Sequence { // Defined as diff --git a/libs/sign-sdk/src/ASN1/IssuerAndSerialNumber.cpp b/libs/sign-sdk/src/ASN1/IssuerAndSerialNumber.cpp index 07d2963d..ffe84865 100644 --- a/libs/sign-sdk/src/ASN1/IssuerAndSerialNumber.cpp +++ b/libs/sign-sdk/src/ASN1/IssuerAndSerialNumber.cpp @@ -1,40 +1,36 @@ -// IssuerAndSerialNumber.cpp: implementation of the CIssuerAndSerialNumber class. +// IssuerAndSerialNumber.cpp: implementation of the CIssuerAndSerialNumber +// class. // - #include "ASN1/IssuerAndSerialNumber.h" + #include "ASN1OptionalField.h" // Construction/Destruction CIssuerAndSerialNumber::CIssuerAndSerialNumber(UUCBufferedReader& reader) - : CASN1Sequence(reader) { -} - -CIssuerAndSerialNumber::CIssuerAndSerialNumber(const CASN1Object& issuerAndSerNum) - : CASN1Sequence(issuerAndSerNum) { - -} - -CIssuerAndSerialNumber::CIssuerAndSerialNumber(const CName& issuer, const CASN1Integer& serNum, bool contextSpecific) { - if(contextSpecific) { - CASN1Sequence issuerField; - issuerField.addElement(issuer); - - CASN1Sequence innerSequence; - innerSequence.addElement(CASN1OptionalField(issuerField, 0x04)); - - addElement(innerSequence); - addElement(serNum); - } else { - addElement(issuer); - addElement(serNum); - } - - - + : CASN1Sequence(reader) {} + +CIssuerAndSerialNumber::CIssuerAndSerialNumber( + const CASN1Object& issuerAndSerNum) + : CASN1Sequence(issuerAndSerNum) {} + +CIssuerAndSerialNumber::CIssuerAndSerialNumber(const CName& issuer, + const CASN1Integer& serNum, + bool contextSpecific) { + if (contextSpecific) { + CASN1Sequence issuerField; + issuerField.addElement(issuer); + + CASN1Sequence innerSequence; + innerSequence.addElement(CASN1OptionalField(issuerField, 0x04)); + + addElement(innerSequence); + addElement(serNum); + } else { + addElement(issuer); + addElement(serNum); + } } -CIssuerAndSerialNumber::~CIssuerAndSerialNumber() { - -} +CIssuerAndSerialNumber::~CIssuerAndSerialNumber() {} diff --git a/libs/sign-sdk/src/ASN1/IssuerAndSerialNumber.h b/libs/sign-sdk/src/ASN1/IssuerAndSerialNumber.h index b67acb5d..591f1ad6 100644 --- a/libs/sign-sdk/src/ASN1/IssuerAndSerialNumber.h +++ b/libs/sign-sdk/src/ASN1/IssuerAndSerialNumber.h @@ -14,15 +14,15 @@ #include "Name.h" class CIssuerAndSerialNumber : public CASN1Sequence { - public: - CIssuerAndSerialNumber(UUCBufferedReader& reader); + public: + CIssuerAndSerialNumber(UUCBufferedReader& reader); - CIssuerAndSerialNumber(const CASN1Object& issuerAndSerNum); + CIssuerAndSerialNumber(const CASN1Object& issuerAndSerNum); - CIssuerAndSerialNumber(const CName& issuer, const CASN1Integer& serNum, - bool contextSpecific); + CIssuerAndSerialNumber(const CName& issuer, const CASN1Integer& serNum, + bool contextSpecific); - virtual ~CIssuerAndSerialNumber(); + virtual ~CIssuerAndSerialNumber(); }; #endif // !defined(AFX_ISSUERANDSERIALNUMBER_H__0AE18C2B_4100_4D86_98D3_167D05277E42__INCLUDED_) diff --git a/libs/sign-sdk/src/ASN1/Name.cpp b/libs/sign-sdk/src/ASN1/Name.cpp index 758f7735..c7006e6e 100644 --- a/libs/sign-sdk/src/ASN1/Name.cpp +++ b/libs/sign-sdk/src/ASN1/Name.cpp @@ -1,14 +1,13 @@ // Name.cpp: implementation of the CName class. // - #include "Name.h" -//#include "ASN1Exception.h" -//#include "ASN1PrintableString.h" -#include "RelativeDistinguishedName.h" +// #include "ASN1Exception.h" +// #include "ASN1PrintableString.h" +#include "ASN1/ASN1Setof.h" #include "ASN1ObjectIdentifier.h" -#include "UUCProperties.h" #include "UUCLogger.h" +#include "UUCProperties.h" USE_LOG; @@ -16,176 +15,174 @@ extern UUCProperties g_mapOIDProps; // Construction/Destruction -CName::CName(UUCBufferedReader& reader) - : CASN1Sequence(reader) { -} +CName::CName(UUCBufferedReader& reader) : CASN1Sequence(reader) {} -CName::CName(const CASN1Object& name) - : CASN1Sequence(name) { -} +CName::CName(const CASN1Object& name) : CASN1Sequence(name) {} string CName::getField(const char* fieldOID) { - string strname = ""; + string strname = ""; - for(int i = 0; i < size(); i++) { - CASN1SetOf element = elementAt(i); - CASN1Sequence value = element.elementAt(0); + for (int i = 0; i < size(); i++) { + CASN1SetOf element = elementAt(i); + CASN1Sequence value = element.elementAt(0); - if(value.elementAt(0) == CASN1ObjectIdentifier(fieldOID)) { - CASN1Object name(value.elementAt(1)); - strname.append((char*)name.getValue()->getContent(), name.getLength()); - break; - } + if (value.elementAt(0) == CASN1ObjectIdentifier(fieldOID)) { + CASN1Object name(value.elementAt(1)); + strname.append((char*)name.getValue()->getContent(), name.getLength()); + break; } + } - return strname; - + return strname; } void CName::getNameAsString(UUCByteArray& sname) { - LOG_DBG((0, "--> CName::getNameAsString", "")); - - int sz = size(); - - //LOG_DBG((0, "CName::getNameAsString", "size: %d", sz)); + LOG_DBG((0, "--> CName::getNameAsString", "")); - for(int i = 0; i < sz; i++) - //for (int i = sz-1; i >= 0; i--) - { - //LOG_DBG((0, "CName::getNameAsString", "i: %d", i)); + int sz = size(); - CASN1SetOf element = elementAt(i); + // LOG_DBG((0, "CName::getNameAsString", "size: %d", sz)); - CASN1Sequence value = element.elementAt(0); - UUCByteArray oid; - UUCByteArray OID; - CASN1ObjectIdentifier fieldOID = value.elementAt(0); - fieldOID.ToOidString(OID); + for (int i = 0; i < sz; i++) + // for (int i = sz-1; i >= 0; i--) + { + // LOG_DBG((0, "CName::getNameAsString", "i: %d", i)); - //LOG_DBG((0, "CName::getNameAsString", "OID: %s", OID.getContent())); + CASN1SetOf element = elementAt(i); - const char* szOID = g_mapOIDProps.getProperty((char*)OID.getContent(), (char*)OID.getContent()); + CASN1Sequence value = element.elementAt(0); + UUCByteArray oid; + UUCByteArray OID; + CASN1ObjectIdentifier fieldOID = value.elementAt(0); + fieldOID.ToOidString(OID); - //LOG_DBG((0, "CName::getNameAsString", "szOID: %s", szOID)); + // LOG_DBG((0, "CName::getNameAsString", "OID: %s", OID.getContent())); - if(value.size() > 1) { - CASN1Object name(value.elementAt(1)); - if(sname.getLength() != 0) - sname.append(','); + const char* szOID = g_mapOIDProps.getProperty((char*)OID.getContent(), + (char*)OID.getContent()); - sname.append((BYTE*)szOID, strlen(szOID)); - sname.append('='); + // LOG_DBG((0, "CName::getNameAsString", "szOID: %s", szOID)); - sname.append(name.getValue()->getContent(), name.getLength()); + if (value.size() > 1) { + CASN1Object name(value.elementAt(1)); + if (sname.getLength() != 0) sname.append(','); - //LOG_DBG((0, "CName::getNameAsString", "strname: %s", sname.getContent())); - } + sname.append((BYTE*)szOID, strlen(szOID)); + sname.append('='); -// LOG_DBG((0, "CName::getNameAsString", "next")); + sname.append(name.getValue()->getContent(), name.getLength()); + // LOG_DBG((0, "CName::getNameAsString", "strname: %s", + // sname.getContent())); } - sname.append((BYTE)'\0'); - LOG_DBG((0, "<-- CName::getNameAsString", "%s", sname.getContent())); -} - - + // LOG_DBG((0, "CName::getNameAsString", "next")); + } + sname.append((BYTE)'\0'); + LOG_DBG((0, "<-- CName::getNameAsString", "%s", sname.getContent())); +} /* CName::CName(LPCTSTR lpszName) { - // parse the string - char szName1[256];// = new char[strlen(lpszName)]; - char* szName = szName1; - - strcpy(szName, lpszName); - - szName = strtok(szName, "="); - - LPSTR lpszOID; - LPSTR lpszVal; - - CAttributeValueAssertion* pAttVal; - - while(szName != NULL) - { - // OID - lpszOID = szName; - lpszVal = strtok(NULL, ";"); - - szName = lpszVal + strlen(lpszVal) + 1; - // Check OID - if(stricmp(lpszOID, "CN") == 0) - { - pAttVal = - new CAttributeValueAssertion(CAttributeType(CAttributeType::STR_COMMON_NAME), - CASN1PrintableString(lpszVal)); - } - else if(stricmp(lpszOID, "O") == 0) - { - pAttVal = - new CAttributeValueAssertion(CAttributeType(CAttributeType::STR_ORGANIZATION_NAME), - CASN1PrintableString(lpszVal)); - } - else if(stricmp(lpszOID, "OU") == 0) - { - pAttVal = - new CAttributeValueAssertion(CAttributeType(CAttributeType::STR_ORGANIZATIONAL_UNIT_NAME), - CASN1PrintableString(lpszVal)); - } - else if(stricmp(lpszOID, "C") == 0) - { - pAttVal = - new CAttributeValueAssertion(CAttributeType(CAttributeType::STR_COUNTRY_NAME), - CASN1PrintableString(lpszVal)); - } - else if(stricmp(lpszOID, "email") == 0) - { - pAttVal = - new CAttributeValueAssertion(CAttributeType(CAttributeType::STR_EMAIL_ADDRESS), - CASN1PrintableString(lpszVal)); - } - else if(stricmp(lpszOID, "L") == 0) - { - pAttVal = - new CAttributeValueAssertion(CAttributeType(CAttributeType::STR_LOCALITY_NAME), - CASN1PrintableString(lpszVal)); - } - else if(stricmp(lpszOID, "S") == 0) - { - pAttVal = - new CAttributeValueAssertion(CAttributeType(CAttributeType::STR_STATE_OR_PROVINCE_NAME), - CASN1PrintableString(lpszVal)); - } - else if(stricmp(lpszOID, "SN") == 0) - { - pAttVal = - new CAttributeValueAssertion(CAttributeType(CAttributeType::STR_SURNAME), - CASN1PrintableString(lpszVal)); - } - - CRelativeDistinguishedName relName; - - relName.addAttributeValue((*pAttVal)); - - addName(relName); - - delete pAttVal; - - szName = strtok(szName, "="); - } - - //delete[] szName1; + // parse the string + char szName1[256];// = new char[strlen(lpszName)]; + char* szName = szName1; + + strcpy(szName, lpszName); + + szName = strtok(szName, "="); + + LPSTR lpszOID; + LPSTR lpszVal; + + CAttributeValueAssertion* pAttVal; + + while(szName != NULL) + { + // OID + lpszOID = szName; + lpszVal = strtok(NULL, ";"); + + szName = lpszVal + strlen(lpszVal) + 1; + // Check OID + if(stricmp(lpszOID, "CN") == 0) + { + pAttVal = + new +CAttributeValueAssertion(CAttributeType(CAttributeType::STR_COMMON_NAME), + CASN1PrintableString(lpszVal)); + } + else if(stricmp(lpszOID, "O") == 0) + { + pAttVal = + new +CAttributeValueAssertion(CAttributeType(CAttributeType::STR_ORGANIZATION_NAME), + CASN1PrintableString(lpszVal)); + } + else if(stricmp(lpszOID, "OU") == 0) + { + pAttVal = + new +CAttributeValueAssertion(CAttributeType(CAttributeType::STR_ORGANIZATIONAL_UNIT_NAME), + CASN1PrintableString(lpszVal)); + } + else if(stricmp(lpszOID, "C") == 0) + { + pAttVal = + new +CAttributeValueAssertion(CAttributeType(CAttributeType::STR_COUNTRY_NAME), + CASN1PrintableString(lpszVal)); + } + else if(stricmp(lpszOID, "email") == 0) + { + pAttVal = + new +CAttributeValueAssertion(CAttributeType(CAttributeType::STR_EMAIL_ADDRESS), + CASN1PrintableString(lpszVal)); + } + else if(stricmp(lpszOID, "L") == 0) + { + pAttVal = + new +CAttributeValueAssertion(CAttributeType(CAttributeType::STR_LOCALITY_NAME), + CASN1PrintableString(lpszVal)); + } + else if(stricmp(lpszOID, "S") == 0) + { + pAttVal = + new +CAttributeValueAssertion(CAttributeType(CAttributeType::STR_STATE_OR_PROVINCE_NAME), + CASN1PrintableString(lpszVal)); + } + else if(stricmp(lpszOID, "SN") == 0) + { + pAttVal = + new +CAttributeValueAssertion(CAttributeType(CAttributeType::STR_SURNAME), + CASN1PrintableString(lpszVal)); + } + + CRelativeDistinguishedName relName; + + relName.addAttributeValue((*pAttVal)); + + addName(relName); + + delete pAttVal; + + szName = strtok(szName, "="); + } + + //delete[] szName1; } */ /* void CName::addName(const CRelativeDistinguishedName& name) { - addElement(new CRelativeDistinguishedName(name)); + addElement(new CRelativeDistinguishedName(name)); } */ -CName::~CName() { - -} +CName::~CName() {} diff --git a/libs/sign-sdk/src/ASN1/Name.h b/libs/sign-sdk/src/ASN1/Name.h index 1a6f3dae..ee428362 100644 --- a/libs/sign-sdk/src/ASN1/Name.h +++ b/libs/sign-sdk/src/ASN1/Name.h @@ -11,7 +11,6 @@ #include #include "ASN1/ASN1Sequence.h" -#include "RelativeDistinguishedName.h" #define OID_EMAIL_ADDRESS "1.2.840.113549.1.9.1" #define OID_UNSTRUCTURED_NAME "1.2.840.113549.1.9.2" @@ -40,16 +39,16 @@ using namespace std; class CName : public CASN1Sequence { - public: - CName(UUCBufferedReader& reader); + public: + CName(UUCBufferedReader& reader); - CName(const CASN1Object& name); + CName(const CASN1Object& name); - string getField(const char* fieldOID); + string getField(const char* fieldOID); - void getNameAsString(UUCByteArray& objId); + void getNameAsString(UUCByteArray& objId); - virtual ~CName(); + virtual ~CName(); }; #endif // !defined(AFX_NAME_H__554A2FC6_1A7E_4639_8E45_487603ACB583__INCLUDED_) diff --git a/libs/sign-sdk/src/ASN1/OCSPRequest.cpp b/libs/sign-sdk/src/ASN1/OCSPRequest.cpp index 0b01e186..4b8a3db4 100644 --- a/libs/sign-sdk/src/ASN1/OCSPRequest.cpp +++ b/libs/sign-sdk/src/ASN1/OCSPRequest.cpp @@ -49,7 +49,6 @@ #include -#include "ASN1OptionalField.h" #include "ASN1/AlgorithmIdentifier.h" #include "RSA/sha1.h" diff --git a/libs/sign-sdk/src/ASN1/OCSPRequest.h b/libs/sign-sdk/src/ASN1/OCSPRequest.h index 7de296b6..1a7ac0f2 100644 --- a/libs/sign-sdk/src/ASN1/OCSPRequest.h +++ b/libs/sign-sdk/src/ASN1/OCSPRequest.h @@ -38,7 +38,6 @@ CertID ::= SEQUENCE { #ifndef _OCSPRequest_H #define _OCSPRequest_H -#include "ASN1/ASN1Integer.h" #include "ASN1/ASN1Sequence.h" #include "ASN1/Certificate.h" diff --git a/libs/sign-sdk/src/ASN1/PKIStatusInfo.cpp b/libs/sign-sdk/src/ASN1/PKIStatusInfo.cpp index a02ac847..1e4d8a53 100644 --- a/libs/sign-sdk/src/ASN1/PKIStatusInfo.cpp +++ b/libs/sign-sdk/src/ASN1/PKIStatusInfo.cpp @@ -12,20 +12,11 @@ // Construction/Destruction CPKIStatusInfo::CPKIStatusInfo(UUCBufferedReader& reader) - : CASN1Sequence(reader) { - -} + : CASN1Sequence(reader) {} CPKIStatusInfo::CPKIStatusInfo(const CASN1Object& pkiStatusInfo) - : CASN1Sequence(pkiStatusInfo) { - -} - -CPKIStatusInfo::~CPKIStatusInfo() { - -} + : CASN1Sequence(pkiStatusInfo) {} +CPKIStatusInfo::~CPKIStatusInfo() {} -CASN1Integer CPKIStatusInfo::getStatus() { - return elementAt(0); -} +CASN1Integer CPKIStatusInfo::getStatus() { return elementAt(0); } diff --git a/libs/sign-sdk/src/ASN1/PKIStatusInfo.h b/libs/sign-sdk/src/ASN1/PKIStatusInfo.h index 2ba9844b..14917553 100644 --- a/libs/sign-sdk/src/ASN1/PKIStatusInfo.h +++ b/libs/sign-sdk/src/ASN1/PKIStatusInfo.h @@ -10,8 +10,10 @@ #ifndef _PKISTATUSINFO_H #define _PKISTATUSINFO_H -#include "ASN1/TimeStampToken.h" - +#include "ASN1/ASN1Integer.h" +#include "ASN1/ASN1Object.h" +#include "ASN1/ASN1Sequence.h" +#include "ASN1/UUCBufferedReader.h" class CPKIStatusInfo : public CASN1Sequence { public: CPKIStatusInfo(UUCBufferedReader& reader); diff --git a/libs/sign-sdk/src/ASN1/RSAPrivateKey.h b/libs/sign-sdk/src/ASN1/RSAPrivateKey.h index 4a0c0210..d7a6bd28 100644 --- a/libs/sign-sdk/src/ASN1/RSAPrivateKey.h +++ b/libs/sign-sdk/src/ASN1/RSAPrivateKey.h @@ -1,7 +1,7 @@ #pragma once #include "ASN1/ASN1Integer.h" -#include "ASN1Object.h" #include "ASN1/ASN1Sequence.h" +#include "ASN1Object.h" // RSAPrivateKey :: = SEQUENCE{ // version Version, diff --git a/libs/sign-sdk/src/ASN1/RSAPublicKey.cpp b/libs/sign-sdk/src/ASN1/RSAPublicKey.cpp index d8b7be80..f67b1a9b 100644 --- a/libs/sign-sdk/src/ASN1/RSAPublicKey.cpp +++ b/libs/sign-sdk/src/ASN1/RSAPublicKey.cpp @@ -1,27 +1,18 @@ #include "RSAPublicKey.h" CRSAPublicKey::CRSAPublicKey(UUCBufferedReader& reader) - : CASN1Sequence(reader) { -} - -CRSAPublicKey::CRSAPublicKey(const CASN1Object& obj) - : CASN1Sequence(obj) { + : CASN1Sequence(reader) {} -} +CRSAPublicKey::CRSAPublicKey(const CASN1Object& obj) : CASN1Sequence(obj) {} -CRSAPublicKey::CRSAPublicKey(const CASN1Integer& modulus, const CASN1Integer& exponent) { - addElement(modulus); - addElement(exponent); +CRSAPublicKey::CRSAPublicKey(const CASN1Integer& modulus, + const CASN1Integer& exponent) { + addElement(modulus); + addElement(exponent); } -CRSAPublicKey::~CRSAPublicKey(void) { +CRSAPublicKey::~CRSAPublicKey(void) {} -} +CASN1Integer CRSAPublicKey::getModulus() { return CASN1Integer(elementAt(0)); } -CASN1Integer CRSAPublicKey::getModulus() { - return CASN1Integer(elementAt(0)); -} - -CASN1Integer CRSAPublicKey::getExponent() { - return CASN1Integer(elementAt(1)); -} +CASN1Integer CRSAPublicKey::getExponent() { return CASN1Integer(elementAt(1)); } diff --git a/libs/sign-sdk/src/ASN1/RSAPublicKey.h b/libs/sign-sdk/src/ASN1/RSAPublicKey.h index f82f8d40..13954068 100644 --- a/libs/sign-sdk/src/ASN1/RSAPublicKey.h +++ b/libs/sign-sdk/src/ASN1/RSAPublicKey.h @@ -1,22 +1,22 @@ #pragma once #include "ASN1/ASN1Integer.h" -#include "ASN1Object.h" #include "ASN1/ASN1Sequence.h" +#include "ASN1Object.h" class CRSAPublicKey : public CASN1Sequence { - public: - // Costruttori - CRSAPublicKey(UUCBufferedReader& reader); + public: + // Costruttori + CRSAPublicKey(UUCBufferedReader& reader); - CRSAPublicKey(const CASN1Object& obj); + CRSAPublicKey(const CASN1Object& obj); - CRSAPublicKey(const CASN1Integer& modulus, const CASN1Integer& exponent); + CRSAPublicKey(const CASN1Integer& modulus, const CASN1Integer& exponent); - virtual ~CRSAPublicKey(); + virtual ~CRSAPublicKey(); - CASN1Integer getModulus(); + CASN1Integer getModulus(); - CASN1Integer getExponent(); + CASN1Integer getExponent(); - private: + private: }; diff --git a/libs/sign-sdk/src/ASN1/RelativeDistinguishedName.cpp b/libs/sign-sdk/src/ASN1/RelativeDistinguishedName.cpp index f8699fd5..99085fff 100644 --- a/libs/sign-sdk/src/ASN1/RelativeDistinguishedName.cpp +++ b/libs/sign-sdk/src/ASN1/RelativeDistinguishedName.cpp @@ -1,32 +1,27 @@ -// RelativeDistinguishedName.cpp: implementation of the CRelativeDistinguishedName class. +// RelativeDistinguishedName.cpp: implementation of the +// CRelativeDistinguishedName class. // - #include "RelativeDistinguishedName.h" -//#include "ASN1Exception.h" +// #include "ASN1Exception.h" // Construction/Destruction -CRelativeDistinguishedName::CRelativeDistinguishedName() { - -} - -CRelativeDistinguishedName::CRelativeDistinguishedName(UUCBufferedReader& reader) - : CASN1SetOf(reader) { +CRelativeDistinguishedName::CRelativeDistinguishedName() {} -} +CRelativeDistinguishedName::CRelativeDistinguishedName( + UUCBufferedReader& reader) + : CASN1SetOf(reader) {} CRelativeDistinguishedName::CRelativeDistinguishedName(const CASN1Object& name) - : CASN1SetOf(name) { -} + : CASN1SetOf(name) {} /* -void CRelativeDistinguishedName::addAttributeValue(const CAttributeValueAssertion& value) +void CRelativeDistinguishedName::addAttributeValue(const +CAttributeValueAssertion& value) { - addElement(new CAttributeValueAssertion(value)); + addElement(new CAttributeValueAssertion(value)); } */ -CRelativeDistinguishedName::~CRelativeDistinguishedName() { - -} +CRelativeDistinguishedName::~CRelativeDistinguishedName() {} diff --git a/libs/sign-sdk/src/ASN1/RelativeDistinguishedName.h b/libs/sign-sdk/src/ASN1/RelativeDistinguishedName.h index 898d738f..0a2c6517 100644 --- a/libs/sign-sdk/src/ASN1/RelativeDistinguishedName.h +++ b/libs/sign-sdk/src/ASN1/RelativeDistinguishedName.h @@ -11,19 +11,19 @@ #endif // _MSC_VER > 1000 #include "ASN1Setof.h" -//#include "AttributeValueAssertion.h" +// #include "AttributeValueAssertion.h" class CRelativeDistinguishedName : public CASN1SetOf { - public: - CRelativeDistinguishedName(UUCBufferedReader& reader); + public: + CRelativeDistinguishedName(UUCBufferedReader& reader); - CRelativeDistinguishedName(); + CRelativeDistinguishedName(); - CRelativeDistinguishedName(const CASN1Object& rname); + CRelativeDistinguishedName(const CASN1Object& rname); - // void addAttributeValue(const CAttributeValueAssertion& algos); + // void addAttributeValue(const CAttributeValueAssertion& algos); - virtual ~CRelativeDistinguishedName(); + virtual ~CRelativeDistinguishedName(); }; #endif // !defined(AFX_RELATIVEDISTINGUISHEDNAME_H__6344EC52_BE52_4C57_AA1D_3D2790EDDD7E__INCLUDED_) diff --git a/libs/sign-sdk/src/ASN1/SignedData.cpp b/libs/sign-sdk/src/ASN1/SignedData.cpp index eb7005ba..32c7e812 100644 --- a/libs/sign-sdk/src/ASN1/SignedData.cpp +++ b/libs/sign-sdk/src/ASN1/SignedData.cpp @@ -1,18 +1,12 @@ // SignedData.cpp: implementation of the CSignedData class. #include "SignedData.h" -#include "ASN1OptionalField.h" -#include "ASN1Exception.h" -#include "SignerInfo.h" -#include "RSAPublicKey.h" -#include "RSA/rsaeuro.h" -#include "RSA/rsa.h" -#include "RSA/sha2.h" -#include "DigestInfo.h" + #include + #include "ASN1/Certificate.h" -#include "ASN1/Crl.h" -#include -#include "RSA/sha1.h" +#include "ASN1OptionalField.h" +#include "RSA/rsa.h" +#include "SignerInfo.h" // Construction/Destruction CSignedData::CSignedData(UUCBufferedReader& reader) : CASN1Sequence(reader) {} diff --git a/libs/sign-sdk/src/ASN1/SignedData.h b/libs/sign-sdk/src/ASN1/SignedData.h index 0a08132c..732bbf96 100644 --- a/libs/sign-sdk/src/ASN1/SignedData.h +++ b/libs/sign-sdk/src/ASN1/SignedData.h @@ -1,6 +1,7 @@ // SignedData.h: interface for the CSignedData class. // +#include "ASN1/ASN1Setof.h" #if !defined(AFX_SIGNEDDATA_H__C408FDA9_5C26_4F85_8073_EA7278527011__INCLUDED_) #define AFX_SIGNEDDATA_H__C408FDA9_5C26_4F85_8073_EA7278527011__INCLUDED_ @@ -9,39 +10,38 @@ #endif // _MSC_VER > 1000 #include "ASN1/ASN1Sequence.h" -#include "ASN1/AlgorithmIdentifier.h" #include "ASN1/Certificate.h" #include "ASN1/ContentInfo.h" #include "Sign/disigonsdk.h" class CSignedData : public CASN1Sequence { - public: - CSignedData(UUCBufferedReader& reader); + public: + CSignedData(UUCBufferedReader& reader); - CSignedData(const CASN1Object& signedData); + CSignedData(const CASN1Object& signedData); - CSignedData(const CASN1SetOf& algos, const CContentInfo& contentInfo, - const CASN1SetOf& signerInfos, const CASN1SetOf& certificates); + CSignedData(const CASN1SetOf& algos, const CContentInfo& contentInfo, + const CASN1SetOf& signerInfos, const CASN1SetOf& certificates); - virtual ~CSignedData(); + virtual ~CSignedData(); - CASN1SetOf getDigestAlgorithmIdentifiers(); + CASN1SetOf getDigestAlgorithmIdentifiers(); - CContentInfo getContentInfo(); + CContentInfo getContentInfo(); - CASN1SetOf getSignerInfos(); + CASN1SetOf getSignerInfos(); - CASN1SetOf getCertificates(); + CASN1SetOf getCertificates(); - CCertificate getSignerCertificate(int index); + CCertificate getSignerCertificate(int index); - void makeDetached(); + void makeDetached(); - void setContent(UUCByteArray& content); + void setContent(UUCByteArray& content); - int verify(int i); + int verify(int i); - int verify(int i, const char* dateTime, REVOCATION_INFO* pRevocationInfo); + int verify(int i, const char* dateTime, REVOCATION_INFO* pRevocationInfo); }; #endif // !defined(AFX_SIGNEDDATA_H__C408FDA9_5C26_4F85_8073_EA7278527011__INCLUDED_) diff --git a/libs/sign-sdk/src/ASN1/SignerInfo.cpp b/libs/sign-sdk/src/ASN1/SignerInfo.cpp index 0cafa539..49ed9c0b 100644 --- a/libs/sign-sdk/src/ASN1/SignerInfo.cpp +++ b/libs/sign-sdk/src/ASN1/SignerInfo.cpp @@ -2,696 +2,712 @@ // #include "SignerInfo.h" + +#include +#include +#include +#include +#include + +#include "ASN1/ASN1Setof.h" +#include "ASN1/Certificate.h" #include "ASN1OptionalField.h" -#include "ASN1Exception.h" -#include "RSAPublicKey.h" -#include "RSA/rsaeuro.h" -#include "RSA/rsa.h" #include "CertStore.h" #include "DigestInfo.h" -#include -#include "ASN1/Certificate.h" -#include "ASN1/Crl.h" -#include +#include "RSA/rsa.h" +#include "RSA/rsaeuro.h" #include "RSA/sha1.h" #include "RSA/sha2.h" #include "UUCLogger.h" -#include - -#include -#include -#include - -//#import +// #import USE_LOG; // Construction/Destruction +CSignerInfo::~CSignerInfo() {} -CSignerInfo::~CSignerInfo() { - -} - -CSignerInfo::CSignerInfo(UUCBufferedReader& reader) - : CASN1Sequence(reader) { -} +CSignerInfo::CSignerInfo(UUCBufferedReader& reader) : CASN1Sequence(reader) {} CSignerInfo::CSignerInfo(const CASN1Object& signerInfo) - : CASN1Sequence(signerInfo) { -} + : CASN1Sequence(signerInfo) {} -CSignerInfo::CSignerInfo(const CIssuerAndSerialNumber& issuer, const CAlgorithmIdentifier& digestAlgo, const CAlgorithmIdentifier& encAlgo, const CASN1OctetString& encDigest) { - addElement(CASN1Integer(1)); - addElement(issuer); - addElement(digestAlgo); +CSignerInfo::CSignerInfo(const CIssuerAndSerialNumber& issuer, + const CAlgorithmIdentifier& digestAlgo, + const CAlgorithmIdentifier& encAlgo, + const CASN1OctetString& encDigest) { + addElement(CASN1Integer(1)); + addElement(issuer); + addElement(digestAlgo); - // Optional Field - //addElement(authAttributes); + // Optional Field + // addElement(authAttributes); - addElement(encAlgo); - addElement(encDigest); + addElement(encAlgo); + addElement(encDigest); - // Optional Field - //addElement(NULL); + // Optional Field + // addElement(NULL); } - void CSignerInfo::addAuthenticatedAttributes(const CASN1SetOf& attributes) { - addElementAt(CASN1OptionalField(attributes, 0x00), 3); + addElementAt(CASN1OptionalField(attributes, 0x00), 3); } void CSignerInfo::addUnauthenticatedAttributes(const CASN1SetOf& attributes) { - if(size() < 7) - addElement(CASN1OptionalField(attributes, 0x01)); - else - setElementAt(CASN1OptionalField(attributes, 0x01), 6); + if (size() < 7) + addElement(CASN1OptionalField(attributes, 0x01)); + else + setElementAt(CASN1OptionalField(attributes, 0x01), 6); } CASN1OctetString CSignerInfo::getEncryptedDigest() { - CASN1Object obj(elementAt(3)); - - if(obj.getTag() == 0xA0) { // optional auth attributes present - return (CASN1OctetString)elementAt(5); - } else { - return (CASN1OctetString)elementAt(4); - } - + CASN1Object obj(elementAt(3)); + if (obj.getTag() == 0xA0) { // optional auth attributes present + return (CASN1OctetString)elementAt(5); + } else { + return (CASN1OctetString)elementAt(4); + } } CAlgorithmIdentifier CSignerInfo::getDigestAlgorithn() { - //CASN1Object obj(elementAt(3)); - - //if(obj.getTag() == 0xA0) // optional auth attributes present - //{ - return (CAlgorithmIdentifier)elementAt(2); - //} - //else - //{ - // return (CAlgorithmIdentifier)elementAt(5); - //} + // CASN1Object obj(elementAt(3)); + + // if(obj.getTag() == 0xA0) // optional auth attributes present + //{ + return (CAlgorithmIdentifier)elementAt(2); + //} + // else + //{ + // return (CAlgorithmIdentifier)elementAt(5); + //} } CIssuerAndSerialNumber CSignerInfo::getIssuerAndSerialNumber() { - return (CIssuerAndSerialNumber)elementAt(1); + return (CIssuerAndSerialNumber)elementAt(1); } CASN1SetOf CSignerInfo::getAuthenticatedAttributes() { - CASN1Object obj(elementAt(3)); - - if(obj.getTag() == 0xA0) { // optional auth attributes present - return obj; - } else { - CASN1SetOf empty; - return empty; - } + CASN1Object obj(elementAt(3)); + + if (obj.getTag() == 0xA0) { // optional auth attributes present + return obj; + } else { + CASN1SetOf empty; + return empty; + } } CASN1SetOf CSignerInfo::getUnauthenticatedAttributes() { - if(size() > 6) { - CASN1Object obj(elementAt(3)); - - if(obj.getTag() == 0xA0) // optional auth attributes present - return elementAt(6); - else - return elementAt(5); - } else { - return CASN1SetOf(); - } + if (size() > 6) { + CASN1Object obj(elementAt(3)); + if (obj.getTag() == 0xA0) // optional auth attributes present + return elementAt(6); + else + return elementAt(5); + } else { + return CASN1SetOf(); + } } CASN1UTCTime CSignerInfo::getSigningTime() { - CASN1SetOf attrs = getAuthenticatedAttributes(); - - int size = attrs.size(); - for(int j = 0; j < size; j++) { - CASN1Sequence attr = attrs.elementAt(j); - CASN1ObjectIdentifier objId(attr.elementAt(0)); - if(objId.equals(CASN1ObjectIdentifier(szSigningTimeOID))) - return (CASN1SetOf(attr.elementAt(1))).elementAt(0); - } + CASN1SetOf attrs = getAuthenticatedAttributes(); - throw -1L; + int size = attrs.size(); + for (int j = 0; j < size; j++) { + CASN1Sequence attr = attrs.elementAt(j); + CASN1ObjectIdentifier objId(attr.elementAt(0)); + if (objId.equals(CASN1ObjectIdentifier(szSigningTimeOID))) + return (CASN1SetOf(attr.elementAt(1))).elementAt(0); + } + + throw -1L; } /* NON CORRETTO CASN1ObjectIdentifier CSignerInfo::getSigningCertificateV2() { - CASN1SetOf attrs = getAuthenticatedAttributes(); - - for(int j = 0; j < attrs.size(); j++) - { - CASN1Sequence attr = attrs.elementAt(j); - CASN1ObjectIdentifier objId(attr.elementAt(0)); - if(objId.equals(CASN1ObjectIdentifier(szIdAASigningCertificateV2OID))) - return (CASN1SetOf(attr.elementAt(1))).elementAt(0); - } + CASN1SetOf attrs = getAuthenticatedAttributes(); + + for(int j = 0; j < attrs.size(); j++) + { + CASN1Sequence attr = attrs.elementAt(j); + CASN1ObjectIdentifier objId(attr.elementAt(0)); + if(objId.equals(CASN1ObjectIdentifier(szIdAASigningCertificateV2OID))) + return (CASN1SetOf(attr.elementAt(1))).elementAt(0); + } - throw -1L; + throw -1L; } */ CASN1OctetString CSignerInfo::getContentHash() { - CASN1SetOf attrs = getAuthenticatedAttributes(); - int size = attrs.size(); - for(int j = 0; j < size; j++) { - CASN1Sequence attr = attrs.elementAt(j); - CASN1ObjectIdentifier objId(attr.elementAt(0)); - if(objId.equals(CASN1ObjectIdentifier(szMessageDigestOID))) - return (CASN1SetOf(attr.elementAt(1))).elementAt(0); - } - - throw -1L; + CASN1SetOf attrs = getAuthenticatedAttributes(); + int size = attrs.size(); + for (int j = 0; j < size; j++) { + CASN1Sequence attr = attrs.elementAt(j); + CASN1ObjectIdentifier objId(attr.elementAt(0)); + if (objId.equals(CASN1ObjectIdentifier(szMessageDigestOID))) + return (CASN1SetOf(attr.elementAt(1))).elementAt(0); + } + + throw -1L; } CTimeStampToken CSignerInfo::getTimeStampToken() { - CASN1SetOf attributes = getUnauthenticatedAttributes(); - CASN1ObjectIdentifier oidTimestampToken(szTimestampTokenOID); - - // search for timestamp oid - int nSize = attributes.size(); - for(int i = 0; i < nSize; i++) { - CASN1Sequence attribute = attributes.elementAt(i); - CASN1ObjectIdentifier oid = attribute.elementAt(0); - - if(oid.equals(oidTimestampToken)) { - CASN1SetOf values(attribute.elementAt(1)); - return values.elementAt(0); - } + CASN1SetOf attributes = getUnauthenticatedAttributes(); + CASN1ObjectIdentifier oidTimestampToken(szTimestampTokenOID); + + // search for timestamp oid + int nSize = attributes.size(); + for (int i = 0; i < nSize; i++) { + CASN1Sequence attribute = attributes.elementAt(i); + CASN1ObjectIdentifier oid = attribute.elementAt(0); + + if (oid.equals(oidTimestampToken)) { + CASN1SetOf values(attribute.elementAt(1)); + return values.elementAt(0); } + } - return CTimeStampToken(CASN1Sequence()); + return CTimeStampToken(CASN1Sequence()); } bool CSignerInfo::hasTimeStampToken() { - CASN1SetOf tst = getTimeStampToken(); - return tst.size() > 0; + CASN1SetOf tst = getTimeStampToken(); + return tst.size() > 0; } CASN1SetOf CSignerInfo::getCountersignatures() { - CASN1SetOf counterSignatures; - CASN1SetOf attributes = getUnauthenticatedAttributes(); - CASN1ObjectIdentifier oid(szCounterSignatureOID); - - // search for countersignature oid - int nSize = attributes.size(); - for(int i = 0; i < nSize; i++) { - CASN1Sequence attribute = attributes.elementAt(i); - CASN1ObjectIdentifier oid1 = attribute.elementAt(0); - - if(oid.equals(oid1)) { - CASN1SetOf set(attribute.elementAt(1)); - counterSignatures.addElement(set.elementAt(0)); - //return attribute.elementAt(1); - } + CASN1SetOf counterSignatures; + CASN1SetOf attributes = getUnauthenticatedAttributes(); + CASN1ObjectIdentifier oid(szCounterSignatureOID); + + // search for countersignature oid + int nSize = attributes.size(); + for (int i = 0; i < nSize; i++) { + CASN1Sequence attribute = attributes.elementAt(i); + CASN1ObjectIdentifier oid1 = attribute.elementAt(0); + + if (oid.equals(oid1)) { + CASN1SetOf set(attribute.elementAt(1)); + counterSignatures.addElement(set.elementAt(0)); + // return attribute.elementAt(1); } + } - return counterSignatures; + return counterSignatures; } -void CSignerInfo::setCountersignatures(int index, CSignerInfo& countersignature) { - CASN1SetOf attributes = getUnauthenticatedAttributes(); - CASN1ObjectIdentifier oid(szCounterSignatureOID); - int counter = 0; - // search for countersignature oid - int nSize = attributes.size(); - for(int i = 0; i < nSize; i++) { - CASN1Sequence attribute = attributes.elementAt(i); - CASN1ObjectIdentifier oid1 = attribute.elementAt(0); - - if(oid.equals(oid1)) { - if(counter == index) { - CASN1Sequence v; - v.addElement(oid); - CASN1SetOf cs; - cs.addElement(countersignature); - v.addElement(cs); - - attributes.setElementAt(v, i); - - addUnauthenticatedAttributes(attributes); - return; - } - - counter++; - } +void CSignerInfo::setCountersignatures(int index, + CSignerInfo& countersignature) { + CASN1SetOf attributes = getUnauthenticatedAttributes(); + CASN1ObjectIdentifier oid(szCounterSignatureOID); + int counter = 0; + // search for countersignature oid + int nSize = attributes.size(); + for (int i = 0; i < nSize; i++) { + CASN1Sequence attribute = attributes.elementAt(i); + CASN1ObjectIdentifier oid1 = attribute.elementAt(0); + + if (oid.equals(oid1)) { + if (counter == index) { + CASN1Sequence v; + v.addElement(oid); + CASN1SetOf cs; + cs.addElement(countersignature); + v.addElement(cs); + + attributes.setElementAt(v, i); + + addUnauthenticatedAttributes(attributes); + return; + } + + counter++; } + } } void CSignerInfo::addCountersignatures(CSignerInfo& countersignature) { - CASN1SetOf attributes = getUnauthenticatedAttributes(); - CASN1ObjectIdentifier oid(szCounterSignatureOID); + CASN1SetOf attributes = getUnauthenticatedAttributes(); + CASN1ObjectIdentifier oid(szCounterSignatureOID); - CASN1Sequence v; - v.addElement(oid); - CASN1SetOf cs; - cs.addElement(countersignature); - v.addElement(cs); - attributes.addElement(v); + CASN1Sequence v; + v.addElement(oid); + CASN1SetOf cs; + cs.addElement(countersignature); + v.addElement(cs); + attributes.addElement(v); - addUnauthenticatedAttributes(attributes); + addUnauthenticatedAttributes(attributes); } void CSignerInfo::setTimeStampToken(CTimeStampToken& tst) { - CASN1SetOf attributes = getUnauthenticatedAttributes(); - CASN1ObjectIdentifier oid(szTimestampTokenOID); + CASN1SetOf attributes = getUnauthenticatedAttributes(); + CASN1ObjectIdentifier oid(szTimestampTokenOID); - CASN1Sequence v; - v.addElement(oid); - CASN1SetOf cs; - cs.addElement(tst); - v.addElement(cs); - attributes.addElement(v); + CASN1Sequence v; + v.addElement(oid); + CASN1SetOf cs; + cs.addElement(tst); + v.addElement(cs); + attributes.addElement(v); - addUnauthenticatedAttributes(attributes); + addUnauthenticatedAttributes(attributes); } int CSignerInfo::getCountersignatureCount() { - CASN1SetOf countersignatures(getCountersignatures()); - return countersignatures.size(); + CASN1SetOf countersignatures(getCountersignatures()); + return countersignatures.size(); } int CSignerInfo::verifyCountersignature(int i, CASN1SetOf& certificates) { - return verifyCountersignature(i, certificates, NULL, NULL); + return verifyCountersignature(i, certificates, NULL, NULL); } -int CSignerInfo::verifyCountersignature(int i, CASN1SetOf& certificates, const char* szDateTime, REVOCATION_INFO* pRevocationInfo) { - CASN1SetOf countersignatures(getCountersignatures()); - CSignerInfo countersignature(countersignatures.elementAt(i)); - CASN1OctetString source(getEncryptedDigest()); - return verifySignature(source, countersignature, certificates, szDateTime, pRevocationInfo); +int CSignerInfo::verifyCountersignature(int i, CASN1SetOf& certificates, + const char* szDateTime, + REVOCATION_INFO* pRevocationInfo) { + CASN1SetOf countersignatures(getCountersignatures()); + CSignerInfo countersignature(countersignatures.elementAt(i)); + CASN1OctetString source(getEncryptedDigest()); + return verifySignature(source, countersignature, certificates, szDateTime, + pRevocationInfo); } - -int CSignerInfo::verifySignature(CASN1OctetString& source, CSignerInfo& signerInfo, CASN1SetOf& certificates, const char* szDateTime, REVOCATION_INFO* pRevocationInfo) { - LOG_DBG((0, "--> CSignerInfo::verifySignature", "Verify Revocation: %d", (pRevocationInfo != NULL))); - - CCertificate cert = getSignatureCertificate(signerInfo, certificates); - - int bitmask = 0; - - // verifica il certificato - if(cert.isValid(szDateTime)) { - bitmask |= VERIFIED_CERT_VALIDITY; - } - - if(cert.isQualified()) { - bitmask |= VERIFIED_CERT_QUALIFIED; - } - - if(cert.isNonRepudiation()) { - bitmask |= VERIFIED_KEY_USAGE; - } - - if(cert.isSHA256()) { - bitmask |= VERIFIED_CERT_SHA256; +int CSignerInfo::verifySignature(CASN1OctetString& source, + CSignerInfo& signerInfo, + CASN1SetOf& certificates, + const char* szDateTime, + REVOCATION_INFO* pRevocationInfo) { + LOG_DBG((0, "--> CSignerInfo::verifySignature", "Verify Revocation: %d", + (pRevocationInfo != NULL))); + + CCertificate cert = getSignatureCertificate(signerInfo, certificates); + + int bitmask = 0; + + // verifica il certificato + if (cert.isValid(szDateTime)) { + bitmask |= VERIFIED_CERT_VALIDITY; + } + + if (cert.isQualified()) { + bitmask |= VERIFIED_CERT_QUALIFIED; + } + + if (cert.isNonRepudiation()) { + bitmask |= VERIFIED_KEY_USAGE; + } + + if (cert.isSHA256()) { + bitmask |= VERIFIED_CERT_SHA256; + } + + if (pRevocationInfo) { + pRevocationInfo->nRevocationStatus = REVOCATION_STATUS_UNKNOWN; + + // verify revocation status only if the certificate is valid + if (bitmask & VERIFIED_CERT_VALIDITY) { + int verifyStatus = cert.verifyStatus(szDateTime, pRevocationInfo); + + switch (verifyStatus) { + case REVOCATION_STATUS_GOOD: + bitmask |= VERIFIED_CERT_GOOD; + bitmask |= VERIFIED_CRL_LOADED; + break; + + case REVOCATION_STATUS_REVOKED: + bitmask |= VERIFIED_CRL_LOADED; + bitmask |= VERIFIED_CERT_REVOKED; + break; + + case REVOCATION_STATUS_SUSPENDED: + bitmask |= VERIFIED_CERT_SUSPENDED; + bitmask |= VERIFIED_CRL_LOADED; + break; + + case REVOCATION_STATUS_UNKNOWN: + bitmask |= VERIFIED_CRL_LOADED; + break; + + default: + break; + } } - - - if(pRevocationInfo) { - pRevocationInfo->nRevocationStatus = REVOCATION_STATUS_UNKNOWN; - - // verify revocation status only if the certificate is valid - if(bitmask & VERIFIED_CERT_VALIDITY) { - int verifyStatus = cert.verifyStatus(szDateTime, pRevocationInfo); - - switch (verifyStatus) { - case REVOCATION_STATUS_GOOD: - bitmask |= VERIFIED_CERT_GOOD; - bitmask |= VERIFIED_CRL_LOADED; - break; - - case REVOCATION_STATUS_REVOKED: - bitmask |= VERIFIED_CRL_LOADED; - bitmask |= VERIFIED_CERT_REVOKED; - break; - - case REVOCATION_STATUS_SUSPENDED: - bitmask |= VERIFIED_CERT_SUSPENDED; - bitmask |= VERIFIED_CRL_LOADED; - break; - - case REVOCATION_STATUS_UNKNOWN: - bitmask |= VERIFIED_CRL_LOADED; - break; - - default: - break; - } + } + + // verifica la cert chain + // CName issuerName(cert.getIssuer()); + // UUCByteArray issuer; + // issuerName.getNameAsString(issuer);//getField(OID_COMMON_NAME); + // + CCertificate* pCert = &cert; + CCertificate* pCACert = CCertStore::GetCertificate(cert); + while (pCACert && pCert->verifySignature(*pCACert)) { + bitmask |= VERIFIED_CACERT_FOUND; + + // NSLog(@"issuer: %s, SN: %s", issuer.c_str(), serialNumber.toHexString()); + + if (pCACert->isValid(szDateTime)) { + bitmask |= VERIFIED_CACERT_VALIDITY; + if (pRevocationInfo) { + int verifyStatus = pCACert->verifyStatus(szDateTime, NULL); + + switch (verifyStatus) { + case REVOCATION_STATUS_GOOD: + bitmask |= VERIFIED_CACERT_GOOD; + bitmask |= VERIFIED_CACRL_LOADED; + break; + + case REVOCATION_STATUS_REVOKED: + bitmask |= VERIFIED_CACRL_LOADED; + bitmask |= VERIFIED_CACERT_REVOKED; + break; + + case REVOCATION_STATUS_SUSPENDED: + bitmask |= VERIFIED_CACERT_SUSPENDED; + bitmask |= VERIFIED_CACRL_LOADED; + break; + + case REVOCATION_STATUS_UNKNOWN: + break; } + } } - - // verifica la cert chain -// CName issuerName(cert.getIssuer()); -// UUCByteArray issuer; -// issuerName.getNameAsString(issuer);//getField(OID_COMMON_NAME); -// - CCertificate* pCert = &cert; - CCertificate* pCACert = CCertStore::GetCertificate(cert); - while(pCACert && pCert->verifySignature(*pCACert)) { - bitmask |= VERIFIED_CACERT_FOUND; - - //NSLog(@"issuer: %s, SN: %s", issuer.c_str(), serialNumber.toHexString()); - - if(pCACert->isValid(szDateTime)) { - bitmask |= VERIFIED_CACERT_VALIDITY; - if(pRevocationInfo) { - int verifyStatus = pCACert->verifyStatus(szDateTime, NULL); - - switch (verifyStatus) { - case REVOCATION_STATUS_GOOD: - bitmask |= VERIFIED_CACERT_GOOD; - bitmask |= VERIFIED_CACRL_LOADED; - break; - - case REVOCATION_STATUS_REVOKED: - bitmask |= VERIFIED_CACRL_LOADED; - bitmask |= VERIFIED_CACERT_REVOKED; - break; - - case REVOCATION_STATUS_SUSPENDED: - bitmask |= VERIFIED_CACERT_SUSPENDED; - bitmask |= VERIFIED_CACRL_LOADED; - break; - - case REVOCATION_STATUS_UNKNOWN: - break; - } - } + pCert = pCACert; + pCACert = CCertStore::GetCertificate(*pCACert); + } + + if (!pCACert) { + bitmask |= VERIFIED_CERT_CHAIN; + } else { + // NSLog(@"CA Cert not valid"); + } + + // verifica la firma + + // OpenSSL + UUCByteArray baCert; + cert.toByteArray(baCert); + + // int result; + // BIO* bio; + X509* x509 = NULL; + + const BYTE* content = baCert.getContent(); + x509 = d2i_X509(NULL, &content, baCert.getLength()); + + EVP_PKEY* evp_pubkey; + RSA* rsa_pubkey; + + evp_pubkey = X509_get_pubkey(x509); + rsa_pubkey = EVP_PKEY_get1_RSA(evp_pubkey); + + ////////////// + + // CSubjectPublicKeyInfo publicKeyInfo = + // cert.getCertificateInfo().getSubjectPublicKeyInfo(); + + CASN1OctetString encryptedDigest(signerInfo.getEncryptedDigest()); + const UUCByteArray* pEncDigest = encryptedDigest.getValue(); + + // //UUCByteArray aux1; + // CASN1BitString publicKey(publicKeyInfo.getPublicKey()); + // //publicKey.toByteArray(aux1); + // //szHex = aux1.toHexString(); + // + // UUCByteArray* pBitString = (UUCByteArray*)publicKey.getValue(); + // + // //szHex = pBitString->toHexString(); + // pBitString->remove(0); + // //szHex = pBitString->toHexString(); + // + // UUCBufferedReader reader(*pBitString); + // CRSAPublicKey pubKey(reader); + // + // //UUCByteArray aux; + // //pubKey.toByteArray(aux); + // //szHex = aux.toHexString(); + // + // CASN1Integer modulus = pubKey.getModulus(); + // CASN1Integer exp = pubKey.getExponent(); + // + // R_RSA_PUBLIC_KEY rsakey; + // + // int retVal=0; + + try { + // UUCByteArray* modArr = (UUCByteArray*)modulus.getValue(); + // UUCByteArray* expArr = (UUCByteArray*)exp.getValue(); + // + // memset(rsakey.exponent,0x00,MAX_RSA_MODULUS_LEN); + // memset(rsakey.modulus,0x00,MAX_RSA_MODULUS_LEN); + // + // // copio l'esponente + // memcpy(rsakey.exponent + MAX_RSA_MODULUS_LEN - + // expArr->getLength(), expArr->getContent(), expArr->getLength()); + // + // //copio il modulo + // memcpy(rsakey.modulus + MAX_RSA_MODULUS_LEN - + // modArr->getLength(), modArr->getContent(), modArr->getLength()); + // + // // calcola il numero di bit effettivo + // int i=0; + // while(rsakey.modulus[i] == 0 && i < MAX_RSA_MODULUS_LEN) + // i++; + // + // rsakey.bits = (MAX_RSA_MODULUS_LEN - i) * 8; + // + BYTE decrypted[MAX_RSA_MODULUS_LEN]; + unsigned int len = MAX_RSA_MODULUS_LEN; + + const BYTE* encrypted = pEncDigest->getContent(); + const int encrypted_len = (int)pEncDigest->getLength(); + + len = RSA_public_decrypt(encrypted_len, (BYTE*)encrypted, decrypted, + rsa_pubkey, RSA_PKCS1_PADDING); + + RSA_free(rsa_pubkey); + EVP_PKEY_free(evp_pubkey); + X509_free(x509); + + // ritorna il DigestInfo pulito, senza padding + // retVal = RSAPublicDecrypt(decrypted, &len, + //(BYTE*)pEncDigest->getContent(), (unsigned int)pEncDigest->getLength(), + //&rsakey); + if (len) { + LOG_DBG((0, "CSignerInfo::verifySignature", "RSAPublicDecrypt OK")); + + char szAux[100]; + + UUCByteArray dec(decrypted, len); + UUCBufferedReader reader(dec); + CDigestInfo digestInfo(reader); + // UUCByteArray* pauxdi = (UUCByteArray*)digestInfo.getValue(); + // szHex = pauxdi->toHexString(); + + CASN1OctetString digest = digestInfo.getDigest(); + UUCByteArray* pDigestValue = (UUCByteArray*)digest.getValue(); + // szHex = pDigestValue->toHexString(); + + // content + UUCByteArray content; + CASN1OctetString octetString(source); + // UUCByteArray baoctetstring; + // octetString.toByteArray(baoctetstring); + // NSLog([NSString stringWithCString:baoctetstring.toHexString()]); + + if (octetString.getTag() == 0x24) { // contructed octet string + CASN1Sequence contentArray(octetString); + int size = contentArray.size(); + for (int i = 0; i < size; i++) { + content.append(contentArray.elementAt(i).getValue()->getContent(), + contentArray.elementAt(i).getLength()); + } + } else { + content.append(octetString.getValue()->getContent(), + octetString.getLength()); + } + + BYTE* buff; + int bufflen = 0; + + UUCByteArray messageDigest; + + // estra i signedattributes + UUCByteArray signedAttr; + CASN1SetOf authAttr(signerInfo.getAuthenticatedAttributes()); + int authAttrSize = authAttr.size(); + + LOG_DBG( + (0, "CSignerInfo::verifySignature", "Attrsize: %d", authAttrSize)); + + if (authAttrSize > 0) { + CASN1ObjectIdentifier oid(szMessageDigestOID); + CASN1ObjectIdentifier oid1(szContentTypeOID); + CASN1ObjectIdentifier oid2(szIdAASigningCertificateV2OID); + for (int i = 0; i < authAttrSize; i++) { + CASN1Sequence attr(authAttr.elementAt(i)); + + if (oid.equals(attr.elementAt(0))) { + bitmask |= VERIFIED_SIGNED_ATTRIBUTE_MD; + CASN1SetOf values(attr.elementAt(1)); + CASN1OctetString val(values.elementAt(0)); + const UUCByteArray* pval = val.getValue(); + messageDigest.append(*pval); + } else if (oid1.equals(attr.elementAt(0))) { + bitmask |= VERIFIED_SIGNED_ATTRIBUTE_CT; + } else if (oid2.equals(attr.elementAt(0))) { + bitmask |= VERIFIED_SIGNED_ATTRIBUTE_SC; + } } - pCert = pCACert; - pCACert = CCertStore::GetCertificate(*pCACert); - } - - if(!pCACert) { - bitmask |= VERIFIED_CERT_CHAIN; - } else { - //NSLog(@"CA Cert not valid"); - } - - - // verifica la firma - - // OpenSSL - UUCByteArray baCert; - cert.toByteArray(baCert); - -// int result; -// BIO* bio; - X509 *x509 = NULL; - - const BYTE* content = baCert.getContent(); - x509 = d2i_X509(NULL, &content, baCert.getLength()); - - EVP_PKEY *evp_pubkey; - RSA *rsa_pubkey; - - evp_pubkey = X509_get_pubkey(x509); - rsa_pubkey = EVP_PKEY_get1_RSA(evp_pubkey); - - ////////////// - -// CSubjectPublicKeyInfo publicKeyInfo = cert.getCertificateInfo().getSubjectPublicKeyInfo(); - - CASN1OctetString encryptedDigest(signerInfo.getEncryptedDigest()); - const UUCByteArray* pEncDigest = encryptedDigest.getValue(); - -// //UUCByteArray aux1; -// CASN1BitString publicKey(publicKeyInfo.getPublicKey()); -// //publicKey.toByteArray(aux1); -// //szHex = aux1.toHexString(); -// -// UUCByteArray* pBitString = (UUCByteArray*)publicKey.getValue(); -// -// //szHex = pBitString->toHexString(); -// pBitString->remove(0); -// //szHex = pBitString->toHexString(); -// -// UUCBufferedReader reader(*pBitString); -// CRSAPublicKey pubKey(reader); -// -// //UUCByteArray aux; -// //pubKey.toByteArray(aux); -// //szHex = aux.toHexString(); -// -// CASN1Integer modulus = pubKey.getModulus(); -// CASN1Integer exp = pubKey.getExponent(); -// -// R_RSA_PUBLIC_KEY rsakey; -// -// int retVal=0; - - try { -// UUCByteArray* modArr = (UUCByteArray*)modulus.getValue(); -// UUCByteArray* expArr = (UUCByteArray*)exp.getValue(); -// -// memset(rsakey.exponent,0x00,MAX_RSA_MODULUS_LEN); -// memset(rsakey.modulus,0x00,MAX_RSA_MODULUS_LEN); -// -// // copio l'esponente -// memcpy(rsakey.exponent + MAX_RSA_MODULUS_LEN - expArr->getLength(), expArr->getContent(), expArr->getLength()); -// -// //copio il modulo -// memcpy(rsakey.modulus + MAX_RSA_MODULUS_LEN - modArr->getLength(), modArr->getContent(), modArr->getLength()); -// -// // calcola il numero di bit effettivo -// int i=0; -// while(rsakey.modulus[i] == 0 && i < MAX_RSA_MODULUS_LEN) -// i++; -// -// rsakey.bits = (MAX_RSA_MODULUS_LEN - i) * 8; -// - BYTE decrypted[MAX_RSA_MODULUS_LEN]; - unsigned int len = MAX_RSA_MODULUS_LEN; - - const BYTE* encrypted = pEncDigest->getContent(); - const int encrypted_len = (int)pEncDigest->getLength(); - - len = RSA_public_decrypt(encrypted_len, (BYTE*)encrypted, decrypted, rsa_pubkey,RSA_PKCS1_PADDING); - - RSA_free(rsa_pubkey); - EVP_PKEY_free(evp_pubkey); - X509_free(x509); - - // ritorna il DigestInfo pulito, senza padding -// retVal = RSAPublicDecrypt(decrypted, &len, (BYTE*)pEncDigest->getContent(), (unsigned int)pEncDigest->getLength(), &rsakey); - if(len) { - LOG_DBG((0, "CSignerInfo::verifySignature", "RSAPublicDecrypt OK")); - - char szAux[100]; - - UUCByteArray dec(decrypted, len); - UUCBufferedReader reader(dec); - CDigestInfo digestInfo(reader); - //UUCByteArray* pauxdi = (UUCByteArray*)digestInfo.getValue(); - //szHex = pauxdi->toHexString(); - - CASN1OctetString digest = digestInfo.getDigest(); - UUCByteArray* pDigestValue = (UUCByteArray*)digest.getValue(); - //szHex = pDigestValue->toHexString(); - - // content - UUCByteArray content; - CASN1OctetString octetString(source); - //UUCByteArray baoctetstring; - //octetString.toByteArray(baoctetstring); - //NSLog([NSString stringWithCString:baoctetstring.toHexString()]); - - if(octetString.getTag() == 0x24) { // contructed octet string - CASN1Sequence contentArray(octetString); - int size = contentArray.size(); - for(int i = 0; i < size; i++) { - content.append(contentArray.elementAt(i).getValue()->getContent(), contentArray.elementAt(i).getLength()); - } + authAttr.toByteArray(signedAttr); + // szHex = signedAttr.toHexString(); + // NSLog([NSString stringWithCString:szHex]); + + buff = (BYTE*)signedAttr.getContent(); + bufflen = signedAttr.getLength(); + + // LOG_DBG((0, "CSignerInfo::verifySignature", "Buf: %s, %d", + // signedAttr.toHexString(), bufflen)); + } else { + // se non ci sono signedattributes l'hash va fatto sul content + buff = (BYTE*)content.getContent(); + bufflen = content.getLength(); + + // LOG_DBG((0, "CSignerInfo::verifySignature", "Buf2: %s, %d", + // content.toHexString(), bufflen)); + } + + CAlgorithmIdentifier digestAlgo(digestInfo.getDigestAlgorithm()); + CAlgorithmIdentifier sha256Algo(szSHA256OID); + CAlgorithmIdentifier sha1Algo(szSHA1OID); + if (digestAlgo.elementAt(0) == sha256Algo.elementAt(0)) { + LOG_DBG((0, "CSignerInfo::verifySignature", "SHA256 OK")); + + bitmask |= VERIFIED_SHA256; + + BYTE hash[32]; + BYTE hash2[32]; + /* + sha256_context ctx256; + sha256_starts(&ctx256); + sha256_update(&ctx256, buff, bufflen); + sha256_finish(&ctx256, hash); + + sha256_context ctx2561; + sha256_starts(&ctx2561); + sha256_update(&ctx2561, content.getContent(), + content.getLength()); sha256_finish(&ctx2561, hash2); + */ + sha2(buff, bufflen, hash, 0); + sha2(content.getContent(), content.getLength(), hash2, 0); + /* + + SHA256_CTX ctx256; + SHA256_Init(&ctx256); + SHA256_Update(&ctx256, buff, bufflen); + SHA256_Final(hash, &ctx256); + + SHA256_CTX ctx2561; + SHA256_Init(&ctx2561); + SHA256_Update(&ctx2561, + content.getContent(), content.getLength()); SHA256_Final(hash2, + &ctx2561); + */ + + UUCByteArray bahash((BYTE*)hash, 32); + LOG_DBG((0, "CSignerInfo::verifySignature", "DigestValue: %s, %s", + pDigestValue->toHexString(), bahash.toHexString())); + + if (memcmp(hash, pDigestValue->getContent(), 32) == 0) { + LOG_DBG((0, "CSignerInfo::verifySignature", "SHA256 Len OK")); + + // verifica l'hash del content + if (messageDigest.getLength() > 0) { + if (memcmp(hash2, messageDigest.getContent(), 32) == 0) { + bitmask |= VERIFIED_SIGNATURE; + LOG_DBG( + (0, "CSignerInfo::verifySignature", "VERIFIED: %x", bitmask)); } else { - content.append(octetString.getValue()->getContent(), octetString.getLength()); + LOG_DBG((0, "CSignerInfo::verifySignature", "Not verified")); } + } else { + if (memcmp(hash2, hash, 32) == 0) { + bitmask |= VERIFIED_SIGNATURE; + LOG_DBG((0, "CSignerInfo::verifySignature", "VERIFIED 2: %x", + bitmask)); + } else { + LOG_DBG((0, "CSignerInfo::verifySignature", "Not verified 2")); + } + } + } else { + LOG_DBG((0, "CSignerInfo::verifySignature", "Not verified 3")); + } + } else if (digestAlgo.elementAt(0) == + sha1Algo.elementAt( + 0)) { // if(digestAlgo == CAlgorithmIdentifier(szSHA1OID)) + LOG_DBG((0, "CSignerInfo::verifySignature", "SHA1")); + // BYTE hash[20]; + // calcola l'hash SHA1 + SHA1Context sha; - BYTE* buff; - int bufflen = 0; - - UUCByteArray messageDigest; - - // estra i signedattributes - UUCByteArray signedAttr; - CASN1SetOf authAttr(signerInfo.getAuthenticatedAttributes()); - int authAttrSize = authAttr.size(); - - LOG_DBG((0, "CSignerInfo::verifySignature", "Attrsize: %d", authAttrSize)); + SHA1Reset(&sha); - if(authAttrSize > 0) { - CASN1ObjectIdentifier oid(szMessageDigestOID); - CASN1ObjectIdentifier oid1(szContentTypeOID); - CASN1ObjectIdentifier oid2(szIdAASigningCertificateV2OID); - for(int i = 0; i < authAttrSize; i++) { - CASN1Sequence attr(authAttr.elementAt(i)); + SHA1Input(&sha, buff, bufflen); - if(oid.equals(attr.elementAt(0))) { - bitmask |= VERIFIED_SIGNED_ATTRIBUTE_MD; - CASN1SetOf values(attr.elementAt(1)); - CASN1OctetString val(values.elementAt(0)); - const UUCByteArray* pval = val.getValue(); - messageDigest.append(*pval); - } else if(oid1.equals(attr.elementAt(0))) { - bitmask |= VERIFIED_SIGNED_ATTRIBUTE_CT; - } else if(oid2.equals(attr.elementAt(0))) { - bitmask |= VERIFIED_SIGNED_ATTRIBUTE_SC; - } + SHA1Result(&sha); - } + sprintf(szAux, "%08X%08X%08X%08X%08X ", sha.Message_Digest[0], + sha.Message_Digest[1], sha.Message_Digest[2], + sha.Message_Digest[3], sha.Message_Digest[4]); + UUCByteArray hashaux(szAux); - authAttr.toByteArray(signedAttr); - //szHex = signedAttr.toHexString(); - //NSLog([NSString stringWithCString:szHex]); + SHA1Reset(&sha); + SHA1Input(&sha, content.getContent(), content.getLength()); + SHA1Result(&sha); + sprintf(szAux, "%08X%08X%08X%08X%08X ", sha.Message_Digest[0], + sha.Message_Digest[1], sha.Message_Digest[2], + sha.Message_Digest[3], sha.Message_Digest[4]); + UUCByteArray contentHash(szAux); - buff = (BYTE*)signedAttr.getContent(); - bufflen = signedAttr.getLength(); + if (memcmp(hashaux.getContent(), pDigestValue->getContent(), + hashaux.getLength()) == 0) { + LOG_DBG((0, "CSignerInfo::verifySignature", "length 1")); - //LOG_DBG((0, "CSignerInfo::verifySignature", "Buf: %s, %d", signedAttr.toHexString(), bufflen)); + // verifica l'hash del content + if (messageDigest.getLength() > 0) { + LOG_DBG((0, "CSignerInfo::verifySignature", "length 2")); + if (memcmp(contentHash.getContent(), messageDigest.getContent(), + contentHash.getLength()) == 0) { + bitmask |= VERIFIED_SIGNATURE; } else { - // se non ci sono signedattributes l'hash va fatto sul content - buff = (BYTE*)content.getContent(); - bufflen = content.getLength(); - - //LOG_DBG((0, "CSignerInfo::verifySignature", "Buf2: %s, %d", content.toHexString(), bufflen)); + LOG_DBG((0, "CSignerInfo::verifySignature", "Not verified 2")); } - - CAlgorithmIdentifier digestAlgo(digestInfo.getDigestAlgorithm()); - CAlgorithmIdentifier sha256Algo(szSHA256OID); - CAlgorithmIdentifier sha1Algo(szSHA1OID); - if(digestAlgo.elementAt(0) == sha256Algo.elementAt(0)) { - LOG_DBG((0, "CSignerInfo::verifySignature", "SHA256 OK")); - - bitmask |= VERIFIED_SHA256; - - BYTE hash[32]; - BYTE hash2[32]; - /* - sha256_context ctx256; - sha256_starts(&ctx256); - sha256_update(&ctx256, buff, bufflen); - sha256_finish(&ctx256, hash); - - sha256_context ctx2561; - sha256_starts(&ctx2561); - sha256_update(&ctx2561, content.getContent(), content.getLength()); - sha256_finish(&ctx2561, hash2); - */ - sha2(buff, bufflen, hash, 0); - sha2(content.getContent(), content.getLength(), hash2, 0); - /* - - SHA256_CTX ctx256; - SHA256_Init(&ctx256); - SHA256_Update(&ctx256, buff, bufflen); - SHA256_Final(hash, &ctx256); - - SHA256_CTX ctx2561; - SHA256_Init(&ctx2561); - SHA256_Update(&ctx2561, content.getContent(), content.getLength()); - SHA256_Final(hash2, &ctx2561); - */ - - UUCByteArray bahash((BYTE*)hash, 32); - LOG_DBG((0, "CSignerInfo::verifySignature", "DigestValue: %s, %s", pDigestValue->toHexString(), bahash.toHexString())); - - if(memcmp(hash, pDigestValue->getContent(), 32) == 0) { - LOG_DBG((0, "CSignerInfo::verifySignature", "SHA256 Len OK")); - - // verifica l'hash del content - if(messageDigest.getLength() > 0) { - if(memcmp(hash2, messageDigest.getContent(), 32) == 0) { - bitmask |= VERIFIED_SIGNATURE; - LOG_DBG((0, "CSignerInfo::verifySignature", "VERIFIED: %x", bitmask)); - } else { - LOG_DBG((0, "CSignerInfo::verifySignature", "Not verified")); - } - } else { - if(memcmp(hash2, hash, 32) == 0) { - bitmask |= VERIFIED_SIGNATURE; - LOG_DBG((0, "CSignerInfo::verifySignature", "VERIFIED 2: %x", bitmask)); - } else { - LOG_DBG((0, "CSignerInfo::verifySignature", "Not verified 2")); - } - } - } else { - LOG_DBG((0, "CSignerInfo::verifySignature", "Not verified 3")); - } - } else if(digestAlgo.elementAt(0) == sha1Algo.elementAt(0)) { //if(digestAlgo == CAlgorithmIdentifier(szSHA1OID)) - LOG_DBG((0, "CSignerInfo::verifySignature", "SHA1")); - //BYTE hash[20]; - - // calcola l'hash SHA1 - SHA1Context sha; - - SHA1Reset(&sha); - - SHA1Input(&sha, buff, bufflen); - - SHA1Result(&sha); - - sprintf(szAux, "%08X%08X%08X%08X%08X ", sha.Message_Digest[0], sha.Message_Digest[1], sha.Message_Digest[2], sha.Message_Digest[3], sha.Message_Digest[4]); - - UUCByteArray hashaux(szAux); - - SHA1Reset(&sha); - SHA1Input(&sha, content.getContent(), content.getLength()); - SHA1Result(&sha); - sprintf(szAux, "%08X%08X%08X%08X%08X ", sha.Message_Digest[0], sha.Message_Digest[1], sha.Message_Digest[2], sha.Message_Digest[3], sha.Message_Digest[4]); - UUCByteArray contentHash(szAux); - - - if(memcmp(hashaux.getContent(), pDigestValue->getContent(), hashaux.getLength()) == 0) { - LOG_DBG((0, "CSignerInfo::verifySignature", "length 1")); - - // verifica l'hash del content - if(messageDigest.getLength() > 0) { - LOG_DBG((0, "CSignerInfo::verifySignature", "length 2")); - if(memcmp(contentHash.getContent(), messageDigest.getContent(), contentHash.getLength()) == 0) { - bitmask |= VERIFIED_SIGNATURE; - } else { - LOG_DBG((0, "CSignerInfo::verifySignature", "Not verified 2")); - } - } else { - if(memcmp(contentHash.getContent(), hashaux.getContent(), contentHash.getLength()) == 0) { - bitmask |= VERIFIED_SIGNATURE; - } else { - LOG_DBG((0, "CSignerInfo::verifySignature", "Not verified 3")); - } - } - } + } else { + if (memcmp(contentHash.getContent(), hashaux.getContent(), + contentHash.getLength()) == 0) { + bitmask |= VERIFIED_SIGNATURE; + } else { + LOG_DBG((0, "CSignerInfo::verifySignature", "Not verified 3")); } - } else { - LOG_ERR((0, "CSignerInfo::verifySignature", "RSA Signature not verified")); + } } - } catch(...) { - LOG_ERR((0, "CSignerInfo::verifySignature", "Unexpected Exception")); + } + } else { + LOG_ERR( + (0, "CSignerInfo::verifySignature", "RSA Signature not verified")); } + } catch (...) { + LOG_ERR((0, "CSignerInfo::verifySignature", "Unexpected Exception")); + } - - return bitmask; + return bitmask; } -CCertificate CSignerInfo::getSignatureCertificate(CSignerInfo& signature, CASN1SetOf& certificates) { - CIssuerAndSerialNumber issuerAndSerialNumber = - signature.getIssuerAndSerialNumber(); +CCertificate CSignerInfo::getSignatureCertificate(CSignerInfo& signature, + CASN1SetOf& certificates) { + CIssuerAndSerialNumber issuerAndSerialNumber = + signature.getIssuerAndSerialNumber(); - for(int i = 0; i < certificates.size(); i++) { - CCertificate cert = certificates.elementAt(i); - CName issuer = cert.getIssuer(); - CASN1Integer serialNumber = cert.getSerialNumber(); + for (int i = 0; i < certificates.size(); i++) { + CCertificate cert = certificates.elementAt(i); + CName issuer = cert.getIssuer(); + CASN1Integer serialNumber = cert.getSerialNumber(); - CIssuerAndSerialNumber issuerAndSerial(issuer, serialNumber, false); + CIssuerAndSerialNumber issuerAndSerial(issuer, serialNumber, false); - if(issuerAndSerial == issuerAndSerialNumber) { - return cert; - } + if (issuerAndSerial == issuerAndSerialNumber) { + return cert; } + } - throw -1; + throw -1; } diff --git a/libs/sign-sdk/src/ASN1/SignerInfo.h b/libs/sign-sdk/src/ASN1/SignerInfo.h index 315ecc9d..15966d24 100644 --- a/libs/sign-sdk/src/ASN1/SignerInfo.h +++ b/libs/sign-sdk/src/ASN1/SignerInfo.h @@ -1,6 +1,8 @@ // SignerInfo.h: interface for the CSignerInfo class. // +#include "ASN1/ASN1Setof.h" +#include "ASN1/Certificate.h" #if !defined(AFX_SIGNERINFO_H__ED6FFA3F_0A25_4A42_A3E5_BC704B9C25B3__INCLUDED_) #define AFX_SIGNERINFO_H__ED6FFA3F_0A25_4A42_A3E5_BC704B9C25B3__INCLUDED_ @@ -8,60 +10,60 @@ #pragma once #endif // _MSC_VER > 1000 -#include "ASN1Octetstring.h" #include "ASN1/ASN1Sequence.h" #include "ASN1/ASN1UTCTime.h" #include "ASN1/AlgorithmIdentifier.h" #include "ASN1/IssuerAndSerialNumber.h" #include "ASN1/TimeStampToken.h" +#include "ASN1Octetstring.h" #include "Sign/disigonsdk.h" class CSignerInfo : public CASN1Sequence { - public: - CSignerInfo(UUCBufferedReader& reader); + public: + CSignerInfo(UUCBufferedReader& reader); - CSignerInfo(const CASN1Object& signerInfo); + CSignerInfo(const CASN1Object& signerInfo); - CSignerInfo(const CIssuerAndSerialNumber& issuer, - const CAlgorithmIdentifier& digestAlgo, - const CAlgorithmIdentifier& encAlgo, - const CASN1OctetString& encDigest); + CSignerInfo(const CIssuerAndSerialNumber& issuer, + const CAlgorithmIdentifier& digestAlgo, + const CAlgorithmIdentifier& encAlgo, + const CASN1OctetString& encDigest); - void addAuthenticatedAttributes(const CASN1SetOf& attributes); - void addUnauthenticatedAttributes(const CASN1SetOf& attributes); + void addAuthenticatedAttributes(const CASN1SetOf& attributes); + void addUnauthenticatedAttributes(const CASN1SetOf& attributes); - CASN1OctetString getEncryptedDigest(); - CAlgorithmIdentifier getDigestAlgorithn(); - CIssuerAndSerialNumber getIssuerAndSerialNumber(); - CASN1SetOf getAuthenticatedAttributes(); - CASN1SetOf getUnauthenticatedAttributes(); + CASN1OctetString getEncryptedDigest(); + CAlgorithmIdentifier getDigestAlgorithn(); + CIssuerAndSerialNumber getIssuerAndSerialNumber(); + CASN1SetOf getAuthenticatedAttributes(); + CASN1SetOf getUnauthenticatedAttributes(); - CTimeStampToken getTimeStampToken(); - CASN1UTCTime getSigningTime(); - // CASN1ObjectIdentifier getSigningCertificateV2(); - CASN1OctetString getContentHash(); + CTimeStampToken getTimeStampToken(); + CASN1UTCTime getSigningTime(); + // CASN1ObjectIdentifier getSigningCertificateV2(); + CASN1OctetString getContentHash(); - CASN1SetOf getCountersignatures(); - void setCountersignatures(int index, CSignerInfo& countersignature); - void addCountersignatures(CSignerInfo& countersignature); - int getCountersignatureCount(); - bool hasTimeStampToken(); + CASN1SetOf getCountersignatures(); + void setCountersignatures(int index, CSignerInfo& countersignature); + void addCountersignatures(CSignerInfo& countersignature); + int getCountersignatureCount(); + bool hasTimeStampToken(); - int verifyCountersignature(int i, CASN1SetOf& certificates); - int verifyCountersignature(int i, CASN1SetOf& certificates, - const char* szDateTime, - REVOCATION_INFO* pRevocationInfo); + int verifyCountersignature(int i, CASN1SetOf& certificates); + int verifyCountersignature(int i, CASN1SetOf& certificates, + const char* szDateTime, + REVOCATION_INFO* pRevocationInfo); - void setTimeStampToken(CTimeStampToken& tst); + void setTimeStampToken(CTimeStampToken& tst); - virtual ~CSignerInfo(); + virtual ~CSignerInfo(); - static CCertificate getSignatureCertificate(CSignerInfo& signature, - CASN1SetOf& certificates); + static CCertificate getSignatureCertificate(CSignerInfo& signature, + CASN1SetOf& certificates); - static int verifySignature(CASN1OctetString& source, CSignerInfo& sinfo, - CASN1SetOf& certificates, const char* date, - REVOCATION_INFO* pRevocationInfo); + static int verifySignature(CASN1OctetString& source, CSignerInfo& sinfo, + CASN1SetOf& certificates, const char* date, + REVOCATION_INFO* pRevocationInfo); }; #endif // !defined(AFX_SIGNERINFO_H__ED6FFA3F_0A25_4A42_A3E5_BC704B9C25B3__INCLUDED_) diff --git a/libs/sign-sdk/src/ASN1/SubjectPublicKeyInfo.cpp b/libs/sign-sdk/src/ASN1/SubjectPublicKeyInfo.cpp index 205b1107..e3a30b52 100644 --- a/libs/sign-sdk/src/ASN1/SubjectPublicKeyInfo.cpp +++ b/libs/sign-sdk/src/ASN1/SubjectPublicKeyInfo.cpp @@ -6,22 +6,15 @@ // Construction/Destruction CSubjectPublicKeyInfo::CSubjectPublicKeyInfo(UUCBufferedReader& reader) - : CASN1Sequence(reader) { -} - + : CASN1Sequence(reader) {} CSubjectPublicKeyInfo::CSubjectPublicKeyInfo(const CASN1Object& subPubKey) - : CASN1Sequence(subPubKey) { -} + : CASN1Sequence(subPubKey) {} -CSubjectPublicKeyInfo::~CSubjectPublicKeyInfo() { - -} +CSubjectPublicKeyInfo::~CSubjectPublicKeyInfo() {} CAlgorithmIdentifier CSubjectPublicKeyInfo::getAlgorithmIdentifier() { - return elementAt(0); + return elementAt(0); } -CASN1BitString CSubjectPublicKeyInfo::getPublicKey() { - return elementAt(1); -} +CASN1BitString CSubjectPublicKeyInfo::getPublicKey() { return elementAt(1); } diff --git a/libs/sign-sdk/src/ASN1/SubjectPublicKeyInfo.h b/libs/sign-sdk/src/ASN1/SubjectPublicKeyInfo.h index 35a43d53..34e11953 100644 --- a/libs/sign-sdk/src/ASN1/SubjectPublicKeyInfo.h +++ b/libs/sign-sdk/src/ASN1/SubjectPublicKeyInfo.h @@ -9,21 +9,21 @@ #pragma once #endif // _MSC_VER > 1000 -#include "ASN1BitString.h" #include "ASN1/ASN1Sequence.h" #include "ASN1/AlgorithmIdentifier.h" +#include "ASN1BitString.h" class CSubjectPublicKeyInfo : public CASN1Sequence { - public: - CSubjectPublicKeyInfo(UUCBufferedReader& reader); + public: + CSubjectPublicKeyInfo(UUCBufferedReader& reader); - CSubjectPublicKeyInfo(const CASN1Object& obj); + CSubjectPublicKeyInfo(const CASN1Object& obj); - virtual ~CSubjectPublicKeyInfo(); + virtual ~CSubjectPublicKeyInfo(); - CAlgorithmIdentifier getAlgorithmIdentifier(); + CAlgorithmIdentifier getAlgorithmIdentifier(); - CASN1BitString getPublicKey(); + CASN1BitString getPublicKey(); }; #endif // !defined(AFX_SUBJECTPUBLICKEYINFO_H__93E2619B_704A_49A2_8DEA_DCCC521605CE__INCLUDED_) diff --git a/libs/sign-sdk/src/ASN1/TSTInfo.cpp b/libs/sign-sdk/src/ASN1/TSTInfo.cpp index 5ca69cd5..7c9ecb07 100644 --- a/libs/sign-sdk/src/ASN1/TSTInfo.cpp +++ b/libs/sign-sdk/src/ASN1/TSTInfo.cpp @@ -2,50 +2,38 @@ // #include "TSTInfo.h" -#include "ASN1OptionalField.h" -#include "ASN1Exception.h" + #include "ASN1/ASN1UTCTime.h" // Construction/Destruction -CTSTInfo::CTSTInfo(UUCBufferedReader& reader) - :CASN1Sequence(reader) { -} +CTSTInfo::CTSTInfo(UUCBufferedReader& reader) : CASN1Sequence(reader) {} -CTSTInfo::CTSTInfo(const CASN1Object& tstInfo) - :CASN1Sequence(tstInfo) { -} +CTSTInfo::CTSTInfo(const CASN1Object& tstInfo) : CASN1Sequence(tstInfo) {} -CTSTInfo::~CTSTInfo() { -} +CTSTInfo::~CTSTInfo() {} -CASN1Sequence CTSTInfo::getMessageImprint() { - return elementAt(2); -} +CASN1Sequence CTSTInfo::getMessageImprint() { return elementAt(2); } -CASN1UTCTime CTSTInfo::getUTCTime() { - return elementAt(4); -} +CASN1UTCTime CTSTInfo::getUTCTime() { return elementAt(4); } CAlgorithmIdentifier CTSTInfo::getDigestAlgorithn() { - CASN1Sequence messageImprint(elementAt(2)); - return (CAlgorithmIdentifier)messageImprint.elementAt(0); + CASN1Sequence messageImprint(elementAt(2)); + return (CAlgorithmIdentifier)messageImprint.elementAt(0); } -CASN1Integer CTSTInfo::getSerialNumber() { - return elementAt(3); -} +CASN1Integer CTSTInfo::getSerialNumber() { return elementAt(3); } CName CTSTInfo::getTSAName() { - int siz = size(); - for (int i = 1; i < siz; i++) { - CASN1Object obj = elementAt(i); - if (obj.getTag() == 0xA0) { - CASN1Sequence val(obj); - CASN1Sequence val1(val.elementAt(0)); - return val1.elementAt(0); - } + int siz = size(); + for (int i = 1; i < siz; i++) { + CASN1Object obj = elementAt(i); + if (obj.getTag() == 0xA0) { + CASN1Sequence val(obj); + CASN1Sequence val1(val.elementAt(0)); + return val1.elementAt(0); } + } - throw -1; + throw -1; } diff --git a/libs/sign-sdk/src/ASN1/TSTInfo.h b/libs/sign-sdk/src/ASN1/TSTInfo.h index 642860b2..278b298e 100644 --- a/libs/sign-sdk/src/ASN1/TSTInfo.h +++ b/libs/sign-sdk/src/ASN1/TSTInfo.h @@ -17,24 +17,24 @@ #include "Name.h" class CTSTInfo : public CASN1Sequence { - public: - CTSTInfo(UUCBufferedReader& reader); + public: + CTSTInfo(UUCBufferedReader& reader); - CTSTInfo(const CASN1Object& tstInfo); + CTSTInfo(const CASN1Object& tstInfo); - virtual ~CTSTInfo(); + virtual ~CTSTInfo(); - CASN1UTCTime getUTCTime(); + CASN1UTCTime getUTCTime(); - CASN1Integer getSerialNumber(); + CASN1Integer getSerialNumber(); - CAlgorithmIdentifier getDigestAlgorithn(); + CAlgorithmIdentifier getDigestAlgorithn(); - CASN1Sequence getMessageImprint(); + CASN1Sequence getMessageImprint(); - // N.B. il campo TSAName è opzionale. se non presente nel tstoken torna - // eccezione - CName getTSAName(); + // N.B. il campo TSAName è opzionale. se non presente nel tstoken torna + // eccezione + CName getTSAName(); }; #endif // !defined(AFX_TSTINFO_H__7B1086C5_9AEE_4973_8D52_FBB757D01E2A__INCLUDED_) diff --git a/libs/sign-sdk/src/ASN1/TimeStampData.cpp b/libs/sign-sdk/src/ASN1/TimeStampData.cpp index 9debc055..61deb707 100644 --- a/libs/sign-sdk/src/ASN1/TimeStampData.cpp +++ b/libs/sign-sdk/src/ASN1/TimeStampData.cpp @@ -7,7 +7,6 @@ * */ - /* ContentInfo ::= SEQUENCE { contentType ContentType, @@ -59,68 +58,65 @@ */ #include "TimeStampData.h" + #include "ASN1OptionalField.h" // Construction/Destruction CTimeStampData::CTimeStampData(UUCBufferedReader& reader) - :CContentInfo(reader) { - -} + : CContentInfo(reader) {} CTimeStampData::CTimeStampData(const CASN1Object& timeStampData) - :CContentInfo(timeStampData) { - -} + : CContentInfo(timeStampData) {} CTimeStampData::CTimeStampData(UUCByteArray& content, CTimeStampToken& tst) : CContentInfo(CContentType(szTimeStampDataOID)) { - CASN1Sequence timeStampData; - timeStampData.addElement(CASN1Integer(1)); // version - - timeStampData.addElement(CASN1OctetString(content)); // content + CASN1Sequence timeStampData; + timeStampData.addElement(CASN1Integer(1)); // version - CASN1Sequence evidence; - CASN1Sequence tsAndCrl; + timeStampData.addElement(CASN1OctetString(content)); // content - tsAndCrl.addElement(tst); - evidence.addElement(tsAndCrl); - timeStampData.addElement(CASN1OptionalField(evidence, 0)); + CASN1Sequence evidence; + CASN1Sequence tsAndCrl; + tsAndCrl.addElement(tst); + evidence.addElement(tsAndCrl); + timeStampData.addElement(CASN1OptionalField(evidence, 0)); - setContent(timeStampData); + setContent(timeStampData); } CTimeStampData::~CTimeStampData() { - //NSLog(@"~CASN1Object()"); + // NSLog(@"~CASN1Object()"); } int CTimeStampData::verify(REVOCATION_INFO* pRevocationInfo) { - return verify(NULL, pRevocationInfo); + return verify(NULL, pRevocationInfo); } -int CTimeStampData::verify(const char* szDateTime, REVOCATION_INFO* pRevocationInfo) { - CTimeStampToken tst(getTimeStampToken()); +int CTimeStampData::verify(const char* szDateTime, + REVOCATION_INFO* pRevocationInfo) { + CTimeStampToken tst(getTimeStampToken()); - return tst.verify(szDateTime, pRevocationInfo); + return tst.verify(szDateTime, pRevocationInfo); } CTimeStampToken CTimeStampData::getTimeStampToken() { - CASN1Sequence timeStampData(getContent()); + CASN1Sequence timeStampData(getContent()); - int size = timeStampData.size(); + int size = timeStampData.size(); - CASN1Sequence evidence(timeStampData.elementAt(size -1)); - CASN1Sequence tsAndCrl(evidence.elementAt(0)); + CASN1Sequence evidence(timeStampData.elementAt(size - 1)); + CASN1Sequence tsAndCrl(evidence.elementAt(0)); - CTimeStampToken tst(tsAndCrl.elementAt(0)); + CTimeStampToken tst(tsAndCrl.elementAt(0)); - return tst; + return tst; } CASN1OctetString CTimeStampData::getTimeStampDataContent() { - CASN1Sequence timeStampData(getContent()); + CASN1Sequence timeStampData(getContent()); - int size = timeStampData.size(); + int size = timeStampData.size(); - return timeStampData.elementAt(size - 2); + return timeStampData.elementAt(size - 2); } diff --git a/libs/sign-sdk/src/ASN1/TimeStampData.h b/libs/sign-sdk/src/ASN1/TimeStampData.h index ddc7a0e9..23068b96 100644 --- a/libs/sign-sdk/src/ASN1/TimeStampData.h +++ b/libs/sign-sdk/src/ASN1/TimeStampData.h @@ -7,9 +7,8 @@ * */ -#include "ASN1Octetstring.h" -#include "PKIStatusInfo.h" #include "ASN1/TimeStampToken.h" +#include "ASN1Octetstring.h" #include "Sign/disigonsdk.h" /* diff --git a/libs/sign-sdk/src/ASN1/TimeStampRequest.cpp b/libs/sign-sdk/src/ASN1/TimeStampRequest.cpp index 615103ba..e1899e6a 100644 --- a/libs/sign-sdk/src/ASN1/TimeStampRequest.cpp +++ b/libs/sign-sdk/src/ASN1/TimeStampRequest.cpp @@ -8,37 +8,36 @@ */ #include "TimeStampRequest.h" + +#include "ASN1/ASN1Octetstring.h" #include "ASN1Boolean.h" // Construction/Destruction CTimeStampRequest::CTimeStampRequest(UUCBufferedReader& reader) - :CASN1Sequence(reader) { - -} + : CASN1Sequence(reader) {} CTimeStampRequest::CTimeStampRequest(const CASN1Object& timeStampToken) - :CASN1Sequence(timeStampToken) { + : CASN1Sequence(timeStampToken) {} -} - -CTimeStampRequest::CTimeStampRequest(const char* szHashAlgoOID, UUCByteArray& digest, const char* szPolicyOID, CASN1Integer& nounce) +CTimeStampRequest::CTimeStampRequest(const char* szHashAlgoOID, + UUCByteArray& digest, + const char* szPolicyOID, + CASN1Integer& nounce) : CASN1Sequence() { - addElement(CASN1Integer(1)); - CASN1Sequence messageImprint; + addElement(CASN1Integer(1)); + CASN1Sequence messageImprint; - messageImprint.addElement(CAlgorithmIdentifier(szHashAlgoOID)); - messageImprint.addElement(CASN1OctetString(digest)); - addElement(messageImprint); + messageImprint.addElement(CAlgorithmIdentifier(szHashAlgoOID)); + messageImprint.addElement(CASN1OctetString(digest)); + addElement(messageImprint); - if(szPolicyOID != NULL && strlen(szPolicyOID) > 0) { - CASN1ObjectIdentifier policyOid(szPolicyOID);//"1.3.6.1.4.1.29741.1.1.6"); - addElement(policyOid); - } + if (szPolicyOID != NULL && strlen(szPolicyOID) > 0) { + CASN1ObjectIdentifier policyOid(szPolicyOID); //"1.3.6.1.4.1.29741.1.1.6"); + addElement(policyOid); + } - addElement(nounce); - addElement(CASN1Boolean(true)); // certReq + addElement(nounce); + addElement(CASN1Boolean(true)); // certReq } -CTimeStampRequest::~CTimeStampRequest() { - -} +CTimeStampRequest::~CTimeStampRequest() {} diff --git a/libs/sign-sdk/src/ASN1/TimeStampRequest.h b/libs/sign-sdk/src/ASN1/TimeStampRequest.h index 0e5a4caf..44cdc7bb 100644 --- a/libs/sign-sdk/src/ASN1/TimeStampRequest.h +++ b/libs/sign-sdk/src/ASN1/TimeStampRequest.h @@ -10,13 +10,13 @@ #include "ASN1/TimeStampToken.h" class CTimeStampRequest : public CASN1Sequence { - public: - CTimeStampRequest(UUCBufferedReader& reader); + public: + CTimeStampRequest(UUCBufferedReader& reader); - CTimeStampRequest(const CASN1Object& timeStampToken); + CTimeStampRequest(const CASN1Object& timeStampToken); - CTimeStampRequest(const char* szHashAlgoOID, UUCByteArray& digest, - const char* szPolicyOID, CASN1Integer& nounce); + CTimeStampRequest(const char* szHashAlgoOID, UUCByteArray& digest, + const char* szPolicyOID, CASN1Integer& nounce); - virtual ~CTimeStampRequest(); + virtual ~CTimeStampRequest(); }; diff --git a/libs/sign-sdk/src/ASN1/TimeStampResponse.cpp b/libs/sign-sdk/src/ASN1/TimeStampResponse.cpp index c9fd93b2..441e3cc6 100644 --- a/libs/sign-sdk/src/ASN1/TimeStampResponse.cpp +++ b/libs/sign-sdk/src/ASN1/TimeStampResponse.cpp @@ -8,44 +8,29 @@ */ #include "TimeStampResponse.h" + #include "ASN1/TimeStampToken.h" -#include "ASN1/ContentInfo.h" -#include "ContentType.h" // Construction/Destruction CTimeStampResponse::CTimeStampResponse(UUCBufferedReader& reader) - : CASN1Sequence(reader) { - -} + : CASN1Sequence(reader) {} CTimeStampResponse::CTimeStampResponse(const CASN1Object& timeStampresponse) - : CASN1Sequence(timeStampresponse) { - -} + : CASN1Sequence(timeStampresponse) {} CTimeStampResponse::CTimeStampResponse(const BYTE* content, int length) - : CASN1Sequence(content, length) { - -} - -CTimeStampResponse::~CTimeStampResponse() { + : CASN1Sequence(content, length) {} -} +CTimeStampResponse::~CTimeStampResponse() {} -CTimeStampToken CTimeStampResponse::getTimeStampToken() { - return elementAt(1); -} +CTimeStampToken CTimeStampResponse::getTimeStampToken() { return elementAt(1); } -CPKIStatusInfo CTimeStampResponse::getPKIStatusInfo() { - return elementAt(0); -} +CPKIStatusInfo CTimeStampResponse::getPKIStatusInfo() { return elementAt(0); } -int CTimeStampResponse::verify() { - return verify(NULL); -} +int CTimeStampResponse::verify() { return verify(NULL); } int CTimeStampResponse::verify(const char* szDateTime) { - CTimeStampToken tst(elementAt(1)); - return tst.verify(szDateTime, NULL); + CTimeStampToken tst(elementAt(1)); + return tst.verify(szDateTime, NULL); } diff --git a/libs/sign-sdk/src/ASN1/TimeStampResponse.h b/libs/sign-sdk/src/ASN1/TimeStampResponse.h index 17c6989d..19df2bd9 100644 --- a/libs/sign-sdk/src/ASN1/TimeStampResponse.h +++ b/libs/sign-sdk/src/ASN1/TimeStampResponse.h @@ -7,24 +7,24 @@ * */ -#include "PKIStatusInfo.h" #include "ASN1/TimeStampToken.h" +#include "PKIStatusInfo.h" class CTimeStampResponse : public CASN1Sequence { - public: - CTimeStampResponse(UUCBufferedReader& reader); + public: + CTimeStampResponse(UUCBufferedReader& reader); - CTimeStampResponse(const CASN1Object& timeStampresponse); + CTimeStampResponse(const CASN1Object& timeStampresponse); - CTimeStampResponse(const BYTE* content, int length); + CTimeStampResponse(const BYTE* content, int length); - virtual ~CTimeStampResponse(); + virtual ~CTimeStampResponse(); - CTimeStampToken getTimeStampToken(); + CTimeStampToken getTimeStampToken(); - CPKIStatusInfo getPKIStatusInfo(); + CPKIStatusInfo getPKIStatusInfo(); - int verify(const char* szDateTime); + int verify(const char* szDateTime); - int verify(); + int verify(); }; diff --git a/libs/sign-sdk/src/ASN1/TimeStampToken.cpp b/libs/sign-sdk/src/ASN1/TimeStampToken.cpp index 4d95255d..9de73dd8 100644 --- a/libs/sign-sdk/src/ASN1/TimeStampToken.cpp +++ b/libs/sign-sdk/src/ASN1/TimeStampToken.cpp @@ -1,50 +1,45 @@ // TimeStampToken.cpp: implementation of the CTimeStampToken class. #include "ASN1/TimeStampToken.h" + #include "ASN1/ContentInfo.h" -#include "ContentType.h" -#include "SignedData.h" #include "ASN1Octetstring.h" +#include "SignedData.h" // Construction/Destruction CTimeStampToken::CTimeStampToken(UUCBufferedReader& reader) - :CContentInfo(reader) { - -} + : CContentInfo(reader) {} CTimeStampToken::CTimeStampToken(const CASN1Object& timeStampToken) - :CContentInfo(timeStampToken) { + : CContentInfo(timeStampToken) {} -} - -CTimeStampToken::~CTimeStampToken() { - -} +CTimeStampToken::~CTimeStampToken() {} CTSTInfo CTimeStampToken::getTSTInfo() { - CSignedData signedData(getContent()); + CSignedData signedData(getContent()); - CContentInfo contentInfo(signedData.getContentInfo()); + CContentInfo contentInfo(signedData.getContentInfo()); - CASN1OctetString tst(contentInfo.getContent()); + CASN1OctetString tst(contentInfo.getContent()); - UUCBufferedReader reader(*tst.getValue()); - return CTSTInfo(reader); + UUCBufferedReader reader(*tst.getValue()); + return CTSTInfo(reader); } int CTimeStampToken::verify(REVOCATION_INFO* pRevocationInfo) { - CSignedData signedData(getContent()); + CSignedData signedData(getContent()); - return signedData.verify(0, NULL, pRevocationInfo); + return signedData.verify(0, NULL, pRevocationInfo); } -int CTimeStampToken::verify(const char* szDateTime, REVOCATION_INFO* pRevocationInfo) { - CSignedData signedData(getContent()); +int CTimeStampToken::verify(const char* szDateTime, + REVOCATION_INFO* pRevocationInfo) { + CSignedData signedData(getContent()); - return signedData.verify(0, szDateTime, pRevocationInfo); + return signedData.verify(0, szDateTime, pRevocationInfo); } CASN1SetOf CTimeStampToken::getCertificates() { - CSignedData signedData(getContent()); + CSignedData signedData(getContent()); - return signedData.getCertificates(); + return signedData.getCertificates(); } diff --git a/libs/sign-sdk/src/ASN1/TimeStampToken.h b/libs/sign-sdk/src/ASN1/TimeStampToken.h index 6ff2349c..e74a6187 100644 --- a/libs/sign-sdk/src/ASN1/TimeStampToken.h +++ b/libs/sign-sdk/src/ASN1/TimeStampToken.h @@ -3,6 +3,8 @@ #ifndef _TIMESTAMPTOKEN_H #define _TIMESTAMPTOKEN_H +#include "ASN1/ASN1Setof.h" +#include "Sign/disigonsdk.h" #if !defined( \ AFX_TIMESTAMPTOKEN_H__2D568B92_6258_4EBE_B1CA_17F3746F3801__INCLUDED_) #define AFX_TIMESTAMPTOKEN_H__2D568B92_6258_4EBE_B1CA_17F3746F3801__INCLUDED_ @@ -12,24 +14,22 @@ #endif // _MSC_VER > 1000 #include "ASN1/ContentInfo.h" -#include "SignedData.h" #include "TSTInfo.h" -#include "ASN1/TimeStampToken.h" class CTimeStampToken : public CContentInfo { - public: - CTimeStampToken(UUCBufferedReader& reader); + public: + CTimeStampToken(UUCBufferedReader& reader); - CTimeStampToken(const CASN1Object& timeStampToken); + CTimeStampToken(const CASN1Object& timeStampToken); - CTSTInfo getTSTInfo(); + CTSTInfo getTSTInfo(); - virtual ~CTimeStampToken(); + virtual ~CTimeStampToken(); - int verify(REVOCATION_INFO* pRevocationInfo); - int verify(const char* szDateTime, REVOCATION_INFO* pRevocationInfo); + int verify(REVOCATION_INFO* pRevocationInfo); + int verify(const char* szDateTime, REVOCATION_INFO* pRevocationInfo); - CASN1SetOf getCertificates(); + CASN1SetOf getCertificates(); }; #endif // !defined(AFX_TIMESTAMPTOKEN_H__2D568B92_6258_4EBE_B1CA_17F3746F3801__INCLUDED_) diff --git a/libs/sign-sdk/src/ASN1/UUCBufferedReader.cpp b/libs/sign-sdk/src/ASN1/UUCBufferedReader.cpp index ffd0a202..d9458149 100644 --- a/libs/sign-sdk/src/ASN1/UUCBufferedReader.cpp +++ b/libs/sign-sdk/src/ASN1/UUCBufferedReader.cpp @@ -17,111 +17,106 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -#include "Sign/definitions.h" #include "UUCBufferedReader.h" + #include -#define MAX_BUF 2000 -#define MAX_STACK_SIZE 100 +#define MAX_BUF 2000 +#define MAX_STACK_SIZE 100 // Construction/Destruction UUCBufferedReader::UUCBufferedReader(const UUCByteArray& buffer) { - m_pbtBuffer = (BYTE*)buffer.getContent(); - m_nBufLen = buffer.getLength(); - - m_nBufPos = 0; - m_nIndex = 0; - m_bEOF = true; - m_pnStack = (unsigned int*)realloc(NULL, MAX_STACK_SIZE * sizeof(unsigned int)); - m_nStackSize = MAX_STACK_SIZE; - m_nTop = -1; + m_pbtBuffer = (BYTE*)buffer.getContent(); + m_nBufLen = buffer.getLength(); + + m_nBufPos = 0; + m_nIndex = 0; + m_bEOF = true; + m_pnStack = + (unsigned int*)realloc(NULL, MAX_STACK_SIZE * sizeof(unsigned int)); + m_nStackSize = MAX_STACK_SIZE; + m_nTop = -1; } UUCBufferedReader::UUCBufferedReader(const BYTE* pbtBuffer, int len) : m_pbtBuffer(NULL) { - m_pbtBuffer = (BYTE*)pbtBuffer; - m_nBufLen = len; - - m_nBufPos = 0; - m_nIndex = 0; - m_bEOF = true; - m_pnStack = (unsigned int*)realloc(NULL, MAX_STACK_SIZE * sizeof(unsigned int)); - m_nStackSize = MAX_STACK_SIZE; - m_nTop = -1; + m_pbtBuffer = (BYTE*)pbtBuffer; + m_nBufLen = len; + + m_nBufPos = 0; + m_nIndex = 0; + m_bEOF = true; + m_pnStack = + (unsigned int*)realloc(NULL, MAX_STACK_SIZE * sizeof(unsigned int)); + m_nStackSize = MAX_STACK_SIZE; + m_nTop = -1; } - - - UUCBufferedReader::~UUCBufferedReader() { - try { - free(m_pnStack); - } catch(...) { - } + try { + free(m_pnStack); + } catch (...) { + } } unsigned int UUCBufferedReader::read(BYTE* pbtBuffer, unsigned int nLen) { - int nRead = 0; - if (m_nIndex + nLen > m_nBufLen) { - if (!m_bEOF) { - return read(pbtBuffer, nLen); - } else { - memcpy(pbtBuffer, m_pbtBuffer + m_nIndex, (m_nBufLen - m_nIndex)); - nRead = (m_nBufLen - m_nIndex); - m_nIndex += nRead; - } + int nRead = 0; + if (m_nIndex + nLen > m_nBufLen) { + if (!m_bEOF) { + return read(pbtBuffer, nLen); } else { - memcpy(pbtBuffer, m_pbtBuffer + m_nIndex, nLen); - nRead = nLen; - m_nIndex += nRead; + memcpy(pbtBuffer, m_pbtBuffer + m_nIndex, (m_nBufLen - m_nIndex)); + nRead = (m_nBufLen - m_nIndex); + m_nIndex += nRead; } + } else { + memcpy(pbtBuffer, m_pbtBuffer + m_nIndex, nLen); + nRead = nLen; + m_nIndex += nRead; + } - return nRead; + return nRead; } unsigned int UUCBufferedReader::read(UUCByteArray& byteArray) { - BYTE pbtBuf[255]; - unsigned int nRead = 0; - unsigned int nCount = 0; - while ((nRead = read(pbtBuf, 255)) != 0) { - byteArray.append(pbtBuf, nRead); - nCount += nRead; - } - - nCount += nRead; + BYTE pbtBuf[255]; + unsigned int nRead = 0; + unsigned int nCount = 0; + while ((nRead = read(pbtBuf, 255)) != 0) { byteArray.append(pbtBuf, nRead); + nCount += nRead; + } - return nCount; + nCount += nRead; + byteArray.append(pbtBuf, nRead); + + return nCount; } void UUCBufferedReader::mark() { - m_nTop++; - if (m_nTop >= m_nStackSize) { - m_nStackSize += MAX_STACK_SIZE; - m_pnStack = (unsigned int*) realloc(m_pnStack, m_nStackSize * sizeof(unsigned int)); - } - - m_pnStack[m_nTop] = m_nIndex; + m_nTop++; + if (m_nTop >= m_nStackSize) { + m_nStackSize += MAX_STACK_SIZE; + m_pnStack = + (unsigned int*)realloc(m_pnStack, m_nStackSize * sizeof(unsigned int)); + } + + m_pnStack[m_nTop] = m_nIndex; } void UUCBufferedReader::releaseMark() { - if (m_nTop > 0) { - m_nTop--; - } + if (m_nTop > 0) { + m_nTop--; + } } void UUCBufferedReader::reset() { - if (m_nTop > -1) { - m_nIndex = m_pnStack[m_nTop]; - m_nTop--; - } + if (m_nTop > -1) { + m_nIndex = m_pnStack[m_nTop]; + m_nTop--; + } } +unsigned int UUCBufferedReader::getPosition() { return m_nIndex; } -unsigned int UUCBufferedReader::getPosition() { - return m_nIndex; -} - -void UUCBufferedReader::setPosition(unsigned int index) { - m_nIndex = index; -} +void UUCBufferedReader::setPosition(unsigned int index) { m_nIndex = index; } diff --git a/libs/sign-sdk/src/ASN1/UUCBufferedReader.h b/libs/sign-sdk/src/ASN1/UUCBufferedReader.h index 20a6d9bd..edaefbc8 100644 --- a/libs/sign-sdk/src/ASN1/UUCBufferedReader.h +++ b/libs/sign-sdk/src/ASN1/UUCBufferedReader.h @@ -19,7 +19,6 @@ */ #pragma once -#include #include "Util/UUCByteArray.h" diff --git a/libs/sign-sdk/src/CounterSignatureGenerator.cpp b/libs/sign-sdk/src/CounterSignatureGenerator.cpp index 261996c7..1b8f9af8 100644 --- a/libs/sign-sdk/src/CounterSignatureGenerator.cpp +++ b/libs/sign-sdk/src/CounterSignatureGenerator.cpp @@ -95,7 +95,8 @@ void CounterSignatureGenerator::toByteArray(UUCByteArray& pkcs7SignedData) { m_signerInfos, m_certificates); // Infine crea ContentInfo - CContentInfo contentInfo(szSignedDataOID, signedData); + char signedDataOID[] = szSignedDataOID; + CContentInfo contentInfo(signedDataOID, signedData); contentInfo.toByteArray(pkcs7SignedData); } diff --git a/libs/sign-sdk/src/PCSC.cpp b/libs/sign-sdk/src/PCSC.cpp index 1e11552e..3fdcffb9 100644 --- a/libs/sign-sdk/src/PCSC.cpp +++ b/libs/sign-sdk/src/PCSC.cpp @@ -1,6 +1,9 @@ +#include #include #include +#include "Sign/definitions.h" + struct transData { SCARDCONTEXT context; bool started; @@ -16,7 +19,6 @@ safeTransaction::safeTransaction(safeConnection &conn, DWORD dwDisposition) { td->started = false; auto thread = std::thread([td]() { for (int i = 0; i < 10; i++) { - Sleep(500); if (td->started) { return 0; } diff --git a/libs/sign-sdk/src/RSA/des.h b/libs/sign-sdk/src/RSA/des.h index cabd7100..f7503141 100644 --- a/libs/sign-sdk/src/RSA/des.h +++ b/libs/sign-sdk/src/RSA/des.h @@ -56,11 +56,11 @@ typedef unsigned short int UINT2; /* UINT4 defines a four byte word */ typedef unsigned long int UINT4; -//#if PROTOTYPES +// #if PROTOTYPES #define PROTO_LIST(list) list -//#else -//#define PROTO_LIST(list) () -//#endif +// #else +// #define PROTO_LIST(list) () +// #endif #define R_memset(x, y, z) memset(x, y, z) #define R_memcpy(x, y, z) memcpy(x, y, z) diff --git a/libs/sign-sdk/src/RSA/nn.h b/libs/sign-sdk/src/RSA/nn.h index 8ba2ba6f..6a4d98f5 100644 --- a/libs/sign-sdk/src/RSA/nn.h +++ b/libs/sign-sdk/src/RSA/nn.h @@ -57,7 +57,7 @@ typedef UINT2 NN_HALF_DIGIT; /* Macros. */ -#define LOW_HALF(x) ((x)&MAX_NN_HALF_DIGIT) +#define LOW_HALF(x) ((x) & MAX_NN_HALF_DIGIT) #define HIGH_HALF(x) (((x) >> NN_HALF_DIGIT_BITS) & MAX_NN_HALF_DIGIT) #define TO_HIGH_HALF(x) (((NN_DIGIT)(x)) << NN_HALF_DIGIT_BITS) #define DIGIT_MSB(x) (unsigned int)(((x) >> (NN_DIGIT_BITS - 1)) & 1) diff --git a/libs/sign-sdk/src/RSA/rsa.h b/libs/sign-sdk/src/RSA/rsa.h index 8d21eb9a..8bb0deee 100644 --- a/libs/sign-sdk/src/RSA/rsa.h +++ b/libs/sign-sdk/src/RSA/rsa.h @@ -18,6 +18,7 @@ Revision 1.00 - JSAK. */ +#include "RSA/rsaeuro.h" #ifdef __cplusplus extern "C" { #endif @@ -25,15 +26,12 @@ extern "C" { // int RSAPublicEncrypt PROTO_LIST ((unsigned char *, unsigned int *, unsigned // char *, unsigned int, // R_RSA_PUBLIC_KEY *, R_RANDOM_STRUCT *)); -int RSAPrivateEncrypt PROTO_LIST((unsigned char *, unsigned int *, - unsigned char *, unsigned int, - R_RSA_PRIVATE_KEY *)); -int RSAPublicDecrypt PROTO_LIST((unsigned char *, unsigned int *, - unsigned char *, unsigned int, - R_RSA_PUBLIC_KEY *)); -int RSAPrivateDecrypt PROTO_LIST((unsigned char *, unsigned int *, - unsigned char *, unsigned int, - R_RSA_PRIVATE_KEY *)); +int RSAPrivateEncrypt(unsigned char *, unsigned int *, unsigned char *, + unsigned int, R_RSA_PRIVATE_KEY *); +int RSAPublicDecrypt(unsigned char *, unsigned int *, unsigned char *, + unsigned int, R_RSA_PUBLIC_KEY *); +int RSAPrivateDecrypt(unsigned char *, unsigned int *, unsigned char *, + unsigned int, R_RSA_PRIVATE_KEY *); #ifdef __cplusplus } diff --git a/libs/sign-sdk/src/SignatureGenerator.cpp b/libs/sign-sdk/src/SignatureGenerator.cpp index 8cc29b81..8347d9b8 100644 --- a/libs/sign-sdk/src/SignatureGenerator.cpp +++ b/libs/sign-sdk/src/SignatureGenerator.cpp @@ -3,8 +3,6 @@ #include -#include - #include "ASN1/ASN1ObjectIdentifier.h" #include "ASN1/ASN1Octetstring.h" #include "ASN1/ASN1Sequence.h" @@ -338,7 +336,8 @@ long CSignatureGenerator::Generate(UUCByteArray& pkcs7SignedData, LOG_DBG((0, "CSignatureGenerator::Generate", "ContentInfo")); // Infine crea ContentInfo - CContentInfo contentInfo(szSignedDataOID, *pSignedData); + char signedDataOID[] = szSignedDataOID; + CContentInfo contentInfo(signedDataOID, *pSignedData); pkcs7SignedData.removeAll(); contentInfo.toByteArray(pkcs7SignedData); diff --git a/libs/sign-sdk/src/SignedDataGeneratorEx.cpp b/libs/sign-sdk/src/SignedDataGeneratorEx.cpp index 647acf83..b05f31b2 100644 --- a/libs/sign-sdk/src/SignedDataGeneratorEx.cpp +++ b/libs/sign-sdk/src/SignedDataGeneratorEx.cpp @@ -142,7 +142,8 @@ void SignedDataGeneratorEx::toByteArray(UUCByteArray& pkcs7SignedData) { } // Infine crea ContentInfo - CContentInfo contentInfo(szSignedDataOID, *pSignedData); + char signedDataOID[] = szSignedDataOID; + CContentInfo contentInfo(signedDataOID, *pSignedData); contentInfo.toByteArray(pkcs7SignedData); diff --git a/libs/sign-sdk/src/SignedDocument.cpp b/libs/sign-sdk/src/SignedDocument.cpp index 5b7c586b..2f279f17 100644 --- a/libs/sign-sdk/src/SignedDocument.cpp +++ b/libs/sign-sdk/src/SignedDocument.cpp @@ -8,17 +8,14 @@ */ #include "SignedDocument.h" -#include "ASN1/ASN1Object.h" -#include "ASN1/UUCBufferedReader.h" -#include "ASN1/RSAPublicKey.h" -#include "RSA/rsaeuro.h" -#include "RSA/rsa.h" -#include "RSA/sha2.h" -#include "ASN1/DigestInfo.h" + #include + +#include "ASN1/ASN1Object.h" #include "ASN1/Certificate.h" -#include "ASN1/Crl.h" +#include "ASN1/UUCBufferedReader.h" #include "Base64.h" +#include "RSA/rsa.h" #include "UUCLogger.h" USE_LOG; @@ -149,7 +146,6 @@ int CSignedDocument::verify(int i, const char* dateTime, // attribute is available, that the certificate was valid at the time the // signature was generated. - return m_pSignedData->verify(i, dateTime, pRevocationInfo); }