|
| 1 | +/* |
| 2 | + ArduinoECCX08 - Self Signed Cert |
| 3 | +
|
| 4 | + This sketch can be used to generate a self signed certificate |
| 5 | + for a private key generated in an ECC508/ECC608 crypto chip slot. |
| 6 | + The issue and expired date, and signature are stored in another |
| 7 | + slot for reconstrution. |
| 8 | +
|
| 9 | + If the ECC508/ECC608 is not configured and locked it prompts |
| 10 | + the user to configure and lock the chip with a default TLS |
| 11 | + configuration. |
| 12 | +
|
| 13 | + The user can also select the slot number to use for the private key |
| 14 | + and storage. |
| 15 | + A new private key can also be generated in this slot. |
| 16 | +
|
| 17 | + The circuit: |
| 18 | + - Arduino MKR board equipped with ECC508 or ECC608 chip |
| 19 | +
|
| 20 | + This example code is in the public domain. |
| 21 | +*/ |
| 22 | + |
| 23 | +#include <ArduinoECCX08.h> |
| 24 | +#include <utility/ECCX08SelfSignedCert.h> |
| 25 | +#include <utility/ECCX08DefaultTLSConfig.h> |
| 26 | + |
| 27 | +void setup() { |
| 28 | + Serial.begin(9600); |
| 29 | + while (!Serial); |
| 30 | + |
| 31 | + if (!ECCX08.begin()) { |
| 32 | + Serial.println("No ECCX08 present!"); |
| 33 | + while (1); |
| 34 | + } |
| 35 | + |
| 36 | + String serialNumber = ECCX08.serialNumber(); |
| 37 | + |
| 38 | + Serial.print("ECCX08 Serial Number = "); |
| 39 | + Serial.println(serialNumber); |
| 40 | + Serial.println(); |
| 41 | + |
| 42 | + if (!ECCX08.locked()) { |
| 43 | + String lock = promptAndReadLine("The ECCX08 on your board is not locked, would you like to PERMANENTLY configure and lock it now? (y/N)", "N"); |
| 44 | + lock.toLowerCase(); |
| 45 | + |
| 46 | + if (!lock.startsWith("y")) { |
| 47 | + Serial.println("Unfortunately you can't proceed without locking it :("); |
| 48 | + while (1); |
| 49 | + } |
| 50 | + |
| 51 | + if (!ECCX08.writeConfiguration(ECCX08_DEFAULT_TLS_CONFIG)) { |
| 52 | + Serial.println("Writing ECCX08 configuration failed!"); |
| 53 | + while (1); |
| 54 | + } |
| 55 | + |
| 56 | + if (!ECCX08.lock()) { |
| 57 | + Serial.println("Locking ECCX08 configuration failed!"); |
| 58 | + while (1); |
| 59 | + } |
| 60 | + |
| 61 | + Serial.println("ECCX08 locked successfully"); |
| 62 | + Serial.println(); |
| 63 | + } |
| 64 | + |
| 65 | + Serial.println("Hi there, in order to generate a new self signed cert for your board, we'll need the following information ..."); |
| 66 | + Serial.println(); |
| 67 | + |
| 68 | + String issueYear = promptAndReadLine("Please enter the issue year of the certificate? (2000 - 2031)", "2019"); |
| 69 | + String issueMonth = promptAndReadLine("Please enter the issue month of the certificate? (1 - 12)", "1"); |
| 70 | + String issueDay = promptAndReadLine("Please enter the issue day of the certificate? (1 - 31)", "1"); |
| 71 | + String issueHour = promptAndReadLine("Please enter the issue hour of the certificate? (0 - 23)", "0"); |
| 72 | + String expireYears = promptAndReadLine("Please enter how many years the certificate is valid for? (1 - 31)", "31"); |
| 73 | + String privateKeySlot = promptAndReadLine("What slot would you like to use for the private key? (0 - 4)", "0"); |
| 74 | + String storageSlot = promptAndReadLine("What slot would you like to use for storage? (8 - 15)", "8"); |
| 75 | + String generateNewKey = promptAndReadLine("Would you like to generate a new private key? (Y/n)", "Y"); |
| 76 | + |
| 77 | + Serial.println(); |
| 78 | + |
| 79 | + generateNewKey.toLowerCase(); |
| 80 | + |
| 81 | + if (!ECCX08SelfSignedCert.beginStorage(privateKeySlot.toInt(), storageSlot.toInt(), generateNewKey.startsWith("y"))) { |
| 82 | + Serial.println("Error starting self signed cert generation!"); |
| 83 | + while (1); |
| 84 | + } |
| 85 | + |
| 86 | + ECCX08SelfSignedCert.setCommonName(ECCX08.serialNumber()); |
| 87 | + ECCX08SelfSignedCert.setIssueYear(issueYear.toInt()); |
| 88 | + ECCX08SelfSignedCert.setIssueMonth(issueMonth.toInt()); |
| 89 | + ECCX08SelfSignedCert.setIssueDay(issueDay.toInt()); |
| 90 | + ECCX08SelfSignedCert.setIssueHour(issueHour.toInt()); |
| 91 | + ECCX08SelfSignedCert.setExpireYears(expireYears.toInt()); |
| 92 | + |
| 93 | + String cert = ECCX08SelfSignedCert.endStorage(); |
| 94 | + |
| 95 | + if (!cert) { |
| 96 | + Serial.println("Error generating self signed cert!"); |
| 97 | + while (1); |
| 98 | + } |
| 99 | + |
| 100 | + Serial.println("Here's your self signed cert, enjoy!"); |
| 101 | + Serial.println(); |
| 102 | + Serial.println(cert); |
| 103 | +} |
| 104 | + |
| 105 | +void loop() { |
| 106 | + // do nothing |
| 107 | +} |
| 108 | + |
| 109 | +String promptAndReadLine(const char* prompt, const char* defaultValue) { |
| 110 | + Serial.print(prompt); |
| 111 | + Serial.print(" ["); |
| 112 | + Serial.print(defaultValue); |
| 113 | + Serial.print("]: "); |
| 114 | + |
| 115 | + String s = readLine(); |
| 116 | + |
| 117 | + if (s.length() == 0) { |
| 118 | + s = defaultValue; |
| 119 | + } |
| 120 | + |
| 121 | + Serial.println(s); |
| 122 | + |
| 123 | + return s; |
| 124 | +} |
| 125 | + |
| 126 | +String readLine() { |
| 127 | + String line; |
| 128 | + |
| 129 | + while (1) { |
| 130 | + if (Serial.available()) { |
| 131 | + char c = Serial.read(); |
| 132 | + |
| 133 | + if (c == '\r') { |
| 134 | + // ignore |
| 135 | + continue; |
| 136 | + } else if (c == '\n') { |
| 137 | + break; |
| 138 | + } |
| 139 | + |
| 140 | + line += c; |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + return line; |
| 145 | +} |
0 commit comments