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;
53 if (!access("/var/run/egd-pool", F_OK))
54 RAND_egd("/var/run/egd-pool");
58 "PRNG not adequately seeded, won't do SSL/TLS\n");
62 malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t *));
64 lprintf(1, "citserver: can't allocate memory!!\n");
65 /* Nothing's been initialized, just die */
70 for (a = 0; a < CRYPTO_num_locks(); a++) {
71 SSLCritters[a] = malloc(sizeof(pthread_mutex_t));
72 if (!SSLCritters[a]) {
74 "citserver: can't allocate memory!!\n");
75 /* Nothing's been initialized, just die */
78 pthread_mutex_init(SSLCritters[a], NULL);
83 * Initialize SSL transport layer
86 OpenSSL_add_all_algorithms();
87 SSL_load_error_strings();
88 ssl_method = SSLv2_server_method();
89 if (!(ssl_ctx = SSL_CTX_new(ssl_method))) {
90 lprintf(3, "SSL_CTX_new failed: %s\n",
91 ERR_reason_error_string(ERR_get_error()));
95 CRYPTO_set_locking_callback(ssl_lock);
96 CRYPTO_set_id_callback(id_callback);
98 /* Load DH parameters into the context */
101 lprintf(3, "init_ssl() can't allocate a DH object: %s\n",
102 ERR_reason_error_string(ERR_get_error()));
103 SSL_CTX_free(ssl_ctx);
107 if (!(BN_hex2bn(&(dh->p), DH_P))) {
108 lprintf(3, "init_ssl() can't assign DH_P: %s\n",
109 ERR_reason_error_string(ERR_get_error()));
110 SSL_CTX_free(ssl_ctx);
114 if (!(BN_hex2bn(&(dh->g), DH_G))) {
115 lprintf(3, "init_ssl() can't assign DH_G: %s\n",
116 ERR_reason_error_string(ERR_get_error()));
117 SSL_CTX_free(ssl_ctx);
122 SSL_CTX_set_tmp_dh(ssl_ctx, dh);
125 /* Get our certificates in order.
126 * First, create the key/cert directory if it's not there already...
128 mkdir(CTDL_CRYPTO_DIR, 0700);
131 * Before attempting to generate keys/certificates, first try
132 * link to them from the Citadel server if it's on the same host.
133 * We ignore any error return because it either meant that there
134 * was nothing in Citadel to link from (in which case we just
135 * generate new files) or the target files already exist (which
136 * is not fatal either).
138 if (!strcasecmp(ctdlhost, "uds")) {
139 sprintf(buf, "%s/keys/citadel.key", ctdlport);
140 symlink(buf, CTDL_KEY_PATH);
141 sprintf(buf, "%s/keys/citadel.csr", ctdlport);
142 symlink(buf, CTDL_CSR_PATH);
143 sprintf(buf, "%s/keys/citadel.cer", ctdlport);
144 symlink(buf, CTDL_CER_PATH);
148 * If we still don't have a private key, generate one.
150 if (access(CTDL_KEY_PATH, R_OK) != 0) {
151 lprintf(5, "Generating RSA key pair.\n");
152 rsa = RSA_generate_key(1024, /* modulus size */
153 65537, /* exponent */
154 NULL, /* no callback */
155 NULL); /* no callback */
157 lprintf(3, "Key generation failed: %s\n",
158 ERR_reason_error_string(ERR_get_error()));
161 fp = fopen(CTDL_KEY_PATH, "w");
163 chmod(CTDL_KEY_PATH, 0600);
164 if (PEM_write_RSAPrivateKey(fp, /* the file */
167 NULL, /* no passphr */
169 NULL, /* no callbk */
172 lprintf(3, "Cannot write key: %s\n",
173 ERR_reason_error_string(ERR_get_error()));
174 unlink(CTDL_KEY_PATH);
183 * Generate a CSR if we don't have one.
185 if (access(CTDL_CSR_PATH, R_OK) != 0) {
186 lprintf(5, "Generating a certificate signing request.\n");
189 * Read our key from the file. No, we don't just keep this
190 * in memory from the above key-generation function, because
191 * there is the possibility that the key was already on disk
192 * and we didn't just generate it now.
194 fp = fopen(CTDL_KEY_PATH, "r");
196 rsa = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
202 /* Create a public key from the private key */
203 if (pk=EVP_PKEY_new(), pk != NULL) {
204 EVP_PKEY_assign_RSA(pk, rsa);
205 if (req = X509_REQ_new(), req != NULL) {
207 /* Set the public key */
208 X509_REQ_set_pubkey(req, pk);
209 X509_REQ_set_version(req, 0L);
211 name = X509_REQ_get_subject_name(req);
213 /* Tell it who we are */
216 X509_NAME_add_entry_by_txt(name, "C",
217 MBSTRING_ASC, "US", -1, -1, 0);
219 X509_NAME_add_entry_by_txt(name, "ST",
220 MBSTRING_ASC, "New York", -1, -1, 0);
222 X509_NAME_add_entry_by_txt(name, "L",
223 MBSTRING_ASC, "Mount Kisco", -1, -1, 0);
226 X509_NAME_add_entry_by_txt(name, "O",
227 MBSTRING_ASC, "FIXME.FIXME.org", -1, -1, 0);
229 X509_NAME_add_entry_by_txt(name, "OU",
230 MBSTRING_ASC, "Citadel server", -1, -1, 0);
232 X509_NAME_add_entry_by_txt(name, "CN",
233 MBSTRING_ASC, "FIXME.FIXME.org", -1, -1, 0);
235 X509_REQ_set_subject_name(req, name);
238 if (!X509_REQ_sign(req, pk, EVP_md5())) {
239 lprintf(3, "X509_REQ_sign(): error\n");
242 /* Write it to disk. */
243 fp = fopen(CTDL_CSR_PATH, "w");
245 chmod(CTDL_CSR_PATH, 0600);
246 PEM_write_X509_REQ(fp, req);
259 lprintf(3, "Unable to read private key.\n");
266 * Generate a self-signed certificate if we don't have one.
268 if (access(CTDL_CER_PATH, R_OK) != 0) {
269 lprintf(5, "Generating a self-signed certificate.\n");
271 /* Same deal as before: always read the key from disk because
272 * it may or may not have just been generated.
274 fp = fopen(CTDL_KEY_PATH, "r");
276 rsa = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
280 /* This also holds true for the CSR. */
285 if (pk=EVP_PKEY_new(), pk != NULL) {
286 EVP_PKEY_assign_RSA(pk, rsa);
289 fp = fopen(CTDL_CSR_PATH, "r");
291 req = PEM_read_X509_REQ(fp, NULL, NULL, NULL);
296 if (cer = X509_new(), cer != NULL) {
298 ASN1_INTEGER_set(X509_get_serialNumber(cer), 0);
299 X509_set_issuer_name(cer, req->req_info->subject);
300 X509_set_subject_name(cer, req->req_info->subject);
301 X509_gmtime_adj(X509_get_notBefore(cer), 0);
302 X509_gmtime_adj(X509_get_notAfter(cer),(long)60*60*24*SIGN_DAYS);
304 req_pkey = X509_REQ_get_pubkey(req);
305 X509_set_pubkey(cer, req_pkey);
306 EVP_PKEY_free(req_pkey);
309 if (!X509_sign(cer, pk, EVP_md5())) {
310 lprintf(3, "X509_sign(): error\n");
313 /* Write it to disk. */
314 fp = fopen(CTDL_CER_PATH, "w");
316 chmod(CTDL_CER_PATH, 0600);
317 PEM_write_X509(fp, cer);
331 * Now try to bind to the key and certificate.
333 SSL_CTX_use_certificate_file(ssl_ctx, CTDL_CER_PATH, SSL_FILETYPE_PEM);
334 SSL_CTX_use_PrivateKey_file(ssl_ctx, CTDL_KEY_PATH, SSL_FILETYPE_PEM);
335 if ( !SSL_CTX_check_private_key(ssl_ctx) ) {
336 lprintf(3, "Cannot install certificate: %s\n",
337 ERR_reason_error_string(ERR_get_error()));
344 * starttls() starts SSL/TLS encryption for the current session.
346 int starttls(int sock) {
347 int retval, bits, alg_bits;
350 pthread_setspecific(ThreadSSL, NULL);
355 if (!(newssl = SSL_new(ssl_ctx))) {
356 lprintf(3, "SSL_new failed: %s\n",
357 ERR_reason_error_string(ERR_get_error()));
360 if (!(SSL_set_fd(newssl, sock))) {
361 lprintf(3, "SSL_set_fd failed: %s\n",
362 ERR_reason_error_string(ERR_get_error()));
366 retval = SSL_accept(newssl);
369 * Can't notify the client of an error here; they will
370 * discover the problem at the SSL layer and should
371 * revert to unencrypted communications.
375 errval = SSL_get_error(newssl, retval);
376 lprintf(3, "SSL_accept failed: %s\n",
377 ERR_reason_error_string(ERR_get_error()));
382 BIO_set_close(newssl->rbio, BIO_NOCLOSE);
384 SSL_CIPHER_get_bits(SSL_get_current_cipher(newssl),
386 lprintf(5, "SSL/TLS using %s on %s (%d of %d bits)\n",
387 SSL_CIPHER_get_name(SSL_get_current_cipher(newssl)),
388 SSL_CIPHER_get_version(SSL_get_current_cipher(newssl)),
391 pthread_setspecific(ThreadSSL, newssl);
398 * endtls() shuts down the TLS connection
400 * WARNING: This may make your session vulnerable to a known plaintext
401 * attack in the current implmentation.
405 lprintf(5, "Ending SSL/TLS\n");
406 SSL_shutdown(THREADSSL);
408 pthread_setspecific(ThreadSSL, NULL);
413 * ssl_lock() callback for OpenSSL mutex locks
415 void ssl_lock(int mode, int n, const char *file, int line)
417 if (mode & CRYPTO_LOCK)
418 pthread_mutex_lock(SSLCritters[n]);
420 pthread_mutex_unlock(SSLCritters[n]);
424 * client_write_ssl() Send binary data to the client encrypted.
426 void client_write_ssl(char *buf, int nbytes)
434 while (nremain > 0) {
435 if (SSL_want_write(THREADSSL)) {
436 if ((SSL_read(THREADSSL, junk, 0)) < 1) {
437 lprintf(9, "SSL_read in client_write: %s\n", ERR_reason_error_string(ERR_get_error()));
441 SSL_write(THREADSSL, &buf[nbytes - nremain], nremain);
445 errval = SSL_get_error(THREADSSL, retval);
446 if (errval == SSL_ERROR_WANT_READ ||
447 errval == SSL_ERROR_WANT_WRITE) {
451 lprintf(9, "SSL_write got error %ld, ret %d\n", errval, retval);
453 lprintf(9, "errno is %d\n", errno);
455 client_write(&buf[nbytes - nremain], nremain);
464 * client_read_ssl() - read data from the encrypted layer.
466 int client_read_ssl(char *buf, int bytes, int timeout)
478 while (len < bytes) {
481 * This code is disabled because we don't need it when
482 * using blocking reads (which we are). -IO
485 s = BIO_get_fd(THREADSSL->rbio, NULL);
490 retval = select(s + 1, &rfds, NULL, NULL, &tv);
492 if (FD_ISSET(s, &rfds) == 0) {
497 if (SSL_want_read(THREADSSL)) {
498 if ((SSL_write(THREADSSL, junk, 0)) < 1) {
499 lprintf(9, "SSL_write in client_read: %s\n", ERR_reason_error_string(ERR_get_error()));
502 rlen = SSL_read(THREADSSL, &buf[len], bytes - len);
506 errval = SSL_get_error(THREADSSL, rlen);
507 if (errval == SSL_ERROR_WANT_READ ||
508 errval == SSL_ERROR_WANT_WRITE) {
512 lprintf(9, "SSL_read got error %ld\n", errval);
514 return (client_read_to
515 (WC->http_sock, &buf[len], bytes - len, timeout));
523 #endif /* HAVE_OPENSSL */