sm-crypto-wasm 0.7.0 → 0.7.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 (126) hide show
  1. package/README.md +41 -876
  2. package/dist/asn1/decode.d.ts +103 -12
  3. package/dist/asn1/decode.js +129 -10
  4. package/dist/asn1/encode.d.ts +120 -19
  5. package/dist/asn1/encode.js +158 -19
  6. package/dist/asn1/index.d.ts +6 -0
  7. package/dist/asn1/index.js +6 -0
  8. package/dist/asn1/oid.d.ts +18 -0
  9. package/dist/asn1/oid.js +30 -1
  10. package/dist/asn1/time.d.ts +32 -0
  11. package/dist/asn1/time.js +55 -1
  12. package/dist/asn1/types.d.ts +100 -16
  13. package/dist/asn1/types.js +47 -6
  14. package/dist/cms/errors.d.ts +12 -2
  15. package/dist/cms/errors.js +7 -1
  16. package/dist/cms/index.d.ts +5 -0
  17. package/dist/cms/index.js +5 -0
  18. package/dist/cms/oids.d.ts +37 -7
  19. package/dist/cms/oids.js +37 -7
  20. package/dist/cms/parse.d.ts +155 -13
  21. package/dist/cms/parse.js +98 -4
  22. package/dist/cms/sign.d.ts +80 -10
  23. package/dist/cms/sign.js +54 -2
  24. package/dist/cms/verify.d.ts +84 -9
  25. package/dist/cms/verify.js +54 -5
  26. package/dist/common/hybrid.d.ts +25 -0
  27. package/dist/common/hybrid.js +25 -0
  28. package/dist/common/sealed.d.ts +119 -15
  29. package/dist/common/sealed.js +67 -9
  30. package/dist/esbuild-plugin.d.ts +74 -5
  31. package/dist/esbuild-plugin.js +69 -4
  32. package/dist/index.d.ts +19 -0
  33. package/dist/index.js +22 -0
  34. package/dist/pem/asn1.d.ts +13 -0
  35. package/dist/pem/asn1.js +13 -0
  36. package/dist/pem/index.d.ts +2 -0
  37. package/dist/pem/index.js +2 -0
  38. package/dist/pem/pkcs8.d.ts +64 -8
  39. package/dist/pem/pkcs8.js +66 -6
  40. package/dist/pem/sm2.d.ts +76 -4
  41. package/dist/pem/sm2.js +169 -12
  42. package/dist/pem/sm9.d.ts +42 -2
  43. package/dist/pem/sm9.js +42 -2
  44. package/dist/runtime/base64.core.d.ts +25 -1
  45. package/dist/runtime/base64.core.js +31 -1
  46. package/dist/runtime/convert.d.ts +70 -7
  47. package/dist/runtime/convert.js +129 -9
  48. package/dist/runtime/errors.d.ts +64 -8
  49. package/dist/runtime/errors.js +64 -8
  50. package/dist/runtime/index.d.ts +5 -0
  51. package/dist/runtime/index.js +5 -0
  52. package/dist/runtime/loadWasm.browser.d.ts +33 -0
  53. package/dist/runtime/loadWasm.browser.js +46 -0
  54. package/dist/runtime/loadWasm.core.d.ts +59 -3
  55. package/dist/runtime/loadWasm.core.js +53 -3
  56. package/dist/runtime/loadWasm.d.ts +44 -0
  57. package/dist/runtime/loadWasm.js +79 -4
  58. package/dist/runtime/random.browser.d.ts +24 -0
  59. package/dist/runtime/random.browser.js +31 -1
  60. package/dist/runtime/random.core.d.ts +21 -0
  61. package/dist/runtime/random.core.js +27 -0
  62. package/dist/runtime/random.d.ts +28 -0
  63. package/dist/runtime/random.js +40 -1
  64. package/dist/sm2/exchange.d.ts +132 -16
  65. package/dist/sm2/exchange.js +120 -9
  66. package/dist/sm2/hybrid.d.ts +65 -3
  67. package/dist/sm2/hybrid.js +53 -1
  68. package/dist/sm2/index.d.ts +260 -21
  69. package/dist/sm2/index.js +346 -23
  70. package/dist/sm2/kem.d.ts +62 -5
  71. package/dist/sm2/kem.js +37 -2
  72. package/dist/sm2/keyInput.d.ts +36 -3
  73. package/dist/sm2/keyInput.js +36 -3
  74. package/dist/sm3/index.d.ts +30 -0
  75. package/dist/sm3/index.js +55 -1
  76. package/dist/sm4/index.d.ts +125 -17
  77. package/dist/sm4/index.js +201 -14
  78. package/dist/sm9/core.d.ts +58 -8
  79. package/dist/sm9/core.js +36 -5
  80. package/dist/sm9/hybrid.d.ts +66 -3
  81. package/dist/sm9/hybrid.js +46 -1
  82. package/dist/sm9/index.d.ts +265 -19
  83. package/dist/sm9/index.js +175 -5
  84. package/dist/sm9/input.d.ts +37 -2
  85. package/dist/sm9/input.js +50 -1
  86. package/dist/sm9/kem.d.ts +57 -4
  87. package/dist/sm9/kem.js +47 -1
  88. package/dist/webcrypto/algorithms.d.ts +58 -12
  89. package/dist/webcrypto/algorithms.js +58 -10
  90. package/dist/webcrypto/cryptoKey.d.ts +81 -15
  91. package/dist/webcrypto/cryptoKey.js +69 -11
  92. package/dist/webcrypto/errors.d.ts +29 -5
  93. package/dist/webcrypto/errors.js +29 -5
  94. package/dist/webcrypto/index.d.ts +10 -0
  95. package/dist/webcrypto/index.js +10 -0
  96. package/dist/webcrypto/subtle.d.ts +53 -3
  97. package/dist/webcrypto/subtle.js +69 -1
  98. package/dist/x509/encode.d.ts +86 -16
  99. package/dist/x509/encode.js +89 -16
  100. package/dist/x509/errors.d.ts +16 -1
  101. package/dist/x509/errors.js +16 -1
  102. package/dist/x509/extensions.d.ts +46 -7
  103. package/dist/x509/extensions.js +67 -9
  104. package/dist/x509/index.d.ts +7 -0
  105. package/dist/x509/index.js +19 -0
  106. package/dist/x509/ip.d.ts +9 -0
  107. package/dist/x509/ip.js +21 -0
  108. package/dist/x509/issue.d.ts +45 -2
  109. package/dist/x509/issue.js +107 -0
  110. package/dist/x509/oids.d.ts +41 -7
  111. package/dist/x509/oids.js +58 -7
  112. package/dist/x509/parse.d.ts +21 -0
  113. package/dist/x509/parse.js +115 -1
  114. package/dist/x509/sha1.d.ts +12 -1
  115. package/dist/x509/sha1.js +18 -1
  116. package/dist/x509/sha256.d.ts +12 -1
  117. package/dist/x509/sha256.js +31 -1
  118. package/dist/x509/sig.d.ts +15 -0
  119. package/dist/x509/sig.js +28 -1
  120. package/dist/x509/types.d.ts +312 -57
  121. package/dist/x509/types.js +6 -0
  122. package/dist/x509/verify.d.ts +51 -4
  123. package/dist/x509/verify.js +42 -3
  124. package/dist/zuc/index.d.ts +128 -10
  125. package/dist/zuc/index.js +136 -7
  126. package/package.json +53 -36
@@ -12,17 +12,46 @@
12
12
  * - `raw` 是对输入缓冲的**视图**(不拷贝),调用方不应假定其生命周期;
13
13
  * 需要长期持有时自行 `slice()`;
14
14
  * - 解析错误统一为 `Asn1Error`;上层(X.509/CMS)负责转换为各自的错误类型。
15
+ *
16
+ * Shared ASN.1 types and constants (a DER/BER subset).
17
+ *
18
+ * Relationship to the legacy `src/pem/asn1.ts`:
19
+ * - The old implementation only covered the minimal types used by PKCS#8 and did
20
+ * not preserve the `constructed` bit;
21
+ * - This layer is the shared infrastructure for X.509 / CMS / WebCrypto: it covers
22
+ * BOOLEAN, SET, string types, time, and context/application/private tags.
23
+ *
24
+ * Design notes:
25
+ * - Each node keeps `raw` (the original slice including tag + length + value).
26
+ * X.509 TBS digests and CMS `signedAttrs` verification must hash the original
27
+ * bytes, never a re-encoding;
28
+ * - `raw` is a **view** onto the input buffer (no copy); callers must not assume
29
+ * its lifetime and should `slice()` when they need to keep it;
30
+ * - Parse errors are always `Asn1Error`; upper layers (X.509/CMS) map them to
31
+ * their own error types.
32
+ */
33
+ /**
34
+ * ASN.1 标签类别。
35
+ *
36
+ * ASN.1 tag class.
15
37
  */
16
- /** ASN.1 标签类别。 */
17
38
  export type Asn1Class = 0 | 1 | 2 | 3;
18
- /** ASN.1 标签类别常量。 */
39
+ /**
40
+ * ASN.1 标签类别常量。
41
+ *
42
+ * ASN.1 tag class constants.
43
+ */
19
44
  export declare const Asn1Class: {
20
45
  readonly Universal: 0;
21
46
  readonly Application: 1;
22
47
  readonly ContextSpecific: 2;
23
48
  readonly Private: 3;
24
49
  };
25
- /** UNIVERSAL 类常用标签编号。 */
50
+ /**
51
+ * UNIVERSAL 类常用标签编号。
52
+ *
53
+ * Frequently used UNIVERSAL tag numbers.
54
+ */
26
55
  export declare const UniversalTag: {
27
56
  readonly EndOfContent: 0;
28
57
  readonly Boolean: 1;
@@ -45,32 +74,87 @@ export declare const UniversalTag: {
45
74
  readonly UniversalString: 28;
46
75
  readonly BmpString: 30;
47
76
  };
48
- /** ASN.1 节点。 */
77
+ /**
78
+ * ASN.1 节点。
79
+ *
80
+ * An ASN.1 node.
81
+ */
49
82
  export interface Asn1Node {
50
- /** 标签类别:UNIVERSAL=0、APPLICATION=1、CONTEXT=2、PRIVATE=3。 */
83
+ /**
84
+ * 标签类别:UNIVERSAL=0、APPLICATION=1、CONTEXT=2、PRIVATE=3。
85
+ *
86
+ * Tag class: UNIVERSAL=0, APPLICATION=1, CONTEXT=2, PRIVATE=3.
87
+ */
51
88
  readonly cls: Asn1Class;
52
- /** 标签编号(高 tag number 已展开为数值,不含 0x1f 标志位)。 */
89
+ /**
90
+ * 标签编号(高 tag number 已展开为数值,不含 0x1f 标志位)。
91
+ *
92
+ * Tag number (high tag numbers are already decoded to a plain number, without
93
+ * the 0x1f marker bit).
94
+ */
53
95
  readonly tag: number;
54
- /** 是否为 constructed 编码。 */
96
+ /**
97
+ * 是否为 constructed 编码。
98
+ *
99
+ * Whether the encoding is constructed.
100
+ */
55
101
  readonly constructed: boolean;
56
- /** 是否使用 BER indefinite-length(`0x80` + EOC)。DER 节点恒为 false。 */
102
+ /**
103
+ * 是否使用 BER indefinite-length(`0x80` + EOC)。DER 节点恒为 false。
104
+ *
105
+ * Whether BER indefinite-length (`0x80` + EOC) is used; always false for DER.
106
+ */
57
107
  readonly indefinite: boolean;
58
- /** 内容字节数(indefinite 时为实际内容长度,不含 EOC)。 */
108
+ /**
109
+ * 内容字节数(indefinite 时为实际内容长度,不含 EOC)。
110
+ *
111
+ * Content length in bytes (for indefinite nodes this is the real content length,
112
+ * excluding the EOC).
113
+ */
59
114
  readonly length: number;
60
- /** primitive 的 value;constructed 为子节点编码拼接后的内容字节。 */
115
+ /**
116
+ * primitive 的 value;constructed 为子节点编码拼接后的内容字节。
117
+ *
118
+ * The value of a primitive node; for a constructed node, the concatenated
119
+ * encodings of its children.
120
+ */
61
121
  readonly value: Uint8Array;
62
- /** constructed 节点的子节点;primitive 为 `undefined`。 */
122
+ /**
123
+ * constructed 节点的子节点;primitive 为 `undefined`。
124
+ *
125
+ * Children of a constructed node; `undefined` for primitive nodes.
126
+ */
63
127
  readonly children?: readonly Asn1Node[];
64
- /** 节点原始字节(含 tag + length + value;indefinite 时含 EOC)。 */
128
+ /**
129
+ * 节点原始字节(含 tag + length + value;indefinite 时含 EOC)。
130
+ *
131
+ * Raw node bytes (tag + length + value; including the EOC when indefinite).
132
+ */
65
133
  readonly raw: Uint8Array;
66
134
  }
67
- /** ASN.1 解析/编码错误。 */
135
+ /**
136
+ * ASN.1 解析/编码错误。
137
+ *
138
+ * ASN.1 parse/encode error.
139
+ */
68
140
  export declare class Asn1Error extends Error {
69
141
  readonly name = "Asn1Error";
70
142
  }
71
- /** 判断节点是否为指定 UNIVERSAL 类型。 */
143
+ /**
144
+ * 判断节点是否为指定 UNIVERSAL 类型。
145
+ *
146
+ * Tests whether a node is the given UNIVERSAL type.
147
+ */
72
148
  export declare function isUniversal(node: Asn1Node, tag: number): boolean;
73
- /** 断言节点为指定 UNIVERSAL 类型,否则抛 `Asn1Error`。 */
149
+ /**
150
+ * 断言节点为指定 UNIVERSAL 类型,否则抛 `Asn1Error`。
151
+ *
152
+ * Asserts that a node is the given UNIVERSAL type, otherwise throws `Asn1Error`.
153
+ */
74
154
  export declare function expectUniversal(node: Asn1Node | undefined, tag: number, what: string): Asn1Node;
75
- /** 断言节点为 constructed SEQUENCE 并返回子节点。 */
155
+ /**
156
+ * 断言节点为 constructed SEQUENCE 并返回子节点。
157
+ *
158
+ * Asserts that a node is a constructed SEQUENCE and returns its children.
159
+ */
76
160
  export declare function expectSequence(node: Asn1Node | undefined, what: string): readonly Asn1Node[];
@@ -12,15 +12,40 @@
12
12
  * - `raw` 是对输入缓冲的**视图**(不拷贝),调用方不应假定其生命周期;
13
13
  * 需要长期持有时自行 `slice()`;
14
14
  * - 解析错误统一为 `Asn1Error`;上层(X.509/CMS)负责转换为各自的错误类型。
15
+ *
16
+ * Shared ASN.1 types and constants (a DER/BER subset).
17
+ *
18
+ * Relationship to the legacy `src/pem/asn1.ts`:
19
+ * - The old implementation only covered the minimal types used by PKCS#8 and did
20
+ * not preserve the `constructed` bit;
21
+ * - This layer is the shared infrastructure for X.509 / CMS / WebCrypto: it covers
22
+ * BOOLEAN, SET, string types, time, and context/application/private tags.
23
+ *
24
+ * Design notes:
25
+ * - Each node keeps `raw` (the original slice including tag + length + value).
26
+ * X.509 TBS digests and CMS `signedAttrs` verification must hash the original
27
+ * bytes, never a re-encoding;
28
+ * - `raw` is a **view** onto the input buffer (no copy); callers must not assume
29
+ * its lifetime and should `slice()` when they need to keep it;
30
+ * - Parse errors are always `Asn1Error`; upper layers (X.509/CMS) map them to
31
+ * their own error types.
32
+ */
33
+ /**
34
+ * ASN.1 标签类别常量。
35
+ *
36
+ * ASN.1 tag class constants.
15
37
  */
16
- /** ASN.1 标签类别常量。 */
17
38
  export const Asn1Class = {
18
39
  Universal: 0,
19
40
  Application: 1,
20
41
  ContextSpecific: 2,
21
42
  Private: 3,
22
43
  };
23
- /** UNIVERSAL 类常用标签编号。 */
44
+ /**
45
+ * UNIVERSAL 类常用标签编号。
46
+ *
47
+ * Frequently used UNIVERSAL tag numbers.
48
+ */
24
49
  export const UniversalTag = {
25
50
  EndOfContent: 0,
26
51
  Boolean: 1,
@@ -43,15 +68,27 @@ export const UniversalTag = {
43
68
  UniversalString: 28,
44
69
  BmpString: 30,
45
70
  };
46
- /** ASN.1 解析/编码错误。 */
71
+ /**
72
+ * ASN.1 解析/编码错误。
73
+ *
74
+ * ASN.1 parse/encode error.
75
+ */
47
76
  export class Asn1Error extends Error {
48
77
  name = 'Asn1Error';
49
78
  }
50
- /** 判断节点是否为指定 UNIVERSAL 类型。 */
79
+ /**
80
+ * 判断节点是否为指定 UNIVERSAL 类型。
81
+ *
82
+ * Tests whether a node is the given UNIVERSAL type.
83
+ */
51
84
  export function isUniversal(node, tag) {
52
85
  return node.cls === Asn1Class.Universal && node.tag === tag;
53
86
  }
54
- /** 断言节点为指定 UNIVERSAL 类型,否则抛 `Asn1Error`。 */
87
+ /**
88
+ * 断言节点为指定 UNIVERSAL 类型,否则抛 `Asn1Error`。
89
+ *
90
+ * Asserts that a node is the given UNIVERSAL type, otherwise throws `Asn1Error`.
91
+ */
55
92
  export function expectUniversal(node, tag, what) {
56
93
  if (node === undefined) {
57
94
  throw new Asn1Error(`${what}: missing element (expected tag ${tag})`);
@@ -61,7 +98,11 @@ export function expectUniversal(node, tag, what) {
61
98
  }
62
99
  return node;
63
100
  }
64
- /** 断言节点为 constructed SEQUENCE 并返回子节点。 */
101
+ /**
102
+ * 断言节点为 constructed SEQUENCE 并返回子节点。
103
+ *
104
+ * Asserts that a node is a constructed SEQUENCE and returns its children.
105
+ */
65
106
  export function expectSequence(node, what) {
66
107
  const seq = expectUniversal(node, UniversalTag.Sequence, what);
67
108
  if (!seq.children) {
@@ -1,13 +1,23 @@
1
1
  /**
2
2
  * CMS(PKCS#7 SignedData)错误类型。
3
+ *
4
+ * CMS (PKCS#7 SignedData) error types.
3
5
  */
4
6
  import { SmCryptoError } from '../runtime/errors.js';
5
- /** CMS 解析/签名/验签失败。 */
7
+ /**
8
+ * CMS 解析/签名/验签失败。
9
+ *
10
+ * CMS parse/sign/verify failure.
11
+ */
6
12
  export declare class CmsError extends SmCryptoError {
7
13
  readonly name: string;
8
14
  constructor(message: string, options?: {
9
15
  cause?: unknown;
10
16
  });
11
17
  }
12
- /** `verifySignedData` 的结构化失败原因。 */
18
+ /**
19
+ * `verifySignedData` 的结构化失败原因。
20
+ *
21
+ * Structured failure reasons returned by `verifySignedData`.
22
+ */
13
23
  export type CmsVerifyErrorCode = 'NO_CERTIFICATE' | 'DIGEST_MISMATCH' | 'SIGNATURE_INVALID' | 'UNSUPPORTED_ALGORITHM' | 'MALFORMED';
@@ -1,8 +1,14 @@
1
1
  /**
2
2
  * CMS(PKCS#7 SignedData)错误类型。
3
+ *
4
+ * CMS (PKCS#7 SignedData) error types.
3
5
  */
4
6
  import { SmCryptoError } from '../runtime/errors.js';
5
- /** CMS 解析/签名/验签失败。 */
7
+ /**
8
+ * CMS 解析/签名/验签失败。
9
+ *
10
+ * CMS parse/sign/verify failure.
11
+ */
6
12
  export class CmsError extends SmCryptoError {
7
13
  name = 'CmsError';
8
14
  constructor(message, options) {
@@ -2,6 +2,11 @@
2
2
  * CMS SignedData 子模块(`sm-crypto-wasm/cms`)。
3
3
  *
4
4
  * 只做签名/验签,不建立信任链;证书有效期/用途/链校验由调用方负责。
5
+ *
6
+ * CMS SignedData submodule (`sm-crypto-wasm/cms`).
7
+ *
8
+ * Signing/verification only, no trust chain building: certificate validity, key usage
9
+ * and chain validation are the caller's responsibility.
5
10
  */
6
11
  export { CmsError, type CmsVerifyErrorCode } from './errors.js';
7
12
  export * from './oids.js';
package/dist/cms/index.js CHANGED
@@ -2,6 +2,11 @@
2
2
  * CMS SignedData 子模块(`sm-crypto-wasm/cms`)。
3
3
  *
4
4
  * 只做签名/验签,不建立信任链;证书有效期/用途/链校验由调用方负责。
5
+ *
6
+ * CMS SignedData submodule (`sm-crypto-wasm/cms`).
7
+ *
8
+ * Signing/verification only, no trust chain building: certificate validity, key usage
9
+ * and chain validation are the caller's responsibility.
5
10
  */
6
11
  export { CmsError } from './errors.js';
7
12
  export * from './oids.js';
@@ -1,17 +1,47 @@
1
1
  /**
2
2
  * CMS oid 常量。
3
+ *
4
+ * CMS OID constants.
5
+ */
6
+ /**
7
+ * id-data(1.2.840.113549.1.7.1)。
8
+ *
9
+ * id-data (1.2.840.113549.1.7.1).
3
10
  */
4
- /** id-data(1.2.840.113549.1.7.1)。 */
5
11
  export declare const OID_CMS_DATA = "1.2.840.113549.1.7.1";
6
- /** id-signedData(1.2.840.113549.1.7.2)。 */
12
+ /**
13
+ * id-signedData(1.2.840.113549.1.7.2)。
14
+ *
15
+ * id-signedData (1.2.840.113549.1.7.2).
16
+ */
7
17
  export declare const OID_CMS_SIGNED_DATA = "1.2.840.113549.1.7.2";
8
- /** contentType 签名属性(1.2.840.113549.1.9.3)。 */
18
+ /**
19
+ * contentType 签名属性(1.2.840.113549.1.9.3)。
20
+ *
21
+ * The contentType signed attribute (1.2.840.113549.1.9.3).
22
+ */
9
23
  export declare const OID_CMS_CONTENT_TYPE = "1.2.840.113549.1.9.3";
10
- /** messageDigest 签名属性(1.2.840.113549.1.9.4)。 */
24
+ /**
25
+ * messageDigest 签名属性(1.2.840.113549.1.9.4)。
26
+ *
27
+ * The messageDigest signed attribute (1.2.840.113549.1.9.4).
28
+ */
11
29
  export declare const OID_CMS_MESSAGE_DIGEST = "1.2.840.113549.1.9.4";
12
- /** signingTime 签名属性(1.2.840.113549.1.9.5)。 */
30
+ /**
31
+ * signingTime 签名属性(1.2.840.113549.1.9.5)。
32
+ *
33
+ * The signingTime signed attribute (1.2.840.113549.1.9.5).
34
+ */
13
35
  export declare const OID_CMS_SIGNING_TIME = "1.2.840.113549.1.9.5";
14
- /** SM3 摘要算法(1.2.156.10197.1.401)。 */
36
+ /**
37
+ * SM3 摘要算法(1.2.156.10197.1.401)。
38
+ *
39
+ * The SM3 digest algorithm (1.2.156.10197.1.401).
40
+ */
15
41
  export declare const OID_SM3 = "1.2.156.10197.1.401";
16
- /** SM3withSM2(1.2.156.10197.1.501)。 */
42
+ /**
43
+ * SM3withSM2(1.2.156.10197.1.501)。
44
+ *
45
+ * SM3withSM2 (1.2.156.10197.1.501).
46
+ */
17
47
  export declare const OID_SM3_WITH_SM2 = "1.2.156.10197.1.501";
package/dist/cms/oids.js CHANGED
@@ -1,17 +1,47 @@
1
1
  /**
2
2
  * CMS oid 常量。
3
+ *
4
+ * CMS OID constants.
5
+ */
6
+ /**
7
+ * id-data(1.2.840.113549.1.7.1)。
8
+ *
9
+ * id-data (1.2.840.113549.1.7.1).
3
10
  */
4
- /** id-data(1.2.840.113549.1.7.1)。 */
5
11
  export const OID_CMS_DATA = '1.2.840.113549.1.7.1';
6
- /** id-signedData(1.2.840.113549.1.7.2)。 */
12
+ /**
13
+ * id-signedData(1.2.840.113549.1.7.2)。
14
+ *
15
+ * id-signedData (1.2.840.113549.1.7.2).
16
+ */
7
17
  export const OID_CMS_SIGNED_DATA = '1.2.840.113549.1.7.2';
8
- /** contentType 签名属性(1.2.840.113549.1.9.3)。 */
18
+ /**
19
+ * contentType 签名属性(1.2.840.113549.1.9.3)。
20
+ *
21
+ * The contentType signed attribute (1.2.840.113549.1.9.3).
22
+ */
9
23
  export const OID_CMS_CONTENT_TYPE = '1.2.840.113549.1.9.3';
10
- /** messageDigest 签名属性(1.2.840.113549.1.9.4)。 */
24
+ /**
25
+ * messageDigest 签名属性(1.2.840.113549.1.9.4)。
26
+ *
27
+ * The messageDigest signed attribute (1.2.840.113549.1.9.4).
28
+ */
11
29
  export const OID_CMS_MESSAGE_DIGEST = '1.2.840.113549.1.9.4';
12
- /** signingTime 签名属性(1.2.840.113549.1.9.5)。 */
30
+ /**
31
+ * signingTime 签名属性(1.2.840.113549.1.9.5)。
32
+ *
33
+ * The signingTime signed attribute (1.2.840.113549.1.9.5).
34
+ */
13
35
  export const OID_CMS_SIGNING_TIME = '1.2.840.113549.1.9.5';
14
- /** SM3 摘要算法(1.2.156.10197.1.401)。 */
36
+ /**
37
+ * SM3 摘要算法(1.2.156.10197.1.401)。
38
+ *
39
+ * The SM3 digest algorithm (1.2.156.10197.1.401).
40
+ */
15
41
  export const OID_SM3 = '1.2.156.10197.1.401';
16
- /** SM3withSM2(1.2.156.10197.1.501)。 */
42
+ /**
43
+ * SM3withSM2(1.2.156.10197.1.501)。
44
+ *
45
+ * SM3withSM2 (1.2.156.10197.1.501).
46
+ */
17
47
  export const OID_SM3_WITH_SM2 = '1.2.156.10197.1.501';
@@ -9,53 +9,173 @@
9
9
  * 除首字节外不做任何重编码,避免 SET 顺序/长度编码差异破坏合法签名。
10
10
  *
11
11
  * BER 只在 CMS 路径启用;证书本身仍按 DER 严格解析。
12
+ *
13
+ * CMS SignedData parsing (read-only).
14
+ *
15
+ * Key point (red line #1): the signing input of `signedAttrs` is the **original DER
16
+ * bytes** (a SET OF Attribute), not a re-encoding of the parsed form. This module keeps:
17
+ * - `raw`: the bytes obtained by replacing the leading byte of `[0] IMPLICIT` (0xA0)
18
+ * with the SET tag (0x31);
19
+ * - for BER indefinite-length, the value is first encoded as definite-length and then
20
+ * the leading byte is replaced.
21
+ * Nothing but the leading byte is re-encoded, so SET ordering or length-encoding
22
+ * differences can never break a valid signature.
23
+ *
24
+ * BER is enabled on the CMS path only; certificates themselves are still parsed as
25
+ * strict DER.
12
26
  */
13
27
  import { type Asn1Node } from '../asn1/index.js';
14
28
  import type { Certificate } from '../x509/types.js';
15
- /** 一个签名属性(Attribute ::= SEQUENCE { attrType, attrValues SET OF }). */
29
+ /**
30
+ * 一个签名属性(Attribute ::= SEQUENCE { attrType, attrValues SET OF }).
31
+ *
32
+ * A single signed attribute (`Attribute ::= SEQUENCE { attrType, attrValues SET OF }`).
33
+ */
16
34
  export interface CmsAttribute {
35
+ /**
36
+ * 属性类型 OID。
37
+ *
38
+ * The attribute type OID.
39
+ */
17
40
  readonly oid: string;
18
- /** AttributeValue 节点(原始视图)。 */
41
+ /**
42
+ * AttributeValue 节点(原始视图)。
43
+ *
44
+ * AttributeValue nodes (raw views).
45
+ */
19
46
  readonly values: readonly Asn1Node[];
20
- /** 整个 Attribute 的原始 DER。 */
47
+ /**
48
+ * 整个 Attribute 的原始 DER。
49
+ *
50
+ * Raw DER of the whole Attribute.
51
+ */
21
52
  readonly raw: Uint8Array;
22
53
  }
23
- /** 签名者标识。 */
54
+ /**
55
+ * 签名者标识。
56
+ *
57
+ * The signer identifier.
58
+ */
24
59
  export type CmsSignerIdentifier = {
25
60
  readonly type: 'issuerAndSerialNumber';
26
- /** 签发者 Name 的原始 DER,用于按字节匹配证书。 */
61
+ /**
62
+ * 签发者 Name 的原始 DER,用于按字节匹配证书。
63
+ *
64
+ * Raw DER of the issuer Name, used to match certificates byte for byte.
65
+ */
27
66
  readonly issuerRaw: Uint8Array;
67
+ /**
68
+ * 证书序列号(ASN.1 INTEGER,无符号大整数)。
69
+ *
70
+ * The certificate serial number (an ASN.1 INTEGER held as a bigint).
71
+ */
28
72
  readonly serialNumber: bigint;
29
73
  } | {
30
74
  readonly type: 'subjectKeyIdentifier';
75
+ /**
76
+ * `subjectKeyIdentifier` 的原始字节。
77
+ *
78
+ * Raw bytes of the `subjectKeyIdentifier` extension value.
79
+ */
31
80
  readonly keyIdentifier: Uint8Array;
32
81
  };
33
- /** 解析后的 SignerInfo。 */
82
+ /**
83
+ * 解析后的 SignerInfo。
84
+ *
85
+ * A parsed SignerInfo.
86
+ */
34
87
  export interface CmsSignerInfo {
88
+ /**
89
+ * SignerInfo 版本号(原样解析,不做校验)。
90
+ *
91
+ * The SignerInfo version, taken as parsed and not validated.
92
+ */
35
93
  readonly version: number;
94
+ /**
95
+ * 签名者标识(`issuerAndSerialNumber` 或 `subjectKeyIdentifier`)。
96
+ *
97
+ * The signer identifier (`issuerAndSerialNumber` or `subjectKeyIdentifier`).
98
+ */
36
99
  readonly sid: CmsSignerIdentifier;
100
+ /**
101
+ * 摘要算法 OID;验签层只接受 SM3(`OID_SM3`)。
102
+ *
103
+ * Digest algorithm OID; the verification layer only accepts SM3 (`OID_SM3`).
104
+ */
37
105
  readonly digestAlgorithm: string;
38
106
  /**
39
107
  * signedAttrs 的签名输入字节(SET tag 0x31)。
40
108
  * 没有 signedAttrs 时为 `undefined`(直接签内容)。
109
+ *
110
+ * The signing-input bytes of signedAttrs (SET tag 0x31).
111
+ * `undefined` when there are no signedAttrs (the content is signed directly).
41
112
  */
42
113
  readonly signedAttrs?: {
43
114
  readonly raw: Uint8Array;
44
115
  readonly attributes: readonly CmsAttribute[];
45
116
  };
117
+ /**
118
+ * 签名算法 OID;验签层只接受 SM3withSM2(`OID_SM3_WITH_SM2`)。
119
+ *
120
+ * Signature algorithm OID; the verification layer only accepts SM3withSM2
121
+ * (`OID_SM3_WITH_SM2`).
122
+ */
46
123
  readonly signatureAlgorithm: string;
124
+ /**
125
+ * 签名字节(DER 的 `SEQUENCE { r, s }`,验签前转成裸 `r || s`)。
126
+ *
127
+ * Signature bytes (a DER `SEQUENCE { r, s }`, converted to raw `r || s` before
128
+ * verification).
129
+ */
47
130
  readonly signature: Uint8Array;
48
131
  }
49
- /** 解析后的 SignedData。 */
132
+ /**
133
+ * 解析后的 SignedData。
134
+ *
135
+ * A parsed SignedData.
136
+ */
50
137
  export interface ParsedSignedData {
138
+ /**
139
+ * SignedData 版本号(原样解析,不做校验)。
140
+ *
141
+ * The SignedData version, taken as parsed and not validated.
142
+ */
51
143
  readonly version: number;
144
+ /**
145
+ * `digestAlgorithms` 里的算法 OID 列表。
146
+ *
147
+ * The algorithm OIDs listed in `digestAlgorithms`.
148
+ */
52
149
  readonly digestAlgorithms: readonly string[];
150
+ /**
151
+ * 内容类型 OID(本仓库的签名路径固定输出 id-data)。
152
+ *
153
+ * The content type OID (the signing path here always emits id-data).
154
+ */
53
155
  readonly eContentType: string;
54
- /** 内嵌内容;detached 时为 `undefined`。 */
156
+ /**
157
+ * 内嵌内容;detached 时为 `undefined`。
158
+ *
159
+ * The embedded content; `undefined` for detached signatures.
160
+ */
55
161
  readonly eContent?: Uint8Array;
162
+ /**
163
+ * 解析成功的 X.509 证书(无法解析的 CertificateChoices 已跳过)。
164
+ *
165
+ * Successfully parsed X.509 certificates (unparseable CertificateChoices skipped).
166
+ */
56
167
  readonly certificates: readonly Certificate[];
168
+ /**
169
+ * SignerInfo 列表,顺序与 CMS 中一致。
170
+ *
171
+ * The SignerInfo entries, in the order they appear in the CMS.
172
+ */
57
173
  readonly signerInfos: readonly CmsSignerInfo[];
58
- /** 整个 ContentInfo 的原始 DER。 */
174
+ /**
175
+ * 整个 ContentInfo 的原始 DER。
176
+ *
177
+ * Raw DER of the whole ContentInfo.
178
+ */
59
179
  readonly raw: Uint8Array;
60
180
  }
61
181
  /**
@@ -63,13 +183,35 @@ export interface ParsedSignedData {
63
183
  *
64
184
  * @param input PEM(`CMS` / `PKCS7`)、base64、hex 或 DER/BER 字节。
65
185
  * @throws CmsError 结构非法、不是 SignedData、或尾部有多余字节。
186
+ *
187
+ * Parses a CMS ContentInfo (SignedData only).
188
+ *
189
+ * @param input PEM (`CMS` / `PKCS7`), base64, hex, or DER/BER bytes.
190
+ * @throws CmsError When the structure is invalid, is not SignedData, or has trailing
191
+ * bytes.
66
192
  */
67
193
  export declare function parseSignedData(input: string | Uint8Array): ParsedSignedData;
68
- /** 读取签名属性中的时间值(供验签层使用)。 */
194
+ /**
195
+ * 读取签名属性中的时间值(供验签层使用)。
196
+ *
197
+ * Reads the time value of a signed attribute (used by the verification layer).
198
+ */
69
199
  export declare function attributeTime(attribute: CmsAttribute | undefined): Date | undefined;
70
- /** 读取签名属性中的 OCTET STRING 值(messageDigest)。 */
200
+ /**
201
+ * 读取签名属性中的 OCTET STRING 值(messageDigest)。
202
+ *
203
+ * Reads the OCTET STRING value of a signed attribute (messageDigest).
204
+ */
71
205
  export declare function attributeOctets(attribute: CmsAttribute | undefined): Uint8Array | undefined;
72
- /** 读取签名属性中的 OID 值(contentType)。 */
206
+ /**
207
+ * 读取签名属性中的 OID 值(contentType)。
208
+ *
209
+ * Reads the OID value of a signed attribute (contentType).
210
+ */
73
211
  export declare function attributeOid(attribute: CmsAttribute | undefined): string | undefined;
74
- /** 十六进制便捷函数(供错误信息/调试使用)。 */
212
+ /**
213
+ * 十六进制便捷函数(供错误信息/调试使用)。
214
+ *
215
+ * Hex convenience helper, for error messages and debugging.
216
+ */
75
217
  export declare function cmsHex(bytes: Uint8Array): string;