| 
									
										
										
										
											2017-11-14 21:54:12 -05:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-15 18:23:19 -05:00
										 |  |  | const crypto = require('crypto'); | 
					
						
							| 
									
										
										
										
											2017-12-30 20:14:05 -05:00
										 |  |  | const log = require('./log'); | 
					
						
							| 
									
										
										
										
											2017-11-10 22:55:19 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-15 22:13:45 -05:00
										 |  |  | function arraysIdentical(a, b) { | 
					
						
							|  |  |  |     let i = a.length; | 
					
						
							|  |  |  |     if (i !== b.length) return false; | 
					
						
							|  |  |  |     while (i--) { | 
					
						
							|  |  |  |         if (a[i] !== b[i]) return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function shaArray(content) { | 
					
						
							|  |  |  |     // we use this as simple checksum and don't rely on its security so SHA-1 is good enough
 | 
					
						
							| 
									
										
										
										
											2017-11-15 23:39:50 -05:00
										 |  |  |     return crypto.createHash('sha1').update(content).digest(); | 
					
						
							| 
									
										
										
										
											2017-11-15 22:13:45 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function pad(data) { | 
					
						
							| 
									
										
										
										
											2019-01-11 23:04:51 +01:00
										 |  |  |     if (data.length > 16) { | 
					
						
							|  |  |  |         data = data.slice(0, 16); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else if (data.length < 16) { | 
					
						
							|  |  |  |         const zeros = Array(16 - data.length).fill(0); | 
					
						
							| 
									
										
										
										
											2017-11-15 22:13:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-11 23:04:51 +01:00
										 |  |  |         data = Buffer.concat([data, Buffer.from(zeros)]); | 
					
						
							| 
									
										
										
										
											2017-11-15 18:23:19 -05:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-11-15 22:13:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-13 00:24:51 +01:00
										 |  |  |     return Buffer.from(data); | 
					
						
							| 
									
										
										
										
											2017-11-15 22:13:45 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-11 23:04:51 +01:00
										 |  |  | function encrypt(key, plainText, ivLength = 13) { | 
					
						
							| 
									
										
										
										
											2017-11-16 00:22:00 -05:00
										 |  |  |     if (!key) { | 
					
						
							| 
									
										
										
										
											2017-11-15 22:13:45 -05:00
										 |  |  |         throw new Error("No data key!"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-15 23:39:50 -05:00
										 |  |  |     const plainTextBuffer = Buffer.from(plainText); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-11 23:04:51 +01:00
										 |  |  |     const iv = crypto.randomBytes(ivLength); | 
					
						
							| 
									
										
										
										
											2017-11-16 00:22:00 -05:00
										 |  |  |     const cipher = crypto.createCipheriv('aes-128-cbc', pad(key), pad(iv)); | 
					
						
							| 
									
										
										
										
											2017-11-15 22:13:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-15 23:39:50 -05:00
										 |  |  |     const digest = shaArray(plainTextBuffer).slice(0, 4); | 
					
						
							| 
									
										
										
										
											2017-11-15 22:13:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-15 23:39:50 -05:00
										 |  |  |     const digestWithPayload = Buffer.concat([digest, plainTextBuffer]); | 
					
						
							| 
									
										
										
										
											2017-11-15 22:13:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-15 23:39:50 -05:00
										 |  |  |     const encryptedData = Buffer.concat([cipher.update(digestWithPayload), cipher.final()]); | 
					
						
							| 
									
										
										
										
											2017-11-15 22:13:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-11 23:04:51 +01:00
										 |  |  |     const encryptedDataWithIv = Buffer.concat([iv, encryptedData]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return encryptedDataWithIv.toString('base64'); | 
					
						
							| 
									
										
										
										
											2017-11-15 22:13:45 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-11 23:04:51 +01:00
										 |  |  | function decrypt(key, cipherText, ivLength = 13) { | 
					
						
							| 
									
										
										
										
											2017-11-16 00:22:00 -05:00
										 |  |  |     if (!key) { | 
					
						
							| 
									
										
										
										
											2017-11-15 22:13:45 -05:00
										 |  |  |         return "[protected]"; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-04 16:05:28 +02:00
										 |  |  |     try { | 
					
						
							|  |  |  |         const cipherTextBufferWithIv = Buffer.from(cipherText.toString(), 'base64'); | 
					
						
							|  |  |  |         const iv = cipherTextBufferWithIv.slice(0, ivLength); | 
					
						
							| 
									
										
										
										
											2019-01-11 23:04:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-04 16:05:28 +02:00
										 |  |  |         const cipherTextBuffer = cipherTextBufferWithIv.slice(ivLength); | 
					
						
							| 
									
										
										
										
											2019-01-11 23:04:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-04 16:05:28 +02:00
										 |  |  |         const decipher = crypto.createDecipheriv('aes-128-cbc', pad(key), pad(iv)); | 
					
						
							| 
									
										
										
										
											2017-11-15 23:39:50 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-04 16:05:28 +02:00
										 |  |  |         const decryptedBytes = Buffer.concat([decipher.update(cipherTextBuffer), decipher.final()]); | 
					
						
							| 
									
										
										
										
											2017-11-15 23:39:50 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-04 16:05:28 +02:00
										 |  |  |         const digest = decryptedBytes.slice(0, 4); | 
					
						
							|  |  |  |         const payload = decryptedBytes.slice(4); | 
					
						
							| 
									
										
										
										
											2017-11-15 22:13:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-04 16:05:28 +02:00
										 |  |  |         const computedDigest = shaArray(payload).slice(0, 4); | 
					
						
							| 
									
										
										
										
											2017-11-15 22:13:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-04 16:05:28 +02:00
										 |  |  |         if (!arraysIdentical(digest, computedDigest)) { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-11-15 22:13:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-04 16:05:28 +02:00
										 |  |  |         return payload; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     catch (e) { | 
					
						
							|  |  |  |         // recovery from https://github.com/zadam/trilium/issues/510
 | 
					
						
							|  |  |  |         if (e.message && e.message.includes("WRONG_FINAL_BLOCK_LENGTH")) { | 
					
						
							|  |  |  |             log.info("Caught WRONG_FINAL_BLOCK_LENGTH, returning cipherText instead"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return cipherText; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             throw e; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-11-15 22:13:45 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-11 23:04:51 +01:00
										 |  |  | function decryptString(dataKey, cipherText) { | 
					
						
							|  |  |  |     const buffer = decrypt(dataKey, cipherText); | 
					
						
							| 
									
										
										
										
											2017-11-15 23:39:50 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-30 20:14:05 -05:00
										 |  |  |     const str = buffer.toString('utf-8'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (str === 'false') { | 
					
						
							|  |  |  |         log.error("Could not decrypt string. Buffer: " + buffer); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         throw new Error("Could not decrypt string."); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return str; | 
					
						
							| 
									
										
										
										
											2017-11-15 23:39:50 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-10 22:55:19 -05:00
										 |  |  | module.exports = { | 
					
						
							| 
									
										
										
										
											2017-11-18 12:53:17 -05:00
										 |  |  |     encrypt, | 
					
						
							|  |  |  |     decrypt, | 
					
						
							| 
									
										
										
										
											2019-01-11 23:04:51 +01:00
										 |  |  |     decryptString | 
					
						
							| 
									
										
										
										
											2017-11-10 22:55:19 -05:00
										 |  |  | }; |