Blame src/lib/kdb/encrypt_key.c

Packit fd8b60
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
Packit fd8b60
/* lib/kdb/encrypt_key.c */
Packit fd8b60
/*
Packit fd8b60
 * Copyright 1990,1991 by the Massachusetts Institute of Technology.
Packit fd8b60
 * All Rights Reserved.
Packit fd8b60
 *
Packit fd8b60
 * Export of this software from the United States of America may
Packit fd8b60
 *   require a specific license from the United States Government.
Packit fd8b60
 *   It is the responsibility of any person or organization contemplating
Packit fd8b60
 *   export to obtain such a license before exporting.
Packit fd8b60
 *
Packit fd8b60
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
Packit fd8b60
 * distribute this software and its documentation for any purpose and
Packit fd8b60
 * without fee is hereby granted, provided that the above copyright
Packit fd8b60
 * notice appear in all copies and that both that copyright notice and
Packit fd8b60
 * this permission notice appear in supporting documentation, and that
Packit fd8b60
 * the name of M.I.T. not be used in advertising or publicity pertaining
Packit fd8b60
 * to distribution of the software without specific, written prior
Packit fd8b60
 * permission.  Furthermore if you modify this software you must label
Packit fd8b60
 * your software as modified software and not distribute it in such a
Packit fd8b60
 * fashion that it might be confused with the original M.I.T. software.
Packit fd8b60
 * M.I.T. makes no representations about the suitability of
Packit fd8b60
 * this software for any purpose.  It is provided "as is" without express
Packit fd8b60
 * or implied warranty.
Packit fd8b60
 */
Packit fd8b60
/*
Packit fd8b60
 * Copyright (C) 1998 by the FundsXpress, INC.
Packit fd8b60
 *
Packit fd8b60
 * All rights reserved.
Packit fd8b60
 *
Packit fd8b60
 * Export of this software from the United States of America may require
Packit fd8b60
 * a specific license from the United States Government.  It is the
Packit fd8b60
 * responsibility of any person or organization contemplating export to
Packit fd8b60
 * obtain such a license before exporting.
Packit fd8b60
 *
Packit fd8b60
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
Packit fd8b60
 * distribute this software and its documentation for any purpose and
Packit fd8b60
 * without fee is hereby granted, provided that the above copyright
Packit fd8b60
 * notice appear in all copies and that both that copyright notice and
Packit fd8b60
 * this permission notice appear in supporting documentation, and that
Packit fd8b60
 * the name of FundsXpress. not be used in advertising or publicity pertaining
Packit fd8b60
 * to distribution of the software without specific, written prior
Packit fd8b60
 * permission.  FundsXpress makes no representations about the suitability of
Packit fd8b60
 * this software for any purpose.  It is provided "as is" without express
Packit fd8b60
 * or implied warranty.
Packit fd8b60
 *
Packit fd8b60
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
Packit fd8b60
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
Packit fd8b60
 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
Packit fd8b60
 */
Packit fd8b60
Packit fd8b60
#include "k5-int.h"
Packit fd8b60
#include "kdb.h"
Packit fd8b60
Packit fd8b60
/*
Packit fd8b60
 * Encrypt a key for storage in the database.  "eblock" is used
Packit fd8b60
 * to encrypt the key in "in" into "out"; the storage pointed to by "out"
Packit fd8b60
 * is allocated before use.
Packit fd8b60
 */
Packit fd8b60
Packit fd8b60
krb5_error_code
Packit fd8b60
krb5_dbe_def_encrypt_key_data( krb5_context             context,
Packit fd8b60
                               const krb5_keyblock    * mkey,
Packit fd8b60
                               const krb5_keyblock    * dbkey,
Packit fd8b60
                               const krb5_keysalt     * keysalt,
Packit fd8b60
                               int                      keyver,
Packit fd8b60
                               krb5_key_data          * key_data)
Packit fd8b60
{
Packit fd8b60
    krb5_error_code               retval;
Packit fd8b60
    krb5_octet                  * ptr;
Packit fd8b60
    size_t                        len;
Packit fd8b60
    int                           i;
Packit fd8b60
    krb5_data                     plain;
Packit fd8b60
    krb5_enc_data                 cipher;
Packit fd8b60
Packit fd8b60
    for (i = 0; i < key_data->key_data_ver; i++) {
Packit fd8b60
        free(key_data->key_data_contents[i]);
Packit fd8b60
        key_data->key_data_contents[i] = NULL;
Packit fd8b60
    }
Packit fd8b60
Packit fd8b60
    key_data->key_data_ver = 1;
Packit fd8b60
    key_data->key_data_kvno = keyver;
Packit fd8b60
Packit fd8b60
    /*
Packit fd8b60
     * The First element of the type/length/contents
Packit fd8b60
     * fields is the key type/length/contents
Packit fd8b60
     */
Packit fd8b60
    if ((retval = krb5_c_encrypt_length(context, mkey->enctype, dbkey->length,
Packit fd8b60
                                        &len)))
Packit fd8b60
        return(retval);
Packit fd8b60
Packit fd8b60
    ptr = malloc(2 + len);
Packit fd8b60
    if (ptr == NULL)
Packit fd8b60
        return(ENOMEM);
Packit fd8b60
Packit fd8b60
    key_data->key_data_type[0] = dbkey->enctype;
Packit fd8b60
    key_data->key_data_length[0] = 2 + len;
Packit fd8b60
    key_data->key_data_contents[0] = ptr;
Packit fd8b60
Packit fd8b60
    krb5_kdb_encode_int16(dbkey->length, ptr);
Packit fd8b60
    ptr += 2;
Packit fd8b60
Packit fd8b60
    plain.length = dbkey->length;
Packit fd8b60
    plain.data = (char *) dbkey->contents;
Packit fd8b60
Packit fd8b60
    cipher.ciphertext.length = len;
Packit fd8b60
    cipher.ciphertext.data = (char *) ptr;
Packit fd8b60
Packit fd8b60
    if ((retval = krb5_c_encrypt(context, mkey, /* XXX */ 0, 0,
Packit fd8b60
                                 &plain, &cipher))) {
Packit fd8b60
        free(key_data->key_data_contents[0]);
Packit fd8b60
        return retval;
Packit fd8b60
    }
Packit fd8b60
Packit fd8b60
    /* After key comes the salt in necessary */
Packit fd8b60
    if (keysalt) {
Packit fd8b60
        if (keysalt->type > 0) {
Packit fd8b60
            key_data->key_data_ver++;
Packit fd8b60
            key_data->key_data_type[1] = keysalt->type;
Packit fd8b60
            if ((key_data->key_data_length[1] = keysalt->data.length) != 0) {
Packit fd8b60
                key_data->key_data_contents[1] = malloc(keysalt->data.length);
Packit fd8b60
                if (key_data->key_data_contents[1] == NULL) {
Packit fd8b60
                    free(key_data->key_data_contents[0]);
Packit fd8b60
                    return ENOMEM;
Packit fd8b60
                }
Packit fd8b60
                memcpy(key_data->key_data_contents[1], keysalt->data.data,
Packit fd8b60
                       (size_t) keysalt->data.length);
Packit fd8b60
            }
Packit fd8b60
        }
Packit fd8b60
    }
Packit fd8b60
Packit fd8b60
    return retval;
Packit fd8b60
}