module OpenSSL.EVP.Verify
( VerifyStatus(..)
, verify
, verifyBS
, verifyLBS
)
where
import qualified Data.ByteString.Char8 as B8
import qualified Data.ByteString.Lazy.Char8 as L8
import qualified Data.ByteString.Unsafe as B8
import Data.Typeable
import Foreign
import Foreign.C
import OpenSSL.EVP.Digest
import OpenSSL.EVP.PKey
import OpenSSL.EVP.Internal
import OpenSSL.Utils
data VerifyStatus = VerifySuccess
| VerifyFailure
deriving (Show, Eq, Typeable)
foreign import ccall unsafe "EVP_VerifyFinal"
_VerifyFinal :: Ptr EVP_MD_CTX -> Ptr CChar -> CUInt -> Ptr EVP_PKEY -> IO CInt
verifyFinalBS :: PublicKey k =>
DigestCtx
-> B8.ByteString
-> k
-> IO VerifyStatus
verifyFinalBS ctx sig k
= withDigestCtxPtr ctx $ \ ctxPtr ->
B8.unsafeUseAsCStringLen sig $ \ (buf, len) ->
withPKeyPtr' k $ \ pkeyPtr ->
_VerifyFinal ctxPtr buf (fromIntegral len) pkeyPtr >>= interpret
where
interpret :: CInt -> IO VerifyStatus
interpret 1 = return VerifySuccess
interpret 0 = return VerifyFailure
interpret _ = raiseOpenSSLError
verify :: PublicKey key =>
Digest
-> String
-> key
-> String
-> IO VerifyStatus
verify md sig pkey input
= verifyLBS md (B8.pack sig) pkey (L8.pack input)
verifyBS :: PublicKey key =>
Digest
-> B8.ByteString
-> key
-> B8.ByteString
-> IO VerifyStatus
verifyBS md sig pkey input
= do ctx <- digestStrictly md input
verifyFinalBS ctx sig pkey
verifyLBS :: PublicKey key =>
Digest
-> B8.ByteString
-> key
-> L8.ByteString
-> IO VerifyStatus
verifyLBS md sig pkey input
= do ctx <- digestLazily md input
verifyFinalBS ctx sig pkey