Skip to content

Commit f599ff8

Browse files
cameronrichikeyasu
cameronrich
authored andcommitted
* Tightened up the buffer sizes
* Removed support for TLS1.0. git-svn-id: svn://svn.code.sf.net/p/axtls/code/trunk@267 9a5d90b5-6617-0410-8a86-bb477d3ed2e3
1 parent 871a70e commit f599ff8

File tree

4 files changed

+44
-63
lines changed

4 files changed

+44
-63
lines changed

ssl/tls1.c

Lines changed: 30 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -704,39 +704,27 @@ static void add_hmac_digest(SSL *ssl, int mode, uint8_t *hmac_header,
704704
*/
705705
static int verify_digest(SSL *ssl, int mode, const uint8_t *buf, int read_len)
706706
{
707-
uint8_t hmac_buf[128];
707+
uint8_t hmac_buf[SHA256_SIZE]; // size of largest digest
708708
int hmac_offset;
709709

710-
if (ssl->cipher_info->padding_size)
711-
{
712-
int last_blk_size = buf[read_len-1], i;
713-
hmac_offset = read_len-last_blk_size-ssl->cipher_info->digest_size-1;
710+
int last_blk_size = buf[read_len-1], i;
711+
hmac_offset = read_len-last_blk_size-ssl->cipher_info->digest_size-1;
714712

715-
/* guard against a timing attack - make sure we do the digest */
716-
if (hmac_offset < 0)
717-
{
718-
hmac_offset = 0;
719-
}
720-
else
721-
{
722-
/* already looked at last byte */
723-
for (i = 1; i < last_blk_size; i++)
724-
{
725-
if (buf[read_len-i] != last_blk_size)
726-
{
727-
hmac_offset = 0;
728-
break;
729-
}
730-
}
731-
}
713+
/* guard against a timing attack - make sure we do the digest */
714+
if (hmac_offset < 0)
715+
{
716+
hmac_offset = 0;
732717
}
733-
else /* stream cipher */
718+
else
734719
{
735-
hmac_offset = read_len - ssl->cipher_info->digest_size;
736-
737-
if (hmac_offset < 0)
720+
/* already looked at last byte */
721+
for (i = 1; i < last_blk_size; i++)
738722
{
739-
hmac_offset = 0;
723+
if (buf[read_len-i] != last_blk_size)
724+
{
725+
hmac_offset = 0;
726+
break;
727+
}
740728
}
741729
}
742730

@@ -759,7 +747,7 @@ static int verify_digest(SSL *ssl, int mode, const uint8_t *buf, int read_len)
759747
*/
760748
void add_packet(SSL *ssl, const uint8_t *pkt, int len)
761749
{
762-
// TLS1.2
750+
// TLS1.2+
763751
if (ssl->version >= SSL_PROTOCOL_VERSION_TLS1_2 || ssl->version == 0)
764752
{
765753
SHA256_Update(&ssl->dc->sha256_ctx, pkt, len);
@@ -780,7 +768,7 @@ void add_packet(SSL *ssl, const uint8_t *pkt, int len)
780768
static void p_hash_md5(const uint8_t *sec, int sec_len,
781769
uint8_t *seed, int seed_len, uint8_t *out, int olen)
782770
{
783-
uint8_t a1[128];
771+
uint8_t a1[MD5_SIZE+77];
784772

785773
/* A(1) */
786774
hmac_md5(seed, seed_len, sec, sec_len, a1);
@@ -808,7 +796,7 @@ static void p_hash_md5(const uint8_t *sec, int sec_len,
808796
static void p_hash_sha1(const uint8_t *sec, int sec_len,
809797
uint8_t *seed, int seed_len, uint8_t *out, int olen)
810798
{
811-
uint8_t a1[128];
799+
uint8_t a1[SHA1_SIZE+77];
812800

813801
/* A(1) */
814802
hmac_sha1(seed, seed_len, sec, sec_len, a1);
@@ -836,7 +824,7 @@ static void p_hash_sha1(const uint8_t *sec, int sec_len,
836824
static void p_hash_sha256(const uint8_t *sec, int sec_len,
837825
uint8_t *seed, int seed_len, uint8_t *out, int olen)
838826
{
839-
uint8_t a1[128];
827+
uint8_t a1[SHA256_SIZE+77];
840828

841829
/* A(1) */
842830
hmac_sha256(seed, seed_len, sec, sec_len, a1);
@@ -865,7 +853,7 @@ static void prf(SSL *ssl, const uint8_t *sec, int sec_len,
865853
uint8_t *seed, int seed_len,
866854
uint8_t *out, int olen)
867855
{
868-
if (ssl->version >= SSL_PROTOCOL_VERSION_TLS1_2) // TLS1.2
856+
if (ssl->version >= SSL_PROTOCOL_VERSION_TLS1_2) // TLS1.2+
869857
{
870858
p_hash_sha256(sec, sec_len, seed, seed_len, out, olen);
871859
}
@@ -895,7 +883,7 @@ static void prf(SSL *ssl, const uint8_t *sec, int sec_len,
895883
*/
896884
void generate_master_secret(SSL *ssl, const uint8_t *premaster_secret)
897885
{
898-
uint8_t buf[128];
886+
uint8_t buf[77];
899887
//print_blob("premaster secret", premaster_secret, 48);
900888
strcpy((char *)buf, "master secret");
901889
memcpy(&buf[13], ssl->dc->client_random, SSL_RANDOM_SIZE);
@@ -916,7 +904,7 @@ static void generate_key_block(SSL *ssl,
916904
uint8_t *client_random, uint8_t *server_random,
917905
uint8_t *master_secret, uint8_t *key_block, int key_block_size)
918906
{
919-
uint8_t buf[128];
907+
uint8_t buf[77];
920908
strcpy((char *)buf, "key expansion");
921909
memcpy(&buf[13], server_random, SSL_RANDOM_SIZE);
922910
memcpy(&buf[45], client_random, SSL_RANDOM_SIZE);
@@ -930,7 +918,7 @@ static void generate_key_block(SSL *ssl,
930918
*/
931919
int finished_digest(SSL *ssl, const char *label, uint8_t *digest)
932920
{
933-
uint8_t mac_buf[128];
921+
uint8_t mac_buf[SHA1_SIZE+MD5_SIZE+15];
934922
uint8_t *q = mac_buf;
935923
int dgst_len;
936924

@@ -940,7 +928,7 @@ int finished_digest(SSL *ssl, const char *label, uint8_t *digest)
940928
q += strlen(label);
941929
}
942930

943-
if (ssl->version >= SSL_PROTOCOL_VERSION_TLS1_2) // TLS1.2
931+
if (ssl->version >= SSL_PROTOCOL_VERSION_TLS1_2) // TLS1.2+
944932
{
945933
SHA256_CTX sha256_ctx = ssl->dc->sha256_ctx; // interim copy
946934
SHA256_Final(q, &sha256_ctx);
@@ -1140,8 +1128,7 @@ int send_packet(SSL *ssl, uint8_t protocol, const uint8_t *in, int length)
11401128
&ssl->bm_data[msg_length]);
11411129
msg_length += ssl->cipher_info->digest_size;
11421130

1143-
/* add padding? */
1144-
if (ssl->cipher_info->padding_size)
1131+
/* add padding */
11451132
{
11461133
int last_blk_size = msg_length%ssl->cipher_info->padding_size;
11471134
int pad_bytes = ssl->cipher_info->padding_size - last_blk_size;
@@ -1158,8 +1145,6 @@ int send_packet(SSL *ssl, uint8_t protocol, const uint8_t *in, int length)
11581145
increment_write_sequence(ssl);
11591146

11601147
/* add the explicit IV for TLS1.1 */
1161-
if (ssl->version >= SSL_PROTOCOL_VERSION_TLS1_1 &&
1162-
ssl->cipher_info->iv_size)
11631148
{
11641149
uint8_t iv_size = ssl->cipher_info->iv_size;
11651150
uint8_t *t_buf = malloc(msg_length + iv_size);
@@ -1373,13 +1358,8 @@ int basic_read(SSL *ssl, uint8_t **in_data)
13731358
if (IS_SET_SSL_FLAG(SSL_RX_ENCRYPTED))
13741359
{
13751360
ssl->cipher_info->decrypt(ssl->decrypt_ctx, buf, buf, read_len);
1376-
1377-
if (ssl->version >= SSL_PROTOCOL_VERSION_TLS1_1 &&
1378-
ssl->cipher_info->iv_size)
1379-
{
1380-
buf += ssl->cipher_info->iv_size;
1381-
read_len -= ssl->cipher_info->iv_size;
1382-
}
1361+
buf += ssl->cipher_info->iv_size;
1362+
read_len -= ssl->cipher_info->iv_size;
13831363

13841364
read_len = verify_digest(ssl,
13851365
is_client ? SSL_CLIENT_READ : SSL_SERVER_READ, buf, read_len);
@@ -1564,7 +1544,7 @@ int send_change_cipher_spec(SSL *ssl)
15641544
*/
15651545
int send_finished(SSL *ssl)
15661546
{
1567-
uint8_t buf[128] = {
1547+
uint8_t buf[SHA1_SIZE+MD5_SIZE+15+4] = {
15681548
HS_FINISHED, 0, 0, SSL_FINISHED_HASH_SIZE };
15691549

15701550
/* now add the finished digest mac (12 bytes) */
@@ -1725,9 +1705,11 @@ int send_certificate(SSL *ssl)
17251705
buf[1] = 0;
17261706
buf[4] = 0;
17271707

1708+
/* spec says we must check if the hash/sig algorithm is OK */
17281709
if (ssl->version >= SSL_PROTOCOL_VERSION_TLS1_2 &&
17291710
((ret = check_certificate_chain(ssl)) != SSL_OK))
17301711
{
1712+
ret = SSL_ERROR_INVALID_CERT_HASH_ALG;
17311713
goto error;
17321714
}
17331715

ssl/tls1.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,8 @@ extern "C" {
4747
#include "crypto.h"
4848
#include "crypto_misc.h"
4949

50-
#define SSL_PROTOCOL_MIN_VERSION 0x31 /* TLS v1.0 */
50+
#define SSL_PROTOCOL_MIN_VERSION 0x32 /* TLS v1.1 */
5151
#define SSL_PROTOCOL_VERSION_MAX 0x33 /* TLS v1.2 */
52-
#define SSL_PROTOCOL_VERSION_TLS1_1 0x32 /* TLS v1.1 */
5352
#define SSL_PROTOCOL_VERSION_TLS1_2 0x33 /* TLS v1.2 */
5453
#define SSL_RANDOM_SIZE 32
5554
#define SSL_SECRET_SIZE 48

ssl/tls1_clnt.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@
3737

3838
#ifdef CONFIG_SSL_ENABLE_CLIENT /* all commented out if no client */
3939

40-
/* support sha512/384/256/1 rsa */
40+
/* support sha512/384/256/1 RSA */
4141
static const uint8_t g_sig_alg[] = {
4242
0x00, 0x0e,
4343
0x00, SIG_ALG_EXTENSION,
4444
0x00, 0x0a, 0x00, 0x08,
45-
SIG_ALG_SHA256, SIG_ALG_RSA,
4645
SIG_ALG_SHA512, SIG_ALG_RSA,
4746
SIG_ALG_SHA384, SIG_ALG_RSA,
47+
SIG_ALG_SHA256, SIG_ALG_RSA,
4848
SIG_ALG_SHA1, SIG_ALG_RSA
4949
};
5050

@@ -245,7 +245,7 @@ static int send_client_hello(SSL *ssl)
245245
buf[offset++] = 0;
246246

247247
/* send the signature algorithm extension for TLS 1.2+ */
248-
if (ssl->version >= SSL_PROTOCOL_VERSION_TLS1_2)
248+
if (ssl->version >= SSL_PROTOCOL_VERSION_TLS1_2)
249249
{
250250
memcpy(&buf[offset], g_sig_alg, sizeof(g_sig_alg));
251251
offset += sizeof(g_sig_alg);
@@ -390,7 +390,7 @@ static int process_cert_req(SSL *ssl)
390390
ssl->next_state = HS_SERVER_HELLO_DONE;
391391
SET_SSL_FLAG(SSL_HAS_CERT_REQ);
392392

393-
if (ssl->version >= SSL_PROTOCOL_VERSION_TLS1_2) // TLS1.2
393+
if (ssl->version >= SSL_PROTOCOL_VERSION_TLS1_2) // TLS1.2+
394394
{
395395
// supported certificate types
396396
cert_type_len = buf[offset++];
@@ -429,7 +429,7 @@ static int process_cert_req(SSL *ssl)
429429
static int send_cert_verify(SSL *ssl)
430430
{
431431
uint8_t *buf = ssl->bm_data;
432-
uint8_t dgst[128];
432+
uint8_t dgst[SHA1_SIZE+MD5_SIZE+15];
433433
RSA_CTX *rsa_ctx = ssl->ssl_ctx->rsa_ctx;
434434
int n = 0, ret;
435435
int offset = 0;
@@ -443,7 +443,7 @@ static int send_cert_verify(SSL *ssl)
443443
buf[0] = HS_CERT_VERIFY;
444444
buf[1] = 0;
445445

446-
if (ssl->version >= SSL_PROTOCOL_VERSION_TLS1_2) // TLS1.2
446+
if (ssl->version >= SSL_PROTOCOL_VERSION_TLS1_2) // TLS1.2+
447447
{
448448
buf[4] = SIG_ALG_SHA256;
449449
buf[5] = SIG_ALG_RSA;
@@ -476,7 +476,7 @@ static int send_cert_verify(SSL *ssl)
476476
buf[offset+1] = n & 0xff;
477477
n += 2;
478478

479-
if (ssl->version >= SSL_PROTOCOL_VERSION_TLS1_2) // TLS1.2
479+
if (ssl->version >= SSL_PROTOCOL_VERSION_TLS1_2) // TLS1.2+
480480
{
481481
n += 2; // sig/alg
482482
offset -= 2;

ssl/tls1_svr.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ static const uint8_t g_cert_request_v1[] = { HS_CERT_REQ, 0, 0, 4, 1, 0, 0, 0 };
433433
*/
434434
static int send_certificate_request(SSL *ssl)
435435
{
436-
if (ssl->version >= SSL_PROTOCOL_VERSION_TLS1_2) // TLS1.2
436+
if (ssl->version >= SSL_PROTOCOL_VERSION_TLS1_2) // TLS1.2+
437437
{
438438
return send_packet(ssl, PT_HANDSHAKE_PROTOCOL,
439439
g_cert_request, sizeof(g_cert_request));
@@ -454,7 +454,7 @@ static int process_cert_verify(SSL *ssl)
454454
uint8_t *buf = &ssl->bm_data[ssl->dc->bm_proc_index];
455455
int pkt_size = ssl->bm_index;
456456
uint8_t dgst_buf[MAX_KEY_BYTE_SIZE];
457-
uint8_t dgst[128];
457+
uint8_t dgst[MD5_SIZE + SHA1_SIZE];
458458
X509_CTX *x509_ctx = ssl->x509_ctx;
459459
int ret = SSL_OK;
460460
int offset = 6;
@@ -463,14 +463,14 @@ static int process_cert_verify(SSL *ssl)
463463

464464
DISPLAY_RSA(ssl, x509_ctx->rsa_ctx);
465465

466-
if (ssl->version >= SSL_PROTOCOL_VERSION_TLS1_2) // TLS1.2
466+
if (ssl->version >= SSL_PROTOCOL_VERSION_TLS1_2) // TLS1.2+
467467
{
468-
// TODO: need to be able to handle another hash type here
468+
// TODO: should really need to be able to handle other algorihms. An
469+
// assumption is made on RSA/SHA256 and appears to be OK.
469470
//uint8_t hash_alg = buf[4];
470471
//uint8_t sig_alg = buf[5];
471472
offset = 8;
472473
rsa_len = (buf[6] << 8) + buf[7];
473-
//printf("YO, GOT %d %d\n", hash_alg, sig_alg);
474474
}
475475
else
476476
{
@@ -485,7 +485,7 @@ static int process_cert_verify(SSL *ssl)
485485
sizeof(dgst_buf), 0);
486486
SSL_CTX_UNLOCK(ssl->ssl_ctx->mutex);
487487

488-
if (ssl->version >= SSL_PROTOCOL_VERSION_TLS1_2) // TLS1.2
488+
if (ssl->version >= SSL_PROTOCOL_VERSION_TLS1_2) // TLS1.2+
489489
{
490490
if (memcmp(dgst_buf, g_asn1_sha256, sizeof(g_asn1_sha256)))
491491
{

0 commit comments

Comments
 (0)