-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
218 lines (192 loc) · 6.72 KB
/
index.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
/* eslint-disable valid-jsdoc */
'use strict';
const CFB = require('cfb');
const common = require('./src/util/common');
const ecma376Standard = require('./src/crypto/ecma376_standard');
const ecma376Agile = require('./src/crypto/ecma376_agile');
const xls97File = require('./src/util/xls97');
const doc97File = require('./src/util/doc97');
const ppt97File = require('./src/util/ppt97');
/**
*
* @param {*} input
* @param {*} options
* @returns
*/
async function decrypt(input, options) {
if (!Buffer.isBuffer(input)) {
// This is an ArrayBuffer in the browser. Convert to a Buffer.
if (ArrayBuffer.isView(input) || input instanceof ArrayBuffer) {
input = Buffer.from(input);
} else {
throw new Error('The input must be a buffer');
}
}
if (!options || !options.password) throw new Error('options.password is required');
const cfb = CFB.read(input, {type: 'buffer'});
const encryptionInfo = CFB.find(cfb, '/EncryptionInfo');
const password = options.password;
let output;
if (encryptionInfo) { // This is encrypted in xlsx format
const encryptedPackage = CFB.find(cfb, '/EncryptedPackage');
const einfo = common.parseEncryptionInfo(encryptionInfo.content);
if (einfo.type === 'standard') {
const {Flags, AlgID, AlgIDHash, KeySize, ProviderType} = einfo.h;
const {Salt, Verifier, VerifierHash} = einfo.v;
const saltSize = 16;
const key = ecma376Standard.convertPasswordToKey(password, AlgID, AlgIDHash, ProviderType, KeySize, saltSize, Salt);
const valid = ecma376Standard.verifyKey(key, Verifier, VerifierHash);
if (!valid) throw new Error('The password is incorrect');
output = ecma376Standard.decrypt(key, encryptedPackage.content);
return output;
}
if (einfo.type === 'agile') {
const data = {
encryptionInfoBuffer: encryptionInfo.content,
encryptedPackageBuffer: encryptedPackage.content,
};
output = await ecma376Agile.decrypt(data, password);
return output;
}
if (einfo.type === 'extensible') {
}
throw new Error('Unsupported encryption algorithms');
}
const Workbook = CFB.find(cfb, 'Workbook') || CFB.find(cfb, 'Book');
if (Workbook) {
let workbookContent = Workbook.content;
if (!Buffer.isBuffer(workbookContent)) {
workbookContent = Buffer.from(workbookContent);
CFB.utils.prep_blob(workbookContent, 0);
}
output = xls97File.decrypt(cfb, workbookContent, password, input);
return output;
}
const WordWorkbook = CFB.find(cfb, 'WordDocument');
if (WordWorkbook) {
let wordWorkbookContent = WordWorkbook.content;
if (!Buffer.isBuffer(wordWorkbookContent)) {
wordWorkbookContent = Buffer.from(wordWorkbookContent);
CFB.utils.prep_blob(wordWorkbookContent, 0);
}
output = doc97File.decrypt(cfb, wordWorkbookContent, password, input);
return output;
}
const PowerPointBook = CFB.find(cfb, 'PowerPoint Document');
if (PowerPointBook) {
let powerPointBookContent = PowerPointBook.content;
if (!Buffer.isBuffer(powerPointBookContent)) {
powerPointBookContent = Buffer.from(powerPointBookContent);
CFB.utils.prep_blob(powerPointBookContent, 0);
}
output = ppt97File.decrypt(cfb, powerPointBookContent, password, input);
return output;
}
if (!encryptionInfo) return input; // Not encrypted returns directly to the original buffer
throw new Error('Unsupported encryption algorithms');
}
/**
*
* @param {*} input
* @param {*} options
* @returns
*/
function encrypt(input, options) {
if (!Buffer.isBuffer(input)) {
// This is an ArrayBuffer in the browser. Convert to a Buffer.
if (ArrayBuffer.isView(input) || input instanceof ArrayBuffer) {
input = Buffer.from(input);
} else {
throw new Error('The input must be a buffer');
}
}
if (!options || !options.password) throw new Error('options.password is required');
const maxFieldLength = 255;
if (options.password.length > maxFieldLength) throw new Error(`The maximum password length is ${maxFieldLength}`);
let output;
if (options.hasOwnProperty('type') && !['standard'].includes(options.type)) {
throw new Error(`options.type must be ['standard']`);
}
if (options.type === 'standard') {
output = ecma376Standard.encryptStandard(input, options.password);
} else {
output = ecma376Agile.encrypt(input, options.password);
}
return output;
}
/**
*
* @param {*} input
* @returns
*/
function isEncrypted(input) {
if (!Buffer.isBuffer(input)) {
// This is an ArrayBuffer in the browser. Convert to a Buffer.
if (ArrayBuffer.isView(input) || input instanceof ArrayBuffer) {
input = Buffer.from(input);
} else {
throw new Error('The input must be a buffer');
}
}
const cfb = CFB.read(input, {type: 'buffer'});
const encryptionInfo = CFB.find(cfb, '/EncryptionInfo');
if (encryptionInfo) return true;
const Workbook = CFB.find(cfb, 'Workbook') || CFB.find(cfb, 'Book');
if (Workbook) {
let blob = Workbook.content;
if (!Buffer.isBuffer(blob)) {
blob = Buffer.from(blob);
CFB.utils.prep_blob(blob, 0);
}
const bof = blob.read_shift(2);
const bofSize = blob.read_shift(2);
blob.l = blob.l + bofSize; // -> skip BOF record
const record = blob.read_shift(2);
let filePass = record;
if (record === 134) { // 'WriteProtect': 134
// Skip if record is WriteProtect
const writeProtectSize = blob.read_shift(2);
filePass = blob.read_shift(2);
}
if (filePass === 47) { // 'FilePass': 47,
return true;
}
}
const WordDocument = CFB.find(cfb, 'WordDocument');
if (WordDocument) {
let blob = WordDocument.content;
if (!Buffer.isBuffer(blob)) {
blob = Buffer.from(blob);
CFB.utils.prep_blob(blob, 0);
}
const fibBase = doc97File.parseFibBase(blob);
const fEncrypted = fibBase.fEncrypted;
if (fEncrypted === 1) {
return true;
}
}
const PowerPointBook = CFB.find(cfb, 'PowerPoint Document');
if (PowerPointBook) {
let blob = PowerPointBook.content;
if (!Buffer.isBuffer(blob)) {
blob = Buffer.from(blob);
CFB.utils.prep_blob(blob, 0);
}
const CurrentUser = CFB.find(cfb, 'Current User');
let currentUserBlob = CurrentUser.content;
if (!Buffer.isBuffer(currentUserBlob)) {
currentUserBlob = Buffer.from(currentUserBlob);
CFB.utils.prep_blob(currentUserBlob, 0);
}
const currentUser = ppt97File.parseCurrentUser(currentUserBlob);
blob.l = currentUser.currentUserAtom.offsetToCurrentEdit;
const userEditAtom = ppt97File.parseUserEditAtom(blob);
if (userEditAtom.rh.recLen === 0x00000020) {
return true;
}
}
return false;
}
exports.decrypt = decrypt;
exports.encrypt = encrypt;
exports.isEncrypted = isEncrypted;