{-# LANGUAGE PatternSynonyms #-}-- |-- Module : Data.X509-- License : BSD-style-- Maintainer : Vincent Hanquez <vincent@snarc.org>-- Stability : experimental-- Portability : unknown---- Read/Write X509 Certificate, CRL and their signed equivalents.---- Follows RFC5280 / RFC6818module Data.X509 ( -- * Types SignedCertificate, SignedCRL, Certificate (..), PubKey (..), PubKeyEC (..), PubKeyDH, dhPublicKey, pubkeyDH_p, pubkeyDH_g, pubkeyDH_q, pubkeyDH_j, pubkeyDH_validation, pubkeyDH_y, SerializedPoint (..), PrivKey (..), PrivKeyEC (..), PrivKeyMLDSA, MLKeyForm (..), MLDSAKeyForm, pattern MLDSAKeySeed, pattern MLDSAKeyExpanded, pattern MLDSAKeyBoth, privkeyMLDSAFromSeed, privkeyMLDSAFromKey, privkeyMLDSA_key, privkeyMLDSA_seed, privkeyMLDSA_form, PrivKeyMLKEM, privkeyMLKEMFromSeed, privkeyMLKEMFromKey, privkeyMLKEM_key, privkeyMLKEM_seed, privkeyMLKEM_form, pubkeyToAlg, privkeyToAlg, module Data.X509.AlgorithmIdentifier, module Data.X509.Ext, module Data.X509.ExtensionRaw, -- * Certificate Revocation List (CRL) module Data.X509.CRL, -- * Naming DistinguishedName (DistinguishedName, getDistinguishedElements), RelativeDistinguishedName, getRDNs, fromRDNs, DnElement (..), ASN1CharacterString (..), getDnElement, canonicalDistinguishedName, -- * Certificate Chain module Data.X509.CertificateChain, -- * Signed types and marshalling Signed (..), SignedExact, getSigned, getSignedData, objectToSignedExact, objectToSignedExactF, encodeSignedObject, decodeSignedObject, -- * Parametrized Signed accessor getCertificate, getCRL, decodeSignedCertificate, decodeSignedCRL, -- * Hash distinguished names related function hashDN, hashDN_old,) whereimport Control.Arrow (second)import Data.ASN1.BinaryEncodingimport Data.ASN1.Encodingimport Data.ASN1.Typesimport qualified Data.ByteArray as BAimport qualified Data.ByteString as Bimport Data.X509.AlgorithmIdentifier hiding (pssKeyAlgToASN1, pssKeyParamsFromASN1)import Data.X509.CRLimport Data.X509.Certimport Data.X509.CertificateChainimport Data.X509.DistinguishedNameimport Data.X509.Extimport Data.X509.ExtensionRawimport Data.X509.PrivateKeyimport Data.X509.PublicKeyimport Data.X509.Signedimport Crypto.Hash-- | A Signed Certificatetype SignedCertificate = SignedExact Certificate-- | A Signed CRLtype SignedCRL = SignedExact CRL-- | Get the Certificate associated to a SignedCertificategetCertificate :: SignedCertificate -> CertificategetCertificate = signedObject . getSigned-- | Get the CRL associated to a SignedCRLgetCRL :: SignedCRL -> CRLgetCRL = signedObject . getSigned-- | Try to decode a bytestring to a SignedCertificatedecodeSignedCertificate :: B.ByteString -> Either String SignedCertificatedecodeSignedCertificate = decodeSignedObject-- | Try to decode a bytestring to a SignedCRLdecodeSignedCRL :: B.ByteString -> Either String SignedCRLdecodeSignedCRL = decodeSignedObject-- | Make an OpenSSL style hash of distinguished name---- OpenSSL algorithm is odd, and has been replicated here somewhat.-- only lower the case of ascii character.hashDN :: DistinguishedName -> B.ByteStringhashDN = shorten . hashWith SHA1 . encodeASN1' DER . flip toASN1 [] . DistinguishedNameInner . dnLowerUTF8 where dnLowerUTF8 = fromRDNs . map (map (second toLowerUTF8)) . getRDNs toLowerUTF8 (ASN1CharacterString _ s) = ASN1CharacterString UTF8 (B.map asciiToLower s) asciiToLower c | c >= w8A && c <= w8Z = fromIntegral (fromIntegral c - fromEnum 'A' + fromEnum 'a') | otherwise = c w8A = fromIntegral $ fromEnum 'A' w8Z = fromIntegral $ fromEnum 'Z'-- | Create an openssl style old hash of distinguished namehashDN_old :: DistinguishedName -> B.ByteStringhashDN_old = shorten . hashWith MD5 . encodeASN1' DER . flip toASN1 []shorten :: Digest a -> B.ByteStringshorten b = B.pack $ map i [3, 2, 1, 0] where i n = BA.index b n