Skip to content

Commit 7b86882

Browse files
committed
Add functions to import signature and key id to compressed cert struct
1 parent cd3eda5 commit 7b86882

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

src/ECP256Certificate.cpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,16 @@ int ECP256Certificate::importCert(const byte certDER[], size_t derLen)
302302

303303
memcpy(_certBuffer, certDER, _certBufferLen);
304304

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+
305315
return 1;
306316
}
307317

@@ -914,3 +924,72 @@ int ECP256Certificate::appendAuthorityKeyId(const byte authorityKeyId[], int len
914924

915925
return length + 17;
916926
}
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+
}

src/ECP256Certificate.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@ class ECP256Certificate {
176176
int appendEcdsaWithSHA256(byte out[]);
177177
int appendAuthorityKeyId(const byte authorityKeyId[], int length, byte out[]);
178178

179+
int importCompressedAuthorityKeyIdentifier();
180+
int importCompressedSignature();
181+
179182
};
180183

181184
#endif /* ECP256_CERTIFICATE_H */

0 commit comments

Comments
 (0)