Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

New functions in OpenSSL.Session: contextSetPrivateKey, contextSetCer…

…tificate

Ignore-this: a84fe83df872e0327b82fa4a25773c02

darcs-hash:20090602090828-62b54-06ca59d0c1be7c1fb606a84d53109ed3dbf93031.gz
  • Loading branch information...
commit b3bfd7ed30ca6c00f57e868422e94536a5767828 1 parent a8095f9
@phonohawk phonohawk authored
Showing with 29 additions and 1 deletion.
  1. +2 −0  NEWS
  2. +27 −1 OpenSSL/Session.hsc
View
2  NEWS
@@ -7,6 +7,8 @@ Changes from 0.6 to 0.6.1
# lazyRead
# lazyWrite
# contextGetCAStore
+ # contextSetPrivateKey
+ # contextSetCertificate
Changes from 0.5.2 to 0.6
-------------------------
View
28 OpenSSL/Session.hsc
@@ -6,6 +6,8 @@ module OpenSSL.Session
( -- * Contexts
SSLContext
, context
+ , contextSetPrivateKey
+ , contextSetCertificate
, contextSetPrivateKeyFile
, contextSetCertificateFile
, contextSetCiphers
@@ -51,8 +53,9 @@ import System.IO.Unsafe
import System.Posix.Types (Fd(..))
import Network.Socket (Socket(..))
+import OpenSSL.EVP.PKey
import OpenSSL.Utils (failIfNull, failIf)
-import OpenSSL.X509 (X509, X509_, wrapX509)
+import OpenSSL.X509 (X509, X509_, wrapX509, withX509Ptr)
import OpenSSL.X509.Store
data SSLContext_
@@ -102,6 +105,29 @@ contextLoadFile f context path =
then return ()
else f ctx cpath (#const SSL_FILETYPE_ASN1) >>= failIf (/= 1) >> return ()
+foreign import ccall unsafe "SSL_CTX_use_PrivateKey"
+ _ssl_ctx_use_privatekey :: Ptr SSLContext_ -> Ptr EVP_PKEY -> IO CInt
+foreign import ccall unsafe "SSL_CTX_use_certificate"
+ _ssl_ctx_use_certificate :: Ptr SSLContext_ -> Ptr X509_ -> IO CInt
+
+-- | Install a private key into a context.
+contextSetPrivateKey :: KeyPair k => SSLContext -> k -> IO ()
+contextSetPrivateKey context key
+ = withContext context $ \ ctx ->
+ withPKeyPtr' key $ \ keyPtr ->
+ _ssl_ctx_use_privatekey ctx keyPtr
+ >>= failIf (/= 1)
+ >> return ()
+
+-- | Install a certificate (public key) into a context.
+contextSetCertificate :: SSLContext -> X509 -> IO ()
+contextSetCertificate context cert
+ = withContext context $ \ ctx ->
+ withX509Ptr cert $ \ certPtr ->
+ _ssl_ctx_use_certificate ctx certPtr
+ >>= failIf (/= 1)
+ >> return ()
+
foreign import ccall unsafe "SSL_CTX_use_PrivateKey_file"
_ssl_ctx_use_privatekey_file :: Ptr SSLContext_ -> CString -> CInt -> IO CInt
foreign import ccall unsafe "SSL_CTX_use_certificate_file"
Please sign in to comment.
Something went wrong with that request. Please try again.