Merge remote-tracking branch 'origin/master' into mpetrunic/browser-tests

# Conflicts:
#	protos/payload.proto
This commit is contained in:
Marin Petrunić
2020-02-06 09:53:10 +01:00
2 changed files with 18 additions and 28 deletions

8
protos/payload.proto Normal file
View File

@ -0,0 +1,8 @@
syntax = "proto3";
package pb;
message NoiseHandshakePayload {
bytes identity_key = 1;
bytes identity_sig = 2;
bytes data = 3;
}

View File

@ -29,30 +29,29 @@ export async function getPayload(
earlyData?: bytes, earlyData?: bytes,
): Promise<bytes> { ): Promise<bytes> {
const signedPayload = await signPayload(localPeer, getHandshakePayload(staticPublicKey)); const signedPayload = await signPayload(localPeer, getHandshakePayload(staticPublicKey));
const signedEarlyDataPayload = await signEarlyDataPayload(localPeer, earlyData || Buffer.alloc(0)); const earlyDataPayload = earlyData || Buffer.alloc(0);
return await createHandshakePayload( return await createHandshakePayload(
localPeer.marshalPubKey(), localPeer.marshalPubKey(),
signedPayload, signedPayload,
signedEarlyDataPayload earlyDataPayload
); );
} }
export async function createHandshakePayload( export async function createHandshakePayload(
libp2pPublicKey: bytes, libp2pPublicKey: bytes,
signedPayload: bytes, signedPayload: bytes,
signedEarlyData?: EarlyDataPayload, earlyData?: bytes,
): Promise<bytes> { ): Promise<bytes> {
const NoiseHandshakePayload = await loadPayloadProto(); const NoiseHandshakePayload = await loadPayloadProto();
const earlyDataPayload = signedEarlyData ? const earlyDataPayload = earlyData ?
{ {
libp2pData: signedEarlyData.libp2pData, data: earlyData,
libp2pDataSignature: signedEarlyData.libp2pDataSignature,
} : {}; } : {};
const payloadInit = NoiseHandshakePayload.create({ const payloadInit = NoiseHandshakePayload.create({
libp2pKey: libp2pPublicKey, identityKey: libp2pPublicKey,
noiseStaticKeySignature: signedPayload, identitySig: signedPayload,
...earlyDataPayload, ...earlyDataPayload,
}); });
@ -64,25 +63,8 @@ export async function signPayload(peerId: PeerId, payload: bytes): Promise<bytes
return peerId.privKey.sign(payload); return peerId.privKey.sign(payload);
} }
type EarlyDataPayload = {
libp2pData: bytes;
libp2pDataSignature: bytes;
}
export async function signEarlyDataPayload(peerId: PeerId, earlyData: bytes): Promise<EarlyDataPayload> {
const payload = getEarlyDataPayload(earlyData);
const signedPayload = await signPayload(peerId, payload);
return {
libp2pData: payload,
libp2pDataSignature: signedPayload,
}
}
export const getHandshakePayload = (publicKey: bytes ) => Buffer.concat([Buffer.from("noise-libp2p-static-key:"), publicKey]); export const getHandshakePayload = (publicKey: bytes ) => Buffer.concat([Buffer.from("noise-libp2p-static-key:"), publicKey]);
export const getEarlyDataPayload = (earlyData: bytes) => Buffer.concat([Buffer.from("noise-libp2p-early-data:"), earlyData]);
async function isValidPeerId(peerId: bytes, publicKeyProtobuf: bytes) { async function isValidPeerId(peerId: bytes, publicKeyProtobuf: bytes) {
const generatedPeerId = await PeerId.createFromPubKey(publicKeyProtobuf); const generatedPeerId = await PeerId.createFromPubKey(publicKeyProtobuf);
return generatedPeerId.id.equals(peerId); return generatedPeerId.id.equals(peerId);
@ -103,15 +85,15 @@ export async function verifySignedPayload(noiseStaticKey: bytes, plaintext: byte
throw new Error("Failed to decode received payload. Reason: " + e.message); throw new Error("Failed to decode received payload. Reason: " + e.message);
} }
if (!(await isValidPeerId(peerId, receivedPayload.libp2pKey)) ) { if (!(await isValidPeerId(peerId, receivedPayload.identityKey)) ) {
throw new Error("Peer ID doesn't match libp2p public key."); throw new Error("Peer ID doesn't match libp2p public key.");
} }
const generatedPayload = getHandshakePayload(noiseStaticKey); const generatedPayload = getHandshakePayload(noiseStaticKey);
// Unmarshaling from PublicKey protobuf // Unmarshaling from PublicKey protobuf
const publicKey = crypto.keys.unmarshalPublicKey(receivedPayload.libp2pKey); const publicKey = crypto.keys.unmarshalPublicKey(receivedPayload.identityKey);
if (!publicKey.verify(generatedPayload, receivedPayload.noiseStaticKeySignature)) { if (!publicKey.verify(generatedPayload, receivedPayload.identitySig)) {
throw new Error("Static key doesn't match to peer that signed payload!"); throw new Error("Static key doesn't match to peer that signed payload!");
} }
} }