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