|
24 | 24 | from oscrypto.asymmetric import rsa_pkcs1v15_sign, load_private_key |
25 | 25 | from oscrypto.keys import parse_pkcs12, parse_certificate, parse_private |
26 | 26 |
|
27 | | - |
| 27 | +def get_encoded_data(data:bytes|str, encoding = 'file') -> bytes: |
| 28 | + if encoding == 'file': |
| 29 | + with open(data, 'rb') as kf: |
| 30 | + return kf.read() |
| 31 | + elif encoding == 'hex': |
| 32 | + return bytes.fromhex(data) |
| 33 | + elif encoding == 'b64' or encoding == 'base64': |
| 34 | + if isinstance(data, str): |
| 35 | + data = data.encode() |
| 36 | + return base64.b64decode(data) |
| 37 | + elif encoding == 'raw': |
| 38 | + if isinstance(data, str): |
| 39 | + data = data.encode() |
| 40 | + return data |
| 41 | + raise Exception('Unknown encoding "%s"!' % encoding) |
28 | 42 |
|
29 | 43 | class KerberosCredential: |
30 | 44 | def __init__(self): |
@@ -116,6 +130,9 @@ def get_key_for_enctype(self, etype:EncryptionType, salt:bytes = None) -> bytes: |
116 | 130 | raise Exception('Unsupported encryption type: %s' % etype.name) |
117 | 131 |
|
118 | 132 | def get_supported_enctypes(self, as_int = True) -> List[EncryptionType]: |
| 133 | + """ |
| 134 | + Returns a list of all EncryptionTypes this credentials can use for authentication |
| 135 | + """ |
119 | 136 | supp_enctypes = collections.OrderedDict() |
120 | 137 | if self.kerberos_key_aes_256: |
121 | 138 | supp_enctypes[EncryptionType.AES256_CTS_HMAC_SHA1_96] = 1 |
@@ -146,20 +163,50 @@ def get_supported_enctypes(self, as_int = True) -> List[EncryptionType]: |
146 | 163 | if as_int == True: |
147 | 164 | return [etype.value for etype in supp_enctypes] |
148 | 165 | return [etype for etype in supp_enctypes] |
149 | | - |
| 166 | + |
150 | 167 | @staticmethod |
151 | | - def from_krbcred(keytab_file_path: str, principal: str = None, realm: str = None) -> KerberosCredential: |
152 | | - return KerberosCredential.from_kirbi(keytab_file_path, principal, realm) |
| 168 | + def from_keytab(keytab_file_path: str, principal: str, realm: str, encoding = 'file') -> KerberosCredential: |
| 169 | + """Returns a kerberos credential object from Keytab file/data""" |
| 170 | + cred = KerberosCredential() |
| 171 | + cred.username = principal |
| 172 | + cred.domain = realm |
| 173 | + data = get_encoded_data(keytab_file_path, encoding=encoding) |
| 174 | + return KerberosCredential.from_keytab_string(data, principal, realm) |
| 175 | + |
| 176 | + @staticmethod |
| 177 | + def from_ccache(data, principal: str = None, realm: str = None, encoding = 'file') -> KerberosCredential: |
| 178 | + """Returns a kerberos credential object with CCACHE database""" |
| 179 | + data = get_encoded_data(data, encoding=encoding) |
| 180 | + k = KerberosCredential() |
| 181 | + k.username = principal |
| 182 | + k.domain = realm |
| 183 | + k.ccache = CCACHE.from_bytes(data) |
| 184 | + return k |
153 | 185 |
|
154 | 186 | @staticmethod |
155 | | - def from_kirbi(keytab_file_path: str, principal: str = None, realm: str = None) -> KerberosCredential: |
| 187 | + def from_kirbi(keytab_file_path: str, principal: str = None, realm: str = None, encoding = 'file') -> KerberosCredential: |
| 188 | + """Returns a kerberos credential object from .kirbi file""" |
| 189 | + data = get_encoded_data(keytab_file_path, encoding=encoding) |
156 | 190 | cred = KerberosCredential() |
157 | 191 | cred.username = principal |
158 | 192 | cred.domain = realm |
159 | | - cred.ccache = CCACHE.from_kirbifile(keytab_file_path) |
| 193 | + cred.ccache = CCACHE.from_kirbi(data) |
160 | 194 | cred.ccache_spn_strict_check = False |
161 | 195 | return cred |
162 | | - |
| 196 | + |
| 197 | + @staticmethod |
| 198 | + def from_pfx(data:str, password:str, dhparams:DirtyDH = None, username:str = None, domain:str = None, encoding = 'file') -> KerberosCredential: |
| 199 | + """ |
| 200 | + Retruns a credential object from data found in the PFX file |
| 201 | + Username and domain will override the values found in the certificate |
| 202 | + """ |
| 203 | + data = get_encoded_data(data, encoding=encoding) |
| 204 | + return KerberosCredential.from_pfx_string(data, password, dhparams = dhparams, username = username, domain = domain) |
| 205 | + |
| 206 | + @staticmethod |
| 207 | + def from_krbcred(keytab_file_path: str, principal: str = None, realm: str = None) -> KerberosCredential: |
| 208 | + return KerberosCredential.from_kirbi(keytab_file_path, principal, realm) |
| 209 | + |
163 | 210 | @staticmethod |
164 | 211 | def from_keytab_string(self, keytabdata: str|bytes, principal: str, realm: str) -> KerberosCredential: |
165 | 212 | cred = KerberosCredential() |
@@ -195,17 +242,9 @@ def from_keytab_string(self, keytabdata: str|bytes, principal: str, realm: str) |
195 | 242 |
|
196 | 243 | return cred |
197 | 244 |
|
198 | | - @staticmethod |
199 | | - def from_keytab(keytab_file_path: str, principal: str, realm: str) -> KerberosCredential: |
200 | | - cred = KerberosCredential() |
201 | | - cred.username = principal |
202 | | - cred.domain = realm |
203 | | - |
204 | | - with open(keytab_file_path, 'rb') as kf: |
205 | | - return KerberosCredential.from_keytab_string(kf.read(), principal, realm) |
206 | | - |
207 | 245 | @staticmethod |
208 | 246 | def from_ccache_file(filepath, principal: str = None, realm: str = None) -> KerberosCredential: |
| 247 | + """Depricated! Use from_ccache with proper encoding instead!""" |
209 | 248 | k = KerberosCredential() |
210 | 249 | k.username = principal |
211 | 250 | k.domain = realm |
|
0 commit comments