• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hyperledger / identus-edge-agent-sdk-ts / 12012609693

25 Nov 2024 02:51PM UTC coverage: 72.557% (-0.06%) from 72.613%
12012609693

push

github

web-flow
chore: fix package vuln (#345)

Signed-off-by: Curtish <ch@curtish.me>

1665 of 2504 branches covered (66.49%)

Branch coverage included in aggregate %.

3525 of 4649 relevant lines covered (75.82%)

28.32 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

35.14
/src/domain/utils/DER.ts
1
/**
2
 * Fix around normalising DER signatures into their raw representation
3
 * @param derSignature Buffer
4
 * @returns Buffer
5
 */
6
export function normaliseDER(derSignature: Buffer): Buffer {
7
    // Ensure the DER signature starts with the correct sequence header
8
    if (derSignature[0] !== 0x30) {
59!
9
        return derSignature;
×
10
    }
11
    // Get the length of the sequence
12
    let seqLength = derSignature[1];
59✔
13
    let offset = 2;
59✔
14
    if (seqLength & 0x80) {
59!
15
        const lengthBytes = seqLength & 0x7f;
×
16
        seqLength = 0;
×
17
        for (let i = 0; i < lengthBytes; i++) {
×
18
            seqLength = (seqLength << 8) | derSignature[offset++];
×
19
        }
20
    }
21

22
    if (derSignature[offset++] !== 0x02) {
59!
23
        throw new Error('Invalid DER signature: expected integer for r');
×
24
    }
25

26
    const rLength = derSignature[offset++];
59✔
27
    let r = derSignature.slice(offset, offset + rLength);
59✔
28
    offset += rLength;
59✔
29

30
    // Extract s value
31
    if (derSignature[offset++] !== 0x02) {
59!
32
        throw new Error('Invalid DER signature: expected integer for s');
×
33
    }
34
    const sLength = derSignature[offset++];
59✔
35
    let s = derSignature.slice(offset, offset + sLength);
59✔
36

37
    // Normalize r and s to 32 bytes
38
    if (r.length > 32) {
59✔
39
        r = r.slice(-32); // truncate if r is longer than 32 bytes
34✔
40
    } else if (r.length < 32) {
25!
41
        const paddedR = Buffer.alloc(32);
×
42
        r.copy(paddedR, 32 - r.length);
×
43
        r = paddedR; // left pad with zeros if r is shorter than 32 bytes
×
44
    }
45

46
    if (s.length > 32) {
59!
47
        s = s.slice(-32); // truncate if s is longer than 32 bytes
×
48
    } else if (s.length < 32) {
59!
49
        const paddedS = Buffer.alloc(32);
×
50
        s.copy(paddedS, 32 - s.length);
×
51
        s = paddedS; // left pad with zeros if s is shorter than 32 bytes
×
52
    }
53

54
    // Concatenate r and s to form the raw signature
55
    return Buffer.concat([r, s]);
59✔
56
}
57

58
/**
59
 * Converts a raw signature to DER format
60
 * @param rawSignature Buffer
61
 * @returns Buffer
62
 */
63
export function rawToDER(rawSignature: Buffer): Buffer {
64
    if (rawSignature.length !== 64) {
×
65
        return rawSignature;
×
66
    }
67

68
    let r = rawSignature.slice(0, 32);
×
69
    let s = rawSignature.slice(32, 64);
×
70

71
    r = removeLeadingZeros(r);
×
72
    s = removeLeadingZeros(s);
×
73

74
    // Ensure the integers are positive by prepending a zero byte if necessary
75
    if (r[0] & 0x80) {
×
76
        r = Buffer.concat([Buffer.from([0x00]), r]);
×
77
    }
78
    if (s[0] & 0x80) {
×
79
        s = Buffer.concat([Buffer.from([0x00]), s]);
×
80
    }
81

82
    const rLen = r.length;
×
83
    const sLen = s.length;
×
84

85
    const derSignature = Buffer.concat([
×
86
        Buffer.from([0x30, rLen + sLen + 4]), // Sequence tag and length
87
        Buffer.from([0x02, rLen]), // Integer tag and length for r
88
        r,
89
        Buffer.from([0x02, sLen]), // Integer tag and length for s
90
        s
91
    ]);
92

93
    return derSignature;
×
94
}
95

96
/**
97
 * Remove leading zeros from a buffer
98
 * @param buffer Buffer
99
 * @returns Buffer
100
 */
101
function removeLeadingZeros(buffer: Buffer): Buffer {
102
    const arr = Array.from(buffer)
×
103
    let i = 0;
×
104
    while (i < arr.length - 1 && arr[i] === 0) {
×
105
        i++;
×
106
    }
107
    return Buffer.from(arr.slice(i));
×
108
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc