Blame SOURCES/openssl-1.0.2k-starttls.patch

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