]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
f2d18633 | 2 | * "$Id$" |
ef416fc2 | 3 | * |
71e16022 | 4 | * MD5 password support for CUPS. |
ef416fc2 | 5 | * |
71e16022 | 6 | * Copyright 2007-2010 by Apple Inc. |
ef416fc2 | 7 | * Copyright 1997-2005 by Easy Software Products. |
8 | * | |
9 | * These coded instructions, statements, and computer programs are the | |
bc44d920 | 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/". | |
ef416fc2 | 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 | ||
f7deaa1a | 29 | #include "http-private.h" |
71e16022 | 30 | #include "string-private.h" |
ef416fc2 | 31 | |
32 | ||
33 | /* | |
34 | * 'httpMD5()' - Compute the MD5 sum of the username:group:password. | |
35 | */ | |
36 | ||
37 | char * /* O - MD5 sum */ | |
38 | httpMD5(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); | |
757d2cad | 53 | _cupsMD5Init(&state); |
54 | _cupsMD5Append(&state, (unsigned char *)line, (int)strlen(line)); | |
55 | _cupsMD5Finish(&state, sum); | |
ef416fc2 | 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 | ||
71 | char * /* O - New sum */ | |
72 | httpMD5Final(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); | |
757d2cad | 88 | _cupsMD5Init(&state); |
89 | _cupsMD5Append(&state, (unsigned char *)line, (int)strlen(line)); | |
90 | _cupsMD5Finish(&state, sum); | |
ef416fc2 | 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 | ||
757d2cad | 101 | _cupsMD5Init(&state); |
102 | _cupsMD5Append(&state, (unsigned char *)line, (int)strlen(line)); | |
103 | _cupsMD5Finish(&state, sum); | |
ef416fc2 | 104 | |
105 | return (httpMD5String(sum, md5)); | |
106 | } | |
107 | ||
108 | ||
109 | /* | |
110 | * 'httpMD5String()' - Convert an MD5 sum to a character string. | |
111 | */ | |
112 | ||
113 | char * /* O - MD5 sum in hex */ | |
114 | httpMD5String(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 | /* | |
f2d18633 | 141 | * End of "$Id$". |
ef416fc2 | 142 | */ |