Skip to content

Commit cc3b617

Browse files
authored
Merge pull request #308 from bcmi-labs/se050_release
Prepare library to support SE050 crypto device
2 parents 23e7f09 + 6aad616 commit cc3b617

10 files changed

+129
-34
lines changed

Diff for: src/ArduinoIoTCloudTCP.cpp

+18-18
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@
2828
#include "tls/utility/CryptoUtil.h"
2929
#endif
3030

31+
#ifdef BOARD_HAS_SE050
32+
#include "tls/AIoTCSSCert.h"
33+
#include "tls/utility/CryptoUtil.h"
34+
#endif
35+
3136
#ifdef BOARD_HAS_OFFLOADED_ECCX08
3237
#include <ArduinoECCX08.h>
3338
#include "tls/utility/CryptoUtil.h"
@@ -203,7 +208,7 @@ int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress,
203208
_ota_img_sha256 = sha256_str;
204209
#endif /* OTA_ENABLED */
205210

206-
#ifdef BOARD_HAS_OFFLOADED_ECCX08
211+
#if defined(BOARD_HAS_ECCX08) || defined(BOARD_HAS_OFFLOADED_ECCX08) || defined(BOARD_HAS_SE050)
207212
if (!_crypto.begin())
208213
{
209214
DEBUG_ERROR("_crypto.begin() failed.");
@@ -214,34 +219,29 @@ int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress,
214219
DEBUG_ERROR("_crypto.readDeviceId(...) failed.");
215220
return 0;
216221
}
217-
#endif
222+
#endif
218223

219-
#ifdef BOARD_HAS_ECCX08
220-
if (!_crypto.begin())
221-
{
222-
DEBUG_ERROR("Cryptography processor failure. Make sure you have a compatible board.");
223-
return 0;
224-
}
225-
if (!_crypto.readDeviceId(getDeviceId(), CryptoSlot::DeviceId))
226-
{
227-
DEBUG_ERROR("Cryptography processor read failure.");
228-
return 0;
229-
}
224+
#if defined(BOARD_HAS_ECCX08) || defined(BOARD_HAS_SE050)
230225
if (!_crypto.readCert(_cert, CryptoSlot::CompressedCertificate))
231226
{
232227
DEBUG_ERROR("Cryptography certificate reconstruction failure.");
233228
return 0;
234229
}
235-
_sslClient.setClient(_connection->getClient());
236230
_sslClient.setEccSlot(static_cast<int>(CryptoSlot::Key), _cert.bytes(), _cert.length());
237-
#elif defined(BOARD_ESP)
231+
#endif
232+
233+
#if defined(BOARD_HAS_ECCX08)
234+
_sslClient.setClient(_connection->getClient());
235+
#elif defined(BOARD_HAS_SE050)
236+
_sslClient.appendCustomCACert(AIoTSSCert);
237+
#elif defined(BOARD_ESP)
238238
_sslClient.setInsecure();
239-
#endif
239+
#endif
240240

241241
_mqttClient.setClient(_sslClient);
242-
#ifdef BOARD_ESP
242+
#ifdef BOARD_ESP
243243
_mqttClient.setUsernamePassword(getDeviceId(), _password);
244-
#endif
244+
#endif
245245
_mqttClient.onMessage(ArduinoIoTCloudTCP::onMessage);
246246
_mqttClient.setKeepAliveInterval(30 * 1000);
247247
_mqttClient.setConnectionTimeout(1500);

Diff for: src/ArduinoIoTCloudTCP.h

+8-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
#include "tls/utility/CryptoUtil.h"
3232
#elif defined(BOARD_ESP)
3333
#include <WiFiClientSecure.h>
34+
#elif defined(BOARD_HAS_SE050)
35+
#include "tls/utility/CryptoUtil.h"
36+
#include <WiFiSSLSE050Client.h>
3437
#endif
3538

3639
#ifdef BOARD_HAS_OFFLOADED_ECCX08
@@ -71,7 +74,7 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
7174
virtual int connected () override;
7275
virtual void printDebugInfo() override;
7376

74-
#if defined(BOARD_HAS_ECCX08) || defined(BOARD_HAS_OFFLOADED_ECCX08)
77+
#if defined(BOARD_HAS_ECCX08) || defined(BOARD_HAS_OFFLOADED_ECCX08) || defined(BOARD_HAS_SE050)
7578
int begin(ConnectionHandler & connection, bool const enable_watchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS_SECURE_AUTH, uint16_t brokerPort = DEFAULT_BROKER_PORT_SECURE_AUTH);
7679
#else
7780
int begin(ConnectionHandler & connection, bool const enable_watchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS_USER_PASS_AUTH, uint16_t brokerPort = DEFAULT_BROKER_PORT_USER_PASS_AUTH);
@@ -143,6 +146,10 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
143146
#elif defined(BOARD_ESP)
144147
WiFiClientSecure _sslClient;
145148
String _password;
149+
#elif defined(BOARD_HAS_SE050)
150+
ArduinoIoTCloudCertClass _cert;
151+
WiFiSSLSE050Client _sslClient;
152+
CryptoUtil _crypto;
146153
#endif
147154

148155
MqttClient _mqttClient;

Diff for: src/tls/AIoTCSSCert.h

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
This file is part of ArduinoIoTBearSSL.
3+
4+
Copyright 2019 ARDUINO SA (http://www.arduino.cc/)
5+
6+
This software is released under the GNU General Public License version 3,
7+
which covers the main part of ArduinoIoTBearSSL.
8+
The terms of this license can be found at:
9+
https://www.gnu.org/licenses/gpl-3.0.en.html
10+
11+
You can be released from the requirements of the above licenses by purchasing
12+
a commercial license. Buying such a license is mandatory if you want to modify or
13+
otherwise use the software for commercial activities involving the Arduino
14+
software without disclosing the source code of your own applications. To purchase
15+
a commercial license, send an email to [email protected].
16+
17+
*/
18+
19+
#ifndef _AIOTC_SS_CERT_H_
20+
#define _AIOTC_SS_CERT_H_
21+
22+
/******************************************************************************
23+
* INCLUDE
24+
******************************************************************************/
25+
26+
#include <AIoTC_Config.h>
27+
#ifdef BOARD_HAS_SE050
28+
29+
/******************************************************************************
30+
* CONSTANTS
31+
******************************************************************************/
32+
static const char AIoTSSCert[] =
33+
"-----BEGIN CERTIFICATE-----\n"
34+
"MIIBzzCCAXSgAwIBAgIUHxAd66fhJecnwaOR4+wNF03tSlkwCgYIKoZIzj0EAwIw\n"
35+
"RTELMAkGA1UEBhMCVVMxFzAVBgNVBAoTDkFyZHVpbm8gTExDIFVTMQswCQYDVQQL\n"
36+
"EwJJVDEQMA4GA1UEAxMHQXJkdWlubzAeFw0xODA3MjQwOTQ3MDBaFw00ODA3MTYw\n"
37+
"OTQ3MDBaMEUxCzAJBgNVBAYTAlVTMRcwFQYDVQQKEw5BcmR1aW5vIExMQyBVUzEL\n"
38+
"MAkGA1UECxMCSVQxEDAOBgNVBAMTB0FyZHVpbm8wWTATBgcqhkjOPQIBBggqhkjO\n"
39+
"PQMBBwNCAARtd2xaz2EcfUSYUfJe4QJAd7ecvUmio4xOq16YrIL8aVtEIne0TS6O\n"
40+
"3ypxwTls1jkUvdlrGEtL7LPV7kKJiVUio0IwQDAOBgNVHQ8BAf8EBAMCAQYwDwYD\n"
41+
"VR0TAQH/BAUwAwEB/zAdBgNVHQ4EFgQUWz4qa47JsBqoVOY2m4wJ+fzhuYAwCgYI\n"
42+
"KoZIzj0EAwIDSQAwRgIhAL/T3CNmaLUK3D8NDsNz4grH92CqEA3TIL/hApabawXY\n"
43+
"AiEA6tnZ2lrNElKXCajtZg/hjWRE/+giFzBP8riar8qOz2w=\n"
44+
"-----END CERTIFICATE-----\n";
45+
46+
#endif /* #ifdef BOARD_HAS_SE050 */
47+
48+
#endif /* _AIOTC_SS_CERT_H_ */

Diff for: src/tls/bearssl/dec32be.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*/
2424

2525
#include <AIoTC_Config.h>
26-
#if defined(BOARD_HAS_ECCX08) || defined(BOARD_HAS_OFFLOADED_ECCX08)
26+
#if defined(BOARD_HAS_ECCX08) || defined(BOARD_HAS_OFFLOADED_ECCX08) || defined(BOARD_HAS_SE050)
2727

2828
#include "inner.h"
2929

Diff for: src/tls/bearssl/enc32be.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*/
2424

2525
#include <AIoTC_Config.h>
26-
#if defined(BOARD_HAS_ECCX08) || defined(BOARD_HAS_OFFLOADED_ECCX08)
26+
#if defined(BOARD_HAS_ECCX08) || defined(BOARD_HAS_OFFLOADED_ECCX08) || defined(BOARD_HAS_SE050)
2727

2828
#include "inner.h"
2929

Diff for: src/tls/bearssl/sha2small.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*/
2424

2525
#include <AIoTC_Config.h>
26-
#if defined(BOARD_HAS_ECCX08) || defined(BOARD_HAS_OFFLOADED_ECCX08)
26+
#if defined(BOARD_HAS_ECCX08) || defined(BOARD_HAS_OFFLOADED_ECCX08) || defined(BOARD_HAS_SE050)
2727

2828
#include "inner.h"
2929

Diff for: src/tls/utility/Cert.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
#include <AIoTC_Config.h>
2525

26-
#if defined(BOARD_HAS_ECCX08) || defined(BOARD_HAS_OFFLOADED_ECCX08)
26+
#if defined(BOARD_HAS_ECCX08) || defined(BOARD_HAS_OFFLOADED_ECCX08) || defined(BOARD_HAS_SE050)
2727

2828
#include "Cert.h"
2929

@@ -915,4 +915,4 @@ int ArduinoIoTCloudCertClass::appendAuthorityKeyId(const byte authorityKeyId[],
915915
return length + 17;
916916
}
917917

918-
#endif /* (BOARD_HAS_ECCX08) || defined(BOARD_HAS_OFFLOADED_ECCX08) */
918+
#endif /* (BOARD_HAS_ECCX08) || defined(BOARD_HAS_OFFLOADED_ECCX08) || defined(BOARD_HAS_SE050) */

Diff for: src/tls/utility/Cert.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
#include <AIoTC_Config.h>
2626

27-
#if defined(BOARD_HAS_ECCX08) || defined(BOARD_HAS_OFFLOADED_ECCX08)
27+
#if defined(BOARD_HAS_ECCX08) || defined(BOARD_HAS_OFFLOADED_ECCX08) || defined(BOARD_HAS_SE050)
2828

2929
/******************************************************************************
3030
* DEFINE
@@ -182,6 +182,6 @@ class ArduinoIoTCloudCertClass {
182182

183183
};
184184

185-
#endif /* BOARD_HAS_ECCX08 || BOARD_HAS_OFFLOADED_ECCX08 */
185+
#endif /* BOARD_HAS_ECCX08 || BOARD_HAS_OFFLOADED_ECCX08 || BOARD_HAS_SE050*/
186186

187187
#endif /* ARDUINO_IOT_CLOUD_CERT_H */

Diff for: src/tls/utility/CryptoUtil.cpp

+24-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
#include <AIoTC_Config.h>
2323

24-
#if defined(BOARD_HAS_ECCX08) || defined(BOARD_HAS_OFFLOADED_ECCX08)
24+
#if defined(BOARD_HAS_ECCX08) || defined(BOARD_HAS_OFFLOADED_ECCX08) || defined(BOARD_HAS_SE050)
2525

2626
#include "CryptoUtil.h"
2727
#include "SHA256.h"
@@ -36,7 +36,11 @@
3636
* CTOR/DTOR
3737
**************************************************************************************/
3838
CryptoUtil::CryptoUtil()
39+
#if defined(BOARD_HAS_SE050)
40+
: _crypto {SE05X}
41+
#else
3942
: _crypto {ECCX08}
43+
#endif
4044
{
4145

4246
}
@@ -133,18 +137,35 @@ int CryptoUtil::writeDeviceId(String & device_id, const CryptoSlot device_id_slo
133137

134138
int CryptoUtil::writeCert(ArduinoIoTCloudCertClass & cert, const CryptoSlot certSlot)
135139
{
140+
#if defined(BOARD_HAS_SE050)
141+
if (!_crypto.writeSlot(static_cast<int>(certSlot), cert.bytes(), cert.length())) {
142+
return 0;
143+
}
144+
#else
136145
if (!_crypto.writeSlot(static_cast<int>(certSlot), cert.compressedCertSignatureAndDatesBytes(), cert.compressedCertSignatureAndDatesLength())) {
137146
return 0;
138147
}
139148

140149
if (!_crypto.writeSlot(static_cast<int>(certSlot) + 1, cert.compressedCertSerialAndAuthorityKeyIdBytes(), cert.compressedCertSerialAndAuthorityKeyIdLenght())) {
141150
return 0;
142151
}
152+
#endif
143153
return 1;
144154
}
145155

146156
int CryptoUtil::readCert(ArduinoIoTCloudCertClass & cert, const CryptoSlot certSlot)
147157
{
158+
#if defined(BOARD_HAS_SE050)
159+
byte derBuffer[CRYPTO_CERT_BUFFER_LENGTH];
160+
size_t derLen;
161+
if (!_crypto.readBinaryObject(static_cast<int>(certSlot), derBuffer, sizeof(derBuffer), &derLen)) {
162+
return 0;
163+
}
164+
165+
if (!cert.importCert(derBuffer, derLen)) {
166+
return 0;
167+
}
168+
#else
148169
String deviceId;
149170
byte publicKey[CERT_PUBLIC_KEY_LENGTH];
150171

@@ -183,7 +204,8 @@ int CryptoUtil::readCert(ArduinoIoTCloudCertClass & cert, const CryptoSlot certS
183204
if (!cert.signCert()) {
184205
return 0;
185206
}
207+
#endif
186208
return 1;
187209
}
188210

189-
#endif /* (BOARD_HAS_ECCX08) || defined(BOARD_HAS_OFFLOADED_ECCX08) */
211+
#endif /* (BOARD_HAS_ECCX08) || defined(BOARD_HAS_OFFLOADED_ECCX08) || defined(BOARD_HAS_SE050) */

Diff for: src/tls/utility/CryptoUtil.h

+24-6
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,34 @@
2424

2525
#include <AIoTC_Config.h>
2626

27-
#if defined(BOARD_HAS_ECCX08) || defined(BOARD_HAS_OFFLOADED_ECCX08)
27+
#if defined(BOARD_HAS_ECCX08) || defined(BOARD_HAS_OFFLOADED_ECCX08) || defined(BOARD_HAS_SE050)
2828
#include <Arduino.h>
2929
#include "Cert.h"
30+
31+
#if defined(BOARD_HAS_SE050)
32+
#include <SE05X.h>
33+
#else
3034
#include <ArduinoECCX08.h>
35+
#endif
36+
37+
/******************************************************************************
38+
* DEFINE
39+
******************************************************************************/
40+
#if defined(BOARD_HAS_SE050)
41+
#define CRYPTO_SLOT_OFFSET 100
42+
#else
43+
#define CRYPTO_SLOT_OFFSET 0
44+
#endif
3145

3246
/******************************************************************************
3347
TYPEDEF
3448
******************************************************************************/
3549
enum class CryptoSlot : int
3650
{
37-
Key = 0,
38-
CompressedCertificate = 10,
39-
SerialNumberAndAuthorityKeyIdentifier = 11,
40-
DeviceId = 12
51+
Key = (0 + CRYPTO_SLOT_OFFSET),
52+
CompressedCertificate = (10 + CRYPTO_SLOT_OFFSET),
53+
SerialNumberAndAuthorityKeyIdentifier = (11 + CRYPTO_SLOT_OFFSET),
54+
DeviceId = (12 + CRYPTO_SLOT_OFFSET)
4155
};
4256

4357
/******************************************************************************
@@ -64,10 +78,14 @@ class CryptoUtil
6478
int readCert(ArduinoIoTCloudCertClass & cert, const CryptoSlot certSlot);
6579

6680
private:
81+
#if defined(BOARD_HAS_SE050)
82+
SE05XClass & _crypto;
83+
#else
6784
ECCX08Class & _crypto;
85+
#endif
6886

6987
};
7088

71-
#endif /* BOARD_HAS_ECCX08 || BOARD_HAS_OFFLOADED_ECCX08 */
89+
#endif /* BOARD_HAS_ECCX08 || BOARD_HAS_OFFLOADED_ECCX08 || BOARD_HAS_SE050 */
7290

7391
#endif /* ARDUINO_IOT_CLOUD_UTILITY_CRYPTO_CRYPTO_UTIL_H_ */

0 commit comments

Comments
 (0)