Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 1 | |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 2 | #include <linux/ceph/ceph_debug.h> |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 3 | |
| 4 | #include <linux/err.h> |
| 5 | #include <linux/module.h> |
| 6 | #include <linux/random.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 7 | #include <linux/slab.h> |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 8 | |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 9 | #include <linux/ceph/decode.h> |
| 10 | #include <linux/ceph/auth.h> |
| 11 | |
| 12 | #include "crypto.h" |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 13 | #include "auth_x.h" |
| 14 | #include "auth_x_protocol.h" |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 15 | |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 16 | static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed); |
| 17 | |
| 18 | static int ceph_x_is_authenticated(struct ceph_auth_client *ac) |
| 19 | { |
| 20 | struct ceph_x_info *xi = ac->private; |
| 21 | int need; |
| 22 | |
| 23 | ceph_x_validate_tickets(ac, &need); |
| 24 | dout("ceph_x_is_authenticated want=%d need=%d have=%d\n", |
| 25 | ac->want_keys, need, xi->have_keys); |
| 26 | return (ac->want_keys & xi->have_keys) == ac->want_keys; |
| 27 | } |
| 28 | |
Sage Weil | a41359f | 2010-05-25 15:39:06 -0700 | [diff] [blame] | 29 | static int ceph_x_should_authenticate(struct ceph_auth_client *ac) |
| 30 | { |
| 31 | struct ceph_x_info *xi = ac->private; |
| 32 | int need; |
| 33 | |
| 34 | ceph_x_validate_tickets(ac, &need); |
| 35 | dout("ceph_x_should_authenticate want=%d need=%d have=%d\n", |
| 36 | ac->want_keys, need, xi->have_keys); |
| 37 | return need != 0; |
| 38 | } |
| 39 | |
Sage Weil | 807c86e | 2010-03-15 15:52:17 -0700 | [diff] [blame] | 40 | static int ceph_x_encrypt_buflen(int ilen) |
| 41 | { |
| 42 | return sizeof(struct ceph_x_encrypt_header) + ilen + 16 + |
| 43 | sizeof(u32); |
| 44 | } |
| 45 | |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 46 | static int ceph_x_encrypt(struct ceph_crypto_key *secret, |
| 47 | void *ibuf, int ilen, void *obuf, size_t olen) |
| 48 | { |
| 49 | struct ceph_x_encrypt_header head = { |
| 50 | .struct_v = 1, |
| 51 | .magic = cpu_to_le64(CEPHX_ENC_MAGIC) |
| 52 | }; |
| 53 | size_t len = olen - sizeof(u32); |
| 54 | int ret; |
| 55 | |
| 56 | ret = ceph_encrypt2(secret, obuf + sizeof(u32), &len, |
| 57 | &head, sizeof(head), ibuf, ilen); |
| 58 | if (ret) |
| 59 | return ret; |
| 60 | ceph_encode_32(&obuf, len); |
| 61 | return len + sizeof(u32); |
| 62 | } |
| 63 | |
| 64 | static int ceph_x_decrypt(struct ceph_crypto_key *secret, |
Ilya Dryomov | c27a3e4 | 2014-09-09 19:39:15 +0400 | [diff] [blame] | 65 | void **p, void *end, void **obuf, size_t olen) |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 66 | { |
| 67 | struct ceph_x_encrypt_header head; |
| 68 | size_t head_len = sizeof(head); |
| 69 | int len, ret; |
| 70 | |
| 71 | len = ceph_decode_32(p); |
| 72 | if (*p + len > end) |
| 73 | return -EINVAL; |
| 74 | |
| 75 | dout("ceph_x_decrypt len %d\n", len); |
Ilya Dryomov | c27a3e4 | 2014-09-09 19:39:15 +0400 | [diff] [blame] | 76 | if (*obuf == NULL) { |
| 77 | *obuf = kmalloc(len, GFP_NOFS); |
| 78 | if (!*obuf) |
| 79 | return -ENOMEM; |
| 80 | olen = len; |
| 81 | } |
| 82 | |
| 83 | ret = ceph_decrypt2(secret, &head, &head_len, *obuf, &olen, *p, len); |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 84 | if (ret) |
| 85 | return ret; |
| 86 | if (head.struct_v != 1 || le64_to_cpu(head.magic) != CEPHX_ENC_MAGIC) |
| 87 | return -EPERM; |
| 88 | *p += len; |
| 89 | return olen; |
| 90 | } |
| 91 | |
| 92 | /* |
| 93 | * get existing (or insert new) ticket handler |
| 94 | */ |
Yehuda Sadeh | cd84db6 | 2010-06-11 16:58:48 -0700 | [diff] [blame] | 95 | static struct ceph_x_ticket_handler * |
| 96 | get_ticket_handler(struct ceph_auth_client *ac, int service) |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 97 | { |
| 98 | struct ceph_x_ticket_handler *th; |
| 99 | struct ceph_x_info *xi = ac->private; |
| 100 | struct rb_node *parent = NULL, **p = &xi->ticket_handlers.rb_node; |
| 101 | |
| 102 | while (*p) { |
| 103 | parent = *p; |
| 104 | th = rb_entry(parent, struct ceph_x_ticket_handler, node); |
| 105 | if (service < th->service) |
| 106 | p = &(*p)->rb_left; |
| 107 | else if (service > th->service) |
| 108 | p = &(*p)->rb_right; |
| 109 | else |
| 110 | return th; |
| 111 | } |
| 112 | |
| 113 | /* add it */ |
| 114 | th = kzalloc(sizeof(*th), GFP_NOFS); |
| 115 | if (!th) |
| 116 | return ERR_PTR(-ENOMEM); |
| 117 | th->service = service; |
| 118 | rb_link_node(&th->node, parent, p); |
| 119 | rb_insert_color(&th->node, &xi->ticket_handlers); |
| 120 | return th; |
| 121 | } |
| 122 | |
| 123 | static void remove_ticket_handler(struct ceph_auth_client *ac, |
| 124 | struct ceph_x_ticket_handler *th) |
| 125 | { |
| 126 | struct ceph_x_info *xi = ac->private; |
| 127 | |
| 128 | dout("remove_ticket_handler %p %d\n", th, th->service); |
| 129 | rb_erase(&th->node, &xi->ticket_handlers); |
| 130 | ceph_crypto_key_destroy(&th->session_key); |
| 131 | if (th->ticket_blob) |
| 132 | ceph_buffer_put(th->ticket_blob); |
| 133 | kfree(th); |
| 134 | } |
| 135 | |
Ilya Dryomov | 597cda3 | 2014-09-08 17:25:34 +0400 | [diff] [blame] | 136 | static int process_one_ticket(struct ceph_auth_client *ac, |
| 137 | struct ceph_crypto_key *secret, |
Ilya Dryomov | c27a3e4 | 2014-09-09 19:39:15 +0400 | [diff] [blame] | 138 | void **p, void *end) |
Ilya Dryomov | 597cda3 | 2014-09-08 17:25:34 +0400 | [diff] [blame] | 139 | { |
| 140 | struct ceph_x_info *xi = ac->private; |
| 141 | int type; |
| 142 | u8 tkt_struct_v, blob_struct_v; |
| 143 | struct ceph_x_ticket_handler *th; |
Ilya Dryomov | c27a3e4 | 2014-09-09 19:39:15 +0400 | [diff] [blame] | 144 | void *dbuf = NULL; |
Ilya Dryomov | 597cda3 | 2014-09-08 17:25:34 +0400 | [diff] [blame] | 145 | void *dp, *dend; |
| 146 | int dlen; |
| 147 | char is_enc; |
| 148 | struct timespec validity; |
| 149 | struct ceph_crypto_key old_key; |
Ilya Dryomov | c27a3e4 | 2014-09-09 19:39:15 +0400 | [diff] [blame] | 150 | void *ticket_buf = NULL; |
Ilya Dryomov | 597cda3 | 2014-09-08 17:25:34 +0400 | [diff] [blame] | 151 | void *tp, *tpend; |
Ilya Dryomov | e9226d7 | 2014-10-22 18:15:37 +0400 | [diff] [blame] | 152 | void **ptp; |
Ilya Dryomov | 597cda3 | 2014-09-08 17:25:34 +0400 | [diff] [blame] | 153 | struct ceph_timespec new_validity; |
| 154 | struct ceph_crypto_key new_session_key; |
| 155 | struct ceph_buffer *new_ticket_blob; |
| 156 | unsigned long new_expires, new_renew_after; |
| 157 | u64 new_secret_id; |
| 158 | int ret; |
| 159 | |
| 160 | ceph_decode_need(p, end, sizeof(u32) + 1, bad); |
| 161 | |
| 162 | type = ceph_decode_32(p); |
| 163 | dout(" ticket type %d %s\n", type, ceph_entity_type_name(type)); |
| 164 | |
| 165 | tkt_struct_v = ceph_decode_8(p); |
| 166 | if (tkt_struct_v != 1) |
| 167 | goto bad; |
| 168 | |
| 169 | th = get_ticket_handler(ac, type); |
| 170 | if (IS_ERR(th)) { |
| 171 | ret = PTR_ERR(th); |
| 172 | goto out; |
| 173 | } |
| 174 | |
| 175 | /* blob for me */ |
Ilya Dryomov | c27a3e4 | 2014-09-09 19:39:15 +0400 | [diff] [blame] | 176 | dlen = ceph_x_decrypt(secret, p, end, &dbuf, 0); |
Ilya Dryomov | 597cda3 | 2014-09-08 17:25:34 +0400 | [diff] [blame] | 177 | if (dlen <= 0) { |
| 178 | ret = dlen; |
| 179 | goto out; |
| 180 | } |
| 181 | dout(" decrypted %d bytes\n", dlen); |
| 182 | dp = dbuf; |
| 183 | dend = dp + dlen; |
| 184 | |
| 185 | tkt_struct_v = ceph_decode_8(&dp); |
| 186 | if (tkt_struct_v != 1) |
| 187 | goto bad; |
| 188 | |
| 189 | memcpy(&old_key, &th->session_key, sizeof(old_key)); |
| 190 | ret = ceph_crypto_key_decode(&new_session_key, &dp, dend); |
| 191 | if (ret) |
| 192 | goto out; |
| 193 | |
| 194 | ceph_decode_copy(&dp, &new_validity, sizeof(new_validity)); |
| 195 | ceph_decode_timespec(&validity, &new_validity); |
| 196 | new_expires = get_seconds() + validity.tv_sec; |
| 197 | new_renew_after = new_expires - (validity.tv_sec / 4); |
| 198 | dout(" expires=%lu renew_after=%lu\n", new_expires, |
| 199 | new_renew_after); |
| 200 | |
| 201 | /* ticket blob for service */ |
| 202 | ceph_decode_8_safe(p, end, is_enc, bad); |
Ilya Dryomov | 597cda3 | 2014-09-08 17:25:34 +0400 | [diff] [blame] | 203 | if (is_enc) { |
| 204 | /* encrypted */ |
| 205 | dout(" encrypted ticket\n"); |
Ilya Dryomov | c27a3e4 | 2014-09-09 19:39:15 +0400 | [diff] [blame] | 206 | dlen = ceph_x_decrypt(&old_key, p, end, &ticket_buf, 0); |
Ilya Dryomov | 597cda3 | 2014-09-08 17:25:34 +0400 | [diff] [blame] | 207 | if (dlen < 0) { |
| 208 | ret = dlen; |
| 209 | goto out; |
| 210 | } |
Ilya Dryomov | c27a3e4 | 2014-09-09 19:39:15 +0400 | [diff] [blame] | 211 | tp = ticket_buf; |
Ilya Dryomov | e9226d7 | 2014-10-22 18:15:37 +0400 | [diff] [blame] | 212 | ptp = &tp; |
| 213 | tpend = *ptp + dlen; |
Ilya Dryomov | 597cda3 | 2014-09-08 17:25:34 +0400 | [diff] [blame] | 214 | } else { |
| 215 | /* unencrypted */ |
Ilya Dryomov | e9226d7 | 2014-10-22 18:15:37 +0400 | [diff] [blame] | 216 | ptp = p; |
| 217 | tpend = end; |
Ilya Dryomov | 597cda3 | 2014-09-08 17:25:34 +0400 | [diff] [blame] | 218 | } |
Ilya Dryomov | e9226d7 | 2014-10-22 18:15:37 +0400 | [diff] [blame] | 219 | ceph_decode_32_safe(ptp, tpend, dlen, bad); |
Ilya Dryomov | 597cda3 | 2014-09-08 17:25:34 +0400 | [diff] [blame] | 220 | dout(" ticket blob is %d bytes\n", dlen); |
Ilya Dryomov | e9226d7 | 2014-10-22 18:15:37 +0400 | [diff] [blame] | 221 | ceph_decode_need(ptp, tpend, 1 + sizeof(u64), bad); |
| 222 | blob_struct_v = ceph_decode_8(ptp); |
| 223 | new_secret_id = ceph_decode_64(ptp); |
| 224 | ret = ceph_decode_buffer(&new_ticket_blob, ptp, tpend); |
Ilya Dryomov | 597cda3 | 2014-09-08 17:25:34 +0400 | [diff] [blame] | 225 | if (ret) |
| 226 | goto out; |
| 227 | |
| 228 | /* all is well, update our ticket */ |
| 229 | ceph_crypto_key_destroy(&th->session_key); |
| 230 | if (th->ticket_blob) |
| 231 | ceph_buffer_put(th->ticket_blob); |
| 232 | th->session_key = new_session_key; |
| 233 | th->ticket_blob = new_ticket_blob; |
| 234 | th->validity = new_validity; |
| 235 | th->secret_id = new_secret_id; |
| 236 | th->expires = new_expires; |
| 237 | th->renew_after = new_renew_after; |
| 238 | dout(" got ticket service %d (%s) secret_id %lld len %d\n", |
| 239 | type, ceph_entity_type_name(type), th->secret_id, |
| 240 | (int)th->ticket_blob->vec.iov_len); |
| 241 | xi->have_keys |= th->service; |
| 242 | |
| 243 | out: |
Ilya Dryomov | c27a3e4 | 2014-09-09 19:39:15 +0400 | [diff] [blame] | 244 | kfree(ticket_buf); |
| 245 | kfree(dbuf); |
Ilya Dryomov | 597cda3 | 2014-09-08 17:25:34 +0400 | [diff] [blame] | 246 | return ret; |
| 247 | |
| 248 | bad: |
| 249 | ret = -EINVAL; |
| 250 | goto out; |
| 251 | } |
| 252 | |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 253 | static int ceph_x_proc_ticket_reply(struct ceph_auth_client *ac, |
| 254 | struct ceph_crypto_key *secret, |
| 255 | void *buf, void *end) |
| 256 | { |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 257 | void *p = buf; |
Sage Weil | b736b3d | 2010-04-30 12:45:02 -0700 | [diff] [blame] | 258 | u8 reply_struct_v; |
Ilya Dryomov | 597cda3 | 2014-09-08 17:25:34 +0400 | [diff] [blame] | 259 | u32 num; |
| 260 | int ret; |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 261 | |
Ilya Dryomov | 597cda3 | 2014-09-08 17:25:34 +0400 | [diff] [blame] | 262 | ceph_decode_8_safe(&p, end, reply_struct_v, bad); |
Sage Weil | b736b3d | 2010-04-30 12:45:02 -0700 | [diff] [blame] | 263 | if (reply_struct_v != 1) |
Ilya Dryomov | 597cda3 | 2014-09-08 17:25:34 +0400 | [diff] [blame] | 264 | return -EINVAL; |
| 265 | |
| 266 | ceph_decode_32_safe(&p, end, num, bad); |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 267 | dout("%d tickets\n", num); |
Ilya Dryomov | 597cda3 | 2014-09-08 17:25:34 +0400 | [diff] [blame] | 268 | |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 269 | while (num--) { |
Ilya Dryomov | c27a3e4 | 2014-09-09 19:39:15 +0400 | [diff] [blame] | 270 | ret = process_one_ticket(ac, secret, &p, end); |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 271 | if (ret) |
Ilya Dryomov | c27a3e4 | 2014-09-09 19:39:15 +0400 | [diff] [blame] | 272 | return ret; |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 273 | } |
| 274 | |
Ilya Dryomov | c27a3e4 | 2014-09-09 19:39:15 +0400 | [diff] [blame] | 275 | return 0; |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 276 | |
| 277 | bad: |
Ilya Dryomov | c27a3e4 | 2014-09-09 19:39:15 +0400 | [diff] [blame] | 278 | return -EINVAL; |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | static int ceph_x_build_authorizer(struct ceph_auth_client *ac, |
| 282 | struct ceph_x_ticket_handler *th, |
| 283 | struct ceph_x_authorizer *au) |
| 284 | { |
Sage Weil | 807c86e | 2010-03-15 15:52:17 -0700 | [diff] [blame] | 285 | int maxlen; |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 286 | struct ceph_x_authorize_a *msg_a; |
| 287 | struct ceph_x_authorize_b msg_b; |
| 288 | void *p, *end; |
| 289 | int ret; |
| 290 | int ticket_blob_len = |
| 291 | (th->ticket_blob ? th->ticket_blob->vec.iov_len : 0); |
| 292 | |
| 293 | dout("build_authorizer for %s %p\n", |
| 294 | ceph_entity_type_name(th->service), au); |
| 295 | |
Yan, Zheng | ae385ea | 2014-11-04 16:32:35 +0800 | [diff] [blame^] | 296 | ceph_crypto_key_destroy(&au->session_key); |
| 297 | ret = ceph_crypto_key_clone(&au->session_key, &th->session_key); |
| 298 | if (ret) |
| 299 | return ret; |
| 300 | |
Sage Weil | 807c86e | 2010-03-15 15:52:17 -0700 | [diff] [blame] | 301 | maxlen = sizeof(*msg_a) + sizeof(msg_b) + |
| 302 | ceph_x_encrypt_buflen(ticket_blob_len); |
| 303 | dout(" need len %d\n", maxlen); |
| 304 | if (au->buf && au->buf->alloc_len < maxlen) { |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 305 | ceph_buffer_put(au->buf); |
| 306 | au->buf = NULL; |
| 307 | } |
| 308 | if (!au->buf) { |
Sage Weil | 807c86e | 2010-03-15 15:52:17 -0700 | [diff] [blame] | 309 | au->buf = ceph_buffer_new(maxlen, GFP_NOFS); |
Yan, Zheng | ae385ea | 2014-11-04 16:32:35 +0800 | [diff] [blame^] | 310 | if (!au->buf) { |
| 311 | ceph_crypto_key_destroy(&au->session_key); |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 312 | return -ENOMEM; |
Yan, Zheng | ae385ea | 2014-11-04 16:32:35 +0800 | [diff] [blame^] | 313 | } |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 314 | } |
| 315 | au->service = th->service; |
Sage Weil | 0bed9b5 | 2013-03-25 10:26:01 -0700 | [diff] [blame] | 316 | au->secret_id = th->secret_id; |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 317 | |
| 318 | msg_a = au->buf->vec.iov_base; |
| 319 | msg_a->struct_v = 1; |
| 320 | msg_a->global_id = cpu_to_le64(ac->global_id); |
| 321 | msg_a->service_id = cpu_to_le32(th->service); |
| 322 | msg_a->ticket_blob.struct_v = 1; |
| 323 | msg_a->ticket_blob.secret_id = cpu_to_le64(th->secret_id); |
| 324 | msg_a->ticket_blob.blob_len = cpu_to_le32(ticket_blob_len); |
| 325 | if (ticket_blob_len) { |
| 326 | memcpy(msg_a->ticket_blob.blob, th->ticket_blob->vec.iov_base, |
| 327 | th->ticket_blob->vec.iov_len); |
| 328 | } |
| 329 | dout(" th %p secret_id %lld %lld\n", th, th->secret_id, |
| 330 | le64_to_cpu(msg_a->ticket_blob.secret_id)); |
| 331 | |
| 332 | p = msg_a + 1; |
| 333 | p += ticket_blob_len; |
| 334 | end = au->buf->vec.iov_base + au->buf->vec.iov_len; |
| 335 | |
| 336 | get_random_bytes(&au->nonce, sizeof(au->nonce)); |
| 337 | msg_b.struct_v = 1; |
| 338 | msg_b.nonce = cpu_to_le64(au->nonce); |
Yan, Zheng | ae385ea | 2014-11-04 16:32:35 +0800 | [diff] [blame^] | 339 | ret = ceph_x_encrypt(&au->session_key, &msg_b, sizeof(msg_b), |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 340 | p, end - p); |
| 341 | if (ret < 0) |
| 342 | goto out_buf; |
| 343 | p += ret; |
| 344 | au->buf->vec.iov_len = p - au->buf->vec.iov_base; |
| 345 | dout(" built authorizer nonce %llx len %d\n", au->nonce, |
| 346 | (int)au->buf->vec.iov_len); |
Sage Weil | 807c86e | 2010-03-15 15:52:17 -0700 | [diff] [blame] | 347 | BUG_ON(au->buf->vec.iov_len > maxlen); |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 348 | return 0; |
| 349 | |
| 350 | out_buf: |
| 351 | ceph_buffer_put(au->buf); |
| 352 | au->buf = NULL; |
| 353 | return ret; |
| 354 | } |
| 355 | |
| 356 | static int ceph_x_encode_ticket(struct ceph_x_ticket_handler *th, |
| 357 | void **p, void *end) |
| 358 | { |
| 359 | ceph_decode_need(p, end, 1 + sizeof(u64), bad); |
| 360 | ceph_encode_8(p, 1); |
| 361 | ceph_encode_64(p, th->secret_id); |
| 362 | if (th->ticket_blob) { |
| 363 | const char *buf = th->ticket_blob->vec.iov_base; |
| 364 | u32 len = th->ticket_blob->vec.iov_len; |
| 365 | |
| 366 | ceph_encode_32_safe(p, end, len, bad); |
| 367 | ceph_encode_copy_safe(p, end, buf, len, bad); |
| 368 | } else { |
| 369 | ceph_encode_32_safe(p, end, 0, bad); |
| 370 | } |
| 371 | |
| 372 | return 0; |
| 373 | bad: |
| 374 | return -ERANGE; |
| 375 | } |
| 376 | |
| 377 | static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed) |
| 378 | { |
| 379 | int want = ac->want_keys; |
| 380 | struct ceph_x_info *xi = ac->private; |
| 381 | int service; |
| 382 | |
| 383 | *pneed = ac->want_keys & ~(xi->have_keys); |
| 384 | |
| 385 | for (service = 1; service <= want; service <<= 1) { |
| 386 | struct ceph_x_ticket_handler *th; |
| 387 | |
| 388 | if (!(ac->want_keys & service)) |
| 389 | continue; |
| 390 | |
| 391 | if (*pneed & service) |
| 392 | continue; |
| 393 | |
| 394 | th = get_ticket_handler(ac, service); |
| 395 | |
Dan Carpenter | b545787 | 2010-08-26 11:12:38 +0200 | [diff] [blame] | 396 | if (IS_ERR(th)) { |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 397 | *pneed |= service; |
| 398 | continue; |
| 399 | } |
| 400 | |
| 401 | if (get_seconds() >= th->renew_after) |
| 402 | *pneed |= service; |
| 403 | if (get_seconds() >= th->expires) |
| 404 | xi->have_keys &= ~service; |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | |
| 409 | static int ceph_x_build_request(struct ceph_auth_client *ac, |
| 410 | void *buf, void *end) |
| 411 | { |
| 412 | struct ceph_x_info *xi = ac->private; |
| 413 | int need; |
| 414 | struct ceph_x_request_header *head = buf; |
| 415 | int ret; |
| 416 | struct ceph_x_ticket_handler *th = |
| 417 | get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH); |
| 418 | |
Dan Carpenter | b545787 | 2010-08-26 11:12:38 +0200 | [diff] [blame] | 419 | if (IS_ERR(th)) |
| 420 | return PTR_ERR(th); |
| 421 | |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 422 | ceph_x_validate_tickets(ac, &need); |
| 423 | |
| 424 | dout("build_request want %x have %x need %x\n", |
| 425 | ac->want_keys, xi->have_keys, need); |
| 426 | |
| 427 | if (need & CEPH_ENTITY_TYPE_AUTH) { |
| 428 | struct ceph_x_authenticate *auth = (void *)(head + 1); |
| 429 | void *p = auth + 1; |
| 430 | struct ceph_x_challenge_blob tmp; |
| 431 | char tmp_enc[40]; |
| 432 | u64 *u; |
| 433 | |
| 434 | if (p > end) |
| 435 | return -ERANGE; |
| 436 | |
| 437 | dout(" get_auth_session_key\n"); |
| 438 | head->op = cpu_to_le16(CEPHX_GET_AUTH_SESSION_KEY); |
| 439 | |
| 440 | /* encrypt and hash */ |
| 441 | get_random_bytes(&auth->client_challenge, sizeof(u64)); |
| 442 | tmp.client_challenge = auth->client_challenge; |
| 443 | tmp.server_challenge = cpu_to_le64(xi->server_challenge); |
| 444 | ret = ceph_x_encrypt(&xi->secret, &tmp, sizeof(tmp), |
| 445 | tmp_enc, sizeof(tmp_enc)); |
| 446 | if (ret < 0) |
| 447 | return ret; |
| 448 | |
| 449 | auth->struct_v = 1; |
| 450 | auth->key = 0; |
| 451 | for (u = (u64 *)tmp_enc; u + 1 <= (u64 *)(tmp_enc + ret); u++) |
Yehuda Sadeh | cd84db6 | 2010-06-11 16:58:48 -0700 | [diff] [blame] | 452 | auth->key ^= *(__le64 *)u; |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 453 | dout(" server_challenge %llx client_challenge %llx key %llx\n", |
| 454 | xi->server_challenge, le64_to_cpu(auth->client_challenge), |
| 455 | le64_to_cpu(auth->key)); |
| 456 | |
| 457 | /* now encode the old ticket if exists */ |
| 458 | ret = ceph_x_encode_ticket(th, &p, end); |
| 459 | if (ret < 0) |
| 460 | return ret; |
| 461 | |
| 462 | return p - buf; |
| 463 | } |
| 464 | |
| 465 | if (need) { |
| 466 | void *p = head + 1; |
| 467 | struct ceph_x_service_ticket_request *req; |
| 468 | |
| 469 | if (p > end) |
| 470 | return -ERANGE; |
| 471 | head->op = cpu_to_le16(CEPHX_GET_PRINCIPAL_SESSION_KEY); |
| 472 | |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 473 | ret = ceph_x_build_authorizer(ac, th, &xi->auth_authorizer); |
| 474 | if (ret) |
| 475 | return ret; |
| 476 | ceph_encode_copy(&p, xi->auth_authorizer.buf->vec.iov_base, |
| 477 | xi->auth_authorizer.buf->vec.iov_len); |
| 478 | |
| 479 | req = p; |
| 480 | req->keys = cpu_to_le32(need); |
| 481 | p += sizeof(*req); |
| 482 | return p - buf; |
| 483 | } |
| 484 | |
| 485 | return 0; |
| 486 | } |
| 487 | |
| 488 | static int ceph_x_handle_reply(struct ceph_auth_client *ac, int result, |
| 489 | void *buf, void *end) |
| 490 | { |
| 491 | struct ceph_x_info *xi = ac->private; |
| 492 | struct ceph_x_reply_header *head = buf; |
| 493 | struct ceph_x_ticket_handler *th; |
| 494 | int len = end - buf; |
| 495 | int op; |
| 496 | int ret; |
| 497 | |
| 498 | if (result) |
| 499 | return result; /* XXX hmm? */ |
| 500 | |
| 501 | if (xi->starting) { |
| 502 | /* it's a hello */ |
| 503 | struct ceph_x_server_challenge *sc = buf; |
| 504 | |
| 505 | if (len != sizeof(*sc)) |
| 506 | return -EINVAL; |
| 507 | xi->server_challenge = le64_to_cpu(sc->server_challenge); |
| 508 | dout("handle_reply got server challenge %llx\n", |
| 509 | xi->server_challenge); |
| 510 | xi->starting = false; |
| 511 | xi->have_keys &= ~CEPH_ENTITY_TYPE_AUTH; |
| 512 | return -EAGAIN; |
| 513 | } |
| 514 | |
Yehuda Sadeh | 0cf5537 | 2010-06-11 15:57:06 -0700 | [diff] [blame] | 515 | op = le16_to_cpu(head->op); |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 516 | result = le32_to_cpu(head->result); |
| 517 | dout("handle_reply op %d result %d\n", op, result); |
| 518 | switch (op) { |
| 519 | case CEPHX_GET_AUTH_SESSION_KEY: |
| 520 | /* verify auth key */ |
| 521 | ret = ceph_x_proc_ticket_reply(ac, &xi->secret, |
| 522 | buf + sizeof(*head), end); |
| 523 | break; |
| 524 | |
| 525 | case CEPHX_GET_PRINCIPAL_SESSION_KEY: |
| 526 | th = get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH); |
Dan Carpenter | b545787 | 2010-08-26 11:12:38 +0200 | [diff] [blame] | 527 | if (IS_ERR(th)) |
| 528 | return PTR_ERR(th); |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 529 | ret = ceph_x_proc_ticket_reply(ac, &th->session_key, |
| 530 | buf + sizeof(*head), end); |
| 531 | break; |
| 532 | |
| 533 | default: |
| 534 | return -EINVAL; |
| 535 | } |
| 536 | if (ret) |
| 537 | return ret; |
| 538 | if (ac->want_keys == xi->have_keys) |
| 539 | return 0; |
| 540 | return -EAGAIN; |
| 541 | } |
| 542 | |
| 543 | static int ceph_x_create_authorizer( |
| 544 | struct ceph_auth_client *ac, int peer_type, |
Alex Elder | 74f1869 | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 545 | struct ceph_auth_handshake *auth) |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 546 | { |
| 547 | struct ceph_x_authorizer *au; |
| 548 | struct ceph_x_ticket_handler *th; |
| 549 | int ret; |
| 550 | |
| 551 | th = get_ticket_handler(ac, peer_type); |
| 552 | if (IS_ERR(th)) |
| 553 | return PTR_ERR(th); |
| 554 | |
| 555 | au = kzalloc(sizeof(*au), GFP_NOFS); |
| 556 | if (!au) |
| 557 | return -ENOMEM; |
| 558 | |
| 559 | ret = ceph_x_build_authorizer(ac, th, au); |
| 560 | if (ret) { |
| 561 | kfree(au); |
| 562 | return ret; |
| 563 | } |
| 564 | |
Alex Elder | 74f1869 | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 565 | auth->authorizer = (struct ceph_authorizer *) au; |
| 566 | auth->authorizer_buf = au->buf->vec.iov_base; |
| 567 | auth->authorizer_buf_len = au->buf->vec.iov_len; |
| 568 | auth->authorizer_reply_buf = au->reply_buf; |
| 569 | auth->authorizer_reply_buf_len = sizeof (au->reply_buf); |
| 570 | |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 571 | return 0; |
| 572 | } |
| 573 | |
Sage Weil | 0bed9b5 | 2013-03-25 10:26:01 -0700 | [diff] [blame] | 574 | static int ceph_x_update_authorizer( |
| 575 | struct ceph_auth_client *ac, int peer_type, |
| 576 | struct ceph_auth_handshake *auth) |
| 577 | { |
| 578 | struct ceph_x_authorizer *au; |
| 579 | struct ceph_x_ticket_handler *th; |
Sage Weil | 0bed9b5 | 2013-03-25 10:26:01 -0700 | [diff] [blame] | 580 | |
| 581 | th = get_ticket_handler(ac, peer_type); |
| 582 | if (IS_ERR(th)) |
| 583 | return PTR_ERR(th); |
| 584 | |
| 585 | au = (struct ceph_x_authorizer *)auth->authorizer; |
| 586 | if (au->secret_id < th->secret_id) { |
| 587 | dout("ceph_x_update_authorizer service %u secret %llu < %llu\n", |
| 588 | au->service, au->secret_id, th->secret_id); |
| 589 | return ceph_x_build_authorizer(ac, th, au); |
| 590 | } |
| 591 | return 0; |
| 592 | } |
| 593 | |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 594 | static int ceph_x_verify_authorizer_reply(struct ceph_auth_client *ac, |
| 595 | struct ceph_authorizer *a, size_t len) |
| 596 | { |
| 597 | struct ceph_x_authorizer *au = (void *)a; |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 598 | int ret = 0; |
| 599 | struct ceph_x_authorize_reply reply; |
Ilya Dryomov | c27a3e4 | 2014-09-09 19:39:15 +0400 | [diff] [blame] | 600 | void *preply = &reply; |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 601 | void *p = au->reply_buf; |
| 602 | void *end = p + sizeof(au->reply_buf); |
| 603 | |
Yan, Zheng | ae385ea | 2014-11-04 16:32:35 +0800 | [diff] [blame^] | 604 | ret = ceph_x_decrypt(&au->session_key, &p, end, &preply, sizeof(reply)); |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 605 | if (ret < 0) |
| 606 | return ret; |
| 607 | if (ret != sizeof(reply)) |
| 608 | return -EPERM; |
| 609 | |
| 610 | if (au->nonce + 1 != le64_to_cpu(reply.nonce_plus_one)) |
| 611 | ret = -EPERM; |
| 612 | else |
| 613 | ret = 0; |
| 614 | dout("verify_authorizer_reply nonce %llx got %llx ret %d\n", |
| 615 | au->nonce, le64_to_cpu(reply.nonce_plus_one), ret); |
| 616 | return ret; |
| 617 | } |
| 618 | |
| 619 | static void ceph_x_destroy_authorizer(struct ceph_auth_client *ac, |
| 620 | struct ceph_authorizer *a) |
| 621 | { |
| 622 | struct ceph_x_authorizer *au = (void *)a; |
| 623 | |
Yan, Zheng | ae385ea | 2014-11-04 16:32:35 +0800 | [diff] [blame^] | 624 | ceph_crypto_key_destroy(&au->session_key); |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 625 | ceph_buffer_put(au->buf); |
| 626 | kfree(au); |
| 627 | } |
| 628 | |
| 629 | |
| 630 | static void ceph_x_reset(struct ceph_auth_client *ac) |
| 631 | { |
| 632 | struct ceph_x_info *xi = ac->private; |
| 633 | |
| 634 | dout("reset\n"); |
| 635 | xi->starting = true; |
| 636 | xi->server_challenge = 0; |
| 637 | } |
| 638 | |
| 639 | static void ceph_x_destroy(struct ceph_auth_client *ac) |
| 640 | { |
| 641 | struct ceph_x_info *xi = ac->private; |
| 642 | struct rb_node *p; |
| 643 | |
| 644 | dout("ceph_x_destroy %p\n", ac); |
| 645 | ceph_crypto_key_destroy(&xi->secret); |
| 646 | |
| 647 | while ((p = rb_first(&xi->ticket_handlers)) != NULL) { |
| 648 | struct ceph_x_ticket_handler *th = |
| 649 | rb_entry(p, struct ceph_x_ticket_handler, node); |
| 650 | remove_ticket_handler(ac, th); |
| 651 | } |
| 652 | |
Sage Weil | 22b1de0 | 2010-07-05 15:36:49 -0700 | [diff] [blame] | 653 | if (xi->auth_authorizer.buf) |
| 654 | ceph_buffer_put(xi->auth_authorizer.buf); |
| 655 | |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 656 | kfree(ac->private); |
| 657 | ac->private = NULL; |
| 658 | } |
| 659 | |
| 660 | static void ceph_x_invalidate_authorizer(struct ceph_auth_client *ac, |
| 661 | int peer_type) |
| 662 | { |
| 663 | struct ceph_x_ticket_handler *th; |
| 664 | |
| 665 | th = get_ticket_handler(ac, peer_type); |
Dan Carpenter | b545787 | 2010-08-26 11:12:38 +0200 | [diff] [blame] | 666 | if (!IS_ERR(th)) |
Sage Weil | 4b8e8b5 | 2013-03-25 10:25:49 -0700 | [diff] [blame] | 667 | memset(&th->validity, 0, sizeof(th->validity)); |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | |
| 671 | static const struct ceph_auth_client_ops ceph_x_ops = { |
Sage Weil | 559c1e0 | 2010-05-14 09:55:18 -0700 | [diff] [blame] | 672 | .name = "x", |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 673 | .is_authenticated = ceph_x_is_authenticated, |
Sage Weil | a41359f | 2010-05-25 15:39:06 -0700 | [diff] [blame] | 674 | .should_authenticate = ceph_x_should_authenticate, |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 675 | .build_request = ceph_x_build_request, |
| 676 | .handle_reply = ceph_x_handle_reply, |
| 677 | .create_authorizer = ceph_x_create_authorizer, |
Sage Weil | 0bed9b5 | 2013-03-25 10:26:01 -0700 | [diff] [blame] | 678 | .update_authorizer = ceph_x_update_authorizer, |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 679 | .verify_authorizer_reply = ceph_x_verify_authorizer_reply, |
| 680 | .destroy_authorizer = ceph_x_destroy_authorizer, |
| 681 | .invalidate_authorizer = ceph_x_invalidate_authorizer, |
| 682 | .reset = ceph_x_reset, |
| 683 | .destroy = ceph_x_destroy, |
| 684 | }; |
| 685 | |
| 686 | |
| 687 | int ceph_x_init(struct ceph_auth_client *ac) |
| 688 | { |
| 689 | struct ceph_x_info *xi; |
| 690 | int ret; |
| 691 | |
| 692 | dout("ceph_x_init %p\n", ac); |
Sage Weil | b0930f8 | 2010-04-29 13:26:53 -0700 | [diff] [blame] | 693 | ret = -ENOMEM; |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 694 | xi = kzalloc(sizeof(*xi), GFP_NOFS); |
| 695 | if (!xi) |
Sage Weil | b0930f8 | 2010-04-29 13:26:53 -0700 | [diff] [blame] | 696 | goto out; |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 697 | |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 698 | ret = -EINVAL; |
Tommi Virtanen | 8323c3a | 2011-03-25 16:32:57 -0700 | [diff] [blame] | 699 | if (!ac->key) { |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 700 | pr_err("no secret set (for auth_x protocol)\n"); |
Sage Weil | b0930f8 | 2010-04-29 13:26:53 -0700 | [diff] [blame] | 701 | goto out_nomem; |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 702 | } |
| 703 | |
Tommi Virtanen | 8323c3a | 2011-03-25 16:32:57 -0700 | [diff] [blame] | 704 | ret = ceph_crypto_key_clone(&xi->secret, ac->key); |
| 705 | if (ret < 0) { |
| 706 | pr_err("cannot clone key: %d\n", ret); |
Sage Weil | b0930f8 | 2010-04-29 13:26:53 -0700 | [diff] [blame] | 707 | goto out_nomem; |
Tommi Virtanen | 8323c3a | 2011-03-25 16:32:57 -0700 | [diff] [blame] | 708 | } |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 709 | |
| 710 | xi->starting = true; |
| 711 | xi->ticket_handlers = RB_ROOT; |
| 712 | |
| 713 | ac->protocol = CEPH_AUTH_CEPHX; |
| 714 | ac->private = xi; |
| 715 | ac->ops = &ceph_x_ops; |
| 716 | return 0; |
| 717 | |
Sage Weil | b0930f8 | 2010-04-29 13:26:53 -0700 | [diff] [blame] | 718 | out_nomem: |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 719 | kfree(xi); |
Sage Weil | b0930f8 | 2010-04-29 13:26:53 -0700 | [diff] [blame] | 720 | out: |
Sage Weil | ec0994e | 2010-02-02 16:25:35 -0800 | [diff] [blame] | 721 | return ret; |
| 722 | } |
| 723 | |
| 724 | |