553f11a8f542196fd02498006338dd33c64045c3fuankg/* Licensed to the Apache Software Foundation (ASF) under one or more
553f11a8f542196fd02498006338dd33c64045c3fuankg * contributor license agreements. See the NOTICE file distributed with
553f11a8f542196fd02498006338dd33c64045c3fuankg * this work for additional information regarding copyright ownership.
553f11a8f542196fd02498006338dd33c64045c3fuankg * The ASF licenses this file to You under the Apache License, Version 2.0
553f11a8f542196fd02498006338dd33c64045c3fuankg * (the "License"); you may not use this file except in compliance with
553f11a8f542196fd02498006338dd33c64045c3fuankg * the License. You may obtain a copy of the License at
553f11a8f542196fd02498006338dd33c64045c3fuankg * Unless required by applicable law or agreed to in writing, software
553f11a8f542196fd02498006338dd33c64045c3fuankg * distributed under the License is distributed on an "AS IS" BASIS,
553f11a8f542196fd02498006338dd33c64045c3fuankg * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
553f11a8f542196fd02498006338dd33c64045c3fuankg * See the License for the specific language governing permissions and
553f11a8f542196fd02498006338dd33c64045c3fuankg * limitations under the License.
553f11a8f542196fd02498006338dd33c64045c3fuankgstatic int generate_salt(char *s, size_t size, const char **errstr,
553f11a8f542196fd02498006338dd33c64045c3fuankg "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
553f11a8f542196fd02498006338dd33c64045c3fuankg if (n > sizeof(rnd)) {
553f11a8f542196fd02498006338dd33c64045c3fuankg *errstr = apr_psprintf(pool, "generate_salt(): BUG: Buffer too small");
553f11a8f542196fd02498006338dd33c64045c3fuankg *errstr = apr_psprintf(pool, "Unable to generate random bytes: %pm",
553f11a8f542196fd02498006338dd33c64045c3fuankg while (size > 0) {
553f11a8f542196fd02498006338dd33c64045c3fuankg * Make a password record from the given information. A zero return
553f11a8f542196fd02498006338dd33c64045c3fuankg * indicates success; on failure, ctx->errstr points to the error message.
553f11a8f542196fd02498006338dd33c64045c3fuankg /* XXX out >= 28 + strlen(sha1) chars - fixed len SHA */
553f11a8f542196fd02498006338dd33c64045c3fuankg ret = generate_salt(salt, 8, &ctx->errstr, ctx->pool);
553f11a8f542196fd02498006338dd33c64045c3fuankg if (ret != 0) {
553f11a8f542196fd02498006338dd33c64045c3fuankg rv = apr_md5_encode(pw, salt, ctx->out, ctx->out_len);
553f11a8f542196fd02498006338dd33c64045c3fuankg ret = generate_salt(salt, 8, &ctx->errstr, ctx->pool);
553f11a8f542196fd02498006338dd33c64045c3fuankg ctx->errstr = apr_psprintf(ctx->pool, "crypt() failed: %pm", &rv);
553f11a8f542196fd02498006338dd33c64045c3fuankg "Warning: Password truncated to 8 "
553f11a8f542196fd02498006338dd33c64045c3fuankg "characters by CRYPT algorithm.");
553f11a8f542196fd02498006338dd33c64045c3fuankg#endif /* CRYPT_ALGO_SUPPORTED */
553f11a8f542196fd02498006338dd33c64045c3fuankg rv = apr_generate_random_bytes((unsigned char*)salt, 16);
553f11a8f542196fd02498006338dd33c64045c3fuankg ctx->errstr = apr_psprintf(ctx->pool, "Unable to generate random "
553f11a8f542196fd02498006338dd33c64045c3fuankg rv = apr_bcrypt_encode(pw, ctx->cost, (unsigned char*)salt, 16,
553f11a8f542196fd02498006338dd33c64045c3fuankg ctx->errstr = apr_psprintf(ctx->pool, "Unable to encode with "
553f11a8f542196fd02498006338dd33c64045c3fuankg#endif /* BCRYPT_ALGO_SUPPORTED */
e6c7108c75060e44f33554b268b528a26c9bb483fuankg "mk_password_hash(): unsupported algorithm %d",