1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 #include "apr_errno.h"
21 #include "apr_strings.h"
22 #include "apr_file_io.h"
23 #include "apr_thread_pool.h"
30 #if defined(WIN32) || defined(BEOS) || defined(NETWARE)
31 #define CRYPT_ALGO_SUPPORTED 0
33 #define CRYPT_ALGO_SUPPORTED 1
36 #if defined __GLIBC_PREREQ
37 #if __GLIBC_PREREQ(2,7)
38 #define GLIBCSHA_ALGO_SUPPORTED
42 #if CRYPT_ALGO_SUPPORTED
50 passwords and hashes created with Apache's htpasswd utility like this:
52 htpasswd -c -b passwords pass1 pass1
53 htpasswd -b passwords pass2 pass2
54 htpasswd -b passwords pass3 pass3
55 htpasswd -b passwords pass4 pass4
56 htpasswd -b passwords pass5 pass5
57 htpasswd -b passwords pass6 pass6
58 htpasswd -b passwords pass7 pass7
59 htpasswd -b passwords pass8 pass8
60 (insert Perl one-liner to convert to initializer :) )
62 {"pass1", "1fWDc9QWYCWrQ"},
63 {"pass2", "1fiGx3u7QoXaM"},
64 {"pass3", "1fzijMylTiwCs"},
65 {"pass4", "nHUYc8U2UOP7s"},
66 {"pass5", "nHpETGLGPwAmA"},
67 {"pass6", "nHbsbWmJ3uyhc"},
68 {"pass7", "nHQ3BbF0Y9vpI"},
69 {"pass8", "nHZA1rViSldQk"}
71 static int num_passwords = sizeof(passwords) / sizeof(passwords[0]);
73 static void test_crypt(abts_case *tc, void *data)
77 for (i = 0; i < num_passwords; i++) {
78 apr_assert_success(tc, "check for valid password",
79 apr_password_validate(passwords[i].password,
86 static void * APR_THREAD_FUNC testing_thread(apr_thread_t *thd,
92 for (i = 0; i < 100; i++) {
101 /* test for threadsafe crypt() */
102 static void test_threadsafe(abts_case *tc, void *data)
106 apr_thread_pool_t *thrp;
108 rv = apr_thread_pool_create(&thrp, NUM_THR/2, NUM_THR, p);
109 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
111 for (i = 0; i < NUM_THR; i++) {
112 rv = apr_thread_pool_push(thrp, testing_thread, tc, 0, NULL);
113 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
116 apr_thread_pool_destroy(thrp);
120 #endif /* CRYPT_ALGO_SUPPORTED */
122 static void test_shapass(abts_case *tc, void *data)
124 const char *pass = "hellojed";
125 const char *pass2 = "hellojed2";
128 apr_sha1_base64(pass, strlen(pass), hash);
130 apr_assert_success(tc, "SHA1 password validated",
131 apr_password_validate(pass, hash));
132 APR_ASSERT_FAILURE(tc, "wrong SHA1 password should not validate",
133 apr_password_validate(pass2, hash));
136 static void test_md5pass(abts_case *tc, void *data)
138 const char *pass = "hellojed", *salt = "sardine";
139 const char *pass2 = "hellojed2";
142 apr_md5_encode(pass, salt, hash, sizeof hash);
144 apr_assert_success(tc, "MD5 password validated",
145 apr_password_validate(pass, hash));
146 APR_ASSERT_FAILURE(tc, "wrong MD5 password should not validate",
147 apr_password_validate(pass2, hash));
150 #ifdef GLIBCSHA_ALGO_SUPPORTED
153 const char *password;
155 } glibc_sha_pws[] = {
157 { "secret1", "$5$0123456789abcdef$SFX.CooXBS8oXsbAPgU/UyiCodhrLQ19sBgvcA3Zh1D" },
158 { "secret2", "$5$rounds=100000$0123456789abcdef$dLXfO5m4d.xv8G66kpz2LyL0.Mi5wjLlH0m7rtgyhyB" },
160 { "secret3", "$6$0123456789abcdef$idOsOfoWwnCQkJm9hd2hxS4NnEs9nBA9poOFXsvtrYSoSHaOToCfyUoZwKe.ZCZnq7D95tGVoi2jxZZMyVwTL1" },
161 { "secret4", "$6$rounds=100000$0123456789abcdef$ZiAMjbeA.iIGTWxq2oks9Bvz9sfxaoGPgAtpwimPEwFwkSNMTK7lLwABzzldds/n4UgCQ16HqawPrCrePr4YX1" },
165 static void test_glibc_shapass(abts_case *tc, void *data)
168 while (glibc_sha_pws[i].password) {
169 apr_assert_success(tc, "check for valid glibc crypt-sha password",
170 apr_password_validate(glibc_sha_pws[i].password,
171 glibc_sha_pws[i].hash));
177 static void test_bcryptpass(abts_case *tc, void *data)
179 const char *pass = "hellojed";
180 const char *pass2 = "hellojed2";
181 unsigned char salt[] = "sardine_sardine";
183 const char *hash2 = "$2a$08$qipUJiI9fySUN38hcbz.lucXvAmtgowKOWYtB9y3CXyl6lTknruou";
184 const char *pass3 = "foobar";
186 apr_assert_success(tc, "bcrypt encode password",
187 apr_bcrypt_encode(pass, 5, salt, sizeof(salt), hash,
190 apr_assert_success(tc, "bcrypt password validated",
191 apr_password_validate(pass, hash));
192 APR_ASSERT_FAILURE(tc, "wrong bcrypt password should not validate",
193 apr_password_validate(pass2, hash));
194 apr_assert_success(tc, "bcrypt password validated",
195 apr_password_validate(pass3, hash2));
199 abts_suite *testpass(abts_suite *suite)
201 suite = ADD_SUITE(suite);
203 #if CRYPT_ALGO_SUPPORTED
204 abts_run_test(suite, test_crypt, NULL);
206 abts_run_test(suite, test_threadsafe, NULL);
208 #endif /* CRYPT_ALGO_SUPPORTED */
209 abts_run_test(suite, test_shapass, NULL);
210 abts_run_test(suite, test_md5pass, NULL);
211 abts_run_test(suite, test_bcryptpass, NULL);
212 #ifdef GLIBCSHA_ALGO_SUPPORTED
213 abts_run_test(suite, test_glibc_shapass, NULL);