{-# LANGUAGE CPP #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE ViewPatterns #-}
module Data.X509.DistinguishedName (
DistinguishedName (DistinguishedName, getDistinguishedElements),
RelativeDistinguishedName,
getRDNs,
fromRDNs,
DistinguishedNameInner (..),
ASN1CharacterString (..),
DnElement (..),
getDnElement,
canonicalDistinguishedName,
) where
#if !(MIN_VERSION_base(4,11,0))
import Data.Semigroup
#endif
import Data.ASN1.Types
import Data.Char (toLower)
import Data.List (sort)
import Data.X509.Internal
newtype DistinguishedName = RDNSequence [RelativeDistinguishedName]
deriving (Int -> DistinguishedName -> ShowS
[DistinguishedName] -> ShowS
DistinguishedName -> String
(Int -> DistinguishedName -> ShowS)
-> (DistinguishedName -> String)
-> ([DistinguishedName] -> ShowS)
-> Show DistinguishedName
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> DistinguishedName -> ShowS
showsPrec :: Int -> DistinguishedName -> ShowS
$cshow :: DistinguishedName -> String
show :: DistinguishedName -> String
$cshowList :: [DistinguishedName] -> ShowS
showList :: [DistinguishedName] -> ShowS
Show, DistinguishedName -> DistinguishedName -> Bool
(DistinguishedName -> DistinguishedName -> Bool)
-> (DistinguishedName -> DistinguishedName -> Bool)
-> Eq DistinguishedName
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: DistinguishedName -> DistinguishedName -> Bool
== :: DistinguishedName -> DistinguishedName -> Bool
$c/= :: DistinguishedName -> DistinguishedName -> Bool
/= :: DistinguishedName -> DistinguishedName -> Bool
Eq, Eq DistinguishedName
Eq DistinguishedName =>
(DistinguishedName -> DistinguishedName -> Ordering)
-> (DistinguishedName -> DistinguishedName -> Bool)
-> (DistinguishedName -> DistinguishedName -> Bool)
-> (DistinguishedName -> DistinguishedName -> Bool)
-> (DistinguishedName -> DistinguishedName -> Bool)
-> (DistinguishedName -> DistinguishedName -> DistinguishedName)
-> (DistinguishedName -> DistinguishedName -> DistinguishedName)
-> Ord DistinguishedName
DistinguishedName -> DistinguishedName -> Bool
DistinguishedName -> DistinguishedName -> Ordering
DistinguishedName -> DistinguishedName -> DistinguishedName
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: DistinguishedName -> DistinguishedName -> Ordering
compare :: DistinguishedName -> DistinguishedName -> Ordering
$c< :: DistinguishedName -> DistinguishedName -> Bool
< :: DistinguishedName -> DistinguishedName -> Bool
$c<= :: DistinguishedName -> DistinguishedName -> Bool
<= :: DistinguishedName -> DistinguishedName -> Bool
$c> :: DistinguishedName -> DistinguishedName -> Bool
> :: DistinguishedName -> DistinguishedName -> Bool
$c>= :: DistinguishedName -> DistinguishedName -> Bool
>= :: DistinguishedName -> DistinguishedName -> Bool
$cmax :: DistinguishedName -> DistinguishedName -> DistinguishedName
max :: DistinguishedName -> DistinguishedName -> DistinguishedName
$cmin :: DistinguishedName -> DistinguishedName -> DistinguishedName
min :: DistinguishedName -> DistinguishedName -> DistinguishedName
Ord)
type RelativeDistinguishedName = [(OID, ASN1CharacterString)]
getRDNs :: DistinguishedName -> [RelativeDistinguishedName]
getRDNs :: DistinguishedName -> [RelativeDistinguishedName]
getRDNs (RDNSequence [RelativeDistinguishedName]
rdns) = [RelativeDistinguishedName]
rdns
fromRDNs :: [RelativeDistinguishedName] -> DistinguishedName
fromRDNs :: [RelativeDistinguishedName] -> DistinguishedName
fromRDNs = [RelativeDistinguishedName] -> DistinguishedName
RDNSequence
pattern DistinguishedName
:: [(OID, ASN1CharacterString)] -> DistinguishedName
pattern $mDistinguishedName :: forall {r}.
DistinguishedName
-> (RelativeDistinguishedName -> r) -> ((# #) -> r) -> r
$bDistinguishedName :: RelativeDistinguishedName -> DistinguishedName
DistinguishedName{DistinguishedName -> RelativeDistinguishedName
getDistinguishedElements} <-
(concat . getRDNs -> getDistinguishedElements)
where
DistinguishedName RelativeDistinguishedName
els = [RelativeDistinguishedName] -> DistinguishedName
RDNSequence (((OID, ASN1CharacterString) -> RelativeDistinguishedName)
-> RelativeDistinguishedName -> [RelativeDistinguishedName]
forall a b. (a -> b) -> [a] -> [b]
map ((OID, ASN1CharacterString)
-> RelativeDistinguishedName -> RelativeDistinguishedName
forall a. a -> [a] -> [a]
: []) RelativeDistinguishedName
els)
{-# COMPLETE DistinguishedName #-}
data DnElement
=
DnCommonName
|
DnCountry
|
DnOrganization
|
DnOrganizationUnit
|
DnEmailAddress
|
DnLocality
|
DnStateOrProvince
|
DnStreetAddress
|
DnPostalCode
|
DnSerialNumber
|
DnTitle
|
DnName
|
DnSurname
|
DnGivenName
|
DnInitials
|
DnGenerationQualifier
|
DnQualifier
|
DnPseudonym
|
DnDomainComponent
|
DnUserId
deriving (Int -> DnElement -> ShowS
[DnElement] -> ShowS
DnElement -> String
(Int -> DnElement -> ShowS)
-> (DnElement -> String)
-> ([DnElement] -> ShowS)
-> Show DnElement
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> DnElement -> ShowS
showsPrec :: Int -> DnElement -> ShowS
$cshow :: DnElement -> String
show :: DnElement -> String
$cshowList :: [DnElement] -> ShowS
showList :: [DnElement] -> ShowS
Show, DnElement -> DnElement -> Bool
(DnElement -> DnElement -> Bool)
-> (DnElement -> DnElement -> Bool) -> Eq DnElement
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: DnElement -> DnElement -> Bool
== :: DnElement -> DnElement -> Bool
$c/= :: DnElement -> DnElement -> Bool
/= :: DnElement -> DnElement -> Bool
Eq, Int -> DnElement
DnElement -> Int
DnElement -> [DnElement]
DnElement -> DnElement
DnElement -> DnElement -> [DnElement]
DnElement -> DnElement -> DnElement -> [DnElement]
(DnElement -> DnElement)
-> (DnElement -> DnElement)
-> (Int -> DnElement)
-> (DnElement -> Int)
-> (DnElement -> [DnElement])
-> (DnElement -> DnElement -> [DnElement])
-> (DnElement -> DnElement -> [DnElement])
-> (DnElement -> DnElement -> DnElement -> [DnElement])
-> Enum DnElement
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
$csucc :: DnElement -> DnElement
succ :: DnElement -> DnElement
$cpred :: DnElement -> DnElement
pred :: DnElement -> DnElement
$ctoEnum :: Int -> DnElement
toEnum :: Int -> DnElement
$cfromEnum :: DnElement -> Int
fromEnum :: DnElement -> Int
$cenumFrom :: DnElement -> [DnElement]
enumFrom :: DnElement -> [DnElement]
$cenumFromThen :: DnElement -> DnElement -> [DnElement]
enumFromThen :: DnElement -> DnElement -> [DnElement]
$cenumFromTo :: DnElement -> DnElement -> [DnElement]
enumFromTo :: DnElement -> DnElement -> [DnElement]
$cenumFromThenTo :: DnElement -> DnElement -> DnElement -> [DnElement]
enumFromThenTo :: DnElement -> DnElement -> DnElement -> [DnElement]
Enum, DnElement
DnElement -> DnElement -> Bounded DnElement
forall a. a -> a -> Bounded a
$cminBound :: DnElement
minBound :: DnElement
$cmaxBound :: DnElement
maxBound :: DnElement
Bounded)
instance OIDable DnElement where
getObjectID :: DnElement -> OID
getObjectID DnElement
DnCommonName = [Integer
2, Integer
5, Integer
4, Integer
3]
getObjectID DnElement
DnCountry = [Integer
2, Integer
5, Integer
4, Integer
6]
getObjectID DnElement
DnOrganization = [Integer
2, Integer
5, Integer
4, Integer
10]
getObjectID DnElement
DnOrganizationUnit = [Integer
2, Integer
5, Integer
4, Integer
11]
getObjectID DnElement
DnEmailAddress = [Integer
1, Integer
2, Integer
840, Integer
113549, Integer
1, Integer
9, Integer
1]
getObjectID DnElement
DnLocality = [Integer
2, Integer
5, Integer
4, Integer
7]
getObjectID DnElement
DnStateOrProvince = [Integer
2, Integer
5, Integer
4, Integer
8]
getObjectID DnElement
DnStreetAddress = [Integer
2, Integer
5, Integer
4, Integer
9]
getObjectID DnElement
DnPostalCode = [Integer
2, Integer
5, Integer
4, Integer
17]
getObjectID DnElement
DnSerialNumber = [Integer
2, Integer
5, Integer
4, Integer
5]
getObjectID DnElement
DnTitle = [Integer
2, Integer
5, Integer
4, Integer
12]
getObjectID DnElement
DnName = [Integer
2, Integer
5, Integer
4, Integer
41]
getObjectID DnElement
DnSurname = [Integer
2, Integer
5, Integer
4, Integer
4]
getObjectID DnElement
DnGivenName = [Integer
2, Integer
5, Integer
4, Integer
42]
getObjectID DnElement
DnInitials = [Integer
2, Integer
5, Integer
4, Integer
43]
getObjectID DnElement
DnGenerationQualifier = [Integer
2, Integer
5, Integer
4, Integer
44]
getObjectID DnElement
DnQualifier = [Integer
2, Integer
5, Integer
4, Integer
46]
getObjectID DnElement
DnPseudonym = [Integer
2, Integer
5, Integer
4, Integer
65]
getObjectID DnElement
DnDomainComponent = [Integer
0, Integer
9, Integer
2342, Integer
19200300, Integer
100, Integer
1, Integer
25]
getObjectID DnElement
DnUserId = [Integer
0, Integer
9, Integer
2342, Integer
19200300, Integer
100, Integer
1, Integer
1]
instance OIDNameable DnElement where
fromObjectID :: OID -> Maybe DnElement
fromObjectID OID
oid = OID -> [(OID, DnElement)] -> Maybe DnElement
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup OID
oid [(DnElement -> OID
forall a. OIDable a => a -> OID
getObjectID DnElement
e, DnElement
e) | DnElement
e <- [DnElement
forall a. Bounded a => a
minBound .. DnElement
forall a. Bounded a => a
maxBound]]
getDnElement :: DnElement -> DistinguishedName -> Maybe ASN1CharacterString
getDnElement :: DnElement -> DistinguishedName -> Maybe ASN1CharacterString
getDnElement DnElement
element DistinguishedName
dn = OID -> RelativeDistinguishedName -> Maybe ASN1CharacterString
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup (DnElement -> OID
forall a. OIDable a => a -> OID
getObjectID DnElement
element) (RelativeDistinguishedName -> Maybe ASN1CharacterString)
-> RelativeDistinguishedName -> Maybe ASN1CharacterString
forall a b. (a -> b) -> a -> b
$ DistinguishedName -> RelativeDistinguishedName
getDistinguishedElements DistinguishedName
dn
canonicalDistinguishedName :: DistinguishedName -> DistinguishedName
canonicalDistinguishedName :: DistinguishedName -> DistinguishedName
canonicalDistinguishedName (RDNSequence [RelativeDistinguishedName]
rdns) =
[RelativeDistinguishedName] -> DistinguishedName
RDNSequence [RelativeDistinguishedName -> RelativeDistinguishedName
forall a. Ord a => [a] -> [a]
sort [(OID
oid, ASN1CharacterString -> ASN1CharacterString
canon ASN1CharacterString
v) | (OID
oid, ASN1CharacterString
v) <- RelativeDistinguishedName
rdn] | RelativeDistinguishedName
rdn <- [RelativeDistinguishedName]
rdns]
where
canon :: ASN1CharacterString -> ASN1CharacterString
canon ASN1CharacterString
v = case ASN1CharacterString -> Maybe String
safeCharacterToString ASN1CharacterString
v of
Just String
str -> ASN1StringEncoding -> String -> ASN1CharacterString
asn1CharacterString ASN1StringEncoding
UTF8 (String -> ASN1CharacterString) -> String -> ASN1CharacterString
forall a b. (a -> b) -> a -> b
$ [String] -> String
unwords ([String] -> String) -> [String] -> String
forall a b. (a -> b) -> a -> b
$ String -> [String]
words (String -> [String]) -> String -> [String]
forall a b. (a -> b) -> a -> b
$ (Char -> Char) -> ShowS
forall a b. (a -> b) -> [a] -> [b]
map Char -> Char
toLower String
str
Maybe String
Nothing -> ASN1CharacterString
v
newtype DistinguishedNameInner = DistinguishedNameInner DistinguishedName
deriving (Int -> DistinguishedNameInner -> ShowS
[DistinguishedNameInner] -> ShowS
DistinguishedNameInner -> String
(Int -> DistinguishedNameInner -> ShowS)
-> (DistinguishedNameInner -> String)
-> ([DistinguishedNameInner] -> ShowS)
-> Show DistinguishedNameInner
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> DistinguishedNameInner -> ShowS
showsPrec :: Int -> DistinguishedNameInner -> ShowS
$cshow :: DistinguishedNameInner -> String
show :: DistinguishedNameInner -> String
$cshowList :: [DistinguishedNameInner] -> ShowS
showList :: [DistinguishedNameInner] -> ShowS
Show, DistinguishedNameInner -> DistinguishedNameInner -> Bool
(DistinguishedNameInner -> DistinguishedNameInner -> Bool)
-> (DistinguishedNameInner -> DistinguishedNameInner -> Bool)
-> Eq DistinguishedNameInner
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: DistinguishedNameInner -> DistinguishedNameInner -> Bool
== :: DistinguishedNameInner -> DistinguishedNameInner -> Bool
$c/= :: DistinguishedNameInner -> DistinguishedNameInner -> Bool
/= :: DistinguishedNameInner -> DistinguishedNameInner -> Bool
Eq)
#if MIN_VERSION_base(4,9,0)
instance Semigroup DistinguishedName where
RDNSequence [RelativeDistinguishedName]
l1 <> :: DistinguishedName -> DistinguishedName -> DistinguishedName
<> RDNSequence [RelativeDistinguishedName]
l2 = [RelativeDistinguishedName] -> DistinguishedName
RDNSequence ([RelativeDistinguishedName]
l1[RelativeDistinguishedName]
-> [RelativeDistinguishedName] -> [RelativeDistinguishedName]
forall a. [a] -> [a] -> [a]
++[RelativeDistinguishedName]
l2)
#endif
instance Monoid DistinguishedName where
mempty :: DistinguishedName
mempty = [RelativeDistinguishedName] -> DistinguishedName
RDNSequence []
#if !(MIN_VERSION_base(4,11,0))
mappend (RDNSequence l1) (RDNSequence l2) = RDNSequence (l1++l2)
#endif
instance ASN1Object DistinguishedName where
toASN1 :: DistinguishedName -> ASN1S
toASN1 DistinguishedName
dn = \[ASN1]
xs -> DistinguishedName -> [ASN1]
encodeDN DistinguishedName
dn [ASN1] -> ASN1S
forall a. [a] -> [a] -> [a]
++ [ASN1]
xs
fromASN1 :: [ASN1] -> Either String (DistinguishedName, [ASN1])
fromASN1 = ParseASN1 DistinguishedName
-> [ASN1] -> Either String (DistinguishedName, [ASN1])
forall a. ParseASN1 a -> [ASN1] -> Either String (a, [ASN1])
runParseASN1State ParseASN1 DistinguishedName
parseDN
instance ASN1Object DistinguishedNameInner where
toASN1 :: DistinguishedNameInner -> ASN1S
toASN1 (DistinguishedNameInner DistinguishedName
dn) = \[ASN1]
xs -> DistinguishedName -> [ASN1]
encodeDNinner DistinguishedName
dn [ASN1] -> ASN1S
forall a. [a] -> [a] -> [a]
++ [ASN1]
xs
fromASN1 :: [ASN1] -> Either String (DistinguishedNameInner, [ASN1])
fromASN1 =
ParseASN1 DistinguishedNameInner
-> [ASN1] -> Either String (DistinguishedNameInner, [ASN1])
forall a. ParseASN1 a -> [ASN1] -> Either String (a, [ASN1])
runParseASN1State (DistinguishedName -> DistinguishedNameInner
DistinguishedNameInner (DistinguishedName -> DistinguishedNameInner)
-> ([RelativeDistinguishedName] -> DistinguishedName)
-> [RelativeDistinguishedName]
-> DistinguishedNameInner
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [RelativeDistinguishedName] -> DistinguishedName
RDNSequence ([RelativeDistinguishedName] -> DistinguishedNameInner)
-> ParseASN1 [RelativeDistinguishedName]
-> ParseASN1 DistinguishedNameInner
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParseASN1 [RelativeDistinguishedName]
parseDNInner)
parseDN :: ParseASN1 DistinguishedName
parseDN :: ParseASN1 DistinguishedName
parseDN = [RelativeDistinguishedName] -> DistinguishedName
RDNSequence ([RelativeDistinguishedName] -> DistinguishedName)
-> ParseASN1 [RelativeDistinguishedName]
-> ParseASN1 DistinguishedName
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ASN1ConstructionType
-> ParseASN1 [RelativeDistinguishedName]
-> ParseASN1 [RelativeDistinguishedName]
forall a. ASN1ConstructionType -> ParseASN1 a -> ParseASN1 a
onNextContainer ASN1ConstructionType
Sequence ParseASN1 [RelativeDistinguishedName]
parseDNInner
parseDNInner :: ParseASN1 [RelativeDistinguishedName]
parseDNInner :: ParseASN1 [RelativeDistinguishedName]
parseDNInner = ParseASN1 RelativeDistinguishedName
-> ParseASN1 [RelativeDistinguishedName]
forall a. ParseASN1 a -> ParseASN1 [a]
getMany ParseASN1 RelativeDistinguishedName
parseOneDN
parseOneDN :: ParseASN1 RelativeDistinguishedName
parseOneDN :: ParseASN1 RelativeDistinguishedName
parseOneDN = ASN1ConstructionType
-> ParseASN1 RelativeDistinguishedName
-> ParseASN1 RelativeDistinguishedName
forall a. ASN1ConstructionType -> ParseASN1 a -> ParseASN1 a
onNextContainer ASN1ConstructionType
Set (ParseASN1 RelativeDistinguishedName
-> ParseASN1 RelativeDistinguishedName)
-> ParseASN1 RelativeDistinguishedName
-> ParseASN1 RelativeDistinguishedName
forall a b. (a -> b) -> a -> b
$ ParseASN1 (OID, ASN1CharacterString)
-> ParseASN1 RelativeDistinguishedName
forall a. ParseASN1 a -> ParseASN1 [a]
getMany (ParseASN1 (OID, ASN1CharacterString)
-> ParseASN1 RelativeDistinguishedName)
-> ParseASN1 (OID, ASN1CharacterString)
-> ParseASN1 RelativeDistinguishedName
forall a b. (a -> b) -> a -> b
$ do
[ASN1]
s <- ASN1ConstructionType -> ParseASN1 [ASN1]
getNextContainer ASN1ConstructionType
Sequence
case [ASN1]
s of
[OID OID
oid, ASN1String ASN1CharacterString
cs] -> (OID, ASN1CharacterString) -> ParseASN1 (OID, ASN1CharacterString)
forall a. a -> ParseASN1 a
forall (m :: * -> *) a. Monad m => a -> m a
return (OID
oid, ASN1CharacterString
cs)
[ASN1]
_ ->
String -> ParseASN1 (OID, ASN1CharacterString)
forall a. String -> ParseASN1 a
throwParseError (String
"expecting [OID,String] got " String -> ShowS
forall a. [a] -> [a] -> [a]
++ [ASN1] -> String
forall a. Show a => a -> String
show [ASN1]
s)
encodeDNinner :: DistinguishedName -> [ASN1]
encodeDNinner :: DistinguishedName -> [ASN1]
encodeDNinner (RDNSequence [RelativeDistinguishedName]
rdns) = (RelativeDistinguishedName -> [ASN1])
-> [RelativeDistinguishedName] -> [ASN1]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap RelativeDistinguishedName -> [ASN1]
forall {t :: * -> *}.
Foldable t =>
t (OID, ASN1CharacterString) -> [ASN1]
rdnSet [RelativeDistinguishedName]
rdns
where
rdnSet :: t (OID, ASN1CharacterString) -> [ASN1]
rdnSet t (OID, ASN1CharacterString)
rdn = ASN1ConstructionType -> ASN1S
asn1Container ASN1ConstructionType
Set ASN1S -> ASN1S
forall a b. (a -> b) -> a -> b
$ ((OID, ASN1CharacterString) -> [ASN1])
-> t (OID, ASN1CharacterString) -> [ASN1]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (OID, ASN1CharacterString) -> [ASN1]
atv t (OID, ASN1CharacterString)
rdn
atv :: (OID, ASN1CharacterString) -> [ASN1]
atv (OID
oid, ASN1CharacterString
cs) = ASN1ConstructionType -> ASN1S
asn1Container ASN1ConstructionType
Sequence [OID -> ASN1
OID OID
oid, ASN1CharacterString -> ASN1
ASN1String ASN1CharacterString
cs]
encodeDN :: DistinguishedName -> [ASN1]
encodeDN :: DistinguishedName -> [ASN1]
encodeDN DistinguishedName
dn = ASN1ConstructionType -> ASN1S
asn1Container ASN1ConstructionType
Sequence ASN1S -> ASN1S
forall a b. (a -> b) -> a -> b
$ DistinguishedName -> [ASN1]
encodeDNinner DistinguishedName
dn