tls_listeningpoint_polarssl.c 4.21 KB
Newer Older
1 2 3 4 5 6
/*
	belle-sip - SIP (RFC3261) library.
    Copyright (C) 2013  Belledonne Communications SARL

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
7
    the Free Software Foundation, either version 2 of the License, or
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "belle_sip_internal.h"

#ifdef HAVE_POLARSSL

23 24
#include <polarssl/ssl.h>

25
static void belle_sip_tls_listening_point_uninit(belle_sip_tls_listening_point_t *lp){
Simon Morlat's avatar
Simon Morlat committed
26
	belle_sip_object_unref(lp->verify_ctx);
27 28 29
}

static belle_sip_channel_t *tls_create_channel(belle_sip_listening_point_t *lp, const belle_sip_hop_t *hop){
Simon Morlat's avatar
Simon Morlat committed
30
	belle_sip_channel_t *chan=belle_sip_channel_new_tls(lp->stack, ((belle_sip_tls_listening_point_t*) lp)->verify_ctx
31 32 33 34 35 36 37 38
				,belle_sip_uri_get_host(lp->listening_uri)
				,belle_sip_uri_get_port(lp->listening_uri)
				,hop->cname
				,hop->host,hop->port);
	return chan;
}

BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(belle_sip_tls_listening_point_t);
39
BELLE_SIP_INSTANCIATE_CUSTOM_VPTR_BEGIN(belle_sip_tls_listening_point_t)
40 41
	{
		{
Simon Morlat's avatar
Simon Morlat committed
42
			BELLE_SIP_VPTR_INIT(belle_sip_tls_listening_point_t, belle_sip_stream_listening_point_t,TRUE),
43 44 45 46 47 48 49
			(belle_sip_object_destroy_t)belle_sip_tls_listening_point_uninit,
			NULL,
			NULL
		},
		"TLS",
		tls_create_channel
	}
50
BELLE_SIP_INSTANCIATE_CUSTOM_VPTR_END
51

52
#ifdef ENABLE_SERVER_SOCKETS
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
static int on_new_connection(void *userdata, unsigned int revents){
	belle_sip_socket_t child;
	struct sockaddr_storage addr;
	socklen_t slen=sizeof(addr);
	belle_sip_tls_listening_point_t *lp=(belle_sip_tls_listening_point_t*)userdata;
	belle_sip_stream_listening_point_t *super=(belle_sip_stream_listening_point_t*)lp;
	
	child=accept(super->server_sock,(struct sockaddr*)&addr,&slen);
	if (child==(belle_sip_socket_t)-1){
		belle_sip_error("Listening point [%p] accept() failed on TLS server socket: %s",lp,belle_sip_get_socket_error_string());
		belle_sip_stream_listening_point_destroy_server_socket(super);
		belle_sip_stream_listening_point_setup_server_socket(super,on_new_connection);
		return BELLE_SIP_STOP;
	}
	belle_sip_message("New connection arriving on TLS, not handled !");
	close_socket(child);
	return BELLE_SIP_CONTINUE;
}
71
#endif /* ENABLE_SERVER_SOCKETS */
72 73 74

belle_sip_listening_point_t * belle_sip_tls_listening_point_new(belle_sip_stack_t *s, const char *ipaddress, int port){
	belle_sip_tls_listening_point_t *lp=belle_sip_object_new(belle_sip_tls_listening_point_t);
75
#ifdef ENABLE_SERVER_SOCKETS
76
	belle_sip_stream_listening_point_init((belle_sip_stream_listening_point_t*)lp,s,ipaddress,port,on_new_connection);
77 78 79
#else
	belle_sip_stream_listening_point_init((belle_sip_stream_listening_point_t*)lp,s,ipaddress,port);
#endif /* ENABLE_SERVER_SOCKETS */
80
	
Simon Morlat's avatar
Simon Morlat committed
81 82
	lp->verify_ctx=belle_tls_verify_policy_new();

83 84 85 86
	return BELLE_SIP_LISTENING_POINT(lp);
}

int belle_sip_tls_listening_point_set_root_ca(belle_sip_tls_listening_point_t *lp, const char *path){
Simon Morlat's avatar
Simon Morlat committed
87
	return belle_tls_verify_policy_set_root_ca(lp->verify_ctx,path);
88 89 90
}

int belle_sip_tls_listening_point_set_verify_exceptions(belle_sip_tls_listening_point_t *lp, int flags){
Simon Morlat's avatar
Simon Morlat committed
91
	belle_tls_verify_policy_set_exceptions(lp->verify_ctx,flags);
92 93 94
	return 0;
}

Simon Morlat's avatar
Simon Morlat committed
95 96
int belle_sip_tls_listening_point_set_verify_policy(belle_sip_tls_listening_point_t *s, belle_tls_verify_policy_t *pol){
	SET_OBJECT_PROPERTY(s,verify_ctx,pol);
jehan's avatar
jehan committed
97
	return 0;
Simon Morlat's avatar
Simon Morlat committed
98 99
}

100 101 102 103
int belle_sip_tls_listening_point_available(void){
	return TRUE;
}

104 105 106 107 108 109 110 111 112 113 114 115 116 117
#else

belle_sip_listening_point_t * belle_sip_tls_listening_point_new(belle_sip_stack_t *s, const char *ipaddress, int port){
	return NULL;
}

int belle_sip_tls_listening_point_set_root_ca(belle_sip_tls_listening_point_t *s, const char *path){
	return -1;
}

int belle_sip_tls_listening_point_set_verify_exceptions(belle_sip_tls_listening_point_t *s, int value){
	return -1;
}

118 119 120 121
int belle_sip_tls_listening_point_available(void){
	return FALSE;
}

122
#endif
Simon Morlat's avatar
Simon Morlat committed
123