From 1922d8c2cf7dcaacafd17394849577794b1f10eb Mon Sep 17 00:00:00 2001 From: open-license-manager <rillf@maildrop.cc> Date: 周六, 13 9月 2014 06:24:55 +0800 Subject: [PATCH] signature windows --- /dev/null | 33 ---- src/bootstrap/win/CryptoHelper.cpp | 6 src/license-generator/win/LicenseSigner.cpp | 351 ++++++++++++++++++++++++++++++++++++++----- src/license-generator/license-generator.cpp | 33 +-- src/library/os/win/os-win.c | 2 src/license-generator/license-generator.h | 2 src/license-generator/LicenseSigner.h | 5 src/library/base/logger.h | 14 + 8 files changed, 341 insertions(+), 105 deletions(-) diff --git a/src/bootstrap/bootstrap_win.cpp b/src/bootstrap/bootstrap_win.cpp deleted file mode 100644 index 2dc4dc5..0000000 --- a/src/bootstrap/bootstrap_win.cpp +++ /dev/null @@ -1,33 +0,0 @@ -/* - * boostrap.c - * - * Created on: Apr 5, 2014 - * Author: devel - */ - -#include <string> - - -using namespace std; - - -void print_usage() { - printf("usage: bootstrap private-fname public-fname\n"); -} - -int main(int argc, char** argv) { - - if (argc != 3) { - print_usage(); - exit(2); - } else { - printf("********************************************\n"); - printf("* Bootstrap!!! *\n"); - printf("********************************************\n"); - - } - string private_fname = string(argv[1]); - string public_fname(argv[2]); - - return 0; -} diff --git a/src/bootstrap/win/CryptoHelper.cpp b/src/bootstrap/win/CryptoHelper.cpp index 27c528a..50f337b 100644 --- a/src/bootstrap/win/CryptoHelper.cpp +++ b/src/bootstrap/win/CryptoHelper.cpp @@ -1,6 +1,6 @@ #include "CryptoHelper.h" // The RSA public-key key exchange algorithm -#define ENCRYPT_ALGORITHM CALG_RSA_KEYX +#define ENCRYPT_ALGORITHM CALG_RSA_SIGN // The high order WORD 0x0200 (decimal 512) // determines the key length in bits. #define KEYLENGTH 0x02000000 @@ -53,7 +53,7 @@ wszContainerName, MS_ENHANCED_PROV, PROV_RSA_FULL, - CRYPT_MACHINE_KEYSET)) + 0)) { // An error occurred in acquiring the context. This could mean // that the key container requested does not exist. In this case, @@ -64,7 +64,7 @@ if(!CryptAcquireContext(&m_hCryptProv, wszContainerName, MS_ENHANCED_PROV, PROV_RSA_FULL, - CRYPT_MACHINE_KEYSET|CRYPT_NEWKEYSET)) + CRYPT_NEWKEYSET)) { dwErrCode = GetLastError(); return HRESULT_FROM_WIN32(dwErrCode); diff --git a/src/library/base/logger.h b/src/library/base/logger.h index a35afd6..aa7a1f7 100644 --- a/src/library/base/logger.h +++ b/src/library/base/logger.h @@ -3,6 +3,10 @@ #ifndef LOG_ENABLED #include <errno.h> +#ifdef __cplusplus +extern "C" { +#endif + #define clean_errno() (errno == 0 ? "None" : strerror(errno)) #ifdef NDEBUG @@ -10,9 +14,9 @@ #else #define LOG_DEBUG(M,...) #endif -#define LOG_INFO(M, ...) _log("[INFO] %s (%s:%d) " M "\n", __FILE__, __LINE__, ##__VA_ARGS__) -#define LOG_WARN(M, ...) _log("[WARN] %s (%s:%d: errno: %s) " M "\n", __FILE__, __LINE__, clean_errno(), ##__VA_ARGS__) -#define LOG_ERROR(M, ...) _log("[ERROR] %s (%s:%d: errno: %s) " M "\n", __FILE__, __LINE__, clean_errno(), ##__VA_ARGS__) +#define LOG_INFO(M, ...) _log("[INFO] (%s:%d) " M "\n", __FILE__, __LINE__, ##__VA_ARGS__) +#define LOG_WARN(M, ...) _log("[WARN] (%s:%d: errno: %s) " M "\n", __FILE__, __LINE__, clean_errno(), ##__VA_ARGS__) +#define LOG_ERROR(M, ...) _log("[ERROR] (%s:%d: errno: %s) " M "\n", __FILE__, __LINE__, clean_errno(), ##__VA_ARGS__) #else #define LOG_DEBUG(M,...) @@ -23,4 +27,8 @@ void _log(char* format, ...); void _shutdown_log(); + +#ifdef __cplusplus +} +#endif #endif diff --git a/src/library/os/win/os-win.c b/src/library/os/win/os-win.c index e7bb77a..6fd115d 100644 --- a/src/library/os/win/os-win.c +++ b/src/library/os/win/os-win.c @@ -1,6 +1,6 @@ #include <Windows.h> #include <iphlpapi.h> -//definition of size_t +//definition of size_t #include <stdlib.h> #include "../../base/logger.h" #include"../os.h" diff --git a/src/license-generator/LicenseSigner.h b/src/license-generator/LicenseSigner.h index 899960e..f00b3ad 100644 --- a/src/license-generator/LicenseSigner.h +++ b/src/license-generator/LicenseSigner.h @@ -13,7 +13,12 @@ namespace license { class LicenseSigner { +#ifdef __unix__ const string privateKey; +#else + HCRYPTPROV hProv; + HCRYPTKEY hPubKey; +#endif string signString(const string& license); string Opensslb64Encode(size_t slen, unsigned char* signature); diff --git a/src/license-generator/license-generator.cpp b/src/license-generator/license-generator.cpp index 466d5d7..846d5d9 100644 --- a/src/license-generator/license-generator.cpp +++ b/src/license-generator/license-generator.cpp @@ -81,7 +81,7 @@ time_t curtime = time(NULL); strftime(curdate, 20, "%Y-%m-%d", localtime(&curtime)); begin_date.assign(curdate); - } catch (invalid_argument &e) { + } catch (const invalid_argument &e) { cerr << endl << "End date not recognized: " << dt_end << " Please enter a valid date in format YYYYMMDD" << endl; exit(2); @@ -189,29 +189,24 @@ } -const std::locale formats[] = { std::locale(std::locale::classic(), - new bt::time_input_facet("%Y-%m-%d")), // -std::locale(std::locale::classic(), new bt::time_input_facet("%Y/%m/%d")), // -std::locale(std::locale::classic(), new bt::time_input_facet("%Y%m%d")) }; -const size_t formats_n = sizeof(formats) / sizeof(formats[0]); +const std::string formats[] = { "%4u-%2u-%2u","%4u/%2u/%2u","%4u%2u%2u" }; +const size_t formats_n = 3; -string LicenseGenerator::normalize_date(const std::string& s) { - bt::ptime pt; - for (size_t i = 0; i < formats_n; ++i) { - std::istringstream is(s); - is.imbue(formats[i]); - is >> pt; - if (pt != bt::ptime()) { - break; +string LicenseGenerator::normalize_date(const char * s) { + unsigned int year, month, day; + int chread; + bool found = false; + for (size_t i = 0; i < formats_n &&!found; ++i) { + chread = sscanf(s, formats[i].c_str(),&year, &month, &day); + if (chread == 3){ + found = true; } } - if (pt == bt::ptime()) { - throw invalid_argument(string("Date not regognized") + s); + if (!found){ + throw invalid_argument("Date not recognized."); } ostringstream oss; - bt::time_facet *facet = new bt::time_facet("%Y-%m-%d"); - oss.imbue(locale(cout.getloc(), facet)); - oss << pt; + oss << year << "-"<<setfill('0') << std::setw(2)<< month<<"-"<<day; //delete (facet); return oss.str(); } diff --git a/src/license-generator/license-generator.h b/src/license-generator/license-generator.h index 612c9a6..e062e38 100644 --- a/src/license-generator/license-generator.h +++ b/src/license-generator/license-generator.h @@ -26,7 +26,7 @@ static vector<FullLicenseInfo> parseLicenseInfo(po::variables_map vm); static void generateAndOutputLicenses(const po::variables_map& vm, ostream& outputFile); - static string normalize_date(const std::string& s); + static string normalize_date(const char * s); public: static int generateLicense(int argc, const char** argv); }; diff --git a/src/license-generator/win/LicenseSigner.cpp b/src/license-generator/win/LicenseSigner.cpp index 4b104a9..c6aca67 100644 --- a/src/license-generator/win/LicenseSigner.cpp +++ b/src/license-generator/win/LicenseSigner.cpp @@ -1,45 +1,306 @@ -/* - * LicenseSigner.cpp (Windows) - * - * Created on: Apr 6, 2014 - * Author: devel - */ - -#include "../LicenseSigner.h" -#include "../private-key.h" -#include <stdexcept> -#include <string.h> -#include <iostream> -#include <cmath> - -namespace license { -using namespace std; - -LicenseSigner::LicenseSigner() { - os_initialize(); -} - -LicenseSigner::LicenseSigner(const std::string& alternatePrimaryKey) { - os_initialize(); -} - -string LicenseSigner::Opensslb64Encode(size_t slen, unsigned char* signature) { - - return NULL; -} - -string LicenseSigner::signString(const string& license) { - return NULL; -} - -void LicenseSigner::signLicense(FullLicenseInfo& licenseInfo) { - string license = licenseInfo.printForSign(); - string signature = signString(license); - licenseInfo.license_signature = signature; -} - -LicenseSigner::~LicenseSigner() { - -} - -} /* namespace license */ +/* + * LicenseSigner.cpp (Windows) + * + * Created on: Apr 6, 2014 + * Author: devel + */ + +#include <stdexcept> +#include <string.h> +#include <iostream> +#include <cmath> + +#pragma comment(lib, "crypt32.lib") + +#include <stdio.h> +#include <windows.h> +#include <Wincrypt.h> +#define MY_ENCODING_TYPE (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING) +#include "../private-key.h" + +#include "../LicenseSigner.h" +#include "../../library/base/logger.h" +namespace license { + using namespace std; + + LicenseSigner::LicenseSigner() { + os_initialize(); + + if (CryptAcquireContext( + &hProv, + "license-manager2++", + MS_ENHANCED_PROV, + PROV_RSA_FULL, //CRYPT_NEWKEYSET + 0)) { + LOG_DEBUG("CSP context acquired."); + } + else + { + LOG_ERROR("Error during CryptAcquireContextc %d.",GetLastError()); + throw exception(); + } + if (CryptImportKey( + hProv, + PRIVATE_KEY, + sizeof(PRIVATE_KEY), + 0, + 0, + &hPubKey)) + { + LOG_DEBUG("The key has been imported.\n"); + } + else + { + LOG_ERROR("Private key import failed.\n"); + throw exception(); + } + + } + + LicenseSigner::LicenseSigner(const std::string& alternatePrimaryKey) { + os_initialize(); + } + + string LicenseSigner::Opensslb64Encode(size_t slen, unsigned char* signature) { + + return NULL; + } + + string LicenseSigner::signString(const string& license) { + + //------------------------------------------------------------------- + // Declare and initialize variables. + BYTE *pbBuffer = (BYTE *)license.c_str(); + DWORD dwBufferLen = strlen((char *)pbBuffer) + 1; + HCRYPTHASH hHash; + + HCRYPTKEY hKey; + BYTE *pbKeyBlob; + BYTE *pbSignature; + DWORD dwSigLen; + DWORD dwBlobLen; + + //------------------------------------------------------------------- + // Acquire a cryptographic provider context handle. + + + //------------------------------------------------------------------- + // Get the public at signature key. This is the public key + // that will be used by the receiver of the hash to verify + // the signature. In situations where the receiver could obtain the + // sender's public key from a certificate, this step would not be + // needed. + + if (CryptGetUserKey( + hProv, + AT_SIGNATURE, + &hKey)) + { + printf("The signature key has been acquired. \n"); + } + else + { + printf("Error during CryptGetUserKey for signkey. %d", GetLastError()); + } + + //------------------------------------------------------------------- + // Create the hash object. + + if (CryptCreateHash( + hProv, + CALG_SHA1, + 0, + 0, + &hHash)) + { + printf("Hash object created. \n"); + } + else + { + LOG_ERROR("Error during CryptCreateHash."); + } + //------------------------------------------------------------------- + // Compute the cryptographic hash of the buffer. + + if (CryptHashData( + hHash, + pbBuffer, + dwBufferLen, + 0)) + { + printf("The data buffer has been hashed.\n"); + } + else + { + LOG_ERROR("Error during CryptHashData."); + } + //------------------------------------------------------------------- + // Determine the size of the signature and allocate memory. + + dwSigLen = 0; + if (CryptSignHash( + hHash, + AT_SIGNATURE, + NULL, + 0, + NULL, + &dwSigLen)) + { + printf("Signature length %d found.\n", dwSigLen); + } + else + { + LOG_ERROR("Error during CryptSignHash."); + } + //------------------------------------------------------------------- + // Allocate memory for the signature buffer. + + if (pbSignature = (BYTE *)malloc(dwSigLen)) + { + printf("Memory allocated for the signature.\n"); + } + else + { + LOG_ERROR("Out of memory."); + } + //------------------------------------------------------------------- + // Sign the hash object. + + if (CryptSignHash( + hHash, + AT_SIGNATURE, + NULL, + 0, + pbSignature, + &dwSigLen)) + { + printf("pbSignature is the hash signature.\n"); + } + else + { + LOG_ERROR("Error during CryptSignHash."); + } + //------------------------------------------------------------------- + // Destroy the hash object. + + if (hHash) + CryptDestroyHash(hHash); + + printf("The hash object has been destroyed.\n"); + printf("The signing phase of this program is completed.\n\n"); + + //------------------------------------------------------------------- + // In the second phase, the hash signature is verified. + // This would most often be done by a different user in a + // separate program. The hash, signature, and the PUBLICKEYBLOB + // would be read from a file, an email message, + // or some other source. + + // Here, the original pbBuffer, pbSignature, szDescription. + // pbKeyBlob, and their lengths are used. + + // The contents of the pbBuffer must be the same data + // that was originally signed. + + //------------------------------------------------------------------- + // Get the public key of the user who created the digital signature + // and import it into the CSP by using CryptImportKey. This returns + // a handle to the public key in hPubKey. + + /*if (CryptImportKey( + hProv, + pbKeyBlob, + dwBlobLen, + 0, + 0, + &hPubKey)) + { + printf("The key has been imported.\n"); + } + else + { + MyHandleError("Public key import failed."); + } + //------------------------------------------------------------------- + // Create a new hash object. + + if (CryptCreateHash( + hProv, + CALG_MD5, + 0, + 0, + &hHash)) + { + printf("The hash object has been recreated. \n"); + } + else + { + MyHandleError("Error during CryptCreateHash."); + } + //------------------------------------------------------------------- + // Compute the cryptographic hash of the buffer. + + if (CryptHashData( + hHash, + pbBuffer, + dwBufferLen, + 0)) + { + printf("The new hash has been created.\n"); + } + else + { + MyHandleError("Error during CryptHashData."); + } + //------------------------------------------------------------------- + // Validate the digital signature. + + if (CryptVerifySignature( + hHash, + pbSignature, + dwSigLen, + hPubKey, + NULL, + 0)) + { + printf("The signature has been verified.\n"); + } + else + { + printf("Signature not validated!\n"); + } + //------------------------------------------------------------------- + // Free memory to be used to store signature. + + if (pbSignature) + free(pbSignature); + + //------------------------------------------------------------------- + // Destroy the hash object. + + if (hHash) + CryptDestroyHash(hHash);*/ + + //------------------------------------------------------------------- + // Release the provider handle. + + if (hProv) + CryptReleaseContext(hProv, 0); + return string(""); + } // End of main + + + + + + void LicenseSigner::signLicense(FullLicenseInfo& licenseInfo) { + string license = licenseInfo.printForSign(); + string signature = signString(license); + licenseInfo.license_signature = signature; + } + + LicenseSigner::~LicenseSigner() { + + } + +} /* namespace license */ -- Gitblit v1.9.1