strncpy(d, s, strlen(s)) is a special kind of stupid. even when it's right,
authortedu <tedu@openbsd.org>
Wed, 16 Apr 2014 01:43:06 +0000 (01:43 +0000)
committertedu <tedu@openbsd.org>
Wed, 16 Apr 2014 01:43:06 +0000 (01:43 +0000)
it looks wrong. replace with auditable code and eliminate many strlen calls
to improve efficiency. (wait, did somebody say FASTER?) ok beck

lib/libssl/d1_srvr.c
lib/libssl/s3_srvr.c
lib/libssl/src/ssl/d1_srvr.c
lib/libssl/src/ssl/s3_srvr.c

index 47a0c0e..6040dd9 100644 (file)
@@ -1017,6 +1017,9 @@ dtls1_send_server_key_exchange(SSL *s)
        int curve_id = 0;
        BN_CTX *bn_ctx = NULL;
 
+#endif
+#ifndef OPENSSL_NO_PSK
+       size_t pskhintlen;
 #endif
        EVP_PKEY *pkey;
        unsigned char *p, *d;
@@ -1226,8 +1229,9 @@ dtls1_send_server_key_exchange(SSL *s)
 #endif /* !OPENSSL_NO_ECDH */
 #ifndef OPENSSL_NO_PSK
                if (type & SSL_kPSK) {
+                       pskhintlen = strlen(s->ctx->psk_identity_hint);
                        /* reserve size for record length and PSK identity hint*/
-                       n += 2 + strlen(s->ctx->psk_identity_hint);
+                       n += 2 + pskhintlen;
                } else
 #endif /* !OPENSSL_NO_PSK */
                {
@@ -1293,10 +1297,10 @@ dtls1_send_server_key_exchange(SSL *s)
 #ifndef OPENSSL_NO_PSK
                if (type & SSL_kPSK) {
                        /* copy PSK identity hint */
-                       s2n(strlen(s->ctx->psk_identity_hint), p);
+                       s2n(pskhintlen, p);
 
-                       strncpy((char *)p, s->ctx->psk_identity_hint, strlen(s->ctx->psk_identity_hint));
-                       p += strlen(s->ctx->psk_identity_hint);
+                       memcpy(p, s->ctx->psk_identity_hint, pskhintlen);
+                       p += pskhintlen;
                }
 #endif
 
index 0794a29..f532e25 100644 (file)
@@ -1573,6 +1573,9 @@ ssl3_send_server_key_exchange(SSL *s)
        int curve_id = 0;
        BN_CTX *bn_ctx = NULL;
 
+#endif
+#ifndef OPENSSL_NO_PSK
+       size_t pskhintlen;
 #endif
        EVP_PKEY *pkey;
        const EVP_MD *md = NULL;
@@ -1804,10 +1807,9 @@ ssl3_send_server_key_exchange(SSL *s)
 #endif /* !OPENSSL_NO_ECDH */
 #ifndef OPENSSL_NO_PSK
                if (type & SSL_kPSK) {
-                       /*
-                        * Reserve size for record length and PSK identity hint.
-                        */
-                       n += 2 + strlen(s->ctx->psk_identity_hint);
+                       pskhintlen = strlen(s->ctx->psk_identity_hint);
+                       /* reserve size for record length and PSK identity hint*/
+                       n += 2 + pskhintlen;
                } else
 #endif /* !OPENSSL_NO_PSK */
 #ifndef OPENSSL_NO_SRP
@@ -1900,11 +1902,10 @@ ssl3_send_server_key_exchange(SSL *s)
 #ifndef OPENSSL_NO_PSK
                if (type & SSL_kPSK) {
                        /* copy PSK identity hint */
-                       s2n(strlen(s->ctx->psk_identity_hint), p);
+                       s2n(pskhintlen, p);
 
-                       strncpy((char *)p, s->ctx->psk_identity_hint,
-                          strlen(s->ctx->psk_identity_hint));
-                       p += strlen(s->ctx->psk_identity_hint);
+                       memcpy(p, s->ctx->psk_identity_hint, pskhintlen);
+                       p += pskhintlen;
                }
 #endif
 
index 47a0c0e..6040dd9 100644 (file)
@@ -1017,6 +1017,9 @@ dtls1_send_server_key_exchange(SSL *s)
        int curve_id = 0;
        BN_CTX *bn_ctx = NULL;
 
+#endif
+#ifndef OPENSSL_NO_PSK
+       size_t pskhintlen;
 #endif
        EVP_PKEY *pkey;
        unsigned char *p, *d;
@@ -1226,8 +1229,9 @@ dtls1_send_server_key_exchange(SSL *s)
 #endif /* !OPENSSL_NO_ECDH */
 #ifndef OPENSSL_NO_PSK
                if (type & SSL_kPSK) {
+                       pskhintlen = strlen(s->ctx->psk_identity_hint);
                        /* reserve size for record length and PSK identity hint*/
-                       n += 2 + strlen(s->ctx->psk_identity_hint);
+                       n += 2 + pskhintlen;
                } else
 #endif /* !OPENSSL_NO_PSK */
                {
@@ -1293,10 +1297,10 @@ dtls1_send_server_key_exchange(SSL *s)
 #ifndef OPENSSL_NO_PSK
                if (type & SSL_kPSK) {
                        /* copy PSK identity hint */
-                       s2n(strlen(s->ctx->psk_identity_hint), p);
+                       s2n(pskhintlen, p);
 
-                       strncpy((char *)p, s->ctx->psk_identity_hint, strlen(s->ctx->psk_identity_hint));
-                       p += strlen(s->ctx->psk_identity_hint);
+                       memcpy(p, s->ctx->psk_identity_hint, pskhintlen);
+                       p += pskhintlen;
                }
 #endif
 
index 0794a29..f532e25 100644 (file)
@@ -1573,6 +1573,9 @@ ssl3_send_server_key_exchange(SSL *s)
        int curve_id = 0;
        BN_CTX *bn_ctx = NULL;
 
+#endif
+#ifndef OPENSSL_NO_PSK
+       size_t pskhintlen;
 #endif
        EVP_PKEY *pkey;
        const EVP_MD *md = NULL;
@@ -1804,10 +1807,9 @@ ssl3_send_server_key_exchange(SSL *s)
 #endif /* !OPENSSL_NO_ECDH */
 #ifndef OPENSSL_NO_PSK
                if (type & SSL_kPSK) {
-                       /*
-                        * Reserve size for record length and PSK identity hint.
-                        */
-                       n += 2 + strlen(s->ctx->psk_identity_hint);
+                       pskhintlen = strlen(s->ctx->psk_identity_hint);
+                       /* reserve size for record length and PSK identity hint*/
+                       n += 2 + pskhintlen;
                } else
 #endif /* !OPENSSL_NO_PSK */
 #ifndef OPENSSL_NO_SRP
@@ -1900,11 +1902,10 @@ ssl3_send_server_key_exchange(SSL *s)
 #ifndef OPENSSL_NO_PSK
                if (type & SSL_kPSK) {
                        /* copy PSK identity hint */
-                       s2n(strlen(s->ctx->psk_identity_hint), p);
+                       s2n(pskhintlen, p);
 
-                       strncpy((char *)p, s->ctx->psk_identity_hint,
-                          strlen(s->ctx->psk_identity_hint));
-                       p += strlen(s->ctx->psk_identity_hint);
+                       memcpy(p, s->ctx->psk_identity_hint, pskhintlen);
+                       p += pskhintlen;
                }
 #endif