예제 #1
0
 def test_read_multiple(self):
     buf = b'\x02\x01\x01\x02\x01\x02'
     dec = asn1.Decoder()
     dec.start(buf)
     tag, val = dec.read()
     assert val == 1
     tag, val = dec.read()
     assert val == 2
     assert dec.eof()
예제 #2
0
 def test_integer(self):
     buf = b'\x02\x01\x01'
     dec = asn1.Decoder()
     dec.start(buf)
     tag = dec.peek()
     assert tag == (asn1.Numbers.Integer, asn1.Types.Primitive, asn1.Classes.Universal)
     tag, val = dec.read()
     assert isinstance(val, int)
     assert val == 1
예제 #3
0
 def test_private(self):
     buf = b'\xe1\x03\x02\x01\x01'
     dec = asn1.Decoder()
     dec.start(buf)
     tag = dec.peek()
     assert tag == (1, asn1.Types.Constructed, asn1.Classes.Private)
     dec.enter()
     tag, val = dec.read()
     assert val == 1
예제 #4
0
 def test_long_tag_id(self):
     buf = b'\x3f\x83\xff\x7f\x03\x02\x01\x01'
     dec = asn1.Decoder()
     dec.start(buf)
     tag = dec.peek()
     assert tag == (0xffff, asn1.Types.Constructed, asn1.Classes.Universal)
     dec.enter()
     tag, val = dec.read()
     assert val == 1
예제 #5
0
 def test_application(self):
     buf = b'\x61\x03\x02\x01\x01'
     dec = asn1.Decoder()
     dec.start(buf)
     tag = dec.peek()
     assert tag == (1, asn1.Types.Constructed, asn1.Classes.Application)
     dec.enter()
     tag, val = dec.read()
     assert val == 1
예제 #6
0
 def test_object_identifier(self):
     dec = asn1.Decoder()
     buf = b'\x06\x02\x2a\x03'
     dec.start(buf)
     tag = dec.peek()
     assert tag == (asn1.Numbers.ObjectIdentifier, asn1.Types.Primitive,
                    asn1.Classes.Universal)
     tag, val = dec.read()
     assert val == u'1.2.3'
예제 #7
0
 def test_null(self):
     buf = b'\x05\x00'
     dec = asn1.Decoder()
     dec.start(buf)
     tag = dec.peek()
     assert tag == (asn1.Numbers.Null, asn1.Types.Primitive,
                    asn1.Classes.Universal)
     tag, val = dec.read()
     assert val is None
예제 #8
0
 def test_unicode_printable_string(self):
     buf = b'\x13\x05\x66\x6f\x6f\xc3\xa9'
     dec = asn1.Decoder()
     dec.start(buf)
     tag = dec.peek()
     assert tag == (asn1.Numbers.PrintableString, asn1.Types.Primitive,
                    asn1.Classes.Universal)
     tag, val = dec.read()
     assert val == u'fooé'
예제 #9
0
 def test_octet_string(self):
     buf = b'\x04\x03foo'
     dec = asn1.Decoder()
     dec.start(buf)
     tag = dec.peek()
     assert tag == (asn1.Numbers.OctetString, asn1.Types.Primitive,
                    asn1.Classes.Universal)
     tag, val = dec.read()
     assert val == b'foo'
예제 #10
0
def RSAdecode(filename):  # type: (filename) -> (n, e, c, ciphertext)
    integers = []  # list of integers in ASN.1 file
    with open(filename, "rb") as file:
        data = file.read()
        decoder = asn1.Decoder()
        decoder.start(data)
        integers = parse(decoder, integers)
        cipher = data[-integers[
            -1]:]  # get last integrs[last] symbols, this is ciphertext
    return integers[0], integers[1], integers[2], cipher
예제 #11
0
 def test_octet_string(self):
     buf = '\x04\x03foo'
     dec = asn1.Decoder()
     dec.start(buf)
     tag = dec.peek()
     assert tag == (asn1.OctetString, asn1.TypePrimitive,
                    asn1.ClassUniversal)
     tag, val = dec.read()
     assert isinstance(val, str)
     assert val == 'foo'
예제 #12
0
def parse_receipt(rec):
    ret_val = []
    for y in [z for z in rec if type(z) is list]:
        try:
            dec = asn1.Decoder()
            dec.start(y[2][2])
            ret_val.append(MASattr(MAS_types.get(y[0][2], '0x%02X' % y[0][2]), y[1][2], unwind(dec)[0][-1]))
        except:
            ret_val.append(MASattr(MAS_types.get(y[0][2], '0x%02X' % y[0][2]), y[1][2], y[2][2]))
    return ret_val
예제 #13
0
파일: asn.py 프로젝트: Sobieg/CryptoLabs
def GOSTdecodeSign(
        filename
):  # type: (filename) -> (xq, yq, prime, A, B, xp, yp, q, r, s)
    integers = []  # list of integers in ASN.1 file
    with open(filename, "rb") as file:
        data = file.read()
        decoder = asn1.Decoder()
        decoder.start(data)
        integers = parse(decoder, integers)
    return integers
예제 #14
0
 def test_enumerated(self):
     buf = '\x0a\x01\x01'
     dec = asn1.Decoder()
     dec.start(buf)
     tag = dec.peek()
     assert tag == (asn1.Enumerated, asn1.TypePrimitive,
                    asn1.ClassUniversal)
     tag, val = dec.read()
     assert isinstance(val, int)
     assert val == 1
예제 #15
0
def cli(paths, add, verbose, debug, ipython, null):

    byte = b'\n'
    if null:
        byte = b'\x00'

    config, config_mtime = click_read_config(click_instance=click,
                                             app_name=APP_NAME,
                                             verbose=verbose)
    if verbose:
        ic(config, config_mtime)

    for index, path in enumerate(input_iterator(strings=paths,
                                                null=null,
                                                verbose=verbose)):
        if verbose:
            ic(index, path)

        with open(path, 'rb') as fh:
            asn1_bytes = fh.read()

        ic(len(asn1_bytes))

        #received_record, rest_of_substrate = decode(asn1_bytes)

        decoder = asn1.Decoder()
        decoder.start(asn1_bytes)
        tag, value = decoder.read()

        #ic(pem.detect(asn1_bytes))

        #parsed = load(asn1_bytes)


        ##parsed = BitString.load(der_byte_string)
        ##parsed = Sequence.load(der_byte_string)
        #serialized = parsed.dump()
        #ic(len(serialized))


        if ipython:
            import IPython; IPython.embed()

        if add:
            section = "test_section"
            key = "test_key"
            value = "test_value"
            config, config_mtime = click_write_config_entry(click_instance=click,
                                                            app_name=APP_NAME,
                                                            section=section,
                                                            key=key,
                                                            value=value,
                                                            verbose=verbose)
            if verbose:
                ic(config)
예제 #16
0
def decode(filePath):

    decoded_parameters = []
    # decoded_parameters[0] is n (module)
    # decoded_parameters[1] is e (exponent)
    # decoded_parameters[2] is key (encrypted_AES_key)
    # decoded_parameters[3] is iv
    # decoded_parameters[4] is cipher_len (cipher_text length)
    data = open(filePath, 'rb').read()
    #data = file.read()
    decoder = asn1.Decoder()
    decoder.start(data)
    # decoded_parameters = parse(decoder, decoded_parameters)

    decoder.enter()
    decoder.enter()
    decoder.enter()

    decoder.read()  #b'\x00\x01'
    decoder.read()  #b'Data'

    decoder.enter()
    n = decoder.read()[1]
    e = decoder.read()[1]
    decoder.leave()

    decoder.enter()
    decoder.leave()

    decoder.enter()
    enc_K = decoder.read()[1]
    decoder.leave()

    decoder.leave()
    decoder.leave()

    decoder.enter()
    decoder.read()[1]
    iv = decoder.read()[1]
    kol = decoder.read()[1]
    decoder.leave()
    decoder.leave()
    data = decoder.read()[1]

    #data = bytearray(data)
    #cipher_len = decoded_parameters[-1]
    #cipher_bytes = bytearray()

    #for i in range(len(data) - cipher_len, len(data)):
    #  cipher_bytes.append(data[i])

    with open('~tmp', 'wb') as file_cipher:
        file_cipher.write(data)  #cipher_bytes)

    return n, e, enc_K, iv  #decoded_parameters[0], decoded_parameters[1], decoded_parameters[2]
예제 #17
0
 def test_sequence_of(self):
     buf = b'\x30\x06\x02\x01\x01\x02\x01\x02'
     dec = asn1.Decoder()
     dec.start(buf)
     tag = dec.peek()
     assert tag == (asn1.Numbers.Sequence, asn1.Types.Constructed, asn1.Classes.Universal)
     dec.enter()
     tag, val = dec.read()
     assert val == 1
     tag, val = dec.read()
     assert val == 2
예제 #18
0
 def test_set_of(self):
     buf = '\x31\x06\x02\x01\x01\x02\x01\x02'
     dec = asn1.Decoder()
     dec.start(buf)
     tag = dec.peek()
     assert tag == (asn1.Set, asn1.TypeConstructed, asn1.ClassUniversal)
     dec.enter()
     tag, val = dec.read()
     assert val == 1
     tag, val = dec.read()
     assert val == 2
예제 #19
0
 def test_set(self):
     buf = b'\x31\x08\x02\x01\x01\x04\x03foo'
     dec = asn1.Decoder()
     dec.start(buf)
     tag = dec.peek()
     assert tag == (asn1.Numbers.Set, asn1.Types.Constructed, asn1.Classes.Universal)
     dec.enter()
     tag, val = dec.read()
     assert val == 1
     tag, val = dec.read()
     assert val == b'foo'
예제 #20
0
 def test_sequence(self):
     buf = '\x30\x08\x02\x01\x01\x04\x03foo'
     dec = asn1.Decoder()
     dec.start(buf)
     tag = dec.peek()
     assert tag == (asn1.Sequence, asn1.TypeConstructed,
                    asn1.ClassUniversal)
     dec.enter()
     tag, val = dec.read()
     assert val == 1
     tag, val = dec.read()
     assert val == 'foo'
예제 #21
0
def load_block(name: str, key_size=1024, data_size=800, nonce_size=200):
    with Controller.from_port() as controller:
        controller.authenticate()
        a = str(controller.get_hidden_service_descriptor(name))
        public = a[a.find('PUBLIC KEY-----')+15:a.find('-----END')].replace('\n', '')
        decoder = asn1.Decoder()
        decoder.start(base64.b64decode(public))
        decoder.start(decoder.read()[1])
        n = decoder.read()[1]
        logger.debug('Received public key with n = %s', hex(n))
        data = data_from_public_key(n, key_size=key_size, data_size=data_size, nonce_size=nonce_size)
        return data
예제 #22
0
def der_decode_privkey(DERbytes):
    decoder = asn1.Decoder()
    decoder.start(DERbytes)
    ensure_tag(decoder, asn1.Numbers.Sequence)
    decoder.enter()
    # privkey
    ensure_tag(decoder, asn1.Numbers.OctetString)
    _, privkey = decoder.read()
    privkey = _bebytes_to_int(privkey)
    curve = _der_decode_curve(decoder)
    decoder.leave()
    return (privkey, curve)
예제 #23
0
    def decode_asn1_value(value_to_decode):
        extension_value_asn1 = value_to_decode
        decoder = asn1.Decoder()

        decoder.start(extension_value_asn1)
        tag, once_decoded_value = decoder.read()

        decoder.start(once_decoded_value)
        tag, twice_decoded_value = decoder.read()

        utf8_value = twice_decoded_value.decode("utf-8")
        return utf8_value
예제 #24
0
 def find_keybags(self, input_stream):
     CURRENT_KEYBAG_TYPE = None
     while not input_stream.eof():  # loop until end of file
         tag = input_stream.peek()  # peek into asn1 tag
         if tag.typ == asn1.Types.Primitive:  # if type is Primitive
             tag, value = input_stream.read()
             valstr = self.value_to_string(value)
             if tag.nr == asn1.Numbers.Integer:
                 if value == 1:  # check INTEGER, 1 = prod or 2 = dev
                     CURRENT_KEYBAG_TYPE = "production"
                 elif value == 2:
                     CURRENT_KEYBAG_TYPE = "development"
                 # initialize key if it doesn't already exist in the dict
                 if CURRENT_KEYBAG_TYPE not in self._KBAGS.keys():
                     self._KBAGS[CURRENT_KEYBAG_TYPE] = ""
                 # iv
                 tag, value = input_stream.read()  # read one
                 valstr = self.value_to_string(value).replace(
                     '0xb', '').replace('\'',
                                        '')  # value to str and cleanup
                 if len(valstr) == 32:  # iv len
                     self._KBAGS[
                         CURRENT_KEYBAG_TYPE] += valstr  # append to keybag
                 # key
                 tag, value = input_stream.read()  # read one
                 valstr = self.value_to_string(value).replace(
                     '0xb', '').replace('\'',
                                        '')  # value to str and cleanup
                 if len(valstr) == 64:  # key len
                     self._KBAGS[
                         CURRENT_KEYBAG_TYPE] += valstr  # append to keybag
                 #print(self._KBAGS[CURRENT_KEYBAG_TYPE])
             else:  # any other prim. type other than Integer
                 if len(valstr) < 0x420:  # filter out the image payload
                     if tag.nr == asn1.Numbers.OctetString and len(
                             valstr) == 0xed:
                         keybagDecoder = asn1.Decoder()
                         keybagDecoder.start(value)
                         self.find_keybags(keybagDecoder)
                     else:
                         if ('IM4P' in valstr):
                             self._MAGIC = valstr
                         elif (valstr in [
                                 'ibss', 'ibec', 'ibot', 'dtre', 'illb',
                                 'logo', 'rlgo', 'rdsk'
                         ]):
                             self._TYPE = valstr
         elif tag.typ == asn1.Types.Constructed:  # if type if Constructed
             input_stream.enter()
             self.find_keybags(input_stream)
             input_stream.leave()
     return self._KBAGS
def decodeSign(data):

   
    #data = file.read()
    decoder = asn1.Decoder()
    decoder.start(data)
   # decoded_parameters = parse(decoder, decoded_parameters)
    decoder.enter()
    decoder.enter()
    decoder.enter()
    
    decoder.read() #b'\x80\x06\x07\00'
    decoder.read() #b'gostSignKey'

    decoder.enter()
    x_q = decoder.read()[1]
    y_q = decoder.read()[1]
    decoder.leave()

    decoder.enter()

    decoder.enter()
    p = decoder.read()[1]
    decoder.leave()

    decoder.enter()
    a = decoder.read()[1]
    b = decoder.read()[1]
    decoder.leave()

    decoder.enter()
    x_p = decoder.read()[1]
    y_p = decoder.read()[1]
    decoder.leave()

    q = decoder.read()[1]
    decoder.leave()

    decoder.enter()
    r = decoder.read()[1]
    s = decoder.read()[1]
    decoder.leave()
    decoder.leave()

   
    decoder.enter()
    decoder.leave()

    decoder.leave()
    decoder.leave()

    return  x_q, y_q, r, s #decoded_parameters[0], decoded_parameters[1], decoded_parameters[2]
예제 #26
0
def get_key_algorithm(public_key):
    """Given a public key, return the algorithm type it uses.
    Currently only supports EC and RSA."""
    key_data = '\n'.join(public_key.split('\n')[1:-1])
    key_bytes = base64.b64decode(key_data)
    decoder = asn1.Decoder()
    decoder.start(key_bytes)
    tag = decoder.peek()
    while tag.nr != asn1.Numbers.ObjectIdentifier:
        decoder.enter()
        tag = decoder.peek()
    _, oid = decoder.read()
    return OID_ALGORITHMS[oid]
예제 #27
0
def der_decode_pubkey(DERbytes):
    decoder = asn1.Decoder()
    decoder.start(DERbytes)
    ensure_tag(decoder, asn1.Numbers.Sequence)
    decoder.enter()
    # pubkey
    ensure_tag(decoder, asn1.Numbers.OctetString)
    _, pubbytes = decoder.read()
    curve = _der_decode_curve(decoder)
    # curve
    pubkey = ECPoint(curve, pubbytes)
    decoder.leave()
    return (pubkey, curve)
def get_pubk(clcert):
    clcert_decoder = asn1.Decoder()
    clcert_decoder.start(clcert)
    clcert_decoder.enter()  # Seq, 3 elem
    clcert_decoder.enter()  # Seq, 8 elem
    clcert_decoder.read()
    clcert_decoder.read()
    clcert_decoder.read()
    clcert_decoder.read()
    clcert_decoder.read()
    clcert_decoder.read()
    clcert_decoder.enter()
    clcert_decoder.enter()
    t, v = clcert_decoder.read()
    assert (v == '1.2.840.113549.1.1.1')  # rsaEncryption(PKCS #1)
    clcert_decoder.leave()
    t, v = clcert_decoder.read()
    rsa_decoder = asn1.Decoder()
    rsa_decoder.start(v[1:])
    rsa_decoder.enter()
    t, N = rsa_decoder.read()
    t, E = rsa_decoder.read()
    return (E, N)
예제 #29
0
 def test_long_object_identifier(self):
     dec = asn1.Decoder()
     buf = '\x06\x03\x8c\x1a\x03'
     dec.start(buf)
     tag, val = dec.read()
     assert val == '39.2.3'
     buf = '\x06\x02\x4f\x03'
     dec.start(buf)
     tag, val = dec.read()
     assert val == '1.39.3'
     buf = '\x06\x04\x2a\x92\xa7\x60'
     dec.start(buf)
     tag, val = dec.read()
     assert val == '1.2.300000'
예제 #30
0
 def test_big_negative_numbers(self):
     for v in \
     (
         -668929531791034950848739021124816874,
         -667441897913742713771034596334288035,
         -664674827807729028941298133900846368,
         -666811959353093594446621165172641478,
     ):
         encoder = asn1.Encoder()
         encoder.start()
         encoder.write(v, asn1.Numbers.Integer)
         encoded_bytes = encoder.output()
         decoder = asn1.Decoder()
         decoder.start(encoded_bytes)
         tag, value = decoder.read()
         assert value == v