SSL WebServer Qt -
i've modified code in incomingconnection, when connect browser on _https://localhost:8080, server not connect...shows error message:
qsslsocket::startserverencryption: cannot start handshake on non-plain connection
i create certificate console these commands:
$openssl genrsa -des3 -out server.key 4096 $openssl req -new -key server.key -out server.csr $openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt $openssl rsa -in server.key -out server.key.insecure
server.cpp
#include <qtnetwork> #include <qmessagebox> #include "server.h" server::server(qobject *parent) : qtcpserver(parent) { server_socket.setprotocol(qssl::sslv3); qbytearray key; qbytearray cert; qfile file_key("server.key"); if(file_key.open(qiodevice::readonly)) { key = file_key.readall(); file_key.close(); } else { qdebug() << file_key.errorstring(); } qfile file_cert("server.crt"); if(file_cert.open(qiodevice::readonly)) { cert = file_cert.readall(); file_cert.close(); } else { qdebug() << file_cert.errorstring(); } qsslkey ssl_key(key, qssl::rsa, qssl::pem, qssl::privatekey, "server"); qsslcertificate ssl_cert(cert); server_socket.addcacertificate(ssl_cert); server_socket.setlocalcertificate(ssl_cert); server_socket.setprivatekey(ssl_key); connect( &server_socket, signal(sslerrors(qlist<qsslerror>)), this, slot(sslerror(qlist<qsslerror>)) ); connect(&server_socket,signal(encrypted()),this,slot(encrypted())); server_socket.setsocketoption(qabstractsocket::keepaliveoption, true ); qdebug() << "server started..."; } server::~server() { server_socket.disconnectfromhost(); server_socket.waitfordisconnected(); } void server::tcpready() { qbytearray array = server_socket.read( server_socket.bytesavailable() ); qdebug() << "tcpreplay"; } void server::encrypted() { connect( &server_socket, signal(readyread()), this, slot(tcpready()) ); emit connection_established("connection established!"); } void server::sslerror(qlist<qsslerror> errors) { qstring errostr=""; foreach (const qsslerror &e, errors) errostr.append(e.errorstring()).append("\n"); qmessagebox::warning( (qwidget *)this->parent(), tr("error"),errostr ); server_socket.ignoresslerrors(); } bool server::start_listen(int port_no) { if( !this->listen( qhostaddress::any, port_no ) ) { qmessagebox::warning( (qwidget *)this->parent(), tr("error!"), tr("cannot listen port %1").arg(port_no) ); } else return true; } void server::incomingconnection(int descriptor) { if(server_socket.state()!=qabstractsocket::unconnectedstate) return; if( !server_socket.setsocketdescriptor( descriptor ) ) { qmessagebox::warning( (qwidget *)this->parent(), tr("error!"), tr("socket error!") ); return; } else { server_socket.startserverencryption(); qdebug() << "incoming connection from..." << server_socket.peeraddress().tostring(); qdebug() << "waiting encryption..."; if(server_socket.waitforencrypted()) { emit statusmessage("encrypted connection!"); server_socket.write("http/1.1 200 ok\r\n" "content-type: text/plain\r\n" "content-length: 12\r\n" "\r\n" "hello world!"); qdebug() << "encrypted"; } else { qdebug() << "nope..."; } } }
any help? tnx advance!
Comments
Post a Comment