diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-02-05 18:03:20 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-02-05 18:03:20 +0100 |
commit | dac0a5319ab1c52a0958647b1593f85339b77e29 (patch) | |
tree | 716bc9e7e0b9fd3a7066530e50e73991fb278d05 /helpers/peertubeCrypto.js | |
parent | a5fa04b0cce851f680a45055f57ed2c3d22f1c82 (diff) | |
download | PeerTube-dac0a5319ab1c52a0958647b1593f85339b77e29.tar.gz PeerTube-dac0a5319ab1c52a0958647b1593f85339b77e29.tar.zst PeerTube-dac0a5319ab1c52a0958647b1593f85339b77e29.zip |
Split utils file
Diffstat (limited to 'helpers/peertubeCrypto.js')
-rw-r--r-- | helpers/peertubeCrypto.js | 152 |
1 files changed, 152 insertions, 0 deletions
diff --git a/helpers/peertubeCrypto.js b/helpers/peertubeCrypto.js new file mode 100644 index 000000000..3e757659b --- /dev/null +++ b/helpers/peertubeCrypto.js | |||
@@ -0,0 +1,152 @@ | |||
1 | ;(function () { | ||
2 | 'use strict' | ||
3 | |||
4 | var config = require('config') | ||
5 | var crypto = require('crypto') | ||
6 | var fs = require('fs') | ||
7 | var openssl = require('openssl-wrapper') | ||
8 | var ursa = require('ursa') | ||
9 | |||
10 | var logger = require('./logger') | ||
11 | |||
12 | var certDir = __dirname + '/../' + config.get('storage.certs') | ||
13 | var algorithm = 'aes-256-ctr' | ||
14 | |||
15 | var peertubeCrypto = { | ||
16 | checkSignature: checkSignature, | ||
17 | createCertsIfNotExist: createCertsIfNotExist, | ||
18 | decrypt: decrypt, | ||
19 | encrypt: encrypt, | ||
20 | getCertDir: getCertDir, | ||
21 | sign: sign | ||
22 | } | ||
23 | |||
24 | function checkSignature (public_key, raw_data, hex_signature) { | ||
25 | var crt = ursa.createPublicKey(public_key) | ||
26 | var is_valid = crt.hashAndVerify('sha256', new Buffer(raw_data).toString('hex'), hex_signature, 'hex') | ||
27 | return is_valid | ||
28 | } | ||
29 | |||
30 | function createCertsIfNotExist (callback) { | ||
31 | certsExist(function (exist) { | ||
32 | if (exist === true) { | ||
33 | return callback(null) | ||
34 | } | ||
35 | |||
36 | createCerts(function (err) { | ||
37 | return callback(err) | ||
38 | }) | ||
39 | }) | ||
40 | } | ||
41 | |||
42 | function decrypt (key, data, callback) { | ||
43 | fs.readFile(getCertDir() + 'peertube.key.pem', function (err, file) { | ||
44 | if (err) return callback(err) | ||
45 | |||
46 | var my_private_key = ursa.createPrivateKey(file) | ||
47 | var decrypted_key = my_private_key.decrypt(key, 'hex', 'utf8') | ||
48 | var decrypted_data = symetricDecrypt(data, decrypted_key) | ||
49 | |||
50 | return callback(null, decrypted_data) | ||
51 | }) | ||
52 | } | ||
53 | |||
54 | function encrypt (public_key, data, callback) { | ||
55 | var crt = ursa.createPublicKey(public_key) | ||
56 | |||
57 | symetricEncrypt(data, function (err, dataEncrypted) { | ||
58 | if (err) return callback(err) | ||
59 | |||
60 | var key = crt.encrypt(dataEncrypted.password, 'utf8', 'hex') | ||
61 | var encrypted = { | ||
62 | data: dataEncrypted.crypted, | ||
63 | key: key | ||
64 | } | ||
65 | |||
66 | callback(null, encrypted) | ||
67 | }) | ||
68 | } | ||
69 | |||
70 | function getCertDir () { | ||
71 | return certDir | ||
72 | } | ||
73 | |||
74 | function sign (data) { | ||
75 | var myKey = ursa.createPrivateKey(fs.readFileSync(certDir + 'peertube.key.pem')) | ||
76 | var signature = myKey.hashAndSign('sha256', data, 'utf8', 'hex') | ||
77 | |||
78 | return signature | ||
79 | } | ||
80 | |||
81 | // --------------------------------------------------------------------------- | ||
82 | |||
83 | module.exports = peertubeCrypto | ||
84 | |||
85 | // --------------------------------------------------------------------------- | ||
86 | |||
87 | function certsExist (callback) { | ||
88 | fs.exists(certDir + 'peertube.key.pem', function (exists) { | ||
89 | return callback(exists) | ||
90 | }) | ||
91 | } | ||
92 | |||
93 | function createCerts (callback) { | ||
94 | certsExist(function (exist) { | ||
95 | if (exist === true) { | ||
96 | var string = 'Certs already exist.' | ||
97 | logger.warning(string) | ||
98 | return callback(new Error(string)) | ||
99 | } | ||
100 | |||
101 | logger.info('Generating a RSA key...') | ||
102 | openssl.exec('genrsa', { 'out': certDir + 'peertube.key.pem', '2048': false }, function (err) { | ||
103 | if (err) { | ||
104 | logger.error('Cannot create private key on this pod.', { error: err }) | ||
105 | return callback(err) | ||
106 | } | ||
107 | logger.info('RSA key generated.') | ||
108 | |||
109 | logger.info('Manage public key...') | ||
110 | openssl.exec('rsa', { 'in': certDir + 'peertube.key.pem', 'pubout': true, 'out': certDir + 'peertube.pub' }, function (err) { | ||
111 | if (err) { | ||
112 | logger.error('Cannot create public key on this pod .', { error: err }) | ||
113 | return callback(err) | ||
114 | } | ||
115 | |||
116 | logger.info('Public key managed.') | ||
117 | return callback(null) | ||
118 | }) | ||
119 | }) | ||
120 | }) | ||
121 | } | ||
122 | |||
123 | function generatePassword (callback) { | ||
124 | crypto.randomBytes(32, function (err, buf) { | ||
125 | if (err) { | ||
126 | return callback(err) | ||
127 | } | ||
128 | |||
129 | callback(null, buf.toString('utf8')) | ||
130 | }) | ||
131 | } | ||
132 | |||
133 | function symetricDecrypt (text, password) { | ||
134 | var decipher = crypto.createDecipher(algorithm, password) | ||
135 | var dec = decipher.update(text, 'hex', 'utf8') | ||
136 | dec += decipher.final('utf8') | ||
137 | return dec | ||
138 | } | ||
139 | |||
140 | function symetricEncrypt (text, callback) { | ||
141 | generatePassword(function (err, password) { | ||
142 | if (err) { | ||
143 | return callback(err) | ||
144 | } | ||
145 | |||
146 | var cipher = crypto.createCipher(algorithm, password) | ||
147 | var crypted = cipher.update(text, 'utf8', 'hex') | ||
148 | crypted += cipher.final('hex') | ||
149 | callback(null, { crypted: crypted, password: password }) | ||
150 | }) | ||
151 | } | ||
152 | })() | ||