Update from 1.0.0-stable
[openssl.git] / apps / s_client.c
index f68553234dc0ee8bd2e8b308d36fa9a9d789aa11..a41a915ed4d5a8e849054e104e2cc2d968e929b7 100644 (file)
@@ -205,7 +205,9 @@ static int c_showcerts=0;
 
 static void sc_usage(void);
 static void print_stuff(BIO *berr,SSL *con,int full);
+#ifndef OPENSSL_NO_TLSEXT
 static int ocsp_resp_cb(SSL *s, void *arg);
+#endif
 static BIO *bio_c_out=NULL;
 static int c_quiet=0;
 static int c_ign_eof=0;
@@ -213,7 +215,7 @@ static int c_ign_eof=0;
 #ifndef OPENSSL_NO_PSK
 /* Default PSK identity and key */
 static char *psk_identity="Client_identity";
-static char *psk_key=NULL; /* by default PSK is not used */
+/*char *psk_key=NULL;  by default PSK is not used */
 
 static unsigned int psk_client_cb(SSL *ssl, const char *hint, char *identity,
        unsigned int max_identity_len, unsigned char *psk,
@@ -235,7 +237,7 @@ static unsigned int psk_client_cb(SSL *ssl, const char *hint, char *identity,
                BIO_printf(bio_c_out, "Received PSK identity hint '%s'\n", hint);
 
        /* lookup PSK identity and PSK key based on the given identity hint here */
-       ret = BIO_snprintf(identity, max_identity_len, psk_identity);
+       ret = BIO_snprintf(identity, max_identity_len, "%s", psk_identity);
        if (ret < 0 || (unsigned int)ret > max_identity_len)
                goto out_err;
        if (c_debug)
@@ -306,15 +308,19 @@ static void sc_usage(void)
        BIO_printf(bio_err," -crlf         - convert LF from terminal into CRLF\n");
        BIO_printf(bio_err," -quiet        - no s_client output\n");
        BIO_printf(bio_err," -ign_eof      - ignore input eof (default when -quiet)\n");
+       BIO_printf(bio_err," -no_ign_eof   - don't ignore input eof\n");
 #ifndef OPENSSL_NO_PSK
        BIO_printf(bio_err," -psk_identity arg - PSK identity\n");
        BIO_printf(bio_err," -psk arg      - PSK in hex (without 0x)\n");
+# ifndef OPENSSL_NO_JPAKE
+       BIO_printf(bio_err," -jpake arg    - JPAKE secret to use\n");
+# endif
 #endif
        BIO_printf(bio_err," -ssl2         - just use SSLv2\n");
        BIO_printf(bio_err," -ssl3         - just use SSLv3\n");
        BIO_printf(bio_err," -tls1         - just use TLSv1\n");
        BIO_printf(bio_err," -dtls1        - just use DTLSv1\n");    
-       BIO_printf(bio_err," -mtu          - set the MTU\n");
+       BIO_printf(bio_err," -mtu          - set the link layer MTU\n");
        BIO_printf(bio_err," -no_tls1/-no_ssl3/-no_ssl2 - turn off that protocol\n");
        BIO_printf(bio_err," -bugs         - Switch on all SSL implementation bug workarounds\n");
        BIO_printf(bio_err," -serverpref   - Use server's cipher preferences (only SSLv2)\n");
@@ -323,7 +329,8 @@ static void sc_usage(void)
        BIO_printf(bio_err," -starttls prot - use the STARTTLS command before starting TLS\n");
        BIO_printf(bio_err,"                 for those protocols that support it, where\n");
        BIO_printf(bio_err,"                 'prot' defines which one to assume.  Currently,\n");
-       BIO_printf(bio_err,"                 only \"smtp\", \"pop3\", \"imap\", and \"ftp\" are supported.\n");
+       BIO_printf(bio_err,"                 only \"smtp\", \"pop3\", \"imap\", \"ftp\" and \"xmpp\"\n");
+       BIO_printf(bio_err,"                 are supported.\n");
 #ifndef OPENSSL_NO_ENGINE
        BIO_printf(bio_err," -engine id    - Initialise and use the specified engine\n");
 #endif
@@ -366,7 +373,8 @@ enum
        PROTO_SMTP,
        PROTO_POP3,
        PROTO_IMAP,
-       PROTO_FTP
+       PROTO_FTP,
+       PROTO_XMPP
 };
 
 int MAIN(int, char **);
@@ -375,7 +383,6 @@ int MAIN(int argc, char **argv)
        {
        int off=0;
        SSL *con=NULL;
-       X509_STORE *store = NULL;
        int s,k,width,state=0;
        char *cbuf=NULL,*sbuf=NULL,*mbuf=NULL;
        int cbuf_len,cbuf_off;
@@ -396,7 +403,9 @@ int MAIN(int argc, char **argv)
        SSL_CTX *ctx=NULL;
        int ret=1,in_init=1,i,nbio_test=0;
        int starttls_proto = PROTO_OFF;
-       int prexit = 0, vflags = 0;
+       int prexit = 0;
+       X509_VERIFY_PARAM *vpm = NULL;
+       int badarg = 0;
        const SSL_METHOD *meth=NULL;
        int socket_type=SOCK_STREAM;
        BIO *sbio;
@@ -405,15 +414,15 @@ int MAIN(int argc, char **argv)
 #ifndef OPENSSL_NO_ENGINE
        char *engine_id=NULL;
        char *ssl_client_engine_id=NULL;
-       ENGINE *e=NULL, *ssl_client_engine=NULL;
+       ENGINE *ssl_client_engine=NULL;
 #endif
+       ENGINE *e=NULL;
 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE) || defined(OPENSSL_SYS_BEOS_R5)
        struct timeval tv;
 #if defined(OPENSSL_SYS_BEOS_R5)
        int stdin_set = 0;
 #endif
 #endif
-
 #ifndef OPENSSL_NO_TLSEXT
        char *servername = NULL; 
         tlsextctx tlsextcbp = 
@@ -425,6 +434,9 @@ int MAIN(int argc, char **argv)
        int peerlen = sizeof(peer);
        int enable_timeouts = 0 ;
        long socket_mtu = 0;
+#ifndef OPENSSL_NO_JPAKE
+       char *jpake_secret = NULL;
+#endif
 
 #if !defined(OPENSSL_NO_SSL2) && !defined(OPENSSL_NO_SSL3)
        meth=SSLv23_client_method();
@@ -510,10 +522,12 @@ int MAIN(int argc, char **argv)
                        if (--argc < 1) goto bad;
                        cert_format = str2fmt(*(++argv));
                        }
-               else if (strcmp(*argv,"-crl_check") == 0)
-                       vflags |= X509_V_FLAG_CRL_CHECK;
-               else if (strcmp(*argv,"-crl_check_all") == 0)
-                       vflags |= X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL;
+               else if (args_verify(&argv, &argc, &badarg, bio_err, &vpm))
+                       {
+                       if (badarg)
+                               goto bad;
+                       continue;
+                       }
                else if (strcmp(*argv,"-verify_return_error") == 0)
                        verify_return_error = 1;
                else if (strcmp(*argv,"-prexit") == 0)
@@ -527,6 +541,8 @@ int MAIN(int argc, char **argv)
                        }
                else if (strcmp(*argv,"-ign_eof") == 0)
                        c_ign_eof=1;
+               else if (strcmp(*argv,"-no_ign_eof") == 0)
+                       c_ign_eof=0;
                else if (strcmp(*argv,"-pause") == 0)
                        c_Pause=1;
                else if (strcmp(*argv,"-debug") == 0)
@@ -662,6 +678,8 @@ int MAIN(int argc, char **argv)
                                starttls_proto = PROTO_IMAP;
                        else if (strcmp(*argv,"ftp") == 0)
                                starttls_proto = PROTO_FTP;
+                       else if (strcmp(*argv, "xmpp") == 0)
+                               starttls_proto = PROTO_XMPP;
                        else
                                goto bad;
                        }
@@ -689,6 +707,13 @@ int MAIN(int argc, char **argv)
                        servername= *(++argv);
                        /* meth=TLSv1_client_method(); */
                        }
+#endif
+#ifndef OPENSSL_NO_JPAKE
+               else if (strcmp(*argv,"-jpake") == 0)
+                       {
+                       if (--argc < 1) goto bad;
+                       jpake_secret = *++argv;
+                       }
 #endif
                else
                        {
@@ -706,6 +731,26 @@ bad:
                goto end;
                }
 
+#if !defined(OPENSSL_NO_JPAKE) && !defined(OPENSSL_NO_PSK)
+       if (jpake_secret)
+               {
+               if (psk_key)
+                       {
+                       BIO_printf(bio_err,
+                                  "Can't use JPAKE and PSK together\n");
+                       goto end;
+                       }
+               psk_identity = "JPAKE";
+               }
+
+       if (cipher)
+               {
+               BIO_printf(bio_err, "JPAKE sets cipher to PSK\n");
+               goto end;
+               }
+       cipher = "PSK";
+#endif
+
        OpenSSL_add_ssl_algorithms();
        SSL_load_error_strings();
 
@@ -789,6 +834,9 @@ bad:
                goto end;
                }
 
+       if (vpm)
+               SSL_CTX_set1_param(ctx, vpm);
+
 #ifndef OPENSSL_NO_ENGINE
        if (ssl_client_engine)
                {
@@ -804,10 +852,14 @@ bad:
 #endif
 
 #ifndef OPENSSL_NO_PSK
+#ifdef OPENSSL_NO_JPAKE
        if (psk_key != NULL)
+#else
+       if (psk_key != NULL || jpake_secret)
+#endif
                {
                if (c_debug)
-                       BIO_printf(bio_c_out, "PSK key given, setting client callback\n");
+                       BIO_printf(bio_c_out, "PSK key given or JPAKE in use, setting client callback\n");
                SSL_CTX_set_psk_client_callback(ctx, psk_client_cb);
                }
 #endif
@@ -844,8 +896,6 @@ bad:
                /* goto end; */
                }
 
-       store = SSL_CTX_get_cert_store(ctx);
-       X509_STORE_set_flags(store, vflags);
 #ifndef OPENSSL_NO_TLSEXT
        if (servername != NULL)
                {
@@ -953,10 +1003,10 @@ re_start:
                        BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT, 0, &timeout);
                        }
 
-               if (socket_mtu > 0)
+               if (socket_mtu > 28)
                        {
                        SSL_set_options(con, SSL_OP_NO_QUERY_MTU);
-                       SSL_set_mtu(con, socket_mtu);
+                       SSL_set_mtu(con, socket_mtu - 28);
                        }
                else
                        /* want to do MTU discovery */
@@ -965,8 +1015,6 @@ re_start:
        else
                sbio=BIO_new_socket(s,BIO_NOCLOSE);
 
-
-
        if (nbio_test)
                {
                BIO *test;
@@ -1010,6 +1058,10 @@ SSL_set_tlsext_status_ids(con, ids);
 #endif
                }
 #endif
+#ifndef OPENSSL_NO_JPAKE
+       if (jpake_secret)
+               jpake_client_auth(bio_c_out, sbio, jpake_secret);
+#endif
 
        SSL_set_bio(con,sbio,sbio);
        SSL_set_connect_state(con);
@@ -1116,6 +1168,28 @@ SSL_set_tlsext_status_ids(con, ids);
                BIO_printf(sbio,"AUTH TLS\r\n");
                BIO_read(sbio,sbuf,BUFSIZZ);
                }
+       if (starttls_proto == PROTO_XMPP)
+               {
+               int seen = 0;
+               BIO_printf(sbio,"<stream:stream "
+                   "xmlns:stream='http://etherx.jabber.org/streams' "
+                   "xmlns='jabber:client' to='%s' version='1.0'>", host);
+               seen = BIO_read(sbio,mbuf,BUFSIZZ);
+               mbuf[seen] = 0;
+               while (!strstr(mbuf, "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'"))
+                       {
+                       if (strstr(mbuf, "/stream:features>"))
+                               goto shut;
+                       seen = BIO_read(sbio,mbuf,BUFSIZZ);
+                       mbuf[seen] = 0;
+                       }
+               BIO_printf(sbio, "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>");
+               seen = BIO_read(sbio,sbuf,BUFSIZZ);
+               sbuf[seen] = 0;
+               if (!strstr(sbuf, "<proceed"))
+                       goto shut;
+               mbuf[0] = 0;
+               }
 
        for (;;)
                {
@@ -1529,7 +1603,7 @@ static void print_stuff(BIO *bio, SSL *s, int full)
        char buf[BUFSIZ];
        STACK_OF(X509) *sk;
        STACK_OF(X509_NAME) *sk2;
-       SSL_CIPHER *c;
+       const SSL_CIPHER *c;
        X509_NAME *xn;
        int j,i;
 #ifndef OPENSSL_NO_COMP
@@ -1653,6 +1727,8 @@ static void print_stuff(BIO *bio, SSL *s, int full)
        (void)BIO_flush(bio);
        }
 
+#ifndef OPENSSL_NO_TLSEXT
+
 static int ocsp_resp_cb(SSL *s, void *arg)
        {
        const unsigned char *p;
@@ -1678,3 +1754,5 @@ static int ocsp_resp_cb(SSL *s, void *arg)
        OCSP_RESPONSE_free(rsp);
        return 1;
        }
+
+#endif