Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ssl-test.cc
Go to the documentation of this file.
1 //
2 #include "common/logging.h"
3 
4 #include <sys/types.h>
5 #include <sys/socket.h>
6 #include <netinet/in.h>
7 #include <netinet/tcp.h>
8 #include <netdb.h>
9 
10 #include <openssl/ssl.h>
11 #include <openssl/err.h>
12 
13 int tcp_connect(char* host, int port) {
14  struct hostent *hp;
15  struct sockaddr_in addr;
16  int sock;
17 
18  if(!(hp = gethostbyname(host))) {
19  LOG(ERROR) << "Couldn't resolve host";
20  exit(1);
21  }
22 
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);
27 
28  if((sock=socket(AF_INET,SOCK_STREAM, IPPROTO_TCP))<0) {
29  LOG(ERROR) << "Couldn't create socket";
30  exit(1);
31  }
32  if(connect(sock,(struct sockaddr *)&addr, sizeof(addr))<0) {
33  LOG(ERROR) << "Couldn't connect socket";
34  exit(1);
35  }
36  return sock;
37 }
38 
39 bool check_cert(SSL* ssl, char* host) {
40  long result = SSL_get_verify_result(ssl);
41  // if (result != X509_V_OK) {
42  // LOG(ERROR) << "Certificate doesn't verify (result: " << result << ")" ;
43  // return false;
44  // }
45 
46  /*Check the cert chain. The chain length
47  is automatically checked by OpenSSL when
48  we set the verify depth in the ctx */
49 
50  /*Check the common name*/
51  X509* peer = SSL_get_peer_certificate(ssl);
52  char peer_CN[256];
53 
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 << ")";
58  return false;
59  }
60 
61  return true;
62 }
63 
64 static int password_cb(char *buf,int num,
65  int rwflag,void *userdata)
66 {
67  if(num<strlen("")+1) return(0);
68 
69  strcpy(buf, "");
70  return(strlen(""));
71 }
72 
73 
74 bool install_certificates(SSL_CTX* ctx, char* keyfile) {
75  if (!(SSL_CTX_use_certificate_chain_file(ctx, keyfile))) {
76  LOG(ERROR) << "Can't read certificate file";
77  return false;
78  }
79 
80  // pass=password;
81  // SSL_CTX_set_default_passwd_cb(ctx, password_cb);
82  // if(!(SSL_CTX_use_PrivateKey_file(ctx, keyfile,SSL_FILETYPE_PEM))) {
83  // LOG(INFO) << "Can't read key file";
84  // return false;
85  // }
86 
87  /* Load the CAs we trust*/
88  // if(!(SSL_CTX_load_verify_locations(ctx, "root.pem", 0))) {
89  // LOG(INFO) << "Can't read CA list";
90  // return false;
91  // }
92 
93  return true;
94 }
95 
96 
97 int main(int argc, char** argv) {
98  google::InitGoogleLogging(argv[0]);
99  SSL_library_init();
100  SSL_load_error_strings();
101  SSL_CTX* ctx = SSL_CTX_new(SSLv23_method());
102  SSL* ssl = SSL_new(ctx);
103 
104  if (!install_certificates(ctx, "/home/henry/src/cloudera/impala/win2k8-ad1-ca.cer")) {
105  BIO* bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
106  ERR_print_errors(bio_err);
107  exit(1);
108  // if (SSL_get_error(ssl, ret) == SSL_ERROR_SYSCALL) {
109  // LOG(INFO) << "Err[" << errno << "]: " << strerror(errno);
110  // }
111  }
112 
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);
117 
118  int ret = SSL_connect(ssl);
119  LOG(INFO) << "SSL_connect() returned: " << ret;
120  if (ret < 0) {
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);
126  }
127  }
128 
129  if (!check_cert(ssl, "10.20.186.46")) {
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);
134  }
135  }
136 }
bool install_certificates(SSL_CTX *ctx, char *keyfile)
Definition: ssl-test.cc:74
int main(int argc, char **argv)
Definition: ssl-test.cc:97
int tcp_connect(char *host, int port)
Definition: ssl-test.cc:13
static int password_cb(char *buf, int num, int rwflag, void *userdata)
Definition: ssl-test.cc:64
bool check_cert(SSL *ssl, char *host)
Definition: ssl-test.cc:39