micro509 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.
|
@@ -1,2 +1,2 @@
|
|
|
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
|
|
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
2
|
//# sourceMappingURL=ecdsa.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ecdsa.js","names":[],"sources":["../../../src/internal/crypto/ecdsa.ts"],"sourcesContent":["/**\n * ECDSA signature encoding conversion between ASN.1 DER (r, s INTEGER) and the\n * fixed-width raw format used by WebCrypto.\n *\n * @module\n */\n\nimport { integer, readSequenceChildren, sequence } from '#micro509/internal/asn1/der.ts';\n\n/** Convert a DER-encoded ECDSA signature (SEQUENCE of two INTEGERs) to fixed-width raw `r || s`. */\nexport function derEcdsaSignatureToRaw(signature: Uint8Array, partLength: number): Uint8Array {\n\tconst parts = readSequenceChildren(signature);\n\tconst r = parts[0];\n\tconst s = parts[1];\n\tif (r === undefined || s === undefined || parts.length !== 2) {\n\t\tthrow new Error('Malformed ECDSA DER signature');\n\t}\n\tif (r.tag !== 0x02 || s.tag !== 0x02) {\n\t\tthrow new Error('ECDSA DER signature must contain two INTEGERs');\n\t}\n\treturn concatFixedWidth(trimLeadingZero(r.value), trimLeadingZero(s.value), partLength);\n}\n\n/** Convert a fixed-width raw `r || s` ECDSA signature to DER-encoded SEQUENCE of two INTEGERs. */\nexport function rawEcdsaSignatureToDer(signature: Uint8Array, partLength: number): Uint8Array {\n\tif (signature.length !== partLength * 2) {\n\t\tthrow new Error('Unexpected ECDSA raw signature length');\n\t}\n\treturn sequence([integer(signature.slice(0, partLength)), integer(signature.slice(partLength))]);\n}\n\n/**\n * Try the opposite ECDSA encoding: DER→raw or raw→DER.\n *\n * Returns `undefined` if conversion fails, letting callers retry verification\n * with the alternate encoding.\n */\nexport function alternateEcdsaSignatureEncoding(\n\tsignature: Uint8Array,\n\tpartLength: number,\n): Uint8Array | undefined {\n\t// Try DER→raw first if it looks like DER (starts with SEQUENCE tag)\n\tif (signature[0] === 0x30) {\n\t\ttry {\n\t\t\treturn derEcdsaSignatureToRaw(signature, partLength);\n\t\t} catch {\n\t\t\t// Fall through to try raw→DER\n\t\t}\n\t}\n\t// Try raw→DER\n\ttry {\n\t\treturn rawEcdsaSignatureToDer(signature, partLength);\n\t} catch {\n\t\treturn undefined;\n\t}\n}\n\n/** Left-pad and concatenate `r` and `s` into a fixed-width `partLength * 2` byte buffer. */\nexport function concatFixedWidth(\n\tleft: Uint8Array,\n\tright: Uint8Array,\n\tpartLength: number,\n): Uint8Array {\n\tif (left.length > partLength || right.length > partLength) {\n\t\tthrow new Error('ECDSA signature integer too large');\n\t}\n\tconst out = new Uint8Array(partLength * 2);\n\tout.set(left, partLength - left.length);\n\tout.set(right, out.length - right.length);\n\treturn out;\n}\n\n/** Strip leading zero bytes from a DER INTEGER value, keeping at least one byte. */\nfunction trimLeadingZero(bytes: Uint8Array): Uint8Array {\n\tlet index = 0;\n\twhile (index < bytes.length - 1 && bytes[index] === 0) {\n\t\tindex += 1;\n\t}\n\treturn bytes.slice(index);\n}\n"],"mappings":"iFAUA,SAAgB,EAAuB,EAAuB,EAAgC,CAC7F,IAAM,EAAQ,EAAqB,CAAS,EACtC,EAAI,EAAM,GACV,EAAI,EAAM,GAChB,GAAI,IAAM,IAAA,IAAa,IAAM,IAAA,IAAa,EAAM,SAAW,EAC1D,MAAU,MAAM,+BAA+B,EAEhD,GAAI,EAAE,MAAQ,GAAQ,EAAE,MAAQ,EAC/B,MAAU,MAAM,+CAA+C,EAEhE,OAAO,EAAiB,EAAgB,EAAE,KAAK,EAAG,EAAgB,EAAE,KAAK,EAAG,CAAU,CACvF,CAGA,SAAgB,EAAuB,EAAuB,EAAgC,CAC7F,GAAI,EAAU,SAAW,EAAa,EACrC,MAAU,MAAM,uCAAuC,EAExD,OAAO,EAAS,CAAC,EAAQ,EAAU,MAAM,EAAG,CAAU,CAAC,EAAG,EAAQ,EAAU,MAAM,CAAU,CAAC,CAAC,CAAC,CAChG,CAQA,SAAgB,EACf,EACA,EACyB,CAEzB,GAAI,EAAU,KAAO,GACpB,GAAI,CACH,OAAO,EAAuB,EAAW,CAAU,CACpD,MAAQ,CAER,CAGD,GAAI,CACH,OAAO,EAAuB,EAAW,CAAU,CACpD,MAAQ,CACP,MACD,CACD,CAGA,SAAgB,EACf,EACA,EACA,EACa,CACb,GAAI,EAAK,OAAS,GAAc,EAAM,OAAS,EAC9C,MAAU,MAAM,mCAAmC,EAEpD,IAAM,EAAM,IAAI,WAAW,EAAa,CAAC,EAGzC,OAFA,EAAI,IAAI,EAAM,EAAa,EAAK,MAAM,EACtC,EAAI,IAAI,EAAO,EAAI,OAAS,EAAM,MAAM,EACjC,CACR,CAGA,SAAS,EAAgB,EAA+B,CACvD,IAAI,EAAQ,EACZ,KAAO,EAAQ,EAAM,OAAS,GAAK,EAAM,KAAW,GACnD,GAAS,EAEV,OAAO,EAAM,MAAM,CAAK,CACzB"}
|
|
1
|
+
{"version":3,"file":"ecdsa.js","names":[],"sources":["../../../src/internal/crypto/ecdsa.ts"],"sourcesContent":["/**\n * ECDSA signature encoding conversion between ASN.1 DER (r, s INTEGER) and the\n * fixed-width raw format used by WebCrypto.\n *\n * @module\n */\n\nimport { integer, readSequenceChildren, sequence } from '#micro509/internal/asn1/der.ts';\n\n/** Convert a DER-encoded ECDSA signature (SEQUENCE of two INTEGERs) to fixed-width raw `r || s`. */\nexport function derEcdsaSignatureToRaw(signature: Uint8Array, partLength: number): Uint8Array {\n\tconst parts = readSequenceChildren(signature);\n\tconst r = parts[0];\n\tconst s = parts[1];\n\tif (r === undefined || s === undefined || parts.length !== 2) {\n\t\tthrow new Error('Malformed ECDSA DER signature');\n\t}\n\tif (r.tag !== 0x02 || s.tag !== 0x02) {\n\t\tthrow new Error('ECDSA DER signature must contain two INTEGERs');\n\t}\n\treturn concatFixedWidth(trimLeadingZero(r.value), trimLeadingZero(s.value), partLength);\n}\n\n/** Convert a fixed-width raw `r || s` ECDSA signature to DER-encoded SEQUENCE of two INTEGERs. */\nexport function rawEcdsaSignatureToDer(signature: Uint8Array, partLength: number): Uint8Array {\n\tif (signature.length !== partLength * 2) {\n\t\tthrow new Error('Unexpected ECDSA raw signature length');\n\t}\n\treturn sequence([integer(signature.slice(0, partLength)), integer(signature.slice(partLength))]);\n}\n\n/**\n * Normalize a WebCrypto ECDSA signature to DER.\n *\n * Spec-compliant WebCrypto always returns fixed-width raw `r || s`\n * (exactly `rawSignatureBytes` long), which needs conversion. Detection is\n * by length, never by sniffing the first byte: a raw signature whose `r`\n * happens to start with `0x30` (1-in-256 per signature) is\n * indistinguishable from DER by tag, and embedding it unconverted produces\n * a certificate/CRL that OpenSSL rejects with a signature failure.\n */\nexport function ecdsaSignatureToDer(signature: Uint8Array, rawSignatureBytes: number): Uint8Array {\n\tif (signature.length === rawSignatureBytes) {\n\t\treturn rawEcdsaSignatureToDer(signature, rawSignatureBytes / 2);\n\t}\n\treturn signature;\n}\n\n/**\n * Try the opposite ECDSA encoding: DER→raw or raw→DER.\n *\n * Returns `undefined` if conversion fails, letting callers retry verification\n * with the alternate encoding.\n */\nexport function alternateEcdsaSignatureEncoding(\n\tsignature: Uint8Array,\n\tpartLength: number,\n): Uint8Array | undefined {\n\t// Try DER→raw first if it looks like DER (starts with SEQUENCE tag)\n\tif (signature[0] === 0x30) {\n\t\ttry {\n\t\t\treturn derEcdsaSignatureToRaw(signature, partLength);\n\t\t} catch {\n\t\t\t// Fall through to try raw→DER\n\t\t}\n\t}\n\t// Try raw→DER\n\ttry {\n\t\treturn rawEcdsaSignatureToDer(signature, partLength);\n\t} catch {\n\t\treturn undefined;\n\t}\n}\n\n/** Left-pad and concatenate `r` and `s` into a fixed-width `partLength * 2` byte buffer. */\nexport function concatFixedWidth(\n\tleft: Uint8Array,\n\tright: Uint8Array,\n\tpartLength: number,\n): Uint8Array {\n\tif (left.length > partLength || right.length > partLength) {\n\t\tthrow new Error('ECDSA signature integer too large');\n\t}\n\tconst out = new Uint8Array(partLength * 2);\n\tout.set(left, partLength - left.length);\n\tout.set(right, out.length - right.length);\n\treturn out;\n}\n\n/** Strip leading zero bytes from a DER INTEGER value, keeping at least one byte. */\nfunction trimLeadingZero(bytes: Uint8Array): Uint8Array {\n\tlet index = 0;\n\twhile (index < bytes.length - 1 && bytes[index] === 0) {\n\t\tindex += 1;\n\t}\n\treturn bytes.slice(index);\n}\n"],"mappings":"iFAUA,SAAgB,EAAuB,EAAuB,EAAgC,CAC7F,IAAM,EAAQ,EAAqB,CAAS,EACtC,EAAI,EAAM,GACV,EAAI,EAAM,GAChB,GAAI,IAAM,IAAA,IAAa,IAAM,IAAA,IAAa,EAAM,SAAW,EAC1D,MAAU,MAAM,+BAA+B,EAEhD,GAAI,EAAE,MAAQ,GAAQ,EAAE,MAAQ,EAC/B,MAAU,MAAM,+CAA+C,EAEhE,OAAO,EAAiB,EAAgB,EAAE,KAAK,EAAG,EAAgB,EAAE,KAAK,EAAG,CAAU,CACvF,CAGA,SAAgB,EAAuB,EAAuB,EAAgC,CAC7F,GAAI,EAAU,SAAW,EAAa,EACrC,MAAU,MAAM,uCAAuC,EAExD,OAAO,EAAS,CAAC,EAAQ,EAAU,MAAM,EAAG,CAAU,CAAC,EAAG,EAAQ,EAAU,MAAM,CAAU,CAAC,CAAC,CAAC,CAChG,CAYA,SAAgB,EAAoB,EAAuB,EAAuC,CAIjG,OAHI,EAAU,SAAW,EACjB,EAAuB,EAAW,EAAoB,CAAC,EAExD,CACR,CAQA,SAAgB,EACf,EACA,EACyB,CAEzB,GAAI,EAAU,KAAO,GACpB,GAAI,CACH,OAAO,EAAuB,EAAW,CAAU,CACpD,MAAQ,CAER,CAGD,GAAI,CACH,OAAO,EAAuB,EAAW,CAAU,CACpD,MAAQ,CACP,MACD,CACD,CAGA,SAAgB,EACf,EACA,EACA,EACa,CACb,GAAI,EAAK,OAAS,GAAc,EAAM,OAAS,EAC9C,MAAU,MAAM,mCAAmC,EAEpD,IAAM,EAAM,IAAI,WAAW,EAAa,CAAC,EAGzC,OAFA,EAAI,IAAI,EAAM,EAAa,EAAK,MAAM,EACtC,EAAI,IAAI,EAAO,EAAI,OAAS,EAAM,MAAM,EACjC,CACR,CAGA,SAAS,EAAgB,EAA+B,CACvD,IAAI,EAAQ,EACZ,KAAO,EAAQ,EAAM,OAAS,GAAK,EAAM,KAAW,GACnD,GAAS,EAEV,OAAO,EAAM,MAAM,CAAK,CACzB"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{nullValue as e,objectIdentifier as t,sequence as n}from"../asn1/der.js";import{OIDS as r}from"../asn1/oids.js";import{getCrypto as i}from"./webcrypto.js";import{encodeRsaPssParameters as a,rsaPssParametersForHash as o}from"./rsa-pss.js";import{
|
|
1
|
+
import{nullValue as e,objectIdentifier as t,sequence as n}from"../asn1/der.js";import{OIDS as r}from"../asn1/oids.js";import{getCrypto as i}from"./webcrypto.js";import{encodeRsaPssParameters as a,rsaPssParametersForHash as o}from"./rsa-pss.js";import{ecdsaSignatureToDer as s}from"./ecdsa.js";function c(t,n={}){let i=t.algorithm;if(n.kind===`rsa-pss`)return l(t,n.saltLength);if(i.name===`RSASSA-PKCS1-v1_5`){if(!f(i))throw Error(`RSA key is missing hash metadata`);switch(i.hash.name){case`SHA-256`:return{algorithmOid:r.sha256WithRSAEncryption,parameters:e(),signParams:{name:`RSASSA-PKCS1-v1_5`}};case`SHA-384`:return{algorithmOid:r.sha384WithRSAEncryption,parameters:e(),signParams:{name:`RSASSA-PKCS1-v1_5`}};case`SHA-512`:return{algorithmOid:r.sha512WithRSAEncryption,parameters:e(),signParams:{name:`RSASSA-PKCS1-v1_5`}};default:throw Error(`Unsupported RSA hash: ${i.hash.name}`)}}if(i.name===`RSA-PSS`)throw Error(`RSA-PSS signing requires an explicit signature profile`);if(i.name===`ECDSA`){if(!p(i))throw Error(`ECDSA key is missing namedCurve metadata`);switch(i.namedCurve){case`P-256`:return{algorithmOid:r.ecdsaWithSHA256,signParams:{name:`ECDSA`,hash:`SHA-256`},ecdsaRawSignatureBytes:64};case`P-384`:return{algorithmOid:r.ecdsaWithSHA384,signParams:{name:`ECDSA`,hash:`SHA-384`},ecdsaRawSignatureBytes:96};case`P-521`:return{algorithmOid:r.ecdsaWithSHA512,signParams:{name:`ECDSA`,hash:`SHA-512`},ecdsaRawSignatureBytes:132};default:throw Error(`Unsupported curve: ${i.namedCurve}`)}}if(i.name===`Ed25519`)return{algorithmOid:r.ed25519,signParams:{name:`Ed25519`}};throw Error(`Unsupported signing key algorithm: ${i.name}`)}function l(e,t){let n=e.algorithm;if(n.name!==`RSA-PSS`)throw Error(`RSA-PSS signature profile requires an RSA-PSS private key`);if(!f(n))throw Error(`RSA-PSS key is missing hash metadata`);let i=m(n.hash.name),s=o(i);if(t!==void 0&&t!==s.saltLength)throw Error(`Unsupported RSA-PSS saltLength ${t} for ${i}; expected ${s.saltLength}`);return{algorithmOid:r.rsassaPss,parameters:a(s),signParams:{name:`RSA-PSS`,saltLength:s.saltLength}}}function u(e){let r=[t(e.algorithmOid)];return e.parameters!==void 0&&r.push(e.parameters),n(r)}async function d(e,t,n){let r=new Uint8Array(n),a=new Uint8Array(await i().subtle.sign(t.signParams,e,r));return t.ecdsaRawSignatureBytes===void 0?a:s(a,t.ecdsaRawSignatureBytes)}function f(e){return`hash`in e}function p(e){return`namedCurve`in e}function m(e){switch(e){case`SHA-256`:return`SHA-256`;case`SHA-384`:return`SHA-384`;case`SHA-512`:return`SHA-512`;default:throw Error(`Unsupported RSA hash: ${e}`)}}export{u as encodeAlgorithmIdentifier,c as getSignatureAlgorithm,d as signBytes};
|
|
2
2
|
//# sourceMappingURL=signing.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"signing.js","names":[],"sources":["../../../src/internal/crypto/signing.ts"],"sourcesContent":["/**\n * Signing helpers that map WebCrypto keys and signature profiles to on-wire\n * `AlgorithmIdentifier` values. Used by certificate and CSR builders.\n *\n * @module\n */\n\nimport { nullValue, objectIdentifier, sequence } from '#micro509/internal/asn1/der.ts';\nimport { OIDS } from '#micro509/internal/asn1/oids.ts';\nimport {
|
|
1
|
+
{"version":3,"file":"signing.js","names":[],"sources":["../../../src/internal/crypto/signing.ts"],"sourcesContent":["/**\n * Signing helpers that map WebCrypto keys and signature profiles to on-wire\n * `AlgorithmIdentifier` values. Used by certificate and CSR builders.\n *\n * @module\n */\n\nimport { nullValue, objectIdentifier, sequence } from '#micro509/internal/asn1/der.ts';\nimport { OIDS } from '#micro509/internal/asn1/oids.ts';\nimport { ecdsaSignatureToDer } from './ecdsa.ts';\nimport { encodeRsaPssParameters, type RsaPssHash, rsaPssParametersForHash } from './rsa-pss.ts';\nimport { getCrypto } from './webcrypto.ts';\n\n/**\n * Controls how the signature algorithm is chosen.\n *\n * `'auto'` (default) infers the algorithm from the key. `'rsa-pss'` forces RSA-PSS\n * padding and requires an RSA-PSS private key.\n */\nexport type SignatureProfileInput =\n\t| {\n\t\t\t/** Infer the signature algorithm from the private key. */\n\t\t\treadonly kind?: 'auto';\n\t }\n\t| {\n\t\t\t/** Force RSA-PSS padding. */\n\t\t\treadonly kind: 'rsa-pss';\n\t\t\t/** Salt length in bytes. Must match the key's hash digest size. */\n\t\t\treadonly saltLength?: 32 | 48 | 64;\n\t };\n\n/** Resolved signature algorithm: the OID/parameters for DER encoding and the WebCrypto sign params. */\nexport interface SignatureAlgorithmIdentifier {\n\t/** ASN.1 OID of the signature algorithm (e.g. sha256WithRSAEncryption). */\n\treadonly algorithmOid: string;\n\t/** DER-encoded algorithm parameters, if the algorithm requires them (e.g. RSA-PSS). */\n\treadonly parameters?: Uint8Array;\n\t/** WebCrypto `sign()` algorithm parameter. */\n\treadonly signParams: Algorithm | EcdsaParams | RsaPssParams;\n\t/** When set, the raw ECDSA signature is this many bytes and must be DER-converted. */\n\treadonly ecdsaRawSignatureBytes?: number;\n}\n\n/** Resolve a private key and optional profile into a {@linkcode SignatureAlgorithmIdentifier}. */\nexport function getSignatureAlgorithm(\n\tprivateKey: CryptoKey,\n\tprofile: SignatureProfileInput = {},\n): SignatureAlgorithmIdentifier {\n\tconst algorithm = privateKey.algorithm;\n\tif (profile.kind === 'rsa-pss') {\n\t\treturn getRsaPssSignatureAlgorithm(privateKey, profile.saltLength);\n\t}\n\tif (algorithm.name === 'RSASSA-PKCS1-v1_5') {\n\t\tif (!hasHash(algorithm)) {\n\t\t\tthrow new Error('RSA key is missing hash metadata');\n\t\t}\n\t\tswitch (algorithm.hash.name) {\n\t\t\tcase 'SHA-256':\n\t\t\t\treturn {\n\t\t\t\t\talgorithmOid: OIDS.sha256WithRSAEncryption,\n\t\t\t\t\tparameters: nullValue(),\n\t\t\t\t\tsignParams: { name: 'RSASSA-PKCS1-v1_5' },\n\t\t\t\t};\n\t\t\tcase 'SHA-384':\n\t\t\t\treturn {\n\t\t\t\t\talgorithmOid: OIDS.sha384WithRSAEncryption,\n\t\t\t\t\tparameters: nullValue(),\n\t\t\t\t\tsignParams: { name: 'RSASSA-PKCS1-v1_5' },\n\t\t\t\t};\n\t\t\tcase 'SHA-512':\n\t\t\t\treturn {\n\t\t\t\t\talgorithmOid: OIDS.sha512WithRSAEncryption,\n\t\t\t\t\tparameters: nullValue(),\n\t\t\t\t\tsignParams: { name: 'RSASSA-PKCS1-v1_5' },\n\t\t\t\t};\n\t\t\tdefault:\n\t\t\t\tthrow new Error(`Unsupported RSA hash: ${algorithm.hash.name}`);\n\t\t}\n\t}\n\n\tif (algorithm.name === 'RSA-PSS') {\n\t\tthrow new Error('RSA-PSS signing requires an explicit signature profile');\n\t}\n\n\tif (algorithm.name === 'ECDSA') {\n\t\tif (!hasNamedCurve(algorithm)) {\n\t\t\tthrow new Error('ECDSA key is missing namedCurve metadata');\n\t\t}\n\t\tswitch (algorithm.namedCurve) {\n\t\t\tcase 'P-256':\n\t\t\t\treturn {\n\t\t\t\t\talgorithmOid: OIDS.ecdsaWithSHA256,\n\t\t\t\t\tsignParams: { name: 'ECDSA', hash: 'SHA-256' },\n\t\t\t\t\tecdsaRawSignatureBytes: 64,\n\t\t\t\t};\n\t\t\tcase 'P-384':\n\t\t\t\treturn {\n\t\t\t\t\talgorithmOid: OIDS.ecdsaWithSHA384,\n\t\t\t\t\tsignParams: { name: 'ECDSA', hash: 'SHA-384' },\n\t\t\t\t\tecdsaRawSignatureBytes: 96,\n\t\t\t\t};\n\t\t\tcase 'P-521':\n\t\t\t\treturn {\n\t\t\t\t\talgorithmOid: OIDS.ecdsaWithSHA512,\n\t\t\t\t\tsignParams: { name: 'ECDSA', hash: 'SHA-512' },\n\t\t\t\t\tecdsaRawSignatureBytes: 132,\n\t\t\t\t};\n\t\t\tdefault:\n\t\t\t\tthrow new Error(`Unsupported curve: ${algorithm.namedCurve}`);\n\t\t}\n\t}\n\n\tif (algorithm.name === 'Ed25519') {\n\t\treturn {\n\t\t\talgorithmOid: OIDS.ed25519,\n\t\t\tsignParams: { name: 'Ed25519' },\n\t\t};\n\t}\n\n\tthrow new Error(`Unsupported signing key algorithm: ${algorithm.name}`);\n}\n\n/** Build an RSA-PSS {@linkcode SignatureAlgorithmIdentifier} from an RSA-PSS private key. */\nfunction getRsaPssSignatureAlgorithm(\n\tprivateKey: CryptoKey,\n\tsaltLength: number | undefined,\n): SignatureAlgorithmIdentifier {\n\tconst algorithm = privateKey.algorithm;\n\tif (algorithm.name !== 'RSA-PSS') {\n\t\tthrow new Error('RSA-PSS signature profile requires an RSA-PSS private key');\n\t}\n\tif (!hasHash(algorithm)) {\n\t\tthrow new Error('RSA-PSS key is missing hash metadata');\n\t}\n\tconst hash = rsaPssHashFromWebCryptoName(algorithm.hash.name);\n\tconst parameters = rsaPssParametersForHash(hash);\n\tif (saltLength !== undefined && saltLength !== parameters.saltLength) {\n\t\tthrow new Error(\n\t\t\t`Unsupported RSA-PSS saltLength ${saltLength} for ${hash}; expected ${parameters.saltLength}`,\n\t\t);\n\t}\n\treturn {\n\t\talgorithmOid: OIDS.rsassaPss,\n\t\tparameters: encodeRsaPssParameters(parameters),\n\t\tsignParams: {\n\t\t\tname: 'RSA-PSS',\n\t\t\tsaltLength: parameters.saltLength,\n\t\t},\n\t};\n}\n\n/** DER-encode a {@linkcode SignatureAlgorithmIdentifier} as an ASN.1 `AlgorithmIdentifier` SEQUENCE. */\nexport function encodeAlgorithmIdentifier(input: SignatureAlgorithmIdentifier): Uint8Array {\n\tconst parts = [objectIdentifier(input.algorithmOid)];\n\tif (input.parameters !== undefined) {\n\t\tparts.push(input.parameters);\n\t}\n\treturn sequence(parts);\n}\n\n/** Sign `data` and return a DER-encoded signature. ECDSA raw signatures are auto-converted to DER. */\nexport async function signBytes(\n\tprivateKey: CryptoKey,\n\talgorithm: SignatureAlgorithmIdentifier,\n\tdata: Uint8Array,\n): Promise<Uint8Array> {\n\tconst view = new Uint8Array(data);\n\tconst signature = new Uint8Array(\n\t\tawait getCrypto().subtle.sign(algorithm.signParams, privateKey, view),\n\t);\n\tif (algorithm.ecdsaRawSignatureBytes !== undefined) {\n\t\treturn ecdsaSignatureToDer(signature, algorithm.ecdsaRawSignatureBytes);\n\t}\n\treturn signature;\n}\n\n/** Type guard: does this key algorithm carry a `hash` property (RSA keys). */\nfunction hasHash(algorithm: KeyAlgorithm): algorithm is RsaHashedKeyAlgorithm {\n\treturn 'hash' in algorithm;\n}\n\n/** Type guard: does this key algorithm carry a `namedCurve` property (EC keys). */\nfunction hasNamedCurve(algorithm: KeyAlgorithm): algorithm is EcKeyAlgorithm {\n\treturn 'namedCurve' in algorithm;\n}\n\n/** Narrow a WebCrypto hash name string to the supported {@linkcode RsaPssHash} union. */\nfunction rsaPssHashFromWebCryptoName(hash: string): RsaPssHash {\n\tswitch (hash) {\n\t\tcase 'SHA-256':\n\t\t\treturn 'SHA-256';\n\t\tcase 'SHA-384':\n\t\t\treturn 'SHA-384';\n\t\tcase 'SHA-512':\n\t\t\treturn 'SHA-512';\n\t\tdefault:\n\t\t\tthrow new Error(`Unsupported RSA hash: ${hash}`);\n\t}\n}\n"],"mappings":"qSA4CA,SAAgB,EACf,EACA,EAAiC,CAAC,EACH,CAC/B,IAAM,EAAY,EAAW,UAC7B,GAAI,EAAQ,OAAS,UACpB,OAAO,EAA4B,EAAY,EAAQ,UAAU,EAElE,GAAI,EAAU,OAAS,oBAAqB,CAC3C,GAAI,CAAC,EAAQ,CAAS,EACrB,MAAU,MAAM,kCAAkC,EAEnD,OAAQ,EAAU,KAAK,KAAvB,CACC,IAAK,UACJ,MAAO,CACN,aAAc,EAAK,wBACnB,WAAY,EAAU,EACtB,WAAY,CAAE,KAAM,mBAAoB,CACzC,EACD,IAAK,UACJ,MAAO,CACN,aAAc,EAAK,wBACnB,WAAY,EAAU,EACtB,WAAY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