Skip to content

Commit

Permalink
ir, genm
Browse files Browse the repository at this point in the history
  • Loading branch information
5HT committed Jul 28, 2023
1 parent da90886 commit bfdfd30
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions lib/cmp.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ defmodule CA.CMP do
# -path . -srvcert ca.pem -ref cmptestp10cr \
# -secret pass:0000 -certout $client.pem -csr $client.csr

def code(), do: :binary.encode_hex(:crypto.strong_rand_bytes(8))
def code(), do: :binary.encode_hex(:crypto.strong_rand_bytes(8))
def start(), do: :erlang.spawn(fn -> listen(1829) end)

def listen(port) do
Expand Down Expand Up @@ -54,6 +54,26 @@ defmodule CA.CMP do
:binary.part(base_key, 0, 20)
end

def message(socket, header, {:ir, req} = body, code) do

Check warning on line 57 in lib/cmp.ex

View workflow job for this annotation

GitHub Actions / build

variable "body" is unused (if the variable is not meant to be used, prefix it with an underscore)

Check warning on line 57 in lib/cmp.ex

View workflow job for this annotation

GitHub Actions / build

variable "code" is unused (if the variable is not meant to be used, prefix it with an underscore)
{:PKIHeader, pvno, from, to, messageTime, {_,oid,{_,param}} = protectionAlg, senderKID, recipKID,
transactionID, senderNonce, recipNonce, freeText, generalInfo} = header
{:ok, parameters} = :"PKIXCMP-2009".decode(:'PBMParameter', param)
{:PBMParameter, salt, {_,owf,_}, counter, {_,mac,_} } = parameters

Check warning on line 61 in lib/cmp.ex

View workflow job for this annotation

GitHub Actions / build

variable "counter" is unused (if the variable is not meant to be used, prefix it with an underscore)
:lists.map(fn {:CertReqMsg, req, sig, code} ->
:io.format 'request: ~p~n', [req]
:io.format 'signature: ~p~n', [sig]
:io.format 'code: ~p~n', [code]
end, req)
end

def message(socket, header, {:genm, req} = body, code) do
{:PKIHeader, pvno, from, to, messageTime, {_,oid,{_,param}} = protectionAlg, senderKID, recipKID,
transactionID, senderNonce, recipNonce, freeText, generalInfo} = header
{:ok, parameters} = :"PKIXCMP-2009".decode(:'PBMParameter', param)
{:PBMParameter, salt, {_,owf,_}, counter, {_,mac,_} } = parameters
:io.format 'generalMessage: ~p~n', [req]
end

def message(socket, header, {:p10cr, csr} = body, code) do
{:PKIHeader, pvno, from, to, messageTime, {_,oid,{_,param}} = protectionAlg, senderKID, recipKID,
transactionID, senderNonce, recipNonce, freeText, generalInfo} = header
Expand Down Expand Up @@ -101,14 +121,11 @@ defmodule CA.CMP do
# :io.format 'issuedOTP: ~p~n', [cert]
# :io.format 'issuedPKIX: ~p~n', [convertOTPtoPKIX(cert)]

pkiheader = CA."PKIHeader"(sender: to,
recipient: from,
pvno: pvno,
protectionAlg: protectionAlg,
messageTime: messageTime,
transactionID: transactionID)
pkibody = {:cp, reply}
pkibody = {:cp, reply}
pkiheader = CA."PKIHeader"(sender: to, recipient: from, pvno: pvno,
transactionID: transactionID, protectionAlg: protectionAlg, messageTime: messageTime)
outgoingProtection = CA."ProtectedPart"(header: pkiheader, body: pkibody)

{:ok, out} = :"PKIXCMP-2009".encode(:'ProtectedPart', outgoingProtection)
kdf = :crypto.pbkdf2_hmac(:sha256, out, salt, counter, 20)
# kdf = mac(out, salt, counter)
Expand All @@ -117,6 +134,10 @@ defmodule CA.CMP do
answer(socket, pkiheader, pkibody, kdf)
end

def message(_socket, _header, body, _code) do
:logger.info 'Unknown message request ~p', [body]
end

def answer(socket, header, body, code) do
message = CA."PKIMessage"(header: header, body: body, protection: code)
{:ok, bytes} = :'PKIXCMP-2009'.encode(:'PKIMessage', message)
Expand Down

0 comments on commit bfdfd30

Please sign in to comment.