fix #4 exportPublicPem from privateKeyPem when using forge

This commit is contained in:
AJ ONeal 2016-08-01 10:01:52 -04:00
parent ce365e9248
commit 3cda5ffaf8
2 changed files with 33 additions and 23 deletions

View File

@ -70,27 +70,8 @@ var extrac = module.exports = {
//
, _forgeImportJwk: require('./rsa-forge')._forgeImportJwk
, _forgeImportPublicJwk: require('./rsa-forge')._forgeImportPublicJwk
, _forgeImportPem: function (keypair) {
if (!keypair._forge && keypair.privateKeyPem) {
keypair._forge = forge.pki.privateKeyFromPem(keypair.privateKeyPem);
}
keypair._forge.toJSON = notToJson;
extrac._forgeImportPublicPem(keypair);
}
, _forgeImportPublicPem: function (keypair) {
if (keypair._forgePublic) {
return;
}
if (keypair._forge) {
keypair._forgePublic = forge.pki.rsa.setPublicKey(keypair._forge.n, keypair._forge.e);
}
else if (keypair.publicKeyPem) {
keypair._forgePublic = keypair._forgePublic || forge.pki.publicKeyFromPem(keypair.publicKeyPem);
}
keypair._forgePublic.toJSON = notToJson;
}
, _forgeImportPem: require('./rsa-forge')._forgeImportPem
, _forgeImportPublicPem: require('./rsa-forge')._forgeImportPublicPem
, importForge: function (keypair) {
extrac._forgeImportJwk(keypair);
if (keypair.privateKeyPem) {

View File

@ -117,6 +117,27 @@ var forgec = module.exports = {
}
keypair._forgePublic.toJSON = notToJson;
}
, _forgeImportPem: function (keypair) {
if (!keypair._forge && keypair.privateKeyPem) {
keypair._forge = forge.pki.privateKeyFromPem(keypair.privateKeyPem);
}
keypair._forge.toJSON = notToJson;
forgec._forgeImportPublicPem(keypair);
}
, _forgeImportPublicPem: function (keypair) {
if (keypair._forgePublic) {
return;
}
if (keypair._forge) {
keypair._forgePublic = forge.pki.rsa.setPublicKey(keypair._forge.n, keypair._forge.e);
}
else if (keypair.publicKeyPem) {
keypair._forgePublic = keypair._forgePublic || forge.pki.publicKeyFromPem(keypair.publicKeyPem);
}
keypair._forgePublic.toJSON = notToJson;
}
, import: function (keypair) {
// no-op since this must be done anyway in extra
return keypair;
@ -153,11 +174,19 @@ var forgec = module.exports = {
forgec._forgeImportPublicJwk(keypair);
}
if (keypair._forge) {
if (!keypair._forge) {
if (keypair.privateKeyPem) {
forgec._forgeImportPem(keypair);
}
}
if (keypair.publicKeyPem) {
return keypair.publicKeyPem;
}
if (keypair._forge || keypair._forgePublic) {
return forge.pki.publicKeyToPem(keypair._forgePublic || keypair._forge);
}
throw new Error("None of publicKeyPem, _forge, publicKeyJwk, privateKeyPem, or privateKeyJwk found. No way to export public key PEM");
throw new Error("None of publicKeyPem, _forge, _forgePublic, publicKeyJwk, privateKeyPem, or privateKeyJwk found. No way to export public key PEM");
}
//, exportPrivateKeyJwk: NOT IMPLEMENTED HERE
//, exportPublicKeyJwk: NOT IMPLEMENTED HERE