Skip to content

Commit 74128b4

Browse files
committed
Extract reconstruction of certificate into util class CryptoUtil
1 parent 8505277 commit 74128b4

File tree

3 files changed

+23
-25
lines changed

3 files changed

+23
-25
lines changed

src/ArduinoIoTCloudTCP.cpp

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,13 @@
2020
#include <ArduinoIoTCloudTCP.h>
2121
#include "utility/time/TimeService.h"
2222
#ifdef BOARD_HAS_ECCX08
23-
#include "utility/crypto/ECCX08Cert.h"
24-
#include "utility/crypto/BearSSLTrustAnchor.h"
2523
#include <ArduinoECCX08.h>
24+
#include "utility/crypto/BearSSLTrustAnchor.h"
2625
#include "utility/crypto/CryptoUtil.h"
2726
#endif
2827

2928
TimeService time_service;
3029

31-
#ifdef BOARD_HAS_ECCX08
32-
const static int keySlot = 0;
33-
const static int compressedCertSlot = 10;
34-
const static int serialNumberAndAuthorityKeyIdentifierSlot = 11;
35-
#endif
36-
3730
const static int CONNECT_SUCCESS = 1;
3831
const static int CONNECT_FAILURE = 0;
3932
const static int CONNECT_FAILURE_SUBSCRIBE = -1;
@@ -86,28 +79,14 @@ int ArduinoIoTCloudTCP::begin(String brokerAddress, uint16_t brokerPort) {
8679
_device_id = CryptoUtil::readDeviceId(ECCX08, ECCX08Slot::DeviceId);
8780
if(_device_id.length() == 0) { Debug.print(DBG_ERROR, "Cryptography processor read failure."); return 0; }
8881

89-
if (!ECCX08Cert.beginReconstruction(keySlot, compressedCertSlot, serialNumberAndAuthorityKeyIdentifierSlot)) {
90-
Debug.print(DBG_ERROR, "Cryptography certificate reconstruction failure.");
91-
return 0;
92-
}
93-
94-
ECCX08Cert.setSubjectCommonName(_device_id);
95-
ECCX08Cert.setIssuerCountryName("US");
96-
ECCX08Cert.setIssuerOrganizationName("Arduino LLC US");
97-
ECCX08Cert.setIssuerOrganizationalUnitName("IT");
98-
ECCX08Cert.setIssuerCommonName("Arduino");
99-
100-
if (!ECCX08Cert.endReconstruction()) {
101-
Debug.print(DBG_ERROR, "Cryptography certificate reconstruction failure.");
102-
return 0;
103-
}
82+
if (!CryptoUtil::reconstructCertificate(ECCX08Cert, _device_id, ECCX08Slot::Key, ECCX08Slot::CompressedCertificate, ECCX08Slot::SerialNumberAndAuthorityKeyIdentifier)) { Debug.print(DBG_ERROR, "Cryptography certificate reconstruction failure."); return 0; }
10483

10584
ArduinoBearSSL.onGetTime(getTime);
10685
#endif /* BOARD_HAS_ECCX08 */
10786

10887
#ifdef BOARD_HAS_ECCX08
10988
_sslClient = new BearSSLClient(_connection->getClient(), ArduinoIoTCloudTrustAnchor, ArduinoIoTCloudTrustAnchor_NUM);
110-
_sslClient->setEccSlot(keySlot, ECCX08Cert.bytes(), ECCX08Cert.length());
89+
_sslClient->setEccSlot(static_cast<int>(ECCX08Slot::Key), ECCX08Cert.bytes(), ECCX08Cert.length());
11190
#elif defined(BOARD_ESP)
11291
_sslClient = new WiFiClientSecure();
11392
_sslClient->setInsecure();

src/utility/crypto/CryptoUtil.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,24 @@ String CryptoUtil::readDeviceId(ECCX08Class & eccx08, ECCX08Slot const slot)
3535
return String(reinterpret_cast<char *>(device_id_bytes));
3636
} else {
3737
return String("");
38-
}
38+
}
39+
}
40+
41+
bool CryptoUtil::reconstructCertificate(ECCX08CertClass & cert, String const & device_id, ECCX08Slot const key, ECCX08Slot const compressed_certificate, ECCX08Slot const serial_number_and_authority_key)
42+
{
43+
if (cert.beginReconstruction(static_cast<int>(key), static_cast<int>(compressed_certificate), static_cast<int>(serial_number_and_authority_key)))
44+
{
45+
cert.setSubjectCommonName(device_id);
46+
cert.setIssuerCountryName("US");
47+
cert.setIssuerOrganizationName("Arduino LLC US");
48+
cert.setIssuerOrganizationalUnitName("IT");
49+
cert.setIssuerCommonName("Arduino");
50+
return cert.endReconstruction();
51+
}
52+
else
53+
{
54+
return false;
55+
}
3956
}
4057

4158
#endif /* BOARD_HAS_ECCX08 */

src/utility/crypto/CryptoUtil.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#include <Arduino.h>
3030
#include <ArduinoECCX08.h>
31+
#include "ECCX08Cert.h"
3132

3233
/******************************************************************************
3334
TYPEDEF
@@ -50,6 +51,7 @@ class CryptoUtil
5051
public:
5152

5253
static String readDeviceId(ECCX08Class & eccx08, ECCX08Slot const slot);
54+
static bool reconstructCertificate(ECCX08CertClass & cert, String const & device_id, ECCX08Slot const key, ECCX08Slot const compressed_certificate, ECCX08Slot const serial_number_and_authority_key);
5355

5456

5557
private:

0 commit comments

Comments
 (0)