Merge branch 'salmon-native-keys' into 'develop'

Salmon: generate private key with native crypto if available.

See merge request pleroma/pleroma!33
This commit is contained in:
lambda 2017-11-28 16:46:01 +00:00
commit 9d1c0ec18d
1 changed files with 22 additions and 9 deletions

View File

@ -73,6 +73,18 @@ def encode_key({:RSAPublicKey, modulus, exponent}) do
"RSA.#{modulus_enc}.#{exponent_enc}" "RSA.#{modulus_enc}.#{exponent_enc}"
end end
# Native generation of RSA keys is only available since OTP 20+ and in default build conditions
# We try at compile time to generate natively an RSA key otherwise we fallback on the old way.
try do
_ = :public_key.generate_key({:rsa, 2048, 65537})
def generate_rsa_pem do
key = :public_key.generate_key({:rsa, 2048, 65537})
entry = :public_key.pem_entry_encode(:RSAPrivateKey, key)
pem = :public_key.pem_encode([entry]) |> String.trim_trailing
{:ok, pem}
end
rescue
_ ->
def generate_rsa_pem do def generate_rsa_pem do
port = Port.open({:spawn, "openssl genrsa"}, [:binary]) port = Port.open({:spawn, "openssl genrsa"}, [:binary])
{:ok, pem} = receive do {:ok, pem} = receive do
@ -85,6 +97,7 @@ def generate_rsa_pem do
:error :error
end end
end end
end
def keys_from_pem(pem) do def keys_from_pem(pem) do
[private_key_code] = :public_key.pem_decode(pem) [private_key_code] = :public_key.pem_decode(pem)