5 * \defgroup https Provides HTTPS, when the OpenSSL library is available.
6 * \ingroup WebcitHttpServer
13 #include "webserver.h"
15 /** where to find the keys */
16 #define CTDL_CRYPTO_DIR "./keys"
17 #define CTDL_KEY_PATH CTDL_CRYPTO_DIR "/citadel.key" /**< the key */
18 #define CTDL_CSR_PATH CTDL_CRYPTO_DIR "/citadel.csr" /**< the csr file */
19 #define CTDL_CER_PATH CTDL_CRYPTO_DIR "/citadel.cer" /**< the cer file */
20 #define SIGN_DAYS 365 /**< how long our certificate should live */
22 SSL_CTX *ssl_ctx; /**< SSL context */
23 pthread_mutex_t **SSLCritters; /**< Things needing locking */
25 pthread_key_t ThreadSSL; /**< Per-thread SSL context */
29 * \return thread id???
31 static unsigned long id_callback(void)
33 return (unsigned long) pthread_self();
37 * \brief initialize ssl engine
38 * load certs and initialize openssl internals
42 SSL_METHOD *ssl_method;
47 EVP_PKEY *req_pkey = NULL;
48 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 SSL_load_error_strings();
86 ssl_method = SSLv23_server_method();
87 if (!(ssl_ctx = SSL_CTX_new(ssl_method))) {
88 lprintf(3, "SSL_CTX_new failed: %s\n",
89 ERR_reason_error_string(ERR_get_error()));
93 CRYPTO_set_locking_callback(ssl_lock);
94 CRYPTO_set_id_callback(id_callback);
97 * Get our certificates in order. \todo dirify. this is a setup job.
98 * First, create the key/cert directory if it's not there already...
100 mkdir(CTDL_CRYPTO_DIR, 0700);
103 * Before attempting to generate keys/certificates, first try
104 * link to them from the Citadel server if it's on the same host.
105 * We ignore any error return because it either meant that there
106 * was nothing in Citadel to link from (in which case we just
107 * generate new files) or the target files already exist (which
108 * is not fatal either). \todo dirify
110 if (!strcasecmp(ctdlhost, "uds")) {
111 sprintf(buf, "%s/keys/citadel.key", ctdlport);
112 symlink(buf, CTDL_KEY_PATH);
113 sprintf(buf, "%s/keys/citadel.csr", ctdlport);
114 symlink(buf, CTDL_CSR_PATH);
115 sprintf(buf, "%s/keys/citadel.cer", ctdlport);
116 symlink(buf, CTDL_CER_PATH);
120 * If we still don't have a private key, generate one.
122 if (access(CTDL_KEY_PATH, R_OK) != 0) {
123 lprintf(5, "Generating RSA key pair.\n");
124 rsa = RSA_generate_key(1024, /**< modulus size */
125 65537, /**< exponent */
126 NULL, /**< no callback */
127 NULL); /**< no callback */
129 lprintf(3, "Key generation failed: %s\n",
130 ERR_reason_error_string(ERR_get_error()));
133 fp = fopen(CTDL_KEY_PATH, "w");
135 chmod(CTDL_KEY_PATH, 0600);
136 if (PEM_write_RSAPrivateKey(fp, /**< the file */
139 NULL, /**< no passphr */
140 0, /**< no passphr */
141 NULL, /**< no callbk */
142 NULL /**< no callbk */
144 lprintf(3, "Cannot write key: %s\n",
145 ERR_reason_error_string(ERR_get_error()));
146 unlink(CTDL_KEY_PATH);
155 * Generate a CSR if we don't have one.
157 if (access(CTDL_CSR_PATH, R_OK) != 0) {
158 lprintf(5, "Generating a certificate signing request.\n");
161 * Read our key from the file. No, we don't just keep this
162 * in memory from the above key-generation function, because
163 * there is the possibility that the key was already on disk
164 * and we didn't just generate it now.
166 fp = fopen(CTDL_KEY_PATH, "r");
168 rsa = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
174 /** Create a public key from the private key */
175 if (pk=EVP_PKEY_new(), pk != NULL) {
176 EVP_PKEY_assign_RSA(pk, rsa);
177 if (req = X509_REQ_new(), req != NULL) {
179 /** Set the public key */
180 X509_REQ_set_pubkey(req, pk);
181 X509_REQ_set_version(req, 0L);
183 name = X509_REQ_get_subject_name(req);
185 /** Tell it who we are */
188 X509_NAME_add_entry_by_txt(name, "C",
189 MBSTRING_ASC, "US", -1, -1, 0);
191 X509_NAME_add_entry_by_txt(name, "ST",
192 MBSTRING_ASC, "New York", -1, -1, 0);
194 X509_NAME_add_entry_by_txt(name, "L",
195 MBSTRING_ASC, "Mount Kisco", -1, -1, 0);
198 X509_NAME_add_entry_by_txt(name, "O",
199 MBSTRING_ASC, "FIXME.FIXME.org", -1, -1, 0);
201 X509_NAME_add_entry_by_txt(name, "OU",
202 MBSTRING_ASC, "Citadel server", -1, -1, 0);
204 X509_NAME_add_entry_by_txt(name, "CN",
205 MBSTRING_ASC, "FIXME.FIXME.org", -1, -1, 0);
207 X509_REQ_set_subject_name(req, name);
210 if (!X509_REQ_sign(req, pk, EVP_md5())) {
211 lprintf(3, "X509_REQ_sign(): error\n");
214 /** Write it to disk. */
215 fp = fopen(CTDL_CSR_PATH, "w");
217 chmod(CTDL_CSR_PATH, 0600);
218 PEM_write_X509_REQ(fp, req);
231 lprintf(3, "Unable to read private key.\n");
238 * Generate a self-signed certificate if we don't have one.
240 if (access(CTDL_CER_PATH, R_OK) != 0) {
241 lprintf(5, "Generating a self-signed certificate.\n");
243 /** Same deal as before: always read the key from disk because
244 * it may or may not have just been generated.
246 fp = fopen(CTDL_KEY_PATH, "r");
248 rsa = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
252 /** This also holds true for the CSR. */
257 if (pk=EVP_PKEY_new(), pk != NULL) {
258 EVP_PKEY_assign_RSA(pk, rsa);
261 fp = fopen(CTDL_CSR_PATH, "r");
263 req = PEM_read_X509_REQ(fp, NULL, NULL, NULL);
268 if (cer = X509_new(), cer != NULL) {
270 ASN1_INTEGER_set(X509_get_serialNumber(cer), 0);
271 X509_set_issuer_name(cer, req->req_info->subject);
272 X509_set_subject_name(cer, req->req_info->subject);
273 X509_gmtime_adj(X509_get_notBefore(cer), 0);
274 X509_gmtime_adj(X509_get_notAfter(cer),(long)60*60*24*SIGN_DAYS);
276 req_pkey = X509_REQ_get_pubkey(req);
277 X509_set_pubkey(cer, req_pkey);
278 EVP_PKEY_free(req_pkey);
281 if (!X509_sign(cer, pk, EVP_md5())) {
282 lprintf(3, "X509_sign(): error\n");
285 /** Write it to disk. */
286 fp = fopen(CTDL_CER_PATH, "w");
288 chmod(CTDL_CER_PATH, 0600);
289 PEM_write_X509(fp, cer);
302 * Now try to bind to the key and certificate.
303 * Note that we use SSL_CTX_use_certificate_chain_file() which allows
304 * the certificate file to contain intermediate certificates.
306 SSL_CTX_use_certificate_chain_file(ssl_ctx, CTDL_CER_PATH);
307 SSL_CTX_use_PrivateKey_file(ssl_ctx, CTDL_KEY_PATH, SSL_FILETYPE_PEM);
308 if ( !SSL_CTX_check_private_key(ssl_ctx) ) {
309 lprintf(3, "Cannot install certificate: %s\n",
310 ERR_reason_error_string(ERR_get_error()));
317 * \brief starts SSL/TLS encryption for the current session.
318 * \param sock the socket connection
321 int starttls(int sock) {
322 int retval, bits, alg_bits;
325 pthread_setspecific(ThreadSSL, NULL);
330 if (!(newssl = SSL_new(ssl_ctx))) {
331 lprintf(3, "SSL_new failed: %s\n",
332 ERR_reason_error_string(ERR_get_error()));
335 if (!(SSL_set_fd(newssl, sock))) {
336 lprintf(3, "SSL_set_fd failed: %s\n",
337 ERR_reason_error_string(ERR_get_error()));
341 retval = SSL_accept(newssl);
344 * Can't notify the client of an error here; they will
345 * discover the problem at the SSL layer and should
346 * revert to unencrypted communications.
350 errval = SSL_get_error(newssl, retval);
351 lprintf(3, "SSL_accept failed: %s\n",
352 ERR_reason_error_string(ERR_get_error()));
357 BIO_set_close(newssl->rbio, BIO_NOCLOSE);
359 SSL_CIPHER_get_bits(SSL_get_current_cipher(newssl),
361 lprintf(5, "SSL/TLS using %s on %s (%d of %d bits)\n",
362 SSL_CIPHER_get_name(SSL_get_current_cipher(newssl)),
363 SSL_CIPHER_get_version(SSL_get_current_cipher(newssl)),
366 pthread_setspecific(ThreadSSL, newssl);
373 * \brief shuts down the TLS connection
375 * WARNING: This may make your session vulnerable to a known plaintext
376 * attack in the current implmentation.
380 if (THREADSSL == NULL) return;
382 lprintf(5, "Ending SSL/TLS\n");
383 SSL_shutdown(THREADSSL);
385 pthread_setspecific(ThreadSSL, NULL);
390 * \brief callback for OpenSSL mutex locks
391 * \param mode which mode??????
392 * \param n how many???
393 * \param file which filename ???
394 * \param line what line????
396 void ssl_lock(int mode, int n, const char *file, int line)
398 if (mode & CRYPTO_LOCK)
399 pthread_mutex_lock(SSLCritters[n]);
401 pthread_mutex_unlock(SSLCritters[n]);
405 * \brief Send binary data to the client encrypted.
406 * \param buf chars to send to the client
407 * \param nbytes how many chars
409 void client_write_ssl(char *buf, int nbytes)
415 if (THREADSSL == NULL) return;
419 while (nremain > 0) {
420 if (SSL_want_write(THREADSSL)) {
421 if ((SSL_read(THREADSSL, junk, 0)) < 1) {
422 lprintf(9, "SSL_read in client_write: %s\n",
423 ERR_reason_error_string(ERR_get_error()));
426 retval = SSL_write(THREADSSL, &buf[nbytes - nremain], nremain);
430 errval = SSL_get_error(THREADSSL, retval);
431 if (errval == SSL_ERROR_WANT_READ ||
432 errval == SSL_ERROR_WANT_WRITE) {
436 lprintf(9, "SSL_write got error %ld, ret %d\n", errval, retval);
438 lprintf(9, "errno is %d\n", errno);
449 * \brief read data from the encrypted layer.
450 * \param buf charbuffer to read to
451 * \param bytes how many
452 * \param timeout how long should we wait?
455 int client_read_ssl(char *buf, int bytes, int timeout)
466 if (THREADSSL == NULL) return(0);
469 while (len < bytes) {
472 * This code is disabled because we don't need it when
473 * using blocking reads (which we are). -IO
476 s = BIO_get_fd(THREADSSL->rbio, NULL);
481 retval = select(s + 1, &rfds, NULL, NULL, &tv);
483 if (FD_ISSET(s, &rfds) == 0) {
488 if (SSL_want_read(THREADSSL)) {
489 if ((SSL_write(THREADSSL, junk, 0)) < 1) {
490 lprintf(9, "SSL_write in client_read: %s\n", ERR_reason_error_string(ERR_get_error()));
493 rlen = SSL_read(THREADSSL, &buf[len], bytes - len);
497 errval = SSL_get_error(THREADSSL, rlen);
498 if (errval == SSL_ERROR_WANT_READ ||
499 errval == SSL_ERROR_WANT_WRITE) {
503 lprintf(9, "SSL_read got error %ld\n", errval);
513 #endif /* HAVE_OPENSSL */