|
| 1 | +GSSAPI="BASE" # This ensures that a full module is generated by Cython |
| 2 | + |
| 3 | +from gssapi.raw.cython_types cimport * |
| 4 | +from gssapi.raw.sec_contexts cimport SecurityContext |
| 5 | +from gssapi.raw.ext_dce cimport IOV, gss_iov_buffer_desc |
| 6 | + |
| 7 | +from gssapi.raw.misc import GSSError, _EnumExtension |
| 8 | +from gssapi.raw import ext_dce |
| 9 | + |
| 10 | +import six |
| 11 | + |
| 12 | +cdef extern from "python_gssapi_ext.h": |
| 13 | + OM_uint32 gss_get_mic_iov(OM_uint32 *min_stat, gss_ctx_id_t context_handle, |
| 14 | + gss_qop_t qop_req, gss_iov_buffer_desc *iov, |
| 15 | + int iov_count) nogil |
| 16 | + |
| 17 | + OM_uint32 gss_get_mic_iov_length(OM_uint32 *min_stat, |
| 18 | + gss_ctx_id_t context_handle, |
| 19 | + gss_qop_t qop_req, |
| 20 | + gss_iov_buffer_desc *iov, |
| 21 | + int iov_count) nogil |
| 22 | + |
| 23 | + OM_uint32 gss_verify_mic_iov(OM_uint32 *min_stat, |
| 24 | + gss_ctx_id_t context_handle, |
| 25 | + gss_qop_t *qop_state, |
| 26 | + gss_iov_buffer_desc *iov, |
| 27 | + int iov_count) nogil |
| 28 | + |
| 29 | + OM_uint32 GSS_IOV_BUFFER_TYPE_MIC_TOKEN |
| 30 | + |
| 31 | + |
| 32 | +@six.add_metaclass(_EnumExtension) |
| 33 | +class IOVBufferType(object): |
| 34 | + __base__ = ext_dce.IOVBufferType |
| 35 | + mic_token = GSS_IOV_BUFFER_TYPE_MIC_TOKEN |
| 36 | + |
| 37 | + |
| 38 | +IOV.AUTO_ALLOC_BUFFERS.add(IOVBufferType.mic_token) |
| 39 | + |
| 40 | + |
| 41 | +def get_mic_iov(SecurityContext context not None, IOV message not None, |
| 42 | + qop=None): |
| 43 | + """ |
| 44 | + Generate MIC tokens for the given IOV message |
| 45 | +
|
| 46 | + This method generates a MIC token for the given IOV message, and places it |
| 47 | + in the :attr:`IOVBufferType.mic_token` buffer in the IOV. This method |
| 48 | + operates entirely in-place, and returns nothing. |
| 49 | +
|
| 50 | + Args: |
| 51 | + context (SecurityContext): the current security context |
| 52 | + message (list): a list of :class:`IOVBuffer` objects |
| 53 | + qop (int): the desired Quality of Protection |
| 54 | + (or None for the default QoP) |
| 55 | +
|
| 56 | + Raises: |
| 57 | + GSSError |
| 58 | + """ |
| 59 | + |
| 60 | + cdef gss_qop_t qop_req = qop if qop is not None else GSS_C_QOP_DEFAULT |
| 61 | + |
| 62 | + cdef gss_iov_buffer_desc *res_arr = message.__cvalue__() |
| 63 | + |
| 64 | + cdef OM_uint32 maj_stat, min_stat |
| 65 | + |
| 66 | + with nogil: |
| 67 | + maj_stat = gss_get_mic_iov(&min_stat, context.raw_ctx, qop_req, |
| 68 | + res_arr, message.iov_len) |
| 69 | + |
| 70 | + if maj_stat == GSS_S_COMPLETE: |
| 71 | + message.c_changed = True |
| 72 | + return |
| 73 | + else: |
| 74 | + raise GSSError(maj_stat, min_stat) |
| 75 | + |
| 76 | + |
| 77 | +def get_mic_iov_length(SecurityContext context not None, IOV message not None, |
| 78 | + qop=None): |
| 79 | + """ |
| 80 | + Allocate space for the MIC buffer in the given IOV message |
| 81 | +
|
| 82 | + This method allocates space for the MIC token buffer |
| 83 | + (:attr:`IOVBufferType.mic_token`) in the given IOV message. |
| 84 | +
|
| 85 | + Args: |
| 86 | + context (SecurityContext): the current security context |
| 87 | + message (list): a list of :class:`IOVBuffer` objects |
| 88 | + qop (int): the desired Quality of Protection |
| 89 | + (or None for the default QoP) |
| 90 | +
|
| 91 | + Raises: |
| 92 | + GSSError |
| 93 | + """ |
| 94 | + |
| 95 | + cdef gss_qop_t qop_req = qop if qop is not None else GSS_C_QOP_DEFAULT |
| 96 | + |
| 97 | + cdef gss_iov_buffer_desc *res_arr = message.__cvalue__() |
| 98 | + |
| 99 | + cdef OM_uint32 maj_stat, min_stat |
| 100 | + |
| 101 | + with nogil: |
| 102 | + maj_stat = gss_get_mic_iov_length(&min_stat, context.raw_ctx, qop_req, |
| 103 | + res_arr, message.iov_len) |
| 104 | + |
| 105 | + if maj_stat == GSS_S_COMPLETE: |
| 106 | + message.c_changed = True |
| 107 | + return |
| 108 | + else: |
| 109 | + raise GSSError(maj_stat, min_stat) |
| 110 | + |
| 111 | + |
| 112 | +def verify_mic_iov(SecurityContext context not None, IOV message not None, |
| 113 | + qop=None): |
| 114 | + """ |
| 115 | + Verify that the MIC matches the data in the given IOV message |
| 116 | +
|
| 117 | + This method verifies that the MIC token in the MIC buffer |
| 118 | + (:attr:`IOVBufferType.mic_token`) match the data buffer(s) |
| 119 | + in the given IOV method. |
| 120 | +
|
| 121 | + Args: |
| 122 | + context (SecurityContext): the current security context |
| 123 | + message (list): a list of :class:`IOVBuffer` objects |
| 124 | +
|
| 125 | + Returns: |
| 126 | + int: the QoP used to generate the MIC token |
| 127 | +
|
| 128 | + Raises: |
| 129 | + GSSError |
| 130 | + """ |
| 131 | + |
| 132 | + cdef gss_iov_buffer_desc *res_arr = message.__cvalue__() |
| 133 | + |
| 134 | + cdef gss_qop_t qop_state |
| 135 | + |
| 136 | + cdef OM_uint32 maj_stat, min_stat |
| 137 | + |
| 138 | + with nogil: |
| 139 | + maj_stat = gss_verify_mic_iov(&min_stat, context.raw_ctx, &qop_state, |
| 140 | + res_arr, message.iov_len) |
| 141 | + |
| 142 | + if maj_stat == GSS_S_COMPLETE: |
| 143 | + return qop_state |
| 144 | + else: |
| 145 | + raise GSSError(maj_stat, min_stat) |
0 commit comments