1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
/* riemann/client/tls-gnutls.c -- Riemann C client library
* Copyright (C) 2013-2017 Gergely Nagy <algernon@madhouse-project.org>
*
* This library is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/socket.h>
#include "riemann/_private.h"
#include "riemann/platform.h"
#include "riemann/client/tls.h"
#include <gnutls/gnutls.h>
#include <gnutls/x509.h>
#if GNUTLS_VERSION_MAJOR == 2 || (GNUTLS_VERSION_MAJOR == 3 && GNUTLS_VERSION_MINOR < 3)
#include "riemann/client/tls-gnutls2.c"
#else
#include "riemann/client/tls-gnutls3.c"
#endif
void
_riemann_client_init_tls (riemann_client_t *client)
{
client->tls.session = NULL;
client->tls.creds = NULL;
}
void
_riemann_client_disconnect_tls (riemann_client_t *client)
{
if (client->send == _riemann_client_send_message_tls)
{
if (client->tls.session)
{
gnutls_deinit (client->tls.session);
client->tls.session = NULL;
}
if (client->tls.creds)
{
gnutls_certificate_free_credentials (client->tls.creds);
client->tls.creds = NULL;
}
}
}
int
_riemann_client_connect_setup_tls (riemann_client_t *client,
struct addrinfo *hints,
va_list aq,
riemann_client_tls_options_t *tls_options)
{
va_list ap;
riemann_client_option_t option;
memset (tls_options, 0, sizeof (riemann_client_tls_options_t));
tls_options->handshake_timeout = GNUTLS_DEFAULT_HANDSHAKE_TIMEOUT;
client->send = _riemann_client_send_message_tls;
client->recv = _riemann_client_recv_message_tls;
hints->ai_socktype = SOCK_STREAM;
va_copy (ap, aq);
option = (riemann_client_option_t) va_arg (ap, int);
do
{
switch (option)
{
case RIEMANN_CLIENT_OPTION_NONE:
break;
case RIEMANN_CLIENT_OPTION_TLS_CA_FILE:
tls_options->cafn = va_arg (ap, char *);
break;
case RIEMANN_CLIENT_OPTION_TLS_CERT_FILE:
tls_options->certfn = va_arg (ap, char *);
break;
case RIEMANN_CLIENT_OPTION_TLS_KEY_FILE:
tls_options->keyfn = va_arg (ap, char *);
break;
case RIEMANN_CLIENT_OPTION_TLS_HANDSHAKE_TIMEOUT:
tls_options->handshake_timeout = va_arg (ap, unsigned int);
break;
case RIEMANN_CLIENT_OPTION_TLS_PRIORITIES:
tls_options->priorities = va_arg (ap, char *);
break;
default:
va_end (ap);
return -EINVAL;
}
if (option != RIEMANN_CLIENT_OPTION_NONE)
option = (riemann_client_option_t) va_arg (ap, int);
}
while (option != RIEMANN_CLIENT_OPTION_NONE);
va_end (ap);
if (!tls_options->cafn || !tls_options->certfn || !tls_options->keyfn)
{
return -EINVAL;
}
return 0;
}
int
_riemann_client_connect_tls_handshake (riemann_client_t *client,
riemann_client_tls_options_t *tls_options)
{
int e;
gnutls_certificate_allocate_credentials (&(client->tls.creds));
gnutls_certificate_set_x509_trust_file (client->tls.creds, tls_options->cafn,
GNUTLS_X509_FMT_PEM);
#if GNUTLS_VERSION_MAJOR > 2 || (GNUTLS_VERSION_MAJOR == 2 && GNUTLS_VERSION_MINOR >= 10)
gnutls_certificate_set_verify_function (client->tls.creds,
_verify_certificate_callback);
#endif
gnutls_certificate_set_x509_key_file (client->tls.creds,
tls_options->certfn, tls_options->keyfn,
GNUTLS_X509_FMT_PEM);
gnutls_init (&client->tls.session, GNUTLS_CLIENT);
if (tls_options->priorities)
{
if (gnutls_priority_set_direct (client->tls.session, tls_options->priorities, NULL) != GNUTLS_E_SUCCESS)
{
e = -1;
goto end;
}
}
else
gnutls_set_default_priority (client->tls.session);
gnutls_credentials_set (client->tls.session, GNUTLS_CRD_CERTIFICATE,
client->tls.creds);
_tls_handshake_setup (client, tls_options);
do {
e = gnutls_handshake (client->tls.session);
} while (e < 0 && e != GNUTLS_E_AGAIN && gnutls_error_is_fatal (e) == 0);
#if GNUTLS_VERSION_MAJOR == 2 && GNUTLS_VERSION_MINOR < 10
if (e == 0 &&
_verify_certificate_callback (client->tls.session) != 0)
e = -1;
#endif
end:
if (e != 0)
{
gnutls_deinit (client->tls.session);
gnutls_certificate_free_credentials (client->tls.creds);
client->tls.session = NULL;
client->tls.creds = NULL;
return -EPROTO;
}
return 0;
}
int
_riemann_client_send_message_tls (riemann_client_t *client,
riemann_message_t *message)
{
uint8_t *buffer;
size_t len;
ssize_t sent;
buffer = riemann_message_to_buffer (message, &len);
if (!buffer)
return -errno;
size_t left = len;
while (left > 0)
{
do {
sent = gnutls_record_send (client->tls.session, buffer + len - left, left);
} while (sent == GNUTLS_E_AGAIN || sent == GNUTLS_E_INTERRUPTED);
if (sent < 0)
{
free (buffer);
return -EPROTO;
}
left -= sent;
}
free (buffer);
return 0;
}
riemann_message_t *
_riemann_client_recv_message_tls (riemann_client_t *client)
{
uint32_t header, len;
uint8_t *buffer;
ssize_t received;
size_t left;
riemann_message_t *message;
len = sizeof (header);
left = len;
while (left > 0)
{
do {
received = gnutls_record_recv (client->tls.session, &header + len - left, left);
} while (received == GNUTLS_E_AGAIN || received == GNUTLS_E_INTERRUPTED);
if (received <= 0)
{
errno = EPROTO;
return NULL;
}
left -= received;
}
len = ntohl (header);
buffer = (uint8_t *) malloc (len);
left = len;
while (left > 0)
{
do {
received = gnutls_record_recv (client->tls.session, buffer + len - left, left);
} while (received == GNUTLS_E_AGAIN || received == GNUTLS_E_INTERRUPTED);
if (received <= 0)
{
free (buffer);
errno = EPROTO;
return NULL;
}
left -= received;
}
message = riemann_message_from_buffer (buffer, len);
if (message == NULL)
{
int e = errno;
free (buffer);
errno = e;
return NULL;
}
free (buffer);
return message;
}
|