diff --git a/lib/rsa-extra.js b/lib/rsa-extra.js index 6f18595..6218b5e 100644 --- a/lib/rsa-extra.js +++ b/lib/rsa-extra.js @@ -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) { diff --git a/lib/rsa-forge.js b/lib/rsa-forge.js index 8ddd5f7..5d592c8 100644 --- a/lib/rsa-forge.js +++ b/lib/rsa-forge.js @@ -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