diff options
author | Joel Sing <jsing@cvs.openbsd.org> | 2017-10-11 17:35:01 +0000 |
---|---|---|
committer | Joel Sing <jsing@cvs.openbsd.org> | 2017-10-11 17:35:01 +0000 |
commit | b0cd2fc7db02605b17c2e4e3ad5a7660203cbf64 (patch) | |
tree | e755e0642dd0b300a7f728478791bf89280ca95f | |
parent | 27197fbc88609b9e8eb53e7e8e02f54d7eaab885 (diff) |
Convert ssl3_client_hello() to CBB.
As part of this, change ssl_cipher_list_to_bytes() to take a CBB argument,
rather than a pointer/length. Some additional clean up/renames while here.
Based on a diff from doug@
-rw-r--r-- | lib/libssl/ssl_clnt.c | 81 | ||||
-rw-r--r-- | lib/libssl/ssl_lib.c | 43 | ||||
-rw-r--r-- | lib/libssl/ssl_locl.h | 8 | ||||
-rw-r--r-- | lib/libssl/t1_lib.c | 25 |
4 files changed, 64 insertions, 93 deletions
diff --git a/lib/libssl/ssl_clnt.c b/lib/libssl/ssl_clnt.c index 6343ec276d6..33352705d18 100644 --- a/lib/libssl/ssl_clnt.c +++ b/lib/libssl/ssl_clnt.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ssl_clnt.c,v 1.20 2017/10/10 15:42:32 jsing Exp $ */ +/* $OpenBSD: ssl_clnt.c,v 1.21 2017/10/11 17:35:00 jsing Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -170,6 +170,7 @@ #endif #include "bytestring.h" +#include "ssl_tlsext.h" static int ca_dn_cmp(const X509_NAME * const *a, const X509_NAME * const *b); @@ -662,12 +663,12 @@ end: int ssl3_client_hello(SSL *s) { - unsigned char *bufend, *p, *d; - uint16_t max_version; - size_t outlen; - int i; + CBB cbb, client_hello, session_id, cookie, cipher_suites; + CBB compression_methods; + uint16_t max_version; + size_t sl; - bufend = (unsigned char *)s->internal->init_buf->data + SSL3_RT_MAX_PLAIN_LENGTH; + memset(&cbb, 0, sizeof(cbb)); if (S3I(s)->hs.state == SSL3_ST_CW_CLNT_HELLO_A) { SSL_SESSION *sess = s->session; @@ -695,7 +696,9 @@ ssl3_client_hello(SSL *s) if (!SSL_IS_DTLS(s) || D1I(s)->send_cookie == 0) arc4random_buf(s->s3->client_random, SSL3_RANDOM_SIZE); - d = p = ssl3_handshake_msg_start(s, SSL3_MT_CLIENT_HELLO); + if (!ssl3_handshake_msg_start_cbb(s, &cbb, &client_hello, + SSL3_MT_CLIENT_HELLO)) + goto err; /* * Version indicates the negotiated version: for example from @@ -727,27 +730,27 @@ ssl3_client_hello(SSL *s) * client_version in client hello and not resetting it to * the negotiated version. */ - - *(p++) = s->client_version >> 8; - *(p++) = s->client_version & 0xff; + if (!CBB_add_u16(&client_hello, s->client_version)) + goto err; /* Random stuff */ - memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE); - p += SSL3_RANDOM_SIZE; + if (!CBB_add_bytes(&client_hello, s->s3->client_random, + sizeof(s->s3->client_random))) + goto err; /* Session ID */ - if (s->internal->new_session) - i = 0; - else - i = s->session->session_id_length; - *(p++) = i; - if (i != 0) { - if (i > (int)sizeof(s->session->session_id)) { + if (!CBB_add_u8_length_prefixed(&client_hello, &session_id)) + goto err; + if (!s->internal->new_session && + s->session->session_id_length > 0) { + sl = s->session->session_id_length; + if (sl > sizeof(s->session->session_id)) { SSLerror(s, ERR_R_INTERNAL_ERROR); goto err; } - memcpy(p, s->session->session_id, i); - p += i; + if (!CBB_add_bytes(&session_id, + s->session->session_id, sl)) + goto err; } /* DTLS Cookie. */ @@ -756,33 +759,37 @@ ssl3_client_hello(SSL *s) SSLerror(s, ERR_R_INTERNAL_ERROR); goto err; } - *(p++) = D1I(s)->cookie_len; - memcpy(p, D1I(s)->cookie, D1I(s)->cookie_len); - p += D1I(s)->cookie_len; + if (!CBB_add_u8_length_prefixed(&client_hello, &cookie)) + goto err; + if (!CBB_add_bytes(&cookie, D1I(s)->cookie, + D1I(s)->cookie_len)) + goto err; } /* Ciphers supported */ - if (!ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &p[2], - bufend - &p[2], &outlen)) - goto err; - if (outlen == 0) { + if (!CBB_add_u16_length_prefixed(&client_hello, &cipher_suites)) + return 0; + if (!ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), + &cipher_suites)) { SSLerror(s, SSL_R_NO_CIPHERS_AVAILABLE); goto err; } - s2n(outlen, p); - p += outlen; - /* add in (no) COMPRESSION */ - *(p++) = 1; - *(p++) = 0; /* Add the NULL method */ + /* Add in compression methods (null) */ + if (!CBB_add_u8_length_prefixed(&client_hello, + &compression_methods)) + goto err; + if (!CBB_add_u8(&compression_methods, 0)) + goto err; - /* TLS extensions*/ - if ((p = ssl_add_clienthello_tlsext(s, p, bufend)) == NULL) { + /* TLS extensions */ + if (!tlsext_clienthello_build(s, &client_hello)) { SSLerror(s, ERR_R_INTERNAL_ERROR); goto err; } - ssl3_handshake_msg_finish(s, p - d); + if (!ssl3_handshake_msg_finish_cbb(s, &cbb)) + goto err; S3I(s)->hs.state = SSL3_ST_CW_CLNT_HELLO_B; } @@ -791,6 +798,8 @@ ssl3_client_hello(SSL *s) return (ssl3_handshake_write(s)); err: + CBB_cleanup(&cbb); + return (-1); } diff --git a/lib/libssl/ssl_lib.c b/lib/libssl/ssl_lib.c index b91ba7f0f39..c7ae2a9631a 100644 --- a/lib/libssl/ssl_lib.c +++ b/lib/libssl/ssl_lib.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ssl_lib.c,v 1.171 2017/10/10 16:51:38 jsing Exp $ */ +/* $OpenBSD: ssl_lib.c,v 1.172 2017/10/11 17:35:00 jsing Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -1380,51 +1380,40 @@ SSL_get_shared_ciphers(const SSL *s, char *buf, int len) } int -ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, unsigned char *p, - size_t maxlen, size_t *outlen) +ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *ciphers, CBB *cbb) { SSL_CIPHER *cipher; - int ciphers = 0; - CBB cbb; + int num_ciphers = 0; int i; - *outlen = 0; - - if (sk == NULL) - return (0); - - if (!CBB_init_fixed(&cbb, p, maxlen)) - goto err; + if (ciphers == NULL) + return 0; - for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) { - cipher = sk_SSL_CIPHER_value(sk, i); + for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) { + if ((cipher = sk_SSL_CIPHER_value(ciphers, i)) == NULL) + return 0; /* Skip TLS v1.2 only ciphersuites if lower than v1.2 */ if ((cipher->algorithm_ssl & SSL_TLSV1_2) && (TLS1_get_client_version(s) < TLS1_2_VERSION)) continue; - if (!CBB_add_u16(&cbb, ssl3_cipher_get_value(cipher))) - goto err; + if (!CBB_add_u16(cbb, ssl3_cipher_get_value(cipher))) + return 0; - ciphers++; + num_ciphers++; } /* Add SCSV if there are other ciphers and we're not renegotiating. */ - if (ciphers > 0 && !s->internal->renegotiate) { - if (!CBB_add_u16(&cbb, SSL3_CK_SCSV & SSL3_CK_VALUE_MASK)) - goto err; + if (num_ciphers > 0 && !s->internal->renegotiate) { + if (!CBB_add_u16(cbb, SSL3_CK_SCSV & SSL3_CK_VALUE_MASK)) + return 0; } - if (!CBB_finish(&cbb, NULL, outlen)) - goto err; + if (!CBB_flush(cbb)) + return 0; return 1; - - err: - CBB_cleanup(&cbb); - - return 0; } STACK_OF(SSL_CIPHER) * diff --git a/lib/libssl/ssl_locl.h b/lib/libssl/ssl_locl.h index 2ce4b056000..92667ec125e 100644 --- a/lib/libssl/ssl_locl.h +++ b/lib/libssl/ssl_locl.h @@ -1,4 +1,4 @@ -/* $OpenBSD: ssl_locl.h,v 1.197 2017/10/11 16:51:39 jsing Exp $ */ +/* $OpenBSD: ssl_locl.h,v 1.198 2017/10/11 17:35:00 jsing Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -1064,9 +1064,8 @@ int ssl_cipher_id_cmp(const SSL_CIPHER *a, const SSL_CIPHER *b); SSL_CIPHER *OBJ_bsearch_ssl_cipher_id(SSL_CIPHER *key, SSL_CIPHER const *base, int num); int ssl_cipher_ptr_id_cmp(const SSL_CIPHER * const *ap, const SSL_CIPHER * const *bp); +int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *ciphers, CBB *cbb); STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, CBS *cbs); -int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, - unsigned char *p, size_t maxlen, size_t *outlen); STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(const SSL_METHOD *meth, STACK_OF(SSL_CIPHER) **pref, STACK_OF(SSL_CIPHER) **sorted, const char *rule_str); @@ -1286,9 +1285,6 @@ uint16_t tls1_ec_nid2curve_id(const int nid); int tls1_check_curve(SSL *s, const uint16_t curve_id); int tls1_get_shared_curve(SSL *s); -unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *p, - unsigned char *limit); - int ssl_parse_clienthello_tlsext(SSL *s, unsigned char **data, unsigned char *d, int n, int *al); int ssl_parse_serverhello_tlsext(SSL *s, unsigned char **data, diff --git a/lib/libssl/t1_lib.c b/lib/libssl/t1_lib.c index 8526ca167b9..1cef08d0946 100644 --- a/lib/libssl/t1_lib.c +++ b/lib/libssl/t1_lib.c @@ -1,4 +1,4 @@ -/* $OpenBSD: t1_lib.c,v 1.138 2017/10/11 16:51:39 jsing Exp $ */ +/* $OpenBSD: t1_lib.c,v 1.139 2017/10/11 17:35:00 jsing Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -661,29 +661,6 @@ tls12_get_req_sig_algs(SSL *s, unsigned char **sigalgs, size_t *sigalgs_len) *sigalgs_len = sizeof(tls12_sigalgs); } -unsigned char * -ssl_add_clienthello_tlsext(SSL *s, unsigned char *p, unsigned char *limit) -{ - size_t len; - CBB cbb; - - if (p >= limit) - return NULL; - - if (!CBB_init_fixed(&cbb, p, limit - p)) - return NULL; - if (!tlsext_clienthello_build(s, &cbb)) { - CBB_cleanup(&cbb); - return NULL; - } - if (!CBB_finish(&cbb, NULL, &len)) { - CBB_cleanup(&cbb); - return NULL; - } - - return (p + len); -} - int ssl_parse_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n, int *al) |