Skip to content

Commit 2fccd69

Browse files
Allow authorization of known public keys (#540)
Signed-off-by: Yishai Yosifov <[email protected]>
1 parent 05a550f commit 2fccd69

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

src/SocketOptions.ts

+1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ import Bluebird from 'bluebird';
33

44
export default interface SocketOptions {
55
auth?: (key: ExtendedPublicKey) => Bluebird<void | boolean>;
6+
knownPublicKeys?: ExtendedPublicKey[];
67
}

src/adb/tcpusb/socket.ts

+22-6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import * as Net from 'net';
1111
import ServiceMap from './servicemap';
1212
import Service from './service';
1313
import SocketOptions from '../../SocketOptions';
14+
import ExtendedPublicKey from '../../ExtendedPublicKey';
1415

1516
const debug = d('adb:tcpusb:socket');
1617
const UINT32_MAX = 0xffffffff;
@@ -159,6 +160,14 @@ export default class Socket extends EventEmitter {
159160
if (!this.signature) {
160161
this.signature = packet.data;
161162
}
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+
162171
debug('O:A_AUTH');
163172
const b = this.write(Packet.assemble(Packet.A_AUTH, AUTH_TOKEN, 0, this.token));
164173
return Bluebird.resolve(b);
@@ -189,18 +198,25 @@ export default class Socket extends EventEmitter {
189198
});
190199
})
191200
.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();
198202
});
199203
default:
200204
throw new Error(`Unknown authentication method ${packet.arg0}`);
201205
}
202206
}
203207

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+
204220
private _handleOpenPacket(packet: Packet): Bluebird<boolean | Service> {
205221
if (!this.authorized) {
206222
throw new Socket.UnauthorizedError();

0 commit comments

Comments
 (0)