1-- | 2-- Module : Network.TLS.Util.ASN1 3-- License : BSD-style 4-- Maintainer : Vincent Hanquez <vincent@snarc.org> 5-- Stability : experimental 6-- Portability : unknown 7-- 8-- ASN1 utils for TLS 9-- 10module Network.TLS.Util.ASN1 11 ( decodeASN1Object 12 , encodeASN1Object 13 ) where 14 15import Network.TLS.Imports 16import Data.ASN1.Types (fromASN1, toASN1, ASN1Object) 17import Data.ASN1.Encoding (decodeASN1', encodeASN1') 18import Data.ASN1.BinaryEncoding (DER(..)) 19 20-- | Attempt to decode a bytestring representing 21-- an DER ASN.1 serialized object into the object. 22decodeASN1Object :: ASN1Object a 23 => String 24 -> ByteString 25 -> Either String a 26decodeASN1Object name bs = 27 case decodeASN1' DER bs of 28 Left e -> Left (name ++ ": cannot decode ASN1: " ++ show e) 29 Right asn1 -> case fromASN1 asn1 of 30 Left e -> Left (name ++ ": cannot parse ASN1: " ++ show e) 31 Right (d,_) -> Right d 32 33-- | Encode an ASN.1 Object to the DER serialized bytestring 34encodeASN1Object :: ASN1Object a 35 => a 36 -> ByteString 37encodeASN1Object obj = encodeASN1' DER $ toASN1 obj [] 38