1
0
Fork 0
peertube/server/helpers/peertube-crypto.ts

156 lines
3.8 KiB
TypeScript
Raw Normal View History

2017-06-05 15:53:49 -04:00
import * as crypto from 'crypto'
2017-07-07 10:57:28 -04:00
import * as Promise from 'bluebird'
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 {
readFilePromise,
bcryptComparePromise,
bcryptGenSaltPromise,
bcryptHashPromise,
accessPromise,
opensslExecPromise
} from './core-utils'
2017-05-15 16:22:03 -04:00
import { logger } from './logger'
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)
let dataString
if (typeof data === 'string') {
dataString = data
} else {
try {
dataString = JSON.stringify(data)
} catch (err) {
2017-07-07 12:26:12 -04:00
logger.error('Cannot check signature.', err)
return false
}
}
verify.update(dataString, 'utf8')
2017-05-15 16:22:03 -04:00
const isValid = verify.verify(publicKey, hexSignature, SIGNATURE_ENCODING)
return isValid
}
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-06-10 16:15:25 -04:00
let dataString: string
if (typeof data === 'string') {
dataString = data
} else {
try {
dataString = JSON.stringify(data)
} catch (err) {
2017-07-07 12:26:12 -04:00
logger.error('Cannot sign data.', err)
2017-07-07 10:57:28 -04:00
return Promise.resolve('')
}
}
sign.update(dataString, 'utf8')
2017-07-07 10:57:28 -04:00
return getMyPrivateCert().then(myKey => {
return sign.sign(myKey, SIGNATURE_ENCODING)
})
}
function comparePassword (plainPassword: string, hashPassword: string) {
return bcryptComparePromise(plainPassword, hashPassword)
2016-08-25 11:57:37 -04:00
}
function createCertsIfNotExist () {
return certsExist().then(exist => {
if (exist === true) {
return undefined
}
return createCerts()
})
}
2016-02-05 12:03:20 -05:00
function cryptPassword (password: string) {
return bcryptGenSaltPromise(BCRYPT_SALT_SIZE).then(salt => bcryptHashPromise(password, salt))
2016-08-25 11:57:37 -04:00
}
function getMyPrivateCert () {
2017-05-15 16:22:03 -04:00
const certPath = join(CONFIG.STORAGE.CERT_DIR, PRIVATE_CERT_NAME)
return readFilePromise(certPath, 'utf8')
2017-01-17 15:42:47 -05:00
}
function getMyPublicCert () {
2017-05-15 16:22:03 -04:00
const certPath = join(CONFIG.STORAGE.CERT_DIR, PUBLIC_CERT_NAME)
return readFilePromise(certPath, 'utf8')
2017-01-17 15:42:47 -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-05 12:03:20 -05:00
function certsExist () {
2017-05-15 16:22:03 -04:00
const certPath = join(CONFIG.STORAGE.CERT_DIR, PRIVATE_CERT_NAME)
// If there is an error the certificates do not exist
return accessPromise(certPath)
.then(() => true)
.catch(() => false)
}
2017-04-16 08:25:37 -04:00
function createCerts () {
return certsExist().then(exist => {
if (exist === true) {
2017-06-10 16:15:25 -04:00
const errorMessage = 'Certs already exist.'
logger.warning(errorMessage)
throw new Error(errorMessage)
}
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
}
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 12:03:20 -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
})
})
}