Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
5HT committed Nov 5, 2024
1 parent 89ba54e commit e7e2c14
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 43 deletions.
38 changes: 29 additions & 9 deletions lib/encryption/cms.ex
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,38 @@ defmodule CA.CMS do

# Test

def pem(name), do: hd(:public_key.pem_decode(:erlang.element(2,:file.read_file(name))))
def testDecryptECC(), do: CA.CMS.decrypt(testECC(), testPrivateKeyECC())
def testDecryptKEK(), do: CA.CMS.decrypt(testKEK(), testPrivateKeyKEK())
def testDecryptRSA(), do: CA.CMS.decrypt(testRSA(), testPrivateKeyRSA())
def pem(name), do: hd(:public_key.pem_decode(:erlang.element(2, :file.read_file(name))))
def testDecryptECC(), do: [cipher: :binary.part(:erlang.element(2, CA.CMS.decrypt(testECC(), testPrivateKeyECC())),0,48)]
def testDecryptKEK(), do: [cipher: :binary.part(:erlang.element(2, CA.CMS.decrypt(testKEK(), testPrivateKeyKEK())),0,48)]
def testDecryptRSA(), do: [cipher: :erlang.element(2, CA.CMS.decrypt(testRSA(), testPrivateKeyRSA()))]
def testDecryptCMS(), do: [cipher: :binary.part(:erlang.element(2, CA.CMS.testCMS()),0,48)]
def test(), do:
[
testDecryptECC(),
testDecryptKEK(),
testDecryptRSA(),
testCMS(),
testFolder("czo"),
testFolder("CAdES"),
testFolder("certs"),
testFolder("cms"),
[
testDecryptECC(),
testDecryptKEK(),
testDecryptCMS(),
testDecryptRSA(),
],
]

def parseKeyFile(file) do {:ok, bin} = :file.read_file file ; list = :public_key.pem_decode(bin) ; :lists.map(fn x -> :public_key.pem_entry_decode(x) end, list) end

def testFolder(folder \\ "CAdES") do
:lists.map(fn x -> case :filename.extension(x) do
'.p7s' -> [cms: byte_size(:erlang.term_to_binary(CA.CMS.parseContentInfoFile(x))), name: x]
'.txt' -> [smime: byte_size(:erlang.term_to_binary(CA.CMS.parseContentInfoSMIME(x))), name: x]
'.key' -> [key: byte_size(:erlang.term_to_binary(CA.CMS.parseKeyFile(x))), name: x]
'.b64' -> [base64: byte_size(:erlang.term_to_binary(CA.CRT.parseCertB64(x))), name: x]
'.pem' -> [pem: byte_size(:erlang.term_to_binary(CA.CRT.parseCertPEM(x))), name: x]
'.cer' -> [cert: byte_size(:erlang.term_to_binary(CA.CRT.parseCertFile(x))), name: x]
end end, :filelib.wildcard ['test/#{folder}/*'])
end

def testPrivateKeyECC() do
privateKey = :public_key.pem_entry_decode(pem("test/certs/client.key"))
{:'ECPrivateKey',_,privateKeyBin,{:namedCurve,schemeOID},_,_} = privateKey
Expand Down Expand Up @@ -131,7 +151,7 @@ defmodule CA.CMS do
end

def testRSA() do
{:ok,x} = :file.read_file "test/cms/rsa-cms.bin"
{:ok,x} = :file.read_file "test/cms/rsa-cms.p7s"
:'CryptographicMessageSyntax-2010'.decode(:ContentInfo, x)
end

Expand Down
27 changes: 15 additions & 12 deletions lib/services/crt.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@ defmodule CA.CRT do
@moduledoc "X.509 Certificates."

def subj({:rdnSequence, attrs}) do
{:rdnSequence, :lists.map(fn
[{t,oid,{:uTF8String,x}}] -> [{t,oid,:asn1rt_nif.encode_ber_tlv({12, :erlang.iolist_to_binary(x)})}]
[{t,oid,x}] when is_list(x) -> [{t,oid,:asn1rt_nif.encode_ber_tlv({19, :erlang.iolist_to_binary(x)})}]
[{t,oid,x}] -> [{t,oid,x}] end, attrs)}
{:rdnSequence, :lists.map(fn
[{t,oid,{:uTF8String,x}}] -> [{t,oid,:asn1rt_nif.encode_ber_tlv({12, :erlang.iolist_to_binary(x)})}]
[{t,oid,x}] when is_list(x) -> [{t,oid,:asn1rt_nif.encode_ber_tlv({19, :erlang.iolist_to_binary(x)})}]
[{t,oid,x}] -> [{t,oid,x}] end, attrs)}
end

def unsubj({:rdnSequence, attrs}) do
{:rdnSequence, :lists.map(fn [{t,oid,x}] when is_binary(x) ->
case :asn1rt_nif.decode_ber_tlv(x) do
{{12,a},_} -> [{t,oid,{:uTF8String,a}}]
{{19,a},_} -> [{t,oid,:erlang.binary_to_list(a)}]
end
x -> x end, attrs)}
{:rdnSequence, :lists.map(fn [{t,oid,x}] when is_binary(x) ->
case :asn1rt_nif.decode_ber_tlv(x) do
{{12,a},_} -> [{t,oid,{:uTF8String,a}}]
{{19,a},_} -> [{t,oid,:erlang.binary_to_list(a)}]
end
{t,oid,x} -> [{t,oid,x}]
x -> x
end, attrs)}
end

def extract(code, person) do
Expand Down Expand Up @@ -53,7 +55,7 @@ defmodule CA.CRT do
def oid({2,5,29,19},v), do: {:basicConstraints, v}
def oid({2,5,29,31},v), do: {:cRLDistributionPoints, pair(v,[])}
def oid({2,5,29,32},v), do: {:certificatePolicies, :lists.map(fn x -> case isString(x) do false -> mapOid(:oid.decode(x)) ; true -> x end end, v) }
def oid({2,5,29,35},v), do: {:authorityKeyIdentifier, :base64.encode(hd(pair(v,[])))}
def oid({2,5,29,35},v), do: {:authorityKeyIdentifier, v}
def oid({2,5,29,46},v), do: {:freshestCRL, pair(v,[])}
def oid({1,2,840,113549,1,9,3},v), do: {:contentType, CA.AT.oid(CA.EST.decodeObjectIdentifier(v)) }
def oid({1,2,840,113549,1,9,4},v), do: {:messageDigest, :base64.encode(:erlang.element(2,:KEP.decode(:MessageDigest, v)))}
Expand Down Expand Up @@ -132,7 +134,8 @@ defmodule CA.CRT do
:lists.map(fn [{_,oid,{_,list}}] -> {rdn(oid),"#{list}"}
[{_,oid,list}] -> {rdn(oid),"#{list}"}
{_,oid,{_,list}} -> {rdn(oid),"#{list}"}
{_,oid, list} -> {rdn(oid),"#{list}"} end, list)
{_,oid, list} -> {rdn(oid),"#{list}"}
x -> x end, list)
end
def rdn(x), do: "#{x}"

Expand Down
26 changes: 13 additions & 13 deletions openssl/ecc/maxim.pem
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
-----BEGIN CERTIFICATE-----
MIICQDCCAcagA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BBAAiA2IABJPKg7Vl0TSbo06qjhZsJQr8VjbdKqgG
xqvLPA1QKxNfCfTKF3uetXunD9wG0Eo4VmL/ZXXZNVFJNy2P7OpZxFSGEASg/2lZ
/7TGtSzSZYk2R/r4RLYrliOvP8rv1gZML6OBkzCBkDAJBgNVHRMEAjAAMA4GA1Ud
DwEB/wQEAwIFoDAdBgNVHSUEFjAUBggrBgEFBQcDAQYIKwYBBQUHAwIwHQYDVR0O
BBYEFMjFrY/R/vLZqh0ugOdnubygiBNzMB8GA1UdIwQYMBaAFPp6XvK7AT6Oxqis
D2VrcWJ3K/EMMBQGA1UdEQQNMAuCCXN5bnJjLmNvbTAKBggqhkjOPQQDAgNnADBk
AjB+dxTb/UlE3wdatCFD7YhoCf9iwQ7lUEb0SA3fKf5FB61M9zZpWDvUlKNpzMb5
v7wCMDYgYeVWlQkZndDy53VelnCRGpt3W5LDl5ADngKsR8APUciKB69osqdliPbR
Pl1+EA==
-----END CERTIFICATE-----
9 changes: 0 additions & 9 deletions test/certs/caroot.key

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit e7e2c14

Please sign in to comment.