| 1 |
# Copyright: (c) 2020, Jordan Borean (@jborean93) <[email protected]> |
| 2 |
# MIT License (see LICENSE or https://opensource.org/licenses/MIT) |
| 3 |
|
| 4 |
import typing |
| 5 |
|
| 6 |
from spnego._asn1 import ( |
| 7 |
ASN1Value, |
| 8 |
TagClass, |
| 9 |
get_sequence_value, |
| 10 |
pack_asn1, |
| 11 |
pack_asn1_integer, |
| 12 |
pack_asn1_octet_string, |
| 13 |
pack_asn1_sequence, |
| 14 |
unpack_asn1, |
| 15 |
unpack_asn1_integer, |
| 16 |
unpack_asn1_octet_string, |
| 17 |
unpack_asn1_tagged_sequence, |
| 18 |
) |
| 19 |
|
| 20 |
|
| 21 |
def unpack_text_field( |
| 22 |
sequence: typing.Dict[int, ASN1Value], |
| 23 |
idx: int, |
| 24 |
structure: str, |
| 25 |
name: str, |
| 26 |
**kwargs: typing.Optional[str], |
| 27 |
) -> typing.Optional[str]: |
| 28 |
"""Extracts a text field from a tagged ASN.1 sequence.""" |
| 29 |
raw_value = get_sequence_value(sequence, idx, structure, name, unpack_asn1_octet_string) |
| 30 |
if raw_value is None: |
| 31 |
if "default" not in kwargs: |
| 32 |
raise ValueError("Missing mandatory text field '%s' in '%s'" % (name, structure)) |
| 33 |
|
| 34 |
return kwargs["default"] |
| 35 |
|
| 36 |
return raw_value.decode("utf-16-le") |
| 37 |
|
| 38 |
|
| 39 |
def unpack_sequence(data: typing.Union[ASN1Value, bytes]) -> typing.Dict[int, ASN1Value]: |
| 40 |
"""Helper function that can unpack a sequence as either it's raw bytes or the already unpacked ASN.1 tuple.""" |
| 41 |
if not isinstance(data, ASN1Value): |
| 42 |
data = unpack_asn1(data)[0] |
| 43 |
|
| 44 |
return unpack_asn1_tagged_sequence(data) |
| 45 |
|
| 46 |
|
| 47 |
class NegoData: |
| 48 |
"""CredSSP NegoData structure. |
| 49 |
|
| 50 |
The NegoData structure contains the SPNEGO tokens, the Kerberos messages, or the NTLM messages. While the |
| 51 |
structure is a SEQUENCE OF SEQUENCE this class just represents an individual SEQUENCE entry. |
| 52 |
|
| 53 |
The ASN.1 definition for the NegoData structure is defined in `MS-CSSP 2.2.1.1 NegoData`_:: |
| 54 |
|
| 55 |
NegoData ::= SEQUENCE OF SEQUENCE { |
| 56 |
negoToken [0] OCTET STRING |
| 57 |
} |
| 58 |
|
| 59 |
Args: |
| 60 |
nego_token: One or more SPNEGO tokens and all Kerberos or NTLM messages, as negotiated by SPNEGO. |
| 61 |
|
| 62 |
Attributes: |
| 63 |
nego_token (bytes): See args. |
| 64 |
|
| 65 |
.. _MS-CSSP 2.2.1.1 NegoData: |
| 66 |
https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-cssp/9664994d-0784-4659-b85b-83b8d54c2336 |
| 67 |
""" |
| 68 |
|
| 69 |
def __init__(self, nego_token: bytes) -> None: |
| 70 |
self.nego_token = nego_token |
| 71 |
|
| 72 |
def pack(self) -> bytes: |
| 73 |
"""Packs the NegoData as a byte string.""" |
| 74 |
return pack_asn1_sequence( |
| 75 |
[ |
| 76 |
pack_asn1(TagClass.context_specific, True, 0, pack_asn1_octet_string(self.nego_token)), |
| 77 |
] |
| 78 |
) |
| 79 |
|
| 80 |
@staticmethod |
| 81 |
def unpack(b_data: typing.Union[ASN1Value, bytes]) -> "NegoData": |
| 82 |
"""Unpacks the NegoData TLV value.""" |
| 83 |
nego_data = unpack_sequence(b_data) |
| 84 |
nego_token = get_sequence_value(nego_data, 0, "NegoData", "negoToken", unpack_asn1_octet_string) |
| 85 |
|
| 86 |
return NegoData(nego_token) |
| 87 |
|
| 88 |
|
| 89 |
class TSRequest: |
| 90 |
"""CredSSP TSRequest structure. |
| 91 |
|
| 92 |
The TSRequest structure is the top-most structure used by the CredSSP client and CredSSP server. The TSRequest |
| 93 |
message is always sent over the TLS-encrypted channel between the client and server in a CredSSP protocol exchange. |
| 94 |
|
| 95 |
The ASN.1 definition for the TSRequest structure is defined in `MS-CSSP 2.2.1 TSRequest`_:: |
| 96 |
|
| 97 |
TSRequest ::= SEQUENCE { |
| 98 |
version [0] INTEGER, |
| 99 |
negoTokens [1] NegoData OPTIONAL, |
| 100 |
authInfo [2] OCTET STRING OPTIONAL, |
| 101 |
pubKeyAuth [3] OCTET STRING OPTIONAL, |
| 102 |
errorCode [4] INTEGER OPTIONAL, |
| 103 |
clientNonce [5] OCTET STRING OPTIONAL |
| 104 |
} |
| 105 |
|
| 106 |
Args: |
| 107 |
version: THe supported version of the CredSSP protocol. |
| 108 |
nego_tokens: A list of NegoData structures that contains te SPNEGO tokens. |
| 109 |
auth_info: The encrypted TSCredentials structure that contains the user's credentials to be delegated to the |
| 110 |
server. |
| 111 |
pub_key_auth: The encrypted public key used in the TLS handshake between the client and the server. |
| 112 |
error_code: The error code that represents the NTSTATUS failure code from the server. |
| 113 |
client_nonce: A 32-byte array of cryptograpically random bytes for the pub_key_auth hash computation (version |
| 114 |
or above). |
| 115 |
|
| 116 |
Attributes: |
| 117 |
version (int): See args. |
| 118 |
nego_tokens (Optional[List[int]]): See args. |
| 119 |
auth_info (bytes): See args. |
| 120 |
pub_key_auth (bytes): See args. |
| 121 |
error_code (int): See args. |
| 122 |
client_nonce (bytes): See args. |
| 123 |
|
| 124 |
.. _MS-CSSP 2.2.1 TSRequest: |
| 125 |
https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-cssp/9664994d-0784-4659-b85b-83b8d54c2336 |
| 126 |
""" |
| 127 |
|
| 128 |
def __init__( |
| 129 |
self, |
| 130 |
version: int, |
| 131 |
nego_tokens: typing.Optional[typing.Union[NegoData, typing.List[NegoData]]] = None, |
| 132 |
auth_info: typing.Optional[bytes] = None, |
| 133 |
pub_key_auth: typing.Optional[bytes] = None, |
| 134 |
error_code: typing.Optional[int] = None, |
| 135 |
client_nonce: typing.Optional[bytes] = None, |
| 136 |
) -> None: |
| 137 |
self.version = version |
| 138 |
|
| 139 |
if nego_tokens is not None and not isinstance(nego_tokens, list): |
| 140 |
nego_tokens = [nego_tokens] |
| 141 |
self.nego_tokens = nego_tokens |
| 142 |
|
| 143 |
self.auth_info = auth_info |
| 144 |
self.pub_key_auth = pub_key_auth |
| 145 |
self.error_code = error_code |
| 146 |
self.client_nonce = client_nonce |
| 147 |
|
| 148 |
def pack(self) -> bytes: |
| 149 |
"""Packs the TSRequest as a byte string.""" |
| 150 |
elements = [pack_asn1(TagClass.context_specific, True, 0, pack_asn1_integer(self.version))] |
| 151 |
if self.nego_tokens: |
| 152 |
nego_tokens = [token.pack() for token in self.nego_tokens] |
| 153 |
elements.append(pack_asn1(TagClass.context_specific, True, 1, pack_asn1_sequence(nego_tokens))) |
| 154 |
|
| 155 |
value_map: typing.List[typing.Tuple[int, typing.Any, typing.Callable]] = [ |
| 156 |
(2, self.auth_info, pack_asn1_octet_string), |
| 157 |
(3, self.pub_key_auth, pack_asn1_octet_string), |
| 158 |
(4, self.error_code, pack_asn1_integer), |
| 159 |
(5, self.client_nonce, pack_asn1_octet_string), |
| 160 |
] |
| 161 |
for tag, value, pack_func in value_map: |
| 162 |
if value is not None: |
| 163 |
elements.append(pack_asn1(TagClass.context_specific, True, tag, pack_func(value))) |
| 164 |
|
| 165 |
return pack_asn1_sequence(elements) |
| 166 |
|
| 167 |
@staticmethod |
| 168 |
def unpack(b_data: typing.Union[ASN1Value, bytes]) -> "TSRequest": |
| 169 |
"""Unpacks the TSRequest TLV value.""" |
| 170 |
request = unpack_sequence(b_data) |
| 171 |
|
| 172 |
version = get_sequence_value(request, 0, "TSRequest", "version", unpack_asn1_integer) |
| 173 |
|
| 174 |
nego_tokens = get_sequence_value(request, 1, "TSRequest", "negoTokens") |
| 175 |
if nego_tokens is not None: |
| 176 |
remaining_bytes = nego_tokens.b_data |
| 177 |
nego_tokens = [] |
| 178 |
while remaining_bytes: |
| 179 |
nego_tokens.append(NegoData.unpack(remaining_bytes)) |
| 180 |
remaining_bytes = unpack_asn1(remaining_bytes)[1] |
| 181 |
|
| 182 |
auth_info = get_sequence_value(request, 2, "TSRequest", "authInfo", unpack_asn1_octet_string) |
| 183 |
pub_key_auth = get_sequence_value(request, 3, "TSRequest", "pubKeyAuth", unpack_asn1_octet_string) |
| 184 |
error_code = get_sequence_value(request, 4, "TSRequest", "errorCode", unpack_asn1_integer) |
| 185 |
client_nonce = get_sequence_value(request, 5, "TSRequest", "clientNonce", unpack_asn1_octet_string) |
| 186 |
|
| 187 |
return TSRequest( |
| 188 |
version, |
| 189 |
nego_tokens=nego_tokens, |
| 190 |
auth_info=auth_info, |
| 191 |
pub_key_auth=pub_key_auth, |
| 192 |
error_code=error_code, |
| 193 |
client_nonce=client_nonce, |
| 194 |
) |
| 195 |
|
| 196 |
|
| 197 |
class TSCredentials: |
| 198 |
"""CredSSP TSCredentials structure. |
| 199 |
|
| 200 |
The TSCredentials structure contains both the user's credentials that are delegated to the server and their type. |
| 201 |
|
| 202 |
The ASN.1 definition for the TSCredentials structure is defined in `MS-CSSP 2.2.1.2 TSCredentials`_:: |
| 203 |
|
| 204 |
TSCredentials ::= SEQUENCE { |
| 205 |
credType [0] INTEGER, |
| 206 |
credentials [1] OCTET STRING |
| 207 |
} |
| 208 |
|
| 209 |
Args: |
| 210 |
credentials: The credential structure; TSPasswordCreds, TSSmartCardCreds, TSRemoteGuardCreds. |
| 211 |
|
| 212 |
Attributes: |
| 213 |
credentials (Union[TSPasswordCreds, TSSmartCardCreds, TSRemoteGuardCreds]): See args. |
| 214 |
|
| 215 |
.. _MS-CSSP 2.2.1.2 TSCredentials: |
| 216 |
https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-cssp/94a1ab00-5500-42fd-8d3d-7a84e6c2cf03 |
| 217 |
""" |
| 218 |
|
| 219 |
def __init__(self, credentials: typing.Union["TSPasswordCreds", "TSSmartCardCreds", "TSRemoteGuardCreds"]) -> None: |
| 220 |
self.credentials = credentials |
| 221 |
|
| 222 |
@property |
| 223 |
def cred_type(self) -> int: |
| 224 |
"""The credential type of credentials as an integer.""" |
| 225 |
if isinstance(self.credentials, TSPasswordCreds): |
| 226 |
return 1 |
| 227 |
|
| 228 |
elif isinstance(self.credentials, TSSmartCardCreds): |
| 229 |
return 2 |
| 230 |
|
| 231 |
elif isinstance(self.credentials, TSRemoteGuardCreds): |
| 232 |
return 6 |
| 233 |
|
| 234 |
else: |
| 235 |
raise ValueError("Invalid credential type set") |
| 236 |
|
| 237 |
def pack(self) -> bytes: |
| 238 |
"""Packs the TSCredentials as a byte string.""" |
| 239 |
cred_type = self.cred_type |
| 240 |
credentials = self.credentials.pack() |
| 241 |
|
| 242 |
return pack_asn1_sequence( |
| 243 |
[ |
| 244 |
pack_asn1(TagClass.context_specific, True, 0, pack_asn1_integer(cred_type)), |
| 245 |
pack_asn1(TagClass.context_specific, True, 1, pack_asn1_octet_string(credentials)), |
| 246 |
] |
| 247 |
) |
| 248 |
|
| 249 |
@staticmethod |
| 250 |
def unpack(b_data: typing.Union[ASN1Value, bytes]) -> "TSCredentials": |
| 251 |
"""Unpacks the TSCredentials TLV value.""" |
| 252 |
credential = unpack_sequence(b_data) |
| 253 |
cred_type = get_sequence_value(credential, 0, "TSCredentials", "credType", unpack_asn1_integer) |
| 254 |
credentials_raw = get_sequence_value(credential, 1, "TSCredentials", "credentials", unpack_asn1_octet_string) |
| 255 |
|
| 256 |
cred_class: typing.Optional[ |
| 257 |
typing.Union[typing.Type[TSPasswordCreds], typing.Type[TSSmartCardCreds], typing.Type[TSRemoteGuardCreds]] |
| 258 |
] = { |
| 259 |
1: TSPasswordCreds, |
| 260 |
2: TSSmartCardCreds, |
| 261 |
6: TSRemoteGuardCreds, |
| 262 |
}.get( |
| 263 |
cred_type |
| 264 |
) |
| 265 |
if not cred_class: |
| 266 |
raise ValueError("Unknown credType %s in TSCredentials, cannot unpack" % cred_type) |
| 267 |
|
| 268 |
credentials = cred_class.unpack(credentials_raw) |
| 269 |
return TSCredentials(credentials) |
| 270 |
|
| 271 |
|
| 272 |
class TSPasswordCreds: |
| 273 |
"""CredSSP TSPasswordCreds structure. |
| 274 |
|
| 275 |
The TSPasswordCreds structure contains the user's password credentials that are delegated to the server. |
| 276 |
|
| 277 |
The ASN.1 definition for the TSPasswordCreds structure is defined in `MS-CSSP 2.2.1.2.1 TSPasswordCreds`_:: |
| 278 |
|
| 279 |
TSPasswordCreds ::= SEQUENCE { |
| 280 |
domainName [0] OCTET STRING, |
| 281 |
userName [1] OCTET STRING, |
| 282 |
password [2] OCTET STRING |
| 283 |
} |
| 284 |
|
| 285 |
Args: |
| 286 |
domain_name: The name of the user's account domain. |
| 287 |
username: The user's account name. |
| 288 |
password: The user's account password. |
| 289 |
|
| 290 |
Attributes: |
| 291 |
domain_name (str): See args. |
| 292 |
username (str): See args. |
| 293 |
password (str): See args. |
| 294 |
|
| 295 |
.. _MS-CSSP 2.2.1.2.1 TSPasswordCreds: |
| 296 |
https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-cssp/9664994d-0784-4659-b85b-83b8d54c2336 |
| 297 |
""" |
| 298 |
|
| 299 |
def __init__(self, domain_name: str, username: str, password: str) -> None: |
| 300 |
self.domain_name = domain_name |
| 301 |
self.username = username |
| 302 |
self.password = password |
| 303 |
|
| 304 |
def pack(self) -> bytes: |
| 305 |
"""Packs the TSPasswordCreds as a byte string.""" |
| 306 |
elements = [] |
| 307 |
for idx, value in enumerate([self.domain_name, self.username, self.password]): |
| 308 |
b_value = value.encode("utf-16-le") |
| 309 |
elements.append(pack_asn1(TagClass.context_specific, True, idx, pack_asn1_octet_string(b_value))) |
| 310 |
|
| 311 |
return pack_asn1_sequence(elements) |
| 312 |
|
| 313 |
@staticmethod |
| 314 |
def unpack(b_data: typing.Union[ASN1Value, bytes]) -> "TSPasswordCreds": |
| 315 |
"""Unpacks the TSPasswordCreds TLV value.""" |
| 316 |
creds = unpack_sequence(b_data) |
| 317 |
|
| 318 |
domain_name = unpack_text_field(creds, 0, "TSPasswordCreds", "domainName") or "" |
| 319 |
username = unpack_text_field(creds, 1, "TSPasswordCreds", "userName") or "" |
| 320 |
password = unpack_text_field(creds, 2, "TSPasswordCreds", "password") or "" |
| 321 |
|
| 322 |
return TSPasswordCreds(domain_name, username, password) |
| 323 |
|
| 324 |
|
| 325 |
class TSSmartCardCreds: |
| 326 |
"""CredSSP TSSmartCardCreds structure. |
| 327 |
|
| 328 |
The TSSmartCardCreds structure contains the user's smart card credentials that are delegated to the server. |
| 329 |
|
| 330 |
The ASN.1 definition for the TSSmartCardCreds structure is defined in `MS-CSSP 2.2.1.2.2 TSSmartCardCreds`_:: |
| 331 |
|
| 332 |
TSSmartCardCreds ::= SEQUENCE { |
| 333 |
pin [0] OCTET STRING, |
| 334 |
cspData [1] TSCspDataDetail, |
| 335 |
userHint [2] OCTET STRING OPTIONAL, |
| 336 |
domainHint [3] OCTET STRING OPTIONAL |
| 337 |
} |
| 338 |
|
| 339 |
Args: |
| 340 |
pin: THe user's smart card PIN. |
| 341 |
csp_data: Info about the cryptographic service provider (CSP). |
| 342 |
user_hint: The user's account hint. |
| 343 |
domain_hint: The user's domain name to which the user's account belongs. |
| 344 |
|
| 345 |
Attributes: |
| 346 |
pin (str): See args. |
| 347 |
csp_data (TSCspDataDetail): See args. |
| 348 |
user_hint (Optional[str]): See args. |
| 349 |
domain_hint (Optional[str]): See args. |
| 350 |
|
| 351 |
.. _MS-CSSP 2.2.1.2.2 TSSmartCardCreds: |
| 352 |
https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-cssp/4251d165-cf01-4513-a5d8-39ee4a98b7a4 |
| 353 |
""" |
| 354 |
|
| 355 |
def __init__( |
| 356 |
self, |
| 357 |
pin: str, |
| 358 |
csp_data: "TSCspDataDetail", |
| 359 |
user_hint: typing.Optional[str] = None, |
| 360 |
domain_hint: typing.Optional[str] = None, |
| 361 |
) -> None: |
| 362 |
self.pin = pin |
| 363 |
self.csp_data = csp_data |
| 364 |
self.user_hint = user_hint |
| 365 |
self.domain_hint = domain_hint |
| 366 |
|
| 367 |
def pack(self) -> bytes: |
| 368 |
"""Packs the TSSmartCardCreds as a byte string.""" |
| 369 |
elements = [ |
| 370 |
pack_asn1(TagClass.context_specific, True, 0, pack_asn1_octet_string(self.pin.encode("utf-16-le"))), |
| 371 |
pack_asn1(TagClass.context_specific, True, 1, self.csp_data.pack()), |
| 372 |
] |
| 373 |
for idx, value in [(2, self.user_hint), (3, self.domain_hint)]: |
| 374 |
if value: |
| 375 |
b_value = value.encode("utf-16-le") |
| 376 |
elements.append(pack_asn1(TagClass.context_specific, True, idx, pack_asn1_octet_string(b_value))) |
| 377 |
|
| 378 |
return pack_asn1_sequence(elements) |
| 379 |
|
| 380 |
@staticmethod |
| 381 |
def unpack(b_data: typing.Union[ASN1Value, bytes]) -> "TSSmartCardCreds": |
| 382 |
"""Unpacks the TSSmartCardCreds TLV value.""" |
| 383 |
creds = unpack_sequence(b_data) |
| 384 |
|
| 385 |
pin = unpack_text_field(creds, 0, "TSSmartCardCreds", "pin") or "" |
| 386 |
csp_data = get_sequence_value(creds, 1, "TSSmartCardCreds", "cspData", TSCspDataDetail.unpack) |
| 387 |
user_hint = unpack_text_field(creds, 2, "TSSmartCardCreds", "userHint", default=None) |
| 388 |
domain_hint = unpack_text_field(creds, 3, "TSSmartCardCreds", "domainHint", default=None) |
| 389 |
|
| 390 |
return TSSmartCardCreds(pin, csp_data, user_hint, domain_hint) |
| 391 |
|
| 392 |
|
| 393 |
class TSCspDataDetail: |
| 394 |
"""CredSSP TSCspDataDetail structure. |
| 395 |
|
| 396 |
The TSCspDataDetail structure contains CSP information used during smart card logon. |
| 397 |
|
| 398 |
The ASN.1 definition for the TSCspDataDetail structure is defined in `MS-CSSP 2.2.1.2.2.1 TSCspDataDetail`_:: |
| 399 |
|
| 400 |
TSCspDataDetail ::= SEQUENCE { |
| 401 |
keySpec [0] INTEGER, |
| 402 |
cardName [1] OCTET STRING OPTIONAL, |
| 403 |
readerName [2] OCTET STRING OPTIONAL, |
| 404 |
containerName [3] OCTET STRING OPTIONAL, |
| 405 |
cspName [4] OCTET STRING OPTIONAL |
| 406 |
} |
| 407 |
|
| 408 |
Args: |
| 409 |
key_spec: The specification of the user's smart card. |
| 410 |
card_name: The name of the smart card. |
| 411 |
reader_name: The name of the smart card reader. |
| 412 |
container_name: The name of the certificate container. |
| 413 |
csp_name: The name of the CSP. |
| 414 |
|
| 415 |
Attributes: |
| 416 |
key_spec (int): See args. |
| 417 |
card_name (Optional[str]): See args. |
| 418 |
reader_name (Optional[str]): See args. |
| 419 |
container_name (Optional[str]): See args. |
| 420 |
csp_name (Optional[str]): See args. |
| 421 |
|
| 422 |
.. _MS-CSSP 2.2.1.2.2.1 TSCspDataDetail: |
| 423 |
https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-cssp/34ee27b3-5791-43bb-9201-076054b58123 |
| 424 |
""" |
| 425 |
|
| 426 |
def __init__( |
| 427 |
self, |
| 428 |
key_spec: int, |
| 429 |
card_name: typing.Optional[str] = None, |
| 430 |
reader_name: typing.Optional[str] = None, |
| 431 |
container_name: typing.Optional[str] = None, |
| 432 |
csp_name: typing.Optional[str] = None, |
| 433 |
) -> None: |
| 434 |
self.key_spec = key_spec |
| 435 |
self.card_name = card_name |
| 436 |
self.reader_name = reader_name |
| 437 |
self.container_name = container_name |
| 438 |
self.csp_name = csp_name |
| 439 |
|
| 440 |
def pack(self) -> bytes: |
| 441 |
"""Packs the TSCspDataDetail as a byte string.""" |
| 442 |
elements = [ |
| 443 |
pack_asn1(TagClass.context_specific, True, 0, pack_asn1_integer(self.key_spec)), |
| 444 |
] |
| 445 |
|
| 446 |
value_map = [ |
| 447 |
(1, self.card_name), |
| 448 |
(2, self.reader_name), |
| 449 |
(3, self.container_name), |
| 450 |
(4, self.csp_name), |
| 451 |
] |
| 452 |
for idx, value in value_map: |
| 453 |
if value: |
| 454 |
b_value = value.encode("utf-16-le") |
| 455 |
elements.append(pack_asn1(TagClass.context_specific, True, idx, pack_asn1_octet_string(b_value))) |
| 456 |
|
| 457 |
return pack_asn1_sequence(elements) |
| 458 |
|
| 459 |
@staticmethod |
| 460 |
def unpack(b_data: typing.Union[ASN1Value, bytes]) -> "TSCspDataDetail": |
| 461 |
"""Unpacks the TSCspDataDetail TLV value.""" |
| 462 |
csp_data = unpack_sequence(b_data) |
| 463 |
|
| 464 |
key_spec = get_sequence_value(csp_data, 0, "TSCspDataDetail", "keySpec", unpack_asn1_integer) |
| 465 |
card_name = unpack_text_field(csp_data, 1, "TSCspDataDetail", "cardName", default=None) |
| 466 |
reader_name = unpack_text_field(csp_data, 2, "TSCspDataDetail", "readerName", default=None) |
| 467 |
container_name = unpack_text_field(csp_data, 3, "TSCspDataDetail", "containerName", default=None) |
| 468 |
csp_name = unpack_text_field(csp_data, 4, "TSCspDataDetail", "cspName", default=None) |
| 469 |
|
| 470 |
return TSCspDataDetail(key_spec, card_name, reader_name, container_name, csp_name) |
| 471 |
|
| 472 |
|
| 473 |
class TSRemoteGuardCreds: |
| 474 |
"""CredSSP TSRemoteGuardCreds structure. |
| 475 |
|
| 476 |
The TSRemoteGuardCreds structure contains a logon credential and supplemental credentials provided by security |
| 477 |
packages. The format of the individual credentials depends on the packages that provided them. |
| 478 |
|
| 479 |
The ASN.1 definition for the TSRemoteGuardCreds structure is defined in `MS-CSSP 2.2.1.2.3 TSRemoteGuardCreds`_:: |
| 480 |
|
| 481 |
TSRemoteGuardCreds ::= SEQUENCE{ |
| 482 |
logonCred [0] TSRemoteGuardPackageCred, |
| 483 |
supplementalCreds [1] SEQUENCE OF TSRemoteGuardPackageCred OPTIONAL, |
| 484 |
} |
| 485 |
|
| 486 |
Args: |
| 487 |
logon_cred: The logon credential for the user. |
| 488 |
supplemental_creds: Optional supplemental credentials for other security packages. |
| 489 |
|
| 490 |
Attributes: |
| 491 |
logon_cred (TSRemoteGuardPackageCred): See args. |
| 492 |
supplemental_creds (List[TSRemoteGuardPackageCred]): See args. |
| 493 |
|
| 494 |
.. _MS-CSSP 2.2.1.2.3 TSRemoteGuardCreds: |
| 495 |
https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-cssp/7ef8229c-44ea-4c1b-867f-00369b882b38 |
| 496 |
""" |
| 497 |
|
| 498 |
def __init__( |
| 499 |
self, |
| 500 |
logon_cred: "TSRemoteGuardPackageCred", |
| 501 |
supplemental_creds: typing.Optional[ |
| 502 |
typing.Union["TSRemoteGuardPackageCred", typing.List["TSRemoteGuardPackageCred"]] |
| 503 |
] = None, |
| 504 |
) -> None: |
| 505 |
self.logon_cred = logon_cred |
| 506 |
|
| 507 |
if supplemental_creds is not None and not isinstance(supplemental_creds, list): |
| 508 |
supplemental_creds = [supplemental_creds] |
| 509 |
self.supplemental_creds = supplemental_creds |
| 510 |
|
| 511 |
def pack(self) -> bytes: |
| 512 |
"""Packs the TSRemoteGuardCreds as a byte string.""" |
| 513 |
elements = [pack_asn1(TagClass.context_specific, True, 0, self.logon_cred.pack())] |
| 514 |
if self.supplemental_creds is not None: |
| 515 |
supplemental_creds = [cred.pack() for cred in self.supplemental_creds] |
| 516 |
elements.append(pack_asn1(TagClass.context_specific, True, 1, pack_asn1_sequence(supplemental_creds))) |
| 517 |
|
| 518 |
return pack_asn1_sequence(elements) |
| 519 |
|
| 520 |
@staticmethod |
| 521 |
def unpack(b_data: typing.Union[ASN1Value, bytes]) -> "TSRemoteGuardCreds": |
| 522 |
"""Unpacks the TSRemoteGuardCreds TLV value.""" |
| 523 |
cred = unpack_sequence(b_data) |
| 524 |
logon_cred = get_sequence_value(cred, 0, "TSRemoteGuardCreds", "logonCred", TSRemoteGuardPackageCred.unpack) |
| 525 |
|
| 526 |
raw_supplemental_creds = get_sequence_value(cred, 1, "TSRemoteGuardCreds", "supplementalCreds") |
| 527 |
if raw_supplemental_creds: |
| 528 |
supplemental_creds = [] |
| 529 |
remaining_bytes = raw_supplemental_creds.b_data |
| 530 |
while remaining_bytes: |
| 531 |
supplemental_creds.append(TSRemoteGuardPackageCred.unpack(remaining_bytes)) |
| 532 |
remaining_bytes = unpack_asn1(remaining_bytes)[1] |
| 533 |
|
| 534 |
else: |
| 535 |
supplemental_creds = None |
| 536 |
|
| 537 |
return TSRemoteGuardCreds(logon_cred, supplemental_creds) |
| 538 |
|
| 539 |
|
| 540 |
class TSRemoteGuardPackageCred: |
| 541 |
"""CredSSP TSRemoteGuardPackageCred structure. |
| 542 |
|
| 543 |
The TSRemoteGuardPackageCred structure contains credentials for a specific security package. |
| 544 |
|
| 545 |
The ASN.1 definition for the TSRemoteGuardPackageCred structure is defined in |
| 546 |
`MS-CSSP 2.2.1.2.3.1 TSRemoteGuardPackageCred`_:: |
| 547 |
|
| 548 |
TSRemoteGuardPackageCred ::= SEQUENCE{ |
| 549 |
packageName [0] OCTET STRING, |
| 550 |
credBuffer [1] OCTET STRING, |
| 551 |
} |
| 552 |
|
| 553 |
Args: |
| 554 |
package_name: The name of the packages for which these credentials are intended. |
| 555 |
cred_buffer: The credentials in a format specified by the CredSSP server. |
| 556 |
|
| 557 |
Attributes: |
| 558 |
package_name (str): See args. |
| 559 |
cred_buffer (bytes): See args. |
| 560 |
|
| 561 |
.. _MS-CSSP 2.2.1.2.3.1 TSRemoteGuardPackageCred: |
| 562 |
https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-cssp/173eee44-1a2c-463f-b909-c15db01e68d7 |
| 563 |
""" |
| 564 |
|
| 565 |
def __init__(self, package_name: str, cred_buffer: bytes) -> None: |
| 566 |
self.package_name = package_name |
| 567 |
self.cred_buffer = cred_buffer |
| 568 |
|
| 569 |
def pack(self) -> bytes: |
| 570 |
"""Packs the TSRemoteGuardPackageCred as a byte string.""" |
| 571 |
b_package_name = self.package_name.encode("utf-16-le") |
| 572 |
return pack_asn1_sequence( |
| 573 |
[ |
| 574 |
pack_asn1(TagClass.context_specific, True, 0, pack_asn1_octet_string(b_package_name)), |
| 575 |
pack_asn1(TagClass.context_specific, True, 1, pack_asn1_octet_string(self.cred_buffer)), |
| 576 |
] |
| 577 |
) |
| 578 |
|
| 579 |
@staticmethod |
| 580 |
def unpack(b_data: typing.Union[ASN1Value, bytes]) -> "TSRemoteGuardPackageCred": |
| 581 |
"""Unpacks the TSRemoteGuardPackageCred TLV value.""" |
| 582 |
package_cred = unpack_sequence(b_data) |
| 583 |
package_name = unpack_text_field(package_cred, 0, "TSRemoteGuardPackageCred", "packageName") or "" |
| 584 |
cred_buffer = get_sequence_value( |
| 585 |
package_cred, 1, "TSRemoteGuardPackageCred", "credBuffer", unpack_asn1_octet_string |
| 586 |
) |
| 587 |
|
| 588 |
return TSRemoteGuardPackageCred(package_name, cred_buffer) |