/
CryptoService.cs
76 lines (66 loc) · 2.79 KB
/
CryptoService.cs
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
using System;
using System.Collections.Generic;
namespace Kerberos.NET.Crypto
{
public static class CryptoService
{
private static readonly Dictionary<EncryptionType, Func<KerberosCryptoTransformer>> CryptoAlgorithms
= new Dictionary<EncryptionType, Func<KerberosCryptoTransformer>>();
static CryptoService()
{
RegisterCryptographicAlgorithm(EncryptionType.RC4_HMAC_NT, () => new RC4Transformer());
RegisterCryptographicAlgorithm(EncryptionType.RC4_HMAC_NT_EXP, () => new RC4Transformer());
RegisterCryptographicAlgorithm(EncryptionType.AES128_CTS_HMAC_SHA1_96, () => new AES128Transformer());
RegisterCryptographicAlgorithm(EncryptionType.AES256_CTS_HMAC_SHA1_96, () => new AES256Transformer());
}
public static void RegisterCryptographicAlgorithm(
EncryptionType type,
Func<KerberosCryptoTransformer> transformerFunc
)
{
CryptoAlgorithms[type] = transformerFunc;
}
public static KerberosCryptoTransformer CreateTransform(EncryptionType etype)
{
if (CryptoAlgorithms.TryGetValue(etype, out Func<KerberosCryptoTransformer> func) && func != null)
{
return func();
}
return null;
}
internal static ChecksumType ConvertType(EncryptionType type)
{
switch (type)
{
case EncryptionType.RC4_HMAC_NT:
case EncryptionType.RC4_HMAC_NT_EXP:
case EncryptionType.RC4_HMAC_OLD:
case EncryptionType.RC4_HMAC_OLD_EXP:
return ChecksumType.KERB_CHECKSUM_HMAC_MD5;
case EncryptionType.AES128_CTS_HMAC_SHA1_96:
return ChecksumType.HMAC_SHA1_96_AES128;
case EncryptionType.AES256_CTS_HMAC_SHA1_96:
return ChecksumType.HMAC_SHA1_96_AES256;
default:
throw new InvalidOperationException($"Unknown encryption type {type}");
}
}
internal static KerberosChecksum CreateChecksum(
ChecksumType type,
ReadOnlyMemory<byte> signature = default,
ReadOnlyMemory<byte> signatureData = default
)
{
switch (type)
{
case ChecksumType.KERB_CHECKSUM_HMAC_MD5:
return new HmacMd5KerberosChecksum(signature, signatureData);
case ChecksumType.HMAC_SHA1_96_AES128:
return new HmacAes128KerberosChecksum(signature, signatureData);
case ChecksumType.HMAC_SHA1_96_AES256:
return new HmacAes256KerberosChecksum(signature, signatureData);
}
return null;
}
}
}