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
commit e97453490c
No known key found for this signature in database
GPG Key ID: 834D07135E110DA5
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,
): Promise<bytes> {
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(
localPeer.marshalPubKey(),
signedPayload,
signedEarlyDataPayload
earlyDataPayload
);
}
export async function createHandshakePayload(
libp2pPublicKey: bytes,
signedPayload: bytes,
signedEarlyData?: EarlyDataPayload,
earlyData?: bytes,
): Promise<bytes> {
const NoiseHandshakePayload = await loadPayloadProto();
const earlyDataPayload = signedEarlyData ?
const earlyDataPayload = earlyData ?
{
libp2pData: signedEarlyData.libp2pData,
libp2pDataSignature: signedEarlyData.libp2pDataSignature,
data: earlyData,
} : {};
const payloadInit = NoiseHandshakePayload.create({
libp2pKey: libp2pPublicKey,
noiseStaticKeySignature: signedPayload,
identityKey: libp2pPublicKey,
identitySig: signedPayload,
...earlyDataPayload,
});
@ -64,25 +63,8 @@ export async function signPayload(peerId: PeerId, payload: bytes): Promise<bytes
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 getEarlyDataPayload = (earlyData: bytes) => Buffer.concat([Buffer.from("noise-libp2p-early-data:"), earlyData]);
async function isValidPeerId(peerId: bytes, publicKeyProtobuf: bytes) {
const generatedPeerId = await PeerId.createFromPubKey(publicKeyProtobuf);
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);
}
if (!(await isValidPeerId(peerId, receivedPayload.libp2pKey)) ) {
if (!(await isValidPeerId(peerId, receivedPayload.identityKey)) ) {
throw new Error("Peer ID doesn't match libp2p public key.");
}
const generatedPayload = getHandshakePayload(noiseStaticKey);
// Unmarshaling from PublicKey protobuf
const publicKey = crypto.keys.unmarshalPublicKey(receivedPayload.libp2pKey);
if (!publicKey.verify(generatedPayload, receivedPayload.noiseStaticKeySignature)) {
const publicKey = crypto.keys.unmarshalPublicKey(receivedPayload.identityKey);
if (!publicKey.verify(generatedPayload, receivedPayload.identitySig)) {
throw new Error("Static key doesn't match to peer that signed payload!");
}
}