Skip to content

Commit bc88de4

Browse files
committed
ArduinoIoTCloudCertClass: reduce RAM usage
1 parent 9c565ac commit bc88de4

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

src/tls/utility/Cert.cpp

+10-4
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,9 @@ static String base64Encode(const byte in[], unsigned int length, const char* pre
9696
******************************************************************************/
9797

9898
ArduinoIoTCloudCertClass::ArduinoIoTCloudCertClass()
99-
: _certBuffer(NULL)
99+
: _certBuffer(nullptr)
100100
, _certBufferLen(0)
101+
, _publicKey(nullptr)
101102
{
102103

103104
}
@@ -106,7 +107,7 @@ ArduinoIoTCloudCertClass::~ArduinoIoTCloudCertClass()
106107
{
107108
if (_certBuffer) {
108109
free(_certBuffer);
109-
_certBuffer = NULL;
110+
_certBuffer = nullptr;
110111
}
111112
}
112113

@@ -117,7 +118,6 @@ ArduinoIoTCloudCertClass::~ArduinoIoTCloudCertClass()
117118
int ArduinoIoTCloudCertClass::begin()
118119
{
119120
memset(_compressedCert.data, 0x00, CERT_COMPRESSED_CERT_LENGTH);
120-
memset(_publicKey, 0x00, CERT_PUBLIC_KEY_LENGTH);
121121
return 1;
122122
}
123123

@@ -146,6 +146,9 @@ int ArduinoIoTCloudCertClass::buildCSR()
146146
out += appendIssuerOrSubject(_subjectData, out);
147147

148148
// public key
149+
if (_publicKey == nullptr) {
150+
return 0;
151+
}
149152
out += appendPublicKey(_publicKey, out);
150153

151154
// terminator
@@ -243,6 +246,9 @@ int ArduinoIoTCloudCertClass::buildCert()
243246
out += appendIssuerOrSubject(_subjectData, out);
244247

245248
// public key
249+
if (_publicKey == nullptr) {
250+
return 0;
251+
}
246252
out += appendPublicKey(_publicKey, out);
247253

248254
int authorityKeyIdLen = authorityKeyIdLength(_compressedCert.slot.two.values.authorityKeyId, CERT_AUTHORITY_KEY_ID_LENGTH);
@@ -377,7 +383,7 @@ int ArduinoIoTCloudCertClass::setAuthorityKeyId(const uint8_t authorityKeyId[],
377383

378384
int ArduinoIoTCloudCertClass::setPublicKey(const byte* publicKey, int publicKeyLen) {
379385
if (publicKeyLen == CERT_PUBLIC_KEY_LENGTH) {
380-
memcpy(_publicKey, publicKey, CERT_PUBLIC_KEY_LENGTH);
386+
_publicKey = publicKey;
381387
return 1;
382388
}
383389
return 0;

src/tls/utility/Cert.h

+5-7
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
#define CERT_PUBLIC_KEY_LENGTH 64
3636
#define CERT_SIGNATURE_LENGTH 64
3737
#define CERT_DATES_LENGTH 3
38-
#define CERT_COMPRESSED_CERT_LENGTH 144
3938
#define CERT_COMPRESSED_CERT_SLOT_LENGTH 72
39+
#define CERT_COMPRESSED_CERT_LENGTH CERT_COMPRESSED_CERT_SLOT_LENGTH + CERT_SERIAL_NUMBER_LENGTH + CERT_AUTHORITY_KEY_ID_LENGTH
4040

4141
#include <Arduino.h>
4242

@@ -86,7 +86,7 @@ class ArduinoIoTCloudCertClass {
8686
inline byte* compressedCertSignatureAndDatesBytes() { return _compressedCert.slot.one.data; }
8787
inline int compressedCertSignatureAndDatesLength() {return CERT_COMPRESSED_CERT_SLOT_LENGTH; }
8888
inline byte* compressedCertSerialAndAuthorityKeyIdBytes() { return _compressedCert.slot.two.data; }
89-
inline int compressedCertSerialAndAuthorityKeyIdLenght() {return CERT_COMPRESSED_CERT_SLOT_LENGTH; }
89+
inline int compressedCertSerialAndAuthorityKeyIdLenght() {return CERT_SERIAL_NUMBER_LENGTH + CERT_AUTHORITY_KEY_ID_LENGTH; }
9090
#endif
9191

9292
/* Build CSR */
@@ -135,22 +135,20 @@ class ArduinoIoTCloudCertClass {
135135
struct __attribute__((__packed__)) SerialNumberAndAuthorityKeyIdType {
136136
byte serialNumber[CERT_SERIAL_NUMBER_LENGTH];
137137
byte authorityKeyId[CERT_AUTHORITY_KEY_ID_LENGTH];
138-
byte unused[36];
139138
} values;
140-
byte data[CERT_COMPRESSED_CERT_SLOT_LENGTH];
139+
byte data[CERT_SERIAL_NUMBER_LENGTH + CERT_AUTHORITY_KEY_ID_LENGTH];
141140
};
142141

143142
union CompressedCertDataUType {
144143
struct __attribute__((__packed__)) CompressedCertDataType {
145144
SignatureAndDateUType one;
146145
SerialNumberAndAuthorityKeyIdUType two;
147146
}slot;
148-
byte data[CERT_COMPRESSED_CERT_LENGTH];
147+
byte data[CERT_COMPRESSED_CERT_SLOT_LENGTH + CERT_SERIAL_NUMBER_LENGTH + CERT_AUTHORITY_KEY_ID_LENGTH];
149148
} _compressedCert;
150149

151150
/* only raw EC X Y values 64 byte */
152-
byte _publicKey[CERT_PUBLIC_KEY_LENGTH];
153-
int _publicKeyLen;
151+
const byte * _publicKey;
154152

155153
byte * _certBuffer;
156154
int _certBufferLen;

0 commit comments

Comments
 (0)