Skip to content

Commit

Permalink
encoding: add JSON interface for asn1 and x509
Browse files Browse the repository at this point in the history
  • Loading branch information
pinheadmz committed Apr 20, 2020
1 parent 756b8be commit 7a8ff9c
Show file tree
Hide file tree
Showing 3 changed files with 363 additions and 6 deletions.
153 changes: 153 additions & 0 deletions lib/encoding/asn1.js
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,32 @@ class Any extends Node {
node: this.node
};
}

getJSON() {
if (this.opt && !this.force)
return null;

return {
type: this.node.constructor.name,
node: this.node.getJSON()
};
}

fromJSON(json) {
if (!json)
return null;

if (this.opt)
this.force = true;

const type = types[json.type.toUpperCase()];
const Node = typeToClass(type);
this.node = new Node();
this.node.fromJSON(json.node);
this.node.flags = this.flags;

return this.node;
}
}

/**
Expand Down Expand Up @@ -645,6 +671,23 @@ class Choice extends Node {
node: this.node
};
}

getJSON() {
return {
type: this.node.constructor.name,
node: this.node.getJSON()
};
}

fromJSON(json) {
const type = types[json.type.toUpperCase()];
const Node = typeToClass(type);
this.node = new Node();
this.node.fromJSON(json.node);
this.node.flags = this.flags;

return this.node;
}
}

/**
Expand Down Expand Up @@ -717,6 +760,17 @@ const Str = class String extends Node {
format() {
return `<${this.constructor.name}: ${this.value}>`;
}

getJSON() {
return this.value;
}

fromJSON(json) {
assert(typeof json === 'string');
this.value = json;

return this;
}
};

/**
Expand Down Expand Up @@ -775,6 +829,17 @@ const Bool = class Boolean extends Node {
format() {
return `<${this.constructor.name}: ${this.value}>`;
}

getJSON() {
return this.value;
}

fromJSON(json) {
assert(typeof json === 'boolean');
this.value = json;

return this;
}
};

/**
Expand Down Expand Up @@ -978,6 +1043,21 @@ class Integer extends Node {

return `<${name}: ${sign}0x${hex}>`;
}

getJSON() {
return {
value: this.value.toString('hex'),
negative: this.negative
};
}

fromJSON(json) {
assert(typeof json.negative === 'boolean');
this.value = Buffer.from(json.value, 'hex');
this.negative = json.negative;

return this;
}
}

/**
Expand Down Expand Up @@ -1019,6 +1099,14 @@ class Unsigned extends Integer {
assert(!this.negative);
return this;
}

getJSON() {
return this.toNumber();
}

fromJSON(json) {
return this.fromNumber(json);
}
}

/**
Expand Down Expand Up @@ -1144,6 +1232,21 @@ class BitString extends Node {

return `<${this.constructor.name}: ${this.bits}:${value.toString('hex')}>`;
}

getJSON() {
return {
bits: this.bits,
value: this.value.toString('hex')
};
}

fromJSON(json) {
assert(typeof json.bits === 'number');
this.bits = json.bits;
this.value = Buffer.from(json.value, 'hex');

return this;
}
}

/**
Expand Down Expand Up @@ -1198,6 +1301,16 @@ class OctString extends Node {

return `<${this.constructor.name}: ${value.toString('hex')}>`;
}

getJSON() {
return this.value.toString('hex');
}

fromJSON(json) {
this.value = Buffer.from(json, 'hex');

return this;
}
}

/**
Expand Down Expand Up @@ -1236,6 +1349,14 @@ class Null extends Node {
format() {
return `<${this.constructor.name}>`;
}

getJSON() {
return null;
}

fromJSON(json) {
return this;
}
}

/**
Expand Down Expand Up @@ -1460,6 +1581,22 @@ class OID extends Node {

return `<${this.constructor.name}: ${str}>`;
}

getJSON() {
const oid = this.toString();
const name = objects.attrsByVal[oid]
|| objects.sigAlgsByVal[oid]
|| objects.keyAlgsByVal[oid]
|| objects.hashesByVal[oid]
|| objects.curvesByVal[oid]
|| objects.extensionsByVal[oid]
|| this.toString();
return name;
}

fromJSON(json) {
return this.fromString(json);
}
}

/**
Expand Down Expand Up @@ -1580,6 +1717,14 @@ class RawSequence extends Node {
format() {
return this.toArray();
}

getJSON() {
throw new Error('Not implemented.');
}

fromJSON(json) {
throw new Error('Not implemented.');
}
}

/**
Expand Down Expand Up @@ -1727,6 +1872,14 @@ class Time extends Node {

return `<${name}: ${value}${off} (${this.toString()})>`;
}

getJSON() {
return this.toString();
}

fromJSON(json) {
return this.fromString(json);
}
}

/**
Expand Down
Loading

0 comments on commit 7a8ff9c

Please sign in to comment.