@@ -18,7 +18,8 @@ struct __attribute__((__packed__)) CompressedCert {
18
18
byte unused[5 ];
19
19
};
20
20
21
- #define SERIAL_NUMBER_LENGTH 16
21
+ #define SERIAL_NUMBER_LENGTH 16
22
+ #define AUTHORITY_KEY_IDENTIFIER_LENGTH 20
22
23
23
24
static String base64Encode (const byte in[], unsigned int length, const char * prefix, const char * suffix)
24
25
{
@@ -73,7 +74,7 @@ ECCX08CertClass::ECCX08CertClass() :
73
74
_keySlot(-1 ),
74
75
_compressedCertSlot(-1 ),
75
76
_serialNumberSlot(-1 ),
76
- _authorityKeyIdentifier( NULL ),
77
+ _authorityKeyIdentifierSlot(- 1 ),
77
78
_bytes(NULL ),
78
79
_length(0 )
79
80
{
@@ -185,7 +186,7 @@ String ECCX08CertClass::endCSR()
185
186
return base64Encode (csr, csrLen + csrHeaderLen, " -----BEGIN CERTIFICATE REQUEST-----\n " , " \n -----END CERTIFICATE REQUEST-----\n " );
186
187
}
187
188
188
- int ECCX08CertClass::beginStorage (int compressedCertSlot, int serialNumberSlot)
189
+ int ECCX08CertClass::beginStorage (int compressedCertSlot, int serialNumberSlot, int authorityKeyIdentifierSlot )
189
190
{
190
191
if (compressedCertSlot < 8 || compressedCertSlot > 15 ) {
191
192
return 0 ;
@@ -195,8 +196,15 @@ int ECCX08CertClass::beginStorage(int compressedCertSlot, int serialNumberSlot)
195
196
return 0 ;
196
197
}
197
198
199
+ if (authorityKeyIdentifierSlot > -1 ) {
200
+ if (authorityKeyIdentifierSlot < 8 || authorityKeyIdentifierSlot > 15 ) {
201
+ return 0 ;
202
+ }
203
+ }
204
+
198
205
_compressedCertSlot = compressedCertSlot;
199
206
_serialNumberSlot = serialNumberSlot;
207
+ _authorityKeyIdentifierSlot = authorityKeyIdentifierSlot;
200
208
201
209
memset (_temp, 0x00 , sizeof (_temp));
202
210
@@ -256,11 +264,16 @@ void ECCX08CertClass::setExpireYears(int expireYears)
256
264
compressedCert->dates [2 ] |= expireYears;
257
265
}
258
266
259
- void ECCX08CertClass::setSerialNumber (byte serialNumber[])
267
+ void ECCX08CertClass::setSerialNumber (const byte serialNumber[])
260
268
{
261
269
memcpy (&_temp[72 ], serialNumber, SERIAL_NUMBER_LENGTH);
262
270
}
263
271
272
+ void ECCX08CertClass::setAuthorityKeyIdentifier (const byte authorityKeyIdentifier[])
273
+ {
274
+ memcpy (&_temp[88 ], authorityKeyIdentifier, AUTHORITY_KEY_IDENTIFIER_LENGTH);
275
+ }
276
+
264
277
int ECCX08CertClass::endStorage ()
265
278
{
266
279
if (!ECCX08.writeSlot (_compressedCertSlot, &_temp[0 ], 72 )) {
@@ -271,10 +284,14 @@ int ECCX08CertClass::endStorage()
271
284
return 0 ;
272
285
}
273
286
287
+ if (!ECCX08.writeSlot (_authorityKeyIdentifierSlot, &_temp[88 ], AUTHORITY_KEY_IDENTIFIER_LENGTH)) {
288
+ return 0 ;
289
+ }
290
+
274
291
return 1 ;
275
292
}
276
293
277
- int ECCX08CertClass::beginReconstruction (int keySlot, int compressedCertSlot, int serialNumberSlot)
294
+ int ECCX08CertClass::beginReconstruction (int keySlot, int compressedCertSlot, int serialNumberSlot, int authorityKeyIdentifierSlot )
278
295
{
279
296
if (keySlot < 0 || keySlot > 8 ) {
280
297
return 0 ;
@@ -288,9 +305,16 @@ int ECCX08CertClass::beginReconstruction(int keySlot, int compressedCertSlot, in
288
305
return 0 ;
289
306
}
290
307
308
+ if (authorityKeyIdentifierSlot > -1 ) {
309
+ if (authorityKeyIdentifierSlot < 8 || authorityKeyIdentifierSlot > 15 ) {
310
+ return 0 ;
311
+ }
312
+ }
313
+
291
314
_keySlot = keySlot;
292
315
_compressedCertSlot = compressedCertSlot;
293
316
_serialNumberSlot = serialNumberSlot;
317
+ _authorityKeyIdentifierSlot = authorityKeyIdentifierSlot;
294
318
295
319
return 1 ;
296
320
}
@@ -300,6 +324,7 @@ int ECCX08CertClass::endReconstruction()
300
324
byte publicKey[64 ];
301
325
struct CompressedCert compressedCert;
302
326
byte serialNumber[SERIAL_NUMBER_LENGTH];
327
+ byte authorityKeyIdentifier[AUTHORITY_KEY_IDENTIFIER_LENGTH];
303
328
304
329
if (!ECCX08.generatePublicKey (_keySlot, publicKey)) {
305
330
return 0 ;
@@ -313,6 +338,11 @@ int ECCX08CertClass::endReconstruction()
313
338
return 0 ;
314
339
}
315
340
341
+ if (_authorityKeyIdentifierSlot > -1 &&
342
+ !ECCX08.readSlot (_authorityKeyIdentifierSlot, authorityKeyIdentifier, sizeof (authorityKeyIdentifier))) {
343
+ return 0 ;
344
+ }
345
+
316
346
int serialNumberLen = serialNumberLength (serialNumber);
317
347
318
348
int issuerLen = issuerOrSubjectLength (_issuerCountryName,
@@ -335,7 +365,11 @@ int ECCX08CertClass::endReconstruction()
335
365
336
366
int publicKeyLen = publicKeyLength ();
337
367
338
- int authorityKeyIdentifierLen = authorityKeyIdentifierLength (_authorityKeyIdentifier);
368
+ int authorityKeyIdentifierLen = 0 ;
369
+
370
+ if (_authorityKeyIdentifierSlot > -1 ) {
371
+ authorityKeyIdentifierLen = authorityKeyIdentifierLength ();
372
+ }
339
373
340
374
int signatureLen = signatureLength (compressedCert.signature );
341
375
@@ -422,7 +456,7 @@ int ECCX08CertClass::endReconstruction()
422
456
out += publicKeyLen;
423
457
424
458
if (authorityKeyIdentifierLen) {
425
- appendAuthorityKeyIdentifier (_authorityKeyIdentifier , out);
459
+ appendAuthorityKeyIdentifier (authorityKeyIdentifier , out);
426
460
out += authorityKeyIdentifierLen;
427
461
} else {
428
462
// null sequence
@@ -509,11 +543,6 @@ void ECCX08CertClass::setSubjectCommonName(const String& commonName)
509
543
_subjectCommonName = commonName;
510
544
}
511
545
512
- void ECCX08CertClass::setAuthorityKeyIdentifier (const byte authorityKeyIdentifier[])
513
- {
514
- _authorityKeyIdentifier = authorityKeyIdentifier;
515
- }
516
-
517
546
int ECCX08CertClass::versionLength ()
518
547
{
519
548
return 3 ;
@@ -566,9 +595,9 @@ int ECCX08CertClass::publicKeyLength()
566
595
return (2 + 2 + 9 + 10 + 4 + 64 );
567
596
}
568
597
569
- int ECCX08CertClass::authorityKeyIdentifierLength (const byte authorityKeyIdentifier[] )
598
+ int ECCX08CertClass::authorityKeyIdentifierLength ()
570
599
{
571
- return (authorityKeyIdentifier == NULL ) ? 0 : 37 ;
600
+ return 37 ;
572
601
}
573
602
574
603
int ECCX08CertClass::signatureLength (const byte signature[])
0 commit comments