1 // Copyright (c) 1987-2022 by the citadel.org team
3 // This program is open source software; you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License version 3.
6 // This program is distributed in the hope that it will be useful,
7 // but WITHOUT ANY WARRANTY; without even the implied warranty of
8 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 // GNU General Public License for more details.
14 #include <sys/types.h>
18 #include <openssl/ssl.h>
19 #include <openssl/err.h>
20 #include <openssl/rand.h>
29 #ifdef HAVE_SYS_SELECT_H
30 #include <sys/select.h>
34 #include <libcitadel.h>
36 #include "serv_crypto.h"
37 #include "sysdep_decls.h"
40 #include "ctdl_module.h"
44 SSL_CTX *ssl_ctx = NULL; // This SSL context is used for all sessions.
45 char *ssl_cipher_list = CIT_CIPHERS;
47 // If a private key does not exist, generate one now.
48 void generate_key(char *keyfilename) {
53 unsigned long e = RSA_F4;
56 if (access(keyfilename, R_OK) == 0) { // Already have one.
57 syslog(LOG_INFO, "crypto: %s exists and is readable", keyfilename);
61 syslog(LOG_INFO, "crypto: generating RSA key pair");
65 ret = BN_set_word(bne,e);
71 ret = RSA_generate_key_ex(rsa, bits, bne, NULL);
77 fp = fopen(keyfilename, "w");
79 chmod(keyfilename, 0600);
80 if (PEM_write_RSAPrivateKey(fp, // the file
83 NULL, // no passphrase
88 syslog(LOG_ERR, "crypto: cannot write key: %s", ERR_reason_error_string(ERR_get_error()));
94 // free the memory we used
101 // If a certificate does not exist, generate a self-signed one now.
102 void generate_certificate(char *keyfilename, char *certfilename) {
103 RSA *private_key = NULL;
104 EVP_PKEY *public_key = NULL;
105 X509_REQ *certificate_signing_request = NULL;
106 X509_NAME *name = NULL;
107 X509 *certificate = NULL;
110 if (access(certfilename, R_OK) == 0) { // already have one.
111 syslog(LOG_INFO, "crypto: %s exists and is readable", certfilename);
115 syslog(LOG_INFO, "crypto: generating a self-signed certificate");
117 // Read in our private key.
118 fp = fopen(keyfilename, "r");
120 private_key = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
125 syslog(LOG_ERR, "crypto: cannot read the private key");
129 // Create a public key from the private key
130 public_key = EVP_PKEY_new();
132 syslog(LOG_ERR, "crypto: cannot allocate public key");
133 RSA_free(private_key);
136 EVP_PKEY_assign_RSA(public_key, private_key);
138 // Create a certificate signing request
139 certificate_signing_request = X509_REQ_new();
140 if (!certificate_signing_request) {
141 syslog(LOG_ERR, "crypto: cannot allocate certificate signing request");
142 RSA_free(private_key);
143 EVP_PKEY_free(public_key);
147 // Assign the public key to the certificate signing request
148 X509_REQ_set_pubkey(certificate_signing_request, public_key);
149 X509_REQ_set_version(certificate_signing_request, 0L);
151 // Tell it who we are
152 name = X509_REQ_get_subject_name(certificate_signing_request);
153 X509_NAME_add_entry_by_txt(name, "C", MBSTRING_ASC, (unsigned const char *)"ZZ", -1, -1, 0);
154 X509_NAME_add_entry_by_txt(name, "ST", MBSTRING_ASC, (unsigned const char *)"The World", -1, -1, 0);
155 X509_NAME_add_entry_by_txt(name, "L", MBSTRING_ASC, (unsigned const char *)"My Location", -1, -1, 0);
156 X509_NAME_add_entry_by_txt(name, "O", MBSTRING_ASC, (unsigned const char *)"Generic certificate", -1, -1, 0);
157 X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_ASC, (unsigned const char *)"Citadel server", -1, -1, 0);
158 X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, (unsigned const char *)"*", -1, -1, 0);
159 X509_REQ_set_subject_name(certificate_signing_request, name);
162 if (!X509_REQ_sign(certificate_signing_request, public_key, EVP_md5())) {
163 syslog(LOG_ERR, "crypto: X509_REQ_sign(): error");
164 X509_REQ_free(certificate_signing_request);
165 RSA_free(private_key);
166 EVP_PKEY_free(public_key);
170 // Generate the self-signed certificate
171 certificate = X509_new();
173 syslog(LOG_ERR, "crypto: cannot allocate X.509 certificate");
174 X509_REQ_free(certificate_signing_request);
175 RSA_free(private_key);
176 EVP_PKEY_free(public_key);
180 ASN1_INTEGER_set(X509_get_serialNumber(certificate), 0);
181 X509_set_issuer_name(certificate, X509_REQ_get_subject_name(certificate_signing_request));
182 X509_set_subject_name(certificate, X509_REQ_get_subject_name(certificate_signing_request));
183 X509_gmtime_adj(X509_get_notBefore(certificate), 0);
184 X509_gmtime_adj(X509_get_notAfter(certificate), (long)60*60*24*SIGN_DAYS);
185 X509_set_pubkey(certificate, public_key);
186 X509_REQ_free(certificate_signing_request); // We're done with the CSR; free it
188 // Finally, sign the certificate with our private key.
189 if (!X509_sign(certificate, public_key, EVP_md5())) {
190 syslog(LOG_ERR, "crypto: X509_sign() error");
191 X509_free(certificate);
192 RSA_free(private_key);
193 EVP_PKEY_free(public_key);
197 // Write the certificate to disk
198 fp = fopen(certfilename, "w");
200 chmod(certfilename, 0600);
201 PEM_write_X509(fp, certificate);
205 syslog(LOG_ERR, "crypto: %s: %m", certfilename);
208 X509_free(certificate);
209 EVP_PKEY_free(public_key);
210 // RSA_free(private_key); // private_key is freed by EVP_PKEY_free() above
214 // Set the private key and certificate chain for the global SSL Context.
215 // This is called during initialization, and can be called again later if the certificate changes.
216 void bind_to_key_and_certificate(void) {
218 SSL_CTX *old_ctx = NULL;
219 SSL_CTX *new_ctx = NULL;
221 const SSL_METHOD *method = SSLv23_server_method();
223 syslog(LOG_ERR, "crypto: TLS_server_method() failed: %s", ERR_reason_error_string(ERR_get_error()));
227 new_ctx = SSL_CTX_new(method);
229 syslog(LOG_ERR, "crypto: SSL_CTX_new failed: %s", ERR_reason_error_string(ERR_get_error()));
233 if (!(SSL_CTX_set_cipher_list(new_ctx, ssl_cipher_list))) {
234 syslog(LOG_ERR, "crypto: SSL_CTX_set_cipher_list failed: %s", ERR_reason_error_string(ERR_get_error()));
238 syslog(LOG_DEBUG, "crypto: using certificate chain %s", file_crpt_file_cer);
239 if (!SSL_CTX_use_certificate_chain_file(new_ctx, file_crpt_file_cer)) {
240 syslog(LOG_ERR, "crypto: SSL_CTX_use_certificate_chain_file failed: %s", ERR_reason_error_string(ERR_get_error()));
244 syslog(LOG_DEBUG, "crypto: using private key %s", file_crpt_file_key);
245 if (!SSL_CTX_use_PrivateKey_file(new_ctx, file_crpt_file_key, SSL_FILETYPE_PEM)) {
246 syslog(LOG_ERR, "crypto: SSL_CTX_use_PrivateKey_file failed: %s", ERR_reason_error_string(ERR_get_error()));
251 ssl_ctx = new_ctx; // All future binds will use the new certificate
253 if (old_ctx != NULL) {
254 sleep(1); // Hopefully wait until all in-progress binds to the old certificate have completed
255 SSL_CTX_free(old_ctx);
260 // Check the modification time of the key and certificate files. Reload if either one changed.
261 void update_key_and_cert_if_needed(void) {
262 static time_t previous_mtime = 0;
264 struct stat certstat;
266 if (stat(file_crpt_file_key, &keystat) != 0) {
267 syslog(LOG_ERR, "%s: %s", file_crpt_file_key, strerror(errno));
270 if (stat(file_crpt_file_cer, &certstat) != 0) {
271 syslog(LOG_ERR, "%s: %s", file_crpt_file_cer, strerror(errno));
275 if ((keystat.st_mtime + certstat.st_mtime) != previous_mtime) {
276 begin_critical_section(S_OPENSSL);
277 bind_to_key_and_certificate();
278 end_critical_section(S_OPENSSL);
279 previous_mtime = keystat.st_mtime + certstat.st_mtime;
284 // Initialize the TLS subsystem.
285 void init_ssl(void) {
287 // Initialize the OpenSSL library
289 SSL_load_error_strings();
291 // Load (or generate) a key and certificate
292 mkdir(ctdl_key_dir, 0700); // If the keys directory does not exist, create it
293 generate_key(file_crpt_file_key); // If a private key does not exist, create it
294 generate_certificate(file_crpt_file_key, file_crpt_file_cer); // If a certificate does not exist, create it
295 bind_to_key_and_certificate(); // Load key and cert from disk, and bind to them.
297 // Register some Citadel protocol commands for dealing with encrypted sessions
298 CtdlRegisterProtoHook(cmd_stls, "STLS", "Start TLS session");
299 CtdlRegisterProtoHook(cmd_gtls, "GTLS", "Get TLS session status");
300 CtdlRegisterSessionHook(endtls, EVT_STOP, PRIO_STOP + 10);
304 // client_write_ssl() Send binary data to the client encrypted.
305 void client_write_ssl(const char *buf, int nbytes) {
312 while (nremain > 0) {
313 if (SSL_want_write(CC->ssl)) {
314 if ((SSL_read(CC->ssl, junk, 0)) < 1) {
315 syslog(LOG_DEBUG, "crypto: SSL_read in client_write: %s", ERR_reason_error_string(ERR_get_error()));
319 SSL_write(CC->ssl, &buf[nbytes - nremain], nremain);
323 errval = SSL_get_error(CC->ssl, retval);
324 if (errval == SSL_ERROR_WANT_READ || errval == SSL_ERROR_WANT_WRITE) {
328 syslog(LOG_DEBUG, "crypto: SSL_write got error %ld, ret %d", errval, retval);
330 syslog(LOG_DEBUG, "crypto: errno is %d", errno);
333 client_write(&buf[nbytes - nremain], nremain);
341 // read data from the encrypted layer.
342 int client_read_sslbuffer(StrBuf *buf, int timeout) {
343 char sbuf[16384]; // OpenSSL communicates in 16k blocks, so let's speak its native tongue.
348 if (pssl == NULL) return(-1);
351 if (SSL_want_read(pssl)) {
352 if ((SSL_write(pssl, junk, 0)) < 1) {
353 syslog(LOG_DEBUG, "crypto: SSL_write in client_read");
356 rlen = SSL_read(pssl, sbuf, sizeof(sbuf));
360 errval = SSL_get_error(pssl, rlen);
361 if (errval == SSL_ERROR_WANT_READ || errval == SSL_ERROR_WANT_WRITE) {
365 syslog(LOG_DEBUG, "crypto: SSL_read got error %ld", errval);
369 StrBufAppendBufPlain(buf, sbuf, rlen, 0);
376 int client_readline_sslbuffer(StrBuf *Line, StrBuf *IOBuf, const char **Pos, int timeout) {
377 const char *pos = NULL;
380 int nSuccessLess = 0;
381 const char *pch = NULL;
383 if ((Line == NULL) || (Pos == NULL) || (IOBuf == NULL)) {
391 if ((StrLength(IOBuf) > 0) && (pos != NULL) && (pos < ChrPtr(IOBuf) + StrLength(IOBuf))) {
393 pch = strchr(pch, '\n');
396 StrBufAppendBufPlain(Line, pos, StrLength(IOBuf) - (pos - ChrPtr(IOBuf)), 0);
402 if ((pch > ChrPtr(IOBuf)) && (*(pch - 1) == '\r')) {
405 StrBufAppendBufPlain(Line, pos, (pch - pos - n), 0);
407 if (StrLength(IOBuf) <= (pch - ChrPtr(IOBuf) + 1)) {
414 return StrLength(Line);
419 while ((nSuccessLess < timeout) && (pLF == NULL) && (CC->ssl != NULL)) {
421 rlen = client_read_sslbuffer(IOBuf, timeout);
426 pLF = strchr(ChrPtr(IOBuf), '\n');
433 if (len > 0 && (*(pLF - 1) == '\r') )
435 StrBufAppendBufPlain(Line, pos, len, 0);
436 if (pLF + 1 >= ChrPtr(IOBuf) + StrLength(IOBuf)) {
442 return StrLength(Line);
448 int client_read_sslblob(StrBuf *Target, long bytes, int timeout) {
453 baselen = StrLength(Target);
455 if (StrLength(CC->RecvBuf.Buf) > 0) {
460 if (CC->RecvBuf.ReadWritePointer == NULL) {
461 CC->RecvBuf.ReadWritePointer = ChrPtr(CC->RecvBuf.Buf);
463 pchs = ChrPtr(CC->RecvBuf.Buf);
464 TotalLen = StrLength(CC->RecvBuf.Buf);
465 RemainLen = TotalLen - (pchs - CC->RecvBuf.ReadWritePointer);
466 if (RemainLen > bytes) {
470 StrBufAppendBufPlain(Target, CC->RecvBuf.ReadWritePointer, RemainLen, 0);
471 CC->RecvBuf.ReadWritePointer += RemainLen;
473 if ((ChrPtr(CC->RecvBuf.Buf) + StrLength(CC->RecvBuf.Buf)) <= CC->RecvBuf.ReadWritePointer) {
474 CC->RecvBuf.ReadWritePointer = NULL;
475 FlushStrBuf(CC->RecvBuf.Buf);
479 if (StrLength(Target) >= bytes + baselen) {
483 CC->RecvBuf.ReadWritePointer = NULL;
485 while ((StrLength(Target) < bytes + baselen) && (retval >= 0)) {
486 retval = client_read_sslbuffer(CC->RecvBuf.Buf, timeout);
488 RemainRead = bytes - (StrLength (Target) - baselen);
489 if (RemainRead < StrLength(CC->RecvBuf.Buf)) {
490 StrBufAppendBufPlain(
492 ChrPtr(CC->RecvBuf.Buf),
494 CC->RecvBuf.ReadWritePointer = ChrPtr(CC->RecvBuf.Buf) + RemainRead;
497 StrBufAppendBuf(Target, CC->RecvBuf.Buf, 0);
498 FlushStrBuf(CC->RecvBuf.Buf);
501 FlushStrBuf(CC->RecvBuf.Buf);
510 // CtdlStartTLS() starts TLS encryption for the current session. It
511 // must be supplied with pre-generated strings for responses of "ok," "no
512 // support for TLS," and "error" so that we can use this in any protocol.
513 void CtdlStartTLS(char *ok_response, char *nosup_response, char *error_response) {
514 int retval, bits, alg_bits;
516 if (CC->redirect_ssl) {
517 syslog(LOG_ERR, "crypto: attempt to begin SSL on an already encrypted connection");
518 if (error_response != NULL) {
519 cprintf("%s", error_response);
525 syslog(LOG_ERR, "crypto: SSL failed: context has not been initialized");
526 if (nosup_response != NULL) {
527 cprintf("%s", nosup_response);
532 update_key_and_cert_if_needed(); // did someone update the key or cert? if so, re-bind them
534 if (!(CC->ssl = SSL_new(ssl_ctx))) {
535 syslog(LOG_ERR, "crypto: SSL_new failed: %s", ERR_reason_error_string(ERR_get_error()));
536 if (error_response != NULL) {
537 cprintf("%s", error_response);
541 if (!(SSL_set_fd(CC->ssl, CC->client_socket))) {
542 syslog(LOG_ERR, "crypto: SSL_set_fd failed: %s", ERR_reason_error_string(ERR_get_error()));
545 if (error_response != NULL) {
546 cprintf("%s", error_response);
550 if (ok_response != NULL) {
551 cprintf("%s", ok_response);
553 retval = SSL_accept(CC->ssl);
555 // Can't notify the client of an error here; they will
556 // discover the problem at the SSL layer and should
557 // revert to unencrypted communications.
558 syslog(LOG_ERR, "crypto: SSL_accept failed: %s", ERR_reason_error_string(ERR_get_error()));
563 bits = SSL_CIPHER_get_bits(SSL_get_current_cipher(CC->ssl), &alg_bits);
564 syslog(LOG_INFO, "crypto: TLS using %s on %s (%d of %d bits)",
565 SSL_CIPHER_get_name(SSL_get_current_cipher(CC->ssl)),
566 SSL_CIPHER_get_version(SSL_get_current_cipher(CC->ssl)),
569 CC->redirect_ssl = 1;
573 // cmd_stls() starts TLS encryption for the current session
574 void cmd_stls(char *params) {
575 char ok_response[SIZ];
576 char nosup_response[SIZ];
577 char error_response[SIZ];
581 sprintf(ok_response, "%d Begin TLS negotiation now\n", CIT_OK);
582 sprintf(nosup_response, "%d TLS not supported here\n", ERROR + CMD_NOT_SUPPORTED);
583 sprintf(error_response, "%d TLS negotiation error\n", ERROR + INTERNAL_ERROR);
585 CtdlStartTLS(ok_response, nosup_response, error_response);
589 // cmd_gtls() returns status info about the TLS connection
590 void cmd_gtls(char *params) {
593 if (!CC->ssl || !CC->redirect_ssl) {
594 cprintf("%d Session is not encrypted.\n", ERROR);
597 bits = SSL_CIPHER_get_bits(SSL_get_current_cipher(CC->ssl), &alg_bits);
598 cprintf("%d %s|%s|%d|%d\n", CIT_OK,
599 SSL_CIPHER_get_version(SSL_get_current_cipher(CC->ssl)),
600 SSL_CIPHER_get_name(SSL_get_current_cipher(CC->ssl)),
605 // endtls() shuts down the TLS connection
608 CC->redirect_ssl = 0;
612 syslog(LOG_INFO, "crypto: ending TLS on this session");
613 SSL_shutdown(CC->ssl);
616 CC->redirect_ssl = 0;
619 #endif // HAVE_OPENSSL