digitalbazaar / forge

A native implementation of TLS in Javascript and tools to write crypto-based and network-heavy webapps
https://digitalbazaar.com/
Other
5.09k stars 785 forks source link

how to decrypt Pkcs8 from FileReader API #550

Open SZEth8 opened 6 years ago

SZEth8 commented 6 years ago

Hy!

Im working in a project that use a "Pkcs12", ".cer and .key" files format.

I need to validate the certificates for authorization and sign purpose with the "pkcs12, x.509" i don't have troubles to parse the files and validate the ownership.

But with the "pkcs8" i don't know how to decrypt and parse to validate the "x509 public key" and the "RSA key" of private key matches.

Currently im Vuejs for the user Interface, FileReader API and "Forge" library to load the "x509" and decrypt the ".key". With the "x509" file i complete the process with the following code:

       let certBase64 = '';
       let keyBase64 = '';
       let certificate = {};

        let fileReader = new FileReader();
        fileReader.onload = () => {
            try {
              if(event.target.name === 'cer') {
                 certBase64 = fileReader.result.split("base64,")[1]
              } else if(event.target.name === 'key') {
                keyBase64 = fileReader.result.split("base64,")[1]
              }
            } catch (e) {
              console.error(e)
            }
        }
        fileReader.readAsDataURL(event.target.files[0])

       let certDecoded = util.decode64(certBase64)
       certificate = pki.certificateFromPem(certDecoded)

Maybe my question it's bad oriented...

Thanks for all.

sibelius commented 3 years ago

check this https://github.com/digitalbazaar/forge/issues/292