Address of an imported symbol is not always a constant
[heimdal.git] / lib / krb5 / test_pknistkdf.c
blob33eb45ba9ee157537b53a07814ca8de4c7bbaf2d
1 /*
2 * Copyright (c) 2008 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of KTH nor the names of its contributors may be
18 * used to endorse or promote products derived from this software without
19 * specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
22 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
33 #include "krb5_locl.h"
34 #include <pkinit_asn1.h>
35 #include <err.h>
36 #include <getarg.h>
37 #include <hex.h>
39 static int verbose_flag = 0;
41 struct testcase {
42 const heim_oid *oid;
43 krb5_data Z;
44 const char *client;
45 const char *server;
46 krb5_enctype enctype;
47 krb5_data as_req;
48 krb5_data pk_as_rep;
49 krb5_data ticket;
51 krb5_data key;
52 } tests[] = {
53 /* 0 */
55 NULL, /* AlgorithmIdentifier */
56 /* == &asn1_oid_id_pkinit_kdf_ah_sha1. Addresses of exported
57 * symbols are not considered constant on all platforms
58 * (Windows). So we set it in main() below. */
60 { /* Z */
61 256,
62 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
63 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
64 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
65 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
66 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
67 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
68 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
69 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
70 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
71 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
72 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
73 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
74 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
75 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
76 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
77 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
79 "lha@SU.SE", /* client, partyUInfo */
80 "krbtgt/SU.SE@SU.SE", /* server, partyVInfo */
81 ETYPE_AES256_CTS_HMAC_SHA1_96, /* enctype */
82 { /* as_req */
83 10,
84 "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
86 { /* pk_as_rep */
88 "\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB"
90 { /* ticket */
91 55,
92 "\x61\x35\x30\x33\xa0\x03\x02\x01\x05\xa1\x07\x1b\x05\x53\x55\x2e"
93 "\x53\x45\xa2\x10\x30\x0e\xa0\x03\x02\x01\x01\xa1\x07\x30\x05\x1b"
94 "\x03\x6c\x68\x61\xa3\x11\x30\x0f\xa0\x03\x02\x01\x12\xa2\x08\x04"
95 "\x06\x68\x65\x6a\x68\x65\x6a"
97 { /* key */
98 32,
99 "\xc7\x62\x89\xec\x4b\x28\xa6\x91\xff\xce\x80\xbb\xb7\xec\x82\x41"
100 "\x52\x3f\x99\xb1\x90\xcf\x2d\x34\x8f\x54\xa8\x65\x81\x2c\x32\x73"
105 #ifdef MAKETICKET
106 static void
107 fooTicket(void)
109 krb5_error_code ret;
110 krb5_data data;
111 size_t size;
112 Ticket t;
114 t.tkt_vno = 5;
115 t.realm = "SU.SE";
116 t.sname.name_type = KRB5_NT_PRINCIPAL;
117 t.sname.name_string.len = 1;
118 t.sname.name_string.val = ecalloc(1, sizeof(t.sname.name_string.val[0]));
119 t.sname.name_string.val[0] = estrdup("lha");
120 t.enc_part.etype = ETYPE_AES256_CTS_HMAC_SHA1_96;
121 t.enc_part.kvno = NULL;
122 t.enc_part.cipher.length = 6;
123 t.enc_part.cipher.data = "hejhej";
125 ASN1_MALLOC_ENCODE(Ticket, data.data, data.length, &t, &size, ret);
126 if (ret)
127 errx(1, "ASN1_MALLOC_ENCODE(Ticket)");
129 rk_dumpdata("foo", data.data, data.length);
130 free(data.data);
132 #endif
134 static void
135 test_dh2key(krb5_context context, int i, struct testcase *c)
137 krb5_error_code ret;
138 krb5_keyblock key;
139 krb5_principal client, server;
140 Ticket ticket;
141 AlgorithmIdentifier ai;
142 size_t size;
144 memset(&ticket, 0, sizeof(&ticket));
146 ai.algorithm = *c->oid;
147 ai.parameters = NULL;
149 ret = decode_Ticket(c->ticket.data, c->ticket.length, &ticket, &size);
150 if (ret)
151 krb5_errx(context, 1, "decode ticket: %d", ret);
153 ret = krb5_parse_name(context, c->client, &client);
154 if (ret)
155 krb5_err(context, 1, ret, "parse_name: %s", c->client);
156 ret = krb5_parse_name(context, c->server, &server);
157 if (ret)
158 krb5_err(context, 1, ret, "parse_name: %s", c->server);
160 if (verbose_flag) {
161 char *str;
162 hex_encode(c->Z.data, c->Z.length, &str);
163 printf("Z: %s\n", str);
164 free(str);
165 printf("client: %s\n", c->client);
166 printf("server: %s\n", c->server);
167 printf("enctype: %d\n", (int)c->enctype);
168 hex_encode(c->as_req.data, c->as_req.length, &str);
169 printf("as-req: %s\n", str);
170 free(str);
171 hex_encode(c->pk_as_rep.data, c->pk_as_rep.length, &str);
172 printf("pk-as-rep: %s\n", str);
173 free(str);
174 hex_encode(c->ticket.data, c->ticket.length, &str);
175 printf("ticket: %s\n", str);
176 free(str);
179 ret = _krb5_pk_kdf(context,
180 &ai,
181 c->Z.data,
182 c->Z.length,
183 client,
184 server,
185 c->enctype,
186 &c->as_req,
187 &c->pk_as_rep,
188 &ticket,
189 &key);
190 krb5_free_principal(context, client);
191 krb5_free_principal(context, server);
192 if (ret)
193 krb5_err(context, 1, ret, "_krb5_pk_kdf: %d", i);
195 if (verbose_flag) {
196 char *str;
197 hex_encode(key.keyvalue.data, key.keyvalue.length, &str);
198 printf("key: %s\n", str);
199 free(str);
202 if (key.keyvalue.length != c->key.length ||
203 memcmp(key.keyvalue.data, c->key.data, c->key.length) != 0)
204 krb5_errx(context, 1, "resulting key wrong: %d", i);
206 krb5_free_keyblock_contents(context, &key);
207 free_Ticket(&ticket);
213 static int version_flag = 0;
214 static int help_flag = 0;
216 static struct getargs args[] = {
217 {"verbose", 0, arg_flag, &verbose_flag,
218 "verbose output", NULL },
219 {"version", 0, arg_flag, &version_flag,
220 "print version", NULL },
221 {"help", 0, arg_flag, &help_flag,
222 NULL, NULL }
225 static void
226 usage (int ret)
228 arg_printusage (args,
229 sizeof(args)/sizeof(*args),
230 NULL,
231 "");
232 exit (ret);
237 main(int argc, char **argv)
239 krb5_context context;
240 krb5_error_code ret;
241 int i, optidx = 0;
243 setprogname(argv[0]);
245 if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
246 usage(1);
248 if (help_flag)
249 usage (0);
251 if(version_flag){
252 print_version(NULL);
253 exit(0);
256 argc -= optidx;
257 argv += optidx;
259 #ifdef MAKETICKET
260 fooTicket();
261 #endif
263 ret = krb5_init_context(&context);
264 if (ret)
265 errx (1, "krb5_init_context failed: %d", ret);
267 tests[0].oid = &asn1_oid_id_pkinit_kdf_ah_sha1;
269 for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++)
270 test_dh2key(context, i, &tests[i]);
272 krb5_free_context(context);
274 return 0;