1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | import {
|
19 | ConnectionOptions,
|
20 | createSecureContext,
|
21 | PeerCertificate,
|
22 | SecureContext,
|
23 | } from 'tls';
|
24 |
|
25 | import { CallCredentials } from './call-credentials';
|
26 | import { CIPHER_SUITES, getDefaultRootsData } from './tls-helpers';
|
27 |
|
28 |
|
29 | function verifyIsBufferOrNull(obj: any, friendlyName: string): void {
|
30 | if (obj && !(obj instanceof Buffer)) {
|
31 | throw new TypeError(`${friendlyName}, if provided, must be a Buffer.`);
|
32 | }
|
33 | }
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | export type CheckServerIdentityCallback = (
|
42 | hostname: string,
|
43 | cert: PeerCertificate
|
44 | ) => Error | undefined;
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 | export interface VerifyOptions {
|
51 | |
52 |
|
53 |
|
54 |
|
55 | checkServerIdentity?: CheckServerIdentityCallback;
|
56 | }
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 | export abstract class ChannelCredentials {
|
64 | protected callCredentials: CallCredentials;
|
65 |
|
66 | protected constructor(callCredentials?: CallCredentials) {
|
67 | this.callCredentials = callCredentials || CallCredentials.createEmpty();
|
68 | }
|
69 | |
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 | abstract compose(callCredentials: CallCredentials): ChannelCredentials;
|
76 |
|
77 | |
78 |
|
79 |
|
80 | _getCallCredentials(): CallCredentials {
|
81 | return this.callCredentials;
|
82 | }
|
83 |
|
84 | |
85 |
|
86 |
|
87 |
|
88 |
|
89 | abstract _getConnectionOptions(): ConnectionOptions | null;
|
90 |
|
91 | |
92 |
|
93 |
|
94 | abstract _isSecure(): boolean;
|
95 |
|
96 | |
97 |
|
98 |
|
99 |
|
100 |
|
101 | abstract _equals(other: ChannelCredentials): boolean;
|
102 |
|
103 | |
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 | static createSsl(
|
113 | rootCerts?: Buffer | null,
|
114 | privateKey?: Buffer | null,
|
115 | certChain?: Buffer | null,
|
116 | verifyOptions?: VerifyOptions
|
117 | ): ChannelCredentials {
|
118 | verifyIsBufferOrNull(rootCerts, 'Root certificate');
|
119 | verifyIsBufferOrNull(privateKey, 'Private key');
|
120 | verifyIsBufferOrNull(certChain, 'Certificate chain');
|
121 | if (privateKey && !certChain) {
|
122 | throw new Error(
|
123 | 'Private key must be given with accompanying certificate chain'
|
124 | );
|
125 | }
|
126 | if (!privateKey && certChain) {
|
127 | throw new Error(
|
128 | 'Certificate chain must be given with accompanying private key'
|
129 | );
|
130 | }
|
131 | const secureContext = createSecureContext({
|
132 | ca: rootCerts ?? getDefaultRootsData() ?? undefined,
|
133 | key: privateKey ?? undefined,
|
134 | cert: certChain ?? undefined,
|
135 | ciphers: CIPHER_SUITES,
|
136 | });
|
137 | return new SecureChannelCredentialsImpl(secureContext, verifyOptions ?? {});
|
138 | }
|
139 |
|
140 | |
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 | static createFromSecureContext(
|
151 | secureContext: SecureContext,
|
152 | verifyOptions?: VerifyOptions
|
153 | ): ChannelCredentials {
|
154 | return new SecureChannelCredentialsImpl(secureContext, verifyOptions ?? {});
|
155 | }
|
156 |
|
157 | |
158 |
|
159 |
|
160 | static createInsecure(): ChannelCredentials {
|
161 | return new InsecureChannelCredentialsImpl();
|
162 | }
|
163 | }
|
164 |
|
165 | class InsecureChannelCredentialsImpl extends ChannelCredentials {
|
166 | constructor(callCredentials?: CallCredentials) {
|
167 | super(callCredentials);
|
168 | }
|
169 |
|
170 | compose(callCredentials: CallCredentials): never {
|
171 | throw new Error('Cannot compose insecure credentials');
|
172 | }
|
173 |
|
174 | _getConnectionOptions(): ConnectionOptions | null {
|
175 | return null;
|
176 | }
|
177 | _isSecure(): boolean {
|
178 | return false;
|
179 | }
|
180 | _equals(other: ChannelCredentials): boolean {
|
181 | return other instanceof InsecureChannelCredentialsImpl;
|
182 | }
|
183 | }
|
184 |
|
185 | class SecureChannelCredentialsImpl extends ChannelCredentials {
|
186 | connectionOptions: ConnectionOptions;
|
187 |
|
188 | constructor(
|
189 | private secureContext: SecureContext,
|
190 | private verifyOptions: VerifyOptions
|
191 | ) {
|
192 | super();
|
193 | this.connectionOptions = {
|
194 | secureContext,
|
195 | };
|
196 |
|
197 | if (verifyOptions?.checkServerIdentity) {
|
198 | this.connectionOptions.checkServerIdentity =
|
199 | verifyOptions.checkServerIdentity;
|
200 | }
|
201 | }
|
202 |
|
203 | compose(callCredentials: CallCredentials): ChannelCredentials {
|
204 | const combinedCallCredentials =
|
205 | this.callCredentials.compose(callCredentials);
|
206 | return new ComposedChannelCredentialsImpl(this, combinedCallCredentials);
|
207 | }
|
208 |
|
209 | _getConnectionOptions(): ConnectionOptions | null {
|
210 |
|
211 | return { ...this.connectionOptions };
|
212 | }
|
213 | _isSecure(): boolean {
|
214 | return true;
|
215 | }
|
216 | _equals(other: ChannelCredentials): boolean {
|
217 | if (this === other) {
|
218 | return true;
|
219 | }
|
220 | if (other instanceof SecureChannelCredentialsImpl) {
|
221 | return (
|
222 | this.secureContext === other.secureContext &&
|
223 | this.verifyOptions.checkServerIdentity ===
|
224 | other.verifyOptions.checkServerIdentity
|
225 | );
|
226 | } else {
|
227 | return false;
|
228 | }
|
229 | }
|
230 | }
|
231 |
|
232 | class ComposedChannelCredentialsImpl extends ChannelCredentials {
|
233 | constructor(
|
234 | private channelCredentials: SecureChannelCredentialsImpl,
|
235 | callCreds: CallCredentials
|
236 | ) {
|
237 | super(callCreds);
|
238 | }
|
239 | compose(callCredentials: CallCredentials) {
|
240 | const combinedCallCredentials =
|
241 | this.callCredentials.compose(callCredentials);
|
242 | return new ComposedChannelCredentialsImpl(
|
243 | this.channelCredentials,
|
244 | combinedCallCredentials
|
245 | );
|
246 | }
|
247 |
|
248 | _getConnectionOptions(): ConnectionOptions | null {
|
249 | return this.channelCredentials._getConnectionOptions();
|
250 | }
|
251 | _isSecure(): boolean {
|
252 | return true;
|
253 | }
|
254 | _equals(other: ChannelCredentials): boolean {
|
255 | if (this === other) {
|
256 | return true;
|
257 | }
|
258 | if (other instanceof ComposedChannelCredentialsImpl) {
|
259 | return (
|
260 | this.channelCredentials._equals(other.channelCredentials) &&
|
261 | this.callCredentials._equals(other.callCredentials)
|
262 | );
|
263 | } else {
|
264 | return false;
|
265 | }
|
266 | }
|
267 | }
|