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

[help needed] Loading an encrypted PKCS#8 private key #386

Open
lviggiani opened this issue Nov 24, 2023 · 0 comments
Open

[help needed] Loading an encrypted PKCS#8 private key #386

lviggiani opened this issue Nov 24, 2023 · 0 comments

Comments

@lviggiani
Copy link

Hi, I'm trying to load an encrypted PKCS#8 private key without success.
Here is my code. In the first part i load an unecrypted PKCS#8 key with success, in hte second part I try loading the encrypted key with error:

// Load Private Key PKCS#8
let buffer1 = pvtsutils.BufferSourceConverter.toArrayBuffer(await Deno.readFile("./domain-pkcs8-nocrypt.key"));
let bynary1 = pvtsutils.Convert.ToBinary(buffer1);
let ber = decodePEM(bynary1, "PRIVATE KEY")[0];
let key = pkijs.PrivateKeyInfo.fromBER(ber);
console.log(key); // --->>> OK

// This fails
buffer1 = pvtsutils.BufferSourceConverter.toArrayBuffer(await Deno.readFile("./domain-pkcs8.key"));
bynary1 = pvtsutils.Convert.ToBinary(buffer1);
ber = decodePEM(bynary1, "ENCRYPTED PRIVATE KEY")[0];
key = pkijs.PrivateKeyInfo.fromBER(ber);
console.log(key);
error: Uncaught (in promise) AsnError: Cannot create 'PrivateKeyInfo' from ASN.1 object
            throw new AsnError(`Cannot create '${this.CLASS_NAME}' from ASN.1 object`);

Keys are generated and converted like this:

# create certificate and private key
openssl req -newkey rsa:2048 -keyout domain.key -x509 -days 365 -out domain.crt

# converti certificate in pem format:
openssl x509 -in domain.crt -out domain.pem -outform PEM

# convert private key in encrypted pkcs#8 format:
openssl pkcs8 -topk8 -inform PEM -outform PEM -in domain.key -out domain-pkcs8.key

# convert private key in pkcs#8 format:
openssl pkcs8 -topk8 -inform PEM -outform PEM -nocrypt -in domain.key -out domain-pkcs8.key

# sign a file like this:
openssl cms -sign -signer domain.pem -inkey domain-pkcs8.key -binary -in README.md -outform der -out signature
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

1 participant