23 #include "runtime.hpp"
25 #include "yassl_int.hpp"
26 #include "handshake.hpp"
29 #include "openssl/ssl.h"
48 Base() : method_(0), ctx_(0), ssl_(0), ca_(0), cert_(0), key_(0), dh_(0)
53 if (dh_) DH_free(dh_);
65 void SetUpBase(
Base& base, ConnectionEnd end, SOCKET_T s)
68 base.ctx_ =
new SSL_CTX(base.method_);
71 if (SSL_CTX_load_verify_locations(base.ctx_,
72 base.ca_, 0) != SSL_SUCCESS)
throw(0);
74 if (SSL_CTX_use_certificate_file(base.ctx_,
75 base.cert_, SSL_FILETYPE_PEM) != SSL_SUCCESS)
throw(0);
77 if (SSL_CTX_use_PrivateKey_file(base.ctx_, base.key_,
78 SSL_FILETYPE_PEM) != SSL_SUCCESS)
throw(0);
80 if (end == server_end) SetDH(base);
82 base.ssl_ =
new SSL(base.ctx_);
83 base.ssl_->useSocket().set_fd(s);
87 void SetDH(Base& base)
89 static unsigned char dh512_p[] =
91 0xDA,0x58,0x3C,0x16,0xD9,0x85,0x22,0x89,0xD0,0xE4,0xAF,0x75,
92 0x6F,0x4C,0xCA,0x92,0xDD,0x4B,0xE5,0x33,0xB8,0x04,0xFB,0x0F,
93 0xED,0x94,0xEF,0x9C,0x8A,0x44,0x03,0xED,0x57,0x46,0x50,0xD3,
94 0x69,0x99,0xDB,0x29,0xD7,0x76,0x27,0x6B,0xA2,0xD3,0xD4,0x12,
95 0xE2,0x18,0xF4,0xDD,0x1E,0x08,0x4C,0xF6,0xD8,0x00,0x3E,0x7C,
99 static unsigned char dh512_g[] =
104 if ( (base.dh_ = DH_new()) ) {
105 base.dh_->p = BN_bin2bn(dh512_p,
sizeof(dh512_p), 0);
106 base.dh_->g = BN_bin2bn(dh512_g,
sizeof(dh512_g), 0);
108 if (!base.dh_->p || !base.dh_->g) {
112 SSL_CTX_set_tmp_dh(base.ctx_, base.dh_);
116 void NewCopy(
char*& dst,
const char* src)
118 size_t len = strlen(src) + 1;
121 strncpy(dst, src, len);
135 Client::~Client() {
delete pimpl_; }
138 int Client::Connect(SOCKET_T s)
140 SetUpBase(pimpl_->base_, client_end, s);
141 return SSL_connect(pimpl_->base_.ssl_);
145 int Client::Write(
const void* buffer,
int sz)
147 return sendData(*pimpl_->base_.ssl_, buffer, sz);
151 int Client::Read(
void* buffer,
int sz)
153 Data data(min(sz, MAX_RECORD_SIZE), static_cast<opaque*>(buffer));
154 return receiveData(*pimpl_->base_.ssl_, data);
158 void Client::SetCA(
const char*
name)
160 NewCopy(pimpl_->base_.ca_, name);
164 void Client::SetCert(
const char*
name)
166 NewCopy(pimpl_->base_.cert_, name);
170 void Client::SetKey(
const char*
name)
172 NewCopy(pimpl_->base_.key_, name);
187 Server::~Server() {
delete pimpl_; }
190 int Server::Accept(SOCKET_T s)
192 SetUpBase(pimpl_->base_, server_end, s);
193 return SSL_accept(pimpl_->base_.ssl_);
197 int Server::Write(
const void* buffer,
int sz)
199 return sendData(*pimpl_->base_.ssl_, buffer, sz);
203 int Server::Read(
void* buffer,
int sz)
205 Data data(min(sz, MAX_RECORD_SIZE), static_cast<opaque*>(buffer));
206 return receiveData(*pimpl_->base_.ssl_, data);
210 void Server::SetCA(
const char*
name)
212 NewCopy(pimpl_->base_.ca_, name);
216 void Server::SetCert(
const char*
name)
218 NewCopy(pimpl_->base_.cert_, name);
222 void Server::SetKey(
const char*
name)
224 NewCopy(pimpl_->base_.key_, name);