Skip to content

Commit 0c3a9f7

Browse files
earlephilhowerigrr
authored andcommitted
Remove compile warnings, move add'l consts to PMEM
There were some simple-to-fix compile warnings relating to missing imports and datatypes. Add proper includes (and replace the hacked util/time.h definition of timeval with the real one in our SDK). Also migrate multiple constant strings with minimal code changes, freeing around 210 additional bytes of heap.
1 parent f740ada commit 0c3a9f7

File tree

10 files changed

+51
-39
lines changed

10 files changed

+51
-39
lines changed

crypto/crypto_misc.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ static HCRYPTPROV gCryptProv;
6161
static uint8_t entropy_pool[ENTROPY_POOL_SIZE];
6262
#endif
6363

64-
const char * const unsupported_str = "Error: Feature not supported\n";
65-
6664
#ifndef CONFIG_SSL_SKELETON_MODE
6765
/**
6866
* Retrieve a file and put it into memory

crypto/rsa.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,9 @@ void RSA_print(const RSA_CTX *rsa_ctx)
238238

239239
printf("----------------- RSA DEBUG ----------------\n");
240240
printf("Size:\t%d\n", rsa_ctx->num_octets);
241-
bi_print("Modulus", rsa_ctx->m);
242-
bi_print("Public Key", rsa_ctx->e);
243-
bi_print("Private Key", rsa_ctx->d);
241+
printf("Modulus"); bi_print("", rsa_ctx->m);
242+
printf("Public Key"); bi_print("", rsa_ctx->e);
243+
printf("Private Key"); bi_print("", rsa_ctx->d);
244244
}
245245
#endif
246246

ssl/asn1.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,6 @@ int asn1_get_bit_string_as_int(const uint8_t *buf, int *offset, uint32_t *val)
229229
{
230230
int res = X509_OK;
231231
int len, i;
232-
int ignore_bits;
233232

234233
if ((len = asn1_next_obj(buf, offset, ASN1_BIT_STRING)) < 0 || len > 5)
235234
{
@@ -238,7 +237,6 @@ int asn1_get_bit_string_as_int(const uint8_t *buf, int *offset, uint32_t *val)
238237
}
239238

240239
/* number of bits left unused in the final byte of content */
241-
ignore_bits = buf[(*offset)++];
242240
len--;
243241
*val = 0;
244242

@@ -251,11 +249,6 @@ int asn1_get_bit_string_as_int(const uint8_t *buf, int *offset, uint32_t *val)
251249

252250
*offset += len;
253251

254-
/*for (i = 0; i < ignore_bits; i++)
255-
{
256-
*val >>= 1;
257-
}*/
258-
259252
end_bit_string_as_int:
260253
return res;
261254
}

ssl/crypto_misc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ int asn1_signature_type(const uint8_t *cert,
190190
**************************************************************************/
191191
#define SALT_SIZE 8
192192

193-
extern const char * const unsupported_str;
193+
#define unsupported_str "Error: Feature not supported\n"
194194

195195
typedef void (*crypt_func)(void *, const uint8_t *, uint8_t *, int);
196196
typedef void (*hmac_func)(const uint8_t *msg, int length, const uint8_t *key,

ssl/loader.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ EXP_FUNC int STDCALL ssl_obj_load(SSL_CTX *ssl_ctx, int obj_type,
8383
ret = ssl_obj_PEM_load(ssl_ctx, obj_type, ssl_obj, password);
8484
#else
8585
#ifdef CONFIG_SSL_FULL_MODE
86-
printf("%s", unsupported_str);
86+
printf(unsupported_str);
8787
#endif
8888
ret = SSL_ERROR_NOT_SUPPORTED;
8989
#endif
@@ -96,7 +96,7 @@ EXP_FUNC int STDCALL ssl_obj_load(SSL_CTX *ssl_ctx, int obj_type,
9696
return ret;
9797
#else
9898
#ifdef CONFIG_SSL_FULL_MODE
99-
printf("%s", unsupported_str);
99+
printf(unsupported_str);
100100
#endif
101101
return SSL_ERROR_NOT_SUPPORTED;
102102
#endif /* CONFIG_SSL_SKELETON_MODE */
@@ -155,7 +155,7 @@ static int do_obj(SSL_CTX *ssl_ctx, int obj_type,
155155
#endif
156156
default:
157157
#ifdef CONFIG_SSL_FULL_MODE
158-
printf("%s", unsupported_str);
158+
printf(unsupported_str);
159159
#endif
160160
ret = SSL_ERROR_NOT_SUPPORTED;
161161
break;

ssl/os_port.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,25 @@ static inline void* memcpy_P(void* dest, PGM_VOID_P src, size_t count) {
137137

138138
return dest;
139139
}
140+
static inline int strlen_P(const char *str) {
141+
int cnt = 0;
142+
while (pgm_read_byte(str++)) cnt++;
143+
return cnt;
144+
}
140145
#define printf(fmt, ...) do { static const char fstr[] PROGMEM = fmt; char rstr[sizeof(fmt)]; memcpy_P(rstr, fstr, sizeof(rstr)); ets_printf(rstr, ##__VA_ARGS__); } while (0)
141146
#define strcpy_P(dst, src) do { static const char fstr[] PROGMEM = src; memcpy_P(dst, fstr, sizeof(src)); } while (0)
142147

148+
// Copied from ets_sys.h to avoid compile warnings
149+
extern int ets_printf(const char *format, ...) __attribute__ ((format (printf, 1, 2)));
150+
extern int ets_putc(int);
151+
152+
// The network interface in WiFiClientSecure
153+
extern int ax_port_read(int fd, uint8_t* buffer, size_t count);
154+
extern int ax_port_write(int fd, uint8_t* buffer, size_t count);
155+
156+
// TODO: Why is this not being imported from <string.h>?
157+
extern char *strdup(const char *orig);
158+
143159
#elif defined(WIN32)
144160

145161
/* Windows CE stuff */

ssl/tls1.c

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -978,7 +978,7 @@ void generate_master_secret(SSL *ssl, const uint8_t *premaster_secret)
978978
{
979979
uint8_t buf[77];
980980
//print_blob("premaster secret", premaster_secret, 48);
981-
strcpy((char *)buf, "master secret");
981+
strcpy_P((char*)buf, "master secret");
982982
memcpy(&buf[13], ssl->dc->client_random, SSL_RANDOM_SIZE);
983983
memcpy(&buf[45], ssl->dc->server_random, SSL_RANDOM_SIZE);
984984
prf(ssl, premaster_secret, SSL_SECRET_SIZE, buf, 77, ssl->dc->master_secret,
@@ -998,7 +998,7 @@ static void generate_key_block(SSL *ssl,
998998
uint8_t *master_secret, uint8_t *key_block, int key_block_size)
999999
{
10001000
uint8_t buf[77];
1001-
strcpy((char *)buf, "key expansion");
1001+
strcpy_P((char *)buf, "key expansion");
10021002
memcpy(&buf[13], server_random, SSL_RANDOM_SIZE);
10031003
memcpy(&buf[45], client_random, SSL_RANDOM_SIZE);
10041004
prf(ssl, master_secret, SSL_SECRET_SIZE, buf, 77,
@@ -1125,7 +1125,7 @@ static int send_raw_packet(SSL *ssl, uint8_t protocol)
11251125
rec_buf[3] = ssl->bm_index >> 8;
11261126
rec_buf[4] = ssl->bm_index & 0xff;
11271127

1128-
DISPLAY_BYTES(ssl, "sending %d bytes", ssl->bm_all_data,
1128+
DISPLAY_BYTES(ssl, PSTR("sending %d bytes"), ssl->bm_all_data,
11291129
pkt_size, pkt_size);
11301130

11311131
while (sent < pkt_size)
@@ -1234,7 +1234,7 @@ int send_packet(SSL *ssl, uint8_t protocol, const uint8_t *in, int length)
12341234
msg_length += pad_bytes;
12351235
}
12361236

1237-
DISPLAY_BYTES(ssl, "unencrypted write", ssl->bm_data, msg_length);
1237+
DISPLAY_BYTES(ssl, PSTR("unencrypted write"), ssl->bm_data, msg_length);
12381238
increment_write_sequence(ssl);
12391239

12401240
/* add the explicit IV for TLS1.1 */
@@ -1392,7 +1392,7 @@ int basic_read(SSL *ssl, uint8_t **in_data)
13921392
goto error;
13931393
}
13941394

1395-
DISPLAY_BYTES(ssl, "received %d bytes",
1395+
DISPLAY_BYTES(ssl, PSTR("received %d bytes"),
13961396
&ssl->bm_data[ssl->bm_read_index], read_len, read_len);
13971397

13981398
ssl->got_bytes += read_len;
@@ -1469,7 +1469,7 @@ int basic_read(SSL *ssl, uint8_t **in_data)
14691469
goto error;
14701470
}
14711471

1472-
DISPLAY_BYTES(ssl, "decrypted", buf, read_len);
1472+
DISPLAY_BYTES(ssl, PSTR("decrypted"), buf, read_len);
14731473
increment_read_sequence(ssl);
14741474
}
14751475

@@ -2290,8 +2290,6 @@ EXP_FUNC int STDCALL ssl_match_spki_sha256(const SSL *ssl, const uint8_t* hash)
22902290
*/
22912291
void DISPLAY_STATE(SSL *ssl, int is_send, uint8_t state, int not_ok)
22922292
{
2293-
const char *str;
2294-
22952293
if (!IS_SET_SSL_FLAG(SSL_DISPLAY_STATES))
22962294
return;
22972295

@@ -2372,7 +2370,12 @@ void DISPLAY_BYTES(SSL *ssl, const char *format,
23722370
return;
23732371

23742372
va_start(ap, size);
2375-
print_blob(format, data, size, va_arg(ap, char *));
2373+
char fmt_ram[64];
2374+
int len = strlen_P(format) + 1;
2375+
if (len > sizeof(fmt_ram)) len = sizeof(fmt_ram);
2376+
memcpy_P(fmt_ram, format, len);
2377+
fmt_ram[sizeof(fmt_ram)-1] = 0;
2378+
print_blob(fmt_ram, data, size, va_arg(ap, char *));
23762379
va_end(ap);
23772380
TTY_FLUSH();
23782381
}
@@ -2580,28 +2583,28 @@ EXP_FUNC void STDCALL ssl_display_error(int error_code) {}
25802583
EXP_FUNC SSL * STDCALL ssl_client_new(SSL_CTX *ssl_ctx, int client_fd, const
25812584
uint8_t *session_id, uint8_t sess_id_size)
25822585
{
2583-
printf("%s", unsupported_str);
2586+
printf(unsupported_str);
25842587
return NULL;
25852588
}
25862589
#endif
25872590

25882591
#if !defined(CONFIG_SSL_CERT_VERIFICATION)
25892592
EXP_FUNC int STDCALL ssl_verify_cert(const SSL *ssl)
25902593
{
2591-
printf("%s", unsupported_str);
2594+
printf(unsupported_str);
25922595
return -1;
25932596
}
25942597

25952598

25962599
EXP_FUNC const char * STDCALL ssl_get_cert_dn(const SSL *ssl, int component)
25972600
{
2598-
printf("%s", unsupported_str);
2601+
printf(unsupported_str);
25992602
return NULL;
26002603
}
26012604

26022605
EXP_FUNC const char * STDCALL ssl_get_cert_subject_alt_dnsname(const SSL *ssl, int index)
26032606
{
2604-
printf("%s", unsupported_str);
2607+
printf(unsupported_str);
26052608
return NULL;
26062609
}
26072610

ssl/tls1.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ typedef struct
175175

176176
typedef struct
177177
{
178-
const char *host_name; /* Needed for the SNI support */
178+
char *host_name; /* Needed for the SNI support */
179179
/* Needed for the Max Fragment Size Extension.
180180
Allowed values: 0,1,2,3..6 corresponding to off,512,1024,2048..16384 bytes
181181
*/

ssl/x509.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include <stdlib.h>
3939
#include <string.h>
4040
#include <time.h>
41+
#include <sys/time.h>
4142
#include "os_port.h"
4243
#include "crypto_misc.h"
4344

@@ -636,12 +637,16 @@ int x509_verify(const CA_CERT_CTX *ca_cert_ctx, const X509_CTX *cert,
636637
/**
637638
* Used for diagnostics.
638639
*/
639-
static const char *not_part_of_cert = "<Not Part Of Certificate>";
640640
void x509_print(const X509_CTX *cert, CA_CERT_CTX *ca_cert_ctx)
641641
{
642642
if (cert == NULL)
643643
return;
644644

645+
char not_part_of_cert[30];
646+
strcpy_P(not_part_of_cert, "<Not Part Of Certificate>");
647+
char critical[16];
648+
strcpy_P(critical, "critical, ");
649+
645650
printf("=== CERTIFICATE ISSUED TO ===\n");
646651
printf("Common Name (CN):\t\t");
647652
printf("%s\n", cert->cert_dn[X509_COMMON_NAME] ?
@@ -679,15 +684,15 @@ void x509_print(const X509_CTX *cert, CA_CERT_CTX *ca_cert_ctx)
679684
{
680685
printf("Basic Constraints:\t\t%sCA:%s, pathlen:%d\n",
681686
cert->basic_constraint_is_critical ?
682-
"critical, " : "",
687+
critical : "",
683688
cert->basic_constraint_cA? "TRUE" : "FALSE",
684689
cert->basic_constraint_pathLenConstraint);
685690
}
686691

687692
if (cert->key_usage_present)
688693
{
689694
printf("Key Usage:\t\t\t%s", cert->key_usage_is_critical ?
690-
"critical, " : "");
695+
critical : "");
691696
bool has_started = false;
692697

693698
if (IS_SET_KEY_USAGE_FLAG(cert, KEY_USAGE_DIGITAL_SIGNATURE))
@@ -774,7 +779,7 @@ void x509_print(const X509_CTX *cert, CA_CERT_CTX *ca_cert_ctx)
774779
if (cert->subject_alt_name_present)
775780
{
776781
printf("Subject Alt Name:\t\t%s", cert->subject_alt_name_is_critical
777-
? "critical, " : "");
782+
? critical : "");
778783
if (cert->subject_alt_dnsnames)
779784
{
780785
int i = 0;

util/time.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
#ifndef TIME_H
22
#define TIME_H
33

4-
struct timeval
5-
{
6-
time_t tv_sec;
7-
long tv_usec;
8-
};
4+
#include <time.h>
5+
#include <sys/time.h>
96

107

118
#endif //TIME_H

0 commit comments

Comments
 (0)