4 * Provides HTTPS, when the OpenSSL library is available.
13 #include <sys/types.h>
18 #include "webserver.h"
20 #define CTDL_CRYPTO_DIR WEBCITDIR "/keys"
21 #define CTDL_KEY_PATH CTDL_CRYPTO_DIR "/citadel.key"
22 #define CTDL_CSR_PATH CTDL_CRYPTO_DIR "/citadel.csr"
23 #define CTDL_CER_PATH CTDL_CRYPTO_DIR "/citadel.cer"
27 /* Shared Diffie-Hellman parameters */
28 #define DH_P "1A74527AEE4EE2568E85D4FB2E65E18C9394B9C80C42507D7A6A0DBE9A9A54B05A9A96800C34C7AA5297095B69C88901EEFD127F969DCA26A54C0E0B5C5473EBAEB00957D2633ECAE3835775425DE66C0DE6D024DBB17445E06E6B0C78415E589B8814F08531D02FD43778451E7685541079CFFB79EF0D26EFEEBBB69D1E80383"
32 SSL_CTX *ssl_ctx; /* SSL context */
33 pthread_mutex_t **SSLCritters; /* Things needing locking */
35 pthread_key_t ThreadSSL; /* Per-thread SSL context */
37 static unsigned long id_callback(void)
39 return (unsigned long) pthread_self();
45 SSL_METHOD *ssl_method;
51 EVP_PKEY *req_pkey = NULL;
52 X509_NAME *name = NULL;
56 if (!access("/var/run/egd-pool", F_OK))
57 RAND_egd("/var/run/egd-pool");
61 "PRNG not adequately seeded, won't do SSL/TLS\n");
65 malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t *));
67 lprintf(1, "citserver: can't allocate memory!!\n");
68 /* Nothing's been initialized, just die */
73 for (a = 0; a < CRYPTO_num_locks(); a++) {
74 SSLCritters[a] = malloc(sizeof(pthread_mutex_t));
75 if (!SSLCritters[a]) {
77 "citserver: can't allocate memory!!\n");
78 /* Nothing's been initialized, just die */
81 pthread_mutex_init(SSLCritters[a], NULL);
86 * Initialize SSL transport layer
89 /* OpenSSL_add_all_algorithms(); */
90 SSL_load_error_strings();
91 ssl_method = SSLv2_server_method();
92 if (!(ssl_ctx = SSL_CTX_new(ssl_method))) {
93 lprintf(3, "SSL_CTX_new failed: %s\n",
94 ERR_reason_error_string(ERR_get_error()));
98 CRYPTO_set_locking_callback(ssl_lock);
99 CRYPTO_set_id_callback(id_callback);
101 /* Load DH parameters into the context */
104 lprintf(3, "init_ssl() can't allocate a DH object: %s\n",
105 ERR_reason_error_string(ERR_get_error()));
106 SSL_CTX_free(ssl_ctx);
110 if (!(BN_hex2bn(&(dh->p), DH_P))) {
111 lprintf(3, "init_ssl() can't assign DH_P: %s\n",
112 ERR_reason_error_string(ERR_get_error()));
113 SSL_CTX_free(ssl_ctx);
117 if (!(BN_hex2bn(&(dh->g), DH_G))) {
118 lprintf(3, "init_ssl() can't assign DH_G: %s\n",
119 ERR_reason_error_string(ERR_get_error()));
120 SSL_CTX_free(ssl_ctx);
125 SSL_CTX_set_tmp_dh(ssl_ctx, dh);
128 /* Get our certificates in order.
129 * First, create the key/cert directory if it's not there already...
131 mkdir(CTDL_CRYPTO_DIR, 0700);
134 * Before attempting to generate keys/certificates, first try
135 * link to them from the Citadel server if it's on the same host.
136 * We ignore any error return because it either meant that there
137 * was nothing in Citadel to link from (in which case we just
138 * generate new files) or the target files already exist (which
139 * is not fatal either).
141 if (!strcasecmp(ctdlhost, "uds")) {
142 sprintf(buf, "%s/keys/citadel.key", ctdlport);
143 symlink(buf, CTDL_KEY_PATH);
144 sprintf(buf, "%s/keys/citadel.csr", ctdlport);
145 symlink(buf, CTDL_CSR_PATH);
146 sprintf(buf, "%s/keys/citadel.cer", ctdlport);
147 symlink(buf, CTDL_CER_PATH);
151 * If we still don't have a private key, generate one.
153 if (access(CTDL_KEY_PATH, R_OK) != 0) {
154 lprintf(5, "Generating RSA key pair.\n");
155 rsa = RSA_generate_key(1024, /* modulus size */
156 65537, /* exponent */
157 NULL, /* no callback */
158 NULL); /* no callback */
160 lprintf(3, "Key generation failed: %s\n",
161 ERR_reason_error_string(ERR_get_error()));
164 fp = fopen(CTDL_KEY_PATH, "w");
166 chmod(CTDL_KEY_PATH, 0600);
167 if (PEM_write_RSAPrivateKey(fp, /* the file */
170 NULL, /* no passphr */
172 NULL, /* no callbk */
175 lprintf(3, "Cannot write key: %s\n",
176 ERR_reason_error_string(ERR_get_error()));
177 unlink(CTDL_KEY_PATH);
186 * Generate a CSR if we don't have one.
188 if (access(CTDL_CSR_PATH, R_OK) != 0) {
189 lprintf(5, "Generating a certificate signing request.\n");
192 * Read our key from the file. No, we don't just keep this
193 * in memory from the above key-generation function, because
194 * there is the possibility that the key was already on disk
195 * and we didn't just generate it now.
197 fp = fopen(CTDL_KEY_PATH, "r");
199 rsa = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
205 /* Create a public key from the private key */
206 if (pk=EVP_PKEY_new(), pk != NULL) {
207 EVP_PKEY_assign_RSA(pk, rsa);
208 if (req = X509_REQ_new(), req != NULL) {
210 /* Set the public key */
211 X509_REQ_set_pubkey(req, pk);
212 X509_REQ_set_version(req, 0L);
214 name = X509_REQ_get_subject_name(req);
216 /* Tell it who we are */
219 X509_NAME_add_entry_by_txt(name, "C",
220 MBSTRING_ASC, "US", -1, -1, 0);
222 X509_NAME_add_entry_by_txt(name, "ST",
223 MBSTRING_ASC, "New York", -1, -1, 0);
225 X509_NAME_add_entry_by_txt(name, "L",
226 MBSTRING_ASC, "Mount Kisco", -1, -1, 0);
229 X509_NAME_add_entry_by_txt(name, "O",
230 MBSTRING_ASC, "FIXME.FIXME.org", -1, -1, 0);
232 X509_NAME_add_entry_by_txt(name, "OU",
233 MBSTRING_ASC, "Citadel server", -1, -1, 0);
235 X509_NAME_add_entry_by_txt(name, "CN",
236 MBSTRING_ASC, "FIXME.FIXME.org", -1, -1, 0);
238 X509_REQ_set_subject_name(req, name);
241 if (!X509_REQ_sign(req, pk, EVP_md5())) {
242 lprintf(3, "X509_REQ_sign(): error\n");
245 /* Write it to disk. */
246 fp = fopen(CTDL_CSR_PATH, "w");
248 chmod(CTDL_CSR_PATH, 0600);
249 PEM_write_X509_REQ(fp, req);
262 lprintf(3, "Unable to read private key.\n");
269 * Generate a self-signed certificate if we don't have one.
271 if (access(CTDL_CER_PATH, R_OK) != 0) {
272 lprintf(5, "Generating a self-signed certificate.\n");
274 /* Same deal as before: always read the key from disk because
275 * it may or may not have just been generated.
277 fp = fopen(CTDL_KEY_PATH, "r");
279 rsa = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
283 /* This also holds true for the CSR. */
288 if (pk=EVP_PKEY_new(), pk != NULL) {
289 EVP_PKEY_assign_RSA(pk, rsa);
292 fp = fopen(CTDL_CSR_PATH, "r");
294 req = PEM_read_X509_REQ(fp, NULL, NULL, NULL);
299 if (cer = X509_new(), cer != NULL) {
301 ASN1_INTEGER_set(X509_get_serialNumber(cer), 0);
302 X509_set_issuer_name(cer, req->req_info->subject);
303 X509_set_subject_name(cer, req->req_info->subject);
304 X509_gmtime_adj(X509_get_notBefore(cer), 0);
305 X509_gmtime_adj(X509_get_notAfter(cer),(long)60*60*24*SIGN_DAYS);
307 req_pkey = X509_REQ_get_pubkey(req);
308 X509_set_pubkey(cer, req_pkey);
309 EVP_PKEY_free(req_pkey);
312 if (!X509_sign(cer, pk, EVP_md5())) {
313 lprintf(3, "X509_sign(): error\n");
316 /* Write it to disk. */
317 fp = fopen(CTDL_CER_PATH, "w");
319 chmod(CTDL_CER_PATH, 0600);
320 PEM_write_X509(fp, cer);
334 * Now try to bind to the key and certificate.
336 SSL_CTX_use_certificate_file(ssl_ctx, CTDL_CER_PATH, SSL_FILETYPE_PEM);
337 SSL_CTX_use_PrivateKey_file(ssl_ctx, CTDL_KEY_PATH, SSL_FILETYPE_PEM);
338 if ( !SSL_CTX_check_private_key(ssl_ctx) ) {
339 lprintf(3, "Cannot install certificate: %s\n",
340 ERR_reason_error_string(ERR_get_error()));
347 * starttls() starts SSL/TLS encryption for the current session.
349 int starttls(int sock) {
350 int retval, bits, alg_bits;
353 pthread_setspecific(ThreadSSL, NULL);
358 if (!(newssl = SSL_new(ssl_ctx))) {
359 lprintf(3, "SSL_new failed: %s\n",
360 ERR_reason_error_string(ERR_get_error()));
363 if (!(SSL_set_fd(newssl, sock))) {
364 lprintf(3, "SSL_set_fd failed: %s\n",
365 ERR_reason_error_string(ERR_get_error()));
369 retval = SSL_accept(newssl);
372 * Can't notify the client of an error here; they will
373 * discover the problem at the SSL layer and should
374 * revert to unencrypted communications.
378 errval = SSL_get_error(newssl, retval);
379 lprintf(3, "SSL_accept failed: %s\n",
380 ERR_reason_error_string(ERR_get_error()));
385 BIO_set_close(newssl->rbio, BIO_NOCLOSE);
387 SSL_CIPHER_get_bits(SSL_get_current_cipher(newssl),
389 lprintf(5, "SSL/TLS using %s on %s (%d of %d bits)\n",
390 SSL_CIPHER_get_name(SSL_get_current_cipher(newssl)),
391 SSL_CIPHER_get_version(SSL_get_current_cipher(newssl)),
394 pthread_setspecific(ThreadSSL, newssl);
401 * endtls() shuts down the TLS connection
403 * WARNING: This may make your session vulnerable to a known plaintext
404 * attack in the current implmentation.
408 lprintf(5, "Ending SSL/TLS\n");
409 SSL_shutdown(THREADSSL);
411 pthread_setspecific(ThreadSSL, NULL);
416 * ssl_lock() callback for OpenSSL mutex locks
418 void ssl_lock(int mode, int n, const char *file, int line)
420 if (mode & CRYPTO_LOCK)
421 pthread_mutex_lock(SSLCritters[n]);
423 pthread_mutex_unlock(SSLCritters[n]);
427 * client_write_ssl() Send binary data to the client encrypted.
429 void client_write_ssl(char *buf, int nbytes)
437 while (nremain > 0) {
438 if (SSL_want_write(THREADSSL)) {
439 if ((SSL_read(THREADSSL, junk, 0)) < 1) {
440 lprintf(9, "SSL_read in client_write: %s\n", ERR_reason_error_string(ERR_get_error()));
444 SSL_write(THREADSSL, &buf[nbytes - nremain], nremain);
448 errval = SSL_get_error(THREADSSL, retval);
449 if (errval == SSL_ERROR_WANT_READ ||
450 errval == SSL_ERROR_WANT_WRITE) {
454 lprintf(9, "SSL_write got error %ld, ret %d\n", errval, retval);
456 lprintf(9, "errno is %d\n", errno);
466 * client_read_ssl() - read data from the encrypted layer.
468 int client_read_ssl(char *buf, int bytes, int timeout)
480 while (len < bytes) {
483 * This code is disabled because we don't need it when
484 * using blocking reads (which we are). -IO
487 s = BIO_get_fd(THREADSSL->rbio, NULL);
492 retval = select(s + 1, &rfds, NULL, NULL, &tv);
494 if (FD_ISSET(s, &rfds) == 0) {
499 if (SSL_want_read(THREADSSL)) {
500 if ((SSL_write(THREADSSL, junk, 0)) < 1) {
501 lprintf(9, "SSL_write in client_read: %s\n", ERR_reason_error_string(ERR_get_error()));
504 rlen = SSL_read(THREADSSL, &buf[len], bytes - len);
508 errval = SSL_get_error(THREADSSL, rlen);
509 if (errval == SSL_ERROR_WANT_READ ||
510 errval == SSL_ERROR_WANT_WRITE) {
514 lprintf(9, "SSL_read got error %ld\n", errval);
524 #endif /* HAVE_OPENSSL */