bn/bntest.c: make it indent-friendly.
[openssl.git] / ssl / t1_lib.c
index debad3bb492fc8ac69366a4b714433ee5eebf72b..2e8e149b96319ccb782f560ebb91c492fe94f4c4 100644 (file)
@@ -504,11 +504,12 @@ int tls1_check_curve(SSL *s, const unsigned char *p, size_t len)
        return 0;
        }
 
-/* Return nth shared curve. If nmatch == -1 return number of
- * matches. For nmatch == -2 return the NID of the curve to use for
- * an EC tmp key.
+/*-
+ * Return |nmatch|th shared curve or NID_undef if there is no match.
+ * For nmatch == -1, return number of  matches
+ * For nmatch == -2, return the NID of the curve to use for
+ * an EC tmp key, or NID_undef if there is no match.
  */
-
 int tls1_shared_curve(SSL *s, int nmatch)
        {
        const unsigned char *pref, *supp;
@@ -542,10 +543,11 @@ int tls1_shared_curve(SSL *s, int nmatch)
         */
        if (!tls1_get_curvelist(s, (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) != 0,
                        &supp, &num_supp))
-               return 0;
+               /* In practice, NID_undef == 0 but let's be precise. */
+               return nmatch == -1 ? 0 : NID_undef;
        if(!tls1_get_curvelist(s, !(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE),
                        &pref, &num_pref))
-               return 0;
+               return nmatch == -1 ? 0 : NID_undef;
        k = 0;
        for (i = 0; i < num_pref; i++, pref+=2)
                {
@@ -567,7 +569,8 @@ int tls1_shared_curve(SSL *s, int nmatch)
                }
        if (nmatch == -1)
                return k;
-       return 0;
+       /* Out of range (nmatch > k). */
+       return NID_undef;
        }
 
 int tls1_set_curves(unsigned char **pext, size_t *pextlen,
@@ -825,6 +828,7 @@ static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
                }
        return rv;
        }
+#ifndef OPENSSL_NO_ECDH
 /* Check EC temporary key is compatible with client extensions */
 int tls1_check_ec_tmp_key(SSL *s, unsigned long cid)
        {
@@ -891,6 +895,7 @@ int tls1_check_ec_tmp_key(SSL *s, unsigned long cid)
        return tls1_check_ec_key(s, curve_id, NULL);
 #endif
        }
+#endif /* OPENSSL_NO_ECDH */
 
 #else
 
@@ -930,7 +935,7 @@ static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
                tlsext_sigalg_dsa(md) \
                tlsext_sigalg_ecdsa(md)
 
-static unsigned char tls12_sigalgs[] = {
+static const unsigned char tls12_sigalgs[] = {
 #ifndef OPENSSL_NO_SHA512
        tlsext_sigalg(TLSEXT_hash_sha512)
        tlsext_sigalg(TLSEXT_hash_sha384)
@@ -944,7 +949,7 @@ static unsigned char tls12_sigalgs[] = {
 #endif
 };
 #ifndef OPENSSL_NO_ECDSA
-static unsigned char suiteb_sigalgs[] = {
+static const unsigned char suiteb_sigalgs[] = {
        tlsext_sigalg_ecdsa(TLSEXT_hash_sha256)
        tlsext_sigalg_ecdsa(TLSEXT_hash_sha384)
 };
@@ -1217,13 +1222,14 @@ unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *buf, unsigned c
                unsigned long size_str;
                long lenmax; 
 
-               /* check for enough space.
-                  4 for the servername type and entension length
-                  2 for servernamelist length
-                  1 for the hostname type
-                  2 for hostname length
-                  + hostname length 
-               */
+               /*-
+                * check for enough space.
+                * 4 for the servername type and entension length
+                * 2 for servernamelist length
+                * 1 for the hostname type
+                * 2 for hostname length
+                * + hostname length 
+                */
                   
                if ((lenmax = limit - ret - 9) < 0 
                    || (size_str = strlen(s->tlsext_hostname)) > (unsigned long)lenmax) 
@@ -1255,11 +1261,12 @@ unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *buf, unsigned c
                        return NULL;
                        } 
 
-               /* check for enough space.
-                  4 for the srp type type and entension length
-                  1 for the srp user identity
-                  + srp user identity length 
-               */
+               /*-
+                * check for enough space.
+                * 4 for the srp type type and entension length
+                * 1 for the srp user identity
+                * + srp user identity length 
+                */
                if ((limit - ret - 5 - login_len) < 0) return NULL; 
 
                /* fill in the extension */
@@ -1459,7 +1466,8 @@ unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *buf, unsigned c
                return NULL;
        s2n(TLSEXT_TYPE_heartbeat,ret);
        s2n(1,ret);
-       /* Set mode:
+       /*-
+        * Set mode:
         * 1: peer may send requests
         * 2: peer not allowed to send requests
         */
@@ -1493,6 +1501,7 @@ unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *buf, unsigned c
                ret += s->alpn_client_proto_list_len;
                }
 
+#ifndef OPENSSL_NO_SRTP
         if(SSL_IS_DTLS(s) && SSL_get_srtp_profiles(s))
                 {
                 int el;
@@ -1511,6 +1520,7 @@ unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *buf, unsigned c
                        }
                 ret += el;
                 }
+#endif
        custom_ext_init(&s->cert->cli_ext);
        /* Add custom TLS Extensions to ClientHello */
        if (!custom_ext_add(s, 0, &ret, limit, al))
@@ -1673,6 +1683,7 @@ unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *buf, unsigned c
                }
 #endif
 
+#ifndef OPENSSL_NO_SRTP
         if(SSL_IS_DTLS(s) && s->srtp_profile)
                 {
                 int el;
@@ -1691,6 +1702,7 @@ unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *buf, unsigned c
                        }
                 ret+=el;
                 }
+#endif
 
        if (((s->s3->tmp.new_cipher->id & 0xFFFF)==0x80 || (s->s3->tmp.new_cipher->id & 0xFFFF)==0x81) 
                && (SSL_get_options(s) & SSL_OP_CRYPTOPRO_TLSEXT_BUG))
@@ -1715,7 +1727,8 @@ unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *buf, unsigned c
                        return NULL;
                s2n(TLSEXT_TYPE_heartbeat,ret);
                s2n(1,ret);
-               /* Set mode:
+               /*-
+                * Set mode:
                 * 1: peer may send requests
                 * 2: peer not allowed to send requests
                 */
@@ -1862,7 +1875,8 @@ parse_error:
        }
 
 #ifndef OPENSSL_NO_EC
-/* ssl_check_for_safari attempts to fingerprint Safari using OS X
+/*-
+ * ssl_check_for_safari attempts to fingerprint Safari using OS X
  * SecureTransport using the TLS extension block in |d|, of length |n|.
  * Safari, since 10.6, sends exactly these extensions, in this order:
  *   SNI,
@@ -1986,6 +2000,16 @@ static int ssl_scan_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char
        s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC;
 #endif
 
+#ifndef OPENSSL_NO_SRP
+       if (s->srp_ctx.login != NULL)
+               {
+               OPENSSL_free(s->srp_ctx.login);
+               s->srp_ctx.login = NULL;
+               }
+#endif
+
+       s->srtp_profile = NULL;
+
        if (data >= (d+n-2))
                goto ri_check;
        n2s(data,len);
@@ -2014,28 +2038,30 @@ static int ssl_scan_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char
                        }
                else if (s->version == SSL3_VERSION)
                        {}
-/* The servername extension is treated as follows:
-
-   - Only the hostname type is supported with a maximum length of 255.
-   - The servername is rejected if too long or if it contains zeros,
-     in which case an fatal alert is generated.
-   - The servername field is maintained together with the session cache.
-   - When a session is resumed, the servername call back invoked in order
-     to allow the application to position itself to the right context. 
-   - The servername is acknowledged if it is new for a session or when 
-     it is identical to a previously used for the same session. 
-     Applications can control the behaviour.  They can at any time
-     set a 'desirable' servername for a new SSL object. This can be the
-     case for example with HTTPS when a Host: header field is received and
-     a renegotiation is requested. In this case, a possible servername
-     presented in the new client hello is only acknowledged if it matches
-     the value of the Host: field. 
-   - Applications must  use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
-     if they provide for changing an explicit servername context for the session,
-     i.e. when the session has been established with a servername extension. 
-   - On session reconnect, the servername extension may be absent. 
-
-*/      
+/*-
+ * The servername extension is treated as follows:
+ *
+ * - Only the hostname type is supported with a maximum length of 255.
+ * - The servername is rejected if too long or if it contains zeros,
+ *   in which case an fatal alert is generated.
+ * - The servername field is maintained together with the session cache.
+ * - When a session is resumed, the servername call back invoked in order
+ *   to allow the application to position itself to the right context. 
+ * - The servername is acknowledged if it is new for a session or when 
+ *   it is identical to a previously used for the same session. 
+ *   Applications can control the behaviour.  They can at any time
+ *   set a 'desirable' servername for a new SSL object. This can be the
+ *   case for example with HTTPS when a Host: header field is received and
+ *   a renegotiation is requested. In this case, a possible servername
+ *   presented in the new client hello is only acknowledged if it matches
+ *   the value of the Host: field. 
+ * - Applications must  use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
+ *   if they provide for changing an explicit servername context for the 
+ *   session, i.e. when the session has been established with a servername 
+ *   extension. 
+ * - On session reconnect, the servername extension may be absent. 
+ *
+ */      
 
                else if (type == TLSEXT_TYPE_server_name)
                        {
@@ -2240,8 +2266,10 @@ static int ssl_scan_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char
 
                        if (s->s3->client_opaque_prf_input != NULL) /* shouldn't really happen */
                                OPENSSL_free(s->s3->client_opaque_prf_input);
+                               
+                       /* dummy byte just to get non-NULL */
                        if (s->s3->client_opaque_prf_input_len == 0)
-                               s->s3->client_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
+                               s->s3->client_opaque_prf_input = OPENSSL_malloc(1);
                        else
                                s->s3->client_opaque_prf_input = BUF_memdup(sdata, s->s3->client_opaque_prf_input_len);
                        if (s->s3->client_opaque_prf_input == NULL)
@@ -2414,7 +2442,8 @@ static int ssl_scan_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char
                         s->s3->tmp.finish_md_len == 0 &&
                         s->s3->alpn_selected == NULL)
                        {
-                       /* We shouldn't accept this extension on a
+                       /*-
+                        * We shouldn't accept this extension on a
                         * renegotiation.
                         *
                         * s->new_session will be set on renegotiation, but we
@@ -2423,12 +2452,13 @@ static int ssl_scan_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char
                         * there's some other reason to disallow resuming an
                         * earlier session -- the current code won't be doing
                         * anything like that, but this might change).
-
+                        *
                         * A valid sign that there's been a previous handshake
                         * in this connection is if s->s3->tmp.finish_md_len >
                         * 0.  (We are talking about a check that will happen
                         * in the Hello protocol round, well before a new
-                        * Finished message could have been computed.) */
+                        * Finished message could have been computed.) 
+                        */
                        s->s3->next_proto_neg_seen = 1;
                        }
 #endif
@@ -2446,6 +2476,7 @@ static int ssl_scan_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char
                        }
 
                /* session ticket processed earlier */
+#ifndef OPENSSL_NO_SRTP
                else if (SSL_IS_DTLS(s) && SSL_get_srtp_profiles(s)
                                && type == TLSEXT_TYPE_use_srtp)
                         {
@@ -2453,6 +2484,7 @@ static int ssl_scan_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char
                                                              al))
                                return 0;
                         }
+#endif
 #ifdef TLSEXT_TYPE_encrypt_then_mac
                else if (type == TLSEXT_TYPE_encrypt_then_mac)
                        s->s3->flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC;
@@ -2741,10 +2773,12 @@ static int ssl_scan_serverhello_tlsext(SSL *s, unsigned char **p, unsigned char
                                *al = TLS1_AD_DECODE_ERROR;
                                return 0;
                                }
-                       /* The extension data consists of:
+                       /*- 
+                        * The extension data consists of:
                         *   uint16 list_length
                         *   uint8 proto_length;
-                        *   uint8 proto[proto_length]; */
+                        *   uint8 proto[proto_length]; 
+                        */
                        len = data[0];
                        len <<= 8;
                        len |= data[1];
@@ -2787,12 +2821,14 @@ static int ssl_scan_serverhello_tlsext(SSL *s, unsigned char **p, unsigned char
                                }
                        }
 #endif
+#ifndef OPENSSL_NO_SRTP
                else if (SSL_IS_DTLS(s) && type == TLSEXT_TYPE_use_srtp)
                         {
                         if(ssl_parse_serverhello_use_srtp_ext(s, data, size,
                                                              al))
                                 return 0;
                         }
+#endif
 #ifdef TLSEXT_TYPE_encrypt_then_mac
                else if (type == TLSEXT_TYPE_encrypt_then_mac)
                        {
@@ -3243,7 +3279,8 @@ int ssl_parse_serverhello_tlsext(SSL *s, unsigned char **p, unsigned char *d, in
        return 1;
 }
 
-/* Since the server cache lookup is done early on in the processing of the
+/*-
+ * Since the server cache lookup is done early on in the processing of the
  * ClientHello, and other operations depend on the result, we need to handle
  * any TLS session ticket extension at the same time.
  *
@@ -3363,7 +3400,8 @@ int tls1_process_ticket(SSL *s, unsigned char *session_id, int len,
        return 0;
        }
 
-/* tls_decrypt_ticket attempts to decrypt a session ticket.
+/*-
+ * tls_decrypt_ticket attempts to decrypt a session ticket.
  *
  *   etick: points to the body of the session ticket extension.
  *   eticklen: the length of the session tickets extenion.
@@ -3490,7 +3528,7 @@ typedef struct
        int id;
        } tls12_lookup;
 
-static tls12_lookup tls12_md[] = {
+static const tls12_lookup tls12_md[] = {
        {NID_md5, TLSEXT_hash_md5},
        {NID_sha1, TLSEXT_hash_sha1},
        {NID_sha224, TLSEXT_hash_sha224},
@@ -3499,13 +3537,13 @@ static tls12_lookup tls12_md[] = {
        {NID_sha512, TLSEXT_hash_sha512}
 };
 
-static tls12_lookup tls12_sig[] = {
+static const tls12_lookup tls12_sig[] = {
        {EVP_PKEY_RSA, TLSEXT_signature_rsa},
        {EVP_PKEY_DSA, TLSEXT_signature_dsa},
        {EVP_PKEY_EC, TLSEXT_signature_ecdsa}
 };
 
-static int tls12_find_id(int nid, tls12_lookup *table, size_t tlen)
+static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen)
        {
        size_t i;
        for (i = 0; i < tlen; i++)
@@ -3516,7 +3554,7 @@ static int tls12_find_id(int nid, tls12_lookup *table, size_t tlen)
        return -1;
        }
 
-static int tls12_find_nid(int id, tls12_lookup *table, size_t tlen)
+static int tls12_find_nid(int id, const tls12_lookup *table, size_t tlen)
        {
        size_t i;
        for (i = 0; i < tlen; i++)
@@ -3596,10 +3634,8 @@ static const tls12_hash_info *tls12_get_hash_info(unsigned char hash_alg)
 const EVP_MD *tls12_get_hash(unsigned char hash_alg)
        {
        const tls12_hash_info *inf;
-#ifndef OPENSSL_FIPS
        if (hash_alg == TLSEXT_hash_md5 && FIPS_mode())
                return NULL;
-#endif
        inf = tls12_get_hash_info(hash_alg);
        if (!inf || !inf->mfunc)
                return NULL; 
@@ -4002,6 +4038,11 @@ tls1_process_heartbeat(SSL *s)
                 * payload, plus padding
                 */
                buffer = OPENSSL_malloc(1 + 2 + payload + padding);
+               if (buffer == NULL)
+                       {
+                       SSLerr(SSL_F_TLS1_PROCESS_HEARTBEAT,ERR_R_MALLOC_FAILURE);
+                       return -1;
+                       }
                bp = buffer;
                
                /* Enter response type, length and copy payload */
@@ -4078,7 +4119,8 @@ tls1_heartbeat(SSL *s)
         */
        OPENSSL_assert(payload + padding <= 16381);
 
-       /* Create HeartBeat message, we just use a sequence number
+       /*-
+        * Create HeartBeat message, we just use a sequence number
         * as payload to distuingish different messages and add
         * some random stuff.
         *  - Message Type, 1 byte
@@ -4088,6 +4130,11 @@ tls1_heartbeat(SSL *s)
         *  - Padding
         */
        buf = OPENSSL_malloc(1 + 2 + payload + padding);
+       if (buf == NULL)
+               {
+               SSLerr(SSL_F_TLS1_HEARTBEAT,ERR_R_MALLOC_FAILURE);
+               return -1;
+               }
        p = buf;
        /* Message Type */
        *p++ = TLS1_HB_REQUEST;