Skip to content

Commit

Permalink
Merge pull request #6 from lilatomic/feature/more-keyutils-functions
Browse files Browse the repository at this point in the history
Include remaining keyutils functions
  • Loading branch information
lilatomic authored Nov 12, 2023
2 parents de617a9 + b370f64 commit 2db7358
Show file tree
Hide file tree
Showing 9 changed files with 993 additions and 139 deletions.
152 changes: 121 additions & 31 deletions keyutils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@

from __future__ import absolute_import

from typing import Optional, Union

from . import _keyutils


for k, v in _keyutils.constants.__dict__.items():
globals()[k] = v
del k, v


from errno import ( # noqa: F401,E402 , imported for reexport; TODO: better reexport
EACCES,
EDQUOT,
Expand All @@ -34,49 +35,65 @@
)


Error = _keyutils.error
KeyutilsError = _keyutils.error


def _handle_keyerror(err: Exception):
if err.args[0] == _keyutils.constants.ENOKEY:
return None
raise err


def add_key(desc, value, keyring, keyType=b"user"):
return _keyutils.add_key(keyType, desc, value, keyring)

def add_key(key, value, keyring, keyType=b"user"):
return _keyutils.add_key(keyType, key, value, keyring)

def add_ring(desc, keyring) -> Optional[int]:
return _keyutils.add_key(b"keyring", desc, None, keyring)

def request_key(key, keyring, keyType=b"user"):

def request_key(keyDesc, keyring, keyType=b"user", callout_info=None):
try:
return _keyutils.request_key(keyType, key, None, keyring)
except Error as err:
if err.args[0] == _keyutils.constants.ENOKEY:
return None
raise
return _keyutils.request_key(keyType, keyDesc, callout_info, keyring)
except KeyutilsError as err:
return _handle_keyerror(err)


def search(keyring, description, destination=0, keyType=b"user"):
def get_keyring_id(key, create: bool):
try:
return _keyutils.search(keyring, keyType, description, destination)
except Error as err:
if err.args[0] == _keyutils.constants.ENOKEY:
return None
raise
return _keyutils.get_keyring_id(key, create)
except KeyutilsError as err:
return _handle_keyerror(err)


def join_session_keyring(name=None):
return _keyutils.join_session_keyring(name)


def update_key(key, value):
return _keyutils.update_key(key, value)


def read_key(keyId):
return _keyutils.read_key(keyId)
def revoke(key):
return _keyutils.revoke(key)


def describe_key(keyId):
return _keyutils.describe_key(keyId)
def chown(key, uid: Union[int, None], gid: Union[int, None]) -> None:
if uid is None:
uid = -1
if gid is None:
gid = -1
# TODO: map other errors
return _keyutils.chown(key, uid, gid)


def join_session_keyring(name=None):
return _keyutils.join_session_keyring(name)
def set_perm(key, perm):
return _keyutils.set_perm(key, perm)


def session_to_parent():
return _keyutils.session_to_parent()
def clear(keyring):
"""Clear the keyring."""
return _keyutils.clear(keyring)


def link(key, keyring):
Expand All @@ -87,19 +104,92 @@ def unlink(key, keyring):
return _keyutils.unlink(key, keyring)


def revoke(key):
return _keyutils.revoke(key)
def search(keyring, description, destination=0, keyType=b"user"):
try:
return _keyutils.search(keyring, keyType, description, destination)
except KeyutilsError as err:
return _handle_keyerror(err)


instantiate = _keyutils.instantiate


def negate(key, keyring, timeout=0):
return _keyutils.negate(key, timeout, keyring)


def set_timeout(key, timeout):
"""Set timeout in seconds (int)."""
return _keyutils.set_timeout(key, timeout)


def set_perm(key, perm):
return _keyutils.set_perm(key, perm)
def assume_authority(key):
return _keyutils.assume_authority(key)


def clear(keyring):
"""Clear the keyring."""
return _keyutils.clear(keyring)
def session_to_parent():
return _keyutils.session_to_parent()


def reject(key, keyring, error, timeout=0):
return _keyutils.reject(key, timeout, error, keyring)


def invalidate(key):
return _keyutils.invalidate(key)


def get_persistent(uid, key):
return _keyutils.get_persistent(uid, key)


def dh_compute(key_priv, key_prime, key_base):
return _keyutils.dh_compute(key_priv, key_prime, key_base)


def dh_compute_kdf(key_priv, key_prime, key_base, hashname, buflen, otherinfo=None):
return _keyutils.dh_compute_kdf(key_priv, key_prime, key_base, hashname, buflen, otherinfo)


def restrict_keyring(keyring, key_type, restriction):
return _keyutils.restrict_keyring(keyring, key_type, restriction)


def pkey_query(key, info: bytes = b''):
return _keyutils.pkey_query(key, info)


def pkey_encrypt(key, data: bytes, info: bytes = b'') -> bytes:
return _keyutils.pkey_encrypt(key, info, data)


def pkey_decrypt(key, enc: bytes, info: bytes = b'') -> bytes:
return _keyutils.pkey_decrypt(key, info, enc)


def pkey_sign(key, data: bytes, info: bytes = b'') -> bytes:
return _keyutils.pkey_sign(key, info, data)


def pkey_verify(key, data: bytes, sig: bytes, info: bytes = b'') -> bytes:
return _keyutils.pkey_verify(key, info, data, sig)


def move(key, from_ringid, to_ringid, flags=0):
return _keyutils.move(key, from_ringid, to_ringid, flags)


def capabilities() -> bytes:
return _keyutils.capabilities()


def describe_key(keyId):
return _keyutils.describe_key(keyId)


def read_key(keyId):
return _keyutils.read_key(keyId)


def get_security(keyid):
return _keyutils.get_security(keyid)
Loading

0 comments on commit 2db7358

Please sign in to comment.