@@ -11,6 +11,7 @@ import * as Net from 'net';
11
11
import ServiceMap from './servicemap' ;
12
12
import Service from './service' ;
13
13
import SocketOptions from '../../SocketOptions' ;
14
+ import ExtendedPublicKey from '../../ExtendedPublicKey' ;
14
15
15
16
const debug = d ( 'adb:tcpusb:socket' ) ;
16
17
const UINT32_MAX = 0xffffffff ;
@@ -159,6 +160,14 @@ export default class Socket extends EventEmitter {
159
160
if ( ! this . signature ) {
160
161
this . signature = packet . data ;
161
162
}
163
+
164
+ const digest = this . token . toString ( 'binary' ) ;
165
+ const sig = this . signature . toString ( 'binary' ) ;
166
+ for ( const key of this . options . knownPublicKeys ?? [ ] ) {
167
+ // If signature matches one of the known public keys, we can safely accept the connection
168
+ if ( key . verify ( digest , sig ) ) return this . _acceptConnection ( ) ;
169
+ }
170
+
162
171
debug ( 'O:A_AUTH' ) ;
163
172
const b = this . write ( Packet . assemble ( Packet . A_AUTH , AUTH_TOKEN , 0 , this . token ) ) ;
164
173
return Bluebird . resolve ( b ) ;
@@ -189,18 +198,25 @@ export default class Socket extends EventEmitter {
189
198
} ) ;
190
199
} )
191
200
. then ( ( ) => {
192
- return this . _deviceId ( ) ;
193
- } )
194
- . then ( ( id ) => {
195
- this . authorized = true ;
196
- debug ( 'O:A_CNXN' ) ;
197
- return this . write ( Packet . assemble ( Packet . A_CNXN , Packet . swap32 ( this . version ) , this . maxPayload , id ) ) ;
201
+ return this . _acceptConnection ( ) ;
198
202
} ) ;
199
203
default :
200
204
throw new Error ( `Unknown authentication method ${ packet . arg0 } ` ) ;
201
205
}
202
206
}
203
207
208
+ /**
209
+ * Mark the incoming connection as authorized
210
+ * and send the connection packet
211
+ */
212
+ private _acceptConnection ( ) : Bluebird < boolean > {
213
+ return this . _deviceId ( ) . then ( ( id ) => {
214
+ this . authorized = true ;
215
+ debug ( 'O:A_CNXN' ) ;
216
+ return this . write ( Packet . assemble ( Packet . A_CNXN , Packet . swap32 ( this . version ) , this . maxPayload , id ) ) ;
217
+ } ) ;
218
+ }
219
+
204
220
private _handleOpenPacket ( packet : Packet ) : Bluebird < boolean | Service > {
205
221
if ( ! this . authorized ) {
206
222
throw new Socket . UnauthorizedError ( ) ;
0 commit comments