ai-remote 0.1.0

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.
Files changed (114) hide show
  1. package/README.md +50 -0
  2. package/dist/index.js +7 -0
  3. package/dist/index.js.map +7 -0
  4. package/dist/protocols/index.js +35 -0
  5. package/dist/protocols/index.js.map +7 -0
  6. package/dist/protocols/rdp/buffer.js +170 -0
  7. package/dist/protocols/rdp/buffer.js.map +7 -0
  8. package/dist/protocols/rdp/caps.js +260 -0
  9. package/dist/protocols/rdp/caps.js.map +7 -0
  10. package/dist/protocols/rdp/cert.js +135 -0
  11. package/dist/protocols/rdp/cert.js.map +7 -0
  12. package/dist/protocols/rdp/client.js +1068 -0
  13. package/dist/protocols/rdp/client.js.map +7 -0
  14. package/dist/protocols/rdp/cliprdr.js +240 -0
  15. package/dist/protocols/rdp/cliprdr.js.map +7 -0
  16. package/dist/protocols/rdp/credssp.js +254 -0
  17. package/dist/protocols/rdp/credssp.js.map +7 -0
  18. package/dist/protocols/rdp/crypto.js +369 -0
  19. package/dist/protocols/rdp/crypto.js.map +7 -0
  20. package/dist/protocols/rdp/display.js +589 -0
  21. package/dist/protocols/rdp/display.js.map +7 -0
  22. package/dist/protocols/rdp/gcc.js +132 -0
  23. package/dist/protocols/rdp/gcc.js.map +7 -0
  24. package/dist/protocols/rdp/keymap.js +132 -0
  25. package/dist/protocols/rdp/keymap.js.map +7 -0
  26. package/dist/protocols/rdp/mcs.js +223 -0
  27. package/dist/protocols/rdp/mcs.js.map +7 -0
  28. package/dist/protocols/rdp/ntlm.js +305 -0
  29. package/dist/protocols/rdp/ntlm.js.map +7 -0
  30. package/dist/protocols/rdp/pdu.js +444 -0
  31. package/dist/protocols/rdp/pdu.js.map +7 -0
  32. package/dist/protocols/rdp/rail.js +383 -0
  33. package/dist/protocols/rdp/rail.js.map +7 -0
  34. package/dist/protocols/rdp/rle.js +276 -0
  35. package/dist/protocols/rdp/rle.js.map +7 -0
  36. package/dist/protocols/rdp/sec.js +156 -0
  37. package/dist/protocols/rdp/sec.js.map +7 -0
  38. package/dist/protocols/rdp/session.js +307 -0
  39. package/dist/protocols/rdp/session.js.map +7 -0
  40. package/dist/protocols/rdp/tls.js +354 -0
  41. package/dist/protocols/rdp/tls.js.map +7 -0
  42. package/dist/protocols/rdp/vchannel.js +59 -0
  43. package/dist/protocols/rdp/vchannel.js.map +7 -0
  44. package/dist/protocols/rdp/x224.js +110 -0
  45. package/dist/protocols/rdp/x224.js.map +7 -0
  46. package/dist/protocols/ssh/kex.js +177 -0
  47. package/dist/protocols/ssh/kex.js.map +7 -0
  48. package/dist/protocols/ssh/messages.js +55 -0
  49. package/dist/protocols/ssh/messages.js.map +7 -0
  50. package/dist/protocols/ssh/session.js +608 -0
  51. package/dist/protocols/ssh/session.js.map +7 -0
  52. package/dist/protocols/ssh/terminal.js +191 -0
  53. package/dist/protocols/ssh/terminal.js.map +7 -0
  54. package/dist/protocols/ssh/transport.js +611 -0
  55. package/dist/protocols/ssh/transport.js.map +7 -0
  56. package/dist/protocols/ssh/wire.js +189 -0
  57. package/dist/protocols/ssh/wire.js.map +7 -0
  58. package/dist/protocols/types.js +1 -0
  59. package/dist/protocols/types.js.map +7 -0
  60. package/dist/protocols/vnc/input.js +16 -0
  61. package/dist/protocols/vnc/input.js.map +7 -0
  62. package/dist/protocols/vnc/session.js +259 -0
  63. package/dist/protocols/vnc/session.js.map +7 -0
  64. package/dist/shared/connection.js +114 -0
  65. package/dist/shared/connection.js.map +7 -0
  66. package/dist/shared/device.js +1 -0
  67. package/dist/shared/device.js.map +7 -0
  68. package/dist/shared/hosts.js +133 -0
  69. package/dist/shared/hosts.js.map +7 -0
  70. package/dist/shared/icons.js +42 -0
  71. package/dist/shared/icons.js.map +7 -0
  72. package/dist/shared/protocol.js +40 -0
  73. package/dist/shared/protocol.js.map +7 -0
  74. package/dist/shared/signals.js +42 -0
  75. package/dist/shared/signals.js.map +7 -0
  76. package/package.json +82 -0
  77. package/src/index.ts +14 -0
  78. package/src/protocols/index.ts +72 -0
  79. package/src/protocols/rdp/buffer.ts +204 -0
  80. package/src/protocols/rdp/caps.ts +304 -0
  81. package/src/protocols/rdp/cert.ts +190 -0
  82. package/src/protocols/rdp/client.ts +1363 -0
  83. package/src/protocols/rdp/cliprdr.ts +344 -0
  84. package/src/protocols/rdp/credssp.ts +319 -0
  85. package/src/protocols/rdp/crypto.ts +416 -0
  86. package/src/protocols/rdp/display.ts +750 -0
  87. package/src/protocols/rdp/gcc.ts +162 -0
  88. package/src/protocols/rdp/keymap.ts +68 -0
  89. package/src/protocols/rdp/mcs.ts +283 -0
  90. package/src/protocols/rdp/ntlm.ts +380 -0
  91. package/src/protocols/rdp/pdu.ts +497 -0
  92. package/src/protocols/rdp/rail.ts +480 -0
  93. package/src/protocols/rdp/rle.ts +343 -0
  94. package/src/protocols/rdp/sec.ts +184 -0
  95. package/src/protocols/rdp/session.ts +393 -0
  96. package/src/protocols/rdp/tls.ts +452 -0
  97. package/src/protocols/rdp/vchannel.ts +89 -0
  98. package/src/protocols/rdp/x224.ts +139 -0
  99. package/src/protocols/ssh/kex.ts +227 -0
  100. package/src/protocols/ssh/messages.ts +54 -0
  101. package/src/protocols/ssh/session.ts +878 -0
  102. package/src/protocols/ssh/terminal.ts +242 -0
  103. package/src/protocols/ssh/transport.ts +732 -0
  104. package/src/protocols/ssh/wire.ts +220 -0
  105. package/src/protocols/types.ts +146 -0
  106. package/src/protocols/vnc/input.ts +13 -0
  107. package/src/protocols/vnc/novnc.d.ts +75 -0
  108. package/src/protocols/vnc/session.ts +367 -0
  109. package/src/shared/connection.ts +206 -0
  110. package/src/shared/device.ts +28 -0
  111. package/src/shared/hosts.ts +240 -0
  112. package/src/shared/icons.ts +49 -0
  113. package/src/shared/protocol.ts +55 -0
  114. package/src/shared/signals.ts +67 -0
@@ -0,0 +1,190 @@
1
+ // Server public key extraction for the RDP Standard Security layer.
2
+ //
3
+ // A host either hands out a "proprietary" certificate -- little more than a
4
+ // wrapper around an RSA public key -- or an X.509 chain whose leaf holds the
5
+ // key (MS-RDPBCGR 2.2.1.4.3.1). Either way the client random is encrypted with
6
+ // that key, so all we need out of it is a modulus and an exponent.
7
+ //
8
+ // Note the proprietary certificate is signed with a private key Microsoft
9
+ // published, so it authenticates nothing: RDP Standard Security gives
10
+ // confidentiality on the wire, not proof of the host's identity.
11
+
12
+ import { Reader } from './buffer';
13
+
14
+ const CERT_PROPRIETARY = 1;
15
+ const CERT_X509 = 2;
16
+ const RSA1_MAGIC = 0x31415352;
17
+ const BB_RSA_KEY_BLOB = 0x0006;
18
+
19
+ const OID_RSA_ENCRYPTION = [0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01];
20
+
21
+ // --- DER --------------------------------------------------------------------
22
+
23
+ function derRead(bytes: Uint8Array, offset: number) {
24
+ let cursor = offset;
25
+ const tag = bytes[cursor++];
26
+
27
+ let length = bytes[cursor++];
28
+ if (length & 0x80) {
29
+ const byteCount = length & 0x7f;
30
+ length = 0;
31
+ for (let i = 0; i < byteCount; i++) length = (length << 8) | bytes[cursor++];
32
+ }
33
+
34
+ return {
35
+ tag,
36
+ contentStart: cursor,
37
+ contentEnd: cursor + length,
38
+ next: cursor + length,
39
+ content: bytes.subarray(cursor, cursor + length),
40
+ };
41
+ }
42
+
43
+ function derChildren(bytes: Uint8Array) {
44
+ const children = [];
45
+ let offset = 0;
46
+ while (offset < bytes.length) {
47
+ const node = derRead(bytes, offset);
48
+ children.push(node);
49
+ if (node.next <= offset) break;
50
+ offset = node.next;
51
+ }
52
+ return children;
53
+ }
54
+
55
+ function containsOid(bytes: Uint8Array, oid: string) {
56
+ outer: for (let i = 0; i + oid.length <= bytes.length; i++) {
57
+ for (let j = 0; j < oid.length; j++) {
58
+ if (bytes[i + j] !== oid[j]) continue outer;
59
+ }
60
+ return true;
61
+ }
62
+ return false;
63
+ }
64
+
65
+ /** Big-endian DER integer -> little-endian bytes with the sign byte removed. */
66
+ function derIntegerToLe(content: Uint8Array) {
67
+ let start = 0;
68
+ while (start < content.length - 1 && content[start] === 0x00) start++;
69
+ return content.subarray(start).slice().reverse();
70
+ }
71
+
72
+ /**
73
+ * Find SubjectPublicKeyInfo in an X.509 certificate and return the RSA key.
74
+ * The structure is walked rather than indexed by position: optional fields in
75
+ * TBSCertificate make fixed offsets wrong for many real certificates.
76
+ */
77
+ function rsaKeyFromX509(der: Uint8Array) {
78
+ const certificate = derRead(der, 0);
79
+ const tbsCertificate = derRead(der, certificate.contentStart);
80
+
81
+ for (const child of derChildren(tbsCertificate.content)) {
82
+ if (child.tag !== 0x30) continue;
83
+
84
+ const parts = derChildren(child.content);
85
+ if (parts.length < 2) continue;
86
+ if (parts[0].tag !== 0x30 || !containsOid(parts[0].content, OID_RSA_ENCRYPTION)) continue;
87
+ if (parts[1].tag !== 0x03) continue;
88
+
89
+ // BIT STRING content starts with an "unused bits" count.
90
+ const publicKey = derRead(parts[1].content.subarray(1), 0);
91
+ const [modulus, exponent] = derChildren(publicKey.content);
92
+ if (!modulus || !exponent) break;
93
+
94
+ return {
95
+ modulus: derIntegerToLe(modulus.content),
96
+ exponent: derIntegerToLe(exponent.content),
97
+ source: 'x509',
98
+ };
99
+ }
100
+
101
+ throw new Error('RDP: no RSA public key found in the host X.509 certificate');
102
+ }
103
+
104
+ function rsaKeyFromProprietary(reader: Reader) {
105
+ reader.u32le(); // dwSigAlgId
106
+ reader.u32le(); // dwKeyAlgId
107
+
108
+ const publicKeyBlobType = reader.u16le();
109
+ const publicKeyBlobLength = reader.u16le();
110
+ if (publicKeyBlobType !== BB_RSA_KEY_BLOB) {
111
+ throw new Error(`RDP: unsupported public key blob type 0x${publicKeyBlobType.toString(16)}`);
112
+ }
113
+
114
+ const blob = new Reader(reader.raw(publicKeyBlobLength));
115
+ if (blob.u32le() !== RSA1_MAGIC) {
116
+ throw new Error('RDP: the host RSA public key blob is not in RSA1 format');
117
+ }
118
+
119
+ const keyLength = blob.u32le();
120
+ blob.u32le(); // bitlen
121
+ blob.u32le(); // datalen
122
+ const exponent = blob.raw(4).slice();
123
+
124
+ // The blob reserves 8 trailing bytes of padding after the modulus.
125
+ if (keyLength < 9) throw new Error('RDP: the host RSA modulus is too short');
126
+ const modulus = blob.raw(keyLength - 8).slice();
127
+
128
+ return { modulus, exponent, source: 'proprietary' };
129
+ }
130
+
131
+ /**
132
+ * Pull the RSA public key out of a DER X.509 certificate, for the TLS layer.
133
+ *
134
+ * CredSSP needs the exact bytes of the certificate's subjectPublicKey BIT
135
+ * STRING contents -- the DER RSAPublicKey -- because that is what both ends
136
+ * hash to bind the authentication to this TLS channel (MS-CSSP 3.1.5).
137
+ */
138
+ export function parseX509PublicKey(der: Uint8Array) {
139
+ const certificate = derRead(der, 0);
140
+ const tbsCertificate = derRead(der, certificate.contentStart);
141
+
142
+ for (const child of derChildren(tbsCertificate.content)) {
143
+ if (child.tag !== 0x30) continue;
144
+
145
+ const parts = derChildren(child.content);
146
+ if (parts.length < 2) continue;
147
+ if (parts[0].tag !== 0x30 || !containsOid(parts[0].content, OID_RSA_ENCRYPTION)) continue;
148
+ if (parts[1].tag !== 0x03) continue;
149
+
150
+ const subjectPublicKey = parts[1].content.subarray(1); // drop the unused-bit count
151
+ const publicKey = derRead(subjectPublicKey, 0);
152
+ const [modulus, exponent] = derChildren(publicKey.content);
153
+ if (!modulus || !exponent) break;
154
+
155
+ const stripLeadingZero = (bytes: Uint8Array) => {
156
+ let start = 0;
157
+ while (start < bytes.length - 1 && bytes[start] === 0x00) start++;
158
+ return bytes.subarray(start);
159
+ };
160
+
161
+ return {
162
+ modulus: stripLeadingZero(modulus.content),
163
+ exponent: stripLeadingZero(exponent.content),
164
+ subjectPublicKey,
165
+ };
166
+ }
167
+
168
+ throw new Error('TLS: no RSA public key found in the host certificate');
169
+ }
170
+
171
+ /** Parse a server certificate blob into an RSA public key. */
172
+ export function parseServerCertificate(certificateBytes: Uint8Array) {
173
+ const reader = new Reader(certificateBytes);
174
+ const version = reader.u32le() & 0x7fffffff;
175
+
176
+ if (version === CERT_PROPRIETARY) return rsaKeyFromProprietary(reader);
177
+
178
+ if (version === CERT_X509) {
179
+ const certificateCount = reader.u32le();
180
+ let leaf = null;
181
+ for (let i = 0; i < certificateCount; i++) {
182
+ const length = reader.u32le();
183
+ leaf = reader.raw(length);
184
+ }
185
+ if (!leaf) throw new Error('RDP: the host sent an empty X.509 certificate chain');
186
+ return rsaKeyFromX509(leaf);
187
+ }
188
+
189
+ throw new Error(`RDP: unsupported server certificate version ${version}`);
190
+ }