c++ - Server - Client RSA keypair exchange -


i'm new programming , i've got little project going diploma , need on little code. server->client c++ code server generates , sends rsa keypair client. problem both server , client codes compiled without problem when start client side doesn't keypair blank.

here server code:

#include <iostream> #include <cstdlib> #include <openssl/ssl.h> #include <openssl/err.h>   #include <openssl/rsa.h> #include <openssl/pem.h> #include <stdio.h> #include <string.h>  #define key_length  1024 #define pub_exp     3 #define print_keys #define write_to_file  using std::exit; #define int_error(msg) handle_error(__file__, __line__, msg) void handle_error(const char* file, int lineno, const char* msg) { std::cerr << "\n** " << file << ":" << lineno << " " << msg << "\n"; err_print_errors_fp(stderr); exit(-1); } void init_openssl() { std::cout << "initializng openssl library ... "; if (!ssl_library_init())     int_error("openssl initialization failed"); std::cout << "done\n"; ssl_load_error_strings(); } dh* setup_dh() { dh* dh = dh_new(); if (!dh)     int_error("dh_new failed"); std::cout << "generating dh parameters ... "; if (!dh_generate_parameters_ex(dh, 2, dh_generator_2, 0))     int_error("dh_generate_parameters_ex failed"); std::cout << "done\n"; std::cout << "checking dh parameters ... "; int codes = 0; if (!dh_check(dh, &codes))     int_error("dh_check failed"); std::cout << "done\n"; std::cout << "generating dh keys ... "; if (!dh_generate_key(dh))     int_error("dh_generate_key failed"); std::cout << "done\n"; return dh; } ssl_ctx* setup_ctx() { ssl_ctx* ctx; std::cout << "creating context ... "; ctx = ssl_ctx_new(tlsv1_server_method()); if (!ctx)     int_error("ssl_ctx_new failed"); std::cout << "done\n"; dh* dh = setup_dh(); std::cout << "setting dh parameters ... "; ssl_ctx_set_tmp_dh(ctx, dh);     std::cout << "done\n"; std::cout << "setting cipher list ... "; if (ssl_ctx_set_cipher_list(ctx, "adh-aes256-sha") != 1)     int_error("error setting cipher list (no valid ciphers)"); std::cout << "done\n"; return ctx; } int main() { init_openssl(); bio *acc, *client; ssl* ssl; ssl_ctx* ctx; ctx = setup_ctx();  size_t pri_len;         size_t pub_len;             char   *pri_key;            char   *pub_key;      std::cout << "generating rsa (%d bits) keypair...\n", 1024; rsa *keypair = rsa_generate_key(1024, 3, null, null);  bio *pri = bio_new(bio_s_mem()); bio *pub = bio_new(bio_s_mem());  pem_write_bio_rsaprivatekey(pri, keypair, null, null, 0, null, null); pem_write_bio_rsapublickey(pub, keypair);  pri_len = bio_pending(pri); pub_len = bio_pending(pub);  pri_key = (char*)malloc(pri_len + 1); pub_key = (char*)malloc(pub_len + 1);  bio_read(pri, pri_key, pri_len); bio_read(pub, pub_key, pub_len);  pri_key[pri_len] = '\0'; pub_key[pub_len] = '\0';  std::cout << "creating server socket ... "; acc = bio_new_accept("*:5055"); if (!acc)     int_error("error creating server socket"); std::cout << "done\n"; std::cout << "binding server socket ... "; if (bio_do_accept(acc) <= 0)     int_error("error binding server socket"); std::cout << "done\n"; while (true) {     std::cout << "accepting connections ...\n";     if (bio_do_accept(acc) <= 0)         int_error("error accepting connection");     client = bio_pop(acc);     std::cout << "creating context ... ";     if (!(ssl = ssl_new(ctx)))         int_error("error creating ssl context");     std::cout << "done\n";     ssl_set_bio(ssl, client, client);     if (ssl_accept(ssl) <= 0)         int_error("error accepting ssl connection");     std::cout << "ssl connection opened: " << ssl_get_cipher(ssl) << " " <<      ssl_get_cipher_version(ssl) << " (" << ssl_get_cipher_bits(ssl, 0) << "      bits)\n";     char buff[256] = {0};     int r = ssl_read(ssl, buff, sizeof buff);        if (r > 0) {         std::cout << buff;         ssl_write(ssl, keypair, 1024 );     }     ssl_shutdown(ssl);     ssl_free(ssl);     std::cout << "ssl connection finished\n"; } ssl_ctx_free(ctx); bio_free(acc); std::cout << "server closed\n"; } 

here client code:

#include <iostream> #include <cstdlib> #include <string> #include <fstream> #include <openssl/ssl.h> #include <openssl/err.h> using namespace std; using std::exit;  #define int_error(msg) handle_error(__file__, __line__, msg) void handle_error(const char* file, int lineno, const char* msg) { std::cerr << "** " << file << ":" << lineno << " " << msg << "\n"; err_print_errors_fp(stderr); exit(-1); } void init_openssl() { std::cout << "initializng openssl library ... "; if (!ssl_library_init())     int_error("openssl initialization failed"); std::cout << "done\n"; ssl_load_error_strings(); } ssl_ctx* setup_ctx() { ssl_ctx* ctx; std::cout << "creating context ... "; ctx = ssl_ctx_new(tlsv1_client_method()); if (!ctx)     int_error("ssl_ctx_new failed"); std::cout << "done\n"; std::cout << "setting cipher list ... "; if (ssl_ctx_set_cipher_list(ctx, "adh-aes256-sha") != 1)     int_error("error setting cipher list (no valid ciphers)"); std::cout << "done\n"; return ctx; } int main() { init_openssl(); bio* conn; ssl* ssl; ssl_ctx* ctx; ofstream myfile; ctx = setup_ctx(); std::cout << "creating connection ... "; conn = bio_new_connect("192.168.1.10:5055"); if (!conn)     int_error("error creating connection"); std::cout << "done\n"; std::cout << "connecting server ... "; if (bio_do_connect(conn) <= 0)     int_error("error connecting server"); std::cout << "done\n"; std::cout << "creating context ... "; if (!(ssl = ssl_new(ctx)))     int_error("error creating ssl context"); std::cout << "done\n"; ssl_set_bio(ssl, conn, conn); std::cout << "opening connection ... "; if (ssl_connect(ssl) <= 0)     int_error("error connecting ssl object"); std::cout << "done\n"; ssl_write(ssl, "the client connected", 26); char buff[64] = {0}; int bread = ssl_read(ssl, buff, sizeof buff); if (bread > 0)     std::cout << "the key has been recieved\n";       myfile.open ("keypair.pem");       myfile << buff;       myfile.close(); ssl_shutdown(ssl); ssl_free(ssl); std::cout << "ssl connection finished\n"; ssl_ctx_free(ctx); std::cout << "client finished\n"; } 

can show me did wrong , great if can tell me how can private key , public keys(server generated) separately. lot guys...really appreciate it


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -