How to use the node-forge.pem function in node-forge

To help you get started, we’ve selected a few node-forge examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github walletpass / pass-js / src / template.ts View on Github external
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);
  }
github walletpass / pass-js / src / template.ts View on Github external
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);
  }
github walletpass / pass-js / src / lib / decodePrivateKey.js View on Github external
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;
}
github walletpass / pass-js / src / template.ts View on Github external
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);
  }
github walletpass / pass-js / src / template.ts View on Github external
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);
  }
github walletpass / pass-js / src / lib / decodePrivateKey.js View on Github external
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) {
github node-apn / node-apn / lib / credentials / certificate / parsePemCertificate.js View on Github external
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));
github node-apn / node-apn / lib / credentials / certificate / parsePemKey.js View on Github external
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;
github node-apn / node-apn / lib / credentials / pemKeyProperties.js View on Github external
var isPemEncrypted = function(keyPem) {
	var msg = forge.pem.decode(keyPem)[0];
	if (msg.type.match(/ENCRYPTED/) || msg.procType.type === 'ENCRYPTED') {
		return true;
	}
}