Skip to content

Commit

Permalink
tree
Browse files Browse the repository at this point in the history
  • Loading branch information
5HT committed Oct 23, 2024
1 parent db47c1d commit e9787e7
Show file tree
Hide file tree
Showing 13 changed files with 15 additions and 19 deletions.
18 changes: 1 addition & 17 deletions lib/ca.ex → lib/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,14 @@ defmodule CA do
require Record
Enum.each(Record.extract_all(from_lib: "ca/include/PKIXCMP-2009.hrl"),
fn {name, definition} -> Record.defrecord(name, definition) end)

Enum.each(Record.extract_all(from_lib: "public_key/include/public_key.hrl"),
fn {name, definition} -> Record.defrecord(name, definition) end)

def init([]), do: {:ok, { {:one_for_one, 5, 10}, []} }
def start(_type, _args) do
:logger.add_handlers(:ca)
CA.CMP.start
CA.CMC.start
CA.TSP.start
CA.OCSP.start
CA.CMP.start ; CA.CMC.start ; CA.TSP.start ; CA.OCSP.start
:supervisor.start_link({:local, __MODULE__}, __MODULE__, [])
end

def parseSubj(csr) do
{:CertificationRequest, {:CertificationRequestInfo, v, subj, x, y}, b, c} = csr
{:CertificationRequest, {:CertificationRequestInfo, v, CA.CAdES.subj(subj), x, y}, b, c}
end

def convertOTPtoPKIX(cert) do
{:Certificate,{:TBSCertificate,:v3,a,ai,rdn,v,rdn2,{p1,{p21,p22,_pki},p3},b,c,ext},ai,code} =
:public_key.pkix_decode_cert(:public_key.pkix_encode(:OTPCertificate, cert, :otp), :plain)
{:Certificate,{:TBSCertificate,:v3,a,ai,CA.CAdES.unsubj(rdn),v,CA.CAdES.unsubj(rdn2),
{p1,{p21,p22,{:namedCurve,{1,3,132,0,34}}},p3},b,c,ext},ai,code}
end

end
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
16 changes: 14 additions & 2 deletions lib/cmp.ex → lib/services/cmp.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ defmodule CA.CMP do
@moduledoc "CA/CMP TCP server."
require CA

def parseSubj(csr) do
{:CertificationRequest, {:CertificationRequestInfo, v, subj, x, y}, b, c} = csr
{:CertificationRequest, {:CertificationRequestInfo, v, CA.CAdES.subj(subj), x, y}, b, c}
end

def convertOTPtoPKIX(cert) do
{:Certificate,{:TBSCertificate,:v3,a,ai,rdn,v,rdn2,{p1,{p21,p22,_pki},p3},b,c,ext},ai,code} =
:public_key.pkix_decode_cert(:public_key.pkix_encode(:OTPCertificate, cert, :otp), :plain)
{:Certificate,{:TBSCertificate,:v3,a,ai,CA.CAdES.unsubj(rdn),v,CA.CAdES.unsubj(rdn2),
{p1,{p21,p22,{:namedCurve,{1,3,132,0,34}}},p3},b,c,ext},ai,code}
end

def start(), do: :erlang.spawn(fn -> listen(1829) end)

def listen(port) do
Expand Down Expand Up @@ -89,14 +101,14 @@ defmodule CA.CMP do

{ca_key, ca} = CA.CSR.read_ca()
subject = X509.CSR.subject(csr)
true = X509.CSR.valid?(CA.parseSubj(csr))
true = X509.CSR.valid?(parseSubj(csr))
cert = X509.Certificate.new(X509.CSR.public_key(csr), CA.CAdES.subj(subject), ca, ca_key,
extensions: [subject_alt_name: X509.Certificate.Extension.subject_alt_name(["synrc.com"]) ])

reply = CA."CertRepMessage"(response:
[ CA."CertResponse"(certReqId: 0,
certifiedKeyPair: CA."CertifiedKeyPair"(certOrEncCert:
{:certificate, {:x509v3PKCert, CA.convertOTPtoPKIX(cert)}}),
{:certificate, {:x509v3PKCert, convertOTPtoPKIX(cert)}}),
status: CA."PKIStatusInfo"(status: 0))])

pkibody = {:cp, reply}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit e9787e7

Please sign in to comment.