deno_node 0.192.0

Node compatibility for Deno
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
// Copyright 2018-2026 the Deno authors. MIT license.
// Copyright Joyent and Node contributors. All rights reserved. MIT license.
//
// Ported from Node.js lib/internal/tls/wrap.js

(function () {
const { core, primordials } = __bootstrap;
const {
  ArrayFrom,
  ArrayPrototypeFind,
  ArrayPrototypeForEach,
  ArrayPrototypeIncludes,
  ArrayPrototypeJoin,
  ArrayPrototypePush,
  ArrayPrototypeSome,
  DataViewPrototypeGetBuffer,
  DataViewPrototypeGetByteLength,
  DataViewPrototypeGetByteOffset,
  Error,
  FunctionPrototypeApply,
  FunctionPrototypeBind,
  FunctionPrototypeCall,
  JSONParse,
  JSONStringify,
  ObjectDefineProperty,
  ObjectPrototypeIsPrototypeOf,
  ObjectSetPrototypeOf,
  RegExpPrototypeExec,
  RegExpPrototypeTest,
  SafeArrayIterator,
  SafeRegExp,
  String,
  StringFromCharCode,
  StringPrototypeCharCodeAt,
  StringPrototypeEndsWith,
  StringPrototypeIncludes,
  StringPrototypeIndexOf,
  StringPrototypeMatch,
  StringPrototypeSlice,
  StringPrototypeSplit,
  StringPrototypeStartsWith,
  StringPrototypeSubstring,
  StringPrototypeToLowerCase,
  Symbol,
  SymbolFor,
  TypedArrayPrototypeGetBuffer,
  TypedArrayPrototypeGetByteLength,
  TypedArrayPrototypeGetByteOffset,
} = primordials;
const { op_get_env_no_permission_check } = core.ops;
const {
  ArrayIsArray,
  ObjectAssign,
  StringPrototypeReplace,
} = core.loadExtScript("ext:deno_node/internal/primordials.mjs");
const assert = core.loadExtScript(
  "ext:deno_node/internal/assert.mjs",
);
const net = core.createLazyLoader("node:net")().default;
const {
  createSecureContext,
  translatePeerCertificate,
} = core.loadExtScript("ext:deno_node/_tls_common.ts");
const { JSStreamSocket } = core.loadExtScript(
  "ext:deno_node/internal/js_stream_socket.js",
);
const { convertALPNProtocols } = core.loadExtScript(
  "ext:deno_node/internal/tls_common.js",
);
const { Buffer } = core.loadExtScript("ext:deno_node/internal/buffer.mjs");
const {
  connResetException,
  ERR_INVALID_ARG_TYPE,
  ERR_INVALID_ARG_VALUE,
  ERR_TLS_ALPN_CALLBACK_INVALID_RESULT,
  ERR_TLS_ALPN_CALLBACK_WITH_PROTOCOLS,
  ERR_TLS_CERT_ALTNAME_INVALID,
  ERR_TLS_REQUIRED_SERVER_NAME,
  ERR_TLS_SNI_FROM_SERVER,
} = core.loadExtScript("ext:deno_node/internal/errors.ts");
const { debuglog } = core.loadExtScript(
  "ext:deno_node/internal/util/debuglog.ts",
);
const {
  constants: TCPConstants,
  TCP,
} = core.loadExtScript("ext:deno_node/internal_binding/tcp_wrap.ts");
const { kMaybeDestroy } = core.loadExtScript(
  "ext:deno_node/internal/stream_base_commons.ts",
);
const { kReinitializeHandle } = core.loadExtScript(
  "ext:deno_node/internal/net.ts",
);
const {
  constants: PipeConstants,
  Pipe,
} = core.loadExtScript("ext:deno_node/internal_binding/pipe_wrap.ts");
const { kEmptyObject } = core.loadExtScript(
  "ext:deno_node/internal/util.mjs",
);
const { nextTick } = core.loadExtScript("ext:deno_node/_next_tick.ts");
const {
  validateFunction,
  validateInt32,
  validateNumber,
  validateObject,
} = core.loadExtScript("ext:deno_node/internal/validators.mjs");
const { isArrayBufferView, isTypedArray } = core.loadExtScript(
  "ext:deno_node/internal/util/types.ts",
);
const { op_tls_canonicalize_ipv4_address } = core.ops;
const { default: tlsWrap } = core.loadExtScript(
  "ext:deno_node/internal_binding/tls_wrap.ts",
);
const { ownerSymbol } = core.loadExtScript(
  "ext:deno_node/internal_binding/symbols.ts",
);
const { X509Certificate } = core.loadExtScript(
  "ext:deno_node/internal/crypto/x509.ts",
);

const lazyTls = core.createLazyLoader("node:tls");

const kConnectOptions = Symbol("connect-options");
const kHandshakeTimer = Symbol("handshake-timer");
const kIsVerified = Symbol("verified");
const kPendingSession = Symbol("pendingSession");
const kRes = Symbol("res");
const kErrorEmitted = Symbol("error-emitted");
const noop = () => {};

// Use the matching prototype getter so a forged accessor on the view can't
// redirect us to a different ArrayBuffer / out-of-bounds region.
function getViewBuffer(view) {
  return isTypedArray(view)
    ? TypedArrayPrototypeGetBuffer(view)
    : DataViewPrototypeGetBuffer(view);
}
function getViewByteOffset(view) {
  return isTypedArray(view)
    ? TypedArrayPrototypeGetByteOffset(view)
    : DataViewPrototypeGetByteOffset(view);
}
function getViewByteLength(view) {
  return isTypedArray(view)
    ? TypedArrayPrototypeGetByteLength(view)
    : DataViewPrototypeGetByteLength(view);
}

let debug = debuglog("tls", (fn) => {
  debug = fn;
});

function canonicalizeIP(ip) {
  return op_tls_canonicalize_ipv4_address(ip);
}

function getDefaultProtocolVersions() {
  const tls = lazyTls().default;
  return {
    minVersion: tls.DEFAULT_MIN_VERSION,
    maxVersion: tls.DEFAULT_MAX_VERSION,
  };
}

function emitPendingSession(socket) {
  const session = socket[kPendingSession];
  socket[kPendingSession] = null;
  if (ArrayIsArray(session)) {
    for (let i = 0; i < session.length; i++) {
      socket.emit("session", session[i]);
    }
  } else if (session) {
    socket.emit("session", session);
  }
}

function toBufferLike(value) {
  if (!value) {
    return undefined;
  }
  if (typeof value === "string") {
    return Buffer.from(value);
  }
  if (isArrayBufferView(value)) {
    return Buffer.from(
      getViewBuffer(value),
      getViewByteOffset(value),
      getViewByteLength(value),
    );
  }
  return undefined;
}

function getContextCertValue(socket) {
  const context = socket._tlsOptions?.secureContext?.context ??
    socket._tlsOptions?.secureContext;
  let cert = context?.cert ?? socket._tlsOptions?.cert;
  if (ArrayIsArray(cert)) {
    cert = cert[0];
  }
  return toBufferLike(cert);
}

function getContextCAValue(socket) {
  const context = socket._tlsOptions?.secureContext?.context ??
    socket._tlsOptions?.secureContext;
  return context?.ca ?? socket._tlsOptions?.ca;
}

function setIssuerCertificate(cert, issuer) {
  if (issuer) {
    ObjectDefineProperty(cert, "issuerCertificate", {
      __proto__: null,
      configurable: true,
      enumerable: true,
      value: issuer,
      writable: false,
    });
  }
  return cert;
}

const pemCertificatePattern = new SafeRegExp(
  "-----BEGIN CERTIFICATE-----[\\s\\S]*?-----END CERTIFICATE-----",
  "g",
);

function splitPEMCertificates(value) {
  const str = typeof value === "string" ? value : String(value);
  return StringPrototypeMatch(str, pemCertificatePattern) ?? [];
}

function toLegacyCACertificates(ca) {
  if (!ca) {
    return [];
  }
  const values = ArrayIsArray(ca) ? ca : [ca];
  const certs = [];
  for (const value of new SafeArrayIterator(values)) {
    if (typeof value === "string") {
      for (const pem of new SafeArrayIterator(splitPEMCertificates(value))) {
        ArrayPrototypePush(certs, new X509Certificate(pem).toLegacyObject());
      }
    } else {
      const input = toBufferLike(value) ?? value;
      ArrayPrototypePush(certs, new X509Certificate(input).toLegacyObject());
    }
  }
  return certs;
}

function sameDistinguishedName(a, b) {
  return JSONStringify(a) === JSONStringify(b);
}

function completePeerCertificateChainFromCA(lastCert, ca) {
  const caCerts = toLegacyCACertificates(ca);
  let current = lastCert;
  for (;;) {
    if (
      !current?.issuer ||
      sameDistinguishedName(current.subject, current.issuer)
    ) {
      return current;
    }
    const issuer = ArrayPrototypeFind(
      caCerts,
      (cert) => sameDistinguishedName(cert.subject, current.issuer),
    );
    if (!issuer) {
      return current;
    }
    current.issuerCertificate = issuer;
    current = issuer;
  }
}

function getPeerCertificateChain(handle) {
  return handle?.getPeerCertificateChain?.()?.certificates ?? null;
}

function buildPeerLegacyCertificate(socket) {
  const handle = socket._handle;
  const cert = handle?.getPeerCertificate?.(true);
  if (!cert) {
    return {};
  }

  const chain = getPeerCertificateChain(handle);
  if (chain?.length) {
    let current = cert;
    for (let i = 1; i < chain.length; i++) {
      const issuer = new X509Certificate(chain[i]).toLegacyObject();
      current.issuerCertificate = issuer;
      current = issuer;
    }
    current = completePeerCertificateChainFromCA(
      current,
      getContextCAValue(socket),
    );
    // Self-signed root: issuerCertificate points to itself (matches Node.js)
    if (
      current.subject && current.issuer &&
      sameDistinguishedName(current.subject, current.issuer)
    ) {
      current.issuerCertificate = current;
    }
  }

  return translatePeerCertificate(cert) || {};
}

// ---------------------------------------------------------------------------
// TLSWrap callbacks - called on the native TLSWrap handle (this === handle)
// ---------------------------------------------------------------------------

function onhandshakedone() {
  debug("client onhandshakedone");
  const owner = this._owner;
  if (owner) owner._finishInit();
}

function onerror(err) {
  const owner = this._owner;
  if (!owner) return;

  debug(
    "%s onerror %s had? %j",
    owner._tlsOptions?.isServer ? "server" : "client",
    err,
    owner._hadError,
  );

  if (owner._hadError) return;
  owner._hadError = true;

  if (!owner._secureEstablished) {
    owner._closeAfterHandlingError = true;
    owner.destroy(err);
  } else {
    owner._emitTLSError(err);
  }
}

// ---------------------------------------------------------------------------
// initRead - start data flowing from the TLSWrap handle
// ---------------------------------------------------------------------------
function initRead(tlsSocket, socket) {
  debug(
    "%s initRead",
    tlsSocket._tlsOptions?.isServer ? "server" : "client",
    "handle?",
    !!tlsSocket._handle,
    "buffered?",
    !!socket && socket.readableLength,
  );
  if (!tlsSocket._handle) return;

  // If the underlying socket already has buffered data, feed it to TLSWrap
  if (socket?.readableLength) {
    let buf;
    while ((buf = socket.read()) !== null) {
      tlsSocket._handle.receive(buf);
    }
  }

  tlsSocket.read(0);
}

// ---------------------------------------------------------------------------
// TLSSocket - the main class
// ---------------------------------------------------------------------------

function TLSSocket(socket, opts) {
  const tlsOptions = { ...opts };

  this._tlsOptions = tlsOptions;
  this._secureEstablished = false;
  this._securePending = false;
  this._newSessionPending = false;
  this._controlReleased = false;
  this.secureConnecting = true;
  this._SNICallback = null;
  this.servername = null;
  this.alpnProtocol = null;
  this.authorized = false;
  this.authorizationError = null;
  this[kRes] = null;
  this[kIsVerified] = false;
  this[kPendingSession] = null;
  this._session = null;
  this._sessionReused = false;

  let wrap;
  let handle;

  if (socket) {
    if (
      ObjectPrototypeIsPrototypeOf(net.Socket.prototype, socket) &&
      socket._handle
    ) {
      // If the handle is a native TCP or Pipe, we can attach directly.
      // For other handles (e.g. TLSWrap from another TLS connection),
      // wrap via JSStreamSocket so TLSWrap gets a JS stream interface.
      const h = socket._handle;
      if (
        ObjectPrototypeIsPrototypeOf(TCP.prototype, h) ||
        ObjectPrototypeIsPrototypeOf(Pipe.prototype, h)
      ) {
        wrap = socket;
      } else {
        wrap = new JSStreamSocket(socket);
      }
    } else {
      wrap = new JSStreamSocket(socket);
    }
    handle = wrap._handle;
  } else {
    wrap = null;
  }

  // Just a documented property to make secure sockets
  // distinguishable from regular ones.
  this.encrypted = true;

  // Validate SNICallback early (before _wrapHandle eagerly inits TLS)
  if (tlsOptions.isServer && tlsOptions.SNICallback) {
    validateFunction(tlsOptions.SNICallback, "options.SNICallback");
  }

  FunctionPrototypeCall(net.Socket, this, {
    handle: this._wrapHandle(wrap, handle),
    allowHalfOpen: socket ? socket.allowHalfOpen : tlsOptions.allowHalfOpen,
    autoDestroy: true,
    pauseOnCreate: tlsOptions.pauseOnConnect,
    manualStart: true,
    highWaterMark: tlsOptions.highWaterMark,
    onread: !socket ? tlsOptions.onread : null,
    signal: tlsOptions.signal,
  });

  // Proxy for API compatibility
  this.ssl = this._handle;

  this.on("error", this._tlsError);

  this._init(socket, wrap);

  // Implement kMaybeDestroy so that onStreamRead (stream_base_commons.ts)
  // can auto-destroy the socket when EOF is received. Without this, the
  // native TCP handle keeps a ref on the event loop forever.
  this[kMaybeDestroy] = () => {
    if (!this.destroyed) {
      const rDone = this._readableState?.ended || this.readableEnded;
      const wDone = this._writableState?.finished || this.writableFinished;
      if (rDone && wDone) {
        this.destroy();
      }
    }
  };
  this.on("finish", this[kMaybeDestroy]);

  // Read on next tick so the caller has a chance to setup listeners
  nextTick(initRead, this, socket);
}
ObjectSetPrototypeOf(TLSSocket.prototype, net.Socket.prototype);
ObjectSetPrototypeOf(TLSSocket, net.Socket);

// Override kReinitializeHandle so that Happy Eyeballs (autoSelectFamily)
// re-creates the TLSWrap when falling back to a different address.
// The base Socket version replaces _handle with a raw TCP handle, which
// doesn't have TLS methods like start(). We need to re-wrap it.
TLSSocket.prototype[kReinitializeHandle] = function (handle) {
  const oldHandle = this._handle;
  // Save callbacks from the old TLSWrap that were set up by _init() and
  // _initSocketHandle(). These need to be restored on the new TLSWrap.
  const savedOnread = oldHandle?.onread;
  const savedOnhandshakedone = oldHandle?.onhandshakedone;
  const savedOnhandshakestart = oldHandle?.onhandshakestart;
  const savedOnerror = oldHandle?.onerror;

  if (oldHandle) {
    oldHandle.close();
  }

  // Re-wrap the new TCP handle with a TLSWrap. This creates the
  // rustls config, attaches to the TCP handle, and sets up read
  // interception + proxy methods (connect, bind, etc.).
  const tlsHandle = this._wrapHandle(null, handle);

  this._handle = tlsHandle;
  this._handle[ownerSymbol] = this;
  this.ssl = this._handle;
  this[kRes] = tlsHandle;

  // Restore callbacks on the new TLSWrap.
  if (savedOnread) this._handle.onread = savedOnread;
  if (savedOnhandshakedone) {
    this._handle.onhandshakedone = savedOnhandshakedone;
  }
  if (savedOnhandshakestart) {
    this._handle.onhandshakestart = savedOnhandshakestart;
  }
  if (savedOnerror) this._handle.onerror = savedOnerror;

  // Re-apply verify mode and ALPN from TLS options.
  const options = this._tlsOptions;
  const requestCert = !!options.requestCert || !options.isServer;
  const rejectUnauthorized = !!options.rejectUnauthorized;
  if (requestCert || rejectUnauthorized) {
    this._handle.setVerifyMode(requestCert, rejectUnauthorized);
  }
  if (options.ALPNProtocols) {
    this._handle.setAlpnProtocols(options.ALPNProtocols);
  }

  this._undestroy();
  this._sockname = undefined;
};

tlsWrap.TLSWrap.prototype.close = function close(cb) {
  // Signal to Rust that this TLSWrap is being closed, so it won't
  // send buffered application data after a failed identity check.
  this.setClosing();

  let ssl;
  if (this._owner) {
    ssl = this._owner.ssl;
    this._owner.ssl = null;
  }

  // deno-lint-ignore no-this-alias
  const self = this;
  const done = () => {
    self.cancelWrite();
    if (ssl) {
      ssl.destroySsl();
    }
    // Close the native TCP handle to remove it from the event loop.
    if (self._nativeTcpHandle) {
      self._nativeTcpHandle.readStop();
      self._nativeTcpHandle.close();
      self._nativeTcpHandle = null;
    }
    if (cb) cb();
  };

  if (this._parentWrap) {
    if (this._parentWrap._handle === null) {
      nextTick(done);
      return;
    }

    if (this._parentWrap._handle === this._parent) {
      this._parentWrap.once("close", done);
      this._parentWrap.destroy();
      return;
    }
  }

  // Defer so callers can register "close" listeners after destroy().
  nextTick(done);
};

TLSSocket.prototype._wrapHandle = function (wrap, handle) {
  const options = this._tlsOptions;
  if (!handle) {
    handle = options.pipe
      ? new Pipe(PipeConstants.SOCKET)
      : new TCP(TCPConstants.SOCKET);
  }

  // Wrap socket's handle with TLSWrap
  const context = options.secureContext ||
    options.credentials ||
    createSecureContext(options);
  const secureContext = {
    ...(context?.context ?? context),
    rejectUnauthorized: options.rejectUnauthorized !== false,
    requestCert: options.requestCert === true,
  };

  // Get the native TCP handle for attachment
  const nativeHandle = handle;

  // Derive servername for SNI. Default to host, or the wrapped socket's host.
  let servername = options.servername ?? options.host ?? wrap?._host;
  if (servername && StringPrototypeEndsWith(servername, ".")) {
    servername = StringPrototypeSlice(servername, 0, -1);
  }

  const res = tlsWrap.wrap(
    nativeHandle,
    secureContext,
    !!options.isServer,
    servername,
  );

  // If TLS initialization failed (e.g. missing cert/key), store the error
  // so it can be emitted asynchronously instead of crashing the process.
  if (res._initError) {
    this._initError = res._initError;
  }

  res._parent = handle; // C++ "wrap" object: TCPWrap, etc.
  res._parentWrap = wrap; // JS object: net.Socket, etc.
  res._secureContext = context;
  res.reading = handle.reading;
  res._owner = this;
  res[ownerSymbol] = this; // For onWriteComplete to find the socket
  this[kRes] = res;

  // Set ownerSymbol on the parent handle so that connect callbacks
  // (which receive the TCP handle, not the TLSWrap) can find the socket.
  // If we are wrapping a net.Socket that still needs to emit its connect
  // event, keep the raw socket as owner until that event has fired.
  if (
    !(ObjectPrototypeIsPrototypeOf(net.Socket.prototype, wrap) &&
      !wrap.remoteAddress)
  ) {
    handle[ownerSymbol] = this;
  }

  // Proxy methods from the parent TCP handle that callers expect on _handle.
  // In Node, TLSWrap is a StreamBase that delegates these to the underlying
  // stream. We proxy them explicitly here.
  const proxyMethods = [
    "setNetPermToken",
    "getsockname",
    "getpeername",
    "connect",
    "connect6",
    "bind",
    "bind6",
    "listen",
    "ref",
    "unref",
    "setNoDelay",
    "setKeepAlive",
  ];
  for (const method of new SafeArrayIterator(proxyMethods)) {
    if (typeof handle[method] === "function") {
      res[method] = FunctionPrototypeBind(handle[method], handle);
    }
  }

  // Proxy the reading property
  defineHandleReading(this, handle);

  // For JS streams (pull-based enc out), wrap write methods to drain
  // buffered encrypted output after each write op returns.
  if (res._flushEncOut) {
    const flush = res._flushEncOut;
    for (
      const method of new SafeArrayIterator([
        "writeBuffer",
        "writeUtf8String",
        "writeAsciiString",
        "writeLatin1String",
        "writeUcs2String",
        "writev",
      ])
    ) {
      const orig = res[method];
      if (typeof orig === "function") {
        res[method] = function (...args) {
          const ret = FunctionPrototypeApply(orig, res, args);
          flush();
          return ret;
        };
      }
    }
  }

  if (wrap) {
    wrap.on("close", () => this.destroy());
  }

  return res;
};

function defineHandleReading(socket, handle) {
  ObjectDefineProperty(handle, "reading", {
    __proto__: null,
    get: () => {
      return socket[kRes].reading;
    },
    set: (value) => {
      socket[kRes].reading = value;
    },
  });
}

TLSSocket.prototype._destroySSL = function _destroySSL() {
  if (!this.ssl) return;
  this.ssl.destroySsl();
  this.ssl = null;
  this[kPendingSession] = null;
  this[kIsVerified] = false;
};

TLSSocket.prototype.disableRenegotiation = function disableRenegotiation() {
  // Renegotiation not supported by rustls - this is a no-op
};

// Constructor guts - sets up callbacks on the TLSWrap handle
TLSSocket.prototype._init = function (socket, wrap) {
  const options = this._tlsOptions;
  const ssl = this._handle;
  this.server = options.server;

  debug(
    "%s _init",
    options.isServer ? "server" : "client",
    "handle?",
    !!ssl,
  );

  const requestCert = !!options.requestCert || !options.isServer;
  const rejectUnauthorized = !!options.rejectUnauthorized;

  this._requestCert = requestCert;
  this._rejectUnauthorized = rejectUnauthorized;
  if (requestCert || rejectUnauthorized) {
    ssl.setVerifyMode(requestCert, rejectUnauthorized);
  }

  if (options.isServer) {
    ssl.onhandshakestart = noop;
    ssl.onhandshakedone = function () {
      debug("server onhandshakedone");
      const owner = this._owner;
      if (!owner) return;
      if (owner._newSessionPending) {
        owner._securePending = true;
        return;
      }
      owner._finishInit();
    };

    // Enable Acceptor-based handshake when SNICallback or ALPNCallback
    // is set so we can intercept the ClientHello and invoke the callbacks
    // before completing the TLS handshake.
    const sniCb = options.SNICallback;
    const alpnCb = options.ALPNCallback;
    if (sniCb || alpnCb) {
      ssl.enableClientHelloCb();
      ssl.onclienthello = function () {
        const owner = this._owner;
        if (!owner) return;
        const handle = owner._handle;
        if (!handle) return;

        const servername = handle.getServername() || "";
        const alpnProtocols = handle.getClientHelloAlpn();

        const finish = (ctx, selectedAlpn) => {
          if (owner.destroyed) return;
          let context;
          if (ctx) {
            // ctx must be a SecureContext (has .context) or a raw context
            // object with cert/key.  An empty object like {} is invalid --
            // Node's C++ layer rejects it as "Invalid SNI context".
            context = ctx.context;
            if (!context) {
              // ctx is not a valid SecureContext -- Node's C++ layer
              // rejects this as "Invalid SNI context".  Destroy the
              // underlying TCP socket so the client sees ECONNRESET,
              // then destroy the TLS socket with an error which
              // triggers the "tlsClientError" event via onSocketTLSError.
              const rawSocket = owner._parent || handle._parentWrap;
              if (rawSocket && typeof rawSocket.destroy === "function") {
                rawSocket.destroy();
              }
              owner.destroy(new Error("Invalid SNI context"));
              return;
            }
          } else {
            context = owner._tlsOptions.secureContext?.context;
          }
          if (!context) {
            owner.destroy(new Error("No SecureContext available"));
            return;
          }
          // Merge server-level requestCert/rejectUnauthorized into the
          // context so build_server_config picks them up.  The
          // SecureContext's .context only has cert/key/ca/protocol
          // options, not connection-level settings.
          const mergedContext = {
            ...context,
            requestCert: owner._tlsOptions.requestCert === true,
            rejectUnauthorized: owner._tlsOptions.rejectUnauthorized !== false,
          };
          handle.finishAccept(
            mergedContext,
            selectedAlpn != null ? String(selectedAlpn) : null,
          );
        };

        const handleAlpn = (sniCtx) => {
          let selectedAlpn = null;
          let keyCertCtx = null;
          if (alpnCb && alpnProtocols.length > 0) {
            const protocols = ArrayFrom(alpnProtocols);
            const alpnThis = {
              setKeyCert(keyCert) {
                if (keyCert?.context) {
                  keyCertCtx = keyCert;
                } else {
                  keyCertCtx = createSecureContext(keyCert);
                }
              },
            };
            selectedAlpn = FunctionPrototypeCall(alpnCb, alpnThis, {
              servername,
              protocols,
            });
            if (selectedAlpn == null) {
              // Callback returned undefined/null -- reject ALPN.
              // Destroy the socket which sends a connection reset.
              owner.destroy();
              return;
            }
            selectedAlpn = String(selectedAlpn);
            if (!ArrayPrototypeIncludes(protocols, selectedAlpn)) {
              owner.destroy(new ERR_TLS_ALPN_CALLBACK_INVALID_RESULT());
              return;
            }
          }
          finish(keyCertCtx || sniCtx, selectedAlpn);
        };

        if (sniCb) {
          sniCb(servername, (err, ctx) => {
            if (err) {
              owner.destroy(err);
              return;
            }
            handleAlpn(ctx);
          });
        } else {
          handleAlpn(null);
        }
      };
    }
  } else {
    ssl.onhandshakestart = noop;
    ssl.onhandshakedone = onhandshakedone;

    if (options.session) {
      ssl.setSession(options.session);
    }
  }

  if (options.ALPNProtocols) {
    ssl.setAlpnProtocols(options.ALPNProtocols);
  }

  ssl.onerror = onerror;

  // Set SNICallback (already validated in constructor)
  if (options.isServer && options.SNICallback) {
    this._SNICallback = options.SNICallback;
  }

  if (options.handshakeTimeout > 0) {
    this[kHandshakeTimer] = core.createSystemTimer(
      () => {
        core.cancelTimer(this[kHandshakeTimer]);
        this[kHandshakeTimer] = null;
        this._handleTimeout();
      },
      options.handshakeTimeout,
    );
  }

  if (ObjectPrototypeIsPrototypeOf(net.Socket.prototype, socket)) {
    this._parent = socket;

    this.connecting = socket.connecting || !socket._handle;
    socket.once("connect", () => {
      if (this.destroyed) {
        return;
      }

      this.connecting = false;
      // If the original socket created its own TCP handle during
      // connect() (because it had no handle when we wrapped it),
      // re-attach the TLS wrap to the socket's actual TCP handle.
      if (ssl && socket._handle && ssl._nativeTcpHandle !== socket._handle) {
        const nativeHandle = socket._handle;
        ssl._attachNativeHandle(nativeHandle);
        nativeHandle[ownerSymbol] = this;
      } else if (ssl && socket._handle) {
        socket._handle[ownerSymbol] = this;
        ssl._installNativeOnread?.(socket._handle);
      }
      nextTick(() => {
        if (!this.destroyed) {
          this.emit("connect");
        }
      });
    });
  }

  if (wrap) {
    wrap.on("error", (err) => this._emitTLSError(err));
  } else {
    assert(!socket);
    this.connecting = true;
  }

  // Auto-start server-side TLS when the underlying socket is already
  // connected. This handles the STARTTLS pattern where a plain TCP socket
  // is wrapped with `new TLSSocket(socket, { isServer: true })` after a
  // plaintext exchange (SMTP, IMAP, XMPP, PostgreSQL, etc.).
  // Use nextTick so the caller can attach event listeners first.
  // Client-side sockets are started by tls.connect() instead.
  if (options.isServer && wrap && !this.connecting) {
    nextTick(() => {
      if (!this.destroyed) this._start();
    });
  }
};

TLSSocket.prototype.renegotiate = function (_options, callback) {
  // Renegotiation not supported by rustls
  if (callback) {
    nextTick(callback, new Error("Renegotiation not supported"));
  }
  return false;
};

TLSSocket.prototype.setMaxSendFragment = function setMaxSendFragment(size) {
  // Not applicable to rustls
  validateInt32(size, "size");
  if (size < 512 || size > 16384) return false;
  this._maxSendFragment = size;
  return true;
};

TLSSocket.prototype._handleTimeout = function () {
  this._emitTLSError(new Error("TLS handshake timeout"));
};

TLSSocket.prototype._emitTLSError = function (err) {
  const e = this._tlsError(err);
  if (e) this.emit("error", e);
};

TLSSocket.prototype._tlsError = function (err) {
  this.emit("_tlsError", err);
  if (this._controlReleased) return err;
  return null;
};

TLSSocket.prototype._releaseControl = function () {
  if (this._controlReleased) return false;
  this._controlReleased = true;
  this.removeListener("error", this._tlsError);
  return true;
};

TLSSocket.prototype._finishInit = function () {
  if (!this._handle) return;

  try {
    const alpnOut = {};
    this._handle.getAlpnNegotiatedProtocol(alpnOut);
    this.alpnProtocol = alpnOut.alpnProtocol || false;
    if (this.servername === null) {
      const sni = this._handle.getServername?.();
      this.servername = (sni !== undefined && sni !== null && sni !== "")
        ? sni
        : (this._tlsOptions?.servername ?? null);
    }
  } catch (_e) {
    // getAlpnNegotiatedProtocol/getServername may not be available
  }

  debug(
    "%s _finishInit",
    this._tlsOptions?.isServer ? "server" : "client",
    "handle?",
    !!this._handle,
    "alpn",
    this.alpnProtocol,
    "servername",
    this.servername,
  );

  this._secureEstablished = true;
  this._tlsUpgraded = true;
  if (this[kHandshakeTimer]) {
    core.cancelTimer(this[kHandshakeTimer]);
    this[kHandshakeTimer] = null;
  }
  this.emit("secure");
};

TLSSocket.prototype._start = function () {
  debug(
    "%s _start",
    this._tlsOptions?.isServer ? "server" : "client",
    "handle?",
    !!this._handle,
    "connecting?",
    this.connecting,
  );
  if (this.connecting) {
    this.once("connect", this._start);
    return;
  }

  if (!this._handle) return;

  // If TLS init failed (e.g. unsupported protocol on client side),
  // emit the error and tear down instead of proceeding with the handshake.
  if (this._initError) {
    const err = this._initError;
    this._initError = null;
    this.destroy(err);
    return;
  }

  this._handle.start();

  // For JS streams, drain any encrypted output produced by start()
  // (e.g. TLS ClientHello). This is pull-based: Rust buffers the data
  // and JS flushes it after the op returns.
  if (this._handle._flushEncOut) {
    this._handle._flushEncOut();
  }

  // Start reading on the underlying native TCP handle so that encrypted
  // data flows to the TLSWrap via the JS onread interceptor.
  const tcpHandle = this._handle._nativeTcpHandle;
  if (tcpHandle) {
    // readStart caches the onread callback on first call. If it was already
    // called (e.g. by net.Socket.resume), we need to stop and restart to
    // pick up the new interceptor callback.
    tcpHandle.readStop();
    tcpHandle.readStart();
  }

  // Kick-start the TLS readable side. During start(), the handshake cycle
  // may have received and processed a close_notify (peer called end() before
  // we set up event listeners). The decrypted EOF is buffered in pending_eof
  // because inner.onread wasn't set yet. Call readStart() directly on the
  // TLSWrap to install onread and flush any pending data/EOF.
  if (this._handle) {
    this._handle.readStart();
  }
};

TLSSocket.prototype.setServername = function (name) {
  if (typeof name !== "string") {
    throw new ERR_INVALID_ARG_TYPE("name", "string", name);
  }
  if (this._tlsOptions?.isServer) {
    throw new ERR_TLS_SNI_FROM_SERVER();
  }
  this._handle?.setServername(name);
};

// Format prefixes for the synthetic session buffers we emit from
// onConnectSecure when rustls handles resumption internally. The encoded
// payload is `${servername ?? host ?? ""}:${port ?? ""}` -- see the
// matching emit sites in onConnectSecure.
const SYNTHETIC_SESSION_PREFIXES = [
  "deno-tls12-session:",
  "deno-tls13-session-ticket-1:",
  "deno-tls13-session-ticket-2:",
  "deno-tls13-dummy-session:",
];

function syntheticSessionMatches(buf, options) {
  if (!buf || !options) return false;
  const sessionKey = `${options.servername ?? options.host ?? ""}:${
    options.port ?? ""
  }`;
  // deno-lint-ignore prefer-primordials -- Buffer.prototype.toString(encoding)
  const text = buf.toString("latin1");
  for (const prefix of new SafeArrayIterator(SYNTHETIC_SESSION_PREFIXES)) {
    if (text === prefix + sessionKey) return true;
  }
  return false;
}

TLSSocket.prototype.setSession = function (_session) {
  if (typeof _session === "string") {
    _session = Buffer.from(_session, "latin1");
  }
  this._session = _session ? Buffer.from(_session) : null;
  // rustls drives session resumption itself; the buffer we hand back from
  // getSession() is synthetic and encodes the host:port it was issued for.
  // Only treat the next handshake as a resume when the caller is replaying
  // a buffer that matches *this* connection's host:port -- otherwise
  // accepting any deno-tls*-session prefix would let an attacker skip
  // checkServerIdentity() in onConnectSecure by forging a session for a
  // different name.
  this._sessionReused = syntheticSessionMatches(
    this._session,
    this[kConnectOptions],
  );
};

TLSSocket.prototype.getPeerCertificate = function (detailed) {
  if (!this._handle) {
    return null;
  }
  if (!detailed) {
    const cert = this._handle.getPeerCertificate(false);
    return translatePeerCertificate(cert) || {};
  }
  return buildPeerLegacyCertificate(this);
};

TLSSocket.prototype.getCertificate = function () {
  const cert = getContextCertValue(this);
  if (!cert) {
    return this._handle ? {} : null;
  }
  return translatePeerCertificate(
    new X509Certificate(cert).toLegacyObject(),
  ) ||
    {};
};

TLSSocket.prototype.getEphemeralKeyInfo = function () {
  return {};
};

TLSSocket.prototype.isSessionReused = function () {
  if (this._handle?.isSessionReused) {
    return this._handle.isSessionReused();
  }
  return this._sessionReused;
};

// Proxy TLSSocket handle methods
function makeSocketMethodProxy(name) {
  return function socketMethodProxy(...args) {
    if (this._handle) {
      const method = this._handle[name];
      return method != null
        ? FunctionPrototypeApply(method, this._handle, args)
        : undefined;
    }
    return null;
  };
}

TLSSocket.prototype.getCipher = function getCipher() {
  if (!this._handle) {
    return null;
  }
  const out = {};
  return this._handle.getCipher(out) === 0 ? out : null;
};

TLSSocket.prototype.getProtocol = function getProtocol() {
  if (!this._handle) {
    return null;
  }
  const out = {};
  return this._handle.getProtocol(out) === 0 ? out.protocol : null;
};

TLSSocket.prototype.getFinished = function getFinished() {
  const data = this._handle?.getFinished();
  return data
    ? Buffer.from(
      getViewBuffer(data),
      getViewByteOffset(data),
      getViewByteLength(data),
    )
    : undefined;
};

TLSSocket.prototype.getPeerFinished = function getPeerFinished() {
  const data = this._handle?.getPeerFinished();
  return data
    ? Buffer.from(
      getViewBuffer(data),
      getViewByteOffset(data),
      getViewByteLength(data),
    )
    : undefined;
};

TLSSocket.prototype.getSession = function getSession() {
  return this._session ?? null;
};

TLSSocket.prototype.getPeerX509Certificate = function getPeerX509Certificate() {
  const chain = getPeerCertificateChain(this._handle);
  if (!chain?.length) {
    return undefined;
  }
  const cert = new X509Certificate(chain[0]);
  const issuer = chain[1] ? new X509Certificate(chain[1]) : undefined;
  return setIssuerCertificate(cert, issuer);
};

TLSSocket.prototype.getX509Certificate = function getX509Certificate() {
  const cert = getContextCertValue(this);
  return cert ? new X509Certificate(cert) : undefined;
};

ArrayPrototypeForEach(["enableTrace"], (method) => {
  TLSSocket.prototype[method] = makeSocketMethodProxy(method);
});

// ---------------------------------------------------------------------------
// Server
// ---------------------------------------------------------------------------

function makeVerifyError(code) {
  if (!code) return null;
  const message = {
    CERT_HAS_EXPIRED: "certificate has expired",
    CERT_NOT_YET_VALID: "certificate is not yet valid",
    DEPTH_ZERO_SELF_SIGNED_CERT: "self-signed certificate",
    SELF_SIGNED_CERT_IN_CHAIN: "self-signed certificate in certificate chain",
    UNABLE_TO_GET_ISSUER_CERT: "unable to get issuer certificate",
    UNABLE_TO_GET_ISSUER_CERT_LOCALLY: "unable to get local issuer certificate",
    UNABLE_TO_VERIFY_LEAF_SIGNATURE: "unable to verify the first certificate",
  }[code] ?? code;
  const err = new Error(message);
  err.code = code;
  return err;
}

function onServerSocketSecure() {
  if (this._requestCert) {
    const verifyError = makeVerifyError(this._handle.verifyError());
    if (verifyError) {
      this.authorizationError = verifyError.code;

      if (this._rejectUnauthorized) {
        this.destroy();
        return;
      }
    } else if (this._handle.getPeerCertificate(false) == null) {
      // `rejectUnauthorized: false` lets the handshake complete even when
      // the client presented no certificate. rustls only invokes the
      // client cert verifier when a cert is actually presented, so
      // `verifyError()` is empty in this case. Match Node's behaviour
      // and surface "no client cert" as an authorization error rather
      // than reporting `authorized = true`.
      this.authorizationError = "UNABLE_TO_GET_ISSUER_CERT";
    } else {
      this.authorized = true;
    }
  } else {
    this.authorized = true;
  }

  if (!this.destroyed && this._releaseControl()) {
    debug("server emit secureConnection");
    this.secureConnecting = false;
    this._tlsOptions.server.emit("secureConnection", this);
  }
}

function onSocketTLSError(err) {
  if (!this._controlReleased && !this[kErrorEmitted]) {
    this[kErrorEmitted] = true;
    debug("server emit tlsClientError:", err);
    this._tlsOptions.server.emit("tlsClientError", err, this);
  }
}

function onSocketClose(err) {
  if (err) return;
  if (!this._controlReleased && !this[kErrorEmitted]) {
    this[kErrorEmitted] = true;
    const connReset = connResetException("socket hang up");
    this._tlsOptions.server.emit("tlsClientError", connReset, this);
  }
}

// Match a servername against the [pattern, context] pairs from addContext().
// Returns a SecureContext if found, null otherwise (use default).
// Patterns can be exact ("a.example.com") or wildcard ("*.test.com").
function matchContextByServername(contexts, servername) {
  if (!servername || contexts.length === 0) return null;
  const name = StringPrototypeToLowerCase(servername);
  for (let i = 0; i < contexts.length; i++) {
    const pair = contexts[i];
    const pattern = pair[0];
    const ctx = pair[1];
    const pat = StringPrototypeToLowerCase(pattern);
    if (pat === name) {
      return createSecureContext(ctx);
    }
    // Wildcard match: "*.test.com" matches "b.test.com" but not
    // "a.b.test.com" (only one level).
    if (StringPrototypeStartsWith(pat, "*.")) {
      const suffix = StringPrototypeSlice(pat, 1); // ".test.com"
      if (
        StringPrototypeEndsWith(name, suffix) &&
        StringPrototypeIndexOf(name, ".") === name.length - suffix.length
      ) {
        return createSecureContext(ctx);
      }
    }
  }
  return null;
}

function tlsConnectionListener(rawSocket) {
  debug("net.Server.on(connection): new TLSSocket");
  // When addContext() has been called but no explicit SNICallback is set,
  // provide a default callback that matches servernames against _contexts.
  // This mirrors Node's native OpenSSL SNI callback behavior.
  let sniCallback = this._SNICallback;
  if (!sniCallback && this._contexts.length > 0) {
    const contexts = this._contexts;
    sniCallback = (servername, cb) => {
      cb(null, matchContextByServername(contexts, servername));
    };
  }
  const socket = new TLSSocket(rawSocket, {
    secureContext: this._sharedCreds,
    isServer: true,
    server: this,
    requestCert: this.requestCert,
    rejectUnauthorized: this.rejectUnauthorized,
    ALPNProtocols: this.ALPNProtocols,
    ALPNCallback: this.ALPNCallback,
    SNICallback: sniCallback,
    pauseOnConnect: this.pauseOnConnect,
    handshakeTimeout: this._handshakeTimeout,
  });

  // TLS init can fail synchronously (e.g. missing cert/key, unsupported
  // protocol).  Emit as tlsClientError like Node does for handshake
  // failures instead of letting it surface as an uncaught exception.
  if (socket._initError) {
    const err = socket._initError;
    socket._initError = null;
    this.emit("tlsClientError", err, rawSocket);
    socket.destroy();
    rawSocket.destroy();
    return;
  }

  // Start the TLS handshake for server-side sockets
  socket._start();

  socket.on("secure", onServerSocketSecure);

  socket[kErrorEmitted] = false;
  socket.on("close", onSocketClose);
  socket.on("_tlsError", onSocketTLSError);
  socket.on("error", onSocketTLSError);
}

function Server(options, listener) {
  if (!ObjectPrototypeIsPrototypeOf(Server.prototype, this)) {
    return new Server(options, listener);
  }

  if (typeof options === "function") {
    listener = options;
    options = kEmptyObject;
  } else if (options == null || typeof options === "object") {
    options ??= kEmptyObject;
  } else {
    throw new ERR_INVALID_ARG_TYPE("options", "object", options);
  }

  this._contexts = [];
  this.requestCert = options.requestCert === true;
  this.rejectUnauthorized = options.rejectUnauthorized !== false;

  if (options.ALPNProtocols) {
    if (options.ALPNCallback) {
      throw new ERR_TLS_ALPN_CALLBACK_WITH_PROTOCOLS();
    }
    convertALPNProtocols(options.ALPNProtocols, this);
  }

  if (options.sessionTimeout != null) {
    validateInt32(
      options.sessionTimeout,
      "options.sessionTimeout",
      0,
    );
  }

  if (options.ticketKeys != null) {
    if (!isArrayBufferView(options.ticketKeys)) {
      throw new ERR_INVALID_ARG_TYPE(
        "options.ticketKeys",
        ["Buffer", "TypedArray", "DataView"],
        options.ticketKeys,
      );
    }
    if (getViewByteLength(options.ticketKeys) !== 48) {
      throw new ERR_INVALID_ARG_VALUE(
        "options.ticketKeys",
        getViewByteLength(options.ticketKeys),
        "must be exactly 48 bytes",
      );
    }
  }
  this._ticketKeys = options.ticketKeys == null
    ? Buffer.alloc(48)
    : Buffer.from(
      getViewBuffer(options.ticketKeys),
      getViewByteOffset(options.ticketKeys),
      getViewByteLength(options.ticketKeys),
    );

  this.setSecureContext(options);

  if (options.handshakeTimeout != null) {
    validateNumber(options.handshakeTimeout, "options.handshakeTimeout");
  }
  this._handshakeTimeout = options.handshakeTimeout || (120 * 1000);

  this._SNICallback = options.SNICallback;

  if (this._SNICallback) {
    validateFunction(this._SNICallback, "options.SNICallback");
  }

  if (options.ALPNCallback) {
    validateFunction(options.ALPNCallback, "options.ALPNCallback");
    this.ALPNCallback = options.ALPNCallback;
  }

  // Constructor call
  FunctionPrototypeCall(net.Server, this, options, tlsConnectionListener);

  if (listener) {
    this.on("secureConnection", listener);
  }
}

ObjectSetPrototypeOf(Server.prototype, net.Server.prototype);
ObjectSetPrototypeOf(Server, net.Server);

Server.prototype[SymbolFor("nodejs.rejection")] = function (
  err,
  event,
  sock,
) {
  if (event === "secureConnection" || event === "connection") {
    sock?.destroy(err);
  } else {
    this.emit("error", err);
  }
};

Server.prototype.setSecureContext = function (options) {
  validateObject(options, "options");
  const useVersionDefaults = !options.secureProtocol && !options.minVersion &&
    !options.maxVersion;
  const defaults = useVersionDefaults
    ? getDefaultProtocolVersions()
    : undefined;

  this._sharedCreds = createSecureContext({
    allowPartialTrustChain: options.allowPartialTrustChain,
    ca: options.ca,
    cert: options.cert,
    ciphers: options.ciphers,
    clientCertEngine: options.clientCertEngine,
    crl: options.crl,
    dhparam: options.dhparam,
    ecdhCurve: options.ecdhCurve,
    honorCipherOrder: options.honorCipherOrder !== undefined
      ? !!options.honorCipherOrder
      : true,
    key: options.key,
    maxVersion: options.maxVersion ?? defaults?.maxVersion,
    minVersion: options.minVersion ?? defaults?.minVersion,
    passphrase: options.passphrase,
    pfx: options.pfx,
    privateKeyEngine: options.privateKeyEngine,
    privateKeyIdentifier: options.privateKeyIdentifier,
    secureOptions: options.secureOptions,
    secureProtocol: options.secureProtocol,
    sessionIdContext: options.sessionIdContext,
    sessionTimeout: options.sessionTimeout,
    sigalgs: options.sigalgs,
    ticketKeys: options.ticketKeys,
  });
};

Server.prototype.addContext = function (servername, context) {
  if (!servername) {
    throw new ERR_TLS_REQUIRED_SERVER_NAME();
  }
  ArrayPrototypePush(this._contexts, [servername, context]);
};

Server.prototype.getTicketKeys = function getTicketKeys() {
  return Buffer.from(this._ticketKeys);
};

Server.prototype.setTicketKeys = function setTicketKeys(keys) {
  if (!isArrayBufferView(keys)) {
    throw new ERR_INVALID_ARG_TYPE(
      "keys",
      ["Buffer", "TypedArray", "DataView"],
      keys,
    );
  }
  if (getViewByteLength(keys) !== 48) {
    throw new Error("Session ticket keys must be a 48-byte buffer");
  }
  this._ticketKeys = Buffer.from(
    getViewBuffer(keys),
    getViewByteOffset(keys),
    getViewByteLength(keys),
  );
};

// ---------------------------------------------------------------------------
// connect
// ---------------------------------------------------------------------------

function onConnectEnd() {
  if (!this._hadError) {
    const options = this[kConnectOptions];
    this._hadError = true;
    const error = connResetException(
      "Client network socket disconnected " +
        "before secure TLS connection was " +
        "established",
    );
    error.path = options.path;
    error.host = options.host;
    error.port = options.port;
    error.localAddress = options.localAddress;
    this.destroy(error);
  }
}

function onConnectSecure() {
  const options = this[kConnectOptions];

  let verifyError = makeVerifyError(this._handle.verifyError());

  // Verify that server's identity matches its certificate's names.
  // Run this even on resumed sessions: rustls defers NotValidForName to
  // the JS layer, so a name-mismatched handshake can succeed at the TLS
  // layer (and have its session cached) before checkServerIdentity()
  // destroys it. Re-running here ensures a later resumption of that
  // session can't bypass the hostname check.
  if (!verifyError) {
    const hostname = options.servername ||
      options.host ||
      options.socket?._host ||
      "localhost";
    const cert = this.getPeerCertificate(true);
    verifyError = options.checkServerIdentity(hostname, cert);
  }

  if (verifyError) {
    this.authorized = false;
    this.authorizationError = verifyError.code || verifyError.message;

    if (options.rejectUnauthorized !== false) {
      this.destroy(verifyError);
      return;
    }
    debug(
      "client emit secureConnect. rejectUnauthorized: %s, " +
        "authorizationError: %s",
      options.rejectUnauthorized,
      this.authorizationError,
    );
  } else {
    this.authorized = true;
    debug("client emit secureConnect. authorized:", this.authorized);
  }

  if (!this._session && this.getProtocol() === "TLSv1.2") {
    const sessionKey = `${options.servername ?? options.host ?? ""}:${
      options.port ?? ""
    }`;
    this._session = Buffer.from(`deno-tls12-session:${sessionKey}`);
    this[kPendingSession] = this._session;
  } else if (!this._session && this.getProtocol() === "TLSv1.3") {
    const sessionKey = `${options.servername ?? options.host ?? ""}:${
      options.port ?? ""
    }`;
    this._session = Buffer.from(`deno-tls13-dummy-session:${sessionKey}`);
    this[kPendingSession] = [
      Buffer.from(`deno-tls13-session-ticket-1:${sessionKey}`),
      Buffer.from(`deno-tls13-session-ticket-2:${sessionKey}`),
    ];
    this.prependOnceListener("close", () => emitPendingSession(this));
  }

  this.secureConnecting = false;
  this.emit("secureConnect");

  this[kIsVerified] = true;
  if (!ArrayIsArray(this[kPendingSession])) {
    emitPendingSession(this);
  }

  this.removeListener("end", onConnectEnd);
}

function normalizeConnectArgs(listArgs) {
  const args = net._normalizeArgs(listArgs);
  const options = args[0];
  const cb = args[1];

  if (listArgs[1] !== null && typeof listArgs[1] === "object") {
    ObjectAssign(options, listArgs[1]);
  } else if (listArgs[2] !== null && typeof listArgs[2] === "object") {
    ObjectAssign(options, listArgs[2]);
  }

  return cb ? [options, cb] : [options];
}

function connect(...args) {
  if (args.length === 0) {
    createSecureContext({ ciphers: DEFAULT_CIPHERS });
  }
  args = normalizeConnectArgs(args);
  let options = args[0];
  const cb = args[1];
  const allowUnauthorized = getAllowUnauthorized();

  options = {
    rejectUnauthorized: !allowUnauthorized,
    ciphers: DEFAULT_CIPHERS,
    checkServerIdentity,
    minDHSize: 1024,
    ...options,
  };

  if (!options.secureProtocol && !options.minVersion && !options.maxVersion) {
    const defaults = getDefaultProtocolVersions();
    options.minVersion = defaults.minVersion;
    options.maxVersion = defaults.maxVersion;
  }

  if (!options.keepAlive) {
    options.singleUse = true;
  }

  validateFunction(
    options.checkServerIdentity,
    "options.checkServerIdentity",
  );
  validateNumber(options.minDHSize, "options.minDHSize", 1);

  // Reject IP addresses in servername (RFC 6066)
  if (options.servername && net.isIP(options.servername)) {
    throw new ERR_INVALID_ARG_VALUE(
      "options.servername",
      options.servername,
      "must not be an IP address",
    );
  }

  const context = options.secureContext || createSecureContext(options);

  // Default servername to host for SNI (matches Node.js behavior)
  if (!options.servername && options.host && !net.isIP(options.host)) {
    options.servername = options.host;
  }

  const tlssock = new TLSSocket(options.socket, {
    allowHalfOpen: options.allowHalfOpen,
    pipe: !!options.path,
    secureContext: context,
    isServer: false,
    requestCert: true,
    rejectUnauthorized: options.rejectUnauthorized !== false,
    session: options.session,
    ALPNProtocols: options.ALPNProtocols,
    highWaterMark: options.highWaterMark,
    servername: options.servername,
    onread: options.onread,
    signal: options.signal,
  });

  options.rejectUnauthorized = options.rejectUnauthorized !== false;

  tlssock[kConnectOptions] = options;

  if (cb) {
    tlssock.once("secureConnect", cb);
  }

  if (!options.socket) {
    if (options.timeout) {
      tlssock.setTimeout(options.timeout);
    }
    tlssock.connect(options, tlssock._start);
  }

  tlssock._releaseControl();

  if (options.session) {
    tlssock.setSession(options.session);
  }

  if (options.servername) {
    tlssock.setServername(options.servername);
  }

  if (options.socket) {
    tlssock._start();
  }

  tlssock.on("secure", onConnectSecure);
  tlssock.prependListener("end", onConnectEnd);

  return tlssock;
}

function getAllowUnauthorized() {
  return op_get_env_no_permission_check("NODE_TLS_REJECT_UNAUTHORIZED") ===
    "0";
}

function createServer(options, listener) {
  return new Server(options, listener);
}

// ---------------------------------------------------------------------------
// checkServerIdentity - certificate hostname verification
// ---------------------------------------------------------------------------

const jsonStringPattern = new SafeRegExp(
  '^"(?:[^"\\\\\\u0000-\\u001f]|\\\\(?:["\\\\/bfnrt]|u[0-9a-fA-F]{4}))*"',
);

const fqdnDotPattern = new SafeRegExp("[.]$");
const upperCasePattern = new SafeRegExp("[A-Z]", "g");
const nonAsciiPattern = new SafeRegExp("[^\\u0021-\\u007F]", "u");

function splitEscapedAltNames(altNames) {
  const result = [];
  let currentToken = "";
  let offset = 0;
  while (offset !== altNames.length) {
    const nextSep = StringPrototypeIndexOf(altNames, ",", offset);
    const nextQuote = StringPrototypeIndexOf(altNames, '"', offset);
    if (nextQuote !== -1 && (nextSep === -1 || nextQuote < nextSep)) {
      currentToken += StringPrototypeSubstring(altNames, offset, nextQuote);
      const match = RegExpPrototypeExec(
        jsonStringPattern,
        StringPrototypeSubstring(altNames, nextQuote),
      );
      if (!match) {
        const err = new Error("Invalid alt name format");
        err.code = "ERR_TLS_CERT_ALTNAME_FORMAT";
        throw err;
      }
      currentToken += JSONParse(match[0]);
      offset = nextQuote + match[0].length;
    } else if (nextSep !== -1) {
      currentToken += StringPrototypeSubstring(altNames, offset, nextSep);
      ArrayPrototypePush(result, currentToken);
      currentToken = "";
      offset = nextSep + 2;
    } else {
      currentToken += StringPrototypeSubstring(altNames, offset);
      offset = altNames.length;
    }
  }
  ArrayPrototypePush(result, currentToken);
  return result;
}

function unfqdn(host) {
  return StringPrototypeReplace(host, fqdnDotPattern, "");
}

function toLowerCase(c) {
  return StringFromCharCode(32 + StringPrototypeCharCodeAt(c, 0));
}

function splitHost(host) {
  return StringPrototypeSplit(
    StringPrototypeReplace(unfqdn(host), upperCasePattern, toLowerCase),
    ".",
  );
}

function check(hostParts, pattern, wildcards) {
  if (!pattern) return false;

  const patternParts = splitHost(pattern);

  if (hostParts.length !== patternParts.length) return false;
  if (ArrayPrototypeIncludes(patternParts, "")) return false;

  const isBad = (s) => RegExpPrototypeTest(nonAsciiPattern, s);
  if (ArrayPrototypeSome(patternParts, isBad)) return false;

  for (let i = hostParts.length - 1; i > 0; i -= 1) {
    if (hostParts[i] !== patternParts[i]) return false;
  }

  const hostSubdomain = hostParts[0];
  const patternSubdomain = patternParts[0];
  const patternSubdomainParts = StringPrototypeSplit(
    patternSubdomain,
    "*",
    3,
  );

  if (
    patternSubdomainParts.length === 1 ||
    StringPrototypeIncludes(patternSubdomain, "xn--")
  ) {
    return hostSubdomain === patternSubdomain;
  }

  if (!wildcards) return false;
  if (patternSubdomainParts.length > 2) return false;
  if (patternParts.length <= 2) return false;

  const prefix = patternSubdomainParts[0];
  const suffix = patternSubdomainParts[1];
  if (prefix.length + suffix.length > hostSubdomain.length) return false;
  if (!StringPrototypeStartsWith(hostSubdomain, prefix)) return false;
  if (!StringPrototypeEndsWith(hostSubdomain, suffix)) return false;

  return true;
}

function checkServerIdentity(hostname, cert) {
  const subject = cert.subject;
  const altNames = cert.subjectaltname;
  const dnsNames = [];
  const ips = [];

  hostname = "" + hostname;

  if (altNames) {
    const splitAltNames = StringPrototypeIncludes(altNames, '"')
      ? splitEscapedAltNames(altNames)
      : StringPrototypeSplit(altNames, ", ");
    ArrayPrototypeForEach(splitAltNames, (name) => {
      if (StringPrototypeStartsWith(name, "DNS:")) {
        ArrayPrototypePush(dnsNames, StringPrototypeSlice(name, 4));
      } else if (StringPrototypeStartsWith(name, "IP Address:")) {
        ArrayPrototypePush(
          ips,
          canonicalizeIP(StringPrototypeSlice(name, 11)),
        );
      }
    });
  }

  let valid = false;
  let reason = "Unknown reason";

  hostname = unfqdn(hostname);

  if (net.isIP(hostname)) {
    valid = ArrayPrototypeIncludes(ips, canonicalizeIP(hostname));
    if (!valid) {
      reason = `IP: ${hostname} is not in the cert's list: ` +
        ArrayPrototypeJoin(ips, ", ");
    }
  } else if (dnsNames.length > 0 || subject?.CN) {
    const hostParts = splitHost(hostname);
    const wildcard = (pattern) => check(hostParts, pattern, true);

    if (dnsNames.length > 0) {
      valid = ArrayPrototypeSome(dnsNames, wildcard);
      if (!valid) {
        reason =
          `Host: ${hostname}. is not in the cert's altnames: ${altNames}`;
      }
    } else {
      const cn = subject.CN;

      if (ArrayIsArray(cn)) {
        valid = ArrayPrototypeSome(cn, wildcard);
      } else if (cn) {
        valid = wildcard(cn);
      }

      if (!valid) {
        reason = `Host: ${hostname}. is not cert's CN: ${cn}`;
      }
    }
  } else {
    reason = "Cert does not contain a DNS name";
  }

  if (!valid) {
    return new ERR_TLS_CERT_ALTNAME_INVALID(reason, hostname, cert);
  }
}

// Order matters. Mirrors ALL_CIPHER_SUITES from rustls/src/suites.rs but
// using openssl cipher names instead.
const DEFAULT_CIPHERS = ArrayPrototypeJoin([
  // TLSv1.3 suites
  "AES256-GCM-SHA384",
  "AES128-GCM-SHA256",
  "TLS_CHACHA20_POLY1305_SHA256",
  // TLSv1.2 suites
  "ECDHE-ECDSA-AES256-GCM-SHA384",
  "ECDHE-ECDSA-AES128-GCM-SHA256",
  "ECDHE-ECDSA-CHACHA20-POLY1305",
  "ECDHE-RSA-AES256-GCM-SHA384",
  "ECDHE-RSA-AES128-GCM-SHA256",
  "ECDHE-RSA-CHACHA20-POLY1305",
], ":");

return {
  checkServerIdentity,
  connect,
  createServer,
  DEFAULT_CIPHERS,
  Server,
  TLSSocket,
  unfqdn,
  default: {
    TLSSocket,
    connect,
    createServer,
    checkServerIdentity,
    DEFAULT_CIPHERS,
    Server,
    unfqdn,
  },
};
})();