blob: 66b0ec9fb57f128002ae69f073bcda69f97615bc (
plain) (
blame)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
using System;
using System.IO;
using Org.BouncyCastle.Tls.Crypto;
namespace Org.BouncyCastle.Tls
{
/// <summary>(D)TLS ECDH_anon key exchange (see RFC 4492).</summary>
public class TlsECDHanonKeyExchange
: AbstractTlsKeyExchange
{
private static int CheckKeyExchange(int keyExchange)
{
switch (keyExchange)
{
case KeyExchangeAlgorithm.ECDH_anon:
return keyExchange;
default:
throw new ArgumentException("unsupported key exchange algorithm", "keyExchange");
}
}
protected TlsECConfig m_ecConfig;
protected TlsAgreement m_agreement;
public TlsECDHanonKeyExchange(int keyExchange)
: this(keyExchange, null)
{
}
public TlsECDHanonKeyExchange(int keyExchange, TlsECConfig ecConfig)
: base(CheckKeyExchange(keyExchange))
{
this.m_ecConfig = ecConfig;
}
public override void SkipServerCredentials()
{
}
public override void ProcessServerCredentials(TlsCredentials serverCredentials)
{
throw new TlsFatalAlert(AlertDescription.internal_error);
}
public override void ProcessServerCertificate(Certificate serverCertificate)
{
throw new TlsFatalAlert(AlertDescription.unexpected_message);
}
public override bool RequiresServerKeyExchange
{
get { return true; }
}
public override byte[] GenerateServerKeyExchange()
{
MemoryStream buf = new MemoryStream();
TlsEccUtilities.WriteECConfig(m_ecConfig, buf);
this.m_agreement = m_context.Crypto.CreateECDomain(m_ecConfig).CreateECDH();
GenerateEphemeral(buf);
return buf.ToArray();
}
public override void ProcessServerKeyExchange(Stream input)
{
this.m_ecConfig = TlsEccUtilities.ReceiveECDHConfig(m_context, input);
byte[] point = TlsUtilities.ReadOpaque8(input, 1);
this.m_agreement = m_context.Crypto.CreateECDomain(m_ecConfig).CreateECDH();
ProcessEphemeral(point);
}
public override short[] GetClientCertificateTypes()
{
return null;
}
public override void ProcessClientCredentials(TlsCredentials clientCredentials)
{
throw new TlsFatalAlert(AlertDescription.internal_error);
}
public override void GenerateClientKeyExchange(Stream output)
{
GenerateEphemeral(output);
}
public override void ProcessClientCertificate(Certificate clientCertificate)
{
throw new TlsFatalAlert(AlertDescription.unexpected_message);
}
public override void ProcessClientKeyExchange(Stream input)
{
byte[] point = TlsUtilities.ReadOpaque8(input, 1);
ProcessEphemeral(point);
}
public override TlsSecret GeneratePreMasterSecret()
{
return m_agreement.CalculateSecret();
}
protected virtual void GenerateEphemeral(Stream output)
{
byte[] point = m_agreement.GenerateEphemeral();
TlsUtilities.WriteOpaque8(point, output);
}
protected virtual void ProcessEphemeral(byte[] point)
{
TlsEccUtilities.CheckPointEncoding(m_ecConfig.NamedGroup, point);
this.m_agreement.ReceivePeerValue(point);
}
}
}
|