xref: /freebsd/crypto/krb5/src/lib/crypto/krb/key.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  * Copyright (C) 2009 by the Massachusetts Institute of Technology.
4  * All rights reserved.
5  *
6  * Export of this software from the United States of America may
7  *   require a specific license from the United States Government.
8  *   It is the responsibility of any person or organization contemplating
9  *   export to obtain such a license before exporting.
10  *
11  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
12  * distribute this software and its documentation for any purpose and
13  * without fee is hereby granted, provided that the above copyright
14  * notice appear in all copies and that both that copyright notice and
15  * this permission notice appear in supporting documentation, and that
16  * the name of M.I.T. not be used in advertising or publicity pertaining
17  * to distribution of the software without specific, written prior
18  * permission.  Furthermore if you modify this software you must label
19  * your software as modified software and not distribute it in such a
20  * fashion that it might be confused with the original M.I.T. software.
21  * M.I.T. makes no representations about the suitability of
22  * this software for any purpose.  It is provided "as is" without express
23  * or implied warranty.
24  *
25  * Functions for manipulating krb5_key structures
26  */
27 
28 #include "crypto_int.h"
29 
30 /*
31  * The krb5_key data type wraps an exposed keyblock in an opaque data
32  * structure, to allow for internal optimizations such as caching of
33  * derived keys.
34  */
35 
36 /* Create a krb5_key from the enctype and key data in a keyblock. */
37 krb5_error_code KRB5_CALLCONV
krb5_k_create_key(krb5_context context,const krb5_keyblock * key_data,krb5_key * out)38 krb5_k_create_key(krb5_context context, const krb5_keyblock *key_data,
39                   krb5_key *out)
40 {
41     krb5_key key = NULL;
42     krb5_error_code code;
43 
44     *out = NULL;
45 
46     key = malloc(sizeof(*key));
47     if (key == NULL)
48         return ENOMEM;
49     code = krb5int_c_copy_keyblock_contents(context, key_data, &key->keyblock);
50     if (code)
51         goto cleanup;
52 
53     key->refcount = 1;
54     key->derived = NULL;
55     key->cache = NULL;
56     *out = key;
57     return 0;
58 
59 cleanup:
60     free(key);
61     return code;
62 }
63 
64 void KRB5_CALLCONV
krb5_k_reference_key(krb5_context context,krb5_key key)65 krb5_k_reference_key(krb5_context context, krb5_key key)
66 {
67     if (key)
68         key->refcount++;
69 }
70 
71 /* Free the memory used by a krb5_key. */
72 void KRB5_CALLCONV
krb5_k_free_key(krb5_context context,krb5_key key)73 krb5_k_free_key(krb5_context context, krb5_key key)
74 {
75     struct derived_key *dk;
76     const struct krb5_keytypes *ktp;
77 
78     if (key == NULL || --key->refcount > 0)
79         return;
80 
81     /* Free the derived key cache. */
82     while ((dk = key->derived) != NULL) {
83         key->derived = dk->next;
84         free(dk->constant.data);
85         krb5_k_free_key(context, dk->dkey);
86         free(dk);
87     }
88     krb5int_c_free_keyblock_contents(context, &key->keyblock);
89     if (key->cache) {
90         ktp = find_enctype(key->keyblock.enctype);
91         if (ktp && ktp->enc->key_cleanup)
92             ktp->enc->key_cleanup(key);
93     }
94     free(key);
95 }
96 
97 /* Retrieve a copy of the keyblock from a krb5_key. */
98 krb5_error_code KRB5_CALLCONV
krb5_k_key_keyblock(krb5_context context,krb5_key key,krb5_keyblock ** key_data)99 krb5_k_key_keyblock(krb5_context context, krb5_key key,
100                     krb5_keyblock **key_data)
101 {
102     return krb5int_c_copy_keyblock(context, &key->keyblock, key_data);
103 }
104 
105 /* Retrieve the enctype of a krb5_key. */
106 krb5_enctype KRB5_CALLCONV
krb5_k_key_enctype(krb5_context context,krb5_key key)107 krb5_k_key_enctype(krb5_context context, krb5_key key)
108 {
109     return key->keyblock.enctype;
110 }
111