@@ -27,9 +27,31 @@ void mbedtls_debug(void *ctx, int level, const char *file, int line, const char
27
27
fflush ((FILE * )ctx );
28
28
}
29
29
30
+ static status_t free_info_context (connect_info_t * const info ) {
31
+ if (info -> https ) {
32
+ mbedtls_ssl_close_notify (info -> ssl_ctx );
33
+ mbedtls_net_free (info -> net_ctx );
34
+ mbedtls_entropy_free (info -> entropy );
35
+ mbedtls_ctr_drbg_free (info -> ctr_drbg );
36
+ mbedtls_ssl_free (info -> ssl_ctx );
37
+ mbedtls_ssl_config_free (info -> ssl_config );
38
+ mbedtls_x509_crt_free (info -> cacert );
39
+ free (info -> net_ctx );
40
+ free (info -> ssl_ctx );
41
+ free (info -> ssl_config );
42
+ free (info -> ctr_drbg );
43
+ free (info -> entropy );
44
+ free (info -> cacert );
45
+ } else {
46
+ mbedtls_net_free (info -> net_ctx );
47
+ free (info -> net_ctx );
48
+ }
49
+ return SC_OK ;
50
+ }
51
+
30
52
status_t http_open (connect_info_t * const info , char const * const seed_nonce , char const * const host ,
31
53
char const * const port ) {
32
- int ret ;
54
+ int ret = SC_OK ;
33
55
34
56
if (info -> https ) {
35
57
info -> net_ctx = (mbedtls_net_context * )malloc (sizeof (mbedtls_net_context ));
@@ -49,33 +71,31 @@ status_t http_open(connect_info_t *const info, char const *const seed_nonce, cha
49
71
ret = mbedtls_ctr_drbg_seed (info -> ctr_drbg , mbedtls_entropy_func , info -> entropy , (const unsigned char * )seed_nonce ,
50
72
strlen (seed_nonce ));
51
73
if (ret != 0 ) {
52
- free (info -> net_ctx );
53
- free (info -> ssl_ctx );
54
- free (info -> ssl_config );
55
- free (info -> ctr_drbg );
56
- free (info -> entropy );
57
- free (info -> cacert );
58
- return SC_UTILS_HTTPS_INIT_ERROR ;
74
+ ret = SC_UTILS_HTTPS_INIT_ERROR ;
75
+ goto exit ;
59
76
}
60
77
61
78
ret = mbedtls_x509_crt_parse (info -> cacert , ca_crt_pem , ca_crt_pem_len );
62
79
if (ret < 0 ) {
63
80
printf ("error: mbedtls_x509_crt_parse returned -0x%x\n\n" , - ret );
64
- return SC_UTILS_HTTPS_X509_ERROR ;
81
+ ret = SC_UTILS_HTTPS_X509_ERROR ;
82
+ goto exit ;
65
83
}
66
84
}
67
85
68
86
ret = mbedtls_net_connect (info -> net_ctx , host , port , MBEDTLS_NET_PROTO_TCP );
69
87
if (ret != 0 ) {
70
88
printf ("error: mbedtls_net_connect returned %d\n\n" , ret );
71
- return SC_UTILS_HTTPS_CONN_ERROR ;
89
+ ret = SC_UTILS_HTTPS_CONN_ERROR ;
90
+ goto exit ;
72
91
}
73
92
74
93
ret = mbedtls_ssl_config_defaults (info -> ssl_config , MBEDTLS_SSL_IS_CLIENT , MBEDTLS_SSL_TRANSPORT_STREAM ,
75
94
MBEDTLS_SSL_PRESET_DEFAULT );
76
95
if (ret != 0 ) {
77
96
printf ("error: mbedtls_ssl_config_defaults returned %d\n\n" , ret );
78
- return SC_UTILS_HTTPS_SSL_ERROR ;
97
+ ret = SC_UTILS_HTTPS_SSL_ERROR ;
98
+ goto exit ;
79
99
}
80
100
81
101
mbedtls_ssl_conf_ca_chain (info -> ssl_config , info -> cacert , NULL );
@@ -87,13 +107,15 @@ status_t http_open(connect_info_t *const info, char const *const seed_nonce, cha
87
107
ret = mbedtls_ssl_setup (info -> ssl_ctx , info -> ssl_config );
88
108
if (ret != 0 ) {
89
109
printf ("error: mbedtls_ssl_setup returned %d\n\n" , ret );
90
- return SC_UTILS_HTTPS_SSL_ERROR ;
110
+ ret = SC_UTILS_HTTPS_SSL_ERROR ;
111
+ goto exit ;
91
112
}
92
113
93
114
ret = mbedtls_ssl_set_hostname (info -> ssl_ctx , host );
94
115
if (ret != 0 ) {
95
116
printf ("error: mbedtls_ssl_set_hostname returned %d\n\n" , ret );
96
- return SC_UTILS_HTTPS_SSL_ERROR ;
117
+ ret = SC_UTILS_HTTPS_SSL_ERROR ;
118
+ goto exit ;
97
119
}
98
120
99
121
// Here is Blocking mode
@@ -104,7 +126,8 @@ status_t http_open(connect_info_t *const info, char const *const seed_nonce, cha
104
126
if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE ) {
105
127
printf ("error: mbedtls_ssl_handshake returned -0x%x\n\n" , - ret );
106
128
mbedtls_ssl_session_reset (info -> ssl_ctx );
107
- return SC_UTILS_HTTPS_SSL_ERROR ;
129
+ ret = SC_UTILS_HTTPS_SSL_ERROR ;
130
+ goto exit ;
108
131
}
109
132
}
110
133
@@ -116,7 +139,10 @@ status_t http_open(connect_info_t *const info, char const *const seed_nonce, cha
116
139
mbedtls_x509_crt_verify_info (vrfy_buf , sizeof (vrfy_buf ), "" , flags );
117
140
printf ("error: %s\n" , vrfy_buf );
118
141
}
119
- return SC_OK ;
142
+
143
+ exit :
144
+ free_info_context (info );
145
+ return ret ;
120
146
}
121
147
122
148
status_t http_send_request (connect_info_t * const info , const char * req ) {
@@ -166,26 +192,7 @@ status_t http_read_response(connect_info_t *const info, char *res, size_t res_le
166
192
}
167
193
168
194
status_t http_close (connect_info_t * const info ) {
169
- if (info -> https ) {
170
- mbedtls_ssl_close_notify (info -> ssl_ctx );
171
-
172
- mbedtls_net_free (info -> net_ctx );
173
- mbedtls_entropy_free (info -> entropy );
174
- mbedtls_ctr_drbg_free (info -> ctr_drbg );
175
- mbedtls_ssl_free (info -> ssl_ctx );
176
- mbedtls_ssl_config_free (info -> ssl_config );
177
- mbedtls_x509_crt_free (info -> cacert );
178
- free (info -> net_ctx );
179
- free (info -> ssl_ctx );
180
- free (info -> ssl_config );
181
- free (info -> ctr_drbg );
182
- free (info -> entropy );
183
- free (info -> cacert );
184
- } else {
185
- mbedtls_net_free (info -> net_ctx );
186
- free (info -> net_ctx );
187
- }
188
-
195
+ free_info_context (info );
189
196
return SC_OK ;
190
197
}
191
198
0 commit comments