Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use crypto api keys with forge #284

Closed
bfoerster opened this issue Aug 6, 2015 · 6 comments
Closed

How to use crypto api keys with forge #284

bfoerster opened this issue Aug 6, 2015 · 6 comments

Comments

@bfoerster
Copy link

Hi,
is there a way to use browsers crypto api key generation with forge?
I create a keypair using the crypto api and want to convert the resulting private key to PEM format, so i can use it in forge or export it.

var crypto = window.crypto.subtle;
crypto.generateKey({
        name: "RSA-OAEP",
        modulusLength: 2048,
        publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
        hash: {
            name: "SHA-256"
        },
    },
    true, ["encrypt", "decrypt"]).then(function (keyPair) {
    //jwk, pkcs8
    crypto.exportKey("jwk", keyPair.privateKey).then(function (exportedPrivateKey) {
        // How to convert to PEM or forge format?
        console.log(keyPair.privateKey);
        console.log(exportedPrivateKey);
    });
});

The problem is that i cannot find a way to convert between the different formats or how to tell forge that it should use window.crypto.subtle.generateKey for key generation.

Hope somebody can help me :)

@stonehouse
Copy link

This would help me out a lot too!

@dlongley
Copy link
Member

dlongley commented Aug 6, 2015

This is related to #255. You probably just want to export as 'pkcs8' and then convert to a forge buffer and use forge's asn1 API calls from there. Let me see if I can get it working real quick.

@dlongley
Copy link
Member

dlongley commented Aug 6, 2015

This worked for me:

(function() {

var crypto = window.crypto.subtle;
crypto.generateKey({
  name: "RSA-OAEP",
  modulusLength: 2048,
  publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
  hash: {
    name: "SHA-256"
  },
}, true, ["encrypt", "decrypt"]).then(function(keyPair) {
  crypto.exportKey("pkcs8", keyPair.privateKey).then(
    function(exportedPrivateKey) {
      // How to convert to PEM or forge format?
      console.log(keyPair.privateKey);
      console.log(exportedPrivateKey);

      // convert to forge buffer, then from DER, then export to PEM
      var buffer = new forge.util.ByteBuffer(exportedPrivateKey);
      var asn1 = forge.asn1.fromDer(buffer);
      var privateKey = forge.pki.privateKeyFromAsn1(asn1);
      var pem = forge.pki.privateKeyToPem(privateKey);
      console.log(pem);
    });
});

})();

Depending on what you want to do, you may be able to instead just base64-encode the exported PKCS8-formatted ArrayBuffer and then wrap it in a PEM header. But the above demonstrates how to get the key into something forge can use and export with its API.

@bfoerster
Copy link
Author

Worked for me too!!!!!
You are the best! Thank you very much!

@stonehouse
Copy link

You are my hero!

@dlongley
Copy link
Member

dlongley commented Aug 6, 2015

Haha! Great, glad it worked for you guys. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants