Skip to content

Commit b3ab153

Browse files
committed
Refactoring loading private key into utils class
1 parent 7378a87 commit b3ab153

File tree

7 files changed

+1330
-6052
lines changed

7 files changed

+1330
-6052
lines changed

lib/mcapi/crypto/jwe-crypto.js

Lines changed: 7 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ function JweCrypto(config) {
8989
const encryptedText = Buffer.from(jweTokenParts[3], 'base64url');
9090
const authTag = Buffer.from(jweTokenParts[4], 'base64url');
9191

92+
// const node10 = utils.isNode10()
93+
9294
let secretKey = nodeCrypto.privateDecrypt(
9395
{
9496
key: this.privateKey,
@@ -152,111 +154,12 @@ function readPublicCertificate(publicCertificatePath) {
152154
* @private
153155
*/
154156
function getPrivateKey(config) {
155-
if (config.privateKey) {
156-
return loadPrivateKey(config.privateKey);
157-
} else if (config.keyStore) {
158-
if (config.keyStore.includes(".p12")) {
159-
return getPrivateKey12(config.keyStore, config.keyStoreAlias, config.keyStorePassword);
160-
}
161-
if (config.keyStore.includes(".pem")) {
162-
return getPrivateKeyPem(config.keyStore);
163-
}
164-
if (config.keyStore.includes(".der")) {
165-
return getPrivateKeyDer(config.keyStore);
166-
}
167-
}
168-
return null;
169-
}
170-
171-
/**
172-
* @private
173-
*/
174-
function getPrivateKey12(p12Path, alias, password) {
175-
const p12Content = fs.readFileSync(p12Path, 'binary');
176-
177-
if (!p12Content || p12Content.length <= 1) {
178-
throw new Error('p12 keystore content is empty');
179-
}
180-
181-
if (!utils.isSet(alias)) {
182-
throw new Error('Key alias is not set');
183-
}
184-
185-
if (!utils.isSet(password)) {
186-
throw new Error('Keystore password is not set');
187-
}
188-
189-
// Get asn1 from DER
190-
const p12Asn1 = forge.asn1.fromDer(p12Content, false);
191-
192-
// Get p12 using the password
193-
const p12 = forge.pkcs12.pkcs12FromAsn1(p12Asn1, false, password);
194-
195-
// Get Key from p12
196-
const keyObj = p12.getBags({
197-
friendlyName: alias,
198-
bagType: forge.pki.oids.pkcs8ShroudedKeyBag
199-
}).friendlyName[0];
200-
201-
if (!utils.isSet(keyObj)) {
202-
throw new Error("No key found for alias [" + alias + "]");
203-
}
204-
205-
return forge.pki.privateKeyToPem(keyObj.key);
206-
}
207-
208-
/**
209-
* @private
210-
*/
211-
function loadPrivateKey(privateKeyPath) {
212-
const privateKeyContent = fs.readFileSync(privateKeyPath, 'binary');
213-
214-
if (!privateKeyContent || privateKeyContent.length <= 1) {
215-
throw new Error('Private key content not valid');
216-
}
217-
218-
const privateKey = nodeCrypto.createPrivateKey({
219-
key: privateKeyContent,
220-
type: 'pkcs8',
221-
format: 'der',
222-
encoding: 'binary'
223-
});
224-
return privateKey;
225-
}
226-
227-
/**
228-
* @private
229-
*/
230-
function getPrivateKeyPem(pemPath) {
231-
const pemContent = fs.readFileSync(pemPath, 'binary');
232-
233-
if (!pemContent || pemContent.length <= 1) {
234-
throw new Error('pem keystore content is empty');
235-
}
236-
const privateKey = nodeCrypto.createPrivateKey({
237-
key: pemContent,
238-
format: 'pem'
239-
});
240-
return privateKey;
241-
}
242-
243-
/**
244-
* @private
245-
*/
246-
function getPrivateKeyDer(derPath) {
247-
const derContent = fs.readFileSync(derPath, 'binary');
248-
249-
if (!derContent || derContent.length <= 1) {
250-
throw new Error('der keystore content is empty');
157+
const privateKey = utils.getPrivateKey(config);
158+
if( privateKey ){
159+
return forge.pki.privateKeyToPem(privateKey);
160+
}else{
161+
return null;
251162
}
252-
253-
const privateKey = nodeCrypto.createPrivateKey({
254-
key: derContent,
255-
type: 'pkcs8',
256-
format: 'der',
257-
encoding: 'binary'
258-
});
259-
return privateKey;
260163
}
261164

262165
/**
@@ -368,4 +271,3 @@ function validateRootMapping(config) {
368271
}
369272

370273
module.exports = JweCrypto;
371-

lib/mcapi/crypto/legacy-crypto.js

Lines changed: 1 addition & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function LegacyCrypto(config) {
1818
this.encryptionCertificate = readPublicCertificate(config.encryptionCertificate);
1919

2020
// Load private key (for decryption)
21-
this.privateKey = getPrivateKey(config);
21+
this.privateKey = utils.getPrivateKey(config);
2222

2323
this.encoding = config.dataEncoding;
2424

@@ -165,19 +165,6 @@ function generateSecretKey(algorithm, size, digest) {
165165
throw new Error('Unsupported symmetric algorithm');
166166
}
167167

168-
/**
169-
* @private
170-
*/
171-
function loadPrivateKey(privateKeyPath) {
172-
const privateKeyContent = fs.readFileSync(privateKeyPath, 'binary');
173-
174-
if (!privateKeyContent || privateKeyContent.length <= 1) {
175-
throw new Error('Private key content not valid');
176-
}
177-
178-
return forge.pki.privateKeyFromAsn1(forge.asn1.fromDer(privateKeyContent));
179-
}
180-
181168
/**
182169
* @private
183170
*/
@@ -189,95 +176,6 @@ function readPublicCertificate(publicCertificatePath) {
189176
return forge.pki.certificateFromPem(certificateContent);
190177
}
191178

192-
/**
193-
* @private
194-
*/
195-
function getPrivateKey(config) {
196-
if (config.privateKey) {
197-
return loadPrivateKey(config.privateKey);
198-
} else if (config.keyStore) {
199-
if (config.keyStore.includes(".p12")) {
200-
return getPrivateKey12(config.keyStore, config.keyStoreAlias, config.keyStorePassword);
201-
}
202-
if (config.keyStore.includes(".pem")) {
203-
return getPrivateKeyPem(config.keyStore);
204-
}
205-
if (config.keyStore.includes(".der")) {
206-
return getPrivateKeyDer(config.keyStore);
207-
}
208-
}
209-
return null;
210-
}
211-
212-
/**
213-
* @private
214-
*/
215-
function getPrivateKey12(p12Path, alias, password) {
216-
const p12Content = fs.readFileSync(p12Path, 'binary');
217-
218-
if (!p12Content || p12Content.length <= 1) {
219-
throw new Error('p12 keystore content is empty');
220-
}
221-
222-
if (!utils.isSet(alias)) {
223-
throw new Error('Key alias is not set');
224-
}
225-
226-
if (!utils.isSet(password)) {
227-
throw new Error('Keystore password is not set');
228-
}
229-
230-
// Get asn1 from DER
231-
const p12Asn1 = forge.asn1.fromDer(p12Content, false);
232-
233-
// Get p12 using the password
234-
const p12 = forge.pkcs12.pkcs12FromAsn1(p12Asn1, false, password);
235-
236-
// Get Key from p12
237-
const keyObj = p12.getBags({
238-
friendlyName: alias,
239-
bagType: forge.pki.oids.pkcs8ShroudedKeyBag
240-
}).friendlyName[0];
241-
242-
if (!utils.isSet(keyObj)) {
243-
throw new Error("No key found for alias [" + alias + "]");
244-
}
245-
246-
return keyObj.key;
247-
}
248-
249-
/**
250-
* Load a PKCS#1 PEM (starts with "-----BEGIN RSA PRIVATE KEY-----")
251-
* or a PKCS#8 PEM (starts with "-----BEGIN PRIVATE KEY-----")"
252-
*/
253-
function getPrivateKeyPem(pemPath) {
254-
let pemContent = fs.readFileSync(pemPath, 'binary');
255-
256-
if (!pemContent || pemContent.length <= 1) {
257-
throw new Error('pem keystore content is empty');
258-
}
259-
260-
pemContent = pemContent.replace("\n", "");
261-
pemContent = pemContent.replace("\r\n", "");
262-
263-
return forge.pki.privateKeyFromPem(pemContent);
264-
}
265-
266-
/**
267-
* Load a Binary DER-encoded PKCS#8
268-
*/
269-
function getPrivateKeyDer(derPath) {
270-
const derContent = fs.readFileSync(derPath, 'binary');
271-
272-
if (!derContent || derContent.length <= 1) {
273-
throw new Error('der keystore content is empty');
274-
}
275-
276-
const pkeyAsn1 = forge.asn1.fromDer(derContent);
277-
return forge.pki.privateKeyFromAsn1(pkeyAsn1);
278-
}
279-
280-
281179
/**
282180
* @private
283181
* @param config Configuration object

lib/mcapi/utils/utils.js

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const forge = require('node-forge');
2+
const fs = require('fs');
23

34
/**
45
* Utils module
@@ -14,6 +15,9 @@ const forge = require('node-forge');
1415
module.exports.isSet = function (value) {
1516
return typeof value !== "undefined" && value !== null && value.length !== 0;
1617
};
18+
isSet = function (value) {
19+
return typeof value !== "undefined" && value !== null && value.length !== 0;
20+
};
1721

1822
/**
1923
* Converts a 'binary' encoded string of bytes to (hex or base64) encoded string.
@@ -139,3 +143,100 @@ function deleteRoot(obj, paths, properties){
139143
});
140144
}
141145
}
146+
147+
// module.exports.isNode10 = function(){
148+
// const majorVersion = parseInt(parseInt(process.version.substring(1,3)));
149+
// if(majorVersion < 10){
150+
// throw new Error("Only node version 10+ supported");
151+
// }
152+
// if(majorVersion === 10){
153+
// return true;
154+
// }
155+
// return false;
156+
// };
157+
158+
module.exports.getPrivateKey = function(config){
159+
if (config.privateKey) {
160+
return loadPrivateKey(config.privateKey);
161+
} else if (config.keyStore) {
162+
if (config.keyStore.includes(".p12")) {
163+
return getPrivateKey12(config.keyStore, config.keyStoreAlias, config.keyStorePassword);
164+
}
165+
if (config.keyStore.includes(".pem")) {
166+
return getPrivateKeyPem(config.keyStore);
167+
}
168+
if (config.keyStore.includes(".der")) {
169+
return getPrivateKeyDer(config.keyStore);
170+
}
171+
}
172+
return null;
173+
};
174+
175+
function getPrivateKey12(p12Path, alias, password) {
176+
const p12Content = fs.readFileSync(p12Path, 'binary');
177+
178+
if (!p12Content || p12Content.length <= 1) {
179+
throw new Error('p12 keystore content is empty');
180+
}
181+
182+
if (!isSet(alias)) {
183+
throw new Error('Key alias is not set');
184+
}
185+
186+
if (!isSet(password)) {
187+
throw new Error('Keystore password is not set');
188+
}
189+
190+
// Get asn1 from DER
191+
const p12Asn1 = forge.asn1.fromDer(p12Content, false);
192+
193+
// Get p12 using the password
194+
const p12 = forge.pkcs12.pkcs12FromAsn1(p12Asn1, false, password);
195+
196+
// Get Key from p12
197+
const keyObj = p12.getBags({
198+
friendlyName: alias,
199+
bagType: forge.pki.oids.pkcs8ShroudedKeyBag
200+
}).friendlyName[0];
201+
202+
if (!isSet(keyObj)) {
203+
throw new Error("No key found for alias [" + alias + "]");
204+
}
205+
206+
return keyObj.key;
207+
}
208+
209+
function getPrivateKeyPem(pemPath) {
210+
let pemContent = fs.readFileSync(pemPath, 'binary');
211+
212+
if (!pemContent || pemContent.length <= 1) {
213+
throw new Error('pem keystore content is empty');
214+
}
215+
216+
pemContent = pemContent.replace("\n", "");
217+
pemContent = pemContent.replace("\r\n", "");
218+
219+
return forge.pki.privateKeyFromPem(pemContent);
220+
}
221+
222+
function getPrivateKeyDer(derPath) {
223+
const derContent = fs.readFileSync(derPath, 'binary');
224+
225+
if (!derContent || derContent.length <= 1) {
226+
throw new Error('der keystore content is empty');
227+
}
228+
229+
const pkeyAsn1 = forge.asn1.fromDer(derContent);
230+
return forge.pki.privateKeyFromAsn1(pkeyAsn1);
231+
}
232+
233+
function loadPrivateKey(privateKeyPath) {
234+
const privateKeyContent = fs.readFileSync(privateKeyPath, 'binary');
235+
236+
if (!privateKeyContent || privateKeyContent.length <= 1) {
237+
throw new Error('Private key content not valid');
238+
}
239+
240+
return forge.pki.privateKeyFromAsn1(forge.asn1.fromDer(privateKeyContent));
241+
}
242+

0 commit comments

Comments
 (0)