Skip to content

Commit

Permalink
topenssl: support openssl 1.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
alaviss committed Jun 15, 2024
1 parent 02a3d3b commit 52098a7
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions tests/stdlib/web/topenssl.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import std/wordwrap
import openssl/[core_dispatch, bio, decoder, evp, rsa]
import openssl/[bio, evp, rsa]

when not defined(nimOpenssl111):
import openssl/[core_dispatch, decoder]
else:
import openssl/pem

const PubKey = r"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAknKWvrdnncCIzBnIGrZ5qtZrPH+Yo3t7ag9WZIu6Gmc/JgIDDaZhJeyGW0YSnifeAEhooWvM4jDWhTEARzktalSHqYtmwI/1Oxwp6NTYH8akMe2LCpZ5pX9FVA6m9o2tkbdXatbDKRqeD4UA8Ow7Iyrdo6eb1SU8vk+26i+uXHTtsb25p8uf2ppOJrJCy+1vr8Gsnuwny1UdoYZTxMsxRFPf+UX/LrSXMHVq/oPVa3SJ4VHMpYrG/httAugVP6K58xiZ93jst63/dd0JL85mWJu1uS3uz92aL5O97xzth3wR4BbdmDUlN4LuTIwi6DtEcC7gUOTnOzH4zgp2b5RyHwIDAQAB"
const PrivateKey = r"MIIEpAIBAAKCAQEAknKWvrdnncCIzBnIGrZ5qtZrPH+Yo3t7ag9WZIu6Gmc/JgIDDaZhJeyGW0YSnifeAEhooWvM4jDWhTEARzktalSHqYtmwI/1Oxwp6NTYH8akMe2LCpZ5pX9FVA6m9o2tkbdXatbDKRqeD4UA8Ow7Iyrdo6eb1SU8vk+26i+uXHTtsb25p8uf2ppOJrJCy+1vr8Gsnuwny1UdoYZTxMsxRFPf+UX/LrSXMHVq/oPVa3SJ4VHMpYrG/httAugVP6K58xiZ93jst63/dd0JL85mWJu1uS3uz92aL5O97xzth3wR4BbdmDUlN4LuTIwi6DtEcC7gUOTnOzH4zgp2b5RyH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g/w+hmdhHUNxV5IGmHlqFnptGyshgYgQGxMMkW0iJ1j8nLamFnkbFQOp5/UKbdPLRKiB86oPpxsqYtPXucDUqEfcMsp57mD1CpGVODbspogFpSUvQpMECkhvI0XLMbolMdo53g=="
Expand All @@ -13,18 +18,32 @@ proc rsaPemDecode(key: string, public: bool): ptr EVP_PKEY =

let bio = BIO_new_mem_buf(addr mKey[0], mKey.len.cint)
doAssert bio != nil
let selection: cint = if public: OSSL_KEYMGMT_SELECT_PUBLIC_KEY else: OSSL_KEYMGMT_SELECT_PRIVATE_KEY
let dctx = OSSL_DECODER_CTX_new_for_pkey(addr result, input_type = "PEM", input_struct = nil, keytype = "RSA", selection, nil, nil)
doAssert dctx != nil
doAssert OSSL_DECODER_from_bio(dctx, bio) == 1
doAssert result != nil
OSSL_DECODER_CTX_free(dctx)
when not defined(nimOpenssl111):
let selection: cint = if public: OSSL_KEYMGMT_SELECT_PUBLIC_KEY else: OSSL_KEYMGMT_SELECT_PRIVATE_KEY
let dctx = OSSL_DECODER_CTX_new_for_pkey(addr result, input_type = "PEM", input_struct = nil, keytype = "RSA", selection, nil, nil)
doAssert dctx != nil
doAssert OSSL_DECODER_from_bio(dctx, bio) == 1
doAssert result != nil
OSSL_DECODER_CTX_free(dctx)
else:
let rsa =
if public:
PEM_read_bio_RSA_PUBKEY(bio, nil, nil, "".cstring)
else:
PEM_read_bio_RSAPrivateKey(bio, nil, nil, "".cstring)

doAssert rsa != nil
result = EVP_PKEY_new()
doAssert result != nil
doAssert EVP_PKEY_assign_RSA(result, rsa) == 1

BIO_free_all(bio)

proc rsaPublicEncrypt(fr: string): string =
let pkey = rsaPemDecode(PubKey, public = true)
let ectx = EVP_PKEY_CTX_new_from_pkey(nil, pkey, nil)
doAssert ectx != nil
EVP_PKEY_free(pkey)
doAssert EVP_PKEY_encrypt_init(ectx) == 1
doAssert EVP_PKEY_CTX_set_rsa_padding(ectx, RSA_PKCS1_PADDING) == 1

Expand All @@ -43,6 +62,7 @@ proc rasPrivateDecrypt(fr: string): string =
let pkey = rsaPemDecode(PrivateKey, public = false)
let ectx = EVP_PKEY_CTX_new_from_pkey(nil, pkey, nil)
doAssert ectx != nil
EVP_PKEY_free(pkey)
doAssert EVP_PKEY_decrypt_init(ectx) == 1
doAssert EVP_PKEY_CTX_set_rsa_padding(ectx, RSA_PKCS1_PADDING) == 1

Expand Down

0 comments on commit 52098a7

Please sign in to comment.