* Fixed a memory leak when doing repeated re-negotiations.
[platform/upstream/curl.git] / lib / http_digest.c
1 /***************************************************************************
2  *                                  _   _ ____  _     
3  *  Project                     ___| | | |  _ \| |    
4  *                             / __| | | | |_) | |    
5  *                            | (__| |_| |  _ <| |___ 
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
10  * This software is licensed as described in the file COPYING, which
11  * you should have received as part of this distribution. The terms
12  * are also available at http://curl.haxx.se/docs/copyright.html.
13  * 
14  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15  * copies of the Software, and permit persons to whom the Software is
16  * furnished to do so, under the terms of the COPYING file.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  * $Id$
22  ***************************************************************************/
23 #include "setup.h"
24
25 #ifndef CURL_DISABLE_HTTP
26 /* -- WIN32 approved -- */
27 #include <stdio.h>
28 #include <string.h>
29 #include <stdarg.h>
30 #include <stdlib.h>
31 #include <ctype.h>
32
33 #include "urldata.h"
34 #include "sendf.h"
35 #include "strequal.h"
36
37 #include "md5.h"
38 #include "http_digest.h"
39
40 #define _MPRINTF_REPLACE /* use our functions only */
41 #include <curl/mprintf.h>
42
43 /* The last #include file should be: */
44 #ifdef CURLDEBUG
45 #include "memdebug.h"
46 #endif
47
48 /* Test example header:
49
50 WWW-Authenticate: Digest realm="testrealm", nonce="1053604598"
51
52 */
53
54 CURLdigest Curl_input_digest(struct connectdata *conn,
55                              char *header) /* rest of the www-authenticate:
56                                               header */
57 {
58   bool more = TRUE;
59   struct SessionHandle *data=conn->data;
60   bool before = FALSE; /* got a nonce before */
61   struct digestdata *d = &data->state.digest;
62
63   /* skip initial whitespaces */
64   while(*header && isspace((int)*header))
65     header++;
66
67   if(checkprefix("Digest", header)) {
68     header += strlen("Digest");
69
70     /* If we already have received a nonce, keep that in mind */
71     if(d->nonce)
72       before = TRUE;
73
74     /* clear off any former leftovers and init to defaults */
75     Curl_digest_cleanup(data);
76
77     while(more) {
78       char value[32];
79       char content[128];
80       size_t totlen=0;
81
82       while(*header && isspace((int)*header))
83         header++;
84     
85       /* how big can these strings be? */
86       if((2 == sscanf(header, "%31[^=]=\"%127[^\"]\"",
87                       value, content)) ||
88          /* try the same scan but without quotes around the content but don't
89             include the possibly trailing comma */
90          (2 ==  sscanf(header, "%31[^=]=%127[^,]",
91                        value, content)) ) {
92         if(strequal(value, "nonce")) {
93           d->nonce = strdup(content);
94         }
95         else if(strequal(value, "stale")) {
96           if(strequal(content, "true"))
97             d->stale = TRUE;
98         }
99         else if(strequal(value, "cnonce")) {
100           d->cnonce = strdup(content);
101         }
102         else if(strequal(value, "realm")) {
103           d->realm = strdup(content);
104         }
105         else if(strequal(value, "algorithm")) {
106           if(strequal(content, "MD5-sess"))
107             d->algo = CURLDIGESTALGO_MD5SESS;
108           else if(strequal(content, "MD5"))
109             d->algo = CURLDIGESTALGO_MD5;
110           else
111             return CURLDIGEST_BADALGO;
112         }
113         else {
114           /* unknown specifier, ignore it! */
115         }
116         totlen = strlen(value)+strlen(content)+3;
117       }
118       else 
119         break; /* we're done here */
120         
121       header += totlen;
122       if(',' == *header)
123         /* allow the list to be comma-separated */
124         header++; 
125     }
126     /* We had a nonce since before, and we got another one now without
127     'stale=true'. This means we provided bad credentials in the previous
128     request */
129
130     if(before && !d->stale)
131       return CURLDIGEST_BAD;
132
133     /* We got this header without a nonce, that's a bad Digest line! */
134     if(!d->nonce)
135       return CURLDIGEST_BAD;
136   }
137   else 
138     /* else not a digest, get out */
139     return CURLDIGEST_NONE;
140
141   return CURLDIGEST_FINE;
142 }
143
144 /* convert md5 chunk to RFC2617 (section 3.1.3) -suitable ascii string*/
145 static void md5_to_ascii(unsigned char *source, /* 16 bytes */
146                          unsigned char *dest) /* 33 bytes */
147 {
148   int i;
149   for(i=0; i<16; i++)
150     sprintf((char *)&dest[i*2], "%02x", source[i]);
151 }
152
153 CURLcode Curl_output_digest(struct connectdata *conn,
154                             unsigned char *request,
155                             unsigned char *uripath)
156 {
157   /* We have a Digest setup for this, use it!  Now, to get all the details for
158      this sorted out, I must urge you dear friend to read up on the RFC2617
159      section 3.2.2, */
160   unsigned char md5buf[16]; /* 16 bytes/128 bits */
161   unsigned char ha1[33]; /* 32 digits and 1 zero byte */
162   unsigned char ha2[33];
163   unsigned char request_digest[33];
164   unsigned char *md5this;
165
166   struct SessionHandle *data = conn->data;
167   struct digestdata *d = &data->state.digest;
168
169   /*
170     if the algorithm is "MD5" or unspecified (which then defaults to MD5):
171     
172     A1 = unq(username-value) ":" unq(realm-value) ":" passwd
173
174     if the algorithm is "MD5-sess" then:
175
176     A1 = H( unq(username-value) ":" unq(realm-value) ":" passwd )
177          ":" unq(nonce-value) ":" unq(cnonce-value)
178   */
179   if(d->algo == CURLDIGESTALGO_MD5SESS) {
180     md5this = (unsigned char *)
181       aprintf("%s:%s:%s:%s:%s",
182               conn->user,
183               d->realm,
184               conn->passwd,
185               d->nonce,
186               d->cnonce);
187   }
188   else {
189     md5this = (unsigned char *)
190       aprintf("%s:%s:%s",
191               conn->user,
192               d->realm,
193               conn->passwd);
194   }
195   Curl_md5it(md5buf, md5this);
196   free(md5this); /* free this again */
197   md5_to_ascii(md5buf, ha1);
198
199   /*
200     A2 = Method ":" digest-uri-value
201     
202     (The "Method" value is the HTTP request method as specified in section
203     5.1.1 of RFC 2616)
204   */
205
206   md5this = (unsigned char *)aprintf("%s:%s", request, uripath);
207   Curl_md5it(md5buf, md5this);
208   free(md5this); /* free this again */
209   md5_to_ascii(md5buf, ha2);
210   
211   md5this = (unsigned char *)aprintf("%s:%s:%s", ha1, d->nonce,
212                                      ha2);
213   Curl_md5it(md5buf, md5this);
214   free(md5this); /* free this again */
215   md5_to_ascii(md5buf, request_digest);
216
217   /* for test case 64 (snooped from a Mozilla 1.3a request)
218
219     Authorization: Digest username="testuser", realm="testrealm", \
220     nonce="1053604145", uri="/64", response="c55f7f30d83d774a3d2dcacf725abaca"
221   */
222
223   Curl_safefree(conn->allocptr.userpwd);
224   conn->allocptr.userpwd =
225     aprintf( "Authorization: Digest "
226              "username=\"%s\", "
227              "realm=\"%s\", "
228              "nonce=\"%s\", "
229              "uri=\"%s\", "
230              "response=\"%s\"\r\n",
231              conn->user,
232              d->realm,
233              d->nonce,
234              uripath, /* this is the PATH part of the URL */ 
235              request_digest );
236
237   return CURLE_OK;
238 }
239
240 void Curl_digest_cleanup(struct SessionHandle *data)
241 {
242   struct digestdata *d = &data->state.digest;
243
244   if(d->nonce)
245     free(d->nonce);
246   d->nonce = NULL;
247
248   if(d->cnonce)
249     free(d->cnonce);
250   d->cnonce = NULL;
251
252   if(d->realm)
253     free(d->realm);
254   d->realm = NULL;
255
256   d->algo = CURLDIGESTALGO_MD5; /* default algorithm */
257
258   d->stale = FALSE; /* default means normal, not stale */
259 }
260
261 #endif