-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsr.cpp
326 lines (290 loc) · 14.8 KB
/
csr.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
#include <OPTIGATrustM.h>
// MbedTLS includes
#include "mbedtls/config.h"
#include "mbedtls/asn1write.h"
#include "mbedtls/oid.h"
#include "csr.h"
#define KEY_LEN 256 // Bytes (2048 bit)
#define KEY_MAXLEN 300
#define SIG_LEN KEY_LEN
#define HASH_LEN 32 // SHA256
#define UID_LENGTH 27
#define ASN1_CHK_ADD(l, d, f) \
do { \
if( ( ret = f ) < 0 ) \
return( ret ); \
else \
l[d] += ret; \
} while( 0 )
uint8_t buffer[0x300];
uint8_t pubKey[300];
static int32_t fillUnsignedCertificateSigningRequestRSA2048(char *countryName, char *state, char *locality, char *organizationName, char *organizationalUnitName, char *commonName, uint8_t *serialNumber, uint8_t serialNumber_len,
uint8_t *pk_modulus, int32_t pk_exponent, uint8_t **to_sign_start, uint16_t *to_sign_len, uint8_t **signature, uint8_t **csr_start, uint16_t *csr_len)
{
int32_t ret;
int32_t len[6] = {0};
uint8_t *c = buffer + sizeof(buffer); // walk from end towards begin of buffer
uint8_t *to_sign_end;
static uint8_t sig[SIG_LEN];
// signature BIT STRING (2048 bit) (@level 1)
ASN1_CHK_ADD(len, 1, mbedtls_asn1_write_bitstring(&c, buffer, sig, SIG_LEN * 8));
*signature = c + 5;
// null tag with zero data (@level 2)
// algorithm OBJECT IDENTIFIER 1.2.840.113549.1.1.11 sha256WithRSAEncryption (PKCS #1) (@level 2)
// signatureAlgorithm AlgorithmIdentifier SEQUENCE (2 elem) (@level 1)
ASN1_CHK_ADD(len, 1, mbedtls_asn1_write_algorithm_identifier(&c, buffer, MBEDTLS_OID_PKCS1_SHA256, MBEDTLS_OID_SIZE(MBEDTLS_OID_PKCS1_SHA256), 0));
to_sign_end = c;
// Attributes [?] [0] (0 elem) (@level 2)
ASN1_CHK_ADD(len, 2, mbedtls_asn1_write_len(&c, buffer, len[2]));
ASN1_CHK_ADD(len, 2, mbedtls_asn1_write_tag(&c, buffer, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC));
// INTEGER public key exponent (@level 5)
ASN1_CHK_ADD(len, 5, mbedtls_asn1_write_int(&c, buffer, pk_exponent));
// INTEGER (2048 bit) public key modulus (@level 5)
c -= KEY_LEN;
*--c = 0x00;
memcpy (&c[1], pk_modulus, KEY_LEN);
len[5] += (KEY_LEN + 1);
ASN1_CHK_ADD(len, 5, mbedtls_asn1_write_len(&c, buffer, KEY_LEN + 1));
ASN1_CHK_ADD(len, 5, mbedtls_asn1_write_tag(&c, buffer, MBEDTLS_ASN1_INTEGER));
// SEQUENCE(2 elem) (@level 4)
len[4] += len[5];
ASN1_CHK_ADD(len, 4, mbedtls_asn1_write_len(&c, buffer, len[5]));
ASN1_CHK_ADD(len, 4, mbedtls_asn1_write_tag(&c, buffer, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
// subjectPublicKey BIT STRING (2160 bit) (@level 4)
*--c = 0x00;
len[4]++;
len[3] += len[4];
ASN1_CHK_ADD(len, 3, mbedtls_asn1_write_len(&c, buffer, len[4]));
ASN1_CHK_ADD(len, 3, mbedtls_asn1_write_tag(&c, buffer, MBEDTLS_ASN1_BIT_STRING));
// null tag with zero data (@level 4)
// algorithm OBJECT IDENTIFIER 1.2.840.113549.1.1.1 rsaEncryption (PKCS #1) (@level 4)
// algorithm AlgorithmIdentifier SEQUENCE (2 elem) (@level 3)
ASN1_CHK_ADD(len, 3, mbedtls_asn1_write_algorithm_identifier(&c, buffer, MBEDTLS_OID_PKCS1_RSA, MBEDTLS_OID_SIZE(MBEDTLS_OID_PKCS1_RSA), 0));
// subjectPKInfo SubjectPublicKeyInfo SEQUENCE (2 elem) (@level 2)
len[2] += len[3];
ASN1_CHK_ADD(len, 2, mbedtls_asn1_write_len(&c, buffer, len[3]));
ASN1_CHK_ADD(len, 2, mbedtls_asn1_write_tag(&c, buffer, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
len[3] = 0;
if (serialNumber != NULL)
{
// value AttributeValue [?] PrintableString (@level 5)
len[4] = 0;
len[5] = 0;
ASN1_CHK_ADD(len, 5, mbedtls_asn1_write_printable_string(&c, buffer, (char*)serialNumber, serialNumber_len));
// type AttributeType OBJECT IDENTIFIER 2.5.4.5 serialNumber (X.520 DN component) (@level 5)
ASN1_CHK_ADD(len, 5, mbedtls_asn1_write_oid(&c, buffer, MBEDTLS_OID_AT_SERIAL_NUMBER, MBEDTLS_OID_SIZE(MBEDTLS_OID_AT_SERIAL_NUMBER)));
// AttributeTypeAndValue SEQUENCE (2 elem) (@level 4)
len[4] += len[5];
ASN1_CHK_ADD(len, 4, mbedtls_asn1_write_len(&c, buffer, len[5]));
ASN1_CHK_ADD(len, 4, mbedtls_asn1_write_tag(&c, buffer, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
// RelativeDistinguishedName SET(1 elem) (@level 3)
len[3] += len[4];
ASN1_CHK_ADD(len, 3, mbedtls_asn1_write_len(&c, buffer, len[4]));
ASN1_CHK_ADD(len, 3, mbedtls_asn1_write_tag(&c, buffer, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET));
}
if (commonName != NULL)
{
// value AttributeValue [?] UTF8String (@level 5)
len[4] = 0;
len[5] = 0;
ASN1_CHK_ADD(len, 5, mbedtls_asn1_write_utf8_string(&c, buffer, commonName, strlen(commonName)));
// type AttributeType OBJECT IDENTIFIER 2.5.4.3 commonName (X.520 DN component) (@level 5)
ASN1_CHK_ADD(len, 5, mbedtls_asn1_write_oid(&c, buffer, MBEDTLS_OID_AT_CN, MBEDTLS_OID_SIZE(MBEDTLS_OID_AT_CN)));
// AttributeTypeAndValue SEQUENCE (2 elem) (@level 4)
len[4] += len[5];
ASN1_CHK_ADD(len, 4, mbedtls_asn1_write_len(&c, buffer, len[5]));
ASN1_CHK_ADD(len, 4, mbedtls_asn1_write_tag(&c, buffer, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
// RelativeDistinguishedName SET(1 elem) (@level 3)
len[3] += len[4];
ASN1_CHK_ADD(len, 3, mbedtls_asn1_write_len(&c, buffer, len[4]));
ASN1_CHK_ADD(len, 3, mbedtls_asn1_write_tag(&c, buffer, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET));
}
if (organizationalUnitName != NULL)
{
// value AttributeValue [?] UTF8String (@level 5)
len[4] = 0;
len[5] = 0;
ASN1_CHK_ADD(len, 5, mbedtls_asn1_write_utf8_string(&c, buffer, organizationalUnitName, strlen(organizationalUnitName)));
// type AttributeType OBJECT IDENTIFIER 2.5.4.11 organizationalUnitName (X.520 DN component) (@level 5)
ASN1_CHK_ADD(len, 5, mbedtls_asn1_write_oid(&c, buffer, MBEDTLS_OID_AT_ORG_UNIT, MBEDTLS_OID_SIZE(MBEDTLS_OID_AT_ORG_UNIT)));
// AttributeTypeAndValue SEQUENCE (2 elem) (@level 4)
len[4] += len[5];
ASN1_CHK_ADD(len, 4, mbedtls_asn1_write_len(&c, buffer, len[5]));
ASN1_CHK_ADD(len, 4, mbedtls_asn1_write_tag(&c, buffer, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
// RelativeDistinguishedName SET(1 elem) (@level 3)
len[3] += len[4];
ASN1_CHK_ADD(len, 3, mbedtls_asn1_write_len(&c, buffer, len[4]));
ASN1_CHK_ADD(len, 3, mbedtls_asn1_write_tag(&c, buffer, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET));
}
if (organizationName != NULL)
{
// value AttributeValue [?] UTF8String (@level 5)
len[4] = 0;
len[5] = 0;
ASN1_CHK_ADD(len, 5, mbedtls_asn1_write_utf8_string(&c, buffer, organizationName, strlen(organizationName)));
// type AttributeType OBJECT IDENTIFIER 2.5.4.10 organizationName (X.520 DN component) (@level 5)
ASN1_CHK_ADD(len, 5, mbedtls_asn1_write_oid(&c, buffer, MBEDTLS_OID_AT_ORGANIZATION, MBEDTLS_OID_SIZE(MBEDTLS_OID_AT_ORGANIZATION)));
// AttributeTypeAndValue SEQUENCE (2 elem) (@level 4)
len[4] += len[5];
ASN1_CHK_ADD(len, 4, mbedtls_asn1_write_len(&c, buffer, len[5]));
ASN1_CHK_ADD(len, 4, mbedtls_asn1_write_tag(&c, buffer, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
// RelativeDistinguishedName SET(1 elem) (@level 3)
len[3] += len[4];
ASN1_CHK_ADD(len, 3, mbedtls_asn1_write_len(&c, buffer, len[4]));
ASN1_CHK_ADD(len, 3, mbedtls_asn1_write_tag(&c, buffer, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET));
}
if (locality != NULL)
{
// value AttributeValue [?] UTF8String (@level 5)
len[4] = 0;
len[5] = 0;
ASN1_CHK_ADD(len, 5, mbedtls_asn1_write_utf8_string(&c, buffer, locality, strlen(locality)));
// type AttributeType OBJECT IDENTIFIER 2.5.4.7 locality (X.520 DN component) (@level 5)
ASN1_CHK_ADD(len, 5, mbedtls_asn1_write_oid(&c, buffer, MBEDTLS_OID_AT_LOCALITY, MBEDTLS_OID_SIZE(MBEDTLS_OID_AT_LOCALITY)));
// AttributeTypeAndValue SEQUENCE (2 elem) (@level 4)
len[4] += len[5];
ASN1_CHK_ADD(len, 4, mbedtls_asn1_write_len(&c, buffer, len[5]));
ASN1_CHK_ADD(len, 4, mbedtls_asn1_write_tag(&c, buffer, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
// RelativeDistinguishedName SET(1 elem) (@level 3)
len[3] += len[4];
ASN1_CHK_ADD(len, 3, mbedtls_asn1_write_len(&c, buffer, len[4]));
ASN1_CHK_ADD(len, 3, mbedtls_asn1_write_tag(&c, buffer, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET));
}
if (state != NULL)
{
// value AttributeValue [?] UTF8String (@level 5)
len[4] = 0;
len[5] = 0;
ASN1_CHK_ADD(len, 5, mbedtls_asn1_write_utf8_string(&c, buffer, state, strlen(state)));
// type AttributeType OBJECT IDENTIFIER 2.5.4.8 state (X.520 DN component) (@level 5)
ASN1_CHK_ADD(len, 5, mbedtls_asn1_write_oid(&c, buffer, MBEDTLS_OID_AT_STATE, MBEDTLS_OID_SIZE(MBEDTLS_OID_AT_STATE)));
// AttributeTypeAndValue SEQUENCE (2 elem) (@level 4)
len[4] += len[5];
ASN1_CHK_ADD(len, 4, mbedtls_asn1_write_len(&c, buffer, len[5]));
ASN1_CHK_ADD(len, 4, mbedtls_asn1_write_tag(&c, buffer, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
// RelativeDistinguishedName SET(1 elem) (@level 3)
len[3] += len[4];
ASN1_CHK_ADD(len, 3, mbedtls_asn1_write_len(&c, buffer, len[4]));
ASN1_CHK_ADD(len, 3, mbedtls_asn1_write_tag(&c, buffer, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET));
}
if (countryName != NULL)
{
// value AttributeValue [?] PrintableString (@level 5)
len[4] = 0;
len[5] = 0;
ASN1_CHK_ADD(len, 5, mbedtls_asn1_write_printable_string(&c, buffer, countryName, strlen(countryName)));
// type AttributeType OBJECT IDENTIFIER 2.5.4.6 countryName (X.520 DN component) (@level 5)
ASN1_CHK_ADD(len, 5, mbedtls_asn1_write_oid(&c, buffer, MBEDTLS_OID_AT_COUNTRY, MBEDTLS_OID_SIZE(MBEDTLS_OID_AT_COUNTRY)));
// AttributeTypeAndValue SEQUENCE (2 elem) (@level 4)
len[4] += len[5];
ASN1_CHK_ADD(len, 4, mbedtls_asn1_write_len(&c, buffer, len[5]));
ASN1_CHK_ADD(len, 4, mbedtls_asn1_write_tag(&c, buffer, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
// RelativeDistinguishedName SET(1 elem) (@level 3)
len[3] += len[4];
ASN1_CHK_ADD(len, 3, mbedtls_asn1_write_len(&c, buffer, len[4]));
ASN1_CHK_ADD(len, 3, mbedtls_asn1_write_tag(&c, buffer, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET));
}
// subject Name SEQUENCE (6 elem) (@level 2)
len[2] += len[3];
ASN1_CHK_ADD(len, 2, mbedtls_asn1_write_len(&c, buffer, len[3]));
ASN1_CHK_ADD(len, 2, mbedtls_asn1_write_tag(&c, buffer, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
// version INTEGER 0 (@level 2)
ASN1_CHK_ADD(len, 2, mbedtls_asn1_write_int(&c, buffer, 0));
// certificationRequestInfo CertificationRequestInfo SEQUENCE (4 elem) (@level 1)
len[1] += len[2];
ASN1_CHK_ADD(len, 1, mbedtls_asn1_write_len(&c, buffer, len[2]));
ASN1_CHK_ADD(len, 1, mbedtls_asn1_write_tag(&c, buffer, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
*to_sign_start = c;
*to_sign_len = (uint16_t)(to_sign_end - *to_sign_start);
// CertificationRequest SEQUENCE (3 elem) (@level 0)
len[0] += len[1];
ASN1_CHK_ADD(len, 0, mbedtls_asn1_write_len(&c, buffer, len[1]));
ASN1_CHK_ADD(len, 0, mbedtls_asn1_write_tag(&c, buffer, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
*csr_start = c;
*csr_len = (uint16_t)len[0];
return 0;
}
int32_t generateCertificateSigningRequestRSA2048(char *countryName, char *state, char *locality, char *organizationName, char *organizationalUnitName, char *commonName, uint8_t **csr_start, uint16_t *csr_len)
{
int32_t ret;
uint8_t uid[UID_LENGTH];
uint16_t uidLen = UID_LENGTH;
uint16_t pubKeyLen = KEY_MAXLEN;
uint8_t *to_sign_start; // pointer to begin of to be signed data
uint16_t to_sign_len; // length of to be signed data
uint8_t *signature; // pointer to signature (256 bytes/2048 bits)
uint8_t hash[HASH_LEN];
uint16_t signLen = SIG_LEN;
Serial.print(" Getting co-processor Unique ID ... ");
ret = trustM.getUniqueID(uid, uidLen);
if (ret != 0)
{
Serial.println("Failed");
return 1;
}
else
{
Serial.println("Ok");
}
//Serial.println("Unique ID:");
//dumpHex(uid, uidLen);
Serial.print(" Generate Key Pair RSA 2048. Store Private Key on Board ... ");
ret = trustM.generateKeypairRSA2048(pubKey, pubKeyLen, OPTIGA_KEY_ID_E0FD);
if (ret != 0)
{
Serial.println("Failed");
return 1;
}
else
{
Serial.println("Ok");
}
//Serial.println("Public Key (DER):");
//dumpHex(pubKey, pubKeyLen);
Serial.print(" Fill unsigned CSR ... ");
ret = fillUnsignedCertificateSigningRequestRSA2048(countryName,
state,
locality,
organizationName,
organizationalUnitName,
commonName,
uid,
uidLen,
&pubKey[14], 65537,
&to_sign_start, &to_sign_len, &signature,
csr_start, csr_len);
if (ret != 0)
{
Serial.println("Failed");
return 1;
}
else
{
Serial.println("Ok");
}
Serial.print(" Calculate Hash of data ... ");
ret = trustM.sha256(to_sign_start, to_sign_len, hash);
if (ret != 0)
{
Serial.println("Failed");
return 1;
}
else
{
Serial.println("Ok");
}
Serial.print(" Signing the hash ... ");
ret = trustM.calculateSignatureRSA(hash, HASH_LEN, OPTIGA_KEY_ID_E0FD, signature, signLen);
if (ret != 0)
{
Serial.println("Failed");
return 1;
}
else
{
Serial.println("Ok");
}
//Serial.println("Signature:");
//dumpHex(signature, SIG_LEN);
return 0;
}