xref: /minix3/crypto/external/bsd/heimdal/dist/lib/ntlm/test_commonauth.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: test_commonauth.c,v 1.1.1.2 2014/04/24 12:45:51 pettai Exp $	*/
2ebfedea0SLionel Sambuc 
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc  * Copyright (c) 2006 - 2008 Kungliga Tekniska Högskolan
5ebfedea0SLionel Sambuc  * (Royal Institute of Technology, Stockholm, Sweden).
6ebfedea0SLionel Sambuc  * All rights reserved.
7ebfedea0SLionel Sambuc  *
8ebfedea0SLionel Sambuc  * Portions Copyright (c) 2010 Apple Inc. All rights reserved.
9ebfedea0SLionel Sambuc  *
10ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
11ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
12ebfedea0SLionel Sambuc  * are met:
13ebfedea0SLionel Sambuc  *
14ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
15ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
16ebfedea0SLionel Sambuc  *
17ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
18ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
19ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
20ebfedea0SLionel Sambuc  *
21ebfedea0SLionel Sambuc  * 3. Neither the name of the Institute nor the names of its contributors
22ebfedea0SLionel Sambuc  *    may be used to endorse or promote products derived from this software
23ebfedea0SLionel Sambuc  *    without specific prior written permission.
24ebfedea0SLionel Sambuc  *
25ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
26ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
29ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35ebfedea0SLionel Sambuc  * SUCH DAMAGE.
36ebfedea0SLionel Sambuc  */
37ebfedea0SLionel Sambuc 
38ebfedea0SLionel Sambuc #include <stdio.h>
39ebfedea0SLionel Sambuc #include <stdlib.h>
40ebfedea0SLionel Sambuc #include <unistd.h>
41ebfedea0SLionel Sambuc #include <krb5/roken.h>
42ebfedea0SLionel Sambuc #include "heim-auth.h"
43ebfedea0SLionel Sambuc 
44ebfedea0SLionel Sambuc static int
test_sasl_digest_md5(void)45ebfedea0SLionel Sambuc test_sasl_digest_md5(void)
46ebfedea0SLionel Sambuc {
47ebfedea0SLionel Sambuc     heim_digest_t ctx;
48ebfedea0SLionel Sambuc     const char *user;
49ebfedea0SLionel Sambuc     char *r;
50ebfedea0SLionel Sambuc 
51ebfedea0SLionel Sambuc     if ((ctx = heim_digest_create(1, HEIM_DIGEST_TYPE_AUTO)) == NULL)
52ebfedea0SLionel Sambuc 	abort();
53ebfedea0SLionel Sambuc 
54ebfedea0SLionel Sambuc     if (heim_digest_parse_challenge(ctx, "realm=\"elwood.innosoft.com\",nonce=\"OA6MG9tEQGm2hh\",qop=\"auth\",algorithm=md5-sess,charset=utf-8"))
55ebfedea0SLionel Sambuc 	abort();
56ebfedea0SLionel Sambuc     if (heim_digest_parse_response(ctx, "charset=utf-8,username=\"chris\",realm=\"elwood.innosoft.com\",nonce=\"OA6MG9tEQGm2hh\",nc=00000001,cnonce=\"OA6MHXh6VqTrRk\",digest-uri=\"imap/elwood.innosoft.com\",response=d388dad90d4bbd760a152321f2143af7,qop=auth"))
57ebfedea0SLionel Sambuc 	abort();
58ebfedea0SLionel Sambuc 
59ebfedea0SLionel Sambuc     if ((user = heim_digest_get_key(ctx, "username")) == NULL)
60ebfedea0SLionel Sambuc 	abort();
61ebfedea0SLionel Sambuc     if (strcmp(user, "chris") != 0)
62ebfedea0SLionel Sambuc 	abort();
63ebfedea0SLionel Sambuc 
64ebfedea0SLionel Sambuc     heim_digest_set_key(ctx, "password", "secret");
65ebfedea0SLionel Sambuc 
66ebfedea0SLionel Sambuc     if (heim_digest_verify(ctx, &r))
67ebfedea0SLionel Sambuc 	abort();
68ebfedea0SLionel Sambuc 
69ebfedea0SLionel Sambuc     if (strcmp(r, "rspauth=ea40f60335c427b5527b84dbabcdfffd") != 0)
70ebfedea0SLionel Sambuc 	abort();
71ebfedea0SLionel Sambuc 
72ebfedea0SLionel Sambuc     free(r);
73ebfedea0SLionel Sambuc 
74ebfedea0SLionel Sambuc     heim_digest_release(ctx);
75ebfedea0SLionel Sambuc 
76ebfedea0SLionel Sambuc     return 0;
77ebfedea0SLionel Sambuc }
78ebfedea0SLionel Sambuc 
79ebfedea0SLionel Sambuc static int
test_http_digest_md5(void)80ebfedea0SLionel Sambuc test_http_digest_md5(void)
81ebfedea0SLionel Sambuc {
82ebfedea0SLionel Sambuc     heim_digest_t ctx;
83ebfedea0SLionel Sambuc     const char *user;
84ebfedea0SLionel Sambuc 
85ebfedea0SLionel Sambuc     if ((ctx = heim_digest_create(1, HEIM_DIGEST_TYPE_AUTO)) == NULL)
86ebfedea0SLionel Sambuc 	abort();
87ebfedea0SLionel Sambuc 
88ebfedea0SLionel Sambuc     if (heim_digest_parse_challenge(ctx, "realm=\"testrealm@host.com\","
89ebfedea0SLionel Sambuc 				    "nonce=\"dcd98b7102dd2f0e8b11d0f600bfb0c093\","
90ebfedea0SLionel Sambuc 				    "opaque=\"5ccc069c403ebaf9f0171e9517f40e41\""))
91ebfedea0SLionel Sambuc 	abort();
92ebfedea0SLionel Sambuc 
93ebfedea0SLionel Sambuc     if (heim_digest_parse_response(ctx, "username=\"Mufasa\","
94ebfedea0SLionel Sambuc 				   "realm=\"testrealm@host.com\","
95ebfedea0SLionel Sambuc 				   "nonce=\"dcd98b7102dd2f0e8b11d0f600bfb0c093\","
96ebfedea0SLionel Sambuc 				   "uri=\"/dir/index.html\","
97ebfedea0SLionel Sambuc 				   "response=\"1949323746fe6a43ef61f9606e7febea\","
98ebfedea0SLionel Sambuc 				   "opaque=\"5ccc069c403ebaf9f0171e9517f40e41\""))
99ebfedea0SLionel Sambuc 	abort();
100ebfedea0SLionel Sambuc 
101ebfedea0SLionel Sambuc     if ((user = heim_digest_get_key(ctx, "username")) == NULL)
102ebfedea0SLionel Sambuc 	abort();
103ebfedea0SLionel Sambuc     if (strcmp(user, "Mufasa") != 0)
104ebfedea0SLionel Sambuc 	abort();
105ebfedea0SLionel Sambuc 
106ebfedea0SLionel Sambuc     heim_digest_set_key(ctx, "password", "CircleOfLife");
107ebfedea0SLionel Sambuc 
108ebfedea0SLionel Sambuc     if (heim_digest_verify(ctx, NULL))
109ebfedea0SLionel Sambuc 	abort();
110ebfedea0SLionel Sambuc 
111ebfedea0SLionel Sambuc     heim_digest_release(ctx);
112ebfedea0SLionel Sambuc 
113ebfedea0SLionel Sambuc     return 0;
114ebfedea0SLionel Sambuc }
115ebfedea0SLionel Sambuc 
116ebfedea0SLionel Sambuc static int
test_cram_md5(void)117ebfedea0SLionel Sambuc test_cram_md5(void)
118ebfedea0SLionel Sambuc {
119ebfedea0SLionel Sambuc     const char *chal = "<1896.697170952@postoffice.reston.mci.net>";
120ebfedea0SLionel Sambuc     const char *secret = "tanstaaftanstaaf";
121ebfedea0SLionel Sambuc     const char *resp = "b913a602c7eda7a495b4e6e7334d3890";
122ebfedea0SLionel Sambuc     heim_CRAM_MD5_STATE state;
123ebfedea0SLionel Sambuc     heim_cram_md5 ctx;
124ebfedea0SLionel Sambuc     char *t;
125ebfedea0SLionel Sambuc 
126ebfedea0SLionel Sambuc     const uint8_t *prestate = (uint8_t *)
127ebfedea0SLionel Sambuc 	"\x87\x1E\x24\x10\xB4\x0C\x72\x5D\xA3\x95\x2D\x5B\x8B\xFC\xDD\xE1"
128ebfedea0SLionel Sambuc 	"\x29\x90\xCB\xA7\x66\xF6\xB3\x40\xE8\xAC\x48\x2C\xE4\xE3\xA4\x40";
129ebfedea0SLionel Sambuc 
130ebfedea0SLionel Sambuc     /*
131ebfedea0SLionel Sambuc      * Test prebuild blobs
132ebfedea0SLionel Sambuc      */
133ebfedea0SLionel Sambuc 
134ebfedea0SLionel Sambuc     if (sizeof(state) != 32)
135ebfedea0SLionel Sambuc 	abort();
136ebfedea0SLionel Sambuc 
137ebfedea0SLionel Sambuc     heim_cram_md5_export("foo", &state);
138ebfedea0SLionel Sambuc 
139ebfedea0SLionel Sambuc     if (memcmp(prestate, &state, 32) != 0)
140ebfedea0SLionel Sambuc 	abort();
141ebfedea0SLionel Sambuc 
142ebfedea0SLionel Sambuc     /*
143ebfedea0SLionel Sambuc      * Check example
144ebfedea0SLionel Sambuc      */
145ebfedea0SLionel Sambuc 
146ebfedea0SLionel Sambuc 
147ebfedea0SLionel Sambuc     if (heim_cram_md5_verify(chal, secret, resp) != 0)
148ebfedea0SLionel Sambuc 	abort();
149ebfedea0SLionel Sambuc 
150ebfedea0SLionel Sambuc 
151ebfedea0SLionel Sambuc     /*
152ebfedea0SLionel Sambuc      * Do it ourself
153ebfedea0SLionel Sambuc      */
154ebfedea0SLionel Sambuc 
155ebfedea0SLionel Sambuc     t = heim_cram_md5_create(chal, secret);
156ebfedea0SLionel Sambuc     if (t == NULL)
157ebfedea0SLionel Sambuc 	abort();
158ebfedea0SLionel Sambuc 
159ebfedea0SLionel Sambuc     if (strcmp(resp, t) != 0)
160ebfedea0SLionel Sambuc 	abort();
161ebfedea0SLionel Sambuc 
162ebfedea0SLionel Sambuc     heim_cram_md5_export(secret, &state);
163ebfedea0SLionel Sambuc 
164ebfedea0SLionel Sambuc     /* here you can store the memcpy-ed version of state somewhere else */
165ebfedea0SLionel Sambuc 
166ebfedea0SLionel Sambuc     ctx = heim_cram_md5_import(&state, sizeof(state));
167ebfedea0SLionel Sambuc 
168ebfedea0SLionel Sambuc     memset(&state, 0, sizeof(state));
169ebfedea0SLionel Sambuc 
170ebfedea0SLionel Sambuc     if (heim_cram_md5_verify_ctx(ctx, chal, resp) != 0)
171ebfedea0SLionel Sambuc 	abort();
172ebfedea0SLionel Sambuc 
173ebfedea0SLionel Sambuc     heim_cram_md5_free(ctx);
174ebfedea0SLionel Sambuc 
175ebfedea0SLionel Sambuc     free(t);
176ebfedea0SLionel Sambuc 
177ebfedea0SLionel Sambuc     return 0;
178ebfedea0SLionel Sambuc }
179ebfedea0SLionel Sambuc 
180ebfedea0SLionel Sambuc static int
test_apop(void)181ebfedea0SLionel Sambuc test_apop(void)
182ebfedea0SLionel Sambuc {
183ebfedea0SLionel Sambuc     const char *chal = "<1896.697170952@dbc.mtview.ca.us>";
184ebfedea0SLionel Sambuc     const char *secret = "tanstaaf";
185ebfedea0SLionel Sambuc     const char *resp = "c4c9334bac560ecc979e58001b3e22fb";
186ebfedea0SLionel Sambuc     char *t;
187ebfedea0SLionel Sambuc 
188ebfedea0SLionel Sambuc 
189ebfedea0SLionel Sambuc     t = heim_apop_create(chal, secret);
190ebfedea0SLionel Sambuc     if (t == NULL)
191ebfedea0SLionel Sambuc 	abort();
192ebfedea0SLionel Sambuc 
193ebfedea0SLionel Sambuc     if (strcmp(resp, t) != 0)
194ebfedea0SLionel Sambuc 	abort();
195ebfedea0SLionel Sambuc 
196ebfedea0SLionel Sambuc     if (heim_apop_verify(chal, secret, resp) != 0)
197ebfedea0SLionel Sambuc 	abort();
198ebfedea0SLionel Sambuc 
199ebfedea0SLionel Sambuc     free(t);
200ebfedea0SLionel Sambuc 
201ebfedea0SLionel Sambuc     return 0;
202ebfedea0SLionel Sambuc }
203ebfedea0SLionel Sambuc 
204ebfedea0SLionel Sambuc 
205ebfedea0SLionel Sambuc int
main(int argc,char ** argv)206ebfedea0SLionel Sambuc main(int argc, char **argv)
207ebfedea0SLionel Sambuc {
208ebfedea0SLionel Sambuc     int ret = 0;
209ebfedea0SLionel Sambuc 
210ebfedea0SLionel Sambuc     ret |= test_sasl_digest_md5();
211ebfedea0SLionel Sambuc     ret |= test_http_digest_md5();
212ebfedea0SLionel Sambuc     ret |= test_cram_md5();
213ebfedea0SLionel Sambuc     ret |= test_apop();
214ebfedea0SLionel Sambuc 
215ebfedea0SLionel Sambuc     system("bash");
216ebfedea0SLionel Sambuc 
217ebfedea0SLionel Sambuc     return ret;
218ebfedea0SLionel Sambuc }
219