X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fhelpers%2Fpeertube-crypto.js;h=ef6808d5ce1edf4dc54e89535babebe622dda8a2;hb=f282639b07deee1e35403bd86c46ea521fb4845e;hp=1ff638b04ecce144a71698c9eaffea2efcef8391;hpb=a6375e69668ea42e19531c6bc68dcd37f3f7cbd7;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/helpers/peertube-crypto.js b/server/helpers/peertube-crypto.js index 1ff638b04..ef6808d5c 100644 --- a/server/helpers/peertube-crypto.js +++ b/server/helpers/peertube-crypto.js @@ -1,32 +1,70 @@ 'use strict' -const bcrypt = require('bcrypt') const crypto = require('crypto') +const bcrypt = require('bcrypt') const fs = require('fs') const openssl = require('openssl-wrapper') -const ursa = require('ursa') +const pathUtils = require('path') const constants = require('../initializers/constants') const logger = require('./logger') -const algorithm = 'aes-256-ctr' - const peertubeCrypto = { checkSignature, comparePassword, createCertsIfNotExist, cryptPassword, - decrypt, - encrypt, + getMyPrivateCert, + getMyPublicCert, sign } -function checkSignature (publicKey, rawData, hexSignature) { - const crt = ursa.createPublicKey(publicKey) - const isValid = crt.hashAndVerify('sha256', new Buffer(rawData).toString('hex'), hexSignature, 'hex') +function checkSignature (publicKey, data, hexSignature) { + const verify = crypto.createVerify(constants.SIGNATURE_ALGORITHM) + + let dataString + if (typeof data === 'string') { + dataString = data + } else { + try { + dataString = JSON.stringify(data) + } catch (err) { + logger.error('Cannot check signature.', { error: err }) + return false + } + } + + verify.update(dataString, 'utf8') + + const isValid = verify.verify(publicKey, hexSignature, constants.SIGNATURE_ENCODING) return isValid } +function sign (data) { + const sign = crypto.createSign(constants.SIGNATURE_ALGORITHM) + + let dataString + if (typeof data === 'string') { + dataString = data + } else { + try { + dataString = JSON.stringify(data) + } catch (err) { + logger.error('Cannot sign data.', { error: err }) + return '' + } + } + + sign.update(dataString, 'utf8') + + // TODO: make async + 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 +} + function comparePassword (plainPassword, hashPassword, callback) { bcrypt.compare(plainPassword, hashPassword, function (err, isPasswordMatch) { if (err) return callback(err) @@ -57,39 +95,14 @@ function cryptPassword (password, callback) { }) } -function decrypt (key, data, callback) { - fs.readFile(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem', function (err, file) { - 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) - }) +function getMyPrivateCert (callback) { + const certPath = pathUtils.join(constants.CONFIG.STORAGE.CERT_DIR, constants.PRIVATE_CERT_NAME) + fs.readFile(certPath, 'utf8', callback) } -function sign (data) { - const myKey = ursa.createPrivateKey(fs.readFileSync(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem')) - const signature = myKey.hashAndSign('sha256', data, 'utf8', 'hex') - - return signature +function getMyPublicCert (callback) { + const certPath = pathUtils.join(constants.CONFIG.STORAGE.CERT_DIR, constants.PUBLIC_CERT_NAME) + fs.readFile(certPath, 'utf8', callback) } // --------------------------------------------------------------------------- @@ -99,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) }) } @@ -114,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) @@ -143,29 +160,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 }) - }) -}