aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/peertube-crypto.js
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-08-19 21:34:51 +0200
committerChocobozzz <florian.bigard@gmail.com>2016-08-19 21:34:51 +0200
commite861452fb26553177ad4e32bfb18b4fd8a5b1816 (patch)
tree7c0cfd464709243a452b431665f5107a973df682 /server/helpers/peertube-crypto.js
parent5c39adb7313e0696aabb4b71196ab7b0b378c359 (diff)
downloadPeerTube-e861452fb26553177ad4e32bfb18b4fd8a5b1816.tar.gz
PeerTube-e861452fb26553177ad4e32bfb18b4fd8a5b1816.tar.zst
PeerTube-e861452fb26553177ad4e32bfb18b4fd8a5b1816.zip
Server: put config in constants
Diffstat (limited to 'server/helpers/peertube-crypto.js')
-rw-r--r--server/helpers/peertube-crypto.js29
1 files changed, 16 insertions, 13 deletions
diff --git a/server/helpers/peertube-crypto.js b/server/helpers/peertube-crypto.js
index 46dff8d03..ef130ea5c 100644
--- a/server/helpers/peertube-crypto.js
+++ b/server/helpers/peertube-crypto.js
@@ -1,15 +1,13 @@
1'use strict' 1'use strict'
2 2
3const config = require('config')
4const crypto = require('crypto') 3const crypto = require('crypto')
5const fs = require('fs') 4const fs = require('fs')
6const openssl = require('openssl-wrapper') 5const openssl = require('openssl-wrapper')
7const path = require('path')
8const ursa = require('ursa') 6const ursa = require('ursa')
9 7
8const constants = require('../initializers/constants')
10const logger = require('./logger') 9const logger = require('./logger')
11 10
12const certDir = path.join(__dirname, '..', '..', config.get('storage.certs'))
13const algorithm = 'aes-256-ctr' 11const algorithm = 'aes-256-ctr'
14 12
15const peertubeCrypto = { 13const peertubeCrypto = {
@@ -17,7 +15,6 @@ const peertubeCrypto = {
17 createCertsIfNotExist: createCertsIfNotExist, 15 createCertsIfNotExist: createCertsIfNotExist,
18 decrypt: decrypt, 16 decrypt: decrypt,
19 encrypt: encrypt, 17 encrypt: encrypt,
20 getCertDir: getCertDir,
21 sign: sign 18 sign: sign
22} 19}
23 20
@@ -40,7 +37,7 @@ function createCertsIfNotExist (callback) {
40} 37}
41 38
42function decrypt (key, data, callback) { 39function decrypt (key, data, callback) {
43 fs.readFile(getCertDir() + 'peertube.key.pem', function (err, file) { 40 fs.readFile(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem', function (err, file) {
44 if (err) return callback(err) 41 if (err) return callback(err)
45 42
46 const myPrivateKey = ursa.createPrivateKey(file) 43 const myPrivateKey = ursa.createPrivateKey(file)
@@ -67,12 +64,8 @@ function encrypt (publicKey, data, callback) {
67 }) 64 })
68} 65}
69 66
70function getCertDir () {
71 return certDir
72}
73
74function sign (data) { 67function sign (data) {
75 const myKey = ursa.createPrivateKey(fs.readFileSync(certDir + 'peertube.key.pem')) 68 const myKey = ursa.createPrivateKey(fs.readFileSync(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem'))
76 const signature = myKey.hashAndSign('sha256', data, 'utf8', 'hex') 69 const signature = myKey.hashAndSign('sha256', data, 'utf8', 'hex')
77 70
78 return signature 71 return signature
@@ -85,7 +78,7 @@ module.exports = peertubeCrypto
85// --------------------------------------------------------------------------- 78// ---------------------------------------------------------------------------
86 79
87function certsExist (callback) { 80function certsExist (callback) {
88 fs.exists(certDir + 'peertube.key.pem', function (exists) { 81 fs.exists(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem', function (exists) {
89 return callback(exists) 82 return callback(exists)
90 }) 83 })
91} 84}
@@ -99,15 +92,25 @@ function createCerts (callback) {
99 } 92 }
100 93
101 logger.info('Generating a RSA key...') 94 logger.info('Generating a RSA key...')
102 openssl.exec('genrsa', { 'out': certDir + 'peertube.key.pem', '2048': false }, function (err) { 95
96 let options = {
97 'out': constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem',
98 '2048': false
99 }
100 openssl.exec('genrsa', options, function (err) {
103 if (err) { 101 if (err) {
104 logger.error('Cannot create private key on this pod.') 102 logger.error('Cannot create private key on this pod.')
105 return callback(err) 103 return callback(err)
106 } 104 }
107 logger.info('RSA key generated.') 105 logger.info('RSA key generated.')
108 106
107 options = {
108 'in': constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem',
109 'pubout': true,
110 'out': constants.CONFIG.STORAGE.CERT_DIR + 'peertube.pub'
111 }
109 logger.info('Manage public key...') 112 logger.info('Manage public key...')
110 openssl.exec('rsa', { 'in': certDir + 'peertube.key.pem', 'pubout': true, 'out': certDir + 'peertube.pub' }, function (err) { 113 openssl.exec('rsa', options, function (err) {
111 if (err) { 114 if (err) {
112 logger.error('Cannot create public key on this pod.') 115 logger.error('Cannot create public key on this pod.')
113 return callback(err) 116 return callback(err)