Search in sources :

Example 1 with EncryptionType

use of org.apache.directory.shared.kerberos.codec.types.EncryptionType in project wildfly by wildfly.

the class AbstractKrb5ConfServerSetupTask method createKeytab.

/**
     * Creates a keytab file for given principal.
     *
     * @param principalName
     * @param passPhrase
     * @param keytabFile
     * @throws IOException
     */
protected void createKeytab(final String principalName, final String passPhrase, final File keytabFile) throws IOException {
    LOGGER.trace("Principal name: " + principalName);
    final KerberosTime timeStamp = new KerberosTime();
    DataOutputStream dos = null;
    try {
        dos = new DataOutputStream(new FileOutputStream(keytabFile));
        dos.write(Keytab.VERSION_0X502_BYTES);
        for (Map.Entry<EncryptionType, EncryptionKey> keyEntry : KerberosKeyFactory.getKerberosKeys(principalName, passPhrase).entrySet()) {
            final EncryptionKey key = keyEntry.getValue();
            final byte keyVersion = (byte) key.getKeyVersion();
            // entries.add(new KeytabEntry(principalName, principalType, timeStamp, keyVersion, key));
            final ByteArrayOutputStream baos = new ByteArrayOutputStream();
            DataOutputStream entryDos = new DataOutputStream(baos);
            // handle principal name
            String[] spnSplit = principalName.split("@");
            String nameComponent = spnSplit[0];
            String realm = spnSplit[1];
            String[] nameComponents = nameComponent.split("/");
            try {
                // increment for v1
                entryDos.writeShort((short) nameComponents.length);
                entryDos.writeUTF(realm);
                // write components
                for (String component : nameComponents) {
                    entryDos.writeUTF(component);
                }
                // principal type: KRB5_NT_PRINCIPAL
                entryDos.writeInt(1);
                entryDos.writeInt((int) (timeStamp.getTime() / 1000));
                entryDos.write(keyVersion);
                entryDos.writeShort((short) key.getKeyType().getValue());
                byte[] data = key.getKeyValue();
                entryDos.writeShort((short) data.length);
                entryDos.write(data);
            } finally {
                IOUtils.closeQuietly(entryDos);
            }
            final byte[] entryBytes = baos.toByteArray();
            dos.writeInt(entryBytes.length);
            dos.write(entryBytes);
        }
    // } catch (IOException ioe) {
    } finally {
        IOUtils.closeQuietly(dos);
    }
}
Also used : EncryptionType(org.apache.directory.shared.kerberos.codec.types.EncryptionType) DataOutputStream(java.io.DataOutputStream) FileOutputStream(java.io.FileOutputStream) KerberosTime(org.apache.directory.shared.kerberos.KerberosTime) EncryptionKey(org.apache.directory.shared.kerberos.components.EncryptionKey) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with EncryptionType

use of org.apache.directory.shared.kerberos.codec.types.EncryptionType in project karaf by apache.

the class Krb5LoginModuleTest method createKeytabEntry.

private KeytabEntry createKeytabEntry() throws ParseException {
    String principalName = "hnelson@EXAMPLE.COM";
    int principalType = 1;
    String zuluTime = "20070217235745Z";
    Date date = null;
    synchronized (KerberosUtils.UTC_DATE_FORMAT) {
        date = KerberosUtils.UTC_DATE_FORMAT.parse(zuluTime);
    }
    KerberosTime timeStamp = new KerberosTime(date.getTime());
    byte keyVersion = 1;
    String passPhrase = "secret";
    Map<EncryptionType, EncryptionKey> keys = KerberosKeyFactory.getKerberosKeys(principalName, passPhrase);
    EncryptionKey key = keys.get(EncryptionType.AES128_CTS_HMAC_SHA1_96);
    return new KeytabEntry(principalName, principalType, timeStamp, keyVersion, key);
}
Also used : EncryptionType(org.apache.directory.shared.kerberos.codec.types.EncryptionType) KerberosTime(org.apache.directory.shared.kerberos.KerberosTime) EncryptionKey(org.apache.directory.shared.kerberos.components.EncryptionKey) Date(java.util.Date) KeytabEntry(org.apache.directory.server.kerberos.shared.keytab.KeytabEntry)

Aggregations

KerberosTime (org.apache.directory.shared.kerberos.KerberosTime)2 EncryptionType (org.apache.directory.shared.kerberos.codec.types.EncryptionType)2 EncryptionKey (org.apache.directory.shared.kerberos.components.EncryptionKey)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataOutputStream (java.io.DataOutputStream)1 FileOutputStream (java.io.FileOutputStream)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 KeytabEntry (org.apache.directory.server.kerberos.shared.keytab.KeytabEntry)1