forked from rudonick/crypto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gostViewer.js
322 lines (302 loc) · 13.4 KB
/
gostViewer.js
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
* Copyright (c) 2014, Rudolf Nickolaev.
* All rights reserved.
*
* ASN.1 decode viewers
*
*/
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define(['gostCrypto', 'gostCoding', 'gostSecurity', 'gostASN1'], factory);
} else if (typeof exports === 'object') {
module.exports = factory(require('gostCrypto'), require('gostCoding'), require('gostSecurity'), require('gostASN1'));
} else {
root.gostViewer = factory(root.gostCrypto, root.gostCoding, root.gostSecurity, root.gostASN1);
}
}(this, function (gostCrypto) {
var coding = gostCrypto.coding, names = gostCrypto.security.names, asn1 = gostCrypto.asn1;
var printASN1 = (function () {
var BERtypes = {
0x00: 'EOC',
0x01: 'BOOLEAN',
0x02: 'INTEGER',
0x03: 'BIT STRING',
0x04: 'OCTET STRING',
0x05: 'NULL',
0x06: 'OBJECT IDENTIFIER',
0x07: 'ObjectDescriptor',
0x08: 'EXTERNAL',
0x09: 'REAL',
0x0A: 'ENUMERATED',
0x0B: 'EMBEDDED PDV',
0x0C: 'UTF8String',
0x10: 'SEQUENCE',
0x11: 'SET',
0x12: 'NumericString',
0x13: 'PrintableString', // ASCII subset
0x14: 'TeletexString', // aka T61String
0x15: 'VideotexString',
0x16: 'IA5String', // ASCII
0x17: 'UTCTime',
0x18: 'GeneralizedTime',
0x19: 'GraphicString',
0x1A: 'VisibleString', // ASCII subset
0x1B: 'GeneralString',
0x1C: 'UniversalString',
0x1E: 'BMPString'
};
function hex(start, buffer) {
var s = '', d = new Uint8Array(buffer);
for (var i = 0; i < d.length; i++) {
s += (i % 16 === 0 ? (i > 0 ? '</b>' : '') + '\r\n' + start + '<b>' : ' ') +
('00' + d[i].toString(16)).slice(-2);
}
return s ? s + '</b>' : '';
}
function childs(block, offset, ident) {
var s = '', start = ' : ';
if (block.object.length > 0) {
s += ' {\r\n';
block.object.forEach(function (child) {
s += process(child, offset, ident + ' ');
offset += child.header.length + child.content.length;
});
s += start + ident + '}';
} else
s += ' {}';
return s;
}
function process(block, offset, ident) {
if (block.tagClass > 2)
throw new Error('Private and Application tags is not supported')
offset = offset || 0;
ident = ident || '';
// type name
var typeName = block.tagClass === 1 ? 'Application_' + block.tagNumber :
block.tagClass === 2 ? '[' + block.tagNumber.toString() + ']' : // Context
block.tagClass === 3 ? 'Private_' + block.tagNumber :
BERtypes[block.tagNumber] || "Universal_" + block.tagNumber.toString();
var start = ' : ',
s = (' ' + offset).slice(-5) +
' ' + ('00' + block.header[0].toString(16)).slice(-2) +
(' ' + block.content.length).slice(-5) + ': ' +
ident + typeName;
if (block.tagConstructed)
s += childs(block, offset + block.header.length, ident);
else
switch (typeName.toUpperCase()) {
case 'OBJECT IDENTIFIER':
var id = block.object,
name = names[id];
s += ' <b>' + (name ? name : '?') + '</b> (' + id + ')';
break;
case 'INTEGER':
case 'ENUMERATED':
if (typeof block.object === 'number') {
s += ' <b>' + block.object + '</b>';
} else
s += hex(start + ident + ' ', block.content);
break;
case 'GENERALIZEDTIME':
case 'UTCTIME':
s += ' <b>' + block.object + '</b>';
break;
case 'PRINTABLESTRING':
case 'IA5STRING':
case 'VISIBLESTRING':
case 'VIDEOTEXSTRING':
case 'NUMERICSTRING':
case 'BMPSTRING':
case 'UTF8STRING':
case 'UNIVERSALSTRING':
s += ' <b>"' + block.object + '"</b>';
break;
case 'BOOLEAN':
s += ' <b>' + (block.object ? 'true' : 'false') + '</b>';
break;
case 'OCTET STRING':
try {
s += ', encapsulates' + childs({object: [coding.BER.decode(block.content)]},
offset + block.header.length, ident);
} catch (e) {
s += hex(start + ident + ' ', block.content);
}
break;
case 'BIT STRING':
s += ', unused ' + block.content[0] + ' bits';
if (block.object instanceof ArrayBuffer) {
try {
s += ', encapsulates' + childs({object: [coding.BER.decode(block.object)]},
offset + block.header.length, ident);
} catch (e) {
s += hex(start + ident + ' ', block.object);
}
} else
s += '\r\n' + start + ident + ' <b>' + block.object + 'B</b>';
break;
default:
try {
s += ', encapsulates' + childs({object: [coding.BER.decode(block.content)]},
offset + block.header.length, ident);
} catch (e) {
s += hex(start + ident + ' ', block.content);
}
}
return s + '\r\n';
}
return function (value) {
if (typeof value === 'string' || value instanceof String) { // text
var t = /([A-Fa-f0-9\s]+)/g.exec(value);
if (t && t[1].length === value.length) // Hex format
value = coding.Hex.decode(value);
else // PEM format
value = coding.PEM.decode(value);
} else { // binary
try {
value = coding.PEM.decode(coding.Chars.encode(value, 'ascii'));
} catch (e) {
}
}
return process(coding.BER.decode(value));
};
})();
var printSyntax = (function () {
function process(value, ident) {
ident = ident || '';
if (typeof value === 'undefined')
return '<b>undefined</b>';
else if (value instanceof Array) {
var r = [], l = 0;
for (var i = 0, n = value.length; i < n; i++) {
r[i] = process(value[i], ident + ' ');
l += r[i].replace(/\<[^\>]+\>/g, '').length + 2;
}
if (l > 80) {
var s = '[', m = 0;
for (var i = 0, n = r.length; i < n; i++) {
s += (i > 0 ? ', ' : '') +
(m === 0 && l > 80 ? '\r\n' + ident + ' ' : ' ') + r[i];
m += r[i].replace(/\<[^\>]+\>/g, '').length + 2;
if (m > 80)
m = 0;
}
s += l > 80 ? '\r\n' + ident + ']' : ' ]';
return s;
} else
return '[ ' + r.join(', ') + ' ]';
} else if (typeof value === 'string' || value instanceof String) {
if (value.toString() === '') // null
return '<b>null</b>';
else
return '<b>"' + value.replace(/([\"])/g, '\\$1') + '"</b>';
} else if (typeof value === 'number' || value instanceof Number) {
return '<b>' + value + '</b>';
} else if (value instanceof Date) {
return '<b>' + JSON.stringify(value) + '</b>';
} else if (typeof value === 'boolean' || value instanceof Boolean) {
return '<b>' + value + '</b>';
} else if (value instanceof ArrayBuffer || value instanceof Uint8Array) {
var d = new Uint8Array(value);
var s = '';
for (var i = 0, n = d.length; i < n; i++) {
s += (i > 0 && i % 16 === 0 ? '\r\n' + ident + ' ' : ' ') +
'<b>0x' + ('00' + d[i].toString(16)).slice(-2) + '</b>' +
(i < n - 1 ? ',' : '');
}
if (d.length > 16)
return '[\r\n' + ident + ' ' + s + '\r\n' + ident + ']';
else
return '[' + s + ' ]';
} else if (typeof value === 'object') {
var first = true;
var s = '{';
for (var name in value) {
if (typeof value[name] === 'function' || value[name] === undefined)
continue;
var norm = /^[a-zA-Z\_][a-zA-Z0-9\_]*$/.test(name);
if (first)
first = false;
else
s += ',';
s += '\r\n' + ident + ' ' +
(norm ? '' : '"') + name + (norm ? '' : '"') + ': ' +
process(value[name], ident + ' ');
}
s += (first ? '' : '\r\n' + ident) + '}';
return s;
}
return 'unrecognized';
}
return function (value, type) {
if (typeof value === 'string' || value instanceof String) { // text
var t = /([A-Fa-f0-9\s]+)/g.exec(value);
if (t && t[1].length === value.length) // Hex format
value = coding.Hex.decode(value);
else // PEM format
value = coding.PEM.decode(value);
} else { // binary
try {
value = coding.PEM.decode(coding.Chars.encode(value, 'ascii'));
} catch (e) {
}
}
if (type) {
return process(asn1[type].decode(value));
} else
return process(coding.BER.decode(value));
};
})();
function open(header, content, item) {
var el = document.getElementById('print');
if (el)
el.parentNode.removeChild(el);
el = document.createElement('div');
el.id = 'print';
el.innerHTML =
'<span class="label">' + header + '</span>' +
'<pre class="encoded">' + content + '</pre>' +
'<button onclick="(function(x){x.parentNode.removeChild(x);})(document.getElementById(\'print\'))">Close View</button>';
var next = item.nextElementSibling;
while (next.nodeName.toLowerCase() === 'button')
next = next.nextElementSibling;
next.parentNode.insertBefore(el, next);
}
function openASN1(item) {
open('ASN.1 Data', printASN1(item.textContent), item);
}
function openSyntax(item, type) {
open('Syntax ' + (type ? 'gostCrypto.asn1.' + type + '.decode(value):' :
'gostCrypto.coding.PEM.decode(value):'), printSyntax(item.textContent, type), item);
}
return {
printASN1: printASN1,
printSyntax: printSyntax,
openASN1: openASN1,
openSyntax: openSyntax
};
}));