From e9787e724ae3374278a3fd65cb7880aaf873b639 Mon Sep 17 00:00:00 2001 From: 5HT Date: Wed, 23 Oct 2024 21:57:39 +0300 Subject: [PATCH] tree --- lib/{ca.ex => application.ex} | 18 +----------------- lib/{ => derivation}/hkdf.ex | 0 lib/{ => derivation}/kdf.ex | 0 lib/{ => encryption}/aes.ex | 0 lib/{ => encryption}/alg.ex | 0 lib/{ => encryption}/cms.ex | 0 lib/{ => services}/cmc.ex | 0 lib/{ => services}/cmp.ex | 16 ++++++++++++++-- lib/{ => services}/csr.ex | 0 lib/{ => services}/ocsp.ex | 0 lib/{ => services}/tsp.ex | 0 lib/{ => signing}/CAdES.ex | 0 lib/{ => signing}/ecdsa.ex | 0 13 files changed, 15 insertions(+), 19 deletions(-) rename lib/{ca.ex => application.ex} (53%) rename lib/{ => derivation}/hkdf.ex (100%) rename lib/{ => derivation}/kdf.ex (100%) rename lib/{ => encryption}/aes.ex (100%) rename lib/{ => encryption}/alg.ex (100%) rename lib/{ => encryption}/cms.ex (100%) rename lib/{ => services}/cmc.ex (100%) rename lib/{ => services}/cmp.ex (87%) rename lib/{ => services}/csr.ex (100%) rename lib/{ => services}/ocsp.ex (100%) rename lib/{ => services}/tsp.ex (100%) rename lib/{ => signing}/CAdES.ex (100%) rename lib/{ => signing}/ecdsa.ex (100%) diff --git a/lib/ca.ex b/lib/application.ex similarity index 53% rename from lib/ca.ex rename to lib/application.ex index aec938f..a0ecafc 100644 --- a/lib/ca.ex +++ b/lib/application.ex @@ -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 diff --git a/lib/hkdf.ex b/lib/derivation/hkdf.ex similarity index 100% rename from lib/hkdf.ex rename to lib/derivation/hkdf.ex diff --git a/lib/kdf.ex b/lib/derivation/kdf.ex similarity index 100% rename from lib/kdf.ex rename to lib/derivation/kdf.ex diff --git a/lib/aes.ex b/lib/encryption/aes.ex similarity index 100% rename from lib/aes.ex rename to lib/encryption/aes.ex diff --git a/lib/alg.ex b/lib/encryption/alg.ex similarity index 100% rename from lib/alg.ex rename to lib/encryption/alg.ex diff --git a/lib/cms.ex b/lib/encryption/cms.ex similarity index 100% rename from lib/cms.ex rename to lib/encryption/cms.ex diff --git a/lib/cmc.ex b/lib/services/cmc.ex similarity index 100% rename from lib/cmc.ex rename to lib/services/cmc.ex diff --git a/lib/cmp.ex b/lib/services/cmp.ex similarity index 87% rename from lib/cmp.ex rename to lib/services/cmp.ex index cb25c4a..c071759 100644 --- a/lib/cmp.ex +++ b/lib/services/cmp.ex @@ -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 @@ -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} diff --git a/lib/csr.ex b/lib/services/csr.ex similarity index 100% rename from lib/csr.ex rename to lib/services/csr.ex diff --git a/lib/ocsp.ex b/lib/services/ocsp.ex similarity index 100% rename from lib/ocsp.ex rename to lib/services/ocsp.ex diff --git a/lib/tsp.ex b/lib/services/tsp.ex similarity index 100% rename from lib/tsp.ex rename to lib/services/tsp.ex diff --git a/lib/CAdES.ex b/lib/signing/CAdES.ex similarity index 100% rename from lib/CAdES.ex rename to lib/signing/CAdES.ex diff --git a/lib/ecdsa.ex b/lib/signing/ecdsa.ex similarity index 100% rename from lib/ecdsa.ex rename to lib/signing/ecdsa.ex