1+ /*
2+ ArduinoSecureElement - 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 or SE050 crypto chip slot.
6+
7+ If the crypto chip 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 < utility/SElementCertificate.h>
22+
23+ void setup () {
24+ Serial.begin (9600 );
25+ while (!Serial);
26+
27+ SecureElement secureElement;
28+
29+ if (!secureElement.begin ()) {
30+ Serial.println (" No SecureElement present!" );
31+ while (1 );
32+ }
33+
34+ String serialNumber = secureElement.serialNumber ();
35+
36+ Serial.print (" SecureElement Serial Number = " );
37+ Serial.println (serialNumber);
38+ Serial.println ();
39+
40+ if (!secureElement.locked ()) {
41+ String lock = promptAndReadLine (" The SecureElement on your board is not locked, would you like to PERMANENTLY configure and lock it now? (y/N)" , " N" );
42+ lock.toLowerCase ();
43+
44+ if (!lock.startsWith (" y" )) {
45+ Serial.println (" Unfortunately you can't proceed without locking it :(" );
46+ while (1 );
47+ }
48+
49+ if (!secureElement.writeConfiguration ()) {
50+ Serial.println (" Writing SecureElement configuration failed!" );
51+ while (1 );
52+ }
53+
54+ if (!secureElement.lock ()) {
55+ Serial.println (" Locking SecureElement configuration failed!" );
56+ while (1 );
57+ }
58+
59+ Serial.println (" SecureElement locked successfully" );
60+ Serial.println ();
61+ }
62+
63+ Serial.println (" Hi there, in order to generate a new self signed cert for your board, we'll need the following information ..." );
64+ Serial.println ();
65+
66+ String issueYear = promptAndReadLine (" Issue year of the certificate? (2000 - 2031)" , " 2019" );
67+ String issueMonth = promptAndReadLine (" Issue month of the certificate? (1 - 12)" , " 1" );
68+ String issueDay = promptAndReadLine (" Issue day of the certificate? (1 - 31)" , " 1" );
69+ String issueHour = promptAndReadLine (" Issue hour of the certificate? (0 - 23)" , " 0" );
70+ String expireYears = promptAndReadLine (" How many years the certificate is valid for? (1 - 31)" , " 31" );
71+ String privateKeySlot = promptAndReadLine (" What slot would you like to use for the private key? (0 - 4)" , " 0" );
72+ String generateNewKey = promptAndReadLine (" Would you like to generate a new private key? (Y/n)" , " Y" );
73+
74+ Serial.println ();
75+
76+ generateNewKey.toLowerCase ();
77+
78+ ECP256Certificate Certificate;
79+
80+ Certificate.begin ();
81+ Certificate.setIssuerCommonName (secureElement.serialNumber ());
82+ Certificate.setSubjectCommonName (secureElement.serialNumber ());
83+ Certificate.setIssueYear (issueYear.toInt ());
84+ Certificate.setIssueMonth (issueMonth.toInt ());
85+ Certificate.setIssueDay (issueDay.toInt ());
86+ Certificate.setIssueHour (issueHour.toInt ());
87+ Certificate.setExpireYears (expireYears.toInt ());
88+
89+ if (!SElementCertificate::build (secureElement, Certificate, privateKeySlot.toInt (), generateNewKey.startsWith (" y" ), true /* self signed certificate */ )) {
90+ Serial.println (" Error starting certificate generation!" );
91+ while (1 );
92+ }
93+
94+ String cert = Certificate.getCertPEM ();
95+
96+ if (!cert) {
97+ Serial.println (" Error generating self signed certificate!" );
98+ while (1 );
99+ }
100+
101+ Serial.println (" Here's your self signed cert, enjoy!" );
102+ Serial.println ();
103+ Serial.println (cert);
104+ Serial.println ();
105+
106+ }
107+
108+ void loop () {
109+ // do nothing
110+ }
111+
112+ String promptAndReadLine (const char * prompt, const char * defaultValue) {
113+ Serial.print (prompt);
114+ Serial.print (" [" );
115+ Serial.print (defaultValue);
116+ Serial.print (" ]: " );
117+
118+ String s = readLine ();
119+
120+ if (s.length () == 0 ) {
121+ s = defaultValue;
122+ }
123+
124+ Serial.println (s);
125+
126+ return s;
127+ }
128+
129+ String readLine () {
130+ String line;
131+
132+ while (1 ) {
133+ if (Serial.available ()) {
134+ char c = Serial.read ();
135+
136+ if (c == ' \r ' ) {
137+ // ignore
138+ continue ;
139+ } else if (c == ' \n ' ) {
140+ break ;
141+ }
142+
143+ line += c;
144+ }
145+ }
146+
147+ return line;
148+ }
0 commit comments