2017-06-05 21:53:49 +02:00
|
|
|
import * as crypto from 'crypto'
|
2017-05-15 22:22:03 +02: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 13:26:25 +02:00
|
|
|
import {
|
|
|
|
readFilePromise,
|
|
|
|
bcryptComparePromise,
|
|
|
|
bcryptGenSaltPromise,
|
|
|
|
bcryptHashPromise,
|
|
|
|
accessPromise,
|
|
|
|
opensslExecPromise
|
|
|
|
} from './core-utils'
|
2017-05-15 22:22:03 +02:00
|
|
|
import { logger } from './logger'
|
2016-02-07 11:23:23 +01:00
|
|
|
|
2017-06-10 22:15:25 +02:00
|
|
|
function checkSignature (publicKey: string, data: string, hexSignature: string) {
|
2017-05-15 22:22:03 +02:00
|
|
|
const verify = crypto.createVerify(SIGNATURE_ALGORITHM)
|
2017-01-04 22:23:07 +01:00
|
|
|
|
|
|
|
let dataString
|
|
|
|
if (typeof data === 'string') {
|
|
|
|
dataString = data
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
dataString = JSON.stringify(data)
|
|
|
|
} catch (err) {
|
2017-07-07 18:26:12 +02:00
|
|
|
logger.error('Cannot check signature.', err)
|
2017-01-04 22:23:07 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
verify.update(dataString, 'utf8')
|
|
|
|
|
2017-05-15 22:22:03 +02:00
|
|
|
const isValid = verify.verify(publicKey, hexSignature, SIGNATURE_ENCODING)
|
2016-05-11 21:19:34 +02:00
|
|
|
return isValid
|
2016-02-07 11:23:23 +01:00
|
|
|
}
|
|
|
|
|
2017-10-25 16:03:33 +02:00
|
|
|
async function sign (data: string|Object) {
|
2017-05-15 22:22:03 +02:00
|
|
|
const sign = crypto.createSign(SIGNATURE_ALGORITHM)
|
2017-01-04 22:23:07 +01:00
|
|
|
|
2017-06-10 22:15:25 +02:00
|
|
|
let dataString: string
|
2017-01-04 22:23:07 +01:00
|
|
|
if (typeof data === 'string') {
|
|
|
|
dataString = data
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
dataString = JSON.stringify(data)
|
|
|
|
} catch (err) {
|
2017-07-07 18:26:12 +02:00
|
|
|
logger.error('Cannot sign data.', err)
|
2017-10-25 16:03:33 +02:00
|
|
|
return ''
|
2017-01-04 22:23:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sign.update(dataString, 'utf8')
|
|
|
|
|
2017-10-25 16:03:33 +02:00
|
|
|
const myKey = await getMyPrivateCert()
|
2017-10-25 16:52:01 +02:00
|
|
|
return sign.sign(myKey, SIGNATURE_ENCODING)
|
2017-01-04 22:23:07 +01:00
|
|
|
}
|
|
|
|
|
2017-07-05 13:26:25 +02:00
|
|
|
function comparePassword (plainPassword: string, hashPassword: string) {
|
|
|
|
return bcryptComparePromise(plainPassword, hashPassword)
|
2016-08-25 17:57:37 +02:00
|
|
|
}
|
|
|
|
|
2017-10-25 16:03:33 +02:00
|
|
|
async function createCertsIfNotExist () {
|
|
|
|
const exist = await certsExist()
|
|
|
|
if (exist === true) {
|
2017-10-25 16:52:01 +02:00
|
|
|
return
|
2017-10-25 16:03:33 +02:00
|
|
|
}
|
2016-02-07 11:23:23 +01:00
|
|
|
|
2017-10-25 16:03:33 +02:00
|
|
|
return await createCerts()
|
2016-02-07 11:23:23 +01:00
|
|
|
}
|
2016-02-05 18:03:20 +01:00
|
|
|
|
2017-10-25 16:03:33 +02:00
|
|
|
async function cryptPassword (password: string) {
|
|
|
|
const salt = await bcryptGenSaltPromise(BCRYPT_SALT_SIZE)
|
|
|
|
|
|
|
|
return await bcryptHashPromise(password, salt)
|
2016-08-25 17:57:37 +02:00
|
|
|
}
|
|
|
|
|
2017-07-05 13:26:25 +02:00
|
|
|
function getMyPrivateCert () {
|
2017-05-15 22:22:03 +02:00
|
|
|
const certPath = join(CONFIG.STORAGE.CERT_DIR, PRIVATE_CERT_NAME)
|
2017-07-05 13:26:25 +02:00
|
|
|
return readFilePromise(certPath, 'utf8')
|
2017-01-17 21:42:47 +01:00
|
|
|
}
|
|
|
|
|
2017-07-05 13:26:25 +02:00
|
|
|
function getMyPublicCert () {
|
2017-05-15 22:22:03 +02:00
|
|
|
const certPath = join(CONFIG.STORAGE.CERT_DIR, PUBLIC_CERT_NAME)
|
2017-07-05 13:26:25 +02:00
|
|
|
return readFilePromise(certPath, 'utf8')
|
2017-01-17 21:42:47 +01:00
|
|
|
}
|
|
|
|
|
2016-02-07 11:23:23 +01:00
|
|
|
// ---------------------------------------------------------------------------
|
2016-02-05 18:03:20 +01:00
|
|
|
|
2017-05-15 22:22:03 +02:00
|
|
|
export {
|
|
|
|
checkSignature,
|
|
|
|
comparePassword,
|
|
|
|
createCertsIfNotExist,
|
|
|
|
cryptPassword,
|
|
|
|
getMyPrivateCert,
|
|
|
|
getMyPublicCert,
|
|
|
|
sign
|
|
|
|
}
|
2016-02-05 18:03:20 +01:00
|
|
|
|
2016-02-07 11:23:23 +01:00
|
|
|
// ---------------------------------------------------------------------------
|
2016-02-05 18:03:20 +01:00
|
|
|
|
2017-10-25 16:03:33 +02:00
|
|
|
async function certsExist () {
|
2017-05-15 22:22:03 +02:00
|
|
|
const certPath = join(CONFIG.STORAGE.CERT_DIR, PRIVATE_CERT_NAME)
|
2016-02-07 11:23:23 +01:00
|
|
|
|
2017-07-05 13:26:25 +02:00
|
|
|
// If there is an error the certificates do not exist
|
2017-10-25 16:03:33 +02:00
|
|
|
try {
|
|
|
|
await accessPromise(certPath)
|
|
|
|
|
|
|
|
return true
|
|
|
|
} catch {
|
|
|
|
return false
|
|
|
|
}
|
2017-07-05 13:26:25 +02:00
|
|
|
}
|
2017-04-16 14:25:37 +02:00
|
|
|
|
2017-10-25 16:03:33 +02:00
|
|
|
async function createCerts () {
|
|
|
|
const exist = await certsExist()
|
|
|
|
if (exist === true) {
|
|
|
|
const errorMessage = 'Certs already exist.'
|
|
|
|
logger.warning(errorMessage)
|
|
|
|
throw new Error(errorMessage)
|
|
|
|
}
|
2016-02-07 11:23:23 +01:00
|
|
|
|
2017-10-25 16:03:33 +02:00
|
|
|
logger.info('Generating a RSA key...')
|
2016-08-19 21:34:51 +02:00
|
|
|
|
2017-10-25 16:03:33 +02:00
|
|
|
const privateCertPath = join(CONFIG.STORAGE.CERT_DIR, PRIVATE_CERT_NAME)
|
|
|
|
const genRsaOptions = {
|
|
|
|
'out': privateCertPath,
|
|
|
|
'2048': false
|
|
|
|
}
|
|
|
|
|
|
|
|
await opensslExecPromise('genrsa', genRsaOptions)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
await opensslExecPromise('rsa', rsaOptions)
|
2016-02-07 11:23:23 +01:00
|
|
|
}
|