2 * Copyright (C) 2015 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
29 * Hyper Text Transfer Protocol (HTTP) Digest authentication
38 #include <ipxe/base16.h>
39 #include <ipxe/vsprintf.h>
40 #include <ipxe/http.h>
42 /* Disambiguate the various error causes */
43 #define EACCES_USERNAME __einfo_error ( EINFO_EACCES_USERNAME )
44 #define EINFO_EACCES_USERNAME \
45 __einfo_uniqify ( EINFO_EACCES, 0x01, \
46 "No username available for Digest authentication" )
49 * Initialise HTTP Digest
51 * @v ctx Digest context
52 * @v string Initial string
54 static void http_digest_init ( struct md5_context *ctx ) {
56 /* Initialise MD5 digest */
57 digest_init ( &md5_algorithm, ctx );
61 * Update HTTP Digest with new data
63 * @v ctx Digest context
64 * @v string String to append
66 static void http_digest_update ( struct md5_context *ctx, const char *string ) {
67 static const char colon = ':';
69 /* Add (possibly colon-separated) field to MD5 digest */
71 digest_update ( &md5_algorithm, ctx, &colon, sizeof ( colon ) );
72 digest_update ( &md5_algorithm, ctx, string, strlen ( string ) );
76 * Finalise HTTP Digest
78 * @v ctx Digest context
79 * @v out Buffer for digest output
80 * @v len Buffer length
82 static void http_digest_final ( struct md5_context *ctx, char *out,
84 uint8_t digest[MD5_DIGEST_SIZE];
86 /* Finalise and base16-encode MD5 digest */
87 digest_final ( &md5_algorithm, ctx, digest );
88 base16_encode ( digest, sizeof ( digest ), out, len );
92 * Perform HTTP Digest authentication
94 * @v http HTTP transaction
95 * @ret rc Return status code
97 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;
100 char ha1[ base16_encoded_len ( MD5_DIGEST_SIZE ) + 1 /* NUL */ ];
101 char ha2[ base16_encoded_len ( MD5_DIGEST_SIZE ) + 1 /* NUL */ ];
102 static const char md5sess[] = "MD5-sess";
103 static const char md5[] = "MD5";
104 struct md5_context ctx;
106 /* Check for required response parameters */
107 if ( ! rsp->realm ) {
108 DBGC ( http, "HTTP %p has no realm for Digest authentication\n",
112 if ( ! rsp->nonce ) {
113 DBGC ( http, "HTTP %p has no nonce for Digest authentication\n",
118 /* Record username and password */
119 if ( ! http->uri->user ) {
120 DBGC ( http, "HTTP %p has no username for Digest "
121 "authentication\n", http );
122 return -EACCES_USERNAME;
124 req->username = http->uri->user;
125 req->password = ( http->uri->password ? http->uri->password : "" );
127 /* Handle quality of protection */
130 /* Use "auth" in subsequent request */
133 /* Generate a client nonce */
134 snprintf ( req->cnonce, sizeof ( req->cnonce ),
137 /* Determine algorithm */
138 req->algorithm = md5;
139 if ( rsp->algorithm &&
140 ( strcasecmp ( rsp->algorithm, md5sess ) == 0 ) ) {
141 req->algorithm = md5sess;
146 http_digest_init ( &ctx );
147 http_digest_update ( &ctx, req->username );
148 http_digest_update ( &ctx, rsp->realm );
149 http_digest_update ( &ctx, req->password );
150 http_digest_final ( &ctx, ha1, sizeof ( ha1 ) );
151 if ( req->algorithm == md5sess ) {
152 http_digest_init ( &ctx );
153 http_digest_update ( &ctx, ha1 );
154 http_digest_update ( &ctx, rsp->nonce );
155 http_digest_update ( &ctx, req->cnonce );
156 http_digest_final ( &ctx, ha1, sizeof ( ha1 ) );
160 http_digest_init ( &ctx );
161 http_digest_update ( &ctx, http->request.method->name );
162 http_digest_update ( &ctx, http->request.uri );
163 http_digest_final ( &ctx, ha2, sizeof ( ha2 ) );
165 /* Generate response */
166 http_digest_init ( &ctx );
167 http_digest_update ( &ctx, ha1 );
168 http_digest_update ( &ctx, rsp->nonce );
170 http_digest_update ( &ctx, HTTP_DIGEST_NC );
171 http_digest_update ( &ctx, req->cnonce );
172 http_digest_update ( &ctx, req->qop );
174 http_digest_update ( &ctx, ha2 );
175 http_digest_final ( &ctx, req->response, sizeof ( req->response ) );
181 * Construct HTTP "Authorization" header for Digest authentication
183 * @v http HTTP transaction
185 * @v len Length of buffer
186 * @ret len Length of header value, or negative error
188 static int http_format_digest_auth ( struct http_transaction *http,
189 char *buf, size_t len ) {
190 struct http_request_auth *req = &http->request.auth;
191 struct http_response_auth *rsp = &http->response.auth;
195 assert ( rsp->realm != NULL );
196 assert ( rsp->nonce != NULL );
197 assert ( req->username != NULL );
199 assert ( req->algorithm != NULL );
200 assert ( req->cnonce[0] != '\0' );
202 assert ( req->response[0] != '\0' );
204 /* Construct response */
205 used += ssnprintf ( ( buf + used ), ( len - used ),
206 "realm=\"%s\", nonce=\"%s\", uri=\"%s\", "
207 "username=\"%s\"", rsp->realm, rsp->nonce,
208 http->request.uri, req->username );
210 used += ssnprintf ( ( buf + used ), ( len - used ),
211 ", opaque=\"%s\"", rsp->opaque );
214 used += ssnprintf ( ( buf + used ), ( len - used ),
215 ", qop=%s, algorithm=%s, cnonce=\"%s\", "
216 "nc=" HTTP_DIGEST_NC, req->qop,
217 req->algorithm, req->cnonce );
219 used += ssnprintf ( ( buf + used ), ( len - used ),
220 ", response=\"%s\"", req->response );
225 /** HTTP Digest authentication scheme */
226 struct http_authentication http_digest_auth __http_authentication = {
228 .authenticate = http_digest_authenticate,
229 .format = http_format_digest_auth,
232 /* Drag in HTTP authentication support */
233 REQUIRING_SYMBOL ( http_digest_auth );
234 REQUIRE_OBJECT ( httpauth );