Blame SOURCES/openssl-1.0.2k-starttls.patch

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