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

Error using parse_key! on key string #71

Closed
joshbode opened this issue Sep 26, 2016 · 2 comments
Closed

Error using parse_key! on key string #71

joshbode opened this issue Sep 26, 2016 · 2 comments

Comments

@joshbode
Copy link
Contributor

I am getting an error using parse_key! on a string containing a key.

ERROR: MethodError: no method matching unsafe_convert(::Type{Ptr{Void}}, ::String)
...

The existing function is converting the key input to a String, but it looks like the C function needs an array of UInt8 which I've shown in my example below.

I am using Julia 0.5 and the latest version of MbedTLS.jl

Example

key = """\
-----BEGIN PRIVATE KEY-----
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDyyk0DCzjsQimZ
/KoOIhJQaKugt8tNaoYb4fxbgp5hIRZES1hC5dl63+hk3/1YWRmDwIzq45zk1o0T
fPPS0qFM81o9fuSeVuCeqAWKKz8DLAfAe4iif9oxtvat9svS0TCpa6l3qsZk0Q+R
0r55iBh6pSHeqzWzIef/JKOcx5Bh+ycuqgospY8RVNGqNgz0ZVCY+qodrlWwSVBv
Pd4Eyn8KQwoU5zvrM6NqYHzVWZ2GpGaSIyhsMhC8jQklJHoVOESUb21ALAZ3KwF0
rHuKVo346wbYuqPiAb6dj/7q22Qzye4IO/5fSVYTiUM7JTm+ooLuNPpnKnpg6fo7
B0spILrPAgMBAAECggEBANHZQykfWQrzwMtk1IM0tcmkPrvCQElgihutfxaFVmkz
VPCgguAHlKscvtv5shrboC79jY80PpaYTwWKGaCeB2bga539gJLPGoRnS6p56JPQ
Sb73x0hU433W223Pg6pqvvmgobBZ/fKDtEZg9dtLY4UWF6lDKD31T3V68KOA5e2Y
nNhg4cv6ETNvqpfoXdtIl+Gzg0LMHdyz+pSCrx2mtf8esEJ3wiDYoRUdrq5WRP9S
Vihui0V4JY0WJlPxPFmOZ8JGLGOWZxtKozXmWkcHq1o0r2eFmxIdwF+ZNDosHOID
RV097BUUMcnP6zRdUhk489aStTdHl5aFOcD+fS6oD6kCgYEA/u9PoGs8TCZmwYD5
EG40N2F6BtCWu+7W+bGUwEUAtBxp3KldajTTGARpTClsOvTYts/jshSsnB+e037X
VxAac0ufcKm2YnU0SADoocoSxwZzQoDn/GtalPFOH8bV6SoPZ8y7YEIpPmP9ztk9
z5vCnBw30X9uAZ5zfmWs8tAcwsUCgYEA883/24uDg34mMGnMNNwaY001D4CrjGSG
RJte9He6RAWhdyKIRC95kZGCxZ3WCnv2BsMnMveLxNzMtvCBxKNnd+Wr1UwgNAhR
tMpRTBSYtmlLrFXPy5Q9/FU6yBJ2TCrAQNJRMuoIVtuGK+B3eNufNgZDFaJ5H+UH
IS2jRyoV0IMCgYBpeMTuz67wqmqU4DErNQzxZKjBrQbP1e6m+2tZaLLz0VWS75cw
W46CP0D5O6IFeks2xyejA56uMP7a7X1WDdmDnePnDq/gaDErIQUd+qZuJSMGfqJ7
8kZVnMf/qZnSj440GaXkGztJDsFxFLvX6s9HGv2U9S2kCdQqlRJKmwNrKQKBgDim
WRkuZbScD0uYibaOniMMwZOEllfc08YFRB8R4toDZO1FvNv7HAhTA0vfzOhKvXP8
zJLvKi7oDzlC0SRbw6L8Xt0E/0aX2ERl7lX7QzC/brHwjoQDQhPryejLQWmZVWq6
c9ucvatQ/vAbDCetAGWRNu1D/1kF4o43CIV0pfT5AoGBAOcvh0Pl7r0fCECwZq3m
tT9TnkJW6OEE0jeBcUUGgBbICqD/3kfwx5cIQ30OWCU7TQQZY47xZq/APJFHLbo+
7LdqZxqYZ908PLboTFAd0BmLe26nuOzOiaiGTOmjejZ5StmcbHBjVE2n60xLO2C2
g88/oYhs3pHEXv0doTlECvrm
-----END PRIVATE KEY-----"""

Not Working

This doesn't work:

import MbedTLS
ctx = MbedTLS.PKContext()
MbedTLS.parse_key!(ctx, key)

with the error:

ERROR: MethodError: no method matching unsafe_convert(::Type{Ptr{Void}}, ::String)
Closest candidates are:
  unsafe_convert(::Type{Ptr{UInt8}}, ::String) at pointer.jl:27
  unsafe_convert(::Type{Ptr{Int8}}, ::String) at pointer.jl:28
  unsafe_convert{T}(::Type{Ptr{Void}}, ::Base.RefValue{T}) at refpointer.jl:49
  ...
 in macro expansion at /Users/josh/.julia/v0.5/MbedTLS/src/error.jl:3 [inlined]
 in parse_key!(::MbedTLS.PKContext, ::Array{UInt8,1}, ::Nullable{Union{}}) at /Users/josh/.julia/v0.5/MbedTLS/src/pk.jl:46
 in parse_key!(::MbedTLS.PKContext, ::Array{UInt8,1}) at /Users/josh/.julia/v0.5/MbedTLS/src/pk.jl:38

Working

This does work if I make these changes in pk.jl:

function parse_key!(ctx::PKContext, key, maybe_pw::Nullable = Nullable())
    key_bs = convert(Vector{UInt8}, key)  # was String(key)
    if isnull(maybe_pw)
        pw = C_NULL
        pw_size = 0
    else
        pw = convert(Vector{UInt8}, get(maybe_pw))  # was String(get(maybe_pw))
        pw_size = sizeof(pw)  # Might be off-by-one
    end
    @err_check ccall((:mbedtls_pk_parse_key, MBED_TLS), Cint,
        (Ptr{Void}, Ptr{Void}, Csize_t, Ptr{Void}, Csize_t),
        ctx.data, key_bs, sizeof(key_bs)+1, pw, pw_size)
end
@joshbode
Copy link
Contributor Author

This works, too, by changing Ptr{Void} to Ptr{Cuchar} which may make more sense:

function parse_key!(ctx::PKContext, key, maybe_pw::Nullable = Nullable())
    key_bs = String(key)
    if isnull(maybe_pw)
        pw = C_NULL
        pw_size = 0
    else
        pw = String(get(maybe_pw))
        pw_size = sizeof(pw)  # Might be off-by-one
    end
    @err_check ccall((:mbedtls_pk_parse_key, MBED_TLS), Cint,
        (Ptr{Void}, Ptr{Cuchar}, Csize_t, Ptr{Cuchar}, Csize_t),
        ctx.data, key_bs, sizeof(key_bs)+1, pw, pw_size)
end

@malmaud
Copy link
Contributor

malmaud commented Oct 18, 2016

Fixed in #72

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants