xref: /linux-6.15/include/linux/ceph/auth.h (revision 285ea34f)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _FS_CEPH_AUTH_H
3 #define _FS_CEPH_AUTH_H
4 
5 #include <linux/ceph/types.h>
6 #include <linux/ceph/buffer.h>
7 
8 /*
9  * Abstract interface for communicating with the authenticate module.
10  * There is some handshake that takes place between us and the monitor
11  * to acquire the necessary keys.  These are used to generate an
12  * 'authorizer' that we use when connecting to a service (mds, osd).
13  */
14 
15 struct ceph_auth_client;
16 struct ceph_msg;
17 
18 struct ceph_authorizer {
19 	void (*destroy)(struct ceph_authorizer *);
20 };
21 
22 struct ceph_auth_handshake {
23 	struct ceph_authorizer *authorizer;
24 	void *authorizer_buf;
25 	size_t authorizer_buf_len;
26 	void *authorizer_reply_buf;
27 	size_t authorizer_reply_buf_len;
28 	int (*sign_message)(struct ceph_auth_handshake *auth,
29 			    struct ceph_msg *msg);
30 	int (*check_message_signature)(struct ceph_auth_handshake *auth,
31 				       struct ceph_msg *msg);
32 };
33 
34 struct ceph_auth_client_ops {
35 	const char *name;
36 
37 	/*
38 	 * true if we are authenticated and can connect to
39 	 * services.
40 	 */
41 	int (*is_authenticated)(struct ceph_auth_client *ac);
42 
43 	/*
44 	 * true if we should (re)authenticate, e.g., when our tickets
45 	 * are getting old and crusty.
46 	 */
47 	int (*should_authenticate)(struct ceph_auth_client *ac);
48 
49 	/*
50 	 * build requests and process replies during monitor
51 	 * handshake.  if handle_reply returns -EAGAIN, we build
52 	 * another request.
53 	 */
54 	int (*build_request)(struct ceph_auth_client *ac, void *buf, void *end);
55 	int (*handle_reply)(struct ceph_auth_client *ac, int result,
56 			    void *buf, void *end, u8 *session_key,
57 			    int *session_key_len, u8 *con_secret,
58 			    int *con_secret_len);
59 
60 	/*
61 	 * Create authorizer for connecting to a service, and verify
62 	 * the response to authenticate the service.
63 	 */
64 	int (*create_authorizer)(struct ceph_auth_client *ac, int peer_type,
65 				 struct ceph_auth_handshake *auth);
66 	/* ensure that an existing authorizer is up to date */
67 	int (*update_authorizer)(struct ceph_auth_client *ac, int peer_type,
68 				 struct ceph_auth_handshake *auth);
69 	int (*add_authorizer_challenge)(struct ceph_auth_client *ac,
70 					struct ceph_authorizer *a,
71 					void *challenge_buf,
72 					int challenge_buf_len);
73 	int (*verify_authorizer_reply)(struct ceph_auth_client *ac,
74 				       struct ceph_authorizer *a,
75 				       void *reply, int reply_len,
76 				       u8 *session_key, int *session_key_len,
77 				       u8 *con_secret, int *con_secret_len);
78 	void (*invalidate_authorizer)(struct ceph_auth_client *ac,
79 				      int peer_type);
80 
81 	/* reset when we (re)connect to a monitor */
82 	void (*reset)(struct ceph_auth_client *ac);
83 
84 	void (*destroy)(struct ceph_auth_client *ac);
85 
86 	int (*sign_message)(struct ceph_auth_handshake *auth,
87 			    struct ceph_msg *msg);
88 	int (*check_message_signature)(struct ceph_auth_handshake *auth,
89 				       struct ceph_msg *msg);
90 };
91 
92 struct ceph_auth_client {
93 	u32 protocol;           /* CEPH_AUTH_* */
94 	void *private;          /* for use by protocol implementation */
95 	const struct ceph_auth_client_ops *ops;  /* null iff protocol==0 */
96 
97 	bool negotiating;       /* true if negotiating protocol */
98 	const char *name;       /* entity name */
99 	u64 global_id;          /* our unique id in system */
100 	const struct ceph_crypto_key *key;     /* our secret key */
101 	unsigned want_keys;     /* which services we want */
102 
103 	struct mutex mutex;
104 };
105 
106 extern struct ceph_auth_client *ceph_auth_init(const char *name,
107 					       const struct ceph_crypto_key *key);
108 extern void ceph_auth_destroy(struct ceph_auth_client *ac);
109 
110 extern void ceph_auth_reset(struct ceph_auth_client *ac);
111 
112 extern int ceph_auth_build_hello(struct ceph_auth_client *ac,
113 				 void *buf, size_t len);
114 extern int ceph_handle_auth_reply(struct ceph_auth_client *ac,
115 				  void *buf, size_t len,
116 				  void *reply_buf, size_t reply_len);
117 int ceph_auth_entity_name_encode(const char *name, void **p, void *end);
118 
119 extern int ceph_build_auth(struct ceph_auth_client *ac,
120 		    void *msg_buf, size_t msg_len);
121 
122 extern int ceph_auth_is_authenticated(struct ceph_auth_client *ac);
123 extern int ceph_auth_create_authorizer(struct ceph_auth_client *ac,
124 				       int peer_type,
125 				       struct ceph_auth_handshake *auth);
126 void ceph_auth_destroy_authorizer(struct ceph_authorizer *a);
127 extern int ceph_auth_update_authorizer(struct ceph_auth_client *ac,
128 				       int peer_type,
129 				       struct ceph_auth_handshake *a);
130 int ceph_auth_add_authorizer_challenge(struct ceph_auth_client *ac,
131 				       struct ceph_authorizer *a,
132 				       void *challenge_buf,
133 				       int challenge_buf_len);
134 int ceph_auth_verify_authorizer_reply(struct ceph_auth_client *ac,
135 				      struct ceph_authorizer *a,
136 				      void *reply, int reply_len,
137 				      u8 *session_key, int *session_key_len,
138 				      u8 *con_secret, int *con_secret_len);
139 extern void ceph_auth_invalidate_authorizer(struct ceph_auth_client *ac,
140 					    int peer_type);
141 
142 static inline int ceph_auth_sign_message(struct ceph_auth_handshake *auth,
143 					 struct ceph_msg *msg)
144 {
145 	if (auth->sign_message)
146 		return auth->sign_message(auth, msg);
147 	return 0;
148 }
149 
150 static inline
151 int ceph_auth_check_message_signature(struct ceph_auth_handshake *auth,
152 				      struct ceph_msg *msg)
153 {
154 	if (auth->check_message_signature)
155 		return auth->check_message_signature(auth, msg);
156 	return 0;
157 }
158 #endif
159