some prototyping and fiddling around with ASN.1

This commit is contained in:
Trolli Schmittlauch 2020-04-26 17:40:25 +02:00
parent 992320b67b
commit 7e6c4985ce
3 changed files with 35 additions and 5 deletions

View file

@ -46,7 +46,7 @@ category: Network
extra-source-files: CHANGELOG.md
common deps
build-depends: base ^>=4.12.0.0, containers ^>=0.6.0.1, bytestring, utf8-string ^>=1.0.1.1, network ^>=2.8.0.1, time ^>=1.8.0.2, cmdargs ^>= 0.10, cryptonite ^>= 0.25, memory, binary-strict, async, asn1-encoding, asn1-types, asn1-parse
build-depends: base ^>=4.12.0.0, containers ^>=0.6.0.1, bytestring, utf8-string ^>=1.0.1.1, network ^>=2.8.0.1, time ^>=1.8.0.2, cmdargs ^>= 0.10, cryptonite ^>= 0.25, memory, async, asn1-encoding, asn1-types, asn1-parse
ghc-options: -Wall

View file

@ -1,9 +1,12 @@
{ pkgs ? import (
builtins.fetchGit {
name = "nixpkgs-pinned";
url = https://github.com/NixOS/nixpkgs/;
ref = "refs/heads/release-20.03";
rev = "c778e137bdf921e7574f37f96874c247c5df768e";
#url = https://github.com/NixOS/nixpkgs/;
url = https://github.com/schmittlauch/nixpkgs/;
#ref = "refs/heads/release-20.03";
ref = "refs/heads/bump-asn1-types";
#rev = "10100a97c8964e82b30f180fda41ade8e6f69e41";
rev = "cb0e38127c7a66c292e60d7e8c1ab4eeb26c44a4";
}) {},
compiler ? "ghc865"
}:

View file

@ -3,16 +3,43 @@ module Main where
import qualified Data.ASN1.Encoding as ASN1 -- asn1-encoding package
import qualified Data.ASN1.BinaryEncoding as ASN1
import qualified Data.ASN1.Error as ASN1
import qualified Data.ASN1.Types as ASN1 -- asn1-types package
import qualified Data.ASN1.Parse as ASN1P
import qualified Data.ByteString as BS
import Data.Maybe (fromMaybe)
-- import Hash2Pub.Fedichord
-- encoding values as ASN.1 types is done using Data.ASN1.Prim
someASN1 = ASN1.encodeASN1' ASN1.DER $ ASN1.Start ASN1.Sequence : ASN1.ASN1String (ASN1.asn1CharacterString ASN1.Visible domain) : ASN1.ASN1String (ASN1.asn1CharacterString ASN1.UTF8 unicode) : map ASN1.IntVal xs ++ [ASN1.End ASN1.Sequence]
someASN1 :: [ASN1.ASN1]
someASN1 = ASN1.Start ASN1.Sequence : ASN1.ASN1String (ASN1.asn1CharacterString ASN1.Visible domain) : ASN1.ASN1String (ASN1.asn1CharacterString ASN1.UTF8 unicode) : map ASN1.IntVal xs ++ [ASN1.End ASN1.Sequence]
where
domain = "domains.are.ascii.on.ly"
unicode = "Hähä, but unicode string!"
xs = [ 23, 42, 2342 ]
asn1AsDer :: [ASN1.ASN1] -> BS.ByteString
asn1AsDer = ASN1.encodeASN1' ASN1.DER
derToAsn1 :: BS.ByteString -> Either ASN1.ASN1Error [ASN1.ASN1]
derToAsn1 = ASN1.decodeASN1' ASN1.DER
getUnicodeField :: [ASN1.ASN1] -> String
getUnicodeField ((ASN1.Start ASN1.Sequence) : _ : (ASN1.ASN1String strASN1) : _) = fromMaybe "" $ ASN1.asn1CharacterToString strASN1
testParser :: ASN1P.ParseASN1 String
testParser = do
show <$> ASN1P.getNext
--stringParser asn1obj =
-- fmap (fromMaybe "" $ ASN1.asn1CharacterToString) ASN1P.getNext
main = do
print someASN1
print $ asn1AsDer someASN1
print $ derToAsn1 . asn1AsDer $ someASN1
putStrLn $ getUnicodeField someASN1
print $ ASN1.decodeASN1Repr' ASN1.DER $ asn1AsDer someASN1
putStrLn "\nLet's try a real parser combinator:"
print $ ASN1P.runParseASN1State testParser someASN1