File: ssl.c

package info (click to toggle)
webfs 1.21+ds1-12
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 1,188 kB
  • ctags: 1,570
  • sloc: ansic: 3,293; sh: 418; makefile: 20; perl: 7
file content (331 lines) | stat: -rw-r--r-- 8,034 bytes parent folder | download | duplicates (2)
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/socket.h>

#ifdef USE_SSL
#include <openssl/err.h>
#include <openssl/evp.h>
#endif

#include "httpd.h"

#ifdef USE_THREADS
static pthread_mutex_t lock_ssl = PTHREAD_MUTEX_INITIALIZER;
#endif

ssize_t ssl_read(struct REQUEST *req, char *buf, size_t len)
{
    ssize_t rc;

#ifdef USE_GNUTLS
    memset(buf, 0, len);
    while (1) {
	rc = gnutls_record_recv(req->client_session, buf, len);
	if (rc == GNUTLS_E_INTERRUPTED || rc == GNUTLS_E_AGAIN)
	    continue;
	break;
    }
    if (debug && (rc < 0)) {
	fprintf(stderr, "%03d: gnutls read error: %s\n", req->fd,
		    gnutls_strerror(rc));
	/* An invalidating event has happened. Probably an
	 * interrupted session. */
	errno = EINVAL;
	return -1;
    }
#elif defined(USE_SSL)
    ERR_clear_error();
    rc = SSL_read(req->ssl_s, buf, len);
    if (rc < 0  &&  SSL_get_error(req->ssl_s, rc) == SSL_ERROR_WANT_READ) {
	errno = EAGAIN;
	return -1;
    }

    if (debug) {
	unsigned long err;
	while (0 != (err = ERR_get_error()))
	    fprintf(stderr, "%03d: ssl read error: %s\n", req->fd,
		    ERR_error_string(err, NULL));
    }

    if (rc < 0) {
	errno = EIO;
	return -1;
    }
#endif
    return rc;
}

ssize_t ssl_write(struct REQUEST *req, char *buf, size_t len)
{
    ssize_t rc;

#ifdef USE_GNUTLS
    while (1) {
	rc = gnutls_record_send(req->client_session, buf, len);
	if (rc == GNUTLS_E_INTERRUPTED || rc == GNUTLS_E_AGAIN)
	    continue;
	break;
    }
    if (debug && (rc < 0)) {
	fprintf(stderr, "%03d: gnutls write error: %s\n", req->fd,
		    gnutls_strerror(rc));
	errno = EINVAL;
	return -1;
    }
#elif defined(USE_SSL)
    ERR_clear_error();
    rc = SSL_write(req->ssl_s, buf, len);
    if (rc < 0 && SSL_get_error(req->ssl_s, rc) == SSL_ERROR_WANT_WRITE) {
	errno = EAGAIN;
	return -1;
    }

    if (debug) {
	unsigned long err;
	while (0 != (err = ERR_get_error()))
	    fprintf(stderr, "%03d: ssl read error: %s\n", req->fd,
		    ERR_error_string(err, NULL));
    }

    if (rc < 0) {
	errno = EIO;
	return -1;
    }
#endif
    return rc;
}

ssize_t ssl_blk_write(struct REQUEST *req, off_t offset, size_t len)
{
    ssize_t  rc;
    char buf[4 * 4096];

    if (lseek(req->bfd, offset, SEEK_SET) == -1) {
	if (debug)
	    perror("lseek");
	return -1;
    }

    if (len > sizeof(buf))
	len = sizeof(buf);
    rc = read(req->bfd, buf, len);
    if (rc <= 0) {
	/* shouldn't happen ... */
	req->state = STATE_CLOSE;
	return rc;
    }
    return ssl_write(req, buf, (size_t) rc);
}

#if defined(USE_SSL) && !defined(USE_GNUTLS)
static int password_cb(char *buf, int num, int rwflag, void *userdata)
{
    if (NULL == password)
	return 0;
    if (num < strlen(password)+1)
	return 0;
    
    strcpy(buf,password);
    return(strlen(buf));
}
#endif

int init_ssl(void)
{
    int rc;

#ifdef USE_GNUTLS
    DO_LOCK(lock_ssl);
    gnutls_global_init();

    gnutls_certificate_allocate_credentials(&x509_cred);
    if (cacert && strlen(cacert) &&
	    (rc = gnutls_certificate_set_x509_trust_file(x509_cred,
					cacert, GNUTLS_X509_FMT_PEM) <=0)){
	fprintf(stderr, "GnuTLS CA-cert error [No CA entries]\n");
	DO_UNLOCK(lock_ssl);
	exit (1);
    }
    rc = gnutls_certificate_set_x509_key_file(x509_cred,
                         certificate, keyfile, GNUTLS_X509_FMT_PEM);
    if (rc != GNUTLS_E_SUCCESS) {
	fprintf(stderr, "GnuTLS certificate error [%s]\n",
		gnutls_strerror(rc));
	DO_UNLOCK(lock_ssl);
	exit (1);
    } else if (debug)
	fprintf(stderr, "GnuTLS certificate load successful.\n");

    gnutls_dh_params_init(&dh_params);
    gnutls_dh_params_generate2(dh_params, DH_PARAMS_LEN);

    rc = gnutls_priority_init(&tls_priority_cache, cipherprio, NULL);
    if (rc != GNUTLS_E_SUCCESS) {
	fprintf(stderr, "Cipher priority error [%s]\nPriority string: %s\n",
		gnutls_strerror(rc), cipherprio);
	DO_UNLOCK(lock_ssl);
	exit (1);
    } else if (debug)
	fprintf(stderr, "Cipher type \"%s\" successful.\n", cipherprio);

    gnutls_certificate_set_dh_params(x509_cred, dh_params);
    DO_UNLOCK(lock_ssl);
    return rc;

#elif defined(USE_SSL)
    OpenSSL_add_all_algorithms();
    SSL_load_error_strings();
    SSL_library_init();
    ctx = SSL_CTX_new(SSLv23_server_method());
    if (NULL == ctx) {
        fprintf(stderr, "SSL init error [%s]",strerror(errno));
	exit (1);
    }

    rc = SSL_CTX_use_certificate_chain_file(ctx, certificate);
    switch (rc) {
    case 1:
	if (debug)
	    fprintf(stderr, "SSL certificate load ok\n");
	break;
    default:
	fprintf(stderr, "SSL cert load error [%s]\n",
		ERR_error_string(ERR_get_error(), NULL));
	break;
    }

    SSL_CTX_set_default_passwd_cb(ctx, password_cb);
    SSL_CTX_use_PrivateKey_file(ctx, certificate, SSL_FILETYPE_PEM);
    switch (rc) {
    case 1:
	if (debug)
	    fprintf(stderr, "SSL private key load ok\n");
	break;
    default:
	fprintf(stderr, "SSL privkey load error [%s]\n",
		ERR_error_string(ERR_get_error(), NULL));
	break;
    }

    SSL_CTX_set_options(ctx, SSL_OP_ALL | SSL_OP_NO_SSLv2);
    return rc;
#endif
}

int open_ssl_session(struct REQUEST *req)
{
#ifdef USE_GNUTLS
    int rc;

    DO_LOCK(lock_ssl);
    rc = gnutls_init(&req->client_session, GNUTLS_SERVER);
    if (rc != GNUTLS_E_SUCCESS) {
	DO_UNLOCK(lock_ssl);
	return rc;
    }
    rc = gnutls_priority_set(req->client_session, tls_priority_cache);
    if (rc != GNUTLS_E_SUCCESS) {
	gnutls_deinit(req->client_session);
	DO_UNLOCK(lock_ssl);
	return rc;
    }
    gnutls_credentials_set(req->client_session,
		GNUTLS_CRD_CERTIFICATE, x509_cred);
    gnutls_certificate_server_set_request(req->client_session,
		verify_cert ? GNUTLS_CERT_REQUIRE : GNUTLS_CERT_REQUEST);

#if !defined(USE_TLS_NO_COMPATIBILITY)
    gnutls_session_enable_compatibility_mode(req->client_session);
#endif
    DO_UNLOCK(lock_ssl);
    return GNUTLS_E_SUCCESS;

#elif defined(USE_SSL)
    DO_LOCK(lock_ssl);
    req->ssl_s = SSL_new(ctx);
    if (req->ssl_s == NULL) {
	if (debug)
	    fprintf(stderr,"%03d: SSL session init error [%s]\n",
		    req->fd, strerror(errno));
	/* FIXME: how to handle that one? */
    }
    SSL_set_fd(req->ssl_s, req->fd);
    SSL_set_accept_state(req->ssl_s);
    SSL_set_read_ahead(req->ssl_s, 0); /* to prevent unwanted buffering in ssl layer */
    DO_UNLOCK(lock_ssl);
    return 0;
#endif
}

#if defined(USE_GNUTLS)
int verify_certificate(gnutls_session_t session, char **answer) 
{
    unsigned int rc, j, len, answ;
    const gnutls_datum_t *certlist;
    gnutls_x509_crt_t cert;

    rc = gnutls_certificate_verify_peers2(session, &answ);
    if (rc != GNUTLS_E_SUCCESS)
	return rc;

    if (gnutls_certificate_type_get(session) != GNUTLS_CRT_X509) {
	*answer = "Certificate is not in x509v3 form.";
	return -1;
    }

    if (answ & GNUTLS_CERT_INVALID) {
	*answer = "Invalid certificate.";
	return -1;
    }

    if (answ & GNUTLS_CERT_SIGNER_NOT_FOUND) {
	*answer = "Certificate is not signed.";
	return -1;
    }

    if (answ & GNUTLS_CERT_REVOKED) {
	*answer = "Certificate is revoked.";
	return -1;
    }

    if (gnutls_x509_crt_init(&cert) != GNUTLS_E_SUCCESS)
	return rc;

    certlist = gnutls_certificate_get_peers(session, &len);
    if (certlist == NULL) {
	gnutls_x509_crt_deinit(cert);
	*answer = "No certificate chain was provided.";
	return -1;
    }

    for (j = 0; j < len; ++j) {
	if (gnutls_x509_crt_import(cert, &certlist[j], GNUTLS_X509_FMT_DER)
		!= GNUTLS_E_SUCCESS) {
	    gnutls_x509_crt_deinit(cert);
	    *answer = "Invalid certificate in chain.";
	    return -1;
	}
    }

    if (gnutls_x509_crt_get_expiration_time(cert) < time(0)) {
	gnutls_x509_crt_deinit(cert);
	*answer = "Certificate has expired.";
	return -1;
    }

    if (gnutls_x509_crt_get_activation_time(cert) > time(0)) {
	gnutls_x509_crt_deinit(cert);
	*answer = "Certificate is not yet activated.";
	return -1;
    }

    gnutls_x509_crt_deinit(cert);

    return GNUTLS_E_SUCCESS;
} /* verify_certificate(gnutls_session_t session, char *answer) */
#endif