kjur / jsrsasign

The 'jsrsasign' (RSA-Sign JavaScript Library) is an opensource free cryptography library supporting RSA/RSAPSS/ECDSA/DSA signing/validation, ASN.1, PKCS#1/5/8 private/public key, X.509 certificate, CRL, OCSP, CMS SignedData, TimeStamp, CAdES and JSON Web Signature/Token in pure JavaScript.
https://kjur.github.io/jsrsasign
Other
3.27k stars 643 forks source link

Cannot read RSA Public Key #253

Closed nathalials closed 7 years ago

nathalials commented 7 years ago

Hey, I'm trying to parse information of a PEM certificate file and I'm having some issues.

This is the certificate content:

----BEGIN CERTIFICATE-----
MIIDJzCCAg8CAQswDQYJKoZIhvcNAQELBQAwbTELMAkGA1UEBhMCVVMxDjAMBgNV
BAgTBVRleGFzMQ8wDQYDVQQHEwZBdXN0aW4xDTALBgNVBAoTBERFTU8xFzAVBgNV
BAsTDkRFTU8gQ29ycG9yYXRlMRUwEwYDVQQDEwxERU1PIFJvb3QgQ0EwHhcNMTcw
NDExMTY0NjE5WhcNMjcwMTA5MTY0NjE5WjBGMUQwQgYDVQQDDDs5eTNnNXEubWVz
c2FnaW5nLmZyYTAyLTMudGVzdC5pbnRlcm5ldG9mdGhpbmdzLmlibWNsb3VkLmNv
bTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBANrwaPyG3KshIzr7Kvo9
uzQZO675VkS+WO9JKUiaYYaYbMPDIZqEvnYheKPlXDVPTxViE7Zat0wGcFI8AguM
ZzYUIWfeJCXUFJXORGVwWuSfYRrRabf9ReAV7kYdeR8kfzCXFT+6nsgbRqYMCi2q
nRaGCs3+WOgOsVa71VerngkRpVpFjv15V93bqFFkUKcA08Q5eP5DzgXxfX/kgXWK
Gjb6VsbADhaZzeM/jCrP3kvYRfofHCzKbZOJGrmvd6il+a5CKOL00IHUktN7shhp
sMqfPyHWgWy6Ik+pA9rGut5XzGeLoRZiZnRudWTcQa2c2POH5fyxN6edj2CXWYY9
5zUCAwEAATANBgkqhkiG9w0BAQsFAAOCAQEATOfLas14rU9Lrq2zNj9iHpuMXKBW
XCoFduttNA5VGgZYFHy1NrgYleDISRCjCk9lXQsjV/m7VnBTrI3ncwYvVdS+n2Dv
UN9zcm8+SzyVDBzXVl+kXOUXy5rHGagxqT3M1cij3NAxosufzXn61fSRCaVgezEF
7crQjMhYPepbYCvsv6NGzdA2D3+uAfb4hq3J8y1qHrNxBcgKogeGWMs0/I827YBB
vAFw1qhcoTZb+EymNNl5xstmby25fd3jVVOwa5FbrImCdCglNbq+7UC4ZgCd9F7F
0MMxg4N7v8ruoVffaUwKKNtNykA/sKyrtOhEdTbbRNToSOjD99Adc4rhoQ==
-----END CERTIFICATE-----

It's possible to parse and check all certificate information in terminal, including the public key.

This is the code to get public key information:

      x509.readCertPEM(raw);
      const type = x509.subjectPublicKeyRSA.type;
      const pubKey = X509.getPublicKeyFromCertPEM(raw).n.bitLength();

But the library is returning me this error (when I run the code above): Uncaught malformed X.509 certificate PEM (code:003)

Would you mind help me understand why this occurs, please?

kjur commented 7 years ago

readCertPEM seems to have some bug. Please try following:

hCert = ASN1HEX.pemToHex(pemCertString);
pubkey = X509.getPublicKeyFromHex(hCert);
keylen = pubkey.n.bitLength(); // for RSA public key

readCertPEM can't read X509v1 certificate such like your certificate and read only RSA public key certificate. I'll fix this in the next version.

nathalials commented 7 years ago

Ok. It worked for public key, thanks! Now ... how can I get other certificate information? (since readCertPEM has a bug, methods like x509.getSubjectString, x509.getNotAfter, getNotBefore is not working - on this certificate pem above)

kjur commented 7 years ago

Yes, they are needed to X509v1 certificate support. Please wait future update.

nathalials commented 7 years ago

For now, it's not possible to parse any information of this certificate?

X509v1 means version 1?

kjur commented 7 years ago

Yes, you are right. It's a v1 certificate.

nathalials commented 7 years ago

Do you have any idea when it will support X509v1?

kjur commented 7 years ago

I hope v1 supprt update will be released in a few days.

kjur commented 7 years ago

I've released 7.1.4 which supports fields for X509v1 certificate today.

nathalials commented 7 years ago

wow, that was fast! thank you very much!