275 lines
9.2 KiB
C
275 lines
9.2 KiB
C
/******************************************************************************************************************************
|
|
* MODULE IMPLEMENTATION FILE
|
|
******************************************************************************************************************************
|
|
* COPYRIGHT (C) 2019 ASR Corporation.
|
|
*
|
|
* This file and the software in it is furnished under
|
|
* license and may only be used or copied in accordance with the terms of the
|
|
* license. The information in this file is furnished for informational use
|
|
* only, is subject to change without notice, and should not be construed as
|
|
* a commitment by ASR Corporation. ASR Corporation assumes no
|
|
* responsibility or liability for any errors or inaccuracies that may appear
|
|
* in this document or any software that may be provided in association with
|
|
* this document.
|
|
* Except as permitted by such license, no part of this document may be
|
|
* reproduced, stored in a retrieval system, or transmitted in any form or by
|
|
* any means without the express written consent of ASR Corporation.
|
|
*
|
|
* Title: HTTPS Package HTTPS API
|
|
*
|
|
* Filename: ssl_http_client.c
|
|
* Target, subsystem: Common Platform, HAL
|
|
*
|
|
* Authors: Roy Shi
|
|
*
|
|
* Description: This file includes all the API that the https Package support
|
|
*
|
|
* Last Modified: <Initial> <date>
|
|
*
|
|
* Notes:
|
|
*********************************************************************************************************************************/
|
|
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include "sockets.h"
|
|
#include "ssl_http_client.h"
|
|
#include "httpclient_sys.h"
|
|
#include "mbedtls/config.h"
|
|
|
|
#if !defined(NOT_SUPPORT_HTTPS)
|
|
|
|
#define ssl_debug(fmt, args...) do { CPUartLogPrintf("[ssl]"fmt, ##args); } while(0)
|
|
|
|
#ifdef MBEDTLS_X509_USE_C
|
|
//#define CA_CERT
|
|
//#define OWN_CERT
|
|
#endif
|
|
|
|
#ifdef CA_CERT
|
|
const unsigned char ca1_cert[] = {
|
|
"-----BEGIN CERTIFICATE-----\r\n"
|
|
"MIIFZjCCA06gAwIBAgIIP61/1qm/uDowDQYJKoZIhvcNAQELBQAwUTELMAkGA1UE\r\n"
|
|
"BhMCRVMxFDASBgNVBAoMC1N0YXJ0Q29tIENBMSwwKgYDVQQDDCNTdGFydENvbSBD\r\n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};
|
|
|
|
#endif
|
|
static unsigned int havege_rand(void *unused, unsigned char * unusedstr, size_t unusedt)
|
|
{
|
|
char * p = (char *)malloc(16);
|
|
unsigned int rand = (unsigned int)p;
|
|
free(p);
|
|
if ((rand & 0xff) == 0) {
|
|
rand |= 0x96;
|
|
}
|
|
return rand;
|
|
}
|
|
|
|
static void debug(void *userdata, int level,
|
|
const char *filename, int line,
|
|
const char *msg)
|
|
{
|
|
ssl_debug("%s", msg);
|
|
}
|
|
|
|
static int ssl_client_recv(void * ctx, unsigned char * buf, unsigned int len)
|
|
{
|
|
struct ssl_client * client = (struct ssl_client *)ctx;
|
|
return recv(client->fd, buf, len, 0);
|
|
}
|
|
|
|
static int ssl_client_send(void * ctx, const unsigned char *buf, unsigned int len)
|
|
{
|
|
struct ssl_client * client = (struct ssl_client *)ctx;
|
|
return send(client->fd, buf, len, 0);
|
|
}
|
|
|
|
#ifdef CA_CERT
|
|
#define HOSTNAME "asr"
|
|
#endif
|
|
struct ssl_client * ssl_client_init(int fd)
|
|
{
|
|
char *pers = "ssl_client";
|
|
int ret;
|
|
int res=0;
|
|
struct ssl_client * client = (struct ssl_client *)malloc(sizeof(struct ssl_client));
|
|
if (!client) {
|
|
|
|
res=-1;
|
|
goto failed;
|
|
}
|
|
|
|
memset(client, 0, sizeof(struct ssl_client));
|
|
|
|
#ifdef CA_CERT
|
|
mbedtls_x509_crt *ca_chain;
|
|
ca_chain = (mbedtls_x509_crt *)malloc(sizeof(mbedtls_x509_crt));
|
|
memset(ca_chain, 0, sizeof(mbedtls_x509_crt));
|
|
#endif
|
|
|
|
mbedtls_entropy_init(&client->entropy);
|
|
mbedtls_ctr_drbg_init(&client->ctr_drbg);
|
|
if((ret = mbedtls_ctr_drbg_seed(&client->ctr_drbg, mbedtls_entropy_func,
|
|
&client->entropy,
|
|
(unsigned char *)pers, strlen(pers))) != 0)
|
|
{
|
|
res=-2;
|
|
goto failed;
|
|
}
|
|
|
|
mbedtls_ssl_init(&(client->ssl));
|
|
mbedtls_ssl_config_init(&(client->config));
|
|
client->fd = fd;
|
|
mbedtls_ssl_conf_rng(&(client->config), mbedtls_ctr_drbg_random, &client->ctr_drbg);
|
|
|
|
#ifdef CA_CERT
|
|
mbedtls_x509_crt_parse(ca_chain,ca1_cert,strlen(ca1_cert));
|
|
mbedtls_ssl_conf_ca_chain(&(client->config), ca_chain, NULL);
|
|
mbedtls_ssl_set_hostname(&(client->ssl), HOSTNAME);
|
|
#endif
|
|
|
|
#ifdef OWN_CERT
|
|
mbedtls_x509_crt *client_chain = NULL;
|
|
mbedtls_pk_context *client_rsa = NULL;
|
|
|
|
client_chain = (mbedtls_x509_crt *)malloc(sizeof(mbedtls_x509_crt));
|
|
memset(client_chain, 0, sizeof(mbedtls_x509_crt));
|
|
mbedtls_x509_crt_parse(client_chain,client_cert,strlen(client_cert));
|
|
|
|
client_rsa = (mbedtls_pk_context *)malloc(sizeof(mbedtls_pk_context));
|
|
memset(client_rsa, 0, sizeof(mbedtls_pk_context));
|
|
mbedtls_pk_parse_key(client_rsa, client_key, strlen(client_key), NULL,0);
|
|
mbedtls_ssl_conf_own_cert(&client->config, client_chain, client_rsa);
|
|
#endif
|
|
|
|
//mbedtls_ssl_conf_endpoint(&(client->config), MBEDTLS_SSL_IS_CLIENT);
|
|
// if using MBEDTLS_SSL_PRESET_SUITEB,need open SUPPORT_TLS_SUITEB in config-no-entropy.h
|
|
|
|
#ifdef LPA_SUPPORT
|
|
mbedtls_ssl_config_defaults(&client->config, MBEDTLS_SSL_IS_CLIENT,
|
|
MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_SUITEB);
|
|
#else
|
|
mbedtls_ssl_config_defaults(&client->config, MBEDTLS_SSL_IS_CLIENT,
|
|
MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT);
|
|
#endif
|
|
|
|
#ifdef CA_CERT
|
|
mbedtls_ssl_conf_authmode(&client->config, MBEDTLS_SSL_VERIFY_REQUIRED);
|
|
#else
|
|
mbedtls_ssl_conf_authmode(&client->config, MBEDTLS_SSL_VERIFY_OPTIONAL);
|
|
#endif
|
|
|
|
mbedtls_ssl_conf_rng(&client->config, mbedtls_ctr_drbg_random, &client->ctr_drbg);
|
|
mbedtls_ssl_conf_dbg(&client->config, debug, NULL);
|
|
//mbedtls_debug_set_threshold(1000);
|
|
mbedtls_ssl_set_bio(&client->ssl, client,
|
|
ssl_client_send, ssl_client_recv, NULL);
|
|
|
|
mbedtls_ssl_setup(&client->ssl, &client->config);
|
|
return client;
|
|
failed:
|
|
if(res!=0)
|
|
http_error(192,"%s failed,res[%d]\n",__func__,res);
|
|
#ifdef CA_CERT
|
|
if(ca_chain) free(ca_chain);
|
|
#endif
|
|
if (client) free(client);
|
|
return NULL;
|
|
}
|
|
|
|
|
|
|
|
|
|
void ssl_client_shutdown(struct ssl_client * client)
|
|
{
|
|
|
|
mbedtls_ssl_config *config=NULL;
|
|
|
|
if (!client) return;
|
|
#ifdef CA_CERT
|
|
config = (mbedtls_ssl_config *)&(client->config);
|
|
if(config->ca_chain){
|
|
mbedtls_x509_crt_free(config->ca_chain);
|
|
free(config->ca_chain);
|
|
}
|
|
#endif
|
|
|
|
#ifdef OWN_CERT
|
|
mbedtls_ssl_key_cert * cert=NULL;
|
|
config = (mbedtls_ssl_config *)&(client->config);
|
|
cert = config->key_cert;
|
|
|
|
if(cert&&cert->cert){
|
|
free(cert->cert);
|
|
cert->cert=NULL;
|
|
}
|
|
if(cert&&cert->key){
|
|
free(cert->key);
|
|
cert->key=NULL;
|
|
}
|
|
#endif
|
|
|
|
http_debug(113,"ssl close notify");
|
|
mbedtls_ssl_close_notify(&client->ssl);
|
|
http_debug(114,"ssl ctr drbg free");
|
|
mbedtls_ctr_drbg_free(&client->ctr_drbg);
|
|
http_debug(115,"ssl entropy free");
|
|
mbedtls_entropy_free(&client->entropy);
|
|
http_debug(116,"ssl free");
|
|
mbedtls_ssl_free(&client->ssl);
|
|
http_debug(117,"ssl config free");
|
|
mbedtls_ssl_config_free(&client->config);
|
|
http_debug(118,"client free");
|
|
free(client);
|
|
client = NULL;
|
|
http_debug(119,"shutdown end");
|
|
}
|
|
|
|
int ssl_client_write( void *ssl, const unsigned char *buf, int len )
|
|
{
|
|
return ssl_write(ssl, buf, len );
|
|
}
|
|
|
|
int ssl_client_read( void *ssl, unsigned char *buf, int len )
|
|
{
|
|
return ssl_read( ssl, buf, len );
|
|
}
|
|
|
|
#endif
|