Exemplo n.º 1
0
    def test_auto_c_array_string(self, str_val):
        """
        Initialize an array from string.
        :param str_val: randomly generated string
        """
        c_array = AutoCArray(data=str_val)

        assert c_array.size.contents.value == len(c_array) == len(str_val)
        assert c_array.ctype == c_ubyte
        assert b"".join(c_array) == b(str_val)
Exemplo n.º 2
0
    def test_auto_c_array_byte_list(self, list_val):
        """
        Initalize an array from list of bytes.
        :param list_val: list of ints to be converted to c_byte's
        """
        list_val = [c_byte(x) for x in list_val]
        c_array = AutoCArray(data=list_val, ctype=c_byte)

        assert c_array.size.contents.value == len(c_array) == len(list_val)
        assert c_array.ctype == c_byte
        assert b"".join([bytes(c_byte(x)) for x in c_array]) == b"".join([bytes(x) for x in list_val])
        assert c_array.array[0] == cast(c_array.array, POINTER(c_byte)).contents.value
Exemplo n.º 3
0
def ca_destroy_multiple_objects(h_session, objects):
    """Delete multiple objects corresponding to given object handles

    :param int h_session: Session handle
    :param list objects: The handles of the objects to delete
    :returns: Return code
    """
    handles_count = len(objects)
    handles = AutoCArray(data=objects, ctype=CK_ULONG)
    ret = CA_DestroyMultipleObjects(h_session, handles_count, handles.array,
                                    byref(CK_ULONG()))
    return ret
Exemplo n.º 4
0
    def test_auto_c_array_no_type_fail(self, list_val, test_type):
        """
        Attempt to initialize an array of 'test_type' without specifying the type. Should error
        :param list_val: Generated list, convert to 'test_type'
        :param test_type: c_types to test with
        """
        if test_type == c_char:
            new_list = [c_char(b(chr(x))) for x in list_val]
        else:
            new_list = [test_type(x) for x in list_val]

        with pytest.raises(TypeError):
            c_array = AutoCArray(data=new_list)
Exemplo n.º 5
0
    def test_auto_c_array_char_list(self, list_val):
        """
        Initalize an array from list of c_chars
        :param list_val: list of char to be converted to c_char's
        """
        list_val = [bytes(b(x)) for x in list_val]
        new_list_val = [c_char(x) for x in list_val]
        c_array = AutoCArray(data=new_list_val, ctype=c_char)

        assert c_array.size.contents.value == len(c_array) == len(list_val)
        assert c_array.ctype == c_char
        assert b"".join([x for x in c_array]) == b"".join(list_val)
        assert c_array.array[0] == cast(c_array.array, POINTER(c_char)).contents.value
Exemplo n.º 6
0
def ca_bip32_export_public_key(session, key):
    """
    Exports a BIP32 Key from the HSM.

    :param int session: Session handle.
    :return: retcode, key data (base58 encoded bytestring)
    """
    c_key_data = AutoCArray(ctype=CK_BYTE, size=CK_ULONG(CKG_BIP32_MAX_SERIALIZED_LEN))
    ret = CA_Bip32ExportPublicKey(session, key, c_key_data.array, c_key_data.size)
    if ret != CKR_OK:
        return ret, None

    return ret, string_at(c_key_data.array, c_key_data.size.contents.value)
Exemplo n.º 7
0
    def test_auto_c_array_empty(self, typ_val):
        """
        Initialize an empty array w/ elements of the given c_type.
        :param typ_val: randomly selected ctype
        """
        c_array = AutoCArray(ctype=typ_val)

        assert c_array.array is None
        assert c_array.size.contents.value == len(c_array) == 0
        assert c_array.ctype == typ_val

        if typ_val == c_char:
            assert c_array.array.contents.value == typ_val(b'\x00').value
        else:
            assert c_array.array.contents.value == typ_val(0).value
Exemplo n.º 8
0
def ca_bip32_import_public_key(session, key_data, attributes):
    """
    Imports a BIP32 Key to the HSM.

    :param int session: Session handle.
    :param bytes key_data: Key data, in bytes (base58 encoded)
    :param dict attributes: Attributes for the key.
    :return: retcode, key_handle.
    """
    attrs = Attributes(attributes).get_c_struct()
    c_key_data = AutoCArray(ctype=CK_BYTE, data=key_data)
    key_handle = CK_ULONG()
    ret = CA_Bip32ImportPublicKey(
        session, c_key_data.array, len(c_key_data), attrs, len(attributes), byref(key_handle)
    )
    if ret != CKR_OK:
        return ret, None

    return ret, key_handle.value
Exemplo n.º 9
0
def ca_derive_key_and_wrap(h_session,
                           derive_mechanism,
                           h_base_key,
                           derive_template,
                           wrapping_key,
                           wrap_mechanism,
                           output_buffer=2048):
    """
    Derive a key from the base key and wrap it off the HSM using the wrapping key

    :param int h_session: The session to use
    :param int h_base_key: The base key
    :param dict derive_template: A python template of attributes to set on derived key
    :param derive_mechanism: See the :py:func:`~pycryptoki.mechanism.parse_mechanism` function
        for possible values.
    :param int wrapping_key: The wrapping key based on the encryption flavor
    :param wrap_mechanism: See the :py:func:`~pycryptoki.mechanism.parse_mechanism` function
        for possible values.
    :param output_buffer: The size of the wrapped key, defaulted to a cert size
    :returns: (Retcode, python bytestring representing wrapped key)
    :rtype: tuple
    """
    # derive key parameters preparation
    derive_mech = parse_mechanism(derive_mechanism)
    c_template = Attributes(derive_template).get_c_struct()
    # wrapping key parameter preparation
    wrap_mech = parse_mechanism(wrap_mechanism)
    # derive key and wrap function requires the size and in that way is
    # inconsistent with wrap function
    size = CK_ULONG(output_buffer)
    wrapped_key = AutoCArray(ctype=c_ubyte, size=size)

    ret = CA_DeriveKeyAndWrap(h_session, derive_mech,
                              CK_OBJECT_HANDLE(h_base_key), c_template,
                              CK_ULONG(len(derive_template)), wrap_mech,
                              CK_OBJECT_HANDLE(wrapping_key),
                              wrapped_key.array, wrapped_key.size)

    if ret != CKR_OK:
        return ret, None

    return ret, string_at(wrapped_key.array, wrapped_key.size.contents.value)