New option "-crlf" to s_client and s_server which tells them to convert
[openssl.git] / apps / s_server.c
index c82c0f33d83e420fbed0e9a54c59afe5865a65e8..a78a4bcd7b02dbb5191a4e69ac3cc7c4dcf3a89d 100644 (file)
  * [including the GNU Public Licence.]
  */
 
  * [including the GNU Public Licence.]
  */
 
-/* With IPv6, it looks like Digital has mixed up the proper order of
-   recursive header file inclusion, resulting in the compiler complaining
-   that u_int isn't defined, but only if _POSIX_C_SOURCE is defined, which
-   is needed to have fileno() declared correctly...  So let's define u_int */
-#if defined(VMS) && defined(__DECC) && !defined(__U_INT)
-#define __U_INT
-typedef unsigned int u_int;
+#ifdef APPS_CRLF
+# include <assert.h>
 #endif
 #endif
-
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -73,6 +67,16 @@ typedef unsigned int u_int;
 #ifdef NO_STDIO
 #define APPS_WIN16
 #endif
 #ifdef NO_STDIO
 #define APPS_WIN16
 #endif
+
+/* With IPv6, it looks like Digital has mixed up the proper order of
+   recursive header file inclusion, resulting in the compiler complaining
+   that u_int isn't defined, but only if _POSIX_C_SOURCE is defined, which
+   is needed to have fileno() declared correctly...  So let's define u_int */
+#if defined(VMS) && defined(__DECC) && !defined(__U_INT)
+#define __U_INT
+typedef unsigned int u_int;
+#endif
+
 #include <openssl/lhash.h>
 #include <openssl/bn.h>
 #define USE_SOCKETS
 #include <openssl/lhash.h>
 #include <openssl/bn.h>
 #define USE_SOCKETS
@@ -165,6 +169,9 @@ static char *s_dcert_file=NULL,*s_dkey_file=NULL;
 static int s_nbio=0;
 #endif
 static int s_nbio_test=0;
 static int s_nbio=0;
 #endif
 static int s_nbio_test=0;
+#ifdef APPS_CRLF
+       int s_crlf=0;
+#endif
 static SSL_CTX *ctx=NULL;
 static int www=0;
 
 static SSL_CTX *ctx=NULL;
 static int www=0;
 
@@ -212,6 +219,9 @@ static void sv_usage(void)
        BIO_printf(bio_err," -nbio         - Run with non-blocking IO\n");
 #endif
        BIO_printf(bio_err," -nbio_test    - test with the non-blocking test bio\n");
        BIO_printf(bio_err," -nbio         - Run with non-blocking IO\n");
 #endif
        BIO_printf(bio_err," -nbio_test    - test with the non-blocking test bio\n");
+#ifdef APPS_CRLF
+       BIO_printf(bio_err," -crlf         - convert LF from terminal into CRLF\n");
+#endif
        BIO_printf(bio_err," -debug        - Print more output\n");
        BIO_printf(bio_err," -state        - Print the SSL states\n");
        BIO_printf(bio_err," -CApath arg   - PEM format directory of CA's\n");
        BIO_printf(bio_err," -debug        - Print more output\n");
        BIO_printf(bio_err," -state        - Print the SSL states\n");
        BIO_printf(bio_err," -CApath arg   - PEM format directory of CA's\n");
@@ -226,6 +236,9 @@ static void sv_usage(void)
        BIO_printf(bio_err," -no_ssl2      - Just disable SSLv2\n");
        BIO_printf(bio_err," -no_ssl3      - Just disable SSLv3\n");
        BIO_printf(bio_err," -no_tls1      - Just disable TLSv1\n");
        BIO_printf(bio_err," -no_ssl2      - Just disable SSLv2\n");
        BIO_printf(bio_err," -no_ssl3      - Just disable SSLv3\n");
        BIO_printf(bio_err," -no_tls1      - Just disable TLSv1\n");
+#ifndef NO_DH
+       BIO_printf(bio_err," -no_dhe       - Disable ephemeral DH\n");
+#endif
        BIO_printf(bio_err," -bugs         - Turn on SSL bug compatability\n");
        BIO_printf(bio_err," -www          - Respond to a 'GET /' with a status page\n");
        BIO_printf(bio_err," -WWW          - Respond to a 'GET /<path> HTTP/1.0' with file ./<path>\n");
        BIO_printf(bio_err," -bugs         - Turn on SSL bug compatability\n");
        BIO_printf(bio_err," -www          - Respond to a 'GET /' with a status page\n");
        BIO_printf(bio_err," -WWW          - Respond to a 'GET /<path> HTTP/1.0' with file ./<path>\n");
@@ -393,7 +406,7 @@ int MAIN(int argc, char *argv[])
        int badop=0,bugs=0;
        int ret=1;
        int off=0;
        int badop=0,bugs=0;
        int ret=1;
        int off=0;
-       int no_tmp_rsa=0,nocert=0;
+       int no_tmp_rsa=0,no_dhe=0,nocert=0;
        int state=0;
        SSL_METHOD *meth=NULL;
 #ifndef NO_DH
        int state=0;
        SSL_METHOD *meth=NULL;
 #ifndef NO_DH
@@ -512,12 +525,18 @@ int MAIN(int argc, char *argv[])
                        { hack=1; }
                else if (strcmp(*argv,"-state") == 0)
                        { state=1; }
                        { hack=1; }
                else if (strcmp(*argv,"-state") == 0)
                        { state=1; }
+#ifdef APPS_CRLF
+               else if (strcmp(*argv,"-crlf") == 0)
+                       { s_crlf=1; }
+#endif
                else if (strcmp(*argv,"-quiet") == 0)
                        { s_quiet=1; }
                else if (strcmp(*argv,"-bugs") == 0)
                        { bugs=1; }
                else if (strcmp(*argv,"-no_tmp_rsa") == 0)
                        { no_tmp_rsa=1; }
                else if (strcmp(*argv,"-quiet") == 0)
                        { s_quiet=1; }
                else if (strcmp(*argv,"-bugs") == 0)
                        { bugs=1; }
                else if (strcmp(*argv,"-no_tmp_rsa") == 0)
                        { no_tmp_rsa=1; }
+               else if (strcmp(*argv,"-no_dhe") == 0)
+                       { no_dhe=1; }
                else if (strcmp(*argv,"-www") == 0)
                        { www=1; }
                else if (strcmp(*argv,"-WWW") == 0)
                else if (strcmp(*argv,"-www") == 0)
                        { www=1; }
                else if (strcmp(*argv,"-WWW") == 0)
@@ -620,21 +639,24 @@ bad:
                }
 
 #ifndef NO_DH
                }
 
 #ifndef NO_DH
-       /* EAY EAY EAY evil hack */
-       dh=load_dh_param();
-       if (dh != NULL)
-               {
-               BIO_printf(bio_s_out,"Setting temp DH parameters\n");
-               }
-       else
+       if (!no_dhe)
                {
                {
-               BIO_printf(bio_s_out,"Using default temp DH parameters\n");
-               dh=get_dh512();
-               }
-       (void)BIO_flush(bio_s_out);
+               /* EAY EAY EAY evil hack */
+               dh=load_dh_param();
+               if (dh != NULL)
+                       {
+                       BIO_printf(bio_s_out,"Setting temp DH parameters\n");
+                       }
+               else
+                       {
+                       BIO_printf(bio_s_out,"Using default temp DH parameters\n");
+                       dh=get_dh512();
+                       }
+               (void)BIO_flush(bio_s_out);
 
 
-       SSL_CTX_set_tmp_dh(ctx,dh);
-       DH_free(dh);
+               SSL_CTX_set_tmp_dh(ctx,dh);
+               DH_free(dh);
+               }
 #endif
        
        if (!set_cert_stuff(ctx,s_cert_file,s_key_file))
 #endif
        
        if (!set_cert_stuff(ctx,s_cert_file,s_key_file))
@@ -791,7 +813,32 @@ static int sv_body(char *hostname, int s, unsigned char *context)
                if (i <= 0) continue;
                if (FD_ISSET(fileno(stdin),&readfds))
                        {
                if (i <= 0) continue;
                if (FD_ISSET(fileno(stdin),&readfds))
                        {
-                       i=read(fileno(stdin),buf,bufsize);
+#ifdef APPS_CRLF
+                       if (s_crlf)
+                               {
+                               int j, lf_num;
+
+                               i=read(fileno(stdin), buf, bufsize/2);
+                               lf_num = 0;
+                               /* both loops are skipped when i <= 0 */
+                               for (j = 0; j < i; j++)
+                                       if (buf[j] == '\n')
+                                               lf_num++;
+                               for (j = i-1; j >= 0; j--)
+                                       {
+                                       buf[j+lf_num] = buf[j];
+                                       if (buf[j] == '\n')
+                                               {
+                                               lf_num--;
+                                               i++;
+                                               buf[j+lf_num] = '\r';
+                                               }
+                                       }
+                               assert(lf_num == 0);
+                               }
+                       else
+#endif
+                               i=read(fileno(stdin),buf,bufsize);
                        if (!s_quiet)
                                {
                                if ((i <= 0) || (buf[0] == 'Q'))
                        if (!s_quiet)
                                {
                                if ((i <= 0) || (buf[0] == 'Q'))
@@ -1019,7 +1066,7 @@ static DH *load_dh_param(void)
 
        if ((bio=BIO_new_file(DH_PARAM,"r")) == NULL)
                goto err;
 
        if ((bio=BIO_new_file(DH_PARAM,"r")) == NULL)
                goto err;
-       ret=PEM_read_bio_DHparams(bio,NULL,NULL);
+       ret=PEM_read_bio_DHparams(bio,NULL,NULL,NULL);
 err:
        if (bio != NULL) BIO_free(bio);
        return(ret);
 err:
        if (bio != NULL) BIO_free(bio);
        return(ret);