blob: 3c2644d8d340410253214d18682a44092ab8c1cb [file] [log] [blame]
James E. King, III36628a22017-02-13 15:25:41 -05001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +020019#define _POSIX_C_SOURCE 200112L /* https://stackoverflow.com/questions/37541985/storage-size-of-addrinfo-isnt-known */
James E. King, III36628a22017-02-13 15:25:41 -050020
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +020021
James E. King, III36628a22017-02-13 15:25:41 -050022#include <sys/wait.h>
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +020023#include <arpa/inet.h>
James E. King, III36628a22017-02-13 15:25:41 -050024#include <sys/types.h>
25#include <sys/socket.h>
26#include <netdb.h>
James E. King, III36628a22017-02-13 15:25:41 -050027
28#include <thrift/c_glib/transport/thrift_transport.h>
29#include <thrift/c_glib/transport/thrift_buffered_transport.h>
30#include <thrift/c_glib/transport/thrift_server_transport.h>
31#include <thrift/c_glib/transport/thrift_server_socket.h>
32#include <thrift/c_glib/transport/thrift_ssl_socket.h>
33
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +020034/* #define TEST_DATA { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' } */
James E. King, III36628a22017-02-13 15:25:41 -050035#define TEST_DATA { "GET / HTTP/1.1\n\n" }
36
37
38/* substituted functions to test failures of system and library calls */
39static int socket_error = 0;
40int
41my_socket(int domain, int type, int protocol)
42{
James E. King, III43f4bf22017-10-28 12:54:02 -040043 if (socket_error == 0)
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +020044 {
45 return socket (domain, type, protocol);
46 }
James E. King, III43f4bf22017-10-28 12:54:02 -040047 return -1;
James E. King, III36628a22017-02-13 15:25:41 -050048}
49
50static int recv_error = 0;
51ssize_t
52my_recv(int socket, void *buffer, size_t length, int flags)
53{
James E. King, III43f4bf22017-10-28 12:54:02 -040054 if (recv_error == 0)
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +020055 {
56 return recv (socket, buffer, length, flags);
57 }
James E. King, III43f4bf22017-10-28 12:54:02 -040058 return -1;
James E. King, III36628a22017-02-13 15:25:41 -050059}
60
61static int send_error = 0;
62ssize_t
63my_send(int socket, const void *buffer, size_t length, int flags)
64{
James E. King, III43f4bf22017-10-28 12:54:02 -040065 if (send_error == 0)
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +020066 {
67 return send (socket, buffer, length, flags);
68 }
James E. King, III43f4bf22017-10-28 12:54:02 -040069 return -1;
James E. King, III36628a22017-02-13 15:25:41 -050070}
71
72#define socket my_socket
73#define recv my_recv
74#define send my_send
75#include "../src/thrift/c_glib/transport/thrift_ssl_socket.c"
76#undef socket
77#undef recv
78#undef send
79
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +020080static void thrift_socket_server (const int port);
James E. King, III36628a22017-02-13 15:25:41 -050081
82/* test object creation and destruction */
83static void
84test_ssl_create_and_destroy(void)
85{
James E. King, III43f4bf22017-10-28 12:54:02 -040086 gchar *hostname = NULL;
87 guint port = 0;
James E. King, III36628a22017-02-13 15:25:41 -050088
James E. King, III43f4bf22017-10-28 12:54:02 -040089 GObject *object = NULL;
90 object = g_object_new (THRIFT_TYPE_SSL_SOCKET, NULL);
91 g_assert (object != NULL);
92 g_object_get (G_OBJECT(object), "hostname", &hostname, "port", &port, NULL);
93 g_free (hostname);
94 g_object_unref (object);
James E. King, III36628a22017-02-13 15:25:41 -050095}
96
97static void
98test_ssl_create_and_set_properties(void)
99{
James E. King, III43f4bf22017-10-28 12:54:02 -0400100 gchar *hostname = NULL;
101 guint port = 0;
102 SSL_CTX* ssl_ctx= NULL;
103 GError *error=NULL;
James E. King, III36628a22017-02-13 15:25:41 -0500104
James E. King, III43f4bf22017-10-28 12:54:02 -0400105 GObject *object = NULL;
106 object = thrift_ssl_socket_new(SSLTLS, &error);
107 g_object_get (G_OBJECT(object), "hostname", &hostname, "port", &port, "ssl_context", &ssl_ctx, NULL);
108 g_assert (ssl_ctx!=NULL);
James E. King, III36628a22017-02-13 15:25:41 -0500109
James E. King, III43f4bf22017-10-28 12:54:02 -0400110 g_free (hostname);
111 g_object_unref (object);
James E. King, III36628a22017-02-13 15:25:41 -0500112}
113
114static void
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200115test_ssl_open_and_close_non_ssl_server(void)
James E. King, III36628a22017-02-13 15:25:41 -0500116{
James E. King, III43f4bf22017-10-28 12:54:02 -0400117 ThriftSSLSocket *tSSLSocket = NULL;
118 ThriftTransport *transport = NULL;
119 GError *error=NULL;
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200120 pid_t pid;
121 int non_ssl_port = 51198;
122 char errormsg[255];
123
124
125 pid = fork ();
126 g_assert ( pid >= 0 );
127
128 if ( pid == 0 )
129 {
130 /* child listens */
131 /* This is a non SSL server */
132 thrift_socket_server (non_ssl_port);
133 exit (0);
134 } else {
135 /* parent connects, wait a bit for the socket to be created */
136 sleep (1);
137
138 /* open a connection and close it */
139 tSSLSocket = thrift_ssl_socket_new_with_host(SSLTLS, "localhost", non_ssl_port, &error);
140
141 transport = THRIFT_TRANSPORT (tSSLSocket);
142 g_assert (thrift_ssl_socket_open (transport, &error) == FALSE);
143 g_assert_cmpstr(error->message, == ,"Error while connect/bind: 68 -> Connection reset by peer");
144 g_clear_error (&error);
145 g_assert (thrift_ssl_socket_is_open (transport) == FALSE);
146 thrift_ssl_socket_close (transport, NULL);
147 g_assert (thrift_ssl_socket_is_open (transport) == FALSE);
148
149 /* test close failure */
150 THRIFT_SOCKET(tSSLSocket)->sd = -1;
151 thrift_ssl_socket_close (transport, NULL);
152 g_object_unref (tSSLSocket);
153
154 /* try a hostname lookup failure */
155 tSSLSocket = thrift_ssl_socket_new_with_host(SSLTLS, "localhost.broken", non_ssl_port, &error);
156 transport = THRIFT_TRANSPORT (tSSLSocket);
157 g_assert (thrift_ssl_socket_open (transport, &error) == FALSE);
158 snprintf(errormsg, 255, "host lookup failed for localhost.broken:%d - Unknown host", non_ssl_port);
159 g_assert_cmpstr(error->message, ==, errormsg);
160 g_clear_error (&error);
161 g_object_unref (tSSLSocket);
162 error = NULL;
163
164 /* try an error call to socket() */
165 /*
166 tSSLSocket = thrift_ssl_socket_new_with_host(SSLTLS, "localhost", port, &error);
167 transport = THRIFT_TRANSPORT (tSSLSocket);
168 socket_error = 1;
169 assert (thrift_ssl_socket_open (transport, &error) == FALSE);
170 socket_error = 0;
171 g_object_unref (tSSLSocket);
172 g_error_free (error);
173 */
174 }
175}
176
177static void
178test_ssl_write_invalid_socket(void)
179{
180 ThriftSSLSocket *tSSLSocket = NULL;
181 ThriftTransport *transport = NULL;
182 GError *error=NULL;
183 char buffer[] = "this must not break";
James E. King, III36628a22017-02-13 15:25:41 -0500184
James E. King, III43f4bf22017-10-28 12:54:02 -0400185 /* open a connection and close it */
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200186 tSSLSocket = thrift_ssl_socket_new_with_host(SSLTLS, "localhost", 51188+1, &error);
James E. King, III36628a22017-02-13 15:25:41 -0500187
James E. King, III43f4bf22017-10-28 12:54:02 -0400188 transport = THRIFT_TRANSPORT (tSSLSocket);
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200189 g_assert (thrift_ssl_socket_open (transport, NULL) == FALSE);
190 g_assert (thrift_ssl_socket_is_open (transport) == FALSE);
191
192 /* FIXME This must be tested but since the assertion inside thrift_ssl_socket_write breaks the test unit
193 it's disabled. They idea is to disable trap/coredump during this test
194 g_assert (thrift_ssl_socket_write(transport, buffer, sizeof(buffer), &error) == FALSE);
195 g_message ("write_failed_with_error: %s",
196 error != NULL ? error->message : "No");
197 g_clear_error (&error);
198 */
James E. King, III43f4bf22017-10-28 12:54:02 -0400199 thrift_ssl_socket_close (transport, NULL);
200 g_assert (thrift_ssl_socket_is_open (transport) == FALSE);
James E. King, III36628a22017-02-13 15:25:41 -0500201
James E. King, III43f4bf22017-10-28 12:54:02 -0400202 /* test close failure */
203 THRIFT_SOCKET(tSSLSocket)->sd = -1;
204 thrift_ssl_socket_close (transport, NULL);
205 g_object_unref (tSSLSocket);
James E. King, III36628a22017-02-13 15:25:41 -0500206}
207
208
209
210/**
211 * Print the common name of certificate
212 */
213unsigned char * get_cn_name(X509_NAME* const name)
214{
James E. King, III43f4bf22017-10-28 12:54:02 -0400215 int idx = -1;
216 unsigned char *utf8 = NULL;
James E. King, III36628a22017-02-13 15:25:41 -0500217
James E. King, III43f4bf22017-10-28 12:54:02 -0400218 do
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200219 {
220 if(!name) break; /* failed */
James E. King, III36628a22017-02-13 15:25:41 -0500221
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200222 idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1);
223 if(!(idx > -1)) break; /* failed */
James E. King, III36628a22017-02-13 15:25:41 -0500224
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200225 X509_NAME_ENTRY* entry = X509_NAME_get_entry(name, idx);
226 if(!entry) break; /* failed */
James E. King, III36628a22017-02-13 15:25:41 -0500227
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200228 ASN1_STRING* data = X509_NAME_ENTRY_get_data(entry);
229 if(!data) break; /* failed */
James E. King, III36628a22017-02-13 15:25:41 -0500230
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200231 int length = ASN1_STRING_to_UTF8(&utf8, data);
232 if(!utf8 || !(length > 0)) break; /* failed */
James E. King, III36628a22017-02-13 15:25:41 -0500233
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200234 } while (0);
James E. King, III43f4bf22017-10-28 12:54:02 -0400235 return utf8;
James E. King, III36628a22017-02-13 15:25:41 -0500236}
237
238/*
239 * Handle IPV4 and IPV6 addr
240 */
241void *get_in_addr(struct sockaddr *sa)
242{
James E. King, III43f4bf22017-10-28 12:54:02 -0400243 if (sa->sa_family == AF_INET)
244 return &(((struct sockaddr_in*)sa)->sin_addr);
245 return &(((struct sockaddr_in6*)sa)->sin6_addr);
James E. King, III36628a22017-02-13 15:25:41 -0500246}
247
248int verify_ip(char * hostname, struct sockaddr_storage *addr)
249{
James E. King, III43f4bf22017-10-28 12:54:02 -0400250 struct addrinfo *addr_info,*p;
251 struct addrinfo hints;
252 int res;
253 int retval = 0;
James E. King, III36628a22017-02-13 15:25:41 -0500254
255
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200256 memset(&hints, 0, sizeof (struct addrinfo));
257 hints.ai_family = AF_UNSPEC; /* use AF_INET6 to force IPv6 */
James E. King, III43f4bf22017-10-28 12:54:02 -0400258 hints.ai_socktype = SOCK_STREAM;
James E. King, III36628a22017-02-13 15:25:41 -0500259
260
James E. King, III43f4bf22017-10-28 12:54:02 -0400261 if ( (res = getaddrinfo(hostname, NULL, &hints, &addr_info) ) != 0)
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200262 {
263 /* get the host info */
264 g_error("Cannot get the host address");
265 return retval;
266 }
267 /* loop through all the results and connect to the first we can */
268 char dnshost[INET6_ADDRSTRLEN]; /* bigger addr supported IPV6 */
James E. King, III43f4bf22017-10-28 12:54:02 -0400269 char socket_ip[INET6_ADDRSTRLEN];
270 if(inet_ntop(addr->ss_family, get_in_addr(addr), socket_ip, INET6_ADDRSTRLEN)==socket_ip){
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200271 g_debug("We are connected to host %s checking against certificate...", socket_ip);
272 int sizeip = socket_ip!=NULL ? strlen(socket_ip) : 0;
273 for(p = addr_info; p != NULL; p = p->ai_next) {
274 if(inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr), dnshost, INET6_ADDRSTRLEN)==dnshost){
275 if(dnshost!=NULL){
276 g_info("DNS address [%i -> %s]", p->ai_addr, dnshost);
277 if(!strncmp(dnshost, socket_ip, sizeip)){
278 retval=1;
279 break; /* if we get here, we must have connected successfully */
280 }
281 }
282 }
James E. King, III43f4bf22017-10-28 12:54:02 -0400283 }
James E. King, III43f4bf22017-10-28 12:54:02 -0400284 }
James E. King, III36628a22017-02-13 15:25:41 -0500285
James E. King, III43f4bf22017-10-28 12:54:02 -0400286 if(addr_info)
287 freeaddrinfo(addr_info);
James E. King, III36628a22017-02-13 15:25:41 -0500288
James E. King, III43f4bf22017-10-28 12:54:02 -0400289 return retval;
James E. King, III36628a22017-02-13 15:25:41 -0500290}
291
292static void
293read_from_file(char *buffer, long size, const char *file_name)
294{
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200295 char ch;
296 long index=0;
297 FILE *fp;
James E. King, III36628a22017-02-13 15:25:41 -0500298
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200299 fp = fopen(file_name,"r"); /* read mode */
James E. King, III36628a22017-02-13 15:25:41 -0500300
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200301 if( fp == NULL )
302 {
303 perror("Error while opening the file.\n");
304 exit(EXIT_FAILURE);
305 }
James E. King, III36628a22017-02-13 15:25:41 -0500306
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200307 printf("The contents of %s file are :\n", file_name);
James E. King, III36628a22017-02-13 15:25:41 -0500308
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200309 while(index<size && ( ch = fgetc(fp) ) != EOF ){
310 buffer[index++] = ch;
311 }
James E. King, III36628a22017-02-13 15:25:41 -0500312
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200313 fclose(fp);
James E. King, III36628a22017-02-13 15:25:41 -0500314}
315
316#define ISSUER_CN_PINNING "The Apache Software Foundation"
317#define SUBJECT_CN_PINNING "localhost"
318#define CERT_SERIAL_NUMBER "1"
319
320gboolean verify_certificate_sn(X509 *cert, const unsigned char *serial_number)
321{
James E. King, III43f4bf22017-10-28 12:54:02 -0400322 gboolean retval = FALSE;
James E. King, III36628a22017-02-13 15:25:41 -0500323
James E. King, III43f4bf22017-10-28 12:54:02 -0400324 ASN1_INTEGER *serial = X509_get_serialNumber(cert);
James E. King, III36628a22017-02-13 15:25:41 -0500325
James E. King, III43f4bf22017-10-28 12:54:02 -0400326 BIGNUM *bn = ASN1_INTEGER_to_BN(serial, NULL);
327 if (!bn) {
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200328 fprintf(stderr, "unable to convert ASN1INTEGER to BN\n");
329 return EXIT_FAILURE;
James E. King, III43f4bf22017-10-28 12:54:02 -0400330 }
331 char *tmp = BN_bn2dec(bn);
332 if (!tmp) {
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200333 g_warning(stderr, "unable to convert BN to decimal string.\n");
334 BN_free(bn);
335 return EXIT_FAILURE;
James E. King, III43f4bf22017-10-28 12:54:02 -0400336 }
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200337 /*
338 if (strlen(tmp) >= len) {
339 g_warn(stderr, "buffer length shorter than serial number\n");
340 BN_free(bn);
341 OPENSSL_free(tmp);
342 return EXIT_FAILURE;
343 }
344 */
James E. King, III43f4bf22017-10-28 12:54:02 -0400345 if(!strncmp(serial_number, tmp, strlen(serial_number))){
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200346 retval=TRUE;
James E. King, III43f4bf22017-10-28 12:54:02 -0400347 }else{
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200348 g_warning("Serial number is not valid");
James E. King, III43f4bf22017-10-28 12:54:02 -0400349 }
James E. King, III36628a22017-02-13 15:25:41 -0500350
James E. King, III43f4bf22017-10-28 12:54:02 -0400351 BN_free(bn);
352 OPENSSL_free(tmp);
353 return retval;
James E. King, III36628a22017-02-13 15:25:41 -0500354}
355
356gboolean my_access_manager(ThriftTransport * transport, X509 *cert, struct sockaddr_storage *addr, GError **error)
357{
James E. King, III43f4bf22017-10-28 12:54:02 -0400358 ThriftSSLSocket *sslSocket = THRIFT_SSL_SOCKET (transport);
James E. King, III36628a22017-02-13 15:25:41 -0500359
James E. King, III43f4bf22017-10-28 12:54:02 -0400360 g_info("Processing access to the server");
361 X509_NAME* iname = cert ? X509_get_issuer_name(cert) : NULL;
362 X509_NAME* sname = cert ? X509_get_subject_name(cert) : NULL;
James E. King, III36628a22017-02-13 15:25:41 -0500363
James E. King, III43f4bf22017-10-28 12:54:02 -0400364 /* Issuer is the authority we trust that warrants nothing useful */
365 const unsigned char * issuer = get_cn_name(iname);
366 if(issuer){
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200367 gboolean valid = TRUE;
368 g_info("Issuer (cn) %s", issuer);
James E. King, III36628a22017-02-13 15:25:41 -0500369
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200370 /* Issuer pinning */
371 if(strncmp(ISSUER_CN_PINNING, issuer, strlen(ISSUER_CN_PINNING))){
372 g_warning("The Issuer of the certificate is not valid");
373 valid=FALSE;
374 }
375 OPENSSL_free(issuer);
376 if(!valid)
377 return valid;
James E. King, III43f4bf22017-10-28 12:54:02 -0400378 }
James E. King, III36628a22017-02-13 15:25:41 -0500379
380
James E. King, III43f4bf22017-10-28 12:54:02 -0400381 /* Subject is who the certificate is issued to by the authority */
382 const unsigned char * subject = get_cn_name(sname);
383 if(subject){
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200384 g_info("Subject (cn) %s", subject);
385 gboolean valid = TRUE;
James E. King, III36628a22017-02-13 15:25:41 -0500386
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200387 /* Subject pinning */
388 if(strncmp(SUBJECT_CN_PINNING, subject, strlen(SUBJECT_CN_PINNING))){
389 g_warning("The subject of the certificate is not valid");
390 valid=FALSE;
391 }
James E. King, III36628a22017-02-13 15:25:41 -0500392
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200393 if(!valid)
394 return valid;
James E. King, III36628a22017-02-13 15:25:41 -0500395
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200396 /* Host pinning */
397 if(verify_ip(subject, addr)){
398 g_info("Verified subject");
399 }else{
400 g_info("Cannot verify subject");
401 valid=FALSE;
402 }
403 OPENSSL_free(subject);
James E. King, III36628a22017-02-13 15:25:41 -0500404
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200405 if(!valid)
406 return valid;
James E. King, III43f4bf22017-10-28 12:54:02 -0400407 }
James E. King, III36628a22017-02-13 15:25:41 -0500408
James E. King, III43f4bf22017-10-28 12:54:02 -0400409 if(!verify_certificate_sn(cert, CERT_SERIAL_NUMBER)){
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200410 return FALSE;
James E. King, III43f4bf22017-10-28 12:54:02 -0400411 }else{
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200412 g_info("Verified serial number");
James E. King, III43f4bf22017-10-28 12:54:02 -0400413 }
James E. King, III36628a22017-02-13 15:25:41 -0500414
James E. King, III43f4bf22017-10-28 12:54:02 -0400415 return TRUE;
James E. King, III36628a22017-02-13 15:25:41 -0500416
417}
418
419
420
421
422#ifdef BUILD_SERVER
423static void
424test_ssl_authorization_manager(void)
425{
James E. King, III43f4bf22017-10-28 12:54:02 -0400426 int status=0;
427 pid_t pid;
428 ThriftSSLSocket *tSSLsocket = NULL;
429 ThriftTransport *transport = NULL;
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200430 /* int port = 51199; */
James E. King, III43f4bf22017-10-28 12:54:02 -0400431 int port = 443;
432 GError *error=NULL;
James E. King, III36628a22017-02-13 15:25:41 -0500433
James E. King, III43f4bf22017-10-28 12:54:02 -0400434 guchar buf[17] = TEST_DATA; /* a buffer */
James E. King, III36628a22017-02-13 15:25:41 -0500435
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200436/*
437 pid = fork ();
438 g_assert ( pid >= 0 );
439
440 if ( pid == 0 )
441 {
442 thrift_ssl_socket_server (port);
443 exit (0);
444 } else {
445 */
James E. King, III43f4bf22017-10-28 12:54:02 -0400446 /* parent connects, wait a bit for the socket to be created */
447 sleep (1);
James E. King, III36628a22017-02-13 15:25:41 -0500448
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200449 /* Test against level2 owncloud certificate */
James E. King, III43f4bf22017-10-28 12:54:02 -0400450 tSSLsocket = thrift_ssl_socket_new_with_host(SSLTLS, "localhost", port, &error);
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200451 thrift_ssl_socket_set_manager(tSSLsocket, my_access_manager); /* Install pinning manager */
452 /* thrift_ssl_load_cert_from_file(tSSLsocket, "./owncloud.level2crm.pem"); */
James E. King, III43f4bf22017-10-28 12:54:02 -0400453 unsigned char cert_buffer[65534];
454 read_from_file(cert_buffer, 65534, "../../keys/client.pem");
455 if(!thrift_ssl_load_cert_from_buffer(tSSLsocket, cert_buffer)){
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200456 g_warning("Certificates cannot be loaded!");
James E. King, III43f4bf22017-10-28 12:54:02 -0400457 }
James E. King, III36628a22017-02-13 15:25:41 -0500458
James E. King, III43f4bf22017-10-28 12:54:02 -0400459 transport = THRIFT_TRANSPORT (tSSLsocket);
460 g_assert (thrift_ssl_socket_open (transport, NULL) == TRUE);
461 g_assert (thrift_ssl_socket_is_open (transport));
James E. King, III36628a22017-02-13 15:25:41 -0500462
James E. King, III43f4bf22017-10-28 12:54:02 -0400463 thrift_ssl_socket_write (transport, buf, 17, NULL);
James E. King, III36628a22017-02-13 15:25:41 -0500464
James E. King, III43f4bf22017-10-28 12:54:02 -0400465 /* write fail */
466 send_error = 1;
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200467 /*
468 thrift_ssl_socket_write (transport, buf, 1, NULL);
469 send_error = 0;
470 thrift_ssl_socket_write_end (transport, NULL);
471 thrift_ssl_socket_flush (transport, NULL);
472 */
James E. King, III43f4bf22017-10-28 12:54:02 -0400473 thrift_ssl_socket_close (transport, NULL);
474 g_object_unref (tSSLsocket);
James E. King, III36628a22017-02-13 15:25:41 -0500475
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200476 /* g_assert ( wait (&status) == pid ); */
James E. King, III43f4bf22017-10-28 12:54:02 -0400477 g_assert ( status == 0 );
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200478 /* } */
James E. King, III36628a22017-02-13 15:25:41 -0500479}
480#endif
481
482
James E. King, III36628a22017-02-13 15:25:41 -0500483static void
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200484thrift_socket_server (const int port)
James E. King, III36628a22017-02-13 15:25:41 -0500485{
James E. King, III43f4bf22017-10-28 12:54:02 -0400486 int bytes = 0;
487 ThriftServerTransport *transport = NULL;
488 ThriftTransport *client = NULL;
489 guchar buf[10]; /* a buffer */
490 guchar match[10] = TEST_DATA;
James E. King, III36628a22017-02-13 15:25:41 -0500491
James E. King, III43f4bf22017-10-28 12:54:02 -0400492 ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200493 "port", port, NULL);
James E. King, III36628a22017-02-13 15:25:41 -0500494
James E. King, III43f4bf22017-10-28 12:54:02 -0400495 transport = THRIFT_SERVER_TRANSPORT (tsocket);
496 thrift_server_transport_listen (transport, NULL);
497 client = thrift_server_transport_accept (transport, NULL);
498 g_assert (client != NULL);
James E. King, III36628a22017-02-13 15:25:41 -0500499
James E. King, III43f4bf22017-10-28 12:54:02 -0400500 /* read 10 bytes */
501 bytes = thrift_ssl_socket_read (client, buf, 10, NULL);
502 g_assert (bytes == 10); /* make sure we've read 10 bytes */
503 g_assert ( memcmp(buf, match, 10) == 0 ); /* make sure what we got matches */
James E. King, III36628a22017-02-13 15:25:41 -0500504
James E. King, III43f4bf22017-10-28 12:54:02 -0400505 /* failed read */
506 recv_error = 1;
507 thrift_ssl_socket_read (client, buf, 1, NULL);
508 recv_error = 0;
James E. King, III36628a22017-02-13 15:25:41 -0500509
James E. King, III43f4bf22017-10-28 12:54:02 -0400510 thrift_ssl_socket_read_end (client, NULL);
511 thrift_ssl_socket_close (client, NULL);
512 g_object_unref (tsocket);
513 g_object_unref (client);
James E. King, III36628a22017-02-13 15:25:41 -0500514}
515
516int
517main(int argc, char *argv[])
518{
James E. King, III43f4bf22017-10-28 12:54:02 -0400519 int retval;
James E. King, III36628a22017-02-13 15:25:41 -0500520#if (!GLIB_CHECK_VERSION (2, 36, 0))
James E. King, III43f4bf22017-10-28 12:54:02 -0400521 g_type_init();
James E. King, III36628a22017-02-13 15:25:41 -0500522#endif
523
James E. King, III43f4bf22017-10-28 12:54:02 -0400524 g_test_init (&argc, &argv, NULL);
James E. King, III36628a22017-02-13 15:25:41 -0500525
James E. King, III43f4bf22017-10-28 12:54:02 -0400526 thrift_ssl_socket_initialize_openssl();
James E. King, III36628a22017-02-13 15:25:41 -0500527
James E. King, III43f4bf22017-10-28 12:54:02 -0400528 g_test_add_func ("/testtransportsslsocket/CreateAndDestroy", test_ssl_create_and_destroy);
529 g_test_add_func ("/testtransportsslsocket/CreateAndSetProperties", test_ssl_create_and_set_properties);
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200530 g_test_add_func ("/testtransportsslsocket/OpenAndCloseNonSSLServer", test_ssl_open_and_close_non_ssl_server);
531 g_test_add_func ("/testtransportsslsocket/OpenAndWriteInvalidSocket", test_ssl_write_invalid_socket);
532
533
534
James E. King, III36628a22017-02-13 15:25:41 -0500535
James E. King, III43f4bf22017-10-28 12:54:02 -0400536 retval = g_test_run ();
James E. King, III36628a22017-02-13 15:25:41 -0500537
James E. King, III43f4bf22017-10-28 12:54:02 -0400538 thrift_ssl_socket_finalize_openssl();
James E. King, III36628a22017-02-13 15:25:41 -0500539
James E. King, III43f4bf22017-10-28 12:54:02 -0400540 return retval;
James E. King, III36628a22017-02-13 15:25:41 -0500541}
542