tls_client.c 6.7 KB
Newer Older
M
mamingshuai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/**
 * Copyright (c) 2020 Huawei Device Co., 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.
 */

#include "tls_client.h"

#include <stdio.h>
#include <string.h>
#include "securec.h"
L
liyufan 已提交
21
//#include "securec.h"
M
mamingshuai 已提交
22 23 24
#include "tls_certificate.h"
#include "mbedtls_log.h"

L
liyufan 已提交
25
#include "mbedtls/build_info.h"
M
mamingshuai 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

static void SslDebug(void *ctx, int level, const char *file, int line, const char *str)
{
    LOGD("%s:%04d: %s.", file, line, str);
}

static int MbedtlsSslCertificateVerify(MbedTLSSession *session)
{
    if (session == NULL) {
        return -RET_ERROR;
    }
    int ret = mbedtls_ssl_get_verify_result(&session->ssl);
    if (ret != 0) {
        LOGD("verify peer certificate fail...");
        (void)memset_s(session->buffer, session->buffer_len, 0x00, session->buffer_len);
        mbedtls_x509_crt_verify_info((char *)session->buffer, session->buffer_len, "  ! ", ret);
        LOGD("verification info: %s.", session->buffer);
        return -RET_ERROR;
    }
    return RET_EOK;
}

int MbedtlsClientInit(MbedTLSSession *session, void *entropy, size_t entropyLen)
{
    if (session == NULL || entropy == NULL) {
        return -RET_ERROR;
    }
    mbedtls_net_init(&session->server_fd);
    mbedtls_ssl_init(&session->ssl);
    mbedtls_ssl_config_init(&session->conf);
    mbedtls_ctr_drbg_init(&session->ctr_drbg);
    mbedtls_entropy_init(&session->entropy);
    mbedtls_x509_crt_init(&session->cacert);
    int ret = mbedtls_ctr_drbg_seed(&session->ctr_drbg, mbedtls_entropy_func, &session->entropy,
                                    (unsigned char *)entropy, entropyLen);
    if (ret != 0) {
        LOGD("mbedtls_ctr_drbg_seed error, return -0x%x.", -ret);
        return ret;
    }
    LOGD("mbedtls client struct init success...");
    return RET_EOK;
}

int MbedtlsClientClose(MbedTLSSession *session)
{
    if (session == NULL) {
        return -RET_ERROR;
    }
    mbedtls_ssl_close_notify(&session->ssl);
    mbedtls_net_free(&session->server_fd);
    mbedtls_x509_crt_free(&session->cacert);
    mbedtls_entropy_free(&session->entropy);
    mbedtls_ctr_drbg_free(&session->ctr_drbg);
    mbedtls_ssl_config_free(&session->conf);
    mbedtls_ssl_free(&session->ssl);
    LOGD("MbedTLS connection close success.");
    return RET_EOK;
}

int MbedtlsClientContext(MbedTLSSession *session)
{
    if (session == NULL) {
        return -RET_ERROR;
    }
    int ret = mbedtls_x509_crt_parse(&session->cacert, (const unsigned char *)G_MBEDTLS_ROOT_CERTIFICATE,
                                     G_MBEDTLS_ROOT_CERTIFICATE_LEN);
    if (ret < 0) {
M
maosiping 已提交
93
        LOGD("mbedtls_x509_crt_parse error,  return -0x%x.", -ret);
M
mamingshuai 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
        return ret;
    }

    LOGD("Loading the CA root certificate success...");

    // Hostname set here should match CN in server certificate
    if (session->host != NULL) {
        ret = mbedtls_ssl_set_hostname(&session->ssl, session->host);
        if (ret != 0) {
            LOGD("mbedtls_ssl_set_hostname error, return -0x%x", -ret);
            return ret;
        }
    }

    ret = mbedtls_ssl_config_defaults(&session->conf,
                                      MBEDTLS_SSL_IS_CLIENT,
                                      MBEDTLS_SSL_TRANSPORT_STREAM,
                                      MBEDTLS_SSL_PRESET_DEFAULT);
    if (ret != 0) {
        LOGD("mbedtls_ssl_config_defaults error, return -0x%x.", -ret);
        return ret;
    }

    // If you want to verify the validity of the server certificate,
    // you need to replace MBEDTLS_SSL_VERIFY_NONE with MBEDTLS_SSL_VERIFY_REQUIRED
    mbedtls_ssl_conf_authmode(&session->conf, MBEDTLS_SSL_VERIFY_NONE);
    mbedtls_ssl_conf_ca_chain(&session->conf, &session->cacert, NULL);
    mbedtls_ssl_conf_rng(&session->conf, mbedtls_ctr_drbg_random, &session->ctr_drbg);
    mbedtls_ssl_conf_dbg(&session->conf, SslDebug, NULL);
    ret = mbedtls_ssl_setup(&session->ssl, &session->conf);
    if (ret != 0) {
        LOGD("mbedtls_ssl_setup error, return -0x%x.", -ret);
        return ret;
    }
    LOGD("mbedtls client context init success...");

    return RET_EOK;
}

int MbedtlsClientConnect(MbedTLSSession *session)
{
    if (session == NULL) {
        return -RET_ERROR;
    }
M
maosiping 已提交
138
    LOGD("connect: host:%s, port: %s", session->host, session->port);
M
mamingshuai 已提交
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192

    int ret = mbedtls_net_connect(&session->server_fd, session->host, session->port, MBEDTLS_NET_PROTO_TCP);
    if (ret != 0) {
        LOGD("mbedtls_net_connect error, return -0x%x.", -ret);
        return ret;
    }
    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);

    while ((ret = mbedtls_ssl_handshake(&session->ssl)) != 0) {
        LOGD("mbedtls_ssl_handshake ret=0x%x.", -ret);
        if (RET_EOK != MbedtlsSslCertificateVerify(session)) {
            return -RET_ERROR;
        }
        if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
            LOGD("mbedtls_ssl_handshake error, return -0x%x.", -ret);
            return ret;
        }
    }

    if (RET_EOK != MbedtlsSslCertificateVerify(session)) {
        LOGD("Certificate verified err...");
        return -RET_ERROR;
    }

    LOGD("Certificate verified success...");

    return RET_EOK;
}

int MbedtlsClientRead(MbedTLSSession *session, unsigned char *buf, size_t len)
{
    if (session == NULL || buf == NULL) {
        return -RET_ERROR;
    }
    int ret = mbedtls_ssl_read(&session->ssl, (unsigned char *)buf, len);
    if (ret < 0 && ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
        LOGD("MbedtlsClientRead data error, return -0x%x.", -ret);
    }
    return ret;
}

int MbedtlsClientWrite(MbedTLSSession *session, const unsigned char *buf, size_t len)
{
    if (session == NULL || buf == NULL) {
        return -RET_ERROR;
    }
    int ret = mbedtls_ssl_write(&session->ssl, (unsigned char *)buf, len);
    if (ret < 0 && ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
        LOGD("MbedtlsClientWrite data error, return -0x%x.", -ret);
    }
    return ret;
}