2017-06-05 15:53:49 -04:00
|
|
|
import * as crypto from 'crypto'
|
|
|
|
import * as bcrypt from 'bcrypt'
|
|
|
|
import * as fs from 'fs'
|
|
|
|
import * as openssl from 'openssl-wrapper'
|
2017-05-15 16:22:03 -04:00
|
|
|
import { join } from 'path'
|
|
|
|
|
|
|
|
import {
|
|
|
|
SIGNATURE_ALGORITHM,
|
|
|
|
SIGNATURE_ENCODING,
|
|
|
|
PRIVATE_CERT_NAME,
|
|
|
|
CONFIG,
|
|
|
|
BCRYPT_SALT_SIZE,
|
|
|
|
PUBLIC_CERT_NAME
|
|
|
|
} from '../initializers'
|
|
|
|
import { logger } from './logger'
|
2016-02-07 05:23:23 -05:00
|
|
|
|
2017-06-10 16:15:25 -04:00
|
|
|
function checkSignature (publicKey: string, data: string, hexSignature: string) {
|
2017-05-15 16:22:03 -04:00
|
|
|
const verify = crypto.createVerify(SIGNATURE_ALGORITHM)
|
2017-01-04 16:23:07 -05:00
|
|
|
|
|
|
|
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')
|
|
|
|
|
2017-05-15 16:22:03 -04:00
|
|
|
const isValid = verify.verify(publicKey, hexSignature, SIGNATURE_ENCODING)
|
2016-05-11 15:19:34 -04:00
|
|
|
return isValid
|
2016-02-07 05:23:23 -05:00
|
|
|
}
|
|
|
|
|
2017-06-10 16:15:25 -04:00
|
|
|
function sign (data: string|Object) {
|
2017-05-15 16:22:03 -04:00
|
|
|
const sign = crypto.createSign(SIGNATURE_ALGORITHM)
|
2017-01-04 16:23:07 -05:00
|
|
|
|
2017-06-10 16:15:25 -04:00
|
|
|
let dataString: string
|
2017-01-04 16:23:07 -05:00
|
|
|
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-05-15 16:22:03 -04:00
|
|
|
const certPath = join(CONFIG.STORAGE.CERT_DIR, PRIVATE_CERT_NAME)
|
2017-01-17 15:42:47 -05:00
|
|
|
const myKey = fs.readFileSync(certPath)
|
2017-05-15 16:22:03 -04:00
|
|
|
const signature = sign.sign(myKey.toString(), SIGNATURE_ENCODING)
|
2017-01-04 16:23:07 -05:00
|
|
|
|
|
|
|
return signature
|
|
|
|
}
|
|
|
|
|
2017-06-10 16:15:25 -04:00
|
|
|
function comparePassword (plainPassword: string, hashPassword: string, callback: (err: Error, match?: boolean) => void) {
|
2016-08-25 11:57:37 -04:00
|
|
|
bcrypt.compare(plainPassword, hashPassword, function (err, isPasswordMatch) {
|
|
|
|
if (err) return callback(err)
|
|
|
|
|
|
|
|
return callback(null, isPasswordMatch)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-06-10 16:15:25 -04:00
|
|
|
function createCertsIfNotExist (callback: (err: Error) => void) {
|
2017-04-16 08:25:37 -04:00
|
|
|
certsExist(function (err, exist) {
|
|
|
|
if (err) return callback(err)
|
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
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
|
|
|
|
2017-06-10 16:15:25 -04:00
|
|
|
function cryptPassword (password: string, callback: (err: Error, hash?: string) => void) {
|
2017-05-15 16:22:03 -04:00
|
|
|
bcrypt.genSalt(BCRYPT_SALT_SIZE, function (err, salt) {
|
2016-08-25 11:57:37 -04:00
|
|
|
if (err) return callback(err)
|
|
|
|
|
|
|
|
bcrypt.hash(password, salt, function (err, hash) {
|
|
|
|
return callback(err, hash)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-06-10 16:15:25 -04:00
|
|
|
function getMyPrivateCert (callback: (err: Error, privateCert: string) => void) {
|
2017-05-15 16:22:03 -04:00
|
|
|
const certPath = join(CONFIG.STORAGE.CERT_DIR, PRIVATE_CERT_NAME)
|
2017-01-17 15:42:47 -05:00
|
|
|
fs.readFile(certPath, 'utf8', callback)
|
|
|
|
}
|
|
|
|
|
2017-06-10 16:15:25 -04:00
|
|
|
function getMyPublicCert (callback: (err: Error, publicCert: string) => void) {
|
2017-05-15 16:22:03 -04:00
|
|
|
const certPath = join(CONFIG.STORAGE.CERT_DIR, PUBLIC_CERT_NAME)
|
2017-01-17 15:42:47 -05:00
|
|
|
fs.readFile(certPath, 'utf8', callback)
|
|
|
|
}
|
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
2016-02-05 12:03:20 -05:00
|
|
|
|
2017-05-15 16:22:03 -04:00
|
|
|
export {
|
|
|
|
checkSignature,
|
|
|
|
comparePassword,
|
|
|
|
createCertsIfNotExist,
|
|
|
|
cryptPassword,
|
|
|
|
getMyPrivateCert,
|
|
|
|
getMyPublicCert,
|
|
|
|
sign
|
|
|
|
}
|
2016-02-05 12:03:20 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
2016-02-05 12:03:20 -05:00
|
|
|
|
2017-06-10 16:15:25 -04:00
|
|
|
function certsExist (callback: (err: Error, certsExist: boolean) => void) {
|
2017-05-15 16:22:03 -04:00
|
|
|
const certPath = join(CONFIG.STORAGE.CERT_DIR, PRIVATE_CERT_NAME)
|
2017-04-16 08:25:37 -04:00
|
|
|
fs.access(certPath, function (err) {
|
|
|
|
// If there is an error the certificates do not exist
|
|
|
|
const exists = !err
|
|
|
|
return callback(null, exists)
|
2016-02-07 05:23:23 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-06-10 16:15:25 -04:00
|
|
|
function createCerts (callback: (err: Error) => void) {
|
2017-04-16 08:25:37 -04:00
|
|
|
certsExist(function (err, exist) {
|
|
|
|
if (err) return callback(err)
|
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
if (exist === true) {
|
2017-06-10 16:15:25 -04:00
|
|
|
const errorMessage = 'Certs already exist.'
|
|
|
|
logger.warning(errorMessage)
|
|
|
|
return callback(new Error(errorMessage))
|
2016-02-07 05:23:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
logger.info('Generating a RSA key...')
|
2016-08-19 15:34:51 -04:00
|
|
|
|
2017-05-15 16:22:03 -04:00
|
|
|
const privateCertPath = join(CONFIG.STORAGE.CERT_DIR, PRIVATE_CERT_NAME)
|
2017-01-17 15:42:47 -05:00
|
|
|
const genRsaOptions = {
|
|
|
|
'out': privateCertPath,
|
2016-08-19 15:34:51 -04:00
|
|
|
'2048': false
|
|
|
|
}
|
2017-01-17 15:42:47 -05:00
|
|
|
openssl.exec('genrsa', genRsaOptions, function (err) {
|
2016-02-07 05:23:23 -05:00
|
|
|
if (err) {
|
|
|
|
logger.error('Cannot create private key on this pod.')
|
|
|
|
return callback(err)
|
2016-02-05 12:03:20 -05:00
|
|
|
}
|
2017-01-17 15:42:47 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
logger.info('RSA key generated.')
|
2017-01-17 15:42:47 -05:00
|
|
|
logger.info('Managing public key...')
|
2016-02-05 12:03:20 -05:00
|
|
|
|
2017-05-15 16:22:03 -04:00
|
|
|
const publicCertPath = join(CONFIG.STORAGE.CERT_DIR, 'peertube.pub')
|
2017-01-17 15:42:47 -05:00
|
|
|
const rsaOptions = {
|
|
|
|
'in': privateCertPath,
|
2016-08-19 15:34:51 -04:00
|
|
|
'pubout': true,
|
2017-01-17 15:42:47 -05:00
|
|
|
'out': publicCertPath
|
2016-08-19 15:34:51 -04:00
|
|
|
}
|
2017-01-17 15:42:47 -05:00
|
|
|
openssl.exec('rsa', rsaOptions, 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
|
|
|
})
|
|
|
|
}
|