-
Notifications
You must be signed in to change notification settings - Fork 765
Expand file tree
/
Copy pathscram_algorithm.h
More file actions
407 lines (353 loc) · 13.2 KB
/
Copy pathscram_algorithm.h
File metadata and controls
407 lines (353 loc) · 13.2 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*
* Copyright 2020 Redpanda Data, Inc.
*
* Use of this software is governed by the Business Source License
* included in the file licenses/BSL.md
*
* As of the Change Date specified in that file, in accordance with
* the Business Source License, use of this software will be governed
* by the Apache License, Version 2.0
*/
#pragma once
#include "absl/container/node_hash_map.h"
#include "base/format_to.h"
#include "bytes/bytes.h"
#include "bytes/random.h"
#include "hashing/secure.h"
#include "model/timestamp.h"
#include "net/types.h"
#include "security/scram_credential.h"
#include "ssx/sformat.h"
#include "utils/base64.h"
/**
* scram algorthm - https://tools.ietf.org/html/rfc5802
*
* The implementation follows closely the rfc, with some scenarios and
* configuration not handled by kafka omitted from the implementation. The kafka
* implementation is closely based on the rfc as well, so they are fairly easy
* to compare.
*
* TODO
* ====
*
* 1. unnecessary copying from std::array<char> for hash digest types and
* built-in bytes type. the conversion is mostly mechanical, but std::array
* needs to be propagated to all other types which need to be templated on the
* same scram_algorithm template parameters.
*/
namespace security {
class scram_exception final : public net::authentication_exception {
public:
explicit scram_exception(ss::sstring msg) noexcept
: net::authentication_exception(std::move(msg)) {}
};
/**
* First message received by the server.
*/
class client_first_message {
public:
client_first_message(ss::sstring username, ss::sstring nonce)
: _username(std::move(username))
, _nonce(std::move(nonce)) {}
explicit client_first_message(bytes_view data);
client_first_message(client_first_message&&) = delete;
client_first_message& operator=(client_first_message&&) = delete;
client_first_message(const client_first_message&) = delete;
client_first_message& operator=(const client_first_message&) = delete;
~client_first_message() noexcept = default;
const ss::sstring& authzid() const { return _authzid; }
const ss::sstring& username() const { return _username; }
ss::sstring username_normalized() const;
const ss::sstring& nonce() const { return _nonce; }
ss::sstring bare_message() const {
return ssx::sformat("n={},r={}", _username, _nonce);
}
ss::sstring message() const {
return ssx::sformat("n,{},{}", _authzid, bare_message());
}
bool token_authenticated() const;
fmt::iterator format_to(fmt::iterator it) const {
return fmt::format_to(it, "{{client_first_message}}");
}
private:
ss::sstring _authzid;
ss::sstring _username;
ss::sstring _nonce;
absl::node_hash_map<ss::sstring, ss::sstring> _extensions;
};
/**
* Reply to client's first message.
*/
class server_first_message {
public:
server_first_message(
const ss::sstring& client_nonce,
const ss::sstring& server_nonce,
bytes salt,
int iterations) noexcept
: _nonce(client_nonce + server_nonce)
, _salt(std::move(salt))
, _iterations(iterations) {}
explicit server_first_message(bytes_view);
server_first_message(server_first_message&&) noexcept = default;
server_first_message& operator=(server_first_message&&) = delete;
server_first_message(const server_first_message&) = delete;
server_first_message& operator=(const server_first_message&) = delete;
~server_first_message() noexcept = default;
ss::sstring sasl_message() const;
const ss::sstring& nonce() const { return _nonce; }
const bytes& salt() const { return _salt; }
int iterations() const { return _iterations; }
private:
friend std::ostream& operator<<(std::ostream&, const server_first_message&);
ss::sstring _nonce;
bytes _salt;
int _iterations;
};
/**
* Final client message expected by server.
*/
class client_final_message {
public:
explicit client_final_message(bytes_view data);
client_final_message(bytes channel_binding, ss::sstring nonce)
: _channel_binding(std::move(channel_binding))
, _nonce(std::move(nonce)) {}
client_final_message(client_final_message&&) = delete;
client_final_message& operator=(client_final_message&&) = delete;
client_final_message(const client_final_message&) = delete;
client_final_message& operator=(const client_final_message&) = delete;
~client_final_message() noexcept = default;
const ss::sstring& nonce() const { return _nonce; }
const bytes& proof() const { return _proof; }
const bytes& channel_binding() const { return _channel_binding; }
ss::sstring msg_no_proof() const;
void set_proof(bytes proof) { _proof = std::move(proof); }
ss::sstring message() const {
return ssx::sformat("{},p={}", msg_no_proof(), bytes_to_base64(_proof));
}
fmt::iterator format_to(fmt::iterator it) const {
return fmt::format_to(it, "{{client_final_message}}");
}
private:
bytes _channel_binding;
ss::sstring _nonce;
ss::sstring _extensions;
bytes _proof;
};
/**
* Final reply from the server.
*/
class server_final_message {
public:
server_final_message(
std::optional<ss::sstring> error, bytes signature) noexcept
: _error(std::move(error))
, _signature(std::move(signature)) {}
explicit server_final_message(bytes_view);
server_final_message(server_final_message&&) noexcept = default;
server_final_message& operator=(server_final_message&&) = delete;
server_final_message(const server_final_message&) = delete;
server_final_message& operator=(const server_final_message&) = delete;
~server_final_message() noexcept = default;
ss::sstring sasl_message() const;
const std::optional<ss::sstring>& error() const { return _error; }
const bytes& signature() const { return _signature; }
private:
friend std::ostream& operator<<(std::ostream&, const server_final_message&);
std::optional<ss::sstring> _error;
bytes _signature;
};
template<
typename MacType,
typename HashType,
size_t SaltSize,
int MinIterations>
class scram_algorithm {
public:
static constexpr int min_iterations = MinIterations;
static_assert(min_iterations > 0, "Minimum iterations must be positive");
static constexpr auto key_size = HashType::digest_size;
static bytes client_signature(
bytes_view stored_key,
const client_first_message& client_first,
const server_first_message& server_first,
const client_final_message& client_final) {
MacType mac(stored_key);
mac.update(auth_message(client_first, server_first, client_final));
auto result = mac.reset();
return bytes(result.begin(), result.end());
}
static bytes server_signature(
bytes_view server_key,
const client_first_message& client_first,
const server_first_message& server_first,
const client_final_message& client_final) {
MacType mac(server_key);
mac.update(auth_message(client_first, server_first, client_final));
auto result = mac.reset();
return bytes(result.begin(), result.end());
}
static bytes
computed_stored_key(bytes_view client_signature, bytes_view client_proof) {
HashType hash;
hash.update(client_signature ^ client_proof);
auto result = hash.reset();
return bytes(result.begin(), result.end());
}
/**
* helper for building credentials either for building test cases or
* computing credentials to be stored based on client requests.
*/
static scram_credential make_credentials(
const ss::sstring& password,
int iterations,
model::timestamp password_set_at = model::timestamp::now()) {
bytes salt = random_generators::get_crypto_bytes(SaltSize);
bytes salted_password = salt_password(password, salt, iterations);
return make_credentials(
std::nullopt, salted_password, salt, iterations, password_set_at);
}
static scram_credential make_credentials(
acl_principal principal,
const ss::sstring& password,
int iterations,
model::timestamp password_set_at = model::timestamp::now()) {
bytes salt = random_generators::get_crypto_bytes(SaltSize);
bytes salted_password = salt_password(password, salt, iterations);
auto clientkey = client_key(salted_password);
auto storedkey = stored_key(clientkey);
auto serverkey = server_key(salted_password);
return make_credentials(
std::move(principal),
salted_password,
salt,
iterations,
password_set_at);
}
static scram_credential make_credentials(
std::optional<acl_principal> principal,
bytes_view salted_password,
bytes_view salt,
int iterations,
model::timestamp password_set_at = model::timestamp::now()) {
auto clientkey = client_key(salted_password);
auto storedkey = stored_key(clientkey);
auto serverkey = server_key(salted_password);
return {
bytes{salt},
std::move(serverkey),
std::move(storedkey),
iterations,
std::move(principal),
password_set_at};
}
/// Test method used to generate credentials and it returns the salted
/// password
static std::pair<scram_credential, bytes>
make_credentials_and_return_password(
const ss::sstring& password,
int iterations,
model::timestamp password_set_at = model::timestamp::now()) {
bytes salt = random_generators::get_crypto_bytes(SaltSize);
bytes salted_password = salt_password(password, salt, iterations);
return {
make_credentials(
std::nullopt, salted_password, salt, iterations, password_set_at),
std::move(salted_password)};
}
static bytes client_proof(
bytes_view salted_password,
const client_first_message& client_first,
const server_first_message& server_first,
const client_final_message& client_final) {
auto c_key = client_key(salted_password);
HashType hash;
hash.update(c_key);
auto stored_key = hash.reset();
auto c_signature = client_signature(
bytes(stored_key.begin(), stored_key.end()),
client_first,
server_first,
client_final);
return c_key ^ c_signature;
}
static bytes hi(bytes_view str, bytes_view salt, int iterations) {
MacType mac(str);
mac.update(salt);
mac.update(std::array<char, 4>{0, 0, 0, 1});
auto u1 = mac.reset();
auto prev = u1;
auto result = u1;
for (int i = 2; i <= iterations; i++) {
mac.update(prev);
auto ui = mac.reset();
result = result ^ ui;
prev = ui;
}
return bytes(result.begin(), result.end());
}
static bytes server_key(bytes_view salted_password) {
MacType mac(salted_password);
mac.update("Server Key");
auto result = mac.reset();
return bytes(result.begin(), result.end());
}
/**
* Computes the stored key for a plaintext password, as persisted in a
* scram_credential. This is the expensive part of non-SCRAM password
* validation: the salted password derivation runs `iterations` HMAC
* rounds.
*/
static bytes derive_stored_key(
const ss::sstring& password, bytes_view salt, int iterations) {
auto salted_password = salt_password(password, salt, iterations);
return stored_key(client_key(salted_password));
}
/**
* For doing non-SCRAM authentication, such as HTTP basic auth over TLS,
* using stored SCRAM credentials.
*/
static bool validate_password(
const ss::sstring& password,
bytes_view reference_stored_key,
bytes_view salt,
int iterations) {
return derive_stored_key(password, salt, iterations)
== reference_stored_key;
}
private:
static bytes salt_password(
const ss::sstring& password, bytes_view salt, int iterations) {
bytes password_bytes(password.begin(), password.end());
return hi(password_bytes, salt, iterations);
}
static bytes client_key(bytes_view salted_password) {
MacType mac(salted_password);
mac.update("Client Key");
auto result = mac.reset();
return bytes(result.begin(), result.end());
}
static bytes stored_key(bytes_view client_key) {
HashType hash;
hash.update(client_key);
auto result = hash.reset();
return bytes(result.begin(), result.end());
}
static ss::sstring auth_message(
const client_first_message& client_first,
const server_first_message& server_first,
const client_final_message& client_final) {
return ssx::sformat(
"{},{},{}",
client_first.bare_message(),
server_first.sasl_message(),
client_final.msg_no_proof());
}
};
bool validate_scram_username(std::string_view username);
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
using scram_sha512 = scram_algorithm<hmac_sha512, hash_sha512, 130, 4096>;
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
using scram_sha256 = scram_algorithm<hmac_sha256, hash_sha256, 130, 4096>;
} // namespace security