forked from tomyun/xeit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxeit.js
745 lines (657 loc) · 26.4 KB
/
xeit.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
var xeit = (function () {
"use strict";
/**********
* Vendor *
**********/
var Vendor = function (product) {
this.product = product || '';
this.sender = { name: '?', support: false, hint: '-', keylen: 0 };
};
Vendor.prototype = {
init: function () {},
peel: function (param, decode) {
param = (param) ? param.replace(/\n/g, '') : '';
return (decode) ? CryptoJS.enc.CP949.stringify(CryptoJS.enc.Base64.parse(param)) : param;
},
blob: function (contents) {
return {
src: CryptoJS.enc.Base64.parse(contents).toString(CryptoJS.enc.Latin1),
offset: 0,
read: function (length, trim) {
var trim = trim || false;
var start = this.offset;
var end = this.offset + length;
var value = this.src.slice(start, end);
this.offset = end;
return (trim) ? $.trim(value.split('\0')[0]) : value;
}
};
},
load: function (password) {
return this.render(this.decrypt(password));
},
decrypt: function (password) {
return '';
},
encode: function (content) {
// 메일 본문 인코딩 변환.
var message = CryptoJS.enc.Latin1.stringify(content);
if (message.match(/utf-8/i)) {
return CryptoJS.enc.Utf8.stringify(content);
} else {
message = CryptoJS.enc.CP949.stringify(content);
return message.replace(/euc-kr/ig, 'utf-8');
}
},
render: function (content) {
return this.encode(content);
}
};
/***************************
* SoftForum XecureExpress *
***************************/
var SoftForum = function (html, smime_header, smime_body, info_msg, ui_option, ui_desc) {
this.html = html || '';
this.smime_header = this.peel(smime_header, true);
this.smime_body = this.peel(smime_body);
this.info_msg = this.peel(info_msg, true);
this.ui_option = ui_option || '';
this.ui_desc = ui_desc || '';
};
SoftForum.prototype = new Vendor('XecureExpress');
$.extend(SoftForum.prototype, {
init: function () {
var contentType = this.smime_header.match(/Content-Type: \s*([\w-\/]+);*/i)[1];
if (contentType === 'application/pkcs7-mime') {
this.decrypt = function (password) {
return this.decryptSMIME(this.smime_body, password);
};
} else if (contentType === 'application/x-pwd') {
var match = this.smime_header.match(/X-XE_KEY: \s*([\d]+): \s*([\w+\/=]+);*/i);
var kind = parseInt(match[1]);
var key = match[2];
this.decrypt = function (password) {
return this.decryptPWD(kind, key, this.smime_body, password);
};
}
//HACK: 구분자가 '보안메일'로 동일한 발송기관 강제 구분.
var company = this.ui_desc;
if (company === '보안메일') {
if (this.html.indexOf('kbcard') > -1) {
company = 'Xeit.kbcard';
} else if (/(?=.*lottecard)(?=.*point)/.test(this.smime_header)) {
company = 'Xeit.lottepoint';
} else if (this.html.indexOf('samsungcard.co.kr') > -1) {
company = 'Xeit.samsungcard';
} else if (this.html.indexOf('uplus.co.kr') > -1) {
company = 'Xeit.uplus';
} else if (this.info_msg.indexOf('KEB') > -1) {
company = 'Xeit.yescard';
}
}
this.sender = {
'HyundaiCard': {
name: '현대카드',
support: true,
hint: '주민등록번호 뒤',
keylen: 7
},
'LG카드 보안메일': {
name: '신한카드 (구 LG카드)',
support: true,
hint: '주민등록번호 뒤',
keylen: 7
},
'TRUEFRIEND': {
name: '한국투자증권',
support: true,
hint: '주민등록번호 뒤',
keylen: 7
},
'Xeit.kbcard': {
name: 'KB국민카드',
support: true,
hint: '주민등록번호 뒤',
keylen: 7
},
'Xeit.lottepoint': {
name: '롯데포인트카드',
support: true,
hint: '주민등록번호 뒤',
keylen: 7
},
'Xeit.samsungcard': {
name: '삼성카드',
support: false,
hint: '-',
keylen: 0
},
'Xeit.uplus': {
name: 'LG유플러스',
support: true,
hint: '주민등록번호 뒤',
keylen: 7
},
'Xeit.yescard': {
name: '외환카드',
support: true,
hint: '주민등록번호 뒤',
keylen: 7,
render_hack: function (f, m) {
return {
'frame': f,
'message': m.replace(/href="#topmove"/g, '')
};
}
},
'신한카드 보안메일': {
name: '신한카드',
support: true,
hint: '주민등록번호 뒤',
keylen: 7
}
}[company] || ((company) ? $.extend({}, this.sender, { name: company }) : this.sender);
},
decryptSMIME: function (envelope, password) {
var ciphers = {
desCBC: CryptoJS.DES, // 1.3.14.3.2.7,
seedCBC: CryptoJS.SEED // 1.2.410.200004.1.4
};
ASN1.prototype.contentRaw = function () {
var offset = this.posContent();
var length = this.length;
return this.stream.parseStringISO(offset, offset + length);
};
var der = Base64.unarmor(envelope),
asn1 = ASN1.decode(der),
envelopedData = asn1.sub[1].sub[0];
// 주민등록번호로 암호화된 대칭키 복호화.
var recipientInfos = envelopedData.sub[1],
keyTransportRecipientInfo = recipientInfos.sub[0],
keyEncryptionAlgorithm = oids[keyTransportRecipientInfo.sub[2].sub[0].content()].d,
encryptedKey = CryptoJS.enc.Latin1.parse(keyTransportRecipientInfo.sub[3].contentRaw());
var passwordKey = CryptoJS.SHA1(password);
var iv = CryptoJS.enc.Hex.parse("0");
var decryptedKey = ciphers[keyEncryptionAlgorithm].decrypt(
{ ciphertext: encryptedKey },
passwordKey,
{ iv: iv }
);
this.verify(decryptedKey);
// 대칭키로 암호화된 메일 본문 복호화.
var encryptedContentInfo = envelopedData.sub[2],
contentEncryptionAlgorithm = oids[encryptedContentInfo.sub[1].sub[0].content()].d,
encryptedContent = CryptoJS.enc.Latin1.parse(encryptedContentInfo.sub[2].contentRaw());
var decryptedContent = ciphers[contentEncryptionAlgorithm].decrypt(
{ ciphertext: encryptedContent },
decryptedKey,
{ iv: iv }
);
return decryptedContent;
},
decryptPWD: function (kind, key, content, password) {
var ciphers = {
0: CryptoJS.DES,
10: CryptoJS.SEED
};
var encryptedKey = CryptoJS.enc.Base64.parse(key);
var passwordKey = CryptoJS.SHA1(password);
var iv = CryptoJS.enc.Hex.parse("0");
var decryptedKey = ciphers[kind].decrypt(
{ ciphertext: encryptedKey },
passwordKey,
{ iv: iv }
);
this.verify(decryptedKey);
var encryptedContent = CryptoJS.enc.Base64.parse(content);
var decryptedContent = ciphers[kind].decrypt(
{ ciphertext: encryptedContent },
decryptedKey,
{ iv: iv }
);
return decryptedContent;
},
verify: function (content) {
function value(index) {
return content.words[index >>> 2] >>> ((3 - index % 4) * 8) & 0xff;
}
var last = content.words.length * 4 - 1;
if (last > 0) {
var length = value(last);
if (0x01 <= length && length <= 0x10) {
var pads = [];
for (var i = 0; i < length; i++) {
pads.push(value(last - i));
}
if (pads.every(function (element, index, array) {
return (element == length);
})) {
return;
}
}
}
// PKCS#5/#7 padding이 잘못 되어 있으면 비밀번호 오류로 간주.
throw Error('다시 입력해보세요!');
},
render: function (content) {
var message = this.encode(content);
var frame = this.html;
//HACK: 남아 있는 email header 제거하여 HTML 시작 직전까지 잘라냄.
var offset = /(<!DOCTYPE|<html|<head|<body)/i.exec(message);
message = (offset) ? message.slice(offset.index) : message;
//HACK: 제대로 표시하려면 HTML 조작이 필요한 일부를 위해.
if (this.sender.render_hack) {
var fm = this.sender.render_hack(frame, message);
frame = fm['frame'];
message = fm['message'];
}
return message;
}
});
/************************
* IniTech INISAFE Mail *
************************/
var IniTech = function (html, contents, attachedFile, optData) {
this.html = html || '';
this.contents = this.peel(contents);
this.attachedFile = attachedFile || '';
this.optData = this.peel(optData);
};
IniTech.prototype = new Vendor('INISAFE Mail');
$.extend(IniTech.prototype, {
init: function () {
var S = this.unpack();
this.checkArea = S.checkArea;
this.dataArea = S.dataArea;
this.hasher = {
MD5: CryptoJS.MD5,
SHA1: CryptoJS.SHA1
}[S.hash];
this.cipher = {
DES: CryptoJS.DES,
SEED: CryptoJS.SEED
}[S.crypto[0]];
this.mode = {
CBC: CryptoJS.mode.CBC
}[S.crypto[1]];
this.padding = {
PKCS5Padding: CryptoJS.pad.Pkcs7
}[S.crypto[2]];
this.sender = {
BO: {
name: '신한은행',
support: true,
hint: '보안메일 비밀번호',
keylen: '6,8',
salt: 'shinhanbank'
},
CC: {
name: '우리은행 (BC카드)',
support: true,
hint: '주민등록번호 뒤',
keylen: 7,
salt: 'bccard',
render_hack: function (f, m) {
return {
'frame': f.replace('id="objHeader"', '$& style="display:none"'),
'message': m
};
}
},
TC: {
name: 'SKT',
support: true,
hint: '주민등록번호 앞 또는 뒤',
keylen: '6,7',
salt: 'SKT',
ignore_replacer: true
},
TH: {
name: 'KT',
support: true,
hint: '주민등록번호 뒤',
keylen: 7,
salt: 'ktbill'
}
}[S.company] || ((S.company) ? $.extend({}, this.sender, { name: S.company, hint: S.keygen })
: this.sender);
if (S.keygen == 'INITECH') {
this.iv = CryptoJS.enc.Latin1.parse(S.iv);
this.keygen = this.keygenINITECH;
} else if (S.keygen == 'PBKDF2') {
this.iv = CryptoJS.enc.Base64.parse(S.iv);
this.keygen = this.keygenPBKDF2;
}
},
unpack: function () {
var blob = this.blob(this.contents);
var struct = {
version: blob.read(9),
count: parseInt(blob.read(1), 10),
company: blob.read(2),
crypto: blob.read(25, true).split('/'),
hash: blob.read(20, true),
iv: blob.read(30),
keygen: blob.read(20, true),
checkAreaLength: parseInt(blob.read(10), 10),
dataAreaLength: parseInt(blob.read(10), 10)
};
struct.checkArea = CryptoJS.enc.Latin1.parse(blob.read(struct.checkAreaLength));
struct.dataArea = CryptoJS.enc.Latin1.parse(blob.read(struct.dataAreaLength));
return struct;
},
keygenINITECH: function (password) {
var saltedKey1 = this.sender.salt + '|' + password;
var hashedKey = CryptoJS.SHA1(CryptoJS.SHA1(CryptoJS.SHA1(saltedKey1)));
var saltedKey2 = this.sender.salt + password + hashedKey.toString(CryptoJS.enc.Latin1);
return this.hasher(CryptoJS.enc.Latin1.parse(saltedKey2));
},
keygenPBKDF2: function (password) {
return CryptoJS.PBKDF2(password, this.iv, { keySize: 128/32, iterations: 5139 });
},
decrypt: function (password) {
var key = this.keygen(password);
this.verify('Initech', key);
return this.cipher.decrypt(
{
ciphertext: this.dataArea
},
key,
{
iv: this.iv,
mode: this.mode,
padding: this.padding
}
);
},
verify: function (secret, key) {
if (this.cipher.decrypt(
{
ciphertext: this.checkArea
},
key,
{
iv: this.iv,
mode: this.mode,
padding: this.padding
}
).toString(CryptoJS.enc.Latin1) != secret) {
throw Error('다시 입력해보세요!');
}
},
render: function (content) {
var message = this.encode(content);
var frame = this.html.replace(
/<object [\s\S]*<\/object>/ig,
''
).replace(
/activeControl\([\s\S]*\);?/,
''
);
//HACK: 제대로 표시하려면 HTML 조작이 필요한 일부를 위해.
if (this.sender.render_hack) {
var fm = this.sender.render_hack(frame, message);
frame = fm['frame'];
message = fm['message'];
}
if (this.sender.ignore_replacer) {
var offset = /(<!DOCTYPE|<html|<head|<body)/i.exec(message);
if (offset) {
//HACK: 일부 메일 앞쪽의 알 수 없는 (암호화 관련?) 문자열 제거.
return message.slice(offset.index);
} else {
return message;
}
} else {
return frame.replace(
/id="InitechSMMsgToReplace">/,
'>' + message.replace(/\$/g, '$$$$')
);
}
}
});
/*****************************
* Soft25 JX-Mail Enterprise *
*****************************/
var Soft25 = function (html, contents) {
this.html = html || '';
this.contents = this.peel(contents);
};
Soft25.prototype = new Vendor('JX-Mail Enterprise');
$.extend(Soft25.prototype, {
init: function () {
var S = this.unpack();
$.extend(this, S);
this.sender = {
'병무청(동원담당)': {
name: '병무청',
support: false,
hint: '-',
keylen: 0
},
'KTF<cyberbill@ebill.ktfreetel.com>': {
name: 'KTF',
support: true,
hint: '주민등록번호 뒤',
keylen: 7
}
}[S.Sender] || ((S.Sender) ? $.extend({}, this.sender, { name: S.Sender, hint: S.ContentEncryptionAlgorithm })
: this.sender);
},
unpack: function () {
function ord(string, reverse) {
var reverse = reverse || false,
string = (reverse) ? string.split('').reverse().join('') : string;
var value = 0;
for (var i = 0; i < string.length; i++) {
value <<= 8
value += string.charCodeAt(i)
}
return value;
}
var blob = this.blob(this.contents);
var headerLength = ord(blob.read(2), true),
encryptedHeader = blob.read(headerLength),
decryptedHeader = '';
for (var i = 0; i < headerLength; i++) {
decryptedHeader += String.fromCharCode(encryptedHeader.charCodeAt(i) ^ 0x6b);
}
var struct = {},
params = decryptedHeader.split('\n');
$.each(params, function(i, param) {
var values = param.split(': ');
if (values[0]) {
struct[values[0]] = values[1];
}
});
$.each(struct, function(key, value) {
if (key.match(/Count|Offset|Size|Use$/)) {
struct[key] = (value.length > 0) ? parseInt(value) : '';
} else if (key.match(/HintKey|MailSubject|Sender$/)) {
value = $.trim(value);
var buffer = new ArrayBuffer(value.length),
bufferView = new Uint8Array(buffer);
for (var i = 0; i < value.length; i++) {
bufferView[i] = value.charCodeAt(i);
}
//HACK: couldn't import lib-typedarray.js from CryptoJS
//var wordArray = CryptoJS.lib.WordArray.create(bufferView);
var words = [];
for (var i = 0; i < bufferView.byteLength; i++) {
words[i >>> 2] |= bufferView[i] << (24 - (i % 4) * 8);
}
var wordArray = CryptoJS.lib.WordArray.create(words, bufferView.byteLength);
struct[key] = CryptoJS.enc.CP949.stringify(wordArray);
}
});
if (struct.SenderCertUse) {
struct.SenderCertData = CryptoJS.enc.Latin1.parse(blob.read(struct.SenderCertSize));
}
struct.OriginalFileData = CryptoJS.enc.Latin1.parse(blob.read(struct.OriginalFileSize));
return struct;
},
keygen: function (password) {
return CryptoJS.enc.Latin1.parse(password);
},
decrypt: function (password) {
var key = this.keygen(password);
this.verify('JXBLOCKMAIL!@#', key);
return CryptoJS.RC4.decrypt(
{
ciphertext: this.OriginalFileData
},
key
);
},
verify: function (secret, key) {
// HashKey, HeaderEncKey, ReceiverVid 중 가장 간단한 것으로 비교.
var hash = CryptoJS.MD5(key).toString(CryptoJS.enc.Hex);
if (hash != this.HashKey) {
throw Error('다시 입력해보세요!');
}
},
render: function (content) {
return this.encode(content);
}
});
/*********************
* Natingtel MailDec *
*********************/
var Natingtel = function (html, document_mail) {
this.html = html || '';
this.document_mail = this.peel(document_mail);
};
Natingtel.prototype = new Vendor('MailDec');
$.extend(Natingtel.prototype, {
init: function () {
this.sender = {
name: '대신증권',
support: true,
hint: '주민등록번호 뒤',
keylen: 7
};
},
keygen: function (password) {
var message = CryptoJS.enc.Latin1.parse('cybos family ...'),
key = CryptoJS.enc.Latin1.parse(('11111111111' + password).substring(0, 16));
return {
next: function () {
message = CryptoJS.SEED.encrypt(
message,
key,
{
padding: CryptoJS.pad.NoPadding,
mode: CryptoJS.mode.CBC,
iv: CryptoJS.enc.Hex.parse("0")
}
).ciphertext;
return message;
}
};
},
decrypt: function (password) {
var self = this;
var keyword = (function (password) {
var keygen = self.keygen(password),
key = keygen.next(),
i = 0;
return {
next: function () {
var keyword = key.words[i++];
if (i == key.words.length) {
key = keygen.next();
i = 0;
}
return keyword;
}
}
})(password);
var content = CryptoJS.enc.Base64.parse(this.document_mail),
length = content.words.length;
for (var i = 0; i < length; i++) {
content.words[i] ^= keyword.next();
}
this.verify(content, password);
//HACK: 뒷부분의 비밀번호 비교용 문자열 제거.
content.sigBytes -= password.length;
return content;
},
verify: function (content, password) {
//HACK: encode()의 stringify()와 중복.
if (CryptoJS.enc.CP949.stringify(content).slice(-password.length) != password) {
throw Error('다시 입력해보세요!');
}
}
});
return {
init: function (html) {
//HACK: <object> 태그의 상위 노드로써 DOM에 임시로 추가하여 query 수행.
var $doc = $('<div>', { id: 'Xeit-temp' }).hide().appendTo($('body')).append($.parseHTML(html));
if ($('#XEIViewer').length) {
this.vendor = new SoftForum(
html,
$('param[name="smime_header"]').val(),
$('param[name="smime_body"]').val(),
$('param[name="info_msg"]').val(),
$('param[name="ui_option"]').val(),
$('param[name="ui_desc"]').val()
);
} else if (/prtObj\(([\s\S])*\);/.test(html)) {
//TODO: LGU+ 인식용으로 기존 로직과 병합 가능성 확인 필요. (by RyanYoon)
var data = html.match(/prtObj\(([\s\S])*\);/)[0].match(/[^']+(?!,)/g);
this.vendor = new SoftForum(
html,
data[5],
data[7],
data[9],
data[11],
data[13]
);
} else if (html.indexOf('IniMasPlugin') > -1) {
//HACK: IE에서만 동작하는 activeControl() (function.js) 이슈 회피.
var body = html.replace(
/activeControl\(([\s]*['"])/,
"var activeControl = function (a, b, c) {" +
"var d = document.createElement('div');" +
"d.innerHTML = \"<OBJECT ID='IniMasPluginObj'>\" + a;" +
"d.firstChild.innerHTML = b + c;" +
"document.getElementById('embedControl').appendChild(d);" +
"}($1"
).replace(
/^[\s\S]*<body.*?>|<\/body>[\s\S]*$/ig,
''
);
$doc.empty().append($.parseHTML(body, document, true));
this.vendor = new IniTech(
html,
$('param[name="IniSMContents"]').val(),
$('param[name="AttachedFile"]').val()
);
} else if (html.indexOf('IniCrossMailObj') > -1) {
this.vendor = new IniTech(
html,
$('param[name="IniSMContents"]').val(),
$('param[name="AttachedFile"]').val(),
$('param[name="OptData"]').val()
);
} else if ($('#JXCEAL').length) {
this.vendor = new Soft25(
html,
$('#JSEncContents').val()
);
} else if ($('#MailDec').length) {
this.vendor = new Natingtel(
html,
$('param[name="DocumentMail"]').val()
);
} else {
this.vendor = new Vendor();
}
$doc.remove();
this.vendor.init();
},
load: function (password) {
return this.vendor.load(password);
}
};
})();