| 
									
										
										
										
											2016-09-22 10:28:08 +08:00
										 |  |  | // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Licensed under the Apache License, Version 2.0 (the "License");
 | 
					
						
							|  |  |  | // you may not use this file except in compliance with the License.
 | 
					
						
							|  |  |  | // You may obtain a copy of the License at
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //     http://www.apache.org/licenses/LICENSE-2.0
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Unless required by applicable law or agreed to in writing, software
 | 
					
						
							|  |  |  | // distributed under the License is distributed on an "AS IS" BASIS,
 | 
					
						
							|  |  |  | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
					
						
							|  |  |  | // See the License for the specific language governing permissions and
 | 
					
						
							|  |  |  | // limitations under the License.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:46 +08:00
										 |  |  | #include "ssl_pm.h"
 | 
					
						
							| 
									
										
										
										
											2016-09-22 10:28:08 +08:00
										 |  |  | #include "ssl_port.h"
 | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:46 +08:00
										 |  |  | #include "ssl_dbg.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* mbedtls include */ | 
					
						
							|  |  |  | #include "mbedtls/platform.h"
 | 
					
						
							| 
									
										
										
										
											2017-09-06 15:45:56 +10:00
										 |  |  | #include "mbedtls/net_sockets.h"
 | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:46 +08:00
										 |  |  | #include "mbedtls/debug.h"
 | 
					
						
							|  |  |  | #include "mbedtls/entropy.h"
 | 
					
						
							|  |  |  | #include "mbedtls/ctr_drbg.h"
 | 
					
						
							|  |  |  | #include "mbedtls/error.h"
 | 
					
						
							|  |  |  | #include "mbedtls/certs.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-10 20:49:24 +08:00
										 |  |  | #define X509_INFO_STRING_LENGTH 8192
 | 
					
						
							| 
									
										
										
										
											2016-10-09 16:42:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:46 +08:00
										 |  |  | struct ssl_pm | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /* local socket file description */ | 
					
						
							|  |  |  |     mbedtls_net_context fd; | 
					
						
							|  |  |  |     /* remote client socket file description */ | 
					
						
							|  |  |  |     mbedtls_net_context cl_fd; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     mbedtls_ssl_config conf; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     mbedtls_ctr_drbg_context ctr_drbg; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     mbedtls_ssl_context ssl; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     mbedtls_entropy_context entropy; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct x509_pm | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-09-23 18:13:10 +08:00
										 |  |  |     mbedtls_x509_crt *x509_crt; | 
					
						
							| 
									
										
										
										
											2016-09-23 10:33:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-23 18:13:10 +08:00
										 |  |  |     mbedtls_x509_crt *ex_crt; | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:46 +08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct pkey_pm | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-09-23 18:13:10 +08:00
										 |  |  |     mbedtls_pk_context *pkey; | 
					
						
							| 
									
										
										
										
											2016-09-23 10:33:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-23 18:13:10 +08:00
										 |  |  |     mbedtls_pk_context *ex_pkey; | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:46 +08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | unsigned int max_content_len; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*********************************************************************************************/ | 
					
						
							|  |  |  | /************************************ SSL arch interface *************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-10 20:49:24 +08:00
										 |  |  | #ifdef CONFIG_OPENSSL_LOWLEVEL_DEBUG
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* mbedtls debug level */ | 
					
						
							|  |  |  | #define MBEDTLS_DEBUG_LEVEL 4
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * @brief mbedtls debug function | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | static void ssl_platform_debug(void *ctx, int level, | 
					
						
							|  |  |  |                      const char *file, int line, | 
					
						
							|  |  |  |                      const char *str) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /* Shorten 'file' from the whole file path to just the filename
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |        This is a bit wasteful because the macros are compiled in with | 
					
						
							|  |  |  |        the full _FILE_ path in each case. | 
					
						
							|  |  |  |     */ | 
					
						
							|  |  |  |     char *file_sep = rindex(file, '/'); | 
					
						
							|  |  |  |     if(file_sep) | 
					
						
							|  |  |  |         file = file_sep + 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     SSL_DEBUG(SSL_DEBUG_ON, "%s:%d %s", file, line, str); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-23 14:50:27 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * @brief create SSL low-level object | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:46 +08:00
										 |  |  | int ssl_pm_new(SSL *ssl) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     struct ssl_pm *ssl_pm; | 
					
						
							|  |  |  |     int ret; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-22 11:43:59 +08:00
										 |  |  |     const unsigned char pers[] = "OpenSSL PM"; | 
					
						
							|  |  |  |     size_t pers_len = sizeof(pers); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:46 +08:00
										 |  |  |     int endpoint; | 
					
						
							| 
									
										
										
										
											2016-09-22 10:28:08 +08:00
										 |  |  |     int version; | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const SSL_METHOD *method = ssl->method; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-01 13:07:10 +08:00
										 |  |  |     ssl_pm = ssl_mem_zalloc(sizeof(struct ssl_pm)); | 
					
						
							| 
									
										
										
										
											2017-01-10 20:49:24 +08:00
										 |  |  |     if (!ssl_pm) { | 
					
						
							|  |  |  |         SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "no enough memory > (ssl_pm)"); | 
					
						
							|  |  |  |         goto no_mem; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-01 13:07:10 +08:00
										 |  |  |     max_content_len = ssl->ctx->read_buffer_len; | 
					
						
							| 
									
										
										
										
											2017-01-10 20:49:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:46 +08:00
										 |  |  |     mbedtls_net_init(&ssl_pm->fd); | 
					
						
							|  |  |  |     mbedtls_net_init(&ssl_pm->cl_fd); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     mbedtls_ssl_config_init(&ssl_pm->conf); | 
					
						
							|  |  |  |     mbedtls_ctr_drbg_init(&ssl_pm->ctr_drbg); | 
					
						
							|  |  |  |     mbedtls_entropy_init(&ssl_pm->entropy); | 
					
						
							|  |  |  |     mbedtls_ssl_init(&ssl_pm->ssl); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-22 11:43:59 +08:00
										 |  |  |     ret = mbedtls_ctr_drbg_seed(&ssl_pm->ctr_drbg, mbedtls_entropy_func, &ssl_pm->entropy, pers, pers_len); | 
					
						
							| 
									
										
										
										
											2017-01-10 20:49:24 +08:00
										 |  |  |     if (ret) { | 
					
						
							|  |  |  |         SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "mbedtls_ctr_drbg_seed() return -0x%x", -ret); | 
					
						
							|  |  |  |         goto mbedtls_err1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-22 11:43:59 +08:00
										 |  |  |     if (method->endpoint) { | 
					
						
							|  |  |  |         endpoint = MBEDTLS_SSL_IS_SERVER; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         endpoint = MBEDTLS_SSL_IS_CLIENT; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:46 +08:00
										 |  |  |     ret = mbedtls_ssl_config_defaults(&ssl_pm->conf, endpoint, MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT); | 
					
						
							| 
									
										
										
										
											2017-01-10 20:49:24 +08:00
										 |  |  |     if (ret) { | 
					
						
							|  |  |  |         SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "mbedtls_ssl_config_defaults() return -0x%x", -ret); | 
					
						
							|  |  |  |         goto mbedtls_err2; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-09 17:49:16 +08:00
										 |  |  |     if (TLS_ANY_VERSION != ssl->version) { | 
					
						
							|  |  |  |         if (TLS1_2_VERSION == ssl->version) | 
					
						
							|  |  |  |             version = MBEDTLS_SSL_MINOR_VERSION_3; | 
					
						
							|  |  |  |         else if (TLS1_1_VERSION == ssl->version) | 
					
						
							|  |  |  |             version = MBEDTLS_SSL_MINOR_VERSION_2; | 
					
						
							|  |  |  |         else if (TLS1_VERSION == ssl->version) | 
					
						
							|  |  |  |             version = MBEDTLS_SSL_MINOR_VERSION_1; | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |             version = MBEDTLS_SSL_MINOR_VERSION_0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         mbedtls_ssl_conf_max_version(&ssl_pm->conf, MBEDTLS_SSL_MAJOR_VERSION_3, version); | 
					
						
							|  |  |  |         mbedtls_ssl_conf_min_version(&ssl_pm->conf, MBEDTLS_SSL_MAJOR_VERSION_3, version); | 
					
						
							| 
									
										
										
										
											2016-12-21 12:04:26 +08:00
										 |  |  |     } else { | 
					
						
							|  |  |  |         mbedtls_ssl_conf_max_version(&ssl_pm->conf, MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3); | 
					
						
							|  |  |  |         mbedtls_ssl_conf_min_version(&ssl_pm->conf, MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_0); | 
					
						
							| 
									
										
										
										
											2016-10-09 17:49:16 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-22 10:28:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-26 14:13:37 +05:30
										 |  |  |     if (ssl->ctx->ssl_alpn.alpn_status == ALPN_ENABLE) { | 
					
						
							| 
									
										
										
										
											2017-12-08 13:00:11 +08:00
										 |  |  | #ifdef MBEDTLS_SSL_ALPN
 | 
					
						
							|  |  |  |         mbedtls_ssl_conf_alpn_protocols( &ssl_pm->conf, ssl->ctx->ssl_alpn.alpn_list ); | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  |         SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "CONFIG_MBEDTLS_SSL_ALPN must be enabled to use ALPN", -1); | 
					
						
							|  |  |  | #endif // MBEDTLS_SSL_ALPN
 | 
					
						
							| 
									
										
										
										
											2017-09-26 14:13:37 +05:30
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:46 +08:00
										 |  |  |     mbedtls_ssl_conf_rng(&ssl_pm->conf, mbedtls_ctr_drbg_random, &ssl_pm->ctr_drbg); | 
					
						
							| 
									
										
										
										
											2016-09-22 10:28:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-10 20:49:24 +08:00
										 |  |  | #ifdef CONFIG_OPENSSL_LOWLEVEL_DEBUG
 | 
					
						
							|  |  |  |     mbedtls_debug_set_threshold(MBEDTLS_DEBUG_LEVEL); | 
					
						
							|  |  |  |     mbedtls_ssl_conf_dbg(&ssl_pm->conf, ssl_platform_debug, NULL); | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:46 +08:00
										 |  |  |     mbedtls_ssl_conf_dbg(&ssl_pm->conf, NULL, NULL); | 
					
						
							| 
									
										
										
										
											2017-01-10 20:49:24 +08:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ret = mbedtls_ssl_setup(&ssl_pm->ssl, &ssl_pm->conf); | 
					
						
							| 
									
										
										
										
											2017-01-10 20:49:24 +08:00
										 |  |  |     if (ret) { | 
					
						
							|  |  |  |         SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "mbedtls_ssl_setup() return -0x%x", -ret); | 
					
						
							|  |  |  |         goto mbedtls_err2; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     mbedtls_ssl_set_bio(&ssl_pm->ssl, &ssl_pm->fd, mbedtls_net_send, mbedtls_net_recv, NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ssl->ssl_pm = ssl_pm; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-10 20:49:24 +08:00
										 |  |  | mbedtls_err2: | 
					
						
							| 
									
										
										
										
											2016-09-23 11:41:57 +08:00
										 |  |  |     mbedtls_ssl_config_free(&ssl_pm->conf); | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:46 +08:00
										 |  |  |     mbedtls_ctr_drbg_free(&ssl_pm->ctr_drbg); | 
					
						
							| 
									
										
										
										
											2017-01-10 20:49:24 +08:00
										 |  |  | mbedtls_err1: | 
					
						
							| 
									
										
										
										
											2016-09-23 18:13:10 +08:00
										 |  |  |     mbedtls_entropy_free(&ssl_pm->entropy); | 
					
						
							| 
									
										
										
										
											2016-11-01 13:07:10 +08:00
										 |  |  |     ssl_mem_free(ssl_pm); | 
					
						
							| 
									
										
										
										
											2017-01-10 20:49:24 +08:00
										 |  |  | no_mem: | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:46 +08:00
										 |  |  |     return -1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-23 14:50:27 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * @brief free SSL low-level object | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:46 +08:00
										 |  |  | void ssl_pm_free(SSL *ssl) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     struct ssl_pm *ssl_pm = (struct ssl_pm *)ssl->ssl_pm; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     mbedtls_ctr_drbg_free(&ssl_pm->ctr_drbg); | 
					
						
							|  |  |  |     mbedtls_entropy_free(&ssl_pm->entropy); | 
					
						
							| 
									
										
										
										
											2016-09-22 18:33:55 +08:00
										 |  |  |     mbedtls_ssl_config_free(&ssl_pm->conf); | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:46 +08:00
										 |  |  |     mbedtls_ssl_free(&ssl_pm->ssl); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-01 13:07:10 +08:00
										 |  |  |     ssl_mem_free(ssl_pm); | 
					
						
							| 
									
										
										
										
											2016-09-22 18:33:55 +08:00
										 |  |  |     ssl->ssl_pm = NULL; | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:46 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-23 14:50:27 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * @brief reload SSL low-level certification object | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2016-09-23 11:41:57 +08:00
										 |  |  | static int ssl_pm_reload_crt(SSL *ssl) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int ret; | 
					
						
							|  |  |  |     int mode; | 
					
						
							|  |  |  |     struct ssl_pm *ssl_pm = ssl->ssl_pm; | 
					
						
							|  |  |  |     struct x509_pm *ca_pm = (struct x509_pm *)ssl->client_CA->x509_pm; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     struct pkey_pm *pkey_pm = (struct pkey_pm *)ssl->cert->pkey->pkey_pm; | 
					
						
							|  |  |  |     struct x509_pm *crt_pm = (struct x509_pm *)ssl->cert->x509->x509_pm; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-07 15:54:47 -04:00
										 |  |  |     if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) | 
					
						
							| 
									
										
										
										
											2016-09-23 11:41:57 +08:00
										 |  |  |         mode = MBEDTLS_SSL_VERIFY_REQUIRED; | 
					
						
							| 
									
										
										
										
											2018-07-07 15:54:47 -04:00
										 |  |  |     else if (ssl->verify_mode & SSL_VERIFY_PEER) | 
					
						
							| 
									
										
										
										
											2016-09-27 10:06:24 +08:00
										 |  |  |         mode = MBEDTLS_SSL_VERIFY_OPTIONAL; | 
					
						
							| 
									
										
										
										
											2018-07-07 15:54:47 -04:00
										 |  |  |     else if (ssl->verify_mode & SSL_VERIFY_CLIENT_ONCE) | 
					
						
							| 
									
										
										
										
											2016-09-23 11:41:57 +08:00
										 |  |  |         mode = MBEDTLS_SSL_VERIFY_UNSET; | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |         mode = MBEDTLS_SSL_VERIFY_NONE; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     mbedtls_ssl_conf_authmode(&ssl_pm->conf, mode); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-23 18:13:10 +08:00
										 |  |  |     if (ca_pm->x509_crt) { | 
					
						
							|  |  |  |         mbedtls_ssl_conf_ca_chain(&ssl_pm->conf, ca_pm->x509_crt, NULL); | 
					
						
							| 
									
										
										
										
											2016-09-23 18:47:09 +08:00
										 |  |  |     } else if (ca_pm->ex_crt) { | 
					
						
							| 
									
										
										
										
											2016-09-27 19:06:07 +08:00
										 |  |  |         mbedtls_ssl_conf_ca_chain(&ssl_pm->conf, ca_pm->ex_crt, NULL); | 
					
						
							| 
									
										
										
										
											2016-09-23 11:41:57 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-23 18:13:10 +08:00
										 |  |  |     if (crt_pm->x509_crt && pkey_pm->pkey) { | 
					
						
							|  |  |  |         ret = mbedtls_ssl_conf_own_cert(&ssl_pm->conf, crt_pm->x509_crt, pkey_pm->pkey); | 
					
						
							| 
									
										
										
										
											2016-09-23 18:47:09 +08:00
										 |  |  |     } else if (crt_pm->ex_crt && pkey_pm->ex_pkey) { | 
					
						
							|  |  |  |         ret = mbedtls_ssl_conf_own_cert(&ssl_pm->conf, crt_pm->ex_crt, pkey_pm->ex_pkey); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         ret = 0; | 
					
						
							| 
									
										
										
										
											2016-09-23 11:41:57 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-26 10:12:58 +08:00
										 |  |  |     if (ret) { | 
					
						
							|  |  |  |         SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "mbedtls_ssl_conf_own_cert() return -0x%x", -ret); | 
					
						
							|  |  |  |         ret = -1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-23 18:47:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-26 10:12:58 +08:00
										 |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2016-09-23 11:41:57 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-14 09:40:12 +08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Perform the mbedtls SSL handshake instead of mbedtls_ssl_handshake. | 
					
						
							|  |  |  |  * We can add debug here. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2016-11-21 17:13:55 +08:00
										 |  |  | static int mbedtls_handshake( mbedtls_ssl_context *ssl ) | 
					
						
							| 
									
										
										
										
											2016-11-14 09:40:12 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     int ret = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-26 10:12:58 +08:00
										 |  |  |     while (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) { | 
					
						
							| 
									
										
										
										
											2016-11-14 09:40:12 +08:00
										 |  |  |         ret = mbedtls_ssl_handshake_step(ssl); | 
					
						
							| 
									
										
										
										
											2017-01-10 20:49:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         SSL_DEBUG(SSL_PLATFORM_DEBUG_LEVEL, "ssl ret %d state %d", ret, ssl->state); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-14 09:40:12 +08:00
										 |  |  |         if (ret != 0) | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:46 +08:00
										 |  |  | int ssl_pm_handshake(SSL *ssl) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-01-26 10:12:58 +08:00
										 |  |  |     int ret; | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:46 +08:00
										 |  |  |     struct ssl_pm *ssl_pm = (struct ssl_pm *)ssl->ssl_pm; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-26 10:12:58 +08:00
										 |  |  |     ret = ssl_pm_reload_crt(ssl); | 
					
						
							|  |  |  |     if (ret) | 
					
						
							| 
									
										
										
										
											2016-09-23 11:41:57 +08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:46 +08:00
										 |  |  |     ssl_speed_up_enter(); | 
					
						
							| 
									
										
										
										
											2017-01-10 20:49:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-26 10:12:58 +08:00
										 |  |  |     while((ret = mbedtls_handshake(&ssl_pm->ssl)) != 0) { | 
					
						
							|  |  |  |         if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) { | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:46 +08:00
										 |  |  |            break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-01-10 20:49:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:46 +08:00
										 |  |  |     ssl_speed_up_exit(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-26 10:12:58 +08:00
										 |  |  |     if (ret) { | 
					
						
							|  |  |  |         SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "mbedtls_ssl_handshake() return -0x%x", -ret); | 
					
						
							|  |  |  |         ret = 0; | 
					
						
							|  |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2016-09-23 18:13:10 +08:00
										 |  |  |         struct x509_pm *x509_pm = (struct x509_pm *)ssl->session->peer->x509_pm; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         x509_pm->ex_crt = (mbedtls_x509_crt *)mbedtls_ssl_get_peer_cert(&ssl_pm->ssl); | 
					
						
							| 
									
										
										
										
											2017-01-26 10:12:58 +08:00
										 |  |  |         ret = 1; | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:46 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int ssl_pm_shutdown(SSL *ssl) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-01-26 10:12:58 +08:00
										 |  |  |     int ret; | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:46 +08:00
										 |  |  |     struct ssl_pm *ssl_pm = (struct ssl_pm *)ssl->ssl_pm; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-26 10:12:58 +08:00
										 |  |  |     ret = mbedtls_ssl_close_notify(&ssl_pm->ssl); | 
					
						
							|  |  |  |     if (ret) { | 
					
						
							|  |  |  |         SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "mbedtls_ssl_close_notify() return -0x%x", -ret); | 
					
						
							|  |  |  |         ret = -1; | 
					
						
							|  |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2016-09-23 18:13:10 +08:00
										 |  |  |         struct x509_pm *x509_pm = (struct x509_pm *)ssl->session->peer->x509_pm; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         x509_pm->ex_crt = NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int ssl_pm_clear(SSL *ssl) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return ssl_pm_shutdown(ssl); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int ssl_pm_read(SSL *ssl, void *buffer, int len) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-01-26 10:12:58 +08:00
										 |  |  |     int ret; | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:46 +08:00
										 |  |  |     struct ssl_pm *ssl_pm = (struct ssl_pm *)ssl->ssl_pm; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-26 10:12:58 +08:00
										 |  |  |     ret = mbedtls_ssl_read(&ssl_pm->ssl, buffer, len); | 
					
						
							|  |  |  |     if (ret < 0) { | 
					
						
							|  |  |  |         SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "mbedtls_ssl_read() return -0x%x", -ret); | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:46 +08:00
										 |  |  |         ret = -1; | 
					
						
							| 
									
										
										
										
											2017-01-26 10:12:58 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int ssl_pm_send(SSL *ssl, const void *buffer, int len) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-01-26 10:12:58 +08:00
										 |  |  |     int ret; | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:46 +08:00
										 |  |  |     struct ssl_pm *ssl_pm = (struct ssl_pm *)ssl->ssl_pm; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-26 10:12:58 +08:00
										 |  |  |     ret = mbedtls_ssl_write(&ssl_pm->ssl, buffer, len); | 
					
						
							|  |  |  |     if (ret < 0) { | 
					
						
							|  |  |  |         SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "mbedtls_ssl_write() return -0x%x", -ret); | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:46 +08:00
										 |  |  |         ret = -1; | 
					
						
							| 
									
										
										
										
											2017-01-26 10:12:58 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int ssl_pm_pending(const SSL *ssl) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     struct ssl_pm *ssl_pm = (struct ssl_pm *)ssl->ssl_pm; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return mbedtls_ssl_get_bytes_avail(&ssl_pm->ssl); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ssl_pm_set_fd(SSL *ssl, int fd, int mode) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     struct ssl_pm *ssl_pm = (struct ssl_pm *)ssl->ssl_pm; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ssl_pm->fd.fd = fd; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-02 14:00:13 +05:30
										 |  |  | void ssl_pm_set_hostname(SSL *ssl, const char *hostname) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     struct ssl_pm *ssl_pm = (struct ssl_pm *)ssl->ssl_pm; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     mbedtls_ssl_set_hostname(&ssl_pm->ssl, hostname); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:46 +08:00
										 |  |  | int ssl_pm_get_fd(const SSL *ssl, int mode) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     struct ssl_pm *ssl_pm = (struct ssl_pm *)ssl->ssl_pm; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return ssl_pm->fd.fd; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | OSSL_HANDSHAKE_STATE ssl_pm_get_state(const SSL *ssl) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     OSSL_HANDSHAKE_STATE state; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     struct ssl_pm *ssl_pm = (struct ssl_pm *)ssl->ssl_pm; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     switch (ssl_pm->ssl.state) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         case MBEDTLS_SSL_CLIENT_HELLO: | 
					
						
							|  |  |  |             state = TLS_ST_CW_CLNT_HELLO; | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case MBEDTLS_SSL_SERVER_HELLO: | 
					
						
							|  |  |  |             state = TLS_ST_SW_SRVR_HELLO; | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case MBEDTLS_SSL_SERVER_CERTIFICATE: | 
					
						
							|  |  |  |             state = TLS_ST_SW_CERT; | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case MBEDTLS_SSL_SERVER_HELLO_DONE: | 
					
						
							|  |  |  |             state = TLS_ST_SW_SRVR_DONE; | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE: | 
					
						
							|  |  |  |             state = TLS_ST_CW_KEY_EXCH; | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC: | 
					
						
							|  |  |  |             state = TLS_ST_CW_CHANGE; | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case MBEDTLS_SSL_CLIENT_FINISHED: | 
					
						
							|  |  |  |             state = TLS_ST_CW_FINISHED; | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC: | 
					
						
							|  |  |  |             state = TLS_ST_SW_CHANGE; | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case MBEDTLS_SSL_SERVER_FINISHED: | 
					
						
							|  |  |  |             state = TLS_ST_SW_FINISHED; | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case MBEDTLS_SSL_CLIENT_CERTIFICATE: | 
					
						
							|  |  |  |             state = TLS_ST_CW_CERT; | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case MBEDTLS_SSL_SERVER_KEY_EXCHANGE: | 
					
						
							|  |  |  |             state = TLS_ST_SR_KEY_EXCH; | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET: | 
					
						
							|  |  |  |             state = TLS_ST_SW_SESSION_TICKET; | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT: | 
					
						
							|  |  |  |             state = TLS_ST_SW_CERT_REQ; | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case MBEDTLS_SSL_HANDSHAKE_OVER: | 
					
						
							|  |  |  |             state = TLS_ST_OK; | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         default : | 
					
						
							|  |  |  |             state = TLS_ST_BEFORE; | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return state; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-09 16:42:49 +08:00
										 |  |  | int x509_pm_show_info(X509 *x) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int ret; | 
					
						
							|  |  |  |     char *buf; | 
					
						
							|  |  |  |     mbedtls_x509_crt *x509_crt; | 
					
						
							|  |  |  |     struct x509_pm *x509_pm = x->x509_pm; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (x509_pm->x509_crt) | 
					
						
							|  |  |  |         x509_crt = x509_pm->x509_crt; | 
					
						
							|  |  |  |     else if (x509_pm->ex_crt) | 
					
						
							|  |  |  |         x509_crt = x509_pm->ex_crt; | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |         x509_crt = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!x509_crt) | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-01 13:07:10 +08:00
										 |  |  |     buf = ssl_mem_malloc(X509_INFO_STRING_LENGTH); | 
					
						
							| 
									
										
										
										
											2017-01-10 20:49:24 +08:00
										 |  |  |     if (!buf) { | 
					
						
							|  |  |  |         SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "no enough memory > (buf)"); | 
					
						
							|  |  |  |         goto no_mem; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-10-09 16:42:49 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ret = mbedtls_x509_crt_info(buf, X509_INFO_STRING_LENGTH - 1, "", x509_crt); | 
					
						
							| 
									
										
										
										
											2017-01-10 20:49:24 +08:00
										 |  |  |     if (ret <= 0) { | 
					
						
							|  |  |  |         SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "mbedtls_x509_crt_info() return -0x%x", -ret); | 
					
						
							|  |  |  |         goto mbedtls_err1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-09 16:42:49 +08:00
										 |  |  |     buf[ret] = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-01 13:07:10 +08:00
										 |  |  |     ssl_mem_free(buf); | 
					
						
							| 
									
										
										
										
											2016-10-09 19:02:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-10 20:49:24 +08:00
										 |  |  |     SSL_DEBUG(SSL_DEBUG_ON, "%s", buf); | 
					
						
							| 
									
										
										
										
											2016-10-09 16:42:49 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-10 20:49:24 +08:00
										 |  |  | mbedtls_err1: | 
					
						
							| 
									
										
										
										
											2016-11-01 13:07:10 +08:00
										 |  |  |     ssl_mem_free(buf); | 
					
						
							| 
									
										
										
										
											2017-01-10 20:49:24 +08:00
										 |  |  | no_mem: | 
					
						
							| 
									
										
										
										
											2016-10-09 16:42:49 +08:00
										 |  |  |     return -1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-26 11:14:19 +08:00
										 |  |  | int x509_pm_new(X509 *x, X509 *m_x) | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:46 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-09-22 10:28:08 +08:00
										 |  |  |     struct x509_pm *x509_pm; | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-01 13:07:10 +08:00
										 |  |  |     x509_pm = ssl_mem_zalloc(sizeof(struct x509_pm)); | 
					
						
							| 
									
										
										
										
											2017-01-10 20:49:24 +08:00
										 |  |  |     if (!x509_pm) { | 
					
						
							|  |  |  |         SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "no enough memory > (x509_pm)"); | 
					
						
							|  |  |  |         goto failed1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-22 10:28:08 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     x->x509_pm = x509_pm; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-26 11:14:19 +08:00
										 |  |  |     if (m_x) { | 
					
						
							|  |  |  |         struct x509_pm *m_x509_pm = (struct x509_pm *)m_x->x509_pm; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         x509_pm->ex_crt = m_x509_pm->x509_crt; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-22 10:28:08 +08:00
										 |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2016-09-23 18:13:10 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | failed1: | 
					
						
							|  |  |  |     return -1; | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:46 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-26 11:14:19 +08:00
										 |  |  | void x509_pm_free(X509 *x) | 
					
						
							| 
									
										
										
										
											2016-09-22 16:08:36 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     struct x509_pm *x509_pm = (struct x509_pm *)x->x509_pm; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-23 18:13:10 +08:00
										 |  |  |     if (x509_pm->x509_crt) { | 
					
						
							|  |  |  |         mbedtls_x509_crt_free(x509_pm->x509_crt); | 
					
						
							| 
									
										
										
										
											2016-09-22 16:08:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-01 13:07:10 +08:00
										 |  |  |         ssl_mem_free(x509_pm->x509_crt); | 
					
						
							| 
									
										
										
										
											2016-09-23 18:13:10 +08:00
										 |  |  |         x509_pm->x509_crt = NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-01 13:07:10 +08:00
										 |  |  |     ssl_mem_free(x->x509_pm); | 
					
						
							| 
									
										
										
										
											2016-09-23 18:13:10 +08:00
										 |  |  |     x->x509_pm = NULL; | 
					
						
							| 
									
										
										
										
											2016-09-22 16:08:36 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-22 10:28:08 +08:00
										 |  |  | int x509_pm_load(X509 *x, const unsigned char *buffer, int len) | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:46 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     int ret; | 
					
						
							|  |  |  |     unsigned char *load_buf; | 
					
						
							| 
									
										
										
										
											2016-09-22 10:28:08 +08:00
										 |  |  |     struct x509_pm *x509_pm = (struct x509_pm *)x->x509_pm; | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-27 14:28:39 +08:00
										 |  |  | 	if (x509_pm->x509_crt) | 
					
						
							|  |  |  |         mbedtls_x509_crt_free(x509_pm->x509_crt); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-23 18:13:10 +08:00
										 |  |  |     if (!x509_pm->x509_crt) { | 
					
						
							| 
									
										
										
										
											2016-11-01 13:07:10 +08:00
										 |  |  |         x509_pm->x509_crt = ssl_mem_malloc(sizeof(mbedtls_x509_crt)); | 
					
						
							| 
									
										
										
										
											2017-01-10 20:49:24 +08:00
										 |  |  |         if (!x509_pm->x509_crt) { | 
					
						
							|  |  |  |             SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "no enough memory > (x509_pm->x509_crt)"); | 
					
						
							|  |  |  |             goto no_mem; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-09-23 18:13:10 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-01 13:07:10 +08:00
										 |  |  |     load_buf = ssl_mem_malloc(len + 1); | 
					
						
							| 
									
										
										
										
											2017-01-10 20:49:24 +08:00
										 |  |  |     if (!load_buf) { | 
					
						
							|  |  |  |         SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "no enough memory > (load_buf)"); | 
					
						
							|  |  |  |         goto failed; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ssl_memcpy(load_buf, buffer, len); | 
					
						
							|  |  |  |     load_buf[len] = '\0'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-27 14:28:39 +08:00
										 |  |  |     mbedtls_x509_crt_init(x509_pm->x509_crt); | 
					
						
							| 
									
										
										
										
											2016-09-22 16:08:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-27 14:28:39 +08:00
										 |  |  |     ret = mbedtls_x509_crt_parse(x509_pm->x509_crt, load_buf, len + 1); | 
					
						
							| 
									
										
										
										
											2016-11-01 13:07:10 +08:00
										 |  |  |     ssl_mem_free(load_buf); | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-10 20:49:24 +08:00
										 |  |  |     if (ret) { | 
					
						
							|  |  |  |         SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "mbedtls_x509_crt_parse return -0x%x", -ret); | 
					
						
							|  |  |  |         goto failed; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-22 10:28:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:46 +08:00
										 |  |  |     return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-10 20:49:24 +08:00
										 |  |  | failed: | 
					
						
							| 
									
										
										
										
											2016-11-14 09:40:12 +08:00
										 |  |  |     mbedtls_x509_crt_free(x509_pm->x509_crt); | 
					
						
							| 
									
										
										
										
											2016-11-01 13:07:10 +08:00
										 |  |  |     ssl_mem_free(x509_pm->x509_crt); | 
					
						
							| 
									
										
										
										
											2016-09-23 18:13:10 +08:00
										 |  |  |     x509_pm->x509_crt = NULL; | 
					
						
							| 
									
										
										
										
											2017-01-10 20:49:24 +08:00
										 |  |  | no_mem: | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:46 +08:00
										 |  |  |     return -1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-26 11:14:19 +08:00
										 |  |  | int pkey_pm_new(EVP_PKEY *pk, EVP_PKEY *m_pkey) | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:46 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-09-22 10:28:08 +08:00
										 |  |  |     struct pkey_pm *pkey_pm; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-01 13:07:10 +08:00
										 |  |  |     pkey_pm = ssl_mem_zalloc(sizeof(struct pkey_pm)); | 
					
						
							| 
									
										
										
										
											2016-09-22 10:28:08 +08:00
										 |  |  |     if (!pkey_pm) | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-26 11:14:19 +08:00
										 |  |  |     pk->pkey_pm = pkey_pm; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (m_pkey) { | 
					
						
							|  |  |  |         struct pkey_pm *m_pkey_pm = (struct pkey_pm *)m_pkey->pkey_pm; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         pkey_pm->ex_pkey = m_pkey_pm->pkey; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-22 10:28:08 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:46 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-26 11:14:19 +08:00
										 |  |  | void pkey_pm_free(EVP_PKEY *pk) | 
					
						
							| 
									
										
										
										
											2016-09-22 16:08:36 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-09-26 11:14:19 +08:00
										 |  |  |     struct pkey_pm *pkey_pm = (struct pkey_pm *)pk->pkey_pm; | 
					
						
							| 
									
										
										
										
											2016-09-22 16:08:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-23 18:13:10 +08:00
										 |  |  |     if (pkey_pm->pkey) { | 
					
						
							|  |  |  |         mbedtls_pk_free(pkey_pm->pkey); | 
					
						
							| 
									
										
										
										
											2016-09-22 16:08:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-01 13:07:10 +08:00
										 |  |  |         ssl_mem_free(pkey_pm->pkey); | 
					
						
							| 
									
										
										
										
											2016-09-23 18:13:10 +08:00
										 |  |  |         pkey_pm->pkey = NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-01 13:07:10 +08:00
										 |  |  |     ssl_mem_free(pk->pkey_pm); | 
					
						
							| 
									
										
										
										
											2016-09-26 11:14:19 +08:00
										 |  |  |     pk->pkey_pm = NULL; | 
					
						
							| 
									
										
										
										
											2016-09-22 16:08:36 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-26 11:14:19 +08:00
										 |  |  | int pkey_pm_load(EVP_PKEY *pk, const unsigned char *buffer, int len) | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:46 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     int ret; | 
					
						
							|  |  |  |     unsigned char *load_buf; | 
					
						
							| 
									
										
										
										
											2016-09-26 11:14:19 +08:00
										 |  |  |     struct pkey_pm *pkey_pm = (struct pkey_pm *)pk->pkey_pm; | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-27 14:28:39 +08:00
										 |  |  |     if (pkey_pm->pkey) | 
					
						
							|  |  |  |         mbedtls_pk_free(pkey_pm->pkey); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-23 18:13:10 +08:00
										 |  |  |     if (!pkey_pm->pkey) { | 
					
						
							| 
									
										
										
										
											2016-11-01 13:07:10 +08:00
										 |  |  |         pkey_pm->pkey = ssl_mem_malloc(sizeof(mbedtls_pk_context)); | 
					
						
							| 
									
										
										
										
											2017-01-10 20:49:24 +08:00
										 |  |  |         if (!pkey_pm->pkey) { | 
					
						
							|  |  |  |             SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "no enough memory > (pkey_pm->pkey)"); | 
					
						
							|  |  |  |             goto no_mem; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-09-23 18:13:10 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-01 13:07:10 +08:00
										 |  |  |     load_buf = ssl_mem_malloc(len + 1); | 
					
						
							| 
									
										
										
										
											2017-01-10 20:49:24 +08:00
										 |  |  |     if (!load_buf) { | 
					
						
							|  |  |  |         SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "no enough memory > (load_buf)"); | 
					
						
							|  |  |  |         goto failed; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ssl_memcpy(load_buf, buffer, len); | 
					
						
							|  |  |  |     load_buf[len] = '\0'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-27 14:28:39 +08:00
										 |  |  |     mbedtls_pk_init(pkey_pm->pkey); | 
					
						
							| 
									
										
										
										
											2016-09-22 16:08:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-27 14:28:39 +08:00
										 |  |  |     ret = mbedtls_pk_parse_key(pkey_pm->pkey, load_buf, len + 1, NULL, 0); | 
					
						
							| 
									
										
										
										
											2016-11-01 13:07:10 +08:00
										 |  |  |     ssl_mem_free(load_buf); | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-10 20:49:24 +08:00
										 |  |  |     if (ret) { | 
					
						
							|  |  |  |         SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "mbedtls_pk_parse_key return -0x%x", -ret); | 
					
						
							|  |  |  |         goto failed; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-22 10:28:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:46 +08:00
										 |  |  |     return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-10 20:49:24 +08:00
										 |  |  | failed: | 
					
						
							| 
									
										
										
										
											2016-11-14 09:40:12 +08:00
										 |  |  |     mbedtls_pk_free(pkey_pm->pkey); | 
					
						
							| 
									
										
										
										
											2016-11-01 13:07:10 +08:00
										 |  |  |     ssl_mem_free(pkey_pm->pkey); | 
					
						
							| 
									
										
										
										
											2016-09-23 18:13:10 +08:00
										 |  |  |     pkey_pm->pkey = NULL; | 
					
						
							| 
									
										
										
										
											2017-01-10 20:49:24 +08:00
										 |  |  | no_mem: | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:46 +08:00
										 |  |  |     return -1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-22 10:28:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | void ssl_pm_set_bufflen(SSL *ssl, int len) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     max_content_len = len; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-09-22 15:15:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | long ssl_pm_get_verify_result(const SSL *ssl) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-02-17 15:51:58 +08:00
										 |  |  |     uint32_t ret; | 
					
						
							| 
									
										
										
										
											2016-09-22 15:15:16 +08:00
										 |  |  |     long verify_result; | 
					
						
							|  |  |  |     struct ssl_pm *ssl_pm = (struct ssl_pm *)ssl->ssl_pm; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ret = mbedtls_ssl_get_verify_result(&ssl_pm->ssl); | 
					
						
							| 
									
										
										
										
											2017-01-26 10:12:58 +08:00
										 |  |  |     if (ret) { | 
					
						
							| 
									
										
										
										
											2017-02-17 15:51:58 +08:00
										 |  |  |         SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "mbedtls_ssl_get_verify_result() return 0x%x", ret); | 
					
						
							| 
									
										
										
										
											2016-09-22 15:15:16 +08:00
										 |  |  |         verify_result = X509_V_ERR_UNSPECIFIED; | 
					
						
							| 
									
										
										
										
											2017-01-26 10:12:58 +08:00
										 |  |  |     } else | 
					
						
							|  |  |  |         verify_result = X509_V_OK; | 
					
						
							| 
									
										
										
										
											2016-09-22 15:15:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return verify_result; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-09-10 08:01:38 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * @brief set expected hostname on peer cert CN | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | int X509_VERIFY_PARAM_set1_host(X509_VERIFY_PARAM *param, | 
					
						
							|  |  |  |                                 const char *name, size_t namelen) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     SSL *ssl = (SSL *)((char *)param - offsetof(SSL, param)); | 
					
						
							|  |  |  |     struct ssl_pm *ssl_pm = (struct ssl_pm *)ssl->ssl_pm; | 
					
						
							|  |  |  |     char *name_cstr = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (namelen) { | 
					
						
							|  |  |  |         name_cstr = malloc(namelen + 1); | 
					
						
							|  |  |  |         if (!name_cstr) { | 
					
						
							|  |  |  |             return 0; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         memcpy(name_cstr, name, namelen); | 
					
						
							|  |  |  |         name_cstr[namelen] = '\0'; | 
					
						
							|  |  |  |         name = name_cstr; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     mbedtls_ssl_set_hostname(&ssl_pm->ssl, name); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (namelen) { | 
					
						
							|  |  |  |         free(name_cstr); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } |