Reputation: 6961
I have an asymmetric key created in AWS KMS. I am trying to obtain it's public key using Javascript. To do that, I use the GetPublicKey
API that comes with the aws-sdk
.
In the API's response, the public key is returned as an Uint8Array
which represents a
a DER-encoded X.509 public key, also known as SubjectPublicKeyInfo (SPKI), as defined in RFC 5280
I am trying to retrieve the public key string from this object, so that I can use that for further encryption.
My code looks like below
import * as forge from 'node-forge';
import { GetPublicKeyCommand, KMSClient } from "@aws-sdk/client-kms";
function getPublicKeyFromKMS() {
const response = await kmsClient.send(new GetPublicKeyCommand(getKeyInput));
const uIntArrayKey = response.PublicKey!;
const bufferKey = Buffer.from(uIntArrayKey.buffer);
const cert = forge.pki.certificateFromAsn1(forge.asn1.fromDer(new forge.util.ByteStringBuffer(bufferKey), false ));
console.log(cert.publicKey)
return cert.publicKey;
}
I use the node-forge
module to get the certificate from the DER encoded response.
My issue is :
When I do forge.asn1.fromDer(new forge.util.ByteStringBuffer(bufferKey), false )
, I get the below error.
Error: Unparsed DER bytes remain after ASN.1 parsing. at Object.asn1.fromDer (/Users/####/node_modules/node-forge/lib/asn1.js:460:17)
byteCount: 8192, remaining: 8111
How do I make sure that I pass only the necessary bytes to the fromDer()
function? Or How do I handle the remaining bytes to avoid this error?
Upvotes: 1
Views: 971