5 #include <sys/socket.h>
6 #include <netinet/in.h>
7 #include <netinet/tcp.h>
10 #include <openssl/ssl.h>
11 #include <openssl/err.h>
15 struct sockaddr_in addr;
18 if(!(hp = gethostbyname(host))) {
19 LOG(ERROR) <<
"Couldn't resolve host";
23 memset(&addr,0,
sizeof(addr));
24 addr.sin_addr=*(
struct in_addr*)hp->h_addr_list[0];
25 addr.sin_family=AF_INET;
26 addr.sin_port=htons(port);
28 if((sock=socket(AF_INET,SOCK_STREAM, IPPROTO_TCP))<0) {
29 LOG(ERROR) <<
"Couldn't create socket";
32 if(connect(sock,(
struct sockaddr *)&addr,
sizeof(addr))<0) {
33 LOG(ERROR) <<
"Couldn't connect socket";
40 long result = SSL_get_verify_result(ssl);
51 X509* peer = SSL_get_peer_certificate(ssl);
54 X509_NAME_get_text_by_NID (X509_get_subject_name(peer),
55 NID_commonName, peer_CN, 256);
56 if(strcasecmp(peer_CN,host)) {
57 LOG(ERROR) <<
"Common name doesn't match host name (" << peer_CN <<
")";
65 int rwflag,
void *userdata)
67 if(num<strlen(
"")+1)
return(0);
75 if (!(SSL_CTX_use_certificate_chain_file(ctx, keyfile))) {
76 LOG(ERROR) <<
"Can't read certificate file";
97 int main(
int argc,
char** argv) {
98 google::InitGoogleLogging(argv[0]);
100 SSL_load_error_strings();
101 SSL_CTX* ctx = SSL_CTX_new(SSLv23_method());
102 SSL* ssl = SSL_new(ctx);
105 BIO* bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
106 ERR_print_errors(bio_err);
113 int sock =
tcp_connect((
char*)
"10.20.186.46", 636);
114 LOG(INFO) <<
"Connected, sock: " << sock;
115 BIO* bio = BIO_new_socket(sock, BIO_NOCLOSE);
116 SSL_set_bio(ssl, bio, bio);
118 int ret = SSL_connect(ssl);
119 LOG(INFO) <<
"SSL_connect() returned: " << ret;
121 LOG(INFO) <<
"Errors: " << SSL_get_error(ssl, ret);
122 BIO* bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
123 ERR_print_errors(bio_err);
124 if (SSL_get_error(ssl, ret) == SSL_ERROR_SYSCALL) {
125 LOG(INFO) <<
"Err[" << errno <<
"]: " << strerror(errno);
130 BIO* bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
131 ERR_print_errors(bio_err);
132 if (SSL_get_error(ssl, 20) == SSL_ERROR_SYSCALL) {
133 LOG(INFO) <<
"Err[" << errno <<
"]: " << strerror(errno);
bool install_certificates(SSL_CTX *ctx, char *keyfile)
int main(int argc, char **argv)
int tcp_connect(char *host, int port)
static int password_cb(char *buf, int num, int rwflag, void *userdata)
bool check_cert(SSL *ssl, char *host)