Skip to content

Commit 6095fde

Browse files
committed
Allocation debugging, reduce SSL structure size.
1 parent 6c91aa1 commit 6095fde

File tree

10 files changed

+82
-116
lines changed

10 files changed

+82
-116
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ OBJ_FILES := \
2828
ssl/tls1_clnt.o \
2929
ssl/tls1_svr.o \
3030
ssl/x509.o \
31-
# crypto/crypto_misc.o \
31+
crypto/crypto_misc.o \
3232

3333

3434
CPPFLAGS += -I$(XTENSA_LIBS)/include \
@@ -63,7 +63,7 @@ $(BIN_DIR):
6363
mkdir -p $(BIN_DIR)
6464

6565
clean:
66-
rm -rf $(OBJ_FILES) $(LWIP_AR)
66+
rm -rf $(OBJ_FILES) $(AXTLS_AR)
6767

6868

6969
.PHONY: all clean

crypto/crypto_misc.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@
4242
#include "wincrypt.h"
4343
#endif
4444

45-
#ifndef WIN32
45+
#ifdef ESP8266
46+
#define CONFIG_SSL_SKELETON_MODE 1
47+
#endif
48+
49+
#if defined(CONFIG_USE_DEV_URANDOM)
4650
static int rng_fd = -1;
4751
#elif defined(CONFIG_WIN32_USE_CRYPTO_LIB)
4852
static HCRYPTPROV gCryptProv;
@@ -146,7 +150,7 @@ EXP_FUNC void STDCALL RNG_custom_init(const uint8_t *seed_buf, int size)
146150
*/
147151
EXP_FUNC void STDCALL RNG_terminate(void)
148152
{
149-
#ifndef WIN32
153+
#if defined(CONFIG_USE_DEV_URANDOM)
150154
close(rng_fd);
151155
#elif defined(CONFIG_WIN32_USE_CRYPTO_LIB)
152156
CryptReleaseContext(gCryptProv, 0);

crypto/rsa.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ int RSA_decrypt(const RSA_CTX *ctx, const uint8_t *in_data,
146146
const int byte_size = ctx->num_octets;
147147
int i, size;
148148
bigint *decrypted_bi, *dat_bi;
149-
uint8_t *block = (uint8_t *)alloca(byte_size);
149+
uint8_t *block = (uint8_t *)malloc(byte_size);
150150

151151
memset(out_data, 0, byte_size); /* initialise */
152152

@@ -182,7 +182,7 @@ int RSA_decrypt(const RSA_CTX *ctx, const uint8_t *in_data,
182182
/* get only the bit we want */
183183
if (size > 0)
184184
memcpy(out_data, &block[i], size);
185-
185+
free(block);
186186
return size ? size : -1;
187187
}
188188

ssl/config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
#define CONFIG_X509_MAX_CA_CERTS 150
4949
#define CONFIG_SSL_MAX_CERTS 3
5050
#undef CONFIG_SSL_CTX_MUTEXING
51-
//#define CONFIG_USE_DEV_URANDOM 1
51+
#undef CONFIG_USE_DEV_URANDOM
5252
#undef CONFIG_WIN32_USE_CRYPTO_LIB
5353
#undef CONFIG_OPENSSL_COMPATIBLE
5454
#undef CONFIG_PERFORMANCE_TESTING

ssl/gen_cert.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,14 +214,14 @@ static void gen_utc_time(uint8_t *buf, int *offset)
214214

215215
static void gen_pub_key2(const RSA_CTX *rsa_ctx, uint8_t *buf, int *offset)
216216
{
217-
static const uint8_t pub_key_seq[] =
217+
static const uint8_t pub_key_seq[] =
218218
{
219219
ASN1_INTEGER, 0x03, 0x01, 0x00, 0x01 /* INTEGER 65537 */
220220
};
221221

222222
int seq_offset;
223223
int pub_key_size = rsa_ctx->num_octets;
224-
uint8_t *block = (uint8_t *)alloca(pub_key_size);
224+
uint8_t *block = (uint8_t *)malloc(pub_key_size);
225225
int seq_size = pre_adjust_with_size(
226226
ASN1_SEQUENCE, &seq_offset, buf, offset);
227227
buf[(*offset)++] = ASN1_INTEGER;
@@ -236,6 +236,7 @@ static void gen_pub_key2(const RSA_CTX *rsa_ctx, uint8_t *buf, int *offset)
236236
set_gen_length(pub_key_size, buf, offset);
237237

238238
memcpy(&buf[*offset], block, pub_key_size);
239+
free(block);
239240
*offset += pub_key_size;
240241
memcpy(&buf[*offset], pub_key_seq, sizeof(pub_key_seq));
241242
*offset += sizeof(pub_key_seq);
@@ -282,8 +283,8 @@ static void gen_signature(const RSA_CTX *rsa_ctx, const uint8_t *sha_dgst,
282283
ASN1_NULL, 0x00, ASN1_OCTET_STRING, 0x14
283284
};
284285

285-
uint8_t *enc_block = (uint8_t *)alloca(rsa_ctx->num_octets);
286-
uint8_t *block = (uint8_t *)alloca(sizeof(asn1_sig) + SHA1_SIZE);
286+
uint8_t *enc_block = (uint8_t *)malloc(rsa_ctx->num_octets);
287+
uint8_t *block = (uint8_t *)malloc(sizeof(asn1_sig) + SHA1_SIZE);
287288
int sig_size;
288289

289290
/* add the digest as an embedded asn.1 sequence */
@@ -297,6 +298,8 @@ static void gen_signature(const RSA_CTX *rsa_ctx, const uint8_t *sha_dgst,
297298
set_gen_length(sig_size+1, buf, offset);
298299
buf[(*offset)++] = 0; /* bit string is multiple of 8 */
299300
memcpy(&buf[*offset], enc_block, sig_size);
301+
free(enc_block);
302+
free(block);
300303
*offset += sig_size;
301304
}
302305

@@ -342,7 +345,7 @@ EXP_FUNC int STDCALL ssl_x509_create(SSL_CTX *ssl_ctx, uint32_t options, const c
342345
{
343346
int ret = X509_OK, offset = 0, seq_offset;
344347
/* allocate enough space to load a new certificate */
345-
uint8_t *buf = (uint8_t *)alloca(ssl_ctx->rsa_ctx->num_octets*2 + 512);
348+
uint8_t *buf = (uint8_t *)malloc(ssl_ctx->rsa_ctx->num_octets*2 + 512);
346349
uint8_t sha_dgst[SHA1_SIZE];
347350
int seq_size = pre_adjust_with_size(ASN1_SEQUENCE,
348351
&seq_offset, buf, &offset);
@@ -357,6 +360,7 @@ EXP_FUNC int STDCALL ssl_x509_create(SSL_CTX *ssl_ctx, uint32_t options, const c
357360
memcpy(*cert_data, buf, offset);
358361

359362
error:
363+
free(buf);
360364
return ret < 0 ? ret : offset;
361365
}
362366

ssl/os_port.c

Lines changed: 13 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
/*
22
* Copyright (c) 2007, Cameron Rich
3-
*
3+
*
44
* All rights reserved.
5-
*
6-
* Redistribution and use in source and binary forms, with or without
5+
*
6+
* Redistribution and use in source and binary forms, with or without
77
* modification, are permitted provided that the following conditions are met:
88
*
9-
* * Redistributions of source code must retain the above copyright notice,
9+
* * Redistributions of source code must retain the above copyright notice,
1010
* this list of conditions and the following disclaimer.
11-
* * Redistributions in binary form must reproduce the above copyright notice,
12-
* this list of conditions and the following disclaimer in the documentation
11+
* * Redistributions in binary form must reproduce the above copyright notice,
12+
* this list of conditions and the following disclaimer in the documentation
1313
* and/or other materials provided with the distribution.
14-
* * Neither the name of the axTLS project nor the names of its contributors
15-
* may be used to endorse or promote products derived from this software
14+
* * Neither the name of the axTLS project nor the names of its contributors
15+
* may be used to endorse or promote products derived from this software
1616
* without specific prior written permission.
1717
*
1818
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
@@ -37,17 +37,18 @@
3737
#include <stdlib.h>
3838
#include <errno.h>
3939
#include <stdarg.h>
40+
#include <string.h>
4041
#include "os_port.h"
4142

4243
#ifdef WIN32
4344
/**
44-
* gettimeofday() not in Win32
45+
* gettimeofday() not in Win32
4546
*/
4647
EXP_FUNC void STDCALL gettimeofday(struct timeval* t, void* timezone)
47-
{
48+
{
4849
#if defined(_WIN32_WCE)
4950
t->tv_sec = time(NULL);
50-
t->tv_usec = 0; /* 1sec precision only */
51+
t->tv_usec = 0; /* 1sec precision only */
5152
#else
5253
struct _timeb timebuffer;
5354
_ftime(&timebuffer);
@@ -86,7 +87,7 @@ EXP_FUNC int STDCALL getdomainname(char *buf, int buf_size)
8687

8788
RegQueryValueEx(hKey, "Domain", NULL, &datatype, buf, &bufferlength);
8889
RegCloseKey(hKey);
89-
return 0;
90+
return 0;
9091
}
9192
#endif
9293

@@ -96,63 +97,3 @@ EXP_FUNC int STDCALL getdomainname(char *buf, int buf_size)
9697

9798
static const char * out_of_mem_str = "out of memory";
9899
static const char * file_open_str = "Could not open file \"%s\"";
99-
100-
/*
101-
* Some functions that call display some error trace and then call abort().
102-
* This just makes life much easier on embedded systems, since we're
103-
* suffering major trauma...
104-
*/
105-
EXP_FUNC void * STDCALL ax_malloc(size_t s)
106-
{
107-
void *x;
108-
109-
if ((x = malloc(s)) == NULL)
110-
exit_now(out_of_mem_str);
111-
112-
return x;
113-
}
114-
115-
EXP_FUNC void * STDCALL ax_realloc(void *y, size_t s)
116-
{
117-
void *x;
118-
119-
if ((x = realloc(y, s)) == NULL)
120-
exit_now(out_of_mem_str);
121-
122-
return x;
123-
}
124-
125-
EXP_FUNC void * STDCALL ax_calloc(size_t n, size_t s)
126-
{
127-
void *x;
128-
129-
if ((x = calloc(n, s)) == NULL)
130-
exit_now(out_of_mem_str);
131-
132-
return x;
133-
}
134-
135-
EXP_FUNC int STDCALL ax_open(const char *pathname, int flags)
136-
{
137-
int x;
138-
139-
if ((x = open(pathname, flags)) < 0)
140-
exit_now(file_open_str, pathname);
141-
142-
return x;
143-
}
144-
145-
/**
146-
* This is a call which will deliberately exit an application, but will
147-
* display some information before dying.
148-
*/
149-
void exit_now(const char *format, ...)
150-
{
151-
va_list argp;
152-
153-
va_start(argp, format);
154-
vfprintf(stderr, format, argp);
155-
va_end(argp);
156-
abort();
157-
}
158-

ssl/os_port.h

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
/*
22
* Copyright (c) 2007, Cameron Rich
3-
*
3+
*
44
* All rights reserved.
5-
*
6-
* Redistribution and use in source and binary forms, with or without
5+
*
6+
* Redistribution and use in source and binary forms, with or without
77
* modification, are permitted provided that the following conditions are met:
88
*
9-
* * Redistributions of source code must retain the above copyright notice,
9+
* * Redistributions of source code must retain the above copyright notice,
1010
* this list of conditions and the following disclaimer.
11-
* * Redistributions in binary form must reproduce the above copyright notice,
12-
* this list of conditions and the following disclaimer in the documentation
11+
* * Redistributions in binary form must reproduce the above copyright notice,
12+
* this list of conditions and the following disclaimer in the documentation
1313
* and/or other materials provided with the distribution.
14-
* * Neither the name of the axTLS project nor the names of its contributors
15-
* may be used to endorse or promote products derived from this software
14+
* * Neither the name of the axTLS project nor the names of its contributors
15+
* may be used to endorse or promote products derived from this software
1616
* without specific prior written permission.
1717
*
1818
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
@@ -44,9 +44,6 @@ extern "C" {
4444
#include "os_int.h"
4545
#include <stdio.h>
4646

47-
48-
49-
5047
#ifdef WIN32
5148
#define STDCALL __stdcall
5249
#define EXP_FUNC __declspec(dllexport)
@@ -63,7 +60,8 @@ extern "C" {
6360
#if defined(ESP8266)
6461

6562
#include "util/time.h"
66-
#define alloca(size) __builtin_alloca(size)
63+
#include <errno.h>
64+
// #define alloca(size) __builtin_alloca(size)
6765
#define TTY_FLUSH()
6866
#ifdef putc
6967
#undef putc
@@ -74,6 +72,15 @@ extern "C" {
7472
#endif
7573
#define printf(...) ets_printf(__VA_ARGS__)
7674

75+
#define SOCKET_READ(A,B,C) ax_port_read(A,B,C)
76+
#define SOCKET_WRITE(A,B,C) ax_port_write(A,B,C)
77+
#define SOCKET_CLOSE(A) ax_port_close(A)
78+
#define get_file ax_get_file
79+
#define EWOULDBLOCK EAGAIN
80+
81+
#define hmac_sha1 ax_hmac_sha1
82+
#define hmac_md5 ax_hmac_md5
83+
7784
#elif defined(WIN32)
7885

7986
/* Windows CE stuff */
@@ -122,7 +129,7 @@ extern "C" {
122129

123130
/* This fix gets around a problem where a win32 application on a cygwin xterm
124131
doesn't display regular output (until a certain buffer limit) - but it works
125-
fine under a normal DOS window. This is a hack to get around the issue -
132+
fine under a normal DOS window. This is a hack to get around the issue -
126133
see http://www.khngai.com/emacs/tty.php */
127134
#define TTY_FLUSH() if (!_isatty(_fileno(stdout))) fflush(stdout);
128135

@@ -161,16 +168,27 @@ EXP_FUNC int STDCALL getdomainname(char *buf, int buf_size);
161168
#endif /* Not Win32 */
162169

163170
/* some functions to mutate the way these work */
164-
#define malloc(A) ax_malloc(A)
171+
#define malloc(A) ax_port_malloc(A, __FILE__, __LINE__)
165172
#ifndef realloc
166-
#define realloc(A,B) ax_realloc(A,B)
173+
#define realloc(A,B) ax_port_realloc(A,B, __FILE__, __LINE__)
167174
#endif
168-
#define calloc(A,B) ax_calloc(A,B)
175+
#define calloc(A,B) ax_port_calloc(A,B, __FILE__, __LINE__)
176+
#define free(x) ax_port_free(x)
177+
178+
EXP_FUNC void * STDCALL ax_port_malloc(size_t s, const char*, int);
179+
EXP_FUNC void * STDCALL ax_port_realloc(void *y, size_t s, const char*, int);
180+
EXP_FUNC void * STDCALL ax_port_calloc(size_t n, size_t s, const char*, int);
181+
EXP_FUNC void * STDCALL ax_port_free(void*);
182+
EXP_FUNC int STDCALL ax_open(const char *pathname, int flags);
183+
184+
inline uint32_t htonl(uint32_t n){
185+
return ((n & 0xff) << 24) |
186+
((n & 0xff00) << 8) |
187+
((n & 0xff0000UL) >> 8) |
188+
((n & 0xff000000UL) >> 24);
189+
}
169190

170-
EXP_FUNC void * STDCALL ax_malloc(size_t s);
171-
EXP_FUNC void * STDCALL ax_realloc(void *y, size_t s);
172-
EXP_FUNC void * STDCALL ax_calloc(size_t n, size_t s);
173-
EXP_FUNC int STDCALL ax_open(const char *pathname, int flags);
191+
#define ntohl htonl
174192

175193
#ifdef CONFIG_PLATFORM_LINUX
176194
void exit_now(const char *format, ...) __attribute((noreturn));
@@ -186,7 +204,7 @@ void exit_now(const char *format, ...);
186204
#define SSL_CTX_MUTEX_DESTROY(A) CloseHandle(A)
187205
#define SSL_CTX_LOCK(A) WaitForSingleObject(A, INFINITE)
188206
#define SSL_CTX_UNLOCK(A) ReleaseMutex(A)
189-
#else
207+
#else
190208
#include <pthread.h>
191209
#define SSL_CTX_MUTEX_TYPE pthread_mutex_t
192210
#define SSL_CTX_MUTEX_INIT(A) pthread_mutex_init(&A, NULL)
@@ -205,4 +223,4 @@ void exit_now(const char *format, ...);
205223
}
206224
#endif
207225

208-
#endif
226+
#endif

0 commit comments

Comments
 (0)