Skip to content

Commit 2eca3fa

Browse files
committed
Extract functionality for reading device id from crypto chip from begin method to dedicated util class CryptoUtil for better readability
1 parent 223eaea commit 2eca3fa

File tree

3 files changed

+108
-7
lines changed

3 files changed

+108
-7
lines changed

src/ArduinoIoTCloudTCP.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "utility/crypto/ECCX08Cert.h"
2424
#include "utility/crypto/BearSSLTrustAnchor.h"
2525
#include <ArduinoECCX08.h>
26+
#include "utility/crypto/CryptoUtil.h"
2627
#endif
2728

2829
TimeService time_service;
@@ -31,7 +32,6 @@ TimeService time_service;
3132
const static int keySlot = 0;
3233
const static int compressedCertSlot = 10;
3334
const static int serialNumberAndAuthorityKeyIdentifierSlot = 11;
34-
const static int deviceIdSlot = 12;
3535
#endif
3636

3737
const static int CONNECT_SUCCESS = 1;
@@ -81,17 +81,13 @@ int ArduinoIoTCloudTCP::begin(String brokerAddress, uint16_t brokerPort) {
8181
_brokerPort = brokerPort;
8282

8383
#ifdef BOARD_HAS_ECCX08
84-
byte deviceIdBytes[72];
8584
if (!ECCX08.begin()) {
8685
Debug.print(DBG_ERROR, "Cryptography processor failure. Make sure you have a compatible board.");
8786
return 0;
8887
}
8988

90-
if (!ECCX08.readSlot(deviceIdSlot, deviceIdBytes, sizeof(deviceIdBytes))) {
91-
Debug.print(DBG_ERROR, "Cryptography processor read failure.");
92-
return 0;
93-
}
94-
_device_id = (char*)deviceIdBytes;
89+
_device_id = CryptoUtil::readDeviceId(ECCX08, ECCX08Slot::DeviceId);
90+
if(_device_id.length() == 0) { Debug.print(DBG_ERROR, "Cryptography processor read failure."); return 0; }
9591

9692
if (!ECCX08Cert.beginReconstruction(keySlot, compressedCertSlot, serialNumberAndAuthorityKeyIdentifierSlot)) {
9793
Debug.print(DBG_ERROR, "Cryptography certificate reconstruction failure.");

src/utility/crypto/CryptoUtil.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
This file is part of ArduinoIoTCloud.
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 arduino-cli.
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+
INCLUDE
20+
******************************************************************************/
21+
22+
#include "CryptoUtil.h"
23+
24+
#ifdef BOARD_HAS_ECCX08
25+
26+
/******************************************************************************
27+
PUBLIC MEMBER FUNCTIONS
28+
******************************************************************************/
29+
30+
String CryptoUtil::readDeviceId(ECCX08Class & eccx08, ECCX08Slot const slot)
31+
{
32+
byte device_id_bytes[72] = {0};
33+
34+
if (eccx08.readSlot(static_cast<int>(slot), device_id_bytes, sizeof(device_id_bytes))) {
35+
return String(reinterpret_cast<char *>(device_id_bytes));
36+
} else {
37+
return String("");
38+
}
39+
}
40+
41+
#endif /* BOARD_HAS_ECCX08 */

src/utility/crypto/CryptoUtil.h

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
This file is part of ArduinoIoTCloud.
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 arduino-cli.
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+
#ifndef ARDUINO_IOT_CLOUD_UTILITY_CRYPTO_CRYPTO_UTIL_H_
19+
#define ARDUINO_IOT_CLOUD_UTILITY_CRYPTO_CRYPTO_UTIL_H_
20+
21+
/******************************************************************************
22+
INCLUDE
23+
******************************************************************************/
24+
25+
#include <ArduinoIoTCloud_Defines.h>
26+
27+
#ifdef BOARD_HAS_ECCX08
28+
29+
#include <Arduino.h>
30+
#include <ArduinoECCX08.h>
31+
32+
/******************************************************************************
33+
TYPEDEF
34+
******************************************************************************/
35+
36+
enum class ECCX08Slot : int
37+
{
38+
Key = 0,
39+
CompressedCertificate = 10,
40+
SerialNumberAndAuthorityKeyIdentifier = 11,
41+
DeviceId = 12
42+
};
43+
44+
/******************************************************************************
45+
CLASS DECLARATION
46+
******************************************************************************/
47+
48+
class CryptoUtil
49+
{
50+
public:
51+
52+
static String readDeviceId(ECCX08Class & eccx08, ECCX08Slot const slot);
53+
54+
55+
private:
56+
57+
CryptoUtil() { }
58+
CryptoUtil(CryptoUtil const & other) { }
59+
60+
};
61+
62+
#endif /* BOARD_HAS_ECCX08 */
63+
64+
#endif /* ARDUINO_IOT_CLOUD_UTILITY_CRYPTO_CRYPTO_UTIL_H_ */

0 commit comments

Comments
 (0)