Converting OID of public key,etc in HEX data to the dot format

928 Views Asked by At

Hello guys,

I have CV1 RSA certificates that are slightly modified.So, I dont want to use asn1wrap to parse a 'der' file as it makes it too complex sometimes,instead as tags are already fixed for a CV1 certificate i can parse the HEX data of this 'der' file by converting the binary data to hex and extracting the required range of data.

However for representation i want the OID to be in the dot format eg : ABSOLUTE OID 1.3.6.33.4.11.5318.2888.18.10377.5

i can extract the hex string for this from the whole hex data as : '060D2B0621040BA946964812D10905'

any python3 function that can directly do this conversion. Or can anyone help out with the logic to convert the same.

1

There are 1 best solutions below

0
On

Found an answer for anyone who's interested. Without using the pyasn1 or asn1crypto i did not find any package to convert the hexadecimal value to OID notation. So i browsed around and made a mix of code from other languages and created one in python.

def notation_OID(oidhex_string):

    ''' Input is a hex string and as one byte is 2 charecters i take an 
        empty list and insert 2 characters per element of the list.
        So for a string 'DEADBEEF' it would be ['DE','AD','BE,'EF']. '''
    hex_list = [] 
    for char in range(0,len(oidhex_string),2):
        hex_list.append(oidhex_string[char]+oidhex_string[char+1])

    ''' I have deleted the first two element of the list as my hex string
        includes the standard OID tag '06' and the OID length '0D'. 
        These values are not required for the calculation as i've used 
        absolute OID and not using any ASN.1 modules. Can be removed if you
        have only the data part of the OID in hex string. '''
    del hex_list[0]
    del hex_list[0]

    # An empty string to append the value of the OID in standard notation after
    # processing each element of the list.
    OID_str = ''

    # Convert the list with hex data in str format to int format for 
    # calculations.
    for element in range(len(hex_list)):
        hex_list[element] = int(hex_list[element],16)

    # Convert the OID to its standard notation. Sourced from code in other 
    # languages and adapted for python.

    # The first two digits of the OID are calculated differently from the rest. 
    x = int(hex_list[0] / 40)
    y = int(hex_list[0] % 40)
    if x > 2:
        y += (x-2)*40
        x = 2;

    OID_str += str(x)+'.'+str(y)

    val = 0
    for byte in range(1,len(hex_list)):
        val = ((val<<7) | ((hex_list[byte] & 0x7F)))
        if (hex_list[byte] & 0x80) != 0x80:
            OID_str += "."+str(val)
            val = 0

    # print the OID in dot notation.
    print (OID_str)

notation_OID('060D2B0621040BA946964812D10905')

Hope this helps... cHEErs !