-
Notifications
You must be signed in to change notification settings - Fork 876
Expand file tree
/
Copy pathAuthenticationMessages.cs
More file actions
204 lines (165 loc) · 6.69 KB
/
AuthenticationMessages.cs
File metadata and controls
204 lines (165 loc) · 6.69 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
using System;
using System.Collections.Generic;
using Microsoft.Extensions.Logging;
using Npgsql.Internal;
namespace Npgsql.BackendMessages;
abstract class AuthenticationRequestMessage : IBackendMessage
{
public BackendMessageCode Code => BackendMessageCode.AuthenticationRequest;
internal abstract AuthenticationRequestType AuthRequestType { get; }
}
sealed class AuthenticationOkMessage : AuthenticationRequestMessage
{
internal override AuthenticationRequestType AuthRequestType => AuthenticationRequestType.Ok;
internal static readonly AuthenticationOkMessage Instance = new();
AuthenticationOkMessage() { }
}
sealed class AuthenticationCleartextPasswordMessage : AuthenticationRequestMessage
{
internal override AuthenticationRequestType AuthRequestType => AuthenticationRequestType.CleartextPassword;
internal static readonly AuthenticationCleartextPasswordMessage Instance = new();
AuthenticationCleartextPasswordMessage() { }
}
sealed class AuthenticationMD5PasswordMessage : AuthenticationRequestMessage
{
internal override AuthenticationRequestType AuthRequestType => AuthenticationRequestType.MD5Password;
internal byte[] Salt { get; }
internal static AuthenticationMD5PasswordMessage Load(NpgsqlReadBuffer buf)
{
var salt = new byte[4];
buf.ReadBytes(salt, 0, 4);
return new AuthenticationMD5PasswordMessage(salt);
}
AuthenticationMD5PasswordMessage(byte[] salt)
=> Salt = salt;
}
sealed class AuthenticationGSSMessage : AuthenticationRequestMessage
{
internal override AuthenticationRequestType AuthRequestType => AuthenticationRequestType.GSS;
internal static readonly AuthenticationGSSMessage Instance = new();
AuthenticationGSSMessage() { }
}
sealed class AuthenticationGSSContinueMessage : AuthenticationRequestMessage
{
internal override AuthenticationRequestType AuthRequestType => AuthenticationRequestType.GSSContinue;
internal byte[] AuthenticationData { get; }
internal static AuthenticationGSSContinueMessage Load(NpgsqlReadBuffer buf, int len)
{
len -= 4; // The AuthRequestType code
var authenticationData = new byte[len];
buf.ReadBytes(authenticationData, 0, len);
return new AuthenticationGSSContinueMessage(authenticationData);
}
AuthenticationGSSContinueMessage(byte[] authenticationData)
=> AuthenticationData = authenticationData;
}
sealed class AuthenticationSSPIMessage : AuthenticationRequestMessage
{
internal override AuthenticationRequestType AuthRequestType => AuthenticationRequestType.SSPI;
internal static readonly AuthenticationSSPIMessage Instance = new();
AuthenticationSSPIMessage() { }
}
#region SASL
sealed class AuthenticationSASLMessage : AuthenticationRequestMessage
{
internal override AuthenticationRequestType AuthRequestType => AuthenticationRequestType.SASL;
internal List<string> Mechanisms { get; } = [];
internal AuthenticationSASLMessage(NpgsqlReadBuffer buf)
{
while (buf.Buffer[buf.ReadPosition] != 0)
Mechanisms.Add(buf.ReadNullTerminatedString());
buf.ReadByte();
if (Mechanisms.Count == 0)
throw new NpgsqlException("Received AuthenticationSASL message with 0 mechanisms!");
}
}
sealed class AuthenticationSASLContinueMessage : AuthenticationRequestMessage
{
internal override AuthenticationRequestType AuthRequestType => AuthenticationRequestType.SASLContinue;
internal byte[] Payload { get; }
internal AuthenticationSASLContinueMessage(NpgsqlReadBuffer buf, int len)
{
Payload = new byte[len];
buf.ReadBytes(Payload, 0, len);
}
}
sealed class AuthenticationSCRAMServerFirstMessage
{
internal string Nonce { get; }
internal string Salt { get; }
internal int Iteration { get; }
internal static AuthenticationSCRAMServerFirstMessage Load(byte[] bytes, ILogger connectionLogger)
{
var data = NpgsqlWriteBuffer.UTF8Encoding.GetString(bytes);
string? nonce = null, salt = null;
var iteration = -1;
foreach (var part in data.Split(','))
{
if (part.StartsWith("r=", StringComparison.Ordinal))
nonce = part.Substring(2);
else if (part.StartsWith("s=", StringComparison.Ordinal))
salt = part.Substring(2);
else if (part.StartsWith("i=", StringComparison.Ordinal))
iteration = int.Parse(part.Substring(2));
else
connectionLogger.LogDebug("Unknown part in SCRAM server-first message:" + part);
}
if (nonce == null)
throw new NpgsqlException("Server nonce not received in SCRAM server-first message");
if (salt == null)
throw new NpgsqlException("Server salt not received in SCRAM server-first message");
if (iteration == -1)
throw new NpgsqlException("Server iterations not received in SCRAM server-first message");
return new AuthenticationSCRAMServerFirstMessage(nonce, salt, iteration);
}
AuthenticationSCRAMServerFirstMessage(string nonce, string salt, int iteration)
{
Nonce = nonce;
Salt = salt;
Iteration = iteration;
}
}
sealed class AuthenticationSASLFinalMessage : AuthenticationRequestMessage
{
internal override AuthenticationRequestType AuthRequestType => AuthenticationRequestType.SASLFinal;
internal byte[] Payload { get; }
internal AuthenticationSASLFinalMessage(NpgsqlReadBuffer buf, int len)
{
Payload = new byte[len];
buf.ReadBytes(Payload, 0, len);
}
}
sealed class AuthenticationSCRAMServerFinalMessage
{
internal string ServerSignature { get; }
internal static AuthenticationSCRAMServerFinalMessage Load(byte[] bytes, ILogger connectionLogger)
{
var data = NpgsqlWriteBuffer.UTF8Encoding.GetString(bytes);
string? serverSignature = null;
foreach (var part in data.Split(','))
{
if (part.StartsWith("v=", StringComparison.Ordinal))
serverSignature = part.Substring(2);
else
connectionLogger.LogDebug("Unknown part in SCRAM server-first message:" + part);
}
if (serverSignature == null)
throw new NpgsqlException("Server signature not received in SCRAM server-final message");
return new AuthenticationSCRAMServerFinalMessage(serverSignature);
}
internal AuthenticationSCRAMServerFinalMessage(string serverSignature)
=> ServerSignature = serverSignature;
}
#endregion SASL
enum AuthenticationRequestType
{
Ok = 0,
CleartextPassword = 3,
MD5Password = 5,
GSS = 7,
GSSContinue = 8,
SSPI = 9,
SASL = 10,
SASLContinue = 11,
SASLFinal = 12
}