Browse Source

rm asn1.js

tags/v1.0.0
AJ ONeal 11 months ago
parent
commit
2babbeb1dc
1 changed files with 0 additions and 235 deletions
  1. 0
    235
      lib/asn1.js

+ 0
- 235
lib/asn1.js View File

@@ -1,235 +0,0 @@
1
-'use strict';
2
-
3
-//
4
-// A dumbed-down, minimal ASN.1 parser / packer combo
5
-//
6
-// Note: generally I like to write congruent code
7
-// (i.e. output can be used as input and vice-versa)
8
-// However, this seemed to be more readable and easier
9
-// to use written as-is, asymmetrically.
10
-// (I also generally prefer to export objects rather
11
-// functions but, yet again, asthetics one in this case)
12
-
13
-var Enc = require('./encoding.js');
14
-
15
-//
16
-// Packer
17
-//
18
-
19
-// Almost every ASN.1 type that's important for CSR
20
-// can be represented generically with only a few rules.
21
-var ASN1 = module.exports = function ASN1(/*type, hexstrings...*/) {
22
-  var args = Array.prototype.slice.call(arguments);
23
-  var typ = args.shift();
24
-  var str = args.join('').replace(/\s+/g, '').toLowerCase();
25
-  var len = (str.length/2);
26
-  var lenlen = 0;
27
-  var hex = typ;
28
-
29
-  // We can't have an odd number of hex chars
30
-  if (len !== Math.round(len)) {
31
-    throw new Error("invalid hex");
32
-  }
33
-
34
-  // The first byte of any ASN.1 sequence is the type (Sequence, Integer, etc)
35
-  // The second byte is either the size of the value, or the size of its size
36
-
37
-  // 1. If the second byte is < 0x80 (128) it is considered the size
38
-  // 2. If it is > 0x80 then it describes the number of bytes of the size
39
-  //    ex: 0x82 means the next 2 bytes describe the size of the value
40
-  // 3. The special case of exactly 0x80 is "indefinite" length (to end-of-file)
41
-
42
-  if (len > 127) {
43
-    lenlen += 1;
44
-    while (len > 255) {
45
-      lenlen += 1;
46
-      len = len >> 8;
47
-    }
48
-  }
49
-
50
-  if (lenlen) { hex += Enc.numToHex(0x80 + lenlen); }
51
-  return hex + Enc.numToHex(str.length/2) + str;
52
-};
53
-
54
-// The Integer type has some special rules
55
-ASN1.UInt = function UINT() {
56
-  var str = Array.prototype.slice.call(arguments).join('');
57
-  var first = parseInt(str.slice(0, 2), 16);
58
-
59
-  // If the first byte is 0x80 or greater, the number is considered negative
60
-  // Therefore we add a '00' prefix if the 0x80 bit is set
61
-  if (0x80 & first) { str = '00' + str; }
62
-
63
-  return ASN1('02', str);
64
-};
65
-
66
-// The Bit String type also has a special rule
67
-ASN1.BitStr = function BITSTR() {
68
-  var str = Array.prototype.slice.call(arguments).join('');
69
-  // '00' is a mask of how many bits of the next byte to ignore
70
-  return ASN1('03', '00' + str);
71
-};
72
-
73
-
74
-//
75
-// Parser
76
-//
77
-
78
-ASN1.ELOOP = "uASN1.js Error: iterated over 15+ elements (probably a malformed file)";
79
-ASN1.EDEEP = "uASN1.js Error: element nested 20+ layers deep (probably a malformed file)";
80
-// Container Types are Sequence 0x30, Octect String 0x04, Array? (0xA0, 0xA1)
81
-// Value Types are Integer 0x02, Bit String 0x03, Null 0x05, Object ID 0x06,
82
-// Sometimes Bit String is used as a container (RSA Pub Spki)
83
-ASN1.VTYPES = [ 0x02, 0x03, 0x05, 0x06, 0x0c, 0x82 ];
84
-ASN1.parse = function parseAsn1(buf, depth, ws) {
85
-  if (!ws) { ws = ''; }
86
-  if (depth >= 20) { throw new Error(ASN1.EDEEP); }
87
-
88
-  var index = 2; // we know, at minimum, data starts after type (0) and lengthSize (1)
89
-  var asn1 = { type: buf[0], lengthSize: 0, length: buf[1] };
90
-  var child;
91
-  var iters = 0;
92
-  var adjust = 0;
93
-  var adjustedLen;
94
-
95
-  // Determine how many bytes the length uses, and what it is
96
-  if (0x80 & asn1.length) {
97
-    asn1.lengthSize = 0x7f & asn1.length;
98
-    // I think that buf->hex->int solves the problem of Endianness... not sure
99
-    asn1.length = parseInt(Enc.bufToHex(buf.slice(index, index + asn1.lengthSize)), 16);
100
-    index += asn1.lengthSize;
101
-  }
102
-
103
-  // High-order bit Integers have a leading 0x00 to signify that they are positive.
104
-  // Bit Streams use the first byte to signify padding, which x.509 doesn't use.
105
-  if (0x00 === buf[index] && (0x02 === asn1.type || 0x03 === asn1.type)) {
106
-    // However, 0x00 on its own is a valid number
107
-    if (asn1.length > 1) {
108
-      index += 1;
109
-      adjust = -1;
110
-    }
111
-  }
112
-  adjustedLen = asn1.length + adjust;
113
-
114
-  //console.warn(ws + '0x' + Enc.numToHex(asn1.type), index, 'len:', asn1.length, asn1);
115
-
116
-  // this is a primitive value type
117
-  if (-1 !== ASN1.VTYPES.indexOf(asn1.type)) {
118
-    asn1.value = buf.slice(index, index + adjustedLen);
119
-    return asn1;
120
-  }
121
-
122
-  asn1.children = [];
123
-  //console.warn('1 len:', (2 + asn1.lengthSize + asn1.length), 'idx:', index, 'clen:', 0);
124
-  while (iters < 15 && index < (2 + asn1.length + asn1.lengthSize)) {
125
-    iters += 1;
126
-    child = ASN1.parse(buf.slice(index, index + adjustedLen), (depth || 0) + 1, ws + '  ');
127
-    // The numbers don't match up exactly and I don't remember why...
128
-    // probably something with adjustedLen or some such, but the tests pass
129
-    index += (2 + child.lengthSize + child.length);
130
-    //console.warn('2 len:', (2 + asn1.lengthSize + asn1.length), 'idx:', index, 'clen:', (2 + child.lengthSize + child.length));
131
-    if (index > (2 + asn1.lengthSize + asn1.length)) {
132
-      console.error(JSON.stringify(asn1, function (k, v) {
133
-        if ('value' === k) { return '0x' + Enc.bufToHex(v.data); } return v;
134
-      }, 2));
135
-      throw new Error("Parse error: child value length (" + child.length
136
-        + ") is greater than remaining parent length (" + (asn1.length - index)
137
-        + " = " + asn1.length + " - " + index + ")");
138
-    }
139
-    asn1.children.push(child);
140
-    //console.warn(ws + '0x' + Enc.numToHex(asn1.type), index, 'len:', asn1.length, asn1);
141
-  }
142
-  if (index !== (2 + asn1.lengthSize + asn1.length)) {
143
-    console.warn('index:', index, 'length:', (2 + asn1.lengthSize + asn1.length))
144
-    throw new Error("premature end-of-file");
145
-  }
146
-  if (iters >= 15) { throw new Error(ASN1.ELOOP); }
147
-
148
-  return asn1;
149
-};
150
-
151
-/*
152
-ASN1._stringify = function(asn1) {
153
-  //console.log(JSON.stringify(asn1, null, 2));
154
-  //console.log(asn1);
155
-  var ws = '';
156
-
157
-  function write(asn1) {
158
-    console.log(ws, 'ch', Enc.numToHex(asn1.type), asn1.length);
159
-    if (!asn1.children) {
160
-      return;
161
-    }
162
-    asn1.children.forEach(function (a) {
163
-      ws += '\t';
164
-      write(a);
165
-      ws = ws.slice(1);
166
-    });
167
-  }
168
-  write(asn1);
169
-};
170
-*/
171
-
172
-ASN1.tpl = function (asn1) {
173
-  //console.log(JSON.stringify(asn1, null, 2));
174
-  //console.log(asn1);
175
-  var sp = '  ';
176
-  var ws = sp;
177
-  var i = 0;
178
-  var vars = [];
179
-  var str = ws;
180
-
181
-  function write(asn1, k) {
182
-    str += "\n" + ws;
183
-    var val;
184
-    if ('number' !== typeof k) {
185
-      // ignore
186
-    } else {
187
-      str += ', ';
188
-    }
189
-    if (0x02 === asn1.type) {
190
-      str += "ASN1.UInt(";
191
-    } else if (0x03 === asn1.type) {
192
-      str += "ASN1.BitStr(";
193
-    } else {
194
-      str += "ASN1('" + Enc.numToHex(asn1.type) + "'";
195
-    }
196
-    if (!asn1.children) {
197
-      if (0x05 !== asn1.type) {
198
-        if (0x06 !== asn1.type) {
199
-          val = asn1.value || new Uint8Array(0);
200
-          vars.push("\n// 0x" + Enc.numToHex(val.byteLength) + " (" + val.byteLength + " bytes)\nopts.tpl" + i + " = '"
201
-            + Enc.bufToHex(val) + "';");
202
-          if (0x02 !== asn1.type && 0x03 !== asn1.type) {
203
-            str += ", ";
204
-          }
205
-          str += "Enc.bufToHex(opts.tpl" + i + ")";
206
-        } else {
207
-          str += ", '" + Enc.bufToHex(asn1.value) + "'";
208
-        }
209
-      } else {
210
-        console.warn("XXXXXXXXXXXXXXXXXXXXX");
211
-      }
212
-      str += ")";
213
-      return ;
214
-    }
215
-    asn1.children.forEach(function (a, j) {
216
-      i += 1;
217
-      ws += sp;
218
-      write(a, j);
219
-      ws = ws.slice(sp.length);
220
-    });
221
-    str += "\n" + ws + ")";
222
-  }
223
-
224
-  write(asn1);
225
-  console.log('var opts = {};');
226
-  console.log(vars.join('\n') + '\n');
227
-  console.log();
228
-  console.log('function buildSchema(opts) {');
229
-  console.log(sp + 'return Enc.hexToBuf(' + str.slice(3) + ');');
230
-  console.log('}');
231
-  console.log();
232
-  console.log('buildSchema(opts);');
233
-};
234
-
235
-module.exports = ASN1;

Loading…
Cancel
Save