Skip to content

Commit aa8adf1

Browse files
committed
SecureElement: move functions to read/write certificate inside a dedicated ArduinoCloud utility class
1 parent 451f323 commit aa8adf1

4 files changed

+165
-77
lines changed

src/SecureElement.cpp

-77
Original file line numberDiff line numberDiff line change
@@ -36,83 +36,6 @@ SecureElement::SecureElement()
3636
* PUBLIC MEMBER FUNCTIONS
3737
******************************************************************************/
3838

39-
int SecureElement::writeCert(ECP256Certificate & cert, const int certSlot)
40-
{
41-
#if defined(BOARD_HAS_SE050)
42-
if (!_secureElement.writeSlot(certSlot, cert.bytes(), cert.length())) {
43-
return 0;
44-
}
45-
#else
46-
if (!_secureElement.writeSlot(certSlot, cert.compressedCertSignatureAndDatesBytes(), cert.compressedCertSignatureAndDatesLength())) {
47-
return 0;
48-
}
49-
50-
if (!_secureElement.writeSlot(certSlot + 1, cert.compressedCertSerialAndAuthorityKeyIdBytes(), cert.compressedCertSerialAndAuthorityKeyIdLenght())) {
51-
return 0;
52-
}
53-
54-
if (!_secureElement.writeSlot(certSlot + 2, cert.subjectCommonNameBytes(), cert.subjectCommonNameLenght())) {
55-
return 0;
56-
}
57-
#endif
58-
return 1;
59-
}
60-
61-
int SecureElement::readCert(ECP256Certificate & cert, const int certSlot, const int keySlot)
62-
{
63-
#if defined(BOARD_HAS_SE050)
64-
byte derBuffer[SE_CERT_BUFFER_LENGTH];
65-
size_t derLen;
66-
if (!_secureElement.readBinaryObject(certSlot, derBuffer, sizeof(derBuffer), &derLen)) {
67-
return 0;
68-
}
69-
70-
if (!cert.importCert(derBuffer, derLen)) {
71-
return 0;
72-
}
73-
#else
74-
String deviceId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
75-
byte publicKey[ECP256_CERT_PUBLIC_KEY_LENGTH];
76-
77-
cert.begin();
78-
79-
if (!_secureElement.readSlot(certSlot, cert.compressedCertSignatureAndDatesBytes(), cert.compressedCertSignatureAndDatesLength())) {
80-
return 0;
81-
}
82-
83-
if (!_secureElement.readSlot(certSlot + 1, cert.compressedCertSerialAndAuthorityKeyIdBytes(), cert.compressedCertSerialAndAuthorityKeyIdLenght())) {
84-
return 0;
85-
}
86-
87-
if (!_secureElement.readSlot(certSlot + 2, (byte*)deviceId.begin(), deviceId.length())) {
88-
return 0;
89-
}
90-
91-
if (!_secureElement.generatePublicKey(keySlot, publicKey)) {
92-
return 0;
93-
}
94-
95-
cert.setSubjectCommonName(deviceId);
96-
cert.setIssuerCountryName("US");
97-
cert.setIssuerOrganizationName("Arduino LLC US");
98-
cert.setIssuerOrganizationalUnitName("IT");
99-
cert.setIssuerCommonName("Arduino");
100-
101-
if (!cert.setPublicKey(publicKey, ECP256_CERT_PUBLIC_KEY_LENGTH)) {
102-
return 0;
103-
}
104-
105-
if (!cert.buildCert()) {
106-
return 0;
107-
}
108-
109-
if (!cert.signCert()) {
110-
return 0;
111-
}
112-
#endif
113-
return 1;
114-
}
115-
11639
int SecureElement::SHA256(const uint8_t *buffer, size_t size, uint8_t *digest)
11740
{
11841
#if defined(SECURE_ELEMENT_IS_SOFTSE)

src/utility/SElementArduinoCloud.h

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
This file is part of the Arduino_SecureElement library.
3+
4+
Copyright (c) 2024 Arduino SA
5+
6+
This Source Code Form is subject to the terms of the Mozilla Public
7+
License, v. 2.0. If a copy of the MPL was not distributed with this
8+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
11+
#ifndef SECURE_ELEMENT_ARDUINO_CLOUD_H_
12+
#define SECURE_ELEMENT_ARDUINO_CLOUD_H_
13+
14+
#include <Arduino_SecureElement.h>
15+
16+
/******************************************************************************
17+
* DEFINE
18+
******************************************************************************/
19+
#if defined(SECURE_ELEMENT_IS_SE050)
20+
#define SECURE_ELEMENT_SLOT_OFFSET 100
21+
#else
22+
#define SECURE_ELEMENT_SLOT_OFFSET 0
23+
#endif
24+
25+
/******************************************************************************
26+
TYPEDEF
27+
******************************************************************************/
28+
enum class SElementArduinoCloudSlot : int
29+
{
30+
Key = (0 + SECURE_ELEMENT_SLOT_OFFSET),
31+
CompressedCertificate = (10 + SECURE_ELEMENT_SLOT_OFFSET),
32+
SerialNumberAndAuthorityKeyIdentifier = (11 + SECURE_ELEMENT_SLOT_OFFSET),
33+
DeviceId = (12 + SECURE_ELEMENT_SLOT_OFFSET)
34+
};
35+
36+
#endif /* SECURE_ELEMENT_ARDUINO_CLOUD_H_ */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
This file is part of the Arduino_SecureElement library.
3+
4+
Copyright (c) 2024 Arduino SA
5+
6+
This Source Code Form is subject to the terms of the Mozilla Public
7+
License, v. 2.0. If a copy of the MPL was not distributed with this
8+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
11+
/******************************************************************************
12+
* INCLUDE
13+
******************************************************************************/
14+
15+
#include <utility/SElementArduinoCloudCertificate.h>
16+
17+
int SElementArduinoCloudCertificate::write(SecureElement & se, ECP256Certificate & cert, const SElementArduinoCloudSlot certSlot)
18+
{
19+
#if defined(SECURE_ELEMENT_IS_SE050) || defined(SECURE_ELEMENT_IS_SOFTSE)
20+
if (!se.writeSlot(static_cast<int>(certSlot), cert.bytes(), cert.length())) {
21+
return 0;
22+
}
23+
#else
24+
if (!se.writeSlot(static_cast<int>(certSlot), cert.compressedCertSignatureAndDatesBytes(), cert.compressedCertSignatureAndDatesLength())) {
25+
return 0;
26+
}
27+
28+
if (!se.writeSlot(static_cast<int>(certSlot) + 1, cert.compressedCertSerialAndAuthorityKeyIdBytes(), cert.compressedCertSerialAndAuthorityKeyIdLenght())) {
29+
return 0;
30+
}
31+
32+
if (!se.writeSlot(static_cast<int>(certSlot) + 2, cert.subjectCommonNameBytes(), cert.subjectCommonNameLenght())) {
33+
return 0;
34+
}
35+
#endif
36+
return 1;
37+
}
38+
39+
int SElementArduinoCloudCertificate::read(SecureElement & se, ECP256Certificate & cert, const SElementArduinoCloudSlot certSlot, const SElementArduinoCloudSlot keySlot)
40+
{
41+
#if defined(SECURE_ELEMENT_IS_SE050) || defined(SECURE_ELEMENT_IS_SOFTSE)
42+
byte derBuffer[SE_CERT_BUFFER_LENGTH];
43+
size_t derLen;
44+
if (!se.readSlot(static_cast<int>(certSlot), derBuffer, sizeof(derBuffer))) {
45+
return 0;
46+
}
47+
48+
derLen = (derBuffer[2] << 8 | derBuffer[3]) + 4;
49+
Serial.print("derLen:");
50+
Serial.println(derLen);
51+
if (!cert.importCert(derBuffer, derLen)) {
52+
return 0;
53+
}
54+
#else
55+
String deviceId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
56+
byte publicKey[ECP256_CERT_PUBLIC_KEY_LENGTH];
57+
58+
cert.begin();
59+
60+
if (!se.readSlot(static_cast<int>(certSlot), cert.compressedCertSignatureAndDatesBytes(), cert.compressedCertSignatureAndDatesLength())) {
61+
return 0;
62+
}
63+
64+
if (!se.readSlot(static_cast<int>(certSlot) + 1, cert.compressedCertSerialAndAuthorityKeyIdBytes(), cert.compressedCertSerialAndAuthorityKeyIdLenght())) {
65+
return 0;
66+
}
67+
68+
if (!se.readSlot(static_cast<int>(certSlot) + 2, (byte*)deviceId.begin(), deviceId.length())) {
69+
return 0;
70+
}
71+
72+
if (!se.generatePublicKey(static_cast<int>(keySlot), publicKey)) {
73+
return 0;
74+
}
75+
76+
cert.setSubjectCommonName(deviceId);
77+
cert.setIssuerCountryName("US");
78+
cert.setIssuerOrganizationName("Arduino LLC US");
79+
cert.setIssuerOrganizationalUnitName("IT");
80+
cert.setIssuerCommonName("Arduino");
81+
82+
if (!cert.setPublicKey(publicKey, ECP256_CERT_PUBLIC_KEY_LENGTH)) {
83+
return 0;
84+
}
85+
86+
if (!cert.buildCert()) {
87+
return 0;
88+
}
89+
90+
if (!cert.signCert()) {
91+
return 0;
92+
}
93+
#endif
94+
return 1;
95+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
This file is part of the Arduino_SecureElement library.
3+
4+
Copyright (c) 2024 Arduino SA
5+
6+
This Source Code Form is subject to the terms of the Mozilla Public
7+
License, v. 2.0. If a copy of the MPL was not distributed with this
8+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
11+
#ifndef SECURE_ELEMENT_ARDUINO_CLOUD_CERTIFICATE_H_
12+
#define SECURE_ELEMENT_ARDUINO_CLOUD_CERTIFICATE_H_
13+
14+
/******************************************************************************
15+
* INCLUDE
16+
******************************************************************************/
17+
18+
#include <utility/SElementCertificate.h>
19+
#include <utility/SElementArduinoCloud.h>
20+
21+
/******************************************************************************
22+
* CLASS DECLARATION
23+
******************************************************************************/
24+
25+
class SElementArduinoCloudCertificate : public SElementCertificate
26+
{
27+
public:
28+
29+
static int write(SecureElement & se, ECP256Certificate & cert, const SElementArduinoCloudSlot certSlot);
30+
static int read(SecureElement & se, ECP256Certificate & cert, const SElementArduinoCloudSlot certSlot, const SElementArduinoCloudSlot keySlot = SElementArduinoCloudSlot::Key);
31+
32+
};
33+
34+
#endif /* SECURE_ELEMENT_ARDUINO_CLOUD_CERTIFICATE_H_ */

0 commit comments

Comments
 (0)