blob: a13ce443073b41ad06552e04b81bb0453a147309 [file] [log] [blame]
Sage Weilec0994e2010-02-02 16:25:35 -08001
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002#include <linux/ceph/ceph_debug.h>
Sage Weilec0994e2010-02-02 16:25:35 -08003
4#include <linux/err.h>
5#include <linux/module.h>
6#include <linux/random.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09007#include <linux/slab.h>
Sage Weilec0994e2010-02-02 16:25:35 -08008
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07009#include <linux/ceph/decode.h>
10#include <linux/ceph/auth.h>
Ilya Dryomova51983e2015-10-28 23:52:06 +010011#include <linux/ceph/libceph.h>
Yan, Zheng33d07332014-11-04 16:33:37 +080012#include <linux/ceph/messenger.h>
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070013
14#include "crypto.h"
Sage Weilec0994e2010-02-02 16:25:35 -080015#include "auth_x.h"
16#include "auth_x_protocol.h"
Sage Weilec0994e2010-02-02 16:25:35 -080017
Sage Weilec0994e2010-02-02 16:25:35 -080018static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed);
19
20static 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 Weila41359f2010-05-25 15:39:06 -070031static 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 Dryomov55d9cc82016-12-02 16:35:07 +010042static int ceph_x_encrypt_offset(void)
43{
44 return sizeof(u32) + sizeof(struct ceph_x_encrypt_header);
45}
46
Sage Weil807c86e2010-03-15 15:52:17 -070047static int ceph_x_encrypt_buflen(int ilen)
48{
Ilya Dryomov55d9cc82016-12-02 16:35:07 +010049 return ceph_x_encrypt_offset() + ilen + 16;
Sage Weil807c86e2010-03-15 15:52:17 -070050}
51
Ilya Dryomovd03857c2016-12-02 16:35:07 +010052static int ceph_x_encrypt(struct ceph_crypto_key *secret, void *buf,
53 int buf_len, int plaintext_len)
Sage Weilec0994e2010-02-02 16:25:35 -080054{
Ilya Dryomovd03857c2016-12-02 16:35:07 +010055 struct ceph_x_encrypt_header *hdr = buf + sizeof(u32);
56 int ciphertext_len;
Sage Weilec0994e2010-02-02 16:25:35 -080057 int ret;
58
Ilya Dryomovd03857c2016-12-02 16:35:07 +010059 hdr->struct_v = 1;
60 hdr->magic = cpu_to_le64(CEPHX_ENC_MAGIC);
61
62 ret = ceph_crypt(secret, true, buf + sizeof(u32), buf_len - sizeof(u32),
63 plaintext_len + sizeof(struct ceph_x_encrypt_header),
64 &ciphertext_len);
Sage Weilec0994e2010-02-02 16:25:35 -080065 if (ret)
66 return ret;
Ilya Dryomovd03857c2016-12-02 16:35:07 +010067
68 ceph_encode_32(&buf, ciphertext_len);
69 return sizeof(u32) + ciphertext_len;
Sage Weilec0994e2010-02-02 16:25:35 -080070}
71
72static int ceph_x_decrypt(struct ceph_crypto_key *secret,
Ilya Dryomovc27a3e42014-09-09 19:39:15 +040073 void **p, void *end, void **obuf, size_t olen)
Sage Weilec0994e2010-02-02 16:25:35 -080074{
75 struct ceph_x_encrypt_header head;
76 size_t head_len = sizeof(head);
77 int len, ret;
78
79 len = ceph_decode_32(p);
80 if (*p + len > end)
81 return -EINVAL;
82
83 dout("ceph_x_decrypt len %d\n", len);
Ilya Dryomovc27a3e42014-09-09 19:39:15 +040084 if (*obuf == NULL) {
85 *obuf = kmalloc(len, GFP_NOFS);
86 if (!*obuf)
87 return -ENOMEM;
88 olen = len;
89 }
90
91 ret = ceph_decrypt2(secret, &head, &head_len, *obuf, &olen, *p, len);
Sage Weilec0994e2010-02-02 16:25:35 -080092 if (ret)
93 return ret;
94 if (head.struct_v != 1 || le64_to_cpu(head.magic) != CEPHX_ENC_MAGIC)
95 return -EPERM;
96 *p += len;
97 return olen;
98}
99
100/*
101 * get existing (or insert new) ticket handler
102 */
Yehuda Sadehcd84db62010-06-11 16:58:48 -0700103static struct ceph_x_ticket_handler *
104get_ticket_handler(struct ceph_auth_client *ac, int service)
Sage Weilec0994e2010-02-02 16:25:35 -0800105{
106 struct ceph_x_ticket_handler *th;
107 struct ceph_x_info *xi = ac->private;
108 struct rb_node *parent = NULL, **p = &xi->ticket_handlers.rb_node;
109
110 while (*p) {
111 parent = *p;
112 th = rb_entry(parent, struct ceph_x_ticket_handler, node);
113 if (service < th->service)
114 p = &(*p)->rb_left;
115 else if (service > th->service)
116 p = &(*p)->rb_right;
117 else
118 return th;
119 }
120
121 /* add it */
122 th = kzalloc(sizeof(*th), GFP_NOFS);
123 if (!th)
124 return ERR_PTR(-ENOMEM);
125 th->service = service;
126 rb_link_node(&th->node, parent, p);
127 rb_insert_color(&th->node, &xi->ticket_handlers);
128 return th;
129}
130
131static void remove_ticket_handler(struct ceph_auth_client *ac,
132 struct ceph_x_ticket_handler *th)
133{
134 struct ceph_x_info *xi = ac->private;
135
136 dout("remove_ticket_handler %p %d\n", th, th->service);
137 rb_erase(&th->node, &xi->ticket_handlers);
138 ceph_crypto_key_destroy(&th->session_key);
139 if (th->ticket_blob)
140 ceph_buffer_put(th->ticket_blob);
141 kfree(th);
142}
143
Ilya Dryomov597cda32014-09-08 17:25:34 +0400144static int process_one_ticket(struct ceph_auth_client *ac,
145 struct ceph_crypto_key *secret,
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400146 void **p, void *end)
Ilya Dryomov597cda32014-09-08 17:25:34 +0400147{
148 struct ceph_x_info *xi = ac->private;
149 int type;
150 u8 tkt_struct_v, blob_struct_v;
151 struct ceph_x_ticket_handler *th;
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400152 void *dbuf = NULL;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400153 void *dp, *dend;
154 int dlen;
155 char is_enc;
156 struct timespec validity;
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400157 void *ticket_buf = NULL;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400158 void *tp, *tpend;
Ilya Dryomove9226d72014-10-22 18:15:37 +0400159 void **ptp;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400160 struct ceph_crypto_key new_session_key;
161 struct ceph_buffer *new_ticket_blob;
162 unsigned long new_expires, new_renew_after;
163 u64 new_secret_id;
164 int ret;
165
166 ceph_decode_need(p, end, sizeof(u32) + 1, bad);
167
168 type = ceph_decode_32(p);
169 dout(" ticket type %d %s\n", type, ceph_entity_type_name(type));
170
171 tkt_struct_v = ceph_decode_8(p);
172 if (tkt_struct_v != 1)
173 goto bad;
174
175 th = get_ticket_handler(ac, type);
176 if (IS_ERR(th)) {
177 ret = PTR_ERR(th);
178 goto out;
179 }
180
181 /* blob for me */
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400182 dlen = ceph_x_decrypt(secret, p, end, &dbuf, 0);
Ilya Dryomov597cda32014-09-08 17:25:34 +0400183 if (dlen <= 0) {
184 ret = dlen;
185 goto out;
186 }
187 dout(" decrypted %d bytes\n", dlen);
188 dp = dbuf;
189 dend = dp + dlen;
190
191 tkt_struct_v = ceph_decode_8(&dp);
192 if (tkt_struct_v != 1)
193 goto bad;
194
Ilya Dryomov597cda32014-09-08 17:25:34 +0400195 ret = ceph_crypto_key_decode(&new_session_key, &dp, dend);
196 if (ret)
197 goto out;
198
Ilya Dryomovf6cdb292016-01-15 13:20:01 +0100199 ceph_decode_timespec(&validity, dp);
200 dp += sizeof(struct ceph_timespec);
Ilya Dryomov597cda32014-09-08 17:25:34 +0400201 new_expires = get_seconds() + validity.tv_sec;
202 new_renew_after = new_expires - (validity.tv_sec / 4);
203 dout(" expires=%lu renew_after=%lu\n", new_expires,
204 new_renew_after);
205
206 /* ticket blob for service */
207 ceph_decode_8_safe(p, end, is_enc, bad);
Ilya Dryomov597cda32014-09-08 17:25:34 +0400208 if (is_enc) {
209 /* encrypted */
210 dout(" encrypted ticket\n");
Ilya Dryomov462e6502016-12-02 16:35:06 +0100211 dlen = ceph_x_decrypt(&th->session_key, p, end, &ticket_buf, 0);
Ilya Dryomov597cda32014-09-08 17:25:34 +0400212 if (dlen < 0) {
213 ret = dlen;
214 goto out;
215 }
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400216 tp = ticket_buf;
Ilya Dryomove9226d72014-10-22 18:15:37 +0400217 ptp = &tp;
218 tpend = *ptp + dlen;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400219 } else {
220 /* unencrypted */
Ilya Dryomove9226d72014-10-22 18:15:37 +0400221 ptp = p;
222 tpend = end;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400223 }
Ilya Dryomove9226d72014-10-22 18:15:37 +0400224 ceph_decode_32_safe(ptp, tpend, dlen, bad);
Ilya Dryomov597cda32014-09-08 17:25:34 +0400225 dout(" ticket blob is %d bytes\n", dlen);
Ilya Dryomove9226d72014-10-22 18:15:37 +0400226 ceph_decode_need(ptp, tpend, 1 + sizeof(u64), bad);
227 blob_struct_v = ceph_decode_8(ptp);
228 new_secret_id = ceph_decode_64(ptp);
229 ret = ceph_decode_buffer(&new_ticket_blob, ptp, tpend);
Ilya Dryomov597cda32014-09-08 17:25:34 +0400230 if (ret)
231 goto out;
232
233 /* all is well, update our ticket */
234 ceph_crypto_key_destroy(&th->session_key);
235 if (th->ticket_blob)
236 ceph_buffer_put(th->ticket_blob);
237 th->session_key = new_session_key;
238 th->ticket_blob = new_ticket_blob;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400239 th->secret_id = new_secret_id;
240 th->expires = new_expires;
241 th->renew_after = new_renew_after;
Ilya Dryomov6abe0972016-01-14 16:35:35 +0100242 th->have_key = true;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400243 dout(" got ticket service %d (%s) secret_id %lld len %d\n",
244 type, ceph_entity_type_name(type), th->secret_id,
245 (int)th->ticket_blob->vec.iov_len);
246 xi->have_keys |= th->service;
247
248out:
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400249 kfree(ticket_buf);
250 kfree(dbuf);
Ilya Dryomov597cda32014-09-08 17:25:34 +0400251 return ret;
252
253bad:
254 ret = -EINVAL;
255 goto out;
256}
257
Sage Weilec0994e2010-02-02 16:25:35 -0800258static int ceph_x_proc_ticket_reply(struct ceph_auth_client *ac,
259 struct ceph_crypto_key *secret,
260 void *buf, void *end)
261{
Sage Weilec0994e2010-02-02 16:25:35 -0800262 void *p = buf;
Sage Weilb736b3d2010-04-30 12:45:02 -0700263 u8 reply_struct_v;
Ilya Dryomov597cda32014-09-08 17:25:34 +0400264 u32 num;
265 int ret;
Sage Weilec0994e2010-02-02 16:25:35 -0800266
Ilya Dryomov597cda32014-09-08 17:25:34 +0400267 ceph_decode_8_safe(&p, end, reply_struct_v, bad);
Sage Weilb736b3d2010-04-30 12:45:02 -0700268 if (reply_struct_v != 1)
Ilya Dryomov597cda32014-09-08 17:25:34 +0400269 return -EINVAL;
270
271 ceph_decode_32_safe(&p, end, num, bad);
Sage Weilec0994e2010-02-02 16:25:35 -0800272 dout("%d tickets\n", num);
Ilya Dryomov597cda32014-09-08 17:25:34 +0400273
Sage Weilec0994e2010-02-02 16:25:35 -0800274 while (num--) {
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400275 ret = process_one_ticket(ac, secret, &p, end);
Sage Weilec0994e2010-02-02 16:25:35 -0800276 if (ret)
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400277 return ret;
Sage Weilec0994e2010-02-02 16:25:35 -0800278 }
279
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400280 return 0;
Sage Weilec0994e2010-02-02 16:25:35 -0800281
282bad:
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400283 return -EINVAL;
Sage Weilec0994e2010-02-02 16:25:35 -0800284}
285
Ilya Dryomovcbf99a12015-10-26 11:03:46 +0100286static void ceph_x_authorizer_cleanup(struct ceph_x_authorizer *au)
287{
288 ceph_crypto_key_destroy(&au->session_key);
289 if (au->buf) {
290 ceph_buffer_put(au->buf);
291 au->buf = NULL;
292 }
293}
294
Sage Weilec0994e2010-02-02 16:25:35 -0800295static int ceph_x_build_authorizer(struct ceph_auth_client *ac,
296 struct ceph_x_ticket_handler *th,
297 struct ceph_x_authorizer *au)
298{
Sage Weil807c86e2010-03-15 15:52:17 -0700299 int maxlen;
Sage Weilec0994e2010-02-02 16:25:35 -0800300 struct ceph_x_authorize_a *msg_a;
Ilya Dryomovd03857c2016-12-02 16:35:07 +0100301 struct ceph_x_authorize_b *msg_b;
Sage Weilec0994e2010-02-02 16:25:35 -0800302 void *p, *end;
303 int ret;
304 int ticket_blob_len =
305 (th->ticket_blob ? th->ticket_blob->vec.iov_len : 0);
306
307 dout("build_authorizer for %s %p\n",
308 ceph_entity_type_name(th->service), au);
309
Yan, Zhengae385ea2014-11-04 16:32:35 +0800310 ceph_crypto_key_destroy(&au->session_key);
311 ret = ceph_crypto_key_clone(&au->session_key, &th->session_key);
312 if (ret)
Ilya Dryomovcbf99a12015-10-26 11:03:46 +0100313 goto out_au;
Yan, Zhengae385ea2014-11-04 16:32:35 +0800314
Ilya Dryomov36721ec2016-12-02 16:35:06 +0100315 maxlen = sizeof(*msg_a) + ticket_blob_len +
Ilya Dryomovd03857c2016-12-02 16:35:07 +0100316 ceph_x_encrypt_buflen(sizeof(*msg_b));
Sage Weil807c86e2010-03-15 15:52:17 -0700317 dout(" need len %d\n", maxlen);
318 if (au->buf && au->buf->alloc_len < maxlen) {
Sage Weilec0994e2010-02-02 16:25:35 -0800319 ceph_buffer_put(au->buf);
320 au->buf = NULL;
321 }
322 if (!au->buf) {
Sage Weil807c86e2010-03-15 15:52:17 -0700323 au->buf = ceph_buffer_new(maxlen, GFP_NOFS);
Yan, Zhengae385ea2014-11-04 16:32:35 +0800324 if (!au->buf) {
Ilya Dryomovcbf99a12015-10-26 11:03:46 +0100325 ret = -ENOMEM;
326 goto out_au;
Yan, Zhengae385ea2014-11-04 16:32:35 +0800327 }
Sage Weilec0994e2010-02-02 16:25:35 -0800328 }
329 au->service = th->service;
Sage Weil0bed9b52013-03-25 10:26:01 -0700330 au->secret_id = th->secret_id;
Sage Weilec0994e2010-02-02 16:25:35 -0800331
332 msg_a = au->buf->vec.iov_base;
333 msg_a->struct_v = 1;
334 msg_a->global_id = cpu_to_le64(ac->global_id);
335 msg_a->service_id = cpu_to_le32(th->service);
336 msg_a->ticket_blob.struct_v = 1;
337 msg_a->ticket_blob.secret_id = cpu_to_le64(th->secret_id);
338 msg_a->ticket_blob.blob_len = cpu_to_le32(ticket_blob_len);
339 if (ticket_blob_len) {
340 memcpy(msg_a->ticket_blob.blob, th->ticket_blob->vec.iov_base,
341 th->ticket_blob->vec.iov_len);
342 }
343 dout(" th %p secret_id %lld %lld\n", th, th->secret_id,
344 le64_to_cpu(msg_a->ticket_blob.secret_id));
345
346 p = msg_a + 1;
347 p += ticket_blob_len;
348 end = au->buf->vec.iov_base + au->buf->vec.iov_len;
349
Ilya Dryomovd03857c2016-12-02 16:35:07 +0100350 msg_b = p + ceph_x_encrypt_offset();
351 msg_b->struct_v = 1;
Sage Weilec0994e2010-02-02 16:25:35 -0800352 get_random_bytes(&au->nonce, sizeof(au->nonce));
Ilya Dryomovd03857c2016-12-02 16:35:07 +0100353 msg_b->nonce = cpu_to_le64(au->nonce);
354 ret = ceph_x_encrypt(&au->session_key, p, end - p, sizeof(*msg_b));
Sage Weilec0994e2010-02-02 16:25:35 -0800355 if (ret < 0)
Ilya Dryomovcbf99a12015-10-26 11:03:46 +0100356 goto out_au;
Ilya Dryomov36721ec2016-12-02 16:35:06 +0100357
Sage Weilec0994e2010-02-02 16:25:35 -0800358 p += ret;
Ilya Dryomov36721ec2016-12-02 16:35:06 +0100359 WARN_ON(p > end);
Sage Weilec0994e2010-02-02 16:25:35 -0800360 au->buf->vec.iov_len = p - au->buf->vec.iov_base;
361 dout(" built authorizer nonce %llx len %d\n", au->nonce,
362 (int)au->buf->vec.iov_len);
Sage Weilec0994e2010-02-02 16:25:35 -0800363 return 0;
364
Ilya Dryomovcbf99a12015-10-26 11:03:46 +0100365out_au:
366 ceph_x_authorizer_cleanup(au);
Sage Weilec0994e2010-02-02 16:25:35 -0800367 return ret;
368}
369
370static int ceph_x_encode_ticket(struct ceph_x_ticket_handler *th,
371 void **p, void *end)
372{
373 ceph_decode_need(p, end, 1 + sizeof(u64), bad);
374 ceph_encode_8(p, 1);
375 ceph_encode_64(p, th->secret_id);
376 if (th->ticket_blob) {
377 const char *buf = th->ticket_blob->vec.iov_base;
378 u32 len = th->ticket_blob->vec.iov_len;
379
380 ceph_encode_32_safe(p, end, len, bad);
381 ceph_encode_copy_safe(p, end, buf, len, bad);
382 } else {
383 ceph_encode_32_safe(p, end, 0, bad);
384 }
385
386 return 0;
387bad:
388 return -ERANGE;
389}
390
Ilya Dryomov6abe0972016-01-14 16:35:35 +0100391static bool need_key(struct ceph_x_ticket_handler *th)
392{
393 if (!th->have_key)
394 return true;
395
396 return get_seconds() >= th->renew_after;
397}
398
399static bool have_key(struct ceph_x_ticket_handler *th)
400{
401 if (th->have_key) {
402 if (get_seconds() >= th->expires)
403 th->have_key = false;
404 }
405
406 return th->have_key;
407}
408
Sage Weilec0994e2010-02-02 16:25:35 -0800409static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed)
410{
411 int want = ac->want_keys;
412 struct ceph_x_info *xi = ac->private;
413 int service;
414
415 *pneed = ac->want_keys & ~(xi->have_keys);
416
417 for (service = 1; service <= want; service <<= 1) {
418 struct ceph_x_ticket_handler *th;
419
420 if (!(ac->want_keys & service))
421 continue;
422
423 if (*pneed & service)
424 continue;
425
426 th = get_ticket_handler(ac, service);
Dan Carpenterb5457872010-08-26 11:12:38 +0200427 if (IS_ERR(th)) {
Sage Weilec0994e2010-02-02 16:25:35 -0800428 *pneed |= service;
429 continue;
430 }
431
Ilya Dryomov6abe0972016-01-14 16:35:35 +0100432 if (need_key(th))
Sage Weilec0994e2010-02-02 16:25:35 -0800433 *pneed |= service;
Ilya Dryomov6abe0972016-01-14 16:35:35 +0100434 if (!have_key(th))
Sage Weilec0994e2010-02-02 16:25:35 -0800435 xi->have_keys &= ~service;
436 }
437}
438
Sage Weilec0994e2010-02-02 16:25:35 -0800439static int ceph_x_build_request(struct ceph_auth_client *ac,
440 void *buf, void *end)
441{
442 struct ceph_x_info *xi = ac->private;
443 int need;
444 struct ceph_x_request_header *head = buf;
445 int ret;
446 struct ceph_x_ticket_handler *th =
447 get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
448
Dan Carpenterb5457872010-08-26 11:12:38 +0200449 if (IS_ERR(th))
450 return PTR_ERR(th);
451
Sage Weilec0994e2010-02-02 16:25:35 -0800452 ceph_x_validate_tickets(ac, &need);
453
454 dout("build_request want %x have %x need %x\n",
455 ac->want_keys, xi->have_keys, need);
456
457 if (need & CEPH_ENTITY_TYPE_AUTH) {
458 struct ceph_x_authenticate *auth = (void *)(head + 1);
459 void *p = auth + 1;
Ilya Dryomovd03857c2016-12-02 16:35:07 +0100460 void *enc_buf = xi->auth_authorizer.enc_buf;
461 struct ceph_x_challenge_blob *blob = enc_buf +
462 ceph_x_encrypt_offset();
Sage Weilec0994e2010-02-02 16:25:35 -0800463 u64 *u;
464
465 if (p > end)
466 return -ERANGE;
467
468 dout(" get_auth_session_key\n");
469 head->op = cpu_to_le16(CEPHX_GET_AUTH_SESSION_KEY);
470
471 /* encrypt and hash */
472 get_random_bytes(&auth->client_challenge, sizeof(u64));
Ilya Dryomovd03857c2016-12-02 16:35:07 +0100473 blob->client_challenge = auth->client_challenge;
474 blob->server_challenge = cpu_to_le64(xi->server_challenge);
475 ret = ceph_x_encrypt(&xi->secret, enc_buf, CEPHX_AU_ENC_BUF_LEN,
476 sizeof(*blob));
Sage Weilec0994e2010-02-02 16:25:35 -0800477 if (ret < 0)
478 return ret;
479
480 auth->struct_v = 1;
481 auth->key = 0;
Ilya Dryomovd03857c2016-12-02 16:35:07 +0100482 for (u = (u64 *)enc_buf; u + 1 <= (u64 *)(enc_buf + ret); u++)
Yehuda Sadehcd84db62010-06-11 16:58:48 -0700483 auth->key ^= *(__le64 *)u;
Sage Weilec0994e2010-02-02 16:25:35 -0800484 dout(" server_challenge %llx client_challenge %llx key %llx\n",
485 xi->server_challenge, le64_to_cpu(auth->client_challenge),
486 le64_to_cpu(auth->key));
487
488 /* now encode the old ticket if exists */
489 ret = ceph_x_encode_ticket(th, &p, end);
490 if (ret < 0)
491 return ret;
492
493 return p - buf;
494 }
495
496 if (need) {
497 void *p = head + 1;
498 struct ceph_x_service_ticket_request *req;
499
500 if (p > end)
501 return -ERANGE;
502 head->op = cpu_to_le16(CEPHX_GET_PRINCIPAL_SESSION_KEY);
503
Sage Weilec0994e2010-02-02 16:25:35 -0800504 ret = ceph_x_build_authorizer(ac, th, &xi->auth_authorizer);
505 if (ret)
506 return ret;
507 ceph_encode_copy(&p, xi->auth_authorizer.buf->vec.iov_base,
508 xi->auth_authorizer.buf->vec.iov_len);
509
510 req = p;
511 req->keys = cpu_to_le32(need);
512 p += sizeof(*req);
513 return p - buf;
514 }
515
516 return 0;
517}
518
519static int ceph_x_handle_reply(struct ceph_auth_client *ac, int result,
520 void *buf, void *end)
521{
522 struct ceph_x_info *xi = ac->private;
523 struct ceph_x_reply_header *head = buf;
524 struct ceph_x_ticket_handler *th;
525 int len = end - buf;
526 int op;
527 int ret;
528
529 if (result)
530 return result; /* XXX hmm? */
531
532 if (xi->starting) {
533 /* it's a hello */
534 struct ceph_x_server_challenge *sc = buf;
535
536 if (len != sizeof(*sc))
537 return -EINVAL;
538 xi->server_challenge = le64_to_cpu(sc->server_challenge);
539 dout("handle_reply got server challenge %llx\n",
540 xi->server_challenge);
541 xi->starting = false;
542 xi->have_keys &= ~CEPH_ENTITY_TYPE_AUTH;
543 return -EAGAIN;
544 }
545
Yehuda Sadeh0cf55372010-06-11 15:57:06 -0700546 op = le16_to_cpu(head->op);
Sage Weilec0994e2010-02-02 16:25:35 -0800547 result = le32_to_cpu(head->result);
548 dout("handle_reply op %d result %d\n", op, result);
549 switch (op) {
550 case CEPHX_GET_AUTH_SESSION_KEY:
551 /* verify auth key */
552 ret = ceph_x_proc_ticket_reply(ac, &xi->secret,
553 buf + sizeof(*head), end);
554 break;
555
556 case CEPHX_GET_PRINCIPAL_SESSION_KEY:
557 th = get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
Dan Carpenterb5457872010-08-26 11:12:38 +0200558 if (IS_ERR(th))
559 return PTR_ERR(th);
Sage Weilec0994e2010-02-02 16:25:35 -0800560 ret = ceph_x_proc_ticket_reply(ac, &th->session_key,
561 buf + sizeof(*head), end);
562 break;
563
564 default:
565 return -EINVAL;
566 }
567 if (ret)
568 return ret;
569 if (ac->want_keys == xi->have_keys)
570 return 0;
571 return -EAGAIN;
572}
573
Ilya Dryomov6c1ea262016-04-11 19:34:49 +0200574static void ceph_x_destroy_authorizer(struct ceph_authorizer *a)
575{
576 struct ceph_x_authorizer *au = (void *)a;
577
578 ceph_x_authorizer_cleanup(au);
579 kfree(au);
580}
581
Sage Weilec0994e2010-02-02 16:25:35 -0800582static int ceph_x_create_authorizer(
583 struct ceph_auth_client *ac, int peer_type,
Alex Elder74f18692012-05-16 15:16:39 -0500584 struct ceph_auth_handshake *auth)
Sage Weilec0994e2010-02-02 16:25:35 -0800585{
586 struct ceph_x_authorizer *au;
587 struct ceph_x_ticket_handler *th;
588 int ret;
589
590 th = get_ticket_handler(ac, peer_type);
591 if (IS_ERR(th))
592 return PTR_ERR(th);
593
594 au = kzalloc(sizeof(*au), GFP_NOFS);
595 if (!au)
596 return -ENOMEM;
597
Ilya Dryomov6c1ea262016-04-11 19:34:49 +0200598 au->base.destroy = ceph_x_destroy_authorizer;
599
Sage Weilec0994e2010-02-02 16:25:35 -0800600 ret = ceph_x_build_authorizer(ac, th, au);
601 if (ret) {
602 kfree(au);
603 return ret;
604 }
605
Alex Elder74f18692012-05-16 15:16:39 -0500606 auth->authorizer = (struct ceph_authorizer *) au;
607 auth->authorizer_buf = au->buf->vec.iov_base;
608 auth->authorizer_buf_len = au->buf->vec.iov_len;
Ilya Dryomov7882a262016-12-02 16:35:07 +0100609 auth->authorizer_reply_buf = au->enc_buf;
610 auth->authorizer_reply_buf_len = CEPHX_AU_ENC_BUF_LEN;
Yan, Zheng33d07332014-11-04 16:33:37 +0800611 auth->sign_message = ac->ops->sign_message;
612 auth->check_message_signature = ac->ops->check_message_signature;
Alex Elder74f18692012-05-16 15:16:39 -0500613
Sage Weilec0994e2010-02-02 16:25:35 -0800614 return 0;
615}
616
Sage Weil0bed9b52013-03-25 10:26:01 -0700617static int ceph_x_update_authorizer(
618 struct ceph_auth_client *ac, int peer_type,
619 struct ceph_auth_handshake *auth)
620{
621 struct ceph_x_authorizer *au;
622 struct ceph_x_ticket_handler *th;
Sage Weil0bed9b52013-03-25 10:26:01 -0700623
624 th = get_ticket_handler(ac, peer_type);
625 if (IS_ERR(th))
626 return PTR_ERR(th);
627
628 au = (struct ceph_x_authorizer *)auth->authorizer;
629 if (au->secret_id < th->secret_id) {
630 dout("ceph_x_update_authorizer service %u secret %llu < %llu\n",
631 au->service, au->secret_id, th->secret_id);
632 return ceph_x_build_authorizer(ac, th, au);
633 }
634 return 0;
635}
636
Sage Weilec0994e2010-02-02 16:25:35 -0800637static int ceph_x_verify_authorizer_reply(struct ceph_auth_client *ac,
638 struct ceph_authorizer *a, size_t len)
639{
640 struct ceph_x_authorizer *au = (void *)a;
Sage Weilec0994e2010-02-02 16:25:35 -0800641 int ret = 0;
642 struct ceph_x_authorize_reply reply;
Ilya Dryomovc27a3e42014-09-09 19:39:15 +0400643 void *preply = &reply;
Ilya Dryomov7882a262016-12-02 16:35:07 +0100644 void *p = au->enc_buf;
Sage Weilec0994e2010-02-02 16:25:35 -0800645
Ilya Dryomov7882a262016-12-02 16:35:07 +0100646 ret = ceph_x_decrypt(&au->session_key, &p, p + CEPHX_AU_ENC_BUF_LEN,
647 &preply, sizeof(reply));
Sage Weilec0994e2010-02-02 16:25:35 -0800648 if (ret < 0)
649 return ret;
650 if (ret != sizeof(reply))
651 return -EPERM;
652
653 if (au->nonce + 1 != le64_to_cpu(reply.nonce_plus_one))
654 ret = -EPERM;
655 else
656 ret = 0;
657 dout("verify_authorizer_reply nonce %llx got %llx ret %d\n",
658 au->nonce, le64_to_cpu(reply.nonce_plus_one), ret);
659 return ret;
660}
661
Sage Weilec0994e2010-02-02 16:25:35 -0800662static void ceph_x_reset(struct ceph_auth_client *ac)
663{
664 struct ceph_x_info *xi = ac->private;
665
666 dout("reset\n");
667 xi->starting = true;
668 xi->server_challenge = 0;
669}
670
671static void ceph_x_destroy(struct ceph_auth_client *ac)
672{
673 struct ceph_x_info *xi = ac->private;
674 struct rb_node *p;
675
676 dout("ceph_x_destroy %p\n", ac);
677 ceph_crypto_key_destroy(&xi->secret);
678
679 while ((p = rb_first(&xi->ticket_handlers)) != NULL) {
680 struct ceph_x_ticket_handler *th =
681 rb_entry(p, struct ceph_x_ticket_handler, node);
682 remove_ticket_handler(ac, th);
683 }
684
Ilya Dryomovcbf99a12015-10-26 11:03:46 +0100685 ceph_x_authorizer_cleanup(&xi->auth_authorizer);
Sage Weil22b1de02010-07-05 15:36:49 -0700686
Sage Weilec0994e2010-02-02 16:25:35 -0800687 kfree(ac->private);
688 ac->private = NULL;
689}
690
Ilya Dryomov187d1312016-01-14 17:31:51 +0100691static void invalidate_ticket(struct ceph_auth_client *ac, int peer_type)
Sage Weilec0994e2010-02-02 16:25:35 -0800692{
693 struct ceph_x_ticket_handler *th;
694
695 th = get_ticket_handler(ac, peer_type);
Dan Carpenterb5457872010-08-26 11:12:38 +0200696 if (!IS_ERR(th))
Ilya Dryomov6abe0972016-01-14 16:35:35 +0100697 th->have_key = false;
Sage Weilec0994e2010-02-02 16:25:35 -0800698}
699
Ilya Dryomov187d1312016-01-14 17:31:51 +0100700static void ceph_x_invalidate_authorizer(struct ceph_auth_client *ac,
701 int peer_type)
702{
703 /*
704 * We are to invalidate a service ticket in the hopes of
705 * getting a new, hopefully more valid, one. But, we won't get
706 * it unless our AUTH ticket is good, so invalidate AUTH ticket
707 * as well, just in case.
708 */
709 invalidate_ticket(ac, peer_type);
710 invalidate_ticket(ac, CEPH_ENTITY_TYPE_AUTH);
711}
712
Ilya Dryomov4eb45172016-12-02 16:35:07 +0100713static int calc_signature(struct ceph_x_authorizer *au, struct ceph_msg *msg,
714 __le64 *psig)
Yan, Zheng33d07332014-11-04 16:33:37 +0800715{
Ilya Dryomovd03857c2016-12-02 16:35:07 +0100716 void *enc_buf = au->enc_buf;
Ilya Dryomov4eb45172016-12-02 16:35:07 +0100717 struct {
718 __le32 len;
719 __le32 header_crc;
720 __le32 front_crc;
721 __le32 middle_crc;
722 __le32 data_crc;
Ilya Dryomovd03857c2016-12-02 16:35:07 +0100723 } __packed *sigblock = enc_buf + ceph_x_encrypt_offset();
Ilya Dryomov4eb45172016-12-02 16:35:07 +0100724 int ret;
725
Ilya Dryomovd03857c2016-12-02 16:35:07 +0100726 sigblock->len = cpu_to_le32(4*sizeof(u32));
727 sigblock->header_crc = msg->hdr.crc;
728 sigblock->front_crc = msg->footer.front_crc;
729 sigblock->middle_crc = msg->footer.middle_crc;
730 sigblock->data_crc = msg->footer.data_crc;
731 ret = ceph_x_encrypt(&au->session_key, enc_buf, CEPHX_AU_ENC_BUF_LEN,
732 sizeof(*sigblock));
Yan, Zheng33d07332014-11-04 16:33:37 +0800733 if (ret < 0)
734 return ret;
Ilya Dryomov4eb45172016-12-02 16:35:07 +0100735
Ilya Dryomovd03857c2016-12-02 16:35:07 +0100736 *psig = *(__le64 *)(enc_buf + sizeof(u32));
Yan, Zheng33d07332014-11-04 16:33:37 +0800737 return 0;
738}
739
740static int ceph_x_sign_message(struct ceph_auth_handshake *auth,
741 struct ceph_msg *msg)
742{
Ilya Dryomov4eb45172016-12-02 16:35:07 +0100743 __le64 sig;
Yan, Zheng33d07332014-11-04 16:33:37 +0800744 int ret;
Ilya Dryomov4199b8e2015-10-27 16:42:49 +0100745
Ilya Dryomova51983e2015-10-28 23:52:06 +0100746 if (ceph_test_opt(from_msgr(msg->con->msgr), NOMSGSIGN))
747 return 0;
748
Ilya Dryomov4eb45172016-12-02 16:35:07 +0100749 ret = calc_signature((struct ceph_x_authorizer *)auth->authorizer,
750 msg, &sig);
751 if (ret)
Yan, Zheng33d07332014-11-04 16:33:37 +0800752 return ret;
Ilya Dryomov4eb45172016-12-02 16:35:07 +0100753
754 msg->footer.sig = sig;
Yan, Zheng33d07332014-11-04 16:33:37 +0800755 msg->footer.flags |= CEPH_MSG_FOOTER_SIGNED;
756 return 0;
757}
758
759static int ceph_x_check_message_signature(struct ceph_auth_handshake *auth,
760 struct ceph_msg *msg)
761{
762 __le64 sig_check;
763 int ret;
764
Ilya Dryomova51983e2015-10-28 23:52:06 +0100765 if (ceph_test_opt(from_msgr(msg->con->msgr), NOMSGSIGN))
766 return 0;
767
Ilya Dryomov4eb45172016-12-02 16:35:07 +0100768 ret = calc_signature((struct ceph_x_authorizer *)auth->authorizer,
769 msg, &sig_check);
770 if (ret)
Yan, Zheng33d07332014-11-04 16:33:37 +0800771 return ret;
772 if (sig_check == msg->footer.sig)
773 return 0;
774 if (msg->footer.flags & CEPH_MSG_FOOTER_SIGNED)
775 dout("ceph_x_check_message_signature %p has signature %llx "
776 "expect %llx\n", msg, msg->footer.sig, sig_check);
777 else
778 dout("ceph_x_check_message_signature %p sender did not set "
779 "CEPH_MSG_FOOTER_SIGNED\n", msg);
780 return -EBADMSG;
781}
Sage Weilec0994e2010-02-02 16:25:35 -0800782
783static const struct ceph_auth_client_ops ceph_x_ops = {
Sage Weil559c1e02010-05-14 09:55:18 -0700784 .name = "x",
Sage Weilec0994e2010-02-02 16:25:35 -0800785 .is_authenticated = ceph_x_is_authenticated,
Sage Weila41359f2010-05-25 15:39:06 -0700786 .should_authenticate = ceph_x_should_authenticate,
Sage Weilec0994e2010-02-02 16:25:35 -0800787 .build_request = ceph_x_build_request,
788 .handle_reply = ceph_x_handle_reply,
789 .create_authorizer = ceph_x_create_authorizer,
Sage Weil0bed9b52013-03-25 10:26:01 -0700790 .update_authorizer = ceph_x_update_authorizer,
Sage Weilec0994e2010-02-02 16:25:35 -0800791 .verify_authorizer_reply = ceph_x_verify_authorizer_reply,
Sage Weilec0994e2010-02-02 16:25:35 -0800792 .invalidate_authorizer = ceph_x_invalidate_authorizer,
793 .reset = ceph_x_reset,
794 .destroy = ceph_x_destroy,
Yan, Zheng33d07332014-11-04 16:33:37 +0800795 .sign_message = ceph_x_sign_message,
796 .check_message_signature = ceph_x_check_message_signature,
Sage Weilec0994e2010-02-02 16:25:35 -0800797};
798
799
800int ceph_x_init(struct ceph_auth_client *ac)
801{
802 struct ceph_x_info *xi;
803 int ret;
804
805 dout("ceph_x_init %p\n", ac);
Sage Weilb0930f82010-04-29 13:26:53 -0700806 ret = -ENOMEM;
Sage Weilec0994e2010-02-02 16:25:35 -0800807 xi = kzalloc(sizeof(*xi), GFP_NOFS);
808 if (!xi)
Sage Weilb0930f82010-04-29 13:26:53 -0700809 goto out;
Sage Weilec0994e2010-02-02 16:25:35 -0800810
Sage Weilec0994e2010-02-02 16:25:35 -0800811 ret = -EINVAL;
Tommi Virtanen8323c3a2011-03-25 16:32:57 -0700812 if (!ac->key) {
Sage Weilec0994e2010-02-02 16:25:35 -0800813 pr_err("no secret set (for auth_x protocol)\n");
Sage Weilb0930f82010-04-29 13:26:53 -0700814 goto out_nomem;
Sage Weilec0994e2010-02-02 16:25:35 -0800815 }
816
Tommi Virtanen8323c3a2011-03-25 16:32:57 -0700817 ret = ceph_crypto_key_clone(&xi->secret, ac->key);
818 if (ret < 0) {
819 pr_err("cannot clone key: %d\n", ret);
Sage Weilb0930f82010-04-29 13:26:53 -0700820 goto out_nomem;
Tommi Virtanen8323c3a2011-03-25 16:32:57 -0700821 }
Sage Weilec0994e2010-02-02 16:25:35 -0800822
823 xi->starting = true;
824 xi->ticket_handlers = RB_ROOT;
825
826 ac->protocol = CEPH_AUTH_CEPHX;
827 ac->private = xi;
828 ac->ops = &ceph_x_ops;
829 return 0;
830
Sage Weilb0930f82010-04-29 13:26:53 -0700831out_nomem:
Sage Weilec0994e2010-02-02 16:25:35 -0800832 kfree(xi);
Sage Weilb0930f82010-04-29 13:26:53 -0700833out:
Sage Weilec0994e2010-02-02 16:25:35 -0800834 return ret;
835}
836
837