Blame src/tests/gssapi/t_enctypes.py

Packit fd8b60
from k5test import *
Packit fd8b60
Packit Bot 805b76
# Define some convenience abbreviations for enctypes we will see in
Packit Bot 805b76
# test program output.  For background, aes256 and aes128 are "CFX
Packit Bot 805b76
# enctypes", meaning that they imply support for RFC 4121, while des3
Packit Bot 805b76
# and rc4 are not.  DES3 keys will appear as 'des3-cbc-raw' in
Packit Bot 805b76
# t_enctypes output because that's how GSSAPI does raw triple-DES
Packit Bot 805b76
# encryption without the RFC3961 framing.
Packit fd8b60
aes256 = 'aes256-cts-hmac-sha1-96'
Packit fd8b60
aes128 = 'aes128-cts-hmac-sha1-96'
Packit Bot 805b76
des3 = 'des3-cbc-sha1'
Packit Bot 805b76
d_des3 = 'DEPRECATED:des3-cbc-sha1'
Packit Bot 805b76
des3raw = 'des3-cbc-raw'
Packit Bot 805b76
d_des3raw = 'DEPRECATED:des3-cbc-raw'
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.
Packit Bot 805b76
supp='aes256-cts:normal aes128-cts:normal des3-cbc-sha1:normal rc4-hmac:normal'
Packit Bot 805b76
conf = {'libdefaults': {'permitted_enctypes': 'aes des3 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 Bot 805b76
# If the initiator constrains the permitted session enctypes to des3,
Packit Bot 805b76
# no acceptor subkey will be generated because we can't upgrade to a
Packit Bot 805b76
# CFX enctype.
Packit Bot 805b76
test('init des3', 'des3', None,
Packit Bot 805b76
     tktenc=aes256, tktsession=d_des3,
Packit Bot 805b76
     proto='rfc1964', isubkey=des3raw, asubkey=None)
Packit Bot 805b76
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
Packit Bot 805b76
# [aes256 aes128 des3] 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 Bot 805b76
# If the initiator permits rc4 but prefers des3, it will send an
Packit Bot 805b76
# upgrade list of [des3], but the acceptor won't generate a subkey
Packit Bot 805b76
# because des3 isn't a CFX enctype.
Packit Bot 805b76
test('upgrade init des3+rc4', 'des3 rc4', None,
Packit Bot 805b76
     tktenc=aes256, tktsession=d_rc4,
Packit Bot 805b76
     proto='rfc1964', isubkey=rc4, asubkey=None)
Packit Bot 805b76
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')