@homelib/xiaomi 0.0.0-wip.ba4eafa051eb
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 +19 -0
- package/README.md +22 -0
- package/bld/library/backend/client.d.ts +68 -0
- package/bld/library/backend/client.js +477 -0
- package/bld/library/backend/client.js.map +1 -0
- package/bld/library/backend/constants.d.ts +7 -0
- package/bld/library/backend/constants.js +11 -0
- package/bld/library/backend/constants.js.map +1 -0
- package/bld/library/backend/index.d.ts +3 -0
- package/bld/library/backend/index.js +4 -0
- package/bld/library/backend/index.js.map +1 -0
- package/bld/library/backend/oauth.d.ts +24 -0
- package/bld/library/backend/oauth.js +121 -0
- package/bld/library/backend/oauth.js.map +1 -0
- package/bld/library/binding.d.ts +44 -0
- package/bld/library/binding.js +212 -0
- package/bld/library/binding.js.map +1 -0
- package/bld/library/cloud/client.d.ts +27 -0
- package/bld/library/cloud/client.js +109 -0
- package/bld/library/cloud/client.js.map +1 -0
- package/bld/library/cloud/constants.d.ts +9 -0
- package/bld/library/cloud/constants.js +11 -0
- package/bld/library/cloud/constants.js.map +1 -0
- package/bld/library/cloud/device.d.ts +84 -0
- package/bld/library/cloud/device.js +686 -0
- package/bld/library/cloud/device.js.map +1 -0
- package/bld/library/cloud/index.d.ts +5 -0
- package/bld/library/cloud/index.js +6 -0
- package/bld/library/cloud/index.js.map +1 -0
- package/bld/library/cloud/mqtt.d.ts +69 -0
- package/bld/library/cloud/mqtt.js +517 -0
- package/bld/library/cloud/mqtt.js.map +1 -0
- package/bld/library/cloud/transport.d.ts +8 -0
- package/bld/library/cloud/transport.js +30 -0
- package/bld/library/cloud/transport.js.map +1 -0
- package/bld/library/command.d.ts +6 -0
- package/bld/library/command.js +6 -0
- package/bld/library/command.js.map +1 -0
- package/bld/library/configuration.d.ts +62 -0
- package/bld/library/configuration.js +303 -0
- package/bld/library/configuration.js.map +1 -0
- package/bld/library/device.d.ts +68 -0
- package/bld/library/device.js +235 -0
- package/bld/library/device.js.map +1 -0
- package/bld/library/devices/air-conditioner.d.ts +48 -0
- package/bld/library/devices/air-conditioner.js +155 -0
- package/bld/library/devices/air-conditioner.js.map +1 -0
- package/bld/library/devices/command-effect.d.ts +35 -0
- package/bld/library/devices/command-effect.js +269 -0
- package/bld/library/devices/command-effect.js.map +1 -0
- package/bld/library/devices/dehumidifier.d.ts +41 -0
- package/bld/library/devices/dehumidifier.js +124 -0
- package/bld/library/devices/dehumidifier.js.map +1 -0
- package/bld/library/devices/fan.d.ts +37 -0
- package/bld/library/devices/fan.js +135 -0
- package/bld/library/devices/fan.js.map +1 -0
- package/bld/library/devices/index.d.ts +7 -0
- package/bld/library/devices/index.js +8 -0
- package/bld/library/devices/index.js.map +1 -0
- package/bld/library/devices/light.d.ts +22 -0
- package/bld/library/devices/light.js +112 -0
- package/bld/library/devices/light.js.map +1 -0
- package/bld/library/devices/placeholder.d.ts +3 -0
- package/bld/library/devices/placeholder.js +4 -0
- package/bld/library/devices/placeholder.js.map +1 -0
- package/bld/library/devices/value-range.d.ts +2 -0
- package/bld/library/devices/value-range.js +6 -0
- package/bld/library/devices/value-range.js.map +1 -0
- package/bld/library/endpoint-connection.d.ts +154 -0
- package/bld/library/endpoint-connection.js +510 -0
- package/bld/library/endpoint-connection.js.map +1 -0
- package/bld/library/index.d.ts +26 -0
- package/bld/library/index.js +31 -0
- package/bld/library/index.js.map +1 -0
- package/bld/library/local/certificate.d.ts +31 -0
- package/bld/library/local/certificate.js +298 -0
- package/bld/library/local/certificate.js.map +1 -0
- package/bld/library/local/controller.d.ts +70 -0
- package/bld/library/local/controller.js +497 -0
- package/bld/library/local/controller.js.map +1 -0
- package/bld/library/local/discovery.d.ts +29 -0
- package/bld/library/local/discovery.js +321 -0
- package/bld/library/local/discovery.js.map +1 -0
- package/bld/library/local/index.d.ts +6 -0
- package/bld/library/local/index.js +7 -0
- package/bld/library/local/index.js.map +1 -0
- package/bld/library/local/message.d.ts +11 -0
- package/bld/library/local/message.js +128 -0
- package/bld/library/local/message.js.map +1 -0
- package/bld/library/local/mqtt.d.ts +113 -0
- package/bld/library/local/mqtt.js +1044 -0
- package/bld/library/local/mqtt.js.map +1 -0
- package/bld/library/local/routed-message-client.d.ts +49 -0
- package/bld/library/local/routed-message-client.js +326 -0
- package/bld/library/local/routed-message-client.js.map +1 -0
- package/bld/library/miot/index.d.ts +4 -0
- package/bld/library/miot/index.js +5 -0
- package/bld/library/miot/index.js.map +1 -0
- package/bld/library/miot/matcher.d.ts +65 -0
- package/bld/library/miot/matcher.js +217 -0
- package/bld/library/miot/matcher.js.map +1 -0
- package/bld/library/miot/request.d.ts +16 -0
- package/bld/library/miot/request.js +27 -0
- package/bld/library/miot/request.js.map +1 -0
- package/bld/library/miot/result.d.ts +4 -0
- package/bld/library/miot/result.js +5 -0
- package/bld/library/miot/result.js.map +1 -0
- package/bld/library/miot/spec.d.ts +74 -0
- package/bld/library/miot/spec.js +78 -0
- package/bld/library/miot/spec.js.map +1 -0
- package/bld/library/provider.d.ts +71 -0
- package/bld/library/provider.js +600 -0
- package/bld/library/provider.js.map +1 -0
- package/bld/library/session-manager.d.ts +34 -0
- package/bld/library/session-manager.js +177 -0
- package/bld/library/session-manager.js.map +1 -0
- package/bld/library/session.d.ts +36 -0
- package/bld/library/session.js +370 -0
- package/bld/library/session.js.map +1 -0
- package/bld/library/storage.d.ts +2 -0
- package/bld/library/storage.js +66 -0
- package/bld/library/storage.js.map +1 -0
- package/bld/library/terminal/bindings.d.ts +3 -0
- package/bld/library/terminal/bindings.js +280 -0
- package/bld/library/terminal/bindings.js.map +1 -0
- package/bld/library/terminal/index.d.ts +2 -0
- package/bld/library/terminal/index.js +3 -0
- package/bld/library/terminal/index.js.map +1 -0
- package/bld/library/terminal/provider-details.d.ts +3 -0
- package/bld/library/terminal/provider-details.js +477 -0
- package/bld/library/terminal/provider-details.js.map +1 -0
- package/package.json +29 -0
- package/src/library/backend/client.ts +713 -0
- package/src/library/backend/constants.ts +17 -0
- package/src/library/backend/index.ts +3 -0
- package/src/library/backend/oauth.ts +170 -0
- package/src/library/binding.ts +387 -0
- package/src/library/cloud/client.ts +155 -0
- package/src/library/cloud/constants.ts +19 -0
- package/src/library/cloud/device.ts +938 -0
- package/src/library/cloud/index.ts +5 -0
- package/src/library/cloud/mqtt.ts +671 -0
- package/src/library/cloud/transport.ts +38 -0
- package/src/library/command.ts +7 -0
- package/src/library/configuration.ts +480 -0
- package/src/library/device.ts +548 -0
- package/src/library/devices/air-conditioner.ts +171 -0
- package/src/library/devices/command-effect.ts +425 -0
- package/src/library/devices/dehumidifier.ts +131 -0
- package/src/library/devices/fan.ts +140 -0
- package/src/library/devices/index.ts +7 -0
- package/src/library/devices/light.ts +119 -0
- package/src/library/devices/placeholder.ts +3 -0
- package/src/library/devices/value-range.ts +14 -0
- package/src/library/endpoint-connection.ts +841 -0
- package/src/library/index.ts +62 -0
- package/src/library/local/certificate.ts +401 -0
- package/src/library/local/controller.ts +692 -0
- package/src/library/local/discovery.ts +485 -0
- package/src/library/local/index.ts +6 -0
- package/src/library/local/message.ts +174 -0
- package/src/library/local/mqtt.ts +1493 -0
- package/src/library/local/routed-message-client.ts +377 -0
- package/src/library/miot/index.ts +4 -0
- package/src/library/miot/matcher.ts +461 -0
- package/src/library/miot/request.ts +30 -0
- package/src/library/miot/result.ts +10 -0
- package/src/library/miot/spec.ts +103 -0
- package/src/library/provider.ts +745 -0
- package/src/library/session-manager.ts +224 -0
- package/src/library/session.ts +528 -0
- package/src/library/storage.ts +83 -0
- package/src/library/terminal/bindings.tsx +612 -0
- package/src/library/terminal/index.ts +2 -0
- package/src/library/terminal/provider-details.tsx +871 -0
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AirConditioner,
|
|
3
|
+
Dehumidifier,
|
|
4
|
+
Fan,
|
|
5
|
+
Light,
|
|
6
|
+
register,
|
|
7
|
+
} from '@homelib/core';
|
|
8
|
+
import {
|
|
9
|
+
registerProviderBindingComponent,
|
|
10
|
+
registerProviderDetailsComponent,
|
|
11
|
+
} from '@homelib/terminal';
|
|
12
|
+
|
|
13
|
+
import {registerMiotDevice} from './device.js';
|
|
14
|
+
import {
|
|
15
|
+
MiotAirConditionerEndpointConnection,
|
|
16
|
+
MiotDehumidifierEndpointConnection,
|
|
17
|
+
MiotFanEndpointConnection,
|
|
18
|
+
MiotLightEndpointConnection,
|
|
19
|
+
MiotPlaceholderDevice,
|
|
20
|
+
} from './devices/index.js';
|
|
21
|
+
import {MIOT_NAMESPACE, MiotProvider} from './provider.js';
|
|
22
|
+
import {MiotProviderBindings, MiotProviderDetails} from './terminal/index.js';
|
|
23
|
+
|
|
24
|
+
register(MIOT_NAMESPACE, {
|
|
25
|
+
placeholder: MiotPlaceholderDevice,
|
|
26
|
+
});
|
|
27
|
+
registerMiotDevice(AirConditioner, MiotAirConditionerEndpointConnection);
|
|
28
|
+
registerMiotDevice(Dehumidifier, MiotDehumidifierEndpointConnection);
|
|
29
|
+
registerMiotDevice(Fan, MiotFanEndpointConnection);
|
|
30
|
+
registerMiotDevice(Light, MiotLightEndpointConnection);
|
|
31
|
+
registerProviderBindingComponent(MiotProvider, MiotProviderBindings);
|
|
32
|
+
registerProviderDetailsComponent(MiotProvider, MiotProviderDetails);
|
|
33
|
+
|
|
34
|
+
declare global {
|
|
35
|
+
namespace Home {
|
|
36
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
|
|
37
|
+
interface ProviderNamespaces {
|
|
38
|
+
miot: MiotDeviceConstructors;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
|
|
42
|
+
interface MiotDeviceConstructors {
|
|
43
|
+
placeholder: MiotPlaceholderDevice;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export * from './backend/index.js';
|
|
49
|
+
export * from './binding.js';
|
|
50
|
+
export * from './cloud/index.js';
|
|
51
|
+
export * from './command.js';
|
|
52
|
+
export * from './configuration.js';
|
|
53
|
+
export * from './device.js';
|
|
54
|
+
export * from './devices/index.js';
|
|
55
|
+
export * from './endpoint-connection.js';
|
|
56
|
+
export * from './local/index.js';
|
|
57
|
+
export * from './miot/index.js';
|
|
58
|
+
export * from './provider.js';
|
|
59
|
+
export * from './session-manager.js';
|
|
60
|
+
export * from './session.js';
|
|
61
|
+
export * from './storage.js';
|
|
62
|
+
export * from './terminal/index.js';
|
|
@@ -0,0 +1,401 @@
|
|
|
1
|
+
import {execFile} from 'node:child_process';
|
|
2
|
+
import {
|
|
3
|
+
X509Certificate,
|
|
4
|
+
createHash,
|
|
5
|
+
createPrivateKey,
|
|
6
|
+
createPublicKey,
|
|
7
|
+
generateKeyPairSync,
|
|
8
|
+
} from 'node:crypto';
|
|
9
|
+
import {mkdtemp, readFile, rm, writeFile} from 'node:fs/promises';
|
|
10
|
+
import {tmpdir} from 'node:os';
|
|
11
|
+
import {join} from 'node:path';
|
|
12
|
+
|
|
13
|
+
import * as x from 'x-value';
|
|
14
|
+
|
|
15
|
+
import {writePrivateJsonFile} from '../storage.js';
|
|
16
|
+
|
|
17
|
+
const CERTIFICATE_RENEWAL_MARGIN = 3 * 24 * 60 * 60 * 1000;
|
|
18
|
+
const OPENSSL_TIMEOUT = 10_000;
|
|
19
|
+
const MIJIA_CA_CERTIFICATE_SHA256 =
|
|
20
|
+
'8b7bf306be3632e08b0ead308249e5f2b2520dc921ad143872d5fcc7c68d6759';
|
|
21
|
+
|
|
22
|
+
/** Xiaomi's root and central-gateway CA certificates used by local MIPS TLS. */
|
|
23
|
+
export const MIJIA_CA_CERTIFICATE = [
|
|
24
|
+
'-----BEGIN CERTIFICATE-----',
|
|
25
|
+
'MIIBazCCAQ+gAwIBAgIEA/UKYDAMBggqhkjOPQQDAgUAMCIxEzARBgNVBAoTCk1p',
|
|
26
|
+
'amlhIFJvb3QxCzAJBgNVBAYTAkNOMCAXDTE2MTEyMzAxMzk0NVoYDzIwNjYxMTEx',
|
|
27
|
+
'MDEzOTQ1WjAiMRMwEQYDVQQKEwpNaWppYSBSb290MQswCQYDVQQGEwJDTjBZMBMG',
|
|
28
|
+
'ByqGSM49AgEGCCqGSM49AwEHA0IABL71iwLa4//4VBqgRI+6xE23xpovqPCxtv96',
|
|
29
|
+
'2VHbZij61/Ag6jmi7oZ/3Xg/3C+whglcwoUEE6KALGJ9vccV9PmjLzAtMAwGA1Ud',
|
|
30
|
+
'EwQFMAMBAf8wHQYDVR0OBBYEFJa3onw5sblmM6n40QmyAGDI5sURMAwGCCqGSM49',
|
|
31
|
+
'BAMCBQADSAAwRQIgchciK9h6tZmfrP8Ka6KziQ4Lv3hKfrHtAZXMHPda4IYCIQCG',
|
|
32
|
+
'az93ggFcbrG9u2wixjx1HKW4DUA5NXZG0wWQTpJTbQ==',
|
|
33
|
+
'-----END CERTIFICATE-----',
|
|
34
|
+
'-----BEGIN CERTIFICATE-----',
|
|
35
|
+
'MIIBjzCCATWgAwIBAgIBATAKBggqhkjOPQQDAjAiMRMwEQYDVQQKEwpNaWppYSBS',
|
|
36
|
+
'b290MQswCQYDVQQGEwJDTjAgFw0yMjA2MDkxNDE0MThaGA8yMDcyMDUyNzE0MTQx',
|
|
37
|
+
'OFowLDELMAkGA1UEBhMCQ04xHTAbBgNVBAoMFE1JT1QgQ0VOVFJBTCBHQVRFV0FZ',
|
|
38
|
+
'MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEdYrzbnp/0x/cZLZnuEDXTFf8mhj4',
|
|
39
|
+
'CVpZPwgj9e9Ve5r3K7zvu8Jjj7JF1JjQYvEC6yhp1SzBgglnK4L8xQzdiqNQME4w',
|
|
40
|
+
'HQYDVR0OBBYEFCf9+YBU7pXDs6K6CAQPRhlGJ+cuMB8GA1UdIwQYMBaAFJa3onw5',
|
|
41
|
+
'sblmM6n40QmyAGDI5sURMAwGA1UdEwQFMAMBAf8wCgYIKoZIzj0EAwIDSAAwRQIh',
|
|
42
|
+
'AKUv+c8v98vypkGMTzMwckGjjVqTef8xodsy6PhcSCq+AiA/n9mDs62hAo5zXyJy',
|
|
43
|
+
'Bs1s7mqXPf1XgieoxIvs1MqyiA==',
|
|
44
|
+
'-----END CERTIFICATE-----',
|
|
45
|
+
'',
|
|
46
|
+
].join('\n');
|
|
47
|
+
|
|
48
|
+
const StoredCertificateValue = x.object({
|
|
49
|
+
version: x.literal(1),
|
|
50
|
+
userId: x.string,
|
|
51
|
+
virtualDid: x.string,
|
|
52
|
+
privateKey: x.string,
|
|
53
|
+
certificate: x.string,
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
export type LocalCertificate = {
|
|
57
|
+
readonly privateKey: string;
|
|
58
|
+
readonly certificate: string;
|
|
59
|
+
readonly caCertificate: string;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export type LocalCertificateManagerOptions = {
|
|
63
|
+
/** Path of the private, atomically replaced JSON credential cache. */
|
|
64
|
+
readonly path: string;
|
|
65
|
+
/** Stable 32-character OAuth environment UUID. */
|
|
66
|
+
readonly uuid: string;
|
|
67
|
+
readonly userId: string;
|
|
68
|
+
readonly getCertificate: (certificateRequest: string) => Promise<string>;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
export class LocalCertificateManager {
|
|
72
|
+
private readonly path: string;
|
|
73
|
+
|
|
74
|
+
private readonly virtualDid: string;
|
|
75
|
+
|
|
76
|
+
private readonly userId: string;
|
|
77
|
+
|
|
78
|
+
private readonly getCertificate: (
|
|
79
|
+
certificateRequest: string,
|
|
80
|
+
) => Promise<string>;
|
|
81
|
+
|
|
82
|
+
private pendingCertificate?: Promise<LocalCertificate>;
|
|
83
|
+
|
|
84
|
+
constructor(options: LocalCertificateManagerOptions) {
|
|
85
|
+
validateMijiaCaCertificate();
|
|
86
|
+
|
|
87
|
+
this.path = options.path;
|
|
88
|
+
this.virtualDid = getVirtualDid(options.uuid);
|
|
89
|
+
this.userId = validateDecimalIdentifier(options.userId, 'user id');
|
|
90
|
+
this.getCertificate = options.getCertificate;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
ensureCertificate(): Promise<LocalCertificate> {
|
|
94
|
+
if (this.pendingCertificate === undefined) {
|
|
95
|
+
const pendingCertificate = this.loadOrCreateCertificate();
|
|
96
|
+
|
|
97
|
+
this.pendingCertificate = pendingCertificate;
|
|
98
|
+
const complete = (): void => {
|
|
99
|
+
if (this.pendingCertificate === pendingCertificate) {
|
|
100
|
+
this.pendingCertificate = undefined;
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
void pendingCertificate.then(complete, complete);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return this.pendingCertificate;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
private async loadOrCreateCertificate(): Promise<LocalCertificate> {
|
|
111
|
+
const stored = await readStoredCertificate(this.path);
|
|
112
|
+
let privateKey: string;
|
|
113
|
+
|
|
114
|
+
if (
|
|
115
|
+
stored !== undefined &&
|
|
116
|
+
stored.userId === this.userId &&
|
|
117
|
+
stored.virtualDid === this.virtualDid &&
|
|
118
|
+
isEd25519PrivateKey(stored.privateKey)
|
|
119
|
+
) {
|
|
120
|
+
privateKey = stored.privateKey;
|
|
121
|
+
|
|
122
|
+
if (
|
|
123
|
+
isUsableCertificate(
|
|
124
|
+
stored.certificate,
|
|
125
|
+
privateKey,
|
|
126
|
+
getCertificateCommonName(this.userId, this.virtualDid),
|
|
127
|
+
)
|
|
128
|
+
) {
|
|
129
|
+
return createLocalCertificate(privateKey, stored.certificate);
|
|
130
|
+
}
|
|
131
|
+
} else {
|
|
132
|
+
privateKey = createEd25519PrivateKey();
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const certificateRequest = await createCertificateRequest(
|
|
136
|
+
privateKey,
|
|
137
|
+
this.userId,
|
|
138
|
+
this.virtualDid,
|
|
139
|
+
);
|
|
140
|
+
const certificate = await this.getCertificate(certificateRequest);
|
|
141
|
+
const commonName = getCertificateCommonName(this.userId, this.virtualDid);
|
|
142
|
+
|
|
143
|
+
if (!isUsableCertificate(certificate, privateKey, commonName)) {
|
|
144
|
+
throw new Error('Cloud returned an invalid local MQTT certificate.');
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
await writePrivateJsonFile(this.path, {
|
|
148
|
+
version: 1,
|
|
149
|
+
userId: this.userId,
|
|
150
|
+
virtualDid: this.virtualDid,
|
|
151
|
+
privateKey,
|
|
152
|
+
certificate,
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
return createLocalCertificate(privateKey, certificate);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export function getVirtualDid(uuid: string): string {
|
|
160
|
+
if (!/^[\da-f]{32}$/u.test(uuid)) {
|
|
161
|
+
throw new TypeError('Local MQTT UUID must be 32 lowercase hex characters.');
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return BigInt(`0x${uuid.slice(0, 16)}`).toString();
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export function getCertificateCommonName(
|
|
168
|
+
userId: string,
|
|
169
|
+
virtualDid: string,
|
|
170
|
+
): string {
|
|
171
|
+
const validUserId = validateDecimalIdentifier(userId, 'user id');
|
|
172
|
+
const validVirtualDid = validateDecimalIdentifier(
|
|
173
|
+
virtualDid,
|
|
174
|
+
'virtual device id',
|
|
175
|
+
);
|
|
176
|
+
const didHash = createHash('sha1').update(validVirtualDid).digest('hex');
|
|
177
|
+
|
|
178
|
+
return `mips.${validUserId}.${didHash}.2`;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export function isMipsGatewayCertificate(
|
|
182
|
+
certificate: X509Certificate,
|
|
183
|
+
gatewayDid: string,
|
|
184
|
+
): boolean {
|
|
185
|
+
const validGatewayDid = validateDecimalIdentifier(gatewayDid, 'gateway DID');
|
|
186
|
+
const didHash = createHash('sha1').update(validGatewayDid).digest('hex');
|
|
187
|
+
const subject = parseCertificateSubject(certificate.subject);
|
|
188
|
+
|
|
189
|
+
return (
|
|
190
|
+
subject.get('C') === 'CN' &&
|
|
191
|
+
subject.get('O') === 'Mijia Device' &&
|
|
192
|
+
new RegExp(`^mips\\.\\d+\\.${didHash}\\.14$`, 'u').test(
|
|
193
|
+
subject.get('CN') ?? '',
|
|
194
|
+
)
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
export function createEd25519PrivateKey(): string {
|
|
199
|
+
const {privateKey} = generateKeyPairSync('ed25519', {
|
|
200
|
+
privateKeyEncoding: {format: 'pem', type: 'pkcs8'},
|
|
201
|
+
publicKeyEncoding: {format: 'pem', type: 'spki'},
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
return privateKey;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
export async function createCertificateRequest(
|
|
208
|
+
privateKey: string,
|
|
209
|
+
userId: string,
|
|
210
|
+
virtualDid: string,
|
|
211
|
+
): Promise<string> {
|
|
212
|
+
if (!isEd25519PrivateKey(privateKey)) {
|
|
213
|
+
throw new TypeError('Local MQTT private key must be Ed25519.');
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const directory = await mkdtemp(join(tmpdir(), 'homelib-xiaomi-csr-'));
|
|
217
|
+
const keyPath = join(directory, 'private-key.pem');
|
|
218
|
+
const requestPath = join(directory, 'certificate-request.pem');
|
|
219
|
+
|
|
220
|
+
try {
|
|
221
|
+
await writeFile(keyPath, privateKey, {encoding: 'utf8', mode: 0o600});
|
|
222
|
+
await executeOpenSsl([
|
|
223
|
+
'req',
|
|
224
|
+
'-new',
|
|
225
|
+
'-key',
|
|
226
|
+
keyPath,
|
|
227
|
+
'-subj',
|
|
228
|
+
`/C=CN/O=Mijia Device/CN=${getCertificateCommonName(userId, virtualDid)}`,
|
|
229
|
+
'-out',
|
|
230
|
+
requestPath,
|
|
231
|
+
'-outform',
|
|
232
|
+
'PEM',
|
|
233
|
+
]);
|
|
234
|
+
|
|
235
|
+
return await readFile(requestPath, 'utf8');
|
|
236
|
+
} finally {
|
|
237
|
+
await rm(directory, {recursive: true, force: true});
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function createLocalCertificate(
|
|
242
|
+
privateKey: string,
|
|
243
|
+
certificate: string,
|
|
244
|
+
): LocalCertificate {
|
|
245
|
+
return {
|
|
246
|
+
privateKey,
|
|
247
|
+
certificate,
|
|
248
|
+
caCertificate: MIJIA_CA_CERTIFICATE,
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
async function readStoredCertificate(
|
|
253
|
+
path: string,
|
|
254
|
+
): Promise<x.TypeOf<typeof StoredCertificateValue> | undefined> {
|
|
255
|
+
let source: string;
|
|
256
|
+
|
|
257
|
+
try {
|
|
258
|
+
source = await readFile(path, 'utf8');
|
|
259
|
+
} catch (error) {
|
|
260
|
+
if (isFileNotFoundError(error)) {
|
|
261
|
+
return undefined;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
throw error;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
try {
|
|
268
|
+
return StoredCertificateValue.satisfies(JSON.parse(source) as unknown);
|
|
269
|
+
} catch {
|
|
270
|
+
return undefined;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
function isUsableCertificate(
|
|
275
|
+
certificate: string,
|
|
276
|
+
privateKey: string,
|
|
277
|
+
commonName: string,
|
|
278
|
+
): boolean {
|
|
279
|
+
try {
|
|
280
|
+
const parsedCertificate = new X509Certificate(certificate);
|
|
281
|
+
const validFrom = Date.parse(parsedCertificate.validFrom);
|
|
282
|
+
const validTo = Date.parse(parsedCertificate.validTo);
|
|
283
|
+
const now = Date.now();
|
|
284
|
+
|
|
285
|
+
if (
|
|
286
|
+
parsedCertificate.subject !== `C=CN\nO=Mijia Device\nCN=${commonName}` ||
|
|
287
|
+
!Number.isFinite(validFrom) ||
|
|
288
|
+
!Number.isFinite(validTo) ||
|
|
289
|
+
validFrom > now ||
|
|
290
|
+
validTo <= now + CERTIFICATE_RENEWAL_MARGIN
|
|
291
|
+
) {
|
|
292
|
+
return false;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
const expectedPublicKey = createPublicKey(privateKey).export({
|
|
296
|
+
format: 'der',
|
|
297
|
+
type: 'spki',
|
|
298
|
+
});
|
|
299
|
+
const certificatePublicKey = parsedCertificate.publicKey.export({
|
|
300
|
+
format: 'der',
|
|
301
|
+
type: 'spki',
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
return expectedPublicKey.equals(certificatePublicKey);
|
|
305
|
+
} catch {
|
|
306
|
+
return false;
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
function isEd25519PrivateKey(privateKey: string): boolean {
|
|
311
|
+
try {
|
|
312
|
+
return createPrivateKey(privateKey).asymmetricKeyType === 'ed25519';
|
|
313
|
+
} catch {
|
|
314
|
+
return false;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
function validateMijiaCaCertificate(): void {
|
|
319
|
+
if (
|
|
320
|
+
createHash('sha256').update(MIJIA_CA_CERTIFICATE).digest('hex') !==
|
|
321
|
+
MIJIA_CA_CERTIFICATE_SHA256
|
|
322
|
+
) {
|
|
323
|
+
throw new Error('Embedded Mijia CA certificate bundle failed validation.');
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
const certificates =
|
|
327
|
+
MIJIA_CA_CERTIFICATE.match(
|
|
328
|
+
/-----BEGIN CERTIFICATE-----[\s\S]+?-----END CERTIFICATE-----/gu,
|
|
329
|
+
) ?? [];
|
|
330
|
+
|
|
331
|
+
if (certificates.length !== 2) {
|
|
332
|
+
throw new Error('Embedded Mijia CA certificate bundle is invalid.');
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
const root = new X509Certificate(certificates[0]!);
|
|
336
|
+
const gateway = new X509Certificate(certificates[1]!);
|
|
337
|
+
|
|
338
|
+
if (
|
|
339
|
+
!root.ca ||
|
|
340
|
+
!gateway.ca ||
|
|
341
|
+
root.subject !== 'O=Mijia Root\nC=CN' ||
|
|
342
|
+
root.issuer !== root.subject ||
|
|
343
|
+
gateway.subject !== 'C=CN\nO=MIOT CENTRAL GATEWAY' ||
|
|
344
|
+
!root.verify(root.publicKey) ||
|
|
345
|
+
!gateway.checkIssued(root) ||
|
|
346
|
+
!gateway.verify(root.publicKey)
|
|
347
|
+
) {
|
|
348
|
+
throw new Error('Embedded Mijia CA certificate bundle failed validation.');
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
function validateDecimalIdentifier(value: string, name: string): string {
|
|
353
|
+
if (!/^(?:0|[1-9]\d*)$/u.test(value)) {
|
|
354
|
+
throw new TypeError(`Local MQTT ${name} must be a decimal integer.`);
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
return value;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
function parseCertificateSubject(subject: string): ReadonlyMap<string, string> {
|
|
361
|
+
return new Map(
|
|
362
|
+
subject.split('\n').flatMap(component => {
|
|
363
|
+
const separator = component.indexOf('=');
|
|
364
|
+
|
|
365
|
+
if (separator < 1) {
|
|
366
|
+
return [];
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
return [[component.slice(0, separator), component.slice(separator + 1)]];
|
|
370
|
+
}),
|
|
371
|
+
);
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
function executeOpenSsl(arguments_: readonly string[]): Promise<void> {
|
|
375
|
+
return new Promise((resolve, reject) => {
|
|
376
|
+
execFile(
|
|
377
|
+
'openssl',
|
|
378
|
+
arguments_,
|
|
379
|
+
{timeout: OPENSSL_TIMEOUT, windowsHide: true},
|
|
380
|
+
error => {
|
|
381
|
+
if (error === null) {
|
|
382
|
+
resolve();
|
|
383
|
+
} else {
|
|
384
|
+
reject(
|
|
385
|
+
new Error('Failed to create local MQTT certificate request.', {
|
|
386
|
+
cause: error,
|
|
387
|
+
}),
|
|
388
|
+
);
|
|
389
|
+
}
|
|
390
|
+
},
|
|
391
|
+
);
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
function isFileNotFoundError(error: unknown): boolean {
|
|
396
|
+
if (typeof error !== 'object' || error === null || !('code' in error)) {
|
|
397
|
+
return false;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
return error.code === 'ENOENT';
|
|
401
|
+
}
|