-
Notifications
You must be signed in to change notification settings - Fork 4
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
Encrypted private key PEM format #508
Comments
Reposting here: Regarding the encryption of the private key for web socket server. In order to encrypt the private into an encrypted PEM file, we need to follow the PKCS#5 standard (version 2) to produce an encrypted PKCS#8 pem file. For example, in openssl, it has a pkcs8 subcommand. https://www.openssl.org/docs/man1.0.2/man1/pkcs8.html In this subcommand there is a
So this will basically end using an algorithm specified in pkcs#5 v2 RFC. However it's not clear which algorithm this is, and that it's OID is going to be. One way to solve this is to actually use the Subsequently you then just need to interrogate this file. Probably using another Actually in the updated version https://www.openssl.org/docs/man1.1.1/man1/openssl-pkcs8.html, it turns out the So that means, we can just do the encryption using This identifier is going to be an OID. The OID is just a string that looks like Use the ASN1 parser to see if you can open up the encrypted file and just console.log out the version identifier string. Once you get the confirmed OID, we can actually proceed with building this encrypted pem file. To do this, use https://github.com/PeculiarVentures/asn1-schema/blob/master/packages/pkcs8/src/encrypted_private_key_info.ts. Then construct the The next thing to do is to actually encrypt it. It is explained here. https://datatracker.ietf.org/doc/html/rfc5208#section-6 We would reuse our own existing code: const pkcs8 = new asn1Pkcs8.PrivateKeyInfo({
privateKeyAlgorithm: new asn1X509.AlgorithmIdentifier({
algorithm: x509.idEd25519,
}),
privateKey: new asn1Pkcs8.PrivateKey(
new asn1.OctetString(privateKey).toASN().toBER(),
),
}); That provides us the private key info object.
But what we actually need is something like: new asn1.OctetString(pkcs8).toASN().toBER() That then produces an arraybuffer. Then that array buffer has to go through the encryption process of AES-256-GCM, whatever is specified as part of the OID we discover through The resulting function can be called Good thing is that our libsodium supports aes256gcm. So we should be able re-use symmetric algos. We currently have not exposed this from the sodium-native JS wrapper, so you'll need to have a look at that. |
I just looked at https://github.com/sodium-friends/sodium-native/blob/master/binding.c, it doesn't export the aes256gcm from libsodium. So the only alternative right now is the peculiar webcrypto which would have it. However we need to create a new feature issue to replace webcrypto with whatever is possible in libsodium. |
Hopefully openssl here actually understands ed25519 keys. But I think your test would prove that it works. Also https://blog.pinterjann.is/ed25519-certificates.html |
Producing an encrypted PEM would be useful extra feature to the keys domain that other things could use as well. So in terms of following openssl aes256gcm, this is therefore a secure default. We are going to need to fork the sodium-native wrapper later in the future. |
Important: PeculiarVentures/asn1-schema#82 |
@tegefaulkes you can do this after you've fixed up the PR, this can be a second PR as it adds extra functionality to the keys domain that is then used by the websockets domain. |
See this comment #503 (comment), this explains how we can do this. The motivation for this whole thing is cause uWS doesn't seem to support in-memory key and certificate. The uWS API is lacking alot of bindings that are actually available in the underlying code. So we might want to create our own uWS bindings in the future, and specifically bind to the uWS C code, then we can even just use Node's openssl instead. Of course the whole thing is really about not relying on Node's version of websockets... so maybe boringssl can be used again. Not sure atm. If we end up with needing an HTTP server, I think uWS does supply HTTP too. That would mean something like |
After switching to I'm closing this as a wontfix. |
I think there can be an issue created to enable export of our root key as encrypted PEM format. The above notes I wrote can still be useful if for whatever reason such a utility is needed in the future. |
Recreated as a feature request for #550. |
Specification
In #506 we found that the
uWebsockets
library needed to read SSL keys and certs from the file system. Given this we need to write the cert chain PEM and private key PEM files to the file system so we can provide them to the web socket server.To this end we need to generate a encrypted private key PEM format that the
uWebsocket
library can use. Ideally we can use the peculiarwebcrypto
,asn1-schema
andasn1
libraries to generate this format.Given the crypto ecosystem in JS and node and the level of required knowledge, this issue is pretty tricky to tackle.
Additional context
PrivateKeyInfo
seems to be missing0x04 0x20
PeculiarVentures/asn1-schema#82Tasks
uWebsockets
can load with a provided passphrase.ClientServer
to use this when starting theuWebsockets
server.The text was updated successfully, but these errors were encountered: