x509-store-1.6.1/0000755000000000000000000000000012573323031011543 5ustar0000000000000000x509-store-1.6.1/LICENSE0000644000000000000000000000273112573323031012553 0ustar0000000000000000Copyright (c) 2010-2013 Vincent Hanquez All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the author nor the names of his contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. x509-store-1.6.1/Setup.hs0000644000000000000000000000005612573323031013200 0ustar0000000000000000import Distribution.Simple main = defaultMain x509-store-1.6.1/x509-store.cabal0000644000000000000000000000234612573323031014373 0ustar0000000000000000Name: x509-store Version: 1.6.1 Description: X.509 collection accessing and storing methods for certificate, crl, exception list License: BSD3 License-file: LICENSE Copyright: Vincent Hanquez Author: Vincent Hanquez Maintainer: Vincent Hanquez Synopsis: X.509 collection accessing and storing methods Build-Type: Simple Category: Data stability: experimental Homepage: http://github.com/vincenthz/hs-certificate Cabal-Version: >=1.8 Library Build-Depends: base >= 3 && < 5 , bytestring , mtl , containers , pem >= 0.1 && < 0.3 , asn1-types >= 0.3 && < 0.4 , asn1-encoding >= 0.9 && < 0.10 , cryptonite , x509 >= 1.6 && < 1.7 Exposed-modules: Data.X509.CertificateStore Data.X509.File Data.X509.Memory ghc-options: -Wall source-repository head type: git location: git://github.com/vincenthz/hs-certificate subdir: x509-store x509-store-1.6.1/Data/0000755000000000000000000000000012573323031012414 5ustar0000000000000000x509-store-1.6.1/Data/X509/0000755000000000000000000000000012573323031013061 5ustar0000000000000000x509-store-1.6.1/Data/X509/CertificateStore.hs0000644000000000000000000000337212573323031016661 0ustar0000000000000000module Data.X509.CertificateStore ( CertificateStore , makeCertificateStore -- * Queries , findCertificate , listCertificates ) where import Data.List (foldl') import Data.Monoid import Data.X509 import qualified Data.Map as M import Control.Monad (mplus) -- | A Collection of certificate or store of certificates. data CertificateStore = CertificateStore (M.Map DistinguishedName SignedCertificate) | CertificateStores [CertificateStore] instance Monoid CertificateStore where mempty = CertificateStore M.empty mappend s1@(CertificateStore _) s2@(CertificateStore _) = CertificateStores [s1,s2] mappend (CertificateStores l) s2@(CertificateStore _) = CertificateStores (l ++ [s2]) mappend s1@(CertificateStore _) (CertificateStores l) = CertificateStores ([s1] ++ l) mappend (CertificateStores l1) (CertificateStores l2) = CertificateStores (l1 ++ l2) -- | Create a certificate store out of a list of X509 certificate makeCertificateStore :: [SignedCertificate] -> CertificateStore makeCertificateStore = CertificateStore . foldl' accumulate M.empty where accumulate m x509 = M.insert (certSubjectDN $ getCertificate x509) x509 m -- | Find a certificate using the subject distinguished name findCertificate :: DistinguishedName -> CertificateStore -> Maybe SignedCertificate findCertificate dn store = lookupIn store where lookupIn (CertificateStore m) = M.lookup dn m lookupIn (CertificateStores l) = foldl mplus Nothing $ map lookupIn l -- | List all certificates in a store listCertificates :: CertificateStore -> [SignedCertificate] listCertificates (CertificateStore store) = map snd $ M.toList store listCertificates (CertificateStores l) = concatMap listCertificates l x509-store-1.6.1/Data/X509/File.hs0000644000000000000000000000207712573323031014302 0ustar0000000000000000module Data.X509.File ( readSignedObject , readKeyFile ) where import Control.Applicative import Data.ASN1.Types import Data.ASN1.BinaryEncoding import Data.ASN1.Encoding import Data.Maybe import qualified Data.X509 as X509 import Data.X509.Memory (pemToKey) import Data.PEM (pemParseLBS, pemContent, pemName, PEM) import qualified Data.ByteString.Lazy as L readPEMs :: FilePath -> IO [PEM] readPEMs filepath = do content <- L.readFile filepath return $ either error id $ pemParseLBS content -- | return all the signed objects in a file. -- -- (only one type at a time). readSignedObject :: (ASN1Object a, Eq a, Show a) => FilePath -> IO [X509.SignedExact a] readSignedObject filepath = decodePEMs <$> readPEMs filepath where decodePEMs pems = [ obj | pem <- pems, Right obj <- [X509.decodeSignedObject $ pemContent pem] ] -- | return all the public key that were successfully read from a file. readKeyFile :: FilePath -> IO [X509.PrivKey] readKeyFile path = catMaybes . foldl pemToKey [] <$> readPEMs path x509-store-1.6.1/Data/X509/Memory.hs0000644000000000000000000001007612573323031014671 0ustar0000000000000000-- | -- Module : Data.X509.Memory -- License : BSD-style -- Maintainer : Vincent Hanquez -- Stability : experimental -- Portability : unknown -- -- module Data.X509.Memory ( readKeyFileFromMemory , readSignedObjectFromMemory , pemToKey ) where import Data.ASN1.Types import Data.ASN1.BinaryEncoding import Data.ASN1.Encoding import Data.Maybe import qualified Data.X509 as X509 import Data.PEM (pemParseBS, pemContent, pemName, PEM) import qualified Data.ByteString as B import qualified Crypto.PubKey.DSA as DSA import qualified Crypto.PubKey.RSA as RSA readKeyFileFromMemory :: B.ByteString -> [X509.PrivKey] readKeyFileFromMemory = either (const []) (catMaybes . foldl pemToKey []) . pemParseBS readSignedObjectFromMemory :: (ASN1Object a, Eq a, Show a) => B.ByteString -> [X509.SignedExact a] readSignedObjectFromMemory = either (const []) (foldl pemToSigned []) . pemParseBS where pemToSigned acc pem = case X509.decodeSignedObject $ pemContent pem of Left _ -> acc Right obj -> obj : acc pemToKey :: [Maybe X509.PrivKey] -> PEM -> [Maybe X509.PrivKey] pemToKey acc pem = case decodeASN1' BER (pemContent pem) of Left _ -> acc Right asn1 -> case pemName pem of "PRIVATE KEY" -> tryRSA asn1 : tryDSA asn1 : acc "RSA PRIVATE KEY" -> tryRSA asn1 : acc "DSA PRIVATE KEY" -> tryDSA asn1 : acc _ -> acc where tryRSA asn1 = case rsaFromASN1 asn1 of Left _ -> Nothing Right (k,_) -> Just $ X509.PrivKeyRSA k tryDSA asn1 = case dsaFromASN1 asn1 of Left _ -> Nothing Right (k,_) -> Just $ X509.PrivKeyDSA $ DSA.toPrivateKey k dsaFromASN1 :: [ASN1] -> Either String (DSA.KeyPair, [ASN1]) dsaFromASN1 (Start Sequence : IntVal n : xs) | n /= 0 = Left "fromASN1: DSA.KeyPair: unknown format" | otherwise = case xs of IntVal p : IntVal q : IntVal g : IntVal pub : IntVal priv : End Sequence : xs2 -> let params = DSA.Params { DSA.params_p = p, DSA.params_g = g, DSA.params_q = q } in Right (DSA.KeyPair params pub priv, xs2) _ -> Left "dsaFromASN1: DSA.KeyPair: invalid format (version=0)" dsaFromASN1 _ = Left "dsaFromASN1: DSA.KeyPair: unexpected format" rsaFromASN1 :: [ASN1] -> Either String (RSA.PrivateKey, [ASN1]) rsaFromASN1 (Start Sequence : IntVal 0 : IntVal n : IntVal e : IntVal d : IntVal p1 : IntVal p2 : IntVal pexp1 : IntVal pexp2 : IntVal pcoef : End Sequence : xs) = Right (privKey, xs) where calculate_modulus m i = if (2 ^ (i * 8)) > m then i else calculate_modulus m (i+1) pubKey = RSA.PublicKey { RSA.public_size = calculate_modulus n 1 , RSA.public_n = n , RSA.public_e = e } privKey = RSA.PrivateKey { RSA.private_pub = pubKey , RSA.private_d = d , RSA.private_p = p1 , RSA.private_q = p2 , RSA.private_dP = pexp1 , RSA.private_dQ = pexp2 , RSA.private_qinv = pcoef } rsaFromASN1 ( Start Sequence : IntVal 0 : Start Sequence : OID [1, 2, 840, 113549, 1, 1, 1] : Null : End Sequence : OctetString bs : xs) = let inner = either strError rsaFromASN1 $ decodeASN1' BER bs strError = Left . ("rsaFromASN1: RSA.PrivateKey: " ++) . show in either Left (\(k, _) -> Right (k, xs)) inner rsaFromASN1 _ = Left "rsaFromASN1: unexpected format"