]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/helpers/peertube-crypto.js
Server: fix update remote video
[github/Chocobozzz/PeerTube.git] / server / helpers / peertube-crypto.js
index 1ff638b04ecce144a71698c9eaffea2efcef8391..0f1e02ad6babb6f6364cedcba89fbe6853ffcd4c 100644 (file)
@@ -1,32 +1,66 @@
 'use strict'
 
-const bcrypt = require('bcrypt')
 const crypto = require('crypto')
+const bcrypt = require('bcrypt')
 const fs = require('fs')
 const openssl = require('openssl-wrapper')
-const ursa = require('ursa')
 
 const constants = require('../initializers/constants')
 const logger = require('./logger')
 
-const algorithm = 'aes-256-ctr'
-
 const peertubeCrypto = {
   checkSignature,
   comparePassword,
   createCertsIfNotExist,
   cryptPassword,
-  decrypt,
-  encrypt,
   sign
 }
 
-function checkSignature (publicKey, rawData, hexSignature) {
-  const crt = ursa.createPublicKey(publicKey)
-  const isValid = crt.hashAndVerify('sha256', new Buffer(rawData).toString('hex'), hexSignature, 'hex')
+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
+  const myKey = fs.readFileSync(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem')
+  const signature = sign.sign(myKey, constants.SIGNATURE_ENCODING)
+
+  return signature
+}
+
 function comparePassword (plainPassword, hashPassword, callback) {
   bcrypt.compare(plainPassword, hashPassword, function (err, isPasswordMatch) {
     if (err) return callback(err)
@@ -57,41 +91,6 @@ function cryptPassword (password, callback) {
   })
 }
 
-function decrypt (key, data, callback) {
-  fs.readFile(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem', function (err, file) {
-    if (err) return callback(err)
-
-    const myPrivateKey = ursa.createPrivateKey(file)
-    const decryptedKey = myPrivateKey.decrypt(key, 'hex', 'utf8')
-    const decryptedData = symetricDecrypt(data, decryptedKey)
-
-    return callback(null, decryptedData)
-  })
-}
-
-function encrypt (publicKey, data, callback) {
-  const crt = ursa.createPublicKey(publicKey)
-
-  symetricEncrypt(data, function (err, dataEncrypted) {
-    if (err) return callback(err)
-
-    const key = crt.encrypt(dataEncrypted.password, 'utf8', 'hex')
-    const encrypted = {
-      data: dataEncrypted.crypted,
-      key: key
-    }
-
-    callback(null, encrypted)
-  })
-}
-
-function sign (data) {
-  const myKey = ursa.createPrivateKey(fs.readFileSync(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem'))
-  const signature = myKey.hashAndSign('sha256', data, 'utf8', 'hex')
-
-  return signature
-}
-
 // ---------------------------------------------------------------------------
 
 module.exports = peertubeCrypto
@@ -143,29 +142,3 @@ function createCerts (callback) {
     })
   })
 }
-
-function generatePassword (callback) {
-  crypto.randomBytes(32, function (err, buf) {
-    if (err) return callback(err)
-
-    callback(null, buf.toString('utf8'))
-  })
-}
-
-function symetricDecrypt (text, password) {
-  const decipher = crypto.createDecipher(algorithm, password)
-  let dec = decipher.update(text, 'hex', 'utf8')
-  dec += decipher.final('utf8')
-  return dec
-}
-
-function symetricEncrypt (text, callback) {
-  generatePassword(function (err, password) {
-    if (err) return callback(err)
-
-    const cipher = crypto.createCipher(algorithm, password)
-    let crypted = cipher.update(text, 'utf8', 'hex')
-    crypted += cipher.final('hex')
-    callback(null, { crypted: crypted, password: password })
-  })
-}