Blame SOURCES/openssl-1.0.2k-starttls.patch

e62613
diff -up openssl-1.0.2k/apps/apps.c.starttls openssl-1.0.2k/apps/apps.c
e62613
--- openssl-1.0.2k/apps/apps.c.starttls	2017-01-26 14:22:03.000000000 +0100
e62613
+++ openssl-1.0.2k/apps/apps.c	2017-03-09 17:35:35.519765927 +0100
e62613
@@ -3277,3 +3277,11 @@ int raw_write_stdout(const void *buf, in
e62613
     return write(fileno_stdout(), buf, siz);
e62613
 }
e62613
 #endif
e62613
+
e62613
+void make_uppercase(char *string)
e62613
+{
e62613
+    int i;
e62613
+
e62613
+    for (i = 0; string[i] != '\0'; i++)
e62613
+        string[i] = toupper((unsigned char)string[i]);
e62613
+}
e62613
diff -up openssl-1.0.2k/apps/apps.h.starttls openssl-1.0.2k/apps/apps.h
e62613
--- openssl-1.0.2k/apps/apps.h.starttls	2017-03-09 17:35:28.632604234 +0100
e62613
+++ openssl-1.0.2k/apps/apps.h	2017-03-09 17:35:35.520765950 +0100
e62613
@@ -384,6 +384,8 @@ int raw_write_stdout(const void *, int);
e62613
 # define TM_STOP         1
e62613
 double app_tminterval(int stop, int usertime);
e62613
 
e62613
+void make_uppercase(char *string);
e62613
+
e62613
 # define OPENSSL_NO_SSL_INTERN
e62613
 
e62613
 #endif
e62613
diff -up openssl-1.0.2k/apps/s_client.c.starttls openssl-1.0.2k/apps/s_client.c
e62613
--- openssl-1.0.2k/apps/s_client.c.starttls	2017-03-09 17:35:28.684605455 +0100
e62613
+++ openssl-1.0.2k/apps/s_client.c	2017-03-09 17:52:59.153207946 +0100
e62613
@@ -134,7 +134,8 @@
e62613
  * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
e62613
  * OTHERWISE.
e62613
  */
e62613
-
e62613
+/* for strcasestr */
e62613
+#define _GNU_SOURCE
e62613
 #include <assert.h>
e62613
 #include <ctype.h>
e62613
 #include <stdio.h>
e62613
@@ -202,6 +203,7 @@ static char *krb5svc = NULL;
e62613
 
e62613
 #undef BUFSIZZ
e62613
 #define BUFSIZZ 1024*8
e62613
+#define S_CLIENT_IRC_READ_TIMEOUT 8
e62613
 
e62613
 extern int verify_depth;
e62613
 extern int verify_error;
e62613
@@ -228,6 +230,7 @@ static void print_stuff(BIO *berr, SSL *
e62613
 #ifndef OPENSSL_NO_TLSEXT
e62613
 static int ocsp_resp_cb(SSL *s, void *arg);
e62613
 #endif
e62613
+static int ldap_ExtendedResponse_parse(const char *buf, long rem);
e62613
 static BIO *bio_c_out = NULL;
e62613
 static BIO *bio_c_msg = NULL;
e62613
 static int c_quiet = 0;
e62613
@@ -402,8 +405,14 @@ static void sc_usage(void)
e62613
     BIO_printf(bio_err,
e62613
                "                 'prot' defines which one to assume.  Currently,\n");
e62613
     BIO_printf(bio_err,
e62613
-               "                 only \"smtp\", \"pop3\", \"imap\", \"ftp\" and \"xmpp\"\n");
e62613
-    BIO_printf(bio_err, "                 are supported.\n");
e62613
+               "                 only \"smtp\", \"pop3\", \"imap\", \"ftp\", \"xmpp\",\n");
e62613
+    BIO_printf(bio_err,
e62613
+               "                 \"xmpp-server\", \"irc\", \"postgres\", \"lmtp\", \"nntp\",\n");
e62613
+    BIO_printf(bio_err, "                 \"sieve\" and \"ldap\" are supported.\n");
e62613
+    BIO_printf(bio_err,
e62613
+               " -xmpphost host - Host to use with \"-starttls xmpp[-server]\"\n");
e62613
+    BIO_printf(bio_err,
e62613
+               " -name host     - Hostname to use for \"-starttls lmtp\" or \"-starttls smtp\"\n");
e62613
 #ifndef OPENSSL_NO_KRB5
e62613
     BIO_printf(bio_err, " -krb5svc arg  - Kerberos service name\n");
e62613
 #endif
e62613
@@ -657,7 +666,15 @@ enum {
e62613
     PROTO_POP3,
e62613
     PROTO_IMAP,
e62613
     PROTO_FTP,
e62613
-    PROTO_XMPP
e62613
+    PROTO_TELNET,
e62613
+    PROTO_XMPP,
e62613
+    PROTO_XMPP_SERVER,
e62613
+    PROTO_IRC,
e62613
+    PROTO_POSTGRES,
e62613
+    PROTO_LMTP,
e62613
+    PROTO_NNTP,
e62613
+    PROTO_SIEVE,
e62613
+    PROTO_LDAP
e62613
 };
e62613
 
e62613
 int MAIN(int, char **);
e62613
@@ -726,6 +743,8 @@ int MAIN(int argc, char **argv)
e62613
 #endif
e62613
     char *sess_in = NULL;
e62613
     char *sess_out = NULL;
e62613
+    char *xmpphost = NULL;
e62613
+    const char *ehlo = "openssl.client.net";
e62613
     struct sockaddr peer;
e62613
     int peerlen = sizeof(peer);
e62613
     int fallback_scsv = 0;
e62613
@@ -1097,8 +1116,32 @@ int MAIN(int argc, char **argv)
e62613
                 starttls_proto = PROTO_FTP;
e62613
             else if (strcmp(*argv, "xmpp") == 0)
e62613
                 starttls_proto = PROTO_XMPP;
e62613
+            else if (strcmp(*argv, "xmpp-server") == 0)
e62613
+                starttls_proto = PROTO_XMPP_SERVER;
e62613
+            else if (strcmp(*argv, "telnet") == 0)
e62613
+                starttls_proto = PROTO_TELNET;
e62613
+            else if (strcmp(*argv, "irc") == 0)
e62613
+                starttls_proto = PROTO_IRC;
e62613
+            else if (strcmp(*argv, "postgres") == 0)
e62613
+                starttls_proto = PROTO_POSTGRES;
e62613
+            else if (strcmp(*argv, "lmtp") == 0)
e62613
+                starttls_proto = PROTO_LMTP;
e62613
+            else if (strcmp(*argv, "nntp") == 0)
e62613
+                starttls_proto = PROTO_NNTP;
e62613
+            else if (strcmp(*argv, "sieve") == 0)
e62613
+                starttls_proto = PROTO_SIEVE;
e62613
+            else if (strcmp(*argv, "ldap") == 0)
e62613
+                starttls_proto = PROTO_LDAP;
e62613
             else
e62613
                 goto bad;
e62613
+        } else if (strcmp(*argv, "-xmpphost") == 0) {
e62613
+            if (--argc < 1)
e62613
+                goto bad;
e62613
+            xmpphost = *(++argv);
e62613
+        } else if (strcmp(*argv, "-name") == 0) {
e62613
+            if (--argc < 1)
e62613
+                goto bad;
e62613
+            ehlo = *(++argv);
e62613
         }
e62613
 #ifndef OPENSSL_NO_ENGINE
e62613
         else if (strcmp(*argv, "-engine") == 0) {
e62613
@@ -1599,19 +1642,24 @@ int MAIN(int argc, char **argv)
e62613
      * BIO into the chain that is removed again later on to not disturb the
e62613
      * rest of the s_client operation.
e62613
      */
e62613
-    if (starttls_proto == PROTO_SMTP) {
e62613
+    if (starttls_proto == PROTO_SMTP || starttls_proto == PROTO_LMTP) {
e62613
         int foundit = 0;
e62613
         BIO *fbio = BIO_new(BIO_f_buffer());
e62613
         BIO_push(fbio, sbio);
e62613
-        /* wait for multi-line response to end from SMTP */
e62613
+        /* Wait for multi-line response to end from LMTP or SMTP */
e62613
         do {
e62613
             mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
e62613
         }
e62613
         while (mbuf_len > 3 && mbuf[3] == '-');
e62613
-        /* STARTTLS command requires EHLO... */
e62613
-        BIO_printf(fbio, "EHLO openssl.client.net\r\n");
e62613
+        if (starttls_proto == PROTO_LMTP)
e62613
+            BIO_printf(fbio, "LHLO %s\r\n", ehlo);
e62613
+        else
e62613
+            BIO_printf(fbio, "EHLO %s\r\n", ehlo);
e62613
         (void)BIO_flush(fbio);
e62613
-        /* wait for multi-line response to end EHLO SMTP response */
e62613
+        /*
e62613
+         * Wait for multi-line response to end LHLO LMTP or EHLO SMTP
e62613
+         * response.
e62613
+         */
e62613
         do {
e62613
             mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
e62613
             if (strstr(mbuf, "STARTTLS"))
e62613
@@ -1630,10 +1678,15 @@ int MAIN(int argc, char **argv)
e62613
     } else if (starttls_proto == PROTO_POP3) {
e62613
         BIO_read(sbio, mbuf, BUFSIZZ);
e62613
         BIO_printf(sbio, "STLS\r\n");
e62613
-        BIO_read(sbio, sbuf, BUFSIZZ);
e62613
+        mbuf_len = BIO_read(sbio, sbuf, BUFSIZZ);
e62613
+        if (mbuf_len < 0) {
e62613
+            BIO_printf(bio_err, "BIO_read failed\n");
e62613
+            goto end;
e62613
+        }
e62613
     } else if (starttls_proto == PROTO_IMAP) {
e62613
         int foundit = 0;
e62613
         BIO *fbio = BIO_new(BIO_f_buffer());
e62613
+
e62613
         BIO_push(fbio, sbio);
e62613
         BIO_gets(fbio, mbuf, BUFSIZZ);
e62613
         /* STARTTLS command requires CAPABILITY... */
e62613
@@ -1669,27 +1722,287 @@ int MAIN(int argc, char **argv)
e62613
         BIO_printf(sbio, "AUTH TLS\r\n");
e62613
         BIO_read(sbio, sbuf, BUFSIZZ);
e62613
     }
e62613
-    if (starttls_proto == PROTO_XMPP) {
e62613
+    else if (starttls_proto == PROTO_XMPP || starttls_proto == PROTO_XMPP_SERVER) {
e62613
         int seen = 0;
e62613
         BIO_printf(sbio, "
e62613
                    "xmlns:stream='http://etherx.jabber.org/streams' "
e62613
-                   "xmlns='jabber:client' to='%s' version='1.0'>", host);
e62613
+                   "xmlns='jabber:%s' to='%s' version='1.0'>",
e62613
+                   starttls_proto == PROTO_XMPP ? "client" : "server",
e62613
+                   xmpphost ? xmpphost : host);
e62613
         seen = BIO_read(sbio, mbuf, BUFSIZZ);
e62613
+        if (seen < 0) {
e62613
+            BIO_printf(bio_err, "BIO_read failed\n");
e62613
+            goto end;
e62613
+        }
e62613
         mbuf[seen] = 0;
e62613
-        while (!strstr
e62613
-               (mbuf, "
e62613
-            if (strstr(mbuf, "/stream:features>"))
e62613
-                goto shut;
e62613
+        while (!strcasestr
e62613
+               (mbuf, "
e62613
+               && !strcasestr(mbuf,
e62613
+                              "
e62613
+        {
e62613
             seen = BIO_read(sbio, mbuf, BUFSIZZ);
e62613
+
e62613
+            if (seen <= 0)
e62613
+               goto shut;
e62613
+
e62613
             mbuf[seen] = 0;
e62613
         }
e62613
         BIO_printf(sbio,
e62613
                    "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>");
e62613
         seen = BIO_read(sbio, sbuf, BUFSIZZ);
e62613
+        if (seen < 0) {
e62613
+            BIO_printf(bio_err, "BIO_read failed\n");
e62613
+            goto shut;
e62613
+        }
e62613
         sbuf[seen] = 0;
e62613
         if (!strstr(sbuf, "
e62613
             goto shut;
e62613
         mbuf[0] = 0;
e62613
+    } else if (starttls_proto == PROTO_TELNET) {
e62613
+        static const unsigned char tls_do[] = {
e62613
+            /* IAC    DO   START_TLS */
e62613
+               255,   253, 46
e62613
+        };
e62613
+        static const unsigned char tls_will[] = {
e62613
+            /* IAC  WILL START_TLS */
e62613
+               255, 251, 46
e62613
+        };
e62613
+        static const unsigned char tls_follows[] = {
e62613
+            /* IAC  SB   START_TLS FOLLOWS IAC  SE */
e62613
+               255, 250, 46,       1,      255, 240
e62613
+        };
e62613
+        int bytes;
e62613
+
e62613
+        /* Telnet server should demand we issue START_TLS */
e62613
+        bytes = BIO_read(sbio, mbuf, BUFSIZZ);
e62613
+        if (bytes != 3 || memcmp(mbuf, tls_do, 3) != 0)
e62613
+            goto shut;
e62613
+        /* Agree to issue START_TLS and send the FOLLOWS sub-command */
e62613
+        BIO_write(sbio, tls_will, 3);
e62613
+        BIO_write(sbio, tls_follows, 6);
e62613
+        (void)BIO_flush(sbio);
e62613
+        /* Telnet server also sent the FOLLOWS sub-command */
e62613
+        bytes = BIO_read(sbio, mbuf, BUFSIZZ);
e62613
+        if (bytes != 6 || memcmp(mbuf, tls_follows, 6) != 0)
e62613
+            goto shut;
e62613
+    } else if (starttls_proto == PROTO_IRC) {
e62613
+        int numeric;
e62613
+        BIO *fbio = BIO_new(BIO_f_buffer());
e62613
+
e62613
+        BIO_push(fbio, sbio);
e62613
+        BIO_printf(fbio, "STARTTLS\r\n");
e62613
+        (void)BIO_flush(fbio);
e62613
+        width = SSL_get_fd(con) + 1;
e62613
+
e62613
+        do {
e62613
+            numeric = 0;
e62613
+
e62613
+            FD_ZERO(&readfds);
e62613
+            openssl_fdset(SSL_get_fd(con), &readfds);
e62613
+            timeout.tv_sec = S_CLIENT_IRC_READ_TIMEOUT;
e62613
+            timeout.tv_usec = 0;
e62613
+            /*
e62613
+             * If the IRCd doesn't respond within
e62613
+             * S_CLIENT_IRC_READ_TIMEOUT seconds, assume
e62613
+             * it doesn't support STARTTLS. Many IRCds
e62613
+             * will not give _any_ sort of response to a
e62613
+             * STARTTLS command when it's not supported.
e62613
+             */
e62613
+            if (!BIO_get_buffer_num_lines(fbio)
e62613
+                && !BIO_pending(fbio)
e62613
+                && !BIO_pending(sbio)
e62613
+                && select(width, (void *)&readfds, NULL, NULL,
e62613
+                          &timeout) < 1) {
e62613
+                BIO_printf(bio_err,
e62613
+                           "Timeout waiting for response (%d seconds).\n",
e62613
+                           S_CLIENT_IRC_READ_TIMEOUT);
e62613
+                break;
e62613
+            }
e62613
+
e62613
+            mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
e62613
+            if (mbuf_len < 1 || sscanf(mbuf, "%*s %d", &numeric) != 1)
e62613
+                break;
e62613
+            /* :example.net 451 STARTTLS :You have not registered */
e62613
+            /* :example.net 421 STARTTLS :Unknown command */
e62613
+            if ((numeric == 451 || numeric == 421)
e62613
+                && strstr(mbuf, "STARTTLS") != NULL) {
e62613
+                BIO_printf(bio_err, "STARTTLS not supported: %s", mbuf);
e62613
+                break;
e62613
+            }
e62613
+            if (numeric == 691) {
e62613
+                BIO_printf(bio_err, "STARTTLS negotiation failed: ");
e62613
+                ERR_print_errors(bio_err);
e62613
+                break;
e62613
+            }
e62613
+        } while (numeric != 670);
e62613
+
e62613
+        (void)BIO_flush(fbio);
e62613
+        BIO_pop(fbio);
e62613
+        BIO_free(fbio);
e62613
+        if (numeric != 670) {
e62613
+            BIO_printf(bio_err, "Server does not support STARTTLS.\n");
e62613
+            ret = 1;
e62613
+            goto shut;
e62613
+        }
e62613
+    } else if (starttls_proto == PROTO_POSTGRES) {
e62613
+        static const unsigned char ssl_request[] = {
e62613
+            /* Length        SSLRequest */
e62613
+               0, 0, 0, 8,   4, 210, 22, 47
e62613
+        };
e62613
+        int bytes;
e62613
+
e62613
+        /* Send SSLRequest packet */
e62613
+        BIO_write(sbio, ssl_request, 8);
e62613
+        (void)BIO_flush(sbio);
e62613
+
e62613
+        /* Reply will be a single S if SSL is enabled */
e62613
+        bytes = BIO_read(sbio, sbuf, BUFSIZZ);
e62613
+        if (bytes != 1 || sbuf[0] != 'S')
e62613
+            goto shut;
e62613
+    } else if (starttls_proto == PROTO_NNTP) {
e62613
+        int foundit = 0;
e62613
+        BIO *fbio = BIO_new(BIO_f_buffer());
e62613
+
e62613
+        BIO_push(fbio, sbio);
e62613
+        BIO_gets(fbio, mbuf, BUFSIZZ);
e62613
+        /* STARTTLS command requires CAPABILITIES... */
e62613
+        BIO_printf(fbio, "CAPABILITIES\r\n");
e62613
+        (void)BIO_flush(fbio);
e62613
+        /* wait for multi-line CAPABILITIES response */
e62613
+        do {
e62613
+            mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
e62613
+            if (strstr(mbuf, "STARTTLS"))
e62613
+                foundit = 1;
e62613
+        } while (mbuf_len > 1 && mbuf[0] != '.');
e62613
+        (void)BIO_flush(fbio);
e62613
+        BIO_pop(fbio);
e62613
+        BIO_free(fbio);
e62613
+        if (!foundit)
e62613
+            BIO_printf(bio_err,
e62613
+                       "Didn't find STARTTLS in server response,"
e62613
+                       " trying anyway...\n");
e62613
+        BIO_printf(sbio, "STARTTLS\r\n");
e62613
+        mbuf_len = BIO_read(sbio, mbuf, BUFSIZZ);
e62613
+        if (mbuf_len < 0) {
e62613
+            BIO_printf(bio_err, "BIO_read failed\n");
e62613
+            goto end;
e62613
+        }
e62613
+        mbuf[mbuf_len] = '\0';
e62613
+        if (strstr(mbuf, "382") == NULL) {
e62613
+            BIO_printf(bio_err, "STARTTLS failed: %s", mbuf);
e62613
+            goto shut;
e62613
+        }
e62613
+    } else if (starttls_proto == PROTO_SIEVE) {
e62613
+        int foundit = 0;
e62613
+        BIO *fbio = BIO_new(BIO_f_buffer());
e62613
+
e62613
+        BIO_push(fbio, sbio);
e62613
+        /* wait for multi-line response to end from Sieve */
e62613
+        do {
e62613
+            mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);
e62613
+            /*
e62613
+             * According to RFC 5804 ยง 1.7, capability
e62613
+             * is case-insensitive, make it uppercase
e62613
+             */
e62613
+            if (mbuf_len > 1 && mbuf[0] == '"') {
e62613
+                make_uppercase(mbuf);
e62613
+                if (strncmp(mbuf, "\"STARTTLS\"", 10) == 0)
e62613
+                    foundit = 1;
e62613
+            }
e62613
+        } while (mbuf_len > 1 && mbuf[0] == '"');
e62613
+        (void)BIO_flush(fbio);
e62613
+        BIO_pop(fbio);
e62613
+        BIO_free(fbio);
e62613
+        if (!foundit)
e62613
+            BIO_printf(bio_err,
e62613
+                       "Didn't find STARTTLS in server response,"
e62613
+                       " trying anyway...\n");
e62613
+        BIO_printf(sbio, "STARTTLS\r\n");
e62613
+        mbuf_len = BIO_read(sbio, mbuf, BUFSIZZ);
e62613
+        if (mbuf_len < 0) {
e62613
+            BIO_printf(bio_err, "BIO_read failed\n");
e62613
+            goto end;
e62613
+        }
e62613
+        mbuf[mbuf_len] = '\0';
e62613
+        if (mbuf_len < 2) {
e62613
+            BIO_printf(bio_err, "STARTTLS failed: %s", mbuf);
e62613
+            goto shut;
e62613
+        }
e62613
+        /*
e62613
+         * According to RFC 5804 ยง 2.2, response codes are case-
e62613
+         * insensitive, make it uppercase but preserve the response.
e62613
+         */
e62613
+        strncpy(sbuf, mbuf, 2);
e62613
+        make_uppercase(sbuf);
e62613
+        if (strncmp(sbuf, "OK", 2) != 0) {
e62613
+            BIO_printf(bio_err, "STARTTLS not supported: %s", mbuf);
e62613
+            goto shut;
e62613
+        }
e62613
+    } else if (starttls_proto == PROTO_LDAP) {
e62613
+        /* StartTLS Operation according to RFC 4511 */
e62613
+        static char ldap_tls_genconf[] = "asn1=SEQUENCE:LDAPMessage\n"
e62613
+            "[LDAPMessage]\n"
e62613
+            "messageID=INTEGER:1\n"
e62613
+            "extendedReq=EXPLICIT:23A,IMPLICIT:0C,"
e62613
+            "FORMAT:ASCII,OCT:1.3.6.1.4.1.1466.20037\n";
e62613
+        long errline = -1;
e62613
+        char *genstr = NULL;
e62613
+        int result = -1;
e62613
+        ASN1_TYPE *atyp = NULL;
e62613
+        BIO *ldapbio = BIO_new(BIO_s_mem());
e62613
+        CONF *cnf = NCONF_new(NULL);
e62613
+
e62613
+        if (cnf == NULL) {
e62613
+            BIO_free(ldapbio);
e62613
+            goto end;
e62613
+        }
e62613
+        BIO_puts(ldapbio, ldap_tls_genconf);
e62613
+        if (NCONF_load_bio(cnf, ldapbio, &errline) <= 0) {
e62613
+            BIO_free(ldapbio);
e62613
+            NCONF_free(cnf);
e62613
+            if (errline <= 0) {
e62613
+                BIO_printf(bio_err, "NCONF_load_bio failed\n");
e62613
+                goto end;
e62613
+            } else {
e62613
+                BIO_printf(bio_err, "Error on line %ld\n", errline);
e62613
+                goto end;
e62613
+            }
e62613
+        }
e62613
+        BIO_free(ldapbio);
e62613
+        genstr = NCONF_get_string(cnf, "default", "asn1");
e62613
+        if (genstr == NULL) {
e62613
+            NCONF_free(cnf);
e62613
+            BIO_printf(bio_err, "NCONF_get_string failed\n");
e62613
+            goto end;
e62613
+        }
e62613
+        atyp = ASN1_generate_nconf(genstr, cnf);
e62613
+        if (atyp == NULL) {
e62613
+            NCONF_free(cnf);
e62613
+            BIO_printf(bio_err, "ASN1_generate_nconf failed\n");
e62613
+            goto end;
e62613
+        }
e62613
+        NCONF_free(cnf);
e62613
+        /* Send SSLRequest packet */
e62613
+        BIO_write(sbio, atyp->value.sequence->data,
e62613
+                  atyp->value.sequence->length);
e62613
+        (void)BIO_flush(sbio);
e62613
+        ASN1_TYPE_free(atyp);
e62613
+
e62613
+        mbuf_len = BIO_read(sbio, mbuf, BUFSIZZ);
e62613
+        if (mbuf_len < 0) {
e62613
+            BIO_printf(bio_err, "BIO_read failed\n");
e62613
+            goto end;
e62613
+        }
e62613
+        result = ldap_ExtendedResponse_parse(mbuf, mbuf_len);
e62613
+        if (result < 0) {
e62613
+            BIO_printf(bio_err, "ldap_ExtendedResponse_parse failed\n");
e62613
+            goto shut;
e62613
+        } else if (result > 0) {
e62613
+            BIO_printf(bio_err, "STARTTLS failed, LDAP Result Code: %i\n",
e62613
+                       result);
e62613
+            goto shut;
e62613
+        }
e62613
+        mbuf_len = 0;
e62613
     }
e62613
 
e62613
     for (;;) {
e62613
@@ -1738,7 +2051,7 @@ int MAIN(int argc, char **argv)
e62613
                     full_log--;
e62613
 
e62613
                 if (starttls_proto) {
e62613
-                    BIO_printf(bio_err, "%s", mbuf);
e62613
+                    BIO_write(bio_err, mbuf, mbuf_len);
e62613
                     /* We don't need to know any more */
e62613
                     starttls_proto = PROTO_OFF;
e62613
                 }
e62613
@@ -2372,3 +2685,87 @@ static int ocsp_resp_cb(SSL *s, void *ar
e62613
 }
e62613
 
e62613
 #endif
e62613
+
e62613
+static int ldap_ExtendedResponse_parse(const char *buf, long rem)
e62613
+{
e62613
+    const unsigned char *cur, *end;
e62613
+    long len;
e62613
+    int tag, xclass, inf, ret = -1;
e62613
+
e62613
+    cur = (const unsigned char *)buf;
e62613
+    end = cur + rem;
e62613
+
e62613
+    /*
e62613
+     * From RFC 4511:
e62613
+     *
e62613
+     *    LDAPMessage ::= SEQUENCE {
e62613
+     *         messageID       MessageID,
e62613
+     *         protocolOp      CHOICE {
e62613
+     *              ...
e62613
+     *              extendedResp          ExtendedResponse,
e62613
+     *              ... },
e62613
+     *         controls       [0] Controls OPTIONAL }
e62613
+     *
e62613
+     *    ExtendedResponse ::= [APPLICATION 24] SEQUENCE {
e62613
+     *         COMPONENTS OF LDAPResult,
e62613
+     *         responseName     [10] LDAPOID OPTIONAL,
e62613
+     *         responseValue    [11] OCTET STRING OPTIONAL }
e62613
+     *
e62613
+     *    LDAPResult ::= SEQUENCE {
e62613
+     *         resultCode         ENUMERATED {
e62613
+     *              success                      (0),
e62613
+     *              ...
e62613
+     *              other                        (80),
e62613
+     *              ...  },
e62613
+     *         matchedDN          LDAPDN,
e62613
+     *         diagnosticMessage  LDAPString,
e62613
+     *         referral           [3] Referral OPTIONAL }
e62613
+     */
e62613
+
e62613
+    /* pull SEQUENCE */
e62613
+    inf = ASN1_get_object(&cur, &len, &tag, &xclass, rem);
e62613
+    if (inf != V_ASN1_CONSTRUCTED || tag != V_ASN1_SEQUENCE ||
e62613
+        (rem = end - cur, len > rem)) {
e62613
+        BIO_printf(bio_err, "Unexpected LDAP response\n");
e62613
+        goto end;
e62613
+    }
e62613
+
e62613
+    rem = len;  /* ensure that we don't overstep the SEQUENCE */
e62613
+
e62613
+    /* pull MessageID */
e62613
+    inf = ASN1_get_object(&cur, &len, &tag, &xclass, rem);
e62613
+    if (inf != V_ASN1_UNIVERSAL || tag != V_ASN1_INTEGER ||
e62613
+        (rem = end - cur, len > rem)) {
e62613
+        BIO_printf(bio_err, "No MessageID\n");
e62613
+        goto end;
e62613
+    }
e62613
+
e62613
+    cur += len; /* shall we check for MessageId match or just skip? */
e62613
+
e62613
+    /* pull [APPLICATION 24] */
e62613
+    rem = end - cur;
e62613
+    inf = ASN1_get_object(&cur, &len, &tag, &xclass, rem);
e62613
+    if (inf != V_ASN1_CONSTRUCTED || xclass != V_ASN1_APPLICATION ||
e62613
+        tag != 24) {
e62613
+        BIO_printf(bio_err, "Not ExtendedResponse\n");
e62613
+        goto end;
e62613
+    }
e62613
+
e62613
+    /* pull resultCode */
e62613
+    rem = end - cur;
e62613
+    inf = ASN1_get_object(&cur, &len, &tag, &xclass, rem);
e62613
+    if (inf != V_ASN1_UNIVERSAL || tag != V_ASN1_ENUMERATED || len == 0 ||
e62613
+        (rem = end - cur, len > rem)) {
e62613
+        BIO_printf(bio_err, "Not LDAPResult\n");
e62613
+        goto end;
e62613
+    }
e62613
+
e62613
+    /* len should always be one, but just in case... */
e62613
+    for (ret = 0, inf = 0; inf < len; inf++) {
e62613
+        ret <<= 8;
e62613
+        ret |= cur[inf];
e62613
+    }
e62613
+    /* There is more data, but we don't care... */
e62613
+ end:
e62613
+    return ret;
e62613
+}
e62613
diff -up openssl-1.0.2k/doc/apps/s_client.pod.starttls openssl-1.0.2k/doc/apps/s_client.pod
e62613
--- openssl-1.0.2k/doc/apps/s_client.pod.starttls	2017-03-09 17:35:28.684605455 +0100
e62613
+++ openssl-1.0.2k/doc/apps/s_client.pod	2017-03-09 17:42:54.455070967 +0100
e62613
@@ -46,6 +46,8 @@ B<openssl> B<s_client>
e62613
 [B<-krb5svc service>]
e62613
 [B<-serverpref>]
e62613
 [B<-starttls protocol>]
e62613
+[B<-xmpphost hostname>]
e62613
+[B<-name hostname>]
e62613
 [B<-engine id>]
e62613
 [B<-tlsextdebug>]
e62613
 [B<-no_ticket>]
e62613
@@ -239,7 +241,20 @@ need keys for that principal in its keyt
e62613
 
e62613
 send the protocol-specific message(s) to switch to TLS for communication.
e62613
 B<protocol> is a keyword for the intended protocol.  Currently, the only
e62613
-supported keywords are "smtp", "pop3", "imap", and "ftp".
e62613
+supported keywords are "smtp", "pop3", "imap", "ftp", "xmpp", "xmpp-server",
e62613
+"irc", "postgres", "lmtp", "nntp", "sieve" and "ldap".
e62613
+
e62613
+=item B<-xmpphost hostname>
e62613
+
e62613
+This option, when used with "-starttls xmpp" or "-starttls xmpp-server",
e62613
+specifies the host for the "to" attribute of the stream element.
e62613
+If this option is not specified, then the host specified with "-connect"
e62613
+will be used.
e62613
+
e62613
+=item B<-name hostname>
e62613
+
e62613
+the host name to use with "-starttls smtp".
e62613
+If this option is not specified, the default "openssl.client.net" will be used.
e62613
 
e62613
 =item B<-tlsextdebug>
e62613