]> granicus.if.org Git - apache/blob - server/util_md5.c
Remove all of the calls to functions like "ap_popenf". These functions were
[apache] / server / util_md5.c
1 /* ====================================================================
2  * Copyright (c) 1995-1999 The Apache Group.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer. 
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in
13  *    the documentation and/or other materials provided with the
14  *    distribution.
15  *
16  * 3. All advertising materials mentioning features or use of this
17  *    software must display the following acknowledgment:
18  *    "This product includes software developed by the Apache Group
19  *    for use in the Apache HTTP server project (http://www.apache.org/)."
20  *
21  * 4. The names "Apache Server" and "Apache Group" must not be used to
22  *    endorse or promote products derived from this software without
23  *    prior written permission. For written permission, please contact
24  *    apache@apache.org.
25  *
26  * 5. Products derived from this software may not be called "Apache"
27  *    nor may "Apache" appear in their names without prior written
28  *    permission of the Apache Group.
29  *
30  * 6. Redistributions of any form whatsoever must retain the following
31  *    acknowledgment:
32  *    "This product includes software developed by the Apache Group
33  *    for use in the Apache HTTP server project (http://www.apache.org/)."
34  *
35  * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
36  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE APACHE GROUP OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46  * OF THE POSSIBILITY OF SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Apache Group and was originally based
51  * on public domain software written at the National Center for
52  * Supercomputing Applications, University of Illinois, Urbana-Champaign.
53  * For more information on the Apache Group and the Apache HTTP server
54  * project, please see <http://www.apache.org/>.
55  *
56  */
57
58 /************************************************************************
59  * NCSA HTTPd Server
60  * Software Development Group
61  * National Center for Supercomputing Applications
62  * University of Illinois at Urbana-Champaign
63  * 605 E. Springfield, Champaign, IL 61820
64  * httpd@ncsa.uiuc.edu
65  *
66  * Copyright  (C)  1995, Board of Trustees of the University of Illinois
67  *
68  ************************************************************************
69  *
70  * md5.c: NCSA HTTPd code which uses the md5c.c RSA Code
71  *
72  *  Original Code Copyright (C) 1994, Jeff Hostetler, Spyglass, Inc.
73  *  Portions of Content-MD5 code Copyright (C) 1993, 1994 by Carnegie Mellon
74  *     University (see Copyright below).
75  *  Portions of Content-MD5 code Copyright (C) 1991 Bell Communications 
76  *     Research, Inc. (Bellcore) (see Copyright below).
77  *  Portions extracted from mpack, John G. Myers - jgm+@cmu.edu
78  *  Content-MD5 Code contributed by Martin Hamilton (martin@net.lut.ac.uk)
79  *
80  */
81
82
83
84 /* md5.c --Module Interface to MD5. */
85 /* Jeff Hostetler, Spyglass, Inc., 1994. */
86
87 #include "apr_portable.h"
88 #include "httpd.h"
89 #include "util_md5.h"
90
91 API_EXPORT(char *) ap_md5_binary(ap_context_t *p, const unsigned char *buf, int length)
92 {
93     const char *hex = "0123456789abcdef";
94     AP_MD5_CTX my_md5;
95     unsigned char hash[16];
96     char *r, result[33];
97     int i;
98
99     /*
100      * Take the MD5 hash of the string argument.
101      */
102
103     ap_MD5Init(&my_md5);
104     ap_MD5Update(&my_md5, buf, (unsigned int)length);
105     ap_MD5Final(hash, &my_md5);
106
107     for (i = 0, r = result; i < 16; i++) {
108         *r++ = hex[hash[i] >> 4];
109         *r++ = hex[hash[i] & 0xF];
110     }
111     *r = '\0';
112
113     return ap_pstrdup(p, result);
114 }
115
116 API_EXPORT(char *) ap_md5(ap_context_t *p, const unsigned char *string)
117 {
118     return ap_md5_binary(p, string, (int) strlen((char *)string));
119 }
120
121 /* these portions extracted from mpack, John G. Myers - jgm+@cmu.edu */
122
123 /* (C) Copyright 1993,1994 by Carnegie Mellon University
124  * All Rights Reserved.
125  *
126  * Permission to use, copy, modify, distribute, and sell this software
127  * and its documentation for any purpose is hereby granted without
128  * fee, provided that the above copyright notice appear in all copies
129  * and that both that copyright notice and this permission notice
130  * appear in supporting documentation, and that the name of Carnegie
131  * Mellon University not be used in advertising or publicity
132  * pertaining to distribution of the software without specific,
133  * written prior permission.  Carnegie Mellon University makes no
134  * representations about the suitability of this software for any
135  * purpose.  It is provided "as is" without express or implied
136  * warranty.
137  *
138  * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
139  * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
140  * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
141  * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
142  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
143  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
144  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
145  * SOFTWARE.
146  */
147
148 /*
149  * Copyright (c) 1991 Bell Communications Research, Inc. (Bellcore)
150  *
151  * Permission to use, copy, modify, and distribute this material
152  * for any purpose and without fee is hereby granted, provided
153  * that the above copyright notice and this permission notice
154  * appear in all copies, and that the name of Bellcore not be
155  * used in advertising or publicity pertaining to this
156  * material without the specific, prior written permission
157  * of an authorized representative of Bellcore.  BELLCORE
158  * MAKES NO REPRESENTATIONS ABOUT THE ACCURACY OR SUITABILITY
159  * OF THIS MATERIAL FOR ANY PURPOSE.  IT IS PROVIDED "AS IS",
160  * WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.  
161  */
162
163 static char basis_64[] =
164 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
165
166 API_EXPORT(char *) ap_md5contextTo64(ap_context_t *a, AP_MD5_CTX * context)
167 {
168     unsigned char digest[18];
169     char *encodedDigest;
170     int i;
171     char *p;
172
173     encodedDigest = (char *) ap_pcalloc(a, 25 * sizeof(char));
174
175     ap_MD5Final(digest, context);
176     digest[sizeof(digest) - 1] = digest[sizeof(digest) - 2] = 0;
177
178     p = encodedDigest;
179     for (i = 0; i < sizeof(digest); i += 3) {
180         *p++ = basis_64[digest[i] >> 2];
181         *p++ = basis_64[((digest[i] & 0x3) << 4) | ((int) (digest[i + 1] & 0xF0) >> 4)];
182         *p++ = basis_64[((digest[i + 1] & 0xF) << 2) | ((int) (digest[i + 2] & 0xC0) >> 6)];
183         *p++ = basis_64[digest[i + 2] & 0x3F];
184     }
185     *p-- = '\0';
186     *p-- = '=';
187     *p-- = '=';
188     return encodedDigest;
189 }
190
191 #ifdef CHARSET_EBCDIC
192
193 API_EXPORT(char *) ap_md5digest(ap_context_t *p, int infile, int convert)
194 {
195     AP_MD5_CTX context;
196     unsigned char buf[1000];
197     long length = 0;
198     int nbytes;
199
200     ap_MD5Init(&context);
201     /* ZZZ use AP func instead of fread. */
202     while ((nbytes = read(infile, buf, sizeof(buf)))) {
203         length += nbytes;
204         if (!convert) {
205             ascii2ebcdic(buf, buf, nbytes);
206         }
207         ap_MD5Update(&context, buf, nbytes);
208     }
209     /* ZZZ use AP seek func instead of REWIND. */
210     lseek(infile, 0L, SEEK_SET);
211     return ap_md5contextTo64(p, &context);
212 }
213
214 #else
215
216 API_EXPORT(char *) ap_md5digest(ap_context_t *p, int infile)
217 {
218     AP_MD5_CTX context;
219     unsigned char buf[1000];
220     long length = 0;
221     int nbytes;
222
223     ap_MD5Init(&context);
224     /* ZZZ use AP func instead of fread. */
225     while ((nbytes = read(infile, buf, sizeof(buf)))) {
226         length += nbytes;
227         ap_MD5Update(&context, buf, nbytes);
228     }
229     /* ZZZ use AP seek func instead of REWIND. */
230     lseek(infile, 0L, SEEK_SET);
231     return ap_md5contextTo64(p, &context);
232 }
233
234 #endif