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
|
using System;
using System.IO;
using Org.BouncyCastle.Crypto.Digests;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Modes;
namespace Org.BouncyCastle.Crypto.Tls
{
public class DefaultTlsCipherFactory
: TlsCipherFactory
{
public virtual TlsCipher CreateCipher(TlsClientContext context,
EncryptionAlgorithm encryptionAlgorithm, DigestAlgorithm digestAlgorithm)
{
switch (encryptionAlgorithm)
{
case EncryptionAlgorithm.cls_3DES_EDE_CBC:
return CreateDesEdeCipher(context, 24, digestAlgorithm);
case EncryptionAlgorithm.AES_128_CBC:
return CreateAesCipher(context, 16, digestAlgorithm);
case EncryptionAlgorithm.AES_256_CBC:
return CreateAesCipher(context, 32, digestAlgorithm);
default:
throw new TlsFatalAlert(AlertDescription.internal_error);
}
}
/// <exception cref="IOException"></exception>
protected virtual TlsCipher CreateAesCipher(TlsClientContext context, int cipherKeySize,
DigestAlgorithm digestAlgorithm)
{
return new TlsBlockCipher(context, CreateAesBlockCipher(), CreateAesBlockCipher(),
CreateDigest(digestAlgorithm), CreateDigest(digestAlgorithm), cipherKeySize);
}
/// <exception cref="IOException"></exception>
protected virtual TlsCipher CreateDesEdeCipher(TlsClientContext context, int cipherKeySize,
DigestAlgorithm digestAlgorithm)
{
return new TlsBlockCipher(context, CreateDesEdeBlockCipher(), CreateDesEdeBlockCipher(),
CreateDigest(digestAlgorithm), CreateDigest(digestAlgorithm), cipherKeySize);
}
protected virtual IBlockCipher CreateAesBlockCipher()
{
return new CbcBlockCipher(new AesFastEngine());
}
protected virtual IBlockCipher CreateDesEdeBlockCipher()
{
return new CbcBlockCipher(new DesEdeEngine());
}
/// <exception cref="IOException"></exception>
protected virtual IDigest CreateDigest(DigestAlgorithm digestAlgorithm)
{
switch (digestAlgorithm)
{
case DigestAlgorithm.MD5:
return new MD5Digest();
case DigestAlgorithm.SHA:
return new Sha1Digest();
case DigestAlgorithm.SHA256:
return new Sha256Digest();
case DigestAlgorithm.SHA384:
return new Sha384Digest();
default:
throw new TlsFatalAlert(AlertDescription.internal_error);
}
}
}
}
|