gsasl  2.2.1
test-parser.c
Go to the documentation of this file.
1 /* test-parser.c --- Self tests of DIGEST-MD5 parser & printer.
2  * Copyright (C) 2004-2024 Simon Josefsson
3  *
4  * This file is part of GNU SASL Library.
5  *
6  * GNU SASL Library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License
8  * as published by the Free Software Foundation; either version 2.1 of
9  * the License, or (at your option) any later version.
10  *
11  * GNU SASL Library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with GNU SASL Library; if not, write to the Free
18  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include <config.h>
24 
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 
29 #include "free.h"
30 #include "parser.h"
31 #include "printer.h"
32 #include "digesthmac.h"
33 
34 #include "gc.h"
35 
36 int
37 main (void)
38 {
42  char buf32[33];
43  char buf16[16];
44  int rc;
45  char *tmp;
46 
47  {
48  const char *token = "nonce=4711, foo=bar, algorithm=md5-sess";
49 
50  printf ("challenge `%s': ", token);
51  rc = digest_md5_parse_challenge (token, 0, &c);
52  if (rc != 0)
53  abort ();
54  printf ("nonce `%s': %s", c.nonce,
55  strcmp ("4711", c.nonce) == 0 ? "PASS" : "FAILURE");
56  printf ("\n");
57  tmp = digest_md5_print_challenge (&c);
58  if (!tmp)
59  abort ();
60  printf ("printed `%s' PASS\n", tmp);
61  free (tmp);
63  }
64 
65  {
66  const char *token =
67  "qop=\"auth, auth-conf\", nonce=42, algorithm=md5-sess";
68 
69  printf ("challenge `%s': ", token);
70  rc = digest_md5_parse_challenge (token, 0, &c);
71  if (rc == 0)
72  abort ();
74  printf ("PASS\n");
75  }
76 
77  {
78  const char *token = "cipher=\"des\", nonce=42, algorithm=md5-sess";
79 
80  printf ("challenge `%s': ", token);
81  rc = digest_md5_parse_challenge (token, 0, &c);
82  if (rc == 0)
83  abort ();
85  printf ("PASS\n");
86  }
87 
88  {
89  const char *token = "qop=\"auth, auth-conf\", nonce=42, "
90  "algorithm=md5-sess, cipher=\"des\"";
91 
92  printf ("challenge `%s': ", token);
93  rc = digest_md5_parse_challenge (token, 0, &c);
94  if (rc != 0)
95  abort ();
96  printf ("qop %02x ciphers %02x: %s\n",
97  (unsigned) c.qops, (unsigned) c.ciphers,
98  (c.qops == 5 && c.ciphers == 1) ? "PASS" : "FAILURE");
99  tmp = digest_md5_print_challenge (&c);
100  if (!tmp)
101  abort ();
102  printf ("printed `%s' PASS\n", tmp);
103  free (tmp);
105  }
106 
107  {
108  const char *token = "bar=foo, foo=bar";
109 
110  printf ("challenge `%s': ", token);
111  rc = digest_md5_parse_challenge (token, 0, &c);
112  if (rc == 0)
113  abort ();
114  printf ("PASS\n");
115  }
116 
117  {
118  const char *token = "realm=foo, realm=bar, nonce=42, algorithm=md5-sess";
119 
120  printf ("challenge `%s': ", token);
121  rc = digest_md5_parse_challenge (token, 0, &c);
122  if (rc != 0)
123  abort ();
124  if (c.nrealms != 2)
125  abort ();
126  printf ("realms `%s', `%s': PASS\n", c.realms[0], c.realms[1]);
127  tmp = digest_md5_print_challenge (&c);
128  if (!tmp)
129  abort ();
130  printf ("printed `%s' PASS\n", tmp);
131  free (tmp);
133  }
134 
135  /* Response */
136 
137  {
138  const char *token = "bar=foo, foo=bar";
139 
140  printf ("response `%s': ", token);
141  rc = digest_md5_parse_response (token, 0, &r);
142  if (rc == 0)
143  abort ();
144  printf ("PASS\n");
146  }
147 
148  {
149  const char *token = "username=jas, nonce=42, cnonce=4711, nc=00000001, "
150  "digest-uri=foo, response=01234567890123456789012345678901";
151 
152  printf ("response `%s': ", token);
153  rc = digest_md5_parse_response (token, 0, &r);
154  if (rc != 0)
155  abort ();
156  printf ("username `%s', nonce `%s', cnonce `%s',"
157  " nc %08lx, digest-uri `%s', response `%s': PASS\n",
158  r.username, r.nonce, r.cnonce, r.nc, r.digesturi, r.response);
159  tmp = digest_md5_print_response (&r);
160  if (!tmp)
161  abort ();
162  printf ("printed `%s' PASS\n", tmp);
163  free (tmp);
165  }
166 
167  /* Auth-response, finish. */
168 
169  {
170  const char *token = "rspauth=\"6a204da26b9888ee40bb3052ff056a67\"";
171 
172  printf ("finish `%s': ", token);
173  rc = digest_md5_parse_finish (token, 0, &f);
174  if (rc != 0)
175  abort ();
176  printf ("`%s'? %s\n", f.rspauth,
177  strcmp ("6a204da26b9888ee40bb3052ff056a67", f.rspauth) == 0
178  ? "ok" : "FAILURE");
180  }
181 
182  {
183  const char *token = "bar=foo, foo=bar";
184 
185  printf ("finish `%s': ", token);
186  rc = digest_md5_parse_finish (token, 0, &f);
187  if (rc == 0)
188  abort ();
189  printf ("invalid? PASS\n");
191  }
192 
193  rc = gc_init ();
194  if (rc != 0)
195  {
196  printf ("gc_init error %d\n", rc);
197  abort ();
198  }
199 
200  memset (buf16, 'Q', 16);
201 
202  rc = digest_md5_hmac (buf32, buf16, "nonce", 1, "cnonce",
203  DIGEST_MD5_QOP_AUTH, "authzid", "digesturi",
204  1, 0, NULL, NULL, NULL, NULL);
205  if (rc != 0)
206  abort ();
207  buf32[32] = '\0';
208  if (strcmp (buf32, "6a204da26b9888ee40bb3052ff056a67") != 0)
209  abort ();
210  printf ("digest: `%s': PASS\n", buf32);
211 
212  rc = digest_md5_hmac (buf32, buf16, "nonce", 1, "cnonce",
213  DIGEST_MD5_QOP_AUTH, "authzid", "digesturi", 0, 0,
214  NULL, NULL, NULL, NULL);
215  if (rc != 0)
216  abort ();
217  buf32[32] = '\0';
218  if (strcmp (buf32, "6c1f58bfa46e9c225b93745c84204efd") != 0)
219  abort ();
220  printf ("digest: `%s': PASS\n", buf32);
221 
222  return 0;
223 }
void digest_md5_free_finish(digest_md5_finish *f)
void digest_md5_free_response(digest_md5_response *r)
void digest_md5_free_challenge(digest_md5_challenge *c)
int digest_md5_parse_challenge(const char *challenge, size_t len, digest_md5_challenge *out)
int digest_md5_parse_response(const char *response, size_t len, digest_md5_response *out)
int digest_md5_parse_finish(const char *finish, size_t len, digest_md5_finish *out)
char * digest_md5_print_challenge(digest_md5_challenge *c)
char * digest_md5_print_response(digest_md5_response *r)
@ DIGEST_MD5_QOP_AUTH
int digest_md5_hmac(char *output, char secret[MD5LEN], const char *nonce, unsigned long nc, const char *cnonce, digest_md5_qop qop, const char *authzid, const char *digesturi, int rspauth, digest_md5_cipher cipher, char *kic, char *kis, char *kcc, char *kcs)
Definition: digesthmac.c:77
int rc
Definition: error.c:37
char rspauth[DIGEST_MD5_RESPONSE_LENGTH+1]
char response[DIGEST_MD5_RESPONSE_LENGTH+1]
int main(void)
Definition: test-parser.c:37