|
@@ -0,0 +1,344 @@
|
|
1
|
+--- a/src/libwaitress/waitress.h
|
|
2
|
++++ b/src/libwaitress/waitress.h
|
|
3
|
+@@ -27,7 +27,12 @@ THE SOFTWARE.
|
|
4
|
+ #include <stdlib.h>
|
|
5
|
+ #include <unistd.h>
|
|
6
|
+ #include <stdbool.h>
|
|
7
|
++
|
|
8
|
++#if defined(USE_POLARSSL)
|
|
9
|
++typedef struct _polarssl_ctx polarssl_ctx;
|
|
10
|
++#else
|
|
11
|
+ #include <gnutls/gnutls.h>
|
|
12
|
++#endif
|
|
13
|
+
|
|
14
|
+ #define LIBWAITRESS_NAME "libwaitress"
|
|
15
|
+
|
|
16
|
+@@ -102,8 +107,9 @@ typedef struct {
|
|
17
|
+ WaitressUrl_t url;
|
|
18
|
+ WaitressUrl_t proxy;
|
|
19
|
+
|
|
20
|
++#if !defined(USE_POLARSSL)
|
|
21
|
+ gnutls_certificate_credentials_t tlsCred;
|
|
22
|
+-
|
|
23
|
++#endif
|
|
24
|
+ /* per-request data */
|
|
25
|
+ struct {
|
|
26
|
+ int sockfd;
|
|
27
|
+@@ -121,7 +127,11 @@ typedef struct {
|
|
28
|
+ WaitressReturn_t (*read) (void *, char *, const size_t, size_t *);
|
|
29
|
+ WaitressReturn_t (*write) (void *, const char *, const size_t);
|
|
30
|
+
|
|
31
|
++#if defined(USE_POLARSSL)
|
|
32
|
++ polarssl_ctx* sslCtx;
|
|
33
|
++#else
|
|
34
|
+ gnutls_session_t tlsSession;
|
|
35
|
++#endif
|
|
36
|
+ } request;
|
|
37
|
+ } WaitressHandle_t;
|
|
38
|
+
|
|
39
|
+--- a/src/pianod.c
|
|
40
|
++++ b/src/pianod.c
|
|
41
|
+@@ -531,8 +531,11 @@ static bool initialize_libraries (APPSTA
|
|
42
|
+ gcry_check_version (NULL);
|
|
43
|
+ gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
|
|
44
|
+ gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
|
|
45
|
++
|
|
46
|
++#if !defined(USE_POLARSSL)
|
|
47
|
+ int crystatus = gnutls_global_init ();
|
|
48
|
+ if (crystatus == GNUTLS_E_SUCCESS) {
|
|
49
|
++#endif
|
|
50
|
+ PianoReturn_t status = PianoInit (&app->ph, app->settings.partnerUser, app->settings.partnerPassword,
|
|
51
|
+ app->settings.device, app->settings.inkey, app->settings.outkey);
|
|
52
|
+ if (status == PIANO_RET_OK) {
|
|
53
|
+@@ -545,11 +548,13 @@ static bool initialize_libraries (APPSTA
|
|
54
|
+ } else {
|
|
55
|
+ flog (LOG_ERROR, "initialize_libraries: PianoInit: %s", PianoErrorToStr (status));
|
|
56
|
+ }
|
|
57
|
++#if !defined(USE_POLARSSL)
|
|
58
|
+ gnutls_global_deinit ();
|
|
59
|
+ } else {
|
|
60
|
+ flog (LOG_ERROR, "initialize_libraries: gnutls_global_init: %s", gcry_strerror (crystatus));
|
|
61
|
+
|
|
62
|
+ }
|
|
63
|
++#endif
|
|
64
|
+ return false;
|
|
65
|
+ }
|
|
66
|
+
|
|
67
|
+@@ -728,7 +733,9 @@ int main (int argc, char **argv) {
|
|
68
|
+ PianoDestroyPlaylist (app.song_history);
|
|
69
|
+ PianoDestroyPlaylist (app.playlist);
|
|
70
|
+ WaitressFree (&app.waith);
|
|
71
|
++#if !defined(USE_POLARSSL)
|
|
72
|
+ gnutls_global_deinit ();
|
|
73
|
++#endif
|
|
74
|
+ settings_destroy (&app.settings);
|
|
75
|
+ }
|
|
76
|
+
|
|
77
|
+--- a/src/libwaitress/waitress.c
|
|
78
|
++++ b/src/libwaitress/waitress.c
|
|
79
|
+@@ -41,11 +41,33 @@ THE SOFTWARE.
|
|
80
|
+ #include <assert.h>
|
|
81
|
+ #include <stdint.h>
|
|
82
|
+
|
|
83
|
+-#include <gnutls/x509.h>
|
|
84
|
+
|
|
85
|
+ #include "config.h"
|
|
86
|
+ #include "waitress.h"
|
|
87
|
+
|
|
88
|
++#if defined(USE_POLARSSL)
|
|
89
|
++
|
|
90
|
++#include <polarssl/ssl.h>
|
|
91
|
++#include <polarssl/entropy.h>
|
|
92
|
++#include <polarssl/ctr_drbg.h>
|
|
93
|
++#include <polarssl/x509.h>
|
|
94
|
++#include <polarssl/sha1.h>
|
|
95
|
++
|
|
96
|
++struct _polarssl_ctx
|
|
97
|
++{
|
|
98
|
++ ssl_context ssl;
|
|
99
|
++ ssl_session session;
|
|
100
|
++ entropy_context entrophy;
|
|
101
|
++ ctr_drbg_context rnd;
|
|
102
|
++};
|
|
103
|
++
|
|
104
|
++#else
|
|
105
|
++
|
|
106
|
++// Use gnutls by default (USE_POLARSSL not defined)
|
|
107
|
++#include <gnutls/x509.h>
|
|
108
|
++
|
|
109
|
++#endif
|
|
110
|
++
|
|
111
|
+ #define strcaseeq(a,b) (strcasecmp(a,b) == 0)
|
|
112
|
+ #define WAITRESS_HTTP_VERSION "1.1"
|
|
113
|
+
|
|
114
|
+@@ -56,6 +78,13 @@ typedef struct {
|
|
115
|
+
|
|
116
|
+ static WaitressReturn_t WaitressReceiveHeaders (WaitressHandle_t *, size_t *);
|
|
117
|
+
|
|
118
|
++// gnutls wants (void *) and polarssl want (unsigned char *)
|
|
119
|
++#if defined(USE_POLARSSL)
|
|
120
|
++#define BUFFER_CAST unsigned char
|
|
121
|
++#else
|
|
122
|
++#define BUFFER_CAST void
|
|
123
|
++#endif
|
|
124
|
++
|
|
125
|
+ #define READ_RET(buf, count, size) \
|
|
126
|
+ if ((wRet = waith->request.read (waith, buf, count, size)) != \
|
|
127
|
+ WAITRESS_RET_OK) { \
|
|
128
|
+@@ -444,7 +473,7 @@ static int WaitressPollLoop (int fd, sho
|
|
129
|
+ * @param write count bytes
|
|
130
|
+ * @return number of written bytes or -1 on error
|
|
131
|
+ */
|
|
132
|
+-static ssize_t WaitressPollWrite (void *data, const void *buf, size_t count) {
|
|
133
|
++static ssize_t WaitressPollWrite (void *data, const BUFFER_CAST *buf, size_t count) {
|
|
134
|
+ int pollres = -1;
|
|
135
|
+ ssize_t retSize;
|
|
136
|
+ WaitressHandle_t *waith = data;
|
|
137
|
+@@ -478,13 +507,20 @@ static WaitressReturn_t WaitressOrdinary
|
|
138
|
+ return waith->request.readWriteRet;
|
|
139
|
+ }
|
|
140
|
+
|
|
141
|
+-static WaitressReturn_t WaitressGnutlsWrite (void *data, const char *buf,
|
|
142
|
++static WaitressReturn_t WaitressTlsWrite (void *data, const char *buf,
|
|
143
|
+ const size_t size) {
|
|
144
|
+ WaitressHandle_t *waith = data;
|
|
145
|
++#if defined(USE_POLARSSL)
|
|
146
|
++
|
|
147
|
++ if (ssl_write (&waith->request.sslCtx->ssl, buf, size) < 0) {
|
|
148
|
++ return WAITRESS_RET_TLS_WRITE_ERR;
|
|
149
|
++ }
|
|
150
|
++#else
|
|
151
|
+
|
|
152
|
+ if (gnutls_record_send (waith->request.tlsSession, buf, size) < 0) {
|
|
153
|
+ return WAITRESS_RET_TLS_WRITE_ERR;
|
|
154
|
+ }
|
|
155
|
++#endif
|
|
156
|
+ return waith->request.readWriteRet;
|
|
157
|
+ }
|
|
158
|
+
|
|
159
|
+@@ -494,7 +530,7 @@ static WaitressReturn_t WaitressGnutlsWr
|
|
160
|
+ * @param buffer size
|
|
161
|
+ * @return number of read bytes or -1 on error
|
|
162
|
+ */
|
|
163
|
+-static ssize_t WaitressPollRead (void *data, void *buf, size_t count) {
|
|
164
|
++static ssize_t WaitressPollRead (void *data, BUFFER_CAST *buf, size_t count) {
|
|
165
|
+ int pollres = -1;
|
|
166
|
+ ssize_t retSize;
|
|
167
|
+ WaitressHandle_t *waith = data;
|
|
168
|
+@@ -531,16 +567,34 @@ static WaitressReturn_t WaitressOrdinary
|
|
169
|
+ return waith->request.readWriteRet;
|
|
170
|
+ }
|
|
171
|
+
|
|
172
|
+-static WaitressReturn_t WaitressGnutlsRead (void *data, char *buf,
|
|
173
|
++static WaitressReturn_t WaitressTlsRead (void *data, char *buf,
|
|
174
|
+ const size_t size, size_t *retSize) {
|
|
175
|
+ WaitressHandle_t *waith = data;
|
|
176
|
+
|
|
177
|
++#if defined(USE_POLARSSL)
|
|
178
|
++ int ret;
|
|
179
|
++
|
|
180
|
++ *retSize = 0;
|
|
181
|
++ waith->request.readWriteRet = WAITRESS_RET_OK;
|
|
182
|
++ ret = ssl_read (&waith->request.sslCtx->ssl, buf, size);
|
|
183
|
++
|
|
184
|
++ if (ret < 0) {
|
|
185
|
++ if (ret != POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY) {
|
|
186
|
++ waith->request.readWriteRet = WAITRESS_RET_TLS_READ_ERR;
|
|
187
|
++ }
|
|
188
|
++
|
|
189
|
++ return waith->request.readWriteRet;
|
|
190
|
++ }
|
|
191
|
++
|
|
192
|
++ *retSize = ret;
|
|
193
|
++#else
|
|
194
|
+ ssize_t ret = gnutls_record_recv (waith->request.tlsSession, buf, size);
|
|
195
|
+ if (ret < 0) {
|
|
196
|
+ return WAITRESS_RET_TLS_READ_ERR;
|
|
197
|
+ } else {
|
|
198
|
+ *retSize = ret;
|
|
199
|
+ }
|
|
200
|
++#endif
|
|
201
|
+ return waith->request.readWriteRet;
|
|
202
|
+ }
|
|
203
|
+
|
|
204
|
+@@ -727,10 +781,28 @@ static int WaitressParseStatusline (cons
|
|
205
|
+ /* verify server certificate
|
|
206
|
+ */
|
|
207
|
+ static WaitressReturn_t WaitressTlsVerify (const WaitressHandle_t *waith) {
|
|
208
|
++
|
|
209
|
++#if defined(USE_POLARSSL)
|
|
210
|
++ unsigned char fingerprint[20];
|
|
211
|
++
|
|
212
|
++ const x509_crt* cert = ssl_get_peer_cert (&waith->request.sslCtx->ssl);
|
|
213
|
++
|
|
214
|
++ if (NULL == cert) {
|
|
215
|
++ return WAITRESS_RET_TLS_HANDSHAKE_ERR;
|
|
216
|
++ }
|
|
217
|
++
|
|
218
|
++ sha1 (cert->raw.p, cert->raw.len, fingerprint);
|
|
219
|
++
|
|
220
|
++ if (memcmp (fingerprint, waith->tlsFingerprint, sizeof (fingerprint)) != 0) {
|
|
221
|
++ return WAITRESS_RET_TLS_FINGERPRINT_MISMATCH;
|
|
222
|
++ }
|
|
223
|
++
|
|
224
|
++#else
|
|
225
|
+ gnutls_session_t session = waith->request.tlsSession;
|
|
226
|
+ unsigned int certListSize;
|
|
227
|
+ const gnutls_datum_t *certList;
|
|
228
|
+ gnutls_x509_crt_t cert;
|
|
229
|
++ char fingerprint[20];
|
|
230
|
+
|
|
231
|
+ if (gnutls_certificate_type_get (session) != GNUTLS_CRT_X509) {
|
|
232
|
+ return WAITRESS_RET_TLS_HANDSHAKE_ERR;
|
|
233
|
+@@ -750,7 +822,6 @@ static WaitressReturn_t WaitressTlsVerif
|
|
234
|
+ return WAITRESS_RET_TLS_HANDSHAKE_ERR;
|
|
235
|
+ }
|
|
236
|
+
|
|
237
|
+- char fingerprint[20];
|
|
238
|
+ size_t fingerprintSize = sizeof (fingerprint);
|
|
239
|
+ if (gnutls_x509_crt_get_fingerprint (cert, GNUTLS_DIG_SHA1, fingerprint,
|
|
240
|
+ &fingerprintSize) != 0) {
|
|
241
|
+@@ -763,7 +834,7 @@ static WaitressReturn_t WaitressTlsVerif
|
|
242
|
+ }
|
|
243
|
+
|
|
244
|
+ gnutls_x509_crt_deinit (cert);
|
|
245
|
+-
|
|
246
|
++#endif
|
|
247
|
+ return WAITRESS_RET_OK;
|
|
248
|
+ }
|
|
249
|
+
|
|
250
|
+@@ -807,11 +878,6 @@ static WaitressReturn_t WaitressConnect
|
|
251
|
+ /* we need shorter timeouts for connect() */
|
|
252
|
+ fcntl (sock, F_SETFL, O_NONBLOCK);
|
|
253
|
+
|
|
254
|
+- /* increase socket receive buffer */
|
|
255
|
+- const int sockopt = 256*1024;
|
|
256
|
+- setsockopt (sock, SOL_SOCKET, SO_RCVBUF, &sockopt,
|
|
257
|
+- sizeof (sockopt));
|
|
258
|
+-
|
|
259
|
+ /* non-blocking connect will return immediately */
|
|
260
|
+ if (connect (sock, gacurr->ai_addr, gacurr->ai_addrlen) == -1) {
|
|
261
|
+ // Error if not in-progress or immediate success
|
|
262
|
+@@ -880,6 +946,11 @@ static WaitressReturn_t WaitressConnect
|
|
263
|
+ }
|
|
264
|
+ }
|
|
265
|
+
|
|
266
|
++#if defined(USE_POLARSSL)
|
|
267
|
++ if (ssl_handshake (&waith->request.sslCtx->ssl) != 0) {
|
|
268
|
++ return WAITRESS_RET_TLS_HANDSHAKE_ERR;
|
|
269
|
++ }
|
|
270
|
++#else
|
|
271
|
+ /* Ignore return code as connection will likely still succeed */
|
|
272
|
+ gnutls_server_name_set (waith->request.tlsSession, GNUTLS_NAME_DNS,
|
|
273
|
+ waith->url.host, strlen (waith->url.host));
|
|
274
|
+@@ -887,14 +958,15 @@ static WaitressReturn_t WaitressConnect
|
|
275
|
+ if (gnutls_handshake (waith->request.tlsSession) != GNUTLS_E_SUCCESS) {
|
|
276
|
+ return WAITRESS_RET_TLS_HANDSHAKE_ERR;
|
|
277
|
+ }
|
|
278
|
++#endif
|
|
279
|
+
|
|
280
|
+ if ((wRet = WaitressTlsVerify (waith)) != WAITRESS_RET_OK) {
|
|
281
|
+ return wRet;
|
|
282
|
+ }
|
|
283
|
+
|
|
284
|
+ /* now we can talk encrypted */
|
|
285
|
+- waith->request.read = WaitressGnutlsRead;
|
|
286
|
+- waith->request.write = WaitressGnutlsWrite;
|
|
287
|
++ waith->request.read = WaitressTlsRead;
|
|
288
|
++ waith->request.write = WaitressTlsWrite;
|
|
289
|
+ }
|
|
290
|
+
|
|
291
|
+ return WAITRESS_RET_OK;
|
|
292
|
+@@ -1120,6 +1192,21 @@ WaitressReturn_t WaitressFetchCall (Wait
|
|
293
|
+ waith->request.contentLengthKnown = false;
|
|
294
|
+
|
|
295
|
+ if (waith->url.tls) {
|
|
296
|
++#if defined(USE_POLARSSL)
|
|
297
|
++ waith->request.sslCtx = calloc (1, sizeof(polarssl_ctx));
|
|
298
|
++
|
|
299
|
++ entropy_init (&waith->request.sslCtx->entrophy);
|
|
300
|
++ ctr_drbg_init (&waith->request.sslCtx->rnd, entropy_func, &waith->request.sslCtx->entrophy, "libwaitress", 11);
|
|
301
|
++ ssl_init (&waith->request.sslCtx->ssl);
|
|
302
|
++
|
|
303
|
++ ssl_set_endpoint (&waith->request.sslCtx->ssl, SSL_IS_CLIENT);
|
|
304
|
++ ssl_set_authmode (&waith->request.sslCtx->ssl, SSL_VERIFY_NONE);
|
|
305
|
++ ssl_set_rng (&waith->request.sslCtx->ssl, ctr_drbg_random, &waith->request.sslCtx->rnd);
|
|
306
|
++ ssl_set_session (&waith->request.sslCtx->ssl, &waith->request.sslCtx->session);
|
|
307
|
++ ssl_set_bio (&waith->request.sslCtx->ssl,
|
|
308
|
++ WaitressPollRead, waith,
|
|
309
|
++ WaitressPollWrite, waith);
|
|
310
|
++#else
|
|
311
|
+ gnutls_init (&waith->request.tlsSession, GNUTLS_CLIENT);
|
|
312
|
+ gnutls_set_default_priority (waith->request.tlsSession);
|
|
313
|
+
|
|
314
|
+@@ -1137,6 +1224,7 @@ WaitressReturn_t WaitressFetchCall (Wait
|
|
315
|
+ WaitressPollRead);
|
|
316
|
+ gnutls_transport_set_push_function (waith->request.tlsSession,
|
|
317
|
+ WaitressPollWrite);
|
|
318
|
++#endif
|
|
319
|
+ }
|
|
320
|
+
|
|
321
|
+ /* buffer is required for connect already */
|
|
322
|
+@@ -1148,15 +1236,22 @@ WaitressReturn_t WaitressFetchCall (Wait
|
|
323
|
+ if ((wRet = WaitressSendRequest (waith)) == WAITRESS_RET_OK) {
|
|
324
|
+ wRet = WaitressReceiveResponse (waith);
|
|
325
|
+ }
|
|
326
|
++#if !defined(USE_POLARSSL)
|
|
327
|
+ if (waith->url.tls) {
|
|
328
|
+ gnutls_bye (waith->request.tlsSession, GNUTLS_SHUT_RDWR);
|
|
329
|
+ }
|
|
330
|
++#endif
|
|
331
|
+ }
|
|
332
|
+
|
|
333
|
+ /* cleanup */
|
|
334
|
+ if (waith->url.tls) {
|
|
335
|
++#if defined(USE_POLARSSL)
|
|
336
|
++ ssl_free (&waith->request.sslCtx->ssl);
|
|
337
|
++ free (waith->request.sslCtx);
|
|
338
|
++#else
|
|
339
|
+ gnutls_deinit (waith->request.tlsSession);
|
|
340
|
+ gnutls_certificate_free_credentials (waith->tlsCred);
|
|
341
|
++#endif
|
|
342
|
+ }
|
|
343
|
+ if (waith->request.sockfd != -1) {
|
|
344
|
+ close (waith->request.sockfd);
|