2016-02-07 05:23:23 -05:00
|
|
|
'use strict'
|
|
|
|
|
2016-03-16 17:29:27 -04:00
|
|
|
const config = require('config')
|
|
|
|
const crypto = require('crypto')
|
|
|
|
const fs = require('fs')
|
|
|
|
const openssl = require('openssl-wrapper')
|
|
|
|
const path = require('path')
|
|
|
|
const ursa = require('ursa')
|
2016-02-07 05:23:23 -05:00
|
|
|
|
2016-03-16 17:29:27 -04:00
|
|
|
const logger = require('./logger')
|
2016-02-07 05:23:23 -05:00
|
|
|
|
2016-03-16 17:29:27 -04:00
|
|
|
const certDir = path.join(__dirname, '..', '..', config.get('storage.certs'))
|
|
|
|
const algorithm = 'aes-256-ctr'
|
2016-02-07 05:23:23 -05:00
|
|
|
|
2016-03-16 17:29:27 -04:00
|
|
|
const peertubeCrypto = {
|
2016-02-07 05:23:23 -05:00
|
|
|
checkSignature: checkSignature,
|
|
|
|
createCertsIfNotExist: createCertsIfNotExist,
|
|
|
|
decrypt: decrypt,
|
|
|
|
encrypt: encrypt,
|
|
|
|
getCertDir: getCertDir,
|
|
|
|
sign: sign
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkSignature (public_key, raw_data, hex_signature) {
|
2016-03-16 17:29:27 -04:00
|
|
|
const crt = ursa.createPublicKey(public_key)
|
|
|
|
const is_valid = crt.hashAndVerify('sha256', new Buffer(raw_data).toString('hex'), hex_signature, 'hex')
|
2016-02-07 05:23:23 -05:00
|
|
|
return is_valid
|
|
|
|
}
|
|
|
|
|
|
|
|
function createCertsIfNotExist (callback) {
|
|
|
|
certsExist(function (exist) {
|
|
|
|
if (exist === true) {
|
|
|
|
return callback(null)
|
|
|
|
}
|
|
|
|
|
|
|
|
createCerts(function (err) {
|
|
|
|
return callback(err)
|
2016-02-05 12:03:20 -05:00
|
|
|
})
|
2016-02-07 05:23:23 -05:00
|
|
|
})
|
|
|
|
}
|
2016-02-05 12:03:20 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
function decrypt (key, data, callback) {
|
|
|
|
fs.readFile(getCertDir() + 'peertube.key.pem', function (err, file) {
|
|
|
|
if (err) return callback(err)
|
2016-02-05 12:03:20 -05:00
|
|
|
|
2016-03-16 17:29:27 -04:00
|
|
|
const my_private_key = ursa.createPrivateKey(file)
|
|
|
|
const decrypted_key = my_private_key.decrypt(key, 'hex', 'utf8')
|
|
|
|
const decrypted_data = symetricDecrypt(data, decrypted_key)
|
2016-02-05 12:03:20 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
return callback(null, decrypted_data)
|
|
|
|
})
|
|
|
|
}
|
2016-02-05 12:03:20 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
function encrypt (public_key, data, callback) {
|
2016-03-16 17:29:27 -04:00
|
|
|
const crt = ursa.createPublicKey(public_key)
|
2016-02-05 12:03:20 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
symetricEncrypt(data, function (err, dataEncrypted) {
|
|
|
|
if (err) return callback(err)
|
2016-02-05 12:03:20 -05:00
|
|
|
|
2016-03-16 17:29:27 -04:00
|
|
|
const key = crt.encrypt(dataEncrypted.password, 'utf8', 'hex')
|
|
|
|
const encrypted = {
|
2016-02-07 05:23:23 -05:00
|
|
|
data: dataEncrypted.crypted,
|
|
|
|
key: key
|
|
|
|
}
|
2016-02-05 12:03:20 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
callback(null, encrypted)
|
|
|
|
})
|
|
|
|
}
|
2016-02-05 12:03:20 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
function getCertDir () {
|
|
|
|
return certDir
|
|
|
|
}
|
2016-02-05 12:03:20 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
function sign (data) {
|
2016-03-16 17:29:27 -04:00
|
|
|
const myKey = ursa.createPrivateKey(fs.readFileSync(certDir + 'peertube.key.pem'))
|
|
|
|
const signature = myKey.hashAndSign('sha256', data, 'utf8', 'hex')
|
2016-02-05 12:03:20 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
return signature
|
|
|
|
}
|
2016-02-05 12:03:20 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
2016-02-05 12:03:20 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
module.exports = peertubeCrypto
|
2016-02-05 12:03:20 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
2016-02-05 12:03:20 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
function certsExist (callback) {
|
|
|
|
fs.exists(certDir + 'peertube.key.pem', function (exists) {
|
|
|
|
return callback(exists)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function createCerts (callback) {
|
|
|
|
certsExist(function (exist) {
|
|
|
|
if (exist === true) {
|
2016-03-16 17:29:27 -04:00
|
|
|
const string = 'Certs already exist.'
|
2016-02-07 05:23:23 -05:00
|
|
|
logger.warning(string)
|
|
|
|
return callback(new Error(string))
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.info('Generating a RSA key...')
|
|
|
|
openssl.exec('genrsa', { 'out': certDir + 'peertube.key.pem', '2048': false }, function (err) {
|
|
|
|
if (err) {
|
|
|
|
logger.error('Cannot create private key on this pod.')
|
|
|
|
return callback(err)
|
2016-02-05 12:03:20 -05:00
|
|
|
}
|
2016-02-07 05:23:23 -05:00
|
|
|
logger.info('RSA key generated.')
|
2016-02-05 12:03:20 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
logger.info('Manage public key...')
|
|
|
|
openssl.exec('rsa', { 'in': certDir + 'peertube.key.pem', 'pubout': true, 'out': certDir + 'peertube.pub' }, function (err) {
|
2016-02-05 12:03:20 -05:00
|
|
|
if (err) {
|
2016-02-07 05:23:23 -05:00
|
|
|
logger.error('Cannot create public key on this pod.')
|
2016-02-05 12:03:20 -05:00
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
logger.info('Public key managed.')
|
|
|
|
return callback(null)
|
|
|
|
})
|
2016-02-05 12:03:20 -05:00
|
|
|
})
|
2016-02-07 05:23:23 -05: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 17:29:27 -04:00
|
|
|
const decipher = crypto.createDecipher(algorithm, password)
|
|
|
|
let dec = decipher.update(text, 'hex', 'utf8')
|
2016-02-07 05:23:23 -05:00
|
|
|
dec += decipher.final('utf8')
|
|
|
|
return dec
|
|
|
|
}
|
|
|
|
|
|
|
|
function symetricEncrypt (text, callback) {
|
|
|
|
generatePassword(function (err, password) {
|
|
|
|
if (err) return callback(err)
|
|
|
|
|
2016-03-16 17:29:27 -04:00
|
|
|
const cipher = crypto.createCipher(algorithm, password)
|
|
|
|
let crypted = cipher.update(text, 'utf8', 'hex')
|
2016-02-07 05:23:23 -05:00
|
|
|
crypted += cipher.final('hex')
|
|
|
|
callback(null, { crypted: crypted, password: password })
|
|
|
|
})
|
|
|
|
}
|