|
14 | 14 |
|
15 | 15 | #include <utility/SElementArduinoCloudCertificate.h>
|
16 | 16 |
|
| 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 | + |
17 | 41 | int SElementArduinoCloudCertificate::write(SecureElement & se, ECP256Certificate & cert, const SElementArduinoCloudSlot certSlot)
|
18 | 42 | {
|
19 | 43 | #if defined(SECURE_ELEMENT_IS_SE050) || defined(SECURE_ELEMENT_IS_SOFTSE)
|
@@ -92,3 +116,45 @@ int SElementArduinoCloudCertificate::read(SecureElement & se, ECP256Certificate
|
92 | 116 | #endif
|
93 | 117 | return 1;
|
94 | 118 | }
|
| 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 | +} |
0 commit comments