diff options
Diffstat (limited to 'server/helpers/peertube-crypto.ts')
-rw-r--r-- | server/helpers/peertube-crypto.ts | 103 |
1 files changed, 48 insertions, 55 deletions
diff --git a/server/helpers/peertube-crypto.ts b/server/helpers/peertube-crypto.ts index 89aef99c4..47f0243e7 100644 --- a/server/helpers/peertube-crypto.ts +++ b/server/helpers/peertube-crypto.ts | |||
@@ -1,5 +1,4 @@ | |||
1 | import * as crypto from 'crypto' | 1 | import * as crypto from 'crypto' |
2 | import * as Promise from 'bluebird' | ||
3 | import { join } from 'path' | 2 | import { join } from 'path' |
4 | 3 | ||
5 | import { | 4 | import { |
@@ -41,7 +40,7 @@ function checkSignature (publicKey: string, data: string, hexSignature: string) | |||
41 | return isValid | 40 | return isValid |
42 | } | 41 | } |
43 | 42 | ||
44 | function sign (data: string|Object) { | 43 | async function sign (data: string|Object) { |
45 | const sign = crypto.createSign(SIGNATURE_ALGORITHM) | 44 | const sign = crypto.createSign(SIGNATURE_ALGORITHM) |
46 | 45 | ||
47 | let dataString: string | 46 | let dataString: string |
@@ -52,33 +51,33 @@ function sign (data: string|Object) { | |||
52 | dataString = JSON.stringify(data) | 51 | dataString = JSON.stringify(data) |
53 | } catch (err) { | 52 | } catch (err) { |
54 | logger.error('Cannot sign data.', err) | 53 | logger.error('Cannot sign data.', err) |
55 | return Promise.resolve('') | 54 | return '' |
56 | } | 55 | } |
57 | } | 56 | } |
58 | 57 | ||
59 | sign.update(dataString, 'utf8') | 58 | sign.update(dataString, 'utf8') |
60 | 59 | ||
61 | return getMyPrivateCert().then(myKey => { | 60 | const myKey = await getMyPrivateCert() |
62 | return sign.sign(myKey, SIGNATURE_ENCODING) | 61 | return await sign.sign(myKey, SIGNATURE_ENCODING) |
63 | }) | ||
64 | } | 62 | } |
65 | 63 | ||
66 | function comparePassword (plainPassword: string, hashPassword: string) { | 64 | function comparePassword (plainPassword: string, hashPassword: string) { |
67 | return bcryptComparePromise(plainPassword, hashPassword) | 65 | return bcryptComparePromise(plainPassword, hashPassword) |
68 | } | 66 | } |
69 | 67 | ||
70 | function createCertsIfNotExist () { | 68 | async function createCertsIfNotExist () { |
71 | return certsExist().then(exist => { | 69 | const exist = await certsExist() |
72 | if (exist === true) { | 70 | if (exist === true) { |
73 | return undefined | 71 | return undefined |
74 | } | 72 | } |
75 | 73 | ||
76 | return createCerts() | 74 | return await createCerts() |
77 | }) | ||
78 | } | 75 | } |
79 | 76 | ||
80 | function cryptPassword (password: string) { | 77 | async function cryptPassword (password: string) { |
81 | return bcryptGenSaltPromise(BCRYPT_SALT_SIZE).then(salt => bcryptHashPromise(password, salt)) | 78 | const salt = await bcryptGenSaltPromise(BCRYPT_SALT_SIZE) |
79 | |||
80 | return await bcryptHashPromise(password, salt) | ||
82 | } | 81 | } |
83 | 82 | ||
84 | function getMyPrivateCert () { | 83 | function getMyPrivateCert () { |
@@ -105,51 +104,45 @@ export { | |||
105 | 104 | ||
106 | // --------------------------------------------------------------------------- | 105 | // --------------------------------------------------------------------------- |
107 | 106 | ||
108 | function certsExist () { | 107 | async function certsExist () { |
109 | const certPath = join(CONFIG.STORAGE.CERT_DIR, PRIVATE_CERT_NAME) | 108 | const certPath = join(CONFIG.STORAGE.CERT_DIR, PRIVATE_CERT_NAME) |
110 | 109 | ||
111 | // If there is an error the certificates do not exist | 110 | // If there is an error the certificates do not exist |
112 | return accessPromise(certPath) | 111 | try { |
113 | .then(() => true) | 112 | await accessPromise(certPath) |
114 | .catch(() => false) | 113 | |
114 | return true | ||
115 | } catch { | ||
116 | return false | ||
117 | } | ||
115 | } | 118 | } |
116 | 119 | ||
117 | function createCerts () { | 120 | async function createCerts () { |
118 | return certsExist().then(exist => { | 121 | const exist = await certsExist() |
119 | if (exist === true) { | 122 | if (exist === true) { |
120 | const errorMessage = 'Certs already exist.' | 123 | const errorMessage = 'Certs already exist.' |
121 | logger.warning(errorMessage) | 124 | logger.warning(errorMessage) |
122 | throw new Error(errorMessage) | 125 | throw new Error(errorMessage) |
123 | } | 126 | } |
124 | 127 | ||
125 | logger.info('Generating a RSA key...') | 128 | logger.info('Generating a RSA key...') |
126 | 129 | ||
127 | const privateCertPath = join(CONFIG.STORAGE.CERT_DIR, PRIVATE_CERT_NAME) | 130 | const privateCertPath = join(CONFIG.STORAGE.CERT_DIR, PRIVATE_CERT_NAME) |
128 | const genRsaOptions = { | 131 | const genRsaOptions = { |
129 | 'out': privateCertPath, | 132 | 'out': privateCertPath, |
130 | '2048': false | 133 | '2048': false |
131 | } | 134 | } |
132 | return opensslExecPromise('genrsa', genRsaOptions) | 135 | |
133 | .then(() => { | 136 | await opensslExecPromise('genrsa', genRsaOptions) |
134 | logger.info('RSA key generated.') | 137 | logger.info('RSA key generated.') |
135 | logger.info('Managing public key...') | 138 | logger.info('Managing public key...') |
136 | 139 | ||
137 | const publicCertPath = join(CONFIG.STORAGE.CERT_DIR, 'peertube.pub') | 140 | const publicCertPath = join(CONFIG.STORAGE.CERT_DIR, 'peertube.pub') |
138 | const rsaOptions = { | 141 | const rsaOptions = { |
139 | 'in': privateCertPath, | 142 | 'in': privateCertPath, |
140 | 'pubout': true, | 143 | 'pubout': true, |
141 | 'out': publicCertPath | 144 | 'out': publicCertPath |
142 | } | 145 | } |
143 | return opensslExecPromise('rsa', rsaOptions) | 146 | |
144 | .then(() => logger.info('Public key managed.')) | 147 | await opensslExecPromise('rsa', rsaOptions) |
145 | .catch(err => { | ||
146 | logger.error('Cannot create public key on this pod.') | ||
147 | throw err | ||
148 | }) | ||
149 | }) | ||
150 | .catch(err => { | ||
151 | logger.error('Cannot create private key on this pod.') | ||
152 | throw err | ||
153 | }) | ||
154 | }) | ||
155 | } | 148 | } |