forked from dirkx/makerspaceleiden-payment-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrest.cpp
527 lines (425 loc) · 14.1 KB
/
rest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
#include <WiFi.h>
#include <HTTPClient.h>
#include <WiFiClientSecure.h>
#include <WiFiUdp.h>
#include <Arduino_JSON.h>
#include <Preferences.h>
#include <nvs_flash.h>
#include "mbedtls/md.h"
#include "mbedtls/base64.h"
#include "mbedtls/sha256.h"
#include "mbedtls/x509.h"
#include "mbedtls/x509_crt.h"
#include "mbedtls/x509_csr.h"
#include "mbedtls/entropy.h"
#include "mbedtls/ctr_drbg.h"
#include "mbedtls/error.h"
#include "global.h"
#include "log.h"
#include "display.h"
#include "rest.h"
#include "geneckey.h"
#include "selfsign.h"
mbedtls_x509_crt * client_cert_ptr = NULL, client_cert;
char * ca_root = NULL;
char * nonce = NULL;
char * server_cert_as_pem = NULL;
char * client_cert_as_pem = NULL;
char * client_key_as_pem = NULL;
unsigned char sha256_client[32], sha256_server[32], sha256_server_key[32];
const char * KS_NAME = "keystore";
const char * KS_KEY_VERSION = "version";
const char * KS_KEY_CLIENT_CRT = "ccap";
const char * KS_KEY_CLIENT_KEY = "ckap";
const char * KS_KEY_SERVER_KEY = "ssk";
const char * stationname = "unset";
const unsigned short KS_VERSION = 0x100;
#define NONE_PATH "/none"
#define REGISTER_PATH "/v2/register"
#define PAY_PATH "/v2/pay"
static unsigned char hex_digit(unsigned char c) {
return "0123456789ABCDEF"[c & 0x0F];
};
char *_argencode(char *dst, size_t n, char *src)
{
char c, *d = dst;
while ((c = *src++) != 0)
{
if (c == ' ') {
*d++ = '+';
} else if (strchr("!*'();:@&=+$,/?%#[] ", c) || c < 32 || c > 127 ) {
*d++ = '%';
*d++ = hex_digit(c >> 4);
*d++ = hex_digit(c);
} else {
*d++ = c;
};
if (d + 1 >= dst + n) {
Log.println("Warning - buffer was too small. Truncating.");
break;
}
};
*d++ = '\0';
return dst;
}
bool getks(Preferences keystore, const char * key, char ** dst) {
size_t len = keystore.getBytesLength(key);
if (len == 0)
return false;
if (*dst == 0)
*dst = (char *)malloc(len + 1); // assume/know they are strings that need an extra terminating 0
keystore.getBytes(key, *dst, len);
(*dst)[len] = 0;
return true;
}
bool setupAuth(const char * terminalName) {
mbedtls_x509write_cert crt;
mbedtls_pk_context key;
Preferences keystore;
char tmp[65];
bool paired = false;
if (!keystore.begin(KS_NAME, false))
Log.println("Keystore open failed");
unsigned short version = keystore.getUShort(KS_KEY_VERSION, 0);
if (version != KS_VERSION ||
!getks(keystore, KS_KEY_CLIENT_CRT, &client_cert_as_pem) ||
!getks(keystore, KS_KEY_CLIENT_KEY, &client_key_as_pem) ||
!keystore.getBytes(KS_KEY_SERVER_KEY, sha256_server_key, 32))
{
Log.println("Incomplete/absent keystore");
keystore.end();
wipekeys();
if (!keystore.begin(KS_NAME, false))
Log.println("Keystore open failed");
keystore.putUShort(KS_KEY_VERSION, KS_VERSION);
geneckey(&key);
if (0 != populate_self_signed(&key, terminalName, &crt)) {
Log.println("Generation error. Aborting");
keystore.end();
return false;
}
if (0 != sign_and_topem(&key, &crt, &client_cert_as_pem, &client_key_as_pem)) {
Log.println("Derring error. Aborting");
keystore.end();
return false;
};
} else {
Log.printf("Using existing keys (keystore version 0x%03x), fully configured\n", version);
paired = true;
}
keystore.end();
fingerprint_from_pem(client_cert_as_pem, sha256_client);
Log.print("Fingerprint (as shown in CRM): ");
Log.println(sha256toHEX(sha256_client, tmp));
return paired;
}
void wipekeys() {
Log.println("Wiping keystore");
nvs_flash_erase(); // erase the NVS partition and...
nvs_flash_init(); // initialize the NVS partition.
}
bool fetchCA() {
WiFiClientSecure client;
HTTPClient https;
const mbedtls_x509_crt *peer ;
unsigned char sha256[256 / 8];
int httpCode;
bool ok = false;
JSONVar res;
updateDisplay_progressText("fetching CA");
// Sadly required - due to a limitation in the current SSL stack we must
// provide the root CA. but we do not know it (yet). So learn it first.
//
client.setInsecure();
if (!https.begin(client, PAY_URL NONE_PATH )) {
Log.println("Failed to begin https - fetchCA");
goto exit;
};
https.setTimeout(HTTP_TIMEOUT);
https.setUserAgent(terminalName);
if (https.GET() < 0) {
Log.println("Failed to begin https (GET, fetchCA)");
goto exit;
};
peer = client.getPeerCertificate();
mbedtls_sha256_ret(peer->raw.p, peer->raw.len, sha256_server, 0);
server_cert_as_pem = der2pem("CERTIFICATE", peer->raw.p, peer->raw.len);
// Traverse up to (any) root & serialize the CAcert. We need it in
// PEM format; as that is what setCACert() expects.
//
while (peer->next) peer = peer->next;
ca_root = der2pem("CERTIFICATE", peer->raw.p, peer->raw.len);
updateDisplay_progressText("trust fetched");
ok = true;
exit:
https.end();
client.stop();
if (!ok)
delay(5000);
return ok;
}
bool registerDevice() {
WiFiClientSecure client;
const mbedtls_x509_crt *peer ;
HTTPClient https;
int httpCode;
unsigned char tmp[128], buff[1024], sha256[256 / 8];
bool ok = false;
JSONVar res;
client.setCACert(ca_root);
client.setCertificate(client_cert_as_pem);
client.setPrivateKey(client_key_as_pem);
snprintf((char *) buff, sizeof(buff), PAY_URL REGISTER_PATH "?name=%s",
_argencode((char *) tmp, sizeof(tmp), terminalName));
if (!https.begin(client, (const char*)buff)) {
Log.println("Failed to begin https");
goto exit;
};
https.setTimeout(HTTP_TIMEOUT);
https.setUserAgent(terminalName);
httpCode = https.GET();
if (httpCode != HTTP_CODE_UNAUTHORIZED) {
Log.printf("Not gotten the HTTP_CODE_UNAUTHORIZED I expected; but %d: %s\n", httpCode, https.getString().c_str());
goto exit;
};
peer = client.getPeerCertificate();
mbedtls_sha256_ret(peer->raw.p, peer->raw.len, sha256, 0);
if (memcmp(sha256, sha256_server, 32)) {
Log.println("Server changed mid registration. Aborting");
goto exit;
}
nonce = strdup((https.getString().c_str()));
Log.println("Got a NONCE - waiting for tag swipe");
ok = true;
exit:
https.end();
client.stop();
return ok;
};
bool registerDeviceWithSwipe(char *tag) {
WiFiClientSecure client;
const mbedtls_x509_crt *peer ;
HTTPClient https;
int httpCode;
unsigned char tmp[128], buff[1024], sha256[256 / 8];
bool ok = false;
JSONVar res;
client.setCACert(ca_root);
client.setCertificate(client_cert_as_pem);
client.setPrivateKey(client_key_as_pem);
updateDisplay_progressText("sending credentials");
// Create the reply; SHA256(nonce, tag(secret), client, server);
//
mbedtls_sha256_context sha_ctx;
mbedtls_sha256_init(&sha_ctx);
mbedtls_sha256_starts_ret(&sha_ctx, 0);
// we happen to know that the first two can safely be treated as strings.
//
mbedtls_sha256_update_ret(&sha_ctx, (unsigned char*) nonce, strlen(nonce));
mbedtls_sha256_update_ret(&sha_ctx, (unsigned char*) tag, strlen(tag));
mbedtls_sha256_update_ret(&sha_ctx, sha256_client, 32);
mbedtls_sha256_update_ret(&sha_ctx, sha256_server, 32);
mbedtls_sha256_finish_ret(&sha_ctx, sha256);
sha256toHEX(sha256, (char*)tmp);
mbedtls_sha256_free(&sha_ctx);
snprintf((char *) buff, sizeof(buff), PAY_URL REGISTER_PATH "?response=%s", (char *)tmp);
https.setTimeout(HTTP_TIMEOUT);
https.setUserAgent(terminalName);
if (!https.begin(client, (char *)buff )) {
Log.println("Failed to begin https");
goto exit;
};
httpCode = https.GET();
peer = client.getPeerCertificate();
mbedtls_sha256_ret(peer->raw.p, peer->raw.len, tmp, 0);
if (memcmp(tmp, sha256_server, 32)) {
Log.println("Server changed mid registration. Aborting");
goto exit;
}
if (httpCode != HTTP_CODE_OK) {
Log.println("Failed to register");
goto exit;
}
Log.println("Registration was accepted");
mbedtls_sha256_init(&sha_ctx);
mbedtls_sha256_starts_ret(&sha_ctx, 0);
mbedtls_sha256_update_ret(&sha_ctx, (unsigned char*) tag, strlen(tag));
mbedtls_sha256_update_ret(&sha_ctx, sha256, 32);
mbedtls_sha256_finish_ret(&sha_ctx, sha256);
sha256toHEX(sha256, (char*)tmp);
mbedtls_sha256_free(&sha_ctx);
if (!https.getString().equalsIgnoreCase((char*)tmp)) {
Log.println("Registered OK - but confirmation did not compute. Aborted.");
goto exit;
}
// Extract peer cert and calculate hash over the public key; as, especially
// with Let's Encrypt - the cert itself is regularly renewed.
//
if (fingerprint_from_certpubkey(peer, sha256_server_key)) {
Log.println("Extraction of public key of server failed. Aborted.");
goto exit;
};
sha256toHEX(sha256_server_key, (char*)tmp);
Log.print("Server public key SHA256: ");
Log.println((char*)tmp);
updateDisplay_progressText("OK");
{
Preferences keystore;
if (!keystore.begin(KS_NAME, false))
Log.println("Keystore open failed");
if (keystore.getUShort(KS_KEY_VERSION, 0) != KS_VERSION)
Log.println("**** NVS not working 2 *****");
keystore.putBytes(KS_KEY_CLIENT_CRT, client_cert_as_pem, strlen(client_cert_as_pem));
keystore.putBytes(KS_KEY_CLIENT_KEY, client_key_as_pem, strlen(client_key_as_pem));
keystore.putBytes(KS_KEY_SERVER_KEY, sha256_server_key, 32);
keystore.end();
}
{
Preferences keystore;
keystore.begin(KS_NAME, false);
if (keystore.getUShort(KS_KEY_VERSION, 0) != KS_VERSION)
Log.println("**** NVS not working 3 *****");
Log.printf("version: %x\n", keystore.getBytesLength(KS_KEY_VERSION));
Log.printf("version: %x\n", keystore.getUShort(KS_KEY_VERSION, -1));
Log.printf("client_cert_as_pem: %x\n", keystore.getBytesLength(KS_KEY_CLIENT_CRT));
Log.printf("client_key_as_pem: %x\n", keystore.getBytesLength(KS_KEY_CLIENT_KEY));
Log.printf("sha256_server_key: %x\n", keystore.getBytesLength(KS_KEY_SERVER_KEY));
keystore.end();
}
Log.println("\nWe are fully paired - we've proven to each other we know the secret & there is no MITM.");
ok = true;
exit:
https.end();
client.stop();
return ok;
};
JSONVar rest(const char *url, int * statusCode) {
WiFiClientSecure client;
unsigned char sha256[32];
static JSONVar res;
HTTPClient https;
String payload;
client.setCACert(ca_root);
client.setCertificate(client_cert_as_pem);
client.setPrivateKey(client_key_as_pem);
https.setTimeout(HTTP_TIMEOUT);
https.setUserAgent(terminalName);
if (!https.begin(client, url)) {
Log.println("setup fail");
return 999;
};
int httpCode = https.GET();
Debug.print("HTTP reponse: ");
Debug.println(httpCode);
if (httpCode < 0) {
Log.println("Rebooting, wifi issue" );
displayForceShowErrorModal("FAIL", "No wifi");
delay(1000);
ESP.restart();
}
if (fingerprint_from_certpubkey( client.getPeerCertificate(), sha256)) {
Log.println("Extraction of public key of server failed. Aborted.");
httpCode = 999;
goto exit;
};
if (0 != memcmp(sha256, sha256_server_key, 31)) {
Log.println("Server pubkey changed. Aborting");
httpCode = 666;
goto exit;
}
Debug.print("Payload: ");
payload = https.getString();
Debug.println(payload);
if (httpCode == HTTP_CODE_OK) {
res = JSON.parse(payload);
Debug.println("parsed as json fine");
} else {
Log.printf("REST failed: %d -- %s\n", httpCode, payload.c_str());
Debug.println(payload);
};
exit:
https.end();
client.stop();
*statusCode = httpCode;
return res;
}
int payByREST(char *tag, char * amount, char *lbl, char result[PBR_LEN]) {
char buff[512], desc[128], tmp[128];
memset(result,0,PBR_LEN);
snprintf(desc, sizeof(desc), "%s. Paid at %s", lbl, stationname);
if (0) {
// avoid logging the tag for privacy/security-by-obscurity reasons.
//
snprintf(buff, sizeof(buff), PAY_URL PAY_PATH "?node=%s&src=%s&amount=%s&description=%s",
terminalName, "XX-XX-XX-XXX", amount, _argencode(tmp, sizeof(tmp), desc));
Log.print("URL : ");
Log.println(buff);
};
snprintf(buff, sizeof(buff), PAY_URL PAY_PATH "?node=%s&src=%s&amount=%s&description=%s",
terminalName, tag, amount, _argencode(tmp, sizeof(tmp), desc));
int httpCode = 0;
JSONVar res = rest(buff, &httpCode);
if (httpCode == HTTP_CODE_OK) {
bool ok = false;
strncpy(result, (const char*) res["user"],PBR_LEN-1 /* ensure termination; one less */);
if (res.hasOwnProperty("result"))
ok = (bool) res["result"];
if (!ok) {
Log.println("HTTP_CODE_OK Ok, but false / incpmplete result.");
httpCode = 600;
}
};
return httpCode;
}
int fetchPricelist() {
int httpCode = 0, len = -1;
updateDisplay_progressText("fetching prices");
JSONVar res = rest(PAY_URL REGISTER_PATH, &httpCode);
if (httpCode == HTTP_CODE_BAD_REQUEST) {
// propably too soon/fast
updateDisplay_progressText("re-pairing wit existing key");
return httpCode;
};
if (httpCode != HTTP_CODE_OK) {
Log.println("SKU price list fetch failed.");
return httpCode;
}
const char * nme = res["name"];
const char * desc = res["description"];
if (!nme || !strlen(nme)) {
Log.println("Bogus SKU or not yet assiged a station");
updateDisplay_progressText("no station assigned in CRM");
return 444;
}
if (desc && strlen(desc))
stationname = strdup(desc);
else
stationname = terminalName;
len = res["pricelist"].length();
if (len < 0 || len > 256) {
Log.println("Bogus SKU price list");
return 555;
}
double cap = res["max_permission_amount"];
if (cap > 0) {
Log.printf("Non default permission amount of %.2% euro\n", cap);
amount_no_ok_needed = cap;
};
amounts = (char **) malloc(sizeof(char *) * len);
prices = (char **) malloc(sizeof(char *) * len);
descs = (char **) malloc(sizeof(char *) * len);
default_item = -1;
for (int i = 0; i < len; i++) {
JSONVar item = res["pricelist"][i];
amounts[i] = strdup(item["name"]);
prices[i] = strdup(item["price"]);
descs[i] = strdup(item["description"]);
if (item["default"])
amount = default_item = i;
Debug.printf("%12s %c %s\n", amounts[i], i == default_item ? '*' : ' ', prices[i]);
};
Debug.printf("Pricelist fetched; %d item%s\n", len, len > 1 ? "s" : "");
NA = len;
updateDisplay_progressText("got prices");
return httpCode;
}