gcontini
2019-11-30 e22505aee5a36e2290c32ccec12d50ec4e7fde27
src/library/os/os.c
@@ -1,13 +1,12 @@
//#include <stdlib.h>
#include <stdio.h>
#include "../base/logger.h"
#include "os.h"
#include "public-key.h"
#include "../base/logger.h"
#include <public_key.h>
#include <stdio.h>
#ifndef _MSC_VER
#ifdef __linux__
#include <openssl/pem.h>
/*
static void free_resources(EVP_PKEY* pkey, EVP_MD_CTX* mdctx) {
   if (pkey) {
      EVP_PKEY_free(pkey);
@@ -15,12 +14,12 @@
   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;
   const char pubKey[] = PUBLIC_KEY;
   int func_ret = 0;
   BIO* bio = BIO_new_mem_buf((void*) (pubKey), strlen(pubKey));
@@ -38,7 +37,7 @@
    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];
@@ -53,7 +52,6 @@
   BIO_free_all(biosig);
   /* Create the Message Digest Context */
   if (!(mdctx = EVP_MD_CTX_create())) {
      free_resources(pkey, mdctx);
      LOG_ERROR("Error creating context");
@@ -80,13 +78,15 @@
   free_resources(pkey, mdctx);
   return result;
}
}*/
#else
#include <iphlpapi.h>
#include <Windows.h>
#include <windows.h>
#pragma comment(lib, "IPHLPAPI.lib")
unsigned char* unbase64(const char* ascii, int len, int *flen);
static void printHash(HCRYPTHASH* hHash) {
   BYTE *pbHash;
@@ -115,9 +115,9 @@
   // Declare variables.
   //
   // hProv:           Cryptographic service provider (CSP). This example
   //                  uses the Microsoft Enhanced Cryptographic
   //                  uses the Microsoft Enhanced Cryptographic
   //                  Provider.
   // hKey:            Key to be used. In this example, you import the
   // hKey:            Key to be used. In this example, you import the
   //                  key as a PLAINTEXTKEYBLOB.
   // dwBlobLen:       Length of the plaintext key.
   // pbKeyBlob:       Pointer to the exported key.
@@ -135,10 +135,10 @@
   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
      // 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 (NTE_BAD_KEYSET == (long)GetLastError()) {
         if (!CryptAcquireContext(&hProv, "license++verify",
               MS_ENHANCED_PROV, PROV_RSA_FULL,
               CRYPT_NEWKEYSET | CRYPT_VERIFYCONTEXT)) {
@@ -152,7 +152,7 @@
   }
   // Use the CryptImportKey function to import the PLAINTEXTKEYBLOB
   // BYTE array into the key container. The function returns a
   // 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)) {
@@ -168,7 +168,7 @@
      return FUNC_RET_ERROR;
   }
   if (!CryptHashData(hHash, stringToVerify, (DWORD) strlen(stringToVerify), 0)) {
   if (!CryptHashData(hHash, (const BYTE*)stringToVerify, (DWORD) strlen(stringToVerify), 0)) {
      LOG_ERROR("Error in hashing data 0x%08x ", GetLastError());
      CryptDestroyHash(hHash);
      CryptReleaseContext(hProv, 0);
@@ -178,7 +178,7 @@
   LOG_DEBUG("Lenght %d, hashed Data: [%s]", strlen(stringToVerify), stringToVerify);
   printHash(&hHash);
#endif
   sigBlob = unbase64(signatureB64, (int) strlen(signatureB64), &dwSigLen);
   sigBlob = unbase64(signatureB64, (int) strlen(signatureB64), (int*)&dwSigLen);
   LOG_DEBUG("raw signature lenght %d", dwSigLen);
   if (!CryptVerifySignature(hHash, sigBlob, dwSigLen, hKey, NULL, 0)) {
      LOG_ERROR("Signature not validated!  0x%08x ", GetLastError());