From dd64f246510b6c4e0618130bacbca5046c6295aa Mon Sep 17 00:00:00 2001 From: open-license-manager <rillf@maildrop.cc> Date: ćšć, 17 4æ 2014 05:08:21 +0800 Subject: [PATCH] signature --- src/library/os/linux/os-linux.cpp | 87 ++++++++++++++++++++++++++++++------------- 1 files changed, 60 insertions(+), 27 deletions(-) diff --git a/src/library/os/linux/os-linux.cpp b/src/library/os/linux/os-linux.cpp index b8ad61c..eb515b2 100644 --- a/src/library/os/linux/os-linux.cpp +++ b/src/library/os/linux/os-linux.cpp @@ -13,6 +13,7 @@ #include <openssl/evp.h> #include <openssl/bio.h> #include <openssl/pem.h> +#include <openssl/err.h> namespace license { @@ -63,18 +64,22 @@ } bool OsFunctions::verifySignature(const char* stringToVerify, - const char* signature) { + const char* signatureB64) { EVP_MD_CTX *mdctx = NULL; char *pubKey = PUBLIC_KEY ; - BIO* bio = BIO_new_mem_buf((void*) (pubKey), sizeof(pubKey)); - EVP_PKEY *pktmp = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL); + BIO* bio = BIO_new_mem_buf((void*) (pubKey), strlen(pubKey)); + RSA *rsa = PEM_read_bio_RSAPublicKey(bio, NULL, NULL, NULL); BIO_free(bio); - if (pktmp == NULL) { + if (rsa == NULL) { throw new logic_error("Error reading public key"); } + 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; @@ -83,26 +88,54 @@ //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())) { throw new logic_error("Error creating context"); } - if (1 != EVP_DigestVerifyInit(mdctx, NULL, EVP_sha256(), NULL, pktmp)) { + if (1 != EVP_DigestVerifyInit(mdctx, NULL, EVP_sha256(), NULL, pkey)) { throw new logic_error("Error initializing digest"); } - + int en=strlen(stringToVerify); if (1 != EVP_DigestVerifyUpdate(mdctx, stringToVerify, - strlen(stringToVerify))) { - throw new logic_error("Error initializing digest"); + en)) { + throw new logic_error("Error verifying digest"); } - if (1 - == EVP_DigestVerifyFinal(mdctx, (unsigned char *) signature, - (unsigned long int) strlen(signature))) { - return true; + bool result; + int res= EVP_DigestVerifyFinal(mdctx, buffer, len); + if (1 == res) { + result = true; } else { + result = false; + } + if (pkey) { + EVP_PKEY_free(pkey); + } + if (mdctx) { + EVP_MD_CTX_destroy(mdctx); + } + return result; +} - return false; +void OsFunctions::initialize() { + static bool initialized = false; + if (!initialized) { + initialized = true; + ERR_load_ERR_strings(); + ERR_load_crypto_strings(); + OpenSSL_add_all_algorithms(); } } @@ -111,20 +144,20 @@ // 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" - );*/ + "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; } -- Gitblit v1.9.1