X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fhelpers%2Fpeertube-crypto.js;h=610cb16cdbaec505dc0379bb5bfc679fdcaf86d4;hb=b981a525c37d226b3fa59287a6ce338f54583d0c;hp=46dff8d034df69df5ce7e751cabc7d713d2f973a;hpb=5f698b82c7055df763f3830882ac5bad1397db23;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/helpers/peertube-crypto.js b/server/helpers/peertube-crypto.js index 46dff8d03..610cb16cd 100644 --- a/server/helpers/peertube-crypto.js +++ b/server/helpers/peertube-crypto.js @@ -1,24 +1,19 @@ 'use strict' -const config = require('config') -const crypto = require('crypto') +const bcrypt = require('bcrypt') const fs = require('fs') const openssl = require('openssl-wrapper') -const path = require('path') const ursa = require('ursa') +const constants = require('../initializers/constants') const logger = require('./logger') -const certDir = path.join(__dirname, '..', '..', config.get('storage.certs')) -const algorithm = 'aes-256-ctr' - const peertubeCrypto = { - checkSignature: checkSignature, - createCertsIfNotExist: createCertsIfNotExist, - decrypt: decrypt, - encrypt: encrypt, - getCertDir: getCertDir, - sign: sign + checkSignature, + comparePassword, + createCertsIfNotExist, + cryptPassword, + sign } function checkSignature (publicKey, rawData, hexSignature) { @@ -27,6 +22,14 @@ function checkSignature (publicKey, rawData, hexSignature) { return isValid } +function comparePassword (plainPassword, hashPassword, callback) { + bcrypt.compare(plainPassword, hashPassword, function (err, isPasswordMatch) { + if (err) return callback(err) + + return callback(null, isPasswordMatch) + }) +} + function createCertsIfNotExist (callback) { certsExist(function (exist) { if (exist === true) { @@ -39,40 +42,18 @@ function createCertsIfNotExist (callback) { }) } -function decrypt (key, data, callback) { - fs.readFile(getCertDir() + 'peertube.key.pem', function (err, file) { +function cryptPassword (password, callback) { + bcrypt.genSalt(constants.BCRYPT_SALT_SIZE, function (err, salt) { if (err) return callback(err) - const myPrivateKey = ursa.createPrivateKey(file) - const decryptedKey = myPrivateKey.decrypt(key, 'hex', 'utf8') - const decryptedData = symetricDecrypt(data, decryptedKey) - - return callback(null, decryptedData) - }) -} - -function encrypt (publicKey, data, callback) { - const crt = ursa.createPublicKey(publicKey) - - symetricEncrypt(data, function (err, dataEncrypted) { - if (err) return callback(err) - - const key = crt.encrypt(dataEncrypted.password, 'utf8', 'hex') - const encrypted = { - data: dataEncrypted.crypted, - key: key - } - - callback(null, encrypted) + bcrypt.hash(password, salt, function (err, hash) { + return callback(err, hash) + }) }) } -function getCertDir () { - return certDir -} - function sign (data) { - const myKey = ursa.createPrivateKey(fs.readFileSync(certDir + 'peertube.key.pem')) + const myKey = ursa.createPrivateKey(fs.readFileSync(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem')) const signature = myKey.hashAndSign('sha256', data, 'utf8', 'hex') return signature @@ -85,7 +66,7 @@ module.exports = peertubeCrypto // --------------------------------------------------------------------------- function certsExist (callback) { - fs.exists(certDir + 'peertube.key.pem', function (exists) { + fs.exists(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem', function (exists) { return callback(exists) }) } @@ -99,15 +80,25 @@ function createCerts (callback) { } logger.info('Generating a RSA key...') - openssl.exec('genrsa', { 'out': certDir + 'peertube.key.pem', '2048': false }, function (err) { + + let options = { + 'out': constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem', + '2048': false + } + openssl.exec('genrsa', options, function (err) { if (err) { logger.error('Cannot create private key on this pod.') return callback(err) } logger.info('RSA key generated.') + options = { + 'in': constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem', + 'pubout': true, + 'out': constants.CONFIG.STORAGE.CERT_DIR + 'peertube.pub' + } logger.info('Manage public key...') - openssl.exec('rsa', { 'in': certDir + 'peertube.key.pem', 'pubout': true, 'out': certDir + 'peertube.pub' }, function (err) { + openssl.exec('rsa', options, function (err) { if (err) { logger.error('Cannot create public key on this pod.') return callback(err) @@ -119,29 +110,3 @@ function createCerts (callback) { }) }) } - -function generatePassword (callback) { - crypto.randomBytes(32, function (err, buf) { - if (err) return callback(err) - - callback(null, buf.toString('utf8')) - }) -} - -function symetricDecrypt (text, password) { - const decipher = crypto.createDecipher(algorithm, password) - let dec = decipher.update(text, 'hex', 'utf8') - dec += decipher.final('utf8') - return dec -} - -function symetricEncrypt (text, callback) { - generatePassword(function (err, password) { - if (err) return callback(err) - - const cipher = crypto.createCipher(algorithm, password) - let crypted = cipher.update(text, 'utf8', 'hex') - crypted += cipher.final('hex') - callback(null, { crypted: crypted, password: password }) - }) -}