1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 23d14c5d2SYehuda Sadeh 33d14c5d2SYehuda Sadeh #include <linux/ceph/ceph_debug.h> 43d14c5d2SYehuda Sadeh 53d14c5d2SYehuda Sadeh #include <linux/err.h> 63d14c5d2SYehuda Sadeh #include <linux/module.h> 73d14c5d2SYehuda Sadeh #include <linux/random.h> 83d14c5d2SYehuda Sadeh #include <linux/slab.h> 93d14c5d2SYehuda Sadeh 103d14c5d2SYehuda Sadeh #include <linux/ceph/decode.h> 113d14c5d2SYehuda Sadeh #include <linux/ceph/auth.h> 12cc255c76SIlya Dryomov #include <linux/ceph/ceph_features.h> 13a51983e4SIlya Dryomov #include <linux/ceph/libceph.h> 1433d07337SYan, Zheng #include <linux/ceph/messenger.h> 153d14c5d2SYehuda Sadeh 163d14c5d2SYehuda Sadeh #include "crypto.h" 173d14c5d2SYehuda Sadeh #include "auth_x.h" 183d14c5d2SYehuda Sadeh #include "auth_x_protocol.h" 193d14c5d2SYehuda Sadeh 203d14c5d2SYehuda Sadeh static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed); 213d14c5d2SYehuda Sadeh 223d14c5d2SYehuda Sadeh static int ceph_x_is_authenticated(struct ceph_auth_client *ac) 233d14c5d2SYehuda Sadeh { 243d14c5d2SYehuda Sadeh struct ceph_x_info *xi = ac->private; 253d14c5d2SYehuda Sadeh int need; 263d14c5d2SYehuda Sadeh 273d14c5d2SYehuda Sadeh ceph_x_validate_tickets(ac, &need); 283d14c5d2SYehuda Sadeh dout("ceph_x_is_authenticated want=%d need=%d have=%d\n", 293d14c5d2SYehuda Sadeh ac->want_keys, need, xi->have_keys); 303d14c5d2SYehuda Sadeh return (ac->want_keys & xi->have_keys) == ac->want_keys; 313d14c5d2SYehuda Sadeh } 323d14c5d2SYehuda Sadeh 333d14c5d2SYehuda Sadeh static int ceph_x_should_authenticate(struct ceph_auth_client *ac) 343d14c5d2SYehuda Sadeh { 353d14c5d2SYehuda Sadeh struct ceph_x_info *xi = ac->private; 363d14c5d2SYehuda Sadeh int need; 373d14c5d2SYehuda Sadeh 383d14c5d2SYehuda Sadeh ceph_x_validate_tickets(ac, &need); 393d14c5d2SYehuda Sadeh dout("ceph_x_should_authenticate want=%d need=%d have=%d\n", 403d14c5d2SYehuda Sadeh ac->want_keys, need, xi->have_keys); 413d14c5d2SYehuda Sadeh return need != 0; 423d14c5d2SYehuda Sadeh } 433d14c5d2SYehuda Sadeh 4455d9cc83SIlya Dryomov static int ceph_x_encrypt_offset(void) 4555d9cc83SIlya Dryomov { 4655d9cc83SIlya Dryomov return sizeof(u32) + sizeof(struct ceph_x_encrypt_header); 4755d9cc83SIlya Dryomov } 4855d9cc83SIlya Dryomov 493d14c5d2SYehuda Sadeh static int ceph_x_encrypt_buflen(int ilen) 503d14c5d2SYehuda Sadeh { 5155d9cc83SIlya Dryomov return ceph_x_encrypt_offset() + ilen + 16; 523d14c5d2SYehuda Sadeh } 533d14c5d2SYehuda Sadeh 54d03857c6SIlya Dryomov static int ceph_x_encrypt(struct ceph_crypto_key *secret, void *buf, 55d03857c6SIlya Dryomov int buf_len, int plaintext_len) 563d14c5d2SYehuda Sadeh { 57d03857c6SIlya Dryomov struct ceph_x_encrypt_header *hdr = buf + sizeof(u32); 58d03857c6SIlya Dryomov int ciphertext_len; 593d14c5d2SYehuda Sadeh int ret; 603d14c5d2SYehuda Sadeh 61d03857c6SIlya Dryomov hdr->struct_v = 1; 62d03857c6SIlya Dryomov hdr->magic = cpu_to_le64(CEPHX_ENC_MAGIC); 63d03857c6SIlya Dryomov 64d03857c6SIlya Dryomov ret = ceph_crypt(secret, true, buf + sizeof(u32), buf_len - sizeof(u32), 65d03857c6SIlya Dryomov plaintext_len + sizeof(struct ceph_x_encrypt_header), 66d03857c6SIlya Dryomov &ciphertext_len); 673d14c5d2SYehuda Sadeh if (ret) 683d14c5d2SYehuda Sadeh return ret; 69d03857c6SIlya Dryomov 70d03857c6SIlya Dryomov ceph_encode_32(&buf, ciphertext_len); 71d03857c6SIlya Dryomov return sizeof(u32) + ciphertext_len; 723d14c5d2SYehuda Sadeh } 733d14c5d2SYehuda Sadeh 74c571fe24SIlya Dryomov static int __ceph_x_decrypt(struct ceph_crypto_key *secret, void *p, 75c571fe24SIlya Dryomov int ciphertext_len) 76c571fe24SIlya Dryomov { 77c571fe24SIlya Dryomov struct ceph_x_encrypt_header *hdr = p; 78c571fe24SIlya Dryomov int plaintext_len; 79c571fe24SIlya Dryomov int ret; 80c571fe24SIlya Dryomov 81c571fe24SIlya Dryomov ret = ceph_crypt(secret, false, p, ciphertext_len, ciphertext_len, 82c571fe24SIlya Dryomov &plaintext_len); 83c571fe24SIlya Dryomov if (ret) 84c571fe24SIlya Dryomov return ret; 85c571fe24SIlya Dryomov 86c571fe24SIlya Dryomov if (le64_to_cpu(hdr->magic) != CEPHX_ENC_MAGIC) { 87c571fe24SIlya Dryomov pr_err("%s bad magic\n", __func__); 88c571fe24SIlya Dryomov return -EINVAL; 89c571fe24SIlya Dryomov } 90c571fe24SIlya Dryomov 91c571fe24SIlya Dryomov return plaintext_len - sizeof(*hdr); 92c571fe24SIlya Dryomov } 93c571fe24SIlya Dryomov 94e15fd0a1SIlya Dryomov static int ceph_x_decrypt(struct ceph_crypto_key *secret, void **p, void *end) 953d14c5d2SYehuda Sadeh { 96c571fe24SIlya Dryomov int ciphertext_len; 97e15fd0a1SIlya Dryomov int ret; 983d14c5d2SYehuda Sadeh 99e15fd0a1SIlya Dryomov ceph_decode_32_safe(p, end, ciphertext_len, e_inval); 100e15fd0a1SIlya Dryomov ceph_decode_need(p, end, ciphertext_len, e_inval); 1013d14c5d2SYehuda Sadeh 102c571fe24SIlya Dryomov ret = __ceph_x_decrypt(secret, *p, ciphertext_len); 103c571fe24SIlya Dryomov if (ret < 0) 1043d14c5d2SYehuda Sadeh return ret; 105e15fd0a1SIlya Dryomov 106e15fd0a1SIlya Dryomov *p += ciphertext_len; 107c571fe24SIlya Dryomov return ret; 108e15fd0a1SIlya Dryomov 109e15fd0a1SIlya Dryomov e_inval: 110e15fd0a1SIlya Dryomov return -EINVAL; 1113d14c5d2SYehuda Sadeh } 1123d14c5d2SYehuda Sadeh 1133d14c5d2SYehuda Sadeh /* 1143d14c5d2SYehuda Sadeh * get existing (or insert new) ticket handler 1153d14c5d2SYehuda Sadeh */ 1163d14c5d2SYehuda Sadeh static struct ceph_x_ticket_handler * 1173d14c5d2SYehuda Sadeh get_ticket_handler(struct ceph_auth_client *ac, int service) 1183d14c5d2SYehuda Sadeh { 1193d14c5d2SYehuda Sadeh struct ceph_x_ticket_handler *th; 1203d14c5d2SYehuda Sadeh struct ceph_x_info *xi = ac->private; 1213d14c5d2SYehuda Sadeh struct rb_node *parent = NULL, **p = &xi->ticket_handlers.rb_node; 1223d14c5d2SYehuda Sadeh 1233d14c5d2SYehuda Sadeh while (*p) { 1243d14c5d2SYehuda Sadeh parent = *p; 1253d14c5d2SYehuda Sadeh th = rb_entry(parent, struct ceph_x_ticket_handler, node); 1263d14c5d2SYehuda Sadeh if (service < th->service) 1273d14c5d2SYehuda Sadeh p = &(*p)->rb_left; 1283d14c5d2SYehuda Sadeh else if (service > th->service) 1293d14c5d2SYehuda Sadeh p = &(*p)->rb_right; 1303d14c5d2SYehuda Sadeh else 1313d14c5d2SYehuda Sadeh return th; 1323d14c5d2SYehuda Sadeh } 1333d14c5d2SYehuda Sadeh 1343d14c5d2SYehuda Sadeh /* add it */ 1353d14c5d2SYehuda Sadeh th = kzalloc(sizeof(*th), GFP_NOFS); 1363d14c5d2SYehuda Sadeh if (!th) 1373d14c5d2SYehuda Sadeh return ERR_PTR(-ENOMEM); 1383d14c5d2SYehuda Sadeh th->service = service; 1393d14c5d2SYehuda Sadeh rb_link_node(&th->node, parent, p); 1403d14c5d2SYehuda Sadeh rb_insert_color(&th->node, &xi->ticket_handlers); 1413d14c5d2SYehuda Sadeh return th; 1423d14c5d2SYehuda Sadeh } 1433d14c5d2SYehuda Sadeh 1443d14c5d2SYehuda Sadeh static void remove_ticket_handler(struct ceph_auth_client *ac, 1453d14c5d2SYehuda Sadeh struct ceph_x_ticket_handler *th) 1463d14c5d2SYehuda Sadeh { 1473d14c5d2SYehuda Sadeh struct ceph_x_info *xi = ac->private; 1483d14c5d2SYehuda Sadeh 1493d14c5d2SYehuda Sadeh dout("remove_ticket_handler %p %d\n", th, th->service); 1503d14c5d2SYehuda Sadeh rb_erase(&th->node, &xi->ticket_handlers); 1513d14c5d2SYehuda Sadeh ceph_crypto_key_destroy(&th->session_key); 1523d14c5d2SYehuda Sadeh if (th->ticket_blob) 1533d14c5d2SYehuda Sadeh ceph_buffer_put(th->ticket_blob); 1543d14c5d2SYehuda Sadeh kfree(th); 1553d14c5d2SYehuda Sadeh } 1563d14c5d2SYehuda Sadeh 157597cda35SIlya Dryomov static int process_one_ticket(struct ceph_auth_client *ac, 1583d14c5d2SYehuda Sadeh struct ceph_crypto_key *secret, 159c27a3e4dSIlya Dryomov void **p, void *end) 1603d14c5d2SYehuda Sadeh { 1613d14c5d2SYehuda Sadeh struct ceph_x_info *xi = ac->private; 1623d14c5d2SYehuda Sadeh int type; 1633d14c5d2SYehuda Sadeh u8 tkt_struct_v, blob_struct_v; 1643d14c5d2SYehuda Sadeh struct ceph_x_ticket_handler *th; 1653d14c5d2SYehuda Sadeh void *dp, *dend; 1663d14c5d2SYehuda Sadeh int dlen; 1673d14c5d2SYehuda Sadeh char is_enc; 168473bd2d7SArnd Bergmann struct timespec64 validity; 1693d14c5d2SYehuda Sadeh void *tp, *tpend; 170e9226d7cSIlya Dryomov void **ptp; 171b51456a6SIlya Dryomov struct ceph_crypto_key new_session_key = { 0 }; 1723d14c5d2SYehuda Sadeh struct ceph_buffer *new_ticket_blob; 173473bd2d7SArnd Bergmann time64_t new_expires, new_renew_after; 1743d14c5d2SYehuda Sadeh u64 new_secret_id; 175597cda35SIlya Dryomov int ret; 1763d14c5d2SYehuda Sadeh 177597cda35SIlya Dryomov ceph_decode_need(p, end, sizeof(u32) + 1, bad); 1783d14c5d2SYehuda Sadeh 179597cda35SIlya Dryomov type = ceph_decode_32(p); 1803d14c5d2SYehuda Sadeh dout(" ticket type %d %s\n", type, ceph_entity_type_name(type)); 1813d14c5d2SYehuda Sadeh 182597cda35SIlya Dryomov tkt_struct_v = ceph_decode_8(p); 1833d14c5d2SYehuda Sadeh if (tkt_struct_v != 1) 1843d14c5d2SYehuda Sadeh goto bad; 1853d14c5d2SYehuda Sadeh 1863d14c5d2SYehuda Sadeh th = get_ticket_handler(ac, type); 1873d14c5d2SYehuda Sadeh if (IS_ERR(th)) { 1883d14c5d2SYehuda Sadeh ret = PTR_ERR(th); 1893d14c5d2SYehuda Sadeh goto out; 1903d14c5d2SYehuda Sadeh } 1913d14c5d2SYehuda Sadeh 1923d14c5d2SYehuda Sadeh /* blob for me */ 193e15fd0a1SIlya Dryomov dp = *p + ceph_x_encrypt_offset(); 194e15fd0a1SIlya Dryomov ret = ceph_x_decrypt(secret, p, end); 195e15fd0a1SIlya Dryomov if (ret < 0) 1963d14c5d2SYehuda Sadeh goto out; 197e15fd0a1SIlya Dryomov dout(" decrypted %d bytes\n", ret); 198e15fd0a1SIlya Dryomov dend = dp + ret; 1993d14c5d2SYehuda Sadeh 2003d14c5d2SYehuda Sadeh tkt_struct_v = ceph_decode_8(&dp); 2013d14c5d2SYehuda Sadeh if (tkt_struct_v != 1) 2023d14c5d2SYehuda Sadeh goto bad; 2033d14c5d2SYehuda Sadeh 2043d14c5d2SYehuda Sadeh ret = ceph_crypto_key_decode(&new_session_key, &dp, dend); 2053d14c5d2SYehuda Sadeh if (ret) 2063d14c5d2SYehuda Sadeh goto out; 2073d14c5d2SYehuda Sadeh 208473bd2d7SArnd Bergmann ceph_decode_timespec64(&validity, dp); 209f6cdb292SIlya Dryomov dp += sizeof(struct ceph_timespec); 210473bd2d7SArnd Bergmann new_expires = ktime_get_real_seconds() + validity.tv_sec; 2113d14c5d2SYehuda Sadeh new_renew_after = new_expires - (validity.tv_sec / 4); 212473bd2d7SArnd Bergmann dout(" expires=%llu renew_after=%llu\n", new_expires, 2133d14c5d2SYehuda Sadeh new_renew_after); 2143d14c5d2SYehuda Sadeh 2153d14c5d2SYehuda Sadeh /* ticket blob for service */ 216597cda35SIlya Dryomov ceph_decode_8_safe(p, end, is_enc, bad); 2173d14c5d2SYehuda Sadeh if (is_enc) { 2183d14c5d2SYehuda Sadeh /* encrypted */ 219e15fd0a1SIlya Dryomov tp = *p + ceph_x_encrypt_offset(); 220e15fd0a1SIlya Dryomov ret = ceph_x_decrypt(&th->session_key, p, end); 221e15fd0a1SIlya Dryomov if (ret < 0) 2223d14c5d2SYehuda Sadeh goto out; 223e15fd0a1SIlya Dryomov dout(" encrypted ticket, decrypted %d bytes\n", ret); 224e9226d7cSIlya Dryomov ptp = &tp; 225e15fd0a1SIlya Dryomov tpend = tp + ret; 2263d14c5d2SYehuda Sadeh } else { 2273d14c5d2SYehuda Sadeh /* unencrypted */ 228e9226d7cSIlya Dryomov ptp = p; 229e9226d7cSIlya Dryomov tpend = end; 230c27a3e4dSIlya Dryomov } 231e9226d7cSIlya Dryomov ceph_decode_32_safe(ptp, tpend, dlen, bad); 2323d14c5d2SYehuda Sadeh dout(" ticket blob is %d bytes\n", dlen); 233e9226d7cSIlya Dryomov ceph_decode_need(ptp, tpend, 1 + sizeof(u64), bad); 234e9226d7cSIlya Dryomov blob_struct_v = ceph_decode_8(ptp); 235d18a1247SIlya Dryomov if (blob_struct_v != 1) 236d18a1247SIlya Dryomov goto bad; 237d18a1247SIlya Dryomov 238e9226d7cSIlya Dryomov new_secret_id = ceph_decode_64(ptp); 239e9226d7cSIlya Dryomov ret = ceph_decode_buffer(&new_ticket_blob, ptp, tpend); 2403d14c5d2SYehuda Sadeh if (ret) 2413d14c5d2SYehuda Sadeh goto out; 2423d14c5d2SYehuda Sadeh 2433d14c5d2SYehuda Sadeh /* all is well, update our ticket */ 2443d14c5d2SYehuda Sadeh ceph_crypto_key_destroy(&th->session_key); 2453d14c5d2SYehuda Sadeh if (th->ticket_blob) 2463d14c5d2SYehuda Sadeh ceph_buffer_put(th->ticket_blob); 2473d14c5d2SYehuda Sadeh th->session_key = new_session_key; 2483d14c5d2SYehuda Sadeh th->ticket_blob = new_ticket_blob; 2493d14c5d2SYehuda Sadeh th->secret_id = new_secret_id; 2503d14c5d2SYehuda Sadeh th->expires = new_expires; 2513d14c5d2SYehuda Sadeh th->renew_after = new_renew_after; 2526abe097dSIlya Dryomov th->have_key = true; 2533d14c5d2SYehuda Sadeh dout(" got ticket service %d (%s) secret_id %lld len %d\n", 2543d14c5d2SYehuda Sadeh type, ceph_entity_type_name(type), th->secret_id, 2553d14c5d2SYehuda Sadeh (int)th->ticket_blob->vec.iov_len); 2563d14c5d2SYehuda Sadeh xi->have_keys |= th->service; 257b51456a6SIlya Dryomov return 0; 258597cda35SIlya Dryomov 259597cda35SIlya Dryomov bad: 260597cda35SIlya Dryomov ret = -EINVAL; 261b51456a6SIlya Dryomov out: 262b51456a6SIlya Dryomov ceph_crypto_key_destroy(&new_session_key); 263b51456a6SIlya Dryomov return ret; 264597cda35SIlya Dryomov } 265597cda35SIlya Dryomov 266597cda35SIlya Dryomov static int ceph_x_proc_ticket_reply(struct ceph_auth_client *ac, 267597cda35SIlya Dryomov struct ceph_crypto_key *secret, 268597cda35SIlya Dryomov void *buf, void *end) 269597cda35SIlya Dryomov { 270597cda35SIlya Dryomov void *p = buf; 271597cda35SIlya Dryomov u8 reply_struct_v; 272597cda35SIlya Dryomov u32 num; 273597cda35SIlya Dryomov int ret; 274597cda35SIlya Dryomov 275597cda35SIlya Dryomov ceph_decode_8_safe(&p, end, reply_struct_v, bad); 276597cda35SIlya Dryomov if (reply_struct_v != 1) 277597cda35SIlya Dryomov return -EINVAL; 278597cda35SIlya Dryomov 279597cda35SIlya Dryomov ceph_decode_32_safe(&p, end, num, bad); 280597cda35SIlya Dryomov dout("%d tickets\n", num); 281597cda35SIlya Dryomov 282597cda35SIlya Dryomov while (num--) { 283c27a3e4dSIlya Dryomov ret = process_one_ticket(ac, secret, &p, end); 284597cda35SIlya Dryomov if (ret) 285c27a3e4dSIlya Dryomov return ret; 2863d14c5d2SYehuda Sadeh } 2873d14c5d2SYehuda Sadeh 288c27a3e4dSIlya Dryomov return 0; 2893d14c5d2SYehuda Sadeh 2903d14c5d2SYehuda Sadeh bad: 291c27a3e4dSIlya Dryomov return -EINVAL; 2923d14c5d2SYehuda Sadeh } 2933d14c5d2SYehuda Sadeh 294149cac4aSIlya Dryomov /* 295149cac4aSIlya Dryomov * Encode and encrypt the second part (ceph_x_authorize_b) of the 296149cac4aSIlya Dryomov * authorizer. The first part (ceph_x_authorize_a) should already be 297149cac4aSIlya Dryomov * encoded. 298149cac4aSIlya Dryomov */ 2996daca13dSIlya Dryomov static int encrypt_authorizer(struct ceph_x_authorizer *au, 3006daca13dSIlya Dryomov u64 *server_challenge) 301149cac4aSIlya Dryomov { 302149cac4aSIlya Dryomov struct ceph_x_authorize_a *msg_a; 303149cac4aSIlya Dryomov struct ceph_x_authorize_b *msg_b; 304149cac4aSIlya Dryomov void *p, *end; 305149cac4aSIlya Dryomov int ret; 306149cac4aSIlya Dryomov 307149cac4aSIlya Dryomov msg_a = au->buf->vec.iov_base; 308149cac4aSIlya Dryomov WARN_ON(msg_a->ticket_blob.secret_id != cpu_to_le64(au->secret_id)); 309149cac4aSIlya Dryomov p = (void *)(msg_a + 1) + le32_to_cpu(msg_a->ticket_blob.blob_len); 310149cac4aSIlya Dryomov end = au->buf->vec.iov_base + au->buf->vec.iov_len; 311149cac4aSIlya Dryomov 312149cac4aSIlya Dryomov msg_b = p + ceph_x_encrypt_offset(); 3136daca13dSIlya Dryomov msg_b->struct_v = 2; 314149cac4aSIlya Dryomov msg_b->nonce = cpu_to_le64(au->nonce); 3156daca13dSIlya Dryomov if (server_challenge) { 3166daca13dSIlya Dryomov msg_b->have_challenge = 1; 3176daca13dSIlya Dryomov msg_b->server_challenge_plus_one = 3186daca13dSIlya Dryomov cpu_to_le64(*server_challenge + 1); 3196daca13dSIlya Dryomov } else { 3206daca13dSIlya Dryomov msg_b->have_challenge = 0; 3216daca13dSIlya Dryomov msg_b->server_challenge_plus_one = 0; 3226daca13dSIlya Dryomov } 323149cac4aSIlya Dryomov 324149cac4aSIlya Dryomov ret = ceph_x_encrypt(&au->session_key, p, end - p, sizeof(*msg_b)); 325149cac4aSIlya Dryomov if (ret < 0) 326149cac4aSIlya Dryomov return ret; 327149cac4aSIlya Dryomov 328149cac4aSIlya Dryomov p += ret; 3296daca13dSIlya Dryomov if (server_challenge) { 3306daca13dSIlya Dryomov WARN_ON(p != end); 3316daca13dSIlya Dryomov } else { 332149cac4aSIlya Dryomov WARN_ON(p > end); 333149cac4aSIlya Dryomov au->buf->vec.iov_len = p - au->buf->vec.iov_base; 3346daca13dSIlya Dryomov } 335149cac4aSIlya Dryomov 336149cac4aSIlya Dryomov return 0; 337149cac4aSIlya Dryomov } 338149cac4aSIlya Dryomov 339cbf99a11SIlya Dryomov static void ceph_x_authorizer_cleanup(struct ceph_x_authorizer *au) 340cbf99a11SIlya Dryomov { 341cbf99a11SIlya Dryomov ceph_crypto_key_destroy(&au->session_key); 342cbf99a11SIlya Dryomov if (au->buf) { 343cbf99a11SIlya Dryomov ceph_buffer_put(au->buf); 344cbf99a11SIlya Dryomov au->buf = NULL; 345cbf99a11SIlya Dryomov } 346cbf99a11SIlya Dryomov } 347cbf99a11SIlya Dryomov 3483d14c5d2SYehuda Sadeh static int ceph_x_build_authorizer(struct ceph_auth_client *ac, 3493d14c5d2SYehuda Sadeh struct ceph_x_ticket_handler *th, 3503d14c5d2SYehuda Sadeh struct ceph_x_authorizer *au) 3513d14c5d2SYehuda Sadeh { 3523d14c5d2SYehuda Sadeh int maxlen; 3533d14c5d2SYehuda Sadeh struct ceph_x_authorize_a *msg_a; 354d03857c6SIlya Dryomov struct ceph_x_authorize_b *msg_b; 3553d14c5d2SYehuda Sadeh int ret; 3563d14c5d2SYehuda Sadeh int ticket_blob_len = 3573d14c5d2SYehuda Sadeh (th->ticket_blob ? th->ticket_blob->vec.iov_len : 0); 3583d14c5d2SYehuda Sadeh 3593d14c5d2SYehuda Sadeh dout("build_authorizer for %s %p\n", 3603d14c5d2SYehuda Sadeh ceph_entity_type_name(th->service), au); 3613d14c5d2SYehuda Sadeh 362ae385eafSYan, Zheng ceph_crypto_key_destroy(&au->session_key); 363ae385eafSYan, Zheng ret = ceph_crypto_key_clone(&au->session_key, &th->session_key); 364ae385eafSYan, Zheng if (ret) 365cbf99a11SIlya Dryomov goto out_au; 366ae385eafSYan, Zheng 36736721eceSIlya Dryomov maxlen = sizeof(*msg_a) + ticket_blob_len + 368d03857c6SIlya Dryomov ceph_x_encrypt_buflen(sizeof(*msg_b)); 3693d14c5d2SYehuda Sadeh dout(" need len %d\n", maxlen); 3703d14c5d2SYehuda Sadeh if (au->buf && au->buf->alloc_len < maxlen) { 3713d14c5d2SYehuda Sadeh ceph_buffer_put(au->buf); 3723d14c5d2SYehuda Sadeh au->buf = NULL; 3733d14c5d2SYehuda Sadeh } 3743d14c5d2SYehuda Sadeh if (!au->buf) { 3753d14c5d2SYehuda Sadeh au->buf = ceph_buffer_new(maxlen, GFP_NOFS); 376ae385eafSYan, Zheng if (!au->buf) { 377cbf99a11SIlya Dryomov ret = -ENOMEM; 378cbf99a11SIlya Dryomov goto out_au; 3793d14c5d2SYehuda Sadeh } 380ae385eafSYan, Zheng } 3813d14c5d2SYehuda Sadeh au->service = th->service; 3820bed9b5cSSage Weil au->secret_id = th->secret_id; 3833d14c5d2SYehuda Sadeh 3843d14c5d2SYehuda Sadeh msg_a = au->buf->vec.iov_base; 3853d14c5d2SYehuda Sadeh msg_a->struct_v = 1; 3863d14c5d2SYehuda Sadeh msg_a->global_id = cpu_to_le64(ac->global_id); 3873d14c5d2SYehuda Sadeh msg_a->service_id = cpu_to_le32(th->service); 3883d14c5d2SYehuda Sadeh msg_a->ticket_blob.struct_v = 1; 3893d14c5d2SYehuda Sadeh msg_a->ticket_blob.secret_id = cpu_to_le64(th->secret_id); 3903d14c5d2SYehuda Sadeh msg_a->ticket_blob.blob_len = cpu_to_le32(ticket_blob_len); 3913d14c5d2SYehuda Sadeh if (ticket_blob_len) { 3923d14c5d2SYehuda Sadeh memcpy(msg_a->ticket_blob.blob, th->ticket_blob->vec.iov_base, 3933d14c5d2SYehuda Sadeh th->ticket_blob->vec.iov_len); 3943d14c5d2SYehuda Sadeh } 3953d14c5d2SYehuda Sadeh dout(" th %p secret_id %lld %lld\n", th, th->secret_id, 3963d14c5d2SYehuda Sadeh le64_to_cpu(msg_a->ticket_blob.secret_id)); 3973d14c5d2SYehuda Sadeh 3983d14c5d2SYehuda Sadeh get_random_bytes(&au->nonce, sizeof(au->nonce)); 3996daca13dSIlya Dryomov ret = encrypt_authorizer(au, NULL); 400149cac4aSIlya Dryomov if (ret) { 401149cac4aSIlya Dryomov pr_err("failed to encrypt authorizer: %d", ret); 402cbf99a11SIlya Dryomov goto out_au; 403149cac4aSIlya Dryomov } 40436721eceSIlya Dryomov 4053d14c5d2SYehuda Sadeh dout(" built authorizer nonce %llx len %d\n", au->nonce, 4063d14c5d2SYehuda Sadeh (int)au->buf->vec.iov_len); 4073d14c5d2SYehuda Sadeh return 0; 4083d14c5d2SYehuda Sadeh 409cbf99a11SIlya Dryomov out_au: 410cbf99a11SIlya Dryomov ceph_x_authorizer_cleanup(au); 4113d14c5d2SYehuda Sadeh return ret; 4123d14c5d2SYehuda Sadeh } 4133d14c5d2SYehuda Sadeh 4143d14c5d2SYehuda Sadeh static int ceph_x_encode_ticket(struct ceph_x_ticket_handler *th, 4153d14c5d2SYehuda Sadeh void **p, void *end) 4163d14c5d2SYehuda Sadeh { 4173d14c5d2SYehuda Sadeh ceph_decode_need(p, end, 1 + sizeof(u64), bad); 4183d14c5d2SYehuda Sadeh ceph_encode_8(p, 1); 4193d14c5d2SYehuda Sadeh ceph_encode_64(p, th->secret_id); 4203d14c5d2SYehuda Sadeh if (th->ticket_blob) { 4213d14c5d2SYehuda Sadeh const char *buf = th->ticket_blob->vec.iov_base; 4223d14c5d2SYehuda Sadeh u32 len = th->ticket_blob->vec.iov_len; 4233d14c5d2SYehuda Sadeh 4243d14c5d2SYehuda Sadeh ceph_encode_32_safe(p, end, len, bad); 4253d14c5d2SYehuda Sadeh ceph_encode_copy_safe(p, end, buf, len, bad); 4263d14c5d2SYehuda Sadeh } else { 4273d14c5d2SYehuda Sadeh ceph_encode_32_safe(p, end, 0, bad); 4283d14c5d2SYehuda Sadeh } 4293d14c5d2SYehuda Sadeh 4303d14c5d2SYehuda Sadeh return 0; 4313d14c5d2SYehuda Sadeh bad: 4323d14c5d2SYehuda Sadeh return -ERANGE; 4333d14c5d2SYehuda Sadeh } 4343d14c5d2SYehuda Sadeh 4356abe097dSIlya Dryomov static bool need_key(struct ceph_x_ticket_handler *th) 4366abe097dSIlya Dryomov { 4376abe097dSIlya Dryomov if (!th->have_key) 4386abe097dSIlya Dryomov return true; 4396abe097dSIlya Dryomov 440473bd2d7SArnd Bergmann return ktime_get_real_seconds() >= th->renew_after; 4416abe097dSIlya Dryomov } 4426abe097dSIlya Dryomov 4436abe097dSIlya Dryomov static bool have_key(struct ceph_x_ticket_handler *th) 4446abe097dSIlya Dryomov { 4456abe097dSIlya Dryomov if (th->have_key) { 446473bd2d7SArnd Bergmann if (ktime_get_real_seconds() >= th->expires) 4476abe097dSIlya Dryomov th->have_key = false; 4486abe097dSIlya Dryomov } 4496abe097dSIlya Dryomov 4506abe097dSIlya Dryomov return th->have_key; 4516abe097dSIlya Dryomov } 4526abe097dSIlya Dryomov 4533d14c5d2SYehuda Sadeh static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed) 4543d14c5d2SYehuda Sadeh { 4553d14c5d2SYehuda Sadeh int want = ac->want_keys; 4563d14c5d2SYehuda Sadeh struct ceph_x_info *xi = ac->private; 4573d14c5d2SYehuda Sadeh int service; 4583d14c5d2SYehuda Sadeh 4593d14c5d2SYehuda Sadeh *pneed = ac->want_keys & ~(xi->have_keys); 4603d14c5d2SYehuda Sadeh 4613d14c5d2SYehuda Sadeh for (service = 1; service <= want; service <<= 1) { 4623d14c5d2SYehuda Sadeh struct ceph_x_ticket_handler *th; 4633d14c5d2SYehuda Sadeh 4643d14c5d2SYehuda Sadeh if (!(ac->want_keys & service)) 4653d14c5d2SYehuda Sadeh continue; 4663d14c5d2SYehuda Sadeh 4673d14c5d2SYehuda Sadeh if (*pneed & service) 4683d14c5d2SYehuda Sadeh continue; 4693d14c5d2SYehuda Sadeh 4703d14c5d2SYehuda Sadeh th = get_ticket_handler(ac, service); 4713d14c5d2SYehuda Sadeh if (IS_ERR(th)) { 4723d14c5d2SYehuda Sadeh *pneed |= service; 4733d14c5d2SYehuda Sadeh continue; 4743d14c5d2SYehuda Sadeh } 4753d14c5d2SYehuda Sadeh 4766abe097dSIlya Dryomov if (need_key(th)) 4773d14c5d2SYehuda Sadeh *pneed |= service; 4786abe097dSIlya Dryomov if (!have_key(th)) 4793d14c5d2SYehuda Sadeh xi->have_keys &= ~service; 4803d14c5d2SYehuda Sadeh } 4813d14c5d2SYehuda Sadeh } 4823d14c5d2SYehuda Sadeh 4833d14c5d2SYehuda Sadeh static int ceph_x_build_request(struct ceph_auth_client *ac, 4843d14c5d2SYehuda Sadeh void *buf, void *end) 4853d14c5d2SYehuda Sadeh { 4863d14c5d2SYehuda Sadeh struct ceph_x_info *xi = ac->private; 4873d14c5d2SYehuda Sadeh int need; 4883d14c5d2SYehuda Sadeh struct ceph_x_request_header *head = buf; 4893d14c5d2SYehuda Sadeh int ret; 4903d14c5d2SYehuda Sadeh struct ceph_x_ticket_handler *th = 4913d14c5d2SYehuda Sadeh get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH); 4923d14c5d2SYehuda Sadeh 4933d14c5d2SYehuda Sadeh if (IS_ERR(th)) 4943d14c5d2SYehuda Sadeh return PTR_ERR(th); 4953d14c5d2SYehuda Sadeh 4963d14c5d2SYehuda Sadeh ceph_x_validate_tickets(ac, &need); 4973d14c5d2SYehuda Sadeh 4983d14c5d2SYehuda Sadeh dout("build_request want %x have %x need %x\n", 4993d14c5d2SYehuda Sadeh ac->want_keys, xi->have_keys, need); 5003d14c5d2SYehuda Sadeh 5013d14c5d2SYehuda Sadeh if (need & CEPH_ENTITY_TYPE_AUTH) { 5023d14c5d2SYehuda Sadeh struct ceph_x_authenticate *auth = (void *)(head + 1); 5033d14c5d2SYehuda Sadeh void *p = auth + 1; 504d03857c6SIlya Dryomov void *enc_buf = xi->auth_authorizer.enc_buf; 505d03857c6SIlya Dryomov struct ceph_x_challenge_blob *blob = enc_buf + 506d03857c6SIlya Dryomov ceph_x_encrypt_offset(); 5073d14c5d2SYehuda Sadeh u64 *u; 5083d14c5d2SYehuda Sadeh 5093d14c5d2SYehuda Sadeh if (p > end) 5103d14c5d2SYehuda Sadeh return -ERANGE; 5113d14c5d2SYehuda Sadeh 5123d14c5d2SYehuda Sadeh dout(" get_auth_session_key\n"); 5133d14c5d2SYehuda Sadeh head->op = cpu_to_le16(CEPHX_GET_AUTH_SESSION_KEY); 5143d14c5d2SYehuda Sadeh 5153d14c5d2SYehuda Sadeh /* encrypt and hash */ 5163d14c5d2SYehuda Sadeh get_random_bytes(&auth->client_challenge, sizeof(u64)); 517d03857c6SIlya Dryomov blob->client_challenge = auth->client_challenge; 518d03857c6SIlya Dryomov blob->server_challenge = cpu_to_le64(xi->server_challenge); 519d03857c6SIlya Dryomov ret = ceph_x_encrypt(&xi->secret, enc_buf, CEPHX_AU_ENC_BUF_LEN, 520d03857c6SIlya Dryomov sizeof(*blob)); 5213d14c5d2SYehuda Sadeh if (ret < 0) 5223d14c5d2SYehuda Sadeh return ret; 5233d14c5d2SYehuda Sadeh 5243d14c5d2SYehuda Sadeh auth->struct_v = 1; 5253d14c5d2SYehuda Sadeh auth->key = 0; 526d03857c6SIlya Dryomov for (u = (u64 *)enc_buf; u + 1 <= (u64 *)(enc_buf + ret); u++) 5273d14c5d2SYehuda Sadeh auth->key ^= *(__le64 *)u; 5283d14c5d2SYehuda Sadeh dout(" server_challenge %llx client_challenge %llx key %llx\n", 5293d14c5d2SYehuda Sadeh xi->server_challenge, le64_to_cpu(auth->client_challenge), 5303d14c5d2SYehuda Sadeh le64_to_cpu(auth->key)); 5313d14c5d2SYehuda Sadeh 5323d14c5d2SYehuda Sadeh /* now encode the old ticket if exists */ 5333d14c5d2SYehuda Sadeh ret = ceph_x_encode_ticket(th, &p, end); 5343d14c5d2SYehuda Sadeh if (ret < 0) 5353d14c5d2SYehuda Sadeh return ret; 5363d14c5d2SYehuda Sadeh 5373d14c5d2SYehuda Sadeh return p - buf; 5383d14c5d2SYehuda Sadeh } 5393d14c5d2SYehuda Sadeh 5403d14c5d2SYehuda Sadeh if (need) { 5413d14c5d2SYehuda Sadeh void *p = head + 1; 5423d14c5d2SYehuda Sadeh struct ceph_x_service_ticket_request *req; 5433d14c5d2SYehuda Sadeh 5443d14c5d2SYehuda Sadeh if (p > end) 5453d14c5d2SYehuda Sadeh return -ERANGE; 5463d14c5d2SYehuda Sadeh head->op = cpu_to_le16(CEPHX_GET_PRINCIPAL_SESSION_KEY); 5473d14c5d2SYehuda Sadeh 5483d14c5d2SYehuda Sadeh ret = ceph_x_build_authorizer(ac, th, &xi->auth_authorizer); 5493d14c5d2SYehuda Sadeh if (ret) 5503d14c5d2SYehuda Sadeh return ret; 5513d14c5d2SYehuda Sadeh ceph_encode_copy(&p, xi->auth_authorizer.buf->vec.iov_base, 5523d14c5d2SYehuda Sadeh xi->auth_authorizer.buf->vec.iov_len); 5533d14c5d2SYehuda Sadeh 5543d14c5d2SYehuda Sadeh req = p; 5553d14c5d2SYehuda Sadeh req->keys = cpu_to_le32(need); 5563d14c5d2SYehuda Sadeh p += sizeof(*req); 5573d14c5d2SYehuda Sadeh return p - buf; 5583d14c5d2SYehuda Sadeh } 5593d14c5d2SYehuda Sadeh 5603d14c5d2SYehuda Sadeh return 0; 5613d14c5d2SYehuda Sadeh } 5623d14c5d2SYehuda Sadeh 5633d14c5d2SYehuda Sadeh static int ceph_x_handle_reply(struct ceph_auth_client *ac, int result, 5643d14c5d2SYehuda Sadeh void *buf, void *end) 5653d14c5d2SYehuda Sadeh { 5663d14c5d2SYehuda Sadeh struct ceph_x_info *xi = ac->private; 5673d14c5d2SYehuda Sadeh struct ceph_x_reply_header *head = buf; 5683d14c5d2SYehuda Sadeh struct ceph_x_ticket_handler *th; 5693d14c5d2SYehuda Sadeh int len = end - buf; 5703d14c5d2SYehuda Sadeh int op; 5713d14c5d2SYehuda Sadeh int ret; 5723d14c5d2SYehuda Sadeh 5733d14c5d2SYehuda Sadeh if (result) 5743d14c5d2SYehuda Sadeh return result; /* XXX hmm? */ 5753d14c5d2SYehuda Sadeh 5763d14c5d2SYehuda Sadeh if (xi->starting) { 5773d14c5d2SYehuda Sadeh /* it's a hello */ 5783d14c5d2SYehuda Sadeh struct ceph_x_server_challenge *sc = buf; 5793d14c5d2SYehuda Sadeh 5803d14c5d2SYehuda Sadeh if (len != sizeof(*sc)) 5813d14c5d2SYehuda Sadeh return -EINVAL; 5823d14c5d2SYehuda Sadeh xi->server_challenge = le64_to_cpu(sc->server_challenge); 5833d14c5d2SYehuda Sadeh dout("handle_reply got server challenge %llx\n", 5843d14c5d2SYehuda Sadeh xi->server_challenge); 5853d14c5d2SYehuda Sadeh xi->starting = false; 5863d14c5d2SYehuda Sadeh xi->have_keys &= ~CEPH_ENTITY_TYPE_AUTH; 5873d14c5d2SYehuda Sadeh return -EAGAIN; 5883d14c5d2SYehuda Sadeh } 5893d14c5d2SYehuda Sadeh 5903d14c5d2SYehuda Sadeh op = le16_to_cpu(head->op); 5913d14c5d2SYehuda Sadeh result = le32_to_cpu(head->result); 5923d14c5d2SYehuda Sadeh dout("handle_reply op %d result %d\n", op, result); 5933d14c5d2SYehuda Sadeh switch (op) { 5943d14c5d2SYehuda Sadeh case CEPHX_GET_AUTH_SESSION_KEY: 5953d14c5d2SYehuda Sadeh /* verify auth key */ 5963d14c5d2SYehuda Sadeh ret = ceph_x_proc_ticket_reply(ac, &xi->secret, 5973d14c5d2SYehuda Sadeh buf + sizeof(*head), end); 5983d14c5d2SYehuda Sadeh break; 5993d14c5d2SYehuda Sadeh 6003d14c5d2SYehuda Sadeh case CEPHX_GET_PRINCIPAL_SESSION_KEY: 6013d14c5d2SYehuda Sadeh th = get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH); 6023d14c5d2SYehuda Sadeh if (IS_ERR(th)) 6033d14c5d2SYehuda Sadeh return PTR_ERR(th); 6043d14c5d2SYehuda Sadeh ret = ceph_x_proc_ticket_reply(ac, &th->session_key, 6053d14c5d2SYehuda Sadeh buf + sizeof(*head), end); 6063d14c5d2SYehuda Sadeh break; 6073d14c5d2SYehuda Sadeh 6083d14c5d2SYehuda Sadeh default: 6093d14c5d2SYehuda Sadeh return -EINVAL; 6103d14c5d2SYehuda Sadeh } 6113d14c5d2SYehuda Sadeh if (ret) 6123d14c5d2SYehuda Sadeh return ret; 6133d14c5d2SYehuda Sadeh if (ac->want_keys == xi->have_keys) 6143d14c5d2SYehuda Sadeh return 0; 6153d14c5d2SYehuda Sadeh return -EAGAIN; 6163d14c5d2SYehuda Sadeh } 6173d14c5d2SYehuda Sadeh 6186c1ea260SIlya Dryomov static void ceph_x_destroy_authorizer(struct ceph_authorizer *a) 6196c1ea260SIlya Dryomov { 6206c1ea260SIlya Dryomov struct ceph_x_authorizer *au = (void *)a; 6216c1ea260SIlya Dryomov 6226c1ea260SIlya Dryomov ceph_x_authorizer_cleanup(au); 6236c1ea260SIlya Dryomov kfree(au); 6246c1ea260SIlya Dryomov } 6256c1ea260SIlya Dryomov 6263d14c5d2SYehuda Sadeh static int ceph_x_create_authorizer( 6273d14c5d2SYehuda Sadeh struct ceph_auth_client *ac, int peer_type, 62874f1869fSAlex Elder struct ceph_auth_handshake *auth) 6293d14c5d2SYehuda Sadeh { 6303d14c5d2SYehuda Sadeh struct ceph_x_authorizer *au; 6313d14c5d2SYehuda Sadeh struct ceph_x_ticket_handler *th; 6323d14c5d2SYehuda Sadeh int ret; 6333d14c5d2SYehuda Sadeh 6343d14c5d2SYehuda Sadeh th = get_ticket_handler(ac, peer_type); 6353d14c5d2SYehuda Sadeh if (IS_ERR(th)) 6363d14c5d2SYehuda Sadeh return PTR_ERR(th); 6373d14c5d2SYehuda Sadeh 6383d14c5d2SYehuda Sadeh au = kzalloc(sizeof(*au), GFP_NOFS); 6393d14c5d2SYehuda Sadeh if (!au) 6403d14c5d2SYehuda Sadeh return -ENOMEM; 6413d14c5d2SYehuda Sadeh 6426c1ea260SIlya Dryomov au->base.destroy = ceph_x_destroy_authorizer; 6436c1ea260SIlya Dryomov 6443d14c5d2SYehuda Sadeh ret = ceph_x_build_authorizer(ac, th, au); 6453d14c5d2SYehuda Sadeh if (ret) { 6463d14c5d2SYehuda Sadeh kfree(au); 6473d14c5d2SYehuda Sadeh return ret; 6483d14c5d2SYehuda Sadeh } 6493d14c5d2SYehuda Sadeh 65074f1869fSAlex Elder auth->authorizer = (struct ceph_authorizer *) au; 65174f1869fSAlex Elder auth->authorizer_buf = au->buf->vec.iov_base; 65274f1869fSAlex Elder auth->authorizer_buf_len = au->buf->vec.iov_len; 6537882a26dSIlya Dryomov auth->authorizer_reply_buf = au->enc_buf; 6547882a26dSIlya Dryomov auth->authorizer_reply_buf_len = CEPHX_AU_ENC_BUF_LEN; 65533d07337SYan, Zheng auth->sign_message = ac->ops->sign_message; 65633d07337SYan, Zheng auth->check_message_signature = ac->ops->check_message_signature; 65774f1869fSAlex Elder 6583d14c5d2SYehuda Sadeh return 0; 6593d14c5d2SYehuda Sadeh } 6603d14c5d2SYehuda Sadeh 6610bed9b5cSSage Weil static int ceph_x_update_authorizer( 6620bed9b5cSSage Weil struct ceph_auth_client *ac, int peer_type, 6630bed9b5cSSage Weil struct ceph_auth_handshake *auth) 6640bed9b5cSSage Weil { 6650bed9b5cSSage Weil struct ceph_x_authorizer *au; 6660bed9b5cSSage Weil struct ceph_x_ticket_handler *th; 6670bed9b5cSSage Weil 6680bed9b5cSSage Weil th = get_ticket_handler(ac, peer_type); 6690bed9b5cSSage Weil if (IS_ERR(th)) 6700bed9b5cSSage Weil return PTR_ERR(th); 6710bed9b5cSSage Weil 6720bed9b5cSSage Weil au = (struct ceph_x_authorizer *)auth->authorizer; 6730bed9b5cSSage Weil if (au->secret_id < th->secret_id) { 6740bed9b5cSSage Weil dout("ceph_x_update_authorizer service %u secret %llu < %llu\n", 6750bed9b5cSSage Weil au->service, au->secret_id, th->secret_id); 6760bed9b5cSSage Weil return ceph_x_build_authorizer(ac, th, au); 6770bed9b5cSSage Weil } 6780bed9b5cSSage Weil return 0; 6790bed9b5cSSage Weil } 6800bed9b5cSSage Weil 6816daca13dSIlya Dryomov static int decrypt_authorize_challenge(struct ceph_x_authorizer *au, 6826daca13dSIlya Dryomov void *challenge_buf, 6836daca13dSIlya Dryomov int challenge_buf_len, 6846daca13dSIlya Dryomov u64 *server_challenge) 6856daca13dSIlya Dryomov { 6866daca13dSIlya Dryomov struct ceph_x_authorize_challenge *ch = 6876daca13dSIlya Dryomov challenge_buf + sizeof(struct ceph_x_encrypt_header); 6886daca13dSIlya Dryomov int ret; 6896daca13dSIlya Dryomov 6906daca13dSIlya Dryomov /* no leading len */ 6916daca13dSIlya Dryomov ret = __ceph_x_decrypt(&au->session_key, challenge_buf, 6926daca13dSIlya Dryomov challenge_buf_len); 6936daca13dSIlya Dryomov if (ret < 0) 6946daca13dSIlya Dryomov return ret; 6956daca13dSIlya Dryomov if (ret < sizeof(*ch)) { 6966daca13dSIlya Dryomov pr_err("bad size %d for ceph_x_authorize_challenge\n", ret); 6976daca13dSIlya Dryomov return -EINVAL; 6986daca13dSIlya Dryomov } 6996daca13dSIlya Dryomov 7006daca13dSIlya Dryomov *server_challenge = le64_to_cpu(ch->server_challenge); 7016daca13dSIlya Dryomov return 0; 7026daca13dSIlya Dryomov } 7036daca13dSIlya Dryomov 7046daca13dSIlya Dryomov static int ceph_x_add_authorizer_challenge(struct ceph_auth_client *ac, 7056daca13dSIlya Dryomov struct ceph_authorizer *a, 7066daca13dSIlya Dryomov void *challenge_buf, 7076daca13dSIlya Dryomov int challenge_buf_len) 7086daca13dSIlya Dryomov { 7096daca13dSIlya Dryomov struct ceph_x_authorizer *au = (void *)a; 7106daca13dSIlya Dryomov u64 server_challenge; 7116daca13dSIlya Dryomov int ret; 7126daca13dSIlya Dryomov 7136daca13dSIlya Dryomov ret = decrypt_authorize_challenge(au, challenge_buf, challenge_buf_len, 7146daca13dSIlya Dryomov &server_challenge); 7156daca13dSIlya Dryomov if (ret) { 7166daca13dSIlya Dryomov pr_err("failed to decrypt authorize challenge: %d", ret); 7176daca13dSIlya Dryomov return ret; 7186daca13dSIlya Dryomov } 7196daca13dSIlya Dryomov 7206daca13dSIlya Dryomov ret = encrypt_authorizer(au, &server_challenge); 7216daca13dSIlya Dryomov if (ret) { 7226daca13dSIlya Dryomov pr_err("failed to encrypt authorizer w/ challenge: %d", ret); 7236daca13dSIlya Dryomov return ret; 7246daca13dSIlya Dryomov } 7256daca13dSIlya Dryomov 7266daca13dSIlya Dryomov return 0; 7276daca13dSIlya Dryomov } 7286daca13dSIlya Dryomov 7293d14c5d2SYehuda Sadeh static int ceph_x_verify_authorizer_reply(struct ceph_auth_client *ac, 7300dde5848SIlya Dryomov struct ceph_authorizer *a) 7313d14c5d2SYehuda Sadeh { 7323d14c5d2SYehuda Sadeh struct ceph_x_authorizer *au = (void *)a; 7337882a26dSIlya Dryomov void *p = au->enc_buf; 734e15fd0a1SIlya Dryomov struct ceph_x_authorize_reply *reply = p + ceph_x_encrypt_offset(); 735e15fd0a1SIlya Dryomov int ret; 7363d14c5d2SYehuda Sadeh 737e15fd0a1SIlya Dryomov ret = ceph_x_decrypt(&au->session_key, &p, p + CEPHX_AU_ENC_BUF_LEN); 7383d14c5d2SYehuda Sadeh if (ret < 0) 7393d14c5d2SYehuda Sadeh return ret; 740e15fd0a1SIlya Dryomov if (ret != sizeof(*reply)) 7413d14c5d2SYehuda Sadeh return -EPERM; 7423d14c5d2SYehuda Sadeh 743e15fd0a1SIlya Dryomov if (au->nonce + 1 != le64_to_cpu(reply->nonce_plus_one)) 7443d14c5d2SYehuda Sadeh ret = -EPERM; 7453d14c5d2SYehuda Sadeh else 7463d14c5d2SYehuda Sadeh ret = 0; 7473d14c5d2SYehuda Sadeh dout("verify_authorizer_reply nonce %llx got %llx ret %d\n", 748e15fd0a1SIlya Dryomov au->nonce, le64_to_cpu(reply->nonce_plus_one), ret); 7493d14c5d2SYehuda Sadeh return ret; 7503d14c5d2SYehuda Sadeh } 7513d14c5d2SYehuda Sadeh 7523d14c5d2SYehuda Sadeh static void ceph_x_reset(struct ceph_auth_client *ac) 7533d14c5d2SYehuda Sadeh { 7543d14c5d2SYehuda Sadeh struct ceph_x_info *xi = ac->private; 7553d14c5d2SYehuda Sadeh 7563d14c5d2SYehuda Sadeh dout("reset\n"); 7573d14c5d2SYehuda Sadeh xi->starting = true; 7583d14c5d2SYehuda Sadeh xi->server_challenge = 0; 7593d14c5d2SYehuda Sadeh } 7603d14c5d2SYehuda Sadeh 7613d14c5d2SYehuda Sadeh static void ceph_x_destroy(struct ceph_auth_client *ac) 7623d14c5d2SYehuda Sadeh { 7633d14c5d2SYehuda Sadeh struct ceph_x_info *xi = ac->private; 7643d14c5d2SYehuda Sadeh struct rb_node *p; 7653d14c5d2SYehuda Sadeh 7663d14c5d2SYehuda Sadeh dout("ceph_x_destroy %p\n", ac); 7673d14c5d2SYehuda Sadeh ceph_crypto_key_destroy(&xi->secret); 7683d14c5d2SYehuda Sadeh 7693d14c5d2SYehuda Sadeh while ((p = rb_first(&xi->ticket_handlers)) != NULL) { 7703d14c5d2SYehuda Sadeh struct ceph_x_ticket_handler *th = 7713d14c5d2SYehuda Sadeh rb_entry(p, struct ceph_x_ticket_handler, node); 7723d14c5d2SYehuda Sadeh remove_ticket_handler(ac, th); 7733d14c5d2SYehuda Sadeh } 7743d14c5d2SYehuda Sadeh 775cbf99a11SIlya Dryomov ceph_x_authorizer_cleanup(&xi->auth_authorizer); 7763d14c5d2SYehuda Sadeh 7773d14c5d2SYehuda Sadeh kfree(ac->private); 7783d14c5d2SYehuda Sadeh ac->private = NULL; 7793d14c5d2SYehuda Sadeh } 7803d14c5d2SYehuda Sadeh 781187d131dSIlya Dryomov static void invalidate_ticket(struct ceph_auth_client *ac, int peer_type) 7823d14c5d2SYehuda Sadeh { 7833d14c5d2SYehuda Sadeh struct ceph_x_ticket_handler *th; 7843d14c5d2SYehuda Sadeh 7853d14c5d2SYehuda Sadeh th = get_ticket_handler(ac, peer_type); 7863d14c5d2SYehuda Sadeh if (!IS_ERR(th)) 7876abe097dSIlya Dryomov th->have_key = false; 7883d14c5d2SYehuda Sadeh } 7893d14c5d2SYehuda Sadeh 790187d131dSIlya Dryomov static void ceph_x_invalidate_authorizer(struct ceph_auth_client *ac, 791187d131dSIlya Dryomov int peer_type) 792187d131dSIlya Dryomov { 793187d131dSIlya Dryomov /* 794187d131dSIlya Dryomov * We are to invalidate a service ticket in the hopes of 795187d131dSIlya Dryomov * getting a new, hopefully more valid, one. But, we won't get 796187d131dSIlya Dryomov * it unless our AUTH ticket is good, so invalidate AUTH ticket 797187d131dSIlya Dryomov * as well, just in case. 798187d131dSIlya Dryomov */ 799187d131dSIlya Dryomov invalidate_ticket(ac, peer_type); 800187d131dSIlya Dryomov invalidate_ticket(ac, CEPH_ENTITY_TYPE_AUTH); 801187d131dSIlya Dryomov } 802187d131dSIlya Dryomov 8034eb4517cSIlya Dryomov static int calc_signature(struct ceph_x_authorizer *au, struct ceph_msg *msg, 8044eb4517cSIlya Dryomov __le64 *psig) 80533d07337SYan, Zheng { 806d03857c6SIlya Dryomov void *enc_buf = au->enc_buf; 807cc255c76SIlya Dryomov int ret; 808cc255c76SIlya Dryomov 809cc255c76SIlya Dryomov if (!CEPH_HAVE_FEATURE(msg->con->peer_features, CEPHX_V2)) { 8104eb4517cSIlya Dryomov struct { 8114eb4517cSIlya Dryomov __le32 len; 8124eb4517cSIlya Dryomov __le32 header_crc; 8134eb4517cSIlya Dryomov __le32 front_crc; 8144eb4517cSIlya Dryomov __le32 middle_crc; 8154eb4517cSIlya Dryomov __le32 data_crc; 816d03857c6SIlya Dryomov } __packed *sigblock = enc_buf + ceph_x_encrypt_offset(); 8174eb4517cSIlya Dryomov 818d03857c6SIlya Dryomov sigblock->len = cpu_to_le32(4*sizeof(u32)); 819d03857c6SIlya Dryomov sigblock->header_crc = msg->hdr.crc; 820d03857c6SIlya Dryomov sigblock->front_crc = msg->footer.front_crc; 821d03857c6SIlya Dryomov sigblock->middle_crc = msg->footer.middle_crc; 822d03857c6SIlya Dryomov sigblock->data_crc = msg->footer.data_crc; 823cc255c76SIlya Dryomov 824cc255c76SIlya Dryomov ret = ceph_x_encrypt(&au->session_key, enc_buf, 825cc255c76SIlya Dryomov CEPHX_AU_ENC_BUF_LEN, sizeof(*sigblock)); 82633d07337SYan, Zheng if (ret < 0) 82733d07337SYan, Zheng return ret; 8284eb4517cSIlya Dryomov 829d03857c6SIlya Dryomov *psig = *(__le64 *)(enc_buf + sizeof(u32)); 830cc255c76SIlya Dryomov } else { 831cc255c76SIlya Dryomov struct { 832cc255c76SIlya Dryomov __le32 header_crc; 833cc255c76SIlya Dryomov __le32 front_crc; 834cc255c76SIlya Dryomov __le32 front_len; 835cc255c76SIlya Dryomov __le32 middle_crc; 836cc255c76SIlya Dryomov __le32 middle_len; 837cc255c76SIlya Dryomov __le32 data_crc; 838cc255c76SIlya Dryomov __le32 data_len; 839cc255c76SIlya Dryomov __le32 seq_lower_word; 840cc255c76SIlya Dryomov } __packed *sigblock = enc_buf; 841cc255c76SIlya Dryomov struct { 842cc255c76SIlya Dryomov __le64 a, b, c, d; 843cc255c76SIlya Dryomov } __packed *penc = enc_buf; 844cc255c76SIlya Dryomov int ciphertext_len; 845cc255c76SIlya Dryomov 846cc255c76SIlya Dryomov sigblock->header_crc = msg->hdr.crc; 847cc255c76SIlya Dryomov sigblock->front_crc = msg->footer.front_crc; 848cc255c76SIlya Dryomov sigblock->front_len = msg->hdr.front_len; 849cc255c76SIlya Dryomov sigblock->middle_crc = msg->footer.middle_crc; 850cc255c76SIlya Dryomov sigblock->middle_len = msg->hdr.middle_len; 851cc255c76SIlya Dryomov sigblock->data_crc = msg->footer.data_crc; 852cc255c76SIlya Dryomov sigblock->data_len = msg->hdr.data_len; 853cc255c76SIlya Dryomov sigblock->seq_lower_word = *(__le32 *)&msg->hdr.seq; 854cc255c76SIlya Dryomov 855cc255c76SIlya Dryomov /* no leading len, no ceph_x_encrypt_header */ 856cc255c76SIlya Dryomov ret = ceph_crypt(&au->session_key, true, enc_buf, 857cc255c76SIlya Dryomov CEPHX_AU_ENC_BUF_LEN, sizeof(*sigblock), 858cc255c76SIlya Dryomov &ciphertext_len); 859cc255c76SIlya Dryomov if (ret) 860cc255c76SIlya Dryomov return ret; 861cc255c76SIlya Dryomov 862cc255c76SIlya Dryomov *psig = penc->a ^ penc->b ^ penc->c ^ penc->d; 863cc255c76SIlya Dryomov } 864cc255c76SIlya Dryomov 86533d07337SYan, Zheng return 0; 86633d07337SYan, Zheng } 86733d07337SYan, Zheng 86833d07337SYan, Zheng static int ceph_x_sign_message(struct ceph_auth_handshake *auth, 86933d07337SYan, Zheng struct ceph_msg *msg) 87033d07337SYan, Zheng { 8714eb4517cSIlya Dryomov __le64 sig; 87233d07337SYan, Zheng int ret; 8734199b8eeSIlya Dryomov 874a51983e4SIlya Dryomov if (ceph_test_opt(from_msgr(msg->con->msgr), NOMSGSIGN)) 875a51983e4SIlya Dryomov return 0; 876a51983e4SIlya Dryomov 8774eb4517cSIlya Dryomov ret = calc_signature((struct ceph_x_authorizer *)auth->authorizer, 8784eb4517cSIlya Dryomov msg, &sig); 8794eb4517cSIlya Dryomov if (ret) 88033d07337SYan, Zheng return ret; 8814eb4517cSIlya Dryomov 8824eb4517cSIlya Dryomov msg->footer.sig = sig; 88333d07337SYan, Zheng msg->footer.flags |= CEPH_MSG_FOOTER_SIGNED; 88433d07337SYan, Zheng return 0; 88533d07337SYan, Zheng } 88633d07337SYan, Zheng 88733d07337SYan, Zheng static int ceph_x_check_message_signature(struct ceph_auth_handshake *auth, 88833d07337SYan, Zheng struct ceph_msg *msg) 88933d07337SYan, Zheng { 89033d07337SYan, Zheng __le64 sig_check; 89133d07337SYan, Zheng int ret; 89233d07337SYan, Zheng 893a51983e4SIlya Dryomov if (ceph_test_opt(from_msgr(msg->con->msgr), NOMSGSIGN)) 894a51983e4SIlya Dryomov return 0; 895a51983e4SIlya Dryomov 8964eb4517cSIlya Dryomov ret = calc_signature((struct ceph_x_authorizer *)auth->authorizer, 89733d07337SYan, Zheng msg, &sig_check); 8984eb4517cSIlya Dryomov if (ret) 89933d07337SYan, Zheng return ret; 90033d07337SYan, Zheng if (sig_check == msg->footer.sig) 90133d07337SYan, Zheng return 0; 90233d07337SYan, Zheng if (msg->footer.flags & CEPH_MSG_FOOTER_SIGNED) 90333d07337SYan, Zheng dout("ceph_x_check_message_signature %p has signature %llx " 90433d07337SYan, Zheng "expect %llx\n", msg, msg->footer.sig, sig_check); 90533d07337SYan, Zheng else 90633d07337SYan, Zheng dout("ceph_x_check_message_signature %p sender did not set " 90733d07337SYan, Zheng "CEPH_MSG_FOOTER_SIGNED\n", msg); 90833d07337SYan, Zheng return -EBADMSG; 90933d07337SYan, Zheng } 9103d14c5d2SYehuda Sadeh 9113d14c5d2SYehuda Sadeh static const struct ceph_auth_client_ops ceph_x_ops = { 9123d14c5d2SYehuda Sadeh .name = "x", 9133d14c5d2SYehuda Sadeh .is_authenticated = ceph_x_is_authenticated, 9143d14c5d2SYehuda Sadeh .should_authenticate = ceph_x_should_authenticate, 9153d14c5d2SYehuda Sadeh .build_request = ceph_x_build_request, 9163d14c5d2SYehuda Sadeh .handle_reply = ceph_x_handle_reply, 9173d14c5d2SYehuda Sadeh .create_authorizer = ceph_x_create_authorizer, 9180bed9b5cSSage Weil .update_authorizer = ceph_x_update_authorizer, 9196daca13dSIlya Dryomov .add_authorizer_challenge = ceph_x_add_authorizer_challenge, 9203d14c5d2SYehuda Sadeh .verify_authorizer_reply = ceph_x_verify_authorizer_reply, 9213d14c5d2SYehuda Sadeh .invalidate_authorizer = ceph_x_invalidate_authorizer, 9223d14c5d2SYehuda Sadeh .reset = ceph_x_reset, 9233d14c5d2SYehuda Sadeh .destroy = ceph_x_destroy, 92433d07337SYan, Zheng .sign_message = ceph_x_sign_message, 92533d07337SYan, Zheng .check_message_signature = ceph_x_check_message_signature, 9263d14c5d2SYehuda Sadeh }; 9273d14c5d2SYehuda Sadeh 9283d14c5d2SYehuda Sadeh 9293d14c5d2SYehuda Sadeh int ceph_x_init(struct ceph_auth_client *ac) 9303d14c5d2SYehuda Sadeh { 9313d14c5d2SYehuda Sadeh struct ceph_x_info *xi; 9323d14c5d2SYehuda Sadeh int ret; 9333d14c5d2SYehuda Sadeh 9343d14c5d2SYehuda Sadeh dout("ceph_x_init %p\n", ac); 9353d14c5d2SYehuda Sadeh ret = -ENOMEM; 9363d14c5d2SYehuda Sadeh xi = kzalloc(sizeof(*xi), GFP_NOFS); 9373d14c5d2SYehuda Sadeh if (!xi) 9383d14c5d2SYehuda Sadeh goto out; 9393d14c5d2SYehuda Sadeh 9403d14c5d2SYehuda Sadeh ret = -EINVAL; 9418323c3aaSTommi Virtanen if (!ac->key) { 9423d14c5d2SYehuda Sadeh pr_err("no secret set (for auth_x protocol)\n"); 9433d14c5d2SYehuda Sadeh goto out_nomem; 9443d14c5d2SYehuda Sadeh } 9453d14c5d2SYehuda Sadeh 9468323c3aaSTommi Virtanen ret = ceph_crypto_key_clone(&xi->secret, ac->key); 9478323c3aaSTommi Virtanen if (ret < 0) { 9488323c3aaSTommi Virtanen pr_err("cannot clone key: %d\n", ret); 9493d14c5d2SYehuda Sadeh goto out_nomem; 9508323c3aaSTommi Virtanen } 9513d14c5d2SYehuda Sadeh 9523d14c5d2SYehuda Sadeh xi->starting = true; 9533d14c5d2SYehuda Sadeh xi->ticket_handlers = RB_ROOT; 9543d14c5d2SYehuda Sadeh 9553d14c5d2SYehuda Sadeh ac->protocol = CEPH_AUTH_CEPHX; 9563d14c5d2SYehuda Sadeh ac->private = xi; 9573d14c5d2SYehuda Sadeh ac->ops = &ceph_x_ops; 9583d14c5d2SYehuda Sadeh return 0; 9593d14c5d2SYehuda Sadeh 9603d14c5d2SYehuda Sadeh out_nomem: 9613d14c5d2SYehuda Sadeh kfree(xi); 9623d14c5d2SYehuda Sadeh out: 9633d14c5d2SYehuda Sadeh return ret; 9643d14c5d2SYehuda Sadeh } 965