From 1e166c13aaabe8d2aad1d604c77020a14dc577cd Mon Sep 17 00:00:00 2001 From: open-license-manager <rillf@maildrop.cc> Date: 周二, 16 9月 2014 06:22:21 +0800 Subject: [PATCH] refactorings --- src/library/os/linux/os-linux.cpp | 90 +++++++++++++++++--------------------------- 1 files changed, 35 insertions(+), 55 deletions(-) diff --git a/src/library/os/linux/os-linux.cpp b/src/library/os/linux/os-linux.cpp index f818677..1f65304 100644 --- a/src/library/os/linux/os-linux.cpp +++ b/src/library/os/linux/os-linux.cpp @@ -1,5 +1,19 @@ -#include <paths.h> +#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> @@ -8,37 +22,17 @@ #include <sstream> #include <sys/ioctl.h> #include <sys/stat.h> -#include "../os.hpp" -#include "../../base/public-key.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> -#include <iostream> + namespace license { using namespace std; - -vector<AdapterInfo> OsFunctions::getAdapterInfos() { - return vector<AdapterInfo>(); -} -vector<DiskInfo> OsFunctions::getDiskInfos() { - struct stat filename_stat, mount_stat; - static char discard[1024]; - char device[64], name[64], type[64]; - FILE *mounts = fopen(_PATH_MOUNTED, "r"); - - while (fscanf(mounts, "%64s %64s %64s %1024[^\n]", device, name, type, - discard) != EOF) { - if (stat(device, &mount_stat) != 0) - continue; - if (filename_stat.st_dev == mount_stat.st_rdev) - fprintf(stderr, "device: %s; name: %s; type: %s\n", device, name, - type); - } - - return vector<DiskInfo>(); -} string OsFunctions::getModuleName() { char path[2048] = { 0 }; @@ -59,27 +53,23 @@ return result; } -string OsFunctions::getUserHomePath() { - return ""; -} + bool OsFunctions::verifySignature(const char* stringToVerify, const char* signatureB64) { EVP_MD_CTX *mdctx = NULL; - char *pubKey = PUBLIC_KEY - ; + const char *pubKey = PUBLIC_KEY; 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) { - cout<<"cippa!"<<endl; throw new logic_error("Error reading public key"); } EVP_PKEY *pkey = EVP_PKEY_new(); - cout << "test:" <<EVP_PKEY_assign_RSA(pkey, rsa)<<endl; + EVP_PKEY_assign_RSA(pkey, rsa); /*BIO* bo = BIO_new(BIO_s_mem()); BIO_write(bo, pubKey, strlen(pubKey)); @@ -87,16 +77,16 @@ 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[129]; +//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 +//Can test here if len == decodeLen - if not, then return an error buffer[len] = 0; BIO_free_all(biosig); @@ -108,14 +98,12 @@ 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, - en)) { - throw new logic_error("Error initializing digest"); + int en = strlen(stringToVerify); + if (1 != EVP_DigestVerifyUpdate(mdctx, stringToVerify, en)) { + throw new logic_error("Error verifying digest"); } bool result; - int res= EVP_DigestVerifyFinal(mdctx, buffer, len); + int res = EVP_DigestVerifyFinal(mdctx, buffer, len); if (1 == res) { result = true; } else { @@ -130,20 +118,12 @@ return result; } -void OsFunctions::initialize() { - static bool initialized = false; - if (!initialized) { - initialized = true; - ERR_load_ERR_strings(); - ERR_load_crypto_strings(); - OpenSSL_add_all_algorithms(); - } -} -VIRTUALIZATION OsFunctions::getVirtualization() { - //http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html - // - bool rc = true; + +VIRTUALIZATION getVirtualization() { +//http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html +// +//bool rc = true; /*__asm__ ( "push %edx\n" "push %ecx\n" -- Gitblit v1.9.1