@libp2p/tls 0.0.0-0321812e7
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/LICENSE +4 -0
- package/README.md +45 -0
- package/dist/src/index.d.ts +33 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +25 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/pb/index.d.ts +21 -0
- package/dist/src/pb/index.d.ts.map +1 -0
- package/dist/src/pb/index.js +78 -0
- package/dist/src/pb/index.js.map +1 -0
- package/dist/src/tls.d.ts +36 -0
- package/dist/src/tls.d.ts.map +1 -0
- package/dist/src/tls.js +100 -0
- package/dist/src/tls.js.map +1 -0
- package/dist/src/utils.d.ts +17 -0
- package/dist/src/utils.d.ts.map +1 -0
- package/dist/src/utils.js +274 -0
- package/dist/src/utils.js.map +1 -0
- package/package.json +74 -0
- package/src/index.ts +40 -0
- package/src/pb/index.proto +13 -0
- package/src/pb/index.ts +95 -0
- package/src/tls.ts +115 -0
- package/src/utils.ts +319 -0
package/LICENSE
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
[](http://libp2p.io/)
|
|
2
|
+
[](https://discuss.libp2p.io)
|
|
3
|
+
[](https://codecov.io/gh/libp2p/js-libp2p)
|
|
4
|
+
[](https://github.com/libp2p/js-libp2p/actions/workflows/main.yml?query=branch%3Amain)
|
|
5
|
+
|
|
6
|
+
> A connection encrypter that uses TLS 1.3
|
|
7
|
+
|
|
8
|
+
# About
|
|
9
|
+
|
|
10
|
+
Implements the spec at <https://github.com/libp2p/specs/blob/master/tls/tls.md>
|
|
11
|
+
|
|
12
|
+
## Example
|
|
13
|
+
|
|
14
|
+
```typescript
|
|
15
|
+
import { createLibp2p } from 'libp2p'
|
|
16
|
+
import { tls } from '@libp2p/tls'
|
|
17
|
+
|
|
18
|
+
const node = await createLibp2p({
|
|
19
|
+
// ...other options
|
|
20
|
+
connectionEncryption: [
|
|
21
|
+
tls()
|
|
22
|
+
]
|
|
23
|
+
})
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
# Install
|
|
27
|
+
|
|
28
|
+
```console
|
|
29
|
+
$ npm i @libp2p/tls
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
# API Docs
|
|
33
|
+
|
|
34
|
+
- <https://libp2p.github.io/js-libp2p/modules/_libp2p_tls.html>
|
|
35
|
+
|
|
36
|
+
# License
|
|
37
|
+
|
|
38
|
+
Licensed under either of
|
|
39
|
+
|
|
40
|
+
- Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / <http://www.apache.org/licenses/LICENSE-2.0>)
|
|
41
|
+
- MIT ([LICENSE-MIT](LICENSE-MIT) / <http://opensource.org/licenses/MIT>)
|
|
42
|
+
|
|
43
|
+
# Contribution
|
|
44
|
+
|
|
45
|
+
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
*
|
|
4
|
+
* Implements the spec at https://github.com/libp2p/specs/blob/master/tls/tls.md
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
*
|
|
8
|
+
* ```typescript
|
|
9
|
+
* import { createLibp2p } from 'libp2p'
|
|
10
|
+
* import { tls } from '@libp2p/tls'
|
|
11
|
+
*
|
|
12
|
+
* const node = await createLibp2p({
|
|
13
|
+
* // ...other options
|
|
14
|
+
* connectionEncryption: [
|
|
15
|
+
* tls()
|
|
16
|
+
* ]
|
|
17
|
+
* })
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
import type { ComponentLogger, ConnectionEncrypter } from '@libp2p/interface';
|
|
21
|
+
export declare const PROTOCOL = "/tls/1.0.0";
|
|
22
|
+
export interface TLSComponents {
|
|
23
|
+
logger: ComponentLogger;
|
|
24
|
+
}
|
|
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;
|
|
33
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +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"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
*
|
|
4
|
+
* Implements the spec at https://github.com/libp2p/specs/blob/master/tls/tls.md
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
*
|
|
8
|
+
* ```typescript
|
|
9
|
+
* import { createLibp2p } from 'libp2p'
|
|
10
|
+
* import { tls } from '@libp2p/tls'
|
|
11
|
+
*
|
|
12
|
+
* const node = await createLibp2p({
|
|
13
|
+
* // ...other options
|
|
14
|
+
* connectionEncryption: [
|
|
15
|
+
* tls()
|
|
16
|
+
* ]
|
|
17
|
+
* })
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
import { TLS } from './tls.js';
|
|
21
|
+
export const PROTOCOL = '/tls/1.0.0';
|
|
22
|
+
export function tls(init) {
|
|
23
|
+
return (components) => new TLS(components, init);
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +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"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { type Codec } from 'protons-runtime';
|
|
2
|
+
import type { Uint8ArrayList } from 'uint8arraylist';
|
|
3
|
+
export declare enum KeyType {
|
|
4
|
+
RSA = "RSA",
|
|
5
|
+
Ed25519 = "Ed25519",
|
|
6
|
+
Secp256k1 = "Secp256k1",
|
|
7
|
+
ECDSA = "ECDSA"
|
|
8
|
+
}
|
|
9
|
+
export declare namespace KeyType {
|
|
10
|
+
const codec: () => Codec<KeyType>;
|
|
11
|
+
}
|
|
12
|
+
export interface PublicKey {
|
|
13
|
+
type?: KeyType;
|
|
14
|
+
data?: Uint8Array;
|
|
15
|
+
}
|
|
16
|
+
export declare namespace PublicKey {
|
|
17
|
+
const codec: () => Codec<PublicKey>;
|
|
18
|
+
const encode: (obj: Partial<PublicKey>) => Uint8Array;
|
|
19
|
+
const decode: (buf: Uint8Array | Uint8ArrayList) => PublicKey;
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +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"}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/* eslint-disable import/export */
|
|
2
|
+
/* eslint-disable complexity */
|
|
3
|
+
/* eslint-disable @typescript-eslint/no-namespace */
|
|
4
|
+
/* eslint-disable @typescript-eslint/no-unnecessary-boolean-literal-compare */
|
|
5
|
+
/* eslint-disable @typescript-eslint/no-empty-interface */
|
|
6
|
+
import { decodeMessage, encodeMessage, enumeration, message } from 'protons-runtime';
|
|
7
|
+
export var KeyType;
|
|
8
|
+
(function (KeyType) {
|
|
9
|
+
KeyType["RSA"] = "RSA";
|
|
10
|
+
KeyType["Ed25519"] = "Ed25519";
|
|
11
|
+
KeyType["Secp256k1"] = "Secp256k1";
|
|
12
|
+
KeyType["ECDSA"] = "ECDSA";
|
|
13
|
+
})(KeyType || (KeyType = {}));
|
|
14
|
+
var __KeyTypeValues;
|
|
15
|
+
(function (__KeyTypeValues) {
|
|
16
|
+
__KeyTypeValues[__KeyTypeValues["RSA"] = 0] = "RSA";
|
|
17
|
+
__KeyTypeValues[__KeyTypeValues["Ed25519"] = 1] = "Ed25519";
|
|
18
|
+
__KeyTypeValues[__KeyTypeValues["Secp256k1"] = 2] = "Secp256k1";
|
|
19
|
+
__KeyTypeValues[__KeyTypeValues["ECDSA"] = 3] = "ECDSA";
|
|
20
|
+
})(__KeyTypeValues || (__KeyTypeValues = {}));
|
|
21
|
+
(function (KeyType) {
|
|
22
|
+
KeyType.codec = () => {
|
|
23
|
+
return enumeration(__KeyTypeValues);
|
|
24
|
+
};
|
|
25
|
+
})(KeyType || (KeyType = {}));
|
|
26
|
+
export var PublicKey;
|
|
27
|
+
(function (PublicKey) {
|
|
28
|
+
let _codec;
|
|
29
|
+
PublicKey.codec = () => {
|
|
30
|
+
if (_codec == null) {
|
|
31
|
+
_codec = message((obj, w, opts = {}) => {
|
|
32
|
+
if (opts.lengthDelimited !== false) {
|
|
33
|
+
w.fork();
|
|
34
|
+
}
|
|
35
|
+
if (obj.type != null) {
|
|
36
|
+
w.uint32(8);
|
|
37
|
+
KeyType.codec().encode(obj.type, w);
|
|
38
|
+
}
|
|
39
|
+
if (obj.data != null) {
|
|
40
|
+
w.uint32(18);
|
|
41
|
+
w.bytes(obj.data);
|
|
42
|
+
}
|
|
43
|
+
if (opts.lengthDelimited !== false) {
|
|
44
|
+
w.ldelim();
|
|
45
|
+
}
|
|
46
|
+
}, (reader, length) => {
|
|
47
|
+
const obj = {};
|
|
48
|
+
const end = length == null ? reader.len : reader.pos + length;
|
|
49
|
+
while (reader.pos < end) {
|
|
50
|
+
const tag = reader.uint32();
|
|
51
|
+
switch (tag >>> 3) {
|
|
52
|
+
case 1: {
|
|
53
|
+
obj.type = KeyType.codec().decode(reader);
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
case 2: {
|
|
57
|
+
obj.data = reader.bytes();
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
default: {
|
|
61
|
+
reader.skipType(tag & 7);
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return obj;
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
return _codec;
|
|
70
|
+
};
|
|
71
|
+
PublicKey.encode = (obj) => {
|
|
72
|
+
return encodeMessage(obj, PublicKey.codec());
|
|
73
|
+
};
|
|
74
|
+
PublicKey.decode = (buf) => {
|
|
75
|
+
return decodeMessage(buf, PublicKey.codec());
|
|
76
|
+
};
|
|
77
|
+
})(PublicKey || (PublicKey = {}));
|
|
78
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +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;IACP,2DAAW,CAAA;IACX,+DAAa,CAAA;IACb,uDAAS,CAAA;AACX,CAAC,EALI,eAAe,KAAf,eAAe,QAKnB;AAED,WAAiB,OAAO;IACT,aAAK,GAAG,GAAmB,EAAE;QACxC,OAAO,WAAW,CAAU,eAAe,CAAC,CAAA;IAC9C,CAAC,CAAA;AACH,CAAC,EAJgB,OAAO,KAAP,OAAO,QAIvB;AAMD,MAAM,KAAW,SAAS,CA6DzB;AA7DD,WAAiB,SAAS;IACxB,IAAI,MAAwB,CAAA;IAEf,eAAK,GAAG,GAAqB,EAAE;QAC1C,IAAI,MAAM,IAAI,IAAI,EAAE,CAAC;YACnB,MAAM,GAAG,OAAO,CAAY,CAAC,GAAG,EAAE,CAAC,EAAE,IAA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AC,KAAK,EAAE,CAAA;4BACzB,MAAK;wBACP,CAAC;wBACD,OAAO,CAAC,CAAC,CAAC;4BACR,MAAM,CAAC,QAAQ,CAAC,GAAG,GAAG,CAAC,CAAC,CAAA;4BACxB,MAAK;wBACP,CAAC;oBACH,CAAC;gBACH,CAAC;gBAED,OAAO,GAAG,CAAA;YACZ,CAAC,CAAC,CAAA;QACJ,CAAC;QAED,OAAO,MAAM,CAAA;IACf,CAAC,CAAA;IAEY,gBAAM,GAAG,CAAC,GAAuB,EAAc,EAAE;QAC5D,OAAO,aAAa,CAAC,GAAG,EAAE,SAAS,CAAC,KAAK,EAAE,CAAC,CAAA;IAC9C,CAAC,CAAA;IAEY,gBAAM,GAAG,CAAC,GAAgC,EAAa,EAAE;QACpE,OAAO,aAAa,CAAC,GAAG,EAAE,SAAS,CAAC,KAAK,EAAE,CAAC,CAAA;IAC9C,CAAC,CAAA;AACH,CAAC,EA7DgB,SAAS,KAAT,SAAS,QA6DzB"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
*
|
|
4
|
+
* Implements the spec at https://github.com/libp2p/specs/blob/master/tls/tls.md
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
*
|
|
8
|
+
* ```typescript
|
|
9
|
+
* import { createLibp2p } from 'libp2p'
|
|
10
|
+
* import { tls } from '@libp2p/tls'
|
|
11
|
+
*
|
|
12
|
+
* const node = await createLibp2p({
|
|
13
|
+
* // ...other options
|
|
14
|
+
* connectionEncryption: [
|
|
15
|
+
* tls()
|
|
16
|
+
* ]
|
|
17
|
+
* })
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
import type { TLSComponents, TLSInit } from './index.js';
|
|
21
|
+
import type { MultiaddrConnection, ConnectionEncrypter, SecuredConnection, PeerId } from '@libp2p/interface';
|
|
22
|
+
import type { Duplex } from 'it-stream-types';
|
|
23
|
+
import type { Uint8ArrayList } from 'uint8arraylist';
|
|
24
|
+
export declare class TLS implements ConnectionEncrypter {
|
|
25
|
+
protocol: string;
|
|
26
|
+
private readonly log;
|
|
27
|
+
private readonly timeout;
|
|
28
|
+
constructor(components: TLSComponents, init?: TLSInit);
|
|
29
|
+
secureInbound<Stream extends Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> = MultiaddrConnection>(localId: PeerId, conn: Stream, remoteId?: PeerId): Promise<SecuredConnection<Stream>>;
|
|
30
|
+
secureOutbound<Stream extends Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> = MultiaddrConnection>(localId: PeerId, conn: Stream, remoteId?: PeerId): Promise<SecuredConnection<Stream>>;
|
|
31
|
+
/**
|
|
32
|
+
* Encrypt connection
|
|
33
|
+
*/
|
|
34
|
+
_encrypt<Stream extends Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> = MultiaddrConnection>(localId: PeerId, conn: Stream, isServer: boolean, remoteId?: PeerId): Promise<SecuredConnection<Stream>>;
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=tls.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tls.d.ts","sourceRoot":"","sources":["../../src/tls.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAMH,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;IAKpD,aAAa,CAAE,MAAM,SAAS,MAAM,CAAC,cAAc,CAAC,UAAU,GAAG,cAAc,CAAC,CAAC,GAAG,mBAAmB,EAAG,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,iBAAiB,CAAC,MAAM,CAAC,CAAC;IAI/L,cAAc,CAAE,MAAM,SAAS,MAAM,CAAC,cAAc,CAAC,UAAU,GAAG,cAAc,CAAC,CAAC,GAAG,mBAAmB,EAAG,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,iBAAiB,CAAC,MAAM,CAAC,CAAC;IAItM;;OAEG;IACG,QAAQ,CAAE,MAAM,SAAS,MAAM,CAAC,cAAc,CAAC,UAAU,GAAG,cAAc,CAAC,CAAC,GAAG,mBAAmB,EAAG,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,OAAO,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,iBAAiB,CAAC,MAAM,CAAC,CAAC;CAgEpN"}
|
package/dist/src/tls.js
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
*
|
|
4
|
+
* Implements the spec at https://github.com/libp2p/specs/blob/master/tls/tls.md
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
*
|
|
8
|
+
* ```typescript
|
|
9
|
+
* import { createLibp2p } from 'libp2p'
|
|
10
|
+
* import { tls } from '@libp2p/tls'
|
|
11
|
+
*
|
|
12
|
+
* const node = await createLibp2p({
|
|
13
|
+
* // ...other options
|
|
14
|
+
* connectionEncryption: [
|
|
15
|
+
* tls()
|
|
16
|
+
* ]
|
|
17
|
+
* })
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
import { TLSSocket, connect } from 'node:tls';
|
|
21
|
+
import { CodeError } from '@libp2p/interface';
|
|
22
|
+
import { generateCertificate, verifyPeerCertificate, itToStream, streamToIt } from './utils.js';
|
|
23
|
+
import { PROTOCOL } from './index.js';
|
|
24
|
+
export class TLS {
|
|
25
|
+
protocol = PROTOCOL;
|
|
26
|
+
log;
|
|
27
|
+
timeout;
|
|
28
|
+
constructor(components, init = {}) {
|
|
29
|
+
this.log = components.logger.forComponent('libp2p:tls');
|
|
30
|
+
this.timeout = init.timeout ?? 1000;
|
|
31
|
+
}
|
|
32
|
+
async secureInbound(localId, conn, remoteId) {
|
|
33
|
+
return this._encrypt(localId, conn, false, remoteId);
|
|
34
|
+
}
|
|
35
|
+
async secureOutbound(localId, conn, remoteId) {
|
|
36
|
+
return this._encrypt(localId, conn, true, remoteId);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Encrypt connection
|
|
40
|
+
*/
|
|
41
|
+
async _encrypt(localId, conn, isServer, remoteId) {
|
|
42
|
+
const opts = {
|
|
43
|
+
...await generateCertificate(localId),
|
|
44
|
+
isServer,
|
|
45
|
+
// require TLS 1.3 or later
|
|
46
|
+
minVersion: 'TLSv1.3',
|
|
47
|
+
maxVersion: 'TLSv1.3',
|
|
48
|
+
// accept self-signed certificates
|
|
49
|
+
rejectUnauthorized: false
|
|
50
|
+
};
|
|
51
|
+
let socket;
|
|
52
|
+
if (isServer) {
|
|
53
|
+
socket = new TLSSocket(itToStream(conn), {
|
|
54
|
+
...opts,
|
|
55
|
+
// require clients to send certificates
|
|
56
|
+
requestCert: true
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
socket = connect({
|
|
61
|
+
socket: itToStream(conn),
|
|
62
|
+
...opts
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
return new Promise((resolve, reject) => {
|
|
66
|
+
const abortTimeout = setTimeout(() => {
|
|
67
|
+
socket.destroy(new CodeError('Handshake timeout', 'ERR_HANDSHAKE_TIMEOUT'));
|
|
68
|
+
}, this.timeout);
|
|
69
|
+
const verifyRemote = () => {
|
|
70
|
+
const remote = socket.getPeerCertificate();
|
|
71
|
+
verifyPeerCertificate(remote.raw, remoteId, this.log)
|
|
72
|
+
.then(remotePeer => {
|
|
73
|
+
this.log('remote certificate ok, remote peer %p', remotePeer);
|
|
74
|
+
resolve({
|
|
75
|
+
remotePeer,
|
|
76
|
+
conn: {
|
|
77
|
+
...conn,
|
|
78
|
+
...streamToIt(socket)
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
})
|
|
82
|
+
.catch(err => {
|
|
83
|
+
reject(err);
|
|
84
|
+
})
|
|
85
|
+
.finally(() => {
|
|
86
|
+
clearTimeout(abortTimeout);
|
|
87
|
+
});
|
|
88
|
+
};
|
|
89
|
+
socket.on('error', err => {
|
|
90
|
+
reject(err);
|
|
91
|
+
clearTimeout(abortTimeout);
|
|
92
|
+
});
|
|
93
|
+
socket.on('secure', (evt) => {
|
|
94
|
+
this.log('verifying remote certificate');
|
|
95
|
+
verifyRemote();
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
//# sourceMappingURL=tls.js.map
|
|
@@ -0,0 +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,MAAM,mBAAmB,CAAA;AAC7C,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,G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0 +1,17 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { Duplex as DuplexStream } from 'node:stream';
|
|
3
|
+
import type { PeerId, Logger } from '@libp2p/interface';
|
|
4
|
+
import type { Duplex } from 'it-stream-types';
|
|
5
|
+
import type { Uint8ArrayList } from 'uint8arraylist';
|
|
6
|
+
export declare function verifyPeerCertificate(rawCertificate: Uint8Array, expectedPeerId?: PeerId, log?: Logger): Promise<PeerId>;
|
|
7
|
+
export declare function generateCertificate(peerId: PeerId): Promise<{
|
|
8
|
+
cert: string;
|
|
9
|
+
key: string;
|
|
10
|
+
}>;
|
|
11
|
+
/**
|
|
12
|
+
* @see https://github.com/libp2p/specs/blob/master/tls/tls.md#libp2p-public-key-extension
|
|
13
|
+
*/
|
|
14
|
+
export declare function encodeSignatureData(certPublicKey: ArrayBuffer): Uint8Array;
|
|
15
|
+
export declare function itToStream(conn: Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>>): DuplexStream;
|
|
16
|
+
export declare function streamToIt(stream: DuplexStream): Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>>;
|
|
17
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +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;AAYpD,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,CA8EjG;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"}
|