Skip to content

Commit

Permalink
Add parse_key and parse_public_key methods
Browse files Browse the repository at this point in the history
  • Loading branch information
devmotion authored Feb 12, 2024
1 parent 4c4de15 commit c0fd1d4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
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
8 changes: 8 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,20 @@ let
@test MbedTLS.bitlength(key) == 2048
@test MbedTLS.get_name(key) == "RSA"

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 = 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

0 comments on commit c0fd1d4

Please sign in to comment.