Blame server/util_md5.c

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