summaryrefslogtreecommitdiffstats
path: root/tinyHTTP/src/auth/thttp_challenge.c
diff options
context:
space:
mode:
Diffstat (limited to 'tinyHTTP/src/auth/thttp_challenge.c')
-rwxr-xr-xtinyHTTP/src/auth/thttp_challenge.c83
1 files changed, 40 insertions, 43 deletions
diff --git a/tinyHTTP/src/auth/thttp_challenge.c b/tinyHTTP/src/auth/thttp_challenge.c
index 649a393..0963b23 100755
--- a/tinyHTTP/src/auth/thttp_challenge.c
+++ b/tinyHTTP/src/auth/thttp_challenge.c
@@ -46,19 +46,19 @@ thttp_challenge_t* thttp_challenge_create(tsk_bool_t isproxy, const char* scheme
{
thttp_challenge_t* challenge = tsk_object_new(thttp_challenge_def_t);
if (challenge) {
-
+
challenge->isproxy = isproxy;
challenge->scheme = tsk_strdup(scheme);
challenge->realm = tsk_strdup(realm);
challenge->nonce = tsk_strdup(nonce);
challenge->opaque = tsk_strdup(opaque);
challenge->algorithm = tsk_strdup(algorithm);
-
+
if (!tsk_strnullORempty(qop)) {
challenge->qop = tsk_strcontains(qop, tsk_strlen(qop), "auth-int") ? "auth-int" :
- (tsk_strcontains(qop, tsk_strlen(qop), "auth") ? "auth" : tsk_null);
+ (tsk_strcontains(qop, tsk_strlen(qop), "auth") ? "auth" : tsk_null);
}
-
+
if (challenge->qop) {
_thttp_challenge_reset_cnonce(challenge);
}
@@ -77,15 +77,13 @@ thttp_challenge_t* thttp_challenge_dup(const thttp_challenge_t* self)
static int _thttp_challenge_reset_cnonce(thttp_challenge_t *self)
{
- if (self)
- {
- if (self->qop) /* client nonce is only used if qop=auth, auth-int or both */
- {
+ if (self) {
+ if (self->qop) { /* client nonce is only used if qop=auth, auth-int or both */
#if 0
memcpy(self->cnonce, "f221681c1e42fb5f8f9957bf7e72eb2b", 32);
#else
tsk_istr_t istr;
-
+
tsk_strrandom(&istr);
tsk_md5compute(istr, tsk_strlen(istr), &self->cnonce);
#endif
@@ -97,15 +95,15 @@ static int _thttp_challenge_reset_cnonce(thttp_challenge_t *self)
int thttp_challenge_get_digest_response(thttp_challenge_t *self, const char* username, const char* password, const char* method, const char* uristring, const tsk_buffer_t* entity_body, char** response)
{
- if (THTTP_CHALLENGE_IS_DIGEST(self)){
+ if (THTTP_CHALLENGE_IS_DIGEST(self)) {
tsk_md5string_t ha1, ha2, md5_response;
nonce_count_t nc;
-
+
/* ===
Calculate HA1 = MD5(A1) = M5(username:realm:secret)
*/
thttp_auth_digest_HA1(username, self->realm, password, &ha1);
-
+
/* ===
HA2
*/
@@ -114,9 +112,9 @@ int thttp_challenge_get_digest_response(thttp_challenge_t *self, const char* use
entity_body,
self->qop,
&ha2);
-
+
/* RESPONSE */
- if (self->nc){
+ if (self->nc) {
THTTP_NCOUNT_2_STRING(self->nc, nc);
}
thttp_auth_digest_response((const tsk_md5string_t *)&ha1,
@@ -126,14 +124,14 @@ int thttp_challenge_get_digest_response(thttp_challenge_t *self, const char* use
self->qop,
(const tsk_md5string_t *)&ha2,
&md5_response);
-
- if (self->qop){
+
+ if (self->qop) {
self->nc++;
}
- if (response && !*response){
+ if (response && !*response) {
*response = tsk_strdup(md5_response);
}
-
+
return 0;
}
return -1;
@@ -143,7 +141,7 @@ int thttp_challenge_update(thttp_challenge_t *self, const char* scheme, const ch
{
if (self) {
int noncechanged = !tsk_striequals(self->nonce, nonce);
-
+
tsk_strupdate(&self->scheme, scheme);
tsk_strupdate(&self->realm, realm);
tsk_strupdate(&self->nonce, nonce);
@@ -151,9 +149,9 @@ int thttp_challenge_update(thttp_challenge_t *self, const char* scheme, const ch
tsk_strupdate(&self->algorithm, algorithm);
if (qop) {
self->qop = tsk_strcontains(qop, tsk_strlen(qop), "auth-int") ? "auth-int" :
- (tsk_strcontains(qop, tsk_strlen(qop), "auth") ? "auth" : tsk_null);
+ (tsk_strcontains(qop, tsk_strlen(qop), "auth") ? "auth" : tsk_null);
}
-
+
if (noncechanged && self->qop) {
_thttp_challenge_reset_cnonce(self);
}
@@ -166,12 +164,12 @@ thttp_header_t *thttp_challenge_create_header_authorization(thttp_challenge_t *s
{
char *uristring = tsk_null;
thttp_header_t *header = tsk_null;
-
+
if (!self || !request || !request->line.request.url) {
TSK_DEBUG_ERROR("Invalid parameter");
goto bail;
}
-
+
/* Sets URI: hpath do not start with / ==> append a '/'*/
tsk_sprintf(&uristring, "/%s", request->line.request.url->hpath ? request->line.request.url->hpath : "");
header = thttp_challenge_create_header_authorization_2(self, username, password, request->line.request.method, uristring, request->Content);
@@ -186,20 +184,20 @@ thttp_header_t *thttp_challenge_create_header_authorization_2(thttp_challenge_t
tsk_size_t response_size = 0;
nonce_count_t nc;
thttp_header_t *header = tsk_null;
-
+
if (!self || tsk_strnullORempty(uristring)) {
TSK_DEBUG_ERROR("Invalid parameter");
goto bail;
}
-
+
/* We compute the nc here because @ref thttp_challenge_get_response function will increment it's value. */
if (self->nc) {
THTTP_NCOUNT_2_STRING(self->nc, nc);
}
-
+
/* Computes the response (Basic and Digest)*/
if (THTTP_CHALLENGE_IS_DIGEST(self)) {
- if (thttp_challenge_get_digest_response(self, username, password, method, uristring, entity_body, &response)){
+ if (thttp_challenge_get_digest_response(self, username, password, method, uristring, entity_body, &response)) {
goto bail;
}
response_size = (TSK_MD5_DIGEST_SIZE * 2);
@@ -211,8 +209,8 @@ thttp_header_t *thttp_challenge_create_header_authorization_2(thttp_challenge_t
TSK_DEBUG_ERROR("%s not supported as scheme.", self->scheme);
goto bail;
}
-
-
+
+
#define THTTP_AUTH_COPY_VALUES(hdr) \
hdr->username = tsk_strdup(username); \
hdr->scheme = tsk_strdup(self->scheme); \
@@ -225,25 +223,25 @@ hdr->cnonce = self->nc? tsk_strdup(self->cnonce) : 0; \
hdr->uri = tsk_strdup(uristring); \
hdr->nc = self->nc? tsk_strdup(nc) : 0; \
hdr->response = tsk_strndup(response, response_size); \
-
+
if (self->isproxy) {
thttp_header_Proxy_Authorization_t *proxy_auth = thttp_header_authorization_create(); // Very bad way to create Proxy_auth header.
THTTP_HEADER(proxy_auth)->type = thttp_htype_Proxy_Authorization;
-
+
THTTP_AUTH_COPY_VALUES(proxy_auth);
header = THTTP_HEADER(proxy_auth);
}
- else{
+ else {
thttp_header_Authorization_t *auth = thttp_header_authorization_create();
THTTP_AUTH_COPY_VALUES(auth);
header = THTTP_HEADER(auth);
}
-
+
bail:
TSK_FREE(response);
-
+
return header;
-
+
#undef THTTP_AUTH_COPY_VALUES
}
@@ -279,9 +277,9 @@ static tsk_object_t* thttp_challenge_ctor(tsk_object_t *self, va_list * app)
{
thttp_challenge_t *challenge = self;
if (challenge) {
-
+
}
-
+
return self;
}
@@ -290,24 +288,23 @@ static tsk_object_t* thttp_challenge_ctor(tsk_object_t *self, va_list * app)
static tsk_object_t* thttp_challenge_dtor(tsk_object_t *self)
{
thttp_challenge_t *challenge = self;
- if (challenge){
+ if (challenge) {
TSK_FREE(challenge->scheme);
TSK_FREE(challenge->realm);
TSK_FREE(challenge->nonce);
TSK_FREE(challenge->opaque);
TSK_FREE(challenge->algorithm);
-
+
//TSK_FREE(challenge->qop);
}
- else{
+ else {
TSK_DEBUG_ERROR("Null HTTP challenge object.");
}
-
+
return self;
}
-static const tsk_object_def_t thttp_challenge_def_s =
-{
+static const tsk_object_def_t thttp_challenge_def_s = {
sizeof(thttp_challenge_t),
thttp_challenge_ctor,
thttp_challenge_dtor,
OpenPOWER on IntegriCloud