forked from apache/cassandra-cpp-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdse_auth_gssapi.cpp
More file actions
512 lines (400 loc) · 15 KB
/
dse_auth_gssapi.cpp
File metadata and controls
512 lines (400 loc) · 15 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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/*
Copyright (c) DataStax, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "dse_auth_gssapi.hpp"
#include "logger.hpp"
#include "scoped_ptr.hpp"
#include "string_ref.hpp"
#include <string.h>
#include <gssapi/gssapi.h>
#include <gssapi/gssapi_generic.h>
#include <gssapi/gssapi_krb5.h>
#define DSE_AUTHENTICATOR "com.datastax.bdp.cassandra.auth.DseAuthenticator"
#define GSSAPI_AUTH_MECHANISM "GSSAPI"
#define GSSAPI_AUTH_SERVER_INITIAL_CHALLENGE "GSSAPI-START"
#define PLAINTEXT_AUTH_MECHANISM "PLAIN"
#define PLAINTEXT_AUTH_SERVER_INITIAL_CHALLENGE "PLAIN-START"
using namespace datastax;
using namespace datastax::internal::core;
using namespace datastax::internal::enterprise;
static void dse_gssapi_authenticator_nop_lock(void* data) {}
static void dse_gssapi_authenticator_nop_unlock(void* data) {}
struct GssapiBuffer {
public:
gss_buffer_desc buffer;
public:
GssapiBuffer() {
buffer.value = NULL;
buffer.length = 0;
}
~GssapiBuffer() { release(); }
const unsigned char* value() const { return static_cast<const unsigned char*>(buffer.value); }
const char* data() const { return static_cast<const char*>(buffer.value); }
size_t length() const { return buffer.length; }
bool is_empty() const { return buffer.length == 0; }
void release() {
if (buffer.value) {
OM_uint32 min_stat;
DseGssapiAuthenticator::lock();
gss_release_buffer(&min_stat, &buffer);
DseGssapiAuthenticator::unlock();
}
}
};
class GssapiName {
public:
gss_name_t name;
public:
GssapiName()
: name(GSS_C_NO_NAME) {}
~GssapiName() { release(); }
void release() {
if (name != GSS_C_NO_NAME) {
OM_uint32 min_stat;
DseGssapiAuthenticator::lock();
gss_release_name(&min_stat, &name);
DseGssapiAuthenticator::unlock();
}
}
};
namespace datastax { namespace internal { namespace enterprise {
class GssapiAuthenticatorImpl : public Allocated {
public:
enum State { NEGOTIATION, AUTHENTICATION, AUTHENTICATED };
enum Result { RESULT_ERROR, RESULT_CONTINUE, RESULT_COMPLETE };
enum Type { AUTH_NONE = 1, AUTH_INTEGRITY = 2, AUTH_CONFIDENTIALITY = 3 };
GssapiAuthenticatorImpl(const String& authorization_id);
~GssapiAuthenticatorImpl();
const String& response() const { return response_; }
const String& error() const { return error_; }
Result init(const String& service, const String& principal);
Result process(const String& token);
private:
Result negotiate(gss_buffer_t challenge_token);
Result authenticate(gss_buffer_t challenge_token);
static String display_status(OM_uint32 maj, OM_uint32 min);
private:
gss_ctx_id_t context_;
gss_name_t server_name_;
OM_uint32 gss_flags_;
gss_cred_id_t client_creds_;
String username_;
String response_;
String error_;
State state_;
String authorization_id_;
};
}}} // namespace datastax::internal::enterprise
GssapiAuthenticatorImpl::GssapiAuthenticatorImpl(const String& authorization_id)
: context_(GSS_C_NO_CONTEXT)
, server_name_(GSS_C_NO_NAME)
, gss_flags_(GSS_C_MUTUAL_FLAG | GSS_C_SEQUENCE_FLAG)
, client_creds_(GSS_C_NO_CREDENTIAL)
, state_(NEGOTIATION)
, authorization_id_(authorization_id) {}
GssapiAuthenticatorImpl::~GssapiAuthenticatorImpl() {
OM_uint32 min_stat;
if (context_ != GSS_C_NO_CONTEXT) {
DseGssapiAuthenticator::lock();
gss_delete_sec_context(&min_stat, &context_, GSS_C_NO_BUFFER);
DseGssapiAuthenticator::unlock();
}
if (server_name_ != GSS_C_NO_NAME) {
DseGssapiAuthenticator::lock();
gss_release_name(&min_stat, &server_name_);
DseGssapiAuthenticator::unlock();
}
if (client_creds_ != GSS_C_NO_CREDENTIAL) {
DseGssapiAuthenticator::lock();
gss_release_cred(&min_stat, &client_creds_);
DseGssapiAuthenticator::unlock();
}
}
GssapiAuthenticatorImpl::Result GssapiAuthenticatorImpl::init(const String& service,
const String& principal) {
OM_uint32 maj_stat;
OM_uint32 min_stat;
gss_buffer_desc name_token = GSS_C_EMPTY_BUFFER;
name_token.value = const_cast<void*>(static_cast<const void*>(service.c_str()));
name_token.length = service.size();
DseGssapiAuthenticator::lock();
maj_stat = gss_import_name(&min_stat, &name_token, GSS_C_NT_HOSTBASED_SERVICE, &server_name_);
DseGssapiAuthenticator::unlock();
if (GSS_ERROR(maj_stat)) {
error_.assign("Failed to import server name (gss_import_name()): " +
display_status(maj_stat, min_stat));
return RESULT_ERROR;
}
GssapiName principal_name; // Initialized to GSS_C_NO_NAME
if (!principal.empty()) {
gss_buffer_desc principal_token = GSS_C_EMPTY_BUFFER;
principal_token.value = const_cast<void*>(static_cast<const void*>(principal.c_str()));
principal_token.length = principal.size();
DseGssapiAuthenticator::lock();
maj_stat =
gss_import_name(&min_stat, &principal_token, GSS_C_NT_USER_NAME, &principal_name.name);
DseGssapiAuthenticator::unlock();
if (GSS_ERROR(maj_stat)) {
error_.assign("Failed to import principal name (gss_import_name()): " +
display_status(maj_stat, min_stat));
return RESULT_ERROR;
}
}
DseGssapiAuthenticator::lock();
maj_stat = gss_acquire_cred(&min_stat, principal_name.name, GSS_C_INDEFINITE, GSS_C_NO_OID_SET,
GSS_C_INITIATE, &client_creds_, NULL, NULL);
DseGssapiAuthenticator::unlock();
if (GSS_ERROR(maj_stat)) {
error_.assign("Failed to acquire principal credentials (gss_acquire_cred()): " +
display_status(maj_stat, min_stat));
return RESULT_ERROR;
}
return RESULT_COMPLETE;
}
GssapiAuthenticatorImpl::Result GssapiAuthenticatorImpl::negotiate(gss_buffer_t challenge_token) {
OM_uint32 maj_stat;
OM_uint32 min_stat;
GssapiBuffer output_token;
Result result = RESULT_ERROR;
DseGssapiAuthenticator::lock();
maj_stat = gss_init_sec_context(&min_stat, client_creds_, &context_, server_name_, GSS_C_NO_OID,
gss_flags_, 0, GSS_C_NO_CHANNEL_BINDINGS, challenge_token, NULL,
&output_token.buffer, NULL, NULL);
DseGssapiAuthenticator::unlock();
if (maj_stat != GSS_S_COMPLETE && maj_stat != GSS_S_CONTINUE_NEEDED) {
error_.assign("Failed to initalize security context (gss_init_sec_context()): " +
display_status(maj_stat, min_stat));
return RESULT_ERROR;
}
result = (maj_stat == GSS_S_COMPLETE) ? RESULT_COMPLETE : RESULT_CONTINUE;
if (!output_token.is_empty()) {
response_.assign(output_token.data(), output_token.length());
}
if (result == RESULT_COMPLETE) {
GssapiName user;
DseGssapiAuthenticator::lock();
maj_stat =
gss_inquire_context(&min_stat, context_, &user.name, NULL, NULL, NULL, NULL, NULL, NULL);
DseGssapiAuthenticator::unlock();
if (GSS_ERROR(maj_stat)) {
error_.assign(
"Failed to inquire security context for user principal (gss_inquire_context()): " +
display_status(maj_stat, min_stat));
return RESULT_ERROR;
}
GssapiBuffer user_token;
DseGssapiAuthenticator::lock();
maj_stat = gss_display_name(&min_stat, user.name, &user_token.buffer, NULL);
DseGssapiAuthenticator::unlock();
if (GSS_ERROR(maj_stat)) {
error_.assign("Failed to get display name for user principal (gss_inquire_context()): " +
display_status(maj_stat, min_stat));
return RESULT_ERROR;
} else {
username_.assign(user_token.data(), user_token.length());
state_ = AUTHENTICATION;
}
}
return result;
}
GssapiAuthenticatorImpl::Result
GssapiAuthenticatorImpl::authenticate(gss_buffer_t challenge_token) {
OM_uint32 maj_stat;
OM_uint32 min_stat;
OM_uint32 req_output_size;
OM_uint32 max_input_size;
unsigned char qop;
String input;
gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
GssapiBuffer output_token;
DseGssapiAuthenticator::lock();
maj_stat = gss_unwrap(&min_stat, context_, challenge_token, &output_token.buffer, NULL, NULL);
DseGssapiAuthenticator::unlock();
if (GSS_ERROR(maj_stat)) {
error_.assign("Failed to get unwrap challenge token (gss_unwrap()): " +
display_status(maj_stat, min_stat));
return RESULT_ERROR;
}
if (output_token.length() != 4) {
return RESULT_ERROR;
}
qop = output_token.value()[0];
if (qop & AUTH_CONFIDENTIALITY) {
qop = AUTH_CONFIDENTIALITY;
} else if (qop & AUTH_INTEGRITY) {
qop = AUTH_INTEGRITY;
} else {
qop = AUTH_NONE;
}
req_output_size = ((output_token.value())[1] << 16) | ((output_token.value())[2] << 8) |
((output_token.value())[3] << 0);
req_output_size = req_output_size & 0xFFFFFF;
DseGssapiAuthenticator::lock();
maj_stat = gss_wrap_size_limit(&min_stat, context_, 1, GSS_C_QOP_DEFAULT, req_output_size,
&max_input_size);
DseGssapiAuthenticator::unlock();
if (max_input_size < req_output_size) {
req_output_size = max_input_size;
}
input.push_back(qop);
input.push_back((req_output_size >> 16) & 0xFF);
input.push_back((req_output_size >> 8) & 0xFF);
input.push_back((req_output_size >> 0) & 0xFF);
// Send the authorization_id if present (proxy login), otherwise the username.
const String& authorization_id = authorization_id_.empty() ? username_ : authorization_id_;
input.append(authorization_id);
input_token.length = 4 + authorization_id.size();
input_token.value = const_cast<void*>(static_cast<const void*>(input.c_str()));
output_token.release();
DseGssapiAuthenticator::lock();
maj_stat =
gss_wrap(&min_stat, context_, 0, GSS_C_QOP_DEFAULT, &input_token, NULL, &output_token.buffer);
DseGssapiAuthenticator::unlock();
if (GSS_ERROR(maj_stat)) {
error_.assign("Failed to get wrape response token (gss_wrap()): " +
display_status(maj_stat, min_stat));
return RESULT_ERROR;
}
if (!output_token.is_empty()) {
response_.assign(output_token.data(), output_token.length());
}
state_ = AUTHENTICATED;
return RESULT_COMPLETE;
}
String GssapiAuthenticatorImpl::display_status(OM_uint32 maj, OM_uint32 min) {
String error;
OM_uint32 message_context;
message_context = 0;
do {
GssapiBuffer message;
OM_uint32 maj_stat, min_stat;
DseGssapiAuthenticator::lock();
maj_stat = gss_display_status(&min_stat, maj, GSS_C_GSS_CODE, GSS_C_NO_OID, &message_context,
&message.buffer);
DseGssapiAuthenticator::unlock();
if (GSS_ERROR(maj_stat)) {
error.append("GSSAPI error: (unable to get major error)");
break;
}
error.append(message.data(), message.length());
} while (message_context != 0);
message_context = 0;
error.append(" (");
do {
GssapiBuffer message;
OM_uint32 maj_stat, min_stat;
DseGssapiAuthenticator::lock();
maj_stat = gss_display_status(&min_stat, min, GSS_C_MECH_CODE, GSS_C_NO_OID, &message_context,
&message.buffer);
DseGssapiAuthenticator::unlock();
if (GSS_ERROR(maj_stat)) {
error.append("GSSAPI error: (unable to get minor error)");
break;
}
error.append(message.data(), message.length());
} while (message_context != 0);
error.append(" )");
return error;
}
GssapiAuthenticatorImpl::Result GssapiAuthenticatorImpl::process(const String& token) {
Result result = RESULT_ERROR;
gss_buffer_desc challenge_token = GSS_C_EMPTY_BUFFER;
response_.clear();
if (!token.empty()) {
challenge_token.value = const_cast<char*>(token.c_str());
challenge_token.length = token.length();
}
switch (state_) {
case NEGOTIATION:
result = negotiate(&challenge_token);
break;
case AUTHENTICATION:
result = authenticate(&challenge_token);
break;
default:
break;
}
return result;
}
DseGssapiAuthenticatorLockCallback DseGssapiAuthenticator::lock_callback_ =
dse_gssapi_authenticator_nop_lock;
DseGssapiAuthenticatorUnlockCallback DseGssapiAuthenticator::unlock_callback_ =
dse_gssapi_authenticator_nop_unlock;
void* DseGssapiAuthenticator::data_ = NULL;
CassError
DseGssapiAuthenticator::set_lock_callbacks(DseGssapiAuthenticatorLockCallback lock_callback,
DseGssapiAuthenticatorUnlockCallback unlock_callback,
void* data) {
if (lock_callback == NULL || unlock_callback == NULL || data_ == NULL) {
lock_callback_ = dse_gssapi_authenticator_nop_lock;
unlock_callback_ = dse_gssapi_authenticator_nop_unlock;
data_ = NULL;
return CASS_ERROR_LIB_BAD_PARAMS;
} else {
lock_callback_ = lock_callback;
unlock_callback_ = unlock_callback;
data_ = data;
return CASS_OK;
}
}
DseGssapiAuthenticator::DseGssapiAuthenticator(const Address& address, const String& hostname,
const String& class_name, const String& service,
const String& principal,
const String& authorization_id)
: address_(address)
, hostname_(hostname)
, class_name_(class_name)
, service_(service)
, principal_(principal)
, authorization_id_(authorization_id)
, impl_(new GssapiAuthenticatorImpl(authorization_id)) {}
bool DseGssapiAuthenticator::initial_response(String* response) {
String service;
if (hostname_.empty()) {
service.append(service_);
service.append("@");
service.append(address_.to_string());
} else {
service.append(service_);
service.append("@");
service.append(hostname_);
}
if (impl_->init(service, principal_) == GssapiAuthenticatorImpl::RESULT_ERROR) {
set_error("Unable to initialize GSSAPI: " + impl_->error());
return false;
}
if (class_name_ == DSE_AUTHENTICATOR) {
response->assign(GSSAPI_AUTH_MECHANISM);
return true;
} else {
return evaluate_challenge(GSSAPI_AUTH_SERVER_INITIAL_CHALLENGE, response);
}
}
bool DseGssapiAuthenticator::evaluate_challenge(const String& token, String* response) {
if (token == GSSAPI_AUTH_SERVER_INITIAL_CHALLENGE) {
if (impl_->process(String()) == GssapiAuthenticatorImpl::RESULT_ERROR) {
set_error("GSSAPI initial handshake failed: " + impl_->error());
return false;
}
} else {
if (impl_->process(token) == GssapiAuthenticatorImpl::RESULT_ERROR) {
set_error("GSSAPI challenge handshake failed: " + impl_->error());
return false;
}
}
*response = impl_->response();
return true;
}
bool DseGssapiAuthenticator::success(const String& token) {
// no-op
return true;
}