|
| 1 | +/* |
| 2 | + ArduinoSecureElement - JSON Web Token |
| 3 | +
|
| 4 | + This sketch can be used to generate a JSON Web Token from a private key |
| 5 | + stored in an ECC508/ECC608 or SE050 crypto chip slot. |
| 6 | +
|
| 7 | + If the SecureElement is not configured and locked it prompts |
| 8 | + the user to configure and lock the chip with a default TLS |
| 9 | + configuration. |
| 10 | +
|
| 11 | + The user can also select the slot number to use for the private key. |
| 12 | + A new private key can also be generated in this slot. |
| 13 | +
|
| 14 | + The circuit: |
| 15 | + - A board equipped with ECC508 or ECC608 or SE050 chip |
| 16 | +
|
| 17 | + This example code is in the public domain. |
| 18 | +*/ |
| 19 | + |
| 20 | +#include <Arduino_SecureElement.h> |
| 21 | +#include <Arduino_JSON.h> |
| 22 | +#include <utility/SElementJWS.h> |
| 23 | + |
| 24 | +void setup() { |
| 25 | + Serial.begin(9600); |
| 26 | + while (!Serial); |
| 27 | + |
| 28 | + SecureElement secureElement; |
| 29 | + |
| 30 | + if (!secureElement.begin()) { |
| 31 | + Serial.println("No SecureElement present!"); |
| 32 | + while (1); |
| 33 | + } |
| 34 | + |
| 35 | + String serialNumber = secureElement.serialNumber(); |
| 36 | + |
| 37 | + Serial.print("SecureElement Serial Number = "); |
| 38 | + Serial.println(serialNumber); |
| 39 | + Serial.println(); |
| 40 | + |
| 41 | + if (!secureElement.locked()) { |
| 42 | + String lock = promptAndReadLine("The SecureElement on your board is not locked, would you like to PERMANENTLY configure and lock it now? (y/N)", "N"); |
| 43 | + lock.toLowerCase(); |
| 44 | + |
| 45 | + if (!lock.startsWith("y")) { |
| 46 | + Serial.println("Unfortunately you can't proceed without locking it :("); |
| 47 | + while (1); |
| 48 | + } |
| 49 | + |
| 50 | + if (!secureElement.writeConfiguration()) { |
| 51 | + Serial.println("Writing SecureElement configuration failed!"); |
| 52 | + while (1); |
| 53 | + } |
| 54 | + |
| 55 | + if (!secureElement.lock()) { |
| 56 | + Serial.println("Locking SecureElement configuration failed!"); |
| 57 | + while (1); |
| 58 | + } |
| 59 | + |
| 60 | + Serial.println("SecureElement locked successfully"); |
| 61 | + Serial.println(); |
| 62 | + } |
| 63 | + |
| 64 | + Serial.println("Hi there, in order to generate a PEM public key for your board, we'll need the following information ..."); |
| 65 | + Serial.println(); |
| 66 | + |
| 67 | + String slot = promptAndReadLine("What slot would you like to use? (0 - 4)", "0"); |
| 68 | + String generateNewKey = promptAndReadLine("Would you like to generate a new private key? (Y/n)", "Y"); |
| 69 | + String issuer = promptAndReadLine("Issuer (Device UID)", ""); |
| 70 | + String iat = promptAndReadLine("Issued at (Unix Timestamp)", ""); |
| 71 | + String exp = promptAndReadLine("Expires at (Unix Timestamp)", ""); |
| 72 | + |
| 73 | + Serial.println(); |
| 74 | + |
| 75 | + generateNewKey.toLowerCase(); |
| 76 | + |
| 77 | + SElementJWS jws; |
| 78 | + |
| 79 | + String publicKeyPem = jws.publicKey(secureElement, slot.toInt(), generateNewKey.startsWith("y")); |
| 80 | + |
| 81 | + if (!publicKeyPem || publicKeyPem == "") { |
| 82 | + Serial.println("Error generating public key!"); |
| 83 | + while (1); |
| 84 | + } |
| 85 | + |
| 86 | + Serial.println("Here's your public key PEM, enjoy!"); |
| 87 | + Serial.println(); |
| 88 | + Serial.println(publicKeyPem); |
| 89 | + |
| 90 | + JSONVar jwtHeader; |
| 91 | + JSONVar jwtClaim; |
| 92 | + |
| 93 | + jwtHeader["alg"] = "ES256"; |
| 94 | + jwtHeader["typ"] = "JWT"; |
| 95 | + |
| 96 | + jwtClaim["iss"] = issuer; |
| 97 | + jwtClaim["iat"] = iat.toInt(); |
| 98 | + jwtClaim["exp"] = exp.toInt(); |
| 99 | + |
| 100 | + String token = jws.sign(secureElement, slot.toInt(), JSON.stringify(jwtHeader), JSON.stringify(jwtClaim)); |
| 101 | + |
| 102 | + Serial.println("Here's your JSON Web Token, enjoy!"); |
| 103 | + Serial.println(); |
| 104 | + Serial.println(token); |
| 105 | +} |
| 106 | + |
| 107 | +void loop() { |
| 108 | + // do nothing |
| 109 | +} |
| 110 | + |
| 111 | +String promptAndReadLine(const char* prompt, const char* defaultValue) { |
| 112 | + Serial.print(prompt); |
| 113 | + Serial.print(" ["); |
| 114 | + Serial.print(defaultValue); |
| 115 | + Serial.print("]: "); |
| 116 | + |
| 117 | + String s = readLine(); |
| 118 | + |
| 119 | + if (s.length() == 0) { |
| 120 | + s = defaultValue; |
| 121 | + } |
| 122 | + |
| 123 | + Serial.println(s); |
| 124 | + |
| 125 | + return s; |
| 126 | +} |
| 127 | + |
| 128 | +String readLine() { |
| 129 | + String line; |
| 130 | + |
| 131 | + while (1) { |
| 132 | + if (Serial.available()) { |
| 133 | + char c = Serial.read(); |
| 134 | + |
| 135 | + if (c == '\r') { |
| 136 | + // ignore |
| 137 | + continue; |
| 138 | + } else if (c == '\n') { |
| 139 | + break; |
| 140 | + } |
| 141 | + |
| 142 | + line += c; |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + return line; |
| 147 | +} |
0 commit comments