forked from dirkx/makerspaceleiden-payment-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
selfsign.cpp
358 lines (285 loc) · 10.5 KB
/
selfsign.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
#include <Arduino.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "geneckey.h"
#include "log.h"
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
#include "mbedtls/x509_crt.h"
#include "mbedtls/x509_csr.h"
#include "mbedtls/entropy.h"
#include "mbedtls/ctr_drbg.h"
#include "mbedtls/md.h"
#include "mbedtls/error.h"
#include "mbedtls/ctr_drbg.h"
#include "mbedtls/base64.h"
#include <mbedtls/sha256.h>
#include "esp_log.h"
#include "esp_heap_caps.h"
static const char* TAG = "selfsign";
#define DFL_VERSION MBEDTLS_X509_CRT_VERSION_3
#define DFL_SUBJECT_NAME "CN=%s,O=Stichting Makerspace Leiden,L=Leiden,C=NL"
#define DFL_ISSUER_NAME DFL_SUBJECT_NAME
#define DFL_DIGEST MBEDTLS_MD_SHA256
#define DFL_NOT_BEFORE "20211004000000"
#define DFL_NOT_AFTER "21211004000000"
#define DFL_IS_CA 0
#define DFL_MAX_PATHLEN -1
#define DFL_KEY_USAGE MBEDTLS_X509_KU_DIGITAL_SIGNATURE
#define DFL_NS_CERT_TYPE MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT
#define DFL_AUTH_IDENT 1
#define DFL_SUBJ_IDENT 1
#define DFL_CONSTRAINTS 1
#define DEFAULT_PEM_MAX (4*1024) // larger than 1000-ish cert and 100-150 byte key.
static const char seed[] = "selfsign" __DATE__ __TIME__;
int pem2der(unsigned char * buff) {
size_t len = strlen((const char*) buff);
char * p = (char *) buff, *e = p + len;
if (p[0] == '-') { // chop off the headers.
p = index((const char*) buff, '\n') + 1;
if (buff[len - 1 ] == '\n')
buff[len - 1] = 0;
e = rindex((const char*) buff, '\n');
*e = 0;
if (e[1] == 0) {
e = rindex((const char*) buff, '\n');
*e = 0;
};
};
if (mbedtls_base64_decode(buff, len, &len, (const unsigned char*) p, e - p)) {
Log.println("Invalid PEM to DER");
return -1;
};
return len;
}
const char * der2pem(const char *what, unsigned char * der, size_t derlen) {
unsigned char * tmp = NULL, *p, *ep, * pem = NULL;
size_t len = 0;
mbedtls_base64_encode (NULL, 0, &len, der, derlen);
pem = (unsigned char *) malloc(len);
p = tmp = (unsigned char *) malloc(len + len / 64 + 100);
ep = p + len + 100;
p += snprintf((char *)p, ep - p, "-----BEGIN %s-----\n", what);
if (mbedtls_base64_encode (pem, len, &len, der, derlen) < 0) {
Log.println("Failed to encode");
free(pem); free(tmp);
return NULL;
}
// insert linebreaks; to turn it into a PEM
unsigned char *q = pem;
for (int i = 0 ; i < len; i++) {
*p++ = *q++;
if ((i == len - 1) || (i % 64 == 63)) {
*p++ = '\n';
};
};
p += snprintf((char*)p, ep - p, "-----END %s-----\n", what);
free(pem);
return (const char*) tmp;
}
#define MBOK(x) { \
if ((ret = (x)) < 0) {\
mbedtls_strerror( ret, buf, sizeof(buf) ); \
ESP_LOGE(TAG, #x " failed. returned -x%02x, %s", (unsigned int) - ret, buf); \
goto exit; \
}; \
}
int populate_self_signed(mbedtls_pk_context * key, const char * cn, mbedtls_x509write_cert * crt) {
unsigned char rndbuff[16];
char buf[48];
int ret;
mbedtls_entropy_context entropy_ctx;
mbedtls_ctr_drbg_context ctr_drbg;
mbedtls_mpi serial;
mbedtls_entropy_init( &entropy_ctx );
mbedtls_ctr_drbg_init( &ctr_drbg );
mbedtls_mpi_init( &serial );
mbedtls_x509write_crt_init( crt );
MBOK(mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func,
&entropy_ctx, (const unsigned char *) seed, strlen(seed)));
mbedtls_x509write_crt_set_subject_key( crt, key );
mbedtls_x509write_crt_set_issuer_key( crt, key );
char dn[128];
if (index(cn, ',') == 0) {
snprintf(dn, sizeof(dn), DFL_SUBJECT_NAME, cn);
cn = dn;
};
MBOK(mbedtls_x509write_crt_set_subject_name( crt, dn ) );
if (index(cn, ',') == 0) {
snprintf(dn, sizeof(dn), DFL_ISSUER_NAME, cn);
cn = dn;
};
MBOK(mbedtls_x509write_crt_set_issuer_name( crt, dn ));
mbedtls_x509write_crt_set_version( crt, DFL_VERSION );
mbedtls_x509write_crt_set_md_alg( crt, DFL_DIGEST );
MBOK(mbedtls_ctr_drbg_random(&ctr_drbg, rndbuff, sizeof(rndbuff)));
MBOK(mbedtls_mpi_read_binary( &serial, rndbuff, sizeof(rndbuff)));
MBOK(mbedtls_x509write_crt_set_serial( crt, &serial));
MBOK(mbedtls_x509write_crt_set_validity( crt, DFL_NOT_BEFORE, DFL_NOT_AFTER ));
if (DFL_VERSION == MBEDTLS_X509_CRT_VERSION_3) {
if (DFL_CONSTRAINTS != 0 )
MBOK(mbedtls_x509write_crt_set_basic_constraints( crt, DFL_IS_CA, DFL_MAX_PATHLEN ));
if (DFL_SUBJ_IDENT != 0 )
MBOK(mbedtls_x509write_crt_set_subject_key_identifier( crt ));
if (DFL_AUTH_IDENT != 0 )
MBOK(mbedtls_x509write_crt_set_authority_key_identifier( crt ));
if (DFL_KEY_USAGE != 0 )
MBOK(mbedtls_x509write_crt_set_key_usage( crt, DFL_KEY_USAGE ));
if ( DFL_NS_CERT_TYPE != 0 )
(mbedtls_x509write_crt_set_ns_cert_type( crt, DFL_NS_CERT_TYPE ));
};
exit:
return 0;
mbedtls_mpi_free( &serial );
mbedtls_ctr_drbg_free( &ctr_drbg );
mbedtls_entropy_free( &entropy_ctx );
if (ret)
return ret;
}
int sign_and_topem(mbedtls_pk_context * key, mbedtls_x509write_cert * crt, char ** out_cert_as_pem, char ** out_key_as_pem)
{
unsigned char * tmp = NULL, * cp = NULL, *kp = NULL;
char buf[48];
int ret;
int outkeylen = -1, outcertlen = -1;
mbedtls_entropy_context entropy_ctx;
mbedtls_ctr_drbg_context ctr_drbg;
mbedtls_entropy_init( &entropy_ctx );
mbedtls_ctr_drbg_init( &ctr_drbg );
MBOK(mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func,
&entropy_ctx, (const unsigned char *) seed, strlen(seed)));
// MBOK(mbedtls_pk_write_key_pem(key, NULL, 0)); /* we cannot get the length yet in this version of mbed@espressif */
ret = DEFAULT_PEM_MAX;
kp = tmp = (unsigned char *) malloc(ret);
MBOK(mbedtls_pk_write_key_pem(key, tmp, ret));
// MBOK(mbedtls_x509write_crt_pem(crt, NULL, 0, mbedtls_ctr_drbg_random, &ctr_drbg)); /* we cannot get the length yet in this version of mbed@espressif */
ret = DEFAULT_PEM_MAX;
cp = tmp = (unsigned char *) malloc(ret);
MBOK(mbedtls_x509write_crt_pem(crt, tmp, ret, mbedtls_ctr_drbg_random, &ctr_drbg));
ret = 0;
*out_cert_as_pem = strdup((const char*) cp);
*out_key_as_pem = strdup((const char*) kp);
exit:
if (ret)
free(tmp);
if (cp)
free(cp);
if (kp)
free(kp);
mbedtls_ctr_drbg_free( &ctr_drbg );
mbedtls_entropy_free( &entropy_ctx );
return ret;
}
char * sha256toHEX(unsigned char sha256[256 / 8], char buff[256 / 4 + 1]) {
const char _h[] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};
char * p = buff;
for (int i = 0; i < 32; i++) {
*p++ = _h[(sha256[i] >> 4) & 0xF];
*p++ = _h[(sha256[i] >> 0) & 0xF];
}
*p++ = 0;
return buff;
}
int fingerprint_from_pem(char * buff, unsigned char sha256[256 / 8]) {
int ret;
unsigned char * p = (unsigned char*) strdup(buff);
if (((ret = pem2der(p)) < 0 ) ||
((ret = mbedtls_sha256_ret(p, ret, sha256, 0)) < 0 ))
{
Log.printf("fingerprint_from_pem failed: %02X\n", -ret);
memset(sha256, 0, 32);
};
free(p);
return ret;
};
int fingerprint_from_certpubkey(const mbedtls_x509_crt * crt, unsigned char sha256[256 / 8]) {
mbedtls_pk_context * pk = (mbedtls_pk_context*) & (crt->pk);
unsigned char buff[2 * 1024];
int ret;
if (((ret = mbedtls_pk_write_pubkey_pem(pk , buff, sizeof(buff))) < 0) ||
((ret = pem2der(buff)) < 0 ) ||
((ret = mbedtls_sha256_ret(buff, ret, sha256, 0)) < 0 ))
{
Log.printf("fingerprint_from_certpubkey failed: %02X\n", -ret);
memset(sha256, 0, 32);
};
return ret;
};
#if 0
// there is something odd/broken:
// mbedtls_x509write_crt_der/pem and mbedtls_pk_write_key_pem/der do not actually yeild bytewise the same output.
// The PEM one is fine; but the DER one appears to lack the outer sequence and they key has some fields unpopulated.
void dump_der_as_pem(const char *what, unsigned char * der, size_t derlen) {
unsigned char * tmp = NULL;
size_t len = 0;
Log.printf("-----BEGIN %s-----\n", what);
mbedtls_base64_encode (NULL, 0, &len, der, derlen);
tmp = (unsigned char *) malloc(len);
if (mbedtls_base64_encode (tmp, len, &len, der, derlen) < 0) {
Log.println("Failed to encode");
return;
}
for (int i = 0 ; i < len; i++) {
Log.print((char)tmp[i]);
if ((i == len - 1) || (i % 64 == 63))
Log.println();
};
Log.printf("-----END %s-----\n", what);
free(tmp);
}
int sign_and_toder(mbedtls_pk_context * key, mbedtls_x509write_cert * crt, unsigned char ** out_cert_as_der, size_t * outcertlenp, unsigned char ** out_key_as_der, size_t * outkeylenp)
{
unsigned char * der;
char buf[48];
int ret;
int outkeylen = -1, outcertlen = -1;
mbedtls_entropy_context entropy_ctx;
mbedtls_ctr_drbg_context ctr_drbg;
mbedtls_entropy_init( &entropy_ctx );
mbedtls_ctr_drbg_init( &ctr_drbg );
// some padding routine deep inside mbed tls insist on this.
if (!(der = (unsigned char*) heap_caps_malloc(DEFAULT_DER_MAX, MALLOC_CAP_32BIT))) {
Log.println("Failed to allocate memory");
goto exit;
}
der = (unsigned char *) malloc( 8 * 1024);
MBOK(mbedtls_pk_write_key_der(key, der, 8 * 1024));
outkeylen = ret;
if ((NULL == *out_key_as_der) && (NULL == (*out_key_as_der = (unsigned char*)malloc(outkeylen)))) {
ESP_LOGE(TAG, "outkeylen malloc failed (%u bytes)", outkeylen);
goto exit;
};
memcpy(*out_key_as_der, der, outkeylen);
dump_der_as_pem("EC PRIVATE KEY", *out_key_as_der, outkeylen);
MBOK(mbedtls_pk_write_key_pem(key, der, 8 * 1024));
Log.println((char*)der);
MBOK(mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func,
&entropy_ctx, (const unsigned char *) seed, strlen(seed)));
MBOK(mbedtls_x509write_crt_der(crt, der, DEFAULT_DER_MAX, mbedtls_ctr_drbg_random, &ctr_drbg));
outcertlen = ret;
if ((NULL == *out_cert_as_der) && (NULL == (*out_cert_as_der = (unsigned char*)malloc(outcertlen)))) {
ESP_LOGE(TAG, "outcertlen malloc failed (%u bytes)", outcertlen);
free(*out_cert_as_der);
goto exit;
}
memcpy(*out_cert_as_der, der, outcertlen);
Log.println("completion");
MBOK(mbedtls_x509write_crt_pem(crt, der, DEFAULT_DER_MAX, mbedtls_ctr_drbg_random, &ctr_drbg));
Log.println((char*)der);
dump_der_as_pem("CERTIFICATE", *out_cert_as_der, outcertlen);
ret = 0;
exit:
free(der);
if (outkeylen <= 0 || outcertlen <= 0)
return -1;
*outkeylenp = outkeylen;
*outcertlenp = outcertlen;
mbedtls_ctr_drbg_free( &ctr_drbg );
mbedtls_entropy_free( &entropy_ctx );
return ret;
}
#endif