gsasl  1.8.0
test-parser.c
Go to the documentation of this file.
00001 /* test-parser.c --- Self tests of DIGEST-MD5 parser & printer.
00002  * Copyright (C) 2004-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 #include <stdio.h>
00024 #include <stdlib.h>
00025 #include <string.h>
00026 
00027 #include "parser.h"
00028 #include "printer.h"
00029 #include "digesthmac.h"
00030 
00031 #include "gc.h"
00032 
00033 int
00034 main (int argc, char *argv[])
00035 {
00036   digest_md5_challenge c;
00037   digest_md5_response r;
00038   digest_md5_finish f;
00039   char buf32[33];
00040   char buf16[16];
00041   int rc;
00042   char *tmp;
00043 
00044   {
00045     const char *token = "nonce=4711, foo=bar, algorithm=md5-sess";
00046 
00047     printf ("challenge `%s': ", token);
00048     rc = digest_md5_parse_challenge (token, 0, &c);
00049     if (rc != 0)
00050       abort ();
00051     printf ("nonce `%s': %s", c.nonce,
00052             strcmp ("4711", c.nonce) == 0 ? "PASS" : "FAILURE");
00053     printf ("\n");
00054     tmp = digest_md5_print_challenge (&c);
00055     if (!tmp)
00056       abort ();
00057     printf ("printed `%s' PASS\n", tmp);
00058     free (tmp);
00059   }
00060 
00061   {
00062     const char *token =
00063       "qop=\"auth, auth-conf\", nonce=42, algorithm=md5-sess";
00064 
00065     printf ("challenge `%s': ", token);
00066     rc = digest_md5_parse_challenge (token, 0, &c);
00067     if (rc == 0)
00068       abort ();
00069     printf ("PASS\n");
00070   }
00071 
00072   {
00073     const char *token = "cipher=\"des\", nonce=42, algorithm=md5-sess";
00074 
00075     printf ("challenge `%s': ", token);
00076     rc = digest_md5_parse_challenge (token, 0, &c);
00077     if (rc == 0)
00078       abort ();
00079     printf ("PASS\n");
00080   }
00081 
00082   {
00083     const char *token = "qop=\"auth, auth-conf\", nonce=42, "
00084       "algorithm=md5-sess, cipher=\"des\"";
00085 
00086     printf ("challenge `%s': ", token);
00087     rc = digest_md5_parse_challenge (token, 0, &c);
00088     if (rc != 0)
00089       abort ();
00090     printf ("qop %02x ciphers %02x: %s\n", c.qops, c.ciphers,
00091             (c.qops == 5 && c.ciphers == 1) ? "PASS" : "FAILURE");
00092     tmp = digest_md5_print_challenge (&c);
00093     if (!tmp)
00094       abort ();
00095     printf ("printed `%s' PASS\n", tmp);
00096     free (tmp);
00097   }
00098 
00099   {
00100     const char *token = "bar=foo, foo=bar";
00101 
00102     printf ("challenge `%s': ", token);
00103     rc = digest_md5_parse_challenge (token, 0, &c);
00104     if (rc == 0)
00105       abort ();
00106     printf ("PASS\n");
00107   }
00108 
00109   {
00110     const char *token = "realm=foo, realm=bar, nonce=42, algorithm=md5-sess";
00111 
00112     printf ("challenge `%s': ", token);
00113     rc = digest_md5_parse_challenge (token, 0, &c);
00114     if (rc != 0)
00115       abort ();
00116     if (c.nrealms != 2)
00117       abort ();
00118     printf ("realms `%s', `%s': PASS\n", c.realms[0], c.realms[1]);
00119     tmp = digest_md5_print_challenge (&c);
00120     if (!tmp)
00121       abort ();
00122     printf ("printed `%s' PASS\n", tmp);
00123     free (tmp);
00124   }
00125 
00126   /* Response */
00127 
00128   {
00129     const char *token = "bar=foo, foo=bar";
00130 
00131     printf ("response `%s': ", token);
00132     rc = digest_md5_parse_response (token, 0, &r);
00133     if (rc == 0)
00134       abort ();
00135     printf ("PASS\n");
00136   }
00137 
00138   {
00139     const char *token = "username=jas, nonce=42, cnonce=4711, nc=00000001, "
00140       "digest-uri=foo, response=01234567890123456789012345678901";
00141 
00142     printf ("response `%s': ", token);
00143     rc = digest_md5_parse_response (token, 0, &r);
00144     if (rc != 0)
00145       abort ();
00146     printf ("username `%s', nonce `%s', cnonce `%s',"
00147             " nc %08lx, digest-uri `%s', response `%s': PASS\n",
00148             r.username, r.nonce, r.cnonce, r.nc, r.digesturi, r.response);
00149     tmp = digest_md5_print_response (&r);
00150     if (!tmp)
00151       abort ();
00152     printf ("printed `%s' PASS\n", tmp);
00153     free (tmp);
00154   }
00155 
00156   /* Auth-response, finish. */
00157 
00158   {
00159     const char *token = "rspauth=\"6a204da26b9888ee40bb3052ff056a67\"";
00160 
00161     printf ("finish `%s': ", token);
00162     rc = digest_md5_parse_finish (token, 0, &f);
00163     if (rc != 0)
00164       abort ();
00165     printf ("`%s'? %s\n", f.rspauth,
00166             strcmp ("6a204da26b9888ee40bb3052ff056a67", f.rspauth) == 0
00167             ? "ok" : "FAILURE");
00168   }
00169 
00170   {
00171     const char *token = "bar=foo, foo=bar";
00172 
00173     printf ("finish `%s': ", token);
00174     rc = digest_md5_parse_finish (token, 0, &f);
00175     if (rc == 0)
00176       abort ();
00177     printf ("invalid? PASS\n");
00178   }
00179 
00180   rc = gc_init ();
00181   if (rc != 0)
00182     {
00183       printf ("gc_init error %d\n", rc);
00184       abort ();
00185     }
00186 
00187   memset (buf16, 'Q', 16);
00188 
00189   rc = digest_md5_hmac (buf32, buf16, "nonce", 1, "cnonce",
00190                         DIGEST_MD5_QOP_AUTH, "authzid", "digesturi",
00191                         1, 0, NULL, NULL, NULL, NULL);
00192   if (rc != 0)
00193     abort ();
00194   buf32[32] = '\0';
00195   if (strcmp (buf32, "6a204da26b9888ee40bb3052ff056a67") != 0)
00196     abort ();
00197   printf ("digest: `%s': PASS\n", buf32);
00198 
00199   rc = digest_md5_hmac (buf32, buf16, "nonce", 1, "cnonce",
00200                         DIGEST_MD5_QOP_AUTH, "authzid", "digesturi", 0, 0,
00201                         NULL, NULL, NULL, NULL);
00202   if (rc != 0)
00203     abort ();
00204   buf32[32] = '\0';
00205   if (strcmp (buf32, "6c1f58bfa46e9c225b93745c84204efd") != 0)
00206     abort ();
00207   printf ("digest: `%s': PASS\n", buf32);
00208 
00209   return 0;
00210 }