Skip to content

Commit 2ad2162

Browse files
committed
Add function to rebuild certificate from compressed data
1 parent badc8a6 commit 2ad2162

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

src/utility/SElementArduinoCloudCertificate.cpp

+66
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,30 @@
1414

1515
#include <utility/SElementArduinoCloudCertificate.h>
1616

17+
/******************************************************************************
18+
* LOCAL MODULE FUNCTIONS
19+
******************************************************************************/
20+
21+
static void hexStringToBytes(String in, byte out[], int length) {
22+
int inLength = in.length();
23+
in.toUpperCase();
24+
int outLength = 0;
25+
26+
for (int i = 0; i < inLength && outLength < length; i += 2) {
27+
char highChar = in[i];
28+
char lowChar = in[i + 1];
29+
30+
byte highByte = (highChar <= '9') ? (highChar - '0') : (highChar + 10 - 'A');
31+
byte lowByte = (lowChar <= '9') ? (lowChar - '0') : (lowChar + 10 - 'A');
32+
33+
out[outLength++] = (highByte << 4) | (lowByte & 0xF);
34+
}
35+
}
36+
37+
/******************************************************************************
38+
* PUBLIC MEMBER FUNCTIONS
39+
******************************************************************************/
40+
1741
int SElementArduinoCloudCertificate::write(SecureElement & se, ECP256Certificate & cert, const SElementArduinoCloudSlot certSlot)
1842
{
1943
#if defined(SECURE_ELEMENT_IS_SE050) || defined(SECURE_ELEMENT_IS_SOFTSE)
@@ -92,3 +116,45 @@ int SElementArduinoCloudCertificate::read(SecureElement & se, ECP256Certificate
92116
#endif
93117
return 1;
94118
}
119+
120+
int SElementArduinoCloudCertificate::rebuild(SecureElement & se, ECP256Certificate & cert, const String & deviceId, const String & notBefore, const String & notAfter, const String & serialNumber, const String & authorityKeyIdentifier, const String & signature, const SElementArduinoCloudSlot keySlot)
121+
{
122+
byte serialNumberBytes[ECP256_CERT_SERIAL_NUMBER_LENGTH];
123+
byte authorityKeyIdentifierBytes[ECP256_CERT_AUTHORITY_KEY_ID_LENGTH];
124+
byte signatureBytes[ECP256_CERT_SIGNATURE_LENGTH];
125+
126+
if (!deviceId.length() || !notBefore.length() || !notAfter.length() || !serialNumber.length() || !authorityKeyIdentifier.length() || !signature.length() ) {
127+
DEBUG_ERROR("SEACC::%s input params error.", __FUNCTION__);
128+
return 0;
129+
}
130+
131+
hexStringToBytes(serialNumber, serialNumberBytes, sizeof(serialNumberBytes));
132+
hexStringToBytes(authorityKeyIdentifier, authorityKeyIdentifierBytes, sizeof(authorityKeyIdentifierBytes));
133+
hexStringToBytes(signature, signatureBytes, sizeof(signatureBytes));
134+
135+
if (!cert.begin()) {
136+
DEBUG_ERROR("SEACC::%s cert begin error", __FUNCTION__);
137+
return -1;
138+
}
139+
140+
cert.setSubjectCommonName(deviceId);
141+
cert.setIssuerCountryName("US");
142+
cert.setIssuerOrganizationName("Arduino LLC US");
143+
cert.setIssuerOrganizationalUnitName("IT");
144+
cert.setIssuerCommonName("Arduino");
145+
cert.setSignature(signatureBytes, sizeof(signatureBytes));
146+
cert.setAuthorityKeyId(authorityKeyIdentifierBytes, sizeof(authorityKeyIdentifierBytes));
147+
cert.setSerialNumber(serialNumberBytes, sizeof(serialNumberBytes));
148+
cert.setIssueYear(notBefore.substring(0,4).toInt());
149+
cert.setIssueMonth(notBefore.substring(5,7).toInt());
150+
cert.setIssueDay(notBefore.substring(8,10).toInt());
151+
cert.setIssueHour(notBefore.substring(11,13).toInt());
152+
cert.setExpireYears(notAfter.substring(0,4).toInt() - notBefore.substring(0,4).toInt());
153+
154+
155+
if (!SElementCertificate::build(se, cert, static_cast<int>(keySlot))) {
156+
DEBUG_ERROR("SEACC::%s cert build error", __FUNCTION__);
157+
return -1;
158+
}
159+
return 1;
160+
}

src/utility/SElementArduinoCloudCertificate.h

+4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ class SElementArduinoCloudCertificate : public SElementCertificate
2828

2929
static int write(SecureElement & se, ECP256Certificate & cert, const SElementArduinoCloudSlot certSlot);
3030
static int read(SecureElement & se, ECP256Certificate & cert, const SElementArduinoCloudSlot certSlot, const SElementArduinoCloudSlot keySlot = SElementArduinoCloudSlot::Key);
31+
static int rebuild(SecureElement & se, ECP256Certificate & cert, const String & deviceId,
32+
const String & notBefore, const String & notAfter, const String & serialNumber,
33+
const String & authorityKeyIdentifier, const String & signature,
34+
const SElementArduinoCloudSlot keySlot = SElementArduinoCloudSlot::Key);
3135

3236
};
3337

0 commit comments

Comments
 (0)