xref: /freebsd/crypto/krb5/src/tests/etinfo.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1*7f2fe78bSCy Schubert /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2*7f2fe78bSCy Schubert /* tests/etinfo.c - Test harness for KDC etype-info behavior */
3*7f2fe78bSCy Schubert /*
4*7f2fe78bSCy Schubert  * Copyright (C) 2015 by the Massachusetts Institute of Technology.
5*7f2fe78bSCy Schubert  * All rights reserved.
6*7f2fe78bSCy Schubert  *
7*7f2fe78bSCy Schubert  * Redistribution and use in source and binary forms, with or without
8*7f2fe78bSCy Schubert  * modification, are permitted provided that the following conditions
9*7f2fe78bSCy Schubert  * are met:
10*7f2fe78bSCy Schubert  *
11*7f2fe78bSCy Schubert  * * Redistributions of source code must retain the above copyright
12*7f2fe78bSCy Schubert  *   notice, this list of conditions and the following disclaimer.
13*7f2fe78bSCy Schubert  *
14*7f2fe78bSCy Schubert  * * Redistributions in binary form must reproduce the above copyright
15*7f2fe78bSCy Schubert  *   notice, this list of conditions and the following disclaimer in
16*7f2fe78bSCy Schubert  *   the documentation and/or other materials provided with the
17*7f2fe78bSCy Schubert  *   distribution.
18*7f2fe78bSCy Schubert  *
19*7f2fe78bSCy Schubert  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20*7f2fe78bSCy Schubert  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21*7f2fe78bSCy Schubert  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22*7f2fe78bSCy Schubert  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23*7f2fe78bSCy Schubert  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24*7f2fe78bSCy Schubert  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25*7f2fe78bSCy Schubert  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26*7f2fe78bSCy Schubert  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27*7f2fe78bSCy Schubert  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28*7f2fe78bSCy Schubert  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29*7f2fe78bSCy Schubert  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30*7f2fe78bSCy Schubert  * OF THE POSSIBILITY OF SUCH DAMAGE.
31*7f2fe78bSCy Schubert  */
32*7f2fe78bSCy Schubert 
33*7f2fe78bSCy Schubert /*
34*7f2fe78bSCy Schubert  * Send an AS-REQ to the KDC for a specified principal, with an optionally
35*7f2fe78bSCy Schubert  * specified request enctype list.  Decode the output as either an AS-REP or a
36*7f2fe78bSCy Schubert  * KRB-ERROR and display the PA-ETYPE-INFO2, PA-ETYPE-INFO, and PA-PW-SALT
37*7f2fe78bSCy Schubert  * padata in the following format:
38*7f2fe78bSCy Schubert  *
39*7f2fe78bSCy Schubert  *     error/asrep etype-info2/etype-info/pw-salt enctype salt [s2kparams]
40*7f2fe78bSCy Schubert  *
41*7f2fe78bSCy Schubert  * enctype is omitted for PA-PW-SALT entries.  salt is displayed directly;
42*7f2fe78bSCy Schubert  * s2kparams is displayed in uppercase hex.
43*7f2fe78bSCy Schubert  */
44*7f2fe78bSCy Schubert 
45*7f2fe78bSCy Schubert #include "k5-int.h"
46*7f2fe78bSCy Schubert 
47*7f2fe78bSCy Schubert static krb5_context ctx;
48*7f2fe78bSCy Schubert 
49*7f2fe78bSCy Schubert static void
check(krb5_error_code code)50*7f2fe78bSCy Schubert check(krb5_error_code code)
51*7f2fe78bSCy Schubert {
52*7f2fe78bSCy Schubert     const char *errmsg;
53*7f2fe78bSCy Schubert 
54*7f2fe78bSCy Schubert     if (code) {
55*7f2fe78bSCy Schubert         errmsg = krb5_get_error_message(ctx, code);
56*7f2fe78bSCy Schubert         fprintf(stderr, "%s\n", errmsg);
57*7f2fe78bSCy Schubert         krb5_free_error_message(ctx, errmsg);
58*7f2fe78bSCy Schubert         exit(1);
59*7f2fe78bSCy Schubert     }
60*7f2fe78bSCy Schubert }
61*7f2fe78bSCy Schubert 
62*7f2fe78bSCy Schubert static void
display_etinfo(krb5_etype_info_entry ** list,const char * l1,const char * l2)63*7f2fe78bSCy Schubert display_etinfo(krb5_etype_info_entry **list, const char *l1, const char *l2)
64*7f2fe78bSCy Schubert {
65*7f2fe78bSCy Schubert     krb5_etype_info_entry *info;
66*7f2fe78bSCy Schubert     char etname[256];
67*7f2fe78bSCy Schubert     unsigned int i;
68*7f2fe78bSCy Schubert 
69*7f2fe78bSCy Schubert     for (; *list != NULL; list++) {
70*7f2fe78bSCy Schubert         info = *list;
71*7f2fe78bSCy Schubert         check(krb5_enctype_to_name(info->etype, TRUE, etname, sizeof(etname)));
72*7f2fe78bSCy Schubert         printf("%s %s %s ", l1, l2, etname);
73*7f2fe78bSCy Schubert         if (info->length != KRB5_ETYPE_NO_SALT)
74*7f2fe78bSCy Schubert             printf("%.*s", info->length, info->salt);
75*7f2fe78bSCy Schubert         else
76*7f2fe78bSCy Schubert             printf("(default)");
77*7f2fe78bSCy Schubert         if (info->s2kparams.length > 0) {
78*7f2fe78bSCy Schubert             printf(" ");
79*7f2fe78bSCy Schubert             for (i = 0; i < info->s2kparams.length; i++)
80*7f2fe78bSCy Schubert                 printf("%02X", (unsigned char)info->s2kparams.data[i]);
81*7f2fe78bSCy Schubert         }
82*7f2fe78bSCy Schubert         printf("\n");
83*7f2fe78bSCy Schubert     }
84*7f2fe78bSCy Schubert }
85*7f2fe78bSCy Schubert 
86*7f2fe78bSCy Schubert static void
display_padata(krb5_pa_data ** pa_list,const char * label)87*7f2fe78bSCy Schubert display_padata(krb5_pa_data **pa_list, const char *label)
88*7f2fe78bSCy Schubert {
89*7f2fe78bSCy Schubert     krb5_pa_data *pa;
90*7f2fe78bSCy Schubert     krb5_data d;
91*7f2fe78bSCy Schubert     krb5_etype_info_entry **etinfo_list;
92*7f2fe78bSCy Schubert 
93*7f2fe78bSCy Schubert     for (; pa_list != NULL && *pa_list != NULL; pa_list++) {
94*7f2fe78bSCy Schubert         pa = *pa_list;
95*7f2fe78bSCy Schubert         d = make_data(pa->contents, pa->length);
96*7f2fe78bSCy Schubert         if (pa->pa_type == KRB5_PADATA_ETYPE_INFO2) {
97*7f2fe78bSCy Schubert             check(decode_krb5_etype_info2(&d, &etinfo_list));
98*7f2fe78bSCy Schubert             display_etinfo(etinfo_list, label, "etype_info2");
99*7f2fe78bSCy Schubert             krb5_free_etype_info(ctx, etinfo_list);
100*7f2fe78bSCy Schubert         } else if (pa->pa_type == KRB5_PADATA_ETYPE_INFO) {
101*7f2fe78bSCy Schubert             check(decode_krb5_etype_info(&d, &etinfo_list));
102*7f2fe78bSCy Schubert             display_etinfo(etinfo_list, label, "etype_info");
103*7f2fe78bSCy Schubert             krb5_free_etype_info(ctx, etinfo_list);
104*7f2fe78bSCy Schubert         } else if (pa->pa_type == KRB5_PADATA_PW_SALT) {
105*7f2fe78bSCy Schubert             printf("%s pw_salt %.*s\n", label, (int)d.length, d.data);
106*7f2fe78bSCy Schubert         } else if (pa->pa_type == KRB5_PADATA_AFS3_SALT) {
107*7f2fe78bSCy Schubert             printf("%s afs3_salt %.*s\n", label, (int)d.length, d.data);
108*7f2fe78bSCy Schubert         }
109*7f2fe78bSCy Schubert     }
110*7f2fe78bSCy Schubert }
111*7f2fe78bSCy Schubert 
112*7f2fe78bSCy Schubert int
main(int argc,char ** argv)113*7f2fe78bSCy Schubert main(int argc, char **argv)
114*7f2fe78bSCy Schubert {
115*7f2fe78bSCy Schubert     krb5_principal client;
116*7f2fe78bSCy Schubert     krb5_get_init_creds_opt *opt;
117*7f2fe78bSCy Schubert     krb5_init_creds_context icc;
118*7f2fe78bSCy Schubert     krb5_data reply, request, realm;
119*7f2fe78bSCy Schubert     krb5_error *error;
120*7f2fe78bSCy Schubert     krb5_kdc_rep *asrep;
121*7f2fe78bSCy Schubert     krb5_pa_data **padata;
122*7f2fe78bSCy Schubert     krb5_preauthtype pa_type = KRB5_PADATA_NONE;
123*7f2fe78bSCy Schubert     unsigned int flags;
124*7f2fe78bSCy Schubert     int primary = 0;
125*7f2fe78bSCy Schubert 
126*7f2fe78bSCy Schubert     if (argc < 2 || argc > 3) {
127*7f2fe78bSCy Schubert         fprintf(stderr, "Usage: %s princname [patype]\n", argv[0]);
128*7f2fe78bSCy Schubert         exit(1);
129*7f2fe78bSCy Schubert     }
130*7f2fe78bSCy Schubert     check(krb5_init_context(&ctx));
131*7f2fe78bSCy Schubert     check(krb5_parse_name(ctx, argv[1], &client));
132*7f2fe78bSCy Schubert     if (argc >= 3)
133*7f2fe78bSCy Schubert         pa_type = atoi(argv[2]);
134*7f2fe78bSCy Schubert 
135*7f2fe78bSCy Schubert     check(krb5_get_init_creds_opt_alloc(ctx, &opt));
136*7f2fe78bSCy Schubert     if (pa_type != KRB5_PADATA_NONE)
137*7f2fe78bSCy Schubert         krb5_get_init_creds_opt_set_preauth_list(opt, &pa_type, 1);
138*7f2fe78bSCy Schubert 
139*7f2fe78bSCy Schubert     check(krb5_init_creds_init(ctx, client, NULL, NULL, 0, opt, &icc));
140*7f2fe78bSCy Schubert     reply = empty_data();
141*7f2fe78bSCy Schubert     check(krb5_init_creds_step(ctx, icc, &reply, &request, &realm, &flags));
142*7f2fe78bSCy Schubert     assert(flags == KRB5_INIT_CREDS_STEP_FLAG_CONTINUE);
143*7f2fe78bSCy Schubert     check(krb5_sendto_kdc(ctx, &request, &realm, &reply, &primary, 0));
144*7f2fe78bSCy Schubert 
145*7f2fe78bSCy Schubert     if (decode_krb5_error(&reply, &error) == 0) {
146*7f2fe78bSCy Schubert         decode_krb5_padata_sequence(&error->e_data, &padata);
147*7f2fe78bSCy Schubert         if (error->error == KDC_ERR_PREAUTH_REQUIRED) {
148*7f2fe78bSCy Schubert             display_padata(padata, "error");
149*7f2fe78bSCy Schubert         } else if (error->error == KDC_ERR_MORE_PREAUTH_DATA_REQUIRED) {
150*7f2fe78bSCy Schubert             display_padata(padata, "more");
151*7f2fe78bSCy Schubert         } else {
152*7f2fe78bSCy Schubert             fprintf(stderr, "Unexpected error %d\n", (int)error->error);
153*7f2fe78bSCy Schubert             return 1;
154*7f2fe78bSCy Schubert         }
155*7f2fe78bSCy Schubert         krb5_free_pa_data(ctx, padata);
156*7f2fe78bSCy Schubert         krb5_free_error(ctx, error);
157*7f2fe78bSCy Schubert     } else if (decode_krb5_as_rep(&reply, &asrep) == 0) {
158*7f2fe78bSCy Schubert         display_padata(asrep->padata, "asrep");
159*7f2fe78bSCy Schubert         krb5_free_kdc_rep(ctx, asrep);
160*7f2fe78bSCy Schubert     } else {
161*7f2fe78bSCy Schubert         abort();
162*7f2fe78bSCy Schubert     }
163*7f2fe78bSCy Schubert 
164*7f2fe78bSCy Schubert     krb5_free_data_contents(ctx, &request);
165*7f2fe78bSCy Schubert     krb5_free_data_contents(ctx, &reply);
166*7f2fe78bSCy Schubert     krb5_free_data_contents(ctx, &realm);
167*7f2fe78bSCy Schubert     krb5_get_init_creds_opt_free(ctx, opt);
168*7f2fe78bSCy Schubert     krb5_init_creds_free(ctx, icc);
169*7f2fe78bSCy Schubert     krb5_free_principal(ctx, client);
170*7f2fe78bSCy Schubert     krb5_free_context(ctx);
171*7f2fe78bSCy Schubert     return 0;
172*7f2fe78bSCy Schubert }
173