| 1 | |
| 2 |
|
| 3 | #include <linux/ceph/ceph_debug.h>
|
| 4 |
|
| 5 | #include <linux/err.h>
|
| 6 | #include <linux/module.h>
|
| 7 | #include <linux/random.h>
|
| 8 | #include <linux/slab.h>
|
| 9 |
|
| 10 | #include <linux/ceph/decode.h>
|
| 11 | #include <linux/ceph/auth.h>
|
| 12 | #include <linux/ceph/ceph_features.h>
|
| 13 | #include <linux/ceph/libceph.h>
|
| 14 | #include <linux/ceph/messenger.h>
|
| 15 |
|
| 16 | #include "crypto.h"
|
| 17 | #include "auth_x.h"
|
| 18 | #include "auth_x_protocol.h"
|
| 19 |
|
| 20 | static const u32 ticket_key_usages[] = {
|
| 21 | CEPHX_KEY_USAGE_TICKET_SESSION_KEY,
|
| 22 | CEPHX_KEY_USAGE_TICKET_BLOB,
|
| 23 | CEPHX_KEY_USAGE_AUTH_CONNECTION_SECRET
|
| 24 | };
|
| 25 |
|
| 26 | static const u32 authorizer_key_usages[] = {
|
| 27 | CEPHX_KEY_USAGE_AUTHORIZE,
|
| 28 | CEPHX_KEY_USAGE_AUTHORIZE_CHALLENGE,
|
| 29 | CEPHX_KEY_USAGE_AUTHORIZE_REPLY
|
| 30 | };
|
| 31 |
|
| 32 | static const u32 client_key_usages[] = {
|
| 33 | CEPHX_KEY_USAGE_TICKET_SESSION_KEY
|
| 34 | };
|
| 35 |
|
| 36 | static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed);
|
| 37 |
|
| 38 | static int ceph_x_is_authenticated(struct ceph_auth_client *ac)
|
| 39 | {
|
| 40 | struct ceph_x_info *xi = ac->private;
|
| 41 | int missing;
|
| 42 | int need;
|
| 43 |
|
| 44 | ceph_x_validate_tickets(ac, &need);
|
| 45 | missing = ac->want_keys & ~xi->have_keys;
|
| 46 | WARN_ON((need & missing) != missing);
|
| 47 | dout("%s want 0x%x have 0x%x missing 0x%x -> %d\n", __func__,
|
| 48 | ac->want_keys, xi->have_keys, missing, !missing);
|
| 49 | return !missing;
|
| 50 | }
|
| 51 |
|
| 52 | static int ceph_x_should_authenticate(struct ceph_auth_client *ac)
|
| 53 | {
|
| 54 | struct ceph_x_info *xi = ac->private;
|
| 55 | int need;
|
| 56 |
|
| 57 | ceph_x_validate_tickets(ac, &need);
|
| 58 | dout("%s want 0x%x have 0x%x need 0x%x -> %d\n", __func__,
|
| 59 | ac->want_keys, xi->have_keys, need, !!need);
|
| 60 | return !!need;
|
| 61 | }
|
| 62 |
|
| 63 | static int __ceph_x_encrypt_offset(const struct ceph_crypto_key *key)
|
| 64 | {
|
| 65 | return ceph_crypt_data_offset(key) +
|
| 66 | sizeof(struct ceph_x_encrypt_header);
|
| 67 | }
|
| 68 |
|
| 69 | static int ceph_x_encrypt_offset(const struct ceph_crypto_key *key)
|
| 70 | {
|
| 71 | return sizeof(u32) + __ceph_x_encrypt_offset(key);
|
| 72 | }
|
| 73 |
|
| 74 | |
| 75 | * AES: ciphertext_len | hdr | data... | padding
|
| 76 | * AES256KRB5: ciphertext_len | confounder | hdr | data... | hmac
|
| 77 |
|
| 78 | static int ceph_x_encrypt_buflen(const struct ceph_crypto_key *key,
|
| 79 | int data_len)
|
| 80 | {
|
| 81 | int encrypt_len = sizeof(struct ceph_x_encrypt_header) + data_len;
|
| 82 | return sizeof(u32) + ceph_crypt_buflen(key, encrypt_len);
|
| 83 | }
|
| 84 |
|
| 85 | static int ceph_x_encrypt(const struct ceph_crypto_key *key, int usage_slot,
|
| 86 | void *buf, int buf_len, int plaintext_len)
|
| 87 | {
|
| 88 | struct ceph_x_encrypt_header *hdr;
|
| 89 | int ciphertext_len;
|
| 90 | int ret;
|
| 91 |
|
| 92 | hdr = buf + sizeof(u32) + ceph_crypt_data_offset(key);
|
| 93 | hdr->struct_v = 1;
|
| 94 | hdr->magic = cpu_to_le64(CEPHX_ENC_MAGIC);
|
| 95 |
|
| 96 | ret = ceph_crypt(key, usage_slot, true, buf + sizeof(u32),
|
| 97 | buf_len - sizeof(u32), plaintext_len + sizeof(*hdr),
|
| 98 | &ciphertext_len);
|
| 99 | if (ret)
|
| 100 | return ret;
|
| 101 |
|
| 102 | ceph_encode_32(&buf, ciphertext_len);
|
| 103 | return sizeof(u32) + ciphertext_len;
|
| 104 | }
|
| 105 |
|
| 106 | static int __ceph_x_decrypt(const struct ceph_crypto_key *key, int usage_slot,
|
| 107 | void *p, int ciphertext_len)
|
| 108 | {
|
| 109 | struct ceph_x_encrypt_header *hdr;
|
| 110 | int plaintext_len;
|
| 111 | int ret;
|
| 112 |
|
| 113 | ret = ceph_crypt(key, usage_slot, false, p, ciphertext_len,
|
| 114 | ciphertext_len, &plaintext_len);
|
| 115 | if (ret)
|
| 116 | return ret;
|
| 117 |
|
| 118 | if (plaintext_len < sizeof(*hdr)) {
|
| 119 | pr_err("%s plaintext too small %d\n", __func__, plaintext_len);
|
| 120 | return -EINVAL;
|
| 121 | }
|
| 122 |
|
| 123 | hdr = p + ceph_crypt_data_offset(key);
|
| 124 | if (le64_to_cpu(hdr->magic) != CEPHX_ENC_MAGIC) {
|
| 125 | pr_err("%s bad magic\n", __func__);
|
| 126 | return -EINVAL;
|
| 127 | }
|
| 128 |
|
| 129 | return plaintext_len - sizeof(*hdr);
|
| 130 | }
|
| 131 |
|
| 132 | static int ceph_x_decrypt(const struct ceph_crypto_key *key, int usage_slot,
|
| 133 | void **p, void *end)
|
| 134 | {
|
| 135 | int ciphertext_len;
|
| 136 | int ret;
|
| 137 |
|
| 138 | ceph_decode_32_safe(p, end, ciphertext_len, e_inval);
|
| 139 | ceph_decode_need(p, end, ciphertext_len, e_inval);
|
| 140 |
|
| 141 | ret = __ceph_x_decrypt(key, usage_slot, *p, ciphertext_len);
|
| 142 | if (ret < 0)
|
| 143 | return ret;
|
| 144 |
|
| 145 | *p += ciphertext_len;
|
| 146 | return ret;
|
| 147 |
|
| 148 | e_inval:
|
| 149 | return -EINVAL;
|
| 150 | }
|
| 151 |
|
| 152 | |
| 153 | * get existing (or insert new) ticket handler
|
| 154 |
|
| 155 | static struct ceph_x_ticket_handler *
|
| 156 | get_ticket_handler(struct ceph_auth_client *ac, int service)
|
| 157 | {
|
| 158 | struct ceph_x_ticket_handler *th;
|
| 159 | struct ceph_x_info *xi = ac->private;
|
| 160 | struct rb_node *parent = NULL, **p = &xi->ticket_handlers.rb_node;
|
| 161 |
|
| 162 | while (*p) {
|
| 163 | parent = *p;
|
| 164 | th = rb_entry(parent, struct ceph_x_ticket_handler, node);
|
| 165 | if (service < th->service)
|
| 166 | p = &(*p)->rb_left;
|
| 167 | else if (service > th->service)
|
| 168 | p = &(*p)->rb_right;
|
| 169 | else
|
| 170 | return th;
|
| 171 | }
|
| 172 |
|
| 173 |
|
| 174 | th = kzalloc_obj(*th, GFP_NOFS);
|
| 175 | if (!th)
|
| 176 | return ERR_PTR(-ENOMEM);
|
| 177 | th->service = service;
|
| 178 | rb_link_node(&th->node, parent, p);
|
| 179 | rb_insert_color(&th->node, &xi->ticket_handlers);
|
| 180 | return th;
|
| 181 | }
|
| 182 |
|
| 183 | static void remove_ticket_handler(struct ceph_auth_client *ac,
|
| 184 | struct ceph_x_ticket_handler *th)
|
| 185 | {
|
| 186 | struct ceph_x_info *xi = ac->private;
|
| 187 |
|
| 188 | dout("remove_ticket_handler %p %d\n", th, th->service);
|
| 189 | rb_erase(&th->node, &xi->ticket_handlers);
|
| 190 | ceph_crypto_key_destroy(&th->session_key);
|
| 191 | if (th->ticket_blob)
|
| 192 | ceph_buffer_put(th->ticket_blob);
|
| 193 | kfree(th);
|
| 194 | }
|
| 195 |
|
| 196 | static int process_one_ticket(struct ceph_auth_client *ac,
|
| 197 | struct ceph_crypto_key *secret,
|
| 198 | void **p, void *end)
|
| 199 | {
|
| 200 | struct ceph_x_info *xi = ac->private;
|
| 201 | int type;
|
| 202 | u8 tkt_struct_v, blob_struct_v;
|
| 203 | struct ceph_x_ticket_handler *th;
|
| 204 | void *dp, *dend;
|
| 205 | int dlen;
|
| 206 | char is_enc;
|
| 207 | struct timespec64 validity;
|
| 208 | void *tp, *tpend;
|
| 209 | void **ptp;
|
| 210 | struct ceph_crypto_key new_session_key = { 0 };
|
| 211 | struct ceph_buffer *new_ticket_blob;
|
| 212 | time64_t new_expires, new_renew_after;
|
| 213 | u64 new_secret_id;
|
| 214 | int ret;
|
| 215 |
|
| 216 | ceph_decode_need(p, end, sizeof(u32) + 1, bad);
|
| 217 |
|
| 218 | type = ceph_decode_32(p);
|
| 219 | dout(" ticket type %d %s\n", type, ceph_entity_type_name(type));
|
| 220 |
|
| 221 | tkt_struct_v = ceph_decode_8(p);
|
| 222 | if (tkt_struct_v != 1)
|
| 223 | goto bad;
|
| 224 |
|
| 225 | th = get_ticket_handler(ac, type);
|
| 226 | if (IS_ERR(th)) {
|
| 227 | ret = PTR_ERR(th);
|
| 228 | goto out;
|
| 229 | }
|
| 230 |
|
| 231 |
|
| 232 | dp = *p + ceph_x_encrypt_offset(secret);
|
| 233 | ret = ceph_x_decrypt(secret,
|
| 234 | 0 ,
|
| 235 | p, end);
|
| 236 | if (ret < 0)
|
| 237 | goto out;
|
| 238 | dout(" decrypted %d bytes\n", ret);
|
| 239 | dend = dp + ret;
|
| 240 |
|
| 241 | ceph_decode_8_safe(&dp, dend, tkt_struct_v, bad);
|
| 242 | if (tkt_struct_v != 1)
|
| 243 | goto bad;
|
| 244 |
|
| 245 | ret = ceph_crypto_key_decode(&new_session_key, &dp, dend);
|
| 246 | if (ret)
|
| 247 | goto out;
|
| 248 |
|
| 249 | ret = ceph_crypto_key_prepare(&new_session_key, ticket_key_usages,
|
| 250 | ARRAY_SIZE(ticket_key_usages));
|
| 251 | if (ret)
|
| 252 | goto out;
|
| 253 |
|
| 254 | ceph_decode_need(&dp, dend, sizeof(struct ceph_timespec), bad);
|
| 255 | ceph_decode_timespec64(&validity, dp);
|
| 256 | dp += sizeof(struct ceph_timespec);
|
| 257 | new_expires = ktime_get_real_seconds() + validity.tv_sec;
|
| 258 | new_renew_after = new_expires - (validity.tv_sec / 4);
|
| 259 | dout(" expires=%llu renew_after=%llu\n", new_expires,
|
| 260 | new_renew_after);
|
| 261 |
|
| 262 |
|
| 263 | ceph_decode_8_safe(p, end, is_enc, bad);
|
| 264 | if (is_enc) {
|
| 265 |
|
| 266 | tp = *p + ceph_x_encrypt_offset(&th->session_key);
|
| 267 | ret = ceph_x_decrypt(&th->session_key,
|
| 268 | 1 ,
|
| 269 | p, end);
|
| 270 | if (ret < 0)
|
| 271 | goto out;
|
| 272 | dout(" encrypted ticket, decrypted %d bytes\n", ret);
|
| 273 | ptp = &tp;
|
| 274 | tpend = tp + ret;
|
| 275 | } else {
|
| 276 |
|
| 277 | ptp = p;
|
| 278 | tpend = end;
|
| 279 | }
|
| 280 | ceph_decode_32_safe(ptp, tpend, dlen, bad);
|
| 281 | dout(" ticket blob is %d bytes\n", dlen);
|
| 282 | ceph_decode_need(ptp, tpend, 1 + sizeof(u64), bad);
|
| 283 | blob_struct_v = ceph_decode_8(ptp);
|
| 284 | if (blob_struct_v != 1)
|
| 285 | goto bad;
|
| 286 |
|
| 287 | new_secret_id = ceph_decode_64(ptp);
|
| 288 | ret = ceph_decode_buffer(&new_ticket_blob, ptp, tpend);
|
| 289 | if (ret)
|
| 290 | goto out;
|
| 291 |
|
| 292 |
|
| 293 | ceph_crypto_key_destroy(&th->session_key);
|
| 294 | if (th->ticket_blob)
|
| 295 | ceph_buffer_put(th->ticket_blob);
|
| 296 | th->session_key = new_session_key;
|
| 297 | th->ticket_blob = new_ticket_blob;
|
| 298 | th->secret_id = new_secret_id;
|
| 299 | th->expires = new_expires;
|
| 300 | th->renew_after = new_renew_after;
|
| 301 | th->have_key = true;
|
| 302 | dout(" got ticket service %d (%s) secret_id %lld len %d\n",
|
| 303 | type, ceph_entity_type_name(type), th->secret_id,
|
| 304 | (int)th->ticket_blob->vec.iov_len);
|
| 305 | xi->have_keys |= th->service;
|
| 306 | return 0;
|
| 307 |
|
| 308 | bad:
|
| 309 | ret = -EINVAL;
|
| 310 | out:
|
| 311 | ceph_crypto_key_destroy(&new_session_key);
|
| 312 | return ret;
|
| 313 | }
|
| 314 |
|
| 315 | static int ceph_x_proc_ticket_reply(struct ceph_auth_client *ac,
|
| 316 | struct ceph_crypto_key *secret,
|
| 317 | void **p, void *end)
|
| 318 | {
|
| 319 | u8 reply_struct_v;
|
| 320 | u32 num;
|
| 321 | int ret;
|
| 322 |
|
| 323 | ceph_decode_8_safe(p, end, reply_struct_v, bad);
|
| 324 | if (reply_struct_v != 1)
|
| 325 | return -EINVAL;
|
| 326 |
|
| 327 | ceph_decode_32_safe(p, end, num, bad);
|
| 328 | dout("%d tickets\n", num);
|
| 329 |
|
| 330 | while (num--) {
|
| 331 | ret = process_one_ticket(ac, secret, p, end);
|
| 332 | if (ret)
|
| 333 | return ret;
|
| 334 | }
|
| 335 |
|
| 336 | return 0;
|
| 337 |
|
| 338 | bad:
|
| 339 | return -EINVAL;
|
| 340 | }
|
| 341 |
|
| 342 | |
| 343 | * Encode and encrypt the second part (ceph_x_authorize_b) of the
|
| 344 | * authorizer. The first part (ceph_x_authorize_a) should already be
|
| 345 | * encoded.
|
| 346 |
|
| 347 | static int encrypt_authorizer(struct ceph_x_authorizer *au,
|
| 348 | u64 *server_challenge)
|
| 349 | {
|
| 350 | struct ceph_x_authorize_a *msg_a;
|
| 351 | struct ceph_x_authorize_b *msg_b;
|
| 352 | void *p, *end;
|
| 353 | int ret;
|
| 354 |
|
| 355 | msg_a = au->buf->vec.iov_base;
|
| 356 | WARN_ON(msg_a->ticket_blob.secret_id != cpu_to_le64(au->secret_id));
|
| 357 | p = (void *)(msg_a + 1) + le32_to_cpu(msg_a->ticket_blob.blob_len);
|
| 358 | end = au->buf->vec.iov_base + au->buf->vec.iov_len;
|
| 359 |
|
| 360 | msg_b = p + ceph_x_encrypt_offset(&au->session_key);
|
| 361 | msg_b->struct_v = 2;
|
| 362 | msg_b->nonce = cpu_to_le64(au->nonce);
|
| 363 | if (server_challenge) {
|
| 364 | msg_b->have_challenge = 1;
|
| 365 | msg_b->server_challenge_plus_one =
|
| 366 | cpu_to_le64(*server_challenge + 1);
|
| 367 | } else {
|
| 368 | msg_b->have_challenge = 0;
|
| 369 | msg_b->server_challenge_plus_one = 0;
|
| 370 | }
|
| 371 |
|
| 372 | ret = ceph_x_encrypt(&au->session_key,
|
| 373 | 0 ,
|
| 374 | p, end - p, sizeof(*msg_b));
|
| 375 | if (ret < 0)
|
| 376 | return ret;
|
| 377 |
|
| 378 | p += ret;
|
| 379 | if (server_challenge) {
|
| 380 | WARN_ON(p != end);
|
| 381 | } else {
|
| 382 | WARN_ON(p > end);
|
| 383 | au->buf->vec.iov_len = p - au->buf->vec.iov_base;
|
| 384 | }
|
| 385 |
|
| 386 | return 0;
|
| 387 | }
|
| 388 |
|
| 389 | static void ceph_x_authorizer_cleanup(struct ceph_x_authorizer *au)
|
| 390 | {
|
| 391 | ceph_crypto_key_destroy(&au->session_key);
|
| 392 | if (au->buf) {
|
| 393 | ceph_buffer_put(au->buf);
|
| 394 | au->buf = NULL;
|
| 395 | }
|
| 396 | }
|
| 397 |
|
| 398 | static int ceph_x_build_authorizer(struct ceph_auth_client *ac,
|
| 399 | struct ceph_x_ticket_handler *th,
|
| 400 | struct ceph_x_authorizer *au)
|
| 401 | {
|
| 402 | int maxlen;
|
| 403 | struct ceph_x_authorize_a *msg_a;
|
| 404 | struct ceph_x_authorize_b *msg_b;
|
| 405 | int ret;
|
| 406 | int ticket_blob_len =
|
| 407 | (th->ticket_blob ? th->ticket_blob->vec.iov_len : 0);
|
| 408 |
|
| 409 | dout("build_authorizer for %s %p\n",
|
| 410 | ceph_entity_type_name(th->service), au);
|
| 411 |
|
| 412 | ceph_crypto_key_destroy(&au->session_key);
|
| 413 | ret = ceph_crypto_key_clone(&au->session_key, &th->session_key);
|
| 414 | if (ret)
|
| 415 | goto out_au;
|
| 416 |
|
| 417 | ret = ceph_crypto_key_prepare(&au->session_key, authorizer_key_usages,
|
| 418 | ARRAY_SIZE(authorizer_key_usages));
|
| 419 | if (ret)
|
| 420 | goto out_au;
|
| 421 |
|
| 422 | maxlen = sizeof(*msg_a) + ticket_blob_len +
|
| 423 | ceph_x_encrypt_buflen(&au->session_key, sizeof(*msg_b));
|
| 424 | dout(" need len %d\n", maxlen);
|
| 425 | if (au->buf && au->buf->alloc_len < maxlen) {
|
| 426 | ceph_buffer_put(au->buf);
|
| 427 | au->buf = NULL;
|
| 428 | }
|
| 429 | if (!au->buf) {
|
| 430 | au->buf = ceph_buffer_new(maxlen, GFP_NOFS);
|
| 431 | if (!au->buf) {
|
| 432 | ret = -ENOMEM;
|
| 433 | goto out_au;
|
| 434 | }
|
| 435 | }
|
| 436 | au->service = th->service;
|
| 437 | WARN_ON(!th->secret_id);
|
| 438 | au->secret_id = th->secret_id;
|
| 439 |
|
| 440 | msg_a = au->buf->vec.iov_base;
|
| 441 | msg_a->struct_v = 1;
|
| 442 | msg_a->global_id = cpu_to_le64(ac->global_id);
|
| 443 | msg_a->service_id = cpu_to_le32(th->service);
|
| 444 | msg_a->ticket_blob.struct_v = 1;
|
| 445 | msg_a->ticket_blob.secret_id = cpu_to_le64(th->secret_id);
|
| 446 | msg_a->ticket_blob.blob_len = cpu_to_le32(ticket_blob_len);
|
| 447 | if (ticket_blob_len) {
|
| 448 | memcpy(msg_a->ticket_blob.blob, th->ticket_blob->vec.iov_base,
|
| 449 | th->ticket_blob->vec.iov_len);
|
| 450 | }
|
| 451 | dout(" th %p secret_id %lld %lld\n", th, th->secret_id,
|
| 452 | le64_to_cpu(msg_a->ticket_blob.secret_id));
|
| 453 |
|
| 454 | get_random_bytes(&au->nonce, sizeof(au->nonce));
|
| 455 | ret = encrypt_authorizer(au, NULL);
|
| 456 | if (ret) {
|
| 457 | pr_err("failed to encrypt authorizer: %d", ret);
|
| 458 | goto out_au;
|
| 459 | }
|
| 460 |
|
| 461 | dout(" built authorizer nonce %llx len %d\n", au->nonce,
|
| 462 | (int)au->buf->vec.iov_len);
|
| 463 | return 0;
|
| 464 |
|
| 465 | out_au:
|
| 466 | ceph_x_authorizer_cleanup(au);
|
| 467 | return ret;
|
| 468 | }
|
| 469 |
|
| 470 | static int ceph_x_encode_ticket(struct ceph_x_ticket_handler *th,
|
| 471 | void **p, void *end)
|
| 472 | {
|
| 473 | ceph_decode_need(p, end, 1 + sizeof(u64), bad);
|
| 474 | ceph_encode_8(p, 1);
|
| 475 | ceph_encode_64(p, th->secret_id);
|
| 476 | if (th->ticket_blob) {
|
| 477 | const char *buf = th->ticket_blob->vec.iov_base;
|
| 478 | u32 len = th->ticket_blob->vec.iov_len;
|
| 479 |
|
| 480 | ceph_encode_32_safe(p, end, len, bad);
|
| 481 | ceph_encode_copy_safe(p, end, buf, len, bad);
|
| 482 | } else {
|
| 483 | ceph_encode_32_safe(p, end, 0, bad);
|
| 484 | }
|
| 485 |
|
| 486 | return 0;
|
| 487 | bad:
|
| 488 | return -ERANGE;
|
| 489 | }
|
| 490 |
|
| 491 | static bool need_key(struct ceph_x_ticket_handler *th)
|
| 492 | {
|
| 493 | if (!th->have_key)
|
| 494 | return true;
|
| 495 |
|
| 496 | return ktime_get_real_seconds() >= th->renew_after;
|
| 497 | }
|
| 498 |
|
| 499 | static bool have_key(struct ceph_x_ticket_handler *th)
|
| 500 | {
|
| 501 | if (th->have_key && ktime_get_real_seconds() >= th->expires) {
|
| 502 | dout("ticket %d (%s) secret_id %llu expired\n", th->service,
|
| 503 | ceph_entity_type_name(th->service), th->secret_id);
|
| 504 | th->have_key = false;
|
| 505 | }
|
| 506 |
|
| 507 | return th->have_key;
|
| 508 | }
|
| 509 |
|
| 510 | static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed)
|
| 511 | {
|
| 512 | int want = ac->want_keys;
|
| 513 | struct ceph_x_info *xi = ac->private;
|
| 514 | int service;
|
| 515 |
|
| 516 | *pneed = ac->want_keys & ~(xi->have_keys);
|
| 517 |
|
| 518 | for (service = 1; service <= want; service <<= 1) {
|
| 519 | struct ceph_x_ticket_handler *th;
|
| 520 |
|
| 521 | if (!(ac->want_keys & service))
|
| 522 | continue;
|
| 523 |
|
| 524 | if (*pneed & service)
|
| 525 | continue;
|
| 526 |
|
| 527 | th = get_ticket_handler(ac, service);
|
| 528 | if (IS_ERR(th)) {
|
| 529 | *pneed |= service;
|
| 530 | continue;
|
| 531 | }
|
| 532 |
|
| 533 | if (need_key(th))
|
| 534 | *pneed |= service;
|
| 535 | if (!have_key(th))
|
| 536 | xi->have_keys &= ~service;
|
| 537 | }
|
| 538 | }
|
| 539 |
|
| 540 | static int ceph_x_build_request(struct ceph_auth_client *ac,
|
| 541 | void *buf, void *end)
|
| 542 | {
|
| 543 | struct ceph_x_info *xi = ac->private;
|
| 544 | int need;
|
| 545 | struct ceph_x_request_header *head = buf;
|
| 546 | void *p;
|
| 547 | int ret;
|
| 548 | struct ceph_x_ticket_handler *th =
|
| 549 | get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
|
| 550 |
|
| 551 | if (IS_ERR(th))
|
| 552 | return PTR_ERR(th);
|
| 553 |
|
| 554 | ceph_x_validate_tickets(ac, &need);
|
| 555 | dout("%s want 0x%x have 0x%x need 0x%x\n", __func__, ac->want_keys,
|
| 556 | xi->have_keys, need);
|
| 557 |
|
| 558 | if (need & CEPH_ENTITY_TYPE_AUTH) {
|
| 559 | struct ceph_x_authenticate *auth = (void *)(head + 1);
|
| 560 | void *enc_buf = xi->auth_authorizer.enc_buf;
|
| 561 | struct ceph_x_challenge_blob *blob;
|
| 562 | u64 *u;
|
| 563 |
|
| 564 | p = auth + 1;
|
| 565 | if (p > end)
|
| 566 | return -ERANGE;
|
| 567 |
|
| 568 | dout(" get_auth_session_key\n");
|
| 569 | head->op = cpu_to_le16(CEPHX_GET_AUTH_SESSION_KEY);
|
| 570 |
|
| 571 | if (xi->secret.type == CEPH_CRYPTO_AES) {
|
| 572 | blob = enc_buf + ceph_x_encrypt_offset(&xi->secret);
|
| 573 | } else {
|
| 574 | BUILD_BUG_ON(SHA256_DIGEST_SIZE + sizeof(*blob) >
|
| 575 | CEPHX_AU_ENC_BUF_LEN);
|
| 576 | blob = enc_buf + SHA256_DIGEST_SIZE;
|
| 577 | }
|
| 578 |
|
| 579 | get_random_bytes(&auth->client_challenge, sizeof(u64));
|
| 580 | blob->client_challenge = auth->client_challenge;
|
| 581 | blob->server_challenge = cpu_to_le64(xi->server_challenge);
|
| 582 |
|
| 583 | if (xi->secret.type == CEPH_CRYPTO_AES) {
|
| 584 | ret = ceph_x_encrypt(&xi->secret, 0 ,
|
| 585 | enc_buf, CEPHX_AU_ENC_BUF_LEN,
|
| 586 | sizeof(*blob));
|
| 587 | if (ret < 0)
|
| 588 | return ret;
|
| 589 | } else {
|
| 590 | ceph_hmac_sha256(&xi->secret, blob, sizeof(*blob),
|
| 591 | enc_buf);
|
| 592 | ret = SHA256_DIGEST_SIZE;
|
| 593 | }
|
| 594 |
|
| 595 | auth->struct_v = 3;
|
| 596 | auth->key = 0;
|
| 597 | for (u = (u64 *)enc_buf; u + 1 <= (u64 *)(enc_buf + ret); u++)
|
| 598 | auth->key ^= *(__le64 *)u;
|
| 599 | dout(" server_challenge %llx client_challenge %llx key %llx\n",
|
| 600 | xi->server_challenge, le64_to_cpu(auth->client_challenge),
|
| 601 | le64_to_cpu(auth->key));
|
| 602 |
|
| 603 |
|
| 604 | ret = ceph_x_encode_ticket(th, &p, end);
|
| 605 | if (ret < 0)
|
| 606 | return ret;
|
| 607 |
|
| 608 |
|
| 609 | need = ac->want_keys & ~CEPH_ENTITY_TYPE_AUTH;
|
| 610 | WARN_ON(!need);
|
| 611 | ceph_encode_32_safe(&p, end, need, e_range);
|
| 612 | return p - buf;
|
| 613 | }
|
| 614 |
|
| 615 | if (need) {
|
| 616 | dout(" get_principal_session_key\n");
|
| 617 | ret = ceph_x_build_authorizer(ac, th, &xi->auth_authorizer);
|
| 618 | if (ret)
|
| 619 | return ret;
|
| 620 |
|
| 621 | p = buf;
|
| 622 | ceph_encode_16_safe(&p, end, CEPHX_GET_PRINCIPAL_SESSION_KEY,
|
| 623 | e_range);
|
| 624 | ceph_encode_copy_safe(&p, end,
|
| 625 | xi->auth_authorizer.buf->vec.iov_base,
|
| 626 | xi->auth_authorizer.buf->vec.iov_len, e_range);
|
| 627 | ceph_encode_8_safe(&p, end, 1, e_range);
|
| 628 | ceph_encode_32_safe(&p, end, need, e_range);
|
| 629 | return p - buf;
|
| 630 | }
|
| 631 |
|
| 632 | return 0;
|
| 633 |
|
| 634 | e_range:
|
| 635 | return -ERANGE;
|
| 636 | }
|
| 637 |
|
| 638 | static int decode_con_secret(void **p, void *end, u8 *con_secret,
|
| 639 | int *con_secret_len)
|
| 640 | {
|
| 641 | int len;
|
| 642 |
|
| 643 | ceph_decode_32_safe(p, end, len, bad);
|
| 644 | ceph_decode_need(p, end, len, bad);
|
| 645 |
|
| 646 | dout("%s len %d\n", __func__, len);
|
| 647 | if (con_secret) {
|
| 648 | if (len > CEPH_MAX_CON_SECRET_LEN) {
|
| 649 | pr_err("connection secret too big %d\n", len);
|
| 650 | goto bad_memzero;
|
| 651 | }
|
| 652 | memcpy(con_secret, *p, len);
|
| 653 | *con_secret_len = len;
|
| 654 | }
|
| 655 | memzero_explicit(*p, len);
|
| 656 | *p += len;
|
| 657 | return 0;
|
| 658 |
|
| 659 | bad_memzero:
|
| 660 | memzero_explicit(*p, len);
|
| 661 | bad:
|
| 662 | pr_err("failed to decode connection secret\n");
|
| 663 | return -EINVAL;
|
| 664 | }
|
| 665 |
|
| 666 | static int handle_auth_session_key(struct ceph_auth_client *ac, u64 global_id,
|
| 667 | void **p, void *end,
|
| 668 | u8 *session_key, int *session_key_len,
|
| 669 | u8 *con_secret, int *con_secret_len)
|
| 670 | {
|
| 671 | struct ceph_x_info *xi = ac->private;
|
| 672 | struct ceph_x_ticket_handler *th;
|
| 673 | void *dp, *dend;
|
| 674 | int len;
|
| 675 | int ret;
|
| 676 |
|
| 677 |
|
| 678 | ret = ceph_x_proc_ticket_reply(ac, &xi->secret, p, end);
|
| 679 | if (ret)
|
| 680 | return ret;
|
| 681 |
|
| 682 | ceph_auth_set_global_id(ac, global_id);
|
| 683 | if (*p == end) {
|
| 684 |
|
| 685 | WARN_ON(session_key || con_secret);
|
| 686 | return 0;
|
| 687 | }
|
| 688 |
|
| 689 | th = get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
|
| 690 | if (IS_ERR(th))
|
| 691 | return PTR_ERR(th);
|
| 692 |
|
| 693 | if (session_key) {
|
| 694 | memcpy(session_key, th->session_key.key, th->session_key.len);
|
| 695 | *session_key_len = th->session_key.len;
|
| 696 | }
|
| 697 |
|
| 698 |
|
| 699 | ceph_decode_32_safe(p, end, len, e_inval);
|
| 700 | ceph_decode_need(p, end, len, e_inval);
|
| 701 | dout("%s connection secret blob len %d\n", __func__, len);
|
| 702 | if (len > 0) {
|
| 703 | dp = *p + ceph_x_encrypt_offset(&th->session_key);
|
| 704 | ret = ceph_x_decrypt(&th->session_key,
|
| 705 | 2 ,
|
| 706 | p, *p + len);
|
| 707 | if (ret < 0)
|
| 708 | return ret;
|
| 709 |
|
| 710 | dout("%s decrypted %d bytes\n", __func__, ret);
|
| 711 | dend = dp + ret;
|
| 712 |
|
| 713 | ret = decode_con_secret(&dp, dend, con_secret, con_secret_len);
|
| 714 | if (ret)
|
| 715 | return ret;
|
| 716 | }
|
| 717 |
|
| 718 |
|
| 719 | ceph_decode_32_safe(p, end, len, e_inval);
|
| 720 | ceph_decode_need(p, end, len, e_inval);
|
| 721 | dout("%s service tickets blob len %d\n", __func__, len);
|
| 722 | if (len > 0) {
|
| 723 | ret = ceph_x_proc_ticket_reply(ac, &th->session_key,
|
| 724 | p, *p + len);
|
| 725 | if (ret)
|
| 726 | return ret;
|
| 727 | }
|
| 728 |
|
| 729 | return 0;
|
| 730 |
|
| 731 | e_inval:
|
| 732 | return -EINVAL;
|
| 733 | }
|
| 734 |
|
| 735 | static int ceph_x_handle_reply(struct ceph_auth_client *ac, u64 global_id,
|
| 736 | void *buf, void *end,
|
| 737 | u8 *session_key, int *session_key_len,
|
| 738 | u8 *con_secret, int *con_secret_len)
|
| 739 | {
|
| 740 | struct ceph_x_info *xi = ac->private;
|
| 741 | struct ceph_x_ticket_handler *th;
|
| 742 | int len = end - buf;
|
| 743 | int result;
|
| 744 | void *p;
|
| 745 | int op;
|
| 746 | int ret;
|
| 747 |
|
| 748 | if (xi->starting) {
|
| 749 |
|
| 750 | struct ceph_x_server_challenge *sc = buf;
|
| 751 |
|
| 752 | if (len != sizeof(*sc))
|
| 753 | return -EINVAL;
|
| 754 | xi->server_challenge = le64_to_cpu(sc->server_challenge);
|
| 755 | dout("handle_reply got server challenge %llx\n",
|
| 756 | xi->server_challenge);
|
| 757 | xi->starting = false;
|
| 758 | xi->have_keys &= ~CEPH_ENTITY_TYPE_AUTH;
|
| 759 | return -EAGAIN;
|
| 760 | }
|
| 761 |
|
| 762 | p = buf;
|
| 763 | ceph_decode_16_safe(&p, end, op, e_inval);
|
| 764 | ceph_decode_32_safe(&p, end, result, e_inval);
|
| 765 | dout("handle_reply op %d result %d\n", op, result);
|
| 766 | switch (op) {
|
| 767 | case CEPHX_GET_AUTH_SESSION_KEY:
|
| 768 |
|
| 769 | ret = handle_auth_session_key(ac, global_id, &p, end,
|
| 770 | session_key, session_key_len,
|
| 771 | con_secret, con_secret_len);
|
| 772 | break;
|
| 773 |
|
| 774 | case CEPHX_GET_PRINCIPAL_SESSION_KEY:
|
| 775 | th = get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
|
| 776 | if (IS_ERR(th))
|
| 777 | return PTR_ERR(th);
|
| 778 |
|
| 779 |
|
| 780 | ret = ceph_x_proc_ticket_reply(ac, &th->session_key, &p, end);
|
| 781 | break;
|
| 782 |
|
| 783 | default:
|
| 784 | return -EINVAL;
|
| 785 | }
|
| 786 | if (ret)
|
| 787 | return ret;
|
| 788 | if (ac->want_keys == xi->have_keys)
|
| 789 | return 0;
|
| 790 | return -EAGAIN;
|
| 791 |
|
| 792 | e_inval:
|
| 793 | return -EINVAL;
|
| 794 | }
|
| 795 |
|
| 796 | static void ceph_x_destroy_authorizer(struct ceph_authorizer *a)
|
| 797 | {
|
| 798 | struct ceph_x_authorizer *au = (void *)a;
|
| 799 |
|
| 800 | ceph_x_authorizer_cleanup(au);
|
| 801 | kfree(au);
|
| 802 | }
|
| 803 |
|
| 804 | static int ceph_x_create_authorizer(
|
| 805 | struct ceph_auth_client *ac, int peer_type,
|
| 806 | struct ceph_auth_handshake *auth)
|
| 807 | {
|
| 808 | struct ceph_x_authorizer *au;
|
| 809 | struct ceph_x_ticket_handler *th;
|
| 810 | int ret;
|
| 811 |
|
| 812 | th = get_ticket_handler(ac, peer_type);
|
| 813 | if (IS_ERR(th))
|
| 814 | return PTR_ERR(th);
|
| 815 |
|
| 816 | au = kzalloc_obj(*au, GFP_NOFS);
|
| 817 | if (!au)
|
| 818 | return -ENOMEM;
|
| 819 |
|
| 820 | au->base.destroy = ceph_x_destroy_authorizer;
|
| 821 |
|
| 822 | ret = ceph_x_build_authorizer(ac, th, au);
|
| 823 | if (ret) {
|
| 824 | kfree(au);
|
| 825 | return ret;
|
| 826 | }
|
| 827 |
|
| 828 | auth->authorizer = (struct ceph_authorizer *) au;
|
| 829 | auth->authorizer_buf = au->buf->vec.iov_base;
|
| 830 | auth->authorizer_buf_len = au->buf->vec.iov_len;
|
| 831 | auth->authorizer_reply_buf = au->enc_buf;
|
| 832 | auth->authorizer_reply_buf_len = CEPHX_AU_ENC_BUF_LEN;
|
| 833 | auth->sign_message = ac->ops->sign_message;
|
| 834 | auth->check_message_signature = ac->ops->check_message_signature;
|
| 835 |
|
| 836 | return 0;
|
| 837 | }
|
| 838 |
|
| 839 | static int ceph_x_update_authorizer(
|
| 840 | struct ceph_auth_client *ac, int peer_type,
|
| 841 | struct ceph_auth_handshake *auth)
|
| 842 | {
|
| 843 | struct ceph_x_authorizer *au;
|
| 844 | struct ceph_x_ticket_handler *th;
|
| 845 |
|
| 846 | th = get_ticket_handler(ac, peer_type);
|
| 847 | if (IS_ERR(th))
|
| 848 | return PTR_ERR(th);
|
| 849 |
|
| 850 | au = (struct ceph_x_authorizer *)auth->authorizer;
|
| 851 | if (au->secret_id < th->secret_id) {
|
| 852 | int ret;
|
| 853 |
|
| 854 | dout("ceph_x_update_authorizer service %u secret %llu < %llu\n",
|
| 855 | au->service, au->secret_id, th->secret_id);
|
| 856 | ret = ceph_x_build_authorizer(ac, th, au);
|
| 857 | if (ret)
|
| 858 | return ret;
|
| 859 |
|
| 860 | auth->authorizer_buf = au->buf->vec.iov_base;
|
| 861 | auth->authorizer_buf_len = au->buf->vec.iov_len;
|
| 862 | }
|
| 863 | return 0;
|
| 864 | }
|
| 865 |
|
| 866 | |
| 867 | * CephXAuthorizeChallenge
|
| 868 |
|
| 869 | static int decrypt_authorizer_challenge(struct ceph_crypto_key *secret,
|
| 870 | void *challenge, int challenge_len,
|
| 871 | u64 *server_challenge)
|
| 872 | {
|
| 873 | void *dp, *dend;
|
| 874 | int ret;
|
| 875 |
|
| 876 |
|
| 877 | ret = __ceph_x_decrypt(secret,
|
| 878 | 1 ,
|
| 879 | challenge, challenge_len);
|
| 880 | if (ret < 0)
|
| 881 | return ret;
|
| 882 |
|
| 883 | dout("%s decrypted %d bytes\n", __func__, ret);
|
| 884 | dp = challenge + __ceph_x_encrypt_offset(secret);
|
| 885 | dend = dp + ret;
|
| 886 |
|
| 887 | ceph_decode_skip_8(&dp, dend, e_inval);
|
| 888 | ceph_decode_64_safe(&dp, dend, *server_challenge, e_inval);
|
| 889 | dout("%s server_challenge %llu\n", __func__, *server_challenge);
|
| 890 | return 0;
|
| 891 |
|
| 892 | e_inval:
|
| 893 | return -EINVAL;
|
| 894 | }
|
| 895 |
|
| 896 | static int ceph_x_add_authorizer_challenge(struct ceph_auth_client *ac,
|
| 897 | struct ceph_authorizer *a,
|
| 898 | void *challenge, int challenge_len)
|
| 899 | {
|
| 900 | struct ceph_x_authorizer *au = (void *)a;
|
| 901 | u64 server_challenge;
|
| 902 | int ret;
|
| 903 |
|
| 904 | ret = decrypt_authorizer_challenge(&au->session_key, challenge,
|
| 905 | challenge_len, &server_challenge);
|
| 906 | if (ret) {
|
| 907 | pr_err("failed to decrypt authorize challenge: %d", ret);
|
| 908 | return ret;
|
| 909 | }
|
| 910 |
|
| 911 | ret = encrypt_authorizer(au, &server_challenge);
|
| 912 | if (ret) {
|
| 913 | pr_err("failed to encrypt authorizer w/ challenge: %d", ret);
|
| 914 | return ret;
|
| 915 | }
|
| 916 |
|
| 917 | return 0;
|
| 918 | }
|
| 919 |
|
| 920 | |
| 921 | * CephXAuthorizeReply
|
| 922 |
|
| 923 | static int decrypt_authorizer_reply(struct ceph_crypto_key *secret,
|
| 924 | void **p, void *end, u64 *nonce_plus_one,
|
| 925 | u8 *con_secret, int *con_secret_len)
|
| 926 | {
|
| 927 | void *dp, *dend;
|
| 928 | u8 struct_v;
|
| 929 | int ret;
|
| 930 |
|
| 931 | dp = *p + ceph_x_encrypt_offset(secret);
|
| 932 | ret = ceph_x_decrypt(secret, 2 ,
|
| 933 | p, end);
|
| 934 | if (ret < 0)
|
| 935 | return ret;
|
| 936 |
|
| 937 | dout("%s decrypted %d bytes\n", __func__, ret);
|
| 938 | dend = dp + ret;
|
| 939 |
|
| 940 | ceph_decode_8_safe(&dp, dend, struct_v, e_inval);
|
| 941 | ceph_decode_64_safe(&dp, dend, *nonce_plus_one, e_inval);
|
| 942 | dout("%s nonce_plus_one %llu\n", __func__, *nonce_plus_one);
|
| 943 | if (struct_v >= 2) {
|
| 944 | ret = decode_con_secret(&dp, dend, con_secret, con_secret_len);
|
| 945 | if (ret)
|
| 946 | return ret;
|
| 947 | }
|
| 948 |
|
| 949 | return 0;
|
| 950 |
|
| 951 | e_inval:
|
| 952 | return -EINVAL;
|
| 953 | }
|
| 954 |
|
| 955 | static int ceph_x_verify_authorizer_reply(struct ceph_auth_client *ac,
|
| 956 | struct ceph_authorizer *a,
|
| 957 | void *reply, int reply_len,
|
| 958 | u8 *session_key, int *session_key_len,
|
| 959 | u8 *con_secret, int *con_secret_len)
|
| 960 | {
|
| 961 | struct ceph_x_authorizer *au = (void *)a;
|
| 962 | u64 nonce_plus_one;
|
| 963 | int ret;
|
| 964 |
|
| 965 | if (session_key) {
|
| 966 | memcpy(session_key, au->session_key.key, au->session_key.len);
|
| 967 | *session_key_len = au->session_key.len;
|
| 968 | }
|
| 969 |
|
| 970 | ret = decrypt_authorizer_reply(&au->session_key, &reply,
|
| 971 | reply + reply_len, &nonce_plus_one,
|
| 972 | con_secret, con_secret_len);
|
| 973 | if (ret)
|
| 974 | return ret;
|
| 975 |
|
| 976 | if (nonce_plus_one != au->nonce + 1) {
|
| 977 | pr_err("failed to authenticate server\n");
|
| 978 | return -EPERM;
|
| 979 | }
|
| 980 |
|
| 981 | return 0;
|
| 982 | }
|
| 983 |
|
| 984 | static void ceph_x_reset(struct ceph_auth_client *ac)
|
| 985 | {
|
| 986 | struct ceph_x_info *xi = ac->private;
|
| 987 |
|
| 988 | dout("reset\n");
|
| 989 | xi->starting = true;
|
| 990 | xi->server_challenge = 0;
|
| 991 | }
|
| 992 |
|
| 993 | static void ceph_x_destroy(struct ceph_auth_client *ac)
|
| 994 | {
|
| 995 | struct ceph_x_info *xi = ac->private;
|
| 996 | struct rb_node *p;
|
| 997 |
|
| 998 | dout("ceph_x_destroy %p\n", ac);
|
| 999 | ceph_crypto_key_destroy(&xi->secret);
|
| 1000 |
|
| 1001 | while ((p = rb_first(&xi->ticket_handlers)) != NULL) {
|
| 1002 | struct ceph_x_ticket_handler *th =
|
| 1003 | rb_entry(p, struct ceph_x_ticket_handler, node);
|
| 1004 | remove_ticket_handler(ac, th);
|
| 1005 | }
|
| 1006 |
|
| 1007 | ceph_x_authorizer_cleanup(&xi->auth_authorizer);
|
| 1008 |
|
| 1009 | kfree(ac->private);
|
| 1010 | ac->private = NULL;
|
| 1011 | }
|
| 1012 |
|
| 1013 | static void invalidate_ticket(struct ceph_auth_client *ac, int peer_type)
|
| 1014 | {
|
| 1015 | struct ceph_x_ticket_handler *th;
|
| 1016 |
|
| 1017 | th = get_ticket_handler(ac, peer_type);
|
| 1018 | if (IS_ERR(th))
|
| 1019 | return;
|
| 1020 |
|
| 1021 | if (th->have_key) {
|
| 1022 | dout("ticket %d (%s) secret_id %llu invalidated\n",
|
| 1023 | th->service, ceph_entity_type_name(th->service),
|
| 1024 | th->secret_id);
|
| 1025 | th->have_key = false;
|
| 1026 | }
|
| 1027 | }
|
| 1028 |
|
| 1029 | static void ceph_x_invalidate_authorizer(struct ceph_auth_client *ac,
|
| 1030 | int peer_type)
|
| 1031 | {
|
| 1032 | |
| 1033 | * We are to invalidate a service ticket in the hopes of
|
| 1034 | * getting a new, hopefully more valid, one. But, we won't get
|
| 1035 | * it unless our AUTH ticket is good, so invalidate AUTH ticket
|
| 1036 | * as well, just in case.
|
| 1037 |
|
| 1038 | invalidate_ticket(ac, peer_type);
|
| 1039 | invalidate_ticket(ac, CEPH_ENTITY_TYPE_AUTH);
|
| 1040 | }
|
| 1041 |
|
| 1042 | static int calc_signature(struct ceph_x_authorizer *au, struct ceph_msg *msg,
|
| 1043 | __le64 *psig)
|
| 1044 | {
|
| 1045 | void *enc_buf = au->enc_buf;
|
| 1046 | int ret;
|
| 1047 |
|
| 1048 | if (!CEPH_HAVE_FEATURE(msg->con->peer_features, CEPHX_V2)) {
|
| 1049 | struct {
|
| 1050 | __le32 len;
|
| 1051 | __le32 header_crc;
|
| 1052 | __le32 front_crc;
|
| 1053 | __le32 middle_crc;
|
| 1054 | __le32 data_crc;
|
| 1055 | } __packed *sigblock = enc_buf +
|
| 1056 | ceph_x_encrypt_offset(&au->session_key);
|
| 1057 |
|
| 1058 | sigblock->len = cpu_to_le32(4*sizeof(u32));
|
| 1059 | sigblock->header_crc = msg->hdr.crc;
|
| 1060 | sigblock->front_crc = msg->footer.front_crc;
|
| 1061 | sigblock->middle_crc = msg->footer.middle_crc;
|
| 1062 | sigblock->data_crc = msg->footer.data_crc;
|
| 1063 |
|
| 1064 | ret = ceph_x_encrypt(&au->session_key, 0 ,
|
| 1065 | enc_buf, CEPHX_AU_ENC_BUF_LEN,
|
| 1066 | sizeof(*sigblock));
|
| 1067 | if (ret < 0)
|
| 1068 | return ret;
|
| 1069 |
|
| 1070 | *psig = *(__le64 *)(enc_buf + sizeof(u32));
|
| 1071 | } else {
|
| 1072 | struct {
|
| 1073 | __le32 header_crc;
|
| 1074 | __le32 front_crc;
|
| 1075 | __le32 front_len;
|
| 1076 | __le32 middle_crc;
|
| 1077 | __le32 middle_len;
|
| 1078 | __le32 data_crc;
|
| 1079 | __le32 data_len;
|
| 1080 | __le32 seq_lower_word;
|
| 1081 | } __packed *sigblock;
|
| 1082 | struct {
|
| 1083 | __le64 a, b, c, d;
|
| 1084 | } __packed *penc = enc_buf;
|
| 1085 |
|
| 1086 | if (au->session_key.type == CEPH_CRYPTO_AES) {
|
| 1087 |
|
| 1088 | sigblock = enc_buf;
|
| 1089 | } else {
|
| 1090 | BUILD_BUG_ON(SHA256_DIGEST_SIZE + sizeof(*sigblock) >
|
| 1091 | CEPHX_AU_ENC_BUF_LEN);
|
| 1092 | sigblock = enc_buf + SHA256_DIGEST_SIZE;
|
| 1093 | }
|
| 1094 |
|
| 1095 | sigblock->header_crc = msg->hdr.crc;
|
| 1096 | sigblock->front_crc = msg->footer.front_crc;
|
| 1097 | sigblock->front_len = msg->hdr.front_len;
|
| 1098 | sigblock->middle_crc = msg->footer.middle_crc;
|
| 1099 | sigblock->middle_len = msg->hdr.middle_len;
|
| 1100 | sigblock->data_crc = msg->footer.data_crc;
|
| 1101 | sigblock->data_len = msg->hdr.data_len;
|
| 1102 | sigblock->seq_lower_word = *(__le32 *)&msg->hdr.seq;
|
| 1103 |
|
| 1104 | if (au->session_key.type == CEPH_CRYPTO_AES) {
|
| 1105 | int ciphertext_len;
|
| 1106 |
|
| 1107 | ret = ceph_crypt(&au->session_key, 0 ,
|
| 1108 | true, enc_buf, CEPHX_AU_ENC_BUF_LEN,
|
| 1109 | sizeof(*sigblock), &ciphertext_len);
|
| 1110 | if (ret)
|
| 1111 | return ret;
|
| 1112 | } else {
|
| 1113 | ceph_hmac_sha256(&au->session_key, sigblock,
|
| 1114 | sizeof(*sigblock), enc_buf);
|
| 1115 | }
|
| 1116 |
|
| 1117 | *psig = penc->a ^ penc->b ^ penc->c ^ penc->d;
|
| 1118 | }
|
| 1119 |
|
| 1120 | return 0;
|
| 1121 | }
|
| 1122 |
|
| 1123 | static int ceph_x_sign_message(struct ceph_auth_handshake *auth,
|
| 1124 | struct ceph_msg *msg)
|
| 1125 | {
|
| 1126 | __le64 sig;
|
| 1127 | int ret;
|
| 1128 |
|
| 1129 | if (ceph_test_opt(from_msgr(msg->con->msgr), NOMSGSIGN))
|
| 1130 | return 0;
|
| 1131 |
|
| 1132 | ret = calc_signature((struct ceph_x_authorizer *)auth->authorizer,
|
| 1133 | msg, &sig);
|
| 1134 | if (ret)
|
| 1135 | return ret;
|
| 1136 |
|
| 1137 | msg->footer.sig = sig;
|
| 1138 | msg->footer.flags |= CEPH_MSG_FOOTER_SIGNED;
|
| 1139 | return 0;
|
| 1140 | }
|
| 1141 |
|
| 1142 | static int ceph_x_check_message_signature(struct ceph_auth_handshake *auth,
|
| 1143 | struct ceph_msg *msg)
|
| 1144 | {
|
| 1145 | __le64 sig_check;
|
| 1146 | int ret;
|
| 1147 |
|
| 1148 | if (ceph_test_opt(from_msgr(msg->con->msgr), NOMSGSIGN))
|
| 1149 | return 0;
|
| 1150 |
|
| 1151 | ret = calc_signature((struct ceph_x_authorizer *)auth->authorizer,
|
| 1152 | msg, &sig_check);
|
| 1153 | if (ret)
|
| 1154 | return ret;
|
| 1155 | if (sig_check == msg->footer.sig)
|
| 1156 | return 0;
|
| 1157 | if (msg->footer.flags & CEPH_MSG_FOOTER_SIGNED)
|
| 1158 | dout("ceph_x_check_message_signature %p has signature %llx "
|
| 1159 | "expect %llx\n", msg, msg->footer.sig, sig_check);
|
| 1160 | else
|
| 1161 | dout("ceph_x_check_message_signature %p sender did not set "
|
| 1162 | "CEPH_MSG_FOOTER_SIGNED\n", msg);
|
| 1163 | return -EBADMSG;
|
| 1164 | }
|
| 1165 |
|
| 1166 | static const struct ceph_auth_client_ops ceph_x_ops = {
|
| 1167 | .is_authenticated = ceph_x_is_authenticated,
|
| 1168 | .should_authenticate = ceph_x_should_authenticate,
|
| 1169 | .build_request = ceph_x_build_request,
|
| 1170 | .handle_reply = ceph_x_handle_reply,
|
| 1171 | .create_authorizer = ceph_x_create_authorizer,
|
| 1172 | .update_authorizer = ceph_x_update_authorizer,
|
| 1173 | .add_authorizer_challenge = ceph_x_add_authorizer_challenge,
|
| 1174 | .verify_authorizer_reply = ceph_x_verify_authorizer_reply,
|
| 1175 | .invalidate_authorizer = ceph_x_invalidate_authorizer,
|
| 1176 | .reset = ceph_x_reset,
|
| 1177 | .destroy = ceph_x_destroy,
|
| 1178 | .sign_message = ceph_x_sign_message,
|
| 1179 | .check_message_signature = ceph_x_check_message_signature,
|
| 1180 | };
|
| 1181 |
|
| 1182 |
|
| 1183 | int ceph_x_init(struct ceph_auth_client *ac)
|
| 1184 | {
|
| 1185 | struct ceph_x_info *xi;
|
| 1186 | int ret;
|
| 1187 |
|
| 1188 | dout("ceph_x_init %p\n", ac);
|
| 1189 | xi = kzalloc_obj(*xi, GFP_NOFS);
|
| 1190 | if (!xi)
|
| 1191 | return -ENOMEM;
|
| 1192 |
|
| 1193 | ret = -EINVAL;
|
| 1194 | if (!ac->key) {
|
| 1195 | pr_err("no secret set (for auth_x protocol)\n");
|
| 1196 | goto err_xi;
|
| 1197 | }
|
| 1198 |
|
| 1199 | ret = ceph_crypto_key_clone(&xi->secret, ac->key);
|
| 1200 | if (ret < 0) {
|
| 1201 | pr_err("cannot clone key: %d\n", ret);
|
| 1202 | goto err_xi;
|
| 1203 | }
|
| 1204 |
|
| 1205 | ret = ceph_crypto_key_prepare(&xi->secret, client_key_usages,
|
| 1206 | ARRAY_SIZE(client_key_usages));
|
| 1207 | if (ret) {
|
| 1208 | pr_err("cannot prepare key: %d\n", ret);
|
| 1209 | goto err_secret;
|
| 1210 | }
|
| 1211 |
|
| 1212 | xi->starting = true;
|
| 1213 | xi->ticket_handlers = RB_ROOT;
|
| 1214 |
|
| 1215 | ac->protocol = CEPH_AUTH_CEPHX;
|
| 1216 | ac->private = xi;
|
| 1217 | ac->ops = &ceph_x_ops;
|
| 1218 | return 0;
|
| 1219 |
|
| 1220 | err_secret:
|
| 1221 | ceph_crypto_key_destroy(&xi->secret);
|
| 1222 | err_xi:
|
| 1223 | kfree(xi);
|
| 1224 | return ret;
|
| 1225 | }
|