generated key
openssl ecparam -param_enc named_curve -name secp521r1 -genkey -outform PEM -out key.pem
generated cert
openssl req -x509 -new -key key.pem -keyform PEM -out ca.pem -outform PEM -days 3650
when i use
openssl s_server -accept 5000 -key key.pem -cert ca.pem -tls1_2 -www
i can connect fine using
openssl s_client -host 10.8.0.26 -port 5000
Client tells me it uses
Protocol : TLSv1.2 Cipher : ECDHE-ECDSA-AES256-GCM-SHA384
exactly what i want
when i compile and use the following c code to start the server
compiled with
g++ Main2.cpp -ldl -lcrypto -lssl -o Main)
#include <iostream>
#include <string>
#include <errno.h>
#include <unistd.h>
#include <malloc.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <resolv.h>
#include "openssl/ssl.h"
#include "openssl/err.h"
using namespace std;
void Servlet(SSL* ssl) /* Serve the connection -- threadable */
{ char buf[1024];
char reply[1024];
int sd, bytes, err;
const char* HTMLecho="<html><body><pre>%s</pre></body></html>\n\n";
err = SSL_accept(ssl);
if ( err <= 0 ) { /* do SSL-protocol accept */
printf("%d\n",err);
ERR_print_errors_fp(stderr);
}
else
{
bytes = SSL_read(ssl, buf, sizeof(buf)); /* get request */
if ( bytes > 0 )
{
buf[bytes] = 0;
printf("Client msg: \"%s\"\n", buf);
sprintf(reply, HTMLecho, buf); /* construct reply */
SSL_write(ssl, reply, strlen(reply)); /* send reply */
}
else
ERR_print_errors_fp(stderr);
}
sd = SSL_get_fd(ssl); /* get socket connection */
SSL_free(ssl); /* release SSL state */
close(sd); /* close connection */
}
int OpenListener(int port)
{ int sd;
struct sockaddr_in addr;
sd = socket(PF_INET, SOCK_STREAM, 0);
bzero(&addr, sizeof(addr));
inet_aton("10.8.0.26", &addr.sin_addr);
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
//addr.sin_addr.s_addr = INADDR_ANY;
if ( bind(sd, (struct sockaddr*)&addr, sizeof(addr)) != 0 )
{
perror("can't bind port");
abort();
}
if ( listen(sd, 10) != 0 )
{
perror("Can't configure listening port");
abort();
}
return sd;
}
int main(int argc, const char* argv[]) {
//SSL_library_init();
SSL_load_error_strings();
OpenSSL_add_ssl_algorithms();
cout << SSLeay_version(SSLEAY_VERSION) << endl;
SSL_CTX *ctx;
const SSL_METHOD *method;
method = TLSv1_2_server_method();
//method = SSLv23_server_method();
ctx = SSL_CTX_new(method); /* create new context from method */
if ( ctx == NULL )
{
ERR_print_errors_fp(stderr);
abort();
}
if (SSL_CTX_set_ecdh_auto(ctx, 1) != 1)
ERR_print_errors_fp(stderr);
//if (SSL_CTX_set_cipher_list(ctx, "ECDH-ECDSA-AES256-GCM-SHA384") != 1)
// ERR_print_errors_fp(stderr);
if (SSL_CTX_load_verify_locations(ctx, "ca.pem", "key.pem") != 1)
ERR_print_errors_fp(stderr);
if (SSL_CTX_set_default_verify_paths(ctx) != 1)
ERR_print_errors_fp(stderr);
if (SSL_CTX_use_certificate_file(ctx, "ca.pem", SSL_FILETYPE_PEM) <= 0)
{
ERR_print_errors_fp(stderr);
abort();
}
if (SSL_CTX_use_PrivateKey_file(ctx, "key.pem", SSL_FILETYPE_PEM) <= 0)
{
ERR_print_errors_fp(stderr);
abort();
}
if (!SSL_CTX_check_private_key(ctx))
{
fprintf(stderr, "Private key does not match the public certificate\n");
abort();
}
int server;
server = OpenListener(5000);
struct sockaddr_in addr;
socklen_t len = sizeof(addr);
SSL *ssl;
int client = accept(server, (struct sockaddr*)&addr, &len); /* accept connection as usual */
printf("Connection: %s:%d\n",inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
ssl = SSL_new(ctx); /* get new SSL state with context */
if (ssl == NULL) {
ERR_print_errors_fp(stderr);
return 0;
}
SSL_set_fd(ssl, client); /* set connection socket to SSL state */
Servlet(ssl); /* service connection */
close(server); /* close server socket */
SSL_CTX_free(ctx); /* release context */
return 0;
}
when i now connect using openssl s_client just like above it uses
Protocol : TLSv1.2 Cipher : ECDH-ECDSA-AES256-GCM-SHA384
not ECDHE ! Why ?
When i use chrome/firefox to connect either to the openssl s_server or my server code i get:
140675163166384:error:1408A0C1:SSL routines:SSL3_GET_CLIENT_HELLO:no shared cipher:s3_srvr.c:1396:
Why no connection at all ?