diff --git a/src/pk.jl b/src/pk.jl index 1613af5..25decff 100644 --- a/src/pk.jl +++ b/src/pk.jl @@ -48,6 +48,12 @@ function parse_public_key!(ctx::PKContext, key) ctx.data, key_bs, sizeof(key_bs) + 1) end +function parse_public_key(key) + ctx = PKContext() + parse_public_key!(ctx, key) + ctx +end + function parse_key!(ctx::PKContext, key, maybe_pw = nothing) key_bs = String(key) if maybe_pw === nothing @@ -62,6 +68,12 @@ function parse_key!(ctx::PKContext, key, maybe_pw = nothing) ctx.data, key_bs, sizeof(key_bs) + 1, pw, pw_size) end +function parse_key(key, maybe_pw = nothing) + ctx = PKContext() + parse_key!(ctx, key, maybe_pw) + ctx +end + function bitlength(ctx::PKContext) sz = ccall((:mbedtls_pk_get_bitlen, libmbedcrypto), Csize_t, (Ptr{Cvoid},), ctx.data) diff --git a/test/runtests.jl b/test/runtests.jl index cbd5289..a5eeabe 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -187,12 +187,22 @@ let @test MbedTLS.bitlength(key) == 2048 @test MbedTLS.get_name(key) == "RSA" + key_string = read(open(joinpath(@__DIR__, "key.pem"), "r")) + key = MbedTLS.parse_key(key_string) + @test MbedTLS.bitlength(key) == 2048 + @test MbedTLS.get_name(key) == "RSA" + pubkey_string = read(open(joinpath(@__DIR__, "public_key.pem"), "r")) pubkey = MbedTLS.PKContext() MbedTLS.parse_public_key!(pubkey, pubkey_string) @test MbedTLS.bitlength(pubkey) == 2048 @test MbedTLS.get_name(pubkey) == "RSA" + pubkey_string = read(open(joinpath(@__DIR__, "public_key.pem"), "r")) + pubkey = MbedTLS.parse_public_key(pubkey_string) + @test MbedTLS.bitlength(pubkey) == 2048 + @test MbedTLS.get_name(pubkey) == "RSA" + key = MbedTLS.parse_keyfile(joinpath(@__DIR__, "key.pem")) @test MbedTLS.bitlength(key) == 2048 @test MbedTLS.get_name(key) == "RSA"