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 can I extract the public key from the x.509 certificate ? #278

Closed
tngan opened this issue Jul 4, 2015 · 6 comments
Closed

How can I extract the public key from the x.509 certificate ? #278

tngan opened this issue Jul 4, 2015 · 6 comments

Comments

@tngan
Copy link

tngan commented Jul 4, 2015

I have a x.509 certificate in string format, e.g. MIIB+jCCAWOgAwIBAgIB...
How can I extract the public key just using the string, without saving it as .pem manually first.

@dlongley
Copy link
Member

dlongley commented Jul 4, 2015

// this string format is base64-encoded DER bytes
var certString = 'MIIB+jCCAWOgAwIBAgIB...';

// base64-decode DER bytes
var certDerBytes = forge.util.decode64(certString);

// parse DER to an ASN.1 object
var obj = forge.asn1.fromDer(certDerBytes);

// convert ASN.1 object to forge certificate object
var cert = forge.pki.certificateFromAsn1(obj);

// get forge public key object
var publicKey = cert.publicKey;

// `publicKey` can now be used to verify, encrypt, etc.
publicKey.verify(...);
publicKey.encrypt(...);

// if you did want to convert it to PEM format for transport:
var pem = forge.pki.publicKeyToPem(publicKey);

@tngan
Copy link
Author

tngan commented Jul 5, 2015

It works, thanks very much.

@tngan tngan closed this as completed Jul 5, 2015
@petermikitsh
Copy link

Works great! Thanks.

@kyeotic
Copy link

kyeotic commented Oct 11, 2016

@dlongley I must be missing something, because I am getting an error when calling verify.

Encrypted message length is invalid.] length: 342, expected: 256

I am able to get the public key with the directions above. I have used this public key via the pem package, and validated this payload. I am confident it is correct.

I have tried

var md = forge.md.sha1.create()
md.update(jwt.payload, 'base64')
return key.verify(md.digest().getBytes(), jwt.signature)

and

var buf = new Buffer(jwt.payload, 'base64')
return key.verify(buf, jwt.signature)

both are failing. Am I using the verify method incorrectly?

@dlongley
Copy link
Member

dlongley commented Oct 11, 2016

@tyrsius,

The message digest update API in forge 0.6.x does not support base64 as an encoding. It should really be throwing an error to warn you of this. You'll need to use: forge.util.decode64(foo) on your data to decode it first. Also, you may need to be using a base64url decoder library for JWTs -- they don't use standard base64 encoding.

@yackermann
Copy link

Cannot read public key. OID is not RSA.

*(

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

5 participants