blob: 46c071b6bb76b810fb76b31fcf7e87cca81bcda1 [file] [log] [blame]
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08001/*
2 * TLS 1.3 client-side functions
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS ( https://tls.mbed.org )
20 */
21
22#include "common.h"
23
24#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
25
26#if defined(MBEDTLS_SSL_CLI_C)
27
28#include "ssl_misc.h"
Jerry Yua13c7e72021-08-17 10:44:40 +080029#include <mbedtls/debug.h>
30
Jerry Yu65dd2cc2021-08-18 16:38:40 +080031/* Main entry point; orchestrates the other functions */
Jerry Yua13c7e72021-08-17 10:44:40 +080032static int ssl_client_hello_process( mbedtls_ssl_context* ssl );
Jerry Yu3cc4c2a2021-08-06 16:29:08 +080033
Jerry Yub9930e72021-08-06 17:11:51 +080034int mbedtls_ssl_handshake_client_step_tls1_3( mbedtls_ssl_context *ssl )
35{
Jerry Yua13c7e72021-08-17 10:44:40 +080036 int ret = 0;
37
38 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL )
39 {
40 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Handshake completed but ssl->handshake is NULL.\n" ) );
41 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
42 }
43
44 MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) );
45
46 switch( ssl->state )
47 {
48 case MBEDTLS_SSL_HELLO_REQUEST:
49 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO );
50 break;
51
52 case MBEDTLS_SSL_CLIENT_HELLO:
53 ret = ssl_client_hello_process( ssl );
54 break;
55
56 case MBEDTLS_SSL_SERVER_HELLO:
57 // Stop here : we haven't finished whole flow
58 ret=MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
59 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
60 break;
61
62 default:
63 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
64 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
65 }
66
67 return( ret );
68}
69
Jerry Yu65dd2cc2021-08-18 16:38:40 +080070
71static int ssl_client_hello_prepare( mbedtls_ssl_context* ssl );
72static int ssl_client_hello_write_partial( mbedtls_ssl_context* ssl,
73 unsigned char* buf, size_t buflen,
74 size_t* len_without_binders,
75 size_t* len_with_binders );
76static int ssl_client_hello_postprocess( mbedtls_ssl_context* ssl );
77
Jerry Yua13c7e72021-08-17 10:44:40 +080078static int ssl_client_hello_process( mbedtls_ssl_context* ssl )
79{
80 int ret = 0;
Jerry Yu65dd2cc2021-08-18 16:38:40 +080081 unsigned char *buf;
82 size_t buf_len, msg_len;
83 size_t len_without_binders = 0;
Jerry Yua13c7e72021-08-17 10:44:40 +080084
85 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
86
Jerry Yu65dd2cc2021-08-18 16:38:40 +080087 MBEDTLS_SSL_PROC_CHK( ssl_client_hello_prepare, ( ssl ) );
88
89 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg, ( ssl,
90 MBEDTLS_SSL_HS_CLIENT_HELLO, &buf, &buf_len ) );
91
92 MBEDTLS_SSL_PROC_CHK( ssl_client_hello_write_partial, ( ssl, buf, buf_len,
93 &len_without_binders,
94 &msg_len ) );
95
96 mbedtls_ssl_add_hs_hdr_to_checksum( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
97 msg_len );
98 ssl->handshake->update_checksum( ssl, buf, len_without_binders );
99
100 MBEDTLS_SSL_PROC_CHK( ssl_client_hello_postprocess, ( ssl ) );
101 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg, ( ssl, buf_len, msg_len ) );
Jerry Yua13c7e72021-08-17 10:44:40 +0800102 mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
103
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800104cleanup:
105
Jerry Yua13c7e72021-08-17 10:44:40 +0800106 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
107 /* client_hello_process haven't finished */
108 ret=MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
109 return ret;
Jerry Yub9930e72021-08-06 17:11:51 +0800110}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +0800111
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800112static int ssl_client_hello_prepare( mbedtls_ssl_context* ssl )
113{
114 ((void) ssl);
115 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
116}
117
118static int ssl_client_hello_write_partial( mbedtls_ssl_context* ssl,
119 unsigned char* buf, size_t buflen,
120 size_t* len_without_binders,
121 size_t* len_with_binders )
122{
123 ((void) ssl);
124 ((void) buf);
125 ((void) buflen);
126 ((void) len_without_binders);
127 ((void) len_with_binders);
128 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
129}
130
131static int ssl_client_hello_postprocess( mbedtls_ssl_context* ssl )
132{
133 ((void) ssl);
134 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
135}
136
Jerry Yu3cc4c2a2021-08-06 16:29:08 +0800137#endif /* MBEDTLS_SSL_CLI_C */
138
139#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */