1*d3273b5bSchristos /* $NetBSD: keyblock.c,v 1.2 2017/01/28 21:31:49 christos Exp $ */
2ca1c9b0cSelric
3ca1c9b0cSelric /*
4ca1c9b0cSelric * Copyright (c) 1997 - 2001 Kungliga Tekniska Högskolan
5ca1c9b0cSelric * (Royal Institute of Technology, Stockholm, Sweden).
6ca1c9b0cSelric * All rights reserved.
7ca1c9b0cSelric *
8ca1c9b0cSelric * Redistribution and use in source and binary forms, with or without
9ca1c9b0cSelric * modification, are permitted provided that the following conditions
10ca1c9b0cSelric * are met:
11ca1c9b0cSelric *
12ca1c9b0cSelric * 1. Redistributions of source code must retain the above copyright
13ca1c9b0cSelric * notice, this list of conditions and the following disclaimer.
14ca1c9b0cSelric *
15ca1c9b0cSelric * 2. Redistributions in binary form must reproduce the above copyright
16ca1c9b0cSelric * notice, this list of conditions and the following disclaimer in the
17ca1c9b0cSelric * documentation and/or other materials provided with the distribution.
18ca1c9b0cSelric *
19ca1c9b0cSelric * 3. Neither the name of the Institute nor the names of its contributors
20ca1c9b0cSelric * may be used to endorse or promote products derived from this software
21ca1c9b0cSelric * without specific prior written permission.
22ca1c9b0cSelric *
23ca1c9b0cSelric * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ca1c9b0cSelric * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ca1c9b0cSelric * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ca1c9b0cSelric * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ca1c9b0cSelric * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ca1c9b0cSelric * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ca1c9b0cSelric * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ca1c9b0cSelric * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ca1c9b0cSelric * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ca1c9b0cSelric * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ca1c9b0cSelric * SUCH DAMAGE.
34ca1c9b0cSelric */
35ca1c9b0cSelric
36ca1c9b0cSelric #include "krb5_locl.h"
37ca1c9b0cSelric
38ca1c9b0cSelric /**
39ca1c9b0cSelric * Zero out a keyblock
40ca1c9b0cSelric *
41ca1c9b0cSelric * @param keyblock keyblock to zero out
42ca1c9b0cSelric *
43ca1c9b0cSelric * @ingroup krb5_crypto
44ca1c9b0cSelric */
45ca1c9b0cSelric
46ca1c9b0cSelric KRB5_LIB_FUNCTION void KRB5_LIB_CALL
krb5_keyblock_zero(krb5_keyblock * keyblock)47ca1c9b0cSelric krb5_keyblock_zero(krb5_keyblock *keyblock)
48ca1c9b0cSelric {
49ca1c9b0cSelric keyblock->keytype = 0;
50ca1c9b0cSelric krb5_data_zero(&keyblock->keyvalue);
51ca1c9b0cSelric }
52ca1c9b0cSelric
53ca1c9b0cSelric /**
54ca1c9b0cSelric * Free a keyblock's content, also zero out the content of the keyblock.
55ca1c9b0cSelric *
56ca1c9b0cSelric * @param context a Kerberos 5 context
57ca1c9b0cSelric * @param keyblock keyblock content to free, NULL is valid argument
58ca1c9b0cSelric *
59ca1c9b0cSelric * @ingroup krb5_crypto
60ca1c9b0cSelric */
61ca1c9b0cSelric
62ca1c9b0cSelric KRB5_LIB_FUNCTION void KRB5_LIB_CALL
krb5_free_keyblock_contents(krb5_context context,krb5_keyblock * keyblock)63ca1c9b0cSelric krb5_free_keyblock_contents(krb5_context context,
64ca1c9b0cSelric krb5_keyblock *keyblock)
65ca1c9b0cSelric {
66ca1c9b0cSelric if(keyblock) {
67ca1c9b0cSelric if (keyblock->keyvalue.data != NULL)
68ca1c9b0cSelric memset(keyblock->keyvalue.data, 0, keyblock->keyvalue.length);
69ca1c9b0cSelric krb5_data_free (&keyblock->keyvalue);
70b9d004c6Schristos keyblock->keytype = KRB5_ENCTYPE_NULL;
71ca1c9b0cSelric }
72ca1c9b0cSelric }
73ca1c9b0cSelric
74ca1c9b0cSelric /**
75ca1c9b0cSelric * Free a keyblock, also zero out the content of the keyblock, uses
76ca1c9b0cSelric * krb5_free_keyblock_contents() to free the content.
77ca1c9b0cSelric *
78ca1c9b0cSelric * @param context a Kerberos 5 context
79ca1c9b0cSelric * @param keyblock keyblock to free, NULL is valid argument
80ca1c9b0cSelric *
81ca1c9b0cSelric * @ingroup krb5_crypto
82ca1c9b0cSelric */
83ca1c9b0cSelric
84ca1c9b0cSelric KRB5_LIB_FUNCTION void KRB5_LIB_CALL
krb5_free_keyblock(krb5_context context,krb5_keyblock * keyblock)85ca1c9b0cSelric krb5_free_keyblock(krb5_context context,
86ca1c9b0cSelric krb5_keyblock *keyblock)
87ca1c9b0cSelric {
88ca1c9b0cSelric if(keyblock){
89ca1c9b0cSelric krb5_free_keyblock_contents(context, keyblock);
90ca1c9b0cSelric free(keyblock);
91ca1c9b0cSelric }
92ca1c9b0cSelric }
93ca1c9b0cSelric
94ca1c9b0cSelric /**
95ca1c9b0cSelric * Copy a keyblock, free the output keyblock with
96ca1c9b0cSelric * krb5_free_keyblock_contents().
97ca1c9b0cSelric *
98ca1c9b0cSelric * @param context a Kerberos 5 context
99ca1c9b0cSelric * @param inblock the key to copy
100ca1c9b0cSelric * @param to the output key.
101ca1c9b0cSelric *
102ca1c9b0cSelric * @return 0 on success or a Kerberos 5 error code
103ca1c9b0cSelric *
104ca1c9b0cSelric * @ingroup krb5_crypto
105ca1c9b0cSelric */
106ca1c9b0cSelric
107ca1c9b0cSelric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_copy_keyblock_contents(krb5_context context,const krb5_keyblock * inblock,krb5_keyblock * to)108ca1c9b0cSelric krb5_copy_keyblock_contents (krb5_context context,
109ca1c9b0cSelric const krb5_keyblock *inblock,
110ca1c9b0cSelric krb5_keyblock *to)
111ca1c9b0cSelric {
112ca1c9b0cSelric return copy_EncryptionKey(inblock, to);
113ca1c9b0cSelric }
114ca1c9b0cSelric
115ca1c9b0cSelric /**
116ca1c9b0cSelric * Copy a keyblock, free the output keyblock with
117ca1c9b0cSelric * krb5_free_keyblock().
118ca1c9b0cSelric *
119ca1c9b0cSelric * @param context a Kerberos 5 context
120ca1c9b0cSelric * @param inblock the key to copy
121ca1c9b0cSelric * @param to the output key.
122ca1c9b0cSelric *
123ca1c9b0cSelric * @return 0 on success or a Kerberos 5 error code
124ca1c9b0cSelric *
125ca1c9b0cSelric * @ingroup krb5_crypto
126ca1c9b0cSelric */
127ca1c9b0cSelric
128ca1c9b0cSelric
129ca1c9b0cSelric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_copy_keyblock(krb5_context context,const krb5_keyblock * inblock,krb5_keyblock ** to)130ca1c9b0cSelric krb5_copy_keyblock (krb5_context context,
131ca1c9b0cSelric const krb5_keyblock *inblock,
132ca1c9b0cSelric krb5_keyblock **to)
133ca1c9b0cSelric {
134ca1c9b0cSelric krb5_error_code ret;
135ca1c9b0cSelric krb5_keyblock *k;
136ca1c9b0cSelric
137ca1c9b0cSelric *to = NULL;
138ca1c9b0cSelric
139ca1c9b0cSelric k = calloc (1, sizeof(*k));
140b9d004c6Schristos if (k == NULL)
141b9d004c6Schristos return krb5_enomem(context);
142ca1c9b0cSelric
143ca1c9b0cSelric ret = krb5_copy_keyblock_contents (context, inblock, k);
144ca1c9b0cSelric if (ret) {
145ca1c9b0cSelric free(k);
146ca1c9b0cSelric return ret;
147ca1c9b0cSelric }
148ca1c9b0cSelric *to = k;
149ca1c9b0cSelric return 0;
150ca1c9b0cSelric }
151ca1c9b0cSelric
152ca1c9b0cSelric /**
153ca1c9b0cSelric * Get encryption type of a keyblock.
154ca1c9b0cSelric *
155ca1c9b0cSelric * @ingroup krb5_crypto
156ca1c9b0cSelric */
157ca1c9b0cSelric
158ca1c9b0cSelric KRB5_LIB_FUNCTION krb5_enctype KRB5_LIB_CALL
krb5_keyblock_get_enctype(const krb5_keyblock * block)159ca1c9b0cSelric krb5_keyblock_get_enctype(const krb5_keyblock *block)
160ca1c9b0cSelric {
161ca1c9b0cSelric return block->keytype;
162ca1c9b0cSelric }
163ca1c9b0cSelric
164ca1c9b0cSelric /**
165ca1c9b0cSelric * Fill in `key' with key data of type `enctype' from `data' of length
166ca1c9b0cSelric * `size'. Key should be freed using krb5_free_keyblock_contents().
167ca1c9b0cSelric *
168ca1c9b0cSelric * @return 0 on success or a Kerberos 5 error code
169ca1c9b0cSelric *
170ca1c9b0cSelric * @ingroup krb5_crypto
171ca1c9b0cSelric */
172ca1c9b0cSelric
173ca1c9b0cSelric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_keyblock_init(krb5_context context,krb5_enctype type,const void * data,size_t size,krb5_keyblock * key)174ca1c9b0cSelric krb5_keyblock_init(krb5_context context,
175ca1c9b0cSelric krb5_enctype type,
176ca1c9b0cSelric const void *data,
177ca1c9b0cSelric size_t size,
178ca1c9b0cSelric krb5_keyblock *key)
179ca1c9b0cSelric {
180ca1c9b0cSelric krb5_error_code ret;
181ca1c9b0cSelric size_t len;
182ca1c9b0cSelric
183ca1c9b0cSelric memset(key, 0, sizeof(*key));
184ca1c9b0cSelric
185ca1c9b0cSelric ret = krb5_enctype_keysize(context, type, &len);
186ca1c9b0cSelric if (ret)
187ca1c9b0cSelric return ret;
188ca1c9b0cSelric
189ca1c9b0cSelric if (len != size) {
190ca1c9b0cSelric krb5_set_error_message(context, KRB5_PROG_ETYPE_NOSUPP,
191ca1c9b0cSelric "Encryption key %d is %lu bytes "
192ca1c9b0cSelric "long, %lu was passed in",
193ca1c9b0cSelric type, (unsigned long)len, (unsigned long)size);
194ca1c9b0cSelric return KRB5_PROG_ETYPE_NOSUPP;
195ca1c9b0cSelric }
196ca1c9b0cSelric ret = krb5_data_copy(&key->keyvalue, data, len);
197ca1c9b0cSelric if(ret) {
198ca1c9b0cSelric krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
199ca1c9b0cSelric return ret;
200ca1c9b0cSelric }
201ca1c9b0cSelric key->keytype = type;
202ca1c9b0cSelric
203ca1c9b0cSelric return 0;
204ca1c9b0cSelric }
205