wangzhibo
2026-05-09 53418655c061ac26f66058253f39200aced1ab1d
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// Copyright 2011 Mark Cavage <mcavage@gmail.com> All rights reserved.
 
const assert = require('assert')
const ASN1 = require('./types')
const errors = require('./errors')
 
// --- Globals
 
const newInvalidAsn1Error = errors.newInvalidAsn1Error
 
const DEFAULT_OPTS = {
  size: 1024,
  growthFactor: 8
}
 
// --- Helpers
 
function merge (from, to) {
  assert.ok(from)
  assert.equal(typeof (from), 'object')
  assert.ok(to)
  assert.equal(typeof (to), 'object')
 
  const keys = Object.getOwnPropertyNames(from)
  keys.forEach(function (key) {
    if (to[key]) { return }
 
    const value = Object.getOwnPropertyDescriptor(from, key)
    Object.defineProperty(to, key, value)
  })
 
  return to
}
 
// --- API
 
function Writer (options) {
  options = merge(DEFAULT_OPTS, options || {})
 
  this._buf = Buffer.alloc(options.size || 1024)
  this._size = this._buf.length
  this._offset = 0
  this._options = options
 
  // A list of offsets in the buffer where we need to insert
  // sequence tag/len pairs.
  this._seq = []
}
 
Object.defineProperty(Writer.prototype, Symbol.toStringTag, { value: 'BerWriter' })
 
Object.defineProperty(Writer.prototype, 'buffer', {
  get: function () {
    if (this._seq.length) { throw newInvalidAsn1Error(this._seq.length + ' unended sequence(s)') }
 
    return (this._buf.slice(0, this._offset))
  }
})
 
/**
 * Append a raw buffer to the current writer instance. No validation to
 * determine if the buffer represents a valid BER encoding is performed.
 *
 * @param {Buffer} buffer The buffer to append. If this is not a valid BER
 * sequence of data, it will invalidate the BER represented by the `BerWriter`.
 *
 * @throws If the input is not an instance of Buffer.
 */
Writer.prototype.appendBuffer = function appendBuffer (buffer) {
  if (Buffer.isBuffer(buffer) === false) {
    throw Error('buffer must be an instance of Buffer')
  }
  for (const b of buffer.values()) {
    this.writeByte(b)
  }
}
 
Writer.prototype.writeByte = function (b) {
  if (typeof (b) !== 'number') { throw new TypeError('argument must be a Number') }
 
  this._ensure(1)
  this._buf[this._offset++] = b
}
 
Writer.prototype.writeInt = function (i, tag) {
  if (typeof (i) !== 'number') { throw new TypeError('argument must be a Number') }
  if (typeof (tag) !== 'number') { tag = ASN1.Integer }
 
  let sz = 4
 
  while ((((i & 0xff800000) === 0) || ((i & 0xff800000) === 0xff800000 >> 0)) &&
        (sz > 1)) {
    sz--
    i <<= 8
  }
 
  if (sz > 4) { throw newInvalidAsn1Error('BER ints cannot be > 0xffffffff') }
 
  this._ensure(2 + sz)
  this._buf[this._offset++] = tag
  this._buf[this._offset++] = sz
 
  while (sz-- > 0) {
    this._buf[this._offset++] = ((i & 0xff000000) >>> 24)
    i <<= 8
  }
}
 
Writer.prototype.writeNull = function () {
  this.writeByte(ASN1.Null)
  this.writeByte(0x00)
}
 
Writer.prototype.writeEnumeration = function (i, tag) {
  if (typeof (i) !== 'number') { throw new TypeError('argument must be a Number') }
  if (typeof (tag) !== 'number') { tag = ASN1.Enumeration }
 
  return this.writeInt(i, tag)
}
 
Writer.prototype.writeBoolean = function (b, tag) {
  if (typeof (b) !== 'boolean') { throw new TypeError('argument must be a Boolean') }
  if (typeof (tag) !== 'number') { tag = ASN1.Boolean }
 
  this._ensure(3)
  this._buf[this._offset++] = tag
  this._buf[this._offset++] = 0x01
  this._buf[this._offset++] = b ? 0xff : 0x00
}
 
Writer.prototype.writeString = function (s, tag) {
  if (typeof (s) !== 'string') { throw new TypeError('argument must be a string (was: ' + typeof (s) + ')') }
  if (typeof (tag) !== 'number') { tag = ASN1.OctetString }
 
  const len = Buffer.byteLength(s)
  this.writeByte(tag)
  this.writeLength(len)
  if (len) {
    this._ensure(len)
    this._buf.write(s, this._offset)
    this._offset += len
  }
}
 
Writer.prototype.writeBuffer = function (buf, tag) {
  if (typeof (tag) !== 'number') { throw new TypeError('tag must be a number') }
  if (!Buffer.isBuffer(buf)) { throw new TypeError('argument must be a buffer') }
 
  this.writeByte(tag)
  this.writeLength(buf.length)
  this._ensure(buf.length)
  buf.copy(this._buf, this._offset, 0, buf.length)
  this._offset += buf.length
}
 
Writer.prototype.writeStringArray = function (strings) {
  if (Array.isArray(strings) === false) { throw new TypeError('argument must be an Array[String]') }
 
  const self = this
  strings.forEach(function (s) {
    self.writeString(s)
  })
}
 
// This is really to solve DER cases, but whatever for now
Writer.prototype.writeOID = function (s, tag) {
  if (typeof (s) !== 'string') { throw new TypeError('argument must be a string') }
  if (typeof (tag) !== 'number') { tag = ASN1.OID }
 
  if (!/^([0-9]+\.){3,}[0-9]+$/.test(s)) { throw new Error('argument is not a valid OID string') }
 
  function encodeOctet (bytes, octet) {
    if (octet < 128) {
      bytes.push(octet)
    } else if (octet < 16384) {
      bytes.push((octet >>> 7) | 0x80)
      bytes.push(octet & 0x7F)
    } else if (octet < 2097152) {
      bytes.push((octet >>> 14) | 0x80)
      bytes.push(((octet >>> 7) | 0x80) & 0xFF)
      bytes.push(octet & 0x7F)
    } else if (octet < 268435456) {
      bytes.push((octet >>> 21) | 0x80)
      bytes.push(((octet >>> 14) | 0x80) & 0xFF)
      bytes.push(((octet >>> 7) | 0x80) & 0xFF)
      bytes.push(octet & 0x7F)
    } else {
      bytes.push(((octet >>> 28) | 0x80) & 0xFF)
      bytes.push(((octet >>> 21) | 0x80) & 0xFF)
      bytes.push(((octet >>> 14) | 0x80) & 0xFF)
      bytes.push(((octet >>> 7) | 0x80) & 0xFF)
      bytes.push(octet & 0x7F)
    }
  }
 
  const tmp = s.split('.')
  const bytes = []
  bytes.push(parseInt(tmp[0], 10) * 40 + parseInt(tmp[1], 10))
  tmp.slice(2).forEach(function (b) {
    encodeOctet(bytes, parseInt(b, 10))
  })
 
  const self = this
  this._ensure(2 + bytes.length)
  this.writeByte(tag)
  this.writeLength(bytes.length)
  bytes.forEach(function (b) {
    self.writeByte(b)
  })
}
 
Writer.prototype.writeLength = function (len) {
  if (typeof (len) !== 'number') { throw new TypeError('argument must be a Number') }
 
  this._ensure(4)
 
  if (len <= 0x7f) {
    this._buf[this._offset++] = len
  } else if (len <= 0xff) {
    this._buf[this._offset++] = 0x81
    this._buf[this._offset++] = len
  } else if (len <= 0xffff) {
    this._buf[this._offset++] = 0x82
    this._buf[this._offset++] = len >> 8
    this._buf[this._offset++] = len
  } else if (len <= 0xffffff) {
    this._buf[this._offset++] = 0x83
    this._buf[this._offset++] = len >> 16
    this._buf[this._offset++] = len >> 8
    this._buf[this._offset++] = len
  } else {
    throw newInvalidAsn1Error('Length too long (> 4 bytes)')
  }
}
 
Writer.prototype.startSequence = function (tag) {
  if (typeof (tag) !== 'number') { tag = ASN1.Sequence | ASN1.Constructor }
 
  this.writeByte(tag)
  this._seq.push(this._offset)
  this._ensure(3)
  this._offset += 3
}
 
Writer.prototype.endSequence = function () {
  const seq = this._seq.pop()
  const start = seq + 3
  const len = this._offset - start
 
  if (len <= 0x7f) {
    this._shift(start, len, -2)
    this._buf[seq] = len
  } else if (len <= 0xff) {
    this._shift(start, len, -1)
    this._buf[seq] = 0x81
    this._buf[seq + 1] = len
  } else if (len <= 0xffff) {
    this._buf[seq] = 0x82
    this._buf[seq + 1] = len >> 8
    this._buf[seq + 2] = len
  } else if (len <= 0xffffff) {
    this._shift(start, len, 1)
    this._buf[seq] = 0x83
    this._buf[seq + 1] = len >> 16
    this._buf[seq + 2] = len >> 8
    this._buf[seq + 3] = len
  } else {
    throw newInvalidAsn1Error('Sequence too long')
  }
}
 
Writer.prototype._shift = function (start, len, shift) {
  assert.ok(start !== undefined)
  assert.ok(len !== undefined)
  assert.ok(shift)
 
  this._buf.copy(this._buf, start + shift, start, start + len)
  this._offset += shift
}
 
Writer.prototype._ensure = function (len) {
  assert.ok(len)
 
  if (this._size - this._offset < len) {
    let sz = this._size * this._options.growthFactor
    if (sz - this._offset < len) { sz += len }
 
    const buf = Buffer.alloc(sz)
 
    this._buf.copy(buf, 0, 0, this._offset)
    this._buf = buf
    this._size = sz
  }
}
 
// --- Exported API
 
module.exports = Writer