@vizamodo/aws-sts-core 0.1.17 → 0.1.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/sts/signer.js +46 -7
- package/package.json +1 -1
package/dist/sts/signer.js
CHANGED
|
@@ -10,22 +10,61 @@ import { InternalError } from "./errors";
|
|
|
10
10
|
export async function signStringToSign(stringToSign, signingKey) {
|
|
11
11
|
try {
|
|
12
12
|
const data = new TextEncoder().encode(stringToSign);
|
|
13
|
-
const
|
|
13
|
+
const signatureRaw = await crypto.subtle.sign({
|
|
14
14
|
name: "ECDSA",
|
|
15
15
|
hash: "SHA-256",
|
|
16
16
|
}, signingKey, data);
|
|
17
|
-
|
|
17
|
+
// WebCrypto trả về 64 bytes (R và S)
|
|
18
|
+
// Cần convert sang DER format cho AWS
|
|
19
|
+
const derSignature = rawToDer(new Uint8Array(signatureRaw));
|
|
20
|
+
return uint8ArrayToHex(derSignature);
|
|
18
21
|
}
|
|
19
|
-
catch {
|
|
22
|
+
catch (e) {
|
|
23
|
+
console.error("[SIGNING_DEBUG]", e);
|
|
20
24
|
throw new InternalError("signing_failed");
|
|
21
25
|
}
|
|
22
26
|
}
|
|
23
27
|
/**
|
|
24
|
-
* Convert
|
|
25
|
-
* This matches AWS SigV4 expectations.
|
|
28
|
+
* Convert IEEE P1363 (Raw) ECDSA signature to ASN.1 DER
|
|
26
29
|
*/
|
|
27
|
-
function
|
|
28
|
-
const
|
|
30
|
+
function rawToDer(raw) {
|
|
31
|
+
const r = raw.slice(0, 32);
|
|
32
|
+
const s = raw.slice(32);
|
|
33
|
+
const toAsn1Int = (bytes) => {
|
|
34
|
+
// Loại bỏ các byte 0 ở đầu
|
|
35
|
+
let start = 0;
|
|
36
|
+
while (start < bytes.length - 1 && bytes[start] === 0)
|
|
37
|
+
start++;
|
|
38
|
+
let payload = bytes.slice(start);
|
|
39
|
+
// Nếu bit cao nhất là 1, phải thêm 0x00 để không bị hiểu lầm là số âm
|
|
40
|
+
if (payload[0] > 0x7f) {
|
|
41
|
+
const padded = new Uint8Array(payload.length + 1);
|
|
42
|
+
padded.set(payload, 1);
|
|
43
|
+
return padded;
|
|
44
|
+
}
|
|
45
|
+
return payload;
|
|
46
|
+
};
|
|
47
|
+
const rAsn1 = toAsn1Int(r);
|
|
48
|
+
const sAsn1 = toAsn1Int(s);
|
|
49
|
+
const length = rAsn1.length + sAsn1.length + 4;
|
|
50
|
+
const der = new Uint8Array(length + 2);
|
|
51
|
+
der[0] = 0x30; // Sequence
|
|
52
|
+
der[1] = length;
|
|
53
|
+
let offset = 2;
|
|
54
|
+
der[offset++] = 0x02; // Integer tag
|
|
55
|
+
der[offset++] = rAsn1.length;
|
|
56
|
+
der.set(rAsn1, offset);
|
|
57
|
+
offset += rAsn1.length;
|
|
58
|
+
der[offset++] = 0x02; // Integer tag
|
|
59
|
+
der[offset++] = sAsn1.length;
|
|
60
|
+
der.set(sAsn1, offset);
|
|
61
|
+
return der;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Convert Uint8Array → lowercase hex string.
|
|
65
|
+
* Avoids ArrayBuffer vs SharedArrayBuffer type mismatch.
|
|
66
|
+
*/
|
|
67
|
+
function uint8ArrayToHex(bytes) {
|
|
29
68
|
let hex = "";
|
|
30
69
|
for (let i = 0; i < bytes.length; i++) {
|
|
31
70
|
hex += bytes[i].toString(16).padStart(2, "0");
|