服务器 - 客户端RSA密钥对交换

时间:2014-05-29 19:32:33

标签: c++ c rsa

我是编程新手,我已经获得了这个小项目的文凭,我需要这个小代码的帮助。这是Server->客户端C ++代码,其中服务器生成RSA Keypair并将其发送给客户端。我的问题是服务器和客户端代码编译没有任何问题但是当我启动它时,客户端没有得到任何密钥对只是空白。

这是我的服务器代码:

#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";
}

这是我的客户代码:

#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 to server ... ";
if (BIO_do_connect(conn) <= 0)
    int_error("Error connecting to server");
std::cout << "done\n";
std::cout << "Creating context ... ";
if (!(ssl = SSL_new(ctx)))
    int_error("Error creating an 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 is 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";
}

有人可以告诉我我做错了什么,如果有人可以告诉我如何分别获取我的私钥和公钥(服务器生成),那将会很棒。非常感谢...真的很感激

0 个答案:

没有答案