@@ -9,6 +9,8 @@ abstract class AbstractPolicy implements PolicyInterface {
9
9
static readonly BODY_BYTE_MAX_LEN = 257 ;
10
10
static readonly BINDING_BYTE_MIN_LEN = 8 ;
11
11
static readonly BINDING_BYTE_MAX_LEN = 132 ;
12
+ static readonly SIZE_OF_LENGTH_FIELD = 1 ; // 1 byte for each length field (R and S)
13
+ static readonly GMAC_BINDING_LEN = 8 ;
12
14
13
15
readonly type : PolicyType ;
14
16
readonly binding : Uint8Array ;
@@ -18,7 +20,7 @@ abstract class AbstractPolicy implements PolicyInterface {
18
20
// eslint-disable-next-line @typescript-eslint/no-unused-vars
19
21
buff : Uint8Array ,
20
22
// eslint-disable-next-line @typescript-eslint/no-unused-vars
21
- bindingLength : number ,
23
+ useECDSABinding : boolean ,
22
24
// eslint-disable-next-line @typescript-eslint/no-unused-vars
23
25
type ?: PolicyType
24
26
) : { policy : PolicyInterface ; offset : number } {
@@ -43,6 +45,45 @@ abstract class AbstractPolicy implements PolicyInterface {
43
45
toBuffer ( ) : Uint8Array | never {
44
46
throw new Error ( 'toBuffer() was not implemented' ) ;
45
47
}
48
+
49
+ /**
50
+ * Parses an ECDSA binding from a given buffer.
51
+ *
52
+ * @param {Uint8Array } buff - The buffer containing the ECDSA binding.
53
+ * @returns {{ bindingLength: number; binding: Uint8Array } } - An object containing the binding length and the binding subarray.
54
+ */
55
+ static parseECDSABinding ( buff : Uint8Array ) : { bindingLength : number ; binding : Uint8Array } {
56
+ const lengthOfR = buff [ 0 ] ;
57
+ const lengthOfS = buff [ this . SIZE_OF_LENGTH_FIELD + lengthOfR ] ;
58
+
59
+ const bindingLength =
60
+ this . SIZE_OF_LENGTH_FIELD + lengthOfR + this . SIZE_OF_LENGTH_FIELD + lengthOfS ;
61
+ const binding = buff . subarray ( 0 , bindingLength ) ;
62
+
63
+ return { bindingLength, binding } ;
64
+ }
65
+
66
+ /**
67
+ * Parses a binding from a given buffer based on the specified binding type.
68
+ *
69
+ * @param {Uint8Array } buff - The buffer containing the binding.
70
+ * @param {boolean } useEcdsaBinding - Flag indicating whether to use ECDSA binding.
71
+ * @param {number } offset - The starting offset in the buffer.
72
+ * @returns {{ binding: Uint8Array; newOffset: number } } - An object containing the binding and the new offset.
73
+ */
74
+ static parseBinding (
75
+ buff : Uint8Array ,
76
+ useEcdsaBinding : boolean ,
77
+ offset : number
78
+ ) : { binding : Uint8Array ; newOffset : number } {
79
+ if ( useEcdsaBinding ) {
80
+ const ecdsaBinding = this . parseECDSABinding ( buff . subarray ( offset ) ) ;
81
+ return { binding : ecdsaBinding . binding , newOffset : offset + ecdsaBinding . bindingLength } ;
82
+ } else {
83
+ const binding = buff . subarray ( offset , offset + this . GMAC_BINDING_LEN ) ;
84
+ return { binding, newOffset : offset + this . GMAC_BINDING_LEN } ;
85
+ }
86
+ }
46
87
}
47
88
48
89
export default AbstractPolicy ;
0 commit comments