Path: blob/main/crypto/krb5/src/appl/gss-sample/t_gss_sample.py
34907 views
# Copyright (C) 2010 by the Massachusetts Institute of Technology.1# All rights reserved.2#3# Export of this software from the United States of America may4# require a specific license from the United States Government.5# It is the responsibility of any person or organization contemplating6# export to obtain such a license before exporting.7#8# WITHIN THAT CONSTRAINT, permission to use, copy, modify, and9# distribute this software and its documentation for any purpose and10# without fee is hereby granted, provided that the above copyright11# notice appear in all copies and that both that copyright notice and12# this permission notice appear in supporting documentation, and that13# the name of M.I.T. not be used in advertising or publicity pertaining14# to distribution of the software without specific, written prior15# permission. Furthermore if you modify this software you must label16# your software as modified software and not distribute it in such a17# fashion that it might be confused with the original M.I.T. software.18# M.I.T. makes no representations about the suitability of19# this software for any purpose. It is provided "as is" without express20# or implied warranty.2122from k5test import *2324appdir = os.path.join(buildtop, 'appl', 'gss-sample')25gss_client = os.path.join(appdir, 'gss-client')26gss_server = os.path.join(appdir, 'gss-server')2728# Run a gss-server process and a gss-client process, with additional29# gss-client flags given by options and additional gss-server flags30# given by server_options. Return the output of gss-client.31def run_client_server(realm, options, server_options, **kwargs):32portstr = str(realm.server_port())33server_args = [gss_server, '-export', '-port', portstr]34server_args += server_options + ['host']35server = realm.start_server(server_args, 'starting...')36realm.run([gss_client, '-port', portstr] + options +37[hostname, 'host', 'testmsg'], **kwargs)3839seen1 = seen2 = False40while 'expected_code' not in kwargs and not (seen1 and seen2):41line = server.stdout.readline()42if line == '':43fail('gss-server process exited unexpectedly')44if line == 'Accepted connection: "[email protected]"\n':45seen1 = True46if line == 'Received message: "testmsg"\n':47seen2 = True4849stop_daemon(server)5051# Run a gss-server and gss-client process, and verify that gss-client52# displayed the expected output for a successful negotiation.53def server_client_test(realm, options, server_options):54run_client_server(realm, options, server_options,55expected_msg='Signature verified.')5657# Make up a filename to hold user's initial credentials.58def ccache_savefile(realm):59return os.path.join(realm.testdir, 'ccache.copy')6061# Move user's initial credentials into the save file.62def ccache_save(realm):63os.rename(realm.ccache, ccache_savefile(realm))6465# Copy user's initial credentials from the save file into the ccache.66def ccache_restore(realm):67shutil.copyfile(ccache_savefile(realm), realm.ccache)6869# Perform a regular (TGS path) test of the server and client.70def tgs_test(realm, options, server_options=[]):71ccache_restore(realm)72server_client_test(realm, options, server_options)73realm.klist(realm.user_princ, realm.host_princ)7475# Perform a test of the server and client with initial credentials76# obtained through gss_acquire_cred_with_password().77def pw_test(realm, options, server_options=[]):78if os.path.exists(realm.ccache):79os.remove(realm.ccache)80if '-iakerb' in options:81# Use IAKERB realm discovery.82user = realm.user_princ.split('@')[0]83else:84user = realm.user_princ85options = options + ['-user', user, '-pass', password('user')]86server_client_test(realm, options, server_options)87if os.path.exists(realm.ccache):88fail('gss_acquire_cred_with_password created ccache')8990# Perform a test using the wrong password, and make sure that failure91# occurs during the expected operation (gss_init_sec_context() for92# IAKERB, gss_aqcuire_cred_with_password() otherwise).93def wrong_pw_test(realm, options, server_options=[], iakerb=False):94options = options + ['-user', realm.user_princ, '-pass', 'wrongpw']95failed_op = 'initializing context' if iakerb else 'acquiring creds'96msg = 'GSS-API error ' + failed_op97run_client_server(realm, options, server_options, expected_code=1,98expected_msg=msg)99100# Perform a test of the server and client with initial credentials101# obtained with the client keytab102def kt_test(realm, options, server_options=[]):103if os.path.exists(realm.ccache):104os.remove(realm.ccache)105server_client_test(realm, options, server_options)106realm.klist(realm.user_princ, realm.host_princ)107108for realm in multipass_realms():109ccache_save(realm)110111mark('TGS')112tgs_test(realm, ['-krb5'])113tgs_test(realm, ['-spnego'])114tgs_test(realm, ['-iakerb'], ['-iakerb'])115# test default (i.e., krb5) mechanism with GSS_C_DCE_STYLE116tgs_test(realm, ['-dce'])117118mark('AP')119ccache_save(realm)120tgs_test(realm, ['-krb5'])121tgs_test(realm, ['-spnego'])122tgs_test(realm, ['-iakerb'], ['-iakerb'])123tgs_test(realm, ['-dce'])124125mark('pw')126pw_test(realm, ['-krb5'])127pw_test(realm, ['-spnego'])128pw_test(realm, ['-iakerb'], ['-iakerb'])129pw_test(realm, ['-dce'])130131mark('wrong pw')132wrong_pw_test(realm, ['-krb5'])133wrong_pw_test(realm, ['-spnego'])134wrong_pw_test(realm, ['-iakerb'], ['-iakerb'], True)135wrong_pw_test(realm, ['-dce'])136137mark('client keytab')138realm.extract_keytab(realm.user_princ, realm.client_keytab)139kt_test(realm, ['-krb5'])140kt_test(realm, ['-spnego'])141kt_test(realm, ['-iakerb'], ['-iakerb'])142kt_test(realm, ['-dce'])143144success('GSS sample application')145146147