2011-08-04 20:32:01 +00:00
|
|
|
// Copyright 2011 Mark Cavage, Inc. All rights reserved.
|
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
var assert = require('assert')
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
var asn1 = require('asn1')
|
2011-10-11 20:56:16 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
var Protocol = require('./protocol')
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
/// --- API
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
function Attribute (options) {
|
2011-08-04 20:32:01 +00:00
|
|
|
if (options) {
|
2019-08-27 21:11:49 +00:00
|
|
|
if (typeof (options) !== 'object') { throw new TypeError('options must be an object') }
|
|
|
|
if (options.type && typeof (options.type) !== 'string') { throw new TypeError('options.type must be a string') }
|
2011-08-04 20:32:01 +00:00
|
|
|
} else {
|
2019-08-27 21:11:49 +00:00
|
|
|
options = {}
|
2011-08-04 20:32:01 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
this.type = options.type || ''
|
|
|
|
this._vals = []
|
2011-10-11 20:56:16 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
if (options.vals !== undefined && options.vals !== null) { this.vals = options.vals }
|
2011-08-04 20:32:01 +00:00
|
|
|
}
|
2015-08-18 14:22:11 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
module.exports = Attribute
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2015-08-18 14:22:11 +00:00
|
|
|
Object.defineProperties(Attribute.prototype, {
|
|
|
|
buffers: {
|
2019-08-27 21:11:49 +00:00
|
|
|
get: function getBuffers () {
|
|
|
|
return this._vals
|
2015-08-18 14:22:11 +00:00
|
|
|
},
|
|
|
|
configurable: false
|
|
|
|
},
|
|
|
|
json: {
|
2019-08-27 21:11:49 +00:00
|
|
|
get: function getJson () {
|
2015-08-18 14:22:11 +00:00
|
|
|
return {
|
2015-10-28 02:21:25 +00:00
|
|
|
type: this.type,
|
|
|
|
vals: this.vals
|
2019-08-27 21:11:49 +00:00
|
|
|
}
|
2015-08-18 14:22:11 +00:00
|
|
|
},
|
|
|
|
configurable: false
|
|
|
|
},
|
|
|
|
vals: {
|
2019-08-27 21:11:49 +00:00
|
|
|
get: function getVals () {
|
|
|
|
var eType = _bufferEncoding(this.type)
|
2015-10-28 02:21:25 +00:00
|
|
|
return this._vals.map(function (v) {
|
2019-08-27 21:11:49 +00:00
|
|
|
return v.toString(eType)
|
|
|
|
})
|
2015-08-18 14:22:11 +00:00
|
|
|
},
|
2019-08-27 21:11:49 +00:00
|
|
|
set: function setVals (vals) {
|
|
|
|
var self = this
|
|
|
|
this._vals = []
|
2015-08-18 14:22:11 +00:00
|
|
|
if (Array.isArray(vals)) {
|
2015-10-28 02:21:25 +00:00
|
|
|
vals.forEach(function (v) {
|
2019-08-27 21:11:49 +00:00
|
|
|
self.addValue(v)
|
|
|
|
})
|
2015-08-18 14:22:11 +00:00
|
|
|
} else {
|
2019-08-27 21:11:49 +00:00
|
|
|
self.addValue(vals)
|
2015-08-18 14:22:11 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
configurable: false
|
2011-10-11 20:56:16 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
2015-10-28 02:21:25 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
Attribute.prototype.addValue = function addValue (val) {
|
2015-10-28 02:21:25 +00:00
|
|
|
if (Buffer.isBuffer(val)) {
|
2019-08-27 21:11:49 +00:00
|
|
|
this._vals.push(val)
|
2015-10-28 02:21:25 +00:00
|
|
|
} else {
|
2019-08-27 21:11:49 +00:00
|
|
|
this._vals.push(Buffer.from(val + '', _bufferEncoding(this.type)))
|
2015-10-28 02:21:25 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
}
|
2011-10-11 20:56:16 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
/* BEGIN JSSTYLED */
|
2019-08-27 21:11:49 +00:00
|
|
|
Attribute.compare = function compare (a, b) {
|
2014-07-17 18:33:02 +00:00
|
|
|
if (!(Attribute.isAttribute(a)) || !(Attribute.isAttribute(b))) {
|
2019-08-27 21:11:49 +00:00
|
|
|
throw new TypeError('can only compare Attributes')
|
2014-07-17 18:33:02 +00:00
|
|
|
}
|
2011-08-08 15:03:00 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
if (a.type < b.type) return -1
|
|
|
|
if (a.type > b.type) return 1
|
|
|
|
if (a.vals.length < b.vals.length) return -1
|
|
|
|
if (a.vals.length > b.vals.length) return 1
|
2011-08-08 15:03:00 +00:00
|
|
|
|
|
|
|
for (var i = 0; i < a.vals.length; i++) {
|
2019-08-27 21:11:49 +00:00
|
|
|
if (a.vals[i] < b.vals[i]) return -1
|
|
|
|
if (a.vals[i] > b.vals[i]) return 1
|
2011-08-08 15:03:00 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
return 0
|
|
|
|
}
|
2012-02-18 08:15:52 +00:00
|
|
|
/* END JSSTYLED */
|
2011-08-08 15:03:00 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
Attribute.prototype.parse = function parse (ber) {
|
|
|
|
assert.ok(ber)
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
ber.readSequence()
|
|
|
|
this.type = ber.readString()
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2011-09-23 22:47:02 +00:00
|
|
|
if (ber.peek() === Protocol.LBER_SET) {
|
|
|
|
if (ber.readSequence(Protocol.LBER_SET)) {
|
2019-08-27 21:11:49 +00:00
|
|
|
var end = ber.offset + ber.length
|
|
|
|
while (ber.offset < end) { this._vals.push(ber.readString(asn1.Ber.OctetString, true)) }
|
2011-09-23 22:47:02 +00:00
|
|
|
}
|
2011-08-04 20:32:01 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
return true
|
|
|
|
}
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
Attribute.prototype.toBer = function toBer (ber) {
|
|
|
|
assert.ok(ber)
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
ber.startSequence()
|
|
|
|
ber.writeString(this.type)
|
|
|
|
ber.startSequence(Protocol.LBER_SET)
|
2011-10-11 22:24:00 +00:00
|
|
|
if (this._vals.length) {
|
2012-02-18 08:15:52 +00:00
|
|
|
this._vals.forEach(function (b) {
|
2019-08-27 21:11:49 +00:00
|
|
|
ber.writeByte(asn1.Ber.OctetString)
|
|
|
|
ber.writeLength(b.length)
|
|
|
|
for (var i = 0; i < b.length; i++) { ber.writeByte(b[i]) }
|
|
|
|
})
|
2011-10-11 22:24:00 +00:00
|
|
|
} else {
|
2019-08-27 21:11:49 +00:00
|
|
|
ber.writeStringArray([])
|
2011-10-11 22:24:00 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
ber.endSequence()
|
|
|
|
ber.endSequence()
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
return ber
|
|
|
|
}
|
2014-07-17 18:33:02 +00:00
|
|
|
|
2015-10-28 02:21:25 +00:00
|
|
|
Attribute.prototype.toString = function () {
|
2019-08-27 21:11:49 +00:00
|
|
|
return JSON.stringify(this.json)
|
|
|
|
}
|
2015-10-28 02:21:25 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
Attribute.toBer = function (attr, ber) {
|
2019-08-27 21:11:49 +00:00
|
|
|
return Attribute.prototype.toBer.call(attr, ber)
|
|
|
|
}
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
Attribute.isAttribute = function isAttribute (attr) {
|
2014-07-17 18:33:02 +00:00
|
|
|
if (!attr || typeof (attr) !== 'object') {
|
2019-08-27 21:11:49 +00:00
|
|
|
return false
|
2012-01-24 17:43:46 +00:00
|
|
|
}
|
2014-07-17 18:33:02 +00:00
|
|
|
if (attr instanceof Attribute) {
|
2019-08-27 21:11:49 +00:00
|
|
|
return true
|
2014-07-17 18:33:02 +00:00
|
|
|
}
|
|
|
|
if ((typeof (attr.toBer) === 'function') &&
|
|
|
|
(typeof (attr.type) === 'string') &&
|
|
|
|
(Array.isArray(attr.vals)) &&
|
|
|
|
(attr.vals.filter(function (item) {
|
2019-08-27 21:11:49 +00:00
|
|
|
return (typeof (item) === 'string' ||
|
|
|
|
Buffer.isBuffer(item))
|
|
|
|
}).length === attr.vals.length)) {
|
|
|
|
return true
|
2014-07-17 18:33:02 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
return false
|
|
|
|
}
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
function _bufferEncoding (type) {
|
2015-08-18 14:22:11 +00:00
|
|
|
/* JSSTYLED */
|
2019-08-27 21:11:49 +00:00
|
|
|
return /;binary$/.test(type) ? 'base64' : 'utf8'
|
2015-08-18 14:22:11 +00:00
|
|
|
}
|