X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=ssl%2Fstatem%2Fextensions_srvr.c;h=076a635d24d0e27b2c6f1675c973315d30e577e3;hb=1ee4b98e695cd041da931c10fbdaf82f0ee0f268;hp=5a796cee5a27cf967359091b000c84d9908f4ab5;hpb=805a2e9e1388f21bd381a8c55e66bae2e6325667;p=oweals%2Fopenssl.git diff --git a/ssl/statem/extensions_srvr.c b/ssl/statem/extensions_srvr.c index 5a796cee5a..076a635d24 100644 --- a/ssl/statem/extensions_srvr.c +++ b/ssl/statem/extensions_srvr.c @@ -14,7 +14,8 @@ /* * Parse the client's renegotiation binding and abort if it's not right */ -int tls_parse_client_renegotiate(SSL *s, PACKET *pkt, int *al) +int tls_parse_ctos_renegotiate(SSL *s, PACKET *pkt, unsigned int context, + X509 *x, size_t chainidx, int *al) { unsigned int ilen; const unsigned char *data; @@ -22,7 +23,7 @@ int tls_parse_client_renegotiate(SSL *s, PACKET *pkt, int *al) /* Parse the length byte */ if (!PACKET_get_1(pkt, &ilen) || !PACKET_get_bytes(pkt, &data, ilen)) { - SSLerr(SSL_F_TLS_PARSE_CLIENT_RENEGOTIATE, + SSLerr(SSL_F_TLS_PARSE_CTOS_RENEGOTIATE, SSL_R_RENEGOTIATION_ENCODING_ERR); *al = SSL_AD_ILLEGAL_PARAMETER; return 0; @@ -30,7 +31,7 @@ int tls_parse_client_renegotiate(SSL *s, PACKET *pkt, int *al) /* Check that the extension matches */ if (ilen != s->s3->previous_client_finished_len) { - SSLerr(SSL_F_TLS_PARSE_CLIENT_RENEGOTIATE, + SSLerr(SSL_F_TLS_PARSE_CTOS_RENEGOTIATE, SSL_R_RENEGOTIATION_MISMATCH); *al = SSL_AD_HANDSHAKE_FAILURE; return 0; @@ -38,7 +39,7 @@ int tls_parse_client_renegotiate(SSL *s, PACKET *pkt, int *al) if (memcmp(data, s->s3->previous_client_finished, s->s3->previous_client_finished_len)) { - SSLerr(SSL_F_TLS_PARSE_CLIENT_RENEGOTIATE, + SSLerr(SSL_F_TLS_PARSE_CTOS_RENEGOTIATE, SSL_R_RENEGOTIATION_MISMATCH); *al = SSL_AD_HANDSHAKE_FAILURE; return 0; @@ -49,35 +50,35 @@ int tls_parse_client_renegotiate(SSL *s, PACKET *pkt, int *al) return 1; } -int tls_parse_client_server_name(SSL *s, PACKET *pkt, int *al) +/*- + * 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. + */ +int tls_parse_ctos_server_name(SSL *s, PACKET *pkt, unsigned int context, + X509 *x, size_t chainidx, int *al) { unsigned int servname_type; PACKET sni, hostname; - /*- - * 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. - * - */ if (!PACKET_as_length_prefixed_2(pkt, &sni) /* ServerNameList must be at least 1 byte long. */ || PACKET_remaining(&sni) == 0) { @@ -88,7 +89,7 @@ int tls_parse_client_server_name(SSL *s, PACKET *pkt, int *al) /* * Although the server_name extension was intended to be * extensible to new name types, RFC 4366 defined the - * syntax inextensibility and OpenSSL 1.0.x parses it as + * syntax inextensibly and OpenSSL 1.0.x parses it as * such. * RFC 6066 corrected the mistake but adding new name types * is nevertheless no longer feasible, so act as if no other @@ -115,7 +116,9 @@ int tls_parse_client_server_name(SSL *s, PACKET *pkt, int *al) return 0; } - if (!PACKET_strndup(&hostname, &s->session->tlsext_hostname)) { + OPENSSL_free(s->session->ext.hostname); + s->session->ext.hostname = NULL; + if (!PACKET_strndup(&hostname, &s->session->ext.hostname)) { *al = TLS1_AD_INTERNAL_ERROR; return 0; } @@ -126,16 +129,17 @@ int tls_parse_client_server_name(SSL *s, PACKET *pkt, int *al) * TODO(openssl-team): if the SNI doesn't match, we MUST * fall back to a full handshake. */ - s->servername_done = s->session->tlsext_hostname - && PACKET_equal(&hostname, s->session->tlsext_hostname, - strlen(s->session->tlsext_hostname)); + s->servername_done = s->session->ext.hostname + && PACKET_equal(&hostname, s->session->ext.hostname, + strlen(s->session->ext.hostname)); } return 1; } #ifndef OPENSSL_NO_SRP -int tls_parse_client_srp(SSL *s, PACKET *pkt, int *al) +int tls_parse_ctos_srp(SSL *s, PACKET *pkt, unsigned int context, X509 *x, + size_t chainidx, int *al) { PACKET srp_I; @@ -159,7 +163,8 @@ int tls_parse_client_srp(SSL *s, PACKET *pkt, int *al) #endif #ifndef OPENSSL_NO_EC -int tls_parse_client_ec_pt_formats(SSL *s, PACKET *pkt, int *al) +int tls_parse_ctos_ec_pt_formats(SSL *s, PACKET *pkt, unsigned int context, + X509 *x, size_t chainidx, int *al) { PACKET ec_point_format_list; @@ -171,8 +176,8 @@ int tls_parse_client_ec_pt_formats(SSL *s, PACKET *pkt, int *al) if (!s->hit) { if (!PACKET_memdup(&ec_point_format_list, - &s->session->tlsext_ecpointformatlist, - &s->session->tlsext_ecpointformatlist_length)) { + &s->session->ext.ecpointformats, + &s->session->ext.ecpointformats_len)) { *al = TLS1_AD_INTERNAL_ERROR; return 0; } @@ -182,12 +187,13 @@ int tls_parse_client_ec_pt_formats(SSL *s, PACKET *pkt, int *al) } #endif /* OPENSSL_NO_EC */ -int tls_parse_client_session_ticket(SSL *s, PACKET *pkt, int *al) +int tls_parse_ctos_session_ticket(SSL *s, PACKET *pkt, unsigned int context, + X509 *x, size_t chainidx, int *al) { - if (s->tls_session_ticket_ext_cb && - !s->tls_session_ticket_ext_cb(s, PACKET_data(pkt), - PACKET_remaining(pkt), - s->tls_session_ticket_ext_cb_arg)) { + if (s->ext.session_ticket_cb && + !s->ext.session_ticket_cb(s, PACKET_data(pkt), + PACKET_remaining(pkt), + s->ext.session_ticket_cb_arg)) { *al = TLS1_AD_INTERNAL_ERROR; return 0; } @@ -195,20 +201,19 @@ int tls_parse_client_session_ticket(SSL *s, PACKET *pkt, int *al) return 1; } -int tls_parse_client_sig_algs(SSL *s, PACKET *pkt, int *al) +int tls_parse_ctos_sig_algs(SSL *s, PACKET *pkt, unsigned int context, X509 *x, + size_t chainidx, int *al) { PACKET supported_sig_algs; if (!PACKET_as_length_prefixed_2(pkt, &supported_sig_algs) - || (PACKET_remaining(&supported_sig_algs) % 2) != 0 || PACKET_remaining(&supported_sig_algs) == 0) { *al = SSL_AD_DECODE_ERROR; return 0; } - if (!s->hit && !tls1_save_sigalgs(s, PACKET_data(&supported_sig_algs), - PACKET_remaining(&supported_sig_algs))) { - *al = TLS1_AD_INTERNAL_ERROR; + if (!s->hit && !tls1_save_sigalgs(s, &supported_sig_algs)) { + *al = TLS1_AD_DECODE_ERROR; return 0; } @@ -216,93 +221,98 @@ int tls_parse_client_sig_algs(SSL *s, PACKET *pkt, int *al) } #ifndef OPENSSL_NO_OCSP -int tls_parse_client_status_request(SSL *s, PACKET *pkt, int *al) +int tls_parse_ctos_status_request(SSL *s, PACKET *pkt, unsigned int context, + X509 *x, size_t chainidx, int *al) { - if (!PACKET_get_1(pkt, (unsigned int *)&s->tlsext_status_type)) { + PACKET responder_id_list, exts; + + /* Not defined if we get one of these in a client Certificate */ + if (x != NULL) + return 1; + + if (!PACKET_get_1(pkt, (unsigned int *)&s->ext.status_type)) { *al = SSL_AD_DECODE_ERROR; return 0; } - if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp) { - const unsigned char *ext_data; - PACKET responder_id_list, exts; - if (!PACKET_get_length_prefixed_2 (pkt, &responder_id_list)) { - *al = SSL_AD_DECODE_ERROR; - return 0; - } - + if (s->ext.status_type != TLSEXT_STATUSTYPE_ocsp) { /* - * We remove any OCSP_RESPIDs from a previous handshake - * to prevent unbounded memory growth - CVE-2016-6304 + * We don't know what to do with any other type so ignore it. */ - sk_OCSP_RESPID_pop_free(s->tlsext_ocsp_ids, OCSP_RESPID_free); - if (PACKET_remaining(&responder_id_list) > 0) { - s->tlsext_ocsp_ids = sk_OCSP_RESPID_new_null(); - if (s->tlsext_ocsp_ids == NULL) { - *al = SSL_AD_INTERNAL_ERROR; - return 0; - } - } else { - s->tlsext_ocsp_ids = NULL; - } + s->ext.status_type = TLSEXT_STATUSTYPE_nothing; + return 1; + } - while (PACKET_remaining(&responder_id_list) > 0) { - OCSP_RESPID *id; - PACKET responder_id; - const unsigned char *id_data; + if (!PACKET_get_length_prefixed_2 (pkt, &responder_id_list)) { + *al = SSL_AD_DECODE_ERROR; + return 0; + } - if (!PACKET_get_length_prefixed_2(&responder_id_list, - &responder_id) - || PACKET_remaining(&responder_id) == 0) { - *al = SSL_AD_DECODE_ERROR; - return 0; - } + /* + * We remove any OCSP_RESPIDs from a previous handshake + * to prevent unbounded memory growth - CVE-2016-6304 + */ + sk_OCSP_RESPID_pop_free(s->ext.ocsp.ids, OCSP_RESPID_free); + if (PACKET_remaining(&responder_id_list) > 0) { + s->ext.ocsp.ids = sk_OCSP_RESPID_new_null(); + if (s->ext.ocsp.ids == NULL) { + *al = SSL_AD_INTERNAL_ERROR; + return 0; + } + } else { + s->ext.ocsp.ids = NULL; + } - id_data = PACKET_data(&responder_id); - /* TODO(size_t): Convert d2i_* to size_t */ - id = d2i_OCSP_RESPID(NULL, &id_data, - (int)PACKET_remaining(&responder_id)); - if (id == NULL) { - *al = SSL_AD_DECODE_ERROR; - return 0; - } + while (PACKET_remaining(&responder_id_list) > 0) { + OCSP_RESPID *id; + PACKET responder_id; + const unsigned char *id_data; - if (id_data != PACKET_end(&responder_id)) { - OCSP_RESPID_free(id); - *al = SSL_AD_DECODE_ERROR; - return 0; - } + if (!PACKET_get_length_prefixed_2(&responder_id_list, &responder_id) + || PACKET_remaining(&responder_id) == 0) { + *al = SSL_AD_DECODE_ERROR; + return 0; + } - if (!sk_OCSP_RESPID_push(s->tlsext_ocsp_ids, id)) { - OCSP_RESPID_free(id); - *al = SSL_AD_INTERNAL_ERROR; - return 0; - } + id_data = PACKET_data(&responder_id); + /* TODO(size_t): Convert d2i_* to size_t */ + id = d2i_OCSP_RESPID(NULL, &id_data, + (int)PACKET_remaining(&responder_id)); + if (id == NULL) { + *al = SSL_AD_DECODE_ERROR; + return 0; } - /* Read in request_extensions */ - if (!PACKET_as_length_prefixed_2(pkt, &exts)) { + if (id_data != PACKET_end(&responder_id)) { + OCSP_RESPID_free(id); *al = SSL_AD_DECODE_ERROR; return 0; } - if (PACKET_remaining(&exts) > 0) { - ext_data = PACKET_data(&exts); - sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts, - X509_EXTENSION_free); - s->tlsext_ocsp_exts = - d2i_X509_EXTENSIONS(NULL, &ext_data, - (int)PACKET_remaining(&exts)); - if (s->tlsext_ocsp_exts == NULL || ext_data != PACKET_end(&exts)) { - *al = SSL_AD_DECODE_ERROR; - return 0; - } + if (!sk_OCSP_RESPID_push(s->ext.ocsp.ids, id)) { + OCSP_RESPID_free(id); + *al = SSL_AD_INTERNAL_ERROR; + return 0; + } + } + + /* Read in request_extensions */ + if (!PACKET_as_length_prefixed_2(pkt, &exts)) { + *al = SSL_AD_DECODE_ERROR; + return 0; + } + + if (PACKET_remaining(&exts) > 0) { + const unsigned char *ext_data = PACKET_data(&exts); + + sk_X509_EXTENSION_pop_free(s->ext.ocsp.exts, + X509_EXTENSION_free); + s->ext.ocsp.exts = + d2i_X509_EXTENSIONS(NULL, &ext_data, (int)PACKET_remaining(&exts)); + if (s->ext.ocsp.exts == NULL || ext_data != PACKET_end(&exts)) { + *al = SSL_AD_DECODE_ERROR; + return 0; } - } else { - /* - * We don't know what to do with any other type so ignore it. - */ - s->tlsext_status_type = -1; } return 1; @@ -310,44 +320,31 @@ int tls_parse_client_status_request(SSL *s, PACKET *pkt, int *al) #endif #ifndef OPENSSL_NO_NEXTPROTONEG -int tls_parse_client_npn(SSL *s, PACKET *pkt, int *al) +int tls_parse_ctos_npn(SSL *s, PACKET *pkt, unsigned int context, X509 *x, + size_t chainidx, int *al) { - if (s->s3->tmp.finish_md_len == 0) { - /*- - * We shouldn't accept this extension on a - * renegotiation. - * - * s->new_session will be set on renegotiation, but we - * probably shouldn't rely that it couldn't be set on - * the initial renegotiation too in certain cases (when - * 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.) - */ - s->s3->next_proto_neg_seen = 1; - } + /* + * We shouldn't accept this extension on a + * renegotiation. + */ + if (SSL_IS_FIRST_HANDSHAKE(s)) + s->s3->npn_seen = 1; return 1; } #endif /* - * Save the ALPN extension in a ClientHello. - * pkt: the contents of the ALPN extension, not including type and length. - * al: a pointer to the alert value to send in the event of a failure. - * returns: 1 on success, 0 on error. + * Save the ALPN extension in a ClientHello.|pkt| holds the contents of the ALPN + * extension, not including type and length. |al| is a pointer to the alert + * value to send in the event of a failure. Returns: 1 on success, 0 on error. */ -int tls_parse_client_alpn(SSL *s, PACKET *pkt, int *al) +int tls_parse_ctos_alpn(SSL *s, PACKET *pkt, unsigned int context, X509 *x, + size_t chainidx, int *al) { PACKET protocol_list, save_protocol_list, protocol; - if (s->s3->tmp.finish_md_len != 0) + if (!SSL_IS_FIRST_HANDSHAKE(s)) return 1; if (!PACKET_as_length_prefixed_2(pkt, &protocol_list) @@ -366,6 +363,9 @@ int tls_parse_client_alpn(SSL *s, PACKET *pkt, int *al) } } while (PACKET_remaining(&protocol_list) != 0); + OPENSSL_free(s->s3->alpn_proposed); + s->s3->alpn_proposed = NULL; + s->s3->alpn_proposed_len = 0; if (!PACKET_memdup(&save_protocol_list, &s->s3->alpn_proposed, &s->s3->alpn_proposed_len)) { *al = TLS1_AD_INTERNAL_ERROR; @@ -376,9 +376,9 @@ int tls_parse_client_alpn(SSL *s, PACKET *pkt, int *al) } #ifndef OPENSSL_NO_SRTP -int tls_parse_client_use_srtp(SSL *s, PACKET *pkt, int *al) +int tls_parse_ctos_use_srtp(SSL *s, PACKET *pkt, unsigned int context, X509 *x, + size_t chainidx, int *al) { - SRTP_PROTECTION_PROFILE *sprof; STACK_OF(SRTP_PROTECTION_PROFILE) *srvr; unsigned int ct, mki_len, id; int i, srtp_pref; @@ -389,9 +389,9 @@ int tls_parse_client_use_srtp(SSL *s, PACKET *pkt, int *al) return 1; /* Pull off the length of the cipher suite list and check it is even */ - if (!PACKET_get_net_2(pkt, &ct) - || (ct & 1) != 0 || !PACKET_get_sub_packet(pkt, &subpkt, ct)) { - SSLerr(SSL_F_TLS_PARSE_CLIENT_USE_SRTP, + if (!PACKET_get_net_2(pkt, &ct) || (ct & 1) != 0 + || !PACKET_get_sub_packet(pkt, &subpkt, ct)) { + SSLerr(SSL_F_TLS_PARSE_CTOS_USE_SRTP, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST); *al = SSL_AD_DECODE_ERROR; return 0; @@ -404,7 +404,7 @@ int tls_parse_client_use_srtp(SSL *s, PACKET *pkt, int *al) while (PACKET_remaining(&subpkt)) { if (!PACKET_get_net_2(&subpkt, &id)) { - SSLerr(SSL_F_TLS_PARSE_CLIENT_USE_SRTP, + SSLerr(SSL_F_TLS_PARSE_CTOS_USE_SRTP, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST); *al = SSL_AD_DECODE_ERROR; return 0; @@ -417,7 +417,9 @@ int tls_parse_client_use_srtp(SSL *s, PACKET *pkt, int *al) * does nothing. */ for (i = 0; i < srtp_pref; i++) { - sprof = sk_SRTP_PROTECTION_PROFILE_value(srvr, i); + SRTP_PROTECTION_PROFILE *sprof = + sk_SRTP_PROTECTION_PROFILE_value(srvr, i); + if (sprof->id == id) { s->srtp_profile = sprof; srtp_pref = i; @@ -426,11 +428,9 @@ int tls_parse_client_use_srtp(SSL *s, PACKET *pkt, int *al) } } - /* - * Now extract the MKI value as a sanity check, but discard it for now - */ + /* Now extract the MKI value as a sanity check, but discard it for now */ if (!PACKET_get_1(pkt, &mki_len)) { - SSLerr(SSL_F_TLS_PARSE_CLIENT_USE_SRTP, + SSLerr(SSL_F_TLS_PARSE_CTOS_USE_SRTP, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST); *al = SSL_AD_DECODE_ERROR; return 0; @@ -438,7 +438,7 @@ int tls_parse_client_use_srtp(SSL *s, PACKET *pkt, int *al) if (!PACKET_forward(pkt, mki_len) || PACKET_remaining(pkt)) { - SSLerr(SSL_F_TLS_PARSE_CLIENT_USE_SRTP, SSL_R_BAD_SRTP_MKI_VALUE); + SSLerr(SSL_F_TLS_PARSE_CTOS_USE_SRTP, SSL_R_BAD_SRTP_MKI_VALUE); *al = SSL_AD_DECODE_ERROR; return 0; } @@ -447,41 +447,42 @@ int tls_parse_client_use_srtp(SSL *s, PACKET *pkt, int *al) } #endif -int tls_parse_client_etm(SSL *s, PACKET *pkt, int *al) +int tls_parse_ctos_etm(SSL *s, PACKET *pkt, unsigned int context, X509 *x, + size_t chainidx, int *al) { if (!(s->options & SSL_OP_NO_ENCRYPT_THEN_MAC)) - s->s3->flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC; + s->ext.use_etm = 1; return 1; } /* - * Checks a list of |groups| to determine if the |group_id| is in it. If it is - * and |checkallow| is 1 then additionally check if the group is allowed to be - * used. Returns 1 if the group is in the list (and allowed if |checkallow| is - * 1) or 0 otherwise. + * Process a psk_kex_modes extension received in the ClientHello. |pkt| contains + * the raw PACKET data for the extension. Returns 1 on success or 0 on failure. + * If a failure occurs then |*al| is set to an appropriate alert value. */ -static int check_in_list(SSL *s, unsigned int group_id, - const unsigned char *groups, size_t num_groups, - int checkallow) +int tls_parse_ctos_psk_kex_modes(SSL *s, PACKET *pkt, unsigned int context, + X509 *x, size_t chainidx, int *al) { - size_t i; +#ifndef OPENSSL_NO_TLS1_3 + PACKET psk_kex_modes; + unsigned int mode; - if (groups == NULL || num_groups == 0) + if (!PACKET_as_length_prefixed_1(pkt, &psk_kex_modes) + || PACKET_remaining(&psk_kex_modes) == 0) { + *al = SSL_AD_DECODE_ERROR; return 0; + } - for (i = 0; i < num_groups; i++, groups += 2) { - unsigned int share_id = (groups[0] << 8) | (groups[1]); - - if (group_id == share_id - && (!checkallow || tls_curve_allowed(s, groups, - SSL_SECOP_CURVE_CHECK))) { - break; - } + while (PACKET_get_1(&psk_kex_modes, &mode)) { + if (mode == TLSEXT_KEX_MODE_KE_DHE) + s->ext.psk_kex_mode |= TLSEXT_KEX_MODE_FLAG_KE_DHE; + else if (mode == TLSEXT_KEX_MODE_KE) + s->ext.psk_kex_mode |= TLSEXT_KEX_MODE_FLAG_KE; } +#endif - /* If i == num_groups then not in the list */ - return i < num_groups; + return 1; } /* @@ -489,8 +490,10 @@ static int check_in_list(SSL *s, unsigned int group_id, * the raw PACKET data for the extension. Returns 1 on success or 0 on failure. * If a failure occurs then |*al| is set to an appropriate alert value. */ -int tls_parse_client_key_share(SSL *s, PACKET *pkt, int *al) +int tls_parse_ctos_key_share(SSL *s, PACKET *pkt, unsigned int context, X509 *x, + size_t chainidx, int *al) { +#ifndef OPENSSL_NO_TLS1_3 unsigned int group_id; PACKET key_share_list, encoded_pt; const unsigned char *clntcurves, *srvrcurves; @@ -498,33 +501,37 @@ int tls_parse_client_key_share(SSL *s, PACKET *pkt, int *al) int group_nid, found = 0; unsigned int curve_flags; - if (s->hit) + if (s->hit && (s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE_DHE) == 0) return 1; /* Sanity check */ if (s->s3->peer_tmp != NULL) { *al = SSL_AD_INTERNAL_ERROR; - SSLerr(SSL_F_TLS_PARSE_CLIENT_KEY_SHARE, ERR_R_INTERNAL_ERROR); + SSLerr(SSL_F_TLS_PARSE_CTOS_KEY_SHARE, ERR_R_INTERNAL_ERROR); return 0; } if (!PACKET_as_length_prefixed_2(pkt, &key_share_list)) { *al = SSL_AD_HANDSHAKE_FAILURE; - SSLerr(SSL_F_TLS_PARSE_CLIENT_KEY_SHARE, SSL_R_LENGTH_MISMATCH); + SSLerr(SSL_F_TLS_PARSE_CTOS_KEY_SHARE, SSL_R_LENGTH_MISMATCH); return 0; } /* Get our list of supported curves */ if (!tls1_get_curvelist(s, 0, &srvrcurves, &srvr_num_curves)) { *al = SSL_AD_INTERNAL_ERROR; - SSLerr(SSL_F_TLS_PARSE_CLIENT_KEY_SHARE, ERR_R_INTERNAL_ERROR); + SSLerr(SSL_F_TLS_PARSE_CTOS_KEY_SHARE, ERR_R_INTERNAL_ERROR); return 0; } - /* Get the clients list of supported curves */ + /* + * Get the clients list of supported curves. + * TODO(TLS1.3): We should validate that we actually received + * supported_groups! + */ if (!tls1_get_curvelist(s, 1, &clntcurves, &clnt_num_curves)) { *al = SSL_AD_INTERNAL_ERROR; - SSLerr(SSL_F_TLS_PARSE_CLIENT_KEY_SHARE, ERR_R_INTERNAL_ERROR); + SSLerr(SSL_F_TLS_PARSE_CTOS_KEY_SHARE, ERR_R_INTERNAL_ERROR); return 0; } @@ -533,7 +540,7 @@ int tls_parse_client_key_share(SSL *s, PACKET *pkt, int *al) || !PACKET_get_length_prefixed_2(&key_share_list, &encoded_pt) || PACKET_remaining(&encoded_pt) == 0) { *al = SSL_AD_HANDSHAKE_FAILURE; - SSLerr(SSL_F_TLS_PARSE_CLIENT_KEY_SHARE, + SSLerr(SSL_F_TLS_PARSE_CTOS_KEY_SHARE, SSL_R_LENGTH_MISMATCH); return 0; } @@ -548,7 +555,7 @@ int tls_parse_client_key_share(SSL *s, PACKET *pkt, int *al) /* Check if this share is in supported_groups sent from client */ if (!check_in_list(s, group_id, clntcurves, clnt_num_curves, 0)) { *al = SSL_AD_HANDSHAKE_FAILURE; - SSLerr(SSL_F_TLS_PARSE_CLIENT_KEY_SHARE, SSL_R_BAD_KEY_SHARE); + SSLerr(SSL_F_TLS_PARSE_CTOS_KEY_SHARE, SSL_R_BAD_KEY_SHARE); return 0; } @@ -562,7 +569,7 @@ int tls_parse_client_key_share(SSL *s, PACKET *pkt, int *al) if (group_nid == 0) { *al = SSL_AD_INTERNAL_ERROR; - SSLerr(SSL_F_TLS_PARSE_CLIENT_KEY_SHARE, + SSLerr(SSL_F_TLS_PARSE_CTOS_KEY_SHARE, SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS); return 0; } @@ -573,7 +580,7 @@ int tls_parse_client_key_share(SSL *s, PACKET *pkt, int *al) if (key == NULL || !EVP_PKEY_set_type(key, group_nid)) { *al = SSL_AD_INTERNAL_ERROR; - SSLerr(SSL_F_TLS_PARSE_CLIENT_KEY_SHARE, ERR_R_EVP_LIB); + SSLerr(SSL_F_TLS_PARSE_CTOS_KEY_SHARE, ERR_R_EVP_LIB); EVP_PKEY_free(key); return 0; } @@ -581,13 +588,14 @@ int tls_parse_client_key_share(SSL *s, PACKET *pkt, int *al) } else { /* Set up EVP_PKEY with named curve as parameters */ EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL); + if (pctx == NULL || EVP_PKEY_paramgen_init(pctx) <= 0 || EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, group_nid) <= 0 || EVP_PKEY_paramgen(pctx, &s->s3->peer_tmp) <= 0) { *al = SSL_AD_INTERNAL_ERROR; - SSLerr(SSL_F_TLS_PARSE_CLIENT_KEY_SHARE, ERR_R_EVP_LIB); + SSLerr(SSL_F_TLS_PARSE_CTOS_KEY_SHARE, ERR_R_EVP_LIB); EVP_PKEY_CTX_free(pctx); return 0; } @@ -600,18 +608,20 @@ int tls_parse_client_key_share(SSL *s, PACKET *pkt, int *al) PACKET_data(&encoded_pt), PACKET_remaining(&encoded_pt))) { *al = SSL_AD_DECODE_ERROR; - SSLerr(SSL_F_TLS_PARSE_CLIENT_KEY_SHARE, SSL_R_BAD_ECPOINT); + SSLerr(SSL_F_TLS_PARSE_CTOS_KEY_SHARE, SSL_R_BAD_ECPOINT); return 0; } found = 1; } +#endif return 1; } #ifndef OPENSSL_NO_EC -int tls_parse_client_supported_groups(SSL *s, PACKET *pkt, int *al) +int tls_parse_ctos_supported_groups(SSL *s, PACKET *pkt, unsigned int context, + X509 *x, size_t chainidx, int *al) { PACKET supported_groups_list; @@ -623,10 +633,12 @@ int tls_parse_client_supported_groups(SSL *s, PACKET *pkt, int *al) return 0; } - if (!s->hit - && !PACKET_memdup(&supported_groups_list, - &s->session->tlsext_supportedgroupslist, - &s->session->tlsext_supportedgroupslist_length)) { + OPENSSL_free(s->session->ext.supportedgroups); + s->session->ext.supportedgroups = NULL; + s->session->ext.supportedgroups_len = 0; + if (!PACKET_memdup(&supported_groups_list, + &s->session->ext.supportedgroups, + &s->session->ext.supportedgroups_len)) { *al = SSL_AD_DECODE_ERROR; return 0; } @@ -635,7 +647,8 @@ int tls_parse_client_supported_groups(SSL *s, PACKET *pkt, int *al) } #endif -int tls_parse_client_ems(SSL *s, PACKET *pkt, int *al) +int tls_parse_ctos_ems(SSL *s, PACKET *pkt, unsigned int context, X509 *x, + size_t chainidx, int *al) { /* The extension must always be empty */ if (PACKET_remaining(pkt) != 0) { @@ -648,121 +661,150 @@ int tls_parse_client_ems(SSL *s, PACKET *pkt, int *al) return 1; } -/* - * Process all remaining ClientHello extensions that we collected earlier and - * haven't already processed. - * - * Behaviour upon resumption is extension-specific. If the extension has no - * effect during resumption, it is parsed (to verify its format) but otherwise - * ignored. Returns 1 on success and 0 on failure. Upon failure, sets |al| to - * the appropriate alert. - */ -int tls_scan_clienthello_tlsext(SSL *s, CLIENTHELLO_MSG *hello, int *al) -{ - /* - * We process the supported_groups extension first so that is done before - * we get to key_share which needs to use the information in it. - */ - if (!tls_parse_extension(s, TLSEXT_TYPE_supported_groups, EXT_CLIENT_HELLO, - hello->pre_proc_exts, hello->num_extensions, al)) { - return 0; - } - return tls_parse_all_extensions(s, EXT_CLIENT_HELLO, hello->pre_proc_exts, - hello->num_extensions, al); -} - -/* - * Process the ALPN extension in a ClientHello. - * al: a pointer to the alert value to send in the event of a failure. - * returns 1 on success, 0 on error. - */ -static int tls1_alpn_handle_client_hello_late(SSL *s, int *al) +int tls_parse_ctos_early_data(SSL *s, PACKET *pkt, unsigned int context, + X509 *x, size_t chainidx, int *al) { - const unsigned char *selected = NULL; - unsigned char selected_len = 0; - - if (s->ctx->alpn_select_cb != NULL && s->s3->alpn_proposed != NULL) { - int r = s->ctx->alpn_select_cb(s, &selected, &selected_len, - s->s3->alpn_proposed, - (unsigned int)s->s3->alpn_proposed_len, - s->ctx->alpn_select_cb_arg); - - if (r == SSL_TLSEXT_ERR_OK) { - OPENSSL_free(s->s3->alpn_selected); - s->s3->alpn_selected = OPENSSL_memdup(selected, selected_len); - if (s->s3->alpn_selected == NULL) { - *al = SSL_AD_INTERNAL_ERROR; - return 0; - } - s->s3->alpn_selected_len = selected_len; -#ifndef OPENSSL_NO_NEXTPROTONEG - /* ALPN takes precedence over NPN. */ - s->s3->next_proto_neg_seen = 0; -#endif - } else { - *al = SSL_AD_NO_APPLICATION_PROTOCOL; - return 0; - } + if (PACKET_remaining(pkt) != 0) { + *al = SSL_AD_DECODE_ERROR; + return 0; } return 1; } -/* - * Upon success, returns 1. - * Upon failure, returns 0 and sets |al| to the appropriate fatal alert. - */ -int ssl_check_clienthello_tlsext_late(SSL *s, int *al) +int tls_parse_ctos_psk(SSL *s, PACKET *pkt, unsigned int context, X509 *x, + size_t chainidx, int *al) { - s->tlsext_status_expected = 0; + PACKET identities, binders, binder; + size_t binderoffset, hashsize; + SSL_SESSION *sess = NULL; + unsigned int id, i; + const EVP_MD *md = NULL; + uint32_t ticket_age = 0, now, agesec, agems; /* - * If status request then ask callback what to do. Note: this must be - * called after servername callbacks in case the certificate has changed, - * and must be called after the cipher has been chosen because this may - * influence which certificate is sent + * If we have no PSK kex mode that we recognise then we can't resume so + * ignore this extension */ - if ((s->tlsext_status_type != -1) && s->ctx && s->ctx->tlsext_status_cb) { + if ((s->ext.psk_kex_mode + & (TLSEXT_KEX_MODE_FLAG_KE | TLSEXT_KEX_MODE_FLAG_KE_DHE)) == 0) + return 1; + + if (!PACKET_get_length_prefixed_2(pkt, &identities)) { + *al = SSL_AD_DECODE_ERROR; + return 0; + } + + for (id = 0; PACKET_remaining(&identities) != 0; id++) { + PACKET identity; + unsigned long ticket_agel; int ret; - CERT_PKEY *certpkey; - certpkey = ssl_get_server_send_pkey(s); - /* If no certificate can't return certificate status */ - if (certpkey != NULL) { + + if (!PACKET_get_length_prefixed_2(&identities, &identity) + || !PACKET_get_net_4(&identities, &ticket_agel)) { + *al = SSL_AD_DECODE_ERROR; + return 0; + } + + ticket_age = (uint32_t)ticket_agel; + + ret = tls_decrypt_ticket(s, PACKET_data(&identity), + PACKET_remaining(&identity), NULL, 0, &sess); + if (ret == TICKET_FATAL_ERR_MALLOC || ret == TICKET_FATAL_ERR_OTHER) { + *al = SSL_AD_INTERNAL_ERROR; + return 0; + } + if (ret == TICKET_NO_DECRYPT) + continue; + + md = ssl_md(sess->cipher->algorithm2); + if (md == NULL) { /* - * Set current certificate to one we will use so SSL_get_certificate - * et al can pick it up. + * Don't recognise this cipher so we can't use the session. + * Ignore it */ - s->cert->key = certpkey; - ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg); - switch (ret) { - /* We don't want to send a status request response */ - case SSL_TLSEXT_ERR_NOACK: - s->tlsext_status_expected = 0; - break; - /* status request response should be sent */ - case SSL_TLSEXT_ERR_OK: - if (s->tlsext_ocsp_resp) - s->tlsext_status_expected = 1; - break; - /* something bad happened */ - case SSL_TLSEXT_ERR_ALERT_FATAL: - default: - *al = SSL_AD_INTERNAL_ERROR; - return 0; - } + SSL_SESSION_free(sess); + sess = NULL; + continue; } + + /* + * TODO(TLS1.3): Somehow we need to handle the case of a ticket renewal. + * Ignored for now + */ + + break; } - if (!tls1_alpn_handle_client_hello_late(s, al)) { - return 0; + if (sess == NULL) + return 1; + + binderoffset = PACKET_data(pkt) - (const unsigned char *)s->init_buf->data; + hashsize = EVP_MD_size(md); + + if (!PACKET_get_length_prefixed_2(pkt, &binders)) { + *al = SSL_AD_DECODE_ERROR; + goto err; + } + + for (i = 0; i <= id; i++) { + if (!PACKET_get_length_prefixed_1(&binders, &binder)) { + *al = SSL_AD_DECODE_ERROR; + goto err; + } + } + + if (PACKET_remaining(&binder) != hashsize + || tls_psk_do_binder(s, md, + (const unsigned char *)s->init_buf->data, + binderoffset, PACKET_data(&binder), NULL, + sess, 0) != 1) { + *al = SSL_AD_DECODE_ERROR; + SSLerr(SSL_F_TLS_PARSE_CTOS_PSK, ERR_R_INTERNAL_ERROR); + goto err; } + sess->ext.tick_identity = id; + + now = (uint32_t)time(NULL); + agesec = now - (uint32_t)sess->time; + agems = agesec * (uint32_t)1000; + ticket_age -= sess->ext.tick_age_add; + + + /* + * For simplicity we do our age calculations in seconds. If the client does + * it in ms then it could appear that their ticket age is longer than ours + * (our ticket age calculation should always be slightly longer than the + * client's due to the network latency). Therefore we add 1000ms to our age + * calculation to adjust for rounding errors. + */ + if (sess->timeout >= agesec + && agems / (uint32_t)1000 == agesec + && ticket_age <= agems + 1000 + && ticket_age + TICKET_AGE_ALLOWANCE >= agems + 1000) { + /* + * Ticket age is within tolerance and not expired. We allow it for early + * data + */ + s->ext.early_data_ok = 1; + } + + + SSL_SESSION_free(s->session); + s->session = sess; return 1; +err: + SSL_SESSION_free(sess); + return 0; } -/* Add the server's renegotiation binding */ -int tls_construct_server_renegotiate(SSL *s, WPACKET *pkt, int *al) +/* + * Add the server's renegotiation binding + */ +int tls_construct_stoc_renegotiate(SSL *s, WPACKET *pkt, unsigned int context, + X509 *x, size_t chainidx, int *al) { if (!s->s3->send_connection_binding) return 1; @@ -776,22 +818,23 @@ int tls_construct_server_renegotiate(SSL *s, WPACKET *pkt, int *al) s->s3->previous_server_finished_len) || !WPACKET_close(pkt) || !WPACKET_close(pkt)) { - SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_RENEGOTIATE, ERR_R_INTERNAL_ERROR); + SSLerr(SSL_F_TLS_CONSTRUCT_STOC_RENEGOTIATE, ERR_R_INTERNAL_ERROR); return 0; } return 1; } -int tls_construct_server_server_name(SSL *s, WPACKET *pkt, int *al) +int tls_construct_stoc_server_name(SSL *s, WPACKET *pkt, unsigned int context, + X509 *x, size_t chainidx, int *al) { if (s->hit || s->servername_done != 1 - || s->session->tlsext_hostname == NULL) + || s->session->ext.hostname == NULL) return 1; if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_name) || !WPACKET_put_bytes_u16(pkt, 0)) { - SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_SERVER_NAME, ERR_R_INTERNAL_ERROR); + SSLerr(SSL_F_TLS_CONSTRUCT_STOC_SERVER_NAME, ERR_R_INTERNAL_ERROR); return 0; } @@ -799,12 +842,13 @@ int tls_construct_server_server_name(SSL *s, WPACKET *pkt, int *al) } #ifndef OPENSSL_NO_EC -int tls_construct_server_ec_pt_formats(SSL *s, WPACKET *pkt, int *al) +int tls_construct_stoc_ec_pt_formats(SSL *s, WPACKET *pkt, unsigned int context, + X509 *x, size_t chainidx, int *al) { unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey; unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth; - int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA); - using_ecc = using_ecc && (s->session->tlsext_ecpointformatlist != NULL); + int using_ecc = ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) + && (s->session->ext.ecpointformats != NULL); const unsigned char *plist; size_t plistlen; @@ -812,12 +856,11 @@ int tls_construct_server_ec_pt_formats(SSL *s, WPACKET *pkt, int *al) return 1; tls1_get_formatlist(s, &plist, &plistlen); - if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_ec_point_formats) || !WPACKET_start_sub_packet_u16(pkt) || !WPACKET_sub_memcpy_u8(pkt, plist, plistlen) || !WPACKET_close(pkt)) { - SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_EC_PT_FORMATS, ERR_R_INTERNAL_ERROR); + SSLerr(SSL_F_TLS_CONSTRUCT_STOC_EC_PT_FORMATS, ERR_R_INTERNAL_ERROR); return 0; } @@ -825,16 +868,18 @@ int tls_construct_server_ec_pt_formats(SSL *s, WPACKET *pkt, int *al) } #endif -int tls_construct_server_session_ticket(SSL *s, WPACKET *pkt, int *al) +int tls_construct_stoc_session_ticket(SSL *s, WPACKET *pkt, + unsigned int context, X509 *x, + size_t chainidx, int *al) { - if (!s->tlsext_ticket_expected || !tls_use_ticket(s)) { - s->tlsext_ticket_expected = 0; + if (!s->ext.ticket_expected || !tls_use_ticket(s)) { + s->ext.ticket_expected = 0; return 1; } if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_session_ticket) || !WPACKET_put_bytes_u16(pkt, 0)) { - SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_SESSION_TICKET, ERR_R_INTERNAL_ERROR); + SSLerr(SSL_F_TLS_CONSTRUCT_STOC_SESSION_TICKET, ERR_R_INTERNAL_ERROR); return 0; } @@ -842,14 +887,30 @@ int tls_construct_server_session_ticket(SSL *s, WPACKET *pkt, int *al) } #ifndef OPENSSL_NO_OCSP -int tls_construct_server_status_request(SSL *s, WPACKET *pkt, int *al) +int tls_construct_stoc_status_request(SSL *s, WPACKET *pkt, + unsigned int context, X509 *x, + size_t chainidx, int *al) { - if (!s->tlsext_status_expected) + if (!s->ext.status_expected) + return 1; + + if (SSL_IS_TLS13(s) && chainidx != 0) return 1; if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_status_request) - || !WPACKET_put_bytes_u16(pkt, 0)) { - SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_STATUS_REQUEST, ERR_R_INTERNAL_ERROR); + || !WPACKET_start_sub_packet_u16(pkt)) { + SSLerr(SSL_F_TLS_CONSTRUCT_STOC_STATUS_REQUEST, ERR_R_INTERNAL_ERROR); + return 0; + } + + /* + * In TLSv1.3 we include the certificate status itself. In <= TLSv1.2 we + * send back an empty extension, with the certificate status appearing as a + * separate message + */ + if ((SSL_IS_TLS13(s) && !tls_construct_cert_status_body(s, pkt)) + || !WPACKET_close(pkt)) { + SSLerr(SSL_F_TLS_CONSTRUCT_STOC_STATUS_REQUEST, ERR_R_INTERNAL_ERROR); return 0; } @@ -857,36 +918,38 @@ int tls_construct_server_status_request(SSL *s, WPACKET *pkt, int *al) } #endif - #ifndef OPENSSL_NO_NEXTPROTONEG -int tls_construct_server_next_proto_neg(SSL *s, WPACKET *pkt, int *al) +int tls_construct_stoc_next_proto_neg(SSL *s, WPACKET *pkt, + unsigned int context, X509 *x, + size_t chainidx, int *al) { const unsigned char *npa; unsigned int npalen; int ret; - int next_proto_neg_seen = s->s3->next_proto_neg_seen; + int npn_seen = s->s3->npn_seen; - s->s3->next_proto_neg_seen = 0; - if (!next_proto_neg_seen || s->ctx->next_protos_advertised_cb == NULL) + s->s3->npn_seen = 0; + if (!npn_seen || s->ctx->ext.npn_advertised_cb == NULL) return 1; - ret = s->ctx->next_protos_advertised_cb(s, &npa, &npalen, - s->ctx->next_protos_advertised_cb_arg); + ret = s->ctx->ext.npn_advertised_cb(s, &npa, &npalen, + s->ctx->ext.npn_advertised_cb_arg); if (ret == SSL_TLSEXT_ERR_OK) { if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_next_proto_neg) || !WPACKET_sub_memcpy_u16(pkt, npa, npalen)) { - SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_NEXT_PROTO_NEG, + SSLerr(SSL_F_TLS_CONSTRUCT_STOC_NEXT_PROTO_NEG, ERR_R_INTERNAL_ERROR); return 0; } - s->s3->next_proto_neg_seen = 1; + s->s3->npn_seen = 1; } return 1; } #endif -int tls_construct_server_alpn(SSL *s, WPACKET *pkt, int *al) +int tls_construct_stoc_alpn(SSL *s, WPACKET *pkt, unsigned int context, X509 *x, + size_t chainidx, int *al) { if (s->s3->alpn_selected == NULL) return 1; @@ -899,7 +962,7 @@ int tls_construct_server_alpn(SSL *s, WPACKET *pkt, int *al) s->s3->alpn_selected_len) || !WPACKET_close(pkt) || !WPACKET_close(pkt)) { - SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_ALPN, ERR_R_INTERNAL_ERROR); + SSLerr(SSL_F_TLS_CONSTRUCT_STOC_ALPN, ERR_R_INTERNAL_ERROR); return 0; } @@ -907,18 +970,19 @@ int tls_construct_server_alpn(SSL *s, WPACKET *pkt, int *al) } #ifndef OPENSSL_NO_SRTP -int tls_construct_server_use_srtp(SSL *s, WPACKET *pkt, int *al) +int tls_construct_stoc_use_srtp(SSL *s, WPACKET *pkt, unsigned int context, + X509 *x, size_t chainidx, int *al) { if (s->srtp_profile == NULL) return 1; - + if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_use_srtp) || !WPACKET_start_sub_packet_u16(pkt) || !WPACKET_put_bytes_u16(pkt, 2) || !WPACKET_put_bytes_u16(pkt, s->srtp_profile->id) || !WPACKET_put_bytes_u8(pkt, 0) || !WPACKET_close(pkt)) { - SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_USE_SRTP, ERR_R_INTERNAL_ERROR); + SSLerr(SSL_F_TLS_CONSTRUCT_STOC_USE_SRTP, ERR_R_INTERNAL_ERROR); return 0; } @@ -926,9 +990,10 @@ int tls_construct_server_use_srtp(SSL *s, WPACKET *pkt, int *al) } #endif -int tls_construct_server_etm(SSL *s, WPACKET *pkt, int *al) +int tls_construct_stoc_etm(SSL *s, WPACKET *pkt, unsigned int context, X509 *x, + size_t chainidx, int *al) { - if ((s->s3->flags & TLS1_FLAGS_ENCRYPT_THEN_MAC) == 0) + if (!s->ext.use_etm) return 1; /* @@ -939,71 +1004,90 @@ int tls_construct_server_etm(SSL *s, WPACKET *pkt, int *al) || s->s3->tmp.new_cipher->algorithm_enc == SSL_RC4 || s->s3->tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT || s->s3->tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT12) { - s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC; + s->ext.use_etm = 0; return 1; } if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_encrypt_then_mac) || !WPACKET_put_bytes_u16(pkt, 0)) { - SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_ETM, ERR_R_INTERNAL_ERROR); + SSLerr(SSL_F_TLS_CONSTRUCT_STOC_ETM, ERR_R_INTERNAL_ERROR); return 0; } return 1; } -int tls_construct_server_ems(SSL *s, WPACKET *pkt, int *al) +int tls_construct_stoc_ems(SSL *s, WPACKET *pkt, unsigned int context, X509 *x, + size_t chainidx, int *al) { if ((s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS) == 0) return 1; if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_extended_master_secret) || !WPACKET_put_bytes_u16(pkt, 0)) { - SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_EMS, ERR_R_INTERNAL_ERROR); + SSLerr(SSL_F_TLS_CONSTRUCT_STOC_EMS, ERR_R_INTERNAL_ERROR); return 0; } return 1; } -int tls_construct_server_key_share(SSL *s, WPACKET *pkt, int *al) +int tls_construct_stoc_key_share(SSL *s, WPACKET *pkt, unsigned int context, + X509 *x, size_t chainidx, int *al) { +#ifndef OPENSSL_NO_TLS1_3 unsigned char *encodedPoint; size_t encoded_pt_len = 0; EVP_PKEY *ckey = s->s3->peer_tmp, *skey = NULL; - if (s->hit) - return 1; - if (ckey == NULL) { - SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_SHARE, ERR_R_INTERNAL_ERROR); - return 0; + /* No key_share received from client */ + if (s->hello_retry_request) { + if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_key_share) + || !WPACKET_start_sub_packet_u16(pkt) + || !WPACKET_put_bytes_u16(pkt, s->s3->group_id) + || !WPACKET_close(pkt)) { + SSLerr(SSL_F_TLS_CONSTRUCT_STOC_KEY_SHARE, + ERR_R_INTERNAL_ERROR); + return 0; + } + + return 1; + } + + /* Must be resuming. */ + if (!s->hit || !tls13_generate_handshake_secret(s, NULL, 0)) { + *al = SSL_AD_INTERNAL_ERROR; + SSLerr(SSL_F_TLS_CONSTRUCT_STOC_KEY_SHARE, ERR_R_INTERNAL_ERROR); + return 0; + } + return 1; } if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_key_share) || !WPACKET_start_sub_packet_u16(pkt) || !WPACKET_put_bytes_u16(pkt, s->s3->group_id)) { - SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_SHARE, ERR_R_INTERNAL_ERROR); + SSLerr(SSL_F_TLS_CONSTRUCT_STOC_KEY_SHARE, ERR_R_INTERNAL_ERROR); return 0; } skey = ssl_generate_pkey(ckey); if (skey == NULL) { - SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_SHARE, ERR_R_MALLOC_FAILURE); + SSLerr(SSL_F_TLS_CONSTRUCT_STOC_KEY_SHARE, ERR_R_MALLOC_FAILURE); return 0; } /* Generate encoding of server key */ encoded_pt_len = EVP_PKEY_get1_tls_encodedpoint(skey, &encodedPoint); if (encoded_pt_len == 0) { - SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_SHARE, ERR_R_EC_LIB); + SSLerr(SSL_F_TLS_CONSTRUCT_STOC_KEY_SHARE, ERR_R_EC_LIB); EVP_PKEY_free(skey); return 0; } if (!WPACKET_sub_memcpy_u16(pkt, encodedPoint, encoded_pt_len) || !WPACKET_close(pkt)) { - SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_SHARE, ERR_R_INTERNAL_ERROR); + SSLerr(SSL_F_TLS_CONSTRUCT_STOC_KEY_SHARE, ERR_R_INTERNAL_ERROR); EVP_PKEY_free(skey); OPENSSL_free(encodedPoint); return 0; @@ -1013,14 +1097,16 @@ int tls_construct_server_key_share(SSL *s, WPACKET *pkt, int *al) /* This causes the crypto state to be updated based on the derived keys */ s->s3->tmp.pkey = skey; if (ssl_derive(s, skey, ckey, 1) == 0) { - SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_SHARE, ERR_R_INTERNAL_ERROR); + SSLerr(SSL_F_TLS_CONSTRUCT_STOC_KEY_SHARE, ERR_R_INTERNAL_ERROR); return 0; } +#endif return 1; } -int tls_construct_server_cryptopro_bug(SSL *s, WPACKET *pkt, int *al) +int tls_construct_stoc_cryptopro_bug(SSL *s, WPACKET *pkt, unsigned int context, + X509 *x, size_t chainidx, int *al) { const unsigned char cryptopro_ext[36] = { 0xfd, 0xe8, /* 65000 */ @@ -1037,7 +1123,55 @@ int tls_construct_server_cryptopro_bug(SSL *s, WPACKET *pkt, int *al) return 1; if (!WPACKET_memcpy(pkt, cryptopro_ext, sizeof(cryptopro_ext))) { - SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_CRYPTOPRO_BUG, ERR_R_INTERNAL_ERROR); + SSLerr(SSL_F_TLS_CONSTRUCT_STOC_CRYPTOPRO_BUG, ERR_R_INTERNAL_ERROR); + return 0; + } + + return 1; +} + +int tls_construct_stoc_early_data(SSL *s, WPACKET *pkt, unsigned int context, + X509 *x, size_t chainidx, int *al) +{ + if (context == EXT_TLS1_3_NEW_SESSION_TICKET) { + if (s->max_early_data == 0) + return 1; + + if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_early_data) + || !WPACKET_start_sub_packet_u16(pkt) + || !WPACKET_put_bytes_u32(pkt, s->max_early_data) + || !WPACKET_close(pkt)) { + SSLerr(SSL_F_TLS_CONSTRUCT_STOC_EARLY_DATA, ERR_R_INTERNAL_ERROR); + return 0; + } + + return 1; + } + + if (s->ext.early_data != SSL_EARLY_DATA_ACCEPTED) + return 1; + + if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_early_data) + || !WPACKET_start_sub_packet_u16(pkt) + || !WPACKET_close(pkt)) { + SSLerr(SSL_F_TLS_CONSTRUCT_STOC_EARLY_DATA, ERR_R_INTERNAL_ERROR); + return 0; + } + + return 1; +} + +int tls_construct_stoc_psk(SSL *s, WPACKET *pkt, unsigned int context, X509 *x, + size_t chainidx, int *al) +{ + if (!s->hit) + return 1; + + if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_psk) + || !WPACKET_start_sub_packet_u16(pkt) + || !WPACKET_put_bytes_u16(pkt, s->session->ext.tick_identity) + || !WPACKET_close(pkt)) { + SSLerr(SSL_F_TLS_CONSTRUCT_STOC_PSK, ERR_R_INTERNAL_ERROR); return 0; }