PeerTube/server/helpers/peertube-crypto.js

163 lines
4.1 KiB
JavaScript
Raw Normal View History

'use strict'
const crypto = require('crypto')
2016-08-25 10:57:37 -05:00
const bcrypt = require('bcrypt')
2016-03-16 16:29:27 -05:00
const fs = require('fs')
const openssl = require('openssl-wrapper')
2017-01-17 14:42:47 -06:00
const pathUtils = require('path')
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 peertubeCrypto = {
checkSignature,
comparePassword,
createCertsIfNotExist,
cryptPassword,
2017-01-17 14:42:47 -06:00
getMyPrivateCert,
getMyPublicCert,
sign
}
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
2017-01-17 14:42:47 -06:00
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
}
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)
})
})
}
2017-01-17 14:42:47 -06:00
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)
}
// ---------------------------------------------------------------------------
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) {
2017-01-17 14:42:47 -06:00
const certPath = pathUtils.join(constants.CONFIG.STORAGE.CERT_DIR, constants.PRIVATE_CERT_NAME)
fs.exists(certPath, 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
2017-01-17 14:42:47 -06:00
const privateCertPath = pathUtils.join(constants.CONFIG.STORAGE.CERT_DIR, constants.PRIVATE_CERT_NAME)
const genRsaOptions = {
'out': privateCertPath,
2016-08-19 14:34:51 -05:00
'2048': false
}
2017-01-17 14:42:47 -06:00
openssl.exec('genrsa', genRsaOptions, function (err) {
if (err) {
logger.error('Cannot create private key on this pod.')
return callback(err)
2016-02-05 11:03:20 -06:00
}
2017-01-17 14:42:47 -06:00
logger.info('RSA key generated.')
2017-01-17 14:42:47 -06:00
logger.info('Managing public key...')
2016-02-05 11:03:20 -06:00
2017-01-17 14:42:47 -06:00
const publicCertPath = pathUtils.join(constants.CONFIG.STORAGE.CERT_DIR, 'peertube.pub')
const rsaOptions = {
'in': privateCertPath,
2016-08-19 14:34:51 -05:00
'pubout': true,
2017-01-17 14:42:47 -06:00
'out': publicCertPath
2016-08-19 14:34:51 -05:00
}
2017-01-17 14:42:47 -06:00
openssl.exec('rsa', rsaOptions, 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
})
})
}