|
@@ -45,6 +45,79 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
|
45
|
45
|
__einfo_uniqify ( EINFO_EACCES, 0x01, \
|
46
|
46
|
"No username available for Digest authentication" )
|
47
|
47
|
|
|
48
|
+/** An HTTP Digest "WWW-Authenticate" response field */
|
|
49
|
+struct http_digest_field {
|
|
50
|
+ /** Name */
|
|
51
|
+ const char *name;
|
|
52
|
+ /** Offset */
|
|
53
|
+ size_t offset;
|
|
54
|
+};
|
|
55
|
+
|
|
56
|
+/** Define an HTTP Digest "WWW-Authenticate" response field */
|
|
57
|
+#define HTTP_DIGEST_FIELD( _name ) { \
|
|
58
|
+ .name = #_name, \
|
|
59
|
+ .offset = offsetof ( struct http_transaction, \
|
|
60
|
+ response.auth.digest._name ), \
|
|
61
|
+ }
|
|
62
|
+
|
|
63
|
+/**
|
|
64
|
+ * Set HTTP Digest "WWW-Authenticate" response field value
|
|
65
|
+ *
|
|
66
|
+ * @v http HTTP transaction
|
|
67
|
+ * @v field Response field
|
|
68
|
+ * @v value Field value
|
|
69
|
+ */
|
|
70
|
+static inline void
|
|
71
|
+http_digest_field ( struct http_transaction *http,
|
|
72
|
+ struct http_digest_field *field, char *value ) {
|
|
73
|
+ char **ptr;
|
|
74
|
+
|
|
75
|
+ ptr = ( ( ( void * ) http ) + field->offset );
|
|
76
|
+ *ptr = value;
|
|
77
|
+}
|
|
78
|
+
|
|
79
|
+/** HTTP Digest "WWW-Authenticate" fields */
|
|
80
|
+static struct http_digest_field http_digest_fields[] = {
|
|
81
|
+ HTTP_DIGEST_FIELD ( realm ),
|
|
82
|
+ HTTP_DIGEST_FIELD ( qop ),
|
|
83
|
+ HTTP_DIGEST_FIELD ( algorithm ),
|
|
84
|
+ HTTP_DIGEST_FIELD ( nonce ),
|
|
85
|
+ HTTP_DIGEST_FIELD ( opaque ),
|
|
86
|
+};
|
|
87
|
+
|
|
88
|
+/**
|
|
89
|
+ * Parse HTTP "WWW-Authenticate" header for Digest authentication
|
|
90
|
+ *
|
|
91
|
+ * @v http HTTP transaction
|
|
92
|
+ * @v line Remaining header line
|
|
93
|
+ * @ret rc Return status code
|
|
94
|
+ */
|
|
95
|
+static int http_parse_digest_auth ( struct http_transaction *http,
|
|
96
|
+ char *line ) {
|
|
97
|
+ struct http_digest_field *field;
|
|
98
|
+ char *key;
|
|
99
|
+ char *value;
|
|
100
|
+ unsigned int i;
|
|
101
|
+
|
|
102
|
+ /* Process fields */
|
|
103
|
+ while ( ( key = http_token ( &line, &value ) ) ) {
|
|
104
|
+ for ( i = 0 ; i < ( sizeof ( http_digest_fields ) /
|
|
105
|
+ sizeof ( http_digest_fields[0] ) ) ; i++){
|
|
106
|
+ field = &http_digest_fields[i];
|
|
107
|
+ if ( strcasecmp ( key, field->name ) == 0 )
|
|
108
|
+ http_digest_field ( http, field, value );
|
|
109
|
+ }
|
|
110
|
+ }
|
|
111
|
+
|
|
112
|
+ /* Allow HTTP request to be retried if the request had not
|
|
113
|
+ * already tried authentication.
|
|
114
|
+ */
|
|
115
|
+ if ( ! http->request.auth.auth )
|
|
116
|
+ http->response.flags |= HTTP_RESPONSE_RETRY;
|
|
117
|
+
|
|
118
|
+ return 0;
|
|
119
|
+}
|
|
120
|
+
|
48
|
121
|
/**
|
49
|
122
|
* Initialise HTTP Digest
|
50
|
123
|
*
|
|
@@ -95,13 +168,14 @@ static void http_digest_final ( struct md5_context *ctx, char *out,
|
95
|
168
|
* @ret rc Return status code
|
96
|
169
|
*/
|
97
|
170
|
static int http_digest_authenticate ( struct http_transaction *http ) {
|
98
|
|
- struct http_request_auth *req = &http->request.auth;
|
99
|
|
- struct http_response_auth *rsp = &http->response.auth;
|
|
171
|
+ struct http_request_auth_digest *req = &http->request.auth.digest;
|
|
172
|
+ struct http_response_auth_digest *rsp = &http->response.auth.digest;
|
100
|
173
|
char ha1[ base16_encoded_len ( MD5_DIGEST_SIZE ) + 1 /* NUL */ ];
|
101
|
174
|
char ha2[ base16_encoded_len ( MD5_DIGEST_SIZE ) + 1 /* NUL */ ];
|
102
|
175
|
static const char md5sess[] = "MD5-sess";
|
103
|
176
|
static const char md5[] = "MD5";
|
104
|
177
|
struct md5_context ctx;
|
|
178
|
+ const char *password;
|
105
|
179
|
|
106
|
180
|
/* Check for required response parameters */
|
107
|
181
|
if ( ! rsp->realm ) {
|
|
@@ -122,7 +196,7 @@ static int http_digest_authenticate ( struct http_transaction *http ) {
|
122
|
196
|
return -EACCES_USERNAME;
|
123
|
197
|
}
|
124
|
198
|
req->username = http->uri->user;
|
125
|
|
- req->password = ( http->uri->password ? http->uri->password : "" );
|
|
199
|
+ password = ( http->uri->password ? http->uri->password : "" );
|
126
|
200
|
|
127
|
201
|
/* Handle quality of protection */
|
128
|
202
|
if ( rsp->qop ) {
|
|
@@ -146,7 +220,7 @@ static int http_digest_authenticate ( struct http_transaction *http ) {
|
146
|
220
|
http_digest_init ( &ctx );
|
147
|
221
|
http_digest_update ( &ctx, req->username );
|
148
|
222
|
http_digest_update ( &ctx, rsp->realm );
|
149
|
|
- http_digest_update ( &ctx, req->password );
|
|
223
|
+ http_digest_update ( &ctx, password );
|
150
|
224
|
http_digest_final ( &ctx, ha1, sizeof ( ha1 ) );
|
151
|
225
|
if ( req->algorithm == md5sess ) {
|
152
|
226
|
http_digest_init ( &ctx );
|
|
@@ -187,8 +261,8 @@ static int http_digest_authenticate ( struct http_transaction *http ) {
|
187
|
261
|
*/
|
188
|
262
|
static int http_format_digest_auth ( struct http_transaction *http,
|
189
|
263
|
char *buf, size_t len ) {
|
190
|
|
- struct http_request_auth *req = &http->request.auth;
|
191
|
|
- struct http_response_auth *rsp = &http->response.auth;
|
|
264
|
+ struct http_request_auth_digest *req = &http->request.auth.digest;
|
|
265
|
+ struct http_response_auth_digest *rsp = &http->response.auth.digest;
|
192
|
266
|
size_t used = 0;
|
193
|
267
|
|
194
|
268
|
/* Sanity checks */
|
|
@@ -225,6 +299,7 @@ static int http_format_digest_auth ( struct http_transaction *http,
|
225
|
299
|
/** HTTP Digest authentication scheme */
|
226
|
300
|
struct http_authentication http_digest_auth __http_authentication = {
|
227
|
301
|
.name = "Digest",
|
|
302
|
+ .parse = http_parse_digest_auth,
|
228
|
303
|
.authenticate = http_digest_authenticate,
|
229
|
304
|
.format = http_format_digest_auth,
|
230
|
305
|
};
|