aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/peertubeCrypto.js
diff options
context:
space:
mode:
Diffstat (limited to 'server/helpers/peertubeCrypto.js')
-rw-r--r--server/helpers/peertubeCrypto.js147
1 files changed, 0 insertions, 147 deletions
diff --git a/server/helpers/peertubeCrypto.js b/server/helpers/peertubeCrypto.js
deleted file mode 100644
index 46dff8d03..000000000
--- a/server/helpers/peertubeCrypto.js
+++ /dev/null
@@ -1,147 +0,0 @@
1'use strict'
2
3const config = require('config')
4const crypto = require('crypto')
5const fs = require('fs')
6const openssl = require('openssl-wrapper')
7const path = require('path')
8const ursa = require('ursa')
9
10const logger = require('./logger')
11
12const certDir = path.join(__dirname, '..', '..', config.get('storage.certs'))
13const algorithm = 'aes-256-ctr'
14
15const peertubeCrypto = {
16 checkSignature: checkSignature,
17 createCertsIfNotExist: createCertsIfNotExist,
18 decrypt: decrypt,
19 encrypt: encrypt,
20 getCertDir: getCertDir,
21 sign: sign
22}
23
24function checkSignature (publicKey, rawData, hexSignature) {
25 const crt = ursa.createPublicKey(publicKey)
26 const isValid = crt.hashAndVerify('sha256', new Buffer(rawData).toString('hex'), hexSignature, 'hex')
27 return isValid
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 const myPrivateKey = ursa.createPrivateKey(file)
47 const decryptedKey = myPrivateKey.decrypt(key, 'hex', 'utf8')
48 const decryptedData = symetricDecrypt(data, decryptedKey)
49
50 return callback(null, decryptedData)
51 })
52}
53
54function encrypt (publicKey, data, callback) {
55 const crt = ursa.createPublicKey(publicKey)
56
57 symetricEncrypt(data, function (err, dataEncrypted) {
58 if (err) return callback(err)
59
60 const key = crt.encrypt(dataEncrypted.password, 'utf8', 'hex')
61 const 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 const myKey = ursa.createPrivateKey(fs.readFileSync(certDir + 'peertube.key.pem'))
76 const 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 const 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 const decipher = crypto.createDecipher(algorithm, password)
133 let 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 const cipher = crypto.createCipher(algorithm, password)
143 let crypted = cipher.update(text, 'utf8', 'hex')
144 crypted += cipher.final('hex')
145 callback(null, { crypted: crypted, password: password })
146 })
147}