File indexing completed on 2024-06-23 04:03:33

0001 /*
0002  * Copyright (C) 2003  Justin Karneges
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Lesser General Public
0006  * License as published by the Free Software Foundation; either
0007  * either version 2
0008    of the License, or (at your option) any later version.1 of the License, or (at your option) any later version.
0009  *
0010  * This library is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013  * Lesser General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU Lesser General Public
0016  * License along with this library; if not, write to the Free Software
0017  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
0018  *
0019  */
0020 
0021 #include "xmpp/sasl/digestmd5proplist.h"
0022 
0023 namespace XMPP {
0024 
0025 DIGESTMD5PropList::DIGESTMD5PropList() : QList<DIGESTMD5Prop>() 
0026 {
0027 }
0028 
0029 void DIGESTMD5PropList::set(const QByteArray &var, const QByteArray &val) {
0030     DIGESTMD5Prop p;
0031     p.var = var;
0032     p.val = val;
0033     append(p);
0034 }
0035 
0036 QByteArray DIGESTMD5PropList::get(const QByteArray &var)
0037 {
0038     for(ConstIterator it = constBegin(); it != constEnd(); ++it) {
0039         if((*it).var == var)
0040             return (*it).val;
0041     }
0042     return QByteArray();
0043 }
0044 
0045 QByteArray DIGESTMD5PropList::toString() const
0046 {
0047     QByteArray str;
0048     bool first = true;
0049     for(ConstIterator it = begin(); it != end(); ++it) {
0050         if(!first)
0051             str += ',';
0052         if ((*it).var == "realm" || (*it).var == "nonce" || (*it).var == "username" || (*it).var == "cnonce" || (*it).var == "digest-uri" || (*it).var == "authzid")
0053             str += (*it).var + "=\"" + (*it).val + '\"';
0054         else 
0055             str += (*it).var + "=" + (*it).val;
0056         first = false;
0057     }
0058     return str;
0059 }
0060 
0061 bool DIGESTMD5PropList::fromString(const QByteArray &str)
0062 {
0063     DIGESTMD5PropList list;
0064     int at = 0;
0065     while(1) {
0066         while (at < str.length() && (str[at] == ',' || str[at] == ' ' || str[at] == '\t'))
0067                 ++at;
0068         int n = str.indexOf('=', at);
0069         if(n == -1)
0070             break;
0071         QByteArray var, val;
0072         var = str.mid(at, n-at);
0073         at = n + 1;
0074         if(str[at] == '\"') {
0075             ++at;
0076             n = str.indexOf('\"', at);
0077             if(n == -1)
0078                 break;
0079             val = str.mid(at, n-at);
0080             at = n + 1;
0081         }
0082         else {
0083             n = at;
0084             while (n < str.length() && str[n] != ',' && str[n] != ' ' && str[n] != '\t')
0085                 ++n;
0086             val = str.mid(at, n-at);
0087             at = n;
0088         }
0089         DIGESTMD5Prop prop;
0090         prop.var = var;
0091         if (var == "qop" || var == "cipher") {
0092             int a = 0;
0093             while (a < val.length()) {
0094                 while (a < val.length() && (val[a] == ',' || val[a] == ' ' || val[a] == '\t'))
0095                     ++a;
0096                 if (a == val.length())
0097                     break;
0098                 n = a+1;
0099                 while (n < val.length() && val[n] != ',' && val[n] != ' ' && val[n] != '\t')
0100                     ++n;
0101                 prop.val = val.mid(a, n-a);
0102                 list.append(prop);
0103                 a = n+1;
0104             }
0105         }
0106         else {
0107             prop.val = val;
0108             list.append(prop);
0109         }
0110 
0111         if(at >= str.size() - 1 || (str[at] != ',' && str[at] != ' ' && str[at] != '\t'))
0112             break;
0113     }
0114 
0115     // integrity check
0116     if(list.varCount("nonce") != 1)
0117         return false;
0118     if(list.varCount("algorithm") != 1)
0119         return false;
0120     *this = list;
0121     return true;
0122 }
0123 
0124 int DIGESTMD5PropList::varCount(const QByteArray &var)
0125 {
0126     int n = 0;
0127     for(ConstIterator it = constBegin(); it != constEnd(); ++it) {
0128         if((*it).var == var)
0129             ++n;
0130     }
0131     return n;
0132 }
0133 
0134 }