{-# LANGUAGE TypeApplications #-}
module Data.X509.PublicKey (
PubKey (..),
PubKeyEC (..),
PubKeyDH,
dhPublicKey,
pubkeyDH_p,
pubkeyDH_g,
pubkeyDH_q,
pubkeyDH_j,
pubkeyDH_validation,
pubkeyDH_y,
SerializedPoint (..),
pubkeyToAlg,
) where
import Data.ASN1.BinaryEncoding
import Data.ASN1.BitArray
import Data.ASN1.Encoding
import Data.ASN1.Types
import Data.Bits
import Data.ByteArray (convert)
import Data.ByteString (ByteString)
import Data.X509.AlgorithmIdentifier
import Data.X509.Internal
import Data.X509.OID
import Crypto.Error (CryptoFailable (..))
import Crypto.Number.Basic (numBytes)
import Crypto.Number.Serialize (i2osp, i2ospOf_, os2ip)
import qualified Crypto.PubKey.Curve25519 as X25519
import qualified Crypto.PubKey.Curve448 as X448
import qualified Crypto.PubKey.DSA as DSA
import qualified Crypto.PubKey.ECC.Types as ECC
import qualified Crypto.PubKey.Ed25519 as Ed25519
import qualified Crypto.PubKey.Ed448 as Ed448
import qualified Crypto.PubKey.MLDSA as MLDSA
import qualified Crypto.PubKey.MLKEM as MLKEM
import qualified Crypto.PubKey.RSA.Types as RSA
import Data.Word
import qualified Data.ByteString as B
newtype SerializedPoint = SerializedPoint ByteString
deriving (Int -> SerializedPoint -> ShowS
[SerializedPoint] -> ShowS
SerializedPoint -> String
(Int -> SerializedPoint -> ShowS)
-> (SerializedPoint -> String)
-> ([SerializedPoint] -> ShowS)
-> Show SerializedPoint
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> SerializedPoint -> ShowS
showsPrec :: Int -> SerializedPoint -> ShowS
$cshow :: SerializedPoint -> String
show :: SerializedPoint -> String
$cshowList :: [SerializedPoint] -> ShowS
showList :: [SerializedPoint] -> ShowS
Show, SerializedPoint -> SerializedPoint -> Bool
(SerializedPoint -> SerializedPoint -> Bool)
-> (SerializedPoint -> SerializedPoint -> Bool)
-> Eq SerializedPoint
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: SerializedPoint -> SerializedPoint -> Bool
== :: SerializedPoint -> SerializedPoint -> Bool
$c/= :: SerializedPoint -> SerializedPoint -> Bool
/= :: SerializedPoint -> SerializedPoint -> Bool
Eq)
data PubKeyEC
= PubKeyEC_Prime
{ PubKeyEC -> SerializedPoint
pubkeyEC_pub :: SerializedPoint
, PubKeyEC -> Integer
pubkeyEC_a :: Integer
, PubKeyEC -> Integer
pubkeyEC_b :: Integer
, PubKeyEC -> Integer
pubkeyEC_prime :: Integer
, PubKeyEC -> SerializedPoint
pubkeyEC_generator :: SerializedPoint
, PubKeyEC -> Integer
pubkeyEC_order :: Integer
, PubKeyEC -> Integer
pubkeyEC_cofactor :: Integer
, PubKeyEC -> Integer
pubkeyEC_seed :: Integer
}
| PubKeyEC_Named
{ PubKeyEC -> CurveName
pubkeyEC_name :: ECC.CurveName
, pubkeyEC_pub :: SerializedPoint
}
deriving (Int -> PubKeyEC -> ShowS
[PubKeyEC] -> ShowS
PubKeyEC -> String
(Int -> PubKeyEC -> ShowS)
-> (PubKeyEC -> String) -> ([PubKeyEC] -> ShowS) -> Show PubKeyEC
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PubKeyEC -> ShowS
showsPrec :: Int -> PubKeyEC -> ShowS
$cshow :: PubKeyEC -> String
show :: PubKeyEC -> String
$cshowList :: [PubKeyEC] -> ShowS
showList :: [PubKeyEC] -> ShowS
Show, PubKeyEC -> PubKeyEC -> Bool
(PubKeyEC -> PubKeyEC -> Bool)
-> (PubKeyEC -> PubKeyEC -> Bool) -> Eq PubKeyEC
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PubKeyEC -> PubKeyEC -> Bool
== :: PubKeyEC -> PubKeyEC -> Bool
$c/= :: PubKeyEC -> PubKeyEC -> Bool
/= :: PubKeyEC -> PubKeyEC -> Bool
Eq)
data PubKeyDH = PubKeyDH_
{ PubKeyDH -> Integer
pubkeyDH_p :: Integer
, PubKeyDH -> Integer
pubkeyDH_g :: Integer
, PubKeyDH -> Integer
pubkeyDH_q :: Integer
, PubKeyDH -> Maybe Integer
pubkeyDH_j :: Maybe Integer
, PubKeyDH -> Maybe (BitArray, Integer)
pubkeyDH_validation :: Maybe (BitArray, Integer)
, PubKeyDH -> Integer
pubkeyDH_y :: Integer
}
deriving (Int -> PubKeyDH -> ShowS
[PubKeyDH] -> ShowS
PubKeyDH -> String
(Int -> PubKeyDH -> ShowS)
-> (PubKeyDH -> String) -> ([PubKeyDH] -> ShowS) -> Show PubKeyDH
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PubKeyDH -> ShowS
showsPrec :: Int -> PubKeyDH -> ShowS
$cshow :: PubKeyDH -> String
show :: PubKeyDH -> String
$cshowList :: [PubKeyDH] -> ShowS
showList :: [PubKeyDH] -> ShowS
Show, PubKeyDH -> PubKeyDH -> Bool
(PubKeyDH -> PubKeyDH -> Bool)
-> (PubKeyDH -> PubKeyDH -> Bool) -> Eq PubKeyDH
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PubKeyDH -> PubKeyDH -> Bool
== :: PubKeyDH -> PubKeyDH -> Bool
$c/= :: PubKeyDH -> PubKeyDH -> Bool
/= :: PubKeyDH -> PubKeyDH -> Bool
Eq)
dhPublicKey :: Integer -> Integer -> Integer -> Integer -> PubKeyDH
dhPublicKey :: Integer -> Integer -> Integer -> Integer -> PubKeyDH
dhPublicKey Integer
p Integer
g Integer
q Integer
y = Integer
-> Integer
-> Integer
-> Maybe Integer
-> Maybe (BitArray, Integer)
-> Integer
-> PubKeyDH
PubKeyDH_ Integer
p Integer
g Integer
q Maybe Integer
forall a. Maybe a
Nothing Maybe (BitArray, Integer)
forall a. Maybe a
Nothing Integer
y
data PubKey
=
PubKeyRSA RSA.PublicKey
|
PubKeyRSAPSS RSA.PublicKey (Maybe PSSParams)
|
PubKeyDSA DSA.PublicKey
|
PubKeyDH PubKeyDH
|
PubKeyEC PubKeyEC
|
PubKeyX25519 X25519.PublicKey
|
PubKeyX448 X448.PublicKey
|
PubKeyEd25519 Ed25519.PublicKey
|
PubKeyEd448 Ed448.PublicKey
|
PubKeyMLDSA44 (MLDSA.VerificationKey MLDSA.MLDSA44)
|
PubKeyMLDSA65 (MLDSA.VerificationKey MLDSA.MLDSA65)
|
PubKeyMLDSA87 (MLDSA.VerificationKey MLDSA.MLDSA87)
|
PubKeyMLKEM512 (MLKEM.EncapsulationKey MLKEM.MLKEM512)
|
PubKeyMLKEM768 (MLKEM.EncapsulationKey MLKEM.MLKEM768)
|
PubKeyMLKEM1024 (MLKEM.EncapsulationKey MLKEM.MLKEM1024)
|
PubKeyUnknown OID B.ByteString
deriving (Int -> PubKey -> ShowS
[PubKey] -> ShowS
PubKey -> String
(Int -> PubKey -> ShowS)
-> (PubKey -> String) -> ([PubKey] -> ShowS) -> Show PubKey
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PubKey -> ShowS
showsPrec :: Int -> PubKey -> ShowS
$cshow :: PubKey -> String
show :: PubKey -> String
$cshowList :: [PubKey] -> ShowS
showList :: [PubKey] -> ShowS
Show, PubKey -> PubKey -> Bool
(PubKey -> PubKey -> Bool)
-> (PubKey -> PubKey -> Bool) -> Eq PubKey
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PubKey -> PubKey -> Bool
== :: PubKey -> PubKey -> Bool
$c/= :: PubKey -> PubKey -> Bool
/= :: PubKey -> PubKey -> Bool
Eq)
instance ASN1Object PubKey where
fromASN1 :: [ASN1] -> Either String (PubKey, [ASN1])
fromASN1 (Start ASN1ConstructionType
Sequence : Start ASN1ConstructionType
Sequence : OID OID
pkalg : [ASN1]
xs)
| OID
pkalg OID -> OID -> Bool
forall a. Eq a => a -> a -> Bool
== PubKeyALG -> OID
forall a. OIDable a => a -> OID
getObjectID PubKeyALG
PubKeyALG_RSA =
case ASN1S
removeNull [ASN1]
xs of
End ASN1ConstructionType
Sequence : BitString BitArray
bits : End ASN1ConstructionType
Sequence : [ASN1]
xs2 -> String
-> BitArray
-> [ASN1]
-> ([ASN1] -> Either String (PubKey, [ASN1]))
-> Either String (PubKey, [ASN1])
forall {b} {a} {a}.
String
-> BitArray
-> b
-> ([ASN1] -> Either String (a, [a]))
-> Either String (a, b)
decodeASN1Err String
"RSA" BitArray
bits [ASN1]
xs2 (Either String (PublicKey, [ASN1]) -> Either String (PubKey, [ASN1])
forall {a} {b}. Either a (PublicKey, b) -> Either a (PubKey, b)
toPubKeyRSA (Either String (PublicKey, [ASN1])
-> Either String (PubKey, [ASN1]))
-> ([ASN1] -> Either String (PublicKey, [ASN1]))
-> [ASN1]
-> Either String (PubKey, [ASN1])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [ASN1] -> Either String (PublicKey, [ASN1])
rsaPubFromASN1)
[ASN1]
_ -> String -> Either String (PubKey, [ASN1])
forall a b. a -> Either a b
Left (String
"fromASN1: X509.PubKey: unknown RSA format: " String -> ShowS
forall a. [a] -> [a] -> [a]
++ [ASN1] -> String
forall a. Show a => a -> String
show [ASN1]
xs)
| OID
pkalg OID -> OID -> Bool
forall a. Eq a => a -> a -> Bool
== PubKeyALG -> OID
forall a. OIDable a => a -> OID
getObjectID PubKeyALG
PubKeyALG_RSAPSS = do
(Maybe PSSParams
mp, [ASN1]
ys) <- [ASN1] -> Either String (Maybe PSSParams, [ASN1])
pssKeyParamsFromASN1 [ASN1]
xs
case [ASN1]
ys of
BitString BitArray
bits : End ASN1ConstructionType
Sequence : [ASN1]
xs2 ->
String
-> BitArray
-> [ASN1]
-> ([ASN1] -> Either String (PubKey, [ASN1]))
-> Either String (PubKey, [ASN1])
forall {b} {a} {a}.
String
-> BitArray
-> b
-> ([ASN1] -> Either String (a, [a]))
-> Either String (a, b)
decodeASN1Err String
"RSA-PSS" BitArray
bits [ASN1]
xs2 (Maybe PSSParams
-> Either String (PublicKey, [ASN1])
-> Either String (PubKey, [ASN1])
forall {a} {b}.
Maybe PSSParams -> Either a (PublicKey, b) -> Either a (PubKey, b)
toPubKeyRSAPSS Maybe PSSParams
mp (Either String (PublicKey, [ASN1])
-> Either String (PubKey, [ASN1]))
-> ([ASN1] -> Either String (PublicKey, [ASN1]))
-> [ASN1]
-> Either String (PubKey, [ASN1])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [ASN1] -> Either String (PublicKey, [ASN1])
rsaPubFromASN1)
[ASN1]
_ -> String -> Either String (PubKey, [ASN1])
forall a b. a -> Either a b
Left (String
"fromASN1: X509.PubKey: unknown RSA-PSS format: " String -> ShowS
forall a. [a] -> [a] -> [a]
++ [ASN1] -> String
forall a. Show a => a -> String
show [ASN1]
xs)
| OID
pkalg OID -> OID -> Bool
forall a. Eq a => a -> a -> Bool
== PubKeyALG -> OID
forall a. OIDable a => a -> OID
getObjectID PubKeyALG
PubKeyALG_DSA =
case [ASN1]
xs of
Start ASN1ConstructionType
Sequence : IntVal Integer
p : IntVal Integer
q : IntVal Integer
g : End ASN1ConstructionType
Sequence : End ASN1ConstructionType
Sequence
: BitString BitArray
bits
: End ASN1ConstructionType
Sequence
: [ASN1]
xs2 ->
String
-> BitArray
-> [ASN1]
-> ([ASN1] -> Either String (PubKey, [Any]))
-> Either String (PubKey, [ASN1])
forall {b} {a} {a}.
String
-> BitArray
-> b
-> ([ASN1] -> Either String (a, [a]))
-> Either String (a, b)
decodeASN1Err
String
"DSA"
BitArray
bits
[ASN1]
xs2
( \[ASN1]
l -> case [ASN1]
l of
[IntVal Integer
dsapub] ->
let pubkey :: PublicKey
pubkey =
DSA.PublicKey
{ public_params :: Params
DSA.public_params =
DSA.Params
{ params_p :: Integer
DSA.params_p = Integer
p
, params_q :: Integer
DSA.params_q = Integer
q
, params_g :: Integer
DSA.params_g = Integer
g
}
, public_y :: Integer
DSA.public_y = Integer
dsapub
}
in (PubKey, [Any]) -> Either String (PubKey, [Any])
forall a b. b -> Either a b
Right (PublicKey -> PubKey
PubKeyDSA PublicKey
pubkey, [])
[ASN1]
_ -> String -> Either String (PubKey, [Any])
forall a b. a -> Either a b
Left String
"fromASN1: X509.PubKey: unknown DSA format"
)
[ASN1]
_ -> String -> Either String (PubKey, [ASN1])
forall a b. a -> Either a b
Left String
"fromASN1: X509.PubKey: unknown DSA format"
| OID
pkalg OID -> OID -> Bool
forall a. Eq a => a -> a -> Bool
== PubKeyALG -> OID
forall a. OIDable a => a -> OID
getObjectID PubKeyALG
PubKeyALG_DH =
case [ASN1]
xs of
Start ASN1ConstructionType
Sequence : IntVal Integer
p : IntVal Integer
g : IntVal Integer
q : [ASN1]
rest ->
let (Maybe Integer
j, [ASN1]
rest1) = case [ASN1]
rest of
IntVal Integer
j' : [ASN1]
r -> (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
j', [ASN1]
r)
[ASN1]
r -> (Maybe Integer
forall a. Maybe a
Nothing, [ASN1]
r)
(Maybe (BitArray, Integer)
v, [ASN1]
rest2) = case [ASN1]
rest1 of
Start ASN1ConstructionType
Sequence : BitString BitArray
seed : IntVal Integer
c : End ASN1ConstructionType
Sequence : [ASN1]
r ->
((BitArray, Integer) -> Maybe (BitArray, Integer)
forall a. a -> Maybe a
Just (BitArray
seed, Integer
c), [ASN1]
r)
[ASN1]
r -> (Maybe (BitArray, Integer)
forall a. Maybe a
Nothing, [ASN1]
r)
in case [ASN1]
rest2 of
End ASN1ConstructionType
Sequence : End ASN1ConstructionType
Sequence : BitString BitArray
bits : End ASN1ConstructionType
Sequence : [ASN1]
xs2 ->
String
-> BitArray
-> [ASN1]
-> ([ASN1] -> Either String (PubKey, [Any]))
-> Either String (PubKey, [ASN1])
forall {b} {a} {a}.
String
-> BitArray
-> b
-> ([ASN1] -> Either String (a, [a]))
-> Either String (a, b)
decodeASN1Err String
"DH" BitArray
bits [ASN1]
xs2 (([ASN1] -> Either String (PubKey, [Any]))
-> Either String (PubKey, [ASN1]))
-> ([ASN1] -> Either String (PubKey, [Any]))
-> Either String (PubKey, [ASN1])
forall a b. (a -> b) -> a -> b
$ \[ASN1]
l -> case [ASN1]
l of
[IntVal Integer
y] -> (PubKey, [Any]) -> Either String (PubKey, [Any])
forall a b. b -> Either a b
Right (PubKeyDH -> PubKey
PubKeyDH (Integer
-> Integer
-> Integer
-> Maybe Integer
-> Maybe (BitArray, Integer)
-> Integer
-> PubKeyDH
PubKeyDH_ Integer
p Integer
g Integer
q Maybe Integer
j Maybe (BitArray, Integer)
v Integer
y), [])
[ASN1]
_ -> String -> Either String (PubKey, [Any])
forall a b. a -> Either a b
Left String
"fromASN1: X509.PubKey: unknown DH format"
[ASN1]
_ -> String -> Either String (PubKey, [ASN1])
forall a b. a -> Either a b
Left String
"fromASN1: X509.PubKey: unknown DH format"
[ASN1]
_ -> String -> Either String (PubKey, [ASN1])
forall a b. a -> Either a b
Left String
"fromASN1: X509.PubKey: unknown DH format"
| OID
pkalg OID -> OID -> Bool
forall a. Eq a => a -> a -> Bool
== PubKeyALG -> OID
forall a. OIDable a => a -> OID
getObjectID PubKeyALG
PubKeyALG_EC =
case [ASN1]
xs of
OID OID
curveOid : End ASN1ConstructionType
Sequence : BitString BitArray
bits : End ASN1ConstructionType
Sequence : [ASN1]
xs2 ->
case OIDTable CurveName -> OID -> Maybe CurveName
forall a. OIDTable a -> OID -> Maybe a
lookupByOID OIDTable CurveName
curvesOIDTable OID
curveOid of
Just CurveName
curveName -> (PubKey, [ASN1]) -> Either String (PubKey, [ASN1])
forall a b. b -> Either a b
Right (PubKeyEC -> PubKey
PubKeyEC (PubKeyEC -> PubKey) -> PubKeyEC -> PubKey
forall a b. (a -> b) -> a -> b
$ CurveName -> SerializedPoint -> PubKeyEC
PubKeyEC_Named CurveName
curveName (BitArray -> SerializedPoint
bitArrayToPoint BitArray
bits), [ASN1]
xs2)
Maybe CurveName
Nothing -> String -> Either String (PubKey, [ASN1])
forall a b. a -> Either a b
Left (String
"fromASN1: X509.Pubkey: EC unknown curve " String -> ShowS
forall a. [a] -> [a] -> [a]
++ OID -> String
forall a. Show a => a -> String
show OID
curveOid)
Start ASN1ConstructionType
Sequence
: IntVal Integer
1
: Start ASN1ConstructionType
Sequence
: OID [Integer
1, Integer
2, Integer
840, Integer
10045, Integer
1, Integer
1]
: IntVal Integer
prime
: End ASN1ConstructionType
Sequence
: Start ASN1ConstructionType
Sequence
: OctetString ByteString
a
: OctetString ByteString
b
: BitString BitArray
seed
: End ASN1ConstructionType
Sequence
: OctetString ByteString
generator
: IntVal Integer
order
: IntVal Integer
cofactor
: End ASN1ConstructionType
Sequence
: End ASN1ConstructionType
Sequence
: BitString BitArray
pub
: End ASN1ConstructionType
Sequence
: [ASN1]
xs2 ->
(PubKey, [ASN1]) -> Either String (PubKey, [ASN1])
forall a b. b -> Either a b
Right
( PubKeyEC -> PubKey
PubKeyEC (PubKeyEC -> PubKey) -> PubKeyEC -> PubKey
forall a b. (a -> b) -> a -> b
$
PubKeyEC_Prime
{ pubkeyEC_pub :: SerializedPoint
pubkeyEC_pub = BitArray -> SerializedPoint
bitArrayToPoint BitArray
pub
, pubkeyEC_a :: Integer
pubkeyEC_a = ByteString -> Integer
forall ba. ByteArrayAccess ba => ba -> Integer
os2ip ByteString
a
, pubkeyEC_b :: Integer
pubkeyEC_b = ByteString -> Integer
forall ba. ByteArrayAccess ba => ba -> Integer
os2ip ByteString
b
, pubkeyEC_prime :: Integer
pubkeyEC_prime = Integer
prime
, pubkeyEC_generator :: SerializedPoint
pubkeyEC_generator = ByteString -> SerializedPoint
SerializedPoint ByteString
generator
, pubkeyEC_order :: Integer
pubkeyEC_order = Integer
order
, pubkeyEC_cofactor :: Integer
pubkeyEC_cofactor = Integer
cofactor
, pubkeyEC_seed :: Integer
pubkeyEC_seed = ByteString -> Integer
forall ba. ByteArrayAccess ba => ba -> Integer
os2ip (ByteString -> Integer) -> ByteString -> Integer
forall a b. (a -> b) -> a -> b
$ BitArray -> ByteString
bitArrayGetData BitArray
seed
}
, [ASN1]
xs2
)
[ASN1]
_ ->
String -> Either String (PubKey, [ASN1])
forall a b. a -> Either a b
Left (String -> Either String (PubKey, [ASN1]))
-> String -> Either String (PubKey, [ASN1])
forall a b. (a -> b) -> a -> b
$ String
"fromASN1: X509.PubKey: unknown EC format: " String -> ShowS
forall a. [a] -> [a] -> [a]
++ [ASN1] -> String
forall a. Show a => a -> String
show [ASN1]
xs
| OID
pkalg OID -> OID -> Bool
forall a. Eq a => a -> a -> Bool
== PubKeyALG -> OID
forall a. OIDable a => a -> OID
getObjectID PubKeyALG
PubKeyALG_X25519 =
case [ASN1]
xs of
End ASN1ConstructionType
Sequence : BitString BitArray
bits : End ASN1ConstructionType
Sequence : [ASN1]
xs2 -> String
-> (PublicKey -> PubKey)
-> BitArray
-> [ASN1]
-> (ByteString -> CryptoFailable PublicKey)
-> Either String (PubKey, [ASN1])
forall {t} {a} {b}.
String
-> (t -> a)
-> BitArray
-> b
-> (ByteString -> CryptoFailable t)
-> Either String (a, b)
decodeCF String
"X25519" PublicKey -> PubKey
PubKeyX25519 BitArray
bits [ASN1]
xs2 ByteString -> CryptoFailable PublicKey
forall bs. ByteArrayAccess bs => bs -> CryptoFailable PublicKey
X25519.publicKey
[ASN1]
_ -> String -> Either String (PubKey, [ASN1])
forall a b. a -> Either a b
Left (String
"fromASN1: X509.PubKey: unknown X25519 format: " String -> ShowS
forall a. [a] -> [a] -> [a]
++ [ASN1] -> String
forall a. Show a => a -> String
show [ASN1]
xs)
| OID
pkalg OID -> OID -> Bool
forall a. Eq a => a -> a -> Bool
== PubKeyALG -> OID
forall a. OIDable a => a -> OID
getObjectID PubKeyALG
PubKeyALG_X448 =
case [ASN1]
xs of
End ASN1ConstructionType
Sequence : BitString BitArray
bits : End ASN1ConstructionType
Sequence : [ASN1]
xs2 -> String
-> (PublicKey -> PubKey)
-> BitArray
-> [ASN1]
-> (ByteString -> CryptoFailable PublicKey)
-> Either String (PubKey, [ASN1])
forall {t} {a} {b}.
String
-> (t -> a)
-> BitArray
-> b
-> (ByteString -> CryptoFailable t)
-> Either String (a, b)
decodeCF String
"X448" PublicKey -> PubKey
PubKeyX448 BitArray
bits [ASN1]
xs2 ByteString -> CryptoFailable PublicKey
forall bs. ByteArrayAccess bs => bs -> CryptoFailable PublicKey
X448.publicKey
[ASN1]
_ -> String -> Either String (PubKey, [ASN1])
forall a b. a -> Either a b
Left (String
"fromASN1: X509.PubKey: unknown X448 format: " String -> ShowS
forall a. [a] -> [a] -> [a]
++ [ASN1] -> String
forall a. Show a => a -> String
show [ASN1]
xs)
| OID
pkalg OID -> OID -> Bool
forall a. Eq a => a -> a -> Bool
== PubKeyALG -> OID
forall a. OIDable a => a -> OID
getObjectID PubKeyALG
PubKeyALG_Ed25519 =
case [ASN1]
xs of
End ASN1ConstructionType
Sequence : BitString BitArray
bits : End ASN1ConstructionType
Sequence : [ASN1]
xs2 -> String
-> (PublicKey -> PubKey)
-> BitArray
-> [ASN1]
-> (ByteString -> CryptoFailable PublicKey)
-> Either String (PubKey, [ASN1])
forall {t} {a} {b}.
String
-> (t -> a)
-> BitArray
-> b
-> (ByteString -> CryptoFailable t)
-> Either String (a, b)
decodeCF String
"Ed25519" PublicKey -> PubKey
PubKeyEd25519 BitArray
bits [ASN1]
xs2 ByteString -> CryptoFailable PublicKey
forall ba. ByteArrayAccess ba => ba -> CryptoFailable PublicKey
Ed25519.publicKey
[ASN1]
_ -> String -> Either String (PubKey, [ASN1])
forall a b. a -> Either a b
Left (String
"fromASN1: X509.PubKey: unknown Ed25519 format: " String -> ShowS
forall a. [a] -> [a] -> [a]
++ [ASN1] -> String
forall a. Show a => a -> String
show [ASN1]
xs)
| OID
pkalg OID -> OID -> Bool
forall a. Eq a => a -> a -> Bool
== PubKeyALG -> OID
forall a. OIDable a => a -> OID
getObjectID PubKeyALG
PubKeyALG_Ed448 =
case [ASN1]
xs of
End ASN1ConstructionType
Sequence : BitString BitArray
bits : End ASN1ConstructionType
Sequence : [ASN1]
xs2 -> String
-> (PublicKey -> PubKey)
-> BitArray
-> [ASN1]
-> (ByteString -> CryptoFailable PublicKey)
-> Either String (PubKey, [ASN1])
forall {t} {a} {b}.
String
-> (t -> a)
-> BitArray
-> b
-> (ByteString -> CryptoFailable t)
-> Either String (a, b)
decodeCF String
"Ed448" PublicKey -> PubKey
PubKeyEd448 BitArray
bits [ASN1]
xs2 ByteString -> CryptoFailable PublicKey
forall ba. ByteArrayAccess ba => ba -> CryptoFailable PublicKey
Ed448.publicKey
[ASN1]
_ -> String -> Either String (PubKey, [ASN1])
forall a b. a -> Either a b
Left (String
"fromASN1: X509.PubKey: unknown Ed448 format: " String -> ShowS
forall a. [a] -> [a] -> [a]
++ [ASN1] -> String
forall a. Show a => a -> String
show [ASN1]
xs)
| OID
pkalg OID -> OID -> Bool
forall a. Eq a => a -> a -> Bool
== PubKeyALG -> OID
forall a. OIDable a => a -> OID
getObjectID PubKeyALG
PubKeyALG_MLDSA44 =
case [ASN1]
xs of
End ASN1ConstructionType
Sequence : BitString BitArray
bits : End ASN1ConstructionType
Sequence : [ASN1]
xs2 -> String
-> (VerificationKey MLDSA44 -> PubKey)
-> BitArray
-> [ASN1]
-> (ByteString -> CryptoFailable (VerificationKey MLDSA44))
-> Either String (PubKey, [ASN1])
forall {t} {a} {b}.
String
-> (t -> a)
-> BitArray
-> b
-> (ByteString -> CryptoFailable t)
-> Either String (a, b)
decodeCF String
"ML-DSA-44" VerificationKey MLDSA44 -> PubKey
PubKeyMLDSA44 BitArray
bits [ASN1]
xs2 ByteString -> CryptoFailable (VerificationKey MLDSA44)
forall p ba.
(MLDSA p, ByteArrayAccess ba) =>
ba -> CryptoFailable (VerificationKey p)
MLDSA.verificationKey
[ASN1]
_ -> String -> Either String (PubKey, [ASN1])
forall a b. a -> Either a b
Left (String
"fromASN1: X509.PubKey: unknown ML-DSA-44 format: " String -> ShowS
forall a. [a] -> [a] -> [a]
++ [ASN1] -> String
forall a. Show a => a -> String
show [ASN1]
xs)
| OID
pkalg OID -> OID -> Bool
forall a. Eq a => a -> a -> Bool
== PubKeyALG -> OID
forall a. OIDable a => a -> OID
getObjectID PubKeyALG
PubKeyALG_MLDSA65 =
case [ASN1]
xs of
End ASN1ConstructionType
Sequence : BitString BitArray
bits : End ASN1ConstructionType
Sequence : [ASN1]
xs2 -> String
-> (VerificationKey MLDSA65 -> PubKey)
-> BitArray
-> [ASN1]
-> (ByteString -> CryptoFailable (VerificationKey MLDSA65))
-> Either String (PubKey, [ASN1])
forall {t} {a} {b}.
String
-> (t -> a)
-> BitArray
-> b
-> (ByteString -> CryptoFailable t)
-> Either String (a, b)
decodeCF String
"ML-DSA-65" VerificationKey MLDSA65 -> PubKey
PubKeyMLDSA65 BitArray
bits [ASN1]
xs2 ByteString -> CryptoFailable (VerificationKey MLDSA65)
forall p ba.
(MLDSA p, ByteArrayAccess ba) =>
ba -> CryptoFailable (VerificationKey p)
MLDSA.verificationKey
[ASN1]
_ -> String -> Either String (PubKey, [ASN1])
forall a b. a -> Either a b
Left (String
"fromASN1: X509.PubKey: unknown ML-DSA-65 format: " String -> ShowS
forall a. [a] -> [a] -> [a]
++ [ASN1] -> String
forall a. Show a => a -> String
show [ASN1]
xs)
| OID
pkalg OID -> OID -> Bool
forall a. Eq a => a -> a -> Bool
== PubKeyALG -> OID
forall a. OIDable a => a -> OID
getObjectID PubKeyALG
PubKeyALG_MLDSA87 =
case [ASN1]
xs of
End ASN1ConstructionType
Sequence : BitString BitArray
bits : End ASN1ConstructionType
Sequence : [ASN1]
xs2 -> String
-> (VerificationKey MLDSA87 -> PubKey)
-> BitArray
-> [ASN1]
-> (ByteString -> CryptoFailable (VerificationKey MLDSA87))
-> Either String (PubKey, [ASN1])
forall {t} {a} {b}.
String
-> (t -> a)
-> BitArray
-> b
-> (ByteString -> CryptoFailable t)
-> Either String (a, b)
decodeCF String
"ML-DSA-87" VerificationKey MLDSA87 -> PubKey
PubKeyMLDSA87 BitArray
bits [ASN1]
xs2 ByteString -> CryptoFailable (VerificationKey MLDSA87)
forall p ba.
(MLDSA p, ByteArrayAccess ba) =>
ba -> CryptoFailable (VerificationKey p)
MLDSA.verificationKey
[ASN1]
_ -> String -> Either String (PubKey, [ASN1])
forall a b. a -> Either a b
Left (String
"fromASN1: X509.PubKey: unknown ML-DSA-87 format: " String -> ShowS
forall a. [a] -> [a] -> [a]
++ [ASN1] -> String
forall a. Show a => a -> String
show [ASN1]
xs)
| OID
pkalg OID -> OID -> Bool
forall a. Eq a => a -> a -> Bool
== PubKeyALG -> OID
forall a. OIDable a => a -> OID
getObjectID PubKeyALG
PubKeyALG_MLKEM512 =
case [ASN1]
xs of
End ASN1ConstructionType
Sequence : BitString BitArray
bits : End ASN1ConstructionType
Sequence : [ASN1]
xs2 ->
String
-> (MLKEMEncapsulationKey MLKEM512 -> PubKey)
-> BitArray
-> [ASN1]
-> (ByteString -> CryptoFailable (MLKEMEncapsulationKey MLKEM512))
-> Either String (PubKey, [ASN1])
forall {t} {a} {b}.
String
-> (t -> a)
-> BitArray
-> b
-> (ByteString -> CryptoFailable t)
-> Either String (a, b)
decodeCF String
"ML-KEM-512" EncapsulationKey MLKEM512 -> PubKey
MLKEMEncapsulationKey MLKEM512 -> PubKey
PubKeyMLKEM512 BitArray
bits [ASN1]
xs2 (forall p ba.
(MLKEM p, ByteArrayAccess ba) =>
ba -> CryptoFailable (EncapsulationKey p)
MLKEM.encapsulationKey @MLKEM.MLKEM512)
[ASN1]
_ -> String -> Either String (PubKey, [ASN1])
forall a b. a -> Either a b
Left (String
"fromASN1: X509.PubKey: unknown ML-KEM-512 format: " String -> ShowS
forall a. [a] -> [a] -> [a]
++ [ASN1] -> String
forall a. Show a => a -> String
show [ASN1]
xs)
| OID
pkalg OID -> OID -> Bool
forall a. Eq a => a -> a -> Bool
== PubKeyALG -> OID
forall a. OIDable a => a -> OID
getObjectID PubKeyALG
PubKeyALG_MLKEM768 =
case [ASN1]
xs of
End ASN1ConstructionType
Sequence : BitString BitArray
bits : End ASN1ConstructionType
Sequence : [ASN1]
xs2 ->
String
-> (MLKEMEncapsulationKey MLKEM768 -> PubKey)
-> BitArray
-> [ASN1]
-> (ByteString -> CryptoFailable (MLKEMEncapsulationKey MLKEM768))
-> Either String (PubKey, [ASN1])
forall {t} {a} {b}.
String
-> (t -> a)
-> BitArray
-> b
-> (ByteString -> CryptoFailable t)
-> Either String (a, b)
decodeCF String
"ML-KEM-768" EncapsulationKey MLKEM768 -> PubKey
MLKEMEncapsulationKey MLKEM768 -> PubKey
PubKeyMLKEM768 BitArray
bits [ASN1]
xs2 (forall p ba.
(MLKEM p, ByteArrayAccess ba) =>
ba -> CryptoFailable (EncapsulationKey p)
MLKEM.encapsulationKey @MLKEM.MLKEM768)
[ASN1]
_ -> String -> Either String (PubKey, [ASN1])
forall a b. a -> Either a b
Left (String
"fromASN1: X509.PubKey: unknown ML-KEM-768 format: " String -> ShowS
forall a. [a] -> [a] -> [a]
++ [ASN1] -> String
forall a. Show a => a -> String
show [ASN1]
xs)
| OID
pkalg OID -> OID -> Bool
forall a. Eq a => a -> a -> Bool
== PubKeyALG -> OID
forall a. OIDable a => a -> OID
getObjectID PubKeyALG
PubKeyALG_MLKEM1024 =
case [ASN1]
xs of
End ASN1ConstructionType
Sequence : BitString BitArray
bits : End ASN1ConstructionType
Sequence : [ASN1]
xs2 ->
String
-> (MLKEMEncapsulationKey MLKEM1024 -> PubKey)
-> BitArray
-> [ASN1]
-> (ByteString -> CryptoFailable (MLKEMEncapsulationKey MLKEM1024))
-> Either String (PubKey, [ASN1])
forall {t} {a} {b}.
String
-> (t -> a)
-> BitArray
-> b
-> (ByteString -> CryptoFailable t)
-> Either String (a, b)
decodeCF String
"ML-KEM-1024" EncapsulationKey MLKEM1024 -> PubKey
MLKEMEncapsulationKey MLKEM1024 -> PubKey
PubKeyMLKEM1024 BitArray
bits [ASN1]
xs2 (forall p ba.
(MLKEM p, ByteArrayAccess ba) =>
ba -> CryptoFailable (EncapsulationKey p)
MLKEM.encapsulationKey @MLKEM.MLKEM1024)
[ASN1]
_ -> String -> Either String (PubKey, [ASN1])
forall a b. a -> Either a b
Left (String
"fromASN1: X509.PubKey: unknown ML-KEM-1024 format: " String -> ShowS
forall a. [a] -> [a] -> [a]
++ [ASN1] -> String
forall a. Show a => a -> String
show [ASN1]
xs)
| Bool
otherwise = case Int -> ASN1S
forall {t}. (Eq t, Num t) => t -> ASN1S
dropParams (Int
0 :: Int) [ASN1]
xs of
BitString BitArray
bits : End ASN1ConstructionType
Sequence : [ASN1]
xs2 ->
(PubKey, [ASN1]) -> Either String (PubKey, [ASN1])
forall a b. b -> Either a b
Right (OID -> ByteString -> PubKey
PubKeyUnknown OID
pkalg (BitArray -> ByteString
bitArrayGetData BitArray
bits), [ASN1]
xs2)
[ASN1]
_ -> String -> Either String (PubKey, [ASN1])
forall a b. a -> Either a b
Left (String
"fromASN1: X509.PubKey: unknown format for " String -> ShowS
forall a. [a] -> [a] -> [a]
++ OID -> String
forall a. Show a => a -> String
show OID
pkalg)
where
dropParams :: t -> ASN1S
dropParams t
0 (End ASN1ConstructionType
Sequence : [ASN1]
r) = [ASN1]
r
dropParams t
n (Start ASN1ConstructionType
_ : [ASN1]
r) = t -> ASN1S
dropParams (t
n t -> t -> t
forall a. Num a => a -> a -> a
+ t
1) [ASN1]
r
dropParams t
n (End ASN1ConstructionType
_ : [ASN1]
r) = t -> ASN1S
dropParams (t
n t -> t -> t
forall a. Num a => a -> a -> a
- t
1) [ASN1]
r
dropParams t
n (ASN1
_ : [ASN1]
r) = t -> ASN1S
dropParams t
n [ASN1]
r
dropParams t
_ [] = []
decodeASN1Err :: String
-> BitArray
-> b
-> ([ASN1] -> Either String (a, [a]))
-> Either String (a, b)
decodeASN1Err String
format BitArray
bits b
xs2 [ASN1] -> Either String (a, [a])
f =
case Either ASN1Error [ASN1] -> Either String [ASN1]
forall e a. Show e => Either e [a] -> Either String [a]
safeDecode (BER -> ByteString -> Either ASN1Error [ASN1]
forall a.
ASN1Decoding a =>
a -> ByteString -> Either ASN1Error [ASN1]
decodeASN1' BER
BER (BitArray -> ByteString
bitArrayGetData BitArray
bits)) of
Left String
err ->
String -> Either String (a, b)
forall a b. a -> Either a b
Left
(String
"fromASN1: X509.PubKey " String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
format String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" bitarray cannot be parsed: " String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
err)
Right [ASN1]
s -> case [ASN1] -> Either String (a, [a])
f [ASN1]
s of
Left String
err -> String -> Either String (a, b)
forall a b. a -> Either a b
Left String
err
Right (a
r, []) -> (a, b) -> Either String (a, b)
forall a b. b -> Either a b
Right (a
r, b
xs2)
Right (a, [a])
_ ->
String -> Either String (a, b)
forall a b. a -> Either a b
Left (String
"fromASN1: X509.PubKey " String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
format String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" bitarray has trailing data")
toPubKeyRSA :: Either a (PublicKey, b) -> Either a (PubKey, b)
toPubKeyRSA = (a -> Either a (PubKey, b))
-> ((PublicKey, b) -> Either a (PubKey, b))
-> Either a (PublicKey, b)
-> Either a (PubKey, b)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either a -> Either a (PubKey, b)
forall a b. a -> Either a b
Left (\(PublicKey
rsaKey, b
r) -> (PubKey, b) -> Either a (PubKey, b)
forall a b. b -> Either a b
Right (PublicKey -> PubKey
PubKeyRSA PublicKey
rsaKey, b
r))
toPubKeyRSAPSS :: Maybe PSSParams -> Either a (PublicKey, b) -> Either a (PubKey, b)
toPubKeyRSAPSS Maybe PSSParams
mp = (a -> Either a (PubKey, b))
-> ((PublicKey, b) -> Either a (PubKey, b))
-> Either a (PublicKey, b)
-> Either a (PubKey, b)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either a -> Either a (PubKey, b)
forall a b. a -> Either a b
Left (\(PublicKey
rsaKey, b
r) -> (PubKey, b) -> Either a (PubKey, b)
forall a b. b -> Either a b
Right (PublicKey -> Maybe PSSParams -> PubKey
PubKeyRSAPSS PublicKey
rsaKey Maybe PSSParams
mp, b
r))
bitArrayToPoint :: BitArray -> SerializedPoint
bitArrayToPoint = ByteString -> SerializedPoint
SerializedPoint (ByteString -> SerializedPoint)
-> (BitArray -> ByteString) -> BitArray -> SerializedPoint
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BitArray -> ByteString
bitArrayGetData
removeNull :: ASN1S
removeNull (ASN1
Null : [ASN1]
r) = [ASN1]
r
removeNull [ASN1]
l = [ASN1]
l
decodeCF :: String
-> (t -> a)
-> BitArray
-> b
-> (ByteString -> CryptoFailable t)
-> Either String (a, b)
decodeCF String
format t -> a
c BitArray
bits b
xs2 ByteString -> CryptoFailable t
f = case ByteString -> CryptoFailable t
f (BitArray -> ByteString
bitArrayGetData BitArray
bits) of
CryptoPassed t
pk -> (a, b) -> Either String (a, b)
forall a b. b -> Either a b
Right (t -> a
c t
pk, b
xs2)
CryptoFailed CryptoError
err ->
String -> Either String (a, b)
forall a b. a -> Either a b
Left
( String
"fromASN1: X509.PubKey "
String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
format
String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" bitarray contains an invalid public key: "
String -> ShowS
forall a. [a] -> [a] -> [a]
++ CryptoError -> String
forall a. Show a => a -> String
show CryptoError
err
)
fromASN1 [ASN1]
l = String -> Either String (PubKey, [ASN1])
forall a b. a -> Either a b
Left (String
"fromASN1: X509.PubKey: unknown format:" String -> ShowS
forall a. [a] -> [a] -> [a]
++ [ASN1] -> String
forall a. Show a => a -> String
show [ASN1]
l)
toASN1 :: PubKey -> ASN1S
toASN1 PubKey
a = \[ASN1]
xs -> PubKey -> [ASN1]
encodePK PubKey
a [ASN1] -> ASN1S
forall a. [a] -> [a] -> [a]
++ [ASN1]
xs
pubkeyToAlg :: PubKey -> PubKeyALG
pubkeyToAlg :: PubKey -> PubKeyALG
pubkeyToAlg (PubKeyRSA PublicKey
_) = PubKeyALG
PubKeyALG_RSA
pubkeyToAlg (PubKeyRSAPSS PublicKey
_ Maybe PSSParams
_) = PubKeyALG
PubKeyALG_RSAPSS
pubkeyToAlg (PubKeyDSA PublicKey
_) = PubKeyALG
PubKeyALG_DSA
pubkeyToAlg (PubKeyDH PubKeyDH
_) = PubKeyALG
PubKeyALG_DH
pubkeyToAlg (PubKeyEC PubKeyEC
_) = PubKeyALG
PubKeyALG_EC
pubkeyToAlg (PubKeyX25519 PublicKey
_) = PubKeyALG
PubKeyALG_X25519
pubkeyToAlg (PubKeyX448 PublicKey
_) = PubKeyALG
PubKeyALG_X448
pubkeyToAlg (PubKeyEd25519 PublicKey
_) = PubKeyALG
PubKeyALG_Ed25519
pubkeyToAlg (PubKeyEd448 PublicKey
_) = PubKeyALG
PubKeyALG_Ed448
pubkeyToAlg (PubKeyMLDSA44 VerificationKey MLDSA44
_) = PubKeyALG
PubKeyALG_MLDSA44
pubkeyToAlg (PubKeyMLDSA65 VerificationKey MLDSA65
_) = PubKeyALG
PubKeyALG_MLDSA65
pubkeyToAlg (PubKeyMLDSA87 VerificationKey MLDSA87
_) = PubKeyALG
PubKeyALG_MLDSA87
pubkeyToAlg (PubKeyMLKEM512 EncapsulationKey MLKEM512
_) = PubKeyALG
PubKeyALG_MLKEM512
pubkeyToAlg (PubKeyMLKEM768 EncapsulationKey MLKEM768
_) = PubKeyALG
PubKeyALG_MLKEM768
pubkeyToAlg (PubKeyMLKEM1024 EncapsulationKey MLKEM1024
_) = PubKeyALG
PubKeyALG_MLKEM1024
pubkeyToAlg (PubKeyUnknown OID
oid ByteString
_) = OID -> PubKeyALG
PubKeyALG_Unknown OID
oid
encodePK :: PubKey -> [ASN1]
encodePK :: PubKey -> [ASN1]
encodePK PubKey
key = ASN1ConstructionType -> ASN1S
asn1Container ASN1ConstructionType
Sequence (PubKey -> [ASN1]
encodeInner PubKey
key)
where
pkalg :: ASN1
pkalg = OID -> ASN1
OID (OID -> ASN1) -> OID -> ASN1
forall a b. (a -> b) -> a -> b
$ PubKeyALG -> OID
forall a. OIDable a => a -> OID
getObjectID (PubKeyALG -> OID) -> PubKeyALG -> OID
forall a b. (a -> b) -> a -> b
$ PubKey -> PubKeyALG
pubkeyToAlg PubKey
key
encodeInner :: PubKey -> [ASN1]
encodeInner (PubKeyRSA PublicKey
pubkey) =
ASN1ConstructionType -> ASN1S
asn1Container ASN1ConstructionType
Sequence [ASN1
pkalg, ASN1
Null] [ASN1] -> ASN1S
forall a. [a] -> [a] -> [a]
++ [BitArray -> ASN1
BitString (BitArray -> ASN1) -> BitArray -> ASN1
forall a b. (a -> b) -> a -> b
$ ByteString -> Int -> BitArray
toBitArray ByteString
bits Int
0]
where
bits :: ByteString
bits = DER -> [ASN1] -> ByteString
forall a. ASN1Encoding a => a -> [ASN1] -> ByteString
encodeASN1' DER
DER ([ASN1] -> ByteString) -> [ASN1] -> ByteString
forall a b. (a -> b) -> a -> b
$ PublicKey -> ASN1S
rsaPubToASN1 PublicKey
pubkey []
encodeInner (PubKeyRSAPSS PublicKey
pubkey Maybe PSSParams
mp) =
Maybe PSSParams -> ASN1S
pssKeyAlgToASN1 Maybe PSSParams
mp [BitArray -> ASN1
BitString (BitArray -> ASN1) -> BitArray -> ASN1
forall a b. (a -> b) -> a -> b
$ ByteString -> Int -> BitArray
toBitArray ByteString
bits Int
0]
where
bits :: ByteString
bits = DER -> [ASN1] -> ByteString
forall a. ASN1Encoding a => a -> [ASN1] -> ByteString
encodeASN1' DER
DER ([ASN1] -> ByteString) -> [ASN1] -> ByteString
forall a b. (a -> b) -> a -> b
$ PublicKey -> ASN1S
rsaPubToASN1 PublicKey
pubkey []
encodeInner (PubKeyDSA PublicKey
pubkey) =
ASN1ConstructionType -> ASN1S
asn1Container ASN1ConstructionType
Sequence ([ASN1
pkalg] [ASN1] -> ASN1S
forall a. [a] -> [a] -> [a]
++ [ASN1]
dsaseq) [ASN1] -> ASN1S
forall a. [a] -> [a] -> [a]
++ [BitArray -> ASN1
BitString (BitArray -> ASN1) -> BitArray -> ASN1
forall a b. (a -> b) -> a -> b
$ ByteString -> Int -> BitArray
toBitArray ByteString
bits Int
0]
where
dsaseq :: [ASN1]
dsaseq =
ASN1ConstructionType -> ASN1S
asn1Container
ASN1ConstructionType
Sequence
[ Integer -> ASN1
IntVal (Params -> Integer
DSA.params_p Params
params)
, Integer -> ASN1
IntVal (Params -> Integer
DSA.params_q Params
params)
, Integer -> ASN1
IntVal (Params -> Integer
DSA.params_g Params
params)
]
params :: Params
params = PublicKey -> Params
DSA.public_params PublicKey
pubkey
bits :: ByteString
bits = DER -> [ASN1] -> ByteString
forall a. ASN1Encoding a => a -> [ASN1] -> ByteString
encodeASN1' DER
DER [Integer -> ASN1
IntVal (Integer -> ASN1) -> Integer -> ASN1
forall a b. (a -> b) -> a -> b
$ PublicKey -> Integer
DSA.public_y PublicKey
pubkey]
encodeInner (PubKeyEC (PubKeyEC_Named CurveName
curveName (SerializedPoint ByteString
bits))) =
ASN1ConstructionType -> ASN1S
asn1Container ASN1ConstructionType
Sequence [ASN1
pkalg, OID -> ASN1
OID OID
eOid] [ASN1] -> ASN1S
forall a. [a] -> [a] -> [a]
++ [BitArray -> ASN1
BitString (BitArray -> ASN1) -> BitArray -> ASN1
forall a b. (a -> b) -> a -> b
$ ByteString -> Int -> BitArray
toBitArray ByteString
bits Int
0]
where
eOid :: OID
eOid = case OIDTable CurveName -> CurveName -> Maybe OID
forall a. Eq a => OIDTable a -> a -> Maybe OID
lookupOID OIDTable CurveName
curvesOIDTable CurveName
curveName of
Just OID
oid -> OID
oid
Maybe OID
_ -> String -> OID
forall a. HasCallStack => String -> a
error (String
"undefined curve OID: " String -> ShowS
forall a. [a] -> [a] -> [a]
++ CurveName -> String
forall a. Show a => a -> String
show CurveName
curveName)
encodeInner (PubKeyEC k :: PubKeyEC
k@PubKeyEC_Prime{}) =
ASN1ConstructionType -> ASN1S
asn1Container ASN1ConstructionType
Sequence ([ASN1
pkalg] [ASN1] -> ASN1S
forall a. [a] -> [a] -> [a]
++ [ASN1]
params) [ASN1] -> ASN1S
forall a. [a] -> [a] -> [a]
++ [BitArray -> ASN1
BitString (BitArray -> ASN1) -> BitArray -> ASN1
forall a b. (a -> b) -> a -> b
$ ByteString -> Int -> BitArray
toBitArray ByteString
pub Int
0]
where
SerializedPoint ByteString
pub = PubKeyEC -> SerializedPoint
pubkeyEC_pub PubKeyEC
k
SerializedPoint ByteString
gen = PubKeyEC -> SerializedPoint
pubkeyEC_generator PubKeyEC
k
size :: Int
size = Integer -> Int
numBytes (PubKeyEC -> Integer
pubkeyEC_prime PubKeyEC
k)
params :: [ASN1]
params =
ASN1ConstructionType -> ASN1S
asn1Container ASN1ConstructionType
Sequence ASN1S -> ASN1S
forall a b. (a -> b) -> a -> b
$
[Integer -> ASN1
IntVal Integer
1]
[ASN1] -> ASN1S
forall a. [a] -> [a] -> [a]
++ ASN1ConstructionType -> ASN1S
asn1Container
ASN1ConstructionType
Sequence
[OID -> ASN1
OID [Integer
1, Integer
2, Integer
840, Integer
10045, Integer
1, Integer
1], Integer -> ASN1
IntVal (PubKeyEC -> Integer
pubkeyEC_prime PubKeyEC
k)]
[ASN1] -> ASN1S
forall a. [a] -> [a] -> [a]
++ ASN1ConstructionType -> ASN1S
asn1Container
ASN1ConstructionType
Sequence
[ ByteString -> ASN1
OctetString (Int -> Integer -> ByteString
forall ba. ByteArray ba => Int -> Integer -> ba
i2ospOf_ Int
size (PubKeyEC -> Integer
pubkeyEC_a PubKeyEC
k))
, ByteString -> ASN1
OctetString (Int -> Integer -> ByteString
forall ba. ByteArray ba => Int -> Integer -> ba
i2ospOf_ Int
size (PubKeyEC -> Integer
pubkeyEC_b PubKeyEC
k))
, BitArray -> ASN1
BitString (BitArray -> ASN1) -> BitArray -> ASN1
forall a b. (a -> b) -> a -> b
$ ByteString -> Int -> BitArray
toBitArray (Integer -> ByteString
forall ba. ByteArray ba => Integer -> ba
i2osp (PubKeyEC -> Integer
pubkeyEC_seed PubKeyEC
k)) Int
0
]
[ASN1] -> ASN1S
forall a. [a] -> [a] -> [a]
++ [ ByteString -> ASN1
OctetString ByteString
gen
, Integer -> ASN1
IntVal (PubKeyEC -> Integer
pubkeyEC_order PubKeyEC
k)
, Integer -> ASN1
IntVal (PubKeyEC -> Integer
pubkeyEC_cofactor PubKeyEC
k)
]
encodeInner (PubKeyX25519 PublicKey
pubkey) =
ASN1ConstructionType -> ASN1S
asn1Container ASN1ConstructionType
Sequence [ASN1
pkalg] [ASN1] -> ASN1S
forall a. [a] -> [a] -> [a]
++ [BitArray -> ASN1
BitString (BitArray -> ASN1) -> BitArray -> ASN1
forall a b. (a -> b) -> a -> b
$ ByteString -> Int -> BitArray
toBitArray (PublicKey -> ByteString
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
convert PublicKey
pubkey) Int
0]
encodeInner (PubKeyX448 PublicKey
pubkey) =
ASN1ConstructionType -> ASN1S
asn1Container ASN1ConstructionType
Sequence [ASN1
pkalg] [ASN1] -> ASN1S
forall a. [a] -> [a] -> [a]
++ [BitArray -> ASN1
BitString (BitArray -> ASN1) -> BitArray -> ASN1
forall a b. (a -> b) -> a -> b
$ ByteString -> Int -> BitArray
toBitArray (PublicKey -> ByteString
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
convert PublicKey
pubkey) Int
0]
encodeInner (PubKeyEd25519 PublicKey
pubkey) =
ASN1ConstructionType -> ASN1S
asn1Container ASN1ConstructionType
Sequence [ASN1
pkalg] [ASN1] -> ASN1S
forall a. [a] -> [a] -> [a]
++ [BitArray -> ASN1
BitString (BitArray -> ASN1) -> BitArray -> ASN1
forall a b. (a -> b) -> a -> b
$ ByteString -> Int -> BitArray
toBitArray (PublicKey -> ByteString
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
convert PublicKey
pubkey) Int
0]
encodeInner (PubKeyEd448 PublicKey
pubkey) =
ASN1ConstructionType -> ASN1S
asn1Container ASN1ConstructionType
Sequence [ASN1
pkalg] [ASN1] -> ASN1S
forall a. [a] -> [a] -> [a]
++ [BitArray -> ASN1
BitString (BitArray -> ASN1) -> BitArray -> ASN1
forall a b. (a -> b) -> a -> b
$ ByteString -> Int -> BitArray
toBitArray (PublicKey -> ByteString
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
convert PublicKey
pubkey) Int
0]
encodeInner (PubKeyMLDSA44 VerificationKey MLDSA44
pubkey) =
ASN1ConstructionType -> ASN1S
asn1Container ASN1ConstructionType
Sequence [ASN1
pkalg] [ASN1] -> ASN1S
forall a. [a] -> [a] -> [a]
++ [BitArray -> ASN1
BitString (BitArray -> ASN1) -> BitArray -> ASN1
forall a b. (a -> b) -> a -> b
$ ByteString -> Int -> BitArray
toBitArray (VerificationKey MLDSA44 -> ByteString
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
convert VerificationKey MLDSA44
pubkey) Int
0]
encodeInner (PubKeyMLDSA65 VerificationKey MLDSA65
pubkey) =
ASN1ConstructionType -> ASN1S
asn1Container ASN1ConstructionType
Sequence [ASN1
pkalg] [ASN1] -> ASN1S
forall a. [a] -> [a] -> [a]
++ [BitArray -> ASN1
BitString (BitArray -> ASN1) -> BitArray -> ASN1
forall a b. (a -> b) -> a -> b
$ ByteString -> Int -> BitArray
toBitArray (VerificationKey MLDSA65 -> ByteString
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
convert VerificationKey MLDSA65
pubkey) Int
0]
encodeInner (PubKeyMLDSA87 VerificationKey MLDSA87
pubkey) =
ASN1ConstructionType -> ASN1S
asn1Container ASN1ConstructionType
Sequence [ASN1
pkalg] [ASN1] -> ASN1S
forall a. [a] -> [a] -> [a]
++ [BitArray -> ASN1
BitString (BitArray -> ASN1) -> BitArray -> ASN1
forall a b. (a -> b) -> a -> b
$ ByteString -> Int -> BitArray
toBitArray (VerificationKey MLDSA87 -> ByteString
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
convert VerificationKey MLDSA87
pubkey) Int
0]
encodeInner (PubKeyMLKEM512 EncapsulationKey MLKEM512
pubkey) =
ASN1ConstructionType -> ASN1S
asn1Container ASN1ConstructionType
Sequence [ASN1
pkalg] [ASN1] -> ASN1S
forall a. [a] -> [a] -> [a]
++ [BitArray -> ASN1
BitString (BitArray -> ASN1) -> BitArray -> ASN1
forall a b. (a -> b) -> a -> b
$ ByteString -> Int -> BitArray
toBitArray (MLKEMEncapsulationKey MLKEM512 -> ByteString
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
convert EncapsulationKey MLKEM512
MLKEMEncapsulationKey MLKEM512
pubkey) Int
0]
encodeInner (PubKeyMLKEM768 EncapsulationKey MLKEM768
pubkey) =
ASN1ConstructionType -> ASN1S
asn1Container ASN1ConstructionType
Sequence [ASN1
pkalg] [ASN1] -> ASN1S
forall a. [a] -> [a] -> [a]
++ [BitArray -> ASN1
BitString (BitArray -> ASN1) -> BitArray -> ASN1
forall a b. (a -> b) -> a -> b
$ ByteString -> Int -> BitArray
toBitArray (MLKEMEncapsulationKey MLKEM768 -> ByteString
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
convert EncapsulationKey MLKEM768
MLKEMEncapsulationKey MLKEM768
pubkey) Int
0]
encodeInner (PubKeyMLKEM1024 EncapsulationKey MLKEM1024
pubkey) =
ASN1ConstructionType -> ASN1S
asn1Container ASN1ConstructionType
Sequence [ASN1
pkalg] [ASN1] -> ASN1S
forall a. [a] -> [a] -> [a]
++ [BitArray -> ASN1
BitString (BitArray -> ASN1) -> BitArray -> ASN1
forall a b. (a -> b) -> a -> b
$ ByteString -> Int -> BitArray
toBitArray (MLKEMEncapsulationKey MLKEM1024 -> ByteString
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
convert EncapsulationKey MLKEM1024
MLKEMEncapsulationKey MLKEM1024
pubkey) Int
0]
encodeInner (PubKeyDH PubKeyDH
k) =
ASN1ConstructionType -> ASN1S
asn1Container ASN1ConstructionType
Sequence ([ASN1
pkalg] [ASN1] -> ASN1S
forall a. [a] -> [a] -> [a]
++ [ASN1]
params) [ASN1] -> ASN1S
forall a. [a] -> [a] -> [a]
++ [BitArray -> ASN1
BitString (BitArray -> ASN1) -> BitArray -> ASN1
forall a b. (a -> b) -> a -> b
$ ByteString -> Int -> BitArray
toBitArray ByteString
bits Int
0]
where
params :: [ASN1]
params =
ASN1ConstructionType -> ASN1S
asn1Container ASN1ConstructionType
Sequence ASN1S -> ASN1S
forall a b. (a -> b) -> a -> b
$
[Integer -> ASN1
IntVal (PubKeyDH -> Integer
pubkeyDH_p PubKeyDH
k), Integer -> ASN1
IntVal (PubKeyDH -> Integer
pubkeyDH_g PubKeyDH
k), Integer -> ASN1
IntVal (PubKeyDH -> Integer
pubkeyDH_q PubKeyDH
k)]
[ASN1] -> ASN1S
forall a. [a] -> [a] -> [a]
++ [ASN1] -> (Integer -> [ASN1]) -> Maybe Integer -> [ASN1]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] (\Integer
j -> [Integer -> ASN1
IntVal Integer
j]) (PubKeyDH -> Maybe Integer
pubkeyDH_j PubKeyDH
k)
[ASN1] -> ASN1S
forall a. [a] -> [a] -> [a]
++ [ASN1]
-> ((BitArray, Integer) -> [ASN1])
-> Maybe (BitArray, Integer)
-> [ASN1]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe
[]
(\(BitArray
seed, Integer
c) -> ASN1ConstructionType -> ASN1S
asn1Container ASN1ConstructionType
Sequence [BitArray -> ASN1
BitString BitArray
seed, Integer -> ASN1
IntVal Integer
c])
(PubKeyDH -> Maybe (BitArray, Integer)
pubkeyDH_validation PubKeyDH
k)
bits :: ByteString
bits = DER -> [ASN1] -> ByteString
forall a. ASN1Encoding a => a -> [ASN1] -> ByteString
encodeASN1' DER
DER [Integer -> ASN1
IntVal (Integer -> ASN1) -> Integer -> ASN1
forall a b. (a -> b) -> a -> b
$ PubKeyDH -> Integer
pubkeyDH_y PubKeyDH
k]
encodeInner (PubKeyUnknown OID
_ ByteString
l) =
ASN1ConstructionType -> ASN1S
asn1Container ASN1ConstructionType
Sequence [ASN1
pkalg, ASN1
Null] [ASN1] -> ASN1S
forall a. [a] -> [a] -> [a]
++ [BitArray -> ASN1
BitString (BitArray -> ASN1) -> BitArray -> ASN1
forall a b. (a -> b) -> a -> b
$ ByteString -> Int -> BitArray
toBitArray ByteString
l Int
0]
rsaPubToASN1 :: RSA.PublicKey -> [ASN1] -> [ASN1]
rsaPubToASN1 :: PublicKey -> ASN1S
rsaPubToASN1 PublicKey
pubkey [ASN1]
xs =
ASN1ConstructionType -> ASN1
Start ASN1ConstructionType
Sequence
ASN1 -> ASN1S
forall a. a -> [a] -> [a]
: Integer -> ASN1
IntVal (PublicKey -> Integer
RSA.public_n PublicKey
pubkey)
ASN1 -> ASN1S
forall a. a -> [a] -> [a]
: Integer -> ASN1
IntVal (PublicKey -> Integer
RSA.public_e PublicKey
pubkey)
ASN1 -> ASN1S
forall a. a -> [a] -> [a]
: ASN1ConstructionType -> ASN1
End ASN1ConstructionType
Sequence
ASN1 -> ASN1S
forall a. a -> [a] -> [a]
: [ASN1]
xs
rsaPubFromASN1 :: [ASN1] -> Either String (RSA.PublicKey, [ASN1])
rsaPubFromASN1 :: [ASN1] -> Either String (PublicKey, [ASN1])
rsaPubFromASN1 (Start ASN1ConstructionType
Sequence : IntVal Integer
smodulus : IntVal Integer
pubexp : End ASN1ConstructionType
Sequence : [ASN1]
xs) =
(PublicKey, [ASN1]) -> Either String (PublicKey, [ASN1])
forall a b. b -> Either a b
Right (PublicKey
pub, [ASN1]
xs)
where
pub :: PublicKey
pub =
RSA.PublicKey
{ public_size :: Int
RSA.public_size = Integer -> Int
numBytes Integer
modulus
, public_n :: Integer
RSA.public_n = Integer
modulus
, public_e :: Integer
RSA.public_e = Integer
pubexp
}
modulus :: Integer
modulus = Integer -> Integer
toPositive Integer
smodulus
rsaPubFromASN1
( Start ASN1ConstructionType
Sequence
: IntVal Integer
ver
: Start ASN1ConstructionType
Sequence
: OID OID
oid
: ASN1
Null
: End ASN1ConstructionType
Sequence
: OctetString ByteString
bs
: [ASN1]
xs
)
| Integer
ver Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
/= Integer
0 = String -> Either String (PublicKey, [ASN1])
forall a b. a -> Either a b
Left String
"rsaPubFromASN1: Invalid version, expecting 0"
| OID
oid OID -> OID -> Bool
forall a. Eq a => a -> a -> Bool
/= [Integer
1, Integer
2, Integer
840, Integer
113549, Integer
1, Integer
1, Integer
1] =
String -> Either String (PublicKey, [ASN1])
forall a b. a -> Either a b
Left String
"rsaPubFromASN1: invalid OID"
| Bool
otherwise =
let inner :: Either String (PublicKey, [ASN1])
inner = (String -> Either String (PublicKey, [ASN1]))
-> ([ASN1] -> Either String (PublicKey, [ASN1]))
-> Either String [ASN1]
-> Either String (PublicKey, [ASN1])
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either String -> Either String (PublicKey, [ASN1])
forall {b}. String -> Either String b
strError [ASN1] -> Either String (PublicKey, [ASN1])
rsaPubFromASN1 (Either String [ASN1] -> Either String (PublicKey, [ASN1]))
-> Either String [ASN1] -> Either String (PublicKey, [ASN1])
forall a b. (a -> b) -> a -> b
$ Either ASN1Error [ASN1] -> Either String [ASN1]
forall e a. Show e => Either e [a] -> Either String [a]
safeDecode (Either ASN1Error [ASN1] -> Either String [ASN1])
-> Either ASN1Error [ASN1] -> Either String [ASN1]
forall a b. (a -> b) -> a -> b
$ BER -> ByteString -> Either ASN1Error [ASN1]
forall a.
ASN1Decoding a =>
a -> ByteString -> Either ASN1Error [ASN1]
decodeASN1' BER
BER ByteString
bs
strError :: String -> Either String b
strError = String -> Either String b
forall a b. a -> Either a b
Left (String -> Either String b) -> ShowS -> String -> Either String b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String
"fromASN1: RSA.PublicKey: " String -> ShowS
forall a. [a] -> [a] -> [a]
++)
in case Either String (PublicKey, [ASN1])
inner of
Right (PublicKey
k, []) -> (PublicKey, [ASN1]) -> Either String (PublicKey, [ASN1])
forall a b. b -> Either a b
Right (PublicKey
k, [ASN1]
xs)
Right (PublicKey, [ASN1])
_ -> String -> Either String (PublicKey, [ASN1])
forall a b. a -> Either a b
Left String
"fromASN1: RSA.PublicKey: trailing data"
Left String
e -> String -> Either String (PublicKey, [ASN1])
forall a b. a -> Either a b
Left String
e
rsaPubFromASN1 [ASN1]
_ =
String -> Either String (PublicKey, [ASN1])
forall a b. a -> Either a b
Left String
"fromASN1: RSA.PublicKey: unexpected format"
toPositive :: Integer -> Integer
toPositive :: Integer -> Integer
toPositive Integer
int
| Integer
int Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
0 = [Word8] -> Integer
uintOfBytes ([Word8] -> Integer) -> [Word8] -> Integer
forall a b. (a -> b) -> a -> b
$ Integer -> [Word8]
bytesOfInt Integer
int
| Bool
otherwise = Integer
int
where
uintOfBytes :: [Word8] -> Integer
uintOfBytes = (Integer -> Word8 -> Integer) -> Integer -> [Word8] -> Integer
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl (\Integer
acc Word8
n -> (Integer
acc Integer -> Int -> Integer
forall a. Bits a => a -> Int -> a
`shiftL` Int
8) Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Word8 -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
n) Integer
0
bytesOfInt :: Integer -> [Word8]
bytesOfInt :: Integer -> [Word8]
bytesOfInt Integer
n = case [Word8]
nints of
Word8
h : [Word8]
_ | Word8 -> Int -> Bool
forall a. Bits a => a -> Int -> Bool
testBit Word8
h Int
7 -> [Word8]
nints
[Word8]
_ -> Word8
0xff Word8 -> [Word8] -> [Word8]
forall a. a -> [a] -> [a]
: [Word8]
nints
where
nints :: [Word8]
nints = [Word8] -> [Word8]
forall a. [a] -> [a]
reverse ([Word8] -> [Word8]) -> [Word8] -> [Word8]
forall a b. (a -> b) -> a -> b
$ [Word8] -> [Word8]
forall {a}. (Num a, Eq a) => [a] -> [a]
plusOne ([Word8] -> [Word8]) -> [Word8] -> [Word8]
forall a b. (a -> b) -> a -> b
$ [Word8] -> [Word8]
forall a. [a] -> [a]
reverse ([Word8] -> [Word8]) -> [Word8] -> [Word8]
forall a b. (a -> b) -> a -> b
$ (Word8 -> Word8) -> [Word8] -> [Word8]
forall a b. (a -> b) -> [a] -> [b]
map Word8 -> Word8
forall a. Bits a => a -> a
complement ([Word8] -> [Word8]) -> [Word8] -> [Word8]
forall a b. (a -> b) -> a -> b
$ Integer -> [Word8]
forall {t} {a}. (Integral t, Bits a, Bits t, Num a) => t -> [a]
bytesOfUInt (Integer -> Integer
forall a. Num a => a -> a
abs Integer
n)
plusOne :: [a] -> [a]
plusOne [] = [a
1]
plusOne (a
x : [a]
xs) = if a
x a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
0xff then a
0 a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a] -> [a]
plusOne [a]
xs else (a
x a -> a -> a
forall a. Num a => a -> a -> a
+ a
1) a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
xs
bytesOfUInt :: t -> [a]
bytesOfUInt t
x = [a] -> [a]
forall a. [a] -> [a]
reverse (t -> [a]
forall {t} {a}. (Integral t, Num a, Bits a, Bits t) => t -> [a]
list t
x)
where
list :: t -> [a]
list t
i =
if t
i t -> t -> Bool
forall a. Ord a => a -> a -> Bool
<= t
0xff
then [t -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral t
i]
else (t -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral t
i a -> a -> a
forall a. Bits a => a -> a -> a
.&. a
0xff) a -> [a] -> [a]
forall a. a -> [a] -> [a]
: t -> [a]
list (t
i t -> Int -> t
forall a. Bits a => a -> Int -> a
`shiftR` Int
8)