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