micro509 0.7.2 → 0.9.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 (109) hide show
  1. package/dist/index.d.ts +5 -5
  2. package/dist/index.js +15 -1
  3. package/dist/internal/asn1/asn1.js +280 -2
  4. package/dist/internal/asn1/der.js +378 -2
  5. package/dist/internal/asn1/oids.js +103 -2
  6. package/dist/internal/crypto/algorithm-names.js +58 -2
  7. package/dist/internal/crypto/ecdsa.js +68 -2
  8. package/dist/internal/crypto/hash.js +286 -2
  9. package/dist/internal/crypto/pbes2.js +213 -2
  10. package/dist/internal/crypto/rsa-pss.js +184 -2
  11. package/dist/internal/crypto/sig-verify.js +192 -2
  12. package/dist/internal/crypto/signing.d.ts +1 -2
  13. package/dist/internal/crypto/signing.js +123 -2
  14. package/dist/internal/crypto/webcrypto.js +14 -2
  15. package/dist/internal/shared/base64.js +16 -2
  16. package/dist/internal/shared/dn.js +84 -2
  17. package/dist/internal/shared/ip.js +164 -2
  18. package/dist/internal/verify/name-constraints-engine.js +450 -2
  19. package/dist/internal/verify/policy-engine.js +382 -2
  20. package/dist/internal/verify/verify-path.js +298 -2
  21. package/dist/internal/x509/extension-bits.js +132 -2
  22. package/dist/internal/x509/extension-registry.js +229 -2
  23. package/dist/internal/x509/name-fields.js +93 -2
  24. package/dist/keys/keys.d.ts +380 -13
  25. package/dist/keys/keys.js +1247 -5
  26. package/dist/keys.d.ts +2 -2
  27. package/dist/keys.js +2 -1
  28. package/dist/pem/pem.d.ts +2 -2
  29. package/dist/pem/pem.js +172 -6
  30. package/dist/pem.js +2 -1
  31. package/dist/pkcs/pfx.d.ts +4 -5
  32. package/dist/pkcs/pfx.js +395 -2
  33. package/dist/pkcs/pkcs12-mac.d.ts +2 -2
  34. package/dist/pkcs/pkcs12-mac.js +175 -2
  35. package/dist/pkcs/pkcs7.d.ts +1 -2
  36. package/dist/pkcs/pkcs7.js +653 -2
  37. package/dist/pkcs.d.ts +2 -2
  38. package/dist/pkcs.js +4 -1
  39. package/dist/result/result.d.ts +1 -2
  40. package/dist/result/result.js +111 -2
  41. package/dist/result.js +2 -1
  42. package/dist/revocation/chain.d.ts +4 -5
  43. package/dist/revocation/chain.js +536 -2
  44. package/dist/revocation/crl.d.ts +2 -2
  45. package/dist/revocation/crl.js +1119 -2
  46. package/dist/revocation/ocsp.d.ts +5 -4
  47. package/dist/revocation/ocsp.js +958 -2
  48. package/dist/revocation/revocation.d.ts +1 -2
  49. package/dist/revocation/revocation.js +261 -2
  50. package/dist/revocation.d.ts +2 -2
  51. package/dist/revocation.js +5 -1
  52. package/dist/verify/identity.d.ts +1 -2
  53. package/dist/verify/identity.js +270 -2
  54. package/dist/verify/name-constraints.d.ts +1 -2
  55. package/dist/verify/policy.d.ts +1 -2
  56. package/dist/verify/verify.d.ts +1 -2
  57. package/dist/verify/verify.js +764 -2
  58. package/dist/verify.js +3 -1
  59. package/dist/x509/certificate.d.ts +1 -2
  60. package/dist/x509/certificate.js +151 -2
  61. package/dist/x509/csr.d.ts +1 -2
  62. package/dist/x509/csr.js +71 -2
  63. package/dist/x509/extensions.d.ts +1 -2
  64. package/dist/x509/extensions.js +387 -2
  65. package/dist/x509/name.d.ts +1 -2
  66. package/dist/x509/name.js +173 -2
  67. package/dist/x509/parse.d.ts +84 -2
  68. package/dist/x509/parse.js +1090 -2
  69. package/dist/x509.d.ts +2 -2
  70. package/dist/x509.js +6 -1
  71. package/package.json +74 -34
  72. package/dist/internal/asn1/asn1.js.map +0 -1
  73. package/dist/internal/asn1/der.js.map +0 -1
  74. package/dist/internal/asn1/oids.js.map +0 -1
  75. package/dist/internal/crypto/algorithm-names.js.map +0 -1
  76. package/dist/internal/crypto/ecdsa.js.map +0 -1
  77. package/dist/internal/crypto/hash.js.map +0 -1
  78. package/dist/internal/crypto/pbes2.d.ts +0 -23
  79. package/dist/internal/crypto/pbes2.js.map +0 -1
  80. package/dist/internal/crypto/rsa-pss.js.map +0 -1
  81. package/dist/internal/crypto/sig-verify.js.map +0 -1
  82. package/dist/internal/crypto/signing.js.map +0 -1
  83. package/dist/internal/crypto/webcrypto.js.map +0 -1
  84. package/dist/internal/shared/base64.js.map +0 -1
  85. package/dist/internal/shared/dn.js.map +0 -1
  86. package/dist/internal/shared/ip.js.map +0 -1
  87. package/dist/internal/verify/name-constraints-engine.js.map +0 -1
  88. package/dist/internal/verify/policy-engine.js.map +0 -1
  89. package/dist/internal/verify/verify-path.js.map +0 -1
  90. package/dist/internal/x509/extension-bits.js.map +0 -1
  91. package/dist/internal/x509/extension-registry.js.map +0 -1
  92. package/dist/internal/x509/name-fields.js.map +0 -1
  93. package/dist/keys/keys.js.map +0 -1
  94. package/dist/pem/pem.js.map +0 -1
  95. package/dist/pkcs/pfx.js.map +0 -1
  96. package/dist/pkcs/pkcs12-mac.js.map +0 -1
  97. package/dist/pkcs/pkcs7.js.map +0 -1
  98. package/dist/result/result.js.map +0 -1
  99. package/dist/revocation/chain.js.map +0 -1
  100. package/dist/revocation/crl.js.map +0 -1
  101. package/dist/revocation/ocsp.js.map +0 -1
  102. package/dist/revocation/revocation.js.map +0 -1
  103. package/dist/verify/identity.js.map +0 -1
  104. package/dist/verify/verify.js.map +0 -1
  105. package/dist/x509/certificate.js.map +0 -1
  106. package/dist/x509/csr.js.map +0 -1
  107. package/dist/x509/extensions.js.map +0 -1
  108. package/dist/x509/name.js.map +0 -1
  109. package/dist/x509/parse.js.map +0 -1
@@ -1,2 +1,378 @@
1
- function e(e){if(O(e,`DER length`),e<128)return Uint8Array.of(e);let t=C(e);return Uint8Array.of(128|t.length,...t)}function t(e){let t=e.reduce((e,t)=>e+t.length,0),n=new Uint8Array(t),r=0;for(let t of e)n.set(t,r),r+=t.length;return n}function n(n,r){return k(n),t([Uint8Array.of(n),e(r.length),r])}function r(e){return n(48,t(e))}function i(e){return n(49,t(e.slice().sort((e,t)=>{let n=Math.min(e.length,t.length);for(let r=0;r<n;r++){let n=(e[r]??0)-(t[r]??0);if(n!==0)return n}return e.length-t.length})))}function a(e,t){return A(e),n(160+e,t)}function o(e,t){return A(e),n(160+e,t)}function s(e,t){return A(e),n(128+e,t)}function c(e){if(e.length===0)return n(2,Uint8Array.of(0));let r=0;for(;r<e.length-1&&e[r]===0;)r+=1;let i=e.slice(r);return(i[0]??0)>=128?n(2,t([Uint8Array.of(0),i])):n(2,i)}function l(e){if(!Number.isSafeInteger(e)||e<0)throw Error(`INTEGER must be a non-negative safe integer`);return c(e===0?Uint8Array.of(0):Uint8Array.from(C(e)))}function u(e){return n(1,Uint8Array.of(e?255:0))}function d(){return n(5,new Uint8Array)}function f(e){return n(4,e)}function p(e,r=0){if(r<0||r>7)throw Error(`unusedBits must be between 0 and 7`);if(e.length===0&&r!==0)throw Error(`unusedBits must be 0 when value is empty`);if(e.length>0&&r>0&&(e[e.length-1]??0)&(1<<r)-1)throw Error(`unused bits in the last byte must be zero`);return n(3,t([Uint8Array.of(r),e]))}function m(e){return n(12,new TextEncoder().encode(e))}function h(e){if(!/^[A-Za-z0-9 '()+,\-./:=?]*$/.test(e))throw Error(`Invalid PrintableString: contains characters outside the allowed set`);return n(19,new TextEncoder().encode(e))}function g(e){for(let t=0;t<e.length;t++)if(e.charCodeAt(t)>127)throw Error(`Invalid IA5String: contains non-ASCII characters`);return n(22,new TextEncoder().encode(e))}function _(e){let t=[Number(e&127n)],n=e>>7n;for(;n>0n;)t.unshift(128|Number(n&127n)),n>>=7n;return t}function v(e){let t=/^\d+$/,r=e.split(`.`).map(e=>{if(!t.test(e))throw Error(`Invalid OID segment: ${e}`);return BigInt(e)});if(r.length<2)throw Error(`Invalid OID: ${e}`);let[i,a,...o]=r;if(i===void 0||a===void 0)throw Error(`Invalid OID: ${e}`);if(i!==0n&&i!==1n&&i!==2n)throw Error(`Invalid OID first arc: ${i}`);if((i===0n||i===1n)&&a>=40n)throw Error(`Invalid OID second arc: ${a} (must be < 40 when first arc is ${i})`);let s=_(i*40n+a);for(let e of o)s.push(..._(e));return n(6,Uint8Array.from(s))}function y(e){j(e,`UTCTime`);let t=e.getUTCFullYear();if(t<1950||t>2049)throw RangeError(`UTCTime year must be between 1950 and 2049`);let r=`${[S(t%100),S(e.getUTCMonth()+1),S(e.getUTCDate()),S(e.getUTCHours()),S(e.getUTCMinutes()),S(e.getUTCSeconds())].join(``)}Z`;return n(23,new TextEncoder().encode(r))}function b(e){j(e,`GeneralizedTime`);let t=e.getUTCFullYear();if(t<0||t>9999)throw RangeError(`GeneralizedTime year must be between 0 and 9999`);let r=`${[String(t).padStart(4,`0`),S(e.getUTCMonth()+1),S(e.getUTCDate()),S(e.getUTCHours()),S(e.getUTCMinutes()),S(e.getUTCSeconds())].join(``)}Z`;return n(24,new TextEncoder().encode(r))}function x(e){return e.getUTCFullYear()>=2050||e.getUTCFullYear()<1950?b(e):y(e)}function S(e){return String(e).padStart(2,`0`)}function C(e){O(e,`DER integer`);let t=[],n=e;for(;n>0;)t.unshift(n&255),n=Math.floor(n/256);return t}function w(e,t=0){let n=e[t];if(n===void 0)throw Error(`Unexpected end of DER input`);if((n&31)==31)throw Error(`High-tag-number DER form is not supported`);let r=e[t+1];if(r===void 0)throw Error(`Unexpected end of DER input`);let i=2,a=0;if(!(r&128))a=r;else{let n=r&127;if(n===0)throw Error(`Indefinite lengths are not supported`);let o=e[t+2];if(o===void 0)throw Error(`Unexpected end of DER input`);if(o===0)throw Error(`Non-minimal DER length encoding`);i+=n;for(let r=0;r<n;r+=1){let n=e[t+2+r];if(n===void 0)throw Error(`Unexpected end of DER input`);if(a>Math.floor((2**53-1-n)/256))throw Error(`DER length exceeds safe integer range`);a=a*256+n}if(a<128)throw Error(`Non-minimal DER length encoding`)}let o=t+i,s=o+a;if(s>e.length)throw Error(`DER element exceeds input length`);return{tag:n,headerLength:i,length:a,start:o,end:s,value:e.slice(o,s)}}function T(e,t=64,n){if(!Number.isSafeInteger(t)||t<1)throw Error(`DER max depth must be a positive safe integer`);let r=w(e,0);if(r.end!==e.length)throw Error(`Trailing data after DER element`);let i=[{element:r,depth:1}];for(;i.length>0;){let r=i.pop();if(r===void 0)continue;if(r.depth>t)throw Error(`DER exceeds max depth of ${t}`);if(!(r.element.tag&32))continue;let a=r.element.start,o=!1;for(;a<r.element.end;){let t;try{t=w(e,a)}catch(e){if(M(r.element,a,n)){o=!0,a=r.element.end;break}throw e}if(t.end>r.element.end){if(M(r.element,a,n)){o=!0,a=r.element.end;break}throw Error(`DER child exceeds parent length`)}i.push({element:t,depth:r.depth+1}),a=t.end}if(!o&&a!==r.element.end)throw Error(`Malformed DER container`)}}function E(e,t){T(e,t?.maxDepth??64,t);let n=w(e,0);if(n.end!==e.length)throw Error(`Trailing data after DER element`);return n}function D(e,t){let n=E(e,t);if(n.tag!==48)throw Error(`Expected SEQUENCE`);let r=[],i=n.start;for(;i<n.end;){let t=w(e,i);if(t.end>n.end)throw Error(`DER child exceeds parent length`);r.push(t),i=t.end}if(i!==n.end)throw Error(`Malformed DER sequence`);return r}function O(e,t){if(!Number.isSafeInteger(e)||e<0)throw Error(`${t} must be a non-negative safe integer`)}function k(e){if(!Number.isSafeInteger(e)||e<0||e>255)throw RangeError(`DER tag octet must be between 0 and 255`);if((e&31)==31)throw Error(`High-tag-number DER tags are not supported`)}function A(e){if(!Number.isSafeInteger(e)||e<0||e>=31)throw RangeError(`Context-specific tag number must be between 0 and 30`)}function j(e,t){if(Number.isNaN(e.getTime()))throw RangeError(`${t} requires a valid Date`)}function M(e,t,n){return t===e.start&&n?.allowOpaqueConstructedTags?.includes(e.tag)===!0}export{T as assertDerMaxDepth,p as bitString,u as bool,t as concatBytes,e as encodeLength,a as explicitContext,b as generalizedTime,g as ia5String,o as implicitConstructedContext,s as implicitPrimitiveContext,c as integer,l as integerFromNumber,d as nullValue,v as objectIdentifier,f as octetString,h as printableString,w as readElement,E as readRootElement,D as readSequenceChildren,r as sequence,i as setOf,x as time,n as tlv,y as utcTime,m as utf8String};
2
- //# sourceMappingURL=der.js.map
1
+ /**
2
+ * Produces the DER length octets for a given byte count.
3
+ * Values < 128 use the short form (one octet); larger values use the
4
+ * long form (leading octet encodes the number of subsequent length bytes).
5
+ */
6
+ function encodeLength(length) {
7
+ assertNonNegativeSafeInteger(length, "DER length");
8
+ if (length < 128) return Uint8Array.of(length);
9
+ const parts = encodeBase256(length);
10
+ return Uint8Array.of(128 | parts.length, ...parts);
11
+ }
12
+ /** Concatenates multiple byte arrays into a single `Uint8Array`. */
13
+ function concatBytes(parts) {
14
+ const length = parts.reduce((sum, part) => sum + part.length, 0);
15
+ const out = new Uint8Array(length);
16
+ let offset = 0;
17
+ for (const part of parts) {
18
+ out.set(part, offset);
19
+ offset += part.length;
20
+ }
21
+ return out;
22
+ }
23
+ /**
24
+ * Builds a complete DER TLV (tag-length-value) element: one tag octet,
25
+ * the DER-encoded length, then the raw value bytes.
26
+ */
27
+ function tlv(tag, value) {
28
+ assertSingleOctetDerTag(tag);
29
+ return concatBytes([
30
+ Uint8Array.of(tag),
31
+ encodeLength(value.length),
32
+ value
33
+ ]);
34
+ }
35
+ /** Wraps concatenated children in a SEQUENCE (tag `0x30`). */
36
+ function sequence(parts) {
37
+ return tlv(48, concatBytes(parts));
38
+ }
39
+ /**
40
+ * Wraps children in a SET (tag `0x31`) after DER-sorting them
41
+ * lexicographically by encoded bytes, as required by X.690 DER.
42
+ */
43
+ function setOf(parts) {
44
+ return tlv(49, concatBytes(parts.slice().sort((a, b) => {
45
+ const len = Math.min(a.length, b.length);
46
+ for (let i = 0; i < len; i++) {
47
+ const diff = (a[i] ?? 0) - (b[i] ?? 0);
48
+ if (diff !== 0) return diff;
49
+ }
50
+ return a.length - b.length;
51
+ })));
52
+ }
53
+ /**
54
+ * Wraps a value in an explicit context-specific constructed tag (`0xa0 + tag`).
55
+ * Used for optional SEQUENCE fields tagged with `[tag] EXPLICIT`.
56
+ */
57
+ function explicitContext(tag, value) {
58
+ assertContextSpecificTagNumber(tag);
59
+ return tlv(160 + tag, value);
60
+ }
61
+ /**
62
+ * Wraps a value in an implicit context-specific constructed tag (`0xa0 + tag`).
63
+ * Used for `[tag] IMPLICIT` fields whose underlying type is constructed (e.g. SEQUENCE).
64
+ */
65
+ function implicitConstructedContext(tag, value) {
66
+ assertContextSpecificTagNumber(tag);
67
+ return tlv(160 + tag, value);
68
+ }
69
+ /**
70
+ * Wraps a value in an implicit context-specific primitive tag (`0x80 + tag`).
71
+ * Used for `[tag] IMPLICIT` fields whose underlying type is primitive (e.g. OCTET STRING).
72
+ */
73
+ function implicitPrimitiveContext(tag, value) {
74
+ assertContextSpecificTagNumber(tag);
75
+ return tlv(128 + tag, value);
76
+ }
77
+ /**
78
+ * Encodes raw big-endian bytes as a DER INTEGER (tag `0x02`).
79
+ * Strips leading zero bytes for minimal encoding and prepends a zero
80
+ * byte when the high bit is set to keep the value non-negative.
81
+ */
82
+ function integer(bytes) {
83
+ if (bytes.length === 0) return tlv(2, Uint8Array.of(0));
84
+ let start = 0;
85
+ while (start < bytes.length - 1 && bytes[start] === 0) start += 1;
86
+ const value = bytes.slice(start);
87
+ if ((value[0] ?? 0) >= 128) return tlv(2, concatBytes([Uint8Array.of(0), value]));
88
+ return tlv(2, value);
89
+ }
90
+ /**
91
+ * Encodes a non-negative JavaScript `number` as a DER INTEGER.
92
+ * Throws if the value is not a non-negative safe integer.
93
+ */
94
+ function integerFromNumber(value) {
95
+ if (!Number.isSafeInteger(value) || value < 0) throw new Error("INTEGER must be a non-negative safe integer");
96
+ if (value === 0) return integer(Uint8Array.of(0));
97
+ return integer(Uint8Array.from(encodeBase256(value)));
98
+ }
99
+ /** Encodes a DER BOOLEAN (tag `0x01`): `true` → `0xff`, `false` → `0x00`. */
100
+ function bool(value) {
101
+ return tlv(1, Uint8Array.of(value ? 255 : 0));
102
+ }
103
+ /** Produces a DER NULL element (tag `0x05`, zero-length value). */
104
+ function nullValue() {
105
+ return tlv(5, /* @__PURE__ */ new Uint8Array());
106
+ }
107
+ /** Wraps raw bytes in an OCTET STRING element (tag `0x04`). */
108
+ function octetString(value) {
109
+ return tlv(4, value);
110
+ }
111
+ /**
112
+ * Encodes a DER BIT STRING (tag `0x03`). The value is prefixed with a
113
+ * single octet indicating how many trailing bits in the last byte are unused.
114
+ *
115
+ * @param unusedBits Number of unused trailing bits (0–7). Defaults to 0.
116
+ */
117
+ function bitString(value, unusedBits = 0) {
118
+ if (unusedBits < 0 || unusedBits > 7) throw new Error("unusedBits must be between 0 and 7");
119
+ if (value.length === 0 && unusedBits !== 0) throw new Error("unusedBits must be 0 when value is empty");
120
+ if (value.length > 0 && unusedBits > 0) {
121
+ if (((value[value.length - 1] ?? 0) & (1 << unusedBits) - 1) !== 0) throw new Error("unused bits in the last byte must be zero");
122
+ }
123
+ return tlv(3, concatBytes([Uint8Array.of(unusedBits), value]));
124
+ }
125
+ /** Encodes a DER UTF8String (tag `0x0c`). */
126
+ function utf8String(value) {
127
+ return tlv(12, new TextEncoder().encode(value));
128
+ }
129
+ /**
130
+ * Encodes a DER PrintableString (tag `0x13`).
131
+ * Throws if the input contains characters outside the X.520 PrintableString set.
132
+ */
133
+ function printableString(value) {
134
+ if (!/^[A-Za-z0-9 '()+,\-./:=?]*$/.test(value)) throw new Error("Invalid PrintableString: contains characters outside the allowed set");
135
+ return tlv(19, new TextEncoder().encode(value));
136
+ }
137
+ /**
138
+ * Encodes a DER IA5String (tag `0x16`).
139
+ * Throws if the input contains any non-ASCII character (code point > 0x7f).
140
+ */
141
+ function ia5String(value) {
142
+ for (let i = 0; i < value.length; i++) if (value.charCodeAt(i) > 127) throw new Error("Invalid IA5String: contains non-ASCII characters");
143
+ return tlv(22, new TextEncoder().encode(value));
144
+ }
145
+ /** Encode a non-negative integer as a base-128 sub-identifier (X.690 §8.19.2). */
146
+ function encodeBase128(value) {
147
+ const encoded = [Number(value & 127n)];
148
+ let current = value >> 7n;
149
+ while (current > 0n) {
150
+ encoded.unshift(128 | Number(current & 127n));
151
+ current >>= 7n;
152
+ }
153
+ return encoded;
154
+ }
155
+ /**
156
+ * Encodes a dotted-decimal OID string as a DER OBJECT IDENTIFIER (tag `0x06`).
157
+ * Validates arc constraints per X.660: first arc must be 0–2, second < 40
158
+ * for arcs 0 and 1. Sub-identifiers are encoded with base-128 continuation.
159
+ */
160
+ function objectIdentifier(oid) {
161
+ const digitPattern = /^\d+$/;
162
+ const segments = oid.split(".").map((segment) => {
163
+ if (!digitPattern.test(segment)) throw new Error(`Invalid OID segment: ${segment}`);
164
+ return BigInt(segment);
165
+ });
166
+ if (segments.length < 2) throw new Error(`Invalid OID: ${oid}`);
167
+ const [first, second, ...rest] = segments;
168
+ if (first === void 0 || second === void 0) throw new Error(`Invalid OID: ${oid}`);
169
+ if (first !== 0n && first !== 1n && first !== 2n) throw new Error(`Invalid OID first arc: ${first}`);
170
+ if ((first === 0n || first === 1n) && second >= 40n) throw new Error(`Invalid OID second arc: ${second} (must be < 40 when first arc is ${first})`);
171
+ const bytes = encodeBase128(first * 40n + second);
172
+ for (const segment of rest) bytes.push(...encodeBase128(segment));
173
+ return tlv(6, Uint8Array.from(bytes));
174
+ }
175
+ /**
176
+ * Encodes a `Date` as a DER UTCTime (tag `0x17`), format `YYMMDDHHMMSSZ`.
177
+ * Only the two-digit year is stored; suitable for dates in 1950–2049.
178
+ */
179
+ function utcTime(date) {
180
+ assertValidDate(date, "UTCTime");
181
+ const year = date.getUTCFullYear();
182
+ if (year < 1950 || year > 2049) throw new RangeError("UTCTime year must be between 1950 and 2049");
183
+ const value = `${[
184
+ twoDigits(year % 100),
185
+ twoDigits(date.getUTCMonth() + 1),
186
+ twoDigits(date.getUTCDate()),
187
+ twoDigits(date.getUTCHours()),
188
+ twoDigits(date.getUTCMinutes()),
189
+ twoDigits(date.getUTCSeconds())
190
+ ].join("")}Z`;
191
+ return tlv(23, new TextEncoder().encode(value));
192
+ }
193
+ /**
194
+ * Encodes a `Date` as a DER GeneralizedTime (tag `0x18`), format `YYYYMMDDHHMMSSZ`.
195
+ * Uses a four-digit year; required for dates outside the 1950–2049 range.
196
+ */
197
+ function generalizedTime(date) {
198
+ assertValidDate(date, "GeneralizedTime");
199
+ const year = date.getUTCFullYear();
200
+ if (year < 0 || year > 9999) throw new RangeError("GeneralizedTime year must be between 0 and 9999");
201
+ const value = `${[
202
+ String(year).padStart(4, "0"),
203
+ twoDigits(date.getUTCMonth() + 1),
204
+ twoDigits(date.getUTCDate()),
205
+ twoDigits(date.getUTCHours()),
206
+ twoDigits(date.getUTCMinutes()),
207
+ twoDigits(date.getUTCSeconds())
208
+ ].join("")}Z`;
209
+ return tlv(24, new TextEncoder().encode(value));
210
+ }
211
+ /**
212
+ * Encodes a `Date` as the appropriate DER time type per RFC 5280:
213
+ * {@linkcode utcTime} for 1950–2049, {@linkcode generalizedTime} otherwise.
214
+ */
215
+ function time(date) {
216
+ if (date.getUTCFullYear() >= 2050 || date.getUTCFullYear() < 1950) return generalizedTime(date);
217
+ return utcTime(date);
218
+ }
219
+ /** Zero-pads a number to two digits for time encoding. */
220
+ function twoDigits(value) {
221
+ return String(value).padStart(2, "0");
222
+ }
223
+ /** Encodes a non-negative integer as big-endian base-256 octets. */
224
+ function encodeBase256(value) {
225
+ assertNonNegativeSafeInteger(value, "DER integer");
226
+ const parts = [];
227
+ let current = value;
228
+ while (current > 0) {
229
+ parts.unshift(current & 255);
230
+ current = Math.floor(current / 256);
231
+ }
232
+ return parts;
233
+ }
234
+ /**
235
+ * Reads one TLV element from `bytes` starting at `offset`.
236
+ * Parses the tag byte, decodes the DER length octets, and slices out the
237
+ * value bytes. Throws on truncated input, indefinite lengths, and
238
+ * non-minimal length encodings.
239
+ *
240
+ * @param offset Byte position of the tag octet. Defaults to 0.
241
+ */
242
+ function readElement(bytes, offset = 0) {
243
+ const tag = bytes[offset];
244
+ if (tag === void 0) throw new Error("Unexpected end of DER input");
245
+ if ((tag & 31) === 31) throw new Error("High-tag-number DER form is not supported");
246
+ const lengthByte = bytes[offset + 1];
247
+ if (lengthByte === void 0) throw new Error("Unexpected end of DER input");
248
+ let headerLength = 2;
249
+ let length = 0;
250
+ if ((lengthByte & 128) === 0) length = lengthByte;
251
+ else {
252
+ const octets = lengthByte & 127;
253
+ if (octets === 0) throw new Error("Indefinite lengths are not supported");
254
+ const firstLengthOctet = bytes[offset + 2];
255
+ if (firstLengthOctet === void 0) throw new Error("Unexpected end of DER input");
256
+ if (firstLengthOctet === 0) throw new Error("Non-minimal DER length encoding");
257
+ headerLength += octets;
258
+ for (let index = 0; index < octets; index += 1) {
259
+ const next = bytes[offset + 2 + index];
260
+ if (next === void 0) throw new Error("Unexpected end of DER input");
261
+ if (length > Math.floor((Number.MAX_SAFE_INTEGER - next) / 256)) throw new Error("DER length exceeds safe integer range");
262
+ length = length * 256 + next;
263
+ }
264
+ if (length < 128) throw new Error("Non-minimal DER length encoding");
265
+ }
266
+ const start = offset + headerLength;
267
+ const end = start + length;
268
+ if (end > bytes.length) throw new Error("DER element exceeds input length");
269
+ return {
270
+ tag,
271
+ headerLength,
272
+ length,
273
+ start,
274
+ end,
275
+ value: bytes.slice(start, end)
276
+ };
277
+ }
278
+ /**
279
+ * Walks the full DER tree rooted in `bytes` and throws if nesting exceeds
280
+ * `maxDepth`. Constructed tags with content that cannot be parsed as valid
281
+ * children are tolerated when listed in `allowOpaqueConstructedTags`.
282
+ */
283
+ function assertDerMaxDepth(bytes, maxDepth = 64, options) {
284
+ if (!Number.isSafeInteger(maxDepth) || maxDepth < 1) throw new Error("DER max depth must be a positive safe integer");
285
+ const root = readElement(bytes, 0);
286
+ if (root.end !== bytes.length) throw new Error("Trailing data after DER element");
287
+ const stack = [{
288
+ element: root,
289
+ depth: 1
290
+ }];
291
+ while (stack.length > 0) {
292
+ const current = stack.pop();
293
+ if (current === void 0) continue;
294
+ if (current.depth > maxDepth) throw new Error(`DER exceeds max depth of ${maxDepth}`);
295
+ if ((current.element.tag & 32) === 0) continue;
296
+ let offset = current.element.start;
297
+ let treatedAsOpaqueLeaf = false;
298
+ while (offset < current.element.end) {
299
+ let child;
300
+ try {
301
+ child = readElement(bytes, offset);
302
+ } catch (error) {
303
+ if (canTreatAsOpaqueLeaf(current.element, offset, options)) {
304
+ treatedAsOpaqueLeaf = true;
305
+ offset = current.element.end;
306
+ break;
307
+ }
308
+ throw error;
309
+ }
310
+ if (child.end > current.element.end) {
311
+ if (canTreatAsOpaqueLeaf(current.element, offset, options)) {
312
+ treatedAsOpaqueLeaf = true;
313
+ offset = current.element.end;
314
+ break;
315
+ }
316
+ throw new Error("DER child exceeds parent length");
317
+ }
318
+ stack.push({
319
+ element: child,
320
+ depth: current.depth + 1
321
+ });
322
+ offset = child.end;
323
+ }
324
+ if (!treatedAsOpaqueLeaf && offset !== current.element.end) throw new Error("Malformed DER container");
325
+ }
326
+ }
327
+ /**
328
+ * Reads the single top-level TLV element from `bytes`, optionally
329
+ * validating nesting depth. Throws if there is trailing data after the element.
330
+ */
331
+ function readRootElement(bytes, options) {
332
+ assertDerMaxDepth(bytes, options?.maxDepth ?? 64, options);
333
+ const element = readElement(bytes, 0);
334
+ if (element.end !== bytes.length) throw new Error("Trailing data after DER element");
335
+ return element;
336
+ }
337
+ /**
338
+ * Reads a DER-encoded SEQUENCE from `bytes` and returns its direct children.
339
+ * Throws if the root element is not a SEQUENCE or if child boundaries are inconsistent.
340
+ */
341
+ function readSequenceChildren(bytes, options) {
342
+ const sequenceElement = readRootElement(bytes, options);
343
+ if (sequenceElement.tag !== 48) throw new Error("Expected SEQUENCE");
344
+ const children = [];
345
+ let offset = sequenceElement.start;
346
+ while (offset < sequenceElement.end) {
347
+ const element = readElement(bytes, offset);
348
+ if (element.end > sequenceElement.end) throw new Error("DER child exceeds parent length");
349
+ children.push(element);
350
+ offset = element.end;
351
+ }
352
+ if (offset !== sequenceElement.end) throw new Error("Malformed DER sequence");
353
+ return children;
354
+ }
355
+ /** Throws if `value` is not a non-negative safe integer. */
356
+ function assertNonNegativeSafeInteger(value, label) {
357
+ if (!Number.isSafeInteger(value) || value < 0) throw new Error(`${label} must be a non-negative safe integer`);
358
+ }
359
+ function assertSingleOctetDerTag(tag) {
360
+ if (!Number.isSafeInteger(tag) || tag < 0 || tag > 255) throw new RangeError("DER tag octet must be between 0 and 255");
361
+ if ((tag & 31) === 31) throw new Error("High-tag-number DER tags are not supported");
362
+ }
363
+ function assertContextSpecificTagNumber(tag) {
364
+ if (!Number.isSafeInteger(tag) || tag < 0 || tag >= 31) throw new RangeError("Context-specific tag number must be between 0 and 30");
365
+ }
366
+ function assertValidDate(date, label) {
367
+ if (Number.isNaN(date.getTime())) throw new RangeError(`${label} requires a valid Date`);
368
+ }
369
+ /**
370
+ * Returns `true` when a constructed element's content should be treated as
371
+ * an opaque leaf (not recursed into) because its tag appears in the
372
+ * `allowOpaqueConstructedTags` list and the offset is at the element start.
373
+ */
374
+ function canTreatAsOpaqueLeaf(element, offset, options) {
375
+ return offset === element.start && options?.allowOpaqueConstructedTags?.includes(element.tag) === true;
376
+ }
377
+ //#endregion
378
+ export { assertDerMaxDepth, bitString, bool, concatBytes, encodeLength, explicitContext, generalizedTime, ia5String, implicitConstructedContext, implicitPrimitiveContext, integer, integerFromNumber, nullValue, objectIdentifier, octetString, printableString, readElement, readRootElement, readSequenceChildren, sequence, setOf, time, tlv, utcTime, utf8String };
@@ -1,2 +1,103 @@
1
- const e={commonName:`2.5.4.3`,surname:`2.5.4.4`,serialNumber:`2.5.4.5`,countryName:`2.5.4.6`,localityName:`2.5.4.7`,stateOrProvinceName:`2.5.4.8`,streetAddress:`2.5.4.9`,organizationName:`2.5.4.10`,organizationalUnitName:`2.5.4.11`,title:`2.5.4.12`,givenName:`2.5.4.42`,emailAddress:`1.2.840.113549.1.9.1`,cmsContentType:`1.2.840.113549.1.9.3`,cmsMessageDigest:`1.2.840.113549.1.9.4`,extensionRequest:`1.2.840.113549.1.9.14`,pkcs7Data:`1.2.840.113549.1.7.1`,pkcs7SignedData:`1.2.840.113549.1.7.2`,pkcs7EncryptedData:`1.2.840.113549.1.7.6`,pkcs12KeyBag:`1.2.840.113549.1.12.10.1.1`,pkcs12CertBag:`1.2.840.113549.1.12.10.1.3`,x509CertificateBagType:`1.2.840.113549.1.9.22.1`,friendlyName:`1.2.840.113549.1.9.20`,localKeyId:`1.2.840.113549.1.9.21`,pbes2:`1.2.840.113549.1.5.13`,pbkdf2:`1.2.840.113549.1.5.12`,mgf1:`1.2.840.113549.1.1.8`,hmacWithSHA1:`1.2.840.113549.2.7`,hmacWithSHA256:`1.2.840.113549.2.9`,aes128Cbc:`2.16.840.1.101.3.4.1.2`,aes192Cbc:`2.16.840.1.101.3.4.1.22`,aes256Cbc:`2.16.840.1.101.3.4.1.42`,rsaEncryption:`1.2.840.113549.1.1.1`,sha1WithRSAEncryption:`1.2.840.113549.1.1.5`,rsassaPss:`1.2.840.113549.1.1.10`,sha256WithRSAEncryption:`1.2.840.113549.1.1.11`,sha384WithRSAEncryption:`1.2.840.113549.1.1.12`,sha512WithRSAEncryption:`1.2.840.113549.1.1.13`,ecPublicKey:`1.2.840.10045.2.1`,ecdsaWithSHA1:`1.2.840.10045.4.1`,ecdsaWithSHA224:`1.2.840.10045.4.3.1`,ecdsaWithSHA256:`1.2.840.10045.4.3.2`,ecdsaWithSHA384:`1.2.840.10045.4.3.3`,ecdsaWithSHA512:`1.2.840.10045.4.3.4`,prime256v1:`1.2.840.10045.3.1.7`,secp384r1:`1.3.132.0.34`,secp521r1:`1.3.132.0.35`,ed25519:`1.3.101.112`,subjectKeyIdentifier:`2.5.29.14`,keyUsage:`2.5.29.15`,subjectAltName:`2.5.29.17`,nameConstraints:`2.5.29.30`,certificatePolicies:`2.5.29.32`,policyMappings:`2.5.29.33`,cRLNumber:`2.5.29.20`,cRLReason:`2.5.29.21`,invalidityDate:`2.5.29.24`,certificateIssuer:`2.5.29.29`,issuingDistributionPoint:`2.5.29.28`,deltaCRLIndicator:`2.5.29.27`,basicConstraints:`2.5.29.19`,policyConstraints:`2.5.29.36`,cRLDistributionPoints:`2.5.29.31`,freshestCRL:`2.5.29.46`,inhibitAnyPolicy:`2.5.29.54`,extendedKeyUsage:`2.5.29.37`,authorityKeyIdentifier:`2.5.29.35`,anyPolicy:`2.5.29.32.0`,authorityInfoAccess:`1.3.6.1.5.5.7.1.1`,idOnDnsSrv:`1.3.6.1.5.5.7.8.7`,cpsPolicyQualifier:`1.3.6.1.5.5.7.2.1`,userNoticePolicyQualifier:`1.3.6.1.5.5.7.2.2`,ocspAccessMethod:`1.3.6.1.5.5.7.48.1`,caIssuersAccessMethod:`1.3.6.1.5.5.7.48.2`,ocspBasicResponse:`1.3.6.1.5.5.7.48.1.1`,ocspNonce:`1.3.6.1.5.5.7.48.1.2`,ocspNoCheck:`1.3.6.1.5.5.7.48.1.5`,sha1:`1.3.14.3.2.26`,sha256:`2.16.840.1.101.3.4.2.1`,sha384:`2.16.840.1.101.3.4.2.2`,sha512:`2.16.840.1.101.3.4.2.3`,serverAuth:`1.3.6.1.5.5.7.3.1`,clientAuth:`1.3.6.1.5.5.7.3.2`,codeSigning:`1.3.6.1.5.5.7.3.3`,emailProtection:`1.3.6.1.5.5.7.3.4`,timeStamping:`1.3.6.1.5.5.7.3.8`,ocspSigning:`1.3.6.1.5.5.7.3.9`};export{e as OIDS};
2
- //# sourceMappingURL=oids.js.map
1
+ //#region src/internal/asn1/oids.ts
2
+ /**
3
+ * Centralized ASN.1 object identifier registry referenced by create, parse,
4
+ * verify, and container modules.
5
+ *
6
+ * All OIDs are dotted-decimal string constants keyed by their common short name.
7
+ *
8
+ * @module
9
+ */
10
+ /**
11
+ * Dotted-decimal OID constants for X.500 attributes, PKCS algorithms,
12
+ * X.509v3 extensions, EKU purposes, hash algorithms, and PKIX access methods.
13
+ */
14
+ const OIDS = {
15
+ commonName: "2.5.4.3",
16
+ surname: "2.5.4.4",
17
+ serialNumber: "2.5.4.5",
18
+ countryName: "2.5.4.6",
19
+ localityName: "2.5.4.7",
20
+ stateOrProvinceName: "2.5.4.8",
21
+ streetAddress: "2.5.4.9",
22
+ organizationName: "2.5.4.10",
23
+ organizationalUnitName: "2.5.4.11",
24
+ title: "2.5.4.12",
25
+ givenName: "2.5.4.42",
26
+ emailAddress: "1.2.840.113549.1.9.1",
27
+ cmsContentType: "1.2.840.113549.1.9.3",
28
+ cmsMessageDigest: "1.2.840.113549.1.9.4",
29
+ extensionRequest: "1.2.840.113549.1.9.14",
30
+ pkcs7Data: "1.2.840.113549.1.7.1",
31
+ pkcs7SignedData: "1.2.840.113549.1.7.2",
32
+ pkcs7EncryptedData: "1.2.840.113549.1.7.6",
33
+ pkcs12KeyBag: "1.2.840.113549.1.12.10.1.1",
34
+ pkcs12CertBag: "1.2.840.113549.1.12.10.1.3",
35
+ x509CertificateBagType: "1.2.840.113549.1.9.22.1",
36
+ friendlyName: "1.2.840.113549.1.9.20",
37
+ localKeyId: "1.2.840.113549.1.9.21",
38
+ pbes2: "1.2.840.113549.1.5.13",
39
+ pbkdf2: "1.2.840.113549.1.5.12",
40
+ mgf1: "1.2.840.113549.1.1.8",
41
+ hmacWithSHA1: "1.2.840.113549.2.7",
42
+ hmacWithSHA256: "1.2.840.113549.2.9",
43
+ aes128Cbc: "2.16.840.1.101.3.4.1.2",
44
+ aes192Cbc: "2.16.840.1.101.3.4.1.22",
45
+ aes256Cbc: "2.16.840.1.101.3.4.1.42",
46
+ rsaEncryption: "1.2.840.113549.1.1.1",
47
+ sha1WithRSAEncryption: "1.2.840.113549.1.1.5",
48
+ rsassaPss: "1.2.840.113549.1.1.10",
49
+ sha256WithRSAEncryption: "1.2.840.113549.1.1.11",
50
+ sha384WithRSAEncryption: "1.2.840.113549.1.1.12",
51
+ sha512WithRSAEncryption: "1.2.840.113549.1.1.13",
52
+ ecPublicKey: "1.2.840.10045.2.1",
53
+ ecdsaWithSHA1: "1.2.840.10045.4.1",
54
+ ecdsaWithSHA224: "1.2.840.10045.4.3.1",
55
+ ecdsaWithSHA256: "1.2.840.10045.4.3.2",
56
+ ecdsaWithSHA384: "1.2.840.10045.4.3.3",
57
+ ecdsaWithSHA512: "1.2.840.10045.4.3.4",
58
+ prime256v1: "1.2.840.10045.3.1.7",
59
+ secp384r1: "1.3.132.0.34",
60
+ secp521r1: "1.3.132.0.35",
61
+ ed25519: "1.3.101.112",
62
+ subjectKeyIdentifier: "2.5.29.14",
63
+ keyUsage: "2.5.29.15",
64
+ subjectAltName: "2.5.29.17",
65
+ nameConstraints: "2.5.29.30",
66
+ certificatePolicies: "2.5.29.32",
67
+ policyMappings: "2.5.29.33",
68
+ cRLNumber: "2.5.29.20",
69
+ cRLReason: "2.5.29.21",
70
+ invalidityDate: "2.5.29.24",
71
+ certificateIssuer: "2.5.29.29",
72
+ issuingDistributionPoint: "2.5.29.28",
73
+ deltaCRLIndicator: "2.5.29.27",
74
+ basicConstraints: "2.5.29.19",
75
+ policyConstraints: "2.5.29.36",
76
+ cRLDistributionPoints: "2.5.29.31",
77
+ freshestCRL: "2.5.29.46",
78
+ inhibitAnyPolicy: "2.5.29.54",
79
+ extendedKeyUsage: "2.5.29.37",
80
+ authorityKeyIdentifier: "2.5.29.35",
81
+ anyPolicy: "2.5.29.32.0",
82
+ authorityInfoAccess: "1.3.6.1.5.5.7.1.1",
83
+ idOnDnsSrv: "1.3.6.1.5.5.7.8.7",
84
+ cpsPolicyQualifier: "1.3.6.1.5.5.7.2.1",
85
+ userNoticePolicyQualifier: "1.3.6.1.5.5.7.2.2",
86
+ ocspAccessMethod: "1.3.6.1.5.5.7.48.1",
87
+ caIssuersAccessMethod: "1.3.6.1.5.5.7.48.2",
88
+ ocspBasicResponse: "1.3.6.1.5.5.7.48.1.1",
89
+ ocspNonce: "1.3.6.1.5.5.7.48.1.2",
90
+ ocspNoCheck: "1.3.6.1.5.5.7.48.1.5",
91
+ sha1: "1.3.14.3.2.26",
92
+ sha256: "2.16.840.1.101.3.4.2.1",
93
+ sha384: "2.16.840.1.101.3.4.2.2",
94
+ sha512: "2.16.840.1.101.3.4.2.3",
95
+ serverAuth: "1.3.6.1.5.5.7.3.1",
96
+ clientAuth: "1.3.6.1.5.5.7.3.2",
97
+ codeSigning: "1.3.6.1.5.5.7.3.3",
98
+ emailProtection: "1.3.6.1.5.5.7.3.4",
99
+ timeStamping: "1.3.6.1.5.5.7.3.8",
100
+ ocspSigning: "1.3.6.1.5.5.7.3.9"
101
+ };
102
+ //#endregion
103
+ export { OIDS };
@@ -1,2 +1,58 @@
1
- import{OIDS as e}from"../asn1/oids.js";import{parseRsaPssParameters as t}from"./rsa-pss.js";function n(t){switch(t){case e.sha1:return`SHA-1`;case e.sha256:return`SHA-256`;case e.sha384:return`SHA-384`;case e.sha512:return`SHA-512`;default:return`Unknown (${t})`}}function r(n,r){switch(n){case e.sha1WithRSAEncryption:return`RSA PKCS#1 v1.5 with SHA-1`;case e.sha256WithRSAEncryption:return`RSA PKCS#1 v1.5 with SHA-256`;case e.sha384WithRSAEncryption:return`RSA PKCS#1 v1.5 with SHA-384`;case e.sha512WithRSAEncryption:return`RSA PKCS#1 v1.5 with SHA-512`;case e.rsassaPss:{let e=t(r);return e.ok?`RSA-PSS with ${e.value.hash}`:`RSA-PSS`}case e.ecdsaWithSHA1:return`ECDSA with SHA-1`;case e.ecdsaWithSHA224:return`ECDSA with SHA-224`;case e.ecdsaWithSHA256:return`ECDSA with SHA-256`;case e.ecdsaWithSHA384:return`ECDSA with SHA-384`;case e.ecdsaWithSHA512:return`ECDSA with SHA-512`;case e.ed25519:return`Ed25519`;default:return`Unknown (${n})`}}function i(t,n){switch(t){case e.rsaEncryption:return`RSA`;case e.rsassaPss:return`RSA-PSS`;case e.ecPublicKey:switch(n){case e.prime256v1:return`EC P-256`;case e.secp384r1:return`EC P-384`;case e.secp521r1:return`EC P-521`;default:return`EC`}case e.ed25519:return`Ed25519`;default:return`Unknown (${t})`}}export{n as describeHashAlgorithm,i as describePublicKeyAlgorithm,r as describeSignatureAlgorithm};
2
- //# sourceMappingURL=algorithm-names.js.map
1
+ import { OIDS } from "../asn1/oids.js";
2
+ import { parseRsaPssParameters } from "./rsa-pss.js";
3
+ //#region src/internal/crypto/algorithm-names.ts
4
+ /**
5
+ * Human-readable algorithm labels for parsed OIDs.
6
+ *
7
+ * Shared by certificate, revocation, and PKCS parse surfaces so callers can
8
+ * display meaningful algorithm names without memorizing dotted-decimal OIDs.
9
+ *
10
+ * @module
11
+ */
12
+ /** Convert a hash or message-digest OID into a human-readable name. */
13
+ function describeHashAlgorithm(oid) {
14
+ switch (oid) {
15
+ case OIDS.sha1: return "SHA-1";
16
+ case OIDS.sha256: return "SHA-256";
17
+ case OIDS.sha384: return "SHA-384";
18
+ case OIDS.sha512: return "SHA-512";
19
+ default: return `Unknown (${oid})`;
20
+ }
21
+ }
22
+ /** Convert a signature AlgorithmIdentifier into a human-readable name. */
23
+ function describeSignatureAlgorithm(oid, parametersDer) {
24
+ switch (oid) {
25
+ case OIDS.sha1WithRSAEncryption: return "RSA PKCS#1 v1.5 with SHA-1";
26
+ case OIDS.sha256WithRSAEncryption: return "RSA PKCS#1 v1.5 with SHA-256";
27
+ case OIDS.sha384WithRSAEncryption: return "RSA PKCS#1 v1.5 with SHA-384";
28
+ case OIDS.sha512WithRSAEncryption: return "RSA PKCS#1 v1.5 with SHA-512";
29
+ case OIDS.rsassaPss: {
30
+ const parsed = parseRsaPssParameters(parametersDer);
31
+ return parsed.ok ? `RSA-PSS with ${parsed.value.hash}` : "RSA-PSS";
32
+ }
33
+ case OIDS.ecdsaWithSHA1: return "ECDSA with SHA-1";
34
+ case OIDS.ecdsaWithSHA224: return "ECDSA with SHA-224";
35
+ case OIDS.ecdsaWithSHA256: return "ECDSA with SHA-256";
36
+ case OIDS.ecdsaWithSHA384: return "ECDSA with SHA-384";
37
+ case OIDS.ecdsaWithSHA512: return "ECDSA with SHA-512";
38
+ case OIDS.ed25519: return "Ed25519";
39
+ default: return `Unknown (${oid})`;
40
+ }
41
+ }
42
+ /** Convert a SubjectPublicKeyInfo algorithm identifier into a human-readable name. */
43
+ function describePublicKeyAlgorithm(oid, parametersOid) {
44
+ switch (oid) {
45
+ case OIDS.rsaEncryption: return "RSA";
46
+ case OIDS.rsassaPss: return "RSA-PSS";
47
+ case OIDS.ecPublicKey: switch (parametersOid) {
48
+ case OIDS.prime256v1: return "EC P-256";
49
+ case OIDS.secp384r1: return "EC P-384";
50
+ case OIDS.secp521r1: return "EC P-521";
51
+ default: return "EC";
52
+ }
53
+ case OIDS.ed25519: return "Ed25519";
54
+ default: return `Unknown (${oid})`;
55
+ }
56
+ }
57
+ //#endregion
58
+ export { describeHashAlgorithm, describePublicKeyAlgorithm, describeSignatureAlgorithm };
@@ -1,2 +1,68 @@
1
- import{integer as e,readSequenceChildren as t,sequence as n}from"../asn1/der.js";function r(e,n){let r=t(e),i=r[0],a=r[1];if(i===void 0||a===void 0||r.length!==2)throw Error(`Malformed ECDSA DER signature`);if(i.tag!==2||a.tag!==2)throw Error(`ECDSA DER signature must contain two INTEGERs`);return s(c(i.value),c(a.value),n)}function i(t,r){if(t.length!==r*2)throw Error(`Unexpected ECDSA raw signature length`);return n([e(t.slice(0,r)),e(t.slice(r))])}function a(e,t){return e.length===t?i(e,t/2):e}function o(e,t){if(e[0]===48)try{return r(e,t)}catch{}try{return i(e,t)}catch{return}}function s(e,t,n){if(e.length>n||t.length>n)throw Error(`ECDSA signature integer too large`);let r=new Uint8Array(n*2);return r.set(e,n-e.length),r.set(t,r.length-t.length),r}function c(e){let t=0;for(;t<e.length-1&&e[t]===0;)t+=1;return e.slice(t)}export{o as alternateEcdsaSignatureEncoding,s as concatFixedWidth,r as derEcdsaSignatureToRaw,a as ecdsaSignatureToDer,i as rawEcdsaSignatureToDer};
2
- //# sourceMappingURL=ecdsa.js.map
1
+ import { integer, readSequenceChildren, sequence } from "../asn1/der.js";
2
+ //#region src/internal/crypto/ecdsa.ts
3
+ /**
4
+ * ECDSA signature encoding conversion between ASN.1 DER (r, s INTEGER) and the
5
+ * fixed-width raw format used by WebCrypto.
6
+ *
7
+ * @module
8
+ */
9
+ /** Convert a DER-encoded ECDSA signature (SEQUENCE of two INTEGERs) to fixed-width raw `r || s`. */
10
+ function derEcdsaSignatureToRaw(signature, partLength) {
11
+ const parts = readSequenceChildren(signature);
12
+ const r = parts[0];
13
+ const s = parts[1];
14
+ if (r === void 0 || s === void 0 || parts.length !== 2) throw new Error("Malformed ECDSA DER signature");
15
+ if (r.tag !== 2 || s.tag !== 2) throw new Error("ECDSA DER signature must contain two INTEGERs");
16
+ return concatFixedWidth(trimLeadingZero(r.value), trimLeadingZero(s.value), partLength);
17
+ }
18
+ /** Convert a fixed-width raw `r || s` ECDSA signature to DER-encoded SEQUENCE of two INTEGERs. */
19
+ function rawEcdsaSignatureToDer(signature, partLength) {
20
+ if (signature.length !== partLength * 2) throw new Error("Unexpected ECDSA raw signature length");
21
+ return sequence([integer(signature.slice(0, partLength)), integer(signature.slice(partLength))]);
22
+ }
23
+ /**
24
+ * Normalize a WebCrypto ECDSA signature to DER.
25
+ *
26
+ * Spec-compliant WebCrypto always returns fixed-width raw `r || s`
27
+ * (exactly `rawSignatureBytes` long), which needs conversion. Detection is
28
+ * by length, never by sniffing the first byte: a raw signature whose `r`
29
+ * happens to start with `0x30` (1-in-256 per signature) is
30
+ * indistinguishable from DER by tag, and embedding it unconverted produces
31
+ * a certificate/CRL that OpenSSL rejects with a signature failure.
32
+ */
33
+ function ecdsaSignatureToDer(signature, rawSignatureBytes) {
34
+ if (signature.length === rawSignatureBytes) return rawEcdsaSignatureToDer(signature, rawSignatureBytes / 2);
35
+ return signature;
36
+ }
37
+ /**
38
+ * Try the opposite ECDSA encoding: DER→raw or raw→DER.
39
+ *
40
+ * Returns `undefined` if conversion fails, letting callers retry verification
41
+ * with the alternate encoding.
42
+ */
43
+ function alternateEcdsaSignatureEncoding(signature, partLength) {
44
+ if (signature[0] === 48) try {
45
+ return derEcdsaSignatureToRaw(signature, partLength);
46
+ } catch {}
47
+ try {
48
+ return rawEcdsaSignatureToDer(signature, partLength);
49
+ } catch {
50
+ return;
51
+ }
52
+ }
53
+ /** Left-pad and concatenate `r` and `s` into a fixed-width `partLength * 2` byte buffer. */
54
+ function concatFixedWidth(left, right, partLength) {
55
+ if (left.length > partLength || right.length > partLength) throw new Error("ECDSA signature integer too large");
56
+ const out = new Uint8Array(partLength * 2);
57
+ out.set(left, partLength - left.length);
58
+ out.set(right, out.length - right.length);
59
+ return out;
60
+ }
61
+ /** Strip leading zero bytes from a DER INTEGER value, keeping at least one byte. */
62
+ function trimLeadingZero(bytes) {
63
+ let index = 0;
64
+ while (index < bytes.length - 1 && bytes[index] === 0) index += 1;
65
+ return bytes.slice(index);
66
+ }
67
+ //#endregion
68
+ export { alternateEcdsaSignatureEncoding, concatFixedWidth, derEcdsaSignatureToRaw, ecdsaSignatureToDer, rawEcdsaSignatureToDer };