Branch data Line data Source code
1 : : /* test-parser.c --- Self tests of DIGEST-MD5 parser & printer.
2 : : * Copyright (C) 2004-2012 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 <stdio.h>
24 : : #include <stdlib.h>
25 : : #include <string.h>
26 : :
27 : : #include "parser.h"
28 : : #include "printer.h"
29 : : #include "digesthmac.h"
30 : :
31 : : #include "gc.h"
32 : :
33 : : int
34 : 1 : main (int argc, char *argv[])
35 : : {
36 : : digest_md5_challenge c;
37 : : digest_md5_response r;
38 : : digest_md5_finish f;
39 : : char buf32[33];
40 : : char buf16[16];
41 : : int rc;
42 : : char *tmp;
43 : :
44 : : {
45 : 1 : const char *token = "nonce=4711, foo=bar, algorithm=md5-sess";
46 : :
47 : 1 : printf ("challenge `%s': ", token);
48 : 1 : rc = digest_md5_parse_challenge (token, 0, &c);
49 [ - + ]: 1 : if (rc != 0)
50 : 0 : abort ();
51 [ + - ]: 1 : printf ("nonce `%s': %s", c.nonce,
52 : 1 : strcmp ("4711", c.nonce) == 0 ? "PASS" : "FAILURE");
53 : 1 : printf ("\n");
54 : 1 : tmp = digest_md5_print_challenge (&c);
55 [ - + ]: 1 : if (!tmp)
56 : 0 : abort ();
57 : 1 : printf ("printed `%s' PASS\n", tmp);
58 : 1 : free (tmp);
59 : : }
60 : :
61 : : {
62 : 1 : const char *token =
63 : : "qop=\"auth, auth-conf\", nonce=42, algorithm=md5-sess";
64 : :
65 : 1 : printf ("challenge `%s': ", token);
66 : 1 : rc = digest_md5_parse_challenge (token, 0, &c);
67 [ - + ]: 1 : if (rc == 0)
68 : 0 : abort ();
69 : 1 : printf ("PASS\n");
70 : : }
71 : :
72 : : {
73 : 1 : const char *token = "cipher=\"des\", nonce=42, algorithm=md5-sess";
74 : :
75 : 1 : printf ("challenge `%s': ", token);
76 : 1 : rc = digest_md5_parse_challenge (token, 0, &c);
77 [ - + ]: 1 : if (rc == 0)
78 : 0 : abort ();
79 : 1 : printf ("PASS\n");
80 : : }
81 : :
82 : : {
83 : 1 : const char *token = "qop=\"auth, auth-conf\", nonce=42, "
84 : : "algorithm=md5-sess, cipher=\"des\"";
85 : :
86 : 1 : printf ("challenge `%s': ", token);
87 : 1 : rc = digest_md5_parse_challenge (token, 0, &c);
88 [ - + ]: 1 : if (rc != 0)
89 : 0 : abort ();
90 [ + - ]: 2 : printf ("qop %02x ciphers %02x: %s\n", c.qops, c.ciphers,
91 [ + - ]: 1 : (c.qops == 5 && c.ciphers == 1) ? "PASS" : "FAILURE");
92 : 1 : tmp = digest_md5_print_challenge (&c);
93 [ - + ]: 1 : if (!tmp)
94 : 0 : abort ();
95 : 1 : printf ("printed `%s' PASS\n", tmp);
96 : 1 : free (tmp);
97 : : }
98 : :
99 : : {
100 : 1 : const char *token = "bar=foo, foo=bar";
101 : :
102 : 1 : printf ("challenge `%s': ", token);
103 : 1 : rc = digest_md5_parse_challenge (token, 0, &c);
104 [ - + ]: 1 : if (rc == 0)
105 : 0 : abort ();
106 : 1 : printf ("PASS\n");
107 : : }
108 : :
109 : : {
110 : 1 : const char *token = "realm=foo, realm=bar, nonce=42, algorithm=md5-sess";
111 : :
112 : 1 : printf ("challenge `%s': ", token);
113 : 1 : rc = digest_md5_parse_challenge (token, 0, &c);
114 [ - + ]: 1 : if (rc != 0)
115 : 0 : abort ();
116 [ - + ]: 1 : if (c.nrealms != 2)
117 : 0 : abort ();
118 : 1 : printf ("realms `%s', `%s': PASS\n", c.realms[0], c.realms[1]);
119 : 1 : tmp = digest_md5_print_challenge (&c);
120 [ - + ]: 1 : if (!tmp)
121 : 0 : abort ();
122 : 1 : printf ("printed `%s' PASS\n", tmp);
123 : 1 : free (tmp);
124 : : }
125 : :
126 : : /* Response */
127 : :
128 : : {
129 : 1 : const char *token = "bar=foo, foo=bar";
130 : :
131 : 1 : printf ("response `%s': ", token);
132 : 1 : rc = digest_md5_parse_response (token, 0, &r);
133 [ - + ]: 1 : if (rc == 0)
134 : 0 : abort ();
135 : 1 : printf ("PASS\n");
136 : : }
137 : :
138 : : {
139 : 1 : const char *token = "username=jas, nonce=42, cnonce=4711, nc=00000001, "
140 : : "digest-uri=foo, response=01234567890123456789012345678901";
141 : :
142 : 1 : printf ("response `%s': ", token);
143 : 1 : rc = digest_md5_parse_response (token, 0, &r);
144 [ - + ]: 1 : if (rc != 0)
145 : 0 : abort ();
146 : 1 : printf ("username `%s', nonce `%s', cnonce `%s',"
147 : : " nc %08lx, digest-uri `%s', response `%s': PASS\n",
148 : : r.username, r.nonce, r.cnonce, r.nc, r.digesturi, r.response);
149 : 1 : tmp = digest_md5_print_response (&r);
150 [ - + ]: 1 : if (!tmp)
151 : 0 : abort ();
152 : 1 : printf ("printed `%s' PASS\n", tmp);
153 : 1 : free (tmp);
154 : : }
155 : :
156 : : /* Auth-response, finish. */
157 : :
158 : : {
159 : 1 : const char *token = "rspauth=\"6a204da26b9888ee40bb3052ff056a67\"";
160 : :
161 : 1 : printf ("finish `%s': ", token);
162 : 1 : rc = digest_md5_parse_finish (token, 0, &f);
163 [ - + ]: 1 : if (rc != 0)
164 : 0 : abort ();
165 [ + - ]: 1 : printf ("`%s'? %s\n", f.rspauth,
166 : 1 : strcmp ("6a204da26b9888ee40bb3052ff056a67", f.rspauth) == 0
167 : : ? "ok" : "FAILURE");
168 : : }
169 : :
170 : : {
171 : 1 : const char *token = "bar=foo, foo=bar";
172 : :
173 : 1 : printf ("finish `%s': ", token);
174 : 1 : rc = digest_md5_parse_finish (token, 0, &f);
175 [ - + ]: 1 : if (rc == 0)
176 : 0 : abort ();
177 : 1 : printf ("invalid? PASS\n");
178 : : }
179 : :
180 : 1 : rc = gc_init ();
181 [ - + ]: 1 : if (rc != 0)
182 : : {
183 : 0 : printf ("gc_init error %d\n", rc);
184 : 0 : abort ();
185 : : }
186 : :
187 : 1 : memset (buf16, 'Q', 16);
188 : :
189 : 1 : rc = digest_md5_hmac (buf32, buf16, "nonce", 1, "cnonce",
190 : : DIGEST_MD5_QOP_AUTH, "authzid", "digesturi",
191 : : 1, 0, NULL, NULL, NULL, NULL);
192 [ - + ]: 1 : if (rc != 0)
193 : 0 : abort ();
194 : 1 : buf32[32] = '\0';
195 [ - + ]: 1 : if (strcmp (buf32, "6a204da26b9888ee40bb3052ff056a67") != 0)
196 : 0 : abort ();
197 : 1 : printf ("digest: `%s': PASS\n", buf32);
198 : :
199 : 1 : rc = digest_md5_hmac (buf32, buf16, "nonce", 1, "cnonce",
200 : : DIGEST_MD5_QOP_AUTH, "authzid", "digesturi", 0, 0,
201 : : NULL, NULL, NULL, NULL);
202 [ - + ]: 1 : if (rc != 0)
203 : 0 : abort ();
204 : 1 : buf32[32] = '\0';
205 [ - + ]: 1 : if (strcmp (buf32, "6c1f58bfa46e9c225b93745c84204efd") != 0)
206 : 0 : abort ();
207 : 1 : printf ("digest: `%s': PASS\n", buf32);
208 : :
209 : 1 : return 0;
210 : : }
|