From 61d5674af113bcb783f8039f3c08abc7de73a291 Mon Sep 17 00:00:00 2001 From: Matija Petrunic Date: Tue, 14 Apr 2020 15:10:48 +0200 Subject: [PATCH] Replace log statements with log functions --- src/handshake-ik.ts | 28 ++++++++---------------- src/handshake-xx-fallback.ts | 15 ++++++------- src/handshake-xx.ts | 27 ++++++++---------------- src/logger.ts | 41 ++++++++++++++++++++++++++++++++++-- 4 files changed, 63 insertions(+), 48 deletions(-) diff --git a/src/handshake-ik.ts b/src/handshake-ik.ts index cba1d13..d482053 100644 --- a/src/handshake-ik.ts +++ b/src/handshake-ik.ts @@ -8,7 +8,7 @@ import {Buffer} from "buffer"; import {decode0, decode1, encode0, encode1} from "./encoder"; import {decodePayload, getPeerIdFromPayload, verifySignedPayload} from "./utils"; import {FailedIKError} from "./errors"; -import {logger, sessionKeyLogger} from "./logger"; +import {logger, logLocalStaticKeys, logRemoteStaticKey, logLocalEphemeralKeys, logRemoteEphemeralKey, logSymmetricCipherState, logCipherState} from "./logger"; import PeerId from "peer-id"; export class IKHandshake implements IHandshake { @@ -45,18 +45,14 @@ export class IKHandshake implements IHandshake { } public async stage0(): Promise { - sessionKeyLogger(`LOCAL_STATIC_PUBLIC_KEY ${this.session.hs.s.publicKey.toString('hex')}`) - sessionKeyLogger(`LOCAL_STATIC_PRIVATE_KEY ${this.session.hs.s.privateKey.toString('hex')}`) - sessionKeyLogger(`REMOTE_STATIC_PUBLIC_KEY ${this.session.hs.rs.toString('hex')}`) + logLocalStaticKeys(this.session.hs.s) + logRemoteStaticKey(this.session.hs.re) if (this.isInitiator) { logger("IK Stage 0 - Initiator sending message..."); const messageBuffer = this.ik.sendMessage(this.session, this.payload); this.connection.writeLP(encode1(messageBuffer)); logger("IK Stage 0 - Initiator sent message."); - if(this.session.hs.e){ - sessionKeyLogger(`LOCAL_PUBLIC_EPHEMERAL_KEY ${this.session.hs.e.publicKey.toString('hex')}`) - sessionKeyLogger(`LOCAL_PRIVATE_EPHEMERAL_KEY ${this.session.hs.e.privateKey.toString('hex')}`) - } + logLocalEphemeralKeys(this.session.hs.e) } else { logger("IK Stage 0 - Responder receiving message..."); const receivedMsg = await this.connection.readLP(); @@ -71,14 +67,14 @@ export class IKHandshake implements IHandshake { this.remotePeer = this.remotePeer || await getPeerIdFromPayload(decodedPayload); await verifySignedPayload(this.session.hs.rs, decodedPayload, this.remotePeer); logger("IK Stage 0 - Responder successfully verified payload!"); - sessionKeyLogger(`REMOTE_EPHEMEREAL_KEY ${this.session.hs.re.toString('hex')}`) + logRemoteEphemeralKey(this.session.hs.re) } catch (e) { logger("Responder breaking up with IK handshake in stage 0."); throw new FailedIKError(receivedMsg, `Error occurred while verifying initiator's signed payload: ${e.message}`); } } - sessionKeyLogger(`SYMMETRIC_CIPHER_STATE ${this.session.hs.ss.cs.n} ${this.session.hs.ss.cs.k.toString('hex')}`) + logSymmetricCipherState(this.session.hs.ss) } public async stage1(): Promise { @@ -96,7 +92,7 @@ export class IKHandshake implements IHandshake { this.remotePeer = this.remotePeer || await getPeerIdFromPayload(decodedPayload); await verifySignedPayload(receivedMessageBuffer.ns.slice(0, 32), decodedPayload, this.remotePeer); logger("IK Stage 1 - Initiator successfully verified payload!"); - sessionKeyLogger(`REMOTE_EPHEMERAL_KEY ${this.session.hs.re.toString('hex')}`) + logRemoteEphemeralKey(this.session.hs.re) } catch (e) { logger("Initiator breaking up with IK handshake in stage 1."); throw new FailedIKError(receivedMsg, `Error occurred while verifying responder's signed payload: ${e.message}`); @@ -106,15 +102,9 @@ export class IKHandshake implements IHandshake { const messageBuffer = this.ik.sendMessage(this.session, this.payload); this.connection.writeLP(encode0(messageBuffer)); logger("IK Stage 1 - Responder sent message..."); - if(this.session.hs.e){ - sessionKeyLogger(`LOCAL_PUBLIC_EPHEMERAL_KEY ${this.session.hs.e.publicKey.toString('hex')}`) - sessionKeyLogger(`LOCAL_PRIVATE_EPHEMERAL_KEY ${this.session.hs.e.privateKey.toString('hex')}`) - } - } - if(this.session.cs1 && this.session.cs2){ - sessionKeyLogger(`CIPHER_STATE_1 ${this.session.cs1.n} ${this.session.cs1.k.toString('hex')}`) - sessionKeyLogger(`CIPHER_STATE_2 ${this.session.cs2.n} ${this.session.cs2.k.toString('hex')}`) + logLocalEphemeralKeys(this.session.hs.e) } + logCipherState(this.session) } public decrypt(ciphertext: bytes, session: NoiseSession): {plaintext: bytes; valid: boolean} { diff --git a/src/handshake-xx-fallback.ts b/src/handshake-xx-fallback.ts index ad7a23b..f56df5c 100644 --- a/src/handshake-xx-fallback.ts +++ b/src/handshake-xx-fallback.ts @@ -3,8 +3,8 @@ import {XXHandshake} from "./handshake-xx"; import {XX} from "./handshakes/xx"; import {KeyPair} from "./@types/libp2p"; import {bytes, bytes32} from "./@types/basic"; -import {decodePayload, getPeerIdFromPayload, verifySignedPayload,} from "./utils"; -import {logger, sessionKeyLogger} from "./logger"; +import {decodePayload, getPeerIdFromPayload, verifySignedPayload} from "./utils"; +import {logger, logLocalEphemeralKeys, logRemoteEphemeralKey, logRemoteStaticKey} from "./logger"; import {WrappedConnection} from "./noise"; import {decode0, decode1} from "./encoder"; import PeerId from "peer-id"; @@ -35,11 +35,8 @@ export class XXFallbackHandshake extends XXHandshake { public async propose(): Promise { if (this.isInitiator) { this.xx.sendMessage(this.session, Buffer.alloc(0), this.ephemeralKeys); - if(this.session.hs.e){ - sessionKeyLogger(`LOCAL_PUBLIC_EPHEMERAL_KEY ${this.session.hs.e.publicKey.toString('hex')}`) - sessionKeyLogger(`LOCAL_PRIVATE_EPHEMERAL_KEY ${this.session.hs.e.privateKey.toString('hex')}`) - } logger("XX Fallback Stage 0 - Initialized state as the first message was sent by initiator."); + logLocalEphemeralKeys(this.session.hs.e) } else { logger("XX Fallback Stage 0 - Responder decoding initial msg from IK."); const receivedMessageBuffer = decode0(this.initialMsg); @@ -52,7 +49,7 @@ export class XXFallbackHandshake extends XXHandshake { throw new Error("xx fallback stage 0 decryption validation fail"); } logger("XX Fallback Stage 0 - Responder used received message from IK."); - sessionKeyLogger(`REMOTE_EPHEMEREAL_KEY ${this.session.hs.re.toString('hex')}`) + logRemoteEphemeralKey(this.session.hs.re) } } @@ -65,8 +62,8 @@ export class XXFallbackHandshake extends XXHandshake { throw new Error("xx fallback stage 1 decryption validation fail"); } logger('XX Fallback Stage 1 - Initiator used received message from IK.'); - sessionKeyLogger(`REMOTE_EPHEMEREAL_KEY ${this.session.hs.re.toString('hex')}`) - sessionKeyLogger(`REMOTE_STATIC_KEY ${this.session.hs.rs.toString('hex')}`) + logRemoteEphemeralKey(this.session.hs.re) + logRemoteStaticKey(this.session.hs.rs) logger("Initiator going to check remote's signature..."); try { diff --git a/src/handshake-xx.ts b/src/handshake-xx.ts index 0f7ebe3..20d0408 100644 --- a/src/handshake-xx.ts +++ b/src/handshake-xx.ts @@ -10,7 +10,7 @@ import { getPeerIdFromPayload, verifySignedPayload, } from "./utils"; -import { logger, sessionKeyLogger } from "./logger"; +import { logger, logLocalStaticKeys, logLocalEphemeralKeys, logRemoteEphemeralKey, logRemoteStaticKey, logCipherState, logSymmetricCipherState } from "./logger"; import {decode0, decode1, decode2, encode0, encode1, encode2} from "./encoder"; import { WrappedConnection } from "./noise"; import PeerId from "peer-id"; @@ -50,17 +50,13 @@ export class XXHandshake implements IHandshake { // stage 0 public async propose(): Promise { - sessionKeyLogger(`LOCAL_STATIC_PUBLIC_KEY ${this.session.hs.s.publicKey.toString('hex')}`) - sessionKeyLogger(`LOCAL_STATIC_PRIVATE_KEY ${this.session.hs.s.privateKey.toString('hex')}`) + logLocalStaticKeys(this.session.hs.s) if (this.isInitiator) { logger("Stage 0 - Initiator starting to send first message."); const messageBuffer = this.xx.sendMessage(this.session, Buffer.alloc(0)); this.connection.writeLP(encode0(messageBuffer)); logger("Stage 0 - Initiator finished sending first message."); - if(this.session.hs.e){ - sessionKeyLogger(`LOCAL_PUBLIC_EPHEMERAL_KEY ${this.session.hs.e.publicKey.toString('hex')}`) - sessionKeyLogger(`LOCAL_PRIVATE_EPHEMERAL_KEY ${this.session.hs.e.privateKey.toString('hex')}`) - } + logLocalEphemeralKeys(this.session.hs.e) } else { logger("Stage 0 - Responder waiting to receive first message..."); const receivedMessageBuffer = decode0((await this.connection.readLP()).slice()); @@ -69,7 +65,7 @@ export class XXHandshake implements IHandshake { throw new Error("xx handshake stage 0 validation fail"); } logger("Stage 0 - Responder received first message."); - sessionKeyLogger(`REMOTE_EPHEMEREAL_KEY ${this.session.hs.re.toString('hex')}`) + logRemoteEphemeralKey(this.session.hs.re) } } @@ -83,8 +79,8 @@ export class XXHandshake implements IHandshake { throw new Error("xx handshake stage 1 validation fail"); } logger('Stage 1 - Initiator received the message.'); - sessionKeyLogger(`REMOTE_EPHEMEREAL_KEY ${this.session.hs.re.toString('hex')}`) - sessionKeyLogger(`REMOTE_STATIC_KEY ${this.session.hs.rs.toString('hex')}`) + logRemoteEphemeralKey(this.session.hs.re) + logRemoteStaticKey(this.session.hs.rs) logger("Initiator going to check remote's signature..."); try { @@ -100,10 +96,7 @@ export class XXHandshake implements IHandshake { const messageBuffer = this.xx.sendMessage(this.session, this.payload); this.connection.writeLP(encode1(messageBuffer)); logger('Stage 1 - Responder sent the second handshake message with signed payload.') - if(this.session.hs.e){ - sessionKeyLogger(`LOCAL_PUBLIC_EPHEMERAL_KEY ${this.session.hs.e.publicKey.toString('hex')}`) - sessionKeyLogger(`LOCAL_PRIVATE_EPHEMERAL_KEY ${this.session.hs.e.privateKey.toString('hex')}`) - } + logLocalEphemeralKeys(this.session.hs.e) } } @@ -131,10 +124,8 @@ export class XXHandshake implements IHandshake { throw new Error(`Error occurred while verifying signed payload: ${e.message}`); } } - if(this.session.cs1 && this.session.cs2){ - sessionKeyLogger(`CIPHER_STATE_1 ${this.session.cs1.n} ${this.session.cs1.k.toString('hex')}`) - sessionKeyLogger(`CIPHER_STATE_2 ${this.session.cs2.n} ${this.session.cs2.k.toString('hex')}`) - } + logSymmetricCipherState(this.session.hs.ss) + logCipherState(this.session) } public encrypt(plaintext: bytes, session: NoiseSession): bytes { diff --git a/src/logger.ts b/src/logger.ts index 01b7cea..fe8197a 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -1,6 +1,7 @@ import debug from "debug"; import {DUMP_SESSION_KEYS} from './constants'; - +import { KeyPair } from "./@types/libp2p"; +import { NoiseSession, SymmetricState } from "./@types/handshake"; let keyLogger; if(DUMP_SESSION_KEYS){ @@ -10,5 +11,41 @@ else{ keyLogger = () => {} } -export const sessionKeyLogger = keyLogger; +export function logLocalStaticKeys(s: KeyPair): void { + keyLogger(`LOCAL_STATIC_PUBLIC_KEY ${s.publicKey.toString('hex')}`) + keyLogger(`LOCAL_STATIC_PRIVATE_KEY ${s.privateKey.toString('hex')}`) +} + +export function logLocalEphemeralKeys(e: KeyPair|undefined): void { + if(e){ + keyLogger(`LOCAL_PUBLIC_EPHEMERAL_KEY ${e.publicKey.toString('hex')}`) + keyLogger(`LOCAL_PRIVATE_EPHEMERAL_KEY ${e.privateKey.toString('hex')}`) + } + else{ + keyLogger('Missing local ephemeral keys.') + } +} + +export function logRemoteStaticKey(rs: Buffer): void { + keyLogger(`REMOTE_STATIC_PUBLIC_KEY ${rs.toString('hex')}`) +} + +export function logRemoteEphemeralKey(re: Buffer): void { + keyLogger(`REMOTE_EPHEMERAL_PUBLIC_KEY ${re.toString('hex')}`) +} + +export function logCipherState(session: NoiseSession): void { + if(session.cs1 && session.cs2){ + keyLogger(`CIPHER_STATE_1 ${session.cs1.n} ${session.cs1.k.toString('hex')}`) + keyLogger(`CIPHER_STATE_2 ${session.cs2.n} ${session.cs2.k.toString('hex')}`) + } + else{ + keyLogger('Missing cipher state.') + } +} + +export function logSymmetricCipherState(ss: SymmetricState): void { + keyLogger(`SYMMETRIC_CIPHER_STATE ${ss.cs.n} ${ss.cs.k.toString('hex')}`) +} + export const logger = debug('libp2p:noise'); \ No newline at end of file