Skip to content

Commit e97b845

Browse files
committed
Add CSR generation capabilities and default TLS config
1 parent f55e0a6 commit e97b845

File tree

4 files changed

+864
-0
lines changed

4 files changed

+864
-0
lines changed
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
/*
2+
ArduinoECCX08 - CSR (Certificate Signing Request)
3+
4+
This sketch can be used to generate a CSR for a private key
5+
generated in an ECC508/ECC608 crypto chip slot.
6+
7+
If the ECC508/ECC608 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 is prompted for the following information that is contained
12+
in the generated CSR:
13+
- country
14+
- state or province
15+
- locality
16+
- organization
17+
- organizational unit
18+
- common name
19+
20+
The user can also select a slot number to use for the private key
21+
A new private key can also be generated in this slot.
22+
23+
The circuit:
24+
- Arduino MKR board equipped with ECC508 or ECC608 chip
25+
26+
This example code is in the public domain.
27+
*/
28+
29+
#include <ArduinoECCX08.h>
30+
#include <utility/ECCX08CSR.h>
31+
#include <utility/ECCX08DefaultTLSConfig.h>
32+
33+
void setup() {
34+
Serial.begin(9600);
35+
while (!Serial);
36+
37+
if (!ECCX08.begin()) {
38+
Serial.println("No ECCX08 present!");
39+
while (1);
40+
}
41+
42+
String serialNumber = ECCX08.serialNumber();
43+
44+
Serial.print("ECCX08 Serial Number = ");
45+
Serial.println(serialNumber);
46+
Serial.println();
47+
48+
if (!ECCX08.locked()) {
49+
String lock = promptAndReadLine("The ECCX08 on your board is not locked, would you like to configure and lock it now? (y/N)", "N");
50+
51+
if (!lock.startsWith("y")) {
52+
Serial.println("Unfortunately you can't proceed without locking it :(");
53+
while (1);
54+
}
55+
56+
if (!ECCX08.writeConfiguration(ECCX08_DEFAULT_TLS_CONFIG)) {
57+
Serial.println("Writing ECCX08 configuration failed!");
58+
while (1);
59+
}
60+
61+
if (!ECCX08.lock()) {
62+
Serial.println("Locking ECCX08 configuration failed!");
63+
while (1);
64+
}
65+
66+
Serial.println("ECCX08 locked successfully");
67+
Serial.println();
68+
}
69+
70+
Serial.println("Hi there, in order to generate a new CSR for your board, we'll need the following information ...");
71+
Serial.println();
72+
73+
String country = promptAndReadLine("Country Name (2 letter code)", "");
74+
String stateOrProvince = promptAndReadLine("State or Province Name (full name)", "");
75+
String locality = promptAndReadLine("Locality Name (eg, city)", "");
76+
String organization = promptAndReadLine("Organization Name (eg, company)", "");
77+
String organizationalUnit = promptAndReadLine("Organizational Unit Name (eg, section)", "");
78+
String common = promptAndReadLine("Common Name (e.g. server FQDN or YOUR name)", serialNumber.c_str());
79+
String slot = promptAndReadLine("What slot would you like to use? (0 - 4)", "0");
80+
String generateNewKey = promptAndReadLine("Would you like to generate a new private key? (Y/n)", "Y");
81+
82+
Serial.println();
83+
84+
generateNewKey.toLowerCase();
85+
86+
if (!ECCX08CSR.begin(slot.toInt(), generateNewKey.startsWith("y"))) {
87+
Serial.println("Error starting CSR generation!");
88+
while (1);
89+
}
90+
91+
ECCX08CSR.setCountryName(country);
92+
ECCX08CSR.setStateProvinceName(stateOrProvince);
93+
ECCX08CSR.setLocalityName(locality);
94+
ECCX08CSR.setOrganizationName(organization);
95+
ECCX08CSR.setOrganizationalUnitName(organizationalUnit);
96+
ECCX08CSR.setCommonName(common);
97+
98+
String csr = ECCX08CSR.end();
99+
100+
if (!csr) {
101+
Serial.println("Error generating CSR!");
102+
while (1);
103+
}
104+
105+
Serial.println("Here's your CSR, enjoy!");
106+
Serial.println();
107+
Serial.println(csr);
108+
}
109+
110+
void loop() {
111+
// do nothing
112+
}
113+
114+
String promptAndReadLine(const char* prompt, const char* defaultValue) {
115+
Serial.print(prompt);
116+
Serial.print(" [");
117+
Serial.print(defaultValue);
118+
Serial.print("]: ");
119+
120+
String s = readLine();
121+
122+
if (s.length() == 0) {
123+
s = defaultValue;
124+
}
125+
126+
Serial.println(s);
127+
128+
return s;
129+
}
130+
131+
String readLine() {
132+
String line;
133+
134+
while (1) {
135+
if (Serial.available()) {
136+
char c = Serial.read();
137+
138+
if (c == '\r') {
139+
// ignore
140+
continue;
141+
} else if (c == '\n') {
142+
break;
143+
}
144+
145+
line += c;
146+
}
147+
}
148+
149+
return line;
150+
}

0 commit comments

Comments
 (0)