Skip to content

Commit a32d473

Browse files
arduinopennam
arduino
authored andcommitted
Switch from weak variable to updateCertificate(...) API
1 parent d5cc0e0 commit a32d473

File tree

2 files changed

+47
-31
lines changed

2 files changed

+47
-31
lines changed

src/ArduinoIoTCloudTCP.cpp

+41-29
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,6 @@ unsigned long getTime()
4343
return ArduinoCloud.getInternalTime();
4444
}
4545

46-
char NOT_AFTER[] __attribute__((weak)) = "";
47-
char NOT_BEFORE[] __attribute__((weak)) = "";
48-
char SERIAL_NUMBER[] __attribute__((weak)) = "";
49-
char AUTHORITY_KEY_ID[] __attribute__((weak)) = "";
50-
char SIGNATURE[] __attribute__((weak)) = "";
51-
52-
5346
/******************************************************************************
5447
CTOR/DTOR
5548
******************************************************************************/
@@ -121,33 +114,14 @@ int ArduinoIoTCloudTCP::begin(ConnectionHandler & connection, bool const enable_
121114
DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not read device id.", __FUNCTION__);
122115
return 0;
123116
}
124-
/* read certificate stored in secure element to compare AUTHORITY_KEY_ID */
125-
if (!SElementArduinoCloudCertificate::read(_selement, _cert, SElementArduinoCloudSlot::CompressedCertificate))
126-
{
127-
DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not read device certificate.", __FUNCTION__);
128-
return 0;
129-
}
130-
/* check if we need to update and try rebuild */
131-
int result = SElementArduinoCloudCertificate::update(_selement, _cert, getDeviceId(), String(NOT_BEFORE), String(NOT_AFTER), String(SERIAL_NUMBER), String(AUTHORITY_KEY_ID), String(SIGNATURE));
132-
if (result > 0)
133-
{
134-
DEBUG_INFO("ArduinoIoTCloudTCP::%s device certificate update request.", __FUNCTION__);
135-
_writeOnConnect = true;
136-
}
137-
else if (result < 0)
138-
{
139-
DEBUG_ERROR("ArduinoIoTCloudTCP::%s device certificate rebuild error.", __FUNCTION__);
140-
/* there was an error trying to rebuild certificate re-read old one */
117+
if (!_writeOnConnect) {
118+
/* No update pending read certificate stored in secure element */
141119
if (!SElementArduinoCloudCertificate::read(_selement, _cert, SElementArduinoCloudSlot::CompressedCertificate))
142120
{
143-
DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not read device id.", __FUNCTION__);
121+
DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not read device certificate.", __FUNCTION__);
144122
return 0;
145123
}
146124
}
147-
else
148-
{
149-
DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s device certificate updated.", __FUNCTION__);
150-
}
151125
#if !defined(BOARD_HAS_OFFLOADED_ECCX08)
152126
_brokerClient.setEccSlot(static_cast<int>(SElementArduinoCloudSlot::Key), _cert.bytes(), _cert.length());
153127
#if OTA_ENABLED
@@ -610,6 +584,44 @@ int ArduinoIoTCloudTCP::mqttPort()
610584
return DEFAULT_BROKER_PORT_SECURE_AUTH;
611585
}
612586
}
587+
588+
int ArduinoIoTCloudTCP::updateCertificate(String authorityKeyIdentifier, String serialNumber, String notBefore, String notAfter, String signature)
589+
{
590+
if (!_selement.begin())
591+
{
592+
DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not initialize secure element.", __FUNCTION__);
593+
#if defined(ARDUINO_UNOWIFIR4)
594+
if (String(WiFi.firmwareVersion()) < String("0.4.1")) {
595+
DEBUG_ERROR("ArduinoIoTCloudTCP::%s In order to read device certificate, WiFi firmware needs to be >= 0.4.1, current %s", __FUNCTION__, WiFi.firmwareVersion());
596+
}
597+
#endif
598+
return 0;
599+
}
600+
if (!SElementArduinoCloudDeviceId::read(_selement, getDeviceId(), SElementArduinoCloudSlot::DeviceId))
601+
{
602+
DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not read device id.", __FUNCTION__);
603+
return 0;
604+
}
605+
/* read certificate stored in secure element to compare AUTHORITY_KEY_ID */
606+
if (!SElementArduinoCloudCertificate::read(_selement, _cert, SElementArduinoCloudSlot::CompressedCertificate))
607+
{
608+
DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not read device certificate.", __FUNCTION__);
609+
return 0;
610+
}
611+
/* check if we need to update 0 = equal <0 = error skip rebuild */
612+
if(SElementArduinoCloudCertificate::isAuthorityKeyIdDifferent(_cert, authorityKeyIdentifier) <= 0) {
613+
DEBUG_INFO("ArduinoIoTCloudTCP::%s request skipped.", __FUNCTION__);
614+
return 0;
615+
}
616+
/* rebuild device certificate */
617+
if (SElementArduinoCloudCertificate::rebuild(_selement, _cert, getDeviceId(), notBefore, notAfter, serialNumber, authorityKeyIdentifier, signature))
618+
{
619+
DEBUG_INFO("ArduinoIoTCloudTCP::%s request started.", __FUNCTION__);
620+
_writeOnConnect = true;
621+
return 1;
622+
}
623+
return 0;
624+
}
613625
#endif
614626

615627
/******************************************************************************

src/ArduinoIoTCloudTCP.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,14 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
8080
int begin(ConnectionHandler & connection, bool const enable_watchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS, uint16_t brokerPort = DEFAULT_BROKER_PORT_AUTO);
8181
int begin(bool const enable_watchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS, uint16_t brokerPort = DEFAULT_BROKER_PORT_AUTO);
8282

83-
#ifdef BOARD_HAS_SECRET_KEY
83+
#if defined(BOARD_HAS_SECURE_ELEMENT)
84+
int updateCertificate(String authorityKeyIdentifier, String serialNumber, String notBefore, String notAfter, String signature);
85+
#endif
86+
87+
#ifdef BOARD_HAS_SECRET_KEY
8488
inline void setBoardId (String const device_id) { setDeviceId(device_id); }
8589
inline void setSecretDeviceKey(String const password) { _password = password; }
86-
#endif
90+
#endif
8791

8892
inline String getBrokerAddress() const { return _brokerAddress; }
8993
inline uint16_t getBrokerPort () const { return _brokerPort; }

0 commit comments

Comments
 (0)