@libp2p/tls 1.1.5-e211b46cc → 2.0.0-1210884ed

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -34,7 +34,7 @@ import { tls } from '@libp2p/tls'
34
34
 
35
35
  const node = await createLibp2p({
36
36
  // ...other options
37
- connectionEncryption: [
37
+ connectionEncrypters: [
38
38
  tls()
39
39
  ]
40
40
  })
@@ -0,0 +1,13 @@
1
+ /**
2
+ * The handshake timed out
3
+ */
4
+ export declare class HandshakeTimeoutError extends Error {
5
+ constructor(message?: string);
6
+ }
7
+ /**
8
+ * The certificate was invalid
9
+ */
10
+ export declare class InvalidCertificateError extends Error {
11
+ constructor(message?: string);
12
+ }
13
+ //# sourceMappingURL=errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,qBAAa,qBAAsB,SAAQ,KAAK;gBACjC,OAAO,SAAsB;CAI3C;AAED;;GAEG;AACH,qBAAa,uBAAwB,SAAQ,KAAK;gBACnC,OAAO,SAAwB;CAI7C"}
@@ -0,0 +1,19 @@
1
+ /**
2
+ * The handshake timed out
3
+ */
4
+ export class HandshakeTimeoutError extends Error {
5
+ constructor(message = 'Handshake timeout') {
6
+ super(message);
7
+ this.name = 'HandshakeTimeoutError';
8
+ }
9
+ }
10
+ /**
11
+ * The certificate was invalid
12
+ */
13
+ export class InvalidCertificateError extends Error {
14
+ constructor(message = 'Invalid certificate') {
15
+ super(message);
16
+ this.name = 'InvalidCertificateError';
17
+ }
18
+ }
19
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,OAAO,qBAAsB,SAAQ,KAAK;IAC9C,YAAa,OAAO,GAAG,mBAAmB;QACxC,KAAK,CAAC,OAAO,CAAC,CAAA;QACd,IAAI,CAAC,IAAI,GAAG,uBAAuB,CAAA;IACrC,CAAC;CACF;AAED;;GAEG;AACH,MAAM,OAAO,uBAAwB,SAAQ,KAAK;IAChD,YAAa,OAAO,GAAG,qBAAqB;QAC1C,KAAK,CAAC,OAAO,CAAC,CAAA;QACd,IAAI,CAAC,IAAI,GAAG,yBAAyB,CAAA;IACvC,CAAC;CACF"}
@@ -11,23 +11,17 @@
11
11
  *
12
12
  * const node = await createLibp2p({
13
13
  * // ...other options
14
- * connectionEncryption: [
14
+ * connectionEncrypters: [
15
15
  * tls()
16
16
  * ]
17
17
  * })
18
18
  * ```
19
19
  */
20
- import type { ComponentLogger, ConnectionEncrypter } from '@libp2p/interface';
20
+ import type { ComponentLogger, ConnectionEncrypter, PrivateKey } from '@libp2p/interface';
21
21
  export declare const PROTOCOL = "/tls/1.0.0";
22
22
  export interface TLSComponents {
23
+ privateKey: PrivateKey;
23
24
  logger: ComponentLogger;
24
25
  }
25
- export interface TLSInit {
26
- /**
27
- * The peer id exchange must complete within this many milliseconds
28
- * (default: 1000)
29
- */
30
- timeout?: number;
31
- }
32
- export declare function tls(init?: TLSInit): (components: TLSComponents) => ConnectionEncrypter;
26
+ export declare function tls(): (components: TLSComponents) => ConnectionEncrypter;
33
27
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAGH,OAAO,KAAK,EAAE,eAAe,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAA;AAE7E,eAAO,MAAM,QAAQ,eAAe,CAAA;AAEpC,MAAM,WAAW,aAAa;IAC5B,MAAM,EAAE,eAAe,CAAA;CACxB;AAED,MAAM,WAAW,OAAO;IACtB;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,CAAA;CACjB;AAED,wBAAgB,GAAG,CAAE,IAAI,CAAC,EAAE,OAAO,GAAG,CAAC,UAAU,EAAE,aAAa,KAAK,mBAAmB,CAEvF"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAGH,OAAO,KAAK,EAAE,eAAe,EAAE,mBAAmB,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAA;AAEzF,eAAO,MAAM,QAAQ,eAAe,CAAA;AAEpC,MAAM,WAAW,aAAa;IAC5B,UAAU,EAAE,UAAU,CAAA;IACtB,MAAM,EAAE,eAAe,CAAA;CACxB;AAED,wBAAgB,GAAG,IAAK,CAAC,UAAU,EAAE,aAAa,KAAK,mBAAmB,CAEzE"}
package/dist/src/index.js CHANGED
@@ -11,7 +11,7 @@
11
11
  *
12
12
  * const node = await createLibp2p({
13
13
  * // ...other options
14
- * connectionEncryption: [
14
+ * connectionEncrypters: [
15
15
  * tls()
16
16
  * ]
17
17
  * })
@@ -19,7 +19,7 @@
19
19
  */
20
20
  import { TLS } from './tls.js';
21
21
  export const PROTOCOL = '/tls/1.0.0';
22
- export function tls(init) {
23
- return (components) => new TLS(components, init);
22
+ export function tls() {
23
+ return (components) => new TLS(components);
24
24
  }
25
25
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAA;AAG9B,MAAM,CAAC,MAAM,QAAQ,GAAG,YAAY,CAAA;AAcpC,MAAM,UAAU,GAAG,CAAE,IAAc;IACjC,OAAO,CAAC,UAAU,EAAE,EAAE,CAAC,IAAI,GAAG,CAAC,UAAU,EAAE,IAAI,CAAC,CAAA;AAClD,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAA;AAG9B,MAAM,CAAC,MAAM,QAAQ,GAAG,YAAY,CAAA;AAOpC,MAAM,UAAU,GAAG;IACjB,OAAO,CAAC,UAAU,EAAE,EAAE,CAAC,IAAI,GAAG,CAAC,UAAU,CAAC,CAAA;AAC5C,CAAC"}
@@ -1,9 +1,9 @@
1
- import { type Codec } from 'protons-runtime';
1
+ import { type Codec, type DecodeOptions } from 'protons-runtime';
2
2
  import type { Uint8ArrayList } from 'uint8arraylist';
3
3
  export declare enum KeyType {
4
4
  RSA = "RSA",
5
5
  Ed25519 = "Ed25519",
6
- Secp256k1 = "Secp256k1",
6
+ secp256k1 = "secp256k1",
7
7
  ECDSA = "ECDSA"
8
8
  }
9
9
  export declare namespace KeyType {
@@ -16,6 +16,6 @@ export interface PublicKey {
16
16
  export declare namespace PublicKey {
17
17
  const codec: () => Codec<PublicKey>;
18
18
  const encode: (obj: Partial<PublicKey>) => Uint8Array;
19
- const decode: (buf: Uint8Array | Uint8ArrayList) => PublicKey;
19
+ const decode: (buf: Uint8Array | Uint8ArrayList, opts?: DecodeOptions<PublicKey>) => PublicKey;
20
20
  }
21
21
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/pb/index.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,KAAK,KAAK,EAAsD,MAAM,iBAAiB,CAAA;AAChG,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAEpD,oBAAY,OAAO;IACjB,GAAG,QAAQ;IACX,OAAO,YAAY;IACnB,SAAS,cAAc;IACvB,KAAK,UAAU;CAChB;AASD,yBAAiB,OAAO,CAAC;IAChB,MAAM,KAAK,QAAO,MAAM,OAAO,CAErC,CAAA;CACF;AACD,MAAM,WAAW,SAAS;IACxB,IAAI,CAAC,EAAE,OAAO,CAAA;IACd,IAAI,CAAC,EAAE,UAAU,CAAA;CAClB;AAED,yBAAiB,SAAS,CAAC;IAGlB,MAAM,KAAK,QAAO,MAAM,SAAS,CAiDvC,CAAA;IAEM,MAAM,MAAM,QAAS,QAAQ,SAAS,CAAC,KAAG,UAEhD,CAAA;IAEM,MAAM,MAAM,QAAS,UAAU,GAAG,cAAc,KAAG,SAEzD,CAAA;CACF"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/pb/index.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,KAAK,KAAK,EAAiB,KAAK,aAAa,EAAuC,MAAM,iBAAiB,CAAA;AACpH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAEpD,oBAAY,OAAO;IACjB,GAAG,QAAQ;IACX,OAAO,YAAY;IACnB,SAAS,cAAc;IACvB,KAAK,UAAU;CAChB;AASD,yBAAiB,OAAO,CAAC;IAChB,MAAM,KAAK,QAAO,MAAM,OAAO,CAErC,CAAA;CACF;AACD,MAAM,WAAW,SAAS;IACxB,IAAI,CAAC,EAAE,OAAO,CAAA;IACd,IAAI,CAAC,EAAE,UAAU,CAAA;CAClB;AAED,yBAAiB,SAAS,CAAC;IAGlB,MAAM,KAAK,QAAO,MAAM,SAAS,CAiDvC,CAAA;IAEM,MAAM,MAAM,QAAS,QAAQ,SAAS,CAAC,KAAG,UAEhD,CAAA;IAEM,MAAM,MAAM,QAAS,UAAU,GAAG,cAAc,SAAS,cAAc,SAAS,CAAC,KAAG,SAE1F,CAAA;CACF"}
@@ -8,14 +8,14 @@ export var KeyType;
8
8
  (function (KeyType) {
9
9
  KeyType["RSA"] = "RSA";
10
10
  KeyType["Ed25519"] = "Ed25519";
11
- KeyType["Secp256k1"] = "Secp256k1";
11
+ KeyType["secp256k1"] = "secp256k1";
12
12
  KeyType["ECDSA"] = "ECDSA";
13
13
  })(KeyType || (KeyType = {}));
14
14
  var __KeyTypeValues;
15
15
  (function (__KeyTypeValues) {
16
16
  __KeyTypeValues[__KeyTypeValues["RSA"] = 0] = "RSA";
17
17
  __KeyTypeValues[__KeyTypeValues["Ed25519"] = 1] = "Ed25519";
18
- __KeyTypeValues[__KeyTypeValues["Secp256k1"] = 2] = "Secp256k1";
18
+ __KeyTypeValues[__KeyTypeValues["secp256k1"] = 2] = "secp256k1";
19
19
  __KeyTypeValues[__KeyTypeValues["ECDSA"] = 3] = "ECDSA";
20
20
  })(__KeyTypeValues || (__KeyTypeValues = {}));
21
21
  (function (KeyType) {
@@ -43,7 +43,7 @@ export var PublicKey;
43
43
  if (opts.lengthDelimited !== false) {
44
44
  w.ldelim();
45
45
  }
46
- }, (reader, length) => {
46
+ }, (reader, length, opts = {}) => {
47
47
  const obj = {};
48
48
  const end = length == null ? reader.len : reader.pos + length;
49
49
  while (reader.pos < end) {
@@ -71,8 +71,8 @@ export var PublicKey;
71
71
  PublicKey.encode = (obj) => {
72
72
  return encodeMessage(obj, PublicKey.codec());
73
73
  };
74
- PublicKey.decode = (buf) => {
75
- return decodeMessage(buf, PublicKey.codec());
74
+ PublicKey.decode = (buf, opts) => {
75
+ return decodeMessage(buf, PublicKey.codec(), opts);
76
76
  };
77
77
  })(PublicKey || (PublicKey = {}));
78
78
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/pb/index.ts"],"names":[],"mappings":"AAAA,kCAAkC;AAClC,+BAA+B;AAC/B,oDAAoD;AACpD,8EAA8E;AAC9E,0DAA0D;AAE1D,OAAO,EAAc,aAAa,EAAE,aAAa,EAAE,WAAW,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAA;AAGhG,MAAM,CAAN,IAAY,OAKX;AALD,WAAY,OAAO;IACjB,sBAAW,CAAA;IACX,8BAAmB,CAAA;IACnB,kCAAuB,CAAA;IACvB,0BAAe,CAAA;AACjB,CAAC,EALW,OAAO,KAAP,OAAO,QAKlB;AAED,IAAK,eAKJ;AALD,WAAK,eAAe;IAClB,mDAAO,CAAA;IAC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GAAgC,EAAE,IAA+B,EAAa,EAAE;QACrG,OAAO,aAAa,CAAC,GAAG,EAAE,SAAS,CAAC,KAAK,EAAE,EAAE,IAAI,CAAC,CAAA;IACpD,CAAC,CAAA;AACH,CAAC,EA7DgB,SAAS,KAAT,SAAS,QA6DzB"}
package/dist/src/tls.d.ts CHANGED
@@ -11,29 +11,29 @@
11
11
  *
12
12
  * const node = await createLibp2p({
13
13
  * // ...other options
14
- * connectionEncryption: [
14
+ * connectionEncrypters: [
15
15
  * tls()
16
16
  * ]
17
17
  * })
18
18
  * ```
19
19
  */
20
20
  import { serviceCapabilities } from '@libp2p/interface';
21
- import type { TLSComponents, TLSInit } from './index.js';
22
- import type { MultiaddrConnection, ConnectionEncrypter, SecuredConnection, PeerId } from '@libp2p/interface';
21
+ import type { TLSComponents } from './index.js';
22
+ import type { MultiaddrConnection, ConnectionEncrypter, SecuredConnection, SecureConnectionOptions } from '@libp2p/interface';
23
23
  import type { Duplex } from 'it-stream-types';
24
24
  import type { Uint8ArrayList } from 'uint8arraylist';
25
25
  export declare class TLS implements ConnectionEncrypter {
26
26
  protocol: string;
27
27
  private readonly log;
28
- private readonly timeout;
29
- constructor(components: TLSComponents, init?: TLSInit);
28
+ private readonly privateKey;
29
+ constructor(components: TLSComponents);
30
30
  readonly [Symbol.toStringTag] = "@libp2p/tls";
31
31
  readonly [serviceCapabilities]: string[];
32
- secureInbound<Stream extends Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> = MultiaddrConnection>(localId: PeerId, conn: Stream, remoteId?: PeerId): Promise<SecuredConnection<Stream>>;
33
- secureOutbound<Stream extends Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> = MultiaddrConnection>(localId: PeerId, conn: Stream, remoteId?: PeerId): Promise<SecuredConnection<Stream>>;
32
+ secureInbound<Stream extends Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> = MultiaddrConnection>(conn: Stream, options?: SecureConnectionOptions): Promise<SecuredConnection<Stream>>;
33
+ secureOutbound<Stream extends Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> = MultiaddrConnection>(conn: Stream, options?: SecureConnectionOptions): Promise<SecuredConnection<Stream>>;
34
34
  /**
35
35
  * Encrypt connection
36
36
  */
37
- _encrypt<Stream extends Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> = MultiaddrConnection>(localId: PeerId, conn: Stream, isServer: boolean, remoteId?: PeerId): Promise<SecuredConnection<Stream>>;
37
+ _encrypt<Stream extends Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> = MultiaddrConnection>(conn: Stream, isServer: boolean, options?: SecureConnectionOptions): Promise<SecuredConnection<Stream>>;
38
38
  }
39
39
  //# sourceMappingURL=tls.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"tls.d.ts","sourceRoot":"","sources":["../../src/tls.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAGH,OAAO,EAAa,mBAAmB,EAAE,MAAM,mBAAmB,CAAA;AAGlE,OAAO,KAAK,EAAE,aAAa,EAAE,OAAO,EAAE,MAAM,YAAY,CAAA;AACxD,OAAO,KAAK,EAAE,mBAAmB,EAAE,mBAAmB,EAAE,iBAAiB,EAAE,MAAM,EAAU,MAAM,mBAAmB,CAAA;AACpH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAA;AAC7C,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAEpD,qBAAa,GAAI,YAAW,mBAAmB;IACtC,QAAQ,EAAE,MAAM,CAAW;IAClC,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAQ;IAC5B,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAQ;gBAEnB,UAAU,EAAE,aAAa,EAAE,IAAI,GAAE,OAAY;IAK1D,QAAQ,CAAC,CAAC,MAAM,CAAC,WAAW,CAAC,iBAAgB;IAE7C,QAAQ,CAAC,CAAC,mBAAmB,CAAC,EAAE,MAAM,EAAE,CAEvC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;IAClC,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAQ;IAC5B,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAY;gBAE1B,UAAU,EAAE,aAAa;IAKtC,QAAQ,CAAC,CAAC,MAAM,CAAC,WAAW,CAAC,iBAAgB;IAE7C,QAAQ,CAAC,CAAC,mBAAmB,CAAC,EAAE,MAAM,EAAE,CAEvC;IAEK,aAAa,CAAE,MAAM,SAAS,MAAM,CAAC,cAAc,CAAC,UAAU,GAAG,cAAc,CAAC,CAAC,GAAG,mBAAmB,EAAG,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,uBAAuB,GAAG,OAAO,CAAC,iBAAiB,CAAC,MAAM,CAAC,CAAC;IAI9L,cAAc,CAAE,MAAM,SAAS,MAAM,CAAC,cAAc,CAAC,UAAU,GAAG,cAAc,CAAC,CAAC,GAAG,mBAAmB,EAAG,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,uBAAuB,GAAG,OAAO,CAAC,iBAAiB,CAAC,MAAM,CAAC,CAAC;IAIrM;;OAEG;IACG,QAAQ,CAAE,MAAM,SAAS,MAAM,CAAC,cAAc,CAAC,UAAU,GAAG,cAAc,CAAC,CAAC,GAAG,mBAAmB,EAAG,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,uBAAuB,GAAG,OAAO,CAAC,iBAAiB,CAAC,MAAM,CAAC,CAAC;CA4DnN"}
package/dist/src/tls.js CHANGED
@@ -11,40 +11,41 @@
11
11
  *
12
12
  * const node = await createLibp2p({
13
13
  * // ...other options
14
- * connectionEncryption: [
14
+ * connectionEncrypters: [
15
15
  * tls()
16
16
  * ]
17
17
  * })
18
18
  * ```
19
19
  */
20
20
  import { TLSSocket, connect } from 'node:tls';
21
- import { CodeError, serviceCapabilities } from '@libp2p/interface';
21
+ import { serviceCapabilities } from '@libp2p/interface';
22
+ import { HandshakeTimeoutError } from './errors.js';
22
23
  import { generateCertificate, verifyPeerCertificate, itToStream, streamToIt } from './utils.js';
23
24
  import { PROTOCOL } from './index.js';
24
25
  export class TLS {
25
26
  protocol = PROTOCOL;
26
27
  log;
27
- timeout;
28
- constructor(components, init = {}) {
28
+ privateKey;
29
+ constructor(components) {
29
30
  this.log = components.logger.forComponent('libp2p:tls');
30
- this.timeout = init.timeout ?? 1000;
31
+ this.privateKey = components.privateKey;
31
32
  }
32
33
  [Symbol.toStringTag] = '@libp2p/tls';
33
34
  [serviceCapabilities] = [
34
35
  '@libp2p/connection-encryption'
35
36
  ];
36
- async secureInbound(localId, conn, remoteId) {
37
- return this._encrypt(localId, conn, true, remoteId);
37
+ async secureInbound(conn, options) {
38
+ return this._encrypt(conn, true, options);
38
39
  }
39
- async secureOutbound(localId, conn, remoteId) {
40
- return this._encrypt(localId, conn, false, remoteId);
40
+ async secureOutbound(conn, options) {
41
+ return this._encrypt(conn, false, options);
41
42
  }
42
43
  /**
43
44
  * Encrypt connection
44
45
  */
45
- async _encrypt(localId, conn, isServer, remoteId) {
46
+ async _encrypt(conn, isServer, options) {
46
47
  const opts = {
47
- ...await generateCertificate(localId),
48
+ ...await generateCertificate(this.privateKey),
48
49
  isServer,
49
50
  // require TLS 1.3 or later
50
51
  minVersion: 'TLSv1.3',
@@ -67,12 +68,12 @@ export class TLS {
67
68
  });
68
69
  }
69
70
  return new Promise((resolve, reject) => {
70
- const abortTimeout = setTimeout(() => {
71
- socket.destroy(new CodeError('Handshake timeout', 'ERR_HANDSHAKE_TIMEOUT'));
72
- }, this.timeout);
71
+ options?.signal?.addEventListener('abort', () => {
72
+ socket.destroy(new HandshakeTimeoutError());
73
+ });
73
74
  const verifyRemote = () => {
74
75
  const remote = socket.getPeerCertificate();
75
- verifyPeerCertificate(remote.raw, remoteId, this.log)
76
+ verifyPeerCertificate(remote.raw, options?.remotePeer, this.log)
76
77
  .then(remotePeer => {
77
78
  this.log('remote certificate ok, remote peer %p', remotePeer);
78
79
  resolve({
@@ -85,14 +86,10 @@ export class TLS {
85
86
  })
86
87
  .catch((err) => {
87
88
  reject(err);
88
- })
89
- .finally(() => {
90
- clearTimeout(abortTimeout);
91
89
  });
92
90
  };
93
91
  socket.on('error', (err) => {
94
92
  reject(err);
95
- clearTimeout(abortTimeout);
96
93
  });
97
94
  socket.once('secure', (evt) => {
98
95
  this.log('verifying remote certificate');
@@ -1 +1 @@
1
- {"version":3,"file":"tls.js","sourceRoot":"","sources":["../../src/tls.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,OAAO,EAAE,SAAS,EAAyB,OAAO,EAAE,MAAM,UAAU,CAAA;AACpE,OAAO,EAAE,SAAS,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAA;AAClE,OAAO,EAAE,mBAAmB,EAAE,qBAAqB,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AAC/F,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AAMrC,MAAM,OAAO,GAAG;IACP,QAAQ,GAAW,QAAQ,CAAA;IACjB,GAAG,CAAQ;IACX,OAAO,CAAQ;IAEhC,YAAa,UAAyB,EAAE,OAAgB,EAAE;QACxD,IAAI,CAAC,GAAG,GAAG,UAAU,CAAC,MAAM,CAAC,YAAY,CAAC,YAAY,CAAC,CAAA;QACvD,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,OAAO,IAAI,IAAI,CAAA;IACrC,CAAC;IAEQ,CAAC,MAAM,CAAC,WAAW,CAAC,GAAG,aAAa,CAAA;IAEpC,CAAC,mBAAmB,CAAC,GAAa;QACzC,+BAA+B;KAChC,CAAA;IAED,KAAK,CAAC,aAAa,CAA6F,OAAe,EAAE,IAAY,EAAE,QAAiB;QAC9J,OAAO,IAAI,CAAC,QAAQ,CAAC,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,QAAQ,CAAC,CAAA;IACrD,CAAC;IAED,KAAK,CAAC,cAAc,CAA6F,OAAe,EAAE,IAAY,EAAE,QAAi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p/interface';
3
+ import type { PeerId, Logger, PrivateKey } from '@libp2p/interface';
4
4
  import type { Duplex } from 'it-stream-types';
5
5
  import type { Uint8ArrayList } from 'uint8arraylist';
6
6
  export declare function verifyPeerCertificate(rawCertificate: Uint8Array, expectedPeerId?: PeerId, log?: Logger): Promise<PeerId>;
7
- export declare function generateCertificate(peerId: PeerId): Promise<{
7
+ export declare function generateCertificate(privateKey: PrivateKey): Promise<{
8
8
  cert: string;
9
9
  key: string;
10
10
  }>;
@@ -1 +1 @@
1
- {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,MAAM,IAAI,YAAY,EAAE,MAAM,aAAa,CAAA;AAcpD,OAAO,KAAK,EAAE,MAAM,EAAgC,MAAM,EAAE,MAAM,mBAAmB,CAAA;AACrF,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAA;AAC7C,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAapD,wBAAsB,qBAAqB,CAAE,cAAc,EAAE,UAAU,EAAE,cAAc,CAAC,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CA0E/H;AAED,wBAAsB,mBAAmB,CAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,CAAC,CAkFjG;AAeD;;GAEG;AACH,wBAAgB,mBAAmB,CAAE,aAAa,EAAE,WAAW,GAAG,UAAU,CAQ3E;AAmBD,wBAAgB,UAAU,CAAE,IAAI,EAAE,MAAM,CAAC,cAAc,CAAC,UAAU,GAAG,cAAc,CAAC,CAAC,GAAG,YAAY,CAgCnG;AAED,wBAAgB,UAAU,CAAE,MAAM,EAAE,YAAY,GAAG,MAAM,CAAC,cAAc,CAAC,UAAU,GAAG,cAAc,CAAC,CAAC,CA+CrG"}
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,MAAM,IAAI,YAAY,EAAE,MAAM,aAAa,CAAA;AAepD,OAAO,KAAK,EAAE,MAAM,EAAgC,MAAM,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAA;AACjG,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAA;AAC7C,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAapD,wBAAsB,qBAAqB,CAAE,cAAc,EAAE,UAAU,EAAE,cAAc,CAAC,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CA4D/H;AAED,wBAAsB,mBAAmB,CAAE,UAAU,EAAE,UAAU,GAAG,OAAO,CAAC;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,CAAC,CAiDzG;AAeD;;GAEG;AACH,wBAAgB,mBAAmB,CAAE,aAAa,EAAE,WAAW,GAAG,UAAU,CAQ3E;AAmBD,wBAAgB,UAAU,CAAE,IAAI,EAAE,MAAM,CAAC,cAAc,CAAC,UAAU,GAAG,cAAc,CAAC,CAAC,GAAG,YAAY,CAgCnG;AAED,wBAAgB,UAAU,CAAE,MAAM,EAAE,YAAY,GAAG,MAAM,CAAC,cAAc,CAAC,UAAU,GAAG,cAAc,CAAC,CAAC,CA+CrG"}
package/dist/src/utils.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { Duplex as DuplexStream } from 'node:stream';
2
- import { Ed25519PublicKey, Secp256k1PublicKey, marshalPublicKey, supportedKeys, unmarshalPrivateKey, unmarshalPublicKey } from '@libp2p/crypto/keys';
3
- import { CodeError, InvalidCryptoExchangeError, UnexpectedPeerError } from '@libp2p/interface';
4
- import { peerIdFromKeys } from '@libp2p/peer-id';
2
+ import { publicKeyFromProtobuf } from '@libp2p/crypto/keys';
3
+ import { InvalidCryptoExchangeError, UnexpectedPeerError } from '@libp2p/interface';
4
+ import { peerIdFromCID } from '@libp2p/peer-id';
5
5
  import { AsnConvert } from '@peculiar/asn1-schema';
6
6
  import * as asn1X509 from '@peculiar/asn1-x509';
7
7
  import { Crypto } from '@peculiar/webcrypto';
@@ -11,7 +11,8 @@ import { pushable } from 'it-pushable';
11
11
  import { concat as uint8ArrayConcat } from 'uint8arrays/concat';
12
12
  import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string';
13
13
  import { toString as uint8ArrayToString } from 'uint8arrays/to-string';
14
- import { KeyType, PublicKey } from '../src/pb/index.js';
14
+ import { InvalidCertificateError } from './errors.js';
15
+ import { KeyType, PublicKey } from './pb/index.js';
15
16
  const crypto = new Crypto();
16
17
  x509.cryptoProvider.set(crypto);
17
18
  const LIBP2P_PUBLIC_KEY_EXTENSION = '1.3.6.1.4.1.53594.1.1';
@@ -25,11 +26,11 @@ export async function verifyPeerCertificate(rawCertificate, expectedPeerId, log)
25
26
  const x509Cert = new x509.X509Certificate(rawCertificate);
26
27
  if (x509Cert.notBefore.getTime() > now) {
27
28
  log?.error('the certificate was not valid yet');
28
- throw new CodeError('The certificate is not valid yet', 'ERR_INVALID_CERTIFICATE');
29
+ throw new InvalidCertificateError('The certificate is not valid yet');
29
30
  }
30
31
  if (x509Cert.notAfter.getTime() < now) {
31
32
  log?.error('the certificate has expired');
32
- throw new CodeError('The certificate has expired', 'ERR_INVALID_CERTIFICATE');
33
+ throw new InvalidCertificateError('The certificate has expired');
33
34
  }
34
35
  const certSignatureValid = await x509Cert.verify();
35
36
  if (!certSignatureValid) {
@@ -44,28 +45,13 @@ export async function verifyPeerCertificate(rawCertificate, expectedPeerId, log)
44
45
  const libp2pPublicKeyExtension = x509Cert.extensions[0];
45
46
  if (libp2pPublicKeyExtension == null || libp2pPublicKeyExtension.type !== LIBP2P_PUBLIC_KEY_EXTENSION) {
46
47
  log?.error('the certificate did not include the libp2p public key extension');
47
- throw new CodeError('The certificate did not include the libp2p public key extension', 'ERR_INVALID_CERTIFICATE');
48
+ throw new InvalidCertificateError('The certificate did not include the libp2p public key extension');
48
49
  }
49
50
  const { result: libp2pKeySequence } = asn1js.fromBER(libp2pPublicKeyExtension.value);
50
51
  // @ts-expect-error deep chain
51
52
  const remotePeerIdPb = libp2pKeySequence.valueBlock.value[0].valueBlock.valueHex;
52
53
  const marshalledPeerId = new Uint8Array(remotePeerIdPb, 0, remotePeerIdPb.byteLength);
53
- const remotePublicKey = PublicKey.decode(marshalledPeerId);
54
- const remotePublicKeyData = remotePublicKey.data ?? new Uint8Array(0);
55
- let remoteLibp2pPublicKey;
56
- if (remotePublicKey.type === KeyType.Ed25519) {
57
- remoteLibp2pPublicKey = new Ed25519PublicKey(remotePublicKeyData);
58
- }
59
- else if (remotePublicKey.type === KeyType.Secp256k1) {
60
- remoteLibp2pPublicKey = new Secp256k1PublicKey(remotePublicKeyData);
61
- }
62
- else if (remotePublicKey.type === KeyType.RSA) {
63
- remoteLibp2pPublicKey = supportedKeys.rsa.unmarshalRsaPublicKey(remotePublicKeyData);
64
- }
65
- else {
66
- log?.error('unknown or unsupported key type', remotePublicKey.type);
67
- throw new InvalidCryptoExchangeError('Unknown or unsupported key type');
68
- }
54
+ const remoteLibp2pPublicKey = publicKeyFromProtobuf(marshalledPeerId);
69
55
  // @ts-expect-error deep chain
70
56
  const remoteSignature = libp2pKeySequence.valueBlock.value[1].valueBlock.valueHex;
71
57
  const dataToVerify = encodeSignatureData(x509Cert.publicKey.rawData);
@@ -74,15 +60,14 @@ export async function verifyPeerCertificate(rawCertificate, expectedPeerId, log)
74
60
  log?.error('invalid libp2p signature');
75
61
  throw new InvalidCryptoExchangeError('Could not verify signature');
76
62
  }
77
- const marshalled = marshalPublicKey(remoteLibp2pPublicKey);
78
- const remotePeerId = await peerIdFromKeys(marshalled);
63
+ const remotePeerId = peerIdFromCID(remoteLibp2pPublicKey.toCID());
79
64
  if (expectedPeerId?.equals(remotePeerId) === false) {
80
65
  log?.error('invalid peer id');
81
66
  throw new UnexpectedPeerError();
82
67
  }
83
68
  return remotePeerId;
84
69
  }
85
- export async function generateCertificate(peerId) {
70
+ export async function generateCertificate(privateKey) {
86
71
  const now = Date.now();
87
72
  const alg = {
88
73
  name: 'ECDSA',
@@ -92,35 +77,7 @@ export async function generateCertificate(peerId) {
92
77
  const keys = await crypto.subtle.generateKey(alg, true, ['sign']);
93
78
  const certPublicKeySpki = await crypto.subtle.exportKey('spki', keys.publicKey);
94
79
  const dataToSign = encodeSignatureData(certPublicKeySpki);
95
- if (peerId.privateKey == null) {
96
- throw new InvalidCryptoExchangeError('Private key was missing from PeerId');
97
- }
98
- const privateKey = await unmarshalPrivateKey(peerId.privateKey);
99
80
  const sig = await privateKey.sign(dataToSign);
100
- let keyType;
101
- let keyData;
102
- if (peerId.publicKey == null) {
103
- throw new CodeError('Public key missing from PeerId', 'ERR_INVALID_PEER_ID');
104
- }
105
- const publicKey = unmarshalPublicKey(peerId.publicKey);
106
- if (peerId.type === 'Ed25519') {
107
- // Ed25519: Only the 32 bytes of the public key
108
- keyType = KeyType.Ed25519;
109
- keyData = publicKey.marshal();
110
- }
111
- else if (peerId.type === 'secp256k1') {
112
- // Secp256k1: Only the compressed form of the public key. 33 bytes.
113
- keyType = KeyType.Secp256k1;
114
- keyData = publicKey.marshal();
115
- }
116
- else if (peerId.type === 'RSA') {
117
- // The rest of the keys are encoded as a SubjectPublicKeyInfo structure in PKIX, ASN.1 DER form.
118
- keyType = KeyType.RSA;
119
- keyData = publicKey.marshal();
120
- }
121
- else {
122
- throw new CodeError('Unknown PeerId type', 'ERR_UNKNOWN_PEER_ID_TYPE');
123
- }
124
81
  const notAfter = new Date(now + CERT_VALIDITY_PERIOD_TO);
125
82
  // workaround for https://github.com/PeculiarVentures/x509/issues/73
126
83
  notAfter.setMilliseconds(0);
@@ -137,8 +94,8 @@ export async function generateCertificate(peerId) {
137
94
  // publicKey
138
95
  new asn1js.OctetString({
139
96
  valueHex: PublicKey.encode({
140
- type: keyType,
141
- data: keyData
97
+ type: KeyType[privateKey.type],
98
+ data: privateKey.publicKey.raw
142
99
  })
143
100
  }),
144
101
  // signature
@@ -1 +1 @@
1
- {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,YAAY,EAAE,MAAM,aAAa,CAAA;AACpD,OAAO,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,aAAa,EAAE,mBAAmB,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAA;AACpJ,OAAO,EAAE,SAAS,EAAE,0BAA0B,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAA;AAC9F,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAA;AAChD,OAAO,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAA;AAClD,OAAO,KAAK,QAAQ,MAAM,qBAAqB,CAAA;AAC/C,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAA;AAC5C,OAAO,KAAK,IAAI,MAAM,gBAAgB,CAAA;AACtC,OAAO,KAAK,MAAM,MAAM,QAAQ,CAAA;AAChC,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AACtC,OAAO,EAAE,MAAM,IAAI,gBAAgB,EAAE,MAAM,oBAAoB,CAAA;AAC/D,OAAO,EAAE,UAAU,IAAI,oBAAoB,EAAE,MAAM,yBAAyB,CAAA;AAC5E,OAAO,EAAE,QAAQ,IAAI,kBAAkB,EAAE,MAAM,uBAAuB,CAAA;AACtE,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAA;AAKvD,MAAM,MAAM,GAAG,IAAI,MAAM,EAAE,CAAA;AAC3B,IAAI,CAAC,cAAc,CAAC,GAAG,CAAC,MAAM,CAAC,CAAA;AAE/B,MAAM,2BAA2B,GAAG,uBAAuB,CAAA;AAC3D,MAAM,WAAW,GAAG,uBAAuB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,CAAC;gBACH,CAAC;gBAED,qBAAqB;gBACrB,MAAM,CAAC,GAAG,EAAE,CAAA;YACd,CAAC;YAAC,OAAO,GAAQ,EAAE,CAAC;gBAClB,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,CAAA;gBACnB,MAAM,GAAG,CAAA;YACX,CAAC;QACH,CAAC;KACF,CAAA;IAED,OAAO,MAAM,CAAA;AACf,CAAC;AAED,KAAK,UAAU,mBAAmB,CAAE,MAAoB;IACtD,MAAM,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QAC1C,MAAM,gBAAgB,GAAG,GAAS,EAAE;YAClC,OAAO,EAAE,CAAA;YACT,OAAO,EAAE,CAAA;QACX,CAAC,CAAA;QACD,MAAM,YAAY,GAAG,CAAC,GAAW,EAAQ,EAAE;YACzC,OAAO,EAAE,CAAA;YACT,MAAM,CAAC,GAAG,IAAI,IAAI,KAAK,CAAC,cAAc,CAAC,CAAC,CAAA;QAC1C,CAAC,CAAA;QAED,MAAM,OAAO,GAAG,GAAS,EAAE;YACzB,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE,gBAAgB,CAAC,CAAA;YAChD,MAAM,CAAC,cAAc,CAAC,KAAK,EAAE,YAAY,CAAC,CAAA;YAC1C,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE,YAAY,CAAC,CAAA;QAC9C,CAAC,CAAA;QAED,MAAM,CAAC,WAAW,CAAC,OAAO,EAAE,gBAAgB,CAAC,CAAA;QAC7C,MAAM,CAAC,WAAW,CAAC,KAAK,EAAE,YAAY,CAAC,CAAA;QACvC,MAAM,CAAC,WAAW,CAAC,OAAO,EAAE,YAAY,CAAC,CAAA;IAC3C,CAAC,CAAC,CAAA;AACJ,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@libp2p/tls",
3
- "version": "1.1.5-e211b46cc",
3
+ "version": "2.0.0-1210884ed",
4
4
  "description": "A connection encrypter that uses TLS 1.3",
5
5
  "license": "Apache-2.0 OR MIT",
6
6
  "homepage": "https://github.com/libp2p/js-libp2p/tree/main/packages/connection-encrypter-tls#readme",
@@ -48,9 +48,9 @@
48
48
  "doc-check": "aegir doc-check"
49
49
  },
50
50
  "dependencies": {
51
- "@libp2p/crypto": "4.1.9-e211b46cc",
52
- "@libp2p/interface": "1.7.0-e211b46cc",
53
- "@libp2p/peer-id": "4.2.4-e211b46cc",
51
+ "@libp2p/crypto": "5.0.0-1210884ed",
52
+ "@libp2p/interface": "2.0.0-1210884ed",
53
+ "@libp2p/peer-id": "5.0.0-1210884ed",
54
54
  "@peculiar/asn1-schema": "^2.3.8",
55
55
  "@peculiar/asn1-x509": "^2.3.8",
56
56
  "@peculiar/webcrypto": "^1.4.6",
@@ -63,9 +63,8 @@
63
63
  "uint8arrays": "^5.1.0"
64
64
  },
65
65
  "devDependencies": {
66
- "@libp2p/interface-compliance-tests": "5.4.12-e211b46cc",
67
- "@libp2p/logger": "4.0.20-e211b46cc",
68
- "@libp2p/peer-id-factory": "4.2.4-e211b46cc",
66
+ "@libp2p/interface-compliance-tests": "6.0.0-1210884ed",
67
+ "@libp2p/logger": "5.0.0-1210884ed",
69
68
  "@multiformats/multiaddr": "^12.2.3",
70
69
  "aegir": "^44.0.1",
71
70
  "protons": "^7.5.0",
package/src/errors.ts ADDED
@@ -0,0 +1,19 @@
1
+ /**
2
+ * The handshake timed out
3
+ */
4
+ export class HandshakeTimeoutError extends Error {
5
+ constructor (message = 'Handshake timeout') {
6
+ super(message)
7
+ this.name = 'HandshakeTimeoutError'
8
+ }
9
+ }
10
+
11
+ /**
12
+ * The certificate was invalid
13
+ */
14
+ export class InvalidCertificateError extends Error {
15
+ constructor (message = 'Invalid certificate') {
16
+ super(message)
17
+ this.name = 'InvalidCertificateError'
18
+ }
19
+ }
package/src/index.ts CHANGED
@@ -11,7 +11,7 @@
11
11
  *
12
12
  * const node = await createLibp2p({
13
13
  * // ...other options
14
- * connectionEncryption: [
14
+ * connectionEncrypters: [
15
15
  * tls()
16
16
  * ]
17
17
  * })
@@ -19,22 +19,15 @@
19
19
  */
20
20
 
21
21
  import { TLS } from './tls.js'
22
- import type { ComponentLogger, ConnectionEncrypter } from '@libp2p/interface'
22
+ import type { ComponentLogger, ConnectionEncrypter, PrivateKey } from '@libp2p/interface'
23
23
 
24
24
  export const PROTOCOL = '/tls/1.0.0'
25
25
 
26
26
  export interface TLSComponents {
27
+ privateKey: PrivateKey
27
28
  logger: ComponentLogger
28
29
  }
29
30
 
30
- export interface TLSInit {
31
- /**
32
- * The peer id exchange must complete within this many milliseconds
33
- * (default: 1000)
34
- */
35
- timeout?: number
36
- }
37
-
38
- export function tls (init?: TLSInit): (components: TLSComponents) => ConnectionEncrypter {
39
- return (components) => new TLS(components, init)
31
+ export function tls (): (components: TLSComponents) => ConnectionEncrypter {
32
+ return (components) => new TLS(components)
40
33
  }
@@ -3,7 +3,7 @@ syntax = "proto3";
3
3
  enum KeyType {
4
4
  RSA = 0;
5
5
  Ed25519 = 1;
6
- Secp256k1 = 2;
6
+ secp256k1 = 2;
7
7
  ECDSA = 3;
8
8
  }
9
9
 
package/src/pb/index.ts CHANGED
@@ -4,20 +4,20 @@
4
4
  /* eslint-disable @typescript-eslint/no-unnecessary-boolean-literal-compare */
5
5
  /* eslint-disable @typescript-eslint/no-empty-interface */
6
6
 
7
- import { type Codec, decodeMessage, encodeMessage, enumeration, message } from 'protons-runtime'
7
+ import { type Codec, decodeMessage, type DecodeOptions, encodeMessage, enumeration, message } from 'protons-runtime'
8
8
  import type { Uint8ArrayList } from 'uint8arraylist'
9
9
 
10
10
  export enum KeyType {
11
11
  RSA = 'RSA',
12
12
  Ed25519 = 'Ed25519',
13
- Secp256k1 = 'Secp256k1',
13
+ secp256k1 = 'secp256k1',
14
14
  ECDSA = 'ECDSA'
15
15
  }
16
16
 
17
17
  enum __KeyTypeValues {
18
18
  RSA = 0,
19
19
  Ed25519 = 1,
20
- Secp256k1 = 2,
20
+ secp256k1 = 2,
21
21
  ECDSA = 3
22
22
  }
23
23
 
@@ -54,7 +54,7 @@ export namespace PublicKey {
54
54
  if (opts.lengthDelimited !== false) {
55
55
  w.ldelim()
56
56
  }
57
- }, (reader, length) => {
57
+ }, (reader, length, opts = {}) => {
58
58
  const obj: any = {}
59
59
 
60
60
  const end = length == null ? reader.len : reader.pos + length
@@ -89,7 +89,7 @@ export namespace PublicKey {
89
89
  return encodeMessage(obj, PublicKey.codec())
90
90
  }
91
91
 
92
- export const decode = (buf: Uint8Array | Uint8ArrayList): PublicKey => {
93
- return decodeMessage(buf, PublicKey.codec())
92
+ export const decode = (buf: Uint8Array | Uint8ArrayList, opts?: DecodeOptions<PublicKey>): PublicKey => {
93
+ return decodeMessage(buf, PublicKey.codec(), opts)
94
94
  }
95
95
  }
package/src/tls.ts CHANGED
@@ -11,7 +11,7 @@
11
11
  *
12
12
  * const node = await createLibp2p({
13
13
  * // ...other options
14
- * connectionEncryption: [
14
+ * connectionEncrypters: [
15
15
  * tls()
16
16
  * ]
17
17
  * })
@@ -19,22 +19,23 @@
19
19
  */
20
20
 
21
21
  import { TLSSocket, type TLSSocketOptions, connect } from 'node:tls'
22
- import { CodeError, serviceCapabilities } from '@libp2p/interface'
22
+ import { serviceCapabilities } from '@libp2p/interface'
23
+ import { HandshakeTimeoutError } from './errors.js'
23
24
  import { generateCertificate, verifyPeerCertificate, itToStream, streamToIt } from './utils.js'
24
25
  import { PROTOCOL } from './index.js'
25
- import type { TLSComponents, TLSInit } from './index.js'
26
- import type { MultiaddrConnection, ConnectionEncrypter, SecuredConnection, PeerId, Logger } from '@libp2p/interface'
26
+ import type { TLSComponents } from './index.js'
27
+ import type { MultiaddrConnection, ConnectionEncrypter, SecuredConnection, Logger, SecureConnectionOptions, PrivateKey } from '@libp2p/interface'
27
28
  import type { Duplex } from 'it-stream-types'
28
29
  import type { Uint8ArrayList } from 'uint8arraylist'
29
30
 
30
31
  export class TLS implements ConnectionEncrypter {
31
32
  public protocol: string = PROTOCOL
32
33
  private readonly log: Logger
33
- private readonly timeout: number
34
+ private readonly privateKey: PrivateKey
34
35
 
35
- constructor (components: TLSComponents, init: TLSInit = {}) {
36
+ constructor (components: TLSComponents) {
36
37
  this.log = components.logger.forComponent('libp2p:tls')
37
- this.timeout = init.timeout ?? 1000
38
+ this.privateKey = components.privateKey
38
39
  }
39
40
 
40
41
  readonly [Symbol.toStringTag] = '@libp2p/tls'
@@ -43,20 +44,20 @@ export class TLS implements ConnectionEncrypter {
43
44
  '@libp2p/connection-encryption'
44
45
  ]
45
46
 
46
- async secureInbound <Stream extends Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> = MultiaddrConnection> (localId: PeerId, conn: Stream, remoteId?: PeerId): Promise<SecuredConnection<Stream>> {
47
- return this._encrypt(localId, conn, true, remoteId)
47
+ async secureInbound <Stream extends Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> = MultiaddrConnection> (conn: Stream, options?: SecureConnectionOptions): Promise<SecuredConnection<Stream>> {
48
+ return this._encrypt(conn, true, options)
48
49
  }
49
50
 
50
- async secureOutbound <Stream extends Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> = MultiaddrConnection> (localId: PeerId, conn: Stream, remoteId?: PeerId): Promise<SecuredConnection<Stream>> {
51
- return this._encrypt(localId, conn, false, remoteId)
51
+ async secureOutbound <Stream extends Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> = MultiaddrConnection> (conn: Stream, options?: SecureConnectionOptions): Promise<SecuredConnection<Stream>> {
52
+ return this._encrypt(conn, false, options)
52
53
  }
53
54
 
54
55
  /**
55
56
  * Encrypt connection
56
57
  */
57
- async _encrypt <Stream extends Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> = MultiaddrConnection> (localId: PeerId, conn: Stream, isServer: boolean, remoteId?: PeerId): Promise<SecuredConnection<Stream>> {
58
+ async _encrypt <Stream extends Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> = MultiaddrConnection> (conn: Stream, isServer: boolean, options?: SecureConnectionOptions): Promise<SecuredConnection<Stream>> {
58
59
  const opts: TLSSocketOptions = {
59
- ...await generateCertificate(localId),
60
+ ...await generateCertificate(this.privateKey),
60
61
  isServer,
61
62
  // require TLS 1.3 or later
62
63
  minVersion: 'TLSv1.3',
@@ -81,14 +82,14 @@ export class TLS implements ConnectionEncrypter {
81
82
  }
82
83
 
83
84
  return new Promise((resolve, reject) => {
84
- const abortTimeout = setTimeout(() => {
85
- socket.destroy(new CodeError('Handshake timeout', 'ERR_HANDSHAKE_TIMEOUT'))
86
- }, this.timeout)
85
+ options?.signal?.addEventListener('abort', () => {
86
+ socket.destroy(new HandshakeTimeoutError())
87
+ })
87
88
 
88
89
  const verifyRemote = (): void => {
89
90
  const remote = socket.getPeerCertificate()
90
91
 
91
- verifyPeerCertificate(remote.raw, remoteId, this.log)
92
+ verifyPeerCertificate(remote.raw, options?.remotePeer, this.log)
92
93
  .then(remotePeer => {
93
94
  this.log('remote certificate ok, remote peer %p', remotePeer)
94
95
 
@@ -103,14 +104,10 @@ export class TLS implements ConnectionEncrypter {
103
104
  .catch((err: Error) => {
104
105
  reject(err)
105
106
  })
106
- .finally(() => {
107
- clearTimeout(abortTimeout)
108
- })
109
107
  }
110
108
 
111
109
  socket.on('error', (err: Error) => {
112
110
  reject(err)
113
- clearTimeout(abortTimeout)
114
111
  })
115
112
  socket.once('secure', (evt) => {
116
113
  this.log('verifying remote certificate')
package/src/utils.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { Duplex as DuplexStream } from 'node:stream'
2
- import { Ed25519PublicKey, Secp256k1PublicKey, marshalPublicKey, supportedKeys, unmarshalPrivateKey, unmarshalPublicKey } from '@libp2p/crypto/keys'
3
- import { CodeError, InvalidCryptoExchangeError, UnexpectedPeerError } from '@libp2p/interface'
4
- import { peerIdFromKeys } from '@libp2p/peer-id'
2
+ import { publicKeyFromProtobuf } from '@libp2p/crypto/keys'
3
+ import { InvalidCryptoExchangeError, UnexpectedPeerError } from '@libp2p/interface'
4
+ import { peerIdFromCID } from '@libp2p/peer-id'
5
5
  import { AsnConvert } from '@peculiar/asn1-schema'
6
6
  import * as asn1X509 from '@peculiar/asn1-x509'
7
7
  import { Crypto } from '@peculiar/webcrypto'
@@ -11,8 +11,9 @@ import { pushable } from 'it-pushable'
11
11
  import { concat as uint8ArrayConcat } from 'uint8arrays/concat'
12
12
  import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
13
13
  import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
14
- import { KeyType, PublicKey } from '../src/pb/index.js'
15
- import type { PeerId, PublicKey as Libp2pPublicKey, Logger } from '@libp2p/interface'
14
+ import { InvalidCertificateError } from './errors.js'
15
+ import { KeyType, PublicKey } from './pb/index.js'
16
+ import type { PeerId, PublicKey as Libp2pPublicKey, Logger, PrivateKey } from '@libp2p/interface'
16
17
  import type { Duplex } from 'it-stream-types'
17
18
  import type { Uint8ArrayList } from 'uint8arraylist'
18
19
 
@@ -33,12 +34,12 @@ export async function verifyPeerCertificate (rawCertificate: Uint8Array, expecte
33
34
 
34
35
  if (x509Cert.notBefore.getTime() > now) {
35
36
  log?.error('the certificate was not valid yet')
36
- throw new CodeError('The certificate is not valid yet', 'ERR_INVALID_CERTIFICATE')
37
+ throw new InvalidCertificateError('The certificate is not valid yet')
37
38
  }
38
39
 
39
40
  if (x509Cert.notAfter.getTime() < now) {
40
41
  log?.error('the certificate has expired')
41
- throw new CodeError('The certificate has expired', 'ERR_INVALID_CERTIFICATE')
42
+ throw new InvalidCertificateError('The certificate has expired')
42
43
  }
43
44
 
44
45
  const certSignatureValid = await x509Cert.verify()
@@ -59,7 +60,7 @@ export async function verifyPeerCertificate (rawCertificate: Uint8Array, expecte
59
60
 
60
61
  if (libp2pPublicKeyExtension == null || libp2pPublicKeyExtension.type !== LIBP2P_PUBLIC_KEY_EXTENSION) {
61
62
  log?.error('the certificate did not include the libp2p public key extension')
62
- throw new CodeError('The certificate did not include the libp2p public key extension', 'ERR_INVALID_CERTIFICATE')
63
+ throw new InvalidCertificateError('The certificate did not include the libp2p public key extension')
63
64
  }
64
65
 
65
66
  const { result: libp2pKeySequence } = asn1js.fromBER(libp2pPublicKeyExtension.value)
@@ -67,20 +68,7 @@ export async function verifyPeerCertificate (rawCertificate: Uint8Array, expecte
67
68
  // @ts-expect-error deep chain
68
69
  const remotePeerIdPb = libp2pKeySequence.valueBlock.value[0].valueBlock.valueHex
69
70
  const marshalledPeerId = new Uint8Array(remotePeerIdPb, 0, remotePeerIdPb.byteLength)
70
- const remotePublicKey = PublicKey.decode(marshalledPeerId)
71
- const remotePublicKeyData = remotePublicKey.data ?? new Uint8Array(0)
72
- let remoteLibp2pPublicKey: Libp2pPublicKey
73
-
74
- if (remotePublicKey.type === KeyType.Ed25519) {
75
- remoteLibp2pPublicKey = new Ed25519PublicKey(remotePublicKeyData)
76
- } else if (remotePublicKey.type === KeyType.Secp256k1) {
77
- remoteLibp2pPublicKey = new Secp256k1PublicKey(remotePublicKeyData)
78
- } else if (remotePublicKey.type === KeyType.RSA) {
79
- remoteLibp2pPublicKey = supportedKeys.rsa.unmarshalRsaPublicKey(remotePublicKeyData)
80
- } else {
81
- log?.error('unknown or unsupported key type', remotePublicKey.type)
82
- throw new InvalidCryptoExchangeError('Unknown or unsupported key type')
83
- }
71
+ const remoteLibp2pPublicKey: Libp2pPublicKey = publicKeyFromProtobuf(marshalledPeerId)
84
72
 
85
73
  // @ts-expect-error deep chain
86
74
  const remoteSignature = libp2pKeySequence.valueBlock.value[1].valueBlock.valueHex
@@ -92,8 +80,7 @@ export async function verifyPeerCertificate (rawCertificate: Uint8Array, expecte
92
80
  throw new InvalidCryptoExchangeError('Could not verify signature')
93
81
  }
94
82
 
95
- const marshalled = marshalPublicKey(remoteLibp2pPublicKey)
96
- const remotePeerId = await peerIdFromKeys(marshalled)
83
+ const remotePeerId = peerIdFromCID(remoteLibp2pPublicKey.toCID())
97
84
 
98
85
  if (expectedPeerId?.equals(remotePeerId) === false) {
99
86
  log?.error('invalid peer id')
@@ -103,7 +90,7 @@ export async function verifyPeerCertificate (rawCertificate: Uint8Array, expecte
103
90
  return remotePeerId
104
91
  }
105
92
 
106
- export async function generateCertificate (peerId: PeerId): Promise<{ cert: string, key: string }> {
93
+ export async function generateCertificate (privateKey: PrivateKey): Promise<{ cert: string, key: string }> {
107
94
  const now = Date.now()
108
95
 
109
96
  const alg = {
@@ -113,42 +100,9 @@ export async function generateCertificate (peerId: PeerId): Promise<{ cert: stri
113
100
  }
114
101
 
115
102
  const keys = await crypto.subtle.generateKey(alg, true, ['sign'])
116
-
117
103
  const certPublicKeySpki = await crypto.subtle.exportKey('spki', keys.publicKey)
118
104
  const dataToSign = encodeSignatureData(certPublicKeySpki)
119
-
120
- if (peerId.privateKey == null) {
121
- throw new InvalidCryptoExchangeError('Private key was missing from PeerId')
122
- }
123
-
124
- const privateKey = await unmarshalPrivateKey(peerId.privateKey)
125
105
  const sig = await privateKey.sign(dataToSign)
126
-
127
- let keyType: KeyType
128
- let keyData: Uint8Array
129
-
130
- if (peerId.publicKey == null) {
131
- throw new CodeError('Public key missing from PeerId', 'ERR_INVALID_PEER_ID')
132
- }
133
-
134
- const publicKey = unmarshalPublicKey(peerId.publicKey)
135
-
136
- if (peerId.type === 'Ed25519') {
137
- // Ed25519: Only the 32 bytes of the public key
138
- keyType = KeyType.Ed25519
139
- keyData = publicKey.marshal()
140
- } else if (peerId.type === 'secp256k1') {
141
- // Secp256k1: Only the compressed form of the public key. 33 bytes.
142
- keyType = KeyType.Secp256k1
143
- keyData = publicKey.marshal()
144
- } else if (peerId.type === 'RSA') {
145
- // The rest of the keys are encoded as a SubjectPublicKeyInfo structure in PKIX, ASN.1 DER form.
146
- keyType = KeyType.RSA
147
- keyData = publicKey.marshal()
148
- } else {
149
- throw new CodeError('Unknown PeerId type', 'ERR_UNKNOWN_PEER_ID_TYPE')
150
- }
151
-
152
106
  const notAfter = new Date(now + CERT_VALIDITY_PERIOD_TO)
153
107
  // workaround for https://github.com/PeculiarVentures/x509/issues/73
154
108
  notAfter.setMilliseconds(0)
@@ -166,8 +120,8 @@ export async function generateCertificate (peerId: PeerId): Promise<{ cert: stri
166
120
  // publicKey
167
121
  new asn1js.OctetString({
168
122
  valueHex: PublicKey.encode({
169
- type: keyType,
170
- data: keyData
123
+ type: KeyType[privateKey.type],
124
+ data: privateKey.publicKey.raw
171
125
  })
172
126
  }),
173
127
  // signature