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
|
/*
* relay-network.c - network functions for relay plugin
*
* Copyright (C) 2003-2021 Sébastien Helleu <flashcode@flashtux.org>
*
* This file is part of WeeChat, the extensible chat client.
*
* WeeChat is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* WeeChat 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 WeeChat. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <gnutls/gnutls.h>
#include "../weechat-plugin.h"
#include "relay.h"
#include "relay-network.h"
#include "relay-config.h"
int relay_network_init_ok = 0;
int relay_network_init_ssl_cert_key_ok = 0;
gnutls_certificate_credentials_t relay_gnutls_x509_cred;
gnutls_priority_t *relay_gnutls_priority_cache = NULL;
gnutls_dh_params_t *relay_gnutls_dh_params = NULL;
/*
* Sets SSL certificate/key file.
*
* If verbose == 1, a message is displayed if successful, otherwise a warning
* (if no cert/key found in file).
*/
void
relay_network_set_ssl_cert_key (int verbose)
{
char *certkey_path;
int ret;
struct t_hashtable *options;
gnutls_certificate_free_credentials (relay_gnutls_x509_cred);
gnutls_certificate_allocate_credentials (&relay_gnutls_x509_cred);
relay_network_init_ssl_cert_key_ok = 0;
options = weechat_hashtable_new (
32,
WEECHAT_HASHTABLE_STRING,
WEECHAT_HASHTABLE_STRING,
NULL, NULL);
if (options)
weechat_hashtable_set (options, "directory", "config");
certkey_path = weechat_string_eval_path_home (
weechat_config_string (relay_config_network_ssl_cert_key),
NULL, NULL, options);
if (options)
weechat_hashtable_free (options);
if (certkey_path)
{
ret = gnutls_certificate_set_x509_key_file (relay_gnutls_x509_cred,
certkey_path,
certkey_path,
GNUTLS_X509_FMT_PEM);
if (ret >= 0)
{
relay_network_init_ssl_cert_key_ok = 1;
if (verbose)
{
weechat_printf (NULL,
_("%s: SSL certificate and key have been "
"set"),
RELAY_PLUGIN_NAME);
}
}
else
{
if (verbose)
{
weechat_printf (NULL,
_("%s%s: warning: no SSL certificate/key "
"found (option relay.network.ssl_cert_key)"),
weechat_prefix ("error"), RELAY_PLUGIN_NAME);
}
}
free (certkey_path);
}
}
/*
* Sets gnutls priority cache.
*/
void
relay_network_set_priority ()
{
if (gnutls_priority_init (relay_gnutls_priority_cache,
weechat_config_string (
relay_config_network_ssl_priorities),
NULL) != GNUTLS_E_SUCCESS)
{
weechat_printf (NULL,
_("%s%s: unable to initialize priority for SSL"),
weechat_prefix ("error"), RELAY_PLUGIN_NAME);
free (relay_gnutls_priority_cache);
relay_gnutls_priority_cache = NULL;
}
}
/*
* Initializes network for relay.
*/
void
relay_network_init ()
{
/* credentials */
gnutls_certificate_allocate_credentials (&relay_gnutls_x509_cred);
relay_network_set_ssl_cert_key (0);
/* priority */
relay_gnutls_priority_cache = malloc (sizeof (*relay_gnutls_priority_cache));
if (relay_gnutls_priority_cache)
relay_network_set_priority ();
relay_network_init_ok = 1;
}
/*
* Ends network for relay.
*/
void
relay_network_end ()
{
if (relay_network_init_ok)
{
if (relay_gnutls_priority_cache)
{
gnutls_priority_deinit (*relay_gnutls_priority_cache);
free (relay_gnutls_priority_cache);
relay_gnutls_priority_cache = NULL;
}
if (relay_gnutls_dh_params)
{
gnutls_dh_params_deinit (*relay_gnutls_dh_params);
free (relay_gnutls_dh_params);
relay_gnutls_dh_params = NULL;
}
gnutls_certificate_free_credentials (relay_gnutls_x509_cred);
relay_network_init_ok = 0;
}
}
|