X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fhelpers%2Fpeertube-crypto.js;h=ef6808d5ce1edf4dc54e89535babebe622dda8a2;hb=c1a7ab7f04fdb1601cf1e41c4e372dbd3c81f3de;hp=0f1e02ad6babb6f6364cedcba89fbe6853ffcd4c;hpb=99fe265a5fc077cb66c322e7f3d191ff7110aea0;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/helpers/peertube-crypto.js b/server/helpers/peertube-crypto.js index 0f1e02ad6..ef6808d5c 100644 --- a/server/helpers/peertube-crypto.js +++ b/server/helpers/peertube-crypto.js @@ -4,6 +4,7 @@ const crypto = require('crypto') const bcrypt = require('bcrypt') const fs = require('fs') const openssl = require('openssl-wrapper') +const pathUtils = require('path') const constants = require('../initializers/constants') const logger = require('./logger') @@ -13,6 +14,8 @@ const peertubeCrypto = { comparePassword, createCertsIfNotExist, cryptPassword, + getMyPrivateCert, + getMyPublicCert, sign } @@ -55,7 +58,8 @@ function sign (data) { sign.update(dataString, 'utf8') // TODO: make async - const myKey = fs.readFileSync(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem') + const certPath = pathUtils.join(constants.CONFIG.STORAGE.CERT_DIR, constants.PRIVATE_CERT_NAME) + const myKey = fs.readFileSync(certPath) const signature = sign.sign(myKey, constants.SIGNATURE_ENCODING) return signature @@ -91,6 +95,16 @@ function cryptPassword (password, callback) { }) } +function getMyPrivateCert (callback) { + const certPath = pathUtils.join(constants.CONFIG.STORAGE.CERT_DIR, constants.PRIVATE_CERT_NAME) + fs.readFile(certPath, 'utf8', callback) +} + +function getMyPublicCert (callback) { + const certPath = pathUtils.join(constants.CONFIG.STORAGE.CERT_DIR, constants.PUBLIC_CERT_NAME) + fs.readFile(certPath, 'utf8', callback) +} + // --------------------------------------------------------------------------- module.exports = peertubeCrypto @@ -98,7 +112,8 @@ module.exports = peertubeCrypto // --------------------------------------------------------------------------- function certsExist (callback) { - fs.exists(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem', function (exists) { + const certPath = pathUtils.join(constants.CONFIG.STORAGE.CERT_DIR, constants.PRIVATE_CERT_NAME) + fs.exists(certPath, function (exists) { return callback(exists) }) } @@ -113,24 +128,27 @@ function createCerts (callback) { logger.info('Generating a RSA key...') - let options = { - 'out': constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem', + const privateCertPath = pathUtils.join(constants.CONFIG.STORAGE.CERT_DIR, constants.PRIVATE_CERT_NAME) + const genRsaOptions = { + 'out': privateCertPath, '2048': false } - openssl.exec('genrsa', options, function (err) { + openssl.exec('genrsa', genRsaOptions, function (err) { if (err) { logger.error('Cannot create private key on this pod.') return callback(err) } + logger.info('RSA key generated.') + logger.info('Managing public key...') - options = { - 'in': constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem', + const publicCertPath = pathUtils.join(constants.CONFIG.STORAGE.CERT_DIR, 'peertube.pub') + const rsaOptions = { + 'in': privateCertPath, 'pubout': true, - 'out': constants.CONFIG.STORAGE.CERT_DIR + 'peertube.pub' + 'out': publicCertPath } - logger.info('Manage public key...') - openssl.exec('rsa', options, function (err) { + openssl.exec('rsa', rsaOptions, function (err) { if (err) { logger.error('Cannot create public key on this pod.') return callback(err)