libpurple/protocols/jabber/tests/test_jabber_scram.c

Fri, 01 May 2020 04:42:52 -0500

author
Gary Kramlich <grim@reaperworld.com>
date
Fri, 01 May 2020 04:42:52 -0500
changeset 40358
e6fe6fc1f516
parent 40283
99ac77e41427
child 42057
f72f1db0f42b
permissions
-rw-r--r--

move all protocols, purple plugins, and purple tests to use purple.h instead of including files individually

28863
b4e8c372e06b Fix the Hi() function and actually 'mtn add' the test file.
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
1 #include <string.h>
b4e8c372e06b Fix the Hi() function and actually 'mtn add' the test file.
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
2
40358
e6fe6fc1f516 move all protocols, purple plugins, and purple tests to use purple.h instead of including files individually
Gary Kramlich <grim@reaperworld.com>
parents: 40283
diff changeset
3 #include <purple.h>
e6fe6fc1f516 move all protocols, purple plugins, and purple tests to use purple.h instead of including files individually
Gary Kramlich <grim@reaperworld.com>
parents: 40283
diff changeset
4
37610
8e7c187310d5 Move the scram tests to gtester
Gary Kramlich <grim@reaperworld.com>
parents: 37605
diff changeset
5 #include "protocols/jabber/auth_scram.h"
8e7c187310d5 Move the scram tests to gtester
Gary Kramlich <grim@reaperworld.com>
parents: 37605
diff changeset
6 #include "protocols/jabber/jutil.h"
28863
b4e8c372e06b Fix the Hi() function and actually 'mtn add' the test file.
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
7
38306
3751be7f48c3 jabber: Port to use GHmac instead of PurpleHMACCipher
Mike Ruprecht <cmaiku@gmail.com>
parents: 37610
diff changeset
8 static JabberScramHash sha1_mech = { "-SHA-1", G_CHECKSUM_SHA1 };
29081
ce668db953aa jabber: Clean up the SCRAM code a little.
Paul Aurich <darkrain42@pidgin.im>
parents: 29027
diff changeset
9
28863
b4e8c372e06b Fix the Hi() function and actually 'mtn add' the test file.
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
10 #define assert_pbkdf2_equal(password, salt, count, expected) { \
b4e8c372e06b Fix the Hi() function and actually 'mtn add' the test file.
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
11 GString *p = g_string_new(password); \
b4e8c372e06b Fix the Hi() function and actually 'mtn add' the test file.
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
12 GString *s = g_string_new(salt); \
29081
ce668db953aa jabber: Clean up the SCRAM code a little.
Paul Aurich <darkrain42@pidgin.im>
parents: 29027
diff changeset
13 guchar *result = jabber_scram_hi(&sha1_mech, p, s, count); \
37610
8e7c187310d5 Move the scram tests to gtester
Gary Kramlich <grim@reaperworld.com>
parents: 37605
diff changeset
14 g_assert_nonnull(result); \
8e7c187310d5 Move the scram tests to gtester
Gary Kramlich <grim@reaperworld.com>
parents: 37605
diff changeset
15 g_assert_cmpmem(result, 20, expected, 20); \
28863
b4e8c372e06b Fix the Hi() function and actually 'mtn add' the test file.
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
16 g_string_free(s, TRUE); \
b4e8c372e06b Fix the Hi() function and actually 'mtn add' the test file.
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
17 g_string_free(p, TRUE); \
b4e8c372e06b Fix the Hi() function and actually 'mtn add' the test file.
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
18 }
b4e8c372e06b Fix the Hi() function and actually 'mtn add' the test file.
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
19
37610
8e7c187310d5 Move the scram tests to gtester
Gary Kramlich <grim@reaperworld.com>
parents: 37605
diff changeset
20 static void
8e7c187310d5 Move the scram tests to gtester
Gary Kramlich <grim@reaperworld.com>
parents: 37605
diff changeset
21 test_jabber_scram_pbkdf2(void) {
28863
b4e8c372e06b Fix the Hi() function and actually 'mtn add' the test file.
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
22 assert_pbkdf2_equal("password", "salt", 1, "\x0c\x60\xc8\x0f\x96\x1f\x0e\x71\xf3\xa9\xb5\x24\xaf\x60\x12\x06\x2f\xe0\x37\xa6");
b4e8c372e06b Fix the Hi() function and actually 'mtn add' the test file.
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
23 assert_pbkdf2_equal("password", "salt", 2, "\xea\x6c\x01\x4d\xc7\x2d\x6f\x8c\xcd\x1e\xd9\x2a\xce\x1d\x41\xf0\xd8\xde\x89\x57");
b4e8c372e06b Fix the Hi() function and actually 'mtn add' the test file.
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
24 assert_pbkdf2_equal("password", "salt", 4096, "\x4b\x00\x79\x01\xb7\x65\x48\x9a\xbe\xad\x49\xd9\x26\xf7\x21\xd0\x65\xa4\x29\xc1");
38856
0b799504db33 Disable the super long jabber scram test
Gary Kramlich <grim@reaperworld.com>
parents: 38854
diff changeset
25 /* This test is insane and takes forever, so it's disabled */
0b799504db33 Disable the super long jabber scram test
Gary Kramlich <grim@reaperworld.com>
parents: 38854
diff changeset
26 /*
28863
b4e8c372e06b Fix the Hi() function and actually 'mtn add' the test file.
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
27 assert_pbkdf2_equal("password", "salt", 16777216, "\xee\xfe\x3d\x61\xcd\x4d\xa4\xe4\xe9\x94\x5b\x3d\x6b\xa2\x15\x8c\x26\x34\xe9\x84");
38856
0b799504db33 Disable the super long jabber scram test
Gary Kramlich <grim@reaperworld.com>
parents: 38854
diff changeset
28 */
28863
b4e8c372e06b Fix the Hi() function and actually 'mtn add' the test file.
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
29 }
b4e8c372e06b Fix the Hi() function and actually 'mtn add' the test file.
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
30
37610
8e7c187310d5 Move the scram tests to gtester
Gary Kramlich <grim@reaperworld.com>
parents: 37605
diff changeset
31 static void
8e7c187310d5 Move the scram tests to gtester
Gary Kramlich <grim@reaperworld.com>
parents: 37605
diff changeset
32 test_jabber_scram_proofs(void) {
28866
e3d867ce000b jabber: Complete (though untested) SCRAM implementation.
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
33 JabberScramData *data = g_new0(JabberScramData, 1);
e3d867ce000b jabber: Complete (though untested) SCRAM implementation.
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
34 gboolean ret;
e3d867ce000b jabber: Complete (though untested) SCRAM implementation.
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
35 GString *salt;
e3d867ce000b jabber: Complete (though untested) SCRAM implementation.
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
36 const char *client_proof;
e3d867ce000b jabber: Complete (though untested) SCRAM implementation.
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
37 /* const char *server_signature; */
e3d867ce000b jabber: Complete (though untested) SCRAM implementation.
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
38
29081
ce668db953aa jabber: Clean up the SCRAM code a little.
Paul Aurich <darkrain42@pidgin.im>
parents: 29027
diff changeset
39 data->hash = &sha1_mech;
29027
6af29b140195 jabber: Add SASLprep and the username substitution called for in draft-ietf-sasl-scram-10 5.1.
Paul Aurich <darkrain42@pidgin.im>
parents: 28926
diff changeset
40 data->password = g_strdup("password");
28866
e3d867ce000b jabber: Complete (though untested) SCRAM implementation.
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
41 data->auth_message = g_string_new("n=username@jabber.org,r=8jLxB5515dhFxBil5A0xSXMH,"
e3d867ce000b jabber: Complete (though untested) SCRAM implementation.
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
42 "r=8jLxB5515dhFxBil5A0xSXMHabc,s=c2FsdA==,i=1,"
e3d867ce000b jabber: Complete (though untested) SCRAM implementation.
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
43 "c=biws,r=8jLxB5515dhFxBil5A0xSXMHabc");
e3d867ce000b jabber: Complete (though untested) SCRAM implementation.
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
44 client_proof = "\x48\x61\x30\xa5\x61\x0b\xae\xb9\xe4\x11\xa8\xfd\xa5\xcd\x34\x1d\x8a\x3c\x28\x17";
e3d867ce000b jabber: Complete (though untested) SCRAM implementation.
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
45
e3d867ce000b jabber: Complete (though untested) SCRAM implementation.
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
46 salt = g_string_new("salt");
28868
7415cb6c4587 jabber: Handle the case where the server success-with-data is sent as a challenge/response pair.
Paul Aurich <darkrain42@pidgin.im>
parents: 28866
diff changeset
47 ret = jabber_scram_calc_proofs(data, salt, 1);
37610
8e7c187310d5 Move the scram tests to gtester
Gary Kramlich <grim@reaperworld.com>
parents: 37605
diff changeset
48 g_assert_true(ret);
28866
e3d867ce000b jabber: Complete (though untested) SCRAM implementation.
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
49
37610
8e7c187310d5 Move the scram tests to gtester
Gary Kramlich <grim@reaperworld.com>
parents: 37605
diff changeset
50 g_assert_cmpmem(client_proof, 20, data->client_proof->str, 20);
28866
e3d867ce000b jabber: Complete (though untested) SCRAM implementation.
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
51 g_string_free(salt, TRUE);
29027
6af29b140195 jabber: Add SASLprep and the username substitution called for in draft-ietf-sasl-scram-10 5.1.
Paul Aurich <darkrain42@pidgin.im>
parents: 28926
diff changeset
52
6af29b140195 jabber: Add SASLprep and the username substitution called for in draft-ietf-sasl-scram-10 5.1.
Paul Aurich <darkrain42@pidgin.im>
parents: 28926
diff changeset
53 jabber_scram_data_destroy(data);
28866
e3d867ce000b jabber: Complete (though untested) SCRAM implementation.
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
54 }
e3d867ce000b jabber: Complete (though untested) SCRAM implementation.
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
55
29083
cc7d90ec7a52 Add a second SCRAM test with one of the suggested test characters from the I-D.
Paul Aurich <darkrain42@pidgin.im>
parents: 29081
diff changeset
56 #define assert_successful_exchange(pw, nonce, start_data, challenge1, response1, success) { \
cc7d90ec7a52 Add a second SCRAM test with one of the suggested test characters from the I-D.
Paul Aurich <darkrain42@pidgin.im>
parents: 29081
diff changeset
57 JabberScramData *data = g_new0(JabberScramData, 1); \
cc7d90ec7a52 Add a second SCRAM test with one of the suggested test characters from the I-D.
Paul Aurich <darkrain42@pidgin.im>
parents: 29081
diff changeset
58 gboolean ret; \
cc7d90ec7a52 Add a second SCRAM test with one of the suggested test characters from the I-D.
Paul Aurich <darkrain42@pidgin.im>
parents: 29081
diff changeset
59 gchar *out; \
cc7d90ec7a52 Add a second SCRAM test with one of the suggested test characters from the I-D.
Paul Aurich <darkrain42@pidgin.im>
parents: 29081
diff changeset
60 \
cc7d90ec7a52 Add a second SCRAM test with one of the suggested test characters from the I-D.
Paul Aurich <darkrain42@pidgin.im>
parents: 29081
diff changeset
61 data->step = 1; \
cc7d90ec7a52 Add a second SCRAM test with one of the suggested test characters from the I-D.
Paul Aurich <darkrain42@pidgin.im>
parents: 29081
diff changeset
62 data->hash = &sha1_mech; \
cc7d90ec7a52 Add a second SCRAM test with one of the suggested test characters from the I-D.
Paul Aurich <darkrain42@pidgin.im>
parents: 29081
diff changeset
63 data->password = jabber_saslprep(pw); \
37610
8e7c187310d5 Move the scram tests to gtester
Gary Kramlich <grim@reaperworld.com>
parents: 37605
diff changeset
64 g_assert_nonnull(data->password); \
29083
cc7d90ec7a52 Add a second SCRAM test with one of the suggested test characters from the I-D.
Paul Aurich <darkrain42@pidgin.im>
parents: 29081
diff changeset
65 data->cnonce = g_strdup(nonce); \
cc7d90ec7a52 Add a second SCRAM test with one of the suggested test characters from the I-D.
Paul Aurich <darkrain42@pidgin.im>
parents: 29081
diff changeset
66 data->auth_message = g_string_new(start_data); \
cc7d90ec7a52 Add a second SCRAM test with one of the suggested test characters from the I-D.
Paul Aurich <darkrain42@pidgin.im>
parents: 29081
diff changeset
67 \
cc7d90ec7a52 Add a second SCRAM test with one of the suggested test characters from the I-D.
Paul Aurich <darkrain42@pidgin.im>
parents: 29081
diff changeset
68 ret = jabber_scram_feed_parser(data, challenge1, &out); \
37610
8e7c187310d5 Move the scram tests to gtester
Gary Kramlich <grim@reaperworld.com>
parents: 37605
diff changeset
69 g_assert_true(ret); \
8e7c187310d5 Move the scram tests to gtester
Gary Kramlich <grim@reaperworld.com>
parents: 37605
diff changeset
70 g_assert_cmpstr(response1, ==, out); \
29083
cc7d90ec7a52 Add a second SCRAM test with one of the suggested test characters from the I-D.
Paul Aurich <darkrain42@pidgin.im>
parents: 29081
diff changeset
71 g_free(out); \
cc7d90ec7a52 Add a second SCRAM test with one of the suggested test characters from the I-D.
Paul Aurich <darkrain42@pidgin.im>
parents: 29081
diff changeset
72 \
cc7d90ec7a52 Add a second SCRAM test with one of the suggested test characters from the I-D.
Paul Aurich <darkrain42@pidgin.im>
parents: 29081
diff changeset
73 data->step = 2; \
cc7d90ec7a52 Add a second SCRAM test with one of the suggested test characters from the I-D.
Paul Aurich <darkrain42@pidgin.im>
parents: 29081
diff changeset
74 ret = jabber_scram_feed_parser(data, success, &out); \
37610
8e7c187310d5 Move the scram tests to gtester
Gary Kramlich <grim@reaperworld.com>
parents: 37605
diff changeset
75 g_assert_true(ret); \
8e7c187310d5 Move the scram tests to gtester
Gary Kramlich <grim@reaperworld.com>
parents: 37605
diff changeset
76 g_assert_null(out); \
29083
cc7d90ec7a52 Add a second SCRAM test with one of the suggested test characters from the I-D.
Paul Aurich <darkrain42@pidgin.im>
parents: 29081
diff changeset
77 \
cc7d90ec7a52 Add a second SCRAM test with one of the suggested test characters from the I-D.
Paul Aurich <darkrain42@pidgin.im>
parents: 29081
diff changeset
78 jabber_scram_data_destroy(data); \
cc7d90ec7a52 Add a second SCRAM test with one of the suggested test characters from the I-D.
Paul Aurich <darkrain42@pidgin.im>
parents: 29081
diff changeset
79 }
cc7d90ec7a52 Add a second SCRAM test with one of the suggested test characters from the I-D.
Paul Aurich <darkrain42@pidgin.im>
parents: 29081
diff changeset
80
37610
8e7c187310d5 Move the scram tests to gtester
Gary Kramlich <grim@reaperworld.com>
parents: 37605
diff changeset
81 static void
8e7c187310d5 Move the scram tests to gtester
Gary Kramlich <grim@reaperworld.com>
parents: 37605
diff changeset
82 test_jabber_scram_exchange(void) {
29083
cc7d90ec7a52 Add a second SCRAM test with one of the suggested test characters from the I-D.
Paul Aurich <darkrain42@pidgin.im>
parents: 29081
diff changeset
83 assert_successful_exchange("password", "H7yDYKAWBCrM2Fa5SxGa4iez",
cc7d90ec7a52 Add a second SCRAM test with one of the suggested test characters from the I-D.
Paul Aurich <darkrain42@pidgin.im>
parents: 29081
diff changeset
84 "n=paul,r=H7yDYKAWBCrM2Fa5SxGa4iez",
cc7d90ec7a52 Add a second SCRAM test with one of the suggested test characters from the I-D.
Paul Aurich <darkrain42@pidgin.im>
parents: 29081
diff changeset
85 "r=H7yDYKAWBCrM2Fa5SxGa4iezFPVDPpDUcGxPkH3RzP,s=3rXeErP/os7jUNqU,i=4096",
cc7d90ec7a52 Add a second SCRAM test with one of the suggested test characters from the I-D.
Paul Aurich <darkrain42@pidgin.im>
parents: 29081
diff changeset
86 "c=biws,r=H7yDYKAWBCrM2Fa5SxGa4iezFPVDPpDUcGxPkH3RzP,p=pXkak78EuwwOEwk2/h/OzD7NkEI=",
cc7d90ec7a52 Add a second SCRAM test with one of the suggested test characters from the I-D.
Paul Aurich <darkrain42@pidgin.im>
parents: 29081
diff changeset
87 "v=ldX4EBNnOgDnNTOCmbSfBHAUCOs=");
28866
e3d867ce000b jabber: Complete (though untested) SCRAM implementation.
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
88
29083
cc7d90ec7a52 Add a second SCRAM test with one of the suggested test characters from the I-D.
Paul Aurich <darkrain42@pidgin.im>
parents: 29081
diff changeset
89 assert_successful_exchange("pass½word", "GNb2HsNI7VnTv8ABsE5AnY8W",
cc7d90ec7a52 Add a second SCRAM test with one of the suggested test characters from the I-D.
Paul Aurich <darkrain42@pidgin.im>
parents: 29081
diff changeset
90 "n=paul,r=GNb2HsNI7VnTv8ABsE5AnY8W",
cc7d90ec7a52 Add a second SCRAM test with one of the suggested test characters from the I-D.
Paul Aurich <darkrain42@pidgin.im>
parents: 29081
diff changeset
91 "r=GNb2HsNI7VnTv8ABsE5AnY8W/w/I3eRKM0I7jxFWOH,s=ysAriUjPzFqOXnMQ,i=4096",
cc7d90ec7a52 Add a second SCRAM test with one of the suggested test characters from the I-D.
Paul Aurich <darkrain42@pidgin.im>
parents: 29081
diff changeset
92 "c=biws,r=GNb2HsNI7VnTv8ABsE5AnY8W/w/I3eRKM0I7jxFWOH,p=n/CtgdWjOYnLQ4m9Na+wPn9D2uY=",
cc7d90ec7a52 Add a second SCRAM test with one of the suggested test characters from the I-D.
Paul Aurich <darkrain42@pidgin.im>
parents: 29081
diff changeset
93 "v=4TkZwKWy6JHNmrUbU2+IdAaXtos=");
28866
e3d867ce000b jabber: Complete (though untested) SCRAM implementation.
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
94 }
e3d867ce000b jabber: Complete (though untested) SCRAM implementation.
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
95
37610
8e7c187310d5 Move the scram tests to gtester
Gary Kramlich <grim@reaperworld.com>
parents: 37605
diff changeset
96 gint
8e7c187310d5 Move the scram tests to gtester
Gary Kramlich <grim@reaperworld.com>
parents: 37605
diff changeset
97 main(gint argc, gchar **argv) {
8e7c187310d5 Move the scram tests to gtester
Gary Kramlich <grim@reaperworld.com>
parents: 37605
diff changeset
98 g_test_init(&argc, &argv, NULL);
28863
b4e8c372e06b Fix the Hi() function and actually 'mtn add' the test file.
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
99
37610
8e7c187310d5 Move the scram tests to gtester
Gary Kramlich <grim@reaperworld.com>
parents: 37605
diff changeset
100 g_test_add_func("/jabber/scram/pbkdf2",
8e7c187310d5 Move the scram tests to gtester
Gary Kramlich <grim@reaperworld.com>
parents: 37605
diff changeset
101 test_jabber_scram_pbkdf2);
8e7c187310d5 Move the scram tests to gtester
Gary Kramlich <grim@reaperworld.com>
parents: 37605
diff changeset
102 g_test_add_func("/jabber/scram/proofs",
8e7c187310d5 Move the scram tests to gtester
Gary Kramlich <grim@reaperworld.com>
parents: 37605
diff changeset
103 test_jabber_scram_proofs);
8e7c187310d5 Move the scram tests to gtester
Gary Kramlich <grim@reaperworld.com>
parents: 37605
diff changeset
104 g_test_add_func("/jabber/scram/exchange",
8e7c187310d5 Move the scram tests to gtester
Gary Kramlich <grim@reaperworld.com>
parents: 37605
diff changeset
105 test_jabber_scram_exchange);
28863
b4e8c372e06b Fix the Hi() function and actually 'mtn add' the test file.
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
106
37610
8e7c187310d5 Move the scram tests to gtester
Gary Kramlich <grim@reaperworld.com>
parents: 37605
diff changeset
107 return g_test_run();
28863
b4e8c372e06b Fix the Hi() function and actually 'mtn add' the test file.
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
108 }

mercurial