blob: ba9ffdcaedf69076df36381ce5910f83c76df2ec [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;
zeshuai007997e2d42020-09-29 14:43:18 +0800106 object = (GObject *)thrift_ssl_socket_new(SSLTLS, &error);
James E. King, III43f4bf22017-10-28 12:54:02 -0400107 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;
James E. King, III36628a22017-02-13 15:25:41 -0500183
James E. King, III43f4bf22017-10-28 12:54:02 -0400184 /* open a connection and close it */
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200185 tSSLSocket = thrift_ssl_socket_new_with_host(SSLTLS, "localhost", 51188+1, &error);
James E. King, III36628a22017-02-13 15:25:41 -0500186
James E. King, III43f4bf22017-10-28 12:54:02 -0400187 transport = THRIFT_TRANSPORT (tSSLSocket);
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200188 g_assert (thrift_ssl_socket_open (transport, NULL) == FALSE);
189 g_assert (thrift_ssl_socket_is_open (transport) == FALSE);
190
191 /* FIXME This must be tested but since the assertion inside thrift_ssl_socket_write breaks the test unit
192 it's disabled. They idea is to disable trap/coredump during this test
193 g_assert (thrift_ssl_socket_write(transport, buffer, sizeof(buffer), &error) == FALSE);
194 g_message ("write_failed_with_error: %s",
195 error != NULL ? error->message : "No");
196 g_clear_error (&error);
197 */
James E. King, III43f4bf22017-10-28 12:54:02 -0400198 thrift_ssl_socket_close (transport, NULL);
199 g_assert (thrift_ssl_socket_is_open (transport) == FALSE);
James E. King, III36628a22017-02-13 15:25:41 -0500200
James E. King, III43f4bf22017-10-28 12:54:02 -0400201 /* test close failure */
202 THRIFT_SOCKET(tSSLSocket)->sd = -1;
203 thrift_ssl_socket_close (transport, NULL);
204 g_object_unref (tSSLSocket);
James E. King, III36628a22017-02-13 15:25:41 -0500205}
206
207
208
209/**
210 * Print the common name of certificate
211 */
212unsigned char * get_cn_name(X509_NAME* const name)
213{
James E. King, III43f4bf22017-10-28 12:54:02 -0400214 int idx = -1;
215 unsigned char *utf8 = NULL;
James E. King, III36628a22017-02-13 15:25:41 -0500216
James E. King, III43f4bf22017-10-28 12:54:02 -0400217 do
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200218 {
219 if(!name) break; /* failed */
James E. King, III36628a22017-02-13 15:25:41 -0500220
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200221 idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1);
222 if(!(idx > -1)) break; /* failed */
James E. King, III36628a22017-02-13 15:25:41 -0500223
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200224 X509_NAME_ENTRY* entry = X509_NAME_get_entry(name, idx);
225 if(!entry) break; /* failed */
James E. King, III36628a22017-02-13 15:25:41 -0500226
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200227 ASN1_STRING* data = X509_NAME_ENTRY_get_data(entry);
228 if(!data) break; /* failed */
James E. King, III36628a22017-02-13 15:25:41 -0500229
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200230 int length = ASN1_STRING_to_UTF8(&utf8, data);
231 if(!utf8 || !(length > 0)) break; /* failed */
James E. King, III36628a22017-02-13 15:25:41 -0500232
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200233 } while (0);
James E. King, III43f4bf22017-10-28 12:54:02 -0400234 return utf8;
James E. King, III36628a22017-02-13 15:25:41 -0500235}
236
237/*
238 * Handle IPV4 and IPV6 addr
239 */
240void *get_in_addr(struct sockaddr *sa)
241{
James E. King, III43f4bf22017-10-28 12:54:02 -0400242 if (sa->sa_family == AF_INET)
243 return &(((struct sockaddr_in*)sa)->sin_addr);
244 return &(((struct sockaddr_in6*)sa)->sin6_addr);
James E. King, III36628a22017-02-13 15:25:41 -0500245}
246
247int verify_ip(char * hostname, struct sockaddr_storage *addr)
248{
James E. King, III43f4bf22017-10-28 12:54:02 -0400249 struct addrinfo *addr_info,*p;
250 struct addrinfo hints;
251 int res;
252 int retval = 0;
James E. King, III36628a22017-02-13 15:25:41 -0500253
254
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200255 memset(&hints, 0, sizeof (struct addrinfo));
256 hints.ai_family = AF_UNSPEC; /* use AF_INET6 to force IPv6 */
James E. King, III43f4bf22017-10-28 12:54:02 -0400257 hints.ai_socktype = SOCK_STREAM;
James E. King, III36628a22017-02-13 15:25:41 -0500258
259
James E. King, III43f4bf22017-10-28 12:54:02 -0400260 if ( (res = getaddrinfo(hostname, NULL, &hints, &addr_info) ) != 0)
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200261 {
262 /* get the host info */
263 g_error("Cannot get the host address");
264 return retval;
265 }
266 /* loop through all the results and connect to the first we can */
267 char dnshost[INET6_ADDRSTRLEN]; /* bigger addr supported IPV6 */
James E. King, III43f4bf22017-10-28 12:54:02 -0400268 char socket_ip[INET6_ADDRSTRLEN];
zeshuai007997e2d42020-09-29 14:43:18 +0800269 if(inet_ntop(addr->ss_family, get_in_addr((struct sockaddr*)addr), socket_ip, INET6_ADDRSTRLEN)==socket_ip){
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200270 g_debug("We are connected to host %s checking against certificate...", socket_ip);
271 int sizeip = socket_ip!=NULL ? strlen(socket_ip) : 0;
272 for(p = addr_info; p != NULL; p = p->ai_next) {
273 if(inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr), dnshost, INET6_ADDRSTRLEN)==dnshost){
274 if(dnshost!=NULL){
zeshuai007997e2d42020-09-29 14:43:18 +0800275 g_info("DNS address [%i -> %s]", ((guint32)(p->ai_addrlen)), dnshost);
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200276 if(!strncmp(dnshost, socket_ip, sizeip)){
277 retval=1;
278 break; /* if we get here, we must have connected successfully */
279 }
280 }
281 }
James E. King, III43f4bf22017-10-28 12:54:02 -0400282 }
James E. King, III43f4bf22017-10-28 12:54:02 -0400283 }
James E. King, III36628a22017-02-13 15:25:41 -0500284
James E. King, III43f4bf22017-10-28 12:54:02 -0400285 if(addr_info)
286 freeaddrinfo(addr_info);
James E. King, III36628a22017-02-13 15:25:41 -0500287
James E. King, III43f4bf22017-10-28 12:54:02 -0400288 return retval;
James E. King, III36628a22017-02-13 15:25:41 -0500289}
290
zeshuai007997e2d42020-09-29 14:43:18 +0800291static void
James E. King, III36628a22017-02-13 15:25:41 -0500292read_from_file(char *buffer, long size, const char *file_name)
293{
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200294 char ch;
295 long index=0;
296 FILE *fp;
James E. King, III36628a22017-02-13 15:25:41 -0500297
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200298 fp = fopen(file_name,"r"); /* read mode */
James E. King, III36628a22017-02-13 15:25:41 -0500299
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200300 if( fp == NULL )
301 {
302 perror("Error while opening the file.\n");
303 exit(EXIT_FAILURE);
304 }
James E. King, III36628a22017-02-13 15:25:41 -0500305
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200306 printf("The contents of %s file are :\n", file_name);
James E. King, III36628a22017-02-13 15:25:41 -0500307
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200308 while(index<size && ( ch = fgetc(fp) ) != EOF ){
309 buffer[index++] = ch;
310 }
James E. King, III36628a22017-02-13 15:25:41 -0500311
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200312 fclose(fp);
James E. King, III36628a22017-02-13 15:25:41 -0500313}
314
315#define ISSUER_CN_PINNING "The Apache Software Foundation"
316#define SUBJECT_CN_PINNING "localhost"
317#define CERT_SERIAL_NUMBER "1"
318
319gboolean verify_certificate_sn(X509 *cert, const unsigned char *serial_number)
320{
James E. King, III43f4bf22017-10-28 12:54:02 -0400321 gboolean retval = FALSE;
James E. King, III36628a22017-02-13 15:25:41 -0500322
James E. King, III43f4bf22017-10-28 12:54:02 -0400323 ASN1_INTEGER *serial = X509_get_serialNumber(cert);
James E. King, III36628a22017-02-13 15:25:41 -0500324
James E. King, III43f4bf22017-10-28 12:54:02 -0400325 BIGNUM *bn = ASN1_INTEGER_to_BN(serial, NULL);
326 if (!bn) {
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200327 fprintf(stderr, "unable to convert ASN1INTEGER to BN\n");
328 return EXIT_FAILURE;
James E. King, III43f4bf22017-10-28 12:54:02 -0400329 }
330 char *tmp = BN_bn2dec(bn);
331 if (!tmp) {
zeshuai007997e2d42020-09-29 14:43:18 +0800332 g_warning((const char*)stderr, "unable to convert BN to decimal string.\n");
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200333 BN_free(bn);
334 return EXIT_FAILURE;
James E. King, III43f4bf22017-10-28 12:54:02 -0400335 }
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200336 /*
337 if (strlen(tmp) >= len) {
338 g_warn(stderr, "buffer length shorter than serial number\n");
339 BN_free(bn);
340 OPENSSL_free(tmp);
341 return EXIT_FAILURE;
342 }
343 */
zeshuai007997e2d42020-09-29 14:43:18 +0800344 if(!strncmp((const char*)serial_number, tmp, strlen((const char*)serial_number))){
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200345 retval=TRUE;
James E. King, III43f4bf22017-10-28 12:54:02 -0400346 }else{
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200347 g_warning("Serial number is not valid");
James E. King, III43f4bf22017-10-28 12:54:02 -0400348 }
James E. King, III36628a22017-02-13 15:25:41 -0500349
James E. King, III43f4bf22017-10-28 12:54:02 -0400350 BN_free(bn);
351 OPENSSL_free(tmp);
352 return retval;
James E. King, III36628a22017-02-13 15:25:41 -0500353}
354
355gboolean my_access_manager(ThriftTransport * transport, X509 *cert, struct sockaddr_storage *addr, GError **error)
356{
James E. King, III43f4bf22017-10-28 12:54:02 -0400357 ThriftSSLSocket *sslSocket = THRIFT_SSL_SOCKET (transport);
zeshuai007997e2d42020-09-29 14:43:18 +0800358 THRIFT_UNUSED_VAR (error);
359 THRIFT_UNUSED_VAR (sslSocket);
James E. King, III36628a22017-02-13 15:25:41 -0500360
James E. King, III43f4bf22017-10-28 12:54:02 -0400361 g_info("Processing access to the server");
362 X509_NAME* iname = cert ? X509_get_issuer_name(cert) : NULL;
363 X509_NAME* sname = cert ? X509_get_subject_name(cert) : NULL;
James E. King, III36628a22017-02-13 15:25:41 -0500364
James E. King, III43f4bf22017-10-28 12:54:02 -0400365 /* Issuer is the authority we trust that warrants nothing useful */
366 const unsigned char * issuer = get_cn_name(iname);
367 if(issuer){
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200368 gboolean valid = TRUE;
369 g_info("Issuer (cn) %s", issuer);
James E. King, III36628a22017-02-13 15:25:41 -0500370
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200371 /* Issuer pinning */
zeshuai007997e2d42020-09-29 14:43:18 +0800372 if(strncmp(ISSUER_CN_PINNING, (const char*)issuer, strlen(ISSUER_CN_PINNING))){
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200373 g_warning("The Issuer of the certificate is not valid");
374 valid=FALSE;
375 }
zeshuai007997e2d42020-09-29 14:43:18 +0800376 OPENSSL_free((void*)issuer);
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200377 if(!valid)
378 return valid;
James E. King, III43f4bf22017-10-28 12:54:02 -0400379 }
James E. King, III36628a22017-02-13 15:25:41 -0500380
381
James E. King, III43f4bf22017-10-28 12:54:02 -0400382 /* Subject is who the certificate is issued to by the authority */
383 const unsigned char * subject = get_cn_name(sname);
384 if(subject){
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200385 g_info("Subject (cn) %s", subject);
386 gboolean valid = TRUE;
James E. King, III36628a22017-02-13 15:25:41 -0500387
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200388 /* Subject pinning */
zeshuai007997e2d42020-09-29 14:43:18 +0800389 if(strncmp(SUBJECT_CN_PINNING, (const char*)subject, strlen(SUBJECT_CN_PINNING))){
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200390 g_warning("The subject of the certificate is not valid");
391 valid=FALSE;
392 }
James E. King, III36628a22017-02-13 15:25:41 -0500393
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200394 if(!valid)
395 return valid;
James E. King, III36628a22017-02-13 15:25:41 -0500396
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200397 /* Host pinning */
zeshuai007997e2d42020-09-29 14:43:18 +0800398 if(verify_ip((char*)subject, addr)){
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200399 g_info("Verified subject");
400 }else{
401 g_info("Cannot verify subject");
402 valid=FALSE;
403 }
zeshuai007997e2d42020-09-29 14:43:18 +0800404 OPENSSL_free((void*)subject);
James E. King, III36628a22017-02-13 15:25:41 -0500405
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200406 if(!valid)
407 return valid;
James E. King, III43f4bf22017-10-28 12:54:02 -0400408 }
James E. King, III36628a22017-02-13 15:25:41 -0500409
zeshuai007997e2d42020-09-29 14:43:18 +0800410 if(!verify_certificate_sn(cert, (const unsigned char*)CERT_SERIAL_NUMBER)){
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200411 return FALSE;
James E. King, III43f4bf22017-10-28 12:54:02 -0400412 }else{
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200413 g_info("Verified serial number");
James E. King, III43f4bf22017-10-28 12:54:02 -0400414 }
James E. King, III36628a22017-02-13 15:25:41 -0500415
James E. King, III43f4bf22017-10-28 12:54:02 -0400416 return TRUE;
James E. King, III36628a22017-02-13 15:25:41 -0500417
418}
419
420
421
422
423#ifdef BUILD_SERVER
424static void
425test_ssl_authorization_manager(void)
426{
James E. King, III43f4bf22017-10-28 12:54:02 -0400427 int status=0;
428 pid_t pid;
429 ThriftSSLSocket *tSSLsocket = NULL;
430 ThriftTransport *transport = NULL;
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200431 /* int port = 51199; */
James E. King, III43f4bf22017-10-28 12:54:02 -0400432 int port = 443;
433 GError *error=NULL;
James E. King, III36628a22017-02-13 15:25:41 -0500434
James E. King, III43f4bf22017-10-28 12:54:02 -0400435 guchar buf[17] = TEST_DATA; /* a buffer */
James E. King, III36628a22017-02-13 15:25:41 -0500436
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200437/*
438 pid = fork ();
439 g_assert ( pid >= 0 );
440
441 if ( pid == 0 )
442 {
443 thrift_ssl_socket_server (port);
444 exit (0);
445 } else {
446 */
James E. King, III43f4bf22017-10-28 12:54:02 -0400447 /* parent connects, wait a bit for the socket to be created */
448 sleep (1);
James E. King, III36628a22017-02-13 15:25:41 -0500449
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200450 /* Test against level2 owncloud certificate */
James E. King, III43f4bf22017-10-28 12:54:02 -0400451 tSSLsocket = thrift_ssl_socket_new_with_host(SSLTLS, "localhost", port, &error);
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200452 thrift_ssl_socket_set_manager(tSSLsocket, my_access_manager); /* Install pinning manager */
453 /* thrift_ssl_load_cert_from_file(tSSLsocket, "./owncloud.level2crm.pem"); */
James E. King, III43f4bf22017-10-28 12:54:02 -0400454 unsigned char cert_buffer[65534];
455 read_from_file(cert_buffer, 65534, "../../keys/client.pem");
456 if(!thrift_ssl_load_cert_from_buffer(tSSLsocket, cert_buffer)){
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200457 g_warning("Certificates cannot be loaded!");
James E. King, III43f4bf22017-10-28 12:54:02 -0400458 }
James E. King, III36628a22017-02-13 15:25:41 -0500459
James E. King, III43f4bf22017-10-28 12:54:02 -0400460 transport = THRIFT_TRANSPORT (tSSLsocket);
461 g_assert (thrift_ssl_socket_open (transport, NULL) == TRUE);
462 g_assert (thrift_ssl_socket_is_open (transport));
James E. King, III36628a22017-02-13 15:25:41 -0500463
James E. King, III43f4bf22017-10-28 12:54:02 -0400464 thrift_ssl_socket_write (transport, buf, 17, NULL);
James E. King, III36628a22017-02-13 15:25:41 -0500465
James E. King, III43f4bf22017-10-28 12:54:02 -0400466 /* write fail */
467 send_error = 1;
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200468 /*
469 thrift_ssl_socket_write (transport, buf, 1, NULL);
470 send_error = 0;
471 thrift_ssl_socket_write_end (transport, NULL);
472 thrift_ssl_socket_flush (transport, NULL);
473 */
James E. King, III43f4bf22017-10-28 12:54:02 -0400474 thrift_ssl_socket_close (transport, NULL);
475 g_object_unref (tSSLsocket);
James E. King, III36628a22017-02-13 15:25:41 -0500476
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200477 /* g_assert ( wait (&status) == pid ); */
James E. King, III43f4bf22017-10-28 12:54:02 -0400478 g_assert ( status == 0 );
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200479 /* } */
James E. King, III36628a22017-02-13 15:25:41 -0500480}
481#endif
482
483
James E. King, III36628a22017-02-13 15:25:41 -0500484static void
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200485thrift_socket_server (const int port)
James E. King, III36628a22017-02-13 15:25:41 -0500486{
James E. King, III43f4bf22017-10-28 12:54:02 -0400487 int bytes = 0;
488 ThriftServerTransport *transport = NULL;
489 ThriftTransport *client = NULL;
490 guchar buf[10]; /* a buffer */
zeshuai007997e2d42020-09-29 14:43:18 +0800491 guchar match[] = TEST_DATA;
James E. King, III36628a22017-02-13 15:25:41 -0500492
James E. King, III43f4bf22017-10-28 12:54:02 -0400493 ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200494 "port", port, NULL);
James E. King, III36628a22017-02-13 15:25:41 -0500495
James E. King, III43f4bf22017-10-28 12:54:02 -0400496 transport = THRIFT_SERVER_TRANSPORT (tsocket);
497 thrift_server_transport_listen (transport, NULL);
498 client = thrift_server_transport_accept (transport, NULL);
499 g_assert (client != NULL);
James E. King, III36628a22017-02-13 15:25:41 -0500500
James E. King, III43f4bf22017-10-28 12:54:02 -0400501 /* read 10 bytes */
502 bytes = thrift_ssl_socket_read (client, buf, 10, NULL);
503 g_assert (bytes == 10); /* make sure we've read 10 bytes */
504 g_assert ( memcmp(buf, match, 10) == 0 ); /* make sure what we got matches */
James E. King, III36628a22017-02-13 15:25:41 -0500505
James E. King, III43f4bf22017-10-28 12:54:02 -0400506 /* failed read */
507 recv_error = 1;
508 thrift_ssl_socket_read (client, buf, 1, NULL);
509 recv_error = 0;
James E. King, III36628a22017-02-13 15:25:41 -0500510
James E. King, III43f4bf22017-10-28 12:54:02 -0400511 thrift_ssl_socket_read_end (client, NULL);
512 thrift_ssl_socket_close (client, NULL);
513 g_object_unref (tsocket);
514 g_object_unref (client);
James E. King, III36628a22017-02-13 15:25:41 -0500515}
516
517int
518main(int argc, char *argv[])
519{
James E. King, III43f4bf22017-10-28 12:54:02 -0400520 int retval;
James E. King, III36628a22017-02-13 15:25:41 -0500521#if (!GLIB_CHECK_VERSION (2, 36, 0))
James E. King, III43f4bf22017-10-28 12:54:02 -0400522 g_type_init();
James E. King, III36628a22017-02-13 15:25:41 -0500523#endif
524
James E. King, III43f4bf22017-10-28 12:54:02 -0400525 g_test_init (&argc, &argv, NULL);
James E. King, III36628a22017-02-13 15:25:41 -0500526
James E. King, III43f4bf22017-10-28 12:54:02 -0400527 thrift_ssl_socket_initialize_openssl();
James E. King, III36628a22017-02-13 15:25:41 -0500528
James E. King, III43f4bf22017-10-28 12:54:02 -0400529 g_test_add_func ("/testtransportsslsocket/CreateAndDestroy", test_ssl_create_and_destroy);
530 g_test_add_func ("/testtransportsslsocket/CreateAndSetProperties", test_ssl_create_and_set_properties);
Gonzalo Aguilar Delgado87ad2bc2017-09-15 12:26:02 +0200531 g_test_add_func ("/testtransportsslsocket/OpenAndCloseNonSSLServer", test_ssl_open_and_close_non_ssl_server);
532 g_test_add_func ("/testtransportsslsocket/OpenAndWriteInvalidSocket", test_ssl_write_invalid_socket);
533
534
535
James E. King, III36628a22017-02-13 15:25:41 -0500536
James E. King, III43f4bf22017-10-28 12:54:02 -0400537 retval = g_test_run ();
James E. King, III36628a22017-02-13 15:25:41 -0500538
James E. King, III43f4bf22017-10-28 12:54:02 -0400539 thrift_ssl_socket_finalize_openssl();
James E. King, III36628a22017-02-13 15:25:41 -0500540
James E. King, III43f4bf22017-10-28 12:54:02 -0400541 return retval;
James E. King, III36628a22017-02-13 15:25:41 -0500542}
543