]>
Commit | Line | Data |
---|---|---|
9f10b292 C |
1 | 'use strict' |
2 | ||
bdfbd4f1 | 3 | const crypto = require('crypto') |
26d7d31b | 4 | const bcrypt = require('bcrypt') |
f0f5567b C |
5 | const fs = require('fs') |
6 | const openssl = require('openssl-wrapper') | |
15103f11 | 7 | const pathUtils = require('path') |
9f10b292 | 8 | |
e861452f | 9 | const constants = require('../initializers/constants') |
f0f5567b | 10 | const logger = require('./logger') |
9f10b292 | 11 | |
f0f5567b | 12 | const peertubeCrypto = { |
c4403b29 C |
13 | checkSignature, |
14 | comparePassword, | |
15 | createCertsIfNotExist, | |
16 | cryptPassword, | |
15103f11 C |
17 | getMyPrivateCert, |
18 | getMyPublicCert, | |
c4403b29 | 19 | sign |
9f10b292 C |
20 | } |
21 | ||
bdfbd4f1 C |
22 | function checkSignature (publicKey, data, hexSignature) { |
23 | const verify = crypto.createVerify(constants.SIGNATURE_ALGORITHM) | |
24 | ||
25 | let dataString | |
26 | if (typeof data === 'string') { | |
27 | dataString = data | |
28 | } else { | |
29 | try { | |
30 | dataString = JSON.stringify(data) | |
31 | } catch (err) { | |
32 | logger.error('Cannot check signature.', { error: err }) | |
33 | return false | |
34 | } | |
35 | } | |
36 | ||
37 | verify.update(dataString, 'utf8') | |
38 | ||
39 | const isValid = verify.verify(publicKey, hexSignature, constants.SIGNATURE_ENCODING) | |
bc503c2a | 40 | return isValid |
9f10b292 C |
41 | } |
42 | ||
bdfbd4f1 C |
43 | function sign (data) { |
44 | const sign = crypto.createSign(constants.SIGNATURE_ALGORITHM) | |
45 | ||
46 | let dataString | |
47 | if (typeof data === 'string') { | |
48 | dataString = data | |
49 | } else { | |
50 | try { | |
51 | dataString = JSON.stringify(data) | |
52 | } catch (err) { | |
53 | logger.error('Cannot sign data.', { error: err }) | |
54 | return '' | |
55 | } | |
56 | } | |
57 | ||
58 | sign.update(dataString, 'utf8') | |
59 | ||
60 | // TODO: make async | |
15103f11 C |
61 | const certPath = pathUtils.join(constants.CONFIG.STORAGE.CERT_DIR, constants.PRIVATE_CERT_NAME) |
62 | const myKey = fs.readFileSync(certPath) | |
bdfbd4f1 C |
63 | const signature = sign.sign(myKey, constants.SIGNATURE_ENCODING) |
64 | ||
65 | return signature | |
66 | } | |
67 | ||
26d7d31b C |
68 | function comparePassword (plainPassword, hashPassword, callback) { |
69 | bcrypt.compare(plainPassword, hashPassword, function (err, isPasswordMatch) { | |
70 | if (err) return callback(err) | |
71 | ||
72 | return callback(null, isPasswordMatch) | |
73 | }) | |
74 | } | |
75 | ||
9f10b292 C |
76 | function createCertsIfNotExist (callback) { |
77 | certsExist(function (exist) { | |
78 | if (exist === true) { | |
79 | return callback(null) | |
80 | } | |
81 | ||
82 | createCerts(function (err) { | |
83 | return callback(err) | |
dac0a531 | 84 | }) |
9f10b292 C |
85 | }) |
86 | } | |
dac0a531 | 87 | |
26d7d31b C |
88 | function cryptPassword (password, callback) { |
89 | bcrypt.genSalt(constants.BCRYPT_SALT_SIZE, function (err, salt) { | |
90 | if (err) return callback(err) | |
91 | ||
92 | bcrypt.hash(password, salt, function (err, hash) { | |
93 | return callback(err, hash) | |
94 | }) | |
95 | }) | |
96 | } | |
97 | ||
15103f11 C |
98 | function getMyPrivateCert (callback) { |
99 | const certPath = pathUtils.join(constants.CONFIG.STORAGE.CERT_DIR, constants.PRIVATE_CERT_NAME) | |
100 | fs.readFile(certPath, 'utf8', callback) | |
101 | } | |
102 | ||
103 | function getMyPublicCert (callback) { | |
104 | const certPath = pathUtils.join(constants.CONFIG.STORAGE.CERT_DIR, constants.PUBLIC_CERT_NAME) | |
105 | fs.readFile(certPath, 'utf8', callback) | |
106 | } | |
107 | ||
9f10b292 | 108 | // --------------------------------------------------------------------------- |
dac0a531 | 109 | |
9f10b292 | 110 | module.exports = peertubeCrypto |
dac0a531 | 111 | |
9f10b292 | 112 | // --------------------------------------------------------------------------- |
dac0a531 | 113 | |
9f10b292 | 114 | function certsExist (callback) { |
15103f11 C |
115 | const certPath = pathUtils.join(constants.CONFIG.STORAGE.CERT_DIR, constants.PRIVATE_CERT_NAME) |
116 | fs.exists(certPath, function (exists) { | |
9f10b292 C |
117 | return callback(exists) |
118 | }) | |
119 | } | |
120 | ||
121 | function createCerts (callback) { | |
122 | certsExist(function (exist) { | |
123 | if (exist === true) { | |
f0f5567b | 124 | const string = 'Certs already exist.' |
9f10b292 C |
125 | logger.warning(string) |
126 | return callback(new Error(string)) | |
127 | } | |
128 | ||
129 | logger.info('Generating a RSA key...') | |
e861452f | 130 | |
15103f11 C |
131 | const privateCertPath = pathUtils.join(constants.CONFIG.STORAGE.CERT_DIR, constants.PRIVATE_CERT_NAME) |
132 | const genRsaOptions = { | |
133 | 'out': privateCertPath, | |
e861452f C |
134 | '2048': false |
135 | } | |
15103f11 | 136 | openssl.exec('genrsa', genRsaOptions, function (err) { |
9f10b292 C |
137 | if (err) { |
138 | logger.error('Cannot create private key on this pod.') | |
139 | return callback(err) | |
dac0a531 | 140 | } |
15103f11 | 141 | |
9f10b292 | 142 | logger.info('RSA key generated.') |
15103f11 | 143 | logger.info('Managing public key...') |
dac0a531 | 144 | |
15103f11 C |
145 | const publicCertPath = pathUtils.join(constants.CONFIG.STORAGE.CERT_DIR, 'peertube.pub') |
146 | const rsaOptions = { | |
147 | 'in': privateCertPath, | |
e861452f | 148 | 'pubout': true, |
15103f11 | 149 | 'out': publicCertPath |
e861452f | 150 | } |
15103f11 | 151 | openssl.exec('rsa', rsaOptions, function (err) { |
dac0a531 | 152 | if (err) { |
9f10b292 | 153 | logger.error('Cannot create public key on this pod.') |
dac0a531 C |
154 | return callback(err) |
155 | } | |
dac0a531 | 156 | |
9f10b292 C |
157 | logger.info('Public key managed.') |
158 | return callback(null) | |
159 | }) | |
dac0a531 | 160 | }) |
9f10b292 C |
161 | }) |
162 | } |