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