Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
setCertificate(signerCertData: string, password?: string): void {
// the PEM file from P12 contains both, certificate and private key
// getting signer certificate
this.certificate = forge.pki.certificateFromPem(signerCertData);
if (!this.certificate)
throw new Error('Failed to decode provided certificate');
// check if signerCertData also contains private key and use it
const pemMessages = forge.pem.decode(signerCertData);
// getting signer private key
const signerKeyMessage = pemMessages.find(message =>
message.type.includes('KEY'),
);
if (signerKeyMessage)
this.setPrivateKey(forge.pem.encode(signerKeyMessage), password);
}
setCertificate(signerCertData: string, password?: string): void {
// the PEM file from P12 contains both, certificate and private key
// getting signer certificate
this.certificate = forge.pki.certificateFromPem(signerCertData);
if (!this.certificate)
throw new Error('Failed to decode provided certificate');
// check if signerCertData also contains private key and use it
const pemMessages = forge.pem.decode(signerCertData);
// getting signer private key
const signerKeyMessage = pemMessages.find(message =>
message.type.includes('KEY'),
);
if (signerKeyMessage)
this.setPrivateKey(forge.pem.encode(signerKeyMessage), password);
}
function decodePrivateKey(keydata, password, returnPEM = false) {
const pemMessages = forge.pem.decode(keydata);
// getting signer private key
const signerKeyMessage = pemMessages.find(message =>
message.type.includes('KEY'),
);
if (!signerKeyMessage) {
throw new Error('Invalid certificate, no key found');
}
const key = forge.pki.decryptRsaPrivateKey(
forge.pem.encode(signerKeyMessage),
password,
);
if (!key) {
if (
(signerKeyMessage.procType &&
signerKeyMessage.procType.type === 'ENCRYPTED') ||
signerKeyMessage.type.includes('ENCRYPTED')
) {
throw new Error('Unable to parse key, incorrect passphrase');
}
}
if (returnPEM) return forge.pki.privateKeyToPem(key);
return key;
}
setCertificate(signerCertData: string, password?: string): void {
// the PEM file from P12 contains both, certificate and private key
// getting signer certificate
this.certificate = forge.pki.certificateFromPem(signerCertData);
if (!this.certificate)
throw new Error('Failed to decode provided certificate');
// check if signerCertData also contains private key and use it
const pemMessages = forge.pem.decode(signerCertData);
// getting signer private key
const signerKeyMessage = pemMessages.find(message =>
message.type.includes('KEY'),
);
if (signerKeyMessage)
this.setPrivateKey(forge.pem.encode(signerKeyMessage), password);
}
setCertificate(signerCertData: string, password?: string): void {
// the PEM file from P12 contains both, certificate and private key
// getting signer certificate
this.certificate = forge.pki.certificateFromPem(signerCertData);
if (!this.certificate)
throw new Error('Failed to decode provided certificate');
// check if signerCertData also contains private key and use it
const pemMessages = forge.pem.decode(signerCertData);
// getting signer private key
const signerKeyMessage = pemMessages.find(message =>
message.type.includes('KEY'),
);
if (signerKeyMessage)
this.setPrivateKey(forge.pem.encode(signerKeyMessage), password);
}
function decodePrivateKey(keydata, password, returnPEM = false) {
const pemMessages = forge.pem.decode(keydata);
// getting signer private key
const signerKeyMessage = pemMessages.find(message =>
message.type.includes('KEY'),
);
if (!signerKeyMessage) {
throw new Error('Invalid certificate, no key found');
}
const key = forge.pki.decryptRsaPrivateKey(
forge.pem.encode(signerKeyMessage),
password,
);
if (!key) {
function apnCertificateFromPem(certData) {
if (!certData) {
return null;
}
var pemMessages;
try {
pemMessages = forge.pem.decode(certData);
}
catch (e) {
if (e.message.match("Invalid PEM formatted message.")) {
throw new Error("unable to parse certificate, not a valid PEM file");
}
}
var certificates = [];
pemMessages.forEach(function(message) {
if (!message.type.match(new RegExp("CERTIFICATE$"))) {
return;
}
var certAsn1 = forge.asn1.fromDer(message.body);
var forgeCertificate = forge.pki.certificateFromAsn1(certAsn1);
certificates.push(new APNCertificate(forgeCertificate));
pemMessages.forEach(function(message) {
if (!message.type.match(/KEY/)) {
return;
}
let key = forge.pki.decryptRsaPrivateKey(forge.pem.encode(message), passphrase);
if(!key) {
if ((message.procType && message.procType.type === "ENCRYPTED") || message.type.match(/ENCRYPTED/)) {
throw new Error("unable to parse key, incorrect passphrase");
}
}
else if(apnKey) {
throw new Error("multiple keys found in PEM file");
}
else {
apnKey = new APNKey(key);
}
});
return apnKey;
var isPemEncrypted = function(keyPem) {
var msg = forge.pem.decode(keyPem)[0];
if (msg.type.match(/ENCRYPTED/) || msg.procType.type === 'ENCRYPTED') {
return true;
}
}