2017-06-05 14:53:49 -05:00
|
|
|
import * as crypto from 'crypto'
|
|
|
|
import * as fs from 'fs'
|
2017-05-15 15:22:03 -05:00
|
|
|
import { join } from 'path'
|
|
|
|
|
|
|
|
import {
|
|
|
|
SIGNATURE_ALGORITHM,
|
|
|
|
SIGNATURE_ENCODING,
|
|
|
|
PRIVATE_CERT_NAME,
|
|
|
|
CONFIG,
|
|
|
|
BCRYPT_SALT_SIZE,
|
|
|
|
PUBLIC_CERT_NAME
|
|
|
|
} from '../initializers'
|
2017-07-05 06:26:25 -05:00
|
|
|
import {
|
|
|
|
readFilePromise,
|
|
|
|
bcryptComparePromise,
|
|
|
|
bcryptGenSaltPromise,
|
|
|
|
bcryptHashPromise,
|
|
|
|
accessPromise,
|
|
|
|
opensslExecPromise
|
|
|
|
} from './core-utils'
|
2017-05-15 15:22:03 -05:00
|
|
|
import { logger } from './logger'
|
2016-02-07 04:23:23 -06:00
|
|
|
|
2017-06-10 15:15:25 -05:00
|
|
|
function checkSignature (publicKey: string, data: string, hexSignature: string) {
|
2017-05-15 15:22:03 -05:00
|
|
|
const verify = crypto.createVerify(SIGNATURE_ALGORITHM)
|
2017-01-04 15:23:07 -06: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 15:22:03 -05:00
|
|
|
const isValid = verify.verify(publicKey, hexSignature, SIGNATURE_ENCODING)
|
2016-05-11 14:19:34 -05:00
|
|
|
return isValid
|
2016-02-07 04:23:23 -06:00
|
|
|
}
|
|
|
|
|
2017-06-10 15:15:25 -05:00
|
|
|
function sign (data: string|Object) {
|
2017-05-15 15:22:03 -05:00
|
|
|
const sign = crypto.createSign(SIGNATURE_ALGORITHM)
|
2017-01-04 15:23:07 -06:00
|
|
|
|
2017-06-10 15:15:25 -05:00
|
|
|
let dataString: string
|
2017-01-04 15:23:07 -06: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 15:22:03 -05:00
|
|
|
const certPath = join(CONFIG.STORAGE.CERT_DIR, PRIVATE_CERT_NAME)
|
2017-01-17 14:42:47 -06:00
|
|
|
const myKey = fs.readFileSync(certPath)
|
2017-05-15 15:22:03 -05:00
|
|
|
const signature = sign.sign(myKey.toString(), SIGNATURE_ENCODING)
|
2017-01-04 15:23:07 -06:00
|
|
|
|
|
|
|
return signature
|
|
|
|
}
|
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
function comparePassword (plainPassword: string, hashPassword: string) {
|
|
|
|
return bcryptComparePromise(plainPassword, hashPassword)
|
2016-08-25 10:57:37 -05:00
|
|
|
}
|
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
function createCertsIfNotExist () {
|
|
|
|
return certsExist().then(exist => {
|
2016-02-07 04:23:23 -06:00
|
|
|
if (exist === true) {
|
2017-07-05 06:26:25 -05:00
|
|
|
return undefined
|
2016-02-07 04:23:23 -06:00
|
|
|
}
|
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
return createCerts()
|
2016-02-07 04:23:23 -06:00
|
|
|
})
|
|
|
|
}
|
2016-02-05 11:03:20 -06:00
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
function cryptPassword (password: string) {
|
|
|
|
return bcryptGenSaltPromise(BCRYPT_SALT_SIZE).then(salt => bcryptHashPromise(password, salt))
|
2016-08-25 10:57:37 -05:00
|
|
|
}
|
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
function getMyPrivateCert () {
|
2017-05-15 15:22:03 -05:00
|
|
|
const certPath = join(CONFIG.STORAGE.CERT_DIR, PRIVATE_CERT_NAME)
|
2017-07-05 06:26:25 -05:00
|
|
|
return readFilePromise(certPath, 'utf8')
|
2017-01-17 14:42:47 -06:00
|
|
|
}
|
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
function getMyPublicCert () {
|
2017-05-15 15:22:03 -05:00
|
|
|
const certPath = join(CONFIG.STORAGE.CERT_DIR, PUBLIC_CERT_NAME)
|
2017-07-05 06:26:25 -05:00
|
|
|
return readFilePromise(certPath, 'utf8')
|
2017-01-17 14:42:47 -06:00
|
|
|
}
|
|
|
|
|
2016-02-07 04:23:23 -06:00
|
|
|
// ---------------------------------------------------------------------------
|
2016-02-05 11:03:20 -06:00
|
|
|
|
2017-05-15 15:22:03 -05:00
|
|
|
export {
|
|
|
|
checkSignature,
|
|
|
|
comparePassword,
|
|
|
|
createCertsIfNotExist,
|
|
|
|
cryptPassword,
|
|
|
|
getMyPrivateCert,
|
|
|
|
getMyPublicCert,
|
|
|
|
sign
|
|
|
|
}
|
2016-02-05 11:03:20 -06:00
|
|
|
|
2016-02-07 04:23:23 -06:00
|
|
|
// ---------------------------------------------------------------------------
|
2016-02-05 11:03:20 -06:00
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
function certsExist () {
|
2017-05-15 15:22:03 -05:00
|
|
|
const certPath = join(CONFIG.STORAGE.CERT_DIR, PRIVATE_CERT_NAME)
|
2016-02-07 04:23:23 -06:00
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
// If there is an error the certificates do not exist
|
|
|
|
return accessPromise(certPath)
|
|
|
|
.then(() => true)
|
|
|
|
.catch(() => false)
|
|
|
|
}
|
2017-04-16 07:25:37 -05:00
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
function createCerts () {
|
|
|
|
return certsExist().then(exist => {
|
2016-02-07 04:23:23 -06:00
|
|
|
if (exist === true) {
|
2017-06-10 15:15:25 -05:00
|
|
|
const errorMessage = 'Certs already exist.'
|
|
|
|
logger.warning(errorMessage)
|
2017-07-05 06:26:25 -05:00
|
|
|
throw new Error(errorMessage)
|
2016-02-07 04:23:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
logger.info('Generating a RSA key...')
|
2016-08-19 14:34:51 -05:00
|
|
|
|
2017-05-15 15:22:03 -05:00
|
|
|
const privateCertPath = join(CONFIG.STORAGE.CERT_DIR, PRIVATE_CERT_NAME)
|
2017-01-17 14:42:47 -06:00
|
|
|
const genRsaOptions = {
|
|
|
|
'out': privateCertPath,
|
2016-08-19 14:34:51 -05:00
|
|
|
'2048': false
|
|
|
|
}
|
2017-07-05 06:26:25 -05:00
|
|
|
return opensslExecPromise('genrsa', genRsaOptions)
|
|
|
|
.then(() => {
|
|
|
|
logger.info('RSA key generated.')
|
|
|
|
logger.info('Managing public key...')
|
|
|
|
|
|
|
|
const publicCertPath = join(CONFIG.STORAGE.CERT_DIR, 'peertube.pub')
|
|
|
|
const rsaOptions = {
|
|
|
|
'in': privateCertPath,
|
|
|
|
'pubout': true,
|
|
|
|
'out': publicCertPath
|
2016-02-05 11:03:20 -06:00
|
|
|
}
|
2017-07-05 06:26:25 -05:00
|
|
|
return opensslExecPromise('rsa', rsaOptions)
|
|
|
|
.then(() => logger.info('Public key managed.'))
|
|
|
|
.catch(err => {
|
|
|
|
logger.error('Cannot create public key on this pod.')
|
|
|
|
throw err
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
logger.error('Cannot create private key on this pod.')
|
|
|
|
throw err
|
2016-02-07 04:23:23 -06:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|