Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
throw new Error('p12 keystore content is empty');
}
if (!utils.isSet(alias)) {
throw new Error('Key alias is not set');
}
if (!utils.isSet(password)) {
throw new Error('Keystore password is not set');
}
// Get asn1 from DER
let p12Asn1 = forge.asn1.fromDer(p12Content, false);
// Get p12 using the password
let p12 = forge.pkcs12.pkcs12FromAsn1(p12Asn1, false, password);
// Get Key from p12
let keyObj = p12.getBags({
friendlyName: alias,
bagType: forge.pki.oids.pkcs8ShroudedKeyBag
}).friendlyName[0];
if (!utils.isSet(keyObj)) {
throw new Error("No key found for alias [" + alias + "]");
}
return keyObj.key;
}
function parseCertificate(p12Buffer,password){
logger.info(moduleName, 'Start Parse Certificate');
var p12Der = p12Buffer.toString();
var pkcs12Asn1;
var pkcs12;
try{
pkcs12Asn1 = forge.asn1.fromDer(p12Der);
pkcs12 = forge.pkcs12.pkcs12FromAsn1(pkcs12Asn1, password || '');
}catch(ex) { //For case p12 author cert created by Tizen Studio
var p12Base64 = p12Buffer.toString('base64');
p12Der = forge.util.decode64(p12Base64);
try{
pkcs12Asn1 = forge.asn1.fromDer(p12Der);
pkcs12 = forge.pkcs12.pkcs12FromAsn1(pkcs12Asn1, password || '');
}catch(e){
logger.error(moduleName, 'Parse certificate failed, the password may not match the certificate');
common.showMsgOnWindow(common.ENUM_WINMSG_LEVEL.ERROR, 'Parse certificate failed, the password may not match the certificate');
logger.error(moduleName, ex.message);
throw e;
}
}
// load keypair and cert chain from safe content(s) and map to key ID
function decryptPkcs12FromAsn1(asn1, passphrase) {
try {
return forge.pkcs12.pkcs12FromAsn1(asn1, false, passphrase);
}
catch (e) {
// OpenSSL-exported files need an empty string, if no password was specified
// during export.
if (passphrase) {
throw e;
}
return forge.pkcs12.pkcs12FromAsn1(asn1, false, "");
}
}
pdf = Buffer.concat([
pdf.slice(0, byteRangePos),
Buffer.from(actualByteRange),
pdf.slice(byteRangeEnd),
]);
// Remove the placeholder signature
pdf = Buffer.concat([
pdf.slice(0, byteRange[1]),
pdf.slice(byteRange[2], byteRange[2] + byteRange[3]),
]);
// Convert Buffer P12 to a forge implementation.
const forgeCert = forge.util.createBuffer(p12Buffer.toString('binary'));
const p12Asn1 = forge.asn1.fromDer(forgeCert);
const p12 = forge.pkcs12.pkcs12FromAsn1(
p12Asn1,
options.asn1StrictParsing,
options.passphrase,
);
// Extract safe bags by type.
// We will need all the certificates and the private key.
const certBags = p12.getBags({
bagType: forge.pki.oids.certBag,
})[forge.pki.oids.certBag];
const keyBags = p12.getBags({
bagType: forge.pki.oids.pkcs8ShroudedKeyBag,
})[forge.pki.oids.pkcs8ShroudedKeyBag];
const privateKey = keyBags[0].key;
// Here comes the actual PKCS#7 signing.
private getCertificateInfo(certificatePath: string, certificatePassword: string): ICertificateInfo {
const certificateAbsolutePath = path.resolve(certificatePath);
const certificateContents: any = this.$fs.readFile(certificateAbsolutePath, { encoding: 'binary' });
const pkcs12Asn1 = forge.asn1.fromDer(certificateContents);
const pkcs12 = forge.pkcs12.pkcs12FromAsn1(pkcs12Asn1, false, certificatePassword);
for (let safeContens of pkcs12.safeContents) {
for (let safeBag of safeContens.safeBags) {
if (safeBag.attributes.localKeyId && safeBag.type === forge.pki.oids['certBag']) {
let issuer = safeBag.cert.issuer.getField(constants.CRYPTO.ORGANIZATION_FIELD_NAME);
return {
pemCert: forge.pki.certificateToPem(safeBag.cert),
organization: issuer && issuer.value,
validity: safeBag.cert.validity,
commonName: safeBag.cert.subject.getField(constants.CRYPTO.COMMON_NAME_FIELD_NAME).value,
friendlyName: _.head(safeBag.attributes.friendlyName)
};
}
}
}
public getCertificateInfo(certificatePath: string, certificatePassword: string): ICertificateInfo {
const certificateAbsolutePath = path.resolve(certificatePath);
const certificateContents: any = this.$fs.readFile(certificateAbsolutePath, { encoding: 'binary' });
const pkcs12Asn1 = forge.asn1.fromDer(certificateContents);
const pkcs12 = forge.pkcs12.pkcs12FromAsn1(pkcs12Asn1, false, certificatePassword);
for (let safeContens of pkcs12.safeContents) {
for (let safeBag of safeContens.safeBags) {
if (safeBag.attributes.localKeyId && safeBag.type === forge.pki.oids['certBag']) {
let issuer = safeBag.cert.issuer.getField(constants.CRYPTO.ORGANIZATION_FIELD_NAME);
return {
pemCert: forge.pki.certificateToPem(safeBag.cert),
organization: issuer && issuer.value,
validity: safeBag.cert.validity,
commonName: safeBag.cert.subject.getField(constants.CRYPTO.COMMON_NAME_FIELD_NAME).value,
friendlyName: _.head(safeBag.attributes.friendlyName)
};
}
}
}
//handling for comma values because the gateway expects it to be percent encoded
context.request.getParameters().forEach( (entry) => {
context.request.setParameter(entry.name, entry.value.replace(/,/g, "%25252C"));
});
const qs = buildQueryStringFromParams(context.request.getParameters());
const fullUrl = joinUrlAndQueryString(context.request.getUrl(), qs);
const url = smartEncodeUrl(fullUrl, true);
const mastercard = context.request.getEnvironmentVariable('mastercard');
if (mastercard) {
try {
const p12Content = fs.readFileSync(mastercard.keystoreP12Path, 'binary');
const p12Asn1 = forge.asn1.fromDer(p12Content, false);
const p12 = forge.pkcs12.pkcs12FromAsn1(p12Asn1, false, mastercard.keystorePassword);
const keyObj = p12.getBags({
friendlyName: mastercard.keyAlias,
bagType: forge.pki.oids.pkcs8ShroudedKeyBag
}).friendlyName[0];
const signingKey = forge.pki.privateKeyToPem(keyObj.key);
const authHeader = oauth.getAuthorizationHeader(URL.parse(url), context.request.getMethod(), context.request.getBodyText(), mastercard.consumerKey, signingKey);
context.request.setHeader('Authorization', authHeader);
} catch (err) {
alert(err.message);
}
}
};