File: uid.py

package info (click to toggle)
py-rnp 0.1.0%2Bgit20221014.01b7129-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 424 kB
  • sloc: python: 3,062; sh: 8; makefile: 4
file content (77 lines) | stat: -rw-r--r-- 1,904 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from ctypes import (
    c_size_t,
    c_uint8,
    c_uint32,
    c_bool,
    c_void_p,
    POINTER,
    cast,
    byref,
    addressof,
)

from .lib import _lib


class UID:
    RNP_USER_ID = 1
    RNP_USER_ATTR = 2

    def __init__(self, obj):
        self._obj = obj

    def __del__(self):
        _lib.rnp_uid_handle_destroy(self._obj)

    def obj(self):
        return self._obj

    def type(self):
        uidtype = c_uint32()
        _lib.rnp_uid_get_type(self._obj, byref(uidtype))
        return uidtype.value

    def data(self):
        buf = c_void_p()
        buf_size = c_size_t()
        try:
            _lib.rnp_uid_get_data(self._obj, byref(buf), byref(buf_size))
            buf = cast(buf, POINTER(c_uint8))
            return bytes(
                (c_uint8 * buf_size.value).from_address(addressof(buf.contents))
            )
        finally:
            _lib.rnp_buffer_destroy(buf)

    def is_primary(self):
        primary = c_bool()
        _lib.rnp_uid_is_primary(self._obj, byref(primary))
        return primary.value

    def is_valid(self):
        valid = c_bool()
        _lib.rnp_uid_is_valid(self._obj, byref(valid))
        return valid.value

    def is_revoked(self):
        revoked = c_bool()
        _lib.rnp_uid_is_revoked(self._obj, byref(revoked))
        return revoked.value

    def revocation_signature(self):
        from .signature import Signature

        psig = c_void_p()
        _lib.rnp_uid_get_revocation_signature(self._obj, byref(psig))
        if psig.value:
            return Signature(psig.value)

    def signatures(self):
        from .signature import Signature

        count = c_size_t()
        _lib.rnp_uid_get_signature_count(self._obj, byref(count))
        for i in range(count.value):
            psig = c_void_p()
            _lib.rnp_uid_get_signature_at(self._obj, i, byref(psig))
            yield Signature(psig.value)