Add tags support to server
[oweals/peertube.git] / server / helpers / peertubeCrypto.js
1 'use strict'
2
3 const config = require('config')
4 const crypto = require('crypto')
5 const fs = require('fs')
6 const openssl = require('openssl-wrapper')
7 const path = require('path')
8 const ursa = require('ursa')
9
10 const logger = require('./logger')
11
12 const certDir = path.join(__dirname, '..', '..', config.get('storage.certs'))
13 const algorithm = 'aes-256-ctr'
14
15 const peertubeCrypto = {
16   checkSignature: checkSignature,
17   createCertsIfNotExist: createCertsIfNotExist,
18   decrypt: decrypt,
19   encrypt: encrypt,
20   getCertDir: getCertDir,
21   sign: sign
22 }
23
24 function checkSignature (publicKey, rawData, hexSignature) {
25   const crt = ursa.createPublicKey(publicKey)
26   const isValid = crt.hashAndVerify('sha256', new Buffer(rawData).toString('hex'), hexSignature, 'hex')
27   return isValid
28 }
29
30 function createCertsIfNotExist (callback) {
31   certsExist(function (exist) {
32     if (exist === true) {
33       return callback(null)
34     }
35
36     createCerts(function (err) {
37       return callback(err)
38     })
39   })
40 }
41
42 function decrypt (key, data, callback) {
43   fs.readFile(getCertDir() + 'peertube.key.pem', function (err, file) {
44     if (err) return callback(err)
45
46     const myPrivateKey = ursa.createPrivateKey(file)
47     const decryptedKey = myPrivateKey.decrypt(key, 'hex', 'utf8')
48     const decryptedData = symetricDecrypt(data, decryptedKey)
49
50     return callback(null, decryptedData)
51   })
52 }
53
54 function encrypt (publicKey, data, callback) {
55   const crt = ursa.createPublicKey(publicKey)
56
57   symetricEncrypt(data, function (err, dataEncrypted) {
58     if (err) return callback(err)
59
60     const key = crt.encrypt(dataEncrypted.password, 'utf8', 'hex')
61     const encrypted = {
62       data: dataEncrypted.crypted,
63       key: key
64     }
65
66     callback(null, encrypted)
67   })
68 }
69
70 function getCertDir () {
71   return certDir
72 }
73
74 function sign (data) {
75   const myKey = ursa.createPrivateKey(fs.readFileSync(certDir + 'peertube.key.pem'))
76   const signature = myKey.hashAndSign('sha256', data, 'utf8', 'hex')
77
78   return signature
79 }
80
81 // ---------------------------------------------------------------------------
82
83 module.exports = peertubeCrypto
84
85 // ---------------------------------------------------------------------------
86
87 function certsExist (callback) {
88   fs.exists(certDir + 'peertube.key.pem', function (exists) {
89     return callback(exists)
90   })
91 }
92
93 function createCerts (callback) {
94   certsExist(function (exist) {
95     if (exist === true) {
96       const string = 'Certs already exist.'
97       logger.warning(string)
98       return callback(new Error(string))
99     }
100
101     logger.info('Generating a RSA key...')
102     openssl.exec('genrsa', { 'out': certDir + 'peertube.key.pem', '2048': false }, function (err) {
103       if (err) {
104         logger.error('Cannot create private key on this pod.')
105         return callback(err)
106       }
107       logger.info('RSA key generated.')
108
109       logger.info('Manage public key...')
110       openssl.exec('rsa', { 'in': certDir + 'peertube.key.pem', 'pubout': true, 'out': certDir + 'peertube.pub' }, function (err) {
111         if (err) {
112           logger.error('Cannot create public key on this pod.')
113           return callback(err)
114         }
115
116         logger.info('Public key managed.')
117         return callback(null)
118       })
119     })
120   })
121 }
122
123 function generatePassword (callback) {
124   crypto.randomBytes(32, function (err, buf) {
125     if (err) return callback(err)
126
127     callback(null, buf.toString('utf8'))
128   })
129 }
130
131 function symetricDecrypt (text, password) {
132   const decipher = crypto.createDecipher(algorithm, password)
133   let dec = decipher.update(text, 'hex', 'utf8')
134   dec += decipher.final('utf8')
135   return dec
136 }
137
138 function symetricEncrypt (text, callback) {
139   generatePassword(function (err, password) {
140     if (err) return callback(err)
141
142     const cipher = crypto.createCipher(algorithm, password)
143     let crypted = cipher.update(text, 'utf8', 'hex')
144     crypted += cipher.final('hex')
145     callback(null, { crypted: crypted, password: password })
146   })
147 }