2010-09-09 21:47:19 +00:00
|
|
|
-- |
|
|
|
|
-- Module : Network.TLS.Sending
|
|
|
|
-- License : BSD-style
|
|
|
|
-- Maintainer : Vincent Hanquez <vincent@snarc.org>
|
|
|
|
-- Stability : experimental
|
|
|
|
-- Portability : unknown
|
|
|
|
--
|
|
|
|
-- the Sending module contains calls related to marshalling packets according
|
|
|
|
-- to the TLS state
|
|
|
|
--
|
2012-07-13 19:08:23 +00:00
|
|
|
module Network.TLS.Sending (writePacket, encryptRSA, signRSA) where
|
2010-09-09 21:47:19 +00:00
|
|
|
|
2011-08-12 17:41:49 +00:00
|
|
|
import Control.Applicative ((<$>))
|
2010-09-09 21:47:19 +00:00
|
|
|
import Control.Monad.State
|
|
|
|
|
2010-09-26 09:34:47 +00:00
|
|
|
import Data.ByteString (ByteString)
|
2010-09-09 21:47:19 +00:00
|
|
|
import qualified Data.ByteString as B
|
|
|
|
|
2010-09-26 17:51:23 +00:00
|
|
|
import Network.TLS.Util
|
2010-09-09 21:47:19 +00:00
|
|
|
import Network.TLS.Struct
|
2011-08-12 17:41:49 +00:00
|
|
|
import Network.TLS.Record
|
2010-09-09 21:47:19 +00:00
|
|
|
import Network.TLS.Packet
|
|
|
|
import Network.TLS.State
|
2013-07-20 07:21:15 +00:00
|
|
|
import Network.TLS.Handshake.State
|
2010-09-09 21:47:19 +00:00
|
|
|
import Network.TLS.Crypto
|
|
|
|
|
2013-07-21 05:54:01 +00:00
|
|
|
-- | 'makePacketData' create a Header and a content bytestring related to a packet
|
|
|
|
-- this doesn't change any state
|
2013-07-11 08:03:33 +00:00
|
|
|
makeRecord :: Packet -> RecordM (Record Plaintext)
|
2011-08-12 17:41:49 +00:00
|
|
|
makeRecord pkt = do
|
2013-07-12 06:27:28 +00:00
|
|
|
ver <- stVersion <$> get
|
2013-07-21 05:54:01 +00:00
|
|
|
return $ Record (packetType pkt) ver (fragmentPlaintext $ writePacketContent pkt)
|
|
|
|
where writePacketContent (Handshake hss) = encodeHandshakes hss
|
|
|
|
writePacketContent (Alert a) = encodeAlerts a
|
|
|
|
writePacketContent (ChangeCipherSpec) = encodeChangeCipherSpec
|
|
|
|
writePacketContent (AppData x) = x
|
2010-09-09 21:47:19 +00:00
|
|
|
|
2013-07-21 05:54:01 +00:00
|
|
|
-- | marshall packet data
|
2013-07-11 08:03:33 +00:00
|
|
|
encodeRecord :: Record Ciphertext -> RecordM ByteString
|
2011-08-12 17:41:49 +00:00
|
|
|
encodeRecord record = return $ B.concat [ encodeHeader hdr, content ]
|
2013-07-12 06:27:28 +00:00
|
|
|
where (hdr, content) = recordToRaw record
|
2010-09-09 21:47:19 +00:00
|
|
|
|
2013-07-21 05:54:01 +00:00
|
|
|
-- | writePacket transform a packet into marshalled data related to current state
|
|
|
|
-- and updating state on the go
|
2011-03-01 20:01:40 +00:00
|
|
|
writePacket :: Packet -> TLSSt ByteString
|
2013-07-20 15:07:07 +00:00
|
|
|
writePacket pkt@(Handshake hss) = do
|
|
|
|
forM_ hss $ \hs -> do
|
|
|
|
case hs of
|
|
|
|
Finished fdata -> updateVerifiedData True fdata
|
|
|
|
_ -> return ()
|
|
|
|
let encoded = encodeHandshake hs
|
|
|
|
when (certVerifyHandshakeMaterial hs) $ withHandshakeM $ addHandshakeMessage encoded
|
|
|
|
when (finishHandshakeTypeMaterial $ typeOfHandshake hs) $ withHandshakeM $ updateHandshakeDigest encoded
|
2013-07-20 15:09:16 +00:00
|
|
|
runRecordStateSt (makeRecord pkt >>= engageRecord >>= encodeRecord)
|
|
|
|
writePacket pkt = do
|
|
|
|
d <- runRecordStateSt (makeRecord pkt >>= engageRecord >>= encodeRecord)
|
|
|
|
when (pkt == ChangeCipherSpec) $ runRecordStateSt switchTxEncryption
|
|
|
|
return d
|
2010-09-09 21:47:19 +00:00
|
|
|
|
|
|
|
{------------------------------------------------------------------------------}
|
|
|
|
{- SENDING Helpers -}
|
|
|
|
{------------------------------------------------------------------------------}
|
|
|
|
|
|
|
|
{- if the RSA encryption fails we just return an empty bytestring, and let the protocol
|
|
|
|
- fail by itself; however it would be probably better to just report it since it's an internal problem.
|
|
|
|
-}
|
2011-03-01 20:01:40 +00:00
|
|
|
encryptRSA :: ByteString -> TLSSt ByteString
|
2010-09-09 21:47:19 +00:00
|
|
|
encryptRSA content = do
|
2013-07-11 08:03:33 +00:00
|
|
|
rsakey <- fromJust "rsa public key" . hstRSAPublicKey . fromJust "handshake" . stHandshake <$> get
|
|
|
|
runRecordStateSt $ do
|
2012-03-27 07:57:51 +00:00
|
|
|
st <- get
|
2013-07-11 08:03:33 +00:00
|
|
|
let (v,rng') = withTLSRNG (stRandomGen st) (\g -> kxEncrypt g rsakey content)
|
|
|
|
put (st { stRandomGen = rng' })
|
|
|
|
case v of
|
|
|
|
Left err -> fail ("rsa encrypt failed: " ++ show err)
|
|
|
|
Right econtent -> return econtent
|
2010-09-09 21:47:19 +00:00
|
|
|
|
2012-12-30 15:31:13 +00:00
|
|
|
signRSA :: HashDescr -> ByteString -> TLSSt ByteString
|
2012-07-18 20:19:11 +00:00
|
|
|
signRSA hsh content = do
|
2013-07-11 08:03:33 +00:00
|
|
|
rsakey <- fromJust "rsa client private key" . hstRSAClientPrivateKey . fromJust "handshake" . stHandshake <$> get
|
|
|
|
runRecordStateSt $ do
|
2012-07-13 19:08:23 +00:00
|
|
|
st <- get
|
2012-12-05 08:19:40 +00:00
|
|
|
let (r, rng') = withTLSRNG (stRandomGen st) (\g -> kxSign g rsakey hsh content)
|
|
|
|
put (st { stRandomGen = rng' })
|
|
|
|
case r of
|
2013-07-11 08:03:33 +00:00
|
|
|
Left err -> fail ("rsa sign failed: " ++ show err)
|
|
|
|
Right econtent -> return econtent
|