Blame src/tests/gssapi/t_enctypes.py

Packit fd8b60
from k5test import *
Packit fd8b60
rpm-build 1cb403
# Define some convenience abbreviations for enctypes we will see in test
rpm-build 1cb403
# program output.  For background, aes256 and aes128 are "CFX enctypes",
rpm-build 1cb403
# meaning that they imply support for RFC 4121, while rc4 does not.
Packit fd8b60
aes256 = 'aes256-cts-hmac-sha1-96'
Packit fd8b60
aes128 = 'aes128-cts-hmac-sha1-96'
Packit fd8b60
rc4 = 'arcfour-hmac'
Packit fd8b60
d_rc4 = 'DEPRECATED:arcfour-hmac'
Packit fd8b60
Packit fd8b60
# These tests make assumptions about the default enctype lists, so set
Packit fd8b60
# them explicitly rather than relying on the library defaults.
rpm-build 1cb403
supp='aes256-cts:normal aes128-cts:normal rc4-hmac:normal'
rpm-build 1cb403
conf = {'libdefaults': {'permitted_enctypes': 'aes rc4'},
Packit fd8b60
        'realms': {'$realm': {'supported_enctypes': supp}}}
Packit fd8b60
realm = K5Realm(krb5_conf=conf)
Packit fd8b60
shutil.copyfile(realm.ccache, os.path.join(realm.testdir, 'save'))
Packit fd8b60
Packit fd8b60
# Return an argument list for running t_enctypes with optional initiator
Packit fd8b60
# and acceptor enctype lists.
Packit fd8b60
def cmdline(ienc, aenc):
Packit fd8b60
    iflags = ienc and ['-i', ienc] or []
Packit fd8b60
    aflags = aenc and ['-a', aenc] or []
Packit fd8b60
    return ['./t_enctypes'] + iflags + aflags + ['p:' + realm.host_princ]
Packit fd8b60
Packit fd8b60
Packit fd8b60
# Run t_enctypes with optional initiator and acceptor enctype lists,
Packit fd8b60
# and check that it succeeds with the expected output.  Also check
Packit fd8b60
# that the ticket we got has the expected encryption key and session
Packit fd8b60
# key.
Packit fd8b60
def test(msg, ienc, aenc, tktenc='', tktsession='', proto='', isubkey='',
Packit fd8b60
         asubkey=None):
Packit fd8b60
    shutil.copyfile(os.path.join(realm.testdir, 'save'), realm.ccache)
Packit fd8b60
    # Run the test program and check its output.
Packit fd8b60
    out = realm.run(cmdline(ienc, aenc)).split()
Packit fd8b60
    if out[0] != proto or out[1] != isubkey:
Packit fd8b60
        fail(msg)
Packit fd8b60
    if asubkey is not None and (len(out) < 3 or out[2] != asubkey):
Packit fd8b60
        fail(msg)
Packit fd8b60
    lines = realm.run([klist, '-e']).splitlines()
Packit fd8b60
    for ind, line in enumerate(lines):
Packit fd8b60
        if realm.host_princ in line:
Packit fd8b60
            if lines[ind + 1].strip() != ('Etype (skey, tkt): %s, %s' %
Packit fd8b60
                                          (tktsession, tktenc)):
Packit fd8b60
                fail(msg)
Packit fd8b60
            break
Packit fd8b60
Packit fd8b60
# Run t_enctypes with optional initiator and acceptor enctype lists,
Packit fd8b60
# and check that it fails with the expected error message.
Packit fd8b60
def test_err(msg, ienc, aenc, expected_err):
Packit fd8b60
    shutil.copyfile(os.path.join(realm.testdir, 'save'), realm.ccache)
Packit fd8b60
    realm.run(cmdline(ienc, aenc), expected_code=1, expected_msg=expected_err)
Packit fd8b60
Packit fd8b60
Packit fd8b60
# By default, all of the key enctypes should be aes256.
Packit fd8b60
test('noargs', None, None,
Packit fd8b60
     tktenc=aes256, tktsession=aes256,
Packit fd8b60
     proto='cfx', isubkey=aes256, asubkey=aes256)
Packit fd8b60
Packit fd8b60
# When the initiator constrains the permitted session enctypes to
Packit fd8b60
# aes128, the ticket encryption key should remain aes256.  The client
Packit fd8b60
# initiator will not send an RFC 4537 upgrade list because it sees no
Packit fd8b60
# other permitted enctypes, so the acceptor subkey will not be
Packit fd8b60
# upgraded from aes128.
Packit fd8b60
test('init aes128', 'aes128-cts', None,
Packit fd8b60
     tktenc=aes256, tktsession=aes128,
Packit fd8b60
     proto='cfx', isubkey=aes128, asubkey=aes128)
Packit fd8b60
Packit fd8b60
# If the initiator and acceptor both constrain the permitted session
Packit fd8b60
# enctypes to aes128, we should see the same keys as above.  This
Packit fd8b60
# tests that the acceptor does not mistakenly contrain the ticket
Packit fd8b60
# encryption key.
Packit fd8b60
test('both aes128', 'aes128-cts', 'aes128-cts',
Packit fd8b60
     tktenc=aes256, tktsession=aes128,
Packit fd8b60
     proto='cfx', isubkey=aes128, asubkey=aes128)
Packit fd8b60
Packit fd8b60
# If only the acceptor constrains the permitted session enctypes to
Packit fd8b60
# aes128, subkey negotiation fails because the acceptor considers the
Packit fd8b60
# aes256 session key to be non-permitted.
Packit fd8b60
test_err('acc aes128', None, 'aes128-cts',
Packit fd8b60
         'Encryption type aes256-cts-hmac-sha1-96 not permitted')
Packit fd8b60
Packit fd8b60
# Force the ticket session key to be rc4, so we can test some subkey
Packit fd8b60
# upgrade cases.  The ticket encryption key remains aes256.
Packit fd8b60
realm.run([kadminl, 'setstr', realm.host_princ, 'session_enctypes', 'rc4'])
Packit fd8b60
Packit fd8b60
# With no arguments, the initiator should send an upgrade list of
rpm-build 1cb403
# [aes256 aes128] and the acceptor should upgrade to an aes256
Packit fd8b60
# subkey.
Packit fd8b60
test('upgrade noargs', None, None,
Packit fd8b60
     tktenc=aes256, tktsession=d_rc4,
Packit fd8b60
     proto='cfx', isubkey=rc4, asubkey=aes256)
Packit fd8b60
Packit fd8b60
# If the initiator won't permit rc4 as a session key, it won't be able
Packit fd8b60
# to get a ticket.
Packit fd8b60
test_err('upgrade init aes', 'aes', None, 'no support for encryption type')
Packit fd8b60
Packit fd8b60
# If the initiator permits rc4 but prefers aes128, it will send an
Packit fd8b60
# upgrade list of [aes128] and the acceptor will upgrade to aes128.
Packit fd8b60
test('upgrade init aes128+rc4', 'aes128-cts rc4', None,
Packit fd8b60
     tktenc=aes256, tktsession=d_rc4,
Packit fd8b60
     proto='cfx', isubkey=rc4, asubkey=aes128)
Packit fd8b60
Packit fd8b60
# If the acceptor permits only aes128, subkey negotiation will fail
Packit fd8b60
# because the ticket session key and initiator subkey are
Packit fd8b60
# non-permitted.  (This is unfortunate if the acceptor's restriction
Packit fd8b60
# is only for the sake of the kernel, since we could upgrade to an
Packit fd8b60
# aes128 subkey, but it's the current semantics.)
Packit fd8b60
test_err('upgrade acc aes128', None, 'aes128-cts',
Packit fd8b60
         'Encryption type arcfour-hmac not permitted')
Packit fd8b60
Packit fd8b60
# If the acceptor permits rc4 but prefers aes128, it will negotiate an
Packit fd8b60
# upgrade to aes128.
Packit fd8b60
test('upgrade acc aes128 rc4', None, 'aes128-cts rc4',
Packit fd8b60
     tktenc=aes256, tktsession=d_rc4,
Packit fd8b60
     proto='cfx', isubkey=rc4, asubkey=aes128)
Packit fd8b60
Packit fd8b60
# In this test, the initiator and acceptor each prefer an AES enctype
Packit fd8b60
# to rc4, but they can't agree on which one, so no subkey is
Packit fd8b60
# generated.
Packit fd8b60
test('upgrade mismatch', 'aes128-cts rc4', 'aes256-cts rc4',
Packit fd8b60
     tktenc=aes256, tktsession=d_rc4,
Packit fd8b60
     proto='rfc1964', isubkey=rc4, asubkey=None)
Packit fd8b60
Packit fd8b60
success('gss_krb5_set_allowable_enctypes tests')