2 // Functions in this module handle SSL encryption when WebCit is running
5 // Copyright (c) 1996-2021 by the citadel.org team
7 // This program is open source software. It runs great on the
8 // Linux operating system (and probably elsewhere). You can use,
9 // copy, and run it under the terms of the GNU General Public
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
19 SSL_CTX *ssl_ctx; // SSL context
20 char *ssl_cipher_list = DEFAULT_SSL_CIPHER_LIST;
23 // Generate a private key for SSL
24 void generate_key(char *keyfilename) {
29 unsigned long e = RSA_F4;
32 if (access(keyfilename, R_OK) == 0) {
36 syslog(LOG_INFO, "crypto: generating RSA key pair");
40 ret = BN_set_word(bne, e);
46 ret = RSA_generate_key_ex(rsa, bits, bne, NULL);
51 fp = fopen(keyfilename, "w");
53 chmod(keyfilename, 0600);
54 if (PEM_write_RSAPrivateKey(fp, // the file */
57 NULL, // no passphrase
62 syslog(LOG_ERR, "crypto: cannot write key: %s", ERR_reason_error_string(ERR_get_error()));
74 // Initialize ssl engine, load certs and initialize openssl internals
76 const SSL_METHOD *ssl_method;
81 EVP_PKEY *req_pkey = NULL;
82 X509_NAME *name = NULL;
87 // Initialize SSL transport layer
89 SSL_load_error_strings();
90 ssl_method = SSLv23_server_method();
91 if (!(ssl_ctx = SSL_CTX_new(ssl_method))) {
92 syslog(LOG_WARNING, "SSL_CTX_new failed: %s", ERR_reason_error_string(ERR_get_error()));
96 syslog(LOG_INFO, "Requesting cipher list: %s", ssl_cipher_list);
97 if (!(SSL_CTX_set_cipher_list(ssl_ctx, ssl_cipher_list))) {
98 syslog(LOG_WARNING, "SSL_CTX_set_cipher_list failed: %s", ERR_reason_error_string(ERR_get_error()));
102 // Get our certificates in order.
103 // First, create the key/cert directory if it's not there already...
104 mkdir(CTDL_CRYPTO_DIR, 0700);
106 // If we still don't have a private key, generate one.
107 generate_key(CTDL_KEY_PATH);
109 // If there is no certificate file on disk, we will be generating a self-signed certificate
110 // in the next step. Therefore, if we have neither a CSR nor a certificate, generate
111 // the CSR in this step so that the next step may commence.
112 if ((access(CTDL_CER_PATH, R_OK) != 0) && (access(CTDL_CSR_PATH, R_OK) != 0)) {
113 syslog(LOG_INFO, "Generating a certificate signing request.");
115 // Read our key from the file. No, we don't just keep this
116 // in memory from the above key-generation function, because
117 // there is the possibility that the key was already on disk
118 // and we didn't just generate it now.
119 fp = fopen(CTDL_KEY_PATH, "r");
121 rsa = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
126 // Create a public key from the private key
127 if (pk = EVP_PKEY_new(), pk != NULL) {
128 EVP_PKEY_assign_RSA(pk, rsa);
129 if (req = X509_REQ_new(), req != NULL) {
131 // Set the public key
132 X509_REQ_set_pubkey(req, pk);
133 X509_REQ_set_version(req, 0L);
134 name = X509_REQ_get_subject_name(req);
135 X509_NAME_add_entry_by_txt(name, "O", MBSTRING_ASC, (unsigned char *) "Citadel Server", -1, -1, 0);
136 X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_ASC, (unsigned char *) "Default Certificate PLEASE CHANGE", -1, -1, 0);
137 X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, (unsigned char *) "*", -1, -1, 0);
138 X509_REQ_set_subject_name(req, name);
141 if (!X509_REQ_sign(req, pk, EVP_md5())) {
142 syslog(LOG_WARNING, "X509_REQ_sign(): error");
146 fp = fopen(CTDL_CSR_PATH, "w");
148 chmod(CTDL_CSR_PATH, 0600);
149 PEM_write_X509_REQ(fp, req);
153 syslog(LOG_WARNING, "Cannot write key: %s", CTDL_CSR_PATH);
163 syslog(LOG_WARNING, "Unable to read private key.");
167 // Generate a self-signed certificate if we don't have one.
168 if (access(CTDL_CER_PATH, R_OK) != 0) {
169 syslog(LOG_INFO, "Generating a self-signed certificate.");
171 // Same deal as before: always read the key from disk because
172 // it may or may not have just been generated.
173 fp = fopen(CTDL_KEY_PATH, "r");
175 rsa = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
179 // This also holds true for the CSR
184 if (pk = EVP_PKEY_new(), pk != NULL) {
185 EVP_PKEY_assign_RSA(pk, rsa);
188 fp = fopen(CTDL_CSR_PATH, "r");
190 req = PEM_read_X509_REQ(fp, NULL, NULL, NULL);
195 if (cer = X509_new(), cer != NULL) {
196 ASN1_INTEGER_set(X509_get_serialNumber(cer), 0);
197 X509_set_issuer_name(cer, X509_REQ_get_subject_name(req));
198 X509_set_subject_name(cer, X509_REQ_get_subject_name(req));
199 X509_gmtime_adj(X509_get_notBefore(cer), 0);
200 X509_gmtime_adj(X509_get_notAfter(cer), (long) 60 * 60 * 24 * SIGN_DAYS);
201 req_pkey = X509_REQ_get_pubkey(req);
202 X509_set_pubkey(cer, req_pkey);
203 EVP_PKEY_free(req_pkey);
206 if (!X509_sign(cer, pk, EVP_md5())) {
207 syslog(LOG_WARNING, "X509_sign(): error");
209 else { // Write it to disk
210 fp = fopen(CTDL_CER_PATH, "w");
212 chmod(CTDL_CER_PATH, 0600);
213 PEM_write_X509(fp, cer);
217 syslog(LOG_WARNING, "Cannot write key: %s", CTDL_CER_PATH);
228 // Now try to bind to the key and certificate.
229 // Note that we use SSL_CTX_use_certificate_chain_file() which allows
230 // the certificate file to contain intermediate certificates.
231 SSL_CTX_use_certificate_chain_file(ssl_ctx, CTDL_CER_PATH);
232 SSL_CTX_use_PrivateKey_file(ssl_ctx, CTDL_KEY_PATH, SSL_FILETYPE_PEM);
233 if (!SSL_CTX_check_private_key(ssl_ctx)) {
234 syslog(LOG_WARNING, "Cannot install certificate: %s", ERR_reason_error_string(ERR_get_error()));
240 // starts SSL/TLS encryption for the current session.
241 void starttls(struct client_handle *ch) {
242 int retval, bits, alg_bits;
247 if (!(ch->ssl_handle = SSL_new(ssl_ctx))) {
248 syslog(LOG_WARNING, "SSL_new failed: %s", ERR_reason_error_string(ERR_get_error()));
251 if (!(SSL_set_fd(ch->ssl_handle, ch->sock))) {
252 syslog(LOG_WARNING, "SSL_set_fd failed: %s", ERR_reason_error_string(ERR_get_error()));
253 SSL_free(ch->ssl_handle);
256 retval = SSL_accept(ch->ssl_handle);
259 const char *ssl_error_reason = NULL;
261 errval = SSL_get_error(ch->ssl_handle, retval);
262 ssl_error_reason = ERR_reason_error_string(ERR_get_error());
263 if (ssl_error_reason == NULL) {
264 syslog(LOG_WARNING, "SSL_accept failed: errval=%ld, retval=%d %s", errval, retval, strerror(errval));
267 syslog(LOG_WARNING, "SSL_accept failed: %s\n", ssl_error_reason);
270 retval = SSL_accept(ch->ssl_handle);
274 const char *ssl_error_reason = NULL;
276 errval = SSL_get_error(ch->ssl_handle, retval);
277 ssl_error_reason = ERR_reason_error_string(ERR_get_error());
278 if (ssl_error_reason == NULL) {
279 syslog(LOG_WARNING, "SSL_accept failed: errval=%ld, retval=%d (%s)", errval, retval, strerror(errval));
282 syslog(LOG_WARNING, "SSL_accept failed: %s", ssl_error_reason);
284 SSL_free(ch->ssl_handle);
285 ch->ssl_handle = NULL;
289 syslog(LOG_INFO, "SSL_accept success");
291 bits = SSL_CIPHER_get_bits(SSL_get_current_cipher(ch->ssl_handle), &alg_bits);
292 syslog(LOG_INFO, "SSL/TLS using %s on %s (%d of %d bits)",
293 SSL_CIPHER_get_name(SSL_get_current_cipher(ch->ssl_handle)),
294 SSL_CIPHER_get_version(SSL_get_current_cipher(ch->ssl_handle)), bits, alg_bits);
296 syslog(LOG_INFO, "SSL started");
300 // shuts down the TLS connection
301 void endtls(struct client_handle *ch) {
302 syslog(LOG_INFO, "Ending SSL/TLS");
303 if (ch->ssl_handle != NULL) {
304 SSL_shutdown(ch->ssl_handle);
305 SSL_get_SSL_CTX(ch->ssl_handle);
306 SSL_free(ch->ssl_handle);
308 ch->ssl_handle = NULL;
312 // Send binary data to the client encrypted.
313 int client_write_ssl(struct client_handle *ch, char *buf, int nbytes) {
318 if (ch->ssl_handle == NULL)
322 while (nremain > 0) {
323 if (SSL_want_write(ch->ssl_handle)) {
324 if ((SSL_read(ch->ssl_handle, junk, 0)) < 1) {
325 syslog(LOG_WARNING, "SSL_read in client_write: %s", ERR_reason_error_string(ERR_get_error()));
328 retval = SSL_write(ch->ssl_handle, &buf[nbytes - nremain], nremain);
332 errval = SSL_get_error(ch->ssl_handle, retval);
333 if (errval == SSL_ERROR_WANT_READ || errval == SSL_ERROR_WANT_WRITE) {
337 syslog(LOG_WARNING, "SSL_write got error %ld, ret %d", errval, retval);
339 syslog(LOG_WARNING, "errno is %d", errno);
350 // read data from the encrypted layer
351 int client_read_ssl(struct client_handle *ch, char *buf, int nbytes) {
356 if (ch->ssl_handle == NULL)
359 while (bytes_read < nbytes) {
360 if (SSL_want_read(ch->ssl_handle)) {
361 if ((SSL_write(ch->ssl_handle, junk, 0)) < 1) {
362 syslog(LOG_WARNING, "SSL_write in client_read");
365 rlen = SSL_read(ch->ssl_handle, &buf[bytes_read], nbytes - bytes_read);
368 errval = SSL_get_error(ch->ssl_handle, rlen);
369 if (errval == SSL_ERROR_WANT_READ || errval == SSL_ERROR_WANT_WRITE) {
373 syslog(LOG_WARNING, "SSL_read error %ld", errval);