1/*
2 * "$Id: md5passwd.c 11093 2013-07-03 20:48:42Z msweet $"
3 *
4 *   MD5 password support for CUPS.
5 *
6 *   Copyright 2007-2010 by Apple Inc.
7 *   Copyright 1997-2005 by Easy Software Products.
8 *
9 *   These coded instructions, statements, and computer programs are the
10 *   property of Apple Inc. and are protected by Federal copyright
11 *   law.  Distribution and use rights are outlined in the file "LICENSE.txt"
12 *   which should have been included with this file.  If this file is
13 *   file is missing or damaged, see the license at "http://www.cups.org/".
14 *
15 *   This file is subject to the Apple OS-Developed Software exception.
16 *
17 * Contents:
18 *
19 *   httpMD5()       - Compute the MD5 sum of the username:group:password.
20 *   httpMD5Nonce()  - Combine the MD5 sum of the username, group, and password
21 *                     with the server-supplied nonce value.
22 *   httpMD5String() - Convert an MD5 sum to a character string.
23 */
24
25/*
26 * Include necessary headers...
27 */
28
29#include "http-private.h"
30#include "string-private.h"
31
32
33/*
34 * 'httpMD5()' - Compute the MD5 sum of the username:group:password.
35 */
36
37char *					/* O - MD5 sum */
38httpMD5(const char *username,		/* I - User name */
39        const char *realm,		/* I - Realm name */
40        const char *passwd,		/* I - Password string */
41	char       md5[33])		/* O - MD5 string */
42{
43  _cups_md5_state_t	state;		/* MD5 state info */
44  unsigned char		sum[16];	/* Sum data */
45  char			line[256];	/* Line to sum */
46
47
48 /*
49  * Compute the MD5 sum of the user name, group name, and password.
50  */
51
52  snprintf(line, sizeof(line), "%s:%s:%s", username, realm, passwd);
53  _cupsMD5Init(&state);
54  _cupsMD5Append(&state, (unsigned char *)line, (int)strlen(line));
55  _cupsMD5Finish(&state, sum);
56
57 /*
58  * Return the sum...
59  */
60
61  return (httpMD5String(sum, md5));
62}
63
64
65/*
66 * 'httpMD5Final()' - Combine the MD5 sum of the username, group, and password
67 *                    with the server-supplied nonce value, method, and
68 *                    request-uri.
69 */
70
71char *					/* O - New sum */
72httpMD5Final(const char *nonce,		/* I - Server nonce value */
73             const char *method,	/* I - METHOD (GET, POST, etc.) */
74	     const char *resource,	/* I - Resource path */
75             char       md5[33])	/* IO - MD5 sum */
76{
77  _cups_md5_state_t	state;		/* MD5 state info */
78  unsigned char		sum[16];	/* Sum data */
79  char			line[1024];	/* Line of data */
80  char			a2[33];		/* Hash of method and resource */
81
82
83 /*
84  * First compute the MD5 sum of the method and resource...
85  */
86
87  snprintf(line, sizeof(line), "%s:%s", method, resource);
88  _cupsMD5Init(&state);
89  _cupsMD5Append(&state, (unsigned char *)line, (int)strlen(line));
90  _cupsMD5Finish(&state, sum);
91  httpMD5String(sum, a2);
92
93 /*
94  * Then combine A1 (MD5 of username, realm, and password) with the nonce
95  * and A2 (method + resource) values to get the final MD5 sum for the
96  * request...
97  */
98
99  snprintf(line, sizeof(line), "%s:%s:%s", md5, nonce, a2);
100
101  _cupsMD5Init(&state);
102  _cupsMD5Append(&state, (unsigned char *)line, (int)strlen(line));
103  _cupsMD5Finish(&state, sum);
104
105  return (httpMD5String(sum, md5));
106}
107
108
109/*
110 * 'httpMD5String()' - Convert an MD5 sum to a character string.
111 */
112
113char *					/* O - MD5 sum in hex */
114httpMD5String(const unsigned char *sum,	/* I - MD5 sum data */
115              char                md5[33])
116					/* O - MD5 sum in hex */
117{
118  int		i;			/* Looping var */
119  char		*md5ptr;		/* Pointer into MD5 string */
120  static const char hex[] = "0123456789abcdef";
121					/* Hex digits */
122
123
124 /*
125  * Convert the MD5 sum to hexadecimal...
126  */
127
128  for (i = 16, md5ptr = md5; i > 0; i --, sum ++)
129  {
130    *md5ptr++ = hex[*sum >> 4];
131    *md5ptr++ = hex[*sum & 15];
132  }
133
134  *md5ptr = '\0';
135
136  return (md5);
137}
138
139
140/*
141 * End of "$Id: md5passwd.c 11093 2013-07-03 20:48:42Z msweet $".
142 */
143