aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/peertubeCrypto.js
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-03-07 11:33:59 +0100
committerChocobozzz <florian.bigard@gmail.com>2016-03-07 11:33:59 +0100
commitb9a3e09ad5a7673f64556d1dba122ed4c4fac980 (patch)
tree66d4928b82af19a2372a2505822233884f3fd471 /server/helpers/peertubeCrypto.js
parentb2ff5e3e686eb552c5ccd64ce67b0455972ceef0 (diff)
downloadPeerTube-b9a3e09ad5a7673f64556d1dba122ed4c4fac980.tar.gz
PeerTube-b9a3e09ad5a7673f64556d1dba122ed4c4fac980.tar.zst
PeerTube-b9a3e09ad5a7673f64556d1dba122ed4c4fac980.zip
Prepare folders structure for angular app
Diffstat (limited to 'server/helpers/peertubeCrypto.js')
-rw-r--r--server/helpers/peertubeCrypto.js147
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
3var config = require('config')
4var crypto = require('crypto')
5var fs = require('fs')
6var openssl = require('openssl-wrapper')
7var path = require('path')
8var ursa = require('ursa')
9
10var logger = require('./logger')
11
12var certDir = path.join(__dirname, '..', config.get('storage.certs'))
13var algorithm = 'aes-256-ctr'
14
15var peertubeCrypto = {
16 checkSignature: checkSignature,
17 createCertsIfNotExist: createCertsIfNotExist,
18 decrypt: decrypt,
19 encrypt: encrypt,
20 getCertDir: getCertDir,
21 sign: sign
22}
23
24function 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
30function 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
42function 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
54function 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
70function getCertDir () {
71 return certDir
72}
73
74function 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
83module.exports = peertubeCrypto
84
85// ---------------------------------------------------------------------------
86
87function certsExist (callback) {
88 fs.exists(certDir + 'peertube.key.pem', function (exists) {
89 return callback(exists)
90 })
91}
92
93function 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
123function 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
131function 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
138function 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}