ClienteTFG/conexion_ssl.cpp

40 lines
1.0 KiB
C++
Raw Normal View History

2020-05-08 12:42:02 +02:00
#include "conexion_ssl.h"
#include <openssl/crypto.h>
#include <openssl/x509.h>
#include <openssl/pem.h>
#include <openssl/err.h>
#define CHK_NULL(x) if ((x)==NULL) exit (1)
#define CHK_ERR(err,s) if ((err)==-1) { perror(s); exit(1); }
#define CHK_SSL(err) if ((err)==-1) { ERR_print_errors_fp(stderr); exit(2); }
conexion_ssl::conexion_ssl(config_reader &conf) : conexion(conf)
{
int err;
SSL_CTX* ctx;
const SSL_METHOD *meth;
OpenSSL_add_ssl_algorithms();
meth = TLS_client_method();
SSL_load_error_strings();
ctx = SSL_CTX_new (meth);
CHK_NULL(ctx);
this->ssl=SSL_new(ctx);
SSL_set_fd(this->ssl,this->fd);
err = SSL_connect (ssl);
CHK_SSL(err)
}
ssize_t conexion_ssl::read_string(std::string &entrada, int size){
char* buffer = new char[size+1];
ssize_t ret = SSL_read (this->ssl, buffer, size);
buffer[ret]='\0';
entrada = std::string(buffer);
return ret;
}
ssize_t conexion_ssl::write_string(std::string entrada){
return SSL_write(this->ssl,entrada.data(),sizeof (entrada.data()));
}