Skip to content

Instantly share code, notes, and snippets.

@RevenueGitHubAdmin
Created June 14, 2018 16:12
Show Gist options
  • Save RevenueGitHubAdmin/8e63418835e1eb12c67169e532bed599 to your computer and use it in GitHub Desktop.
Save RevenueGitHubAdmin/8e63418835e1eb12c67169e532bed599 to your computer and use it in GitHub Desktop.
[JavaScript] How to retrieve the certificate and the private key from the KeyStore.
/*
* The following snippet uses the Forge JavaScript library (https://github.com/digitalbazaar/forge)
* to retrieve the certificate and the private key from the keystore. It receives a Forge's PKCS12
* object instance and returns a simple object containing the retrieved certificate and private key
* (assuming that the keystore contains only one certificate). The certificate is required to be sent
* with the signed request, while the private key is used to sign the request before send it.
* For more information about how to use the Forge JavaScript library, please, refer to the aforementioned URL.
*
* Gists provided for illustrative purposes only. Developers can use these as a support tool
* but the Office of the Revenue Commissioners (Revenue) does not provide any warranty with
* these gists.
*/
function getCertificateAndKeyFromKeyStore(pkcs12KeyStore) {
var certificateBag = pkcs12KeyStore.getBags({ 'bagType': forge.pki.oids.certBag });
var privateKeyBag = pkcs12KeyStore.getBags({ 'bagType': forge.pki.oids.pkcs8ShroudedKeyBag });
var certificate = certificateBag[forge.pki.oids.certBag][0].cert;
var privateKey = privateKeyBag[forge.pki.oids.pkcs8ShroudedKeyBag][0].key;
return {
certificate: certificate,
privateKey: privateKey
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment