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