This repository has been archived by the owner on May 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Gcm #106
Merged
Merged
Gcm #106
Changes from 8 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
5099523
gcm encryption
abhinadduri 34c367c
added aad encryption
abhinadduri 4cb3484
use 128-bit GCM
dannycoates 05fe534
use header for file metadata
dannycoates 67f586b
format
dannycoates a11b4b6
updated storage tests
dannycoates 1ce24f7
id is now independent on iv
abhinadduri be470c6
added tagLength property to encrypt and decrypt for functionality in …
abhinadduri dc4682e
added checksums
abhinadduri 6f7930e
changed localstorage id's to match response from server, refactored m…
abhinadduri c4b7a2b
linting issues
abhinadduri 6aa7947
fixing small issues
abhinadduri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
const EventEmitter = require('events'); | ||
const { ivToStr } = require('./utils'); | ||
const { arrayToHex } = require('./utils'); | ||
|
||
const Raven = window.Raven; | ||
|
||
class FileSender extends EventEmitter { | ||
constructor(file) { | ||
super(); | ||
this.file = file; | ||
this.iv = window.crypto.getRandomValues(new Uint8Array(16)); | ||
this.iv = window.crypto.getRandomValues(new Uint8Array(12)); | ||
this.aad = window.crypto.getRandomValues(new Uint8Array(6)); | ||
} | ||
|
||
static delete(fileId, token) { | ||
|
@@ -37,14 +38,18 @@ class FileSender extends EventEmitter { | |
|
||
upload() { | ||
return Promise.all([ | ||
window.crypto.subtle.generateKey( | ||
{ | ||
name: 'AES-CBC', | ||
length: 128 | ||
}, | ||
true, | ||
['encrypt', 'decrypt'] | ||
), | ||
window.crypto.subtle | ||
.generateKey( | ||
{ | ||
name: 'AES-GCM', | ||
length: 128 | ||
}, | ||
true, | ||
['encrypt', 'decrypt'] | ||
) | ||
.catch(err => | ||
console.log('There was an error generating a crypto key') | ||
), | ||
new Promise((resolve, reject) => { | ||
const reader = new FileReader(); | ||
reader.readAsArrayBuffer(this.file); | ||
|
@@ -55,25 +60,28 @@ class FileSender extends EventEmitter { | |
]) | ||
.then(([secretKey, plaintext]) => { | ||
return Promise.all([ | ||
window.crypto.subtle.encrypt( | ||
{ | ||
name: 'AES-CBC', | ||
iv: this.iv | ||
}, | ||
secretKey, | ||
plaintext | ||
), | ||
window.crypto.subtle | ||
.encrypt( | ||
{ | ||
name: 'AES-GCM', | ||
iv: this.iv, | ||
additionalData: this.aad, | ||
tagLength: 128 | ||
}, | ||
secretKey, | ||
plaintext | ||
) | ||
.catch(err => console.log('Error with encrypting.')), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this needs to rethrow |
||
window.crypto.subtle.exportKey('jwk', secretKey) | ||
]); | ||
}) | ||
.then(([encrypted, keydata]) => { | ||
return new Promise((resolve, reject) => { | ||
const file = this.file; | ||
const fileId = ivToStr(this.iv); | ||
const fileId = arrayToHex(this.iv); | ||
const dataView = new DataView(encrypted); | ||
const blob = new Blob([dataView], { type: file.type }); | ||
const fd = new FormData(); | ||
fd.append('fname', file.name); | ||
fd.append('data', blob, file.name); | ||
|
||
const xhr = new XMLHttpRequest(); | ||
|
@@ -99,6 +107,14 @@ class FileSender extends EventEmitter { | |
}; | ||
|
||
xhr.open('post', '/upload/' + fileId, true); | ||
xhr.setRequestHeader( | ||
'X-File-Metadata', | ||
JSON.stringify({ | ||
aad: arrayToHex(this.aad), | ||
iv: fileId, | ||
filename: file.name | ||
}) | ||
); | ||
xhr.send(fd); | ||
}); | ||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,13 +79,14 @@ app.get('/assets/download/:id', (req, res) => { | |
} | ||
|
||
storage | ||
.filename(id) | ||
.then(reply => { | ||
.metadata(id) | ||
.then(meta => { | ||
storage.length(id).then(contentLength => { | ||
res.writeHead(200, { | ||
'Content-Disposition': 'attachment; filename=' + reply, | ||
'Content-Disposition': 'attachment; filename=' + meta.filename, | ||
'Content-Type': 'application/octet-stream', | ||
'Content-Length': contentLength | ||
'Content-Length': contentLength, | ||
'X-File-Metadata': JSON.stringify(meta) | ||
}); | ||
const file_stream = storage.get(id); | ||
|
||
|
@@ -136,20 +137,24 @@ app.post('/delete/:id', (req, res) => { | |
}); | ||
|
||
app.post('/upload/:id', (req, res, next) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We no longer need the |
||
if (!validateID(req.params.id)) { | ||
if (!validateIV(req.params.id)) { | ||
res.sendStatus(404); | ||
return; | ||
} | ||
|
||
const meta = JSON.parse(req.header('X-File-Metadata')); | ||
log.info('meta', meta); | ||
req.pipe(req.busboy); | ||
|
||
req.busboy.on('file', (fieldname, file, filename) => { | ||
log.info('Uploading:', req.params.id); | ||
|
||
const protocol = conf.env === 'production' ? 'https' : req.protocol; | ||
const url = `${protocol}://${req.get('host')}/download/${req.params.id}/`; | ||
|
||
storage.set(req.params.id, file, filename, url).then(linkAndID => { | ||
res.json(linkAndID); | ||
storage.set(req.params.id, file, filename, meta).then(([delete_token, new_id]) => { | ||
const protocol = conf.env === 'production' ? 'https' : req.protocol; | ||
const url = `${protocol}://${req.get('host')}/download/${new_id}/`; | ||
res.json({ | ||
url, | ||
delete: delete_token | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
@@ -171,5 +176,9 @@ app.listen(conf.listen_port, () => { | |
}); | ||
|
||
const validateID = route_id => { | ||
return route_id.match(/^[0-9a-fA-F]{32}$/) !== null; | ||
return route_id.match(/^[0-9a-fA-F]{10}$/) !== null; | ||
}; | ||
|
||
const validateIV = route_id => { | ||
return route_id.match(/^[0-9a-fA-F]{24}$/) !== null; | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
iv
is coming fromX-File-Metadata
now so we shouldn't need this anymore.