1*7f2fe78bSCy Schubert /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2*7f2fe78bSCy Schubert /* tests/replay.c - test replay cache using libkrb5 functions */
3*7f2fe78bSCy Schubert /*
4*7f2fe78bSCy Schubert * Copyright (C) 2019 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 #include "k5-int.h"
34*7f2fe78bSCy Schubert
35*7f2fe78bSCy Schubert int
main(int argc,char ** argv)36*7f2fe78bSCy Schubert main(int argc, char **argv)
37*7f2fe78bSCy Schubert {
38*7f2fe78bSCy Schubert krb5_error_code ret;
39*7f2fe78bSCy Schubert krb5_context ctx;
40*7f2fe78bSCy Schubert krb5_auth_context c_authcon, s_authcon, s_authcon2;
41*7f2fe78bSCy Schubert krb5_rcache rc;
42*7f2fe78bSCy Schubert krb5_ccache cc;
43*7f2fe78bSCy Schubert krb5_principal client, server;
44*7f2fe78bSCy Schubert krb5_creds mcred, *cred, **tmpcreds;
45*7f2fe78bSCy Schubert krb5_data der_apreq, der_krbsafe, der_krbpriv, *der_krbcred, tmpdata;
46*7f2fe78bSCy Schubert krb5_address addr;
47*7f2fe78bSCy Schubert struct in_addr inaddr;
48*7f2fe78bSCy Schubert const char *server_name;
49*7f2fe78bSCy Schubert
50*7f2fe78bSCy Schubert assert(argc == 2);
51*7f2fe78bSCy Schubert server_name = argv[1];
52*7f2fe78bSCy Schubert
53*7f2fe78bSCy Schubert /* Create client and server auth contexts. (They will use a replay cache
54*7f2fe78bSCy Schubert * by default.) */
55*7f2fe78bSCy Schubert ret = krb5_init_context(&ctx);
56*7f2fe78bSCy Schubert assert(ret == 0);
57*7f2fe78bSCy Schubert ret = krb5_auth_con_init(ctx, &c_authcon);
58*7f2fe78bSCy Schubert assert(ret == 0);
59*7f2fe78bSCy Schubert ret = krb5_auth_con_init(ctx, &s_authcon);
60*7f2fe78bSCy Schubert assert(ret == 0);
61*7f2fe78bSCy Schubert
62*7f2fe78bSCy Schubert /* Set dummy addresses for the auth contexts. */
63*7f2fe78bSCy Schubert memset(&inaddr, 0, sizeof(inaddr));
64*7f2fe78bSCy Schubert addr.addrtype = ADDRTYPE_INET;
65*7f2fe78bSCy Schubert addr.length = sizeof(inaddr);
66*7f2fe78bSCy Schubert addr.contents = (uint8_t *)&inaddr;
67*7f2fe78bSCy Schubert ret = krb5_auth_con_setaddrs(ctx, c_authcon, &addr, &addr);
68*7f2fe78bSCy Schubert assert(ret == 0);
69*7f2fe78bSCy Schubert ret = krb5_auth_con_setaddrs(ctx, s_authcon, &addr, &addr);
70*7f2fe78bSCy Schubert assert(ret == 0);
71*7f2fe78bSCy Schubert
72*7f2fe78bSCy Schubert /* Set up replay caches for the auth contexts. */
73*7f2fe78bSCy Schubert tmpdata = string2data("testclient");
74*7f2fe78bSCy Schubert ret = krb5_get_server_rcache(ctx, &tmpdata, &rc);
75*7f2fe78bSCy Schubert assert(ret == 0);
76*7f2fe78bSCy Schubert ret = krb5_auth_con_setrcache(ctx, c_authcon, rc);
77*7f2fe78bSCy Schubert assert(ret == 0);
78*7f2fe78bSCy Schubert tmpdata = string2data("testserver");
79*7f2fe78bSCy Schubert ret = krb5_get_server_rcache(ctx, &tmpdata, &rc);
80*7f2fe78bSCy Schubert assert(ret == 0);
81*7f2fe78bSCy Schubert ret = krb5_auth_con_setrcache(ctx, s_authcon, rc);
82*7f2fe78bSCy Schubert assert(ret == 0);
83*7f2fe78bSCy Schubert
84*7f2fe78bSCy Schubert /* Construct the client and server principal names. */
85*7f2fe78bSCy Schubert ret = krb5_cc_default(ctx, &cc);
86*7f2fe78bSCy Schubert assert(ret == 0);
87*7f2fe78bSCy Schubert ret = krb5_cc_get_principal(ctx, cc, &client);
88*7f2fe78bSCy Schubert assert(ret == 0);
89*7f2fe78bSCy Schubert ret = krb5_parse_name(ctx, server_name, &server);
90*7f2fe78bSCy Schubert assert(ret == 0);
91*7f2fe78bSCy Schubert
92*7f2fe78bSCy Schubert /* Get credentials for the client. */
93*7f2fe78bSCy Schubert memset(&mcred, 0, sizeof(mcred));
94*7f2fe78bSCy Schubert mcred.client = client;
95*7f2fe78bSCy Schubert mcred.server = server;
96*7f2fe78bSCy Schubert ret = krb5_get_credentials(ctx, 0, cc, &mcred, &cred);
97*7f2fe78bSCy Schubert assert(ret == 0);
98*7f2fe78bSCy Schubert
99*7f2fe78bSCy Schubert /* Send an AP-REP to establish the sessions. */
100*7f2fe78bSCy Schubert ret = krb5_mk_req_extended(ctx, &c_authcon, 0, NULL, cred, &der_apreq);
101*7f2fe78bSCy Schubert assert(ret == 0);
102*7f2fe78bSCy Schubert ret = krb5_rd_req(ctx, &s_authcon, &der_apreq, NULL, NULL, NULL, NULL);
103*7f2fe78bSCy Schubert assert(ret == 0);
104*7f2fe78bSCy Schubert
105*7f2fe78bSCy Schubert /* Set up another server auth context with the same rcache name and replay
106*7f2fe78bSCy Schubert * the AP-REQ. */
107*7f2fe78bSCy Schubert ret = krb5_auth_con_init(ctx, &s_authcon2);
108*7f2fe78bSCy Schubert assert(ret == 0);
109*7f2fe78bSCy Schubert tmpdata = string2data("testserver");
110*7f2fe78bSCy Schubert ret = krb5_get_server_rcache(ctx, &tmpdata, &rc);
111*7f2fe78bSCy Schubert assert(ret == 0);
112*7f2fe78bSCy Schubert ret = krb5_auth_con_setrcache(ctx, s_authcon2, rc);
113*7f2fe78bSCy Schubert assert(ret == 0);
114*7f2fe78bSCy Schubert ret = krb5_rd_req(ctx, &s_authcon2, &der_apreq, NULL, NULL, NULL, NULL);
115*7f2fe78bSCy Schubert assert(ret == KRB5KRB_AP_ERR_REPEAT);
116*7f2fe78bSCy Schubert krb5_auth_con_free(ctx, s_authcon2);
117*7f2fe78bSCy Schubert
118*7f2fe78bSCy Schubert /* Make a KRB-SAFE message with the client auth context. */
119*7f2fe78bSCy Schubert tmpdata = string2data("safemsg");
120*7f2fe78bSCy Schubert ret = krb5_mk_safe(ctx, c_authcon, &tmpdata, &der_krbsafe, NULL);
121*7f2fe78bSCy Schubert assert(ret == 0);
122*7f2fe78bSCy Schubert /* Play it back to the client to detect a reflection. */
123*7f2fe78bSCy Schubert ret = krb5_rd_safe(ctx, c_authcon, &der_krbsafe, &tmpdata, NULL);
124*7f2fe78bSCy Schubert assert(ret == KRB5KRB_AP_ERR_REPEAT);
125*7f2fe78bSCy Schubert /* Send it to the server auth context twice, to detect a replay. */
126*7f2fe78bSCy Schubert ret = krb5_rd_safe(ctx, s_authcon, &der_krbsafe, &tmpdata, NULL);
127*7f2fe78bSCy Schubert assert(ret == 0);
128*7f2fe78bSCy Schubert krb5_free_data_contents(ctx, &tmpdata);
129*7f2fe78bSCy Schubert ret = krb5_rd_safe(ctx, s_authcon, &der_krbsafe, &tmpdata, NULL);
130*7f2fe78bSCy Schubert assert(ret == KRB5KRB_AP_ERR_REPEAT);
131*7f2fe78bSCy Schubert
132*7f2fe78bSCy Schubert /* Make a KRB-PRIV message with the client auth context. */
133*7f2fe78bSCy Schubert tmpdata = string2data("safemsg");
134*7f2fe78bSCy Schubert ret = krb5_mk_priv(ctx, c_authcon, &tmpdata, &der_krbpriv, NULL);
135*7f2fe78bSCy Schubert assert(ret == 0);
136*7f2fe78bSCy Schubert /* Play it back to the client to detect a reflection. */
137*7f2fe78bSCy Schubert ret = krb5_rd_priv(ctx, c_authcon, &der_krbpriv, &tmpdata, NULL);
138*7f2fe78bSCy Schubert assert(ret == KRB5KRB_AP_ERR_REPEAT);
139*7f2fe78bSCy Schubert /* Send it to the server auth context twice, to detect a replay. */
140*7f2fe78bSCy Schubert ret = krb5_rd_priv(ctx, s_authcon, &der_krbpriv, &tmpdata, NULL);
141*7f2fe78bSCy Schubert assert(ret == 0);
142*7f2fe78bSCy Schubert krb5_free_data_contents(ctx, &tmpdata);
143*7f2fe78bSCy Schubert ret = krb5_rd_priv(ctx, s_authcon, &der_krbpriv, &tmpdata, NULL);
144*7f2fe78bSCy Schubert assert(ret == KRB5KRB_AP_ERR_REPEAT);
145*7f2fe78bSCy Schubert
146*7f2fe78bSCy Schubert /* Make a KRB-CRED message with the client auth context. */
147*7f2fe78bSCy Schubert tmpdata = string2data("safemsg");
148*7f2fe78bSCy Schubert ret = krb5_mk_1cred(ctx, c_authcon, cred, &der_krbcred, NULL);
149*7f2fe78bSCy Schubert assert(ret == 0);
150*7f2fe78bSCy Schubert /* Play it back to the client to detect a reflection. */
151*7f2fe78bSCy Schubert ret = krb5_rd_cred(ctx, c_authcon, der_krbcred, &tmpcreds, NULL);
152*7f2fe78bSCy Schubert assert(ret == KRB5KRB_AP_ERR_REPEAT);
153*7f2fe78bSCy Schubert /* Send it to the server auth context twice, to detect a replay. */
154*7f2fe78bSCy Schubert ret = krb5_rd_cred(ctx, s_authcon, der_krbcred, &tmpcreds, NULL);
155*7f2fe78bSCy Schubert assert(ret == 0);
156*7f2fe78bSCy Schubert krb5_free_tgt_creds(ctx, tmpcreds);
157*7f2fe78bSCy Schubert ret = krb5_rd_cred(ctx, s_authcon, der_krbcred, &tmpcreds, NULL);
158*7f2fe78bSCy Schubert assert(ret == KRB5KRB_AP_ERR_REPEAT);
159*7f2fe78bSCy Schubert
160*7f2fe78bSCy Schubert krb5_free_data_contents(ctx, &der_apreq);
161*7f2fe78bSCy Schubert krb5_free_data_contents(ctx, &der_krbsafe);
162*7f2fe78bSCy Schubert krb5_free_data_contents(ctx, &der_krbpriv);
163*7f2fe78bSCy Schubert krb5_free_data(ctx, der_krbcred);
164*7f2fe78bSCy Schubert krb5_free_creds(ctx, cred);
165*7f2fe78bSCy Schubert krb5_cc_close(ctx, cc);
166*7f2fe78bSCy Schubert krb5_free_principal(ctx, client);
167*7f2fe78bSCy Schubert krb5_free_principal(ctx, server);
168*7f2fe78bSCy Schubert krb5_auth_con_free(ctx, c_authcon);
169*7f2fe78bSCy Schubert krb5_auth_con_free(ctx, s_authcon);
170*7f2fe78bSCy Schubert krb5_free_context(ctx);
171*7f2fe78bSCy Schubert return 0;
172*7f2fe78bSCy Schubert }
173