printer.c

Go to the documentation of this file.
00001 /* printer.h --- Convert DIGEST-MD5 token structures into strings.
00002  * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009  Simon Josefsson
00003  *
00004  * This file is part of GNU SASL Library.
00005  *
00006  * GNU SASL Library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public License
00008  * as published by the Free Software Foundation; either version 2.1 of
00009  * the License, or (at your option) any later version.
00010  *
00011  * GNU SASL Library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with GNU SASL Library; if not, write to the Free
00018  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019  * Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #ifdef HAVE_CONFIG_H
00024 # include "config.h"
00025 #endif
00026 
00027 /* Get prototypes. */
00028 #include "printer.h"
00029 
00030 /* Get free. */
00031 #include <stdlib.h>
00032 
00033 /* Get asprintf. */
00034 #include <stdio.h>
00035 
00036 /* Get token validator. */
00037 #include "validate.h"
00038 
00039 /* Append a key/value pair to a comma'd string list.  Additionally enclose
00040    the value in quotes if requested. */
00041 static int
00042 comma_append (char **dst, const char *key, const char *value, int quotes)
00043 {
00044   char *tmp;
00045   int result;
00046 
00047   if (*dst)
00048     if (value)
00049       if (quotes)
00050         result = asprintf (&tmp, "%s, %s=\"%s\"", *dst, key, value);
00051       else
00052         result = asprintf (&tmp, "%s, %s=%s", *dst, key, value);
00053     else
00054       result = asprintf (&tmp, "%s, %s", *dst, key);
00055   else if (value)
00056     if (quotes)
00057       result = asprintf (&tmp, "%s=\"%s\"", key, value);
00058     else
00059       result = asprintf (&tmp, "%s=%s", key, value);
00060   else
00061     result = asprintf (&tmp, "%s", key);
00062 
00063   if (result < 0)
00064     return result;
00065 
00066   if (*dst)
00067     free (*dst);
00068 
00069   *dst = tmp;
00070 
00071   return result;
00072 }
00073 
00074 char *
00075 digest_md5_print_challenge (digest_md5_challenge * c)
00076 {
00077   char *out = NULL;
00078   size_t i;
00079 
00080   /* Below we assume the mandatory fields are present, verify that
00081      first to avoid crashes. */
00082   if (digest_md5_validate_challenge (c) != 0)
00083     return NULL;
00084 
00085   for (i = 0; i < c->nrealms; i++)
00086     {
00087       if (comma_append (&out, "realm", c->realms[i], 1) < 0)
00088         {
00089           free (out);
00090           return NULL;
00091         }
00092     }
00093 
00094   if (c->nonce)
00095     if (comma_append (&out, "nonce", c->nonce, 1) < 0)
00096       {
00097         free (out);
00098         return NULL;
00099       }
00100 
00101   if (c->qops)
00102     {
00103       char *tmp = NULL;
00104 
00105       if (c->qops & DIGEST_MD5_QOP_AUTH)
00106         if (comma_append (&tmp, "auth", NULL, 0) < 0)
00107           {
00108             free (tmp);
00109             free (out);
00110             return NULL;
00111           }
00112 
00113       if (c->qops & DIGEST_MD5_QOP_AUTH_INT)
00114         if (comma_append (&tmp, "auth-int", NULL, 0) < 0)
00115           {
00116             free (tmp);
00117             free (out);
00118             return NULL;
00119           }
00120 
00121       if (c->qops & DIGEST_MD5_QOP_AUTH_CONF)
00122         if (comma_append (&tmp, "auth-conf", NULL, 0) < 0)
00123           {
00124             free (tmp);
00125             free (out);
00126             return NULL;
00127           }
00128 
00129       if (comma_append (&out, "qop", tmp, 1) < 0)
00130         {
00131           free (tmp);
00132           free (out);
00133           return NULL;
00134         }
00135 
00136       free (tmp);
00137     }
00138 
00139   if (c->stale)
00140     if (comma_append (&out, "stale", "true", 0) < 0)
00141       {
00142         free (out);
00143         return NULL;
00144       }
00145 
00146   if (c->servermaxbuf)
00147     {
00148       char *tmp;
00149 
00150       if (asprintf (&tmp, "%lu", c->servermaxbuf) < 0)
00151         {
00152           free (out);
00153           return NULL;
00154         }
00155 
00156       if (comma_append (&out, "maxbuf", tmp, 0) < 0)
00157         {
00158           free (out);
00159           return NULL;
00160         }
00161 
00162       free (tmp);
00163     }
00164 
00165   if (c->utf8)
00166     if (comma_append (&out, "charset", "utf-8", 0) < 0)
00167       {
00168         free (out);
00169         return NULL;
00170       }
00171 
00172   if (comma_append (&out, "algorithm", "md5-sess", 0) < 0)
00173     {
00174       free (out);
00175       return NULL;
00176     }
00177 
00178   if (c->ciphers)
00179     {
00180       char *tmp = NULL;
00181 
00182       if (c->ciphers & DIGEST_MD5_CIPHER_3DES)
00183         if (comma_append (&tmp, "3des", NULL, 0) < 0)
00184           {
00185             free (tmp);
00186             free (out);
00187             return NULL;
00188           }
00189 
00190       if (c->ciphers & DIGEST_MD5_CIPHER_DES)
00191         if (comma_append (&tmp, "des", NULL, 0) < 0)
00192           {
00193             free (tmp);
00194             free (out);
00195             return NULL;
00196           }
00197 
00198       if (c->ciphers & DIGEST_MD5_CIPHER_RC4_40)
00199         if (comma_append (&tmp, "rc4-40", NULL, 0) < 0)
00200           {
00201             free (tmp);
00202             free (out);
00203             return NULL;
00204           }
00205 
00206       if (c->ciphers & DIGEST_MD5_CIPHER_RC4)
00207         if (comma_append (&tmp, "rc4", NULL, 0) < 0)
00208           {
00209             free (tmp);
00210             free (out);
00211             return NULL;
00212           }
00213 
00214       if (c->ciphers & DIGEST_MD5_CIPHER_RC4_56)
00215         if (comma_append (&tmp, "rc4-56", NULL, 0) < 0)
00216           {
00217             free (tmp);
00218             free (out);
00219             return NULL;
00220           }
00221 
00222       if (c->ciphers & DIGEST_MD5_CIPHER_AES_CBC)
00223         if (comma_append (&tmp, "aes-cbc", NULL, 0) < 0)
00224           {
00225             free (tmp);
00226             free (out);
00227             return NULL;
00228           }
00229 
00230       if (comma_append (&out, "cipher", tmp, 1) < 0)
00231         {
00232           free (tmp);
00233           free (out);
00234           return NULL;
00235         }
00236 
00237       free (tmp);
00238     }
00239 
00240   return out;
00241 }
00242 
00243 char *
00244 digest_md5_print_response (digest_md5_response * r)
00245 {
00246   char *out = NULL;
00247   const char *qop = NULL;
00248   const char *cipher = NULL;
00249 
00250   /* Below we assume the mandatory fields are present, verify that
00251      first to avoid crashes. */
00252   if (digest_md5_validate_response (r) != 0)
00253     return NULL;
00254 
00255   if (r->qop & DIGEST_MD5_QOP_AUTH_CONF)
00256     qop = "qop=auth-conf";
00257   else if (r->qop & DIGEST_MD5_QOP_AUTH_INT)
00258     qop = "qop=auth-int";
00259   else if (r->qop & DIGEST_MD5_QOP_AUTH)
00260     qop = "qop=auth";
00261 
00262   if (r->cipher & DIGEST_MD5_CIPHER_3DES)
00263     cipher = "cipher=3des";
00264   else if (r->cipher & DIGEST_MD5_CIPHER_DES)
00265     cipher = "cipher=des";
00266   else if (r->cipher & DIGEST_MD5_CIPHER_RC4_40)
00267     cipher = "cipher=rc4-40";
00268   else if (r->cipher & DIGEST_MD5_CIPHER_RC4)
00269     cipher = "cipher=rc4";
00270   else if (r->cipher & DIGEST_MD5_CIPHER_RC4_56)
00271     cipher = "cipher=rc4-56";
00272   else if (r->cipher & DIGEST_MD5_CIPHER_AES_CBC)
00273     cipher = "cipher=aes-cbc";
00274   else if (r->cipher & DIGEST_MD5_CIPHER_3DES)
00275     cipher = "cipher=3des";
00276 
00277   if (r->username)
00278     if (comma_append (&out, "username", r->username, 1) < 0)
00279       {
00280         free (out);
00281         return NULL;
00282       }
00283 
00284   if (r->realm)
00285     if (comma_append (&out, "realm", r->realm, 1) < 0)
00286       {
00287         free (out);
00288         return NULL;
00289       }
00290 
00291   if (r->nonce)
00292     if (comma_append (&out, "nonce", r->nonce, 1) < 0)
00293       {
00294         free (out);
00295         return NULL;
00296       }
00297 
00298   if (r->cnonce)
00299     if (comma_append (&out, "cnonce", r->cnonce, 1) < 0)
00300       {
00301         free (out);
00302         return NULL;
00303       }
00304 
00305   if (r->nc)
00306     {
00307       char *tmp;
00308 
00309       if (asprintf (&tmp, "%08lx", r->nc) < 0)
00310         {
00311           free (out);
00312           return NULL;
00313         }
00314 
00315       if (comma_append (&out, "nc", tmp, 0) < 0)
00316         {
00317           free (tmp);
00318           free (out);
00319           return NULL;
00320         }
00321 
00322       free (tmp);
00323     }
00324 
00325   if (qop)
00326     if (comma_append (&out, qop, NULL, 0) < 0)
00327       {
00328         free (out);
00329         return NULL;
00330       }
00331 
00332   if (r->digesturi)
00333     if (comma_append (&out, "digest-uri", r->digesturi, 1) < 0)
00334       {
00335         free (out);
00336         return NULL;
00337       }
00338 
00339   if (r->response)
00340     if (comma_append (&out, "response", r->response, 0) < 0)
00341       {
00342         free (out);
00343         return NULL;
00344       }
00345 
00346   if (r->clientmaxbuf)
00347     {
00348       char *tmp;
00349 
00350       if (asprintf (&tmp, "%lu", r->clientmaxbuf) < 0)
00351         {
00352           free (out);
00353           return NULL;
00354         }
00355 
00356       if (comma_append (&out, "maxbuf", tmp, 0) < 0)
00357         {
00358           free (tmp);
00359           free (out);
00360           return NULL;
00361         }
00362 
00363       free (tmp);
00364     }
00365 
00366   if (r->utf8)
00367     if (comma_append (&out, "charset", "utf-8", 0) < 0)
00368       {
00369         free (out);
00370         return NULL;
00371       }
00372 
00373   if (cipher)
00374     if (comma_append (&out, cipher, NULL, 0) < 0)
00375       {
00376         free (out);
00377         return NULL;
00378       }
00379 
00380   if (r->authzid)
00381     if (comma_append (&out, "authzid", r->authzid, 1) < 0)
00382       {
00383         free (out);
00384         return NULL;
00385       }
00386 
00387   return out;
00388 }
00389 
00390 char *
00391 digest_md5_print_finish (digest_md5_finish * finish)
00392 {
00393   char *out;
00394 
00395   /* Below we assume the mandatory fields are present, verify that
00396      first to avoid crashes. */
00397   if (digest_md5_validate_finish (finish) != 0)
00398     return NULL;
00399 
00400   if (asprintf (&out, "rspauth=%s", finish->rspauth) < 0)
00401     return NULL;
00402 
00403   return out;
00404 }

Generated on Wed Mar 25 16:12:02 2009 for gsasl by  doxygen 1.5.6