Blame src/lib/crypto/krb/key.c

Packit fd8b60
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
Packit fd8b60
/*
Packit fd8b60
 * Copyright (C) 2009 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
 * Functions for manipulating krb5_key structures
Packit fd8b60
 */
Packit fd8b60
Packit fd8b60
#include "crypto_int.h"
Packit fd8b60
Packit fd8b60
/*
Packit fd8b60
 * The krb5_key data type wraps an exposed keyblock in an opaque data
Packit fd8b60
 * structure, to allow for internal optimizations such as caching of
Packit fd8b60
 * derived keys.
Packit fd8b60
 */
Packit fd8b60
Packit fd8b60
/* Create a krb5_key from the enctype and key data in a keyblock. */
Packit fd8b60
krb5_error_code KRB5_CALLCONV
Packit fd8b60
krb5_k_create_key(krb5_context context, const krb5_keyblock *key_data,
Packit fd8b60
                  krb5_key *out)
Packit fd8b60
{
Packit fd8b60
    krb5_key key = NULL;
Packit fd8b60
    krb5_error_code code;
Packit fd8b60
Packit fd8b60
    *out = NULL;
Packit fd8b60
Packit fd8b60
    key = malloc(sizeof(*key));
Packit fd8b60
    if (key == NULL)
Packit fd8b60
        return ENOMEM;
Packit fd8b60
    code = krb5int_c_copy_keyblock_contents(context, key_data, &key->keyblock);
Packit fd8b60
    if (code)
Packit fd8b60
        goto cleanup;
Packit fd8b60
Packit fd8b60
    key->refcount = 1;
Packit fd8b60
    key->derived = NULL;
Packit fd8b60
    key->cache = NULL;
Packit fd8b60
    *out = key;
Packit fd8b60
    return 0;
Packit fd8b60
Packit fd8b60
cleanup:
Packit fd8b60
    free(key);
Packit fd8b60
    return code;
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
void KRB5_CALLCONV
Packit fd8b60
krb5_k_reference_key(krb5_context context, krb5_key key)
Packit fd8b60
{
Packit fd8b60
    if (key)
Packit fd8b60
        key->refcount++;
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
/* Free the memory used by a krb5_key. */
Packit fd8b60
void KRB5_CALLCONV
Packit fd8b60
krb5_k_free_key(krb5_context context, krb5_key key)
Packit fd8b60
{
Packit fd8b60
    struct derived_key *dk;
Packit fd8b60
    const struct krb5_keytypes *ktp;
Packit fd8b60
Packit fd8b60
    if (key == NULL || --key->refcount > 0)
Packit fd8b60
        return;
Packit fd8b60
Packit fd8b60
    /* Free the derived key cache. */
Packit fd8b60
    while ((dk = key->derived) != NULL) {
Packit fd8b60
        key->derived = dk->next;
Packit fd8b60
        free(dk->constant.data);
Packit fd8b60
        krb5_k_free_key(context, dk->dkey);
Packit fd8b60
        free(dk);
Packit fd8b60
    }
Packit fd8b60
    krb5int_c_free_keyblock_contents(context, &key->keyblock);
Packit fd8b60
    if (key->cache) {
Packit fd8b60
        ktp = find_enctype(key->keyblock.enctype);
Packit fd8b60
        if (ktp && ktp->enc->key_cleanup)
Packit fd8b60
            ktp->enc->key_cleanup(key);
Packit fd8b60
    }
Packit fd8b60
    free(key);
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
/* Retrieve a copy of the keyblock from a krb5_key. */
Packit fd8b60
krb5_error_code KRB5_CALLCONV
Packit fd8b60
krb5_k_key_keyblock(krb5_context context, krb5_key key,
Packit fd8b60
                    krb5_keyblock **key_data)
Packit fd8b60
{
Packit fd8b60
    return krb5int_c_copy_keyblock(context, &key->keyblock, key_data);
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
/* Retrieve the enctype of a krb5_key. */
Packit fd8b60
krb5_enctype KRB5_CALLCONV
Packit fd8b60
krb5_k_key_enctype(krb5_context context, krb5_key key)
Packit fd8b60
{
Packit fd8b60
    return key->keyblock.enctype;
Packit fd8b60
}