Skip to content

Commit

Permalink
extracted pks_s, sk_s, cert_s, added print statements on both sides t…
Browse files Browse the repository at this point in the history
…o confirm keys and signature
  • Loading branch information
AndreasG9 committed Nov 22, 2020
1 parent a529a26 commit 7d62298
Show file tree
Hide file tree
Showing 2 changed files with 203 additions and 19 deletions.
72 changes: 58 additions & 14 deletions Lab6/ca.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@
#include "cryptopp820/files.h"

#define DEFAULT_PORT_NUM 8000
#define BUFFER_LENGTH 512
#define BUFFER_LENGTH 1024

#define ID_CA "ID-CA"
#define ID_S "ID-Server"
#define LIFETIME_SESS 86400

long long int get_epoch_time_seconds();
std::string gen_tmp_key();
std::string encode_hex(std::string ct);

std::string encrypt_rsa(CryptoPP::RSA::PublicKey key, CryptoPP::AutoSeededRandomPool rng, std::string plain);
std::string decrypt_rsa(CryptoPP::RSA::PrivateKey key, std::string cipher);
std::string sign_rsa(CryptoPP::RSA::PrivateKey SK_CA, std::string message);
Expand Down Expand Up @@ -156,16 +159,18 @@ int main(int argc, char** argv) {

// Print out ciphertext and temp des key
std::cout << "\n***************************************************************" << std::endl;
std::cout << "(CA) received ciphertext: " << ciphertext << std::endl;
std::cout << "(CA) received K_TMP1 " << plaintext.substr(0, 8) << std::endl;
//std::cout << "(CA) received ciphertext: " << ciphertext << std::endl;
std::cout << "(CA) received ciphertext (HEX encoded): " << encode_hex(ciphertext) << std::endl;
std::cout << "(CA) received K_TMP1: " << plaintext.substr(0, 8) << std::endl;
std::cout << "***************************************************************\n" << std::endl;


// ===================== STEP 2 ===================================================

// Extract DES_K_TMP1 from plaintext
std::string k_tmp1 = plaintext.substr(0, 8); // been using 8-byte keys

// Generate new public/private key pair for S (params defined above, key-size 1024)
// Generate new public/private key pair for S (params defined above, key-size 512)
CryptoPP::RSA::PrivateKey SK_S(params); // private key
CryptoPP::RSA::PublicKey PK_S(params); // public key

Expand All @@ -178,42 +183,67 @@ int main(int argc, char** argv) {
CryptoPP::StringSink sink2(encoded_SK_S);
SK_S.DEREncode(sink2);

std::cout << "size pk_s: " << encoded_PK_S.size() << "\t size sk_s: " << encoded_SK_S.size() << std::endl;

// Build Cert_S = Sign_SK_CA [ID_S || IC_CA || PK_S]
std::string sign_sk_ca = ID_S;
sign_sk_ca += ID_CA;
sign_sk_ca += encoded_PK_S;
std::string cert_s = ID_S;
cert_s += ID_CA;
cert_s += encoded_PK_S;

// Sign w/ SK_CA
cert_s = sign_rsa(SK_CA, cert_s);

// Sign
sign_sk_ca = sign_rsa(SK_CA, sign_sk_ca);
std::cout << "size: cert_s :" << cert_s.size() << std::endl;

// Build PK_S || SK_S || CERT_S || ID_S || TS_2
plaintext.clear();
plaintext += std::to_string(encoded_PK_S.size());
plaintext += encoded_PK_S;
plaintext += std::to_string(encoded_SK_S.size());
plaintext += encoded_SK_S;
plaintext += sign_sk_ca;
plaintext += std::to_string(cert_s.size());
plaintext += cert_s;
plaintext += ID_S;
plaintext += std::to_string(get_epoch_time_seconds());

// Encrypt using DES_K_TMP1
ciphertext = encrypt_des(k_tmp1, plaintext);

// SEND encrypted msg (new key pair, cert_s, etc...) to client
retval_send = send(ca_socket, ciphertext.c_str(), ciphertext.size(), 0);
std::cout << "ct size: " << ciphertext.size() << std::endl;

// SEND encrypted msg (new key pair, cert_s, etc...) to server
retval_send = send(server_socket, ciphertext.c_str(), ciphertext.size(), 0);
if (retval_send == SOCKET_ERROR) {
std::cout << "Error, failed to send" << std::endl;
closesocket(ca_socket);
closesocket(server_socket);
WSACleanup();
return 1;
}

// public key's {n, e}
const CryptoPP::Integer& public_n = PK_S.GetModulus();
const CryptoPP::Integer& public_e = PK_S.GetPublicExponent();

// private key's {n, d}
const CryptoPP::Integer& private_n = SK_S.GetModulus();
const CryptoPP::Integer& private_d = SK_S.GetPrivateExponent();


// Print out sent ciphertext, generated key pair, and Cert_s
std::cout << "\n***************************************************************" << std::endl;
std::cout << "(CA) sent ciphertext (HEX encoded): " << encode_hex(ciphertext) << std::endl << std::endl;
std::cout << "(CA) generated Public Key PK_S: " << "\nn:" << public_n << "\ne: " << public_e << std::endl;
std::cout << "(CA) generated Private Key SK_S: " << "\nn:" << private_n << "\nd: " << private_d << std::endl << std::endl;
std::cout << "(CA) Cert_s (HEX encoded): " << encode_hex(cert_s) << std::endl;
std::cout << "***************************************************************\n" << std::endl;

break; // CA work is complete (server.cpp will disconnect ...)
}
}

if (retval_receive <= 0) break; // server disconnected (likely connected to C, as work with CA done))
}


// cleanup
shutdown(server_socket, SD_SEND);
closesocket(server_socket);
Expand All @@ -231,6 +261,20 @@ long long int get_epoch_time_seconds() {
return time;
}

std::string encode_hex(std::string ct) {

std::string encoded;

CryptoPP::StringSource(ct, true,
new CryptoPP::HexEncoder(
new CryptoPP::StringSink(encoded)
)
);

return encoded;

}

std::string encrypt_rsa(CryptoPP::RSA::PublicKey key, CryptoPP::AutoSeededRandomPool rng, std::string plain) {
// RSA encryption (RSAES encryption scheme (OAEP using SHA-256). use CryptoPP filters to do so ...)

Expand Down
150 changes: 145 additions & 5 deletions Lab6/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,21 @@
#include "cryptopp820/files.h"

#define DEFAULT_PORT_NUM 8000
#define BUFFER_LENGTH 512
#define BUFFER_LENGTH 1024

#define ID_CA "ID-CA"
#define ID_S "ID-Server"
#define LIFETIME_SESS 86400

long long int get_epoch_time_seconds();
std::string gen_tmp_key();
std::string encode_hex(std::string ct);

std::string encrypt_rsa(CryptoPP::RSA::PublicKey key, std::string plain);
std::string decrypt_rsa(CryptoPP::RSA::PrivateKey key, CryptoPP::AutoSeededRandomPool rng, std::string cipher);

std::string decrypt_des(std::string key_string, std::string ciphertext);


int main(int argc, char** argv) {

Expand Down Expand Up @@ -114,14 +119,16 @@ int main(int argc, char** argv) {
char message_receive[BUFFER_LENGTH] = { 0 };
int retval_send = 0, retval_receive = 0;

std::string plaintext, ciphertext;
std::string plaintext, ciphertext, CERT_S;
CryptoPP::RSA::PrivateKey SK_S;
CryptoPP::RSA::PublicKey PK_S;


while (true) {

// ===================== STEP 1, Register w/ CA ===================================================

// temp DES key (testing) CHANGE LATER
std::string temp_des = "aBcjEFg4";
std::string temp_des = gen_tmp_key();

plaintext.clear();
plaintext += temp_des;
Expand All @@ -147,17 +154,101 @@ int main(int argc, char** argv) {
return 1;
}

std::cout << "\n(S) Waiting to receive a message from CA ... \n" << std::endl;

// Print out sent ciphertext and temp des key
std::cout << "\n***************************************************************" << std::endl;
std::cout << "(S) sent ciphertext (HEX encoded): " << encode_hex(ciphertext) << std::endl;
std::cout << "(S) generated K_TMP1: " << temp_des << std::endl;
std::cout << "***************************************************************\n" << std::endl;

// if retval receive <=0

std::cout << "\n(S) Waiting to receive a message from CA ... \n" << std::endl;

while ((retval_receive = recv(connected_socket, message_receive, BUFFER_LENGTH, 0)) > 0) {

if (retval_receive > 0) {
ciphertext.clear();
ciphertext.append(message_receive, retval_receive);


// Decrypt ct using K_tmp1 to get bunch of info .. including cert, and our public/private key pair
plaintext = decrypt_des(temp_des, ciphertext);


// Extract PK_S (using 1024 RSA::PublicKey/RSA::PrviateKey sizes, but encoded to be sent as c++ strings, so exact size tricky
// but the first three bytes will contain the size of the one key (and same for next key)
// encoded, each key will then be between 630-634 bytes or somewhere between there, but will differ each time execute as different keys generated)
int key_length = std::stoi(plaintext.substr(0, 3));
int current = key_length + 3;
CryptoPP::StringSource ss(plaintext.substr(3, key_length), true);
PK_S.BERDecode(ss); // Decode key (currently string), to get RSA::PublicKey for S

// Extract SK_S
key_length = std::stoi(plaintext.substr(current, 3));
current += 3;
current += key_length;
int start = 3 + std::stoi(plaintext.substr(0, 3)) + 3;
CryptoPP::StringSource ss2(plaintext.substr(start, key_length), true);
SK_S.BERDecode(ss2); // Decode key (currently string), to get RSA::PrivateKey for S

// Extract Cert_S
key_length = std::stoi(plaintext.substr(current, 3));
current += 3;
CERT_S = plaintext.substr(current, key_length);

// public key's {n, e}
const CryptoPP::Integer& public_n = PK_S.GetModulus();
const CryptoPP::Integer& public_e = PK_S.GetPublicExponent();

// private key's {n, d}
const CryptoPP::Integer& private_n = SK_S.GetModulus();
const CryptoPP::Integer& private_d = SK_S.GetPrivateExponent();

// Print recieved key pair and Cert_s
std::cout << "\n***************************************************************" << std::endl;
std::cout << "(S) received ciphertext (HEX encoded): " << encode_hex(ciphertext) << std::endl;
std::cout << "(S) received Public Key PK_S: " << "\nn:" << public_n << "\ne: " << public_e << std::endl;
std::cout << "(S) received Private Key SK_S: " << "\nn:" << private_n << "\nd: " << private_d << std::endl;
std::cout << "(S) Cert_s (HEX encoded): " << encode_hex(CERT_S) << std::endl;
std::cout << "***************************************************************\n" << std::endl;
}
break;
}
break;
}


// close old socket


// CONNECT to C (client.cpp)


while (true) {

// receive ID || TS3


// Extract TS_4

// =========================== STEP 4 ==========================
//plaintext.clear();
//plaintext += PK_S;
//plaintext += CERT_S;
//plaintext += "TS_4";

// send plaintext message

// receive RSA_PK_S[K_TMP_2 || ID_C || IP_C || PORT_C || TS_%]

// Extract K_TMP2



break;
}

//shutdown(connected_socket2, SD_SEND);
//closesocket(connected_socket2);
//WSACleanup();
Expand All @@ -175,6 +266,34 @@ long long int get_epoch_time_seconds() {
return time;
}

std::string gen_tmp_key() {
// generate 8-byte string to be used as temp. des key

std::string chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
srand(time(0));
std::string temp;

for (int i = 0; i < 8; ++i) {
temp += chars[rand() % chars.size()];
}

return temp;
}

std::string encode_hex(std::string ct) {

std::string encoded;

CryptoPP::StringSource(ct, true,
new CryptoPP::HexEncoder(
new CryptoPP::StringSink(encoded)
)
);

return encoded;

}

std::string encrypt_rsa(CryptoPP::RSA::PublicKey key, std::string plain) {
// RSA encryption (RSAES encryption scheme (OAEP using SHA-256). use CryptoPP filters to do so ...)

Expand Down Expand Up @@ -207,7 +326,28 @@ std::string decrypt_rsa(CryptoPP::RSA::PrivateKey key, CryptoPP::AutoSeededRando
return plain;
}

std::string decrypt_des(std::string key_string, std::string ciphertext) {

CryptoPP::SecByteBlock key((const unsigned char*)(key_string.data()), key_string.size());
std::string plaintext;

try {

CryptoPP::ECB_Mode< CryptoPP::DES >::Decryption decrypt;
decrypt.SetKey(key, key.size());

// Decrypt, remove padding if needed
CryptoPP::StringSource s(ciphertext, true,
new CryptoPP::StreamTransformationFilter(decrypt,
new CryptoPP::StringSink(plaintext)
)
);
}

catch (const CryptoPP::Exception& err) {
std::cerr << "ERROR probably exceeded the buffer length\n" << err.what() << std::endl;
exit(1);
}

return plaintext;
}

0 comments on commit 7d62298

Please sign in to comment.