switch to NON CRYPTOGRAPHICALLY SECURE rand
parent
db31dd1fc7
commit
faecf1abf5
|
@ -7,8 +7,7 @@ const error = ccall((:olm_error, libolm), Csize_t, ())
|
||||||
|
|
||||||
struct OlmAccount
|
struct OlmAccount
|
||||||
ptr::Ptr{Cvoid}
|
ptr::Ptr{Cvoid}
|
||||||
# Should I store it?
|
memory::Vector{UInt8}
|
||||||
# memory::Vector{UInt}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
struct OlmSession
|
struct OlmSession
|
||||||
|
@ -26,10 +25,11 @@ export OlmAccount
|
||||||
|
|
||||||
# # Save/Load account!
|
# # Save/Load account!
|
||||||
# a = OlmAccount()
|
# a = OlmAccount()
|
||||||
# generate_one_time_keys(a, 10)
|
# Olm.generate_one_time_keys(a, 10)
|
||||||
# # deleted after use
|
# # deleted after use
|
||||||
# enc_key = "pass" |> collect
|
# enc_key = "pass" |> collect
|
||||||
# dec_key = deepcopy(enc_key)
|
# dec_key = deepcopy(enc_key)
|
||||||
|
|
||||||
# enc_key = dec_key = Char[]
|
# enc_key = dec_key = Char[]
|
||||||
# p = pickle!(a, enc_key)
|
# p = pickle!(a, enc_key)
|
||||||
# write("account.bin", p)
|
# write("account.bin", p)
|
||||||
|
|
|
@ -2,7 +2,8 @@
|
||||||
allocate(size) = zeros(UInt8, size)
|
allocate(size) = zeros(UInt8, size)
|
||||||
const SRND = Random.RandomDevice()
|
const SRND = Random.RandomDevice()
|
||||||
"Allocate a criptographycally secure random UInt8 array of length size"
|
"Allocate a criptographycally secure random UInt8 array of length size"
|
||||||
rallocate(size) = rand(SRND, UInt8, size)
|
rallocate(size) = rand(# SRND, # FIXME: Sometimes it freeze...
|
||||||
|
UInt8, size)
|
||||||
|
|
||||||
"""Replace all elements of an array with zeros. A custom function accepting
|
"""Replace all elements of an array with zeros. A custom function accepting
|
||||||
"type" may be passed as FUNC argument used instead of ZERO"""
|
"type" may be passed as FUNC argument used instead of ZERO"""
|
||||||
|
|
|
@ -5,7 +5,7 @@ account_size() = ccall((:olm_account_size, libolm), Csize_t, ())
|
||||||
The supplied memory must be at least olm_account_size() bytes"""
|
The supplied memory must be at least olm_account_size() bytes"""
|
||||||
function account()
|
function account()
|
||||||
memory = allocate(account_size())
|
memory = allocate(account_size())
|
||||||
ccall((:olm_account, libolm), Ptr{Cvoid}, (Ptr{Cvoid},), memory)
|
(ccall((:olm_account, libolm), Ptr{Cvoid}, (Ptr{Cvoid},), memory), memory)
|
||||||
end
|
end
|
||||||
|
|
||||||
"The number of random bytes needed to create an account."
|
"The number of random bytes needed to create an account."
|
||||||
|
@ -26,7 +26,7 @@ function create(a::OlmAccount)
|
||||||
end
|
end
|
||||||
|
|
||||||
function OlmAccount()
|
function OlmAccount()
|
||||||
a = OlmAccount(account())
|
a = OlmAccount(account()...)
|
||||||
create(a)
|
create(a)
|
||||||
a
|
a
|
||||||
end
|
end
|
||||||
|
@ -37,7 +37,7 @@ used to encrypt the account then olm_account_last_error() will be
|
||||||
"BAD_ACCOUNT_KEY". If the base64 couldn't be decoded then
|
"BAD_ACCOUNT_KEY". If the base64 couldn't be decoded then
|
||||||
olm_account_last_error() will be "INVALID_BASE64". The input pickled buffer is
|
olm_account_last_error() will be "INVALID_BASE64". The input pickled buffer is
|
||||||
destroyed"""
|
destroyed"""
|
||||||
function unpickle!(a::OlmAccount, pickle::Vector{Char}, passphrase::Vector{Char})
|
function unpickle!(a::OlmAccount, pickle::Vector{UInt8}, passphrase::Vector{UInt8})
|
||||||
memlength = pickle_length(a)
|
memlength = pickle_length(a)
|
||||||
res = ccall((:olm_unpickle_account, libolm), Csize_t,
|
res = ccall((:olm_unpickle_account, libolm), Csize_t,
|
||||||
(Ptr{Cvoid},
|
(Ptr{Cvoid},
|
||||||
|
@ -45,7 +45,7 @@ function unpickle!(a::OlmAccount, pickle::Vector{Char}, passphrase::Vector{Char}
|
||||||
Ptr{Cvoid}, Csize_t,),
|
Ptr{Cvoid}, Csize_t,),
|
||||||
a.ptr,
|
a.ptr,
|
||||||
passphrase, length(passphrase),
|
passphrase, length(passphrase),
|
||||||
collect(pickle), length(pickle))
|
pickle, length(pickle))
|
||||||
# If passphrase is empty, pickle is not encrypted, delete it.
|
# If passphrase is empty, pickle is not encrypted, delete it.
|
||||||
# Else, deleting the key is fine
|
# Else, deleting the key is fine
|
||||||
erase!(length(passphrase) == 0 ? passphrase : pickle, func = rand)
|
erase!(length(passphrase) == 0 ? passphrase : pickle, func = rand)
|
||||||
|
@ -58,10 +58,16 @@ end
|
||||||
|
|
||||||
"""Initialize a pickled account. Note htat passphrase is cleared after use.
|
"""Initialize a pickled account. Note htat passphrase is cleared after use.
|
||||||
"""
|
"""
|
||||||
function OlmAccount(pickle::Vector{Char}, passphrase::Vector{Char})
|
function OlmAccount(pickle::Vector{UInt8}, passphrase::Vector{UInt8})
|
||||||
a = OlmAccount(account())
|
a = OlmAccount(account()...)
|
||||||
unpickle!(a, pickle, passphrase)
|
unpickle!(a, pickle, passphrase)
|
||||||
end
|
end
|
||||||
|
OlmAccount(pickle::Vector{UInt8}) = OlmAccount(pickle, Char[])
|
||||||
|
function OlmAccount(pickle::Vector{UInt8}, b::Base.SecretBuffer)
|
||||||
|
res = OlmAccount(pickle, b.data)
|
||||||
|
Base.shred!(b)
|
||||||
|
res
|
||||||
|
end
|
||||||
|
|
||||||
"""A null terminated string describing the most recent error to happen to an
|
"""A null terminated string describing the most recent error to happen to an
|
||||||
account"""
|
account"""
|
||||||
|
@ -88,7 +94,7 @@ supplied key. Returns the length of the pickled account on success.
|
||||||
Returns olm_error() on failure. If the pickle output buffer
|
Returns olm_error() on failure. If the pickle output buffer
|
||||||
is smaller than olm_pickle_account_length() then olm_account_last_error()
|
is smaller than olm_pickle_account_length() then olm_account_last_error()
|
||||||
will be "OUTPUT_BUFFER_TOO_SMALL"."""
|
will be "OUTPUT_BUFFER_TOO_SMALL"."""
|
||||||
function pickle!(a::OlmAccount, passphrase::Vector{Char})
|
function pickle!(a::OlmAccount, passphrase::Vector{UInt8})
|
||||||
memlength = pickle_length(a)
|
memlength = pickle_length(a)
|
||||||
memory = allocate(memlength)
|
memory = allocate(memlength)
|
||||||
res = ccall((:olm_pickle_account, libolm), Csize_t,
|
res = ccall((:olm_pickle_account, libolm), Csize_t,
|
||||||
|
@ -104,11 +110,16 @@ function pickle!(a::OlmAccount, passphrase::Vector{Char})
|
||||||
if res == memlength
|
if res == memlength
|
||||||
memory
|
memory
|
||||||
else
|
else
|
||||||
|
# TODO: Custom exceptions
|
||||||
throw(last_error(a))
|
throw(last_error(a))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
pickle!(a::OlmAccount) = pickle!(a, Char[])
|
pickle!(a::OlmAccount) = pickle!(a, UInt8[])
|
||||||
|
function pickle!(a::OlmAccount, s::Base.SecretBuffer)
|
||||||
|
res = pickle!(a, s.data)
|
||||||
|
Base.shred!(s)
|
||||||
|
res
|
||||||
|
end
|
||||||
# Base.getpass("Account encryption key")
|
# Base.getpass("Account encryption key")
|
||||||
|
|
||||||
"The size of the output buffer needed to hold the identity keys"
|
"The size of the output buffer needed to hold the identity keys"
|
||||||
|
|
Loading…
Reference in New Issue