Browse Source

Changed cert and key to use CString. Removed extra send() function.

release/0.19
JustinAJ 11 years ago
parent
commit
9d286a27a2
  1. 26
      Jupiter/SecureSocket.cpp
  2. 13
      Jupiter/SecureSocket.h

26
Jupiter/SecureSocket.cpp

@ -7,9 +7,10 @@
*/ */
#include <utility> // std::move #include <utility> // std::move
#include "SecureSocket.h"
#include <openssl/ssl.h> // OpenSSL SSL functions #include <openssl/ssl.h> // OpenSSL SSL functions
#include <openssl/err.h> // OpenSSL SSL errors #include <openssl/err.h> // OpenSSL SSL errors
#include "SecureSocket.h"
#include "CString.h"
struct Jupiter::SecureSocket::SSLData struct Jupiter::SecureSocket::SSLData
{ {
@ -17,8 +18,8 @@ struct Jupiter::SecureSocket::SSLData
SSL_CTX *context = nullptr; SSL_CTX *context = nullptr;
const SSL_METHOD *method = nullptr; const SSL_METHOD *method = nullptr;
Jupiter::SecureSocket::EncryptionMethod eMethod = ANY; Jupiter::SecureSocket::EncryptionMethod eMethod = ANY;
char *cert = nullptr; Jupiter::CStringS cert;
char *key = nullptr; Jupiter::CStringS key;
~SSLData(); ~SSLData();
}; };
@ -30,8 +31,6 @@ Jupiter::SecureSocket::SSLData::~SSLData()
SSL_free(Jupiter::SecureSocket::SSLData::handle); SSL_free(Jupiter::SecureSocket::SSLData::handle);
} }
if (Jupiter::SecureSocket::SSLData::context != nullptr) SSL_CTX_free(Jupiter::SecureSocket::SSLData::context); if (Jupiter::SecureSocket::SSLData::context != nullptr) SSL_CTX_free(Jupiter::SecureSocket::SSLData::context);
if (Jupiter::SecureSocket::SSLData::cert != nullptr) delete[] Jupiter::SecureSocket::SSLData::cert;
if (Jupiter::SecureSocket::SSLData::key != nullptr) delete[] Jupiter::SecureSocket::SSLData::key;
} }
Jupiter::SecureSocket::SecureSocket() : Jupiter::Socket() Jupiter::SecureSocket::SecureSocket() : Jupiter::Socket()
@ -147,15 +146,13 @@ bool loadCertificate(SSL_CTX *context, const char *cert, const char *key)
return true; return true;
} }
void Jupiter::SecureSocket::setCertificate(const char *cert, const char *key) void Jupiter::SecureSocket::setCertificate(const Jupiter::ReadableString &cert, const Jupiter::ReadableString &key)
{ {
Jupiter::SecureSocket::SSLdata_->cert = new char[strlen(cert) + 1]; Jupiter::SecureSocket::SSLdata_->cert = cert;
Jupiter::SecureSocket::SSLdata_->key = new char[strlen(key) + 1]; Jupiter::SecureSocket::SSLdata_->key = key;
strcpy(Jupiter::SecureSocket::SSLdata_->cert, cert);
strcpy(Jupiter::SecureSocket::SSLdata_->key, key);
} }
void Jupiter::SecureSocket::setCertificate(const char *pem) void Jupiter::SecureSocket::setCertificate(const Jupiter::ReadableString &pem)
{ {
Jupiter::SecureSocket::setCertificate(pem, pem); Jupiter::SecureSocket::setCertificate(pem, pem);
} }
@ -185,11 +182,6 @@ int Jupiter::SecureSocket::send(const char *data, size_t datalen)
return SSL_write(Jupiter::SecureSocket::SSLdata_->handle, data, datalen); return SSL_write(Jupiter::SecureSocket::SSLdata_->handle, data, datalen);
} }
int Jupiter::SecureSocket::send(const char *message)
{
return Jupiter::SecureSocket::send(message, strlen(message));
}
bool Jupiter::SecureSocket::initSSL() bool Jupiter::SecureSocket::initSSL()
{ {
SSL_load_error_strings(); SSL_load_error_strings();
@ -202,7 +194,7 @@ bool Jupiter::SecureSocket::initSSL()
ERR_print_errors_fp(stderr); ERR_print_errors_fp(stderr);
return false; return false;
} }
if (Jupiter::SecureSocket::SSLdata_->cert != nullptr) loadCertificate(Jupiter::SecureSocket::SSLdata_->context, Jupiter::SecureSocket::SSLdata_->cert, Jupiter::SecureSocket::SSLdata_->key); if (Jupiter::SecureSocket::SSLdata_->cert.size() != 0) loadCertificate(Jupiter::SecureSocket::SSLdata_->context, Jupiter::SecureSocket::SSLdata_->cert.c_str(), Jupiter::SecureSocket::SSLdata_->key.c_str());
Jupiter::SecureSocket::SSLdata_->handle = SSL_new(Jupiter::SecureSocket::SSLdata_->context); Jupiter::SecureSocket::SSLdata_->handle = SSL_new(Jupiter::SecureSocket::SSLdata_->context);
if (Jupiter::SecureSocket::SSLdata_->handle == nullptr) if (Jupiter::SecureSocket::SSLdata_->handle == nullptr)
{ {

13
Jupiter/SecureSocket.h

@ -78,7 +78,7 @@ namespace Jupiter
* @param key String containing file location of private key. * @param key String containing file location of private key.
* @return True on success, false otherwise. * @return True on success, false otherwise.
*/ */
void setCertificate(const char *cert, const char *key); void setCertificate(const Jupiter::ReadableString &cert, const Jupiter::ReadableString &key);
/** /**
* @brief Loads a certificate and key for use. * @brief Loads a certificate and key for use.
@ -86,7 +86,7 @@ namespace Jupiter
* @param pem Combined certificate/key file. * @param pem Combined certificate/key file.
* @return True on success, false otherwise. * @return True on success, false otherwise.
*/ */
void setCertificate(const char *pem); void setCertificate(const Jupiter::ReadableString &pem);
/** /**
* @brief Interface to provide simple connection establishing. * @brief Interface to provide simple connection establishing.
@ -148,15 +148,6 @@ namespace Jupiter
*/ */
virtual int send(const char *data, size_t datalen); virtual int send(const char *data, size_t datalen);
/**
* @brief Sends a null-terminated string of data across the socket.
*
* @param String containing the null-terminated data to send.
* @return Number of bytes sent on success, less than or equal to 0 otherwise.
* Note: Refer to SSL_write() for detailed return values.
*/
virtual int send(const char *msg);
/** /**
* @brief Initializes SSL on the socket. * @brief Initializes SSL on the socket.
* Note: This is only relevant when elevating an existing Socket to a SecureSocket. * Note: This is only relevant when elevating an existing Socket to a SecureSocket.

Loading…
Cancel
Save