|
19 | 19 | * INCLUDE
|
20 | 20 | ******************************************************************************/
|
21 | 21 |
|
| 22 | +#include <AIoTC_Config.h> |
| 23 | + |
| 24 | +#if defined(BOARD_HAS_ECCX08) || defined(BOARD_HAS_OFFLOADED_ECCX08) |
| 25 | + |
22 | 26 | #include "CryptoUtil.h"
|
| 27 | +#include "SHA256.h" |
| 28 | + |
| 29 | +/****************************************************************************** |
| 30 | + * DEFINE |
| 31 | + ******************************************************************************/ |
| 32 | +#define CRYPTO_SHA256_BUFFER_LENGTH 32 |
| 33 | +#define CRYPTO_CERT_BUFFER_LENGTH 1024 |
| 34 | + |
| 35 | +/************************************************************************************** |
| 36 | + * CTOR/DTOR |
| 37 | + **************************************************************************************/ |
| 38 | +CryptoUtil::CryptoUtil() |
| 39 | +: _crypto {ECCX08} |
| 40 | +{ |
23 | 41 |
|
24 |
| -#if defined(BOARD_HAS_ECCX08) || defined (BOARD_HAS_OFFLOADED_ECCX08) |
| 42 | +} |
25 | 43 |
|
26 | 44 | /******************************************************************************
|
27 | 45 | * PUBLIC MEMBER FUNCTIONS
|
28 | 46 | ******************************************************************************/
|
29 | 47 |
|
30 |
| -bool CryptoUtil::readDeviceId(ECCX08Class & eccx08, String & device_id, ECCX08Slot const device_id_slot) |
| 48 | +int CryptoUtil::buildCSR(ArduinoIoTCloudCertClass & cert, const CryptoSlot keySlot, bool newPrivateKey) |
| 49 | +{ |
| 50 | + byte publicKey[CERT_PUBLIC_KEY_LENGTH]; |
| 51 | + byte signature[CERT_SIGNATURE_LENGTH]; |
| 52 | + |
| 53 | + if (newPrivateKey) { |
| 54 | + if (!_crypto.generatePrivateKey(static_cast<int>(keySlot), publicKey)) { |
| 55 | + return 0; |
| 56 | + } |
| 57 | + } else { |
| 58 | + if (!_crypto.generatePublicKey(static_cast<int>(keySlot), publicKey)) { |
| 59 | + return 0; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /* Store public key in csr */ |
| 64 | + if (!cert.setPublicKey(publicKey, CERT_PUBLIC_KEY_LENGTH)) { |
| 65 | + return 0; |
| 66 | + } |
| 67 | + |
| 68 | + /* Build CSR */ |
| 69 | + if (!cert.buildCSR()) { |
| 70 | + return 0; |
| 71 | + } |
| 72 | + |
| 73 | + /* compute CSR SHA256 */ |
| 74 | + SHA256 sha256; |
| 75 | + byte sha256buf[CRYPTO_SHA256_BUFFER_LENGTH]; |
| 76 | + sha256.begin(); |
| 77 | + sha256.update(cert.bytes(), cert.length()); |
| 78 | + sha256.finalize(sha256buf); |
| 79 | + |
| 80 | + if (!_crypto.ecSign(static_cast<int>(keySlot), sha256buf, signature)) { |
| 81 | + return 0; |
| 82 | + } |
| 83 | + |
| 84 | + /* sign CSR */ |
| 85 | + return cert.signCSR(signature); |
| 86 | +} |
| 87 | + |
| 88 | +int CryptoUtil::buildCert(ArduinoIoTCloudCertClass & cert, const CryptoSlot keySlot) |
| 89 | +{ |
| 90 | + byte publicKey[CERT_PUBLIC_KEY_LENGTH]; |
| 91 | + |
| 92 | + if (!_crypto.generatePublicKey(static_cast<int>(keySlot), publicKey)) { |
| 93 | + return 0; |
| 94 | + } |
| 95 | + |
| 96 | + /* Store public key in csr */ |
| 97 | + if (!cert.setPublicKey(publicKey, CERT_PUBLIC_KEY_LENGTH)) { |
| 98 | + return 0; |
| 99 | + } |
| 100 | + |
| 101 | + /* Build CSR */ |
| 102 | + if (!cert.buildCert()) { |
| 103 | + return 0; |
| 104 | + } |
| 105 | + |
| 106 | + /* sign CSR */ |
| 107 | + return cert.signCert(); |
| 108 | +} |
| 109 | + |
| 110 | +int CryptoUtil::readDeviceId(String & device_id, const CryptoSlot device_id_slot) |
| 111 | +{ |
| 112 | + byte device_id_bytes[CERT_COMPRESSED_CERT_SLOT_LENGTH] = {0}; |
| 113 | + |
| 114 | + if (!_crypto.readSlot(static_cast<int>(device_id_slot), device_id_bytes, sizeof(device_id_bytes))) { |
| 115 | + return 0; |
| 116 | + } |
| 117 | + |
| 118 | + device_id = String(reinterpret_cast<char *>(device_id_bytes)); |
| 119 | + return 1; |
| 120 | +} |
| 121 | + |
| 122 | +int CryptoUtil::writeDeviceId(String & device_id, const CryptoSlot device_id_slot) |
31 | 123 | {
|
32 |
| - byte device_id_bytes[72] = {0}; |
| 124 | + byte device_id_bytes[CERT_COMPRESSED_CERT_SLOT_LENGTH] = {0}; |
| 125 | + |
| 126 | + device_id.getBytes(device_id_bytes, sizeof(device_id_bytes)); |
33 | 127 |
|
34 |
| - if (eccx08.readSlot(static_cast<int>(device_id_slot), device_id_bytes, sizeof(device_id_bytes))) { |
35 |
| - device_id = String(reinterpret_cast<char *>(device_id_bytes)); |
36 |
| - return true; |
| 128 | + if (!_crypto.writeSlot(static_cast<int>(device_id_slot), device_id_bytes, sizeof(device_id_bytes))) { |
| 129 | + return 0; |
37 | 130 | }
|
38 |
| - else |
39 |
| - { |
40 |
| - return false; |
| 131 | + return 1; |
| 132 | +} |
| 133 | + |
| 134 | +int CryptoUtil::writeCert(ArduinoIoTCloudCertClass & cert, const CryptoSlot certSlot) |
| 135 | +{ |
| 136 | + if (!_crypto.writeSlot(static_cast<int>(certSlot), cert.compressedCertSignatureAndDatesBytes(), cert.compressedCertSignatureAndDatesLength())) { |
| 137 | + return 0; |
41 | 138 | }
|
| 139 | + |
| 140 | + if (!_crypto.writeSlot(static_cast<int>(certSlot) + 1, cert.compressedCertSerialAndAuthorityKeyIdBytes(), cert.compressedCertSerialAndAuthorityKeyIdLenght())) { |
| 141 | + return 0; |
| 142 | + } |
| 143 | + return 1; |
42 | 144 | }
|
43 | 145 |
|
44 |
| -bool CryptoUtil::reconstructCertificate(ECCX08CertClass & cert, String const & device_id, ECCX08Slot const key, ECCX08Slot const compressed_certificate, ECCX08Slot const serial_number_and_authority_key) |
| 146 | +int CryptoUtil::readCert(ArduinoIoTCloudCertClass & cert, const CryptoSlot certSlot) |
45 | 147 | {
|
46 |
| - if (cert.beginReconstruction(static_cast<int>(key), static_cast<int>(compressed_certificate), static_cast<int>(serial_number_and_authority_key))) |
47 |
| - { |
48 |
| - cert.setSubjectCommonName(device_id); |
49 |
| - cert.setIssuerCountryName("US"); |
50 |
| - cert.setIssuerOrganizationName("Arduino LLC US"); |
51 |
| - cert.setIssuerOrganizationalUnitName("IT"); |
52 |
| - cert.setIssuerCommonName("Arduino"); |
53 |
| - return cert.endReconstruction(); |
54 |
| - } |
55 |
| - else |
56 |
| - { |
57 |
| - return false; |
| 148 | + String deviceId; |
| 149 | + byte publicKey[CERT_PUBLIC_KEY_LENGTH]; |
| 150 | + |
| 151 | + cert.begin(); |
| 152 | + |
| 153 | + if (!readDeviceId(deviceId, CryptoSlot::DeviceId)) { |
| 154 | + return 0; |
| 155 | + } |
| 156 | + |
| 157 | + if (!_crypto.readSlot(static_cast<int>(certSlot), cert.compressedCertSignatureAndDatesBytes(), cert.compressedCertSignatureAndDatesLength())) { |
| 158 | + return 0; |
| 159 | + } |
| 160 | + |
| 161 | + if (!_crypto.readSlot(static_cast<int>(certSlot) + 1, cert.compressedCertSerialAndAuthorityKeyIdBytes(), cert.compressedCertSerialAndAuthorityKeyIdLenght())) { |
| 162 | + return 0; |
| 163 | + } |
| 164 | + |
| 165 | + if (!_crypto.generatePublicKey(static_cast<int>(CryptoSlot::Key), publicKey)) { |
| 166 | + return 0; |
| 167 | + } |
| 168 | + |
| 169 | + cert.setSubjectCommonName(deviceId); |
| 170 | + cert.setIssuerCountryName("US"); |
| 171 | + cert.setIssuerOrganizationName("Arduino LLC US"); |
| 172 | + cert.setIssuerOrganizationalUnitName("IT"); |
| 173 | + cert.setIssuerCommonName("Arduino"); |
| 174 | + |
| 175 | + if (!cert.setPublicKey(publicKey, CERT_PUBLIC_KEY_LENGTH)) { |
| 176 | + return 0; |
| 177 | + } |
| 178 | + |
| 179 | + if (!cert.buildCert()) { |
| 180 | + return 0; |
| 181 | + } |
| 182 | + |
| 183 | + if (!cert.signCert()) { |
| 184 | + return 0; |
58 | 185 | }
|
| 186 | + return 1; |
59 | 187 | }
|
60 | 188 |
|
61 |
| -#endif /* BOARD_HAS_ECCX08 */ |
| 189 | +#endif /* (BOARD_HAS_ECCX08) || defined(BOARD_HAS_OFFLOADED_ECCX08) */ |
0 commit comments