diff options
Diffstat (limited to 'server/helpers/peertubeCrypto.js')
-rw-r--r-- | server/helpers/peertubeCrypto.js | 147 |
1 files changed, 147 insertions, 0 deletions
diff --git a/server/helpers/peertubeCrypto.js b/server/helpers/peertubeCrypto.js new file mode 100644 index 000000000..29b9d79c9 --- /dev/null +++ b/server/helpers/peertubeCrypto.js | |||
@@ -0,0 +1,147 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | var config = require('config') | ||
4 | var crypto = require('crypto') | ||
5 | var fs = require('fs') | ||
6 | var openssl = require('openssl-wrapper') | ||
7 | var path = require('path') | ||
8 | var ursa = require('ursa') | ||
9 | |||
10 | var logger = require('./logger') | ||
11 | |||
12 | var certDir = path.join(__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.') | ||
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.') | ||
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) return callback(err) | ||
126 | |||
127 | callback(null, buf.toString('utf8')) | ||
128 | }) | ||
129 | } | ||
130 | |||
131 | function symetricDecrypt (text, password) { | ||
132 | var decipher = crypto.createDecipher(algorithm, password) | ||
133 | var dec = decipher.update(text, 'hex', 'utf8') | ||
134 | dec += decipher.final('utf8') | ||
135 | return dec | ||
136 | } | ||
137 | |||
138 | function symetricEncrypt (text, callback) { | ||
139 | generatePassword(function (err, password) { | ||
140 | if (err) return callback(err) | ||
141 | |||
142 | var cipher = crypto.createCipher(algorithm, password) | ||
143 | var crypted = cipher.update(text, 'utf8', 'hex') | ||
144 | crypted += cipher.final('hex') | ||
145 | callback(null, { crypted: crypted, password: password }) | ||
146 | }) | ||
147 | } | ||