File: ext_rfc5587.pyx

package info (click to toggle)
python-gssapi 1.10.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 876 kB
  • sloc: python: 3,707; sh: 198; makefile: 154; ansic: 60
file content (106 lines) | stat: -rw-r--r-- 3,608 bytes parent folder | download | duplicates (3)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
from gssapi.raw.cython_types cimport *
from gssapi.raw.oids cimport OID
from gssapi.raw.cython_converters cimport c_create_oid_set
GSSAPI="BASE"  # This ensures that a full module is generated by Cython

from gssapi.raw.cython_converters cimport c_get_mech_oid_set

from gssapi.raw.named_tuples import InquireAttrsResult, DisplayAttrResult
from gssapi.raw.misc import GSSError

cdef extern from "python_gssapi_ext.h":
    OM_uint32 gss_indicate_mechs_by_attrs(
        OM_uint32 *minor_status,
        const gss_OID_set desired_mech_attrs,
        const gss_OID_set except_mech_attrs,
        const gss_OID_set critical_mech_attrs,
        gss_OID_set *mechs) nogil

    OM_uint32 gss_inquire_attrs_for_mech(
        OM_uint32 *minor_status,
        const gss_OID mech,
        gss_OID_set *mech_attrs,
        gss_OID_set *known_mech_attrs) nogil

    OM_uint32 gss_display_mech_attr(
        OM_uint32 *minor_status,
        const gss_OID mech_attr,
        gss_buffer_t name,
        gss_buffer_t short_desc,
        gss_buffer_t long_desc) nogil


def indicate_mechs_by_attrs(desired_mech_attrs=None, except_mech_attrs=None,
                            critical_mech_attrs=None):
    cdef OM_uint32 maj_stat, min_stat
    cdef gss_OID_set desired_attrs = GSS_C_NO_OID_SET
    cdef gss_OID_set except_attrs = GSS_C_NO_OID_SET
    cdef gss_OID_set critical_attrs = GSS_C_NO_OID_SET
    cdef gss_OID_set mechs

    if desired_mech_attrs is not None:
        desired_attrs = c_get_mech_oid_set(desired_mech_attrs)

    if except_mech_attrs is not None:
        except_attrs = c_get_mech_oid_set(except_mech_attrs)

    if critical_mech_attrs is not None:
        critical_attrs = c_get_mech_oid_set(critical_mech_attrs)

    with nogil:
        maj_stat = gss_indicate_mechs_by_attrs(&min_stat, desired_attrs,
                                               except_attrs, critical_attrs,
                                               &mechs)

    if maj_stat == GSS_S_COMPLETE:
        return c_create_oid_set(mechs)
    else:
        raise GSSError(maj_stat, min_stat)


def inquire_attrs_for_mech(OID mech):
    cdef OM_uint32 maj_stat, min_stat
    cdef gss_OID m = GSS_C_NO_OID
    cdef gss_OID_set mech_attrs = GSS_C_NO_OID_SET
    cdef gss_OID_set known_mech_attrs = GSS_C_NO_OID_SET

    if mech is not None:
        m = &mech.raw_oid

    with nogil:
        maj_stat = gss_inquire_attrs_for_mech(&min_stat, m, &mech_attrs,
                                              &known_mech_attrs)

    if maj_stat == GSS_S_COMPLETE:
        return InquireAttrsResult(c_create_oid_set(mech_attrs),
                                  c_create_oid_set(known_mech_attrs))
    else:
        raise GSSError(maj_stat, min_stat)


def display_mech_attr(OID attr):
    cdef OM_uint32 maj_stat, min_stat
    cdef gss_OID a = GSS_C_NO_OID
    cdef gss_buffer_desc name
    cdef gss_buffer_desc short_desc
    cdef gss_buffer_desc long_desc

    if attr is not None:
        a = &attr.raw_oid

    with nogil:
        maj_stat = gss_display_mech_attr(&min_stat, a, &name, &short_desc,
                                         &long_desc)

    if maj_stat == GSS_S_COMPLETE:
        out_name = (<char*>name.value)[:name.length]
        out_short = (<char*>short_desc.value)[:short_desc.length]
        out_long = (<char*>long_desc.value)[:long_desc.length]

        gss_release_buffer(&min_stat, &name)
        gss_release_buffer(&min_stat, &short_desc)
        gss_release_buffer(&min_stat, &long_desc)

        return DisplayAttrResult(out_name, out_short, out_long)
    else:
        raise GSSError(maj_stat, min_stat)