842ae4bd224140319ae7feec1872b93dfd491143fielding/* Licensed to the Apache Software Foundation (ASF) under one or more
842ae4bd224140319ae7feec1872b93dfd491143fielding * contributor license agreements. See the NOTICE file distributed with
842ae4bd224140319ae7feec1872b93dfd491143fielding * this work for additional information regarding copyright ownership.
842ae4bd224140319ae7feec1872b93dfd491143fielding * The ASF licenses this file to You under the Apache License, Version 2.0
842ae4bd224140319ae7feec1872b93dfd491143fielding * (the "License"); you may not use this file except in compliance with
842ae4bd224140319ae7feec1872b93dfd491143fielding * the License. You may obtain a copy of the License at
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * http://www.apache.org/licenses/LICENSE-2.0
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * Unless required by applicable law or agreed to in writing, software
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * distributed under the License is distributed on an "AS IS" BASIS,
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * See the License for the specific language governing permissions and
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * limitations under the License.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/************************************************************************
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * NCSA HTTPd Server
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Software Development Group
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * National Center for Supercomputing Applications
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * University of Illinois at Urbana-Champaign
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * 605 E. Springfield, Champaign, IL 61820
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * httpd@ncsa.uiuc.edu
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Copyright (C) 1995, Board of Trustees of the University of Illinois
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ************************************************************************
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * md5.c: NCSA HTTPd code which uses the md5c.c RSA Code
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Original Code Copyright (C) 1994, Jeff Hostetler, Spyglass, Inc.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Portions of Content-MD5 code Copyright (C) 1993, 1994 by Carnegie Mellon
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * University (see Copyright below).
e8f95a682820a599fe41b22977010636be5c2717jim * Portions of Content-MD5 code Copyright (C) 1991 Bell Communications
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Research, Inc. (Bellcore) (see Copyright below).
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Portions extracted from mpack, John G. Myers - jgm+@cmu.edu
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Content-MD5 Code contributed by Martin Hamilton (martin@net.lut.ac.uk)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/* md5.c --Module Interface to MD5. */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/* Jeff Hostetler, Spyglass, Inc., 1994. */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
2d71630471d1c23f0137309e3c3957c633ecbfd6rbb#include "ap_config.h"
ab5581cc78e9d865b0a6ab1404c53347b3276968rbb#include "apr_portable.h"
b6055b7832a0e4d0818416252fff5925aaebae4brbb#include "apr_strings.h"
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding#include "httpd.h"
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding#include "util_md5.h"
2eaf6dbe7ea643b3a2b8e1973d9684fac6372c46trawick#include "util_ebcdic.h"
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
3d96ee83babeec32482c9082c9426340cee8c44dwroweAP_DECLARE(char *) ap_md5_binary(apr_pool_t *p, const unsigned char *buf, int length)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding{
10a4cdd68ef1ca0e54af296fe1d08ac00150c90bwrowe apr_md5_ctx_t my_md5;
d0ba3b97557d47323bd055fb4002ed7692f703b9jerenkrantz unsigned char hash[APR_MD5_DIGESTSIZE];
5b89dd41cdd1eb736fec0d120d68f71e9503caf6sf char result[2 * APR_MD5_DIGESTSIZE + 1];
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /*
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Take the MD5 hash of the string argument.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
066877f1a045103acfdd376d48cdd473c33f409bdougm apr_md5_init(&my_md5);
db3fa7db7c7910f2f23c3e3ffe0cf9f41a1899b9trawick#if APR_CHARSET_EBCDIC
066877f1a045103acfdd376d48cdd473c33f409bdougm apr_md5_set_xlate(&my_md5, ap_hdrs_to_ascii);
2eaf6dbe7ea643b3a2b8e1973d9684fac6372c46trawick#endif
066877f1a045103acfdd376d48cdd473c33f409bdougm apr_md5_update(&my_md5, buf, (unsigned int)length);
066877f1a045103acfdd376d48cdd473c33f409bdougm apr_md5_final(hash, &my_md5);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
5b89dd41cdd1eb736fec0d120d68f71e9503caf6sf ap_bin2hex(hash, APR_MD5_DIGESTSIZE, result);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
d0ba3b97557d47323bd055fb4002ed7692f703b9jerenkrantz return apr_pstrndup(p, result, APR_MD5_DIGESTSIZE*2);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding}
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
3d96ee83babeec32482c9082c9426340cee8c44dwroweAP_DECLARE(char *) ap_md5(apr_pool_t *p, const unsigned char *string)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding{
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return ap_md5_binary(p, string, (int) strlen((char *)string));
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding}
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/* these portions extracted from mpack, John G. Myers - jgm+@cmu.edu */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/* (C) Copyright 1993,1994 by Carnegie Mellon University
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * All Rights Reserved.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Permission to use, copy, modify, distribute, and sell this software
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * and its documentation for any purpose is hereby granted without
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * fee, provided that the above copyright notice appear in all copies
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * and that both that copyright notice and this permission notice
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * appear in supporting documentation, and that the name of Carnegie
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Mellon University not be used in advertising or publicity
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * pertaining to distribution of the software without specific,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * written prior permission. Carnegie Mellon University makes no
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * representations about the suitability of this software for any
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * purpose. It is provided "as is" without express or implied
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * warranty.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * SOFTWARE.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/*
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Copyright (c) 1991 Bell Communications Research, Inc. (Bellcore)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Permission to use, copy, modify, and distribute this material
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * for any purpose and without fee is hereby granted, provided
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * that the above copyright notice and this permission notice
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * appear in all copies, and that the name of Bellcore not be
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * used in advertising or publicity pertaining to this
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * material without the specific, prior written permission
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * of an authorized representative of Bellcore. BELLCORE
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * MAKES NO REPRESENTATIONS ABOUT THE ACCURACY OR SUITABILITY
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * OF THIS MATERIAL FOR ANY PURPOSE. IT IS PROVIDED "AS IS",
e8f95a682820a599fe41b22977010636be5c2717jim * WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingstatic char basis_64[] =
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
3d96ee83babeec32482c9082c9426340cee8c44dwroweAP_DECLARE(char *) ap_md5contextTo64(apr_pool_t *a, apr_md5_ctx_t *context)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding{
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding unsigned char digest[18];
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding char *encodedDigest;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding int i;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding char *p;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm encodedDigest = (char *) apr_pcalloc(a, 25 * sizeof(char));
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
066877f1a045103acfdd376d48cdd473c33f409bdougm apr_md5_final(digest, context);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding digest[sizeof(digest) - 1] = digest[sizeof(digest) - 2] = 0;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding p = encodedDigest;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding for (i = 0; i < sizeof(digest); i += 3) {
43c3e6a4b559b76b750c245ee95e2782c15b4296jim *p++ = basis_64[digest[i] >> 2];
43c3e6a4b559b76b750c245ee95e2782c15b4296jim *p++ = basis_64[((digest[i] & 0x3) << 4) | ((int) (digest[i + 1] & 0xF0) >> 4)];
43c3e6a4b559b76b750c245ee95e2782c15b4296jim *p++ = basis_64[((digest[i + 1] & 0xF) << 2) | ((int) (digest[i + 2] & 0xC0) >> 6)];
43c3e6a4b559b76b750c245ee95e2782c15b4296jim *p++ = basis_64[digest[i + 2] & 0x3F];
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *p-- = '\0';
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *p-- = '=';
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *p-- = '=';
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return encodedDigest;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding}
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
3d96ee83babeec32482c9082c9426340cee8c44dwroweAP_DECLARE(char *) ap_md5digest(apr_pool_t *p, apr_file_t *infile)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding{
10a4cdd68ef1ca0e54af296fe1d08ac00150c90bwrowe apr_md5_ctx_t context;
fbe3307b402780c3e9300e49c95b990d906dd605jorton unsigned char buf[4096]; /* keep this a multiple of 64 */
f92e51e1951be0ef34da086bdff019d8ddc71c63rbb apr_size_t nbytes;
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm apr_off_t offset = 0L;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
066877f1a045103acfdd376d48cdd473c33f409bdougm apr_md5_init(&context);
7431131ef5bf15f103cf5f338407ccabb716c0c0rbb nbytes = sizeof(buf);
066877f1a045103acfdd376d48cdd473c33f409bdougm while (apr_file_read(infile, buf, &nbytes) == APR_SUCCESS) {
43c3e6a4b559b76b750c245ee95e2782c15b4296jim apr_md5_update(&context, buf, nbytes);
fbe3307b402780c3e9300e49c95b990d906dd605jorton nbytes = sizeof(buf);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
066877f1a045103acfdd376d48cdd473c33f409bdougm apr_file_seek(infile, APR_SET, &offset);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return ap_md5contextTo64(p, &context);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding}
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding