예제 #1
0
def load_teos_id(teos_pk_path):
    """
    Loads the tower id from disk.

    Args:
        teos_pk_path (:obj:`str`): path to the tower's public key file.

    Returns:
        :obj:`str`: The tower id.

    Raises:
        :obj:`InvalidKey`: if the public key is invalid or cannot be loaded.
    """

    if not teos_pk_path:
        raise InvalidKey(
            "TEOS's public key file not found. Have you registered with the tower?"
        )

    try:
        teos_id = Cryptographer.get_compressed_pk(
            PublicKey(Cryptographer.load_key_file(teos_pk_path)))

    except (InvalidParameter, InvalidKey, ValueError):
        raise InvalidKey(
            "TEOS public key cannot be loaded. Try registering again")

    return teos_id
예제 #2
0
def load_keys(user_sk_path):
    """
    Loads all the user private key and id.

    Args:
        user_sk_path (:obj:`str`): path to the user's private key file.

    Returns:
        :obj:`tuple`: A tuple containing a :obj:`PrivateKey` and a :obj:`str` representing the user sk and user id
        (compressed pk) respectively.

    Raises:
        :obj:`InvalidKey`: if any of the keys is invalid or cannot be loaded.
    """

    if not user_sk_path:
        raise InvalidKey(
            "Client's private key file not found. Please check your settings")

    try:
        user_sk_der = Cryptographer.load_key_file(user_sk_path)
        user_sk = Cryptographer.load_private_key_der(user_sk_der)

    except (InvalidParameter, InvalidKey):
        raise InvalidKey("Client private key is invalid or cannot be parsed")

    try:
        user_id = Cryptographer.get_compressed_pk(user_sk.public_key)

    except (InvalidParameter, InvalidKey):
        raise InvalidKey("Client public key cannot be loaded")

    return user_sk, user_id
예제 #3
0
def load_keys(data_dir):
    """
    Loads a the client key pair.

    Args:
        data_dir (:obj:`str`): path to data directory where the keys are stored.

    Returns:
        :obj:`tuple`: a tuple containing a ``PrivateKey`` and a ``str`` representing the client sk and compressed pk
        respectively.

    Raises:
        :obj:`InvalidKey <cli.exceptions.InvalidKey>`: if any of the keys is invalid or cannot be loaded.
    """

    if not isinstance(data_dir, str):
        raise ValueError("Invalid data_dir. Please check your settings")

    sk_file_path = os.path.join(data_dir, "sk.der")

    cli_sk_der = Cryptographer.load_key_file(sk_file_path)
    cli_sk = Cryptographer.load_private_key_der(cli_sk_der)

    if cli_sk is None:
        raise InvalidKey("Client private key is invalid or cannot be parsed")

    compressed_cli_pk = Cryptographer.get_compressed_pk(cli_sk.public_key)

    if compressed_cli_pk is None:
        raise InvalidKey("Client public key cannot be loaded")

    return cli_sk, compressed_cli_pk
예제 #4
0
    def load_private_key_der(sk_der):
        """
        Creates a :obj:`PrivateKey` from a given ``DER`` encoded private key.

        Args:
             sk_der(:obj:`str`): a private key encoded in ``DER`` format.

        Returns:
             :obj:`PrivateKey`: A ``PrivateKey`` object if the private key can be loaded.

        Raises:
            :obj:`InvalidKey`: if a ``PrivateKey`` cannot be loaded from the given data.
        """

        try:
            sk = PrivateKey.from_der(sk_der)
            return sk

        except ValueError:
            raise InvalidKey(
                "The provided key data cannot be deserialized (wrong size or format)"
            )

        except TypeError:
            raise InvalidKey(
                "The provided key data cannot be deserialized (wrong type)")
예제 #5
0
    def get_compressed_pk(pk):
        """
        Computes a compressed, hex-encoded, public key given a ``PublicKey``.

        Args:
            pk(:obj:`PublicKey`): a given public key.

        Returns:
            :obj:`str`: A compressed, hex-encoded, public key (33-byte long) if it can be compressed.

        Raises:
             :obj:`InvalidParameter`: if the value passed as public key is not a PublicKey object.
             :obj:`InvalidKey`: if the public key has not been properly created.
        """

        if not isinstance(pk, PublicKey):
            raise InvalidParameter(
                "Wrong value passed as pk. Received {}, expected (PublicKey)".
                format(type(pk)))

        try:
            compressed_pk = pk.format(compressed=True)
            return hexlify(compressed_pk).decode("utf-8")

        except TypeError as e:
            raise InvalidKey("PublicKey has invalid initializer", error=str(e))
예제 #6
0
    def load_key_file(file_path):
        """
        Loads a key from a key file.

        Args:
            file_path (:obj:`str`): the path to the key file to be loaded.

        Returns:
            :obj:`bytes`: the key file data if the file can be found and read.

        Raises:
             :obj:`InvalidParameter`: if the file_path has wrong format or cannot be found.
             :obj:`InvalidKey`: if the key cannot be loaded from the file. It covers temporary I/O errors.
        """

        if not isinstance(file_path, str):
            raise InvalidParameter(
                "Key file path was expected, {} received".format(
                    type(file_path)))

        try:
            with open(file_path, "rb") as key_file:
                key = key_file.read()
            return key

        except FileNotFoundError:
            raise InvalidParameter(
                "Key file not found at {}. Please check your settings".format(
                    file_path))

        except IOError as e:
            raise InvalidKey("Key file cannot be loaded", exception=e)
예제 #7
0
def load_keys(teos_pk_path, user_sk_path):
    """
    Loads all the keys required so sign, send, and verify the appointment.

    Args:
        teos_pk_path (:obj:`str`): path to the tower's public key file.
        user_sk_path (:obj:`str`): path to the user's private key file.

    Returns:
        :obj:`tuple`: a three-item tuple containing a ``str``, a ``PrivateKey`` and a ``str``
        representing the tower id (compressed pk), user sk and user id (compressed pk) respectively.

    Raises:
        :obj:`InvalidKey <cli.exceptions.InvalidKey>`: if any of the keys is invalid or cannot be loaded.
    """

    if not teos_pk_path:
        raise InvalidKey(
            "TEOS's public key file not found. Please check your settings")

    if not user_sk_path:
        raise InvalidKey(
            "Client's private key file not found. Please check your settings")

    try:
        teos_pk_der = Cryptographer.load_key_file(teos_pk_path)
        teos_id = Cryptographer.get_compressed_pk(PublicKey(teos_pk_der))

    except (InvalidParameter, InvalidKey, ValueError):
        raise InvalidKey("TEOS public key cannot be loaded")

    try:
        user_sk_der = Cryptographer.load_key_file(user_sk_path)
        user_sk = Cryptographer.load_private_key_der(user_sk_der)

    except (InvalidParameter, InvalidKey):
        raise InvalidKey("Client private key is invalid or cannot be parsed")

    try:
        user_id = Cryptographer.get_compressed_pk(user_sk.public_key)

    except (InvalidParameter, InvalidKey):
        raise InvalidKey("Client public key cannot be loaded")

    return teos_id, user_sk, user_id