@@ -59,6 +59,9 @@ ArduinoIoTCloudTCP::ArduinoIoTCloudTCP()
5959#ifdef BOARD_HAS_SECRET_KEY
6060, _password(" " )
6161#endif
62+ #if defined(BOARD_HAS_SECURE_ELEMENT)
63+ , _writeCertOnConnect(false )
64+ #endif
6265, _mqttClient{nullptr }
6366, _messageTopicOut(" " )
6467, _messageTopicIn(" " )
@@ -80,11 +83,6 @@ int ArduinoIoTCloudTCP::begin(ConnectionHandler & connection, bool const enable_
8083{
8184 _connection = &connection;
8285 _brokerAddress = brokerAddress;
83- #ifdef BOARD_HAS_SECRET_KEY
84- _brokerPort = _password.length () ? DEFAULT_BROKER_PORT_USER_PASS_AUTH : brokerPort;
85- #else
86- _brokerPort = brokerPort;
87- #endif
8886
8987 /* Setup broker TLS client */
9088 _brokerClient.begin (connection);
@@ -94,20 +92,7 @@ int ArduinoIoTCloudTCP::begin(ConnectionHandler & connection, bool const enable_
9492 _otaClient.begin (connection);
9593#endif
9694
97- /* Setup TimeService */
98- _time_service.begin (_connection);
99-
100- /* Setup retry timers */
101- _connection_attempt.begin (AIOT_CONFIG_RECONNECTION_RETRY_DELAY_ms, AIOT_CONFIG_MAX_RECONNECTION_RETRY_DELAY_ms);
102- return begin (enable_watchdog, _brokerAddress, _brokerPort);
103- }
104-
105- int ArduinoIoTCloudTCP::begin (bool const enable_watchdog, String brokerAddress, uint16_t brokerPort)
106- {
107- _brokerAddress = brokerAddress;
108- _brokerPort = brokerPort;
109-
110- #if defined(BOARD_HAS_SECRET_KEY)
95+ #if defined (BOARD_HAS_SECRET_KEY)
11196 /* If board is not configured for username and password login */
11297 if (!_password.length ())
11398 {
@@ -129,23 +114,44 @@ int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress,
129114 DEBUG_ERROR (" ArduinoIoTCloudTCP::%s could not read device id." , __FUNCTION__);
130115 return 0 ;
131116 }
132- #if !defined(BOARD_HAS_OFFLOADED_ECCX08)
133- if (!SElementArduinoCloudCertificate::read (_selement, _cert, SElementArduinoCloudSlot::CompressedCertificate))
134- {
135- DEBUG_ERROR (" ArduinoIoTCloudTCP::%s could not read device certificate." , __FUNCTION__);
136- return 0 ;
117+ if (!_writeCertOnConnect) {
118+ /* No update pending read certificate stored in secure element */
119+ if (!SElementArduinoCloudCertificate::read (_selement, _cert, SElementArduinoCloudSlot::CompressedCertificate))
120+ {
121+ DEBUG_ERROR (" ArduinoIoTCloudTCP::%s could not read device certificate." , __FUNCTION__);
122+ return 0 ;
123+ }
137124 }
125+ #if !defined(BOARD_HAS_OFFLOADED_ECCX08)
138126 _brokerClient.setEccSlot (static_cast <int >(SElementArduinoCloudSlot::Key), _cert.bytes (), _cert.length ());
139127 #if OTA_ENABLED
140128 _otaClient.setEccSlot (static_cast <int >(SElementArduinoCloudSlot::Key), _cert.bytes (), _cert.length ());
141129 #endif
142130 #endif
131+ _brokerPort = (brokerPort == DEFAULT_BROKER_PORT_AUTO) ? mqttPort () : brokerPort;
143132#endif
144133
145134#if defined(BOARD_HAS_SECRET_KEY)
146135 }
136+ else
137+ {
138+ _brokerPort = (brokerPort == DEFAULT_BROKER_PORT_AUTO) ? DEFAULT_BROKER_PORT_USER_PASS_AUTH : brokerPort;
139+ }
147140#endif
148141
142+ /* Setup TimeService */
143+ _time_service.begin (_connection);
144+
145+ /* Setup retry timers */
146+ _connection_attempt.begin (AIOT_CONFIG_RECONNECTION_RETRY_DELAY_ms, AIOT_CONFIG_MAX_RECONNECTION_RETRY_DELAY_ms);
147+ return begin (enable_watchdog, _brokerAddress, _brokerPort);
148+ }
149+
150+ int ArduinoIoTCloudTCP::begin (bool const enable_watchdog, String brokerAddress, uint16_t brokerPort)
151+ {
152+ _brokerAddress = brokerAddress;
153+ _brokerPort = brokerPort;
154+
149155 _mqttClient.setClient (_brokerClient);
150156
151157#ifdef BOARD_HAS_SECRET_KEY
@@ -281,6 +287,17 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConnectMqttBroker()
281287 /* Subscribe to message topic to receive commands */
282288 _mqttClient.subscribe (_messageTopicIn);
283289
290+ #if defined(BOARD_HAS_SECURE_ELEMENT)
291+ /* A device certificate update was pending */
292+ if (_writeCertOnConnect)
293+ {
294+ if (SElementArduinoCloudCertificate::write (_selement, _cert, SElementArduinoCloudSlot::CompressedCertificate))
295+ {
296+ DEBUG_INFO (" ArduinoIoTCloudTCP::%s device certificate update done." , __FUNCTION__);
297+ _writeCertOnConnect = false ;
298+ }
299+ }
300+ #endif
284301 DEBUG_VERBOSE (" ArduinoIoTCloudTCP::%s connected to %s:%d" , __FUNCTION__, _brokerAddress.c_str (), _brokerPort);
285302 return State::Connected;
286303 }
@@ -558,6 +575,62 @@ int ArduinoIoTCloudTCP::write(String const topic, byte const data[], int const l
558575 return 0 ;
559576}
560577
578+ #if defined(BOARD_HAS_SECURE_ELEMENT)
579+ int ArduinoIoTCloudTCP::mqttPort ()
580+ {
581+ if (memcmp (DEPRECATED_BROKER_AUTHORITY_KEY_IDENTIFIER, _cert.authorityKeyIdentifierBytes () , ECP256_CERT_AUTHORITY_KEY_ID_LENGTH) == 0 ) {
582+ return DEPRECATED_BROKER_PORT_SECURE_AUTH;
583+ } else {
584+ return DEFAULT_BROKER_PORT_SECURE_AUTH;
585+ }
586+ }
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::signatureCompare (_cert.signatureBytes (), signature) <= 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+ #if defined(BOARD_HAS_OFFLOADED_ECCX08)
621+ if (SElementArduinoCloudCertificate::write (_selement, _cert, SElementArduinoCloudSlot::CompressedCertificate))
622+ {
623+ DEBUG_INFO (" ArduinoIoTCloudTCP::%s update done." , __FUNCTION__);
624+ }
625+ #else
626+ _writeCertOnConnect = true ;
627+ #endif
628+ return 1 ;
629+ }
630+ return 0 ;
631+ }
632+ #endif
633+
561634/* *****************************************************************************
562635 * EXTERN DEFINITION
563636 ******************************************************************************/
0 commit comments