@@ -18,7 +18,8 @@ struct __attribute__((__packed__)) CompressedCert {
1818 byte unused[5 ];
1919};
2020
21- #define SERIAL_NUMBER_LENGTH 16
21+ #define SERIAL_NUMBER_LENGTH 16
22+ #define AUTHORITY_KEY_IDENTIFIER_LENGTH 20
2223
2324static String base64Encode (const byte in[], unsigned int length, const char * prefix, const char * suffix)
2425{
@@ -73,7 +74,7 @@ ECCX08CertClass::ECCX08CertClass() :
7374 _keySlot(-1 ),
7475 _compressedCertSlot(-1 ),
7576 _serialNumberSlot(-1 ),
76- _authorityKeyIdentifier( NULL ),
77+ _authorityKeyIdentifierSlot(- 1 ),
7778 _bytes(NULL ),
7879 _length(0 )
7980{
@@ -185,7 +186,7 @@ String ECCX08CertClass::endCSR()
185186 return base64Encode (csr, csrLen + csrHeaderLen, " -----BEGIN CERTIFICATE REQUEST-----\n " , " \n -----END CERTIFICATE REQUEST-----\n " );
186187}
187188
188- int ECCX08CertClass::beginStorage (int compressedCertSlot, int serialNumberSlot)
189+ int ECCX08CertClass::beginStorage (int compressedCertSlot, int serialNumberSlot, int authorityKeyIdentifierSlot )
189190{
190191 if (compressedCertSlot < 8 || compressedCertSlot > 15 ) {
191192 return 0 ;
@@ -195,8 +196,15 @@ int ECCX08CertClass::beginStorage(int compressedCertSlot, int serialNumberSlot)
195196 return 0 ;
196197 }
197198
199+ if (authorityKeyIdentifierSlot > -1 ) {
200+ if (authorityKeyIdentifierSlot < 8 || authorityKeyIdentifierSlot > 15 ) {
201+ return 0 ;
202+ }
203+ }
204+
198205 _compressedCertSlot = compressedCertSlot;
199206 _serialNumberSlot = serialNumberSlot;
207+ _authorityKeyIdentifierSlot = authorityKeyIdentifierSlot;
200208
201209 memset (_temp, 0x00 , sizeof (_temp));
202210
@@ -256,11 +264,16 @@ void ECCX08CertClass::setExpireYears(int expireYears)
256264 compressedCert->dates [2 ] |= expireYears;
257265}
258266
259- void ECCX08CertClass::setSerialNumber (byte serialNumber[])
267+ void ECCX08CertClass::setSerialNumber (const byte serialNumber[])
260268{
261269 memcpy (&_temp[72 ], serialNumber, SERIAL_NUMBER_LENGTH);
262270}
263271
272+ void ECCX08CertClass::setAuthorityKeyIdentifier (const byte authorityKeyIdentifier[])
273+ {
274+ memcpy (&_temp[88 ], authorityKeyIdentifier, AUTHORITY_KEY_IDENTIFIER_LENGTH);
275+ }
276+
264277int ECCX08CertClass::endStorage ()
265278{
266279 if (!ECCX08.writeSlot (_compressedCertSlot, &_temp[0 ], 72 )) {
@@ -271,10 +284,14 @@ int ECCX08CertClass::endStorage()
271284 return 0 ;
272285 }
273286
287+ if (!ECCX08.writeSlot (_authorityKeyIdentifierSlot, &_temp[88 ], AUTHORITY_KEY_IDENTIFIER_LENGTH)) {
288+ return 0 ;
289+ }
290+
274291 return 1 ;
275292}
276293
277- int ECCX08CertClass::beginReconstruction (int keySlot, int compressedCertSlot, int serialNumberSlot)
294+ int ECCX08CertClass::beginReconstruction (int keySlot, int compressedCertSlot, int serialNumberSlot, int authorityKeyIdentifierSlot )
278295{
279296 if (keySlot < 0 || keySlot > 8 ) {
280297 return 0 ;
@@ -288,9 +305,16 @@ int ECCX08CertClass::beginReconstruction(int keySlot, int compressedCertSlot, in
288305 return 0 ;
289306 }
290307
308+ if (authorityKeyIdentifierSlot > -1 ) {
309+ if (authorityKeyIdentifierSlot < 8 || authorityKeyIdentifierSlot > 15 ) {
310+ return 0 ;
311+ }
312+ }
313+
291314 _keySlot = keySlot;
292315 _compressedCertSlot = compressedCertSlot;
293316 _serialNumberSlot = serialNumberSlot;
317+ _authorityKeyIdentifierSlot = authorityKeyIdentifierSlot;
294318
295319 return 1 ;
296320}
@@ -300,6 +324,7 @@ int ECCX08CertClass::endReconstruction()
300324 byte publicKey[64 ];
301325 struct CompressedCert compressedCert;
302326 byte serialNumber[SERIAL_NUMBER_LENGTH];
327+ byte authorityKeyIdentifier[AUTHORITY_KEY_IDENTIFIER_LENGTH];
303328
304329 if (!ECCX08.generatePublicKey (_keySlot, publicKey)) {
305330 return 0 ;
@@ -313,6 +338,11 @@ int ECCX08CertClass::endReconstruction()
313338 return 0 ;
314339 }
315340
341+ if (_authorityKeyIdentifierSlot > -1 &&
342+ !ECCX08.readSlot (_authorityKeyIdentifierSlot, authorityKeyIdentifier, sizeof (authorityKeyIdentifier))) {
343+ return 0 ;
344+ }
345+
316346 int serialNumberLen = serialNumberLength (serialNumber);
317347
318348 int issuerLen = issuerOrSubjectLength (_issuerCountryName,
@@ -335,7 +365,11 @@ int ECCX08CertClass::endReconstruction()
335365
336366 int publicKeyLen = publicKeyLength ();
337367
338- int authorityKeyIdentifierLen = authorityKeyIdentifierLength (_authorityKeyIdentifier);
368+ int authorityKeyIdentifierLen = 0 ;
369+
370+ if (_authorityKeyIdentifierSlot > -1 ) {
371+ authorityKeyIdentifierLen = authorityKeyIdentifierLength ();
372+ }
339373
340374 int signatureLen = signatureLength (compressedCert.signature );
341375
@@ -422,7 +456,7 @@ int ECCX08CertClass::endReconstruction()
422456 out += publicKeyLen;
423457
424458 if (authorityKeyIdentifierLen) {
425- appendAuthorityKeyIdentifier (_authorityKeyIdentifier , out);
459+ appendAuthorityKeyIdentifier (authorityKeyIdentifier , out);
426460 out += authorityKeyIdentifierLen;
427461 } else {
428462 // null sequence
@@ -509,11 +543,6 @@ void ECCX08CertClass::setSubjectCommonName(const String& commonName)
509543 _subjectCommonName = commonName;
510544}
511545
512- void ECCX08CertClass::setAuthorityKeyIdentifier (const byte authorityKeyIdentifier[])
513- {
514- _authorityKeyIdentifier = authorityKeyIdentifier;
515- }
516-
517546int ECCX08CertClass::versionLength ()
518547{
519548 return 3 ;
@@ -566,9 +595,9 @@ int ECCX08CertClass::publicKeyLength()
566595 return (2 + 2 + 9 + 10 + 4 + 64 );
567596}
568597
569- int ECCX08CertClass::authorityKeyIdentifierLength (const byte authorityKeyIdentifier[] )
598+ int ECCX08CertClass::authorityKeyIdentifierLength ()
570599{
571- return (authorityKeyIdentifier == NULL ) ? 0 : 37 ;
600+ return 37 ;
572601}
573602
574603int ECCX08CertClass::signatureLength (const byte signature[])
0 commit comments