-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathhandshake-ik.ts
158 lines (145 loc) · 5.92 KB
/
handshake-ik.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import type { PeerId } from '@libp2p/interface-peer-id'
import type { ProtobufStream } from 'it-pb-stream'
import type { CipherState, NoiseSession } from './@types/handshake.js'
import type { bytes, bytes32 } from './@types/basic.js'
import type { KeyPair } from './@types/libp2p.js'
import type { IHandshake } from './@types/handshake-interface.js'
import type { ICryptoInterface } from './crypto.js'
import { IK } from './handshakes/ik.js'
import { decode0, decode1, encode0, encode1 } from './encoder.js'
import { FailedIKError } from './errors.js'
import {
logger,
logLocalStaticKeys,
logRemoteStaticKey,
logLocalEphemeralKeys,
logRemoteEphemeralKey,
logCipherState
} from './logger.js'
import { decodePayload, getPeerIdFromPayload, verifySignedPayload } from './utils.js'
export class IKHandshake implements IHandshake {
public isInitiator: boolean
public session: NoiseSession
public remotePeer!: PeerId
public remoteEarlyData: Uint8Array
private readonly payload: bytes
private readonly prologue: bytes32
private readonly staticKeypair: KeyPair
private readonly connection: ProtobufStream
private readonly ik: IK
constructor (
isInitiator: boolean,
payload: bytes,
prologue: bytes32,
crypto: ICryptoInterface,
staticKeypair: KeyPair,
connection: ProtobufStream,
remoteStaticKey: bytes,
remotePeer?: PeerId,
handshake?: IK
) {
this.isInitiator = isInitiator
this.payload = payload
this.prologue = prologue
this.staticKeypair = staticKeypair
this.connection = connection
if (remotePeer) {
this.remotePeer = remotePeer
}
this.ik = handshake ?? new IK(crypto)
this.session = this.ik.initSession(this.isInitiator, this.prologue, this.staticKeypair, remoteStaticKey)
this.remoteEarlyData = new Uint8Array()
}
public async stage0 (): Promise<void> {
logLocalStaticKeys(this.session.hs.s)
logRemoteStaticKey(this.session.hs.rs)
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.')
logLocalEphemeralKeys(this.session.hs.e)
} else {
logger('IK Stage 0 - Responder receiving message...')
const receivedMsg = await this.connection.readLP()
try {
const receivedMessageBuffer = decode1(receivedMsg.slice())
const { plaintext, valid } = this.ik.recvMessage(this.session, receivedMessageBuffer)
if (!valid) {
throw new Error('ik handshake stage 0 decryption validation fail')
}
logger('IK Stage 0 - Responder got message, going to verify payload.')
const decodedPayload = decodePayload(plaintext)
this.remotePeer = this.remotePeer || await getPeerIdFromPayload(decodedPayload)
await verifySignedPayload(this.session.hs.rs, decodedPayload, this.remotePeer)
this.setRemoteEarlyData(decodedPayload.data)
logger('IK Stage 0 - Responder successfully verified payload!')
logRemoteEphemeralKey(this.session.hs.re)
} catch (e) {
const err = e as Error
logger('Responder breaking up with IK handshake in stage 0.')
throw new FailedIKError(receivedMsg.slice(), `Error occurred while verifying initiator's signed payload: ${err.message}`)
}
}
}
public async stage1 (): Promise<void> {
if (this.isInitiator) {
logger('IK Stage 1 - Initiator receiving message...')
const receivedMsg = (await this.connection.readLP()).slice()
const receivedMessageBuffer = decode0(receivedMsg)
const { plaintext, valid } = this.ik.recvMessage(this.session, receivedMessageBuffer)
logger('IK Stage 1 - Initiator got message, going to verify payload.')
try {
if (!valid) {
throw new Error('ik stage 1 decryption validation fail')
}
const decodedPayload = decodePayload(plaintext)
this.remotePeer = this.remotePeer || await getPeerIdFromPayload(decodedPayload)
await verifySignedPayload(receivedMessageBuffer.ns.slice(0, 32), decodedPayload, this.remotePeer)
this.setRemoteEarlyData(decodedPayload.data)
logger('IK Stage 1 - Initiator successfully verified payload!')
logRemoteEphemeralKey(this.session.hs.re)
} catch (e) {
const err = e as Error
logger('Initiator breaking up with IK handshake in stage 1.')
throw new FailedIKError(receivedMsg, `Error occurred while verifying responder's signed payload: ${err.message}`)
}
} else {
logger('IK Stage 1 - Responder sending message...')
const messageBuffer = this.ik.sendMessage(this.session, this.payload)
this.connection.writeLP(encode0(messageBuffer))
logger('IK Stage 1 - Responder sent message...')
logLocalEphemeralKeys(this.session.hs.e)
}
logCipherState(this.session)
}
public decrypt (ciphertext: Uint8Array, session: NoiseSession): {plaintext: bytes, valid: boolean} {
const cs = this.getCS(session, false)
return this.ik.decryptWithAd(cs, new Uint8Array(0), ciphertext)
}
public encrypt (plaintext: Uint8Array, session: NoiseSession): bytes {
const cs = this.getCS(session)
return this.ik.encryptWithAd(cs, new Uint8Array(0), plaintext)
}
public getLocalEphemeralKeys (): KeyPair {
if (!this.session.hs.e) {
throw new Error('Ephemeral keys do not exist.')
}
return this.session.hs.e
}
private getCS (session: NoiseSession, encryption = true): CipherState {
if (!session.cs1 || !session.cs2) {
throw new Error('Handshake not completed properly, cipher state does not exist.')
}
if (this.isInitiator) {
return encryption ? session.cs1 : session.cs2
} else {
return encryption ? session.cs2 : session.cs1
}
}
private setRemoteEarlyData (data: Uint8Array|null|undefined): void {
if (data) {
this.remoteEarlyData = data
}
}
}