minor formatting
[citadel.git] / webcit / crypto.c
index aa509887b733f9afb3a61d759460a0ddb1db2cc0..fb5f7a7140ca811be46f2a203d0bdc3b0a95aec9 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1996-2012 by the citadel.org team
+ * Copyright (c) 1996-2017 by the citadel.org team
  *
  * This program is open source software.  You can redistribute it and/or
  * modify it under the terms of the GNU General Public License, version 3.
@@ -29,6 +29,8 @@ char *ssl_cipher_list = DEFAULT_SSL_CIPHER_LIST;
 
 pthread_key_t ThreadSSL;       /* Per-thread SSL context */
 
+void ssl_lock(int mode, int n, const char *file, int line);
+
 static unsigned long id_callback(void)
 {
        return (unsigned long) pthread_self();
@@ -47,6 +49,60 @@ void shutdown_ssl(void)
         */
 }
 
+
+void generate_key(char *keyfilename)
+{
+       int ret = 0;
+       RSA *rsa = NULL;
+       BIGNUM *bne = NULL;
+       int bits = 2048;
+       unsigned long e = RSA_F4;
+       FILE *fp;
+
+       if (access(keyfilename, R_OK) == 0) {
+               return;
+       }
+
+       syslog(LOG_INFO, "crypto: generating RSA key pair");
+       // generate rsa key
+       bne = BN_new();
+       ret = BN_set_word(bne,e);
+       if (ret != 1) {
+               goto free_all;
+       }
+       rsa = RSA_new();
+       ret = RSA_generate_key_ex(rsa, bits, bne, NULL);
+       if (ret != 1) {
+               goto free_all;
+       }
+
+       // write the key file
+       fp = fopen(keyfilename, "w");
+       if (fp != NULL) {
+               chmod(file_crpt_file_key, 0600);
+               if (PEM_write_RSAPrivateKey(fp, /* the file */
+                                       rsa,    /* the key */
+                                       NULL,   /* no enc */
+                                       NULL,   /* no passphr */
+                                       0,      /* no passphr */
+                                       NULL,   /* no callbk */
+                                       NULL    /* no callbk */
+               ) != 1) {
+                       syslog(LOG_ERR, "crypto: cannot write key: %s", ERR_reason_error_string(ERR_get_error()));
+                       unlink(keyfilename);
+               }
+               fclose(fp);
+       }
+
+    // 4. free
+free_all:
+    RSA_free(rsa);
+    BN_free(bne);
+}
+
+
 /*
  * initialize ssl engine, load certs and initialize openssl internals
  */
@@ -63,9 +119,11 @@ void init_ssl(void)
        char buf[SIZ];
        int rv = 0;
 
+#ifndef OPENSSL_NO_EGD
        if (!access("/var/run/egd-pool", F_OK)) {
                RAND_egd("/var/run/egd-pool");
        }
+#endif
 
        if (!RAND_status()) {
                syslog(LOG_WARNING, "PRNG not adequately seeded, won't do SSL/TLS\n");
@@ -83,7 +141,7 @@ void init_ssl(void)
                for (a = 0; a < CRYPTO_num_locks(); a++) {
                        SSLCritters[a] = malloc(sizeof(pthread_mutex_t));
                        if (!SSLCritters[a]) {
-                               syslog(1,
+                               syslog(LOG_ERR,
                                        "citserver: can't allocate memory!!\n");
                                /** Nothing's been initialized, just die */
                                ShutDownWebcit();
@@ -130,54 +188,19 @@ void init_ssl(void)
        if (!strcasecmp(ctdlhost, "uds")) {
                sprintf(buf, "%s/keys/citadel.key", ctdlport);
                rv = symlink(buf, CTDL_KEY_PATH);
-               if (!rv) syslog(1, "%s\n", strerror(errno));
+               if (!rv) syslog(LOG_DEBUG, "%s\n", strerror(errno));
                sprintf(buf, "%s/keys/citadel.csr", ctdlport);
                rv = symlink(buf, CTDL_CSR_PATH);
-               if (!rv) syslog(1, "%s\n", strerror(errno));
+               if (!rv) syslog(LOG_DEBUG, "%s\n", strerror(errno));
                sprintf(buf, "%s/keys/citadel.cer", ctdlport);
                rv = symlink(buf, CTDL_CER_PATH);
-               if (!rv) syslog(1, "%s\n", strerror(errno));
+               if (!rv) syslog(LOG_DEBUG, "%s\n", strerror(errno));
        }
 
        /*
         * If we still don't have a private key, generate one.
         */
-       if (access(CTDL_KEY_PATH, R_OK) != 0) {
-               syslog(LOG_INFO, "Generating RSA key pair.\n");
-               rsa = RSA_generate_key(1024,    /* modulus size */
-                                       65537,  /* exponent */
-                                       NULL,   /* no callback */
-                                       NULL    /* no callback */
-               );
-               if (rsa == NULL) {
-                       syslog(LOG_WARNING, "Key generation failed: %s\n", ERR_reason_error_string(ERR_get_error()));
-               }
-               if (rsa != NULL) {
-                       fp = fopen(CTDL_KEY_PATH, "w");
-                       if (fp != NULL) {
-                               chmod(CTDL_KEY_PATH, 0600);
-                               if (PEM_write_RSAPrivateKey(fp, /* the file */
-                                                       rsa,    /* the key */
-                                                       NULL,   /* no enc */
-                                                       NULL,   /* no passphr */
-                                                       0,      /* no passphr */
-                                                       NULL,   /* no callbk */
-                                                       NULL    /* no callbk */
-                               ) != 1) {
-                                       syslog(LOG_WARNING, "Cannot write key: %s\n",
-                                               ERR_reason_error_string(ERR_get_error()));
-                                       unlink(CTDL_KEY_PATH);
-                               }
-                               fclose(fp);
-                       }
-                       else {
-                               syslog(LOG_WARNING, "Cannot write key: %s\n", CTDL_KEY_PATH);
-                               ShutDownWebcit();
-                               exit(0);
-                       }
-                       RSA_free(rsa);
-               }
-       }
+       generate_key(CTDL_KEY_PATH);
 
        /*
         * If there is no certificate file on disk, we will be generating a self-signed certificate
@@ -331,8 +354,8 @@ void init_ssl(void)
                                if (cer = X509_new(), cer != NULL) {
 
                                        ASN1_INTEGER_set(X509_get_serialNumber(cer), 0);
-                                       X509_set_issuer_name(cer, req->req_info->subject);
-                                       X509_set_subject_name(cer, req->req_info->subject);
+                                       X509_set_issuer_name(cer, X509_REQ_get_subject_name(req));
+                                       X509_set_subject_name(cer, X509_REQ_get_subject_name(req));
                                        X509_gmtime_adj(X509_get_notBefore(cer), 0);
                                        X509_gmtime_adj(X509_get_notAfter(cer),(long)60*60*24*SIGN_DAYS);
 
@@ -374,10 +397,8 @@ void init_ssl(void)
        SSL_CTX_use_certificate_chain_file(ssl_ctx, CTDL_CER_PATH);
        SSL_CTX_use_PrivateKey_file(ssl_ctx, CTDL_KEY_PATH, SSL_FILETYPE_PEM);
        if ( !SSL_CTX_check_private_key(ssl_ctx) ) {
-               syslog(LOG_WARNING, "Cannot install certificate: %s\n",
-                               ERR_reason_error_string(ERR_get_error()));
+               syslog(LOG_WARNING, "crypto: cannot install certificate: %s\n", ERR_reason_error_string(ERR_get_error()));
        }
-       
 }
 
 
@@ -442,7 +463,7 @@ int starttls(int sock) {
        else {
                syslog(LOG_INFO, "SSL_accept success\n");
        }
-       /*r = */BIO_set_close(newssl->rbio, BIO_NOCLOSE);
+       /*r = */BIO_set_close(SSL_get_rbio(newssl), BIO_NOCLOSE);
        bits = SSL_CIPHER_get_bits(SSL_get_current_cipher(newssl), &alg_bits);
        syslog(LOG_INFO, "SSL/TLS using %s on %s (%d of %d bits)\n",
                SSL_CIPHER_get_name(SSL_get_current_cipher(newssl)),
@@ -475,7 +496,7 @@ void endtls(void)
        /* I don't think this is needed, and it crashes the server anyway
         *
         *      if (ctx != NULL) {
-        *              syslog(9, "Freeing CTX at %x\n", (int)ctx );
+        *              syslog(LOG_DEBUG, "Freeing CTX at %x\n", (int)ctx );
         *              SSL_CTX_free(ctx);
         *      }
         */
@@ -501,7 +522,7 @@ void ssl_lock(int mode, int n, const char *file, int line)
 /*
  * Send binary data to the client encrypted.
  */
-void client_write_ssl(const StrBuf *Buf)
+int client_write_ssl(const StrBuf *Buf)
 {
        const char *buf;
        int retval;
@@ -509,7 +530,7 @@ void client_write_ssl(const StrBuf *Buf)
        long nbytes;
        char junk[1];
 
-       if (THREADSSL == NULL) return;
+       if (THREADSSL == NULL) return -1;
 
        nbytes = nremain = StrLength(Buf);
        buf = ChrPtr(Buf);
@@ -535,10 +556,11 @@ void client_write_ssl(const StrBuf *Buf)
                                syslog(LOG_WARNING, "errno is %d\n", errno);
                        }
                        endtls();
-                       return;
+                       return -1;
                }
                nremain -= retval;
        }
+       return 0;
 }