@@ -26,6 +26,7 @@ export interface KeyCertPair {
26
26
export abstract class ServerCredentials {
27
27
abstract _isSecure ( ) : boolean ;
28
28
abstract _getSettings ( ) : SecureServerOptions | null ;
29
+ abstract _equals ( other : ServerCredentials ) : boolean ;
29
30
30
31
static createInsecure ( ) : ServerCredentials {
31
32
return new InsecureServerCredentials ( ) ;
@@ -48,8 +49,8 @@ export abstract class ServerCredentials {
48
49
throw new TypeError ( 'checkClientCertificate must be a boolean' ) ;
49
50
}
50
51
51
- const cert = [ ] ;
52
- const key = [ ] ;
52
+ const cert : Buffer [ ] = [ ] ;
53
+ const key : Buffer [ ] = [ ] ;
53
54
54
55
for ( let i = 0 ; i < keyCertPairs . length ; i ++ ) {
55
56
const pair = keyCertPairs [ i ] ;
@@ -71,7 +72,7 @@ export abstract class ServerCredentials {
71
72
}
72
73
73
74
return new SecureServerCredentials ( {
74
- ca : rootCerts || getDefaultRootsData ( ) || undefined ,
75
+ ca : rootCerts ?? getDefaultRootsData ( ) ?? undefined ,
75
76
cert,
76
77
key,
77
78
requestCert : checkClientCertificate ,
@@ -88,6 +89,10 @@ class InsecureServerCredentials extends ServerCredentials {
88
89
_getSettings ( ) : null {
89
90
return null ;
90
91
}
92
+
93
+ _equals ( other : ServerCredentials ) : boolean {
94
+ return other instanceof InsecureServerCredentials ;
95
+ }
91
96
}
92
97
93
98
class SecureServerCredentials extends ServerCredentials {
@@ -105,4 +110,82 @@ class SecureServerCredentials extends ServerCredentials {
105
110
_getSettings ( ) : SecureServerOptions {
106
111
return this . options ;
107
112
}
113
+
114
+ /**
115
+ * Checks equality by checking the options that are actually set by
116
+ * createSsl.
117
+ * @param other
118
+ * @returns
119
+ */
120
+ _equals ( other : ServerCredentials ) : boolean {
121
+ if ( this === other ) {
122
+ return true ;
123
+ }
124
+ if ( ! ( other instanceof SecureServerCredentials ) ) {
125
+ return false ;
126
+ }
127
+ // options.ca equality check
128
+ if ( Buffer . isBuffer ( this . options . ca ) && Buffer . isBuffer ( other . options . ca ) ) {
129
+ if ( ! this . options . ca . equals ( other . options . ca ) ) {
130
+ return false ;
131
+ }
132
+ } else {
133
+ if ( this . options . ca !== other . options . ca ) {
134
+ return false ;
135
+ }
136
+ }
137
+ // options.cert equality check
138
+ if ( Array . isArray ( this . options . cert ) && Array . isArray ( other . options . cert ) ) {
139
+ if ( this . options . cert . length !== other . options . cert . length ) {
140
+ return false ;
141
+ }
142
+ for ( let i = 0 ; i < this . options . cert . length ; i ++ ) {
143
+ const thisCert = this . options . cert [ i ] ;
144
+ const otherCert = other . options . cert [ i ] ;
145
+ if ( Buffer . isBuffer ( thisCert ) && Buffer . isBuffer ( otherCert ) ) {
146
+ if ( ! thisCert . equals ( otherCert ) ) {
147
+ return false ;
148
+ }
149
+ } else {
150
+ if ( thisCert !== otherCert ) {
151
+ return false ;
152
+ }
153
+ }
154
+ }
155
+ } else {
156
+ if ( this . options . cert !== other . options . cert ) {
157
+ return false ;
158
+ }
159
+ }
160
+ // options.key equality check
161
+ if ( Array . isArray ( this . options . key ) && Array . isArray ( other . options . key ) ) {
162
+ if ( this . options . key . length !== other . options . key . length ) {
163
+ return false ;
164
+ }
165
+ for ( let i = 0 ; i < this . options . key . length ; i ++ ) {
166
+ const thisKey = this . options . key [ i ] ;
167
+ const otherKey = other . options . key [ i ] ;
168
+ if ( Buffer . isBuffer ( thisKey ) && Buffer . isBuffer ( otherKey ) ) {
169
+ if ( ! thisKey . equals ( otherKey ) ) {
170
+ return false ;
171
+ }
172
+ } else {
173
+ if ( thisKey !== otherKey ) {
174
+ return false ;
175
+ }
176
+ }
177
+ }
178
+ } else {
179
+ if ( this . options . key !== other . options . key ) {
180
+ return false ;
181
+ }
182
+ }
183
+ // options.requestCert equality check
184
+ if ( this . options . requestCert !== other . options . requestCert ) {
185
+ return false ;
186
+ }
187
+ /* ciphers is derived from a value that is constant for the process, so no
188
+ * equality check is needed. */
189
+ return true ;
190
+ }
108
191
}
0 commit comments