asn1.js
5.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/* eslint-disable class-methods-use-this */
const {BigInteger} = require('jsbn')
function bigIntToMinTwosComplementsHex(bigIntegerValue) {
let h = bigIntegerValue.toString(16)
if (h.substr(0, 1) !== '-') {
if (h.length % 2 === 1) {
h = '0' + h
} else if (!h.match(/^[0-7]/)) {
h = '00' + h
}
} else {
const hPos = h.substr(1)
let xorLen = hPos.length
if (xorLen % 2 === 1) {
xorLen += 1
} else if (!h.match(/^[0-7]/)) {
xorLen += 2
}
let hMask = ''
for (let i = 0; i < xorLen; i++) {
hMask += 'f'
}
const biMask = new BigInteger(hMask, 16)
const biNeg = biMask.xor(bigIntegerValue).add(BigInteger.ONE)
h = biNeg.toString(16).replace(/^-/, '')
}
return h
}
/**
* base class for ASN.1 DER encoder object
*/
class ASN1Object {
constructor() {
this.isModified = true
this.hTLV = null
this.hT = '00'
this.hL = '00'
this.hV = ''
}
/**
* get hexadecimal ASN.1 TLV length(L) bytes from TLV value(V)
*/
getLengthHexFromValue() {
const n = this.hV.length / 2
let hN = n.toString(16)
if (hN.length % 2 === 1) {
hN = '0' + hN
}
if (n < 128) {
return hN
} else {
const hNlen = hN.length / 2
const head = 128 + hNlen
return head.toString(16) + hN
}
}
/**
* get hexadecimal string of ASN.1 TLV bytes
*/
getEncodedHex() {
if (this.hTLV == null || this.isModified) {
this.hV = this.getFreshValueHex()
this.hL = this.getLengthHexFromValue()
this.hTLV = this.hT + this.hL + this.hV
this.isModified = false
}
return this.hTLV
}
getFreshValueHex() {
return ''
}
}
/**
* class for ASN.1 DER Integer
*/
class DERInteger extends ASN1Object {
constructor(options) {
super()
this.hT = '02'
if (options && options.bigint) {
this.hTLV = null
this.isModified = true
this.hV = bigIntToMinTwosComplementsHex(options.bigint)
}
}
getFreshValueHex() {
return this.hV
}
}
/**
* class for ASN.1 DER Sequence
*/
class DERSequence extends ASN1Object {
constructor(options) {
super()
this.hT = '30'
this.asn1Array = []
if (options && options.array) {
this.asn1Array = options.array
}
}
getFreshValueHex() {
let h = ''
for (let i = 0; i < this.asn1Array.length; i++) {
const asn1Obj = this.asn1Array[i]
h += asn1Obj.getEncodedHex()
}
this.hV = h
return this.hV
}
}
/**
* get byte length for ASN.1 L(length) bytes
*/
function getByteLengthOfL(s, pos) {
if (s.substring(pos + 2, pos + 3) !== '8') return 1
const i = parseInt(s.substring(pos + 3, pos + 4), 10)
if (i === 0) return -1 // length octet '80' indefinite length
if (i > 0 && i < 10) return i + 1 // including '8?' octet;
return -2 // malformed format
}
/**
* get hexadecimal string for ASN.1 L(length) bytes
*/
function getHexOfL(s, pos) {
const len = getByteLengthOfL(s, pos)
if (len < 1) return ''
return s.substring(pos + 2, pos + 2 + len * 2)
}
/**
* get integer value of ASN.1 length for ASN.1 data
*/
function getIntOfL(s, pos) {
const hLength = getHexOfL(s, pos)
if (hLength === '') return -1
let bi
if (parseInt(hLength.substring(0, 1), 10) < 8) {
bi = new BigInteger(hLength, 16)
} else {
bi = new BigInteger(hLength.substring(2), 16)
}
return bi.intValue()
}
/**
* get ASN.1 value starting string position for ASN.1 object refered by index 'idx'.
*/
function getStartPosOfV(s, pos) {
const lLen = getByteLengthOfL(s, pos)
if (lLen < 0) return lLen
return pos + (lLen + 1) * 2
}
/**
* get hexadecimal string of ASN.1 V(value)
*/
function getHexOfV(s, pos) {
const pos1 = getStartPosOfV(s, pos)
const len = getIntOfL(s, pos)
return s.substring(pos1, pos1 + len * 2)
}
/**
* get next sibling starting index for ASN.1 object string
*/
function getPosOfNextSibling(s, pos) {
const pos1 = getStartPosOfV(s, pos)
const len = getIntOfL(s, pos)
return pos1 + len * 2
}
/**
* get array of indexes of child ASN.1 objects
*/
function getPosArrayOfChildren(h, pos) {
const a = []
const p0 = getStartPosOfV(h, pos)
a.push(p0)
const len = getIntOfL(h, pos)
let p = p0
let k = 0
for (;;) {
const pNext = getPosOfNextSibling(h, p)
if (pNext == null || (pNext - p0 >= (len * 2))) break
if (k >= 200) break
a.push(pNext)
p = pNext
k++
}
return a
}
module.exports = {
/**
* ASN.1 DER编码
*/
encodeDer(r, s) {
const derR = new DERInteger({bigint: r})
const derS = new DERInteger({bigint: s})
const derSeq = new DERSequence({array: [derR, derS]})
return derSeq.getEncodedHex()
},
/**
* 解析 ASN.1 DER
*/
decodeDer(input) {
// 1. Items of ASN.1 Sequence Check
const a = getPosArrayOfChildren(input, 0)
// 2. Integer check
const iTLV1 = a[0]
const iTLV2 = a[1]
// 3. getting value
const hR = getHexOfV(input, iTLV1)
const hS = getHexOfV(input, iTLV2)
const r = new BigInteger(hR, 16)
const s = new BigInteger(hS, 16)
return {r, s}
}
}