Reputation: 321
This has me crawling up the walls. I can't find out why this source doesn't open a socket. It's simple enough but doesn't work. Can someone please help me with this? Thanks for your consideration! BTW: I get no text on the screen and it blocks with the BIO_do_accept() function.
#include <openssl/bio.h>
#include <openssl/err.h>
#include <openssl/rand.h>
#include <openssl/ssl.h>
#include <openssl/x509v3.h>
#include <iostream>
#include <process.h>
using namespace std;
int main() {
SSL_load_error_strings();
SSL_library_init();
OpenSSL_add_all_algorithms();
BIO *abio, *cbio, *cbio2;
ERR_load_crypto_strings();
abio = BIO_new_accept("4444");
/* First call to BIO_accept() sets up accept BIO */
if(BIO_do_accept(abio) <= 0) {
fprintf(stderr, "Error setting up accept\n");
ERR_print_errors_fp(stderr);
exit(0);
}
/* Wait for incoming connection */
if(BIO_do_accept(abio) <= 0) {
fprintf(stderr, "Error accepting connection\n");
ERR_print_errors_fp(stderr);
exit(0);
}
fprintf(stderr, "Connection 1 established\n");
/* Retrieve BIO for connection */
cbio = BIO_pop(abio);
BIO_puts(cbio, "Connection 1: Sending out Data on initial connection\n");
fprintf(stderr, "Sent out data on connection 1\n");
}
Upvotes: 1
Views: 869
Reputation: 392929
I just tested this (on cygwin, with gcc 4.5.3 and openssl-devel 1.0.1 installed)
Your code posted in chat compiled with
g++ -std=c++0x ./test.cpp -lssl -lcrypto -o test
The resulting code obviously doesn't work, because the code refers to server.crt
and server.key
:
openssl genrsa -out server.key 1024
openssl req -new -key server.key -out server.csr
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
Creates a selfsigned certificate with unprotected key (you'd use genrsa -des3
to add a passphrase to the key).
Now, I could test it properly:
test& # in the background
openssl s_client -connect localhost:12120
This lands you in a kind of SSL-enabled telnet
client and it worked nicely.
Upvotes: 1