-- |
-- Module      : Data.X509.AlgorithmIdentifier
-- License     : BSD-style
-- Maintainer  : Vincent Hanquez <vincent@snarc.org>
-- Stability   : experimental
-- Portability : unknown
--
module Data.X509.AlgorithmIdentifier
    ( HashALG(..)
    , PubKeyALG(..)
    , SignatureALG(..)
    ) where

import Data.ASN1.Types
import Data.List (find)

-- | Hash Algorithm
data HashALG =
      HashMD2
    | HashMD5
    | HashSHA1
    | HashSHA224
    | HashSHA256
    | HashSHA384
    | HashSHA512
    deriving (Int -> HashALG -> ShowS
[HashALG] -> ShowS
HashALG -> String
(Int -> HashALG -> ShowS)
-> (HashALG -> String) -> ([HashALG] -> ShowS) -> Show HashALG
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [HashALG] -> ShowS
$cshowList :: [HashALG] -> ShowS
show :: HashALG -> String
$cshow :: HashALG -> String
showsPrec :: Int -> HashALG -> ShowS
$cshowsPrec :: Int -> HashALG -> ShowS
Show,HashALG -> HashALG -> Bool
(HashALG -> HashALG -> Bool)
-> (HashALG -> HashALG -> Bool) -> Eq HashALG
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: HashALG -> HashALG -> Bool
$c/= :: HashALG -> HashALG -> Bool
== :: HashALG -> HashALG -> Bool
$c== :: HashALG -> HashALG -> Bool
Eq)

-- | Public Key Algorithm
data PubKeyALG =
      PubKeyALG_RSA         -- ^ RSA Public Key algorithm
    | PubKeyALG_RSAPSS      -- ^ RSA PSS Key algorithm (RFC 3447)
    | PubKeyALG_DSA         -- ^ DSA Public Key algorithm
    | PubKeyALG_EC          -- ^ ECDSA & ECDH Public Key algorithm
    | PubKeyALG_X25519      -- ^ ECDH 25519 key agreement
    | PubKeyALG_X448        -- ^ ECDH 448 key agreement
    | PubKeyALG_Ed25519     -- ^ EdDSA 25519 signature algorithm
    | PubKeyALG_Ed448       -- ^ EdDSA 448 signature algorithm
    | PubKeyALG_DH          -- ^ Diffie Hellman Public Key algorithm
    | PubKeyALG_Unknown OID -- ^ Unknown Public Key algorithm
    deriving (Int -> PubKeyALG -> ShowS
[PubKeyALG] -> ShowS
PubKeyALG -> String
(Int -> PubKeyALG -> ShowS)
-> (PubKeyALG -> String)
-> ([PubKeyALG] -> ShowS)
-> Show PubKeyALG
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PubKeyALG] -> ShowS
$cshowList :: [PubKeyALG] -> ShowS
show :: PubKeyALG -> String
$cshow :: PubKeyALG -> String
showsPrec :: Int -> PubKeyALG -> ShowS
$cshowsPrec :: Int -> PubKeyALG -> ShowS
Show,PubKeyALG -> PubKeyALG -> Bool
(PubKeyALG -> PubKeyALG -> Bool)
-> (PubKeyALG -> PubKeyALG -> Bool) -> Eq PubKeyALG
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PubKeyALG -> PubKeyALG -> Bool
$c/= :: PubKeyALG -> PubKeyALG -> Bool
== :: PubKeyALG -> PubKeyALG -> Bool
$c== :: PubKeyALG -> PubKeyALG -> Bool
Eq)

-- | Signature Algorithm, often composed of a public key algorithm and a hash
-- algorithm.  For some signature algorithms the hash algorithm is intrinsic to
-- the public key algorithm and is not needed in the data type.
data SignatureALG =
      SignatureALG HashALG PubKeyALG
    | SignatureALG_IntrinsicHash PubKeyALG
    | SignatureALG_Unknown OID
    deriving (Int -> SignatureALG -> ShowS
[SignatureALG] -> ShowS
SignatureALG -> String
(Int -> SignatureALG -> ShowS)
-> (SignatureALG -> String)
-> ([SignatureALG] -> ShowS)
-> Show SignatureALG
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SignatureALG] -> ShowS
$cshowList :: [SignatureALG] -> ShowS
show :: SignatureALG -> String
$cshow :: SignatureALG -> String
showsPrec :: Int -> SignatureALG -> ShowS
$cshowsPrec :: Int -> SignatureALG -> ShowS
Show,SignatureALG -> SignatureALG -> Bool
(SignatureALG -> SignatureALG -> Bool)
-> (SignatureALG -> SignatureALG -> Bool) -> Eq SignatureALG
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SignatureALG -> SignatureALG -> Bool
$c/= :: SignatureALG -> SignatureALG -> Bool
== :: SignatureALG -> SignatureALG -> Bool
$c== :: SignatureALG -> SignatureALG -> Bool
Eq)

instance OIDable PubKeyALG where
    getObjectID :: PubKeyALG -> OID
getObjectID PubKeyALG
PubKeyALG_RSA    = [Integer
1,Integer
2,Integer
840,Integer
113549,Integer
1,Integer
1,Integer
1]
    getObjectID PubKeyALG
PubKeyALG_RSAPSS = [Integer
1,Integer
2,Integer
840,Integer
113549,Integer
1,Integer
1,Integer
10]
    getObjectID PubKeyALG
PubKeyALG_DSA    = [Integer
1,Integer
2,Integer
840,Integer
10040,Integer
4,Integer
1]
    getObjectID PubKeyALG
PubKeyALG_EC     = [Integer
1,Integer
2,Integer
840,Integer
10045,Integer
2,Integer
1]
    getObjectID PubKeyALG
PubKeyALG_X25519    = [Integer
1,Integer
3,Integer
101,Integer
110]
    getObjectID PubKeyALG
PubKeyALG_X448      = [Integer
1,Integer
3,Integer
101,Integer
111]
    getObjectID PubKeyALG
PubKeyALG_Ed25519   = [Integer
1,Integer
3,Integer
101,Integer
112]
    getObjectID PubKeyALG
PubKeyALG_Ed448     = [Integer
1,Integer
3,Integer
101,Integer
113]
    getObjectID PubKeyALG
PubKeyALG_DH     = [Integer
1,Integer
2,Integer
840,Integer
10046,Integer
2,Integer
1]
    getObjectID (PubKeyALG_Unknown OID
oid) = OID
oid

sig_table :: [ (OID, SignatureALG) ]
sig_table :: [(OID, SignatureALG)]
sig_table =
        [ ([Integer
1,Integer
2,Integer
840,Integer
113549,Integer
1,Integer
1,Integer
5], HashALG -> PubKeyALG -> SignatureALG
SignatureALG HashALG
HashSHA1 PubKeyALG
PubKeyALG_RSA)
        , ([Integer
1,Integer
2,Integer
840,Integer
113549,Integer
1,Integer
1,Integer
4], HashALG -> PubKeyALG -> SignatureALG
SignatureALG HashALG
HashMD5 PubKeyALG
PubKeyALG_RSA)
        , ([Integer
1,Integer
2,Integer
840,Integer
113549,Integer
1,Integer
1,Integer
2], HashALG -> PubKeyALG -> SignatureALG
SignatureALG HashALG
HashMD2 PubKeyALG
PubKeyALG_RSA)
        , ([Integer
1,Integer
2,Integer
840,Integer
113549,Integer
1,Integer
1,Integer
11], HashALG -> PubKeyALG -> SignatureALG
SignatureALG HashALG
HashSHA256 PubKeyALG
PubKeyALG_RSA)
        , ([Integer
1,Integer
2,Integer
840,Integer
113549,Integer
1,Integer
1,Integer
12], HashALG -> PubKeyALG -> SignatureALG
SignatureALG HashALG
HashSHA384 PubKeyALG
PubKeyALG_RSA)
        , ([Integer
1,Integer
2,Integer
840,Integer
113549,Integer
1,Integer
1,Integer
13], HashALG -> PubKeyALG -> SignatureALG
SignatureALG HashALG
HashSHA512 PubKeyALG
PubKeyALG_RSA)
        , ([Integer
1,Integer
2,Integer
840,Integer
113549,Integer
1,Integer
1,Integer
14], HashALG -> PubKeyALG -> SignatureALG
SignatureALG HashALG
HashSHA224 PubKeyALG
PubKeyALG_RSA)
        , ([Integer
1,Integer
2,Integer
840,Integer
10040,Integer
4,Integer
3],    HashALG -> PubKeyALG -> SignatureALG
SignatureALG HashALG
HashSHA1 PubKeyALG
PubKeyALG_DSA)
        , ([Integer
1,Integer
2,Integer
840,Integer
10045,Integer
4,Integer
1],    HashALG -> PubKeyALG -> SignatureALG
SignatureALG HashALG
HashSHA1 PubKeyALG
PubKeyALG_EC)
        , ([Integer
1,Integer
2,Integer
840,Integer
10045,Integer
4,Integer
3,Integer
1],  HashALG -> PubKeyALG -> SignatureALG
SignatureALG HashALG
HashSHA224 PubKeyALG
PubKeyALG_EC)
        , ([Integer
1,Integer
2,Integer
840,Integer
10045,Integer
4,Integer
3,Integer
2],  HashALG -> PubKeyALG -> SignatureALG
SignatureALG HashALG
HashSHA256 PubKeyALG
PubKeyALG_EC)
        , ([Integer
1,Integer
2,Integer
840,Integer
10045,Integer
4,Integer
3,Integer
3],  HashALG -> PubKeyALG -> SignatureALG
SignatureALG HashALG
HashSHA384 PubKeyALG
PubKeyALG_EC)
        , ([Integer
1,Integer
2,Integer
840,Integer
10045,Integer
4,Integer
3,Integer
4],  HashALG -> PubKeyALG -> SignatureALG
SignatureALG HashALG
HashSHA512 PubKeyALG
PubKeyALG_EC)
        , ([Integer
2,Integer
16,Integer
840,Integer
1,Integer
101,Integer
3,Integer
4,Integer
2,Integer
1],  HashALG -> PubKeyALG -> SignatureALG
SignatureALG HashALG
HashSHA256 PubKeyALG
PubKeyALG_RSAPSS)
        , ([Integer
2,Integer
16,Integer
840,Integer
1,Integer
101,Integer
3,Integer
4,Integer
2,Integer
2],  HashALG -> PubKeyALG -> SignatureALG
SignatureALG HashALG
HashSHA384 PubKeyALG
PubKeyALG_RSAPSS)
        , ([Integer
2,Integer
16,Integer
840,Integer
1,Integer
101,Integer
3,Integer
4,Integer
2,Integer
3],  HashALG -> PubKeyALG -> SignatureALG
SignatureALG HashALG
HashSHA512 PubKeyALG
PubKeyALG_RSAPSS)
        , ([Integer
2,Integer
16,Integer
840,Integer
1,Integer
101,Integer
3,Integer
4,Integer
2,Integer
4],  HashALG -> PubKeyALG -> SignatureALG
SignatureALG HashALG
HashSHA224 PubKeyALG
PubKeyALG_RSAPSS)
        , ([Integer
2,Integer
16,Integer
840,Integer
1,Integer
101,Integer
3,Integer
4,Integer
3,Integer
1],  HashALG -> PubKeyALG -> SignatureALG
SignatureALG HashALG
HashSHA224 PubKeyALG
PubKeyALG_DSA)
        , ([Integer
2,Integer
16,Integer
840,Integer
1,Integer
101,Integer
3,Integer
4,Integer
3,Integer
2],  HashALG -> PubKeyALG -> SignatureALG
SignatureALG HashALG
HashSHA256 PubKeyALG
PubKeyALG_DSA)
        , ([Integer
1,Integer
3,Integer
101,Integer
112], PubKeyALG -> SignatureALG
SignatureALG_IntrinsicHash PubKeyALG
PubKeyALG_Ed25519)
        , ([Integer
1,Integer
3,Integer
101,Integer
113], PubKeyALG -> SignatureALG
SignatureALG_IntrinsicHash PubKeyALG
PubKeyALG_Ed448)
        ]

oidSig :: OID -> SignatureALG
oidSig :: OID -> SignatureALG
oidSig OID
oid = SignatureALG
-> (SignatureALG -> SignatureALG)
-> Maybe SignatureALG
-> SignatureALG
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (OID -> SignatureALG
SignatureALG_Unknown OID
oid) SignatureALG -> SignatureALG
forall a. a -> a
id (Maybe SignatureALG -> SignatureALG)
-> Maybe SignatureALG -> SignatureALG
forall a b. (a -> b) -> a -> b
$ OID -> [(OID, SignatureALG)] -> Maybe SignatureALG
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup OID
oid [(OID, SignatureALG)]
sig_table

sigOID :: SignatureALG -> OID
sigOID :: SignatureALG -> OID
sigOID (SignatureALG_Unknown OID
oid) = OID
oid
sigOID SignatureALG
sig = OID
-> ((OID, SignatureALG) -> OID) -> Maybe (OID, SignatureALG) -> OID
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (String -> OID
forall a. HasCallStack => String -> a
error (String
"unknown OID for " String -> ShowS
forall a. [a] -> [a] -> [a]
++ SignatureALG -> String
forall a. Show a => a -> String
show SignatureALG
sig)) (OID, SignatureALG) -> OID
forall a b. (a, b) -> a
fst (Maybe (OID, SignatureALG) -> OID)
-> Maybe (OID, SignatureALG) -> OID
forall a b. (a -> b) -> a -> b
$ ((OID, SignatureALG) -> Bool)
-> [(OID, SignatureALG)] -> Maybe (OID, SignatureALG)
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find (SignatureALG -> SignatureALG -> Bool
forall a. Eq a => a -> a -> Bool
(==) SignatureALG
sig (SignatureALG -> Bool)
-> ((OID, SignatureALG) -> SignatureALG)
-> (OID, SignatureALG)
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (OID, SignatureALG) -> SignatureALG
forall a b. (a, b) -> b
snd) [(OID, SignatureALG)]
sig_table

-- | PSS salt length. Always assume ``-sigopt rsa_pss_saltlen:-1``
saltLen :: HashALG -> Integer
saltLen :: HashALG -> Integer
saltLen HashALG
HashSHA256 = Integer
32
saltLen HashALG
HashSHA384 = Integer
48
saltLen HashALG
HashSHA512 = Integer
64
saltLen HashALG
HashSHA224 = Integer
28
saltLen HashALG
_          = String -> Integer
forall a. HasCallStack => String -> a
error String
"toASN1: X509.SignatureAlg.HashAlg: Unknown hash"

instance ASN1Object SignatureALG where
    fromASN1 :: [ASN1] -> Either String (SignatureALG, [ASN1])
fromASN1 (Start ASN1ConstructionType
Sequence:OID OID
oid:ASN1
Null:End ASN1ConstructionType
Sequence:[ASN1]
xs) =
        (SignatureALG, [ASN1]) -> Either String (SignatureALG, [ASN1])
forall a b. b -> Either a b
Right (OID -> SignatureALG
oidSig OID
oid, [ASN1]
xs)
    fromASN1 (Start ASN1ConstructionType
Sequence:OID OID
oid:End ASN1ConstructionType
Sequence:[ASN1]
xs) =
        (SignatureALG, [ASN1]) -> Either String (SignatureALG, [ASN1])
forall a b. b -> Either a b
Right (OID -> SignatureALG
oidSig OID
oid, [ASN1]
xs)
    fromASN1 (Start ASN1ConstructionType
Sequence:OID [Integer
1,Integer
2,Integer
840,Integer
113549,Integer
1,Integer
1,Integer
10]:Start ASN1ConstructionType
Sequence:Start ASN1ConstructionType
_:Start ASN1ConstructionType
Sequence:OID OID
hash1:End ASN1ConstructionType
Sequence:End ASN1ConstructionType
_:Start ASN1ConstructionType
_:Start ASN1ConstructionType
Sequence:OID [Integer
1,Integer
2,Integer
840,Integer
113549,Integer
1,Integer
1,Integer
8]:Start ASN1ConstructionType
Sequence:OID OID
_hash2:End ASN1ConstructionType
Sequence:End ASN1ConstructionType
Sequence:End ASN1ConstructionType
_:Start ASN1ConstructionType
_: IntVal Integer
_iv: End ASN1ConstructionType
_: End ASN1ConstructionType
Sequence : End ASN1ConstructionType
Sequence:[ASN1]
xs) =
        (SignatureALG, [ASN1]) -> Either String (SignatureALG, [ASN1])
forall a b. b -> Either a b
Right (OID -> SignatureALG
oidSig OID
hash1, [ASN1]
xs)
    fromASN1 (Start ASN1ConstructionType
Sequence:OID [Integer
1,Integer
2,Integer
840,Integer
113549,Integer
1,Integer
1,Integer
10]:Start ASN1ConstructionType
Sequence:Start ASN1ConstructionType
_:Start ASN1ConstructionType
Sequence:OID OID
hash1:ASN1
Null:End ASN1ConstructionType
Sequence:End ASN1ConstructionType
_:Start ASN1ConstructionType
_:Start ASN1ConstructionType
Sequence:OID [Integer
1,Integer
2,Integer
840,Integer
113549,Integer
1,Integer
1,Integer
8]:Start ASN1ConstructionType
Sequence:OID OID
_hash2:ASN1
Null:End ASN1ConstructionType
Sequence:End ASN1ConstructionType
Sequence:End ASN1ConstructionType
_:Start ASN1ConstructionType
_: IntVal Integer
_iv: End ASN1ConstructionType
_: End ASN1ConstructionType
Sequence : End ASN1ConstructionType
Sequence:[ASN1]
xs) =
        (SignatureALG, [ASN1]) -> Either String (SignatureALG, [ASN1])
forall a b. b -> Either a b
Right (OID -> SignatureALG
oidSig OID
hash1, [ASN1]
xs)
    fromASN1 [ASN1]
_ =
        String -> Either String (SignatureALG, [ASN1])
forall a b. a -> Either a b
Left String
"fromASN1: X509.SignatureALG: unknown format"
    toASN1 :: SignatureALG -> ASN1S
toASN1 (SignatureALG_Unknown OID
oid) = \[ASN1]
xs -> ASN1ConstructionType -> ASN1
Start ASN1ConstructionType
SequenceASN1 -> ASN1S
forall a. a -> [a] -> [a]
:OID -> ASN1
OID OID
oidASN1 -> ASN1S
forall a. a -> [a] -> [a]
:ASN1
NullASN1 -> ASN1S
forall a. a -> [a] -> [a]
:ASN1ConstructionType -> ASN1
End ASN1ConstructionType
SequenceASN1 -> ASN1S
forall a. a -> [a] -> [a]
:[ASN1]
xs
    toASN1 signatureAlg :: SignatureALG
signatureAlg@(SignatureALG HashALG
hashAlg PubKeyALG
PubKeyALG_RSAPSS) = \[ASN1]
xs -> ASN1ConstructionType -> ASN1
Start ASN1ConstructionType
SequenceASN1 -> ASN1S
forall a. a -> [a] -> [a]
:OID -> ASN1
OID [Integer
1,Integer
2,Integer
840,Integer
113549,Integer
1,Integer
1,Integer
10]ASN1 -> ASN1S
forall a. a -> [a] -> [a]
:ASN1ConstructionType -> ASN1
Start ASN1ConstructionType
SequenceASN1 -> ASN1S
forall a. a -> [a] -> [a]
:ASN1ConstructionType -> ASN1
Start (ASN1Class -> Int -> ASN1ConstructionType
Container ASN1Class
Context Int
0)ASN1 -> ASN1S
forall a. a -> [a] -> [a]
:ASN1ConstructionType -> ASN1
Start ASN1ConstructionType
SequenceASN1 -> ASN1S
forall a. a -> [a] -> [a]
:OID -> ASN1
OID (SignatureALG -> OID
sigOID SignatureALG
signatureAlg)ASN1 -> ASN1S
forall a. a -> [a] -> [a]
:ASN1ConstructionType -> ASN1
End ASN1ConstructionType
SequenceASN1 -> ASN1S
forall a. a -> [a] -> [a]
:ASN1ConstructionType -> ASN1
End (ASN1Class -> Int -> ASN1ConstructionType
Container ASN1Class
Context Int
0)ASN1 -> ASN1S
forall a. a -> [a] -> [a]
:ASN1ConstructionType -> ASN1
Start (ASN1Class -> Int -> ASN1ConstructionType
Container ASN1Class
Context Int
1)ASN1 -> ASN1S
forall a. a -> [a] -> [a]
: ASN1ConstructionType -> ASN1
Start ASN1ConstructionType
SequenceASN1 -> ASN1S
forall a. a -> [a] -> [a]
:OID -> ASN1
OID [Integer
1,Integer
2,Integer
840,Integer
113549,Integer
1,Integer
1,Integer
8]ASN1 -> ASN1S
forall a. a -> [a] -> [a]
:ASN1ConstructionType -> ASN1
Start ASN1ConstructionType
SequenceASN1 -> ASN1S
forall a. a -> [a] -> [a]
:OID -> ASN1
OID (SignatureALG -> OID
sigOID SignatureALG
signatureAlg)ASN1 -> ASN1S
forall a. a -> [a] -> [a]
:ASN1ConstructionType -> ASN1
End ASN1ConstructionType
SequenceASN1 -> ASN1S
forall a. a -> [a] -> [a]
:ASN1ConstructionType -> ASN1
End ASN1ConstructionType
SequenceASN1 -> ASN1S
forall a. a -> [a] -> [a]
:ASN1ConstructionType -> ASN1
End (ASN1Class -> Int -> ASN1ConstructionType
Container ASN1Class
Context Int
1)ASN1 -> ASN1S
forall a. a -> [a] -> [a]
:ASN1ConstructionType -> ASN1
Start (ASN1Class -> Int -> ASN1ConstructionType
Container ASN1Class
Context Int
2)ASN1 -> ASN1S
forall a. a -> [a] -> [a]
:Integer -> ASN1
IntVal (HashALG -> Integer
saltLen HashALG
hashAlg)ASN1 -> ASN1S
forall a. a -> [a] -> [a]
:ASN1ConstructionType -> ASN1
End (ASN1Class -> Int -> ASN1ConstructionType
Container ASN1Class
Context Int
2)ASN1 -> ASN1S
forall a. a -> [a] -> [a]
:ASN1ConstructionType -> ASN1
End ASN1ConstructionType
SequenceASN1 -> ASN1S
forall a. a -> [a] -> [a]
:ASN1ConstructionType -> ASN1
End ASN1ConstructionType
SequenceASN1 -> ASN1S
forall a. a -> [a] -> [a]
:[ASN1]
xs
    toASN1 SignatureALG
signatureAlg = \[ASN1]
xs -> ASN1ConstructionType -> ASN1
Start ASN1ConstructionType
SequenceASN1 -> ASN1S
forall a. a -> [a] -> [a]
:OID -> ASN1
OID (SignatureALG -> OID
sigOID SignatureALG
signatureAlg)ASN1 -> ASN1S
forall a. a -> [a] -> [a]
:ASN1
NullASN1 -> ASN1S
forall a. a -> [a] -> [a]
:ASN1ConstructionType -> ASN1
End ASN1ConstructionType
SequenceASN1 -> ASN1S
forall a. a -> [a] -> [a]
:[ASN1]
xs