Skip to content

Commit a682206

Browse files
authored
Merge pull request esp8266#19 from ikeyasu/merge-200
Merging axtls 2.0.0
2 parents ab516f7 + 9ca7e76 commit a682206

21 files changed

+843
-440
lines changed

crypto/aes.c

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2007, Cameron Rich
2+
* Copyright (c) 2007-2016, Cameron Rich
33
*
44
* All rights reserved.
55
*
@@ -38,9 +38,6 @@
3838
#include "os_port.h"
3939
#include "crypto.h"
4040

41-
/* all commented out in skeleton mode */
42-
#ifndef CONFIG_SSL_SKELETON_MODE
43-
4441
#define rot1(x) (((x) << 24) | ((x) >> 8))
4542
#define rot2(x) (((x) << 16) | ((x) >> 16))
4643
#define rot3(x) (((x) << 8) | ((x) >> 24))
@@ -453,5 +450,3 @@ static void AES_decrypt(const AES_CTX *ctx, uint32_t *data)
453450
data[row-1] = tmp[row-1] ^ *(--k);
454451
}
455452
}
456-
457-
#endif

crypto/crypto.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2007-2015, Cameron Rich
2+
* Copyright (c) 2007-2016, Cameron Rich
33
*
44
* All rights reserved.
55
*
@@ -200,6 +200,8 @@ void hmac_md5(const uint8_t *msg, int length, const uint8_t *key,
200200
int key_len, uint8_t *digest);
201201
void hmac_sha1(const uint8_t *msg, int length, const uint8_t *key,
202202
int key_len, uint8_t *digest);
203+
void hmac_sha256(const uint8_t *msg, int length, const uint8_t *key,
204+
int key_len, uint8_t *digest);
203205

204206
/**************************************************************************
205207
* RSA declarations

crypto/crypto_misc.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ static int rng_fd = -1;
5353
static HCRYPTPROV gCryptProv;
5454
#endif
5555

56-
#if (!defined(CONFIG_USE_DEV_URANDOM) && !defined(CONFIG_WIN32_USE_CRYPTO_LIB))
56+
#if (!defined(ESP8266) && !defined(CONFIG_USE_DEV_URANDOM) && !defined(CONFIG_WIN32_USE_CRYPTO_LIB))
5757
/* change to processor registers as appropriate */
5858
#define ENTROPY_POOL_SIZE 32
5959
#define ENTROPY_COUNTER1 ((((uint64_t)tv.tv_sec)<<32) | tv.tv_usec)
@@ -109,7 +109,7 @@ int get_file(const char *filename, uint8_t **buf)
109109
EXP_FUNC void STDCALL RNG_initialize()
110110
{
111111
#if !defined(WIN32) && defined(CONFIG_USE_DEV_URANDOM)
112-
rng_fd = ax_open("/dev/urandom", O_RDONLY);
112+
rng_fd = open("/dev/urandom", O_RDONLY);
113113
#elif defined(WIN32) && defined(CONFIG_WIN32_USE_CRYPTO_LIB)
114114
if (!CryptAcquireContext(&gCryptProv,
115115
NULL, NULL, PROV_RSA_FULL, 0))
@@ -130,7 +130,7 @@ EXP_FUNC void STDCALL RNG_initialize()
130130
/* start of with a stack to copy across */
131131
int i;
132132
memcpy(entropy_pool, &i, ENTROPY_POOL_SIZE);
133-
srand((unsigned int)&i);
133+
rand_r((unsigned int *)entropy_pool);
134134
#endif
135135
}
136136

@@ -181,7 +181,7 @@ EXP_FUNC int STDCALL get_random(int num_rand_bytes, uint8_t *rand_data)
181181
#else /* nothing else to use, so use a custom RNG */
182182
/* The method we use when we've got nothing better. Use RC4, time
183183
and a couple of random seeds to generate a random sequence */
184-
RC4_CTX rng_ctx;
184+
AES_CTX rng_ctx;
185185
struct timeval tv;
186186
MD5_CTX rng_digest_ctx;
187187
uint8_t digest[MD5_SIZE];
@@ -200,10 +200,10 @@ EXP_FUNC int STDCALL get_random(int num_rand_bytes, uint8_t *rand_data)
200200
MD5_Final(digest, &rng_digest_ctx);
201201

202202
/* come up with the random sequence */
203-
RC4_setup(&rng_ctx, digest, MD5_SIZE); /* use as a key */
203+
AES_set_key(&rng_ctx, digest, (const uint8_t *)ep, AES_MODE_128); /* use as a key */
204204
memcpy(rand_data, entropy_pool, num_rand_bytes < ENTROPY_POOL_SIZE ?
205205
num_rand_bytes : ENTROPY_POOL_SIZE);
206-
RC4_crypt(&rng_ctx, rand_data, rand_data, num_rand_bytes);
206+
AES_cbc_encrypt(&rng_ctx, rand_data, rand_data, num_rand_bytes);
207207

208208
/* move things along */
209209
for (i = ENTROPY_POOL_SIZE-1; i >= MD5_SIZE ; i--)

crypto/hmac.c

+35-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2007, Cameron Rich
2+
* Copyright (c) 2007-2016, Cameron Rich
33
*
44
* All rights reserved.
55
*
@@ -103,3 +103,37 @@ void hmac_sha1(const uint8_t *msg, int length, const uint8_t *key,
103103
SHA1_Update(&context, digest, SHA1_SIZE);
104104
SHA1_Final(digest, &context);
105105
}
106+
107+
/**
108+
* Perform HMAC-SHA256
109+
* NOTE: does not handle keys larger than the block size.
110+
*/
111+
void hmac_sha256(const uint8_t *msg, int length, const uint8_t *key,
112+
int key_len, uint8_t *digest)
113+
{
114+
SHA256_CTX context;
115+
uint8_t k_ipad[64];
116+
uint8_t k_opad[64];
117+
int i;
118+
119+
memset(k_ipad, 0, sizeof k_ipad);
120+
memset(k_opad, 0, sizeof k_opad);
121+
memcpy(k_ipad, key, key_len);
122+
memcpy(k_opad, key, key_len);
123+
124+
for (i = 0; i < 64; i++)
125+
{
126+
k_ipad[i] ^= 0x36;
127+
k_opad[i] ^= 0x5c;
128+
}
129+
130+
SHA256_Init(&context);
131+
SHA256_Update(&context, k_ipad, 64);
132+
SHA256_Update(&context, msg, length);
133+
SHA256_Final(digest, &context);
134+
SHA256_Init(&context);
135+
SHA256_Update(&context, k_opad, 64);
136+
SHA256_Update(&context, digest, SHA256_SIZE);
137+
SHA256_Final(digest, &context);
138+
}
139+

crypto/os_int.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, Cameron Rich
2+
* Copyright (c) 2012-2016, Cameron Rich
33
*
44
* All rights reserved.
55
*
@@ -56,7 +56,6 @@ typedef INT64 int64_t;
5656
#include <inttypes.h>
5757
#else
5858
#include <stdint.h>
59-
#include <endian.h>
6059
#endif /* Not Solaris */
6160

6261
#endif /* Not Win32 */

ssl/crypto_misc.h

-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ const char * x509_display_error(int error);
125125
#define ASN1_EXPLICIT_TAG 0xa0
126126
#define ASN1_V3_DATA 0xa3
127127

128-
#define SIG_TYPE_MD2 0x02
129128
#define SIG_TYPE_MD5 0x04
130129
#define SIG_TYPE_SHA1 0x05
131130
#define SIG_TYPE_SHA256 0x0b

ssl/loader.c

+9-3
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ EXP_FUNC int STDCALL ssl_obj_load(SSL_CTX *ssl_ctx, int obj_type,
8282
#ifdef CONFIG_SSL_HAS_PEM
8383
ret = ssl_obj_PEM_load(ssl_ctx, obj_type, ssl_obj, password);
8484
#else
85+
#ifdef CONFIG_SSL_FULL_MODE
8586
printf("%s", unsupported_str);
87+
#endif
8688
ret = SSL_ERROR_NOT_SUPPORTED;
8789
#endif
8890
}
@@ -93,7 +95,9 @@ EXP_FUNC int STDCALL ssl_obj_load(SSL_CTX *ssl_ctx, int obj_type,
9395
ssl_obj_free(ssl_obj);
9496
return ret;
9597
#else
98+
#ifdef CONFIG_SSL_FULL_MODE
9699
printf("%s", unsupported_str);
100+
#endif
97101
return SSL_ERROR_NOT_SUPPORTED;
98102
#endif /* CONFIG_SSL_SKELETON_MODE */
99103
}
@@ -150,7 +154,9 @@ static int do_obj(SSL_CTX *ssl_ctx, int obj_type,
150154
break;
151155
#endif
152156
default:
157+
#ifdef CONFIG_SSL_FULL_MODE
153158
printf("%s", unsupported_str);
159+
#endif
154160
ret = SSL_ERROR_NOT_SUPPORTED;
155161
break;
156162
}
@@ -223,7 +229,7 @@ static int pem_decrypt(const char *where, const char *end,
223229
if (password == NULL || strlen(password) == 0)
224230
{
225231
#ifdef CONFIG_SSL_FULL_MODE
226-
printf("Error: Need a password for this PEM file\n"); TTY_FLUSH();
232+
printf("Error: Need a password for this PEM file\n");
227233
#endif
228234
goto error;
229235
}
@@ -240,7 +246,7 @@ static int pem_decrypt(const char *where, const char *end,
240246
else
241247
{
242248
#ifdef CONFIG_SSL_FULL_MODE
243-
printf("Error: Unsupported password cipher\n"); TTY_FLUSH();
249+
printf("Error: Unsupported password cipher\n");
244250
#endif
245251
goto error;
246252
}
@@ -475,7 +481,7 @@ int load_key_certs(SSL_CTX *ssl_ctx)
475481
#ifdef CONFIG_SSL_FULL_MODE
476482
if (ret)
477483
{
478-
printf("Error: Certificate or key not loaded\n"); TTY_FLUSH();
484+
printf("Error: Certificate or key not loaded\n");
479485
}
480486
#endif
481487

ssl/openssl.c

+5-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2007, Cameron Rich
2+
* Copyright (c) 2007-2016, Cameron Rich
33
*
44
* All rights reserved.
55
*
@@ -49,10 +49,8 @@
4949

5050
static char *key_password = NULL;
5151

52-
void *SSLv23_server_method(void) { return NULL; }
5352
void *SSLv3_server_method(void) { return NULL; }
5453
void *TLSv1_server_method(void) { return NULL; }
55-
void *SSLv23_client_method(void) { return NULL; }
5654
void *SSLv3_client_method(void) { return NULL; }
5755
void *TLSv1_client_method(void) { return NULL; }
5856

@@ -81,14 +79,13 @@ void SSL_CTX_free(SSL_CTX * ssl_ctx)
8179
SSL * SSL_new(SSL_CTX *ssl_ctx)
8280
{
8381
SSL *ssl;
84-
ssl_func_type_t ssl_func_type;
82+
#ifdef CONFIG_SSL_ENABLE_CLIENT
83+
ssl_func_type_t ssl_func_type = OPENSSL_CTX_ATTR->ssl_func_type;
84+
#endif
8585

8686
ssl = ssl_new(ssl_ctx, -1); /* fd is set later */
87-
ssl_func_type = OPENSSL_CTX_ATTR->ssl_func_type;
88-
8987
#ifdef CONFIG_SSL_ENABLE_CLIENT
90-
if (ssl_func_type == SSLv23_client_method ||
91-
ssl_func_type == SSLv3_client_method ||
88+
if (ssl_func_type == SSLv3_client_method ||
9289
ssl_func_type == TLSv1_client_method)
9390
{
9491
SET_SSL_FLAG(SSL_IS_CLIENT);
@@ -231,8 +228,6 @@ void SSL_CTX_set_client_CA_list(SSL_CTX *ssl_ctx, void *file)
231228
ssl_obj_load(ssl_ctx, SSL_OBJ_X509_CERT, (const char *)file, NULL);
232229
}
233230

234-
void SSLv23_method(void) { }
235-
236231
void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, void *cb) { }
237232

238233
void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u)

ssl/os_port.c

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
2-
* Copyright (c) 2007, Cameron Rich
3-
*
2+
* Copyright (c) 2007-2016, Cameron Rich
3+
*
44
* All rights reserved.
55
*
66
* Redistribution and use in source and binary forms, with or without
@@ -91,9 +91,3 @@ EXP_FUNC int STDCALL getdomainname(char *buf, int buf_size)
9191
}
9292
#endif
9393

94-
#undef malloc
95-
#undef realloc
96-
#undef calloc
97-
98-
static const char * out_of_mem_str = "out of memory";
99-
static const char * file_open_str = "Could not open file \"%s\"";

ssl/os_port.h

+5-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2007-2015, Cameron Rich
2+
* Copyright (c) 2007-2016, Cameron Rich
33
*
44
* All rights reserved.
55
*
@@ -62,7 +62,7 @@ extern "C" {
6262

6363
#include "util/time.h"
6464
#include <errno.h>
65-
// #define alloca(size) __builtin_alloca(size)
65+
#define alloca(size) __builtin_alloca(size)
6666
#define TTY_FLUSH()
6767
#ifdef putc
6868
#undef putc
@@ -80,6 +80,7 @@ extern "C" {
8080
#define EWOULDBLOCK EAGAIN
8181

8282
#define hmac_sha1 ax_hmac_sha1
83+
#define hmac_sha256 ax_hmac_sha256
8384
#define hmac_md5 ax_hmac_md5
8485

8586
#ifndef be64toh
@@ -189,19 +190,6 @@ EXP_FUNC int STDCALL getdomainname(char *buf, int buf_size);
189190
#endif /* Not Win32 */
190191

191192
/* some functions to mutate the way these work */
192-
#define malloc(A) ax_port_malloc(A, __FILE__, __LINE__)
193-
#ifndef realloc
194-
#define realloc(A,B) ax_port_realloc(A,B, __FILE__, __LINE__)
195-
#endif
196-
#define calloc(A,B) ax_port_calloc(A,B, __FILE__, __LINE__)
197-
#define free(x) ax_port_free(x)
198-
199-
EXP_FUNC void * STDCALL ax_port_malloc(size_t s, const char*, int);
200-
EXP_FUNC void * STDCALL ax_port_realloc(void *y, size_t s, const char*, int);
201-
EXP_FUNC void * STDCALL ax_port_calloc(size_t n, size_t s, const char*, int);
202-
EXP_FUNC void * STDCALL ax_port_free(void*);
203-
EXP_FUNC int STDCALL ax_open(const char *pathname, int flags);
204-
205193
inline uint32_t htonl(uint32_t n){
206194
return ((n & 0xff) << 24) |
207195
((n & 0xff00) << 8) |
@@ -211,6 +199,8 @@ inline uint32_t htonl(uint32_t n){
211199

212200
#define ntohl htonl
213201

202+
EXP_FUNC int STDCALL ax_open(const char *pathname, int flags);
203+
214204
#ifdef CONFIG_PLATFORM_LINUX
215205
void exit_now(const char *format, ...) __attribute((noreturn));
216206
#else

ssl/ssl.h

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2007, Cameron Rich
2+
* Copyright (c) 2007-2016, Cameron Rich
33
*
44
* All rights reserved.
55
*
@@ -91,13 +91,16 @@ extern "C" {
9191
#define SSL_ERROR_DEAD -2
9292
#define SSL_CLOSE_NOTIFY -3
9393
#define SSL_ERROR_CONN_LOST -256
94+
#define SSL_ERROR_RECORD_OVERFLOW -257
9495
#define SSL_ERROR_SOCK_SETUP_FAILURE -258
9596
#define SSL_ERROR_INVALID_HANDSHAKE -260
9697
#define SSL_ERROR_INVALID_PROT_MSG -261
9798
#define SSL_ERROR_INVALID_HMAC -262
9899
#define SSL_ERROR_INVALID_VERSION -263
100+
#define SSL_ERROR_UNSUPPORTED_EXTENSION -264
99101
#define SSL_ERROR_INVALID_SESSION -265
100102
#define SSL_ERROR_NO_CIPHER -266
103+
#define SSL_ERROR_INVALID_CERT_HASH_ALG -267
101104
#define SSL_ERROR_BAD_CERTIFICATE -268
102105
#define SSL_ERROR_INVALID_KEY -269
103106
#define SSL_ERROR_FINISHED_INVALID -271
@@ -115,19 +118,25 @@ extern "C" {
115118
#define SSL_ALERT_CLOSE_NOTIFY 0
116119
#define SSL_ALERT_UNEXPECTED_MESSAGE 10
117120
#define SSL_ALERT_BAD_RECORD_MAC 20
121+
#define SSL_ALERT_RECORD_OVERFLOW 22
118122
#define SSL_ALERT_HANDSHAKE_FAILURE 40
119123
#define SSL_ALERT_BAD_CERTIFICATE 42
124+
#define SSL_ALERT_UNSUPPORTED_CERTIFICATE 43
125+
#define SSL_ALERT_CERTIFICATE_EXPIRED 45
126+
#define SSL_ALERT_CERTIFICATE_UNKNOWN 46
120127
#define SSL_ALERT_ILLEGAL_PARAMETER 47
128+
#define SSL_ALERT_UNKNOWN_CA 48
121129
#define SSL_ALERT_DECODE_ERROR 50
122130
#define SSL_ALERT_DECRYPT_ERROR 51
123131
#define SSL_ALERT_INVALID_VERSION 70
124132
#define SSL_ALERT_NO_RENEGOTIATION 100
133+
#define SSL_ALERT_UNSUPPORTED_EXTENSION 110
125134

126135
/* The ciphers that are supported */
127136
#define SSL_AES128_SHA 0x2f
128137
#define SSL_AES256_SHA 0x35
129-
#define SSL_RC4_128_SHA 0x05
130-
#define SSL_RC4_128_MD5 0x04
138+
#define SSL_AES128_SHA256 0x3c
139+
#define SSL_AES256_SHA256 0x3d
131140

132141
/* build mode ids' */
133142
#define SSL_BUILD_SKELETON_MODE 0x01

0 commit comments

Comments
 (0)