crypton-asn1-encoding 0.10.0 → 0.11.0
raw patch · 5 files changed
+91/−41 lines, 5 files
Files
- CHANGELOG.md +9/−0
- crypton-asn1-encoding.cabal +2/−2
- src/Data/ASN1/BinaryEncoding.hs +23/−18
- src/Data/ASN1/Prim.hs +21/−15
- tests/Tests.hs +36/−6
CHANGELOG.md view
@@ -6,6 +6,15 @@ and this project adheres to the [Haskell Package Versioning Policy](https://pvp.haskell.org/). +## 0.11.0 - 2026-11-19 + +* `decodeASN1'` and the other decoding functions return a malformed value as + `Left`, rather than throwing an exception when the result is evaluated. +* `getBitString` yields type decoding failed, as `Left`, if the `ByteString` is + empty, rather than throwing an exception. +* `getBitString` now yields type decoding failed, as `Left`, if unused bits in + the final subsequent octet are specified but there are no subsequent octets. + ## 0.10.0 - 2025-08-01 * Depend on package `time-hourglass`, rather than `hourglass`. The fields of
crypton-asn1-encoding.cabal view
@@ -1,11 +1,11 @@ cabal-version: 1.22 --- This file has been generated from package.yaml by hpack version 0.38.1.+-- This file has been generated from package.yaml by hpack version 0.39.6. -- -- see: https://github.com/sol/hpack name: crypton-asn1-encoding-version: 0.10.0+version: 0.11.0 synopsis: ASN.1 data (raw, BER or DER) readers and writers description: A library providing readers and writers of data following the Abstract Syntax Notation One (ASN.1) standard in raw form or in the high-level forms of Basic
src/Data/ASN1/BinaryEncoding.hs view
@@ -13,7 +13,6 @@ , DER (..) ) where -import qualified Control.Exception as E import Data.ASN1.BinaryEncoding.Parse ( parseLBS ) import Data.ASN1.BinaryEncoding.Writer ( toLazyByteString ) import Data.ASN1.Encoding @@ -36,18 +35,18 @@ data DER = DER instance ASN1DecodingRepr BER where - decodeASN1Repr _ lbs = decodeEventASN1Repr (const Nothing) `fmap` parseLBS lbs + decodeASN1Repr _ lbs = parseLBS lbs >>= decodeEventASN1Repr (const Nothing) instance ASN1Decoding BER where decodeASN1 _ lbs = - (map fst . decodeEventASN1Repr (const Nothing)) `fmap` parseLBS lbs + map fst <$> (parseLBS lbs >>= decodeEventASN1Repr (const Nothing)) instance ASN1DecodingRepr DER where - decodeASN1Repr _ lbs = decodeEventASN1Repr checkDER `fmap` parseLBS lbs + decodeASN1Repr _ lbs = parseLBS lbs >>= decodeEventASN1Repr checkDER instance ASN1Decoding DER where decodeASN1 _ lbs = - (map fst . decodeEventASN1Repr checkDER) `fmap` parseLBS lbs + map fst <$> (parseLBS lbs >>= decodeEventASN1Repr checkDER) instance ASN1Encoding DER where encodeASN1 _ l = toLazyByteString $ encodeToRaw l @@ -57,24 +56,30 @@ decodeConstruction (ASN1Header Universal 0x11 _ _) = Set decodeConstruction (ASN1Header c t _ _) = Container c t -decodeEventASN1Repr :: (ASN1Header -> Maybe ASN1Error) -> [ASN1Event] -> [ASN1Repr] -decodeEventASN1Repr checkHeader = loop [] +-- | An error is returned as 'Left' rather than thrown, so that it reaches +-- the caller of 'decodeASN1' and 'decodeASN1Repr' as the type says. +decodeEventASN1Repr :: + (ASN1Header -> Maybe ASN1Error) + -> [ASN1Event] + -> Either ASN1Error [ASN1Repr] +decodeEventASN1Repr checkHeader = loop [] [] where - loop _ [] = [] - loop acc (h@(Header hdr@(ASN1Header _ _ True _)):ConstructionBegin:xs) = + loop out _ [] = Right $ reverse out + loop out acc (h@(Header hdr@(ASN1Header _ _ True _)):ConstructionBegin:xs) = let ctype = decodeConstruction hdr in case checkHeader hdr of - Nothing -> (Start ctype,[h,ConstructionBegin]) : loop (ctype:acc) xs - Just err -> E.throw err - loop acc (h@(Header hdr@(ASN1Header _ _ False _)):p@(Primitive prim):xs) = + Nothing -> + loop ((Start ctype, [h, ConstructionBegin]) : out) (ctype:acc) xs + Just err -> Left err + loop out acc (h@(Header hdr@(ASN1Header _ _ False _)):p@(Primitive prim):xs) = case checkHeader hdr of Nothing -> case decodePrimitive hdr prim of - Left err -> E.throw err - Right obj -> (obj, [h, p]) : loop acc xs - Just err -> E.throw err - loop (ctype:acc) (ConstructionEnd:xs) = - (End ctype, [ConstructionEnd]) : loop acc xs - loop _ (x:_) = E.throw $ StreamUnexpectedSituation (show x) + Left err -> Left err + Right obj -> loop ((obj, [h, p]) : out) acc xs + Just err -> Left err + loop out (ctype:acc) (ConstructionEnd:xs) = + loop ((End ctype, [ConstructionEnd]) : out) acc xs + loop _ _ (x:_) = Left $ StreamUnexpectedSituation (show x) -- | DER header need to be all of finite size and of minimum possible size. checkDER :: ASN1Header -> Maybe ASN1Error
src/Data/ASN1/Prim.hs view
@@ -298,20 +298,25 @@ | otherwise -> pure $ l + 1 getBitString :: ByteString -> Either ASN1Error ASN1 -getBitString s = - let toSkip = B.head s in - let toSkip' = if toSkip >= 48 && toSkip <= 48 + 7 - then toSkip - fromIntegral ( ord '0') - else toSkip in - let xs = B.tail s in - if toSkip' >= 0 && toSkip' <= 7 - then Right $ BitString $ toBitArray xs (fromIntegral toSkip') - else Left $ TypeDecodingFailed - ( "bitstring: skip number not within bound " - ++ show toSkip' - ++ " " - ++ show s - ) +getBitString s = case B.uncons s of + Nothing -> Left $ TypeDecodingFailed "bitstring: no data" + Just (toSkip, xs) -> + let toSkip' = if toSkip >= 48 && toSkip <= 48 + 7 + then toSkip - fromIntegral ( ord '0') + else toSkip + in if toSkip' >= 0 && toSkip' <= 7 + -- X.690 8.6.2.3: with no subsequent octets, the number of unused + -- bits is zero. + then if B.null xs && toSkip' /= 0 + then Left $ TypeDecodingFailed + "bitstring: unused bits without subsequent octets" + else Right $ BitString $ toBitArray xs (fromIntegral toSkip') + else Left $ TypeDecodingFailed + ( "bitstring: skip number not within bound " + ++ show toSkip' + ++ " " + ++ show s + ) getCharacterString :: ASN1StringEncoding -> ByteString -> Either ASN1Error ASN1 getCharacterString encoding bs = Right $ ASN1String (ASN1CharacterString encoding bs) @@ -369,7 +374,8 @@ adjustUTC dt@(DateTime (Date y m d) tod) | timeType == TimeGeneralized = dt - | y > 2050 = DateTime (Date (y - 100) m d) tod + -- RFC 5280 4.1.2.5.1: YY >= 50 is 19YY + | y >= 2050 = DateTime (Date (y - 100) m d) tod | otherwise = dt formatNoSeconds = init format format
tests/Tests.hs view
@@ -5,7 +5,8 @@ import Data.ASN1.BinaryEncoding.Parse ( parseLBS ) import Data.ASN1.BinaryEncoding.Writer ( toLazyByteString ) import Data.ASN1.BitArray ( BitArray, toBitArray ) -import Data.ASN1.Encoding ( ASN1Decoding (..), ASN1Encoding (..) ) +import Data.ASN1.Encoding + ( ASN1Decoding (..), ASN1Encoding (..), decodeASN1' ) import Data.ASN1.Get ( Result (..), runGet ) import Data.ASN1.Prim ( mkSmallestLength ) import Data.ASN1.Serialize ( getHeader, putHeader ) @@ -27,7 +28,7 @@ import Test.Tasty ( TestTree, defaultMain, testGroup ) import Test.Tasty.QuickCheck ( Arbitrary (..), Gen, choose, elements, listOf1, oneof - , resize, suchThat, testProperty + , once, resize, suchThat, testProperty ) instance Arbitrary ASN1Class where @@ -92,19 +93,20 @@ instance Arbitrary BitArray where arbitrary = do bs <- arbitrary - w <- choose (0, 7) :: Gen Int + -- X.690 8.6.2.3: with no octets, there are no unused bits either. + w <- if B.null bs then pure 0 else choose (0, 7) :: Gen Int pure $ toBitArray bs w instance Arbitrary Date where arbitrary = do - y <- choose (1951, 2050) + y <- choose (1950, 2049) m <- elements [ January .. December] d <- choose (1, 30) pure $ normalizeDate $ Date y m d normalizeDate :: Date -> Date normalizeDate origDate - | y < 1951 = normalizeDate (Date (y + 50) m d) + | y < 1950 = normalizeDate (Date (y + 50) m d) | otherwise = normalizedDate where normalizedDate@(Date y m d) = timeConvert (timeConvert origDate :: Elapsed) @@ -256,6 +258,15 @@ error ("got: " ++ show got ++ " expected: " ++ show expected) | otherwise = True +-- | UTCTime years 50-99 are 1950-1999 and 00-49 are 2000-2049. +prop_utctime_pivot :: Bool +prop_utctime_pivot = + year "500101000000Z" == Right 1950 && year "491231235959Z" == Right 2049 + where + year s = case decodeASN1' DER (B.pack (0x17 : 13 : map (fromIntegral . fromEnum) s)) of + Right [ASN1Time TimeUTC (DateTime (Date y _ _) _) _] -> Right y + r -> Left (show r) + marshallingTests :: TestTree marshallingTests = testGroup "Marshalling" [ testProperty "Header" prop_header_marshalling_id @@ -263,7 +274,26 @@ , testProperty "DER" prop_asn1_der_marshalling_id , testProperty "Real" prop_real_der_marshalling_id , testProperty "Integral Real" prop_integral_real_der_marshalling_id + , testProperty "UTCTime pivot" prop_utctime_pivot ] +-- | Malformed input is a 'Left', not an exception thrown when the result is +-- looked at. Each is the DER signature @30 06 02 01 01 02 01 01@ broken. +malformedTests :: TestTree +malformedTests = testGroup "Malformed" + [ testProperty "primitive SEQUENCE" $ isMalformed + [0x10, 0x06, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01] + , testProperty "BIT STRING with unused bits and no octets" $ isMalformed + [0x30, 0x06, 0x03, 0x01, 0x01, 0x02, 0x01, 0x01] + , testProperty "BIT STRING with too many unused bits" $ isMalformed + [0x30, 0x06, 0x02, 0x01, 0x01, 0x03, 0x01, 0x09] + , testProperty "empty BIT STRING" $ isMalformed + [0x30, 0x05, 0x02, 0x01, 0x01, 0x03, 0x00] + ] + where + isMalformed ws = once $ case decodeASN1' DER (B.pack ws) of + Left _ -> True + Right _ -> False + main :: IO () -main = defaultMain $ testGroup "asn1-encoding" [marshallingTests] +main = defaultMain $ testGroup "asn1-encoding" [marshallingTests, malformedTests]