| | |
| | | #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 |
| | |
| | | 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, |
| | |
| | | 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); |
| | |
| | | |
| | | #ifndef LOG_ENABLED |
| | | #include <errno.h> |
| | | #ifdef __cplusplus |
| | | extern "C" { |
| | | #endif |
| | | |
| | | #define clean_errno() (errno == 0 ? "None" : strerror(errno)) |
| | | |
| | | #ifdef NDEBUG |
| | |
| | | #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,...) |
| | |
| | | |
| | | void _log(char* format, ...); |
| | | void _shutdown_log(); |
| | | |
| | | #ifdef __cplusplus |
| | | } |
| | | #endif |
| | | #endif |
| | |
| | | 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); |
| | | |
| | |
| | | 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); |
| | |
| | | } |
| | | |
| | | |
| | | 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(); |
| | | } |
| | |
| | | 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); |
| | | }; |
| | |
| | | * Author: devel |
| | | */ |
| | | |
| | | #include "../LicenseSigner.h" |
| | | #include "../private-key.h" |
| | | #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) { |
| | |
| | | } |
| | | |
| | | string LicenseSigner::signString(const string& license) { |
| | | return NULL; |
| | |
|
| | | //-------------------------------------------------------------------
|
| | | // 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(); |