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