10 #include <sys/types.h>
15 #include "webserver.h"
17 #define CTDL_CRYPTO_DIR WEBCITDIR "/keys"
18 #define CTDL_KEY_PATH CTDL_CRYPTO_DIR "/citadel.key"
19 #define CTDL_CSR_PATH CTDL_CRYPTO_DIR "/citadel.csr"
20 #define CTDL_CER_PATH CTDL_CRYPTO_DIR "/citadel.cer"
24 /* Shared Diffie-Hellman parameters */
25 #define DH_P "1A74527AEE4EE2568E85D4FB2E65E18C9394B9C80C42507D7A6A0DBE9A9A54B05A9A96800C34C7AA5297095B69C88901EEFD127F969DCA26A54C0E0B5C5473EBAEB00957D2633ECAE3835775425DE66C0DE6D024DBB17445E06E6B0C78415E589B8814F08531D02FD43778451E7685541079CFFB79EF0D26EFEEBBB69D1E80383"
29 SSL_CTX *ssl_ctx; /* SSL context */
30 pthread_mutex_t **SSLCritters; /* Things needing locking */
32 pthread_key_t ThreadSSL; /* Per-thread SSL context */
34 static unsigned long id_callback(void)
36 return (unsigned long) pthread_self();
42 SSL_METHOD *ssl_method;
48 EVP_PKEY *req_pkey = NULL;
49 X509_NAME *name = NULL;
52 if (!access("/var/run/egd-pool", F_OK))
53 RAND_egd("/var/run/egd-pool");
57 "PRNG not adequately seeded, won't do SSL/TLS\n");
61 malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t *));
63 lprintf(1, "citserver: can't allocate memory!!\n");
64 /* Nothing's been initialized, just die */
69 for (a = 0; a < CRYPTO_num_locks(); a++) {
70 SSLCritters[a] = malloc(sizeof(pthread_mutex_t));
71 if (!SSLCritters[a]) {
73 "citserver: can't allocate memory!!\n");
74 /* Nothing's been initialized, just die */
77 pthread_mutex_init(SSLCritters[a], NULL);
82 * Initialize SSL transport layer
85 OpenSSL_add_all_algorithms();
86 SSL_load_error_strings();
87 ssl_method = SSLv2_server_method();
88 if (!(ssl_ctx = SSL_CTX_new(ssl_method))) {
89 lprintf(3, "SSL_CTX_new failed: %s\n",
90 ERR_reason_error_string(ERR_get_error()));
94 CRYPTO_set_locking_callback(ssl_lock);
95 CRYPTO_set_id_callback(id_callback);
97 /* Load DH parameters into the context */
100 lprintf(3, "init_ssl() can't allocate a DH object: %s\n",
101 ERR_reason_error_string(ERR_get_error()));
102 SSL_CTX_free(ssl_ctx);
106 if (!(BN_hex2bn(&(dh->p), DH_P))) {
107 lprintf(3, "init_ssl() can't assign DH_P: %s\n",
108 ERR_reason_error_string(ERR_get_error()));
109 SSL_CTX_free(ssl_ctx);
113 if (!(BN_hex2bn(&(dh->g), DH_G))) {
114 lprintf(3, "init_ssl() can't assign DH_G: %s\n",
115 ERR_reason_error_string(ERR_get_error()));
116 SSL_CTX_free(ssl_ctx);
121 SSL_CTX_set_tmp_dh(ssl_ctx, dh);
124 /* Get our certificates in order.
125 * First, create the key/cert directory if it's not there already...
127 mkdir(CTDL_CRYPTO_DIR, 0700);
130 * Generate a key pair if we don't have one.
132 if (access(CTDL_KEY_PATH, R_OK) != 0) {
133 lprintf(5, "Generating RSA key pair.\n");
134 rsa = RSA_generate_key(1024, /* modulus size */
135 65537, /* exponent */
136 NULL, /* no callback */
137 NULL); /* no callback */
139 lprintf(3, "Key generation failed: %s\n",
140 ERR_reason_error_string(ERR_get_error()));
143 fp = fopen(CTDL_KEY_PATH, "w");
145 chmod(CTDL_KEY_PATH, 0600);
146 if (PEM_write_RSAPrivateKey(fp, /* the file */
149 NULL, /* no passphr */
151 NULL, /* no callbk */
154 lprintf(3, "Cannot write key: %s\n",
155 ERR_reason_error_string(ERR_get_error()));
156 unlink(CTDL_KEY_PATH);
165 * Generate a CSR if we don't have one.
167 if (access(CTDL_CSR_PATH, R_OK) != 0) {
168 lprintf(5, "Generating a certificate signing request.\n");
171 * Read our key from the file. No, we don't just keep this
172 * in memory from the above key-generation function, because
173 * there is the possibility that the key was already on disk
174 * and we didn't just generate it now.
176 fp = fopen(CTDL_KEY_PATH, "r");
178 rsa = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
184 /* Create a public key from the private key */
185 if (pk=EVP_PKEY_new(), pk != NULL) {
186 EVP_PKEY_assign_RSA(pk, rsa);
187 if (req = X509_REQ_new(), req != NULL) {
189 /* Set the public key */
190 X509_REQ_set_pubkey(req, pk);
191 X509_REQ_set_version(req, 0L);
193 name = X509_REQ_get_subject_name(req);
195 /* Tell it who we are */
198 X509_NAME_add_entry_by_txt(name, "C",
199 MBSTRING_ASC, "US", -1, -1, 0);
201 X509_NAME_add_entry_by_txt(name, "ST",
202 MBSTRING_ASC, "New York", -1, -1, 0);
204 X509_NAME_add_entry_by_txt(name, "L",
205 MBSTRING_ASC, "Mount Kisco", -1, -1, 0);
208 X509_NAME_add_entry_by_txt(name, "O",
209 MBSTRING_ASC, "FIXME.FIXME.org", -1, -1, 0);
211 X509_NAME_add_entry_by_txt(name, "OU",
212 MBSTRING_ASC, "Citadel server", -1, -1, 0);
214 X509_NAME_add_entry_by_txt(name, "CN",
215 MBSTRING_ASC, "FIXME.FIXME.org", -1, -1, 0);
217 X509_REQ_set_subject_name(req, name);
220 if (!X509_REQ_sign(req, pk, EVP_md5())) {
221 lprintf(3, "X509_REQ_sign(): error\n");
224 /* Write it to disk. */
225 fp = fopen(CTDL_CSR_PATH, "w");
227 chmod(CTDL_CSR_PATH, 0600);
228 PEM_write_X509_REQ(fp, req);
241 lprintf(3, "Unable to read private key.\n");
248 * Generate a self-signed certificate if we don't have one.
250 if (access(CTDL_CER_PATH, R_OK) != 0) {
251 lprintf(5, "Generating a self-signed certificate.\n");
253 /* Same deal as before: always read the key from disk because
254 * it may or may not have just been generated.
256 fp = fopen(CTDL_KEY_PATH, "r");
258 rsa = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
262 /* This also holds true for the CSR. */
267 if (pk=EVP_PKEY_new(), pk != NULL) {
268 EVP_PKEY_assign_RSA(pk, rsa);
271 fp = fopen(CTDL_CSR_PATH, "r");
273 req = PEM_read_X509_REQ(fp, NULL, NULL, NULL);
278 if (cer = X509_new(), cer != NULL) {
280 X509_set_issuer_name(cer, req->req_info->subject);
281 X509_set_subject_name(cer, req->req_info->subject);
282 X509_gmtime_adj(X509_get_notBefore(cer),0);
283 X509_gmtime_adj(X509_get_notAfter(cer),(long)60*60*24*SIGN_DAYS);
284 req_pkey = X509_REQ_get_pubkey(req);
285 X509_set_pubkey(cer, req_pkey);
286 EVP_PKEY_free(req_pkey);
289 if (!X509_sign(cer, pk, EVP_md5())) {
290 lprintf(3, "X509_sign(): error\n");
293 /* Write it to disk. */
294 fp = fopen(CTDL_CER_PATH, "w");
296 chmod(CTDL_CER_PATH, 0600);
297 PEM_write_X509(fp, cer);
311 * Now try to bind to the key and certificate.
313 SSL_CTX_use_certificate_file(ssl_ctx, CTDL_CER_PATH, SSL_FILETYPE_PEM);
314 SSL_CTX_use_PrivateKey_file(ssl_ctx, CTDL_KEY_PATH, SSL_FILETYPE_PEM);
315 if ( !SSL_CTX_check_private_key(ssl_ctx) ) {
316 lprintf(3, "Cannot install certificate: %s\n",
317 ERR_reason_error_string(ERR_get_error()));
324 * starttls() starts SSL/TLS encryption for the current session.
326 int starttls(int sock) {
327 int retval, bits, alg_bits;
330 pthread_setspecific(ThreadSSL, NULL);
335 if (!(newssl = SSL_new(ssl_ctx))) {
336 lprintf(3, "SSL_new failed: %s\n",
337 ERR_reason_error_string(ERR_get_error()));
340 if (!(SSL_set_fd(newssl, sock))) {
341 lprintf(3, "SSL_set_fd failed: %s\n",
342 ERR_reason_error_string(ERR_get_error()));
346 retval = SSL_accept(newssl);
349 * Can't notify the client of an error here; they will
350 * discover the problem at the SSL layer and should
351 * revert to unencrypted communications.
355 errval = SSL_get_error(newssl, retval);
356 lprintf(3, "SSL_accept failed: %s\n",
357 ERR_reason_error_string(ERR_get_error()));
362 BIO_set_close(newssl->rbio, BIO_NOCLOSE);
364 SSL_CIPHER_get_bits(SSL_get_current_cipher(newssl),
366 lprintf(5, "SSL/TLS using %s on %s (%d of %d bits)\n",
367 SSL_CIPHER_get_name(SSL_get_current_cipher(newssl)),
368 SSL_CIPHER_get_version(SSL_get_current_cipher(newssl)),
371 pthread_setspecific(ThreadSSL, newssl);
378 * endtls() shuts down the TLS connection
380 * WARNING: This may make your session vulnerable to a known plaintext
381 * attack in the current implmentation.
385 lprintf(5, "Ending SSL/TLS\n");
386 SSL_shutdown(THREADSSL);
388 pthread_setspecific(ThreadSSL, NULL);
393 * ssl_lock() callback for OpenSSL mutex locks
395 void ssl_lock(int mode, int n, const char *file, int line)
397 if (mode & CRYPTO_LOCK)
398 pthread_mutex_lock(SSLCritters[n]);
400 pthread_mutex_unlock(SSLCritters[n]);
404 * client_write_ssl() Send binary data to the client encrypted.
406 void client_write_ssl(char *buf, int nbytes)
414 while (nremain > 0) {
415 if (SSL_want_write(THREADSSL)) {
416 if ((SSL_read(THREADSSL, junk, 0)) < 1) {
417 lprintf(9, "SSL_read in client_write: %s\n", ERR_reason_error_string(ERR_get_error()));
421 SSL_write(THREADSSL, &buf[nbytes - nremain], nremain);
425 errval = SSL_get_error(THREADSSL, retval);
426 if (errval == SSL_ERROR_WANT_READ ||
427 errval == SSL_ERROR_WANT_WRITE) {
431 lprintf(9, "SSL_write got error %ld, ret %d\n", errval, retval);
433 lprintf(9, "errno is %d\n", errno);
435 client_write(&buf[nbytes - nremain], nremain);
444 * client_read_ssl() - read data from the encrypted layer.
446 int client_read_ssl(char *buf, int bytes, int timeout)
458 while (len < bytes) {
461 * This code is disabled because we don't need it when
462 * using blocking reads (which we are). -IO
465 s = BIO_get_fd(THREADSSL->rbio, NULL);
470 retval = select(s + 1, &rfds, NULL, NULL, &tv);
472 if (FD_ISSET(s, &rfds) == 0) {
477 if (SSL_want_read(THREADSSL)) {
478 if ((SSL_write(THREADSSL, junk, 0)) < 1) {
479 lprintf(9, "SSL_write in client_read: %s\n", ERR_reason_error_string(ERR_get_error()));
482 rlen = SSL_read(THREADSSL, &buf[len], bytes - len);
486 errval = SSL_get_error(THREADSSL, rlen);
487 if (errval == SSL_ERROR_WANT_READ ||
488 errval == SSL_ERROR_WANT_WRITE) {
492 lprintf(9, "SSL_read got error %ld\n", errval);
494 return (client_read_to
495 (WC->http_sock, &buf[len], bytes - len, timeout));
503 #endif /* HAVE_OPENSSL */