mirror of
https://github.com/espruino/Espruino.git
synced 2025-12-08 19:06:15 +00:00
Now automatically allocating all required data in JsVars
This commit is contained in:
parent
37ebb25235
commit
3db2f75ea1
@ -28,6 +28,7 @@
|
|||||||
Allow events of >12 characters length
|
Allow events of >12 characters length
|
||||||
Fix regression in flash memory write (introduced via AES merge)
|
Fix regression in flash memory write (introduced via AES merge)
|
||||||
Fixed instability when resetting after using SD card on non-standard pins
|
Fixed instability when resetting after using SD card on non-standard pins
|
||||||
|
HTTPS support on Pico (when compiled in)
|
||||||
|
|
||||||
1v81 : Fix regression on UART4/5 (bug #559)
|
1v81 : Fix regression on UART4/5 (bug #559)
|
||||||
Fix Serial3 on C10/C11 for F103 boards (fix #409)
|
Fix Serial3 on C10/C11 for F103 boards (fix #409)
|
||||||
|
|||||||
@ -32,6 +32,10 @@
|
|||||||
|
|
||||||
#define MBEDTLS_PLATFORM_SNPRINTF_MACRO espruino_snprintf
|
#define MBEDTLS_PLATFORM_SNPRINTF_MACRO espruino_snprintf
|
||||||
|
|
||||||
|
// See aes.c. Do we want 10kB of data full of constants? no.
|
||||||
|
#define MBEDTLS_AES_ROM_TABLES
|
||||||
|
|
||||||
|
|
||||||
#ifdef USE_HTTPS
|
#ifdef USE_HTTPS
|
||||||
|
|
||||||
/* mbed TLS feature support */
|
/* mbed TLS feature support */
|
||||||
|
|||||||
@ -10,7 +10,7 @@
|
|||||||
* ----------------------------------------------------------------------------
|
* ----------------------------------------------------------------------------
|
||||||
* Contains functions for handling JsNetwork and doing common networking tasks
|
* Contains functions for handling JsNetwork and doing common networking tasks
|
||||||
* ----------------------------------------------------------------------------
|
* ----------------------------------------------------------------------------
|
||||||
*/#define USE_HTTPS
|
*/
|
||||||
#include "network.h"
|
#include "network.h"
|
||||||
#include "jsparse.h"
|
#include "jsparse.h"
|
||||||
#include "jsinteractive.h"
|
#include "jsinteractive.h"
|
||||||
@ -259,10 +259,6 @@ typedef struct {
|
|||||||
mbedtls_ssl_config conf;
|
mbedtls_ssl_config conf;
|
||||||
} SSLSocketData;
|
} SSLSocketData;
|
||||||
|
|
||||||
SSLSocketData _sd;
|
|
||||||
SSLSocketData *sd = &_sd; // make it easier to use pointers later
|
|
||||||
|
|
||||||
|
|
||||||
BITFIELD_DECL(socketIsHTTPS, 32);
|
BITFIELD_DECL(socketIsHTTPS, 32);
|
||||||
|
|
||||||
static void ssl_debug( void *ctx, int level,
|
static void ssl_debug( void *ctx, int level,
|
||||||
@ -302,22 +298,59 @@ int ssl_entropy( void *data, unsigned char *output, size_t len ) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ssl_freeSocketData(int sckt) {
|
||||||
|
BITFIELD_SET(socketIsHTTPS, sckt, 0);
|
||||||
|
|
||||||
|
JsVar *ssl = jsvObjectGetChild(execInfo.root, "ssl", 0);
|
||||||
|
if (!ssl) return;
|
||||||
|
JsVar *scktVar = jsvNewFromInteger(sckt);
|
||||||
|
JsVar *sslDataVar = jsvFindChildFromVar(ssl, scktVar, false);
|
||||||
|
jsvUnLock(scktVar);
|
||||||
|
JsVar *sslData = jsvSkipName(sslDataVar);
|
||||||
|
jsvRemoveChild(ssl, sslDataVar);
|
||||||
|
jsvUnLock(sslDataVar);
|
||||||
|
jsvUnLock(ssl);
|
||||||
|
SSLSocketData *sd = 0;
|
||||||
|
if (jsvIsFlatString(sslData)) {
|
||||||
|
sd = (SSLSocketData *)jsvGetFlatStringPointer(sslData);
|
||||||
|
mbedtls_ssl_free( &sd->ssl );
|
||||||
|
mbedtls_ssl_config_free( &sd->conf );
|
||||||
|
mbedtls_ctr_drbg_free( &sd->ctr_drbg );
|
||||||
|
}
|
||||||
|
jsvUnLock(sslData);
|
||||||
|
}
|
||||||
|
|
||||||
bool ssl_newSocketData(int sckt) {
|
bool ssl_newSocketData(int sckt) {
|
||||||
|
/* FIXME Warning:
|
||||||
|
*
|
||||||
|
* MBEDTLS_SSL_MAX_CONTENT_LEN = 16kB, so we need over double this = 32kB memory
|
||||||
|
* for just a single connection!!
|
||||||
|
*
|
||||||
|
* Also see https://tls.mbed.org/kb/how-to/reduce-mbedtls-memory-and-storage-footprint
|
||||||
|
* */
|
||||||
|
|
||||||
assert(sckt>=0 && sckt<32);
|
assert(sckt>=0 && sckt<32);
|
||||||
// Create a new socketData using the variable
|
// Create a new socketData using the variable
|
||||||
/*JsVar *ssl = jsvObjectGetChild(execInfo.root, "ssl", JSV_OBJECT);
|
JsVar *ssl = jsvObjectGetChild(execInfo.root, "ssl", JSV_OBJECT);
|
||||||
if (!ssl) return false; // out of memory?
|
if (!ssl) return false; // out of memory?
|
||||||
JsVar *sslData = jsvGetArrayItem(ssl, sckt);
|
JsVar *scktVar = jsvNewFromInteger(sckt);
|
||||||
assert(!sslData); // we should NOT already have socket data
|
JsVar *sslDataVar = jsvFindChildFromVar(ssl, scktVar, true);
|
||||||
sslData = jsvNewFlatStringOfLength(sizeof(SSLSocketData));
|
jsvUnLock(scktVar);
|
||||||
|
jsvUnLock(ssl);
|
||||||
|
if (!sslDataVar) {
|
||||||
|
return 0; // out of memory
|
||||||
|
}
|
||||||
|
JsVar *sslData = jsvNewFlatStringOfLength(sizeof(SSLSocketData));
|
||||||
if (!sslData) {
|
if (!sslData) {
|
||||||
jsError("Not enough memory to allocate SSL socket\n");
|
jsError("Not enough memory to allocate SSL socket\n");
|
||||||
jsvUnLock(ssl);
|
jsvUnLock(sslDataVar);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
SSLSocketData *sd = jsvGetFlatStringPointer(sslData);
|
jsvSetValueOfName(sslDataVar, sslData);
|
||||||
assert(sslData);*/
|
jsvUnLock(sslDataVar);
|
||||||
JsVar *sslData = 0;
|
SSLSocketData *sd = (SSLSocketData *)jsvGetFlatStringPointer(sslData);
|
||||||
|
jsvUnLock(sslData);
|
||||||
|
assert(sd);
|
||||||
|
|
||||||
// Now initialise this
|
// Now initialise this
|
||||||
sd->sckt = sckt;
|
sd->sckt = sckt;
|
||||||
@ -336,7 +369,7 @@ bool ssl_newSocketData(int sckt) {
|
|||||||
(const unsigned char *) pers,
|
(const unsigned char *) pers,
|
||||||
strlen(pers))) != 0 ) {
|
strlen(pers))) != 0 ) {
|
||||||
jsError("HTTPS init failed! mbedtls_ctr_drbg_seed returned %d\n", ret );
|
jsError("HTTPS init failed! mbedtls_ctr_drbg_seed returned %d\n", ret );
|
||||||
jsvUnLock(sslData);
|
ssl_freeSocketData(sckt);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -345,7 +378,7 @@ bool ssl_newSocketData(int sckt) {
|
|||||||
MBEDTLS_SSL_TRANSPORT_STREAM,
|
MBEDTLS_SSL_TRANSPORT_STREAM,
|
||||||
MBEDTLS_SSL_PRESET_DEFAULT )) != 0 ) {
|
MBEDTLS_SSL_PRESET_DEFAULT )) != 0 ) {
|
||||||
jsError( "HTTPS init failed! mbedtls_ssl_config_defaults returned %d\n", ret );
|
jsError( "HTTPS init failed! mbedtls_ssl_config_defaults returned %d\n", ret );
|
||||||
jsvUnLock(sslData);
|
ssl_freeSocketData(sckt);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -357,13 +390,13 @@ bool ssl_newSocketData(int sckt) {
|
|||||||
|
|
||||||
if (( ret = mbedtls_ssl_setup( &sd->ssl, &sd->conf )) != 0) {
|
if (( ret = mbedtls_ssl_setup( &sd->ssl, &sd->conf )) != 0) {
|
||||||
jsError( "Failed! mbedtls_ssl_setup returned %d\n", ret );
|
jsError( "Failed! mbedtls_ssl_setup returned %d\n", ret );
|
||||||
jsvUnLock(sslData);
|
ssl_freeSocketData(sckt);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (( ret = mbedtls_ssl_set_hostname( &sd->ssl, "mbed TLS Server 1" )) != 0) {
|
if (( ret = mbedtls_ssl_set_hostname( &sd->ssl, "mbed TLS Server 1" )) != 0) {
|
||||||
jsError( "HTTPS init failed! mbedtls_ssl_set_hostname returned %d\n", ret );
|
jsError( "HTTPS init failed! mbedtls_ssl_set_hostname returned %d\n", ret );
|
||||||
jsvUnLock(sslData);
|
ssl_freeSocketData(sckt);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -371,29 +404,21 @@ bool ssl_newSocketData(int sckt) {
|
|||||||
|
|
||||||
jsiConsolePrintf( "Performing the SSL/TLS handshake...\n" );
|
jsiConsolePrintf( "Performing the SSL/TLS handshake...\n" );
|
||||||
|
|
||||||
// we're good. Add it to the array
|
|
||||||
jsiConsolePrintf( " FIXME ADD TO ARRAY " );
|
|
||||||
jsvUnLock(sslData);
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ssl_freeSocketData(int sckt) {
|
|
||||||
BITFIELD_SET(socketIsHTTPS, sckt, 0);
|
|
||||||
mbedtls_ssl_free( &sd->ssl );
|
|
||||||
mbedtls_ssl_config_free( &sd->conf );
|
|
||||||
mbedtls_ctr_drbg_free( &sd->ctr_drbg );
|
|
||||||
}
|
|
||||||
|
|
||||||
SSLSocketData *ssl_getSocketData(int sckt) {
|
SSLSocketData *ssl_getSocketData(int sckt) {
|
||||||
// try and find the socket data variable
|
// try and find the socket data variable
|
||||||
/* JsVar *ssl = jsvObjectGetChild(execInfo.root, "ssl", 0);
|
JsVar *ssl = jsvObjectGetChild(execInfo.root, "ssl", 0);
|
||||||
if (!ssl) return 0;
|
if (!ssl) return 0;
|
||||||
JsVar *sslData = jsvGetArrayItem(ssl, sckt);
|
JsVar *sslData = jsvGetArrayItem(ssl, sckt);
|
||||||
|
jsvUnLock(ssl);
|
||||||
SSLSocketData *sd = 0;
|
SSLSocketData *sd = 0;
|
||||||
if (jsvIsFlatString(sslData))
|
if (jsvIsFlatString(sslData))
|
||||||
sd = jsvGetFlatStringPointer(sslData);
|
sd = (SSLSocketData *)jsvGetFlatStringPointer(sslData);
|
||||||
jsvUnLock(sslData);*/
|
jsvUnLock(sslData);
|
||||||
|
|
||||||
// now continue with connection
|
// now continue with connection
|
||||||
if (sd->connecting) {
|
if (sd->connecting) {
|
||||||
|
|||||||
@ -2164,6 +2164,7 @@ JsVar *jsvFindChildFromVar(JsVar *parent, JsVar *childName, bool addIfNotFound)
|
|||||||
|
|
||||||
void jsvRemoveChild(JsVar *parent, JsVar *child) {
|
void jsvRemoveChild(JsVar *parent, JsVar *child) {
|
||||||
assert(jsvHasChildren(parent));
|
assert(jsvHasChildren(parent));
|
||||||
|
assert(jsvIsName(child));
|
||||||
JsVarRef childref = jsvGetRef(child);
|
JsVarRef childref = jsvGetRef(child);
|
||||||
bool wasChild = false;
|
bool wasChild = false;
|
||||||
// unlink from parent
|
// unlink from parent
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user