6个文件已修改
2个文件已添加
2 文件已重命名
4个文件已删除
| | |
| | | #include "public-key.h" |
| | | #include <build_properties.h> |
| | | |
| | | #include "os/os-cpp.h" |
| | | |
| | | namespace license { |
| | | |
| | | const char *FullLicenseInfo::UNUSED_TIME = "0000-00-00"; |
| | |
| | | ADD_LIBRARY(os STATIC |
| | | os-linux.c |
| | | network_id.c |
| | | os-linux.cpp |
| | | ) |
| | | |
| | | target_link_libraries( |
| | |
| | | |
| | | #include <stdio.h> |
| | | #include <ctype.h> |
| | | #include <stdlib.h> |
| | |
| | | #include <sys/stat.h> |
| | | #include "../os.h" |
| | | #include "public-key.h" |
| | | #include "../../base/logger.h" |
| | | |
| | | #include <openssl/evp.h> |
| | | #include <openssl/bio.h> |
| | |
| | | } |
| | | } |
| | | if (drive_found == -1) { |
| | | #ifdef _DEBUG |
| | | printf("mntent: %s %s %d\n", ent->mnt_fsname, ent->mnt_dir, |
| | | mount_stat.st_ino); |
| | | #endif |
| | | LOG_DEBUG("mntent: %s %s %d\n", ent->mnt_fsname, ent->mnt_dir, |
| | | (unsigned long int)mount_stat.st_ino); |
| | | strcpy(tmpDrives[currentDrive].device, ent->mnt_fsname); |
| | | statDrives[currentDrive] = mount_stat.st_ino; |
| | | drive_found = currentDrive; |
| | |
| | | } |
| | | if (strcmp(ent->mnt_dir, "/") == 0) { |
| | | strcpy(tmpDrives[drive_found].label, "root"); |
| | | #ifdef _DEBUG |
| | | printf("drive %s set to preferred\n", ent->mnt_fsname); |
| | | #endif |
| | | LOG_DEBUG("drive %s set to preferred\n", ent->mnt_fsname); |
| | | tmpDrives[drive_found].preferred = true; |
| | | } |
| | | } |
| | |
| | | } else if (*disk_info_size >= currentDrive) { |
| | | disk_by_uuid_dir = opendir("/dev/disk/by-uuid"); |
| | | if (disk_by_uuid_dir == NULL) { |
| | | #ifdef _DEBUG |
| | | printf("Open /dev/disk/by-uuid fail"); |
| | | #endif |
| | | LOG_WARN("Open /dev/disk/by-uuid fail"); |
| | | free(statDrives); |
| | | return FUNC_RET_ERROR; |
| | | } |
| | |
| | | #ifdef _DEBUG |
| | | VALGRIND_CHECK_VALUE_IS_DEFINED(tmpDrives[i].device); |
| | | |
| | | printf("uuid %d %s %02x%02x%02x%02x\n", i, |
| | | LOG_DEBUG("uuid %d %s %02x%02x%02x%02x\n", i, |
| | | tmpDrives[i].device, |
| | | tmpDrives[i].disk_sn[0], |
| | | tmpDrives[i].disk_sn[1], |
| | |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | static void free_resources(EVP_PKEY* pkey, EVP_MD_CTX* mdctx) { |
| | | if (pkey) { |
| | | EVP_PKEY_free(pkey); |
| | | } |
| | | if (mdctx) { |
| | | EVP_MD_CTX_destroy(mdctx); |
| | | } |
| | | } |
| | | |
| | | FUNCTION_RETURN verifySignature(const char* stringToVerify, |
| | | const char* signatureB64) { |
| | | EVP_MD_CTX *mdctx = NULL; |
| | | const char *pubKey = PUBLIC_KEY; |
| | | int func_ret = 0; |
| | | |
| | | BIO* bio = BIO_new_mem_buf((void*) (pubKey), strlen(pubKey)); |
| | | RSA *rsa = PEM_read_bio_RSAPublicKey(bio, NULL, NULL, NULL); |
| | | BIO_free(bio); |
| | | if (rsa == NULL) { |
| | | LOG_ERROR("Error reading public key"); |
| | | return FUNC_RET_ERROR; |
| | | } |
| | | EVP_PKEY *pkey = EVP_PKEY_new(); |
| | | EVP_PKEY_assign_RSA(pkey, rsa); |
| | | |
| | | /*BIO* bo = BIO_new(BIO_s_mem()); |
| | | BIO_write(bo, pubKey, strlen(pubKey)); |
| | | RSA *key = 0; |
| | | PEM_read_bio_RSAPublicKey(bo, &key, 0, 0); |
| | | BIO_free(bo);*/ |
| | | |
| | | //RSA* rsa = EVP_PKEY_get1_RSA( key ); |
| | | //RSA * pubKey = d2i_RSA_PUBKEY(NULL, <der encoded byte stream pointer>, <num bytes>); |
| | | unsigned char buffer[512]; |
| | | BIO* b64 = BIO_new(BIO_f_base64()); |
| | | BIO* encoded_signature = BIO_new_mem_buf((void *) signatureB64, |
| | | strlen(signatureB64)); |
| | | BIO* biosig = BIO_push(b64, encoded_signature); |
| | | BIO_set_flags(biosig, BIO_FLAGS_BASE64_NO_NL); //Do not use newlines to flush buffer |
| | | unsigned int len = BIO_read(biosig, (void *) buffer, strlen(signatureB64)); |
| | | //Can test here if len == decodeLen - if not, then return an error |
| | | buffer[len] = 0; |
| | | |
| | | BIO_free_all(biosig); |
| | | |
| | | /* Create the Message Digest Context */ |
| | | if (!(mdctx = EVP_MD_CTX_create())) { |
| | | free_resources(pkey, mdctx); |
| | | LOG_ERROR("Error creating context"); |
| | | return FUNC_RET_ERROR; |
| | | } |
| | | if (1 != EVP_DigestVerifyInit(mdctx, NULL, EVP_sha256(), NULL, pkey)) { |
| | | LOG_ERROR("Error initializing digest"); |
| | | free_resources(pkey, mdctx); |
| | | return FUNC_RET_ERROR; |
| | | } |
| | | int en = strlen(stringToVerify); |
| | | func_ret = EVP_DigestVerifyUpdate(mdctx, stringToVerify, en); |
| | | if (1 != func_ret) { |
| | | LOG_ERROR("Error verifying digest %d", func_ret); |
| | | free_resources(pkey, mdctx); |
| | | return FUNC_RET_ERROR; |
| | | } |
| | | FUNCTION_RETURN result; |
| | | func_ret = EVP_DigestVerifyFinal(mdctx, buffer, len); |
| | | if (1 != func_ret) { |
| | | LOG_ERROR("Error verifying digest %d", func_ret); |
| | | } |
| | | result = (1 == func_ret ? FUNC_RET_OK : FUNC_RET_ERROR); |
| | | |
| | | free_resources(pkey, mdctx); |
| | | return result; |
| | | } |
| | |
| | | ADD_LIBRARY(os STATIC
|
| | | os-win.c
|
| | | os-win.cpp
|
| | | )
|
| | |
|
| | | target_link_libraries(
|
| | |
| | | FUNCTION_RETURN result = FUNC_RET_ERROR;
|
| | | char buffer[MAX_COMPUTERNAME_LENGTH + 1];
|
| | | int bufsize = MAX_COMPUTERNAME_LENGTH + 1;
|
| | | BOOL cmpName = GetComputerName(
|
| | | buffer, &bufsize);
|
| | | BOOL cmpName = GetComputerName(buffer, &bufsize);
|
| | | if (cmpName){
|
| | | strncpy(identifier, buffer, 6);
|
| | | result = FUNC_RET_OK;
|
| | |
| | | FUNCTION_RETURN return_value;
|
| | | DWORD dwResult = GetLogicalDriveStrings(dwSize, szLogicalDrives);
|
| | |
|
| | | if (dwResult > 0 && dwResult <= MAX_PATH)
|
| | | {
|
| | | if (dwResult > 0 && dwResult <= MAX_PATH) {
|
| | | return_value = FUNC_RET_OK;
|
| | | szSingleDrive = szLogicalDrives;
|
| | | while (*szSingleDrive && ndrives < MAX_UNITS)
|
| | | {
|
| | | while (*szSingleDrive && ndrives < MAX_UNITS) {
|
| | |
|
| | | // get the next drive
|
| | | driveType = GetDriveType(szSingleDrive);
|
| | | if (driveType == DRIVE_FIXED){
|
| | | success = GetVolumeInformation(szSingleDrive, volName, MAX_PATH, &volSerial,
|
| | | &FileMaxLen, &FileFlags, FileSysName, MAX_PATH);
|
| | | success = GetVolumeInformation(szSingleDrive, volName, MAX_PATH,
|
| | | &volSerial, &FileMaxLen, &FileFlags, FileSysName,
|
| | | MAX_PATH);
|
| | | if (success) {
|
| | | LOG_INFO("drive : %s\n", szSingleDrive);
|
| | | LOG_INFO("Volume Name : %s\n", volName);
|
| | | LOG_INFO("Volume Serial : 0x%x\n", volSerial);
|
| | | LOG_DEBUG("Max file length : %d\n", FileMaxLen);
|
| | | LOG_DEBUG("Filesystem : %s\n", FileSysName);
|
| | | LOG_INFO("Volume Serial : 0x%x\n", volSerial); LOG_DEBUG("Max file length : %d\n", FileMaxLen); LOG_DEBUG("Filesystem : %s\n", FileSysName);
|
| | | if (diskInfos != NULL && * disk_info_size < ndrives){
|
| | | strncpy(diskInfos[ndrives].device, volName, MAX_PATH);
|
| | | strncpy(diskInfos[ndrives].label, FileSysName, MAX_PATH);
|
| | | strncpy(diskInfos[ndrives].label, FileSysName,
|
| | | MAX_PATH);
|
| | | diskInfos[ndrives].id = ndrives;
|
| | | diskInfos[ndrives].preferred = (strncmp(szSingleDrive, "C", 1) != 0);
|
| | | diskInfos[ndrives].preferred = (strncmp(szSingleDrive,
|
| | | "C", 1) != 0);
|
| | |
|
| | | }
|
| | | ndrives++;
|
| | | }
|
| | | else {
|
| | | LOG_WARN("Unable to retrieve information of '%s'\n", szSingleDrive);
|
| | | } else {
|
| | | LOG_WARN("Unable to retrieve information of '%s'\n",
|
| | | szSingleDrive);
|
| | | }
|
| | | }
|
| | | LOG_INFO("This volume is not fixed : %s, type: %d\n", szSingleDrive);
|
| | | LOG_INFO("This volume is not fixed : %s, type: %d\n",
|
| | | szSingleDrive);
|
| | | szSingleDrive += strlen(szSingleDrive) + 1;
|
| | | }
|
| | | }
|
| | |
| | | if (isdigit((unsigned char)*str2)) {
|
| | | ipv4[index] *= 10;
|
| | | ipv4[index] += *str2 - '0';
|
| | | }
|
| | | else {
|
| | | } else {
|
| | | index++;
|
| | | }
|
| | | str2++;
|
| | |
| | |
|
| | | //http://stackoverflow.com/questions/18046063/mac-address-using-c
|
| | | //TODO: count only interfaces with type (MIB_IF_TYPE_ETHERNET IF_TYPE_IEEE80211)
|
| | | FUNCTION_RETURN getAdapterInfos(OsAdapterInfo * adapterInfos, size_t * adapter_info_size) {
|
| | | FUNCTION_RETURN getAdapterInfos(OsAdapterInfo * adapterInfos,
|
| | | size_t * adapter_info_size) {
|
| | | DWORD dwStatus;
|
| | | unsigned int i = 0;
|
| | | FUNCTION_RETURN result;
|
| | |
| | |
|
| | | i = 3;
|
| | | do{
|
| | | pAdapterInfo = (PIP_ADAPTER_INFO)malloc(sizeof(IP_ADAPTER_INFO)*dwBufLen);
|
| | | pAdapterInfo = (PIP_ADAPTER_INFO) malloc(
|
| | | sizeof(IP_ADAPTER_INFO) * dwBufLen);
|
| | | dwStatus = GetAdaptersInfo( // Call GetAdapterInfo
|
| | | pAdapterInfo, // [out] buffer to receive data
|
| | | &dwBufLen // [in] size of receive data buffer
|
| | |
| | | i = 0;
|
| | | result = FUNC_RET_OK;
|
| | | while (pAdapter) {
|
| | | strncpy(adapterInfos[i].description, pAdapter->Description, min(sizeof(adapterInfos->description), MAX_ADAPTER_DESCRIPTION_LENGTH));
|
| | | strncpy(adapterInfos[i].description, pAdapter->Description,
|
| | | min(sizeof(adapterInfos->description),
|
| | | MAX_ADAPTER_DESCRIPTION_LENGTH));
|
| | | memcpy(adapterInfos[i].mac_address, pAdapter->Address, 8);
|
| | | translate(pAdapter->IpAddressList.IpAddress.String, adapterInfos[i].ipv4_address);
|
| | | translate(pAdapter->IpAddressList.IpAddress.String,
|
| | | adapterInfos[i].ipv4_address);
|
| | | adapterInfos[i].type = IFACE_TYPE_ETHERNET;
|
| | | i++;
|
| | | pAdapter = pAdapter->Next;
|
| | |
| | | char* hashStr;
|
| | | int i;
|
| | |
|
| | | if (CryptGetHashParam(*hHash,HP_HASHSIZE,(BYTE *)&dwHashLen, &dwHashLenSize, 0))
|
| | | {
|
| | | if (CryptGetHashParam(*hHash, HP_HASHSIZE, (BYTE *) &dwHashLen,
|
| | | &dwHashLenSize, 0)) {
|
| | | pbHash = (BYTE*)malloc(dwHashLen);
|
| | | hashStr = (char*)malloc(dwHashLen*2 +1);
|
| | | if (CryptGetHashParam(*hHash,HP_HASHVAL,pbHash, &dwHashLen, 0)) {
|
| | | for (i = 0; i < dwHashLen; i++) {
|
| | | sprintf(&hashStr[i * 2], "%02x", pbHash[i]);
|
| | | }
|
| | | LOG_DEBUG("Hash to verify: %s", hashStr);
|
| | | } LOG_DEBUG("Hash to verify: %s", hashStr);
|
| | | }
|
| | | free(pbHash);
|
| | | free(hashStr);
|
| | | }
|
| | | }
|
| | |
|
| | | FUNCTION_RETURN verifySignature(const char* stringToVerify, const char* signatureB64) {
|
| | | FUNCTION_RETURN verifySignature(const char* stringToVerify,
|
| | | const char* signatureB64) {
|
| | | //--------------------------------------------------------------------
|
| | | // Declare variables.
|
| | | //
|
| | |
| | | //--------------------------------------------------------------------
|
| | | // Acquire a handle to the CSP.
|
| | |
|
| | | if (!CryptAcquireContext(
|
| | | &hProv,
|
| | | NULL,
|
| | | MS_ENHANCED_PROV,
|
| | | PROV_RSA_FULL,
|
| | | CRYPT_VERIFYCONTEXT))
|
| | | {
|
| | | if (!CryptAcquireContext(&hProv,
|
| | | NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) {
|
| | | // If the key container cannot be opened, try creating a new
|
| | | // container by specifying a container name and setting the
|
| | | // CRYPT_NEWKEYSET flag.
|
| | | LOG_INFO("Error in AcquireContext 0x%08x \n", GetLastError());
|
| | | if (NTE_BAD_KEYSET == GetLastError())
|
| | | {
|
| | | if (!CryptAcquireContext(
|
| | | &hProv,
|
| | | "license++verify",
|
| | | MS_ENHANCED_PROV,
|
| | | PROV_RSA_FULL,
|
| | | CRYPT_NEWKEYSET | CRYPT_VERIFYCONTEXT))
|
| | | {
|
| | | LOG_ERROR("Error in AcquireContext 0x%08x \n",
|
| | | GetLastError());
|
| | | if (NTE_BAD_KEYSET == GetLastError()) {
|
| | | if (!CryptAcquireContext(&hProv, "license++verify",
|
| | | MS_ENHANCED_PROV, PROV_RSA_FULL,
|
| | | CRYPT_NEWKEYSET | CRYPT_VERIFYCONTEXT)) {
|
| | | LOG_ERROR("Error in AcquireContext 0x%08x \n", GetLastError());
|
| | | return FUNC_RET_ERROR;
|
| | | }
|
| | | }
|
| | | else
|
| | | {
|
| | | } else {
|
| | | LOG_ERROR(" Error in AcquireContext 0x%08x \n", GetLastError());
|
| | | return FUNC_RET_ERROR;
|
| | | }
|
| | |
| | | // BYTE array into the key container. The function returns a
|
| | | // pointer to an HCRYPTKEY variable that contains the handle of
|
| | | // the imported key.
|
| | | if (!CryptImportKey(hProv, &pubKey[0], sizeof(pubKey), 0, 0, &hKey))
|
| | | {
|
| | | LOG_ERROR("Error 0x%08x in importing the PublicKey \n",
|
| | | GetLastError());
|
| | | if (!CryptImportKey(hProv, &pubKey[0], sizeof(pubKey), 0, 0, &hKey)) {
|
| | | LOG_ERROR("Error 0x%08x in importing the PublicKey \n", GetLastError());
|
| | | return FUNC_RET_ERROR;
|
| | | }
|
| | |
|
| | | if (CryptCreateHash(hProv, CALG_SHA1, 0, 0, &hHash))
|
| | | {
|
| | | if (CryptCreateHash(hProv, CALG_SHA1, 0, 0, &hHash)) {
|
| | | LOG_DEBUG("Hash object created.");
|
| | | }
|
| | | else
|
| | | {
|
| | | } else {
|
| | | LOG_ERROR("Error in hash creation 0x%08x ", GetLastError());
|
| | | CryptReleaseContext(hProv,0);
|
| | | return FUNC_RET_ERROR;
|
| | |
| | | #endif
|
| | | sigBlob = unbase64(signatureB64, strlen(signatureB64), &dwSigLen);
|
| | | LOG_DEBUG("raw signature lenght %d", dwSigLen);
|
| | | if (!CryptVerifySignature(hHash, sigBlob, dwSigLen, hKey, NULL, 0))
|
| | | {
|
| | | if (!CryptVerifySignature(hHash, sigBlob, dwSigLen, hKey, NULL, 0)) {
|
| | | LOG_ERROR("Signature not validated! 0x%08x ", GetLastError());
|
| | | free(sigBlob);
|
| | | CryptDestroyHash(hHash);
|
New file |
| | |
| | | #ifndef _GNU_SOURCE |
| | | #define _GNU_SOURCE /* To get defns of NI_MAXSERV and NI_MAXHOST */ |
| | | #endif |
| | | #include <arpa/inet.h> |
| | | #include <sys/socket.h> |
| | | #include <netdb.h> |
| | | #include <ifaddrs.h> |
| | | #include <stdio.h> |
| | | #include <stdlib.h> |
| | | #include <unistd.h> |
| | | #include <linux/if_link.h> |
| | | #include <sys/socket.h> |
| | | #include <netpacket/packet.h> |
| | | |
| | | #include <paths.h> |
| | | |
| | | #include <stdlib.h> |
| | | #include <cstring> |
| | | #include <string> |
| | | #include <system_error> |
| | | #include <unistd.h> |
| | | #include <sstream> |
| | | #include <sys/ioctl.h> |
| | | #include <sys/stat.h> |
| | | #include "../os-cpp.h" |
| | | #include "public-key.h" |
| | | |
| | | #include <openssl/evp.h> |
| | | #include <openssl/bio.h> |
| | | #include <openssl/pem.h> |
| | | #include <openssl/err.h> |
| | | |
| | | namespace license { |
| | | |
| | | using namespace std; |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | VIRTUALIZATION getVirtualization() { |
| | | //http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html |
| | | // |
| | | //bool rc = true; |
| | | /*__asm__ ( |
| | | "push %edx\n" |
| | | "push %ecx\n" |
| | | "push %ebx\n" |
| | | "mov %eax, 'VMXh'\n" |
| | | "mov %ebx, 0\n" // any value but not the MAGIC VALUE |
| | | "mov %ecx, 10\n"// get VMWare version |
| | | "mov %edx, 'VX'\n"// port number |
| | | "in %eax, dx\n"// read port on return EAX returns the VERSION |
| | | "cmp %ebx, 'VMXh'\n"// is it a reply from VMWare? |
| | | "setz [rc] \n"// set return value |
| | | "pop %ebx \n" |
| | | "pop %ecx \n" |
| | | "pop %edx \n" |
| | | );*/ |
| | | |
| | | return NONE; |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | #include <stdio.h> |
| | | #include "../base_lib/CryptoHelper.h" |
| | | #include <string> |
| | | #include <stdlib.h> |
| | | #include <iostream> |
| | | |
| | | using namespace std; |
| | | namespace license { |
| | | |
| | | void write_pubkey_file(string public_fname, BYTE *pbPublicKey, |
| | | DWORD dwPublicKeySize) { |
| | | FILE* fp = fopen(public_fname.c_str(), "w"); |
| | | fprintf(fp, "//file generated by bootstrap.cpp, do not edit.\n\n"); |
| | | fprintf(fp, "#ifndef PUBLIC_KEY_H_\n#define PUBLIC_KEY_H_\n"); |
| | | fprintf(fp, "static BYTE PUBLIC_KEY[] = {"); |
| | | for (int i = 0; i < dwPublicKeySize; i++) { |
| | | if (i != 0) { |
| | | fprintf(fp, ","); |
| | | } |
| | | if (i % 15 == 0) { |
| | | fprintf(fp, "\n "); |
| | | } |
| | | fprintf(fp, "%d", pbPublicKey[i]); |
| | | } |
| | | fprintf(fp, "\n};\n\n"); |
| | | int random = rand() % 1000; |
| | | fprintf(fp, "#define SHARED_RANDOM %d;\n", random); |
| | | fprintf(fp, "#endif\n"); |
| | | fclose(fp); |
| | | } |
| | | |
| | | void write_privkey_file(string private_fname, BYTE *privateKey, |
| | | DWORD dwPrivateKeySize) { |
| | | FILE* fp = fopen(private_fname.c_str(), "w"); |
| | | fprintf(fp, "//file generated by bootstrap.cpp, do not edit.\n\n"); |
| | | fprintf(fp, "#ifndef PRIVATE_KEY_H_\n#define PRIVATE_KEY_H_\n"); |
| | | fprintf(fp, "static BYTE PRIVATE_KEY[] = {"); |
| | | for (int i = 0; i < dwPrivateKeySize; i++) { |
| | | if (i != 0) { |
| | | fprintf(fp, ","); |
| | | } |
| | | if (i % 15 == 0) { |
| | | fprintf(fp, "\n "); |
| | | } |
| | | fprintf(fp, "%d", privateKey[i]); |
| | | } |
| | | fprintf(fp, "\n};\n\n"); |
| | | fprintf(fp, "#endif\n"); |
| | | fclose(fp); |
| | | } |
| | | |
| | | void generatePk(string private_include, string public_include) { |
| | | unique_ptr<CryptoHelper> cryptoHlpr = CryptoHelper.getInstance(); |
| | | BYTE *pbPublicKey = NULL, *pbPrivateKey = NULL; |
| | | DWORD dwPublicKeySize = 0, dwPrivateKeySize = 0; |
| | | HRESULT hr = S_OK; |
| | | // Get the key container context. |
| | | if (FAILED(hr = cryptoHlpr.AcquireContext(_T("TestContainer")))) { |
| | | // Call FormatMessage to display the error returned in hr. |
| | | return; |
| | | } |
| | | // Generate the public/private key pair. |
| | | if (FAILED(hr = cryptoHlpr.GenerateKeyPair())) { |
| | | // Call FormatMessage to display the error returned in hr. |
| | | return; |
| | | } |
| | | // Export out the public key blob. |
| | | if (FAILED( |
| | | hr = cryptoHlpr.ExportPublicKey(&pbPublicKey, dwPublicKeySize))) { |
| | | // Call FormatMessage to display the error returned in hr. |
| | | cerr << "error exporting pubkey" << endl; |
| | | return; |
| | | } else { |
| | | write_pubkey_file(public_include, pbPublicKey, dwPublicKeySize); |
| | | } |
| | | // Print out the public key to console as a |
| | | // hexadecimal string. |
| | | wprintf(L"\n\nPublicKey = \""); |
| | | for (DWORD i = 0; i < dwPublicKeySize; i++) { |
| | | wprintf(L"%02x", pbPublicKey[i]); |
| | | } |
| | | wprintf(L"\"\n"); |
| | | // Export out the private key blob. |
| | | if (FAILED(cryptoHlpr.ExportPrivateKey(&pbPrivateKey, dwPrivateKeySize))) { |
| | | cerr << "Error exporting private key." << endl; |
| | | return; |
| | | } else { |
| | | write_privkey_file(private_include, pbPrivateKey, dwPrivateKeySize); |
| | | } |
| | | // Print out the private key to console as a |
| | | // hexadecimal string. |
| | | wprintf(L"\n\nPrivateKey = \""); |
| | | for (DWORD i = 0; i < dwPrivateKeySize; i++) { |
| | | wprintf(L"%02x", pbPrivateKey[i]); |
| | | } |
| | | wprintf(L"\"\n"); |
| | | // Delete the public key blob allocated by the |
| | | // ExportPublicKey method. |
| | | if (pbPublicKey) |
| | | delete[] pbPublicKey; |
| | | // Delete the private key blob allocated by the |
| | | // ExportPrivateKey method. |
| | | if (pbPrivateKey) |
| | | delete[] pbPrivateKey; |
| | | return; |
| | | } |
| | | } |
| | | |
| | | 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]); |
| | | |
| | | license::generatePk(private_fname, public_fname); |
| | | return 0; |
| | | } |
| | |
| | | CryptoHelperWindows::CryptoHelperWindows() {
|
| | | m_hCryptProv = NULL;
|
| | | m_hCryptKey = NULL;
|
| | | if (!CryptAcquireContext(
|
| | | &m_hCryptProv,
|
| | | "license++sign",
|
| | | MS_ENHANCED_PROV,
|
| | | PROV_RSA_FULL,
|
| | | 0))
|
| | | {
|
| | | if (!CryptAcquireContext(&m_hCryptProv, "license++sign", MS_ENHANCED_PROV,
|
| | | PROV_RSA_FULL, 0)) {
|
| | | // If the key container cannot be opened, try creating a new
|
| | | // container by specifying a container name and setting the
|
| | | // CRYPT_NEWKEYSET flag.
|
| | | printf("Error in AcquireContext 0x%08x \n", GetLastError());
|
| | | if (NTE_BAD_KEYSET == GetLastError())
|
| | | {
|
| | | if (!CryptAcquireContext(
|
| | | &m_hCryptProv,
|
| | | "license++sign",
|
| | | MS_ENHANCED_PROV,
|
| | | PROV_RSA_FULL,
|
| | | CRYPT_NEWKEYSET))
|
| | | {
|
| | | printf("Error in AcquireContext 0x%08x \n",
|
| | | GetLastError());
|
| | | if (NTE_BAD_KEYSET == GetLastError()) {
|
| | | if (!CryptAcquireContext(&m_hCryptProv, "license++sign",
|
| | | MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET)) {
|
| | | printf("Error in AcquireContext 0x%08x \n", GetLastError());
|
| | | throw logic_error("");
|
| | | }
|
| | | }
|
| | | else
|
| | | {
|
| | | } else {
|
| | | printf(" Error in AcquireContext 0x%08x \n", GetLastError());
|
| | | throw logic_error("");
|
| | | }
|
| | | }
|
| | |
|
| | | }
|
| | |
|
| | |
|
| | | /**
|
| | | This method calls the CryptGenKey function to get a handle to an
|
| | |
| | | exportable key-pair. The key-pair is generated with the RSA public-key
|
| | | key exchange algorithm using Microsoft Enhanced Cryptographic Provider.
|
| | | */
|
| | | void CryptoHelperWindows::generateKeyPair()
|
| | | {
|
| | | void CryptoHelperWindows::generateKeyPair() {
|
| | | HRESULT hr = S_OK;
|
| | | DWORD dwErrCode;
|
| | | // If the handle to key container is NULL, fail.
|
| | |
| | | // to a new exportable key-pair.
|
| | | if (!CryptGenKey(m_hCryptProv,
|
| | | ENCRYPT_ALGORITHM,
|
| | | KEYLENGTH | CRYPT_EXPORTABLE,
|
| | | &m_hCryptKey))
|
| | | {
|
| | | KEYLENGTH | CRYPT_EXPORTABLE, &m_hCryptKey)) {
|
| | | dwErrCode = GetLastError();
|
| | | throw logic_error(string("Error generating keys ") + to_string(dwErrCode));
|
| | | throw logic_error(
|
| | | string("Error generating keys ") + to_string(dwErrCode));
|
| | | }
|
| | | }
|
| | |
|
| | | /* This method calls the CryptExportKey function to get the Public key
|
| | | in a string suitable for C source inclusion.
|
| | | */
|
| | | const string CryptoHelperWindows::exportPublicKey() const
|
| | | {
|
| | | const string CryptoHelperWindows::exportPublicKey() const {
|
| | | HRESULT hr = S_OK;
|
| | | DWORD dwErrCode;
|
| | | DWORD dwBlobLen;
|
| | |
| | | throw logic_error("call GenerateKey first.");
|
| | | // This call here determines the length of the key
|
| | | // blob.
|
| | | if (!CryptExportKey(
|
| | | m_hCryptKey,
|
| | | NULL,
|
| | | PUBLICKEYBLOB,
|
| | | 0,
|
| | | NULL,
|
| | | &dwBlobLen))
|
| | | {
|
| | | if (!CryptExportKey(m_hCryptKey,
|
| | | NULL, PUBLICKEYBLOB, 0,
|
| | | NULL, &dwBlobLen)) {
|
| | | dwErrCode = GetLastError();
|
| | | throw logic_error(string("Error calculating size of public key ") + to_string(dwErrCode));
|
| | | throw logic_error(
|
| | | string("Error calculating size of public key ")
|
| | | + to_string(dwErrCode));
|
| | | }
|
| | | // Allocate memory for the pbKeyBlob.
|
| | | if ((pbKeyBlob = new BYTE[dwBlobLen]) == NULL)
|
| | | {
|
| | | if ((pbKeyBlob = new BYTE[dwBlobLen]) == NULL) {
|
| | | throw logic_error(string("Out of memory exporting public key "));
|
| | | }
|
| | | // Do the actual exporting into the key BLOB.
|
| | | if (!CryptExportKey(
|
| | | m_hCryptKey,
|
| | | NULL,
|
| | | PUBLICKEYBLOB,
|
| | | 0,
|
| | | pbKeyBlob,
|
| | | &dwBlobLen))
|
| | | {
|
| | | if (!CryptExportKey(m_hCryptKey,
|
| | | NULL, PUBLICKEYBLOB, 0, pbKeyBlob, &dwBlobLen)) {
|
| | | delete pbKeyBlob;
|
| | | dwErrCode = GetLastError();
|
| | | throw logic_error(string("Error exporting public key ") + to_string(dwErrCode));
|
| | | }
|
| | | else
|
| | | {
|
| | | throw logic_error(
|
| | | string("Error exporting public key ") + to_string(dwErrCode));
|
| | | } else {
|
| | | ss << "\t";
|
| | | for (unsigned int i = 0; i < dwBlobLen; i++){
|
| | | if (i != 0){
|
| | |
| | | }
|
| | |
|
| | | CryptoHelperWindows::~CryptoHelperWindows() {
|
| | | if (m_hCryptProv)
|
| | | {
|
| | | if (m_hCryptProv) {
|
| | | CryptReleaseContext(m_hCryptProv, 0);
|
| | | m_hCryptProv = NULL;
|
| | | }
|
| | |
| | | // in a byte array. The byte array is allocated on the heap and the size
|
| | | // of this is returned to the caller. The caller is responsible for releasing // this memory using a delete call.
|
| | | //--------------------------------------------------------------------
|
| | | const string CryptoHelperWindows::exportPrivateKey() const
|
| | | {
|
| | | const string CryptoHelperWindows::exportPrivateKey() const {
|
| | | HRESULT hr = S_OK;
|
| | | DWORD dwErrCode;
|
| | | DWORD dwBlobLen;
|
| | |
| | | throw logic_error(string("call GenerateKey first."));
|
| | | // This call here determines the length of the key
|
| | | // blob.
|
| | | if (!CryptExportKey(
|
| | | m_hCryptKey,
|
| | | NULL,
|
| | | PRIVATEKEYBLOB,
|
| | | 0,
|
| | | NULL,
|
| | | &dwBlobLen))
|
| | | {
|
| | | if (!CryptExportKey(m_hCryptKey,
|
| | | NULL, PRIVATEKEYBLOB, 0,
|
| | | NULL, &dwBlobLen)) {
|
| | | dwErrCode = GetLastError();
|
| | | throw logic_error(string("Error calculating size of private key ") + to_string(dwErrCode));
|
| | | throw logic_error(
|
| | | string("Error calculating size of private key ")
|
| | | + to_string(dwErrCode));
|
| | | }
|
| | | // Allocate memory for the pbKeyBlob.
|
| | | if ((pbKeyBlob = new BYTE[dwBlobLen]) == NULL)
|
| | | {
|
| | | if ((pbKeyBlob = new BYTE[dwBlobLen]) == NULL) {
|
| | | throw logic_error(string("Out of memory exporting private key "));
|
| | | }
|
| | |
|
| | | // Do the actual exporting into the key BLOB.
|
| | | if (!CryptExportKey(
|
| | | m_hCryptKey,
|
| | | NULL,
|
| | | PRIVATEKEYBLOB,
|
| | | 0,
|
| | | pbKeyBlob,
|
| | | &dwBlobLen))
|
| | | {
|
| | | if (!CryptExportKey(m_hCryptKey,
|
| | | NULL, PRIVATEKEYBLOB, 0, pbKeyBlob, &dwBlobLen)) {
|
| | | delete pbKeyBlob;
|
| | | dwErrCode = GetLastError();
|
| | | throw logic_error(string("Error exporting private key ") + to_string(dwErrCode));
|
| | | }
|
| | | else
|
| | | {
|
| | | throw logic_error(
|
| | | string("Error exporting private key ") + to_string(dwErrCode));
|
| | | } else {
|
| | | ss << "\t";
|
| | | for (unsigned int i = 0; i < dwBlobLen; i++){
|
| | | if (i != 0){
|
| | |
| | | char* hashStr;
|
| | | int i;
|
| | |
|
| | | if (CryptGetHashParam(*hHash, HP_HASHSIZE, (BYTE *)&dwHashLen, &dwHashLenSize, 0))
|
| | | {
|
| | | if (CryptGetHashParam(*hHash, HP_HASHSIZE, (BYTE *) &dwHashLen,
|
| | | &dwHashLenSize, 0)) {
|
| | | pbHash = (BYTE*)malloc(dwHashLen);
|
| | | hashStr = (char*)malloc(dwHashLen * 2 + 1);
|
| | | if (CryptGetHashParam(*hHash, HP_HASHVAL, pbHash, &dwHashLen, 0)) {
|
| | |
| | | }
|
| | | }
|
| | |
|
| | | const string CryptoHelperWindows::signString(const void* privateKey, size_t pklen,
|
| | | const string& license) const{
|
| | | const string CryptoHelperWindows::signString(const void* privateKey,
|
| | | size_t pklen, const string& license) const {
|
| | | BYTE *pbBuffer = (BYTE *)license.c_str();
|
| | | DWORD dwBufferLen = strlen((char *)pbBuffer);
|
| | | HCRYPTHASH hHash;
|
| | |
| | | DWORD dwBlobLen;
|
| | | DWORD strLen;
|
| | |
|
| | |
|
| | | //-------------------------------------------------------------------
|
| | | // Acquire a cryptographic provider context handle.
|
| | |
|
| | | if (!CryptImportKey(m_hCryptProv, (const BYTE *) privateKey, pklen, 0, 0, &hKey))
|
| | | {
|
| | | throw logic_error(string("Error in importing the PrivateKey ") + to_string(GetLastError()));
|
| | | if (!CryptImportKey(m_hCryptProv, (const BYTE *) privateKey, pklen, 0, 0,
|
| | | &hKey)) {
|
| | | throw logic_error(
|
| | | string("Error in importing the PrivateKey ")
|
| | | + to_string(GetLastError()));
|
| | | }
|
| | |
|
| | | //-------------------------------------------------------------------
|
| | | // Create the hash object.
|
| | |
|
| | | if (CryptCreateHash(
|
| | | m_hCryptProv,
|
| | | CALG_SHA1,
|
| | | 0,
|
| | | 0,
|
| | | &hHash))
|
| | | {
|
| | | if (CryptCreateHash(m_hCryptProv, CALG_SHA1, 0, 0, &hHash)) {
|
| | | printf("Hash object created. \n");
|
| | | }
|
| | | else
|
| | | {
|
| | | } else {
|
| | | CryptDestroyKey(hKey);
|
| | | throw logic_error(string("Error during CryptCreateHash."));
|
| | | }
|
| | | //-------------------------------------------------------------------
|
| | | // Compute the cryptographic hash of the buffer.
|
| | |
|
| | | if (CryptHashData(hHash,pbBuffer,dwBufferLen, 0))
|
| | | {
|
| | | if (CryptHashData(hHash, pbBuffer, dwBufferLen, 0)) {
|
| | | #ifdef _DEBUG
|
| | | printf("Length of data to be hashed: %d \n", dwBufferLen);
|
| | | printHash(&hHash);
|
| | | #endif
|
| | | }
|
| | | else
|
| | | {
|
| | | } else {
|
| | | throw logic_error(string("Error during CryptHashData."));
|
| | | }
|
| | | //-------------------------------------------------------------------
|
| | | // Determine the size of the signature and allocate memory.
|
| | |
|
| | | dwSigLen = 0;
|
| | | if (CryptSignHash(
|
| | | hHash,
|
| | | AT_SIGNATURE,
|
| | | NULL,
|
| | | 0,
|
| | | NULL,
|
| | | &dwSigLen))
|
| | | {
|
| | | if (CryptSignHash(hHash, AT_SIGNATURE, NULL, 0, NULL, &dwSigLen)) {
|
| | | printf("Signature length %d found.\n", dwSigLen);
|
| | | }
|
| | | else
|
| | | {
|
| | | } else {
|
| | | throw logic_error(string("Error during CryptSignHash."));
|
| | | }
|
| | | //-------------------------------------------------------------------
|
| | | // Allocate memory for the signature buffer.
|
| | |
|
| | | if (pbSignature = (BYTE *)malloc(dwSigLen))
|
| | | {
|
| | | if (pbSignature = (BYTE *) malloc(dwSigLen)) {
|
| | | printf("Memory allocated for the signature.\n");
|
| | | }
|
| | | else
|
| | | {
|
| | | } else {
|
| | | throw logic_error(string("Out of memory."));
|
| | | }
|
| | | //-------------------------------------------------------------------
|
| | | // Sign the hash object.
|
| | |
|
| | | if (CryptSignHash(
|
| | | hHash,
|
| | | AT_SIGNATURE,
|
| | | NULL,
|
| | | 0,
|
| | | pbSignature,
|
| | | &dwSigLen))
|
| | | {
|
| | | if (CryptSignHash(hHash, AT_SIGNATURE,
|
| | | NULL, 0, pbSignature, &dwSigLen)) {
|
| | | printf("pbSignature is the signature length. %d\n", dwSigLen);
|
| | | }
|
| | | else
|
| | | {
|
| | | } else {
|
| | | throw logic_error(string("Error during CryptSignHash."));
|
| | | }
|
| | | //-------------------------------------------------------------------
|
| | | // Destroy the hash object.
|
| | |
|
| | |
|
| | | CryptDestroyHash(hHash);
|
| | | CryptDestroyKey(hKey);
|
| | |
| | |
|
| | | /*if (hProv)
|
| | | CryptReleaseContext(hProv, 0);*/
|
| | | if (pbSignature)
|
| | | if (pbSignature) {
|
| | | free(pbSignature);
|
| | | }
|
| | | return string(&buffer[0]);
|
| | | }
|
| | | } /* namespace license */
|