gsasl  1.8.0
qop.c
Go to the documentation of this file.
00001 /* qop.c --- DIGEST-MD5 QOP handling.
00002  * Copyright (C) 2002-2012 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 "qop.h"
00029 
00030 #include "tokens.h"
00031 #include "parser.h"
00032 
00033 #include <string.h>
00034 #include <stdlib.h>
00035 
00036 int
00037 digest_md5_qopstr2qops (const char *qopstr)
00038 {
00039   int qops = 0;
00040   enum
00041   {
00042     /* the order must match the following struct */
00043     QOP_AUTH = 0,
00044     QOP_AUTH_INT,
00045     QOP_AUTH_CONF
00046   };
00047   const char *const qop_opts[] = {
00048     /* the order must match the previous enum */
00049     "qop-auth",
00050     "qop-int",
00051     "qop-conf",
00052     NULL
00053   };
00054   char *subsubopts;
00055   char *val;
00056   char *qopdup;
00057 
00058   if (!qopstr)
00059     return 0;
00060 
00061   qopdup = strdup (qopstr);
00062   if (!qopdup)
00063     return -1;
00064 
00065   subsubopts = qopdup;
00066   while (*subsubopts != '\0')
00067     switch (digest_md5_getsubopt (&subsubopts, qop_opts, &val))
00068       {
00069       case QOP_AUTH:
00070         qops |= DIGEST_MD5_QOP_AUTH;
00071         break;
00072 
00073       case QOP_AUTH_INT:
00074         qops |= DIGEST_MD5_QOP_AUTH_INT;
00075         break;
00076 
00077       case QOP_AUTH_CONF:
00078         qops |= DIGEST_MD5_QOP_AUTH_CONF;
00079         break;
00080 
00081       default:
00082         /* ignore unrecognized options */
00083         break;
00084       }
00085 
00086   free (qopdup);
00087 
00088   return qops;
00089 }
00090 
00091 const char *
00092 digest_md5_qops2qopstr (int qops)
00093 {
00094   const char *qopstr[] = {
00095     /* 0 */ "qop-auth",
00096     /* 1 */ "qop-auth",
00097     /* 2 */ "qop-int",
00098     /* 3 */ "qop-auth, qop-int",
00099     /* 4 */ "qop-conf",
00100     /* 5 */ "qop-auth, qop-conf",
00101     /* 6 */ "qop-int, qop-conf",
00102     /* 7 */ "qop-auth, qop-int, qop-conf"
00103   };
00104 
00105   return qopstr[qops & 0x07];
00106 }