2N/A/*
2N/A * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A
2N/A/*
2N/A * Copyright (C) 1998 by the FundsXpress, INC.
2N/A *
2N/A * All rights reserved.
2N/A *
2N/A * Export of this software from the United States of America may require
2N/A * a specific license from the United States Government. It is the
2N/A * responsibility of any person or organization contemplating export to
2N/A * obtain such a license before exporting.
2N/A *
2N/A * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
2N/A * distribute this software and its documentation for any purpose and
2N/A * without fee is hereby granted, provided that the above copyright
2N/A * notice appear in all copies and that both that copyright notice and
2N/A * this permission notice appear in supporting documentation, and that
2N/A * the name of FundsXpress. not be used in advertising or publicity pertaining
2N/A * to distribution of the software without specific, written prior
2N/A * permission. FundsXpress makes no representations about the suitability of
2N/A * this software for any purpose. It is provided "as is" without express
2N/A * or implied warranty.
2N/A *
2N/A * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
2N/A * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
2N/A * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
2N/A */
2N/A
2N/A#include "dk.h"
2N/A
2N/Astatic const unsigned char kerberos[] = "kerberos";
2N/A#define kerberos_len (sizeof(kerberos)-1)
2N/A
2N/Akrb5_error_code
2N/Akrb5int_dk_string_to_key(
2N/A krb5_context context,
2N/A const struct krb5_enc_provider *enc,
2N/A const krb5_data *string, const krb5_data *salt,
2N/A const krb5_data *parms, krb5_keyblock *key)
2N/A{
2N/A krb5_error_code ret;
2N/A size_t keybytes, keylength, concatlen;
2N/A unsigned char *concat, *foldstring, *foldkeydata;
2N/A krb5_data indata;
2N/A krb5_keyblock foldkey;
2N/A
2N/A /* key->length is checked by krb5_derive_key */
2N/A
2N/A keybytes = enc->keybytes;
2N/A keylength = enc->keylength;
2N/A
2N/A concatlen = string->length+(salt?salt->length:0);
2N/A
2N/A if ((concat = (unsigned char *) malloc(concatlen)) == NULL)
2N/A return(ENOMEM);
2N/A if ((foldstring = (unsigned char *) malloc(keybytes)) == NULL) {
2N/A free(concat);
2N/A return(ENOMEM);
2N/A }
2N/A if ((foldkeydata = (unsigned char *) malloc(keylength)) == NULL) {
2N/A free(foldstring);
2N/A free(concat);
2N/A return(ENOMEM);
2N/A }
2N/A
2N/A /* construct input string ( = string + salt), fold it, make_key it */
2N/A
2N/A memcpy(concat, string->data, string->length);
2N/A if (salt)
2N/A memcpy(concat+string->length, salt->data, salt->length);
2N/A
2N/A krb5_nfold(concatlen*8, concat, keybytes*8, foldstring);
2N/A
2N/A indata.length = keybytes;
2N/A indata.data = (char *) foldstring;
2N/A
2N/A /* Solaris Kerberos */
2N/A memset(&foldkey, 0, sizeof (krb5_keyblock));
2N/A foldkey.enctype = key->enctype;
2N/A foldkey.length = keylength;
2N/A foldkey.contents = foldkeydata;
2N/A
2N/A /* Solaris Kerberos */
2N/A (*(enc->make_key))(context, &indata, &foldkey);
2N/A
2N/A /* now derive the key from this one */
2N/A
2N/A indata.length = kerberos_len;
2N/A indata.data = (char *) kerberos;
2N/A /* Solaris Kerberos */
2N/A if ((ret = krb5_derive_key(context, enc, &foldkey, key, &indata)))
2N/A (void) memset(key->contents, 0, key->length);
2N/A
2N/A /* ret is set correctly by the prior call */
2N/A
2N/A memset(concat, 0, concatlen);
2N/A memset(foldstring, 0, keybytes);
2N/A memset(foldkeydata, 0, keylength);
2N/A
2N/A free(foldkeydata);
2N/A free(foldstring);
2N/A free(concat);
2N/A
2N/A return(ret);
2N/A}