summaryrefslogtreecommitdiff
path: root/cups/libs/cups/md5passwd.c
blob: 135282c50e6f7576a83966fb9351749b4cb201de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
 * "$Id: md5passwd.c 10996 2013-05-29 11:51:34Z msweet $"
 *
 *   MD5 password support for CUPS.
 *
 *   Copyright 2007-2010 by Apple Inc.
 *   Copyright 1997-2005 by Easy Software Products.
 *
 *   These coded instructions, statements, and computer programs are the
 *   property of Apple Inc. and are protected by Federal copyright
 *   law.  Distribution and use rights are outlined in the file "LICENSE.txt"
 *   which should have been included with this file.  If this file is
 *   file is missing or damaged, see the license at "http://www.cups.org/".
 *
 *   This file is subject to the Apple OS-Developed Software exception.
 *
 * Contents:
 *
 *   httpMD5()       - Compute the MD5 sum of the username:group:password.
 *   httpMD5Nonce()  - Combine the MD5 sum of the username, group, and password
 *                     with the server-supplied nonce value.
 *   httpMD5String() - Convert an MD5 sum to a character string.
 */

/*
 * Include necessary headers...
 */

#include "http-private.h"
#include "string-private.h"


/*
 * 'httpMD5()' - Compute the MD5 sum of the username:group:password.
 */

char *					/* O - MD5 sum */
httpMD5(const char *username,		/* I - User name */
        const char *realm,		/* I - Realm name */
        const char *passwd,		/* I - Password string */
	char       md5[33])		/* O - MD5 string */
{
  _cups_md5_state_t	state;		/* MD5 state info */
  unsigned char		sum[16];	/* Sum data */
  char			line[256];	/* Line to sum */


 /*
  * Compute the MD5 sum of the user name, group name, and password.
  */

  snprintf(line, sizeof(line), "%s:%s:%s", username, realm, passwd);
  _cupsMD5Init(&state);
  _cupsMD5Append(&state, (unsigned char *)line, (int)strlen(line));
  _cupsMD5Finish(&state, sum);

 /*
  * Return the sum...
  */

  return (httpMD5String(sum, md5));
}


/*
 * 'httpMD5Final()' - Combine the MD5 sum of the username, group, and password
 *                    with the server-supplied nonce value, method, and
 *                    request-uri.
 */

char *					/* O - New sum */
httpMD5Final(const char *nonce,		/* I - Server nonce value */
             const char *method,	/* I - METHOD (GET, POST, etc.) */
	     const char *resource,	/* I - Resource path */
             char       md5[33])	/* IO - MD5 sum */
{
  _cups_md5_state_t	state;		/* MD5 state info */
  unsigned char		sum[16];	/* Sum data */
  char			line[1024];	/* Line of data */
  char			a2[33];		/* Hash of method and resource */


 /*
  * First compute the MD5 sum of the method and resource...
  */

  snprintf(line, sizeof(line), "%s:%s", method, resource);
  _cupsMD5Init(&state);
  _cupsMD5Append(&state, (unsigned char *)line, (int)strlen(line));
  _cupsMD5Finish(&state, sum);
  httpMD5String(sum, a2);

 /*
  * Then combine A1 (MD5 of username, realm, and password) with the nonce
  * and A2 (method + resource) values to get the final MD5 sum for the
  * request...
  */

  snprintf(line, sizeof(line), "%s:%s:%s", md5, nonce, a2);

  _cupsMD5Init(&state);
  _cupsMD5Append(&state, (unsigned char *)line, (int)strlen(line));
  _cupsMD5Finish(&state, sum);

  return (httpMD5String(sum, md5));
}


/*
 * 'httpMD5String()' - Convert an MD5 sum to a character string.
 */

char *					/* O - MD5 sum in hex */
httpMD5String(const unsigned char *sum,	/* I - MD5 sum data */
              char                md5[33])
					/* O - MD5 sum in hex */
{
  int		i;			/* Looping var */
  char		*md5ptr;		/* Pointer into MD5 string */
  static const char hex[] = "0123456789abcdef";
					/* Hex digits */


 /*
  * Convert the MD5 sum to hexadecimal...
  */

  for (i = 16, md5ptr = md5; i > 0; i --, sum ++)
  {
    *md5ptr++ = hex[*sum >> 4];
    *md5ptr++ = hex[*sum & 15];
  }

  *md5ptr = '\0';

  return (md5);
}


/*
 * End of "$Id: md5passwd.c 10996 2013-05-29 11:51:34Z msweet $".
 */