Skip to content

Commit

Permalink
CA
Browse files Browse the repository at this point in the history
  • Loading branch information
5HT committed Jul 17, 2023
1 parent 16fe8e0 commit 73b5149
Show file tree
Hide file tree
Showing 15 changed files with 16,352 additions and 14 deletions.
15 changes: 10 additions & 5 deletions lib/cmp.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
defmodule CA.CMP do

# IETF 2510:2005 X.509 PKI CMP

# openssl cmp -cmd genm -server 127.0.0.1:829 \
# -recipient "/CN=CMPserver" -ref 1234 -secret pass:0000

def start(), do: :erlang.spawn(fn -> listen(829) end)
def listen(port) do
{:ok, socket} = :gen_tcp.listen(port,
Expand All @@ -14,11 +19,11 @@ defmodule CA.CMP do
def loop(socket) do
case :gen_tcp.recv(socket, 0) do
{:ok, data} ->
:io.format '~p~n', [data]
case :'LDAP'.decode(:'LDAPMessage',data) do
{:ok,decoded} -> :io.format '~p~n', [decoded] ; loop(socket)
{:error,_} -> :exit
end
{{_,headers},asn} = :asn1rt_nif.decode_ber_tlv(data)
[_,body] = :string.split asn, "\r\n\r\n", :all
dec = :'PKIXCMP-2009'.decode(:'PKIMessage', body)
:io.format 'CMP: ~ps~n', [dec]
loop(socket)
{:error, :closed} -> :exit
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/cms.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ defmodule CA.CMS do
sharedKey = :crypto.compute_key(:ecdh,publicKey,privateKeyBin,scheme)
{_,payload} = :'CMSECCAlgs-2009-02'.encode(:'ECC-CMS-SharedInfo', sharedInfo(ukm,256))
derived = case map(kdf) do
{:kdf,hash} -> KDF.derive({:kdf,hash}, sharedKey, 32, payload)
{:hkdf,hash} -> HKDF.derive({:kdf,hash}, sharedKey, 32, payload)
{:kdf,hash} -> CA.KDF.derive({:kdf,hash}, sharedKey, 32, payload)
{:hkdf,hash} -> CA.HKDF.derive({:kdf,hash}, sharedKey, 32, payload)
end
unwrap = CA.AES.KW.unwrap(encryptedKey, derived)
res = CA.AES.decrypt(enc, data, unwrap, iv)
Expand Down
2 changes: 1 addition & 1 deletion lib/cms_test.ex
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ defmodule CA.CMS.Test do
[{:kari,{_,:v3,{_,{_,_,publicKey}},ukm,_,[{_,_,encryptedKey}]}}|_] = x
sharedKey = :crypto.compute_key(:ecdh,publicKey,privateKey,scheme)
{_,content} = :'CMSECCAlgs-2009-02'.encode(:'ECC-CMS-SharedInfo', CA.CMS.sharedInfo(ukm,256))
kdf = KDF.derive({:kdf, :sha256}, sharedKey, 32, content)
kdf = CA.KDF.derive({:kdf, :sha256}, sharedKey, 32, content)
unwrap = :aes_kw.unwrap(encryptedKey, kdf)
CA.AES.decrypt(:'id-aes256-CBC', data, unwrap, iv)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/csr.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule CSR do
defmodule CA.CSR do

def ca() do
ca_key = X509.PrivateKey.new_ec(:sect571r1)
Expand Down
2 changes: 1 addition & 1 deletion lib/hkdf.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule HKDF do
defmodule CA.HKDF do

def hl(:sha), do: 20
def hl(:sha224), do: 28
Expand Down
2 changes: 1 addition & 1 deletion lib/kdf.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule KDF do
defmodule CA.KDF do

def hl(:md5), do: 16
def hl(:sha), do: 20
Expand Down
4 changes: 1 addition & 3 deletions lib/ldap.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
defmodule CA.LDAP do

def start(), do: :erlang.spawn(fn -> listen(389) end)
def listen(port) do
{:ok, socket} = :gen_tcp.listen(port,
Expand All @@ -14,9 +13,8 @@ defmodule CA.LDAP do
def loop(socket) do
case :gen_tcp.recv(socket, 0) do
{:ok, data} ->
:io.format '~p~n', [data]
case :'LDAP'.decode(:'LDAPMessage',data) do
{:ok,decoded} -> :io.format '~p~n', [decoded] ; loop(socket)
{:ok,decoded} -> :io.format 'LDAP: ~p~n', [decoded] ; loop(socket)
{:error,_} -> :exit
end
{:error, :closed} -> :exit
Expand Down
Loading

0 comments on commit 73b5149

Please sign in to comment.