aboutsummaryrefslogtreecommitdiffhomepage
path: root/helpers/peertubeCrypto.js
blob: 9031f6ae5f46fc988cd0a9b32b096f84f56ff4f8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
;(function () {
  'use strict'

  var config = require('config')
  var crypto = require('crypto')
  var fs = require('fs')
  var openssl = require('openssl-wrapper')
  var path = require('path')
  var ursa = require('ursa')

  var logger = require('./logger')

  var certDir = path.join(__dirname, '..', config.get('storage.certs'))
  var algorithm = 'aes-256-ctr'

  var peertubeCrypto = {
    checkSignature: checkSignature,
    createCertsIfNotExist: createCertsIfNotExist,
    decrypt: decrypt,
    encrypt: encrypt,
    getCertDir: getCertDir,
    sign: sign
  }

  function checkSignature (public_key, raw_data, hex_signature) {
    var crt = ursa.createPublicKey(public_key)
    var is_valid = crt.hashAndVerify('sha256', new Buffer(raw_data).toString('hex'), hex_signature, 'hex')
    return is_valid
  }

  function createCertsIfNotExist (callback) {
    certsExist(function (exist) {
      if (exist === true) {
        return callback(null)
      }

      createCerts(function (err) {
        return callback(err)
      })
    })
  }

  function decrypt (key, data, callback) {
    fs.readFile(getCertDir() + 'peertube.key.pem', function (err, file) {
      if (err) return callback(err)

      var my_private_key = ursa.createPrivateKey(file)
      var decrypted_key = my_private_key.decrypt(key, 'hex', 'utf8')
      var decrypted_data = symetricDecrypt(data, decrypted_key)

      return callback(null, decrypted_data)
    })
  }

  function encrypt (public_key, data, callback) {
    var crt = ursa.createPublicKey(public_key)

    symetricEncrypt(data, function (err, dataEncrypted) {
      if (err) return callback(err)

      var key = crt.encrypt(dataEncrypted.password, 'utf8', 'hex')
      var encrypted = {
        data: dataEncrypted.crypted,
        key: key
      }

      callback(null, encrypted)
    })
  }

  function getCertDir () {
    return certDir
  }

  function sign (data) {
    var myKey = ursa.createPrivateKey(fs.readFileSync(certDir + 'peertube.key.pem'))
    var signature = myKey.hashAndSign('sha256', data, 'utf8', 'hex')

    return signature
  }

  // ---------------------------------------------------------------------------

  module.exports = peertubeCrypto

  // ---------------------------------------------------------------------------

  function certsExist (callback) {
    fs.exists(certDir + 'peertube.key.pem', function (exists) {
      return callback(exists)
    })
  }

  function createCerts (callback) {
    certsExist(function (exist) {
      if (exist === true) {
        var string = 'Certs already exist.'
        logger.warning(string)
        return callback(new Error(string))
      }

      logger.info('Generating a RSA key...')
      openssl.exec('genrsa', { 'out': certDir + 'peertube.key.pem', '2048': false }, function (err) {
        if (err) {
          logger.error('Cannot create private key on this pod.')
          return callback(err)
        }
        logger.info('RSA key generated.')

        logger.info('Manage public key...')
        openssl.exec('rsa', { 'in': certDir + 'peertube.key.pem', 'pubout': true, 'out': certDir + 'peertube.pub' }, function (err) {
          if (err) {
            logger.error('Cannot create public key on this pod.')
            return callback(err)
          }

          logger.info('Public key managed.')
          return callback(null)
        })
      })
    })
  }

  function generatePassword (callback) {
    crypto.randomBytes(32, function (err, buf) {
      if (err) return callback(err)

      callback(null, buf.toString('utf8'))
    })
  }

  function symetricDecrypt (text, password) {
    var decipher = crypto.createDecipher(algorithm, password)
    var 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)

      var cipher = crypto.createCipher(algorithm, password)
      var crypted = cipher.update(text, 'utf8', 'hex')
      crypted += cipher.final('hex')
      callback(null, { crypted: crypted, password: password })
    })
  }
})()