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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
* Ceph - scalable distributed file system
*
* Copyright (C) 2009-2011 New Dream Network
*
* This is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software
* Foundation. See file COPYING.
*
*/
#include "CephxProtocol.h"
#include "common/Clock.h"
#include "common/ceph_context.h"
#include "common/config.h"
#include "common/debug.h"
#include "include/buffer.h"
#define dout_subsys ceph_subsys_auth
#undef dout_prefix
#define dout_prefix *_dout << "cephx: "
void cephx_calc_client_server_challenge(CephContext *cct, CryptoKey& secret, uint64_t server_challenge,
uint64_t client_challenge, uint64_t *key, std::string &error)
{
CephXChallengeBlob b;
b.server_challenge = server_challenge;
b.client_challenge = client_challenge;
bufferlist enc;
if (encode_encrypt(cct, b, secret, enc, error))
return;
uint64_t k = 0;
const ceph_le64 *p = (const ceph_le64 *)enc.c_str();
for (int pos = 0; pos + sizeof(k) <= enc.length(); pos+=sizeof(k), p++)
k ^= *p;
*key = k;
}
/*
* Authentication
*/
bool cephx_build_service_ticket_blob(CephContext *cct, CephXSessionAuthInfo& info,
CephXTicketBlob& blob)
{
CephXServiceTicketInfo ticket_info;
ticket_info.session_key = info.session_key;
ticket_info.ticket = info.ticket;
ticket_info.ticket.caps = info.ticket.caps;
ldout(cct, 10) << "build_service_ticket service "
<< ceph_entity_type_name(info.service_id)
<< " secret_id " << info.secret_id
<< " ticket_info.ticket.name="
<< ticket_info.ticket.name.to_str()
<< " ticket.global_id " << info.ticket.global_id << dendl;
blob.secret_id = info.secret_id;
std::string error;
if (!info.service_secret.get_secret().length())
error = "invalid key"; // Bad key?
else
encode_encrypt_enc_bl(cct, ticket_info, info.service_secret, blob.blob, error);
if (!error.empty()) {
ldout(cct, -1) << "cephx_build_service_ticket_blob failed with error "
<< error << dendl;
return false;
}
return true;
}
/*
* AUTH SERVER: authenticate
*
* Authenticate principal, respond with AuthServiceTicketInfo
*
* {session key, validity}^principal_secret
* {principal_ticket, session key}^service_secret ... "enc_ticket"
*/
bool cephx_build_service_ticket_reply(CephContext *cct,
CryptoKey& principal_secret,
vector<CephXSessionAuthInfo> ticket_info_vec,
bool should_encrypt_ticket,
CryptoKey& ticket_enc_key,
bufferlist& reply)
{
__u8 service_ticket_reply_v = 1;
encode(service_ticket_reply_v, reply);
uint32_t num = ticket_info_vec.size();
encode(num, reply);
ldout(cct, 10) << "build_service_ticket_reply encoding " << num
<< " tickets with secret " << principal_secret << dendl;
for (vector<CephXSessionAuthInfo>::iterator ticket_iter = ticket_info_vec.begin();
ticket_iter != ticket_info_vec.end();
++ticket_iter) {
CephXSessionAuthInfo& info = *ticket_iter;
encode(info.service_id, reply);
__u8 service_ticket_v = 1;
encode(service_ticket_v, reply);
CephXServiceTicket msg_a;
msg_a.session_key = info.session_key;
msg_a.validity = info.validity;
std::string error;
if (encode_encrypt(cct, msg_a, principal_secret, reply, error)) {
ldout(cct, -1) << "error encoding encrypted: " << error << dendl;
return false;
}
bufferlist service_ticket_bl;
CephXTicketBlob blob;
if (!cephx_build_service_ticket_blob(cct, info, blob)) {
return false;
}
encode(blob, service_ticket_bl);
ldout(cct, 30) << "service_ticket_blob is ";
service_ticket_bl.hexdump(*_dout);
*_dout << dendl;
encode((__u8)should_encrypt_ticket, reply);
if (should_encrypt_ticket) {
if (encode_encrypt(cct, service_ticket_bl, ticket_enc_key, reply, error)) {
ldout(cct, -1) << "error encoding encrypted ticket: " << error << dendl;
return false;
}
} else {
encode(service_ticket_bl, reply);
}
}
return true;
}
/*
* PRINCIPAL: verify our attempt to authenticate succeeded. fill out
* this ServiceTicket with the result.
*/
bool CephXTicketHandler::verify_service_ticket_reply(
CryptoKey& secret,
bufferlist::const_iterator& indata)
{
try {
__u8 service_ticket_v;
decode(service_ticket_v, indata);
CephXServiceTicket msg_a;
std::string error;
if (decode_decrypt(cct, msg_a, secret, indata, error)) {
ldout(cct, 0) << __func__ << " failed decode_decrypt, error is: " << error
<< dendl;
return false;
}
__u8 ticket_enc;
decode(ticket_enc, indata);
bufferlist service_ticket_bl;
if (ticket_enc) {
ldout(cct, 10) << __func__ << " got encrypted ticket" << dendl;
std::string error;
if (decode_decrypt(cct, service_ticket_bl, session_key, indata, error)) {
ldout(cct, 10) << __func__ << " decode_decrypt failed "
<< "with " << error << dendl;
return false;
}
} else {
decode(service_ticket_bl, indata);
}
auto iter = service_ticket_bl.cbegin();
decode(ticket, iter);
ldout(cct, 10) << __func__ << " ticket.secret_id=" << ticket.secret_id
<< dendl;
ldout(cct, 10) << __func__ << " service "
<< ceph_entity_type_name(service_id)
<< " secret_id " << ticket.secret_id
<< " session_key " << msg_a.session_key
<< " validity=" << msg_a.validity << dendl;
session_key = msg_a.session_key;
if (!msg_a.validity.is_zero()) {
expires = ceph_clock_now();
expires += msg_a.validity;
renew_after = expires;
renew_after -= ((double)msg_a.validity.sec() / 4);
ldout(cct, 10) << __func__ << " ticket expires=" << expires
<< " renew_after=" << renew_after << dendl;
}
have_key_flag = true;
return true;
} catch (buffer::error& e) {
ldout(cct, 1) << __func__ << " decode error: " << e.what() << dendl;
return false;
}
}
bool CephXTicketHandler::have_key()
{
if (have_key_flag) {
have_key_flag = ceph_clock_now() < expires;
}
return have_key_flag;
}
bool CephXTicketHandler::need_key() const
{
if (have_key_flag) {
return (!expires.is_zero()) && (ceph_clock_now() >= renew_after);
}
return true;
}
bool CephXTicketManager::have_key(uint32_t service_id)
{
map<uint32_t, CephXTicketHandler>::iterator iter = tickets_map.find(service_id);
if (iter == tickets_map.end())
return false;
return iter->second.have_key();
}
bool CephXTicketManager::need_key(uint32_t service_id) const
{
map<uint32_t, CephXTicketHandler>::const_iterator iter = tickets_map.find(service_id);
if (iter == tickets_map.end())
return true;
return iter->second.need_key();
}
void CephXTicketManager::set_have_need_key(uint32_t service_id, uint32_t& have, uint32_t& need)
{
map<uint32_t, CephXTicketHandler>::iterator iter = tickets_map.find(service_id);
if (iter == tickets_map.end()) {
have &= ~service_id;
need |= service_id;
ldout(cct, 10) << "set_have_need_key no handler for service "
<< ceph_entity_type_name(service_id) << dendl;
return;
}
//ldout(cct, 10) << "set_have_need_key service " << ceph_entity_type_name(service_id)
//<< " (" << service_id << ")"
//<< " need=" << iter->second.need_key() << " have=" << iter->second.have_key() << dendl;
if (iter->second.need_key())
need |= service_id;
else
need &= ~service_id;
if (iter->second.have_key())
have |= service_id;
else
have &= ~service_id;
}
void CephXTicketManager::invalidate_ticket(uint32_t service_id)
{
map<uint32_t, CephXTicketHandler>::iterator iter = tickets_map.find(service_id);
if (iter != tickets_map.end())
iter->second.invalidate_ticket();
}
/*
* PRINCIPAL: verify our attempt to authenticate succeeded. fill out
* this ServiceTicket with the result.
*/
bool CephXTicketManager::verify_service_ticket_reply(CryptoKey& secret,
bufferlist::const_iterator& indata)
{
__u8 service_ticket_reply_v;
uint32_t num;
try {
decode(service_ticket_reply_v, indata);
decode(num, indata);
} catch (buffer::error& e) {
ldout(cct, 10) << __func__ << " failed to decode ticket v or count: "
<< e.what() << dendl;
}
ldout(cct, 10) << "verify_service_ticket_reply got " << num << " keys" << dendl;
for (int i=0; i<(int)num; i++) {
uint32_t type;
try {
decode(type, indata);
} catch (buffer::error& e) {
ldout(cct, 10) << __func__ << " failed to decode ticket type: " << e.what()
<< dendl;
}
ldout(cct, 10) << "got key for service_id " << ceph_entity_type_name(type) << dendl;
CephXTicketHandler& handler = get_handler(type);
if (!handler.verify_service_ticket_reply(secret, indata)) {
return false;
}
handler.service_id = type;
}
return true;
}
/*
* PRINCIPAL: build authorizer to access the service.
*
* ticket, {timestamp}^session_key
*/
CephXAuthorizer *CephXTicketHandler::build_authorizer(uint64_t global_id) const
{
CephXAuthorizer *a = new CephXAuthorizer(cct);
a->session_key = session_key;
cct->random()->get_bytes((char*)&a->nonce, sizeof(a->nonce));
__u8 authorizer_v = 1; // see AUTH_MODE_* in Auth.h
encode(authorizer_v, a->bl);
encode(global_id, a->bl);
encode(service_id, a->bl);
encode(ticket, a->bl);
a->base_bl = a->bl;
CephXAuthorize msg;
msg.nonce = a->nonce;
std::string error;
if (encode_encrypt(cct, msg, session_key, a->bl, error)) {
ldout(cct, 0) << "failed to encrypt authorizer: " << error << dendl;
delete a;
return 0;
}
return a;
}
/*
* PRINCIPAL: build authorizer to access the service.
*
* ticket, {timestamp}^session_key
*/
CephXAuthorizer *CephXTicketManager::build_authorizer(uint32_t service_id) const
{
map<uint32_t, CephXTicketHandler>::const_iterator iter = tickets_map.find(service_id);
if (iter == tickets_map.end()) {
ldout(cct, 0) << "no TicketHandler for service "
<< ceph_entity_type_name(service_id) << dendl;
return NULL;
}
const CephXTicketHandler& handler = iter->second;
return handler.build_authorizer(global_id);
}
void CephXTicketManager::validate_tickets(uint32_t mask, uint32_t& have, uint32_t& need)
{
uint32_t i;
need = 0;
for (i = 1; i<=mask; i<<=1) {
if (mask & i) {
set_have_need_key(i, have, need);
}
}
ldout(cct, 10) << "validate_tickets want " << mask << " have " << have
<< " need " << need << dendl;
}
bool cephx_decode_ticket(CephContext *cct, KeyStore *keys,
uint32_t service_id,
const CephXTicketBlob& ticket_blob,
CephXServiceTicketInfo& ticket_info)
{
uint64_t secret_id = ticket_blob.secret_id;
CryptoKey service_secret;
if (!ticket_blob.blob.length()) {
return false;
}
if (secret_id == (uint64_t)-1) {
if (!keys->get_secret(cct->_conf->name, service_secret)) {
ldout(cct, 0) << "ceph_decode_ticket could not get general service secret for service_id="
<< ceph_entity_type_name(service_id) << " secret_id=" << secret_id << dendl;
return false;
}
} else {
if (!keys->get_service_secret(service_id, secret_id, service_secret)) {
ldout(cct, 0) << "ceph_decode_ticket could not get service secret for service_id="
<< ceph_entity_type_name(service_id) << " secret_id=" << secret_id << dendl;
return false;
}
}
std::string error;
decode_decrypt_enc_bl(cct, ticket_info, service_secret, ticket_blob.blob, error);
if (!error.empty()) {
ldout(cct, 0) << "ceph_decode_ticket could not decrypt ticket info. error:"
<< error << dendl;
return false;
}
return true;
}
/*
* SERVICE: verify authorizer and generate reply authorizer
*
* {timestamp + 1}^session_key
*/
bool cephx_verify_authorizer(CephContext *cct, KeyStore *keys,
bufferlist::const_iterator& indata,
size_t connection_secret_required_len,
CephXServiceTicketInfo& ticket_info,
std::unique_ptr<AuthAuthorizerChallenge> *challenge,
std::string *connection_secret,
bufferlist *reply_bl)
{
__u8 authorizer_v;
uint32_t service_id;
uint64_t global_id;
CryptoKey service_secret;
// ticket blob
CephXTicketBlob ticket;
try {
decode(authorizer_v, indata);
decode(global_id, indata);
decode(service_id, indata);
decode(ticket, indata);
} catch (buffer::end_of_buffer &e) {
// Unable to decode!
return false;
}
ldout(cct, 10) << "verify_authorizer decrypted service "
<< ceph_entity_type_name(service_id)
<< " secret_id=" << ticket.secret_id << dendl;
if (ticket.secret_id == (uint64_t)-1) {
EntityName name;
name.set_type(service_id);
if (!keys->get_secret(name, service_secret)) {
ldout(cct, 0) << "verify_authorizer could not get general service secret for service "
<< ceph_entity_type_name(service_id) << " secret_id=" << ticket.secret_id << dendl;
return false;
}
} else {
if (!keys->get_service_secret(service_id, ticket.secret_id, service_secret)) {
ldout(cct, 0) << "verify_authorizer could not get service secret for service "
<< ceph_entity_type_name(service_id) << " secret_id=" << ticket.secret_id << dendl;
if (cct->_conf->auth_debug && ticket.secret_id == 0)
ceph_abort_msg("got secret_id=0");
return false;
}
}
std::string error;
if (!service_secret.get_secret().length())
error = "invalid key"; // Bad key?
else
decode_decrypt_enc_bl(cct, ticket_info, service_secret, ticket.blob, error);
if (!error.empty()) {
ldout(cct, 0) << "verify_authorizer could not decrypt ticket info: error: "
<< error << dendl;
return false;
}
if (ticket_info.ticket.global_id != global_id) {
ldout(cct, 0) << "verify_authorizer global_id mismatch: declared id=" << global_id
<< " ticket_id=" << ticket_info.ticket.global_id << dendl;
return false;
}
ldout(cct, 10) << "verify_authorizer global_id=" << global_id << dendl;
// CephXAuthorize
CephXAuthorize auth_msg;
if (decode_decrypt(cct, auth_msg, ticket_info.session_key, indata, error)) {
ldout(cct, 0) << "verify_authorizercould not decrypt authorize request with error: "
<< error << dendl;
return false;
}
if (challenge) {
auto *c = static_cast<CephXAuthorizeChallenge*>(challenge->get());
if (!auth_msg.have_challenge || !c) {
c = new CephXAuthorizeChallenge;
challenge->reset(c);
cct->random()->get_bytes((char*)&c->server_challenge, sizeof(c->server_challenge));
ldout(cct,10) << __func__ << " adding server_challenge " << c->server_challenge
<< dendl;
encode_encrypt_enc_bl(cct, *c, ticket_info.session_key, *reply_bl, error);
if (!error.empty()) {
ldout(cct, 10) << "verify_authorizer: encode_encrypt error: " << error << dendl;
return false;
}
return false;
}
ldout(cct, 10) << __func__ << " got server_challenge+1 "
<< auth_msg.server_challenge_plus_one
<< " expecting " << c->server_challenge + 1 << dendl;
if (c->server_challenge + 1 != auth_msg.server_challenge_plus_one) {
return false;
}
}
/*
* Reply authorizer:
* {timestamp + 1}^session_key
*/
CephXAuthorizeReply reply;
// reply.trans_id = auth_msg.trans_id;
reply.nonce_plus_one = auth_msg.nonce + 1;
#ifndef WITH_SEASTAR
if (connection_secret) {
// generate a connection secret
connection_secret->resize(connection_secret_required_len);
if (connection_secret_required_len) {
cct->random()->get_bytes(connection_secret->data(),
connection_secret_required_len);
}
reply.connection_secret = *connection_secret;
}
#endif
if (encode_encrypt(cct, reply, ticket_info.session_key, *reply_bl, error)) {
ldout(cct, 10) << "verify_authorizer: encode_encrypt error: " << error << dendl;
return false;
}
ldout(cct, 10) << "verify_authorizer ok nonce " << hex << auth_msg.nonce << dec
<< " reply_bl.length()=" << reply_bl->length() << dendl;
return true;
}
bool CephXAuthorizer::verify_reply(bufferlist::const_iterator& indata,
std::string *connection_secret)
{
CephXAuthorizeReply reply;
std::string error;
if (decode_decrypt(cct, reply, session_key, indata, error)) {
ldout(cct, 0) << "verify_reply couldn't decrypt with error: " << error << dendl;
return false;
}
uint64_t expect = nonce + 1;
if (expect != reply.nonce_plus_one) {
ldout(cct, 0) << "verify_authorizer_reply bad nonce got " << reply.nonce_plus_one << " expected " << expect
<< " sent " << nonce << dendl;
return false;
}
if (connection_secret &&
reply.connection_secret.size()) {
*connection_secret = reply.connection_secret;
}
return true;
}
bool CephXAuthorizer::add_challenge(CephContext *cct,
const bufferlist& challenge)
{
bl = base_bl;
CephXAuthorize msg;
msg.nonce = nonce;
auto p = challenge.begin();
if (!p.end()) {
std::string error;
CephXAuthorizeChallenge ch;
decode_decrypt_enc_bl(cct, ch, session_key, challenge, error);
if (!error.empty()) {
ldout(cct, 0) << "failed to decrypt challenge (" << challenge.length() << " bytes): "
<< error << dendl;
return false;
}
msg.have_challenge = true;
msg.server_challenge_plus_one = ch.server_challenge + 1;
}
std::string error;
if (encode_encrypt(cct, msg, session_key, bl, error)) {
ldout(cct, 0) << __func__ << " failed to encrypt authorizer: " << error << dendl;
return false;
}
return true;
}
|