@@ -302,6 +302,16 @@ int ECP256Certificate::importCert(const byte certDER[], size_t derLen)
302
302
303
303
memcpy (_certBuffer, certDER, _certBufferLen);
304
304
305
+ /* Import Authority Key Identifier to compressed cert struct */
306
+ if (!importCompressedAuthorityKeyIdentifier ()) {
307
+ return 0 ;
308
+ }
309
+
310
+ /* Import signature to compressed cert struct */
311
+ if (!importCompressedSignature ()) {
312
+ return 0 ;
313
+ }
314
+
305
315
return 1 ;
306
316
}
307
317
@@ -914,3 +924,72 @@ int ECP256Certificate::appendAuthorityKeyId(const byte authorityKeyId[], int len
914
924
915
925
return length + 17 ;
916
926
}
927
+
928
+ int ECP256Certificate::importCompressedAuthorityKeyIdentifier () {
929
+ static const byte objectId[] = {0x06 , 0x03 , 0x55 , 0x1D , 0x23 };
930
+ byte * result = nullptr ;
931
+ void * ptr = memmem (_certBuffer, _certBufferLen, objectId, sizeof (objectId));
932
+ if (ptr != nullptr ) {
933
+ result = (byte*)ptr;
934
+ result += 11 ;
935
+ memcpy (_compressedCert.slot .two .values .authorityKeyId , result, ECP256_CERT_AUTHORITY_KEY_ID_LENGTH);
936
+ return 1 ;
937
+ }
938
+ return 0 ;
939
+ }
940
+
941
+ int ECP256Certificate::importCompressedSignature () {
942
+ byte * result = nullptr ;
943
+ byte paddingBytes = 0 ;
944
+ byte rLen = 0 ;
945
+ byte sLen = 0 ;
946
+
947
+ /* Search AuthorityKeyIdentifier */
948
+ static const byte KeyId[] = {0x06 , 0x03 , 0x55 , 0x1D , 0x23 };
949
+ void * ptr = memmem (_certBuffer, _certBufferLen, KeyId, sizeof (KeyId));
950
+ if (ptr == nullptr ) {
951
+ return 0 ;
952
+ }
953
+ result = (byte*)ptr;
954
+
955
+ /* Search Algorithm identifier */
956
+ static const byte AlgId[] = {0x06 , 0x08 , 0x2A , 0x86 , 0x48 , 0xCE , 0x3D , 0x04 , 0x03 , 0x02 };
957
+ ptr = memmem (result, _certBufferLen - (_certBuffer - result), AlgId, sizeof (AlgId));
958
+ if (ptr == nullptr ) {
959
+ return 0 ;
960
+ }
961
+ result = (byte*)ptr;
962
+
963
+ /* Skip algorithm idetifier */
964
+ result += sizeof (AlgId);
965
+
966
+ /* Search signature sequence */
967
+ if (result[0 ] == 0x03 ) {
968
+ /* Move to the first element of R sequence skipping 0x03 0x49 0x00 0x30 0xXX*/
969
+ result += 5 ;
970
+ /* Check if value is padded */
971
+ if (result[0 ] == 0x02 && result[1 ] == 0x21 && result[2 ] == 0x00 ) {
972
+ paddingBytes = 1 ;
973
+ }
974
+ rLen = result[1 ] - paddingBytes;
975
+ /* Skip padding and ASN INTEGER sequence 0x02 0xXX */
976
+ result += (2 + paddingBytes);
977
+ /* Copy data to compressed slot */
978
+ memcpy (_compressedCert.slot .one .values .signature , result, rLen);
979
+ /* reset padding before importing S sequence */
980
+ paddingBytes = 0 ;
981
+ /* Move to the first element of S sequence skipping R values */
982
+ result += rLen;
983
+ /* Check if value is padded */
984
+ if (result[0 ] == 0x02 && result[1 ] == 0x21 && result[2 ] == 0x00 ) {
985
+ paddingBytes = 1 ;
986
+ }
987
+ sLen = result[1 ] - paddingBytes;
988
+ /* Skip padding and ASN INTEGER sequence 0x02 0xXX */
989
+ result += (2 + paddingBytes);
990
+ /* Copy data to compressed slot */
991
+ memcpy (&_compressedCert.slot .one .values .signature [rLen], result, sLen );
992
+ return 1 ;
993
+ }
994
+ return 0 ;
995
+ }
0 commit comments