Codec.Encryption.PKCS8

|  | Contents | Index
Codec.Encryption.PKCS8 |
Portability | non-portable
Stability | experimental
Maintainer | dominic.steinitz@blueyonder.co.uk
Contents
Type declarations Example
Description
Provide Haskell types associated with the ASN.1 types defined in the PKCS8 standard http://www.rsasecurity.com/rsalabs/pkcs/pkcs-8/ and functions to convert them to and from an abstract representation of ASN.1. This representation can be encoded and decoded from BER. It currently only handles the unencrypted PrivateKeyInfo format.
Synopsis
data RSAPrivateKey = MkRSAPrivateKey {
version :: Version
modulus :: Integer
publicExponent :: Integer
privateExponent :: Integer
prime1 :: Integer
prime2 :: Integer
exponent1 :: Integer
exponent2 :: Integer
coefficient :: Integer
}
data Version = V1
type Algorithm = OID
type Parameters = Int
data AlgorithmIdentifier = MkAlgorithmIdentifier {
algorithm :: Algorithm
parameters :: (Maybe Parameters)
}
data PrivateKeyInfo = MkPrivateKeyInfo {
version1 :: Version
privateKeyAlgorithm :: AlgorithmIdentifier
privateKey :: RSAPrivateKey
}
Type declarations
data RSAPrivateKey
Constructors
MkRSAPrivateKey |
version :: Version |
modulus :: Integer |
publicExponent :: Integer |
privateExponent :: Integer |
prime1 :: Integer |
prime2 :: Integer |
exponent1 :: Integer |
exponent2 :: Integer |
coefficient :: Integer |
Instances
ASNable RSAPrivateKey
Show RSAPrivateKey
data Version
Constructors
V1 |
Instances
ASNable Version
Show Version
Enum Version
type Algorithm = OID
type Parameters = Int
This will do for now. DSA has some parameters which are more complicated than this but since we plan to do RSA initially and this has NULL parameters then anything will do to get us going.
data AlgorithmIdentifier
The parameters will only ever be Nothing as this implementation only supports RSA and this has no parameters. So even if the parameters are non-NULL, fromASN will not fail but will ignore them.
Constructors
MkAlgorithmIdentifier |
algorithm :: Algorithm |
parameters :: (Maybe Parameters) |
Instances
ASNable AlgorithmIdentifier
Show AlgorithmIdentifier
data PrivateKeyInfo
Constructors
MkPrivateKeyInfo |
version1 :: Version |
privateKeyAlgorithm :: AlgorithmIdentifier |
privateKey :: RSAPrivateKey |
Instances
ASNable PrivateKeyInfo
Show PrivateKeyInfo
Example
Generate a key pair.
    openssl genrsa -out private.pem
And convert the private key to PKCS8 format without encryption.
    openssl pkcs8 -topk8 -inform PEM -outform DER -in private.pem -nocrypt -out private.der
Now read it with the Haskell program.
    module Main(main) where

    import IO
    import Char
    import Control.Monad.State
    import Codec.ASN1.ASN1
    import Codec.Encryption.Utils
    import Codec.Encryption.PKCS8

    -- Generate a key pair.
    --
    -- > openssl genrsa -out private.pem
    --
    -- And convert the private key to PKCS8 format without encryption.
    --
    -- > openssl pkcs8 -topk8 -inform PEM -outform DER -in private.pem -nocrypt -out private.der
    --
    -- Now read it with the Haskell program.

    main =
       do ifh <- openFile "private.der" ReadMode
          (x,y) <- decode ifh
          let z::PrivateKeyInfo = fromASN NoTag y
          putStrLn $ show z
          -- Decoding can be done using a state monad as an alternative.
          -- stdin is a dummy file handle so that the overloaded function decode can be used.
          let test =
                 runState (decode stdin) (map (chr . fromIntegral)
                                              (encode (toASN NoTag z)))
              test' :: PrivateKeyInfo
              test' = let ((x,y),z) = test in fromASN NoTag y
          putStrLn $ show test'
Produced by Haddock version 0.6