-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Endpoint): Implement creation of channel with private peer (#565)
`Channel`s are now initialised with `Node` and `Peer` objects.
- Loading branch information
Showing
36 changed files
with
895 additions
and
246 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
import { addDays } from 'date-fns'; | ||
|
||
import { CertificationPath } from '../pki/CertificationPath'; | ||
import { MockKeyStoreSet } from '../keyStores/testMocks'; | ||
import { issueEndpointCertificate, issueGatewayCertificate } from '../pki/issuance'; | ||
import { getIdFromIdentityKey } from '../crypto/keys/digest'; | ||
import { generateRSAKeyPair } from '../crypto/keys/generation'; | ||
import { Certificate } from '../crypto/x509/Certificate'; | ||
import { reSerializeCertificate } from '../_test_utils'; | ||
import { StubNodeChannel } from './channels/_test_utils'; | ||
import { Endpoint } from './Endpoint'; | ||
import { PrivateEndpointConnParams } from './PrivateEndpointConnParams'; | ||
import { SessionKeyPair } from '../SessionKeyPair'; | ||
import { derSerializePublicKey } from '../crypto/keys/serialisation'; | ||
import { SessionPublicKeyData } from '../keyStores/PublicKeyStore'; | ||
import { InvalidNodeConnectionParams } from './errors'; | ||
import { Peer } from './peer'; | ||
|
||
const INTERNET_ADDRESS = 'example.com'; | ||
|
||
let peerId: string; | ||
let peerIdentityKeyPair: CryptoKeyPair; | ||
let peerCertificate: Certificate; | ||
beforeAll(async () => { | ||
peerIdentityKeyPair = await generateRSAKeyPair(); | ||
peerId = await getIdFromIdentityKey(peerIdentityKeyPair.publicKey); | ||
peerCertificate = await issueEndpointCertificate({ | ||
issuerPrivateKey: peerIdentityKeyPair.privateKey, | ||
subjectPublicKey: peerIdentityKeyPair.publicKey, | ||
validityEndDate: addDays(new Date(), 1), | ||
}); | ||
}); | ||
|
||
let nodeId: string; | ||
let nodeKeyPair: CryptoKeyPair; | ||
let nodeCertificate: Certificate; | ||
beforeAll(async () => { | ||
nodeKeyPair = await generateRSAKeyPair(); | ||
nodeCertificate = reSerializeCertificate( | ||
await issueGatewayCertificate({ | ||
issuerCertificate: peerCertificate, | ||
issuerPrivateKey: peerIdentityKeyPair.privateKey, | ||
subjectPublicKey: nodeKeyPair.publicKey, | ||
validityEndDate: peerCertificate.expiryDate, | ||
}), | ||
); | ||
nodeId = await getIdFromIdentityKey(nodeKeyPair.publicKey); | ||
}); | ||
|
||
const KEY_STORES = new MockKeyStoreSet(); | ||
beforeEach(async () => { | ||
KEY_STORES.clear(); | ||
}); | ||
|
||
describe('savePrivateEndpointChannel', () => { | ||
let connectionParams: PrivateEndpointConnParams; | ||
beforeAll(async () => { | ||
const deliveryAuth = new CertificationPath(nodeCertificate, [peerCertificate]); | ||
connectionParams = new PrivateEndpointConnParams( | ||
peerIdentityKeyPair.publicKey, | ||
INTERNET_ADDRESS, | ||
deliveryAuth, | ||
); | ||
}); | ||
|
||
test('Delivery authorization should be refused if granted to other node', async () => { | ||
const node = new StubEndpoint(`not-${nodeId}`, nodeKeyPair, KEY_STORES, {}); | ||
|
||
await expect(node.savePrivateEndpointChannel(connectionParams)).rejects.toThrowWithMessage( | ||
InvalidNodeConnectionParams, | ||
`Delivery authorization was granted to another node (${nodeId})`, | ||
); | ||
}); | ||
|
||
test('Delivery authorization should be stored if valid', async () => { | ||
const node = new StubEndpoint(nodeId, nodeKeyPair, KEY_STORES, {}); | ||
|
||
await node.savePrivateEndpointChannel(connectionParams); | ||
|
||
const deliveryAuthorisations = await KEY_STORES.certificateStore.retrieveAll(nodeId, peerId); | ||
expect(deliveryAuthorisations).toHaveLength(1); | ||
const [deliveryAuthorisation] = deliveryAuthorisations; | ||
expect(Buffer.from(deliveryAuthorisation.serialize())).toStrictEqual( | ||
Buffer.from(connectionParams.deliveryAuth.serialize()), | ||
); | ||
}); | ||
|
||
test('Identity public key of peer should be stored', async () => { | ||
const node = new StubEndpoint(nodeId, nodeKeyPair, KEY_STORES, {}); | ||
|
||
await node.savePrivateEndpointChannel(connectionParams); | ||
|
||
expect(KEY_STORES.publicKeyStore.identityKeys).toHaveProperty( | ||
peerId, | ||
await derSerializePublicKey(peerIdentityKeyPair.publicKey), | ||
); | ||
}); | ||
|
||
test('Session public key of peer should be stored if set', async () => { | ||
const node = new StubEndpoint(nodeId, nodeKeyPair, KEY_STORES, {}); | ||
const dateBeforeSave = new Date(); | ||
const { sessionKey } = await SessionKeyPair.generate(); | ||
const paramsWithSessionKey = new PrivateEndpointConnParams( | ||
connectionParams.identityKey, | ||
connectionParams.internetGatewayAddress, | ||
connectionParams.deliveryAuth, | ||
sessionKey, | ||
); | ||
|
||
await node.savePrivateEndpointChannel(paramsWithSessionKey); | ||
|
||
expect(KEY_STORES.publicKeyStore.sessionKeys).toHaveProperty( | ||
peerId, | ||
expect.objectContaining<SessionPublicKeyData>({ | ||
publicKeyId: sessionKey.keyId, | ||
publicKeyDer: await derSerializePublicKey(sessionKey.publicKey), | ||
publicKeyCreationTime: expect.toSatisfy<Date>( | ||
(date) => date <= new Date() && dateBeforeSave <= date, | ||
), | ||
}), | ||
); | ||
}); | ||
|
||
test('Resulting channel should be output', async () => { | ||
const node = new StubEndpoint(nodeId, nodeKeyPair, KEY_STORES, {}); | ||
|
||
const channel = await node.savePrivateEndpointChannel(connectionParams); | ||
|
||
expect(channel.node).toBe(node); | ||
expect(channel.peer).toMatchObject<Peer<string>>({ | ||
id: peerId, | ||
identityPublicKey: peerIdentityKeyPair.publicKey, | ||
internetAddress: INTERNET_ADDRESS, | ||
}); | ||
expect(channel.deliveryAuthPath).toBe(connectionParams.deliveryAuth); | ||
}); | ||
|
||
test('Key store should be propagated', async () => { | ||
const node = new StubEndpoint(nodeId, nodeKeyPair, KEY_STORES, {}); | ||
|
||
const channel = await node.savePrivateEndpointChannel(connectionParams); | ||
|
||
expect(channel.keyStores).toBe(KEY_STORES); | ||
}); | ||
|
||
test('Crypto options should be propagated', async () => { | ||
const node = new StubEndpoint(nodeId, nodeKeyPair, KEY_STORES, { | ||
encryption: { aesKeySize: 128 }, | ||
}); | ||
|
||
const channel = await node.savePrivateEndpointChannel(connectionParams); | ||
|
||
expect(channel.cryptoOptions).toBe(node.cryptoOptions); | ||
}); | ||
}); | ||
|
||
export class StubEndpoint extends Endpoint<undefined> { | ||
protected readonly channelConstructor = StubNodeChannel; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,51 @@ | ||
import { ServiceMessage } from '../messages/payloads/ServiceMessage'; | ||
import { Node } from './Node'; | ||
import { Peer, PeerInternetAddress } from './peer'; | ||
import { Channel } from './channels/Channel'; | ||
import { PrivateEndpointConnParams } from './PrivateEndpointConnParams'; | ||
import { InvalidNodeConnectionParams } from './errors'; | ||
import { getIdFromIdentityKey } from '../crypto/keys/digest'; | ||
|
||
export class Endpoint extends Node<ServiceMessage> {} | ||
export abstract class Endpoint<PeerAddress extends PeerInternetAddress> extends Node< | ||
ServiceMessage, | ||
PeerAddress | ||
> { | ||
/** | ||
* Create or update a channel with a private endpoint. | ||
*/ | ||
public async savePrivateEndpointChannel( | ||
connectionParams: PrivateEndpointConnParams, | ||
): Promise<Channel<ServiceMessage, PeerAddress>> { | ||
const authSubjectId = await connectionParams.deliveryAuth.leafCertificate.calculateSubjectId(); | ||
if (authSubjectId !== this.id) { | ||
throw new InvalidNodeConnectionParams( | ||
`Delivery authorization was granted to another node (${authSubjectId})`, | ||
); | ||
} | ||
|
||
const peer: Peer<string> = { | ||
id: await getIdFromIdentityKey(connectionParams.identityKey), | ||
internetAddress: connectionParams.internetGatewayAddress, | ||
identityPublicKey: connectionParams.identityKey, | ||
}; | ||
|
||
await this.keyStores.certificateStore.save(connectionParams.deliveryAuth, peer.id); | ||
await this.keyStores.publicKeyStore.saveIdentityKey(peer.identityPublicKey); | ||
|
||
if (connectionParams.sessionKey) { | ||
await this.keyStores.publicKeyStore.saveSessionKey( | ||
connectionParams.sessionKey, | ||
peer.id, | ||
new Date(), | ||
); | ||
} | ||
|
||
return new this.channelConstructor( | ||
this, | ||
peer, | ||
connectionParams.deliveryAuth, | ||
this.keyStores, | ||
this.cryptoOptions, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.