crypton-x509-validation 1.10.1 → 1.10.2
raw patch · 4 files changed
+131/−49 lines, 4 filesdep ~crypton-x509PVP ok
version bump matches the API change (PVP)
Dependency ranges changed: crypton-x509
API changes (from Hackage documentation)
Files
- ChangeLog.md +8/−0
- Data/X509/Validation.hs +38/−47
- Tests/Tests.hs +83/−0
- crypton-x509-validation.cabal +2/−2
ChangeLog.md view
@@ -1,5 +1,13 @@ # ChangeLog for crypton-x509-validation +## 1.10.2++* Fix host name validation with iPAddress SANs: an IP address is+ matched only against iPAddress SANs and the CN is not used if the+ SubjectAltName extension is present, even without dNSName.+* An IP address is no longer matched against the CN of a certificate+ without SubjectAltName.+ ## 1.10.1 * Fix name constraint bypasses: constraints are accumulated per RFC
Data/X509/Validation.hs view
@@ -480,7 +480,7 @@ ] cn | useCN = case getNames cert of- Right (Just name, []) -> [maybe (AltNameDNS name) ipToAltName (parseIPAddress name)]+ Right (Just name, Nothing) -> [maybe (AltNameDNS name) ipToAltName (parseIPAddress name)] _ -> [] | otherwise = [] names = case extensionGetE exts of@@ -501,47 +501,39 @@ where (before, after) = certValidity cert --- | 'Left' if the SAN extension is present but cannot be decoded.-getNames :: Certificate -> Either String (Maybe String, [String])+-- | The commonName and the SAN entries ('Nothing' without the extension).+-- 'Left' if the SAN extension is present but cannot be decoded.+getNames :: Certificate -> Either String (Maybe String, Maybe [AltName]) getNames cert = case extensionGetE $ certExtensions cert of Just (Left e) -> Left e- Just (Right san) -> Right (commonName, toAltName san)- Nothing -> Right (commonName, [])+ Just (Right (ExtSubjectAltName names)) -> Right (commonName, Just names)+ Nothing -> Right (commonName, Nothing) where commonName = getDnElement DnCommonName (certSubjectDN cert) >>= safeCharacterToString- toAltName (ExtSubjectAltName names) = catMaybes $ map unAltName names- where- unAltName (AltNameDNS s) = Just s- unAltName _ = Nothing data IPAddress = IPv4Address IPv4 | IPv6Address IPv6 deriving (Eq) -getIPs :: Certificate -> [IPAddress]-getIPs cert = fromMaybe [] (toAltName <$> (extensionGet $ certExtensions cert))+toIPAddress :: ByteString -> Maybe IPAddress+toIPAddress s = case unpack s of+ [a, b, c, d] -> Just $ IPv4Address $ toIPv4 $ fmap fromIntegral [a, b, c, d]+ [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p] ->+ Just $+ IPv6Address $+ toIPv6+ [ fuse a b+ , fuse c d+ , fuse e f+ , fuse g h+ , fuse i j+ , fuse k l+ , fuse m n+ , fuse o p+ ]+ _ -> Nothing where- toAltName (ExtSubjectAltName names) = catMaybes $ map unAltName names-- unAltName (AltNameIP s) = case unpack s of- [a, b, c, d] -> Just $ IPv4Address $ toIPv4 $ fmap fromIntegral [a, b, c, d]- [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p] ->- Just $- IPv6Address $- toIPv6- [ fuse a b- , fuse c d- , fuse e f- , fuse g h- , fuse i j- , fuse k l- , fuse m n- , fuse o p- ]- _ -> Nothing- unAltName _ = Nothing- fuse :: Word8 -> Word8 -> Int fuse a b = shiftL (fromIntegral a) 8 .|. (fromIntegral b) @@ -551,10 +543,10 @@ parseIPAddress _ = Nothing -- | Validate that the fqhn is matched by at least one name in the certificate.--- If the subjectAltname extension is present, then the certificate commonName--- is ignored, and only the DNS names and IP Addresses, if any, in the--- subjectAltName are considered. Otherwise, the commonName from the subjectDN--- is used.+-- An IP address is matched only against the iPAddress entries of the+-- subjectAltName. A host name is matched against the dNSName entries if+-- the subjectAltName extension is present, even if it has no dNSName.+-- Otherwise, the commonName from the subjectDN is used. -- -- If the subjectAltName extension cannot be decoded, the name never matches. --@@ -564,23 +556,22 @@ validateCertificateName :: HostName -> Certificate -> [FailedReason] validateCertificateName fqhn cert = case getNames cert of Left _ -> [NameMismatch fqhn]- Right names -> validateNames fqhn cert names+ Right names -> validateNames fqhn names validateNames- :: HostName -> Certificate -> (Maybe String, [String]) -> [FailedReason]-validateNames fqhn cert (commonName, altNames)- | not $ null altNames =- case parseIPAddress fqhn of- Nothing -> findMatch [] $ map matchDomain altNames- Just ip ->- if elem ip (getIPs cert)- then []- else [NameMismatch fqhn]- | otherwise =- case commonName of+ :: HostName -> (Maybe String, Maybe [AltName]) -> [FailedReason]+validateNames fqhn (commonName, mAltNames) = case parseIPAddress fqhn of+ Just ip+ | ip `elem` [a | AltNameIP b <- altNames, Just a <- [toIPAddress b]] -> []+ | otherwise -> [NameMismatch fqhn]+ Nothing -> case mAltNames of+ Just _ -> findMatch [] $ [matchDomain n | AltNameDNS n <- altNames]+ Nothing -> case commonName of Nothing -> [NoCommonName] Just cn -> findMatch [] $ [matchDomain cn] where+ altNames = fromMaybe [] mAltNames+ findMatch :: [FailedReason] -> [[FailedReason]] -> [FailedReason] findMatch _ [] = [NameMismatch fqhn] findMatch _ ([] : _) = []
Tests/Tests.hs view
@@ -561,6 +561,37 @@ , AltNameDNS "dummy2" ] +-- | Tests a host name against a certificate with the given CN and,+-- if any, SubjectAltName entries.+testNames+ :: IO (RData pub priv)+ -> TestName+ -> String+ -- ^ Certificate CN+ -> Maybe [AltName]+ -- ^ Certificate SubjectAltName+ -> HostName+ -> [FailedReason]+ -> TestTree+testNames res caseName cn san hostname expected = testWithRes res caseName $ \rd -> do+ pair <-+ mkCertificate+ 2+ 100+ (mkDn cn)+ (present rd)+ (exts ++ leafStdExts)+ (CA $ intermediate rd)+ (keys1 rd)+ assertValidationResult+ rd+ defaultChecks+ hostname+ [pair, intermediate rd]+ expected+ where+ exts = maybe [] (\ns -> [mkExtension False $ ExtSubjectAltName ns]) san+ -- | Tests 'checkLeafKeyUsage'. testLeafKeyUsage :: IO (RData pub priv)@@ -1425,6 +1456,58 @@ "::1" True []+ ]+ , testGroup+ "IP address and CN"+ [ testNames+ res+ "IP SAN mismatch, CN match"+ "10.0.0.1"+ (Just [AltNameIP (BS.pack [10, 0, 0, 2])])+ "10.0.0.1"+ [NameMismatch "10.0.0.1"]+ , testNames+ res+ "IP SAN match, CN mismatch"+ "other"+ (Just [AltNameIP (BS.pack [10, 0, 0, 1])])+ "10.0.0.1"+ []+ , testNames+ res+ "IP SAN only, CN host name"+ "www.example.com"+ (Just [AltNameIP (BS.pack [10, 0, 0, 1])])+ "www.example.com"+ [NameMismatch "www.example.com"]+ , testNames+ res+ "no SAN, CN IP"+ "10.0.0.1"+ Nothing+ "10.0.0.1"+ [NameMismatch "10.0.0.1"]+ , testNames+ res+ "no SAN, CN host name"+ "www.example.com"+ Nothing+ "www.example.com"+ []+ , testNames+ res+ "IP in dNSName"+ "other"+ (Just [AltNameDNS "10.0.0.1"])+ "10.0.0.1"+ [NameMismatch "10.0.0.1"]+ , testNames+ res+ "invalid iPAddress length"+ "other"+ (Just [AltNameIP (BS.pack [10, 0, 0, 1, 0]), AltNameDNS "www.example.com"])+ "www.example.com"+ [NameMismatch "www.example.com"] ] , testGroup "disabled"
crypton-x509-validation.cabal view
@@ -1,6 +1,6 @@ cabal-version: >=1.10 name: crypton-x509-validation-version: 1.10.1+version: 1.10.2 license: BSD3 license-file: LICENSE copyright: Vincent Hanquez <vincent@snarc.org>@@ -38,7 +38,7 @@ crypton-asn1-types >=0.4.1 && <0.5, crypton-asn1-encoding >=0.10.0 && <0.11, crypton-pem >=0.2.4 && <0.4,- crypton-x509 >=1.10.1 && <1.11,+ crypton-x509 >=1.10.2 && <1.11, crypton-x509-store >=1.10 && <1.11, data-default, iproute >=1.2.2,