1 /* key.c --- Key related functions.
2 * Copyright (C) 2002, 2003, 2004, 2006, 2007 Simon Josefsson
4 * This file is part of Shishi.
6 * Shishi is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * Shishi is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Shishi; if not, see http://www.gnu.org/licenses or write
18 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
19 * Floor, Boston, MA 02110-1301, USA
31 char value
[MAX_KEY_LEN
];
32 uint32_t kvno
; /* UINT32_MAX means undefined kvno */
36 * shishi_key_principal:
37 * @key: structure that holds key information
39 * Get the principal part of the key owner principal name, i.e.,
42 * Return value: Returns the principal owning the key. (Not a copy of
43 * it, so don't modify or deallocate it.)
46 shishi_key_principal (Shishi_key
* key
)
48 return key
->principal
;
52 * shishi_key_principal_set:
53 * @key: structure that holds key information
54 * @principal: string with new principal name.
56 * Set the principal owning the key. The string is copied into the
57 * key, so you can dispose of the variable immediately after calling
61 shishi_key_principal_set (Shishi_key
* key
, const char *principal
)
64 free (key
->principal
);
66 key
->principal
= xstrdup (principal
);
68 key
->principal
= NULL
;
73 * @key: structure that holds key information
75 * Get the realm part of the key owner principal name.
77 * Return value: Returns the realm for the principal owning the key.
78 * (Not a copy of it, so don't modify or deallocate it.)
81 shishi_key_realm (Shishi_key
* key
)
87 * shishi_key_realm_set:
88 * @key: structure that holds key information
89 * @realm: string with new realm name.
91 * Set the realm for the principal owning the key. The string is
92 * copied into the key, so you can dispose of the variable immediately
93 * after calling this function.
96 shishi_key_realm_set (Shishi_key
* key
, const char *realm
)
101 key
->realm
= xstrdup (realm
);
108 * @key: structure that holds key information
112 * Return value: Returns the type of key as an integer as described in
116 shishi_key_type (Shishi_key
* key
)
122 * shishi_key_type_set:
123 * @key: structure that holds key information
124 * @type: type to set in key.
126 * Set the type of key in key structure.
129 shishi_key_type_set (Shishi_key
* key
, int32_t type
)
136 * @key: structure that holds key information
138 * Get the raw key bytes.
140 * Return value: Returns the key value as a pointer which is valid
141 * throughout the lifetime of the key structure.
144 shishi_key_value (Shishi_key
* key
)
150 * shishi_key_value_set:
151 * @key: structure that holds key information
152 * @value: input array with key data.
154 * Set the key value and length in key structure. The value is copied
155 * into the key (in other words, you can deallocate @value right after
156 * calling this function without modifying the value inside the key).
159 shishi_key_value_set (Shishi_key
* key
, const char *value
)
162 shishi_cipher_keylen (key
->type
) > 0 &&
163 shishi_cipher_keylen (key
->type
) <= MAX_KEY_LEN
)
164 memcpy (key
->value
, value
, shishi_cipher_keylen (key
->type
));
168 * shishi_key_version:
169 * @key: structure that holds key information
171 * Get the "kvno" (key version) of key. It will be UINT32_MAX if the
172 * key is not long-lived.
174 * Return value: Returns the version of key ("kvno").
177 shishi_key_version (Shishi_key
* key
)
183 * shishi_key_version_set:
184 * @key: structure that holds key information
185 * @kvno: new version integer.
187 * Set the version of key ("kvno") in key structure. Use UINT32_MAX
188 * for non-ptermanent keys.
191 shishi_key_version_set (Shishi_key
* key
, uint32_t kvno
)
198 * @key: structure that holds key information
200 * Calls shishi_cipher_name for key type.
202 * Return value: Return name of key.
205 shishi_key_name (Shishi_key
* key
)
207 return shishi_cipher_name (key
->type
);
212 * @key: structure that holds key information
214 * Calls shishi_cipher_keylen for key type.
216 * Return value: Returns the length of the key value.
219 shishi_key_length (Shishi_key
* key
)
221 return shishi_cipher_keylen (key
->type
);
226 * @handle: Shishi library handle create by shishi_init().
227 * @key: pointer to structure that will hold newly created key information
229 * Create a new Key information structure.
231 * Return value: Returns SHISHI_OK iff successful.
234 shishi_key (Shishi
* handle
, Shishi_key
** key
)
236 *key
= xcalloc (1, sizeof (**key
));
238 (*key
)->handle
= handle
;
239 (*key
)->kvno
= UINT32_MAX
;
246 * @key: pointer to structure that holds key information.
248 * Deallocates key information structure.
251 shishi_key_done (Shishi_key
* key
)
256 free (key
->principal
);
262 * @dstkey: structure that holds destination key information
263 * @srckey: structure that holds source key information
265 * Copies source key into existing allocated destination key.
268 shishi_key_copy (Shishi_key
* dstkey
, Shishi_key
* srckey
)
270 shishi_key_principal_set (dstkey
, shishi_key_principal (srckey
));
271 shishi_key_realm_set (dstkey
, shishi_key_realm (srckey
));
272 shishi_key_type_set (dstkey
, shishi_key_type (srckey
));
273 shishi_key_value_set (dstkey
, shishi_key_value (srckey
));
274 shishi_key_version_set (dstkey
, shishi_key_version (srckey
));
278 * shishi_key_from_value:
279 * @handle: Shishi library handle create by shishi_init().
280 * @type: type of key.
281 * @value: input array with key value, or NULL.
282 * @key: pointer to structure that will hold newly created key information
284 * Create a new Key information structure, and set the key type and
285 * key value. KEY contains a newly allocated structure only if this
286 * function is successful.
288 * Return value: Returns SHISHI_OK iff successful.
291 shishi_key_from_value (Shishi
* handle
,
292 int32_t type
, const char *value
, Shishi_key
** key
)
296 rc
= shishi_key (handle
, key
);
300 shishi_key_type_set (*key
, type
);
302 shishi_key_value_set (*key
, value
);
308 * shishi_key_from_base64:
309 * @handle: Shishi library handle create by shishi_init().
310 * @type: type of key.
311 * @value: input string with base64 encoded key value, or NULL.
312 * @key: pointer to structure that will hold newly created key information
314 * Create a new Key information structure, and set the key type and
315 * key value. KEY contains a newly allocated structure only if this
316 * function is successful.
318 * Return value: Returns SHISHI_INVALID_KEY if the base64 encoded key
319 * length doesn't match the key type, and SHISHI_OK on
323 shishi_key_from_base64 (Shishi
* handle
,
324 int32_t type
, const char *value
, Shishi_key
** key
)
328 rc
= shishi_key (handle
, key
);
332 shishi_key_type_set (*key
, type
);
336 size_t len
= MAX_KEY_LEN
;
338 if (!base64_decode (value
, strlen (value
), (*key
)->value
, &len
))
340 shishi_key_done (*key
);
341 return SHISHI_BASE64_ERROR
;
344 if (len
!= shishi_key_length (*key
))
346 shishi_key_done (*key
);
347 return SHISHI_INVALID_KEY
;
356 * @handle: Shishi library handle create by shishi_init().
357 * @type: type of key.
358 * @key: pointer to structure that will hold newly created key information
360 * Create a new Key information structure for the key type and some
361 * random data. KEY contains a newly allocated structure only if this
362 * function is successful.
364 * Return value: Returns SHISHI_OK iff successful.
367 shishi_key_random (Shishi
* handle
, int32_t type
, Shishi_key
** key
)
369 char buf
[MAX_RANDOM_LEN
];
370 int len
= shishi_cipher_randomlen (type
);
373 rc
= shishi_randomize (handle
, 1, buf
, len
);
377 rc
= shishi_key (handle
, key
);
381 rc
= shishi_random_to_key (handle
, type
, buf
, len
, *key
);
384 shishi_key_done (*key
);
392 * shishi_key_from_random
393 * @handle: Shishi library handle create by shishi_init().
394 * @type: type of key.
396 * @rndlen: length of random data.
397 * @outkey: pointer to structure that will hold newly created key information
399 * Create a new Key information structure, and set the key type and
400 * key value using shishi_random_to_key(). KEY contains a newly
401 * allocated structure only if this function is successful.
403 * Return value: Returns SHISHI_OK iff successful.
406 shishi_key_from_random (Shishi
* handle
,
408 const char *rnd
, size_t rndlen
, Shishi_key
** outkey
)
412 rc
= shishi_key (handle
, outkey
);
416 rc
= shishi_random_to_key (handle
, type
, rnd
, rndlen
, *outkey
);
422 * shishi_key_from_string
423 * @handle: Shishi library handle create by shishi_init().
424 * @type: type of key.
425 * @password: input array containing password.
426 * @passwordlen: length of input array containing password.
427 * @salt: input array containing salt.
428 * @saltlen: length of input array containing salt.
429 * @parameter: input array with opaque encryption type specific information.
430 * @outkey: pointer to structure that will hold newly created key information
432 * Create a new Key information structure, and set the key type and
433 * key value using shishi_string_to_key(). KEY contains a newly
434 * allocated structure only if this function is successful.
436 * Return value: Returns SHISHI_OK iff successful.
439 shishi_key_from_string (Shishi
* handle
,
441 const char *password
, size_t passwordlen
,
442 const char *salt
, size_t saltlen
,
443 const char *parameter
, Shishi_key
** outkey
)
447 rc
= shishi_key (handle
, outkey
);
451 rc
= shishi_string_to_key (handle
, type
, password
, passwordlen
,
452 salt
, saltlen
, parameter
, *outkey
);
455 shishi_key_done (*outkey
);
463 * shishi_key_from_name:
464 * @handle: Shishi library handle create by shishi_init().
465 * @type: type of key.
466 * @name: principal name of user.
467 * @password: input array containing password.
468 * @passwordlen: length of input array containing password.
469 * @parameter: input array with opaque encryption type specific information.
470 * @outkey: pointer to structure that will hold newly created key information
472 * Create a new Key information structure, and derive the key from
473 * principal name and password using shishi_key_from_name(). The salt
474 * is derived from the principal name by concatenating the decoded
475 * realm and principal.
477 * Return value: Returns SHISHI_OK iff successful.
480 shishi_key_from_name (Shishi
* handle
,
483 const char *password
, size_t passwordlen
,
484 const char *parameter
, Shishi_key
** outkey
)
489 rc
= shishi_derive_default_salt (handle
, name
, &salt
);
493 rc
= shishi_key_from_string (handle
, type
, password
, passwordlen
,
494 salt
, strlen (salt
), parameter
, outkey
);
500 rc
= shishi_parse_name (handle
, name
, &principal
, &realm
);
503 shishi_key_principal_set (*outkey
, principal
);
504 shishi_key_realm_set (*outkey
, realm
);