|
| 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 | +} |
0 commit comments