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