PeerTube/server/helpers/peertube-crypto.js

172 lines
4.5 KiB
JavaScript
Raw Normal View History

'use strict'
2016-08-25 10:57:37 -05:00
const bcrypt = require('bcrypt')
2016-03-16 16:29:27 -05:00
const crypto = require('crypto')
const fs = require('fs')
const openssl = require('openssl-wrapper')
const ursa = require('ursa')
2016-08-19 14:34:51 -05:00
const constants = require('../initializers/constants')
2016-03-16 16:29:27 -05:00
const logger = require('./logger')
2016-03-16 16:29:27 -05:00
const algorithm = 'aes-256-ctr'
2016-03-16 16:29:27 -05:00
const peertubeCrypto = {
checkSignature: checkSignature,
2016-08-25 10:57:37 -05:00
comparePassword: comparePassword,
createCertsIfNotExist: createCertsIfNotExist,
2016-08-25 10:57:37 -05:00
cryptPassword: cryptPassword,
decrypt: decrypt,
encrypt: encrypt,
sign: sign
}
function checkSignature (publicKey, rawData, hexSignature) {
const crt = ursa.createPublicKey(publicKey)
const isValid = crt.hashAndVerify('sha256', new Buffer(rawData).toString('hex'), hexSignature, 'hex')
return isValid
}
2016-08-25 10:57:37 -05:00
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) {
return callback(null)
}
createCerts(function (err) {
return callback(err)
2016-02-05 11:03:20 -06:00
})
})
}
2016-02-05 11:03:20 -06:00
2016-08-25 10:57:37 -05:00
function cryptPassword (password, callback) {
bcrypt.genSalt(constants.BCRYPT_SALT_SIZE, function (err, salt) {
if (err) return callback(err)
bcrypt.hash(password, salt, function (err, hash) {
return callback(err, hash)
})
})
}
function decrypt (key, data, callback) {
2016-08-19 14:34:51 -05:00
fs.readFile(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem', function (err, file) {
if (err) return callback(err)
2016-02-05 11:03:20 -06:00
const myPrivateKey = ursa.createPrivateKey(file)
const decryptedKey = myPrivateKey.decrypt(key, 'hex', 'utf8')
const decryptedData = symetricDecrypt(data, decryptedKey)
2016-02-05 11:03:20 -06:00
return callback(null, decryptedData)
})
}
2016-02-05 11:03:20 -06:00
function encrypt (publicKey, data, callback) {
const crt = ursa.createPublicKey(publicKey)
2016-02-05 11:03:20 -06:00
symetricEncrypt(data, function (err, dataEncrypted) {
if (err) return callback(err)
2016-02-05 11:03:20 -06:00
2016-03-16 16:29:27 -05:00
const key = crt.encrypt(dataEncrypted.password, 'utf8', 'hex')
const encrypted = {
data: dataEncrypted.crypted,
key: key
}
2016-02-05 11:03:20 -06:00
callback(null, encrypted)
})
}
2016-02-05 11:03:20 -06:00
function sign (data) {
2016-08-19 14:34:51 -05:00
const myKey = ursa.createPrivateKey(fs.readFileSync(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem'))
2016-03-16 16:29:27 -05:00
const signature = myKey.hashAndSign('sha256', data, 'utf8', 'hex')
2016-02-05 11:03:20 -06:00
return signature
}
2016-02-05 11:03:20 -06:00
// ---------------------------------------------------------------------------
2016-02-05 11:03:20 -06:00
module.exports = peertubeCrypto
2016-02-05 11:03:20 -06:00
// ---------------------------------------------------------------------------
2016-02-05 11:03:20 -06:00
function certsExist (callback) {
2016-08-19 14:34:51 -05:00
fs.exists(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem', function (exists) {
return callback(exists)
})
}
function createCerts (callback) {
certsExist(function (exist) {
if (exist === true) {
2016-03-16 16:29:27 -05:00
const string = 'Certs already exist.'
logger.warning(string)
return callback(new Error(string))
}
logger.info('Generating a RSA key...')
2016-08-19 14:34:51 -05:00
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)
2016-02-05 11:03:20 -06:00
}
logger.info('RSA key generated.')
2016-02-05 11:03:20 -06:00
2016-08-19 14:34:51 -05:00
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...')
2016-08-19 14:34:51 -05:00
openssl.exec('rsa', options, function (err) {
2016-02-05 11:03:20 -06:00
if (err) {
logger.error('Cannot create public key on this pod.')
2016-02-05 11:03:20 -06:00
return callback(err)
}
logger.info('Public key managed.')
return callback(null)
})
2016-02-05 11:03:20 -06:00
})
})
}
function generatePassword (callback) {
crypto.randomBytes(32, function (err, buf) {
if (err) return callback(err)
callback(null, buf.toString('utf8'))
})
}
function symetricDecrypt (text, password) {
2016-03-16 16:29:27 -05:00
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)
2016-03-16 16:29:27 -05:00
const cipher = crypto.createCipher(algorithm, password)
let crypted = cipher.update(text, 'utf8', 'hex')
crypted += cipher.final('hex')
callback(null, { crypted: crypted, password: password })
})
}