aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-01-04 22:23:07 +0100
committerChocobozzz <florian.bigard@gmail.com>2017-01-04 22:23:07 +0100
commitbdfbd4f162d66c3a6bd7c312a99e0b692e830792 (patch)
tree30b857f73fe62771a94320e3e78030a1345dc3a8 /server/helpers
parentb981a525c37d226b3fa59287a6ce338f54583d0c (diff)
downloadPeerTube-bdfbd4f162d66c3a6bd7c312a99e0b692e830792.tar.gz
PeerTube-bdfbd4f162d66c3a6bd7c312a99e0b692e830792.tar.zst
PeerTube-bdfbd4f162d66c3a6bd7c312a99e0b692e830792.zip
Server: use crypto instead of ursa for pod signature
Diffstat (limited to 'server/helpers')
-rw-r--r--server/helpers/peertube-crypto.js54
-rw-r--r--server/helpers/requests.js46
2 files changed, 69 insertions, 31 deletions
diff --git a/server/helpers/peertube-crypto.js b/server/helpers/peertube-crypto.js
index 610cb16cd..0f1e02ad6 100644
--- a/server/helpers/peertube-crypto.js
+++ b/server/helpers/peertube-crypto.js
@@ -1,9 +1,9 @@
1'use strict' 1'use strict'
2 2
3const crypto = require('crypto')
3const bcrypt = require('bcrypt') 4const bcrypt = require('bcrypt')
4const fs = require('fs') 5const fs = require('fs')
5const openssl = require('openssl-wrapper') 6const openssl = require('openssl-wrapper')
6const ursa = require('ursa')
7 7
8const constants = require('../initializers/constants') 8const constants = require('../initializers/constants')
9const logger = require('./logger') 9const logger = require('./logger')
@@ -16,12 +16,51 @@ const peertubeCrypto = {
16 sign 16 sign
17} 17}
18 18
19function checkSignature (publicKey, rawData, hexSignature) { 19function checkSignature (publicKey, data, hexSignature) {
20 const crt = ursa.createPublicKey(publicKey) 20 const verify = crypto.createVerify(constants.SIGNATURE_ALGORITHM)
21 const isValid = crt.hashAndVerify('sha256', new Buffer(rawData).toString('hex'), hexSignature, 'hex') 21
22 let dataString
23 if (typeof data === 'string') {
24 dataString = data
25 } else {
26 try {
27 dataString = JSON.stringify(data)
28 } catch (err) {
29 logger.error('Cannot check signature.', { error: err })
30 return false
31 }
32 }
33
34 verify.update(dataString, 'utf8')
35
36 const isValid = verify.verify(publicKey, hexSignature, constants.SIGNATURE_ENCODING)
22 return isValid 37 return isValid
23} 38}
24 39
40function sign (data) {
41 const sign = crypto.createSign(constants.SIGNATURE_ALGORITHM)
42
43 let dataString
44 if (typeof data === 'string') {
45 dataString = data
46 } else {
47 try {
48 dataString = JSON.stringify(data)
49 } catch (err) {
50 logger.error('Cannot sign data.', { error: err })
51 return ''
52 }
53 }
54
55 sign.update(dataString, 'utf8')
56
57 // TODO: make async
58 const myKey = fs.readFileSync(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem')
59 const signature = sign.sign(myKey, constants.SIGNATURE_ENCODING)
60
61 return signature
62}
63
25function comparePassword (plainPassword, hashPassword, callback) { 64function comparePassword (plainPassword, hashPassword, callback) {
26 bcrypt.compare(plainPassword, hashPassword, function (err, isPasswordMatch) { 65 bcrypt.compare(plainPassword, hashPassword, function (err, isPasswordMatch) {
27 if (err) return callback(err) 66 if (err) return callback(err)
@@ -52,13 +91,6 @@ function cryptPassword (password, callback) {
52 }) 91 })
53} 92}
54 93
55function sign (data) {
56 const myKey = ursa.createPrivateKey(fs.readFileSync(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem'))
57 const signature = myKey.hashAndSign('sha256', data, 'utf8', 'hex')
58
59 return signature
60}
61
62// --------------------------------------------------------------------------- 94// ---------------------------------------------------------------------------
63 95
64module.exports = peertubeCrypto 96module.exports = peertubeCrypto
diff --git a/server/helpers/requests.js b/server/helpers/requests.js
index b0cda09fe..095b95e1c 100644
--- a/server/helpers/requests.js
+++ b/server/helpers/requests.js
@@ -28,31 +28,37 @@ function makeSecureRequest (params, callback) {
28 url: constants.REMOTE_SCHEME.HTTP + '://' + params.toPod.host + params.path 28 url: constants.REMOTE_SCHEME.HTTP + '://' + params.toPod.host + params.path
29 } 29 }
30 30
31 // Add data with POST requst ? 31 if (params.method !== 'POST') {
32 if (params.method === 'POST') { 32 return callback(new Error('Cannot make a secure request with a non POST method.'))
33 requestParams.json = {} 33 }
34 34
35 // Add signature if it is specified in the params 35 requestParams.json = {}
36 if (params.sign === true) {
37 const host = constants.CONFIG.WEBSERVER.HOST
38
39 requestParams.json.signature = {
40 host,
41 signature: peertubeCrypto.sign(host)
42 }
43 }
44 36
45 // If there are data informations 37 // Add signature if it is specified in the params
38 if (params.sign === true) {
39 const host = constants.CONFIG.WEBSERVER.HOST
40
41 let dataToSign
46 if (params.data) { 42 if (params.data) {
47 requestParams.json.data = params.data 43 dataToSign = dataToSign = params.data
48 request.post(requestParams, callback)
49 } else { 44 } else {
50 // No data 45 // We do not have data to sign so we just take our host
51 request.post(requestParams, callback) 46 // It is not ideal but the connection should be in HTTPS
47 dataToSign = host
52 } 48 }
53 } else { 49
54 request.get(requestParams, callback) 50 requestParams.json.signature = {
51 host, // Which host we pretend to be
52 signature: peertubeCrypto.sign(dataToSign)
53 }
54 }
55
56 // If there are data informations
57 if (params.data) {
58 requestParams.json.data = params.data
55 } 59 }
60
61 request.post(requestParams, callback)
56} 62}
57 63
58// --------------------------------------------------------------------------- 64// ---------------------------------------------------------------------------