Skip to content

Commit 02099d4

Browse files
committed
SRP6: review, refactor, fix issue #28
This includes only the relevant modules from `botan-bindings` and `botan-low`. In particular, we work around a bug in `srp6ServerSessionStep1` in `botan-low` where an exception is thrown from C code by `botan_srp6_server_session_step1` because it is called twice by `srp6ServerSessionStep1`. This is bug in the Botan C++ library, see randombit/botan#5112. The best we can do for now is to try to not trigger the exception. The work-around we introduce is to make sure we call the erroring function (`botan_srp6_server_session_step1`) only once. Previously, we were calling it once to "query" the size of output buffers, and then again with the correct sizes of output buffers. We can instead use `botan_srp6_group_size`/`srp6GroupSize` to determine the correct sizes of output buffers immediately. This is now done this way automatically where appropriate in `botan-low`, but not in `botan-bindings`. Moreover, users can still call `botan_srp6_server_session_step1`/`srp6ServerSessionStep1` twice if they want to, meaning that the exception can still be triggered. To warn against this, we make sure to include appropriate warnings in the Haddock documentation. The other smaller changes do not affect the functionality of the code, only documentation and such.
1 parent 2dd5921 commit 02099d4

File tree

5 files changed

+184
-149
lines changed

5 files changed

+184
-149
lines changed

botan-bindings/src/Botan/Bindings/SRP6.hs

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,9 @@ Maintainer : [email protected], [email protected]
88
Stability : experimental
99
Portability : POSIX
1010
11-
The library contains an implementation of the SRP6-a password
12-
authenticated key exchange protocol.
13-
14-
A SRP client provides what is called a SRP verifier to the server.
15-
This verifier is based on a password, but the password cannot be
16-
easily derived from the verifier (however brute force attacks are
17-
possible). Later, the client and server can perform an SRP exchange,
18-
which results in a shared secret key. This key can be used for
19-
mutual authentication and/or encryption.
20-
21-
SRP works in a discrete logarithm group. Special parameter sets for
22-
SRP6 are defined, denoted in the library as “modp/srp/<size>”, for
23-
example “modp/srp/2048”.
24-
25-
Warning
26-
27-
While knowledge of the verifier does not easily allow an attacker to
28-
get the raw password, they could still use the verifier to impersonate
29-
the server to the client, so verifiers should be protected as carefully
30-
as a plaintext password would be.
11+
This module is based on the [Secure Remote
12+
Password](https://botan.randombit.net/handbook/api_ref/srp.html) section of the
13+
C++ API reference.
3114
-}
3215

3316
{-# LANGUAGE CApiFFI #-}
@@ -50,12 +33,15 @@ import Botan.Bindings.RNG
5033
-- | Opaque SRP-6 server session struct
5134
data {-# CTYPE "botan/ffi.h" "struct botan_srp6_server_session_struct" #-} BotanSRP6ServerSessionStruct
5235

53-
-- | Botan SRP-6 server session object
36+
-- | SRP-6 server session object
5437
newtype {-# CTYPE "botan/ffi.h" "botan_srp6_server_session_t" #-} BotanSRP6ServerSession
5538
= MkBotanSRP6ServerSession { runBotanSRP6ServerSession :: Ptr BotanSRP6ServerSessionStruct }
5639
deriving newtype (Eq, Ord, Storable)
5740

5841
-- | Frees all resources of the SRP-6 server session object
42+
--
43+
-- NOTE: this a binding to the /address/ of the
44+
-- @botan_srp6_server_session_destroy@ C function.
5945
foreign import capi safe "botan/ffi.h &botan_srp6_server_session_destroy"
6046
botan_srp6_server_session_destroy
6147
:: FinalizerPtr BotanSRP6ServerSessionStruct
@@ -66,7 +52,14 @@ foreign import capi safe "botan/ffi.h botan_srp6_server_session_init"
6652
:: Ptr BotanSRP6ServerSession -- ^ __srp6__: SRP-6 server session object
6753
-> IO CInt
6854

69-
-- | SRP-6 Server side step 1: Generate a server B-value
55+
-- | SRP-6 Server side step 1
56+
--
57+
-- NOTE: this function should be not be invoked twice on the same server
58+
-- session. Regardless of the result of the first invocation, the second
59+
-- invocation will result in an error. See
60+
-- https://github.com/randombit/botan/issues/5112 for more information. If a
61+
-- second invocation can not be prevented, try it on a newly initialised server
62+
-- session instead.
7063
foreign import capi safe "botan/ffi.h botan_srp6_server_session_step1"
7164
botan_srp6_server_session_step1
7265
:: BotanSRP6ServerSession -- ^ __srp6__: SRP-6 server session object
@@ -79,7 +72,7 @@ foreign import capi safe "botan/ffi.h botan_srp6_server_session_step1"
7972
-> Ptr CSize -- ^ __B_pub_len__: SRP-6 B value length
8073
-> IO CInt -- ^ 0 on success, negative on failure
8174

82-
-- | SRP-6 Server side step 2: Generate the server shared key
75+
-- | SRP-6 Server side step 2
8376
foreign import capi safe "botan/ffi.h botan_srp6_server_session_step2"
8477
botan_srp6_server_session_step2
8578
:: BotanSRP6ServerSession -- ^ __srp6__: SRP-6 server session object
@@ -89,7 +82,7 @@ foreign import capi safe "botan/ffi.h botan_srp6_server_session_step2"
8982
-> Ptr CSize -- ^ __key_len__: symmetric key length
9083
-> IO CInt -- ^ 0 on success, negative on failure
9184

92-
-- | SRP-6 Client side step 1: Generate a new SRP-6 verifier
85+
-- | Generate a new SRP-6 verifier
9386
foreign import capi safe "botan/ffi.h botan_srp6_generate_verifier"
9487
botan_srp6_generate_verifier
9588
:: ConstPtr CChar -- ^ __identifier__: a username or other client identifier
@@ -102,7 +95,7 @@ foreign import capi safe "botan/ffi.h botan_srp6_generate_verifier"
10295
-> Ptr CSize -- ^ __verifier_len__: SRP-6 verifier value length
10396
-> IO CInt -- ^ 0 on success, negative on failure
10497

105-
-- | SRP6a Client side step 2: Generate a client A-value and the client shared key
98+
-- | SRP6a Client side
10699
foreign import capi safe "botan/ffi.h botan_srp6_client_agree"
107100
botan_srp6_client_agree
108101
:: ConstPtr CChar -- ^ __username__: the username we are attempting login for
@@ -111,7 +104,7 @@ foreign import capi safe "botan/ffi.h botan_srp6_client_agree"
111104
-> ConstPtr CChar -- ^ __hash_id__: specifies a secure hash function
112105
-> ConstPtr Word8 -- ^ __salt[]__: is the salt value sent by the server
113106
-> CSize -- ^ __salt_len__: the length of salt
114-
-> ConstPtr Word8 -- ^ __uint8_t__: B[] is the server's public value
107+
-> ConstPtr Word8 -- ^ __B[]__: is the server's public value
115108
-> CSize -- ^ __B_len__: is the server's public value length
116109
-> BotanRNG -- ^ __rng_obj__: is a random number generator object
117110
-> Ptr Word8 -- ^ __A[]__: out buffer to store the SRP-6 A value
@@ -121,6 +114,11 @@ foreign import capi safe "botan/ffi.h botan_srp6_client_agree"
121114
-> IO CInt -- ^ 0 on success, negative on failure
122115

123116
-- | Return the size, in bytes, of the prime associated with group_id
117+
--
118+
-- This function can be used to determine the size of output buffers for
119+
-- generated keys in the SRP6 algorithm. Such buffers need to be allocated
120+
-- before calling SRP6 functions. An example of such a buffer is the
121+
-- @verifier[]@ buffer in the 'botan_srp6_generate_verifier' function.
124122
foreign import capi safe "botan/ffi.h botan_srp6_group_size"
125123
botan_srp6_group_size
126124
:: ConstPtr CChar -- ^ __group_id__

0 commit comments

Comments
 (0)