Skip to content

How to write key pair to a string? #39

@deltadecay

Description

@deltadecay

Hello, I want to write a key pair EvpPKey to an IOBuffer in PEM format. How can I do that?

I assumed this would work:

using OpenSSL

rsa_key = OpenSSL.EvpPKey(rsa_generate_key())
io = IOBuffer()
write(io, rsa_key)

but it throws an error

OpenSSL.OpenSSLError("404824FD01000000:error:04880007:PEM routines:PEM_write_bio:BUF lib:crypto/pem/pem_lib.c:664:\n")

For the public key I wrote this function

function write_pubkey(io::IO, evp_pkey::OpenSSL.EvpPKey)
    bio = OpenSSL.BIO(io)
    GC.@preserve bio io begin
        # int PEM_write_bio_PUBKEY(BIO *bp, EVP_PKEY *x);
        if ccall(
            (:PEM_write_bio_PUBKEY, libcrypto),
            Cint,
            (OpenSSL.BIO, OpenSSL.EvpPKey),
            bio,
            evp_pkey) != 1
            throw(OpenSSL.OpenSSLError())
        end
    end
end
using OpenSSL

rsa_key = OpenSSL.EvpPKey(rsa_generate_key())
io = IOBuffer()
write_pubkey(io, rsa_key)

and get the same error

OpenSSL.OpenSSLError("404824FD01000000:error:04880007:PEM routines:PEM_write_bio:BUF lib:crypto/pem/pem_lib.c:664:\n")

Is there something I'm missing when it comes to configuring the IOBuffer and the OpenSSL.BIO?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions