Skip to content
Merged
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
35 changes: 35 additions & 0 deletions src/OpenSSL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2010,6 +2010,41 @@ function Base.write(io::IO, evp_pkey::EvpPKey, evp_cipher::EvpCipher=EvpCipher(C
end
end

function write_pubkey(io::IO, evp_pkey::EvpPKey)
bio = BIO(BIOMethodMemory())

GC.@preserve bio begin
if ccall(
(:PEM_write_bio_PUBKEY, libcrypto),
Cint,
(BIO, EvpPKey),
bio,
evp_pkey) != 1
throw(OpenSSLError())
end
return write(io, bio_get_mem_data(bio))
end
end

function read_pubkey(in_string::AbstractString)::EvpPKey
bio = BIO(BIOMethodMemory())
write(bio, in_string)

evp_pkey = ccall(
(:PEM_read_bio_PUBKEY, libcrypto),
Ptr{Cvoid},
(BIO, Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}),
bio,
C_NULL,
C_NULL,
C_NULL)
if evp_pkey == C_NULL
throw(OpenSSLError())
end
free(bio)
return EvpPKey(evp_pkey)
end

function get_key_type(evp_pkey::EvpPKey)::EvpPKeyType
pkey_type = ccall(
(EVP_PKEY_base_id, libcrypto),
Expand Down
16 changes: 16 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,22 @@ end
free(_evp_pkey)
end


@testset "SerializePublicKey" begin
evp_pkey = EvpPKey(rsa_generate_key())

io = IOBuffer()
n = OpenSSL.write_pubkey(io, evp_pkey)
pubkey_pem = String(take!(io))
@test n == sizeof(pubkey_pem)

@test startswith(pubkey_pem, "-----BEGIN PUBLIC KEY-----")

_evp_pkey = OpenSSL.read_pubkey(pubkey_pem)

@test _evp_pkey == evp_pkey
end

@testset "DSA" begin
evp_pkey = EvpPKey(dsa_generate_key())
@test occursin("priv:", String(evp_pkey))
Expand Down
Loading