xref: /freebsd/crypto/krb5/src/tests/gssapi/t_iakerb.c (revision f1c4c3daccbaf3820f0e2224de53df12fc952fcc)
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* tests/gssapi/t_iakerb.c - IAKERB tests */
3 /*
4  * Copyright (C) 2024, 2025 by the Massachusetts Institute of Technology.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  *   notice, this list of conditions and the following disclaimer.
13  *
14  * * Redistributions in binary form must reproduce the above copyright
15  *   notice, this list of conditions and the following disclaimer in
16  *   the documentation and/or other materials provided with the
17  *   distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30  * OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <stdio.h>
34 #include <string.h>
35 #include "common.h"
36 
37 int
main(int argc,char ** argv)38 main(int argc, char **argv)
39 {
40     OM_uint32 major, minor;
41     const char *password;
42     gss_name_t iname, tname, aname;
43     gss_cred_id_t icred, acred;
44     gss_ctx_id_t ictx, actx;
45     gss_buffer_desc pwbuf;
46 
47     if (argc != 5) {
48         fprintf(stderr, "Usage: %s initiatorname password|- targetname "
49                 "acceptorname\n", argv[0]);
50         return 1;
51     }
52 
53     iname = import_name(argv[1]);
54     password = argv[2];
55     tname = import_name(argv[3]);
56     aname = import_name(argv[4]);
57 
58     if (strcmp(password, "-") != 0) {
59         pwbuf.value = (void *)password;
60         pwbuf.length = strlen(password);
61         major = gss_acquire_cred_with_password(&minor, iname, &pwbuf, 0,
62                                                &mechset_iakerb, GSS_C_INITIATE,
63                                                &icred, NULL, NULL);
64         check_gsserr("gss_acquire_cred_with_password", major, minor);
65     } else {
66         major = gss_acquire_cred(&minor, iname, GSS_C_INDEFINITE,
67                                  &mechset_iakerb, GSS_C_INITIATE, &icred, NULL,
68                                  NULL);
69         check_gsserr("gss_acquire_cred(iname)", major, minor);
70     }
71 
72     major = gss_acquire_cred(&minor, aname, GSS_C_INDEFINITE, &mechset_iakerb,
73                              GSS_C_ACCEPT, &acred, NULL, NULL);
74     check_gsserr("gss_acquire_cred(aname)", major, minor);
75 
76     establish_contexts(&mech_iakerb, icred, acred, tname, 0, &ictx, &actx,
77                        NULL, NULL, NULL);
78 
79     (void)gss_release_name(&minor, &iname);
80     (void)gss_release_name(&minor, &tname);
81     (void)gss_release_name(&minor, &aname);
82     (void)gss_release_cred(&minor, &icred);
83     (void)gss_release_cred(&minor, &acred);
84     (void)gss_delete_sec_context(&minor, &ictx, NULL);
85     (void)gss_delete_sec_context(&minor, &actx, NULL);
86     return 0;
87 }
88