7 import Control.Applicative
8 import "mtl" Control.Monad.Trans
9 import Control.Monad.Unicode
10 import qualified Data.ByteString.Lazy.Char8 as Lazy
11 import Data.Time.Clock
12 import Network.HTTP.Lucu
14 import OpenSSL.EVP.PKey
16 import qualified OpenSSL.Session as SSL
23 key ← generateRSAKey 1024 3 Nothing
25 SSL.contextSetPrivateKey ctx key
26 SSL.contextSetCertificate ctx cert
27 SSL.contextSetDefaultCiphers ctx
29 let config = defaultConfig {
30 cnfServerPort = "9000"
31 , cnfSSLConfig = Just SSLConfig {
32 sslServerPort = "9001"
36 resources = mkResTree [ ([], helloWorld) ]
37 putStrLn "Access https://localhost:9001/ with your browser."
38 runHttpd config resources []
40 helloWorld ∷ ResourceDef
44 = Just $ do setContentType [mimeType| text/plain |]
45 putChunk "getRemoteCertificate = "
46 cert ← do cert ← getRemoteCertificate
48 Just c → liftIO $ Lazy.pack <$> printX509 c
49 Nothing → return "Nothing"
53 genCert ∷ KeyPair k ⇒ k → IO X509
57 setSerialNumber cert 1
58 setIssuerName cert [("CN", "localhost")]
59 setSubjectName cert [("CN", "localhost")]
60 setNotBefore cert =≪ addUTCTime (-1) <$> getCurrentTime
61 setNotAfter cert =≪ addUTCTime (365 * 24 * 60 * 60) <$> getCurrentTime
62 setPublicKey cert pkey
63 signX509 cert pkey Nothing