2 * Copyright (c) 2006 - 2008 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
34 #include "krb5/gsskrb5_locl.h"
38 #include <gssapi_krb5.h>
39 #include <gssapi_spnego.h>
40 #include <gssapi_ntlm.h>
41 #include "test_common.h"
43 static char *type_string
;
44 static char *mech_string
;
45 static char *ret_mech_string
;
46 static char *client_name
;
47 static char *client_password
;
48 static int dns_canon_flag
= -1;
49 static int mutual_auth_flag
= 0;
50 static int dce_style_flag
= 0;
51 static int wrapunwrap_flag
= 0;
52 static int iov_flag
= 0;
53 static int getverifymic_flag
= 0;
54 static int deleg_flag
= 0;
55 static int policy_deleg_flag
= 0;
56 static int server_no_deleg_flag
= 0;
57 static int ei_flag
= 0;
58 static char *gsskrb5_acceptor_identity
= NULL
;
59 static char *session_enctype_string
= NULL
;
60 static int client_time_offset
= 0;
61 static int server_time_offset
= 0;
62 static int max_loops
= 0;
63 static char *limit_enctype_string
= NULL
;
64 static int version_flag
= 0;
65 static int verbose_flag
= 0;
66 static int help_flag
= 0;
68 static krb5_context context
;
69 static krb5_enctype limit_enctype
= 0;
75 { "krb5", NULL
/* GSS_KRB5_MECHANISM */ },
76 { "spnego", NULL
/* GSS_SPNEGO_MECHANISM */ },
77 { "ntlm", NULL
/* GSS_NTLM_MECHANISM */ },
78 { "sasl-digest-md5", NULL
/* GSS_SASL_DIGEST_MD5_MECHANISM */ }
84 o2n
[0].oid
= GSS_KRB5_MECHANISM
;
85 o2n
[1].oid
= GSS_SPNEGO_MECHANISM
;
86 o2n
[2].oid
= GSS_NTLM_MECHANISM
;
87 o2n
[3].oid
= GSS_SASL_DIGEST_MD5_MECHANISM
;
91 string_to_oid(const char *name
)
94 for (i
= 0; i
< sizeof(o2n
)/sizeof(o2n
[0]); i
++)
95 if (strcasecmp(name
, o2n
[i
].name
) == 0)
97 errx(1, "name '%s' not unknown", name
);
101 oid_to_string(const gss_OID oid
)
104 for (i
= 0; i
< sizeof(o2n
)/sizeof(o2n
[0]); i
++)
105 if (gss_oid_equal(oid
, o2n
[i
].oid
))
107 return "unknown oid";
111 loop(gss_OID mechoid
,
112 gss_OID nameoid
, const char *target
,
113 gss_cred_id_t init_cred
,
114 gss_ctx_id_t
*sctx
, gss_ctx_id_t
*cctx
,
115 gss_OID
*actual_mech
,
116 gss_cred_id_t
*deleg_cred
)
118 int server_done
= 0, client_done
= 0;
120 OM_uint32 maj_stat
, min_stat
;
121 gss_name_t gss_target_name
;
122 gss_buffer_desc input_token
, output_token
;
123 OM_uint32 flags
= 0, ret_cflags
, ret_sflags
;
124 gss_OID actual_mech_client
;
125 gss_OID actual_mech_server
;
127 *actual_mech
= GSS_C_NO_OID
;
129 flags
|= GSS_C_INTEG_FLAG
;
130 flags
|= GSS_C_CONF_FLAG
;
132 if (mutual_auth_flag
)
133 flags
|= GSS_C_MUTUAL_FLAG
;
135 flags
|= GSS_C_DCE_STYLE
;
137 flags
|= GSS_C_DELEG_FLAG
;
138 if (policy_deleg_flag
)
139 flags
|= GSS_C_DELEG_POLICY_FLAG
;
141 input_token
.value
= rk_UNCONST(target
);
142 input_token
.length
= strlen(target
);
144 maj_stat
= gss_import_name(&min_stat
,
148 if (GSS_ERROR(maj_stat
))
149 err(1, "import name creds failed with: %d", maj_stat
);
151 input_token
.length
= 0;
152 input_token
.value
= NULL
;
154 while (!server_done
|| !client_done
) {
157 gsskrb5_set_time_offset(client_time_offset
);
159 maj_stat
= gss_init_sec_context(&min_stat
,
172 if (GSS_ERROR(maj_stat
))
173 errx(1, "init_sec_context: %s",
174 gssapi_err(maj_stat
, min_stat
, mechoid
));
175 if (maj_stat
& GSS_S_CONTINUE_NEEDED
)
180 gsskrb5_get_time_offset(&client_time_offset
);
182 if (client_done
&& server_done
)
185 if (input_token
.length
!= 0)
186 gss_release_buffer(&min_stat
, &input_token
);
188 gsskrb5_set_time_offset(server_time_offset
);
190 maj_stat
= gss_accept_sec_context(&min_stat
,
194 GSS_C_NO_CHANNEL_BINDINGS
,
201 if (GSS_ERROR(maj_stat
))
202 errx(1, "accept_sec_context: %s",
203 gssapi_err(maj_stat
, min_stat
, actual_mech_server
));
205 gsskrb5_get_time_offset(&server_time_offset
);
207 if (output_token
.length
!= 0)
208 gss_release_buffer(&min_stat
, &output_token
);
210 if (maj_stat
& GSS_S_CONTINUE_NEEDED
)
215 if (output_token
.length
!= 0)
216 gss_release_buffer(&min_stat
, &output_token
);
217 if (input_token
.length
!= 0)
218 gss_release_buffer(&min_stat
, &input_token
);
219 gss_release_name(&min_stat
, &gss_target_name
);
221 if (deleg_flag
|| policy_deleg_flag
) {
222 if (server_no_deleg_flag
) {
223 if (*deleg_cred
!= GSS_C_NO_CREDENTIAL
)
224 errx(1, "got delegated cred but didn't expect one");
225 } else if (*deleg_cred
== GSS_C_NO_CREDENTIAL
)
226 errx(1, "asked for delegarated cred but did get one");
227 } else if (*deleg_cred
!= GSS_C_NO_CREDENTIAL
)
228 errx(1, "got deleg_cred cred but didn't ask");
230 if (gss_oid_equal(actual_mech_server
, actual_mech_client
) == 0)
231 errx(1, "mech mismatch");
232 *actual_mech
= actual_mech_server
;
234 if (max_loops
&& num_loops
> max_loops
)
235 errx(1, "num loops %d was lager then max loops %d",
236 num_loops
, max_loops
);
239 printf("server time offset: %d\n", server_time_offset
);
240 printf("client time offset: %d\n", client_time_offset
);
241 printf("num loops %d\n", num_loops
);
246 wrapunwrap(gss_ctx_id_t cctx
, gss_ctx_id_t sctx
, int flags
, gss_OID mechoid
)
248 gss_buffer_desc input_token
, output_token
, output_token2
;
249 OM_uint32 min_stat
, maj_stat
;
253 input_token
.value
= "foo";
254 input_token
.length
= 3;
256 maj_stat
= gss_wrap(&min_stat
, cctx
, flags
, 0, &input_token
,
257 &conf_state
, &output_token
);
258 if (maj_stat
!= GSS_S_COMPLETE
)
259 errx(1, "gss_wrap failed: %s",
260 gssapi_err(maj_stat
, min_stat
, mechoid
));
262 maj_stat
= gss_unwrap(&min_stat
, sctx
, &output_token
,
263 &output_token2
, &conf_state
, &qop_state
);
264 if (maj_stat
!= GSS_S_COMPLETE
)
265 errx(1, "gss_unwrap failed: %s",
266 gssapi_err(maj_stat
, min_stat
, mechoid
));
268 gss_release_buffer(&min_stat
, &output_token
);
269 gss_release_buffer(&min_stat
, &output_token2
);
271 #if 0 /* doesn't work for NTLM yet */
272 if (!!conf_state
!= !!flags
)
273 errx(1, "conf_state mismatch");
278 #define USE_HEADER_ONLY 2
279 #define USE_SIGN_ONLY 4
283 wrapunwrap_iov(gss_ctx_id_t cctx
, gss_ctx_id_t sctx
, int flags
, gss_OID mechoid
)
285 krb5_data token
, header
, trailer
;
286 OM_uint32 min_stat
, maj_stat
;
288 int conf_state
, conf_state2
;
289 gss_iov_buffer_desc iov
[6];
292 char header_data
[9] = "ABCheader";
293 char trailer_data
[10] = "trailerXYZ";
295 char token_data
[16] = "0123456789abcdef";
297 memset(&iov
, 0, sizeof(iov
));
299 if (flags
& USE_SIGN_ONLY
) {
300 header
.data
= header_data
;
302 trailer
.data
= trailer_data
;
311 token
.data
= token_data
;
314 iov_len
= sizeof(iov
)/sizeof(iov
[0]);
316 memset(iov
, 0, sizeof(iov
));
318 iov
[0].type
= GSS_IOV_BUFFER_TYPE_HEADER
| GSS_IOV_BUFFER_TYPE_FLAG_ALLOCATE
;
320 if (header
.length
!= 0) {
321 iov
[1].type
= GSS_IOV_BUFFER_TYPE_SIGN_ONLY
;
322 iov
[1].buffer
.length
= header
.length
;
323 iov
[1].buffer
.value
= header
.data
;
325 iov
[1].type
= GSS_IOV_BUFFER_TYPE_EMPTY
;
326 iov
[1].buffer
.length
= 0;
327 iov
[1].buffer
.value
= NULL
;
329 iov
[2].type
= GSS_IOV_BUFFER_TYPE_DATA
;
330 iov
[2].buffer
.length
= token
.length
;
331 iov
[2].buffer
.value
= token
.data
;
332 if (trailer
.length
!= 0) {
333 iov
[3].type
= GSS_IOV_BUFFER_TYPE_SIGN_ONLY
;
334 iov
[3].buffer
.length
= trailer
.length
;
335 iov
[3].buffer
.value
= trailer
.data
;
337 iov
[3].type
= GSS_IOV_BUFFER_TYPE_EMPTY
;
338 iov
[3].buffer
.length
= 0;
339 iov
[3].buffer
.value
= NULL
;
341 if (dce_style_flag
) {
342 iov
[4].type
= GSS_IOV_BUFFER_TYPE_EMPTY
;
344 iov
[4].type
= GSS_IOV_BUFFER_TYPE_PADDING
| GSS_IOV_BUFFER_TYPE_FLAG_ALLOCATE
;
346 iov
[4].buffer
.length
= 0;
347 iov
[4].buffer
.value
= 0;
348 if (dce_style_flag
) {
349 iov
[5].type
= GSS_IOV_BUFFER_TYPE_EMPTY
;
350 } else if (flags
& USE_HEADER_ONLY
) {
351 iov
[5].type
= GSS_IOV_BUFFER_TYPE_EMPTY
;
353 iov
[5].type
= GSS_IOV_BUFFER_TYPE_TRAILER
| GSS_IOV_BUFFER_TYPE_FLAG_ALLOCATE
;
355 iov
[5].buffer
.length
= 0;
356 iov
[5].buffer
.value
= 0;
358 maj_stat
= gss_wrap_iov(&min_stat
, cctx
, dce_style_flag
|| flags
& USE_CONF
, 0, &conf_state
,
360 if (maj_stat
!= GSS_S_COMPLETE
)
361 errx(1, "gss_wrap_iov failed");
364 iov
[0].buffer
.length
+
365 iov
[1].buffer
.length
+
366 iov
[2].buffer
.length
+
367 iov
[3].buffer
.length
+
368 iov
[4].buffer
.length
+
369 iov
[5].buffer
.length
;
370 token
.data
= emalloc(token
.length
);
373 memcpy(p
, iov
[0].buffer
.value
, iov
[0].buffer
.length
);
374 p
+= iov
[0].buffer
.length
;
375 memcpy(p
, iov
[1].buffer
.value
, iov
[1].buffer
.length
);
376 p
+= iov
[1].buffer
.length
;
377 memcpy(p
, iov
[2].buffer
.value
, iov
[2].buffer
.length
);
378 p
+= iov
[2].buffer
.length
;
379 memcpy(p
, iov
[3].buffer
.value
, iov
[3].buffer
.length
);
380 p
+= iov
[3].buffer
.length
;
381 memcpy(p
, iov
[4].buffer
.value
, iov
[4].buffer
.length
);
382 p
+= iov
[4].buffer
.length
;
383 memcpy(p
, iov
[5].buffer
.value
, iov
[5].buffer
.length
);
384 p
+= iov
[5].buffer
.length
;
386 assert(p
- ((unsigned char *)token
.data
) == token
.length
);
388 if ((flags
& (USE_SIGN_ONLY
|FORCE_IOV
)) == 0) {
389 gss_buffer_desc input
, output
;
391 input
.value
= token
.data
;
392 input
.length
= token
.length
;
394 maj_stat
= gss_unwrap(&min_stat
, sctx
, &input
,
395 &output
, &conf_state2
, &qop_state
);
397 if (maj_stat
!= GSS_S_COMPLETE
)
398 errx(1, "gss_unwrap from gss_wrap_iov failed: %s",
399 gssapi_err(maj_stat
, min_stat
, mechoid
));
401 gss_release_buffer(&min_stat
, &output
);
403 maj_stat
= gss_unwrap_iov(&min_stat
, sctx
, &conf_state2
, &qop_state
,
406 if (maj_stat
!= GSS_S_COMPLETE
)
407 errx(1, "gss_unwrap_iov failed: %x %s", flags
,
408 gssapi_err(maj_stat
, min_stat
, mechoid
));
411 if (conf_state2
!= conf_state
)
412 errx(1, "conf state wrong for iov: %x", flags
);
419 getverifymic(gss_ctx_id_t cctx
, gss_ctx_id_t sctx
, gss_OID mechoid
)
421 gss_buffer_desc input_token
, output_token
;
422 OM_uint32 min_stat
, maj_stat
;
425 input_token
.value
= "bar";
426 input_token
.length
= 3;
428 maj_stat
= gss_get_mic(&min_stat
, cctx
, 0, &input_token
,
430 if (maj_stat
!= GSS_S_COMPLETE
)
431 errx(1, "gss_get_mic failed: %s",
432 gssapi_err(maj_stat
, min_stat
, mechoid
));
434 maj_stat
= gss_verify_mic(&min_stat
, sctx
, &input_token
,
435 &output_token
, &qop_state
);
436 if (maj_stat
!= GSS_S_COMPLETE
)
437 errx(1, "gss_verify_mic failed: %s",
438 gssapi_err(maj_stat
, min_stat
, mechoid
));
440 gss_release_buffer(&min_stat
, &output_token
);
446 gss_ctx_id_t ctx
= GSS_C_NO_CONTEXT
;
447 gss_cred_id_t cred
= GSS_C_NO_CREDENTIAL
;
448 gss_name_t name
= GSS_C_NO_NAME
;
449 gss_OID_set oidset
= GSS_C_NO_OID_SET
;
452 gss_delete_sec_context(&junk
, &ctx
, NULL
);
453 gss_release_cred(&junk
, &cred
);
454 gss_release_name(&junk
, &name
);
455 gss_release_oid_set(&junk
, &oidset
);
462 static struct getargs args
[] = {
463 {"name-type",0, arg_string
, &type_string
, "type of name", NULL
},
464 {"mech-type",0, arg_string
, &mech_string
, "type of mech", NULL
},
465 {"ret-mech-type",0, arg_string
, &ret_mech_string
,
466 "type of return mech", NULL
},
467 {"dns-canonicalize",0,arg_negative_flag
, &dns_canon_flag
,
468 "use dns to canonicalize", NULL
},
469 {"mutual-auth",0, arg_flag
, &mutual_auth_flag
,"mutual auth", NULL
},
470 {"client-name", 0, arg_string
, &client_name
, "client name", NULL
},
471 {"client-password", 0, arg_string
, &client_password
, "client password", NULL
},
472 {"limit-enctype",0, arg_string
, &limit_enctype_string
, "enctype", NULL
},
473 {"dce-style",0, arg_flag
, &dce_style_flag
, "dce-style", NULL
},
474 {"wrapunwrap",0, arg_flag
, &wrapunwrap_flag
, "wrap/unwrap", NULL
},
475 {"iov", 0, arg_flag
, &iov_flag
, "wrap/unwrap iov", NULL
},
476 {"getverifymic",0, arg_flag
, &getverifymic_flag
,
477 "get and verify mic", NULL
},
478 {"delegate",0, arg_flag
, &deleg_flag
, "delegate credential", NULL
},
479 {"policy-delegate",0, arg_flag
, &policy_deleg_flag
, "policy delegate credential", NULL
},
480 {"server-no-delegate",0, arg_flag
, &server_no_deleg_flag
,
481 "server should get a credential", NULL
},
482 {"export-import-cred",0, arg_flag
, &ei_flag
, "test export/import cred", NULL
},
483 {"gsskrb5-acceptor-identity", 0, arg_string
, &gsskrb5_acceptor_identity
, "keytab", NULL
},
484 {"session-enctype", 0, arg_string
, &session_enctype_string
, "enctype", NULL
},
485 {"client-time-offset", 0, arg_integer
, &client_time_offset
, "time", NULL
},
486 {"server-time-offset", 0, arg_integer
, &server_time_offset
, "time", NULL
},
487 {"max-loops", 0, arg_integer
, &max_loops
, "time", NULL
},
488 {"version", 0, arg_flag
, &version_flag
, "print version", NULL
},
489 {"verbose", 'v', arg_flag
, &verbose_flag
, "verbose", NULL
},
490 {"help", 0, arg_flag
, &help_flag
, NULL
, NULL
}
496 arg_printusage (args
, sizeof(args
)/sizeof(*args
),
497 NULL
, "service@host");
502 main(int argc
, char **argv
)
505 OM_uint32 min_stat
, maj_stat
;
506 gss_ctx_id_t cctx
, sctx
;
508 gss_OID nameoid
, mechoid
, actual_mech
, actual_mech2
;
509 gss_cred_id_t client_cred
= GSS_C_NO_CREDENTIAL
, deleg_cred
= GSS_C_NO_CREDENTIAL
;
510 gss_OID credential_type
;
511 gss_buffer_desc credential_data
;
513 setprogname(argv
[0]);
517 if (krb5_init_context(&context
))
518 errx(1, "krb5_init_context");
520 cctx
= sctx
= GSS_C_NO_CONTEXT
;
522 if(getarg(args
, sizeof(args
) / sizeof(args
[0]), argc
, argv
, &optind
))
539 if (dns_canon_flag
!= -1)
540 gsskrb5_set_dns_canonicalize(dns_canon_flag
);
542 if (type_string
== NULL
)
543 nameoid
= GSS_C_NT_HOSTBASED_SERVICE
;
544 else if (strcmp(type_string
, "hostbased-service") == 0)
545 nameoid
= GSS_C_NT_HOSTBASED_SERVICE
;
546 else if (strcmp(type_string
, "krb5-principal-name") == 0)
547 nameoid
= GSS_KRB5_NT_PRINCIPAL_NAME
;
549 errx(1, "%s not suppported", type_string
);
551 if (mech_string
== NULL
)
552 mechoid
= GSS_KRB5_MECHANISM
;
554 mechoid
= string_to_oid(mech_string
);
556 if (gsskrb5_acceptor_identity
) {
557 maj_stat
= gsskrb5_register_acceptor_identity(gsskrb5_acceptor_identity
);
559 errx(1, "gsskrb5_acceptor_identity: %s",
560 gssapi_err(maj_stat
, 0, GSS_C_NO_OID
));
563 if (client_password
) {
564 credential_type
= GSS_C_CRED_PASSWORD
;
565 credential_data
.value
= client_password
;
566 credential_data
.length
= strlen(client_password
);
568 credential_type
= GSS_C_NO_OID
;
573 cn
.value
= client_name
;
574 cn
.length
= strlen(client_name
);
575 maj_stat
= gss_import_name(&min_stat
, &cn
, GSS_C_NT_USER_NAME
, &cname
);
577 errx(1, "gss_import_name: %s",
578 gssapi_err(maj_stat
, min_stat
, GSS_C_NO_OID
));
580 maj_stat
= gss_acquire_cred_ext(&min_stat
, cname
,
581 credential_type
, &credential_data
,
582 0, GSS_C_NO_OID
, GSS_C_INITIATE
, &client_cred
);
583 if (GSS_ERROR(maj_stat
))
584 errx(1, "gss_acquire_cred_ext: %s",
585 gssapi_err(maj_stat
, min_stat
, GSS_C_NO_OID
));
586 gss_release_name(&min_stat
, &cname
);
587 } else if (credential_type
) {
588 maj_stat
= gss_acquire_cred_ex(&min_stat
, GSS_C_NO_NAME
,
589 credential_type
, &credential_data
,
590 0, GSS_C_NO_OID
, GSS_C_INITIATE
, &client_cred
);
591 if (GSS_ERROR(maj_stat
))
592 errx(1, "gss_acquire_cred_ex: %s",
593 gssapi_err(maj_stat
, min_stat
, GSS_C_NO_OID
));
596 if (limit_enctype_string
) {
599 ret
= krb5_string_to_enctype(context
,
600 limit_enctype_string
,
603 krb5_err(context
, 1, ret
, "krb5_string_to_enctype");
608 if (client_cred
== NULL
)
609 errx(1, "client_cred missing");
611 maj_stat
= gss_krb5_set_allowable_enctypes(&min_stat
, client_cred
,
614 errx(1, "gss_krb5_set_allowable_enctypes: %s",
615 gssapi_err(maj_stat
, min_stat
, GSS_C_NO_OID
));
618 loop(mechoid
, nameoid
, argv
[0], client_cred
,
619 &sctx
, &cctx
, &actual_mech
, &deleg_cred
);
622 printf("resulting mech: %s\n", oid_to_string(actual_mech
));
624 if (ret_mech_string
) {
627 retoid
= string_to_oid(ret_mech_string
);
629 if (gss_oid_equal(retoid
, actual_mech
) == 0)
630 errx(1, "actual_mech mech is not the expected type %s",
634 /* XXX should be actual_mech */
635 if (gss_oid_equal(mechoid
, GSS_KRB5_MECHANISM
)) {
637 gss_buffer_desc authz_data
;
638 gss_buffer_desc in
, out1
, out2
;
639 krb5_keyblock
*keyblock
, *keyblock2
;
643 ret
= krb5_timeofday(context
, &now
);
645 errx(1, "krb5_timeofday failed");
648 maj_stat
= gss_krb5_export_lucid_sec_context(&min_stat
,
652 if (maj_stat
!= GSS_S_COMPLETE
)
653 errx(1, "gss_krb5_export_lucid_sec_context failed: %s",
654 gssapi_err(maj_stat
, min_stat
, actual_mech
));
657 maj_stat
= gss_krb5_free_lucid_sec_context(&maj_stat
, ctx
);
658 if (maj_stat
!= GSS_S_COMPLETE
)
659 errx(1, "gss_krb5_free_lucid_sec_context failed: %s",
660 gssapi_err(maj_stat
, min_stat
, actual_mech
));
663 maj_stat
= gss_krb5_export_lucid_sec_context(&min_stat
,
667 if (maj_stat
!= GSS_S_COMPLETE
)
668 errx(1, "gss_krb5_export_lucid_sec_context failed: %s",
669 gssapi_err(maj_stat
, min_stat
, actual_mech
));
670 maj_stat
= gss_krb5_free_lucid_sec_context(&min_stat
, ctx
);
671 if (maj_stat
!= GSS_S_COMPLETE
)
672 errx(1, "gss_krb5_free_lucid_sec_context failed: %s",
673 gssapi_err(maj_stat
, min_stat
, actual_mech
));
675 maj_stat
= gsskrb5_extract_authtime_from_sec_context(&min_stat
,
678 if (maj_stat
!= GSS_S_COMPLETE
)
679 errx(1, "gsskrb5_extract_authtime_from_sec_context failed: %s",
680 gssapi_err(maj_stat
, min_stat
, actual_mech
));
683 errx(1, "gsskrb5_extract_authtime_from_sec_context failed: "
684 "time authtime is before now: %ld %ld",
685 (long)time
, (long)now
);
687 maj_stat
= gsskrb5_extract_service_keyblock(&min_stat
,
690 if (maj_stat
!= GSS_S_COMPLETE
)
691 errx(1, "gsskrb5_export_service_keyblock failed: %s",
692 gssapi_err(maj_stat
, min_stat
, actual_mech
));
694 krb5_free_keyblock(context
, keyblock
);
696 maj_stat
= gsskrb5_get_subkey(&min_stat
,
699 if (maj_stat
!= GSS_S_COMPLETE
700 && (!(maj_stat
== GSS_S_FAILURE
&& min_stat
== GSS_KRB5_S_KG_NO_SUBKEY
)))
701 errx(1, "gsskrb5_get_subkey server failed: %s",
702 gssapi_err(maj_stat
, min_stat
, actual_mech
));
704 if (maj_stat
!= GSS_S_COMPLETE
)
706 else if (limit_enctype
&& keyblock
->keytype
!= limit_enctype
)
707 errx(1, "gsskrb5_get_subkey wrong enctype");
709 maj_stat
= gsskrb5_get_subkey(&min_stat
,
712 if (maj_stat
!= GSS_S_COMPLETE
713 && (!(maj_stat
== GSS_S_FAILURE
&& min_stat
== GSS_KRB5_S_KG_NO_SUBKEY
)))
714 errx(1, "gsskrb5_get_subkey client failed: %s",
715 gssapi_err(maj_stat
, min_stat
, actual_mech
));
717 if (maj_stat
!= GSS_S_COMPLETE
)
719 else if (limit_enctype
&& keyblock
->keytype
!= limit_enctype
)
720 errx(1, "gsskrb5_get_subkey wrong enctype");
722 if (keyblock
|| keyblock2
) {
723 if (keyblock
== NULL
)
724 errx(1, "server missing token keyblock");
725 if (keyblock2
== NULL
)
726 errx(1, "client missing token keyblock");
728 if (keyblock
->keytype
!= keyblock2
->keytype
)
729 errx(1, "enctype mismatch");
730 if (keyblock
->keyvalue
.length
!= keyblock2
->keyvalue
.length
)
731 errx(1, "key length mismatch");
732 if (memcmp(keyblock
->keyvalue
.data
, keyblock2
->keyvalue
.data
,
733 keyblock2
->keyvalue
.length
) != 0)
734 errx(1, "key data mismatch");
737 if (session_enctype_string
) {
738 krb5_enctype enctype
;
740 ret
= krb5_string_to_enctype(context
,
741 session_enctype_string
,
745 krb5_err(context
, 1, ret
, "krb5_string_to_enctype");
747 if (enctype
!= keyblock
->keytype
)
748 errx(1, "keytype is not the expected %d != %d",
749 (int)enctype
, (int)keyblock2
->keytype
);
753 krb5_free_keyblock(context
, keyblock
);
755 krb5_free_keyblock(context
, keyblock2
);
757 maj_stat
= gsskrb5_get_initiator_subkey(&min_stat
,
760 if (maj_stat
!= GSS_S_COMPLETE
761 && (!(maj_stat
== GSS_S_FAILURE
&& min_stat
== GSS_KRB5_S_KG_NO_SUBKEY
)))
762 errx(1, "gsskrb5_get_initiator_subkey failed: %s",
763 gssapi_err(maj_stat
, min_stat
, actual_mech
));
765 if (maj_stat
== GSS_S_COMPLETE
) {
767 if (limit_enctype
&& keyblock
->keytype
!= limit_enctype
)
768 errx(1, "gsskrb5_get_initiator_subkey wrong enctype");
769 krb5_free_keyblock(context
, keyblock
);
772 maj_stat
= gsskrb5_extract_authz_data_from_sec_context(&min_stat
,
776 if (maj_stat
== GSS_S_COMPLETE
)
777 gss_release_buffer(&min_stat
, &authz_data
);
780 memset(&out1
, 0, sizeof(out1
));
781 memset(&out2
, 0, sizeof(out2
));
786 gss_pseudo_random(&min_stat
, sctx
, GSS_C_PRF_KEY_FULL
, &in
,
788 gss_pseudo_random(&min_stat
, cctx
, GSS_C_PRF_KEY_FULL
, &in
,
791 if (out1
.length
!= out2
.length
)
792 errx(1, "prf len mismatch");
793 if (memcmp(out1
.value
, out2
.value
, out1
.length
) != 0)
794 errx(1, "prf data mismatch");
796 gss_release_buffer(&min_stat
, &out1
);
798 gss_pseudo_random(&min_stat
, sctx
, GSS_C_PRF_KEY_FULL
, &in
,
801 if (out1
.length
!= out2
.length
)
802 errx(1, "prf len mismatch");
803 if (memcmp(out1
.value
, out2
.value
, out1
.length
) != 0)
804 errx(1, "prf data mismatch");
806 gss_release_buffer(&min_stat
, &out1
);
807 gss_release_buffer(&min_stat
, &out2
);
812 gss_pseudo_random(&min_stat
, sctx
, GSS_C_PRF_KEY_PARTIAL
, &in
,
814 gss_pseudo_random(&min_stat
, cctx
, GSS_C_PRF_KEY_PARTIAL
, &in
,
817 if (out1
.length
!= out2
.length
)
818 errx(1, "prf len mismatch");
819 if (memcmp(out1
.value
, out2
.value
, out1
.length
) != 0)
820 errx(1, "prf data mismatch");
822 gss_release_buffer(&min_stat
, &out1
);
823 gss_release_buffer(&min_stat
, &out2
);
826 getverifymic_flag
= 1;
829 if (wrapunwrap_flag
) {
830 wrapunwrap(cctx
, sctx
, 0, actual_mech
);
831 wrapunwrap(cctx
, sctx
, 1, actual_mech
);
832 wrapunwrap(sctx
, cctx
, 0, actual_mech
);
833 wrapunwrap(sctx
, cctx
, 1, actual_mech
);
837 wrapunwrap_iov(cctx
, sctx
, 0, actual_mech
);
838 wrapunwrap_iov(cctx
, sctx
, USE_HEADER_ONLY
|FORCE_IOV
, actual_mech
);
839 wrapunwrap_iov(cctx
, sctx
, USE_HEADER_ONLY
, actual_mech
);
840 wrapunwrap_iov(cctx
, sctx
, USE_CONF
, actual_mech
);
841 wrapunwrap_iov(cctx
, sctx
, USE_CONF
|USE_HEADER_ONLY
, actual_mech
);
843 wrapunwrap_iov(cctx
, sctx
, FORCE_IOV
, actual_mech
);
844 wrapunwrap_iov(cctx
, sctx
, USE_CONF
|FORCE_IOV
, actual_mech
);
845 wrapunwrap_iov(cctx
, sctx
, USE_HEADER_ONLY
|FORCE_IOV
, actual_mech
);
846 wrapunwrap_iov(cctx
, sctx
, USE_CONF
|USE_HEADER_ONLY
|FORCE_IOV
, actual_mech
);
848 wrapunwrap_iov(cctx
, sctx
, USE_SIGN_ONLY
|FORCE_IOV
, actual_mech
);
849 wrapunwrap_iov(cctx
, sctx
, USE_CONF
|USE_SIGN_ONLY
|FORCE_IOV
, actual_mech
);
850 wrapunwrap_iov(cctx
, sctx
, USE_CONF
|USE_HEADER_ONLY
|USE_SIGN_ONLY
|FORCE_IOV
, actual_mech
);
853 wrapunwrap_iov(cctx
, sctx
, 0, actual_mech
);
854 wrapunwrap_iov(cctx
, sctx
, FORCE_IOV
, actual_mech
);
856 wrapunwrap_iov(cctx
, sctx
, USE_CONF
, actual_mech
);
857 wrapunwrap_iov(cctx
, sctx
, USE_CONF
|FORCE_IOV
, actual_mech
);
859 wrapunwrap_iov(cctx
, sctx
, USE_SIGN_ONLY
, actual_mech
);
860 wrapunwrap_iov(cctx
, sctx
, USE_SIGN_ONLY
|FORCE_IOV
, actual_mech
);
862 wrapunwrap_iov(cctx
, sctx
, USE_CONF
|USE_SIGN_ONLY
, actual_mech
);
863 wrapunwrap_iov(cctx
, sctx
, USE_CONF
|USE_SIGN_ONLY
|FORCE_IOV
, actual_mech
);
865 wrapunwrap_iov(cctx
, sctx
, USE_HEADER_ONLY
, actual_mech
);
866 wrapunwrap_iov(cctx
, sctx
, USE_HEADER_ONLY
|FORCE_IOV
, actual_mech
);
868 wrapunwrap_iov(cctx
, sctx
, USE_CONF
|USE_HEADER_ONLY
, actual_mech
);
869 wrapunwrap_iov(cctx
, sctx
, USE_CONF
|USE_HEADER_ONLY
|FORCE_IOV
, actual_mech
);
872 if (getverifymic_flag
) {
873 getverifymic(cctx
, sctx
, actual_mech
);
874 getverifymic(cctx
, sctx
, actual_mech
);
875 getverifymic(sctx
, cctx
, actual_mech
);
876 getverifymic(sctx
, cctx
, actual_mech
);
880 gss_delete_sec_context(&min_stat
, &cctx
, NULL
);
881 gss_delete_sec_context(&min_stat
, &sctx
, NULL
);
883 if (deleg_cred
!= GSS_C_NO_CREDENTIAL
) {
884 gss_cred_id_t cred2
= GSS_C_NO_CREDENTIAL
;
888 printf("checking actual mech (%s) on delegated cred\n",
889 oid_to_string(actual_mech
));
890 loop(actual_mech
, nameoid
, argv
[0], deleg_cred
, &sctx
, &cctx
, &actual_mech2
, &cred2
);
892 gss_delete_sec_context(&min_stat
, &cctx
, NULL
);
893 gss_delete_sec_context(&min_stat
, &sctx
, NULL
);
895 gss_release_cred(&min_stat
, &cred2
);
897 /* try again using SPNEGO */
899 printf("checking spnego on delegated cred\n");
900 loop(GSS_SPNEGO_MECHANISM
, nameoid
, argv
[0], deleg_cred
, &sctx
, &cctx
,
901 &actual_mech2
, &cred2
);
903 gss_delete_sec_context(&min_stat
, &cctx
, NULL
);
904 gss_delete_sec_context(&min_stat
, &sctx
, NULL
);
906 gss_release_cred(&min_stat
, &cred2
);
908 /* check export/import */
911 maj_stat
= gss_export_cred(&min_stat
, deleg_cred
, &cb
);
912 if (maj_stat
!= GSS_S_COMPLETE
)
913 errx(1, "export failed: %s",
914 gssapi_err(maj_stat
, min_stat
, NULL
));
916 maj_stat
= gss_import_cred(&min_stat
, &cb
, &cred2
);
917 if (maj_stat
!= GSS_S_COMPLETE
)
918 errx(1, "import failed: %s",
919 gssapi_err(maj_stat
, min_stat
, NULL
));
921 gss_release_buffer(&min_stat
, &cb
);
922 gss_release_cred(&min_stat
, &deleg_cred
);
925 printf("checking actual mech (%s) on export/imported cred\n",
926 oid_to_string(actual_mech
));
927 loop(actual_mech
, nameoid
, argv
[0], cred2
, &sctx
, &cctx
,
928 &actual_mech2
, &deleg_cred
);
930 gss_release_cred(&min_stat
, &deleg_cred
);
932 gss_delete_sec_context(&min_stat
, &cctx
, NULL
);
933 gss_delete_sec_context(&min_stat
, &sctx
, NULL
);
935 /* try again using SPNEGO */
937 printf("checking SPNEGO on export/imported cred\n");
938 loop(GSS_SPNEGO_MECHANISM
, nameoid
, argv
[0], cred2
, &sctx
, &cctx
,
939 &actual_mech2
, &deleg_cred
);
941 gss_release_cred(&min_stat
, &deleg_cred
);
943 gss_delete_sec_context(&min_stat
, &cctx
, NULL
);
944 gss_delete_sec_context(&min_stat
, &sctx
, NULL
);
946 gss_release_cred(&min_stat
, &cred2
);
949 gss_release_cred(&min_stat
, &deleg_cred
);
956 krb5_free_context(context
);