• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #pragma once
2 
3 /*
4  * Copyright (C) 2019 The Android Open Source Project
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #include <stdbool.h>
20 #include <stdint.h>
21 #include <sys/cdefs.h>
22 #include <sys/types.h>
23 
24 #if !defined(__INTRODUCED_IN)
25 #define __INTRODUCED_IN(__api_level) /* nothing */
26 #endif
27 
28 __BEGIN_DECLS
29 #if !defined(__ANDROID__) || __ANDROID_API__ >= 30
30 
31 // The transport type of the device connection.
32 enum AdbTransportType : int32_t {
33     kAdbTransportTypeUsb = 0,
34     kAdbTransportTypeWifi,
35 };
36 static_assert(sizeof(AdbTransportType) == sizeof(int32_t), "Unexpected AdbTransportType size");
37 
38 struct AdbdAuthCallbacks {
39     uint32_t version;
40 };
41 
42 struct AdbdAuthCallbacksV1 : AdbdAuthCallbacks {
43     // Callback for a successful user authorization.
44     void (*key_authorized)(void* opaque, uint64_t id);
45     // The framework removed the key from the keystore. This callback notifies
46     // adbd so it can take the appropriate actions (e.g. disconnect all devices
47     // using that key).
48     void (*key_removed)(const char* public_key, size_t length);
49 };
50 
51 struct AdbdAuthContext;
52 typedef struct AdbdAuthContext AdbdAuthContext;
53 
54 /**
55  * Creates a new AdbdAuthContext.
56  *
57  * @param callbacks a set of user-provided callbacks used internally (see
58  * #AdbdAuthCallbacksV1
59  * @return a new AdbdAuthContext instance. Caller is responsible for destroying
60  * the context with #adbd_auth_delete.
61  */
62 AdbdAuthContext* adbd_auth_new(AdbdAuthCallbacks* callbacks) __INTRODUCED_IN(30);
63 
64 /**
65  * Destroys the AdbdAuthContext.
66  *
67  * @param ctx the AdbdAuthContext to destroy.
68  */
69 void adbd_auth_delete(AdbdAuthContext* ctx) __INTRODUCED_IN(30);
70 
71 /**
72  * Starts the AdbdAuthContext.
73  *
74  * The caller may want to run this on a different thread, as this
75  * runs indefinitely.
76  *
77  * @param ctx the AdbdAuthContext
78  */
79 void adbd_auth_run(AdbdAuthContext* ctx) __INTRODUCED_IN(30);
80 
81 /**
82  * Iterate through the list of authorized public keys.
83  *
84  * @param ctx the AdbdAuthContext
85  * @param callback a callback which will get called for every known adb public
86  * key in its keystore. To stop iteration of the keys, return false in the
87  * callback. Otherwise, return true to continue the iteration.
88  * @param opaque an opaque userdata argument
89  */
90 void adbd_auth_get_public_keys(AdbdAuthContext* ctx,
91                                bool (*callback)(void* opaque, const char* public_key, size_t len),
92                                void* opaque) __INTRODUCED_IN(30);
93 
94 /**
95  * Let system_server know that a key has been successfully used for authentication.
96  *
97  * @param ctx the AdbdAuthContext
98  * @param public_key the RSA key that was authorized using the AUTH protocol
99  * @param len the length of the public_key argument
100  * @return an id corresponding to the new connection
101  */
102 uint64_t adbd_auth_notify_auth(AdbdAuthContext* ctx,
103                                const char* public_key,
104                                size_t len) __INTRODUCED_IN(30);
105 
106 /**
107  * Let system_server know that an AUTH connection has been closed.
108  *
109  * @param ctx the AdbdAuthContext
110  * @param id the id of the disconnected device
111  */
112 void adbd_auth_notify_disconnect(AdbdAuthContext* ctx,
113                                  uint64_t id) __INTRODUCED_IN(30);
114 
115 /**
116  * Prompt the user to authorize a public key.
117  *
118  * When this happens, a callback will be run on the auth thread with the result.
119  *
120  * @param ctx the AdbdAuthContext
121  * @param public_key the RSA public key to prompt user with
122  * @param len the length of the public_key argument
123  * @param arg an opaque userdata argument
124  */
125 void adbd_auth_prompt_user(AdbdAuthContext* ctx, const char* public_key, size_t len, void* opaque)
126         __INTRODUCED_IN(30);
127 
128 /**
129  * Prompt the user to authorize a public key.
130  *
131  * When this happens, a callback will be run on the auth thread with the result.
132  *
133  * @param ctx the AdbdAuthContext
134  * @param public_key the RSA public key to prompt user with
135  * @param len the length of the public_key argument
136  * @param arg an opaque userdata argument
137  * @return a unique id which will be returned via callback
138  */
139 __attribute__((weak)) uint64_t adbd_auth_prompt_user_with_id(AdbdAuthContext* ctx,
140                                                              const char* public_key, size_t len,
141                                                              void* opaque) __INTRODUCED_IN(30);
142 
143 /**
144  * Let system_server know that a TLS device has connected.
145  *
146  * @param ctx the AdbdAuthContext
147  * @param type the transport type of the connection (see #AdbTransportType)
148  * @param public_key the RSA public key used to establish the connection
149  * @param len the length of the public_key argument
150  * @return an id corresponding to the new connection
151  */
152 uint64_t adbd_auth_tls_device_connected(AdbdAuthContext* ctx,
153                                         AdbTransportType type,
154                                         const char* public_key,
155                                         size_t len) __INTRODUCED_IN(30);
156 
157 /**
158  * Let system_server know that a TLS device has disconnected.
159  *
160  * @param ctx the AdbdAuthContext
161  * @param type the transport type of the connection (see #AdbTransportType)
162  * @param the id of the disconnected device (see #adbd_tls_device_connected)
163  */
164 void adbd_auth_tls_device_disconnected(AdbdAuthContext* ctx,
165                                        AdbTransportType type,
166                                        uint64_t id) __INTRODUCED_IN(30);
167 
168 /**
169  * Returns the max #AdbdAuthCallbacks version.
170  *
171  * The version starts at 1, with version 1 corresponding to the
172  * #AdbdAuthCallbacksV1 struct.
173  *
174  * @return the max #AdbdAuthCallbacks version.
175  */
176 uint32_t adbd_auth_get_max_version(void) __INTRODUCED_IN(30);
177 
178 enum AdbdAuthFeature : int32_t {
179 };
180 
181 /**
182  * Checks if a feature is supported by the framework. See #AdbdAuthFeature.
183  *
184  * @param feature the feature to check for support
185  * @return true if the feature is supported
186  */
187 bool adbd_auth_supports_feature(AdbdAuthFeature feature);
188 
189 #endif  //!__ANDROID__ || __ANDROID_API__ >= 30
190 __END_DECLS
191