1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #pragma once 18 19 #include <stddef.h> 20 #include <stdint.h> 21 #include <sys/cdefs.h> 22 23 #include <functional> 24 #include <memory> 25 #include <string_view> 26 #include <vector> 27 28 #include "adb/pairing/pairing_connection.h" 29 30 #if !defined(__INTRODUCED_IN) 31 #define __INTRODUCED_IN(__api_level) /* nothing */ 32 #endif 33 34 __BEGIN_DECLS 35 36 // PairingServerCtx is a wrapper around the #PairingConnectionCtx APIs, 37 // which handles multiple client connections. 38 // 39 // See pairing_connection_test.cpp for example usage. 40 // 41 struct PairingServerCtx; 42 typedef struct PairingServerCtx PairingServerCtx; 43 44 // Callback containing the result of the pairing. If #PeerInfo is null, 45 // then the pairing failed. Otherwise, pairing succeeded and #PeerInfo 46 // contains information about the peer. 47 typedef void (*pairing_server_result_cb)(const PeerInfo*, void*) __INTRODUCED_IN(30); 48 49 // Starts the pairing server. 50 // 51 // This call is non-blocking. Upon completion, if the pairing was successful, 52 // then |cb| will be called with the PeerInfo 53 // containing the info of the trusted peer. Otherwise, |cb| will be 54 // called with an empty value. Start can only be called once in the lifetime 55 // of this object. 56 // 57 // @param ctx the PairingServerCtx instance. 58 // @param cb the user-provided callback to notify the result of the pairing. See 59 // #pairing_server_result_cb. 60 // @param opaque the opaque userdata. 61 // @return the port number the server is listening on. Returns 0 on failure. 62 uint16_t pairing_server_start(PairingServerCtx* ctx, pairing_server_result_cb cb, void* opaque) 63 __INTRODUCED_IN(30); 64 65 // Creates a new PairingServerCtx instance. 66 // 67 // @param pswd the password used to authenticate the client and server. 68 // @param pswd_len the length of pswd. 69 // @param peer_info the #PeerInfo struct passed to the client on successful 70 // pairing. 71 // @param x509_cert_pem the X.509 certificate in PEM format. Cannot be empty. 72 // @param x509_size the size of x509_cert_pem. 73 // @param priv_key_pem the private key corresponding to the given X.509 74 // certificate, in PEM format. Cannot be empty. 75 // @param priv_size the size of priv_key_pem. 76 // @param port the port number the server should listen on. Must be within the 77 // valid port range [0, 65535]. If port is 0, then the server will 78 // find an open port to listen on. See #pairing_server_start to 79 // obtain the port used. 80 // @return a new PairingServerCtx instance The caller is responsible 81 // for destroying the context via #pairing_server_destroy. 82 PairingServerCtx* pairing_server_new(const uint8_t* pswd, size_t pswd_len, 83 const PeerInfo* peer_info, const uint8_t* x509_cert_pem, 84 size_t x509_size, const uint8_t* priv_key_pem, 85 size_t priv_size, uint16_t port) __INTRODUCED_IN(30); 86 87 // Same as #pairing_server_new, except that the x509 certificate and private key 88 // is generated internally. 89 // 90 // @param pswd the password used to authenticate the client and server. 91 // @param pswd_len the length of pswd. 92 // @param peer_info the #PeerInfo struct passed to the client on successful 93 // pairing. 94 // @param port the port number the server should listen on. Must be within the 95 // valid port range [0, 65535]. If port is 0, then the server will 96 // find an open port to listen on. See #pairing_server_start to 97 // obtain the port used. 98 // @return a new PairingServerCtx instance The caller is responsible 99 // for destroying the context via #pairing_server_destroy. 100 PairingServerCtx* pairing_server_new_no_cert(const uint8_t* pswd, size_t pswd_len, 101 const PeerInfo* peer_info, uint16_t port) 102 __INTRODUCED_IN(30); 103 104 // Destroys the PairingServerCtx instance. 105 // 106 // @param ctx the PairingServerCtx instance to destroy. 107 void pairing_server_destroy(PairingServerCtx* ctx) __INTRODUCED_IN(30); 108 109 __END_DECLS 110