]>
Commit | Line | Data |
---|---|---|
9f10b292 C |
1 | 'use strict' |
2 | ||
26d7d31b | 3 | const bcrypt = require('bcrypt') |
f0f5567b C |
4 | const fs = require('fs') |
5 | const openssl = require('openssl-wrapper') | |
f0f5567b | 6 | const ursa = require('ursa') |
9f10b292 | 7 | |
e861452f | 8 | const constants = require('../initializers/constants') |
f0f5567b | 9 | const logger = require('./logger') |
9f10b292 | 10 | |
f0f5567b | 11 | const peertubeCrypto = { |
c4403b29 C |
12 | checkSignature, |
13 | comparePassword, | |
14 | createCertsIfNotExist, | |
15 | cryptPassword, | |
c4403b29 | 16 | sign |
9f10b292 C |
17 | } |
18 | ||
bc503c2a C |
19 | function checkSignature (publicKey, rawData, hexSignature) { |
20 | const crt = ursa.createPublicKey(publicKey) | |
21 | const isValid = crt.hashAndVerify('sha256', new Buffer(rawData).toString('hex'), hexSignature, 'hex') | |
22 | return isValid | |
9f10b292 C |
23 | } |
24 | ||
26d7d31b C |
25 | function comparePassword (plainPassword, hashPassword, callback) { |
26 | bcrypt.compare(plainPassword, hashPassword, function (err, isPasswordMatch) { | |
27 | if (err) return callback(err) | |
28 | ||
29 | return callback(null, isPasswordMatch) | |
30 | }) | |
31 | } | |
32 | ||
9f10b292 C |
33 | function createCertsIfNotExist (callback) { |
34 | certsExist(function (exist) { | |
35 | if (exist === true) { | |
36 | return callback(null) | |
37 | } | |
38 | ||
39 | createCerts(function (err) { | |
40 | return callback(err) | |
dac0a531 | 41 | }) |
9f10b292 C |
42 | }) |
43 | } | |
dac0a531 | 44 | |
26d7d31b C |
45 | function cryptPassword (password, callback) { |
46 | bcrypt.genSalt(constants.BCRYPT_SALT_SIZE, function (err, salt) { | |
47 | if (err) return callback(err) | |
48 | ||
49 | bcrypt.hash(password, salt, function (err, hash) { | |
50 | return callback(err, hash) | |
51 | }) | |
52 | }) | |
53 | } | |
54 | ||
9f10b292 | 55 | function sign (data) { |
e861452f | 56 | const myKey = ursa.createPrivateKey(fs.readFileSync(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem')) |
f0f5567b | 57 | const signature = myKey.hashAndSign('sha256', data, 'utf8', 'hex') |
dac0a531 | 58 | |
9f10b292 C |
59 | return signature |
60 | } | |
dac0a531 | 61 | |
9f10b292 | 62 | // --------------------------------------------------------------------------- |
dac0a531 | 63 | |
9f10b292 | 64 | module.exports = peertubeCrypto |
dac0a531 | 65 | |
9f10b292 | 66 | // --------------------------------------------------------------------------- |
dac0a531 | 67 | |
9f10b292 | 68 | function certsExist (callback) { |
e861452f | 69 | fs.exists(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem', function (exists) { |
9f10b292 C |
70 | return callback(exists) |
71 | }) | |
72 | } | |
73 | ||
74 | function createCerts (callback) { | |
75 | certsExist(function (exist) { | |
76 | if (exist === true) { | |
f0f5567b | 77 | const string = 'Certs already exist.' |
9f10b292 C |
78 | logger.warning(string) |
79 | return callback(new Error(string)) | |
80 | } | |
81 | ||
82 | logger.info('Generating a RSA key...') | |
e861452f C |
83 | |
84 | let options = { | |
85 | 'out': constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem', | |
86 | '2048': false | |
87 | } | |
88 | openssl.exec('genrsa', options, function (err) { | |
9f10b292 C |
89 | if (err) { |
90 | logger.error('Cannot create private key on this pod.') | |
91 | return callback(err) | |
dac0a531 | 92 | } |
9f10b292 | 93 | logger.info('RSA key generated.') |
dac0a531 | 94 | |
e861452f C |
95 | options = { |
96 | 'in': constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem', | |
97 | 'pubout': true, | |
98 | 'out': constants.CONFIG.STORAGE.CERT_DIR + 'peertube.pub' | |
99 | } | |
9f10b292 | 100 | logger.info('Manage public key...') |
e861452f | 101 | openssl.exec('rsa', options, function (err) { |
dac0a531 | 102 | if (err) { |
9f10b292 | 103 | logger.error('Cannot create public key on this pod.') |
dac0a531 C |
104 | return callback(err) |
105 | } | |
dac0a531 | 106 | |
9f10b292 C |
107 | logger.info('Public key managed.') |
108 | return callback(null) | |
109 | }) | |
dac0a531 | 110 | }) |
9f10b292 C |
111 | }) |
112 | } |