diff --git a/BUILD.gn b/BUILD.gn index 076c974b4f1ed4de1636331fc032b9c8cbd8313d..6482fea17e31d11d02887870694da1ebaff8cf24 100755 --- a/BUILD.gn +++ b/BUILD.gn @@ -27,10 +27,23 @@ MBEDTLS_SOURCES += [ "library/x509write_csr.c", ] +if (defined(ohos_lite)) { + MBEDTLS_SOURCES -= [ "library/ssl_srv.c" ] +} + if (defined(ohos_lite)) { import("//build/lite/config/component/lite_component.gni") import("//build/lite/ndk/ndk.gni") + if (ohos_kernel_type != "liteos_m") { + MBEDTLS_SOURCES += [ "library/ssl_srv.c" ] + MBEDTLS_SOURCES -= [ + "$MBEDTLSDIR/library/ssl_srv.c", + "$MBEDTLSDIR/port/src/tls_client.c", + "$MBEDTLSDIR/port/src/tls_certificate.c", + ] + } + config("mbedtls_config") { include_dirs = MBEDTLS_INLCUDE_DIRS if (ohos_kernel_type == "liteos_m") { diff --git a/library/net_sockets.c b/library/net_sockets.c index 17a9e4a5760bb19270af584f9acc861c2f9ab4c7..ebe3b5f3d51f3f392f4009fb8aebcdd5bb793c3b 100644 --- a/library/net_sockets.c +++ b/library/net_sockets.c @@ -193,8 +193,15 @@ int mbedtls_net_connect( mbedtls_net_context *ctx, const char *host, ret = MBEDTLS_ERR_NET_UNKNOWN_HOST; for( cur = addr_list; cur != NULL; cur = cur->ai_next ) { +#ifdef LITEOS_VERSION + if (cur->ai_family != AF_INET || cur->ai_socktype != SOCK_STREAM) { + continue; + } + ctx->fd = (int) socket(AF_INET, SOCK_STREAM, 0); +#else ctx->fd = (int) socket( cur->ai_family, cur->ai_socktype, cur->ai_protocol ); +#endif if( ctx->fd < 0 ) { ret = MBEDTLS_ERR_NET_SOCKET_FAILED; @@ -575,8 +582,12 @@ int mbedtls_net_recv( void *ctx, unsigned char *buf, size_t len ) ret = check_fd( fd, 0 ); if( ret != 0 ) return( ret ); +#ifdef LITEOS_VERSION + ret = (int) recv( fd, buf, len, 0); +#else ret = (int) read( fd, buf, len ); +#endif if( ret < 0 ) { @@ -658,7 +669,12 @@ int mbedtls_net_send( void *ctx, const unsigned char *buf, size_t len ) if( ret != 0 ) return( ret ); +#ifdef LITEOS_VERSION + ret = (int) send( fd, buf, len, 0); +#else + ret = (int) write( fd, buf, len ); +#endif if( ret < 0 ) { diff --git a/mbedtls.gni b/mbedtls.gni index 42c368131178598e7f0713c7780ad3a52afcf32e..322d1d769c2871073697a126da2836bd3b50e3f7 100755 --- a/mbedtls.gni +++ b/mbedtls.gni @@ -24,7 +24,7 @@ # OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF -# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. MBEDTLSDIR = "//third_party/mbedtls/" KERNELDIR = "//kernel/liteos_m/" @@ -129,3 +129,13 @@ MBEDTLS_INLCUDE_DIRS = [ "$MBEDTLSDIR/include/mbedtls", "$MBEDTLSDIR/tests/include", ] + +if (defined(ohos_lite)) { + MBEDTLS_SOURCES += [ + "$MBEDTLSDIR/library/ssl_srv.c", + "$MBEDTLSDIR/port/src/tls_client.c", + "$MBEDTLSDIR/port/src/tls_certificate.c", + ] + + MBEDTLS_INLCUDE_DIRS += [ "$MBEDTLSDIR/port/include" ] +} diff --git a/port/BUILD.gn b/port/BUILD.gn index 60ee16b0012ba59d64e6b35000d9b69fe25f9d07..016e59c6c2cc9012601594d832d2eb37e3cd3db7 100755 --- a/port/BUILD.gn +++ b/port/BUILD.gn @@ -11,7 +11,7 @@ # 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. -# +# import("//build/lite/config/component/lite_component.gni") @@ -22,6 +22,7 @@ if (ohos_build_type == "debug") { "//third_party/mbedtls/include", "//commonlibrary/utils_lite/include", "//third_party/bounds_checking_function/include", + "//third_party/mbedtls/port/include", ] } diff --git a/port/include/mbedtls_log.h b/port/include/mbedtls_log.h index 1210dadaed977292ee4e206e0bd74ac16e09d7b5..96767c31f3a7b0412b4d977dd698807e556741f4 100755 --- a/port/include/mbedtls_log.h +++ b/port/include/mbedtls_log.h @@ -30,7 +30,6 @@ #ifndef OHOS_DEBUG #define DECORATOR_HILOG(op, fmt, args...) \ do { \ - op(LOG_CORE, fmt, ##args); \ } while (0) #else #define DECORATOR_HILOG(op, fmt, args...) \ diff --git a/port/include/tls_client.h b/port/include/tls_client.h index 320ce4792a8a334dec313cf65a10369453edbe3c..65f6b4afb6cf5338d2554070cc946a61e0bf33aa 100755 --- a/port/include/tls_client.h +++ b/port/include/tls_client.h @@ -17,7 +17,7 @@ #define MBEDTLS_CLIENT_H #include "mbedtls/platform.h" -#include "mbedtls/net_sockets" +#include "mbedtls/net_sockets.h" #include "mbedtls/ssl.h" #include "mbedtls/entropy.h" #include "mbedtls/ctr_drbg.h" @@ -25,6 +25,9 @@ #define RET_ERROR -1; #define RET_EOK 0 +#ifndef LOG_CORE +#define LOG_CORE 3 +#endif typedef struct MbedTLSSession { char *host; diff --git a/port/src/tls_client.c b/port/src/tls_client.c index a23d288e721b684b7b43fc012727f0de843a0e25..c25dce90e127dbf3a04934102cb530dad3399ea9 100755 --- a/port/src/tls_client.c +++ b/port/src/tls_client.c @@ -90,7 +90,7 @@ int MbedtlsClientContext(MbedTLSSession *session) int ret = mbedtls_x509_crt_parse(&session->cacert, (const unsigned char *)G_MBEDTLS_ROOT_CERTIFICATE, G_MBEDTLS_ROOT_CERTIFICATE_LEN); if (ret < 0) { - LOGE("mbedtls_x509_crt_parse error, return -0x%x.", -ret); + LOGD("mbedtls_x509_crt_parse error, return -0x%x.", -ret); return ret; } @@ -135,7 +135,7 @@ int MbedtlsClientConnect(MbedTLSSession *session) if (session == NULL) { return -RET_ERROR; } - LOGI("connect: host:%s, port: %s", session->host, session->port); + LOGD("connect: host:%s, port: %s", session->host, session->port); int ret = mbedtls_net_connect(&session->server_fd, session->host, session->port, MBEDTLS_NET_PROTO_TCP); if (ret != 0) { @@ -145,7 +145,6 @@ int MbedtlsClientConnect(MbedTLSSession *session) LOGD("Connected %s:%s fd:%d, success...", session->host, session->port, session->server_fd.fd); mbedtls_ssl_set_bio(&session->ssl, &session->server_fd, mbedtls_net_send, mbedtls_net_recv, NULL); - LOGD("ssl state=%d", session->ssl.state); while ((ret = mbedtls_ssl_handshake(&session->ssl)) != 0) { LOGD("mbedtls_ssl_handshake ret=0x%x.", -ret);