]>
Commit | Line | Data |
---|---|---|
1 | 'use strict' | |
2 | ||
3 | const crypto = require('crypto') | |
4 | const bcrypt = require('bcrypt') | |
5 | const fs = require('fs') | |
6 | const openssl = require('openssl-wrapper') | |
7 | const pathUtils = require('path') | |
8 | ||
9 | const constants = require('../initializers/constants') | |
10 | const logger = require('./logger') | |
11 | ||
12 | const peertubeCrypto = { | |
13 | checkSignature, | |
14 | comparePassword, | |
15 | createCertsIfNotExist, | |
16 | cryptPassword, | |
17 | getMyPrivateCert, | |
18 | getMyPublicCert, | |
19 | sign | |
20 | } | |
21 | ||
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) | |
40 | return isValid | |
41 | } | |
42 | ||
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 | |
61 | const certPath = pathUtils.join(constants.CONFIG.STORAGE.CERT_DIR, constants.PRIVATE_CERT_NAME) | |
62 | const myKey = fs.readFileSync(certPath) | |
63 | const signature = sign.sign(myKey, constants.SIGNATURE_ENCODING) | |
64 | ||
65 | return signature | |
66 | } | |
67 | ||
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 | ||
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) | |
84 | }) | |
85 | }) | |
86 | } | |
87 | ||
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 | ||
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 | ||
108 | // --------------------------------------------------------------------------- | |
109 | ||
110 | module.exports = peertubeCrypto | |
111 | ||
112 | // --------------------------------------------------------------------------- | |
113 | ||
114 | function certsExist (callback) { | |
115 | const certPath = pathUtils.join(constants.CONFIG.STORAGE.CERT_DIR, constants.PRIVATE_CERT_NAME) | |
116 | fs.exists(certPath, function (exists) { | |
117 | return callback(exists) | |
118 | }) | |
119 | } | |
120 | ||
121 | function createCerts (callback) { | |
122 | certsExist(function (exist) { | |
123 | if (exist === true) { | |
124 | const string = 'Certs already exist.' | |
125 | logger.warning(string) | |
126 | return callback(new Error(string)) | |
127 | } | |
128 | ||
129 | logger.info('Generating a RSA key...') | |
130 | ||
131 | const privateCertPath = pathUtils.join(constants.CONFIG.STORAGE.CERT_DIR, constants.PRIVATE_CERT_NAME) | |
132 | const genRsaOptions = { | |
133 | 'out': privateCertPath, | |
134 | '2048': false | |
135 | } | |
136 | openssl.exec('genrsa', genRsaOptions, function (err) { | |
137 | if (err) { | |
138 | logger.error('Cannot create private key on this pod.') | |
139 | return callback(err) | |
140 | } | |
141 | ||
142 | logger.info('RSA key generated.') | |
143 | logger.info('Managing public key...') | |
144 | ||
145 | const publicCertPath = pathUtils.join(constants.CONFIG.STORAGE.CERT_DIR, 'peertube.pub') | |
146 | const rsaOptions = { | |
147 | 'in': privateCertPath, | |
148 | 'pubout': true, | |
149 | 'out': publicCertPath | |
150 | } | |
151 | openssl.exec('rsa', rsaOptions, function (err) { | |
152 | if (err) { | |
153 | logger.error('Cannot create public key on this pod.') | |
154 | return callback(err) | |
155 | } | |
156 | ||
157 | logger.info('Public key managed.') | |
158 | return callback(null) | |
159 | }) | |
160 | }) | |
161 | }) | |
162 | } |