def der_read(der_data, expected_t=None): t = byte2int(der_data[0]) if expected_t is not None and expected_t != t: raise ValueError('Wrong tag. Expected: %x, got: %x' % (expected_t, t)) l = byte2int(der_data[1]) offs = 2 if l > 0x80: n_bytes = l - 0x80 l = b2len(der_data[offs:offs + n_bytes]) offs = offs + n_bytes v = der_data[offs:offs + l] rest = der_data[offs + l:] if expected_t is None: return t, v, rest return v, rest
def set_flag(data, flagkey, flagmask, value=True): flags = byte2int(data.get(flagkey, b'\0')[0]) if value: flags |= flagmask else: flags &= ~flagmask data[flagkey] = int2byte(flags)
def __init__(self, key): self._key = key self._authenticated = False try: self._raw_data = self._key.fetch_object(YKPIV_OBJ_PIVMAN_DATA) # TODO: Remove in a few versions... if byte2int(self._raw_data[0]) != TAG_PIVMAN_DATA: self._data = {} self._data[TAG_PIN_TIMESTAMP] = self._raw_data self._data[TAG_SALT] = self._key.fetch_object( YKPIV_OBJ_PIVMAN_DATA + 1) else: # END legacy stuff self._data = parse_pivtool_data(self._raw_data) except PivError: self._raw_data = serialize_pivtool_data({}) self._data = {}
def b2len(bs): l = 0 for b in bs: l *= 256 l += byte2int(b) return l
def has_flag(data, flagkey, flagmask): flags = byte2int(data.get(flagkey, b'\0')[0]) return bool(flags & flagmask)