codekingpro/portable-devtools
114k
1import typing as t2 3if t.TYPE_CHECKING:4 from gssapi.raw.named_tuples import WrapResult, UnwrapResult5 from gssapi.sec_contexts import SecurityContext6 7 8def get_mic(9 context: "SecurityContext",10 message: bytes,11 qop: t.Optional[int] = None,12) -> bytes:13 """Generate a MIC for a message.14 15 This method generates a Message Integrity Check token for the16 given message. This can be separately trasmitted to the other17 entity, unlike wrap, which bundles the MIC and the message18 together.19 20 Args:21 context (~gssapi.raw.sec_contexts.SecurityContext): the current22 security context23 message (bytes): the message for which to generate the MIC24 qop (int): the requested Quality of Protection25 (or None to use the default)26 27 Returns:28 bytes: the generated MIC token29 30 Raises:31 ~gssapi.exceptions.ExpiredContextError32 ~gssapi.exceptions.MissingContextError33 ~gssapi.exceptions.BadQoPError34 """35 36 37def verify_mic(38 context: "SecurityContext",39 message: bytes,40 token: bytes,41) -> int:42 """Verify that a MIC matches a message.43 44 This method verifies that the given MIC matches the given message.45 If the MIC does not match the given message, an exception will46 be raised.47 48 Args:49 context (~gssapi.raw.sec_contexts.SecurityContext): the current50 security context51 message (bytes): the message in question52 token (bytes): the MIC token in question53 54 Returns:55 int: the QoP used.56 57 Raises:58 ~gssapi.exceptions.InvalidTokenError59 ~gssapi.exceptions.BadMICError60 ~gssapi.exceptions.DuplicateTokenError61 ~gssapi.exceptions.ExpiredTokenError62 ~gssapi.exceptions.TokenTooLateError63 ~gssapi.exceptions.TokenTooEarlyError64 ~gssapi.exceptions.ExpiredContextError65 ~gssapi.exceptions.MissingContextError66 """67 68 69def wrap_size_limit(70 context: "SecurityContext",71 output_size: int,72 confidential: bool = True,73 qop: t.Optional[int] = None,74) -> int:75 """Calculate the max message size.76 77 This method calculates the unwrapped/unencrypted message size for78 the given maximum wrapped/encrypted message size.79 80 Args:81 context (~gssapi.raw.sec_contexts.SecurityContext): the current82 security context83 output_size (int): the maximum desired wrapped/encrypted message size84 confidential (bool): whether or not confidentiality is being used85 qop (int): the QoP that will be when you actually call wrap86 (or None for the default QoP)87 88 Returns:89 int: the maximum unencrypted/unwrapped message size90 91 Raises:92 ~gssapi.exceptions.MissingContextError93 ~gssapi.exceptions.ExpiredContextError94 ~gssapi.exceptions.BadQoPError95 """96 97 98def wrap(99 context: "SecurityContext",100 message: bytes,101 confidential: bool = True,102 qop: t.Optional[int] = None,103) -> "WrapResult":104 """Wrap/Encrypt a message.105 106 This method wraps or encrypts a message (depending on the value107 of confidential) with the given Quality of Protection.108 109 Args:110 context (~gssapi.raw.sec_contexts.SecurityContext): the current111 security context112 message (bytes): the message to wrap or encrypt113 confidential (bool): whether or not to encrypt the message (True),114 or just wrap it with a MIC (False)115 qop (int): the desired Quality of Protection116 (or None for the default QoP)117 118 Returns:119 WrapResult: the wrapped/encrypted message, and whether or not120 encryption was actually used121 122 Raises:123 ~gssapi.exceptions.ExpiredContextError124 ~gssapi.exceptions.MissingContextError125 ~gssapi.exceptions.BadQoPError126 """127 128 129def unwrap(130 context: "SecurityContext",131 message: bytes,132) -> "UnwrapResult":133 """Unwrap/Decrypt a message.134 135 This method unwraps or decrypts a message, depending136 on whether the sender used confidentiality.137 138 Args:139 context (~gssapi.raw.sec_contexts.SecurityContext): the current140 security context141 message (bytes): the message to unwrap/decrypt142 143 Returns:144 UnwrapResult: the unwrapped/decrypted message, whether or on145 encryption was used, and the QoP used146 147 Raises:148 ~gssapi.exceptions.InvalidTokenError149 ~gssapi.exceptions.BadMICError150 ~gssapi.exceptions.DuplicateTokenError151 ~gssapi.exceptions.ExpiredTokenError152 ~gssapi.exceptions.TokenTooLateError153 ~gssapi.exceptions.TokenTooEarlyError154 ~gssapi.exceptions.ExpiredContextError155 ~gssapi.exceptions.MissingContextError156 """157 