Package: webfs / 1.21+ds1-12

66_further_gnutls.diff Patch series | download
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
Description: Further useful functionality from libgnutls.
 Continuing on the first implementation for using libgnutls,
 this patch set includes further checks and refined properties.
 .
 Allow server certificate and key to be contained in separate files.
 .
 Allow the server to use a CA-chain file.
 .
 Arrange the cipher priorities to be configurable at start up.
 .
 Implement some useful verifications of the client certificate and
 its certificate chain. This is crafted as an on/off-option.
Author: Mats Erik Andersson <debian@gisladisker.se>
Forwarded: no
Last-Update: 2010-03-26
--- webfs-1.21+ds1/httpd.h.debian
+++ webfs-1.21+ds1/httpd.h
@@ -195,7 +195,11 @@ extern char     *certificate;
 extern char     *password;
 #elif defined(USE_GNUTLS)
 extern int      with_ssl;
+extern int      verify_cert;
+extern char     *cipherprio;
 extern char     *certificate;
+extern char     *keyfile;
+extern char     *cacert;
 extern char     *password;
 extern gnutls_certificate_credentials_t x509_cred;
 extern gnutls_dh_params_t dh_params;
@@ -221,6 +225,10 @@ extern int init_ssl(void);
 extern int open_ssl_session(struct REQUEST *req);
 #endif
 
+#if defined(USE_GNUTLS)
+extern int verify_certificate(gnutls_session_t session, char **answer);
+#endif
+
 /* --- request.c ------------------------------------------------ */
 
 void read_request(struct REQUEST *req, int pipelined);
--- webfs-1.21+ds1/ssl.c.debian
+++ webfs-1.21+ds1/ssl.c
@@ -144,8 +144,15 @@ int init_ssl(void)
     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, certificate, GNUTLS_X509_FMT_PEM);
+                         certificate, keyfile, GNUTLS_X509_FMT_PEM);
     if (rc != GNUTLS_E_SUCCESS) {
 	fprintf(stderr, "GnuTLS certificate error [%s]\n",
 		gnutls_strerror(rc));
@@ -157,14 +164,14 @@ int init_ssl(void)
     gnutls_dh_params_init(&dh_params);
     gnutls_dh_params_generate2(dh_params, DH_PARAMS_LEN);
 
-    rc = gnutls_priority_init(&tls_priority_cache, WEBFS_CIPHERS, NULL);
+    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), WEBFS_CIPHERS);
+		gnutls_strerror(rc), cipherprio);
 	DO_UNLOCK(lock_ssl);
 	exit (1);
     } else if (debug)
-	fprintf(stderr, "Cipher type \"%s\" successful.\n", WEBFS_CIPHERS);
+	fprintf(stderr, "Cipher type \"%s\" successful.\n", cipherprio);
 
     gnutls_certificate_set_dh_params(x509_cred, dh_params);
     DO_UNLOCK(lock_ssl);
@@ -228,9 +235,10 @@ int open_ssl_session(struct REQUEST *req
 	return rc;
     }
     gnutls_credentials_set(req->client_session,
-                    GNUTLS_CRD_CERTIFICATE, x509_cred);
+		GNUTLS_CRD_CERTIFICATE, x509_cred);
     gnutls_certificate_server_set_request(req->client_session,
-                    GNUTLS_CERT_REQUEST);
+		verify_cert ? GNUTLS_CERT_REQUIRE : GNUTLS_CERT_REQUEST);
+
 #if !defined(USE_TLS_NO_COMPATIBILITY)
     gnutls_session_enable_compatibility_mode(req->client_session);
 #endif
@@ -253,3 +261,71 @@ int open_ssl_session(struct REQUEST *req
     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
--- webfs-1.21+ds1/webfsd.c.debian
+++ webfs-1.21+ds1/webfsd.c
@@ -82,7 +82,11 @@ SSL_CTX *ctx;
 BIO	*sbio, *ssl_bio;
 #elif defined(USE_GNUTLS)
 int      with_ssl      = 0;
+int      verify_cert   = 0;
+char     *cipherprio   = WEBFS_CIPHERS;
 char     *certificate  = "server.pem";
+char     *keyfile      = "server.pem";
+char     *cacert       = NULL;
 char     *password     = NULL;
 gnutls_certificate_credentials_t x509_cred;
 gnutls_priority_t tls_priority_cache;
@@ -147,9 +151,14 @@ usage(char *name)
 	    "  -e sec   limit live span of files to sec\n"
 	    "           seconds (using expires header)\n"
 #if defined(USE_SSL) || defined(USE_GNUTLS)
-	    "  -S       enable SSL/TLS mode\n"
+	    "  -S       enable SSL/TLS mode                 [%s]\n"
 	    "  -C file  SSL-Certificate and key file        [%s]\n"
-#   if ! defined(USE_GNUTLS)
+#   if defined(USE_GNUTLS)
+	    "  -K file  key file, may be distinct           [%s]\n"
+	    "  -A file  optional CA-certificate             [%s]\n"
+	    "  -V       verify client certificate           [%s]\n"
+	    "  -Q ciph  priority for ciphers                [%s]\n"
+#   else
 	    "  -P pass  SSL-Certificate password\n"
 #   endif
 #endif
@@ -175,7 +184,14 @@ usage(char *name)
 	    mimetypes,
 	    pidfile ? pidfile : "none",
 #if defined(USE_SSL) || defined(USE_GNUTLS)
+	    with_ssl ? "on" : "off",
 	    certificate,
+#  if defined(USE_GNUTLS)
+	    keyfile,
+	    cacert,
+	    verify_cert ? "on" : "off",
+	    cipherprio,
+#  endif
 #endif
 	    cgipath ? cgipath : "none");
     if (getuid() == 0) {
@@ -517,6 +533,27 @@ mainloop(void *thread_arg)
 				break;
 			    }
 			}
+
+			/* Do sanity checks on the client's chain. */
+			if (!failure && verify_cert) {
+			    char *answer = NULL;
+			    int rc = verify_certificate(req->client_session,
+					    &answer);
+			    if (rc == -1) { /* Private error code. */
+				failure = 1;
+				if (debug)
+				    fprintf(stderr, "%03d: verify: %s\n",
+						req->fd, answer);
+			    } else if (rc != GNUTLS_E_SUCCESS) {
+				failure = 1;
+				if (debug)
+				    fprintf(stderr, "%03d: verify: %s\n",
+					    req->fd, gnutls_strerror(rc));
+			    } else if (debug)
+				fprintf(stderr, "%03d: verify: %s\n",
+					req->fd, "Client is verified.");
+			}
+
 			/* Common elimination of failed connection. */
 			if (failure) {
 			    close(req->fd);
@@ -757,6 +794,17 @@ main(int argc, char *argv[])
     char host[INET6_ADDRSTRLEN+1];
     char serv[16];
     char mypid[12];
+#if defined(USE_GNUTLS)
+    int explicit_keyfile = 0;
+    const char options[] = "hvsdF46jSV"
+		"r:R:f:p:n:N:i:t:c:a:u:g:l:L:m:y:b:k:e:x:C:Q:A:K:~:";
+#elif defined(USE_SSL)
+    const char options[] = "hvsdF46jS"
+		"r:R:f:p:n:N:i:t:c:a:u:g:l:L:m:y:b:k:e:x:C:P:~:";
+#else /* No crypto. */
+    const char options[] = "hvsdF46j"
+		"r:R:f:p:n:N:i:t:c:a:u:g:l:L:m:y:b:k:e:x:~:";
+#endif
 
 #ifdef USE_GNUTLS
     /* Avoid using '/dev/random' since it blocks reading. */
@@ -780,11 +828,15 @@ main(int argc, char *argv[])
     
     /* parse options */
     for (;;) {
-	if (-1 == (c = getopt(argc,argv,"hvsdF46jS"
-			      "r:R:f:p:n:N:i:t:c:a:u:g:l:L:m:y:b:k:e:x:C:P:~:")))
+	if (-1 == (c = getopt(argc,argv, options)))
 	    break;
 	switch (c) {
 	case 'h':
+#if defined(USE_GNUTLS)
+	    /* Just for record keeping. */
+	    if (! explicit_keyfile)
+		keyfile = certificate;
+#endif
 	    usage(argv[0]);
 	    break;
 	case '4':
@@ -882,10 +934,26 @@ main(int argc, char *argv[])
 	case 'C':
 	    certificate = optarg;
 	    break;
+#  if defined(USE_GNUTLS)
+	case 'V':
+	    ++verify_cert;
+	    break;
+	case 'A':
+	    cacert = optarg;
+	    break;
+	case 'K':
+	    explicit_keyfile = 1;
+	    keyfile = optarg;
+	    break;
+	case 'Q':
+	    cipherprio = optarg;
+	    break;
+#  else
 	case 'P':
 	    password = strdup(optarg);
 	    memset(optarg,'x',strlen(optarg));
 	    break;
+#  endif
 #endif
 	case 'j':
 	    no_listing = 1;
@@ -901,8 +969,14 @@ main(int argc, char *argv[])
 	syslog_init();
 
 #ifdef USE_GNUTLS
-    if (with_ssl)
+    if (with_ssl) {
+	/* Is the key file implicit? */
+	if (! explicit_keyfile)
+	    /* It was not explicitely given, so copy
+	     * the reference to the certificate file. */
+	    keyfile = certificate;
 	init_ssl();
+    }
 #endif
 
     /* bind to socket */
--- webfs-1.21+ds1/webfsd.man.debian
+++ webfs-1.21+ds1/webfsd.man
@@ -132,7 +132,30 @@ The first character in the path string m
 .TP
 .B -C path
 File to use as SSL \fBc\fPertificate. This file must be in chained PEM
-format, containing both privat RSA key and corresponding certificate.
+format, and may contain server certificate as well as RSA key, but the
+latter can equally well be a separate entity, see \fB-K\fR.
+.
+.TP
+.B -K path
+File that contains the private key, if the key is not bundled with the
+certificate.
+.
+.TP
+.B -A path
+Optional file containing CA-certificate and certificate chain.
+.
+.TP
+.B -V
+Apply a verification procedure to the client certificate and chain.
+These must, if this option is chosen, be supplied by any client.
+Each chain member must pass verification, and must in turn verify the next
+chain member. The validity time for the client certificate is checked.
+.
+.TP
+.B -Q ciphers
+Specify acceptable cipher priorities for handshake, data exchange, etc.
+The default value is \fINORMAL\fR.
+.
 .\".TP
 .\".B -P
 .\"\fBP\fPassword for accessing the SSL certificate.