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

Add parse_key and parse_public_key methods #282

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/pk.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading