Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rsa #65

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ dist: trusty
sudo: required
script:
- make test
- make integration
addons:
apt:
packages:
Expand Down
9 changes: 6 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ GTEST_FLAGS=-std=c++11 -isystem $(GTEST_DIR)/include
GMOCK_FLAGS=-isystem $(GMOCK_DIR)/include

CXXFLAGS= -g $(CXXOPTIMIZE) -Wall -Werror -static-libgcc -static-libstdc++ -pthread -Wl,-Bstatic -pedantic -std=c++11 $(BOOST)
CLASSES=nginx-configparser/config_parser server/server server/webserver http/httpRequest http/httpMutableRequest http/httpResponse http/http filesystem/file_opener handlers/file_handler handlers/echo_handler handlers/request_handler handlers/not_found_handler handlers/status_handler handlers/proxy_handler handlers/blocking_handler handlers/markdown_handler handlers/cloud_file_handler

GCOV=config_parser.cc server.cc webserver.cc httpRequest.cc httpMutableRequest.cc http.cc httpResponse.cc file_opener.cc file_handler.cc echo_handler.cc request_handler.cc not_found_handler.cc status_handler.cc proxy_handler.cc blocking_handler.cc
CLASSES=nginx-configparser/config_parser server/server server/webserver http/httpRequest http/httpMutableRequest http/httpResponse http/http filesystem/file_opener handlers/file_handler handlers/echo_handler handlers/request_handler handlers/not_found_handler handlers/status_handler handlers/proxy_handler handlers/blocking_handler handlers/markdown_handler handlers/cloud_file_handler handlers/rsa_handler

CLASSES=nginx-configparser/config_parser server/server server/webserver http/httpRequest http/httpMutableRequest http/httpResponse http/http filesystem/file_opener handlers/file_handler handlers/echo_handler handlers/request_handler handlers/not_found_handler handlers/status_handler handlers/proxy_handler handlers/blocking_handler handlers/markdown_handler handlers/cloud_file_handler handlers/rsa_handler

GCOV=config_parser.cc server.cc webserver.cc httpRequest.cc httpMutableRequest.cc http.cc httpResponse.cc file_opener.cc file_handler.cc echo_handler.cc request_handler.cc not_found_handler.cc status_handler.cc proxy_handler.cc blocking_handler.cc rsa_handler.cc

UTIL_CLASSES=$(CLASSES:=.cc)
TESTS=$(CLASSES:=_test)
Expand All @@ -32,6 +35,7 @@ handlers/file_handler.cc: handlers/file_handler.h
handlers/echo_handler.cc: handlers/echo_handler.h
handlers/not_found_handler.cc: handlers/not_found_handler.h
handlers/proxy_handler.cc: handlers/proxy_handler.h
handlers/rsa_handler.cc: handlers/rsa_handler.h

server/webserver.cc: server/webserver.h

Expand Down Expand Up @@ -70,7 +74,6 @@ docker:
rm -f deploy/binary.tar
cd deploy && \
docker build -t httpserver --file ./Dockerfile.run .


clean:
find . -type f -iname \*.o -delete
Expand Down
Binary file removed handlers/markdown_handler_test
Binary file not shown.
Binary file removed handlers/proxy_handler_test
Binary file not shown.
84 changes: 84 additions & 0 deletions handlers/rsa_handler.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#include "rsa_handler.h"

#include <string>
#include <iostream>

//For the crypto-secure rng seeding
#include <boost/nondet_random.hpp>


#include "../http/httpRequest.h"
#include "../http/httpResponse.h"
#include "../http/http.h"

//The init process generates the two numbers p and q,
//and initializes the other member variables using p and q's values.
RequestHandler::Status RsaHandler::Init(const std::string& uri_prefix, const NginxConfig& config) {
//Todo
// Generate p and q using random num generator
// boost::random_device rd;
// p = rd();
//p = (p << 32) | rd();
//q = rd();
//q = (q << 32) | rd();
p = 11;
q = 13;
n = p*q;
t = (p-1)*(q-1);
//Now we calculate e and d
//We hardcode e as is common practice, using a number
//of possible e's, as e must be relatively prime to t.
//Else, we start incrementing e until a found number works.
//Thi is method used by GnuPG v1.2.3
if(t % 41 != 0)
e = 41;
else if(t % 257 != 0)
e = 257;
else{
int temp1 = 0, temp2 = 0;
e = 65537;
while (gcdExtended(e, t, &temp1, &temp2) != 1)
e += 2;
}
//Find d using EEA
int x = 0, y = 0;
gcdExtended(e,t,&x,&y);
d = x;
std::cout << "The initialization values for the RSA Handler:\n";
std::cout << "n: " << n << ", t: " << t << ", e: " << e << ", d: " << d <<"\n";
return OK;
}

//used as reference for below: http://www.sanfoundry.com/cpp-program-implement-rsa-algorithm/
RequestHandler::Status RsaHandler::HandleRequest(const Request& request,
Response* response) {
m = (request.raw_request());
std::size_t lengthm = m.length();
long int encodedArray[128];
std::string output = "";
long int plain;
long int cipher;
std::size_t u;
for(u = 0; u < lengthm; u++) {
plain = m[u];
//Conversion to number
plain = plain - 96;
long k = 1;
for(long j = 0; j < e; j++) {
k = k*plain;
k = k % n;
}
cipher = k + 96;
encodedArray[u] = cipher;
}

for(int temp = 0; temp < 128; temp++){
output += std::to_string(encodedArray[temp]);
}
std::cout << "Encrypted Output:\n";
std::cout <<output << "\n";
response->SetStatus(Response::OK);
response->AddHeader("Content-Type", http::mime_type::ContentTypeAsString(http::mime_type::CONTENT_TYPE_TEXT_PLAIN));
response->SetBody(output.c_str());
return OK;
}
83 changes: 83 additions & 0 deletions handlers/rsa_handler.cc~
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include "rsa_handler.h"

#include <string>
#include <iostream>

//For the crypto-secure rng seeding
#include <boost/nondet_random.hpp>


#include "../http/httpRequest.h"
#include "../http/httpResponse.h"
#include "../http/http.h"

//The init process generates the two numbers p and q,
//and initializes the other member variables using p and q's values.
RequestHandler::Status RsaHandler::Init(const std::string& uri_prefix, const NginxConfig& config) {
//Todo
// Generate p and q using random num generator
// boost::random_device rd;
p = 11;
q = 13;
n = p*q;
t = (p-1)*(q-1);
//Now we calculate e and d
//We hardcode e as is common practice, using a number
//of possible e's, as e must be relatively prime to t.
//Else, we start incrementing e until a found number works.
//Thi is method used by GnuPG v1.2.3
if(t % 41 != 0)
e = 41;
else if(t % 257 != 0)
e = 257;
else{
int temp1 = 0, temp2 = 0;
e = 65537;
while (gcdExtended(e, t, &temp1, &temp2) != 1)
e += 2;
}
//Find d using EEA
int x = 0, y = 0;
gcdExtended(e,t,&x,&y);
d = x;
std::cout << "\nThe initialization values for the RSA Handler:\n";
std::cout << "n: " << n << ", t: " << t << ", e: " << e << ", d: " << d <<"\n";
return OK;
}

RequestHandler::Status RsaHandler::HandleRequest(const Request& request,
Response* response) {
long int encodedArray[128];
std::string output = "";
long int plain;
long int cipher;
m = (request.raw_request());
EorD = m[0];
m.erase(0,1);
std::size_t lengthm = m.length();
std::size_t u;
for(u = 0; u < lengthm; u++) {
plain = m[u];
//Conversion to number
plain = plain - 96;
long k = 1;
for(long j = 0; j < e; j++) {
k = k*plain;
k = k % n;
}
cipher = k + 96;
encodedArray[u] = cipher;
}

for(int temp = 0; temp < 128; temp++){
output += std::to_string(encodedArray[temp]);
}
std::cout <<output << "\n";
response->SetStatus(Response::OK);
std::cout << "!!@EFAE";
response->AddHeader("Content-Type", http::mime_type::ContentTypeAsString(http::mime_type::CONTENT_TYPE_TEXT_PLAIN));
std::cout << "!!@EFAE";
response->SetBody(output.c_str());
std::cout << "!!@EFAE";
return OK;
}
65 changes: 65 additions & 0 deletions handlers/rsa_handler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#ifndef RSA_HANDLER_H
#define RSA_HANDLER_H

#include <string>

#include "../nginx-configparser/config_parser.h"
#include "../http/httpRequest.h"
#include "../http/httpResponse.h"
#include "request_handler.h"

class RsaHandler : public RequestHandler {
public:
Status Init(const std::string& uri_prefix,
const NginxConfig& config);

bool isPrime(long long x){
int sqrtX = std::sqrt(x);
for (int i = 2; i < sqrtX; i++)
if (x % i == 0)
return false;
return true;
}

//Extended Eucliden algorithm used to determine d
//see : http://www.geeksforgeeks.org/basic-and-extended-euclidean-algorithms
int gcdExtended(int a, int b, int *x, int *y)
{
// Base Case
if (a == 0)
{
*x = 0;
*y = 1;
return b;
}

int x1, y1; // To store results of recursive call
int gcd = gcdExtended(b%a, a, &x1, &y1);

// Update x and y using results of recursive
// call
*x = y1 - (b/a) * x1;
*y = x1;

return gcd;
}

//Handle Request encrypts if the first letter of the
//request body is e, and decrypts if it is d, fails otherwise.
//The 'message' in both cases is the rest of the message.
Status HandleRequest(const Request& request,
Response* response);

private:
long long p, q, n, t, e, d;
std::string m;
//p, q are initial prime numbers. n, the product of p and q, is the modulus.
//t is Euler's Totient, computed as (n-1)(p-1).
//e and d are the public and private exponents used in the modular arithmetic
//m is the input message.

};

REGISTER_REQUEST_HANDLER(RsaHandler);

#endif
43 changes: 43 additions & 0 deletions handlers/rsa_handler.h~
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#ifndef RSA_HANDLER_H
#define RSA_HANDLER_H

#include <string>

#include "../nginx-configparser/config_parser.h"
#include "../http/httpRequest.h"
#include "../http/httpResponse.h"
#include "request_handler.h"

class RsaHandler : public RequestHandler {
public:
Status Init(const std::string& uri_prefix,
const NginxConfig& config);

bool isPrime(long long x){
int sqrtX = std::sqrt(x);
for (int i = 2; i < sqrtX; i++)
if (x % i == 0)
return false;
return true;
}

//Handle Request encrypts if the first letter of the
//request body is e, and decrypts if it is d, fails otherwise.
//The 'message' in both cases is the rest of the message.
Status HandleRequest(const Request& request,
Response* response);

private:
long long p, q, n, t, e, d;
std::string m;
std::string EorD;
//p, q are initial prime numbers. n, the product of p and q, is the modulus.
//t is Euler's Totient, computed as (n-1)(p-1).
//e and d are the public and private exponents used in the modular arithmetic
//m is the input message.

};

REGISTER_REQUEST_HANDLER(RsaHandler);

#endif
Empty file added handlers/rsa_handler_test.cc
Empty file.
11 changes: 7 additions & 4 deletions handlers/status_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ RequestHandler::Status StatusHandler::HandleRequest(const Request& request,
if (status_ == nullptr) {
return RequestHandler::MISCONFIGURED_HANDLER;
}
//status_->Status_.stsMx->lock();

ServerStatus::Status status = status_->GetStatus();


response->SetStatus(Response::OK);
response->AddHeader("Content-Type", http::mime_type::ContentTypeAsString(http::mime_type::CONTENT_TYPE_TEXT_HTML));
response->SetBody(StatusToHtml(status));

//status_->Status_.stsMx->unlock();
return OK;
}

Expand All @@ -33,15 +34,17 @@ RequestHandler::Status StatusHandler::InitStatus(ServerStatus* status) {
if (status == nullptr) {
return RequestHandler::MISCONFIGURED_HANDLER;
}
status->GetStatus().stsMx->lock();
status_ = status;
status->GetStatus().stsMx->unlock();
return OK;
}



std::string StatusHandler::StatusToHtml(const ServerStatus::Status& status) {
status.stsMx->lock();
std::string html_string;

html_string.append("<!DOCTYPE HTML>\n");
html_string.append("<html>\n");
html_string.append("<head>Status Page</head>");
Expand Down Expand Up @@ -74,6 +77,6 @@ std::string StatusHandler::StatusToHtml(const ServerStatus::Status& status) {

html_string.append("</body>\n");
html_string.append("</html>\n");

status.stsMx->unlock();
return html_string;
}
25 changes: 25 additions & 0 deletions integration_config
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
port 2029; # This is also a comment.

path /serve StaticHandler {
root files_served/;
}

path /echo EchoHandler {}

path /status StatusHandler {}

path /proxy ProxyHandler {
host www.ucla.edu;
port 80;
}

path /cloud CloudFileHandler {
bucket cs130-s3;
}

path /block BlockingHandler {}

path /rsa RsaHandler {}

# Default response handler if no handlers match.
default NotFoundHandler {}
Loading