| 1 |
-- | |
| 2 |
-- Module : Data.ASN1.BinaryEncoding.Parse |
| 3 |
-- License : BSD-style |
| 4 |
-- Maintainer : Vincent Hanquez <[email protected]> |
| 5 |
-- Stability : experimental |
| 6 |
-- Portability : unknown |
| 7 |
-- |
| 8 |
-- Generic parsing facility for ASN1. |
| 9 |
-- |
| 10 |
module Data.ASN1.BinaryEncoding.Parse |
| 11 |
( |
| 12 |
-- * incremental parsing interfaces |
| 13 |
runParseState |
| 14 |
, isParseDone |
| 15 |
, newParseState |
| 16 |
, ParseState |
| 17 |
, ParseCursor |
| 18 |
-- * simple parsing interfaces |
| 19 |
, parseLBS |
| 20 |
, parseBS |
| 21 |
) where |
| 22 |
|
| 23 |
import Control.Arrow (first) |
| 24 |
import Data.ByteString (ByteString) |
| 25 |
import qualified Data.ByteString as B |
| 26 |
import qualified Data.ByteString.Lazy as L |
| 27 |
import Data.ASN1.Error |
| 28 |
import Data.ASN1.Types |
| 29 |
import Data.ASN1.Types.Lowlevel |
| 30 |
import Data.ASN1.Get |
| 31 |
import Data.ASN1.Serialize |
| 32 |
import Data.Word |
| 33 |
import Data.Maybe (fromJust) |
| 34 |
|
| 35 |
-- | nothing means the parser stop this construction on |
| 36 |
-- an ASN1 end tag, otherwise specify the position |
| 37 |
-- where the construction terminate. |
| 38 |
type ConstructionEndAt = Maybe Word64 |
| 39 |
|
| 40 |
data ParseExpect = ExpectHeader (Maybe (B.ByteString -> Result ASN1Header)) |
| 41 |
| ExpectPrimitive Word64 (Maybe (B.ByteString -> Result ByteString)) |
| 42 |
|
| 43 |
type ParsePosition = Word64 |
| 44 |
|
| 45 |
-- | represent the parsing state of an ASN1 stream. |
| 46 |
-- |
| 47 |
-- * the stack of constructed elements. |
| 48 |
-- * the next expected type. |
| 49 |
-- * the position in the stream. |
| 50 |
-- |
| 51 |
data ParseState = ParseState [ConstructionEndAt] ParseExpect ParsePosition |
| 52 |
|
| 53 |
-- | create a new empty parse state. position is 0 |
| 54 |
newParseState :: ParseState |
| 55 |
newParseState = ParseState [] (ExpectHeader Nothing) 0 |
| 56 |
|
| 57 |
isEOC :: ASN1Header -> Bool |
| 58 |
isEOC (ASN1Header cl t _ _) = cl == Universal && t == 0 |
| 59 |
|
| 60 |
asn1LengthToConst :: ASN1Length -> Maybe Word64 |
| 61 |
asn1LengthToConst (LenShort n) = Just $ fromIntegral n |
| 62 |
asn1LengthToConst (LenLong _ n) = Just $ fromIntegral n |
| 63 |
asn1LengthToConst LenIndefinite = Nothing |
| 64 |
|
| 65 |
-- in the future, drop this for the `mplus` with Either. |
| 66 |
mplusEither :: Either b a -> (a -> Either b c) -> Either b c |
| 67 |
mplusEither (Left e) _ = Left e |
| 68 |
mplusEither (Right e) f = f e |
| 69 |
|
| 70 |
-- | Represent the events and state thus far. |
| 71 |
type ParseCursor = ([ASN1Event], ParseState) |
| 72 |
|
| 73 |
-- | run incrementally the ASN1 parser on a bytestring. |
| 74 |
-- the result can be either an error, or on success a list |
| 75 |
-- of events, and the new parsing state. |
| 76 |
runParseState :: ParseState -- ^ parser state |
| 77 |
-> ByteString -- ^ input data as bytes |
| 78 |
-> Either ASN1Error ParseCursor |
| 79 |
runParseState = loop |
| 80 |
where |
| 81 |
loop iniState bs |
| 82 |
| B.null bs = terminateAugment (([], iniState), bs) `mplusEither` (Right . fst) |
| 83 |
| otherwise = go iniState bs `mplusEither` terminateAugment |
| 84 |
`mplusEither` \((evs, newState), nbs) -> loop newState nbs |
| 85 |
`mplusEither` (Right . first (evs ++)) |
| 86 |
|
| 87 |
terminateAugment ret@((evs, ParseState stackEnd pe pos), r) = |
| 88 |
case stackEnd of |
| 89 |
Just endPos:xs |
| 90 |
| pos > endPos -> Left StreamConstructionWrongSize |
| 91 |
| pos == endPos -> terminateAugment ((evs ++ [ConstructionEnd], ParseState xs pe pos), r) |
| 92 |
| otherwise -> Right ret |
| 93 |
_ -> Right ret |
| 94 |
|
| 95 |
-- go get one element (either a primitive or a header) from the bytes |
| 96 |
-- and returns the new cursor and the remaining byte. |
| 97 |
go :: ParseState -> ByteString -> Either ASN1Error (ParseCursor, ByteString) |
| 98 |
go (ParseState stackEnd (ExpectHeader cont) pos) bs = |
| 99 |
case runGetHeader cont pos bs of |
| 100 |
Fail s -> Left $ ParsingHeaderFail s |
| 101 |
Partial f -> Right (([], ParseState stackEnd (ExpectHeader $ Just f) pos), B.empty) |
| 102 |
Done hdr nPos remBytes |
| 103 |
| isEOC hdr -> case stackEnd of |
| 104 |
[] -> Right (([], ParseState [] (ExpectHeader Nothing) nPos), remBytes) |
| 105 |
Just _:_ -> Left StreamUnexpectedEOC |
| 106 |
Nothing:newStackEnd -> Right ( ( [ConstructionEnd] |
| 107 |
, ParseState newStackEnd (ExpectHeader Nothing) nPos) |
| 108 |
, remBytes) |
| 109 |
| otherwise -> case hdr of |
| 110 |
(ASN1Header _ _ True len) -> |
| 111 |
let nEnd = (nPos +) `fmap` asn1LengthToConst len |
| 112 |
in Right ( ( [Header hdr,ConstructionBegin] |
| 113 |
, ParseState (nEnd:stackEnd) (ExpectHeader Nothing) nPos) |
| 114 |
, remBytes) |
| 115 |
(ASN1Header _ _ False LenIndefinite) -> Left StreamInfinitePrimitive |
| 116 |
(ASN1Header _ _ False len) -> |
| 117 |
let pLength = fromJust $ asn1LengthToConst len |
| 118 |
in if pLength == 0 |
| 119 |
then Right ( ( [Header hdr,Primitive B.empty] |
| 120 |
, ParseState stackEnd (ExpectHeader Nothing) nPos) |
| 121 |
, remBytes) |
| 122 |
else Right ( ( [Header hdr] |
| 123 |
, ParseState stackEnd (ExpectPrimitive pLength Nothing) nPos) |
| 124 |
, remBytes) |
| 125 |
go (ParseState stackEnd (ExpectPrimitive len cont) pos) bs = |
| 126 |
case runGetPrimitive cont len pos bs of |
| 127 |
Fail _ -> Left ParsingPartial |
| 128 |
Partial f -> Right (([], ParseState stackEnd (ExpectPrimitive len $ Just f) pos), B.empty) |
| 129 |
Done p nPos remBytes -> Right (([Primitive p], ParseState stackEnd (ExpectHeader Nothing) nPos), remBytes) |
| 130 |
|
| 131 |
runGetHeader Nothing = \pos -> runGetPos pos getHeader |
| 132 |
runGetHeader (Just f) = const f |
| 133 |
|
| 134 |
runGetPrimitive Nothing n = \pos -> runGetPos pos (getBytes $ fromIntegral n) |
| 135 |
runGetPrimitive (Just f) _ = const f |
| 136 |
|
| 137 |
-- | when no more input is available, it's important to check that the parser is |
| 138 |
-- in a finish state too. |
| 139 |
isParseDone :: ParseState -> Bool |
| 140 |
isParseDone (ParseState [] (ExpectHeader Nothing) _) = True |
| 141 |
isParseDone _ = False |
| 142 |
|
| 143 |
-- | Parse one lazy bytestring and returns on success all ASN1 events associated. |
| 144 |
parseLBS :: L.ByteString -> Either ASN1Error [ASN1Event] |
| 145 |
parseLBS lbs = foldrEither process ([], newParseState) (L.toChunks lbs) `mplusEither` onSuccess |
| 146 |
where |
| 147 |
onSuccess (allEvs, finalState) |
| 148 |
| isParseDone finalState = Right $ concat $ reverse allEvs |
| 149 |
| otherwise = Left ParsingPartial |
| 150 |
|
| 151 |
process :: ([[ASN1Event]], ParseState) -> ByteString -> Either ASN1Error ([[ASN1Event]], ParseState) |
| 152 |
process (pevs, cState) bs = runParseState cState bs `mplusEither` \(es, cState') -> Right (es : pevs, cState') |
| 153 |
|
| 154 |
foldrEither :: (a -> ByteString -> Either ASN1Error a) -> a -> [ByteString] -> Either ASN1Error a |
| 155 |
foldrEither _ acc [] = Right acc |
| 156 |
foldrEither f acc (x:xs) = f acc x `mplusEither` \nacc -> foldrEither f nacc xs |
| 157 |
|
| 158 |
-- | Parse one strict bytestring and returns on success all ASN1 events associated. |
| 159 |
parseBS :: ByteString -> Either ASN1Error [ASN1Event] |
| 160 |
parseBS bs = runParseState newParseState bs `mplusEither` onSuccess |
| 161 |
where onSuccess (evs, pstate) |
| 162 |
| isParseDone pstate = Right evs |
| 163 |
| otherwise = Left ParsingPartial |