util_md5.c revision fbe3307b402780c3e9300e49c95b990d906dd605
b5d34bc3082c1164ce20df48567d192e3e9f229fjim/* ====================================================================
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * The Apache Software License, Version 1.1
b5d34bc3082c1164ce20df48567d192e3e9f229fjim *
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * Copyright (c) 2000-2003 The Apache Software Foundation. All rights
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * reserved.
b5d34bc3082c1164ce20df48567d192e3e9f229fjim *
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * Redistribution and use in source and binary forms, with or without
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * modification, are permitted provided that the following conditions
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * are met:
b5d34bc3082c1164ce20df48567d192e3e9f229fjim *
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * 1. Redistributions of source code must retain the above copyright
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * notice, this list of conditions and the following disclaimer.
b5d34bc3082c1164ce20df48567d192e3e9f229fjim *
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * 2. Redistributions in binary form must reproduce the above copyright
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * notice, this list of conditions and the following disclaimer in
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * the documentation and/or other materials provided with the
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * distribution.
b5d34bc3082c1164ce20df48567d192e3e9f229fjim *
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * 3. The end-user documentation included with the redistribution,
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * if any, must include the following acknowledgment:
12b170a812f740fafc96da32a188a8b2761a3d5cjim * "This product includes software developed by the
2806775e45dfd5164d53fcb3d850d358f8ec2415jfclere * Apache Software Foundation (http://www.apache.org/)."
57a049796ba191c87b588cbde1323e8f65e3fea5jfclere * Alternately, this acknowledgment may appear in the software itself,
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * if and wherever such third-party acknowledgments normally appear.
a231b4cbdf1bd3d65dc13ead8d8a4f7e8b307314pquerna *
a231b4cbdf1bd3d65dc13ead8d8a4f7e8b307314pquerna * 4. The names "Apache" and "Apache Software Foundation" must
a231b4cbdf1bd3d65dc13ead8d8a4f7e8b307314pquerna * not be used to endorse or promote products derived from this
a231b4cbdf1bd3d65dc13ead8d8a4f7e8b307314pquerna * software without prior written permission. For written
a231b4cbdf1bd3d65dc13ead8d8a4f7e8b307314pquerna * permission, please contact apache@apache.org.
a231b4cbdf1bd3d65dc13ead8d8a4f7e8b307314pquerna *
a231b4cbdf1bd3d65dc13ead8d8a4f7e8b307314pquerna * 5. Products derived from this software may not be called "Apache",
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * nor may "Apache" appear in their name, without prior written
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * permission of the Apache Software Foundation.
4ab0fd1b54ce76f0fb6812b89b0b4dc7541014d6minfrin *
4ab0fd1b54ce76f0fb6812b89b0b4dc7541014d6minfrin * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
4ab0fd1b54ce76f0fb6812b89b0b4dc7541014d6minfrin * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
94b9a8750697ea0bf67a2a828e8034e5fe103ccatrawick * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
2806775e45dfd5164d53fcb3d850d358f8ec2415jfclere * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
2806775e45dfd5164d53fcb3d850d358f8ec2415jfclere * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2806775e45dfd5164d53fcb3d850d358f8ec2415jfclere * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2806775e45dfd5164d53fcb3d850d358f8ec2415jfclere * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
2806775e45dfd5164d53fcb3d850d358f8ec2415jfclere * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
2806775e45dfd5164d53fcb3d850d358f8ec2415jfclere * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * SUCH DAMAGE.
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * ====================================================================
b5d34bc3082c1164ce20df48567d192e3e9f229fjim *
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * This software consists of voluntary contributions made by many
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * individuals on behalf of the Apache Software Foundation. For more
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * information on the Apache Software Foundation, please see
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * <http://www.apache.org/>.
c87ed6a25f669f7137024ccde3771d95b9c37e07jfclere *
2806775e45dfd5164d53fcb3d850d358f8ec2415jfclere * Portions of this software are based upon public domain software
4c25fdfa5f370d29e55aea846eb9fe6c1d51ede3wrowe * originally written at the National Center for Supercomputing Applications,
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * University of Illinois, Urbana-Champaign.
b5d34bc3082c1164ce20df48567d192e3e9f229fjim */
b5d34bc3082c1164ce20df48567d192e3e9f229fjim
a515c3892232a3497bb58eeec5a1b9523571cf67jfclere/************************************************************************
a515c3892232a3497bb58eeec5a1b9523571cf67jfclere * NCSA HTTPd Server
a515c3892232a3497bb58eeec5a1b9523571cf67jfclere * Software Development Group
a515c3892232a3497bb58eeec5a1b9523571cf67jfclere * National Center for Supercomputing Applications
a515c3892232a3497bb58eeec5a1b9523571cf67jfclere * University of Illinois at Urbana-Champaign
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * 605 E. Springfield, Champaign, IL 61820
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * httpd@ncsa.uiuc.edu
b5d34bc3082c1164ce20df48567d192e3e9f229fjim *
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * Copyright (C) 1995, Board of Trustees of the University of Illinois
b5d34bc3082c1164ce20df48567d192e3e9f229fjim *
b5d34bc3082c1164ce20df48567d192e3e9f229fjim ************************************************************************
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim *
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * md5.c: NCSA HTTPd code which uses the md5c.c RSA Code
b5d34bc3082c1164ce20df48567d192e3e9f229fjim *
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * Original Code Copyright (C) 1994, Jeff Hostetler, Spyglass, Inc.
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * Portions of Content-MD5 code Copyright (C) 1993, 1994 by Carnegie Mellon
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * University (see Copyright below).
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * Portions of Content-MD5 code Copyright (C) 1991 Bell Communications
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * Research, Inc. (Bellcore) (see Copyright below).
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * Portions extracted from mpack, John G. Myers - jgm+@cmu.edu
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * Content-MD5 Code contributed by Martin Hamilton (martin@net.lut.ac.uk)
b5d34bc3082c1164ce20df48567d192e3e9f229fjim *
b5d34bc3082c1164ce20df48567d192e3e9f229fjim */
b5d34bc3082c1164ce20df48567d192e3e9f229fjim
b5d34bc3082c1164ce20df48567d192e3e9f229fjim
b5d34bc3082c1164ce20df48567d192e3e9f229fjim
b5d34bc3082c1164ce20df48567d192e3e9f229fjim/* md5.c --Module Interface to MD5. */
b5d34bc3082c1164ce20df48567d192e3e9f229fjim/* Jeff Hostetler, Spyglass, Inc., 1994. */
b5d34bc3082c1164ce20df48567d192e3e9f229fjim
b5d34bc3082c1164ce20df48567d192e3e9f229fjim#include "ap_config.h"
b5d34bc3082c1164ce20df48567d192e3e9f229fjim#include "apr_portable.h"
b5d34bc3082c1164ce20df48567d192e3e9f229fjim#include "apr_strings.h"
b5d34bc3082c1164ce20df48567d192e3e9f229fjim#include "httpd.h"
2806775e45dfd5164d53fcb3d850d358f8ec2415jfclere#include "util_md5.h"
b5d34bc3082c1164ce20df48567d192e3e9f229fjim#include "util_ebcdic.h"
b5d34bc3082c1164ce20df48567d192e3e9f229fjim
b5d34bc3082c1164ce20df48567d192e3e9f229fjimAP_DECLARE(char *) ap_md5_binary(apr_pool_t *p, const unsigned char *buf, int length)
b5d34bc3082c1164ce20df48567d192e3e9f229fjim{
b5d34bc3082c1164ce20df48567d192e3e9f229fjim const char *hex = "0123456789abcdef";
b5d34bc3082c1164ce20df48567d192e3e9f229fjim apr_md5_ctx_t my_md5;
b5d34bc3082c1164ce20df48567d192e3e9f229fjim unsigned char hash[MD5_DIGESTSIZE];
b5d34bc3082c1164ce20df48567d192e3e9f229fjim char *r, result[33]; /* (MD5_DIGESTSIZE * 2) + 1 */
b5d34bc3082c1164ce20df48567d192e3e9f229fjim int i;
b5d34bc3082c1164ce20df48567d192e3e9f229fjim
b5d34bc3082c1164ce20df48567d192e3e9f229fjim /*
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * Take the MD5 hash of the string argument.
b5d34bc3082c1164ce20df48567d192e3e9f229fjim */
b5d34bc3082c1164ce20df48567d192e3e9f229fjim
b5d34bc3082c1164ce20df48567d192e3e9f229fjim apr_md5_init(&my_md5);
b5d34bc3082c1164ce20df48567d192e3e9f229fjim#if APR_CHARSET_EBCDIC
b5d34bc3082c1164ce20df48567d192e3e9f229fjim apr_md5_set_xlate(&my_md5, ap_hdrs_to_ascii);
b5d34bc3082c1164ce20df48567d192e3e9f229fjim#endif
b5d34bc3082c1164ce20df48567d192e3e9f229fjim apr_md5_update(&my_md5, buf, (unsigned int)length);
b5d34bc3082c1164ce20df48567d192e3e9f229fjim apr_md5_final(hash, &my_md5);
b5d34bc3082c1164ce20df48567d192e3e9f229fjim
b5d34bc3082c1164ce20df48567d192e3e9f229fjim for (i = 0, r = result; i < MD5_DIGESTSIZE; i++) {
b5d34bc3082c1164ce20df48567d192e3e9f229fjim *r++ = hex[hash[i] >> 4];
b5d34bc3082c1164ce20df48567d192e3e9f229fjim *r++ = hex[hash[i] & 0xF];
b5d34bc3082c1164ce20df48567d192e3e9f229fjim }
b5d34bc3082c1164ce20df48567d192e3e9f229fjim *r = '\0';
b5d34bc3082c1164ce20df48567d192e3e9f229fjim
b5d34bc3082c1164ce20df48567d192e3e9f229fjim return apr_pstrndup(p, result, MD5_DIGESTSIZE*2);
b5d34bc3082c1164ce20df48567d192e3e9f229fjim}
b5d34bc3082c1164ce20df48567d192e3e9f229fjim
b5d34bc3082c1164ce20df48567d192e3e9f229fjimAP_DECLARE(char *) ap_md5(apr_pool_t *p, const unsigned char *string)
b5d34bc3082c1164ce20df48567d192e3e9f229fjim{
b5d34bc3082c1164ce20df48567d192e3e9f229fjim return ap_md5_binary(p, string, (int) strlen((char *)string));
b5d34bc3082c1164ce20df48567d192e3e9f229fjim}
b5d34bc3082c1164ce20df48567d192e3e9f229fjim
b5d34bc3082c1164ce20df48567d192e3e9f229fjim/* these portions extracted from mpack, John G. Myers - jgm+@cmu.edu */
b5d34bc3082c1164ce20df48567d192e3e9f229fjim
a84a6c5a1f176c4fc6ccbc1183dc7c9f401e18f1pquerna/* (C) Copyright 1993,1994 by Carnegie Mellon University
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * All Rights Reserved.
b5d34bc3082c1164ce20df48567d192e3e9f229fjim *
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * Permission to use, copy, modify, distribute, and sell this software
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * and its documentation for any purpose is hereby granted without
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * fee, provided that the above copyright notice appear in all copies
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * and that both that copyright notice and this permission notice
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * appear in supporting documentation, and that the name of Carnegie
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * Mellon University not be used in advertising or publicity
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * pertaining to distribution of the software without specific,
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * written prior permission. Carnegie Mellon University makes no
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * representations about the suitability of this software for any
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * purpose. It is provided "as is" without express or implied
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * warranty.
b5d34bc3082c1164ce20df48567d192e3e9f229fjim *
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * SOFTWARE.
b5d34bc3082c1164ce20df48567d192e3e9f229fjim */
b5d34bc3082c1164ce20df48567d192e3e9f229fjim
b5d34bc3082c1164ce20df48567d192e3e9f229fjim/*
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * Copyright (c) 1991 Bell Communications Research, Inc. (Bellcore)
b5d34bc3082c1164ce20df48567d192e3e9f229fjim *
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * Permission to use, copy, modify, and distribute this material
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * for any purpose and without fee is hereby granted, provided
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * that the above copyright notice and this permission notice
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * appear in all copies, and that the name of Bellcore not be
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * used in advertising or publicity pertaining to this
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * material without the specific, prior written permission
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim * of an authorized representative of Bellcore. BELLCORE
a84a6c5a1f176c4fc6ccbc1183dc7c9f401e18f1pquerna * MAKES NO REPRESENTATIONS ABOUT THE ACCURACY OR SUITABILITY
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * OF THIS MATERIAL FOR ANY PURPOSE. IT IS PROVIDED "AS IS",
b5d34bc3082c1164ce20df48567d192e3e9f229fjim * WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
b5d34bc3082c1164ce20df48567d192e3e9f229fjim */
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim
b5d34bc3082c1164ce20df48567d192e3e9f229fjimstatic char basis_64[] =
b5d34bc3082c1164ce20df48567d192e3e9f229fjim"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
b5d34bc3082c1164ce20df48567d192e3e9f229fjim
c87ed6a25f669f7137024ccde3771d95b9c37e07jfclereAP_DECLARE(char *) ap_md5contextTo64(apr_pool_t *a, apr_md5_ctx_t *context)
b5d34bc3082c1164ce20df48567d192e3e9f229fjim{
b5d34bc3082c1164ce20df48567d192e3e9f229fjim unsigned char digest[18];
b5d34bc3082c1164ce20df48567d192e3e9f229fjim char *encodedDigest;
b5d34bc3082c1164ce20df48567d192e3e9f229fjim int i;
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim char *p;
b5d34bc3082c1164ce20df48567d192e3e9f229fjim
b5d34bc3082c1164ce20df48567d192e3e9f229fjim encodedDigest = (char *) apr_pcalloc(a, 25 * sizeof(char));
b5d34bc3082c1164ce20df48567d192e3e9f229fjim
b5d34bc3082c1164ce20df48567d192e3e9f229fjim apr_md5_final(digest, context);
b5d34bc3082c1164ce20df48567d192e3e9f229fjim digest[sizeof(digest) - 1] = digest[sizeof(digest) - 2] = 0;
b5d34bc3082c1164ce20df48567d192e3e9f229fjim
b5d34bc3082c1164ce20df48567d192e3e9f229fjim p = encodedDigest;
b5d34bc3082c1164ce20df48567d192e3e9f229fjim for (i = 0; i < sizeof(digest); i += 3) {
b5d34bc3082c1164ce20df48567d192e3e9f229fjim *p++ = basis_64[digest[i] >> 2];
b5d34bc3082c1164ce20df48567d192e3e9f229fjim *p++ = basis_64[((digest[i] & 0x3) << 4) | ((int) (digest[i + 1] & 0xF0) >> 4)];
b5d34bc3082c1164ce20df48567d192e3e9f229fjim *p++ = basis_64[((digest[i + 1] & 0xF) << 2) | ((int) (digest[i + 2] & 0xC0) >> 6)];
b5d34bc3082c1164ce20df48567d192e3e9f229fjim *p++ = basis_64[digest[i + 2] & 0x3F];
b5d34bc3082c1164ce20df48567d192e3e9f229fjim }
b5d34bc3082c1164ce20df48567d192e3e9f229fjim *p-- = '\0';
b5d34bc3082c1164ce20df48567d192e3e9f229fjim *p-- = '=';
b5d34bc3082c1164ce20df48567d192e3e9f229fjim *p-- = '=';
c87ed6a25f669f7137024ccde3771d95b9c37e07jfclere return encodedDigest;
c87ed6a25f669f7137024ccde3771d95b9c37e07jfclere}
c87ed6a25f669f7137024ccde3771d95b9c37e07jfclere
c87ed6a25f669f7137024ccde3771d95b9c37e07jfclereAP_DECLARE(char *) ap_md5digest(apr_pool_t *p, apr_file_t *infile)
b5d34bc3082c1164ce20df48567d192e3e9f229fjim{
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim apr_md5_ctx_t context;
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim unsigned char buf[4096]; /* keep this a multiple of 64 */
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim apr_size_t nbytes;
b5d34bc3082c1164ce20df48567d192e3e9f229fjim apr_off_t offset = 0L;
b5d34bc3082c1164ce20df48567d192e3e9f229fjim
b5d34bc3082c1164ce20df48567d192e3e9f229fjim apr_md5_init(&context);
b5d34bc3082c1164ce20df48567d192e3e9f229fjim nbytes = sizeof(buf);
b5d34bc3082c1164ce20df48567d192e3e9f229fjim while (apr_file_read(infile, buf, &nbytes) == APR_SUCCESS) {
b5d34bc3082c1164ce20df48567d192e3e9f229fjim apr_md5_update(&context, buf, nbytes);
b5d34bc3082c1164ce20df48567d192e3e9f229fjim nbytes = sizeof(buf);
b5d34bc3082c1164ce20df48567d192e3e9f229fjim }
b5d34bc3082c1164ce20df48567d192e3e9f229fjim apr_file_seek(infile, APR_SET, &offset);
b5d34bc3082c1164ce20df48567d192e3e9f229fjim return ap_md5contextTo64(p, &context);
b5d34bc3082c1164ce20df48567d192e3e9f229fjim}
2806775e45dfd5164d53fcb3d850d358f8ec2415jfclere
2806775e45dfd5164d53fcb3d850d358f8ec2415jfclere