@@ -59,6 +59,9 @@ ArduinoIoTCloudTCP::ArduinoIoTCloudTCP()
59
59
#ifdef BOARD_HAS_SECRET_KEY
60
60
, _password(" " )
61
61
#endif
62
+ #if defined(BOARD_HAS_SECURE_ELEMENT)
63
+ , _writeCertOnConnect(false )
64
+ #endif
62
65
, _mqttClient{nullptr }
63
66
, _messageTopicOut(" " )
64
67
, _messageTopicIn(" " )
@@ -80,11 +83,6 @@ int ArduinoIoTCloudTCP::begin(ConnectionHandler & connection, bool const enable_
80
83
{
81
84
_connection = &connection;
82
85
_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
88
86
89
87
/* Setup broker TLS client */
90
88
_brokerClient.begin (connection);
@@ -94,20 +92,7 @@ int ArduinoIoTCloudTCP::begin(ConnectionHandler & connection, bool const enable_
94
92
_otaClient.begin (connection);
95
93
#endif
96
94
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)
111
96
/* If board is not configured for username and password login */
112
97
if (!_password.length ())
113
98
{
@@ -129,23 +114,44 @@ int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress,
129
114
DEBUG_ERROR (" ArduinoIoTCloudTCP::%s could not read device id." , __FUNCTION__);
130
115
return 0 ;
131
116
}
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
+ }
137
124
}
125
+ #if !defined(BOARD_HAS_OFFLOADED_ECCX08)
138
126
_brokerClient.setEccSlot (static_cast <int >(SElementArduinoCloudSlot::Key), _cert.bytes (), _cert.length ());
139
127
#if OTA_ENABLED
140
128
_otaClient.setEccSlot (static_cast <int >(SElementArduinoCloudSlot::Key), _cert.bytes (), _cert.length ());
141
129
#endif
142
130
#endif
131
+ _brokerPort = (brokerPort == DEFAULT_BROKER_PORT_AUTO) ? mqttPort () : brokerPort;
143
132
#endif
144
133
145
134
#if defined(BOARD_HAS_SECRET_KEY)
146
135
}
136
+ else
137
+ {
138
+ _brokerPort = (brokerPort == DEFAULT_BROKER_PORT_AUTO) ? DEFAULT_BROKER_PORT_USER_PASS_AUTH : brokerPort;
139
+ }
147
140
#endif
148
141
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
+
149
155
_mqttClient.setClient (_brokerClient);
150
156
151
157
#ifdef BOARD_HAS_SECRET_KEY
@@ -281,6 +287,17 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConnectMqttBroker()
281
287
/* Subscribe to message topic to receive commands */
282
288
_mqttClient.subscribe (_messageTopicIn);
283
289
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
284
301
DEBUG_VERBOSE (" ArduinoIoTCloudTCP::%s connected to %s:%d" , __FUNCTION__, _brokerAddress.c_str (), _brokerPort);
285
302
return State::Connected;
286
303
}
@@ -558,6 +575,62 @@ int ArduinoIoTCloudTCP::write(String const topic, byte const data[], int const l
558
575
return 0 ;
559
576
}
560
577
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
+
561
634
/* *****************************************************************************
562
635
* EXTERN DEFINITION
563
636
******************************************************************************/
0 commit comments