1 /* key.c Key related functions.
2 * Copyright (C) 2002, 2003 Simon Josefsson
4 * This file is part of Shishi.
6 * Shishi is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Shishi is distributed in the hope that it will be useful,
12 * but 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, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 char value
[MAX_KEY_LEN
];
35 * shishi_key_principal:
36 * @key: structure that holds key information
38 * Return value: Returns the principal owning the key. (Not a copy of
39 * it, so don't modify or deallocate it.)
42 shishi_key_principal (Shishi_key
* key
)
44 return key
->principal
;
48 * shishi_key_principal_set:
49 * @key: structure that holds key information
50 * @principal: string with new principal name.
52 * Set the principal owning the key. The string is copied into the
53 * key, so you can dispose of the variable immediately after calling
57 shishi_key_principal_set (Shishi_key
* key
, const char *principal
)
60 free (key
->principal
);
62 key
->principal
= strdup (principal
);
64 key
->principal
= NULL
;
69 * @key: structure that holds key information
71 * Return value: Returns the realm for the principal owning the key.
72 * (Not a copy of it, so don't modify or deallocate it.)
75 shishi_key_realm (Shishi_key
* key
)
81 * shishi_key_realm_set:
82 * @key: structure that holds key information
83 * @realm: string with new realm name.
85 * Set the realm for the principal owning the key. The string is
86 * copied into the key, so you can dispose of the variable immediately
87 * after calling this function.
90 shishi_key_realm_set (Shishi_key
* key
, const char *realm
)
95 key
->realm
= strdup (realm
);
102 * @key: structure that holds key information
104 * Return value: Returns the type of key as an integer as described in
108 shishi_key_type (Shishi_key
* key
)
114 * shishi_key_type_set:
115 * @key: structure that holds key information
117 * Set the type of key in key structure.
120 shishi_key_type_set (Shishi_key
* key
, int32_t type
)
127 * @key: structure that holds key information
129 * Return value: Returns the key value as a pointer which is valid
130 * throughout the lifetime of the key structure.
133 shishi_key_value (Shishi_key
* key
)
139 * shishi_key_value_set:
140 * @key: structure that holds key information
141 * @value: input array with key data.
142 * @length: length of input array with key data.
144 * Set the key value and length in key structure.
147 shishi_key_value_set (Shishi_key
* key
, const char *value
)
150 shishi_cipher_keylen (key
->type
) > 0 &&
151 shishi_cipher_keylen (key
->type
) <= MAX_KEY_LEN
)
152 memcpy (key
->value
, value
, shishi_cipher_keylen (key
->type
));
156 * shishi_key_version:
157 * @key: structure that holds key information
159 * Return value: Returns the version of key ("kvno").
162 shishi_key_version (Shishi_key
* key
)
168 * shishi_key_version_set:
169 * @key: structure that holds key information
170 * @version: new version integer.
172 * Set the version of key ("kvno") in key structure.
175 shishi_key_version_set (Shishi_key
* key
, int version
)
177 key
->version
= version
;
182 * @key: structure that holds key information
184 * Calls shishi_cipher_name for key type.
186 * Return value: Return name of key.
189 shishi_key_name (Shishi_key
* key
)
191 return shishi_cipher_name (key
->type
);
196 * @key: structure that holds key information
198 * Calls shishi_cipher_keylen for key type.
200 * Return value: Returns the length of the key value.
203 shishi_key_length (Shishi_key
* key
)
205 return shishi_cipher_keylen (key
->type
);
210 * @handle: Shishi library handle create by shishi_init().
211 * @key: pointer to structure that will hold newly created key information
213 * Create a new Key information structure.
215 * Return value: Returns SHISHI_MALLOC_ERROR on memory allocation
216 * errors, and SHISHI_OK on success.
219 shishi_key (Shishi
* handle
, Shishi_key
** key
)
221 *key
= malloc (sizeof (**key
));
223 return SHISHI_MALLOC_ERROR
;
224 memset (*key
, 0, sizeof (**key
));
226 (*key
)->handle
= handle
;
233 * @key: pointer to structure that holds key information.
235 * Deallocates key information structure and set key handle to NULL.
238 shishi_key_done (Shishi_key
** key
)
246 * @dstkey: structure that holds destination key information
247 * @srckey: structure that holds source key information
249 * Copies source key into existing allocated destination key.
252 shishi_key_copy (Shishi_key
* dstkey
, Shishi_key
* srckey
)
254 shishi_key_principal_set (dstkey
, shishi_key_principal (srckey
));
255 shishi_key_realm_set (dstkey
, shishi_key_realm (srckey
));
256 shishi_key_type_set (dstkey
, shishi_key_type (srckey
));
257 shishi_key_value_set (dstkey
, shishi_key_value (srckey
));
258 shishi_key_version_set (dstkey
, shishi_key_version (srckey
));
262 * shishi_key_from_value:
263 * @handle: Shishi library handle create by shishi_init().
264 * @type: type of key.
265 * @value: input array with key value, or NULL.
266 * @key: pointer to structure that will hold newly created key information
268 * Create a new Key information structure, and set the key type and
269 * key value. KEY contains a newly allocated structure only if this
270 * function is successful.
272 * Return value: Returns SHISHI_MALLOC_ERROR on memory allocation
273 * errors, and SHISHI_OK on success.
276 shishi_key_from_value (Shishi
* handle
,
277 int32_t type
, char *value
, Shishi_key
** key
)
281 rc
= shishi_key (handle
, key
);
285 shishi_key_type_set (*key
, type
);
287 shishi_key_value_set (*key
, value
);
293 * shishi_key_from_base64:
294 * @handle: Shishi library handle create by shishi_init().
295 * @type: type of key.
296 * @value: input string with base64 encoded key value, or NULL.
297 * @key: pointer to structure that will hold newly created key information
299 * Create a new Key information structure, and set the key type and
300 * key value. KEY contains a newly allocated structure only if this
301 * function is successful.
303 * Return value: Returns SHISHI_MALLOC_ERROR on memory allocation
304 * errors, SHISHI_INVALID_KEY if the base64 encoded key
305 * length doesn't match the key type, and SHISHI_OK on
309 shishi_key_from_base64 (Shishi
* handle
,
310 int32_t type
, char *value
, Shishi_key
** key
)
314 rc
= shishi_key (handle
, key
);
318 shishi_key_type_set (*key
, type
);
325 buf
= malloc (strlen (value
) + 1);
327 return SHISHI_MALLOC_ERROR
;
329 len
= shishi_from_base64 (buf
, value
);
331 if (len
!= shishi_key_length (*key
))
334 return SHISHI_INVALID_KEY
;
337 shishi_key_value_set (*key
, buf
);
347 * @handle: Shishi library handle create by shishi_init().
348 * @type: type of key.
350 * Create a new Key information structure for the key type and some
351 * random data. KEY contains a newly allocated structure only if this
352 * function is successful.
354 * Return value: Returns SHISHI_OK iff successful.
357 shishi_key_random (Shishi
* handle
, int32_t type
, Shishi_key
** key
)
359 char buf
[MAX_RANDOM_LEN
];
360 int len
= shishi_cipher_randomlen (type
);
363 rc
= shishi_key (handle
, key
);
367 rc
= shishi_randomize (handle
, buf
, len
);
371 rc
= shishi_random_to_key (handle
, type
, buf
, len
, *key
);
379 * shishi_key_from_random
380 * @handle: Shishi library handle create by shishi_init().
381 * @type: type of key.
382 * @random: random data.
383 * @randomlen: length of random data.
385 * Create a new Key information structure, and set the key type and
386 * key value using shishi_random_to_key(). KEY contains a newly
387 * allocated structure only if this function is successful.
389 * Return value: Returns SHISHI_MALLOC_ERROR on memory allocation
390 * errors, and SHISHI_OK on success.
393 shishi_key_from_random (Shishi
* handle
,
395 char *random
, size_t randomlen
, Shishi_key
** key
)
399 rc
= shishi_key (handle
, key
);
403 rc
= shishi_random_to_key (handle
, type
, random
, randomlen
, *key
);
409 * shishi_key_from_string
410 * @handle: Shishi library handle create by shishi_init().
411 * @type: type of key.
412 * @password: input array containing password.
413 * @passwordlen: length of input array containing password.
414 * @salt: input array containing salt.
415 * @saltlen: length of input array containing salt.
416 * @parameter: input array with opaque encryption type specific information.
418 * Create a new Key information structure, and set the key type and
419 * key value using shishi_string_to_key(). KEY contains a newly
420 * allocated structure only if this function is successful.
422 * Return value: Returns SHISHI_MALLOC_ERROR on memory allocation
423 * errors, and SHISHI_OK on success.
426 shishi_key_from_string (Shishi
* handle
,
428 const char *password
, size_t passwordlen
,
429 const char *salt
, size_t saltlen
,
430 const char *parameter
, Shishi_key
** key
)
434 rc
= shishi_key (handle
, key
);
438 rc
= shishi_string_to_key (handle
, type
, password
, passwordlen
,
439 salt
, saltlen
, parameter
, *key
);