@@ -12,20 +12,15 @@ module Simplex.Messaging.Notifications.Transport
1212 VersionRangeNTF ,
1313 pattern VersionNTF ,
1414 THandleNTF ,
15- invalidReasonNTFVersion ,
1615 supportedClientNTFVRange ,
1716 supportedServerNTFVRange ,
1817 alpnSupportedNTFHandshakes ,
1918 ntfServerHandshake ,
2019 ntfClientHandshake ,
2120 ) where
2221
23- import Control.Monad (forM )
2422import Control.Monad.Except
2523import Control.Monad.Trans.Except
26- import Data.Attoparsec.ByteString.Char8 (Parser )
27- import Data.ByteString.Char8 (ByteString )
28- import qualified Data.ByteString.Char8 as B
2924import Data.Word (Word16 )
3025import qualified Data.X509 as X
3126import qualified Simplex.Messaging.Crypto as C
@@ -50,13 +45,10 @@ pattern VersionNTF :: Word16 -> VersionNTF
5045pattern VersionNTF v = Version v
5146
5247initialNTFVersion :: VersionNTF
53- initialNTFVersion = VersionNTF 1
48+ initialNTFVersion = VersionNTF 3
5449
55- authBatchCmdsNTFVersion :: VersionNTF
56- authBatchCmdsNTFVersion = VersionNTF 2
57-
58- invalidReasonNTFVersion :: VersionNTF
59- invalidReasonNTFVersion = VersionNTF 3
50+ _invalidReasonNTFVersion :: VersionNTF
51+ _invalidReasonNTFVersion = VersionNTF 3
6052
6153currentClientNTFVersion :: VersionNTF
6254currentClientNTFVersion = VersionNTF 3
@@ -67,9 +59,6 @@ currentServerNTFVersion = VersionNTF 3
6759supportedClientNTFVRange :: VersionRangeNTF
6860supportedClientNTFVRange = mkVersionRange initialNTFVersion currentClientNTFVersion
6961
70- legacyServerNTFVRange :: VersionRangeNTF
71- legacyServerNTFVRange = mkVersionRange initialNTFVersion initialNTFVersion
72-
7362supportedServerNTFVRange :: VersionRangeNTF
7463supportedServerNTFVRange = mkVersionRange initialNTFVersion currentServerNTFVersion
7564
@@ -82,7 +71,7 @@ data NtfServerHandshake = NtfServerHandshake
8271 { ntfVersionRange :: VersionRangeNTF ,
8372 sessionId :: SessionId ,
8473 -- pub key to agree shared secrets for command authorization and entity ID encryption.
85- authPubKey :: Maybe ( X. SignedExact X. PubKey)
74+ authPubKey :: X. SignedExact X. PubKey
8675 }
8776
8877data NtfClientHandshake = NtfClientHandshake
@@ -94,25 +83,13 @@ data NtfClientHandshake = NtfClientHandshake
9483
9584instance Encoding NtfServerHandshake where
9685 smpEncode NtfServerHandshake {ntfVersionRange, sessionId, authPubKey} =
97- B. concat
98- [ smpEncode (ntfVersionRange, sessionId),
99- encodeAuthEncryptCmds (maxVersion ntfVersionRange) $ C. SignedObject <$> authPubKey
100- ]
86+ smpEncode (ntfVersionRange, sessionId, C. SignedObject authPubKey)
10187
10288 smpP = do
10389 (ntfVersionRange, sessionId) <- smpP
104- -- TODO drop SMP v6: remove special parser and make key non-optional
105- authPubKey <- authEncryptCmdsP (maxVersion ntfVersionRange) $ C. getSignedExact <$> smpP
90+ authPubKey <- C. getSignedExact <$> smpP
10691 pure NtfServerHandshake {ntfVersionRange, sessionId, authPubKey}
10792
108- encodeAuthEncryptCmds :: Encoding a => VersionNTF -> Maybe a -> ByteString
109- encodeAuthEncryptCmds v k
110- | v >= authBatchCmdsNTFVersion = maybe " " smpEncode k
111- | otherwise = " "
112-
113- authEncryptCmdsP :: VersionNTF -> Parser a -> Parser (Maybe a )
114- authEncryptCmdsP v p = if v >= authBatchCmdsNTFVersion then Just <$> p else pure Nothing
115-
11693instance Encoding NtfClientHandshake where
11794 smpEncode NtfClientHandshake {ntfVersion, keyHash} =
11895 smpEncode (ntfVersion, keyHash)
@@ -122,11 +99,10 @@ instance Encoding NtfClientHandshake where
12299
123100-- | Notifcations server transport handshake.
124101ntfServerHandshake :: forall c . Transport c => C. APrivateSignKey -> c 'TServer -> C. KeyPairX25519 -> C. KeyHash -> VersionRangeNTF -> ExceptT TransportError IO (THandleNTF c 'TServer)
125- ntfServerHandshake serverSignKey c (k, pk) kh ntfVRange = do
102+ ntfServerHandshake serverSignKey c (k, pk) kh ntfVersionRange = do
126103 let th@ THandle {params = THandleParams {sessionId}} = ntfTHandle c
127- let sk = C. signX509 serverSignKey $ C. publicToX509 k
128- let ntfVersionRange = maybe legacyServerNTFVRange (const ntfVRange) $ getSessionALPN c
129- sendHandshake th $ NtfServerHandshake {sessionId, ntfVersionRange, authPubKey = Just sk}
104+ authPubKey = C. signX509 serverSignKey $ C. publicToX509 k
105+ sendHandshake th $ NtfServerHandshake {sessionId, ntfVersionRange, authPubKey}
130106 getHandshake th >>= \ case
131107 NtfClientHandshake {ntfVersion = v, keyHash}
132108 | keyHash /= kh ->
@@ -140,36 +116,35 @@ ntfServerHandshake serverSignKey c (k, pk) kh ntfVRange = do
140116ntfClientHandshake :: forall c . Transport c => c 'TClient -> C. KeyHash -> VersionRangeNTF -> Bool -> Maybe (ServiceCredentials , C. KeyPairEd25519 ) -> ExceptT TransportError IO (THandleNTF c 'TClient)
141117ntfClientHandshake c keyHash ntfVRange _proxyServer _serviceKeys = do
142118 let th@ THandle {params = THandleParams {sessionId}} = ntfTHandle c
143- NtfServerHandshake {sessionId = sessId, ntfVersionRange, authPubKey = sk' } <- getHandshake th
119+ NtfServerHandshake {sessionId = sessId, ntfVersionRange, authPubKey} <- getHandshake th
144120 if sessionId /= sessId
145121 then throwE TEBadSession
146122 else case ntfVersionRange `compatibleVRange` ntfVRange of
147123 Just (Compatible vr) -> do
148- ck_ <- forM sk' $ \ signedKey -> liftEitherWith (const $ TEHandshake BAD_AUTH ) $ do
124+ ck <- liftEitherWith (const $ TEHandshake BAD_AUTH ) $ do
149125 serverKey <- getServerVerifyKey c
150- pubKey <- C. verifyX509 serverKey signedKey
151- (,CertChainPubKey (getPeerCertChain c) signedKey ) <$> C. x509ToPublic' pubKey
126+ pubKey <- C. verifyX509 serverKey authPubKey
127+ (,CertChainPubKey (getPeerCertChain c) authPubKey ) <$> C. x509ToPublic' pubKey
152128 let v = maxVersion vr
153129 sendHandshake th $ NtfClientHandshake {ntfVersion = v, keyHash}
154- pure $ ntfThHandleClient th v vr ck_
130+ pure $ ntfThHandleClient th v vr ck
155131 Nothing -> throwE TEVersion
156132
157133ntfThHandleServer :: forall c . THandleNTF c 'TServer -> VersionNTF -> VersionRangeNTF -> C. PrivateKeyX25519 -> THandleNTF c 'TServer
158134ntfThHandleServer th v vr pk =
159135 let thAuth = THAuthServer {serverPrivKey = pk, peerClientService = Nothing , sessSecret' = Nothing }
160136 in ntfThHandle_ th v vr (Just thAuth)
161137
162- ntfThHandleClient :: forall c . THandleNTF c 'TClient -> VersionNTF -> VersionRangeNTF -> Maybe (C. PublicKeyX25519 , CertChainPubKey ) -> THandleNTF c 'TClient
138+ ntfThHandleClient :: forall c . THandleNTF c 'TClient -> VersionNTF -> VersionRangeNTF -> (C. PublicKeyX25519 , CertChainPubKey ) -> THandleNTF c 'TClient
163139ntfThHandleClient th v vr ck_ =
164- let thAuth = clientTHParams <$> ck_
140+ let thAuth = Just $ clientTHParams ck_
165141 clientTHParams (k, ck) = THAuthClient {peerServerPubKey = k, peerServerCertKey = ck, clientService = Nothing , sessSecret = Nothing }
166142 in ntfThHandle_ th v vr thAuth
167143
168144ntfThHandle_ :: forall c p . THandleNTF c p -> VersionNTF -> VersionRangeNTF -> Maybe (THandleAuth p ) -> THandleNTF c p
169145ntfThHandle_ th@ THandle {params} v vr thAuth =
170146 -- TODO drop SMP v6: make thAuth non-optional
171- let v3 = v >= authBatchCmdsNTFVersion
172- params' = params {thVersion = v, thServerVRange = vr, thAuth, implySessId = v3, batch = v3}
147+ let params' = params {thVersion = v, thServerVRange = vr, thAuth}
173148 in (th :: THandleNTF c p ) {params = params'}
174149
175150ntfTHandle :: Transport c => c p -> THandleNTF c p
@@ -183,8 +158,7 @@ ntfTHandle c = THandle {connection = c, params}
183158 thVersion = v,
184159 thServerVRange = versionToRange v,
185160 thAuth = Nothing ,
186- implySessId = False ,
161+ implySessId = True ,
187162 encryptBlock = Nothing ,
188- batch = False ,
189163 serviceAuth = False
190164 }
0 commit comments