Merge remote-tracking branch 'agl/1.0.2alpn' into agl-alpn
[openssl.git] / ssl / t1_lib.c
index b3c7cb5df16ae0426143a8ab22349d4e8728c76c..b91afc8f3a4487179281caa27c4e0a97fe1331d8 100644 (file)
@@ -1414,6 +1414,18 @@ unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *p, unsigned cha
                }
 #endif
 
+       if (s->alpn_client_proto_list && !s->s3->tmp.finish_md_len)
+               {
+               if ((size_t)(limit - ret) < 6 + s->alpn_client_proto_list_len)
+                       return NULL;
+               s2n(TLSEXT_TYPE_application_layer_protocol_negotiation,ret);
+               s2n(2 + s->alpn_client_proto_list_len,ret);
+               s2n(s->alpn_client_proto_list_len,ret);
+               memcpy(ret, s->alpn_client_proto_list,
+                      s->alpn_client_proto_list_len);
+               ret += s->alpn_client_proto_list_len;
+               }
+
         if(SSL_get_srtp_profiles(s))
                 {
                 int el;
@@ -1795,6 +1807,21 @@ unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *p, unsigned cha
                        }
                }
 
+       if (s->s3->alpn_selected)
+               {
+               const unsigned char *selected = s->s3->alpn_selected;
+               unsigned len = s->s3->alpn_selected_len;
+
+               if ((long)(limit - ret - 4 - 2 - 1 - len) < 0)
+                       return NULL;
+               s2n(TLSEXT_TYPE_application_layer_protocol_negotiation,ret);
+               s2n(3 + len,ret);
+               s2n(1 + len,ret);
+               *ret++ = len;
+               memcpy(ret, selected, len);
+               ret += len;
+               }
+
        if ((extdatalen = ret-p-2)== 0) 
                return p;
 
@@ -1883,7 +1910,76 @@ static void ssl_check_for_safari(SSL *s, const unsigned char *data, const unsign
 
        s->s3->is_probably_safari = 1;
 }
-#endif /* !OPENSSL_NO_EC */
+
+/* tls1_alpn_handle_client_hello is called to process the ALPN extension in a
+ * ClientHello.
+ *   data: the contents of the extension, not including the type and length.
+ *   data_len: the number of bytes in |data|
+ *   al: a pointer to the alert value to send in the event of a non-zero
+ *       return.
+ *
+ *   returns: 0 on success. */
+static int tls1_alpn_handle_client_hello(SSL *s, const unsigned char *data,
+                                        unsigned data_len, int *al)
+       {
+       unsigned i;
+       unsigned proto_len;
+       const unsigned char *selected;
+       unsigned char selected_len;
+       int r;
+
+       if (s->ctx->alpn_select_cb == NULL)
+               return 0;
+
+       if (data_len < 2)
+               goto parse_error;
+
+       /* data should contain a uint16 length followed by a series of 8-bit,
+        * length-prefixed strings. */
+       i = ((unsigned) data[0]) << 8 |
+           ((unsigned) data[1]);
+       data_len -= 2;
+       data += 2;
+       if (data_len != i)
+               goto parse_error;
+
+       if (data_len < 2)
+               goto parse_error;
+
+       for (i = 0; i < data_len;)
+               {
+               proto_len = data[i];
+               i++;
+
+               if (proto_len == 0)
+                       goto parse_error;
+
+               if (i + proto_len < i || i + proto_len > data_len)
+                       goto parse_error;
+
+               i += proto_len;
+               }
+
+       r = s->ctx->alpn_select_cb(s, &selected, &selected_len, data, data_len,
+                                  s->ctx->alpn_select_cb_arg);
+       if (r == SSL_TLSEXT_ERR_OK) {
+               if (s->s3->alpn_selected)
+                       OPENSSL_free(s->s3->alpn_selected);
+               s->s3->alpn_selected = OPENSSL_malloc(selected_len);
+               if (!s->s3->alpn_selected)
+                       {
+                       *al = SSL_AD_INTERNAL_ERROR;
+                       return -1;
+                       }
+               memcpy(s->s3->alpn_selected, selected, selected_len);
+               s->s3->alpn_selected_len = selected_len;
+       }
+       return 0;
+
+parse_error:
+       *al = SSL_AD_DECODE_ERROR;
+       return -1;
+       }
 
 static int ssl_scan_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n, int *al) 
        {       
@@ -1908,6 +2004,12 @@ static int ssl_scan_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char
                s->s3->tlsext_custom_types = NULL;
                }               
 
+       if (s->s3->alpn_selected)
+               {
+               OPENSSL_free(s->s3->alpn_selected);
+               s->s3->alpn_selected = NULL;
+               }
+
 #ifndef OPENSSL_NO_HEARTBEATS
        s->tlsext_heartbeat &= ~(SSL_TLSEXT_HB_ENABLED |
                               SSL_TLSEXT_HB_DONT_SEND_REQUESTS);
@@ -2369,7 +2471,8 @@ static int ssl_scan_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char
 #endif
 #ifndef OPENSSL_NO_NEXTPROTONEG
                else if (type == TLSEXT_TYPE_next_proto_neg &&
-                        s->s3->tmp.finish_md_len == 0)
+                        s->s3->tmp.finish_md_len == 0 &&
+                        s->s3->alpn_selected == NULL)
                        {
                        /* We shouldn't accept this extension on a
                         * renegotiation.
@@ -2390,6 +2493,16 @@ static int ssl_scan_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char
                        }
 #endif
 
+               else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation &&
+                        s->ctx->alpn_select_cb &&
+                        s->s3->tmp.finish_md_len == 0)
+                       {
+                       if (tls1_alpn_handle_client_hello(s, data, size, al) != 0)
+                               return 0;
+                       /* ALPN takes precedence over NPN. */
+                       s->s3->next_proto_neg_seen = 0;
+                       }
+
                /* session ticket processed earlier */
                else if (type == TLSEXT_TYPE_use_srtp)
                         {
@@ -2579,6 +2692,12 @@ static int ssl_scan_serverhello_tlsext(SSL *s, unsigned char **p, unsigned char
        s->s3->next_proto_neg_seen = 0;
 #endif
 
+       if (s->s3->alpn_selected)
+               {
+               OPENSSL_free(s->s3->alpn_selected);
+               s->s3->alpn_selected = NULL;
+               }
+
 #ifndef OPENSSL_NO_HEARTBEATS
        s->tlsext_heartbeat &= ~(SSL_TLSEXT_HB_ENABLED |
                               SSL_TLSEXT_HB_DONT_SEND_REQUESTS);
@@ -2741,6 +2860,52 @@ static int ssl_scan_serverhello_tlsext(SSL *s, unsigned char **p, unsigned char
                        s->s3->next_proto_neg_seen = 1;
                        }
 #endif
+
+               else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation)
+                       {
+                       unsigned len;
+
+                       /* We must have requested it. */
+                       if (s->alpn_client_proto_list == NULL)
+                               {
+                               *al = TLS1_AD_UNSUPPORTED_EXTENSION;
+                               return 0;
+                               }
+                       if (size < 4)
+                               {
+                               *al = TLS1_AD_DECODE_ERROR;
+                               return 0;
+                               }
+                       /* The extension data consists of:
+                        *   uint16 list_length
+                        *   uint8 proto_length;
+                        *   uint8 proto[proto_length]; */
+                       len = data[0];
+                       len <<= 8;
+                       len |= data[1];
+                       if (len != (unsigned) size - 2)
+                               {
+                               *al = TLS1_AD_DECODE_ERROR;
+                               return 0;
+                               }
+                       len = data[2];
+                       if (len != (unsigned) size - 3)
+                               {
+                               *al = TLS1_AD_DECODE_ERROR;
+                               return 0;
+                               }
+                       if (s->s3->alpn_selected)
+                               OPENSSL_free(s->s3->alpn_selected);
+                       s->s3->alpn_selected = OPENSSL_malloc(len);
+                       if (!s->s3->alpn_selected)
+                               {
+                               *al = TLS1_AD_INTERNAL_ERROR;
+                               return 0;
+                               }
+                       memcpy(s->s3->alpn_selected, data + 3, len);
+                       s->s3->alpn_selected_len = len;
+                       }
+
                else if (type == TLSEXT_TYPE_renegotiate)
                        {
                        if(!ssl_parse_serverhello_renegotiate_ext(s, data, size, al))