@ohos-ports/trezor-device-authenticity 1.1.2-beta.1

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 (40) hide show
  1. package/README.md +7 -0
  2. package/lib/config/deviceAuthenticityBlacklistConfig.d.ts +3 -0
  3. package/lib/config/deviceAuthenticityBlacklistConfig.js +14 -0
  4. package/lib/config/deviceAuthenticityBlacklistConfigTypes.d.ts +11 -0
  5. package/lib/config/deviceAuthenticityBlacklistConfigTypes.js +15 -0
  6. package/lib/config/deviceAuthenticityConfig.d.ts +3 -0
  7. package/lib/config/deviceAuthenticityConfig.js +36 -0
  8. package/lib/config/deviceAuthenticityConfigTypes.d.ts +47 -0
  9. package/lib/config/deviceAuthenticityConfigTypes.js +21 -0
  10. package/lib/index.d.ts +9 -0
  11. package/lib/index.js +68 -0
  12. package/lib/tsconfig.lib.tsbuildinfo +1 -0
  13. package/lib/types.d.ts +27 -0
  14. package/lib/types.js +6 -0
  15. package/lib/utils.d.ts +17 -0
  16. package/lib/utils.js +38 -0
  17. package/lib/verifyAuthenticityProof.d.ts +4 -0
  18. package/lib/verifyAuthenticityProof.js +171 -0
  19. package/lib/x509certificate.d.ts +93 -0
  20. package/lib/x509certificate.js +277 -0
  21. package/libESM/config/deviceAuthenticityBlacklistConfig.d.ts +3 -0
  22. package/libESM/config/deviceAuthenticityBlacklistConfig.js +8 -0
  23. package/libESM/config/deviceAuthenticityBlacklistConfigTypes.d.ts +11 -0
  24. package/libESM/config/deviceAuthenticityBlacklistConfigTypes.js +9 -0
  25. package/libESM/config/deviceAuthenticityConfig.d.ts +3 -0
  26. package/libESM/config/deviceAuthenticityConfig.js +30 -0
  27. package/libESM/config/deviceAuthenticityConfigTypes.d.ts +47 -0
  28. package/libESM/config/deviceAuthenticityConfigTypes.js +15 -0
  29. package/libESM/index.d.ts +9 -0
  30. package/libESM/index.js +8 -0
  31. package/libESM/tsconfig.libESM.tsbuildinfo +1 -0
  32. package/libESM/types.d.ts +27 -0
  33. package/libESM/types.js +1 -0
  34. package/libESM/utils.d.ts +17 -0
  35. package/libESM/utils.js +28 -0
  36. package/libESM/verifyAuthenticityProof.d.ts +4 -0
  37. package/libESM/verifyAuthenticityProof.js +162 -0
  38. package/libESM/x509certificate.d.ts +93 -0
  39. package/libESM/x509certificate.js +269 -0
  40. package/package.json +62 -0
package/README.md ADDED
@@ -0,0 +1,7 @@
1
+ # @trezor/device-authenticity
2
+
3
+ [![NPM](https://img.shields.io/npm/v/@trezor/device-authenticity.svg)](https://www.npmjs.org/package/@trezor/device-authenticity)
4
+
5
+ Functionality regarding Trezor device authenticity verification in Connect, i.e. cryptographic verification of a secure element response.
6
+
7
+ Not to be mistaken with `@suite-common/device-authenticity`, which concerns with processing of Connect response in Suite, and implements it into Redux.
@@ -0,0 +1,3 @@
1
+ import type { DeviceAuthenticityBlacklistConfig } from './deviceAuthenticityBlacklistConfigTypes';
2
+ export declare const deviceAuthenticityBlacklistConfig: DeviceAuthenticityBlacklistConfig;
3
+ //# sourceMappingURL=deviceAuthenticityBlacklistConfig.d.ts.map
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.deviceAuthenticityBlacklistConfig = void 0;
7
+ exports.deviceAuthenticityBlacklistConfig = {
8
+ version: 1,
9
+ blacklistedCaPubKeys: [],
10
+ debug: {
11
+ blacklistedCaPubKeys: []
12
+ }
13
+ };
14
+ //# sourceMappingURL=deviceAuthenticityBlacklistConfig.js.map
@@ -0,0 +1,11 @@
1
+ import { Static } from '@trezor/schema-utils';
2
+ export type DeviceAuthenticityBlacklistConfig = Static<typeof DeviceAuthenticityBlacklistConfig>;
3
+ export declare const DeviceAuthenticityBlacklistConfig: import("@trezor/schema-utils").TIntersect<[import("@trezor/schema-utils").TObject<{
4
+ blacklistedCaPubKeys: import("@trezor/schema-utils").TArray<import("@trezor/schema-utils").TString>;
5
+ }>, import("@trezor/schema-utils").TObject<{
6
+ version: import("@trezor/schema-utils").TNumber;
7
+ debug: import("@trezor/schema-utils").TOptional<import("@trezor/schema-utils").TObject<{
8
+ blacklistedCaPubKeys: import("@trezor/schema-utils").TArray<import("@trezor/schema-utils").TString>;
9
+ }>>;
10
+ }>]>;
11
+ //# sourceMappingURL=deviceAuthenticityBlacklistConfigTypes.d.ts.map
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.DeviceAuthenticityBlacklistConfig = void 0;
7
+ const schema_utils_1 = require("@trezor/schema-utils");
8
+ const CertPubKeysBlacklist = schema_utils_1.Type.Object({
9
+ blacklistedCaPubKeys: schema_utils_1.Type.Array(schema_utils_1.Type.String())
10
+ });
11
+ exports.DeviceAuthenticityBlacklistConfig = schema_utils_1.Type.Intersect([CertPubKeysBlacklist, schema_utils_1.Type.Object({
12
+ version: schema_utils_1.Type.Number(),
13
+ debug: schema_utils_1.Type.Optional(CertPubKeysBlacklist)
14
+ })]);
15
+ //# sourceMappingURL=deviceAuthenticityBlacklistConfigTypes.js.map
@@ -0,0 +1,3 @@
1
+ import type { DeviceAuthenticityConfig } from './deviceAuthenticityConfigTypes';
2
+ export declare const deviceAuthenticityConfig: DeviceAuthenticityConfig;
3
+ //# sourceMappingURL=deviceAuthenticityConfig.d.ts.map
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.deviceAuthenticityConfig = void 0;
7
+ exports.deviceAuthenticityConfig = {
8
+ version: 2,
9
+ T2B1: {
10
+ rootPubKeysOptiga: ['04ca97480ac0d7b1e6efafe518cd433cec2bf8ab9822d76eafd34363b55d63e60380bff20acc75cde03cffcb50ab6f8ce70c878e37ebc58ff7cca0a83b16b15fa5'],
11
+ debug: {
12
+ rootPubKeysOptiga: ['047f77368dea2d4d61e989f474a56723c3212dacf8a808d8795595ef38441427c4389bc454f02089d7f08b873005e4c28d432468997871c0bf286fd3861e21e96a']
13
+ }
14
+ },
15
+ T3B1: {
16
+ rootPubKeysOptiga: ['045b5c3fdd01f3602092834209b86df0ca86a9faf25cac35c73bf6237d66eb21eafcec3706f1ccd5eb4cc7f2fa1751213eccb1c78389afba89a5788ff31ee46a5d'],
17
+ debug: {
18
+ rootPubKeysOptiga: ['047f77368dea2d4d61e989f474a56723c3212dacf8a808d8795595ef38441427c4389bc454f02089d7f08b873005e4c28d432468997871c0bf286fd3861e21e96a']
19
+ }
20
+ },
21
+ T3T1: {
22
+ rootPubKeysOptiga: ['041854b27fb1d9f65abb66828e78c9dc0ca301e66081ab0c6a4d104f9df1cd0ad5a7c75f77a8c092f55cf825d2abaf734f934c9394d5e75f75a5a06a5ee9be93ae'],
23
+ debug: {
24
+ rootPubKeysOptiga: ['04e48b69cd7962068d3cca3bcc6b1747ef496c1e28b5529e34ad7295215ea161dbe8fb08ae0479568f9d2cb07630cb3e52f4af0692102da5873559e45e9fa72959']
25
+ }
26
+ },
27
+ T3W1: {
28
+ rootPubKeysOptiga: ['040dde0d3e0d4da593fac6fd02a461d0e7eef238aca55c7c50b4e9ec37f3873303b6429ef1c9b78b4411a7dcbbc5dde5225979c1c2da3b073e82b1ed3f5f9825bb'],
29
+ rootPubKeysTropic: ['59237acd17134061d655b3f8d624573ca06ce8d862f38ba4e05140ce1d3d609d'],
30
+ debug: {
31
+ rootPubKeysOptiga: ['04521192e173a9da4e3023f747d836563725372681eba3079c56ff11b2fc137ab189eb4155f371127651b5594f8c332fc1e9c0f3b80d4212822668b63189706578'],
32
+ rootPubKeysTropic: []
33
+ }
34
+ }
35
+ };
36
+ //# sourceMappingURL=deviceAuthenticityConfig.js.map
@@ -0,0 +1,47 @@
1
+ import { Static } from '@trezor/schema-utils';
2
+ export type DeviceAuthenticityConfig = Static<typeof DeviceAuthenticityConfig>;
3
+ export declare const DeviceAuthenticityConfig: import("@trezor/schema-utils").TIntersect<[import("@trezor/schema-utils").TIntersect<[import("@trezor/schema-utils").TObject<{
4
+ T2B1: import("@trezor/schema-utils").TIntersect<[import("@trezor/schema-utils").TObject<{
5
+ rootPubKeysOptiga: import("@trezor/schema-utils").TArray<import("@trezor/schema-utils").TString>;
6
+ rootPubKeysTropic: import("@trezor/schema-utils").TOptional<import("@trezor/schema-utils").TArray<import("@trezor/schema-utils").TString>>;
7
+ }>, import("@trezor/schema-utils").TObject<{
8
+ debug: import("@trezor/schema-utils").TOptional<import("@trezor/schema-utils").TObject<{
9
+ rootPubKeysOptiga: import("@trezor/schema-utils").TArray<import("@trezor/schema-utils").TString>;
10
+ rootPubKeysTropic: import("@trezor/schema-utils").TOptional<import("@trezor/schema-utils").TArray<import("@trezor/schema-utils").TString>>;
11
+ }>>;
12
+ }>]>;
13
+ T3B1: import("@trezor/schema-utils").TIntersect<[import("@trezor/schema-utils").TObject<{
14
+ rootPubKeysOptiga: import("@trezor/schema-utils").TArray<import("@trezor/schema-utils").TString>;
15
+ rootPubKeysTropic: import("@trezor/schema-utils").TOptional<import("@trezor/schema-utils").TArray<import("@trezor/schema-utils").TString>>;
16
+ }>, import("@trezor/schema-utils").TObject<{
17
+ debug: import("@trezor/schema-utils").TOptional<import("@trezor/schema-utils").TObject<{
18
+ rootPubKeysOptiga: import("@trezor/schema-utils").TArray<import("@trezor/schema-utils").TString>;
19
+ rootPubKeysTropic: import("@trezor/schema-utils").TOptional<import("@trezor/schema-utils").TArray<import("@trezor/schema-utils").TString>>;
20
+ }>>;
21
+ }>]>;
22
+ T3T1: import("@trezor/schema-utils").TIntersect<[import("@trezor/schema-utils").TObject<{
23
+ rootPubKeysOptiga: import("@trezor/schema-utils").TArray<import("@trezor/schema-utils").TString>;
24
+ rootPubKeysTropic: import("@trezor/schema-utils").TOptional<import("@trezor/schema-utils").TArray<import("@trezor/schema-utils").TString>>;
25
+ }>, import("@trezor/schema-utils").TObject<{
26
+ debug: import("@trezor/schema-utils").TOptional<import("@trezor/schema-utils").TObject<{
27
+ rootPubKeysOptiga: import("@trezor/schema-utils").TArray<import("@trezor/schema-utils").TString>;
28
+ rootPubKeysTropic: import("@trezor/schema-utils").TOptional<import("@trezor/schema-utils").TArray<import("@trezor/schema-utils").TString>>;
29
+ }>>;
30
+ }>]>;
31
+ T3W1: import("@trezor/schema-utils").TIntersect<[import("@trezor/schema-utils").TObject<{
32
+ rootPubKeysOptiga: import("@trezor/schema-utils").TArray<import("@trezor/schema-utils").TString>;
33
+ rootPubKeysTropic: import("@trezor/schema-utils").TOptional<import("@trezor/schema-utils").TArray<import("@trezor/schema-utils").TString>>;
34
+ }>, import("@trezor/schema-utils").TObject<{
35
+ debug: import("@trezor/schema-utils").TOptional<import("@trezor/schema-utils").TObject<{
36
+ rootPubKeysOptiga: import("@trezor/schema-utils").TArray<import("@trezor/schema-utils").TString>;
37
+ rootPubKeysTropic: import("@trezor/schema-utils").TOptional<import("@trezor/schema-utils").TArray<import("@trezor/schema-utils").TString>>;
38
+ }>>;
39
+ }>]>;
40
+ }>, import("@trezor/schema-utils").TObject<{
41
+ T1B1: import("@trezor/schema-utils").TOptional<import("@trezor/schema-utils").TUndefined>;
42
+ T2T1: import("@trezor/schema-utils").TOptional<import("@trezor/schema-utils").TUndefined>;
43
+ UNKNOWN: import("@trezor/schema-utils").TOptional<import("@trezor/schema-utils").TUndefined>;
44
+ }>]>, import("@trezor/schema-utils").TObject<{
45
+ version: import("@trezor/schema-utils").TNumber;
46
+ }>]>;
47
+ //# sourceMappingURL=deviceAuthenticityConfigTypes.d.ts.map
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.DeviceAuthenticityConfig = void 0;
7
+ const protobuf_1 = require("@trezor/protobuf");
8
+ const schema_utils_1 = require("@trezor/schema-utils");
9
+ const CertPubKeys = schema_utils_1.Type.Object({
10
+ rootPubKeysOptiga: schema_utils_1.Type.Array(schema_utils_1.Type.String()),
11
+ rootPubKeysTropic: schema_utils_1.Type.Optional(schema_utils_1.Type.Array(schema_utils_1.Type.String()))
12
+ });
13
+ const ModelsWithKeys = schema_utils_1.Type.Exclude(schema_utils_1.Type.KeyOfEnum(protobuf_1.MessagesSchema.DeviceModelInternal), schema_utils_1.Type.Union([schema_utils_1.Type.Literal('T1B1'), schema_utils_1.Type.Literal('T2T1'), schema_utils_1.Type.Literal('UNKNOWN')]));
14
+ const ModelsWithoutKeys = schema_utils_1.Type.Extract(schema_utils_1.Type.KeyOfEnum(protobuf_1.MessagesSchema.DeviceModelInternal), schema_utils_1.Type.Union([schema_utils_1.Type.Literal('T1B1'), schema_utils_1.Type.Literal('T2T1'), schema_utils_1.Type.Literal('UNKNOWN')]));
15
+ const ModelPubKeys = schema_utils_1.Type.Intersect([schema_utils_1.Type.Record(ModelsWithKeys, schema_utils_1.Type.Intersect([CertPubKeys, schema_utils_1.Type.Object({
16
+ debug: schema_utils_1.Type.Optional(CertPubKeys)
17
+ })])), schema_utils_1.Type.Partial(schema_utils_1.Type.Record(ModelsWithoutKeys, schema_utils_1.Type.Undefined()))]);
18
+ exports.DeviceAuthenticityConfig = schema_utils_1.Type.Intersect([ModelPubKeys, schema_utils_1.Type.Object({
19
+ version: schema_utils_1.Type.Number()
20
+ })]);
21
+ //# sourceMappingURL=deviceAuthenticityConfigTypes.js.map
package/lib/index.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ export { verifyAuthenticityProof, verifySignatureP256 } from './verifyAuthenticityProof';
2
+ export { type AlgorithmName, parseName, parseCertificate } from './x509certificate';
3
+ export { getRandomChallenge } from './utils';
4
+ export type { VerifySignature, VerifyAuthenticityProofParams, VerifyAuthenticityProofResult, } from './types';
5
+ export { deviceAuthenticityBlacklistConfig } from './config/deviceAuthenticityBlacklistConfig';
6
+ export { DeviceAuthenticityBlacklistConfig } from './config/deviceAuthenticityBlacklistConfigTypes';
7
+ export { deviceAuthenticityConfig } from './config/deviceAuthenticityConfig';
8
+ export { DeviceAuthenticityConfig } from './config/deviceAuthenticityConfigTypes';
9
+ //# sourceMappingURL=index.d.ts.map
package/lib/index.js ADDED
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.DeviceAuthenticityConfig = exports.deviceAuthenticityConfig = exports.DeviceAuthenticityBlacklistConfig = exports.deviceAuthenticityBlacklistConfig = exports.getRandomChallenge = exports.parseCertificate = exports.parseName = exports.verifySignatureP256 = exports.verifyAuthenticityProof = void 0;
7
+ var verifyAuthenticityProof_1 = require("./verifyAuthenticityProof");
8
+ Object.defineProperty(exports, "verifyAuthenticityProof", {
9
+ enumerable: true,
10
+ get: function () {
11
+ return verifyAuthenticityProof_1.verifyAuthenticityProof;
12
+ }
13
+ });
14
+ Object.defineProperty(exports, "verifySignatureP256", {
15
+ enumerable: true,
16
+ get: function () {
17
+ return verifyAuthenticityProof_1.verifySignatureP256;
18
+ }
19
+ });
20
+ var x509certificate_1 = require("./x509certificate");
21
+ Object.defineProperty(exports, "parseName", {
22
+ enumerable: true,
23
+ get: function () {
24
+ return x509certificate_1.parseName;
25
+ }
26
+ });
27
+ Object.defineProperty(exports, "parseCertificate", {
28
+ enumerable: true,
29
+ get: function () {
30
+ return x509certificate_1.parseCertificate;
31
+ }
32
+ });
33
+ var utils_1 = require("./utils");
34
+ Object.defineProperty(exports, "getRandomChallenge", {
35
+ enumerable: true,
36
+ get: function () {
37
+ return utils_1.getRandomChallenge;
38
+ }
39
+ });
40
+ var deviceAuthenticityBlacklistConfig_1 = require("./config/deviceAuthenticityBlacklistConfig");
41
+ Object.defineProperty(exports, "deviceAuthenticityBlacklistConfig", {
42
+ enumerable: true,
43
+ get: function () {
44
+ return deviceAuthenticityBlacklistConfig_1.deviceAuthenticityBlacklistConfig;
45
+ }
46
+ });
47
+ var deviceAuthenticityBlacklistConfigTypes_1 = require("./config/deviceAuthenticityBlacklistConfigTypes");
48
+ Object.defineProperty(exports, "DeviceAuthenticityBlacklistConfig", {
49
+ enumerable: true,
50
+ get: function () {
51
+ return deviceAuthenticityBlacklistConfigTypes_1.DeviceAuthenticityBlacklistConfig;
52
+ }
53
+ });
54
+ var deviceAuthenticityConfig_1 = require("./config/deviceAuthenticityConfig");
55
+ Object.defineProperty(exports, "deviceAuthenticityConfig", {
56
+ enumerable: true,
57
+ get: function () {
58
+ return deviceAuthenticityConfig_1.deviceAuthenticityConfig;
59
+ }
60
+ });
61
+ var deviceAuthenticityConfigTypes_1 = require("./config/deviceAuthenticityConfigTypes");
62
+ Object.defineProperty(exports, "DeviceAuthenticityConfig", {
63
+ enumerable: true,
64
+ get: function () {
65
+ return deviceAuthenticityConfigTypes_1.DeviceAuthenticityConfig;
66
+ }
67
+ });
68
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"root":["../src/index.ts","../src/types.ts","../src/utils.ts","../src/verifyAuthenticityProof.ts","../src/x509certificate.ts","../src/config/deviceAuthenticityBlacklistConfig.ts","../src/config/deviceAuthenticityBlacklistConfigTypes.ts","../src/config/deviceAuthenticityConfig.ts","../src/config/deviceAuthenticityConfigTypes.ts"],"version":"5.8.3"}
package/lib/types.d.ts ADDED
@@ -0,0 +1,27 @@
1
+ import { MessagesSchema as PROTO } from '@trezor/protobuf';
2
+ import type { DeviceAuthenticityBlacklistConfig } from './config/deviceAuthenticityBlacklistConfigTypes';
3
+ import type { DeviceAuthenticityConfig } from './config/deviceAuthenticityConfigTypes';
4
+ export type VerifySignature = (rawKey: Buffer, data: Uint8Array, signature: Uint8Array) => boolean | Promise<boolean>;
5
+ export type VerifyAuthenticityProofParams = {
6
+ challenge: Buffer;
7
+ certificates: string[];
8
+ signature: string;
9
+ deviceModel: keyof typeof PROTO.DeviceModelInternal;
10
+ config: DeviceAuthenticityConfig;
11
+ blacklistConfig: DeviceAuthenticityBlacklistConfig;
12
+ allowDebugKeys?: boolean;
13
+ challengePrefix?: string;
14
+ bufferChunks?: Buffer[];
15
+ };
16
+ export type VerifyAuthenticityProofResult = {
17
+ valid: true;
18
+ caPubKey: string;
19
+ rootPubKey: string;
20
+ error?: typeof undefined;
21
+ } | {
22
+ valid: false;
23
+ caPubKey?: string;
24
+ rootPubKey?: string;
25
+ error: 'ROOT_PUBKEY_NOT_FOUND' | 'CA_PUBKEY_BLACKLISTED' | 'INVALID_DEVICE_MODEL' | 'INVALID_DEVICE_CERTIFICATE' | 'INVALID_DEVICE_SIGNATURE' | 'RESPONSE_PAYLOAD_MISSING';
26
+ };
27
+ //# sourceMappingURL=types.d.ts.map
package/lib/types.js ADDED
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ //# sourceMappingURL=types.js.map
package/lib/utils.d.ts ADDED
@@ -0,0 +1,17 @@
1
+ import { MessagesSchema as PROTO } from '@trezor/protobuf';
2
+ import type { DeviceAuthenticityBlacklistConfig } from './config/deviceAuthenticityBlacklistConfigTypes';
3
+ import type { DeviceAuthenticityConfig } from './config/deviceAuthenticityConfigTypes';
4
+ export declare const getRandomChallenge: () => Buffer<ArrayBufferLike>;
5
+ type GetRootPubKeyBlacklistParams = {
6
+ blacklistConfig: DeviceAuthenticityBlacklistConfig;
7
+ allowDebugKeys?: boolean;
8
+ };
9
+ export declare const getRootPubKeyBlacklist: ({ blacklistConfig, allowDebugKeys, }: GetRootPubKeyBlacklistParams) => string[];
10
+ type GetRootPubKeysParams = {
11
+ config: DeviceAuthenticityConfig;
12
+ deviceModel: keyof typeof PROTO.DeviceModelInternal;
13
+ allowDebugKeys?: boolean;
14
+ };
15
+ export declare const getRootPubKeys: ({ config, deviceModel, allowDebugKeys, }: GetRootPubKeysParams) => string[];
16
+ export {};
17
+ //# sourceMappingURL=utils.d.ts.map
package/lib/utils.js ADDED
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.getRootPubKeys = exports.getRootPubKeyBlacklist = exports.getRandomChallenge = void 0;
7
+ const tslib_1 = require("tslib");
8
+ const crypto = tslib_1.__importStar(require("crypto"));
9
+ const getRandomChallenge = () => crypto.randomBytes(32);
10
+ exports.getRandomChallenge = getRandomChallenge;
11
+ const getRootPubKeyBlacklist = ({
12
+ blacklistConfig,
13
+ allowDebugKeys
14
+ }) => {
15
+ const normalBlacklist = blacklistConfig.blacklistedCaPubKeys ?? [];
16
+ const debugBlacklist = blacklistConfig.debug?.blacklistedCaPubKeys ?? [];
17
+ return allowDebugKeys ? normalBlacklist.concat(debugBlacklist) : normalBlacklist;
18
+ };
19
+ exports.getRootPubKeyBlacklist = getRootPubKeyBlacklist;
20
+ const getRootPubKeys = ({
21
+ config,
22
+ deviceModel,
23
+ allowDebugKeys
24
+ }) => {
25
+ const modelConfig = config[deviceModel];
26
+ if (modelConfig === undefined) {
27
+ throw new Error(`Pubkeys for ${deviceModel} not found in config`);
28
+ }
29
+ const rootPubKeysNormalOptiga = modelConfig.rootPubKeysOptiga ?? [];
30
+ const rootPubKeysNormalTropic = modelConfig.rootPubKeysTropic ?? [];
31
+ const rootPubKeysDebugOptiga = modelConfig.debug?.rootPubKeysOptiga ?? [];
32
+ const rootPubKeysDebugTropic = modelConfig.debug?.rootPubKeysTropic ?? [];
33
+ const rootPubKeysNormal = [...rootPubKeysNormalOptiga, ...rootPubKeysNormalTropic];
34
+ if (!allowDebugKeys) return rootPubKeysNormal;
35
+ return [...rootPubKeysNormal, ...rootPubKeysDebugOptiga, ...rootPubKeysDebugTropic];
36
+ };
37
+ exports.getRootPubKeys = getRootPubKeys;
38
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1,4 @@
1
+ import { VerifyAuthenticityProofParams, VerifyAuthenticityProofResult, VerifySignature } from './types';
2
+ export declare const verifySignatureP256: VerifySignature;
3
+ export declare const verifyAuthenticityProof: ({ challenge, certificates, signature, deviceModel, allowDebugKeys, config, blacklistConfig, challengePrefix, bufferChunks, }: VerifyAuthenticityProofParams) => Promise<VerifyAuthenticityProofResult>;
4
+ //# sourceMappingURL=verifyAuthenticityProof.d.ts.map
@@ -0,0 +1,171 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.verifyAuthenticityProof = exports.verifySignatureP256 = void 0;
7
+ const tslib_1 = require("tslib");
8
+ const ed25519_js_1 = require("@noble/curves/ed25519.js");
9
+ const crypto = tslib_1.__importStar(require("crypto"));
10
+ const crypto_utils_1 = require("@trezor/crypto-utils");
11
+ const utils_1 = require("@trezor/utils");
12
+ const utils_2 = require("./utils");
13
+ const x509certificate_1 = require("./x509certificate");
14
+ const verifySignatureP256 = async (rawKey, data, signature) => {
15
+ const signer = crypto.createVerify('sha256');
16
+ signer.update(Buffer.from(data));
17
+ const SubtleCrypto = (0, crypto_utils_1.getSubtleCrypto)();
18
+ try {
19
+ const ecPubKey = await SubtleCrypto.importKey('raw', rawKey, {
20
+ name: 'ECDSA',
21
+ namedCurve: 'P-256'
22
+ }, true, ['verify']);
23
+ const spkiPubKey = await SubtleCrypto.exportKey('spki', ecPubKey);
24
+ const key = `-----BEGIN PUBLIC KEY-----\n${Buffer.from(spkiPubKey).toString('base64')}\n-----END PUBLIC KEY-----`;
25
+ return signer.verify({
26
+ key
27
+ }, Buffer.from(signature));
28
+ } catch {
29
+ return false;
30
+ }
31
+ };
32
+ exports.verifySignatureP256 = verifySignatureP256;
33
+ const verifySignatureEd25519 = (rawKey, data, signature) => {
34
+ try {
35
+ return ed25519_js_1.ed25519.verify(signature, data, rawKey);
36
+ } catch {
37
+ return false;
38
+ }
39
+ };
40
+ const getVerifyFn = algorithmName => {
41
+ if (algorithmName === 'P-256') return exports.verifySignatureP256;
42
+ if (algorithmName === 'Ed25519') return verifySignatureEd25519;
43
+ throw new Error(`Unsupported signature algorithm.`);
44
+ };
45
+ const validateCaCertExtensions = (cert, pathLen) => {
46
+ let hasKeyUsage,
47
+ hasBasicConstraints = false;
48
+ cert.tbsCertificate.extensions.forEach(ext => {
49
+ if (ext.key === 'keyUsage') {
50
+ if (ext.keyCertSign !== '1') {
51
+ throw new Error(`CA keyCertSign not set`);
52
+ }
53
+ hasKeyUsage = true;
54
+ } else if (ext.key === 'basicConstraints') {
55
+ if (!ext.cA) {
56
+ throw new Error(`CA basicConstraints.cA not set`);
57
+ }
58
+ if (typeof ext.pathLenConstraint != 'number') {
59
+ throw new Error('CA basicConstraints.pathLenConstraint not set');
60
+ }
61
+ if (ext.pathLenConstraint < pathLen) {
62
+ throw new Error('Issuer was not permitted to issue certificate');
63
+ }
64
+ hasBasicConstraints = true;
65
+ } else if (ext.critical) {
66
+ throw new Error(`Unknown critical extension ${ext.key} in CA certificate`);
67
+ }
68
+ });
69
+ if (!hasKeyUsage || !hasBasicConstraints) {
70
+ throw new Error(`CA missing extensions keyUsage: ${hasKeyUsage}, basicConstraints: ${hasBasicConstraints}`);
71
+ }
72
+ };
73
+ const verifyAuthenticityProof = async ({
74
+ challenge,
75
+ certificates,
76
+ signature,
77
+ deviceModel,
78
+ allowDebugKeys,
79
+ config,
80
+ blacklistConfig,
81
+ challengePrefix = 'AuthenticateDevice:',
82
+ bufferChunks = []
83
+ }) => {
84
+ const allRootPubKeys = (0, utils_2.getRootPubKeys)({
85
+ config,
86
+ deviceModel,
87
+ allowDebugKeys
88
+ });
89
+ const rootPubKeyBlacklist = (0, utils_2.getRootPubKeyBlacklist)({
90
+ blacklistConfig,
91
+ allowDebugKeys
92
+ });
93
+ const [deviceCert, caCert] = certificates.map((c, i) => {
94
+ const cert = (0, x509certificate_1.parseCertificate)(new Uint8Array(Buffer.from(c, 'hex')));
95
+ if (i === 0) {
96
+ return cert;
97
+ }
98
+ validateCaCertExtensions(cert, i - 1);
99
+ return cert;
100
+ });
101
+ const deviceCertAlgName = deviceCert.signatureAlgorithm.algorithmName;
102
+ const caCertAlgName = caCert.signatureAlgorithm.algorithmName;
103
+ if (deviceCertAlgName !== caCertAlgName) {
104
+ throw new Error('Mismatched signature algorithms in device and CA certificates');
105
+ }
106
+ const verifySignatureFn = getVerifyFn(deviceCertAlgName);
107
+ const caPubKey = Buffer.from(caCert.tbsCertificate.subjectPublicKeyInfo.bits.bytes).toString('hex');
108
+ const isCertSignedByRootPubkey = await Promise.all(allRootPubKeys.map(rootPubKey => verifySignatureFn(Buffer.from(rootPubKey, 'hex'), caCert.tbsCertificate.asn1.raw, caCert.signatureValue.bits.bytes)));
109
+ const rootPubKeyIndex = isCertSignedByRootPubkey.findIndex(valid => !!valid);
110
+ const rootPubKeyMatch = allRootPubKeys[rootPubKeyIndex];
111
+ const caCertValidityFrom = caCert.tbsCertificate.validity.from.getTime();
112
+ if (caCertValidityFrom > new Date().getTime()) {
113
+ throw new Error(`CA validity from ${caCertValidityFrom} can't be in the future!`);
114
+ }
115
+ if (rootPubKeyMatch === undefined) {
116
+ return {
117
+ valid: false,
118
+ caPubKey,
119
+ error: 'ROOT_PUBKEY_NOT_FOUND'
120
+ };
121
+ }
122
+ const [subject] = deviceCert.tbsCertificate.subject;
123
+ if (!subject.parameters || subject.algorithmOid !== '2.5.4.3') {
124
+ throw new Error('Missing certificate subject');
125
+ }
126
+ const subjectValue = Buffer.from(subject.parameters.asn1.contents.subarray(0, 4)).toString();
127
+ if (subjectValue !== deviceModel) {
128
+ return {
129
+ valid: false,
130
+ caPubKey,
131
+ rootPubKey: rootPubKeyMatch,
132
+ error: 'INVALID_DEVICE_MODEL'
133
+ };
134
+ }
135
+ const isDeviceCertValid = await verifySignatureFn(Buffer.from(caCert.tbsCertificate.subjectPublicKeyInfo.bits.bytes), deviceCert.tbsCertificate.asn1.raw, deviceCert.signatureValue.bits.bytes);
136
+ const challengePrefixBuffer = Buffer.from(challengePrefix);
137
+ const chunks = bufferChunks && bufferChunks.length > 0 ? [challengePrefixBuffer, ...bufferChunks] : [challengePrefixBuffer, challenge];
138
+ const prefixedBuffer = Buffer.concat(chunks.flatMap(chunk => [utils_1.bufferUtils.getChunkSize(chunk.byteLength), chunk]));
139
+ const isSignatureValid = await verifySignatureFn(Buffer.from(deviceCert.tbsCertificate.subjectPublicKeyInfo.bits.bytes), prefixedBuffer, Buffer.from(signature, 'hex'));
140
+ if (isDeviceCertValid && isSignatureValid) {
141
+ if (rootPubKeyBlacklist.includes(caPubKey)) {
142
+ return {
143
+ valid: false,
144
+ caPubKey,
145
+ rootPubKey: rootPubKeyMatch,
146
+ error: 'CA_PUBKEY_BLACKLISTED'
147
+ };
148
+ }
149
+ return {
150
+ valid: true,
151
+ caPubKey,
152
+ rootPubKey: rootPubKeyMatch
153
+ };
154
+ }
155
+ if (!isDeviceCertValid) {
156
+ return {
157
+ valid: false,
158
+ caPubKey,
159
+ rootPubKey: rootPubKeyMatch,
160
+ error: 'INVALID_DEVICE_CERTIFICATE'
161
+ };
162
+ }
163
+ return {
164
+ valid: false,
165
+ caPubKey,
166
+ rootPubKey: rootPubKeyMatch,
167
+ error: 'INVALID_DEVICE_SIGNATURE'
168
+ };
169
+ };
170
+ exports.verifyAuthenticityProof = verifyAuthenticityProof;
171
+ //# sourceMappingURL=verifyAuthenticityProof.js.map
@@ -0,0 +1,93 @@
1
+ interface Asn1 {
2
+ cls: number;
3
+ tag: number;
4
+ structured: boolean;
5
+ byteLength: number;
6
+ contents: Uint8Array;
7
+ raw: Uint8Array;
8
+ }
9
+ type Oid = `${number}.${number}.${number}.${number}` | `${number}.${number}.${number}.${number}.${number}.${number}`;
10
+ export type AlgorithmName = 'P-256' | 'Ed25519' | 'unknown';
11
+ type Extension = {
12
+ key: 'keyUsage';
13
+ critical?: boolean;
14
+ keyCertSign: '0' | '1';
15
+ } | {
16
+ key: 'basicConstraints';
17
+ critical?: boolean;
18
+ cA: boolean;
19
+ pathLenConstraint?: number;
20
+ } | (Asn1 & {
21
+ key: Oid;
22
+ critical?: boolean;
23
+ });
24
+ export declare const parseName: (asn1: Asn1) => {
25
+ asn1: Asn1;
26
+ algorithmOid: Oid;
27
+ algorithmName: AlgorithmName;
28
+ parameters: {
29
+ asn1: Asn1;
30
+ } | null;
31
+ }[];
32
+ export declare const parseCertificate: (byteArray: Uint8Array) => {
33
+ asn1: Asn1;
34
+ tbsCertificate: {
35
+ asn1: Asn1;
36
+ version: Asn1;
37
+ serialNumber: Asn1;
38
+ signature: {
39
+ asn1: Asn1;
40
+ algorithmOid: Oid;
41
+ algorithmName: AlgorithmName;
42
+ parameters: {
43
+ asn1: Asn1;
44
+ } | null;
45
+ };
46
+ issuer: Asn1;
47
+ validity: {
48
+ from: Date;
49
+ to: Date;
50
+ };
51
+ subject: {
52
+ asn1: Asn1;
53
+ algorithmOid: Oid;
54
+ algorithmName: AlgorithmName;
55
+ parameters: {
56
+ asn1: Asn1;
57
+ } | null;
58
+ }[];
59
+ subjectPublicKeyInfo: {
60
+ asn1: Asn1;
61
+ algorithm: {
62
+ asn1: Asn1;
63
+ algorithmOid: Oid;
64
+ algorithmName: AlgorithmName;
65
+ parameters: {
66
+ asn1: Asn1;
67
+ } | null;
68
+ };
69
+ bits: {
70
+ unusedBits: number;
71
+ bytes: Uint8Array<ArrayBufferLike>;
72
+ };
73
+ };
74
+ extensions: Extension[];
75
+ };
76
+ signatureAlgorithm: {
77
+ asn1: Asn1;
78
+ algorithmOid: Oid;
79
+ algorithmName: AlgorithmName;
80
+ parameters: {
81
+ asn1: Asn1;
82
+ } | null;
83
+ };
84
+ signatureValue: {
85
+ asn1: Asn1;
86
+ bits: {
87
+ unusedBits: number;
88
+ bytes: Uint8Array<ArrayBufferLike>;
89
+ };
90
+ };
91
+ };
92
+ export {};
93
+ //# sourceMappingURL=x509certificate.d.ts.map