1*82bba4e9Sandvar /* $NetBSD: dst_api.c,v 1.11 2024/02/05 21:46:05 andvar Exp $ */
2ccd87bacSchristos
3ccd87bacSchristos /*
4ccd87bacSchristos * Portions Copyright (c) 1995-1998 by Trusted Information Systems, Inc.
5ccd87bacSchristos *
6ccd87bacSchristos * Permission to use, copy modify, and distribute this software for any
7ccd87bacSchristos * purpose with or without fee is hereby granted, provided that the above
8ccd87bacSchristos * copyright notice and this permission notice appear in all copies.
9ccd87bacSchristos *
10ccd87bacSchristos * THE SOFTWARE IS PROVIDED "AS IS" AND TRUSTED INFORMATION SYSTEMS
11ccd87bacSchristos * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
12ccd87bacSchristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
13ccd87bacSchristos * TRUSTED INFORMATION SYSTEMS BE LIABLE FOR ANY SPECIAL, DIRECT,
14ccd87bacSchristos * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
15ccd87bacSchristos * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
16ccd87bacSchristos * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
17ccd87bacSchristos * WITH THE USE OR PERFORMANCE OF THE SOFTWARE.
18ccd87bacSchristos */
19ccd87bacSchristos /*
20ccd87bacSchristos * This file contains the interface between the DST API and the crypto API.
21ccd87bacSchristos * This is the only file that needs to be changed if the crypto system is
22ccd87bacSchristos * changed. Exported functions are:
23ccd87bacSchristos * void dst_init() Initialize the toolkit
24daa7d68eSandvar * int dst_check_algorithm() Function to determines if alg is supported.
25ccd87bacSchristos * int dst_compare_keys() Function to compare two keys for equality.
26ccd87bacSchristos * int dst_sign_data() Incremental signing routine.
27ccd87bacSchristos * int dst_verify_data() Incremental verify routine.
28ccd87bacSchristos * int dst_generate_key() Function to generate new KEY
29ccd87bacSchristos * DST_KEY *dst_read_key() Function to retrieve private/public KEY.
30ccd87bacSchristos * void dst_write_key() Function to write out a key.
31ccd87bacSchristos * DST_KEY *dst_dnskey_to_key() Function to convert DNS KEY RR to a DST
32ccd87bacSchristos * KEY structure.
33ccd87bacSchristos * int dst_key_to_dnskey() Function to return a public key in DNS
34ccd87bacSchristos * format binary
35ccd87bacSchristos * DST_KEY *dst_buffer_to_key() Converst a data in buffer to KEY
36ccd87bacSchristos * int *dst_key_to_buffer() Writes out DST_KEY key matterial in buffer
37ccd87bacSchristos * void dst_free_key() Releases all memory referenced by key structure
38ccd87bacSchristos */
39ccd87bacSchristos #include <sys/cdefs.h>
40ccd87bacSchristos #if 0
41ccd87bacSchristos static const char rcsid[] = "Header: /proj/cvs/prod/libbind/dst/dst_api.c,v 1.17 2007/09/24 17:18:25 each Exp ";
42ccd87bacSchristos #else
43*82bba4e9Sandvar __RCSID("$NetBSD: dst_api.c,v 1.11 2024/02/05 21:46:05 andvar Exp $");
44ccd87bacSchristos #endif
45ccd87bacSchristos
46ccd87bacSchristos
47ccd87bacSchristos #include "port_before.h"
48ccd87bacSchristos #include <stdio.h>
49ccd87bacSchristos #include <errno.h>
50ccd87bacSchristos #include <fcntl.h>
51ccd87bacSchristos #include <stdlib.h>
52ccd87bacSchristos #include <unistd.h>
53ccd87bacSchristos #include <string.h>
54ccd87bacSchristos #include <memory.h>
55ccd87bacSchristos #include <ctype.h>
56ccd87bacSchristos #include <time.h>
57ccd87bacSchristos #include <sys/param.h>
58ccd87bacSchristos #include <sys/stat.h>
59ccd87bacSchristos #include <sys/socket.h>
60ccd87bacSchristos #include <netinet/in.h>
61ccd87bacSchristos #include <arpa/nameser.h>
62ccd87bacSchristos #include <resolv.h>
63ccd87bacSchristos
64ccd87bacSchristos #include "dst_internal.h"
65ccd87bacSchristos #include "port_after.h"
66ccd87bacSchristos
67ccd87bacSchristos /* static variables */
68ccd87bacSchristos static int done_init = 0;
69ccd87bacSchristos dst_func *dst_t_func[DST_MAX_ALGS];
70ccd87bacSchristos const char *dst_path = "";
71ccd87bacSchristos
72ccd87bacSchristos /* internal I/O functions */
73ccd87bacSchristos static DST_KEY *dst_s_read_public_key(const char *in_name,
74ccd87bacSchristos const u_int16_t in_id, int in_alg);
75ccd87bacSchristos static int dst_s_read_private_key_file(char *name, DST_KEY *pk_key,
76ccd87bacSchristos u_int16_t in_id, int in_alg);
77ccd87bacSchristos static int dst_s_write_public_key(const DST_KEY *key);
78ccd87bacSchristos static int dst_s_write_private_key(const DST_KEY *key);
79ccd87bacSchristos
80ccd87bacSchristos /* internal function to set up data structure */
81ccd87bacSchristos static DST_KEY *dst_s_get_key_struct(const char *name, const int alg,
82ccd87bacSchristos const int flags, const int protocol,
83ccd87bacSchristos const int bits);
84ccd87bacSchristos
85ccd87bacSchristos /*%
86ccd87bacSchristos * dst_init
87ccd87bacSchristos * This function initializes the Digital Signature Toolkit.
88ccd87bacSchristos * Right now, it just checks the DSTKEYPATH environment variable.
89ccd87bacSchristos * Parameters
90ccd87bacSchristos * none
91ccd87bacSchristos * Returns
92ccd87bacSchristos * none
93ccd87bacSchristos */
94ccd87bacSchristos void
dst_init(void)95ccd87bacSchristos dst_init(void)
96ccd87bacSchristos {
97ccd87bacSchristos char *s;
98ccd87bacSchristos size_t len;
99ccd87bacSchristos
100ccd87bacSchristos if (done_init != 0)
101ccd87bacSchristos return;
102ccd87bacSchristos done_init = 1;
103ccd87bacSchristos
104ccd87bacSchristos s = getenv("DSTKEYPATH");
105ccd87bacSchristos len = 0;
106ccd87bacSchristos if (s) {
107ccd87bacSchristos struct stat statbuf;
108ccd87bacSchristos
109ccd87bacSchristos len = strlen(s);
110ccd87bacSchristos if (len > PATH_MAX) {
111ccd87bacSchristos EREPORT(("%s: %s is longer than %d characters,"
112ccd87bacSchristos " ignoring\n", __func__, s, PATH_MAX));
113ccd87bacSchristos } else if (stat(s, &statbuf) != 0 ||
114ccd87bacSchristos !S_ISDIR(statbuf.st_mode)) {
115ccd87bacSchristos EREPORT(("%s: %s is not a valid directory\n",
116ccd87bacSchristos __func__, s));
117ccd87bacSchristos } else {
118ccd87bacSchristos char *tmp;
119ccd87bacSchristos tmp = (char *) malloc(len + 2);
120ccd87bacSchristos memcpy(tmp, s, len + 1);
121ccd87bacSchristos if (tmp[strlen(tmp) - 1] != '/') {
122ccd87bacSchristos tmp[strlen(tmp) + 1] = 0;
123ccd87bacSchristos tmp[strlen(tmp)] = '/';
124ccd87bacSchristos }
125ccd87bacSchristos dst_path = tmp;
126ccd87bacSchristos }
127ccd87bacSchristos }
128ccd87bacSchristos memset(dst_t_func, 0, sizeof(dst_t_func));
129ccd87bacSchristos /* first one is selected */
130ccd87bacSchristos dst_hmac_md5_init();
131ccd87bacSchristos }
132ccd87bacSchristos
133ccd87bacSchristos /*%
134ccd87bacSchristos * dst_check_algorithm
135ccd87bacSchristos * This function determines if the crypto system for the specified
136ccd87bacSchristos * algorithm is present.
137ccd87bacSchristos * Parameters
138ccd87bacSchristos * alg 1 KEY_RSA
139ccd87bacSchristos * 3 KEY_DSA
140ccd87bacSchristos * 157 KEY_HMAC_MD5
141ccd87bacSchristos * future algorithms TBD and registered with IANA.
142ccd87bacSchristos * Returns
143ccd87bacSchristos * 1 - The algorithm is available.
144ccd87bacSchristos * 0 - The algorithm is not available.
145ccd87bacSchristos */
146ccd87bacSchristos int
dst_check_algorithm(const int alg)147ccd87bacSchristos dst_check_algorithm(const int alg)
148ccd87bacSchristos {
149ccd87bacSchristos return (dst_t_func[alg] != NULL);
150ccd87bacSchristos }
151ccd87bacSchristos
152ccd87bacSchristos /*%
153ccd87bacSchristos * dst_s_get_key_struct
154ccd87bacSchristos * This function allocates key structure and fills in some of the
155ccd87bacSchristos * fields of the structure.
156ccd87bacSchristos * Parameters:
157ccd87bacSchristos * name: the name of the key
158ccd87bacSchristos * alg: the algorithm number
159ccd87bacSchristos * flags: the dns flags of the key
160ccd87bacSchristos * protocol: the dns protocol of the key
161ccd87bacSchristos * bits: the size of the key
162ccd87bacSchristos * Returns:
163ccd87bacSchristos * NULL if error
164ccd87bacSchristos * valid pointer otherwise
165ccd87bacSchristos */
166ccd87bacSchristos static DST_KEY *
dst_s_get_key_struct(const char * name,const int alg,const int flags,const int protocol,const int bits)167ccd87bacSchristos dst_s_get_key_struct(const char *name, const int alg, const int flags,
168ccd87bacSchristos const int protocol, const int bits)
169ccd87bacSchristos {
170ccd87bacSchristos DST_KEY *new_key = NULL;
171ccd87bacSchristos
172ccd87bacSchristos if (dst_check_algorithm(alg)) /*%< make sure alg is available */
173ccd87bacSchristos new_key = (DST_KEY *) malloc(sizeof(*new_key));
174ccd87bacSchristos if (new_key == NULL)
175ccd87bacSchristos return (NULL);
176ccd87bacSchristos
177ccd87bacSchristos memset(new_key, 0, sizeof(*new_key));
178ccd87bacSchristos new_key->dk_key_name = strdup(name);
179ccd87bacSchristos if (new_key->dk_key_name == NULL) {
180ccd87bacSchristos free(new_key);
181ccd87bacSchristos return (NULL);
182ccd87bacSchristos }
183ccd87bacSchristos new_key->dk_alg = alg;
184ccd87bacSchristos new_key->dk_flags = flags;
185ccd87bacSchristos new_key->dk_proto = protocol;
186ccd87bacSchristos new_key->dk_KEY_struct = NULL;
187ccd87bacSchristos new_key->dk_key_size = bits;
188ccd87bacSchristos new_key->dk_func = dst_t_func[alg];
189ccd87bacSchristos return (new_key);
190ccd87bacSchristos }
191ccd87bacSchristos
192ccd87bacSchristos /*%
193ccd87bacSchristos * dst_compare_keys
194ccd87bacSchristos * Compares two keys for equality.
195ccd87bacSchristos * Parameters
196ccd87bacSchristos * key1, key2 Two keys to be compared.
197ccd87bacSchristos * Returns
198ccd87bacSchristos * 0 The keys are equal.
199ccd87bacSchristos * non-zero The keys are not equal.
200ccd87bacSchristos */
201ccd87bacSchristos
202ccd87bacSchristos int
dst_compare_keys(const DST_KEY * key1,const DST_KEY * key2)203ccd87bacSchristos dst_compare_keys(const DST_KEY *key1, const DST_KEY *key2)
204ccd87bacSchristos {
205ccd87bacSchristos if (key1 == key2)
206ccd87bacSchristos return (0);
207ccd87bacSchristos if (key1 == NULL || key2 == NULL)
208ccd87bacSchristos return (4);
209ccd87bacSchristos if (key1->dk_alg != key2->dk_alg)
210ccd87bacSchristos return (1);
211ccd87bacSchristos if (key1->dk_key_size != key2->dk_key_size)
212ccd87bacSchristos return (2);
213ccd87bacSchristos if (key1->dk_id != key2->dk_id)
214ccd87bacSchristos return (3);
215ccd87bacSchristos return (key1->dk_func->compare(key1, key2));
216ccd87bacSchristos }
217ccd87bacSchristos
218ccd87bacSchristos /*%
219ccd87bacSchristos * dst_sign_data
220ccd87bacSchristos * An incremental signing function. Data is signed in steps.
221ccd87bacSchristos * First the context must be initialized (SIG_MODE_INIT).
222ccd87bacSchristos * Then data is hashed (SIG_MODE_UPDATE). Finally the signature
223ccd87bacSchristos * itself is created (SIG_MODE_FINAL). This function can be called
224ccd87bacSchristos * once with INIT, UPDATE and FINAL modes all set, or it can be
225ccd87bacSchristos * called separately with a different mode set for each step. The
226ccd87bacSchristos * UPDATE step can be repeated.
227ccd87bacSchristos * Parameters
228ccd87bacSchristos * mode A bit mask used to specify operation(s) to be performed.
229ccd87bacSchristos * SIG_MODE_INIT 1 Initialize digest
230ccd87bacSchristos * SIG_MODE_UPDATE 2 Add data to digest
231ccd87bacSchristos * SIG_MODE_FINAL 4 Generate signature
232ccd87bacSchristos * from signature
233ccd87bacSchristos * SIG_MODE_ALL (SIG_MODE_INIT,SIG_MODE_UPDATE,SIG_MODE_FINAL
234ccd87bacSchristos * data Data to be signed.
235ccd87bacSchristos * len The length in bytes of data to be signed.
236ccd87bacSchristos * in_key Contains a private key to sign with.
237ccd87bacSchristos * KEY structures should be handled (created, converted,
238ccd87bacSchristos * compared, stored, freed) by the DST.
239ccd87bacSchristos * signature
240ccd87bacSchristos * The location to which the signature will be written.
241ccd87bacSchristos * sig_len Length of the signature field in bytes.
242ccd87bacSchristos * Return
243b5860281Sandvar * 0 Successful INIT or Update operation
244ccd87bacSchristos * >0 success FINAL (sign) operation
245ccd87bacSchristos * <0 failure
246ccd87bacSchristos */
247ccd87bacSchristos
248ccd87bacSchristos int
dst_sign_data(const int mode,DST_KEY * in_key,void ** context,const u_char * data,const int len,u_char * signature,const int sig_len)249ccd87bacSchristos dst_sign_data(const int mode, DST_KEY *in_key, void **context,
250ccd87bacSchristos const u_char *data, const int len,
251ccd87bacSchristos u_char *signature, const int sig_len)
252ccd87bacSchristos {
253ccd87bacSchristos DUMP(data, mode, len, "dst_sign_data()");
254ccd87bacSchristos
255ccd87bacSchristos if (mode & SIG_MODE_FINAL &&
256ccd87bacSchristos (in_key->dk_KEY_struct == NULL || signature == NULL))
257ccd87bacSchristos return (MISSING_KEY_OR_SIGNATURE);
258ccd87bacSchristos
259ccd87bacSchristos if (in_key->dk_func && in_key->dk_func->sign)
260ccd87bacSchristos return (in_key->dk_func->sign(mode, in_key, context, data, len,
261ccd87bacSchristos signature, sig_len));
262ccd87bacSchristos return (UNKNOWN_KEYALG);
263ccd87bacSchristos }
264ccd87bacSchristos
265ccd87bacSchristos /*%
266ccd87bacSchristos * dst_verify_data
267ccd87bacSchristos * An incremental verify function. Data is verified in steps.
268ccd87bacSchristos * First the context must be initialized (SIG_MODE_INIT).
269ccd87bacSchristos * Then data is hashed (SIG_MODE_UPDATE). Finally the signature
270ccd87bacSchristos * is verified (SIG_MODE_FINAL). This function can be called
271ccd87bacSchristos * once with INIT, UPDATE and FINAL modes all set, or it can be
272ccd87bacSchristos * called separately with a different mode set for each step. The
273ccd87bacSchristos * UPDATE step can be repeated.
274ccd87bacSchristos * Parameters
275ccd87bacSchristos * mode Operations to perform this time.
276ccd87bacSchristos * SIG_MODE_INIT 1 Initialize digest
277ccd87bacSchristos * SIG_MODE_UPDATE 2 add data to digest
278ccd87bacSchristos * SIG_MODE_FINAL 4 verify signature
279ccd87bacSchristos * SIG_MODE_ALL
280ccd87bacSchristos * (SIG_MODE_INIT,SIG_MODE_UPDATE,SIG_MODE_FINAL)
281ccd87bacSchristos * data Data to pass through the hash function.
282ccd87bacSchristos * len Length of the data in bytes.
283ccd87bacSchristos * in_key Key for verification.
284ccd87bacSchristos * signature Location of signature.
285ccd87bacSchristos * sig_len Length of the signature in bytes.
286ccd87bacSchristos * Returns
287ccd87bacSchristos * 0 Verify success
288ccd87bacSchristos * Non-Zero Verify Failure
289ccd87bacSchristos */
290ccd87bacSchristos
291ccd87bacSchristos int
dst_verify_data(const int mode,DST_KEY * in_key,void ** context,const u_char * data,const int len,const u_char * signature,const int sig_len)292ccd87bacSchristos dst_verify_data(const int mode, DST_KEY *in_key, void **context,
293ccd87bacSchristos const u_char *data, const int len,
294ccd87bacSchristos const u_char *signature, const int sig_len)
295ccd87bacSchristos {
296ccd87bacSchristos DUMP(data, mode, len, "dst_verify_data()");
297ccd87bacSchristos if (mode & SIG_MODE_FINAL &&
298ccd87bacSchristos (in_key->dk_KEY_struct == NULL || signature == NULL))
299ccd87bacSchristos return (MISSING_KEY_OR_SIGNATURE);
300ccd87bacSchristos
301ccd87bacSchristos if (in_key->dk_func == NULL || in_key->dk_func->verify == NULL)
302ccd87bacSchristos return (UNSUPPORTED_KEYALG);
303ccd87bacSchristos return (in_key->dk_func->verify(mode, in_key, context, data, len,
304ccd87bacSchristos signature, sig_len));
305ccd87bacSchristos }
306ccd87bacSchristos
307ccd87bacSchristos /*%
308ccd87bacSchristos * dst_read_private_key
309ccd87bacSchristos * Access a private key. First the list of private keys that have
310ccd87bacSchristos * already been read in is searched, then the key accessed on disk.
311ccd87bacSchristos * If the private key can be found, it is returned. If the key cannot
312ccd87bacSchristos * be found, a null pointer is returned. The options specify required
313ccd87bacSchristos * key characteristics. If the private key requested does not have
314ccd87bacSchristos * these characteristics, it will not be read.
315ccd87bacSchristos * Parameters
316ccd87bacSchristos * in_keyname The private key name.
317ccd87bacSchristos * in_id The id of the private key.
318ccd87bacSchristos * options DST_FORCE_READ Read from disk - don't use a previously
319ccd87bacSchristos * read key.
320ccd87bacSchristos * DST_CAN_SIGN The key must be useable for signing.
321ccd87bacSchristos * DST_NO_AUTHEN The key must be useable for authentication.
322ccd87bacSchristos * DST_STANDARD Return any key
323ccd87bacSchristos * Returns
324ccd87bacSchristos * NULL If there is no key found in the current directory or
325ccd87bacSchristos * this key has not been loaded before.
326ccd87bacSchristos * !NULL Success - KEY structure returned.
327ccd87bacSchristos */
328ccd87bacSchristos
329ccd87bacSchristos DST_KEY *
dst_read_key(const char * in_keyname,const u_int16_t in_id,const int in_alg,const int type)330ccd87bacSchristos dst_read_key(const char *in_keyname, const u_int16_t in_id,
331ccd87bacSchristos const int in_alg, const int type)
332ccd87bacSchristos {
333ccd87bacSchristos char keyname[PATH_MAX];
334ccd87bacSchristos DST_KEY *dg_key = NULL, *pubkey = NULL;
335ccd87bacSchristos
336ccd87bacSchristos if (!dst_check_algorithm(in_alg)) { /*%< make sure alg is available */
337daa7d68eSandvar EREPORT(("%s: Algorithm %d not supported\n", __func__, in_alg));
338ccd87bacSchristos return (NULL);
339ccd87bacSchristos }
340ccd87bacSchristos if ((type & (DST_PUBLIC | DST_PRIVATE)) == 0)
341ccd87bacSchristos return (NULL);
342ccd87bacSchristos if (in_keyname == NULL) {
343ccd87bacSchristos EREPORT(("%s: Null key name passed in\n", __func__));
344ccd87bacSchristos return (NULL);
345ccd87bacSchristos } else if (strlen(in_keyname) >= sizeof(keyname)) {
346ccd87bacSchristos EREPORT(("%s: keyname too big\n", __func__));
347ccd87bacSchristos return (NULL);
348ccd87bacSchristos } else
349ccd87bacSchristos strcpy(keyname, in_keyname);
350ccd87bacSchristos
351ccd87bacSchristos /* before I read in the public key, check if it is allowed to sign */
352ccd87bacSchristos if ((pubkey = dst_s_read_public_key(keyname, in_id, in_alg)) == NULL)
353ccd87bacSchristos return (NULL);
354ccd87bacSchristos
355ccd87bacSchristos if (type == DST_PUBLIC)
356ccd87bacSchristos return pubkey;
357ccd87bacSchristos
358ccd87bacSchristos if (!(dg_key = dst_s_get_key_struct(keyname, pubkey->dk_alg,
359ccd87bacSchristos (int)pubkey->dk_flags,
360ccd87bacSchristos pubkey->dk_proto, 0)))
361ccd87bacSchristos return (dg_key);
362ccd87bacSchristos /* Fill in private key and some fields in the general key structure */
363ccd87bacSchristos if (dst_s_read_private_key_file(keyname, dg_key, pubkey->dk_id,
364ccd87bacSchristos pubkey->dk_alg) == 0)
365ccd87bacSchristos dg_key = dst_free_key(dg_key);
366ccd87bacSchristos
367ccd87bacSchristos (void)dst_free_key(pubkey);
368ccd87bacSchristos return (dg_key);
369ccd87bacSchristos }
370ccd87bacSchristos
371ccd87bacSchristos int
dst_write_key(const DST_KEY * key,const int type)372ccd87bacSchristos dst_write_key(const DST_KEY *key, const int type)
373ccd87bacSchristos {
374ccd87bacSchristos int pub = 0, priv = 0;
375ccd87bacSchristos
376ccd87bacSchristos if (key == NULL)
377ccd87bacSchristos return (0);
378ccd87bacSchristos if (!dst_check_algorithm(key->dk_alg)) { /*%< make sure alg is available */
379daa7d68eSandvar EREPORT(("%s: Algorithm %d not supported\n", __func__,
380ccd87bacSchristos key->dk_alg));
381ccd87bacSchristos return (UNSUPPORTED_KEYALG);
382ccd87bacSchristos }
383ccd87bacSchristos if ((type & (DST_PRIVATE|DST_PUBLIC)) == 0)
384ccd87bacSchristos return (0);
385ccd87bacSchristos
386ccd87bacSchristos if (type & DST_PUBLIC)
387ccd87bacSchristos if ((pub = dst_s_write_public_key(key)) < 0)
388ccd87bacSchristos return (pub);
389ccd87bacSchristos if (type & DST_PRIVATE)
390ccd87bacSchristos if ((priv = dst_s_write_private_key(key)) < 0)
391ccd87bacSchristos return (priv);
392ccd87bacSchristos return (priv+pub);
393ccd87bacSchristos }
394ccd87bacSchristos
395ccd87bacSchristos /*%
396ccd87bacSchristos * dst_write_private_key
397ccd87bacSchristos * Write a private key to disk. The filename will be of the form:
398ccd87bacSchristos * K<key->dk_name>+<key->dk_alg+><key-d>k_id.><private key suffix>.
399ccd87bacSchristos * If there is already a file with this name, an error is returned.
400ccd87bacSchristos *
401ccd87bacSchristos * Parameters
402ccd87bacSchristos * key A DST managed key structure that contains
403ccd87bacSchristos * all information needed about a key.
404ccd87bacSchristos * Return
405ccd87bacSchristos * >= 0 Correct behavior. Returns length of encoded key value
406ccd87bacSchristos * written to disk.
407ccd87bacSchristos * < 0 error.
408ccd87bacSchristos */
409ccd87bacSchristos
410ccd87bacSchristos static int
dst_s_write_private_key(const DST_KEY * key)411ccd87bacSchristos dst_s_write_private_key(const DST_KEY *key)
412ccd87bacSchristos {
413ccd87bacSchristos u_char encoded_block[RAW_KEY_SIZE];
414ccd87bacSchristos char file[PATH_MAX];
415ccd87bacSchristos int len;
416ccd87bacSchristos FILE *fp;
417ccd87bacSchristos
418ccd87bacSchristos /* First encode the key into the portable key format */
419ccd87bacSchristos if (key == NULL)
420ccd87bacSchristos return (-1);
421ccd87bacSchristos if (key->dk_KEY_struct == NULL)
422ccd87bacSchristos return (0); /*%< null key has no private key */
423ccd87bacSchristos if (key->dk_func == NULL || key->dk_func->to_file_fmt == NULL) {
424ccd87bacSchristos EREPORT(("%s: Unsupported operation %d\n", __func__,
425ccd87bacSchristos key->dk_alg));
426ccd87bacSchristos return (-5);
427ccd87bacSchristos } else if ((len = key->dk_func->to_file_fmt(key, (char *)encoded_block,
428ccd87bacSchristos (int)sizeof(encoded_block))) <= 0) {
429ccd87bacSchristos EREPORT(("%s: Failed encoding private RSA bsafe key %d\n",
430ccd87bacSchristos __func__, len));
431ccd87bacSchristos return (-8);
432ccd87bacSchristos }
433ccd87bacSchristos /* Now I can create the file I want to use */
434ccd87bacSchristos dst_s_build_filename(file, key->dk_key_name, key->dk_id, key->dk_alg,
435ccd87bacSchristos PRIVATE_KEY, PATH_MAX);
436ccd87bacSchristos
437ccd87bacSchristos /* Do not overwrite an existing file */
438ccd87bacSchristos if ((fp = dst_s_fopen(file, "w", 0600)) != NULL) {
439ccd87bacSchristos ssize_t nn;
440678bca00Schristos nn = fwrite(encoded_block, 1, len, fp);
441678bca00Schristos if (nn != len) {
442ccd87bacSchristos EREPORT(("%s: Write failure on %s %d != %zd"
443ccd87bacSchristos " errno=%d\n", __func__, file, len, nn, errno));
444678bca00Schristos
445ccd87bacSchristos fclose(fp);
446ccd87bacSchristos return (-5);
447ccd87bacSchristos }
448ccd87bacSchristos fclose(fp);
449ccd87bacSchristos } else {
450ccd87bacSchristos EREPORT(("%s: Can not create file %s\n", __func__,
451ccd87bacSchristos file));
452ccd87bacSchristos return (-6);
453ccd87bacSchristos }
454ccd87bacSchristos memset(encoded_block, 0, len);
455ccd87bacSchristos return (len);
456ccd87bacSchristos }
457ccd87bacSchristos
458ccd87bacSchristos /*%
459ccd87bacSchristos *
460ccd87bacSchristos * dst_read_public_key
461ccd87bacSchristos * Read a public key from disk and store in a DST key structure.
462ccd87bacSchristos * Parameters
463ccd87bacSchristos * in_name K<in_name><in_id>.<public key suffix> is the
464ccd87bacSchristos * filename of the key file to be read.
465ccd87bacSchristos * Returns
466ccd87bacSchristos * NULL If the key does not exist or no name is supplied.
467ccd87bacSchristos * NON-NULL Initialized key structure if the key exists.
468ccd87bacSchristos */
469ccd87bacSchristos
470ccd87bacSchristos static DST_KEY *
dst_s_read_public_key(const char * in_name,const u_int16_t in_id,int in_alg)471ccd87bacSchristos dst_s_read_public_key(const char *in_name, const u_int16_t in_id, int in_alg)
472ccd87bacSchristos {
473ccd87bacSchristos int flags, proto, alg, dlen;
474ccd87bacSchristos size_t len;
475ccd87bacSchristos int c;
476ccd87bacSchristos char name[PATH_MAX], enckey[RAW_KEY_SIZE], *notspace;
477ccd87bacSchristos u_char deckey[RAW_KEY_SIZE];
478ccd87bacSchristos FILE *fp;
479ccd87bacSchristos
480ccd87bacSchristos if (in_name == NULL) {
481ccd87bacSchristos EREPORT(("%s: No key name given\n", __func__));
482ccd87bacSchristos return (NULL);
483ccd87bacSchristos }
484ccd87bacSchristos if (dst_s_build_filename(name, in_name, in_id, in_alg, PUBLIC_KEY,
485ccd87bacSchristos PATH_MAX) == -1) {
486ccd87bacSchristos EREPORT(("%s: Cannot make filename from %s, %d, and %s\n",
487ccd87bacSchristos __func__, in_name, in_id, PUBLIC_KEY));
488ccd87bacSchristos return (NULL);
489ccd87bacSchristos }
490ccd87bacSchristos /*
491ccd87bacSchristos * Open the file and read it's formatted contents up to key
492ccd87bacSchristos * File format:
493ccd87bacSchristos * domain.name [ttl] [IN] KEY <flags> <protocol> <algorithm> <key>
494ccd87bacSchristos * flags, proto, alg stored as decimal (or hex numbers FIXME).
495ccd87bacSchristos * (FIXME: handle parentheses for line continuation.)
496ccd87bacSchristos */
497ccd87bacSchristos if ((fp = dst_s_fopen(name, "r", 0)) == NULL) {
498ccd87bacSchristos EREPORT(("%s: Public Key not found %s\n", __func__, name));
499ccd87bacSchristos return (NULL);
500ccd87bacSchristos }
501ccd87bacSchristos /* Skip domain name, which ends at first blank */
502ccd87bacSchristos while ((c = getc(fp)) != EOF)
503ccd87bacSchristos if (isspace(c))
504ccd87bacSchristos break;
505ccd87bacSchristos /* Skip blank to get to next field */
506ccd87bacSchristos while ((c = getc(fp)) != EOF)
507ccd87bacSchristos if (!isspace(c))
508ccd87bacSchristos break;
509ccd87bacSchristos
510ccd87bacSchristos /* Skip optional TTL -- if initial digit, skip whole word. */
511ccd87bacSchristos if (isdigit(c)) {
512ccd87bacSchristos while ((c = getc(fp)) != EOF)
513ccd87bacSchristos if (isspace(c))
514ccd87bacSchristos break;
515ccd87bacSchristos while ((c = getc(fp)) != EOF)
516ccd87bacSchristos if (!isspace(c))
517ccd87bacSchristos break;
518ccd87bacSchristos }
519ccd87bacSchristos /* Skip optional "IN" */
520ccd87bacSchristos if (c == 'I' || c == 'i') {
521ccd87bacSchristos while ((c = getc(fp)) != EOF)
522ccd87bacSchristos if (isspace(c))
523ccd87bacSchristos break;
524ccd87bacSchristos while ((c = getc(fp)) != EOF)
525ccd87bacSchristos if (!isspace(c))
526ccd87bacSchristos break;
527ccd87bacSchristos }
528ccd87bacSchristos /* Locate and skip "KEY" */
529ccd87bacSchristos if (c != 'K' && c != 'k') {
530ccd87bacSchristos EREPORT(("%s: \"KEY\" doesn't appear in file: %s", __func__,
531ccd87bacSchristos name));
532ccd87bacSchristos return NULL;
533ccd87bacSchristos }
534ccd87bacSchristos while ((c = getc(fp)) != EOF)
535ccd87bacSchristos if (isspace(c))
536ccd87bacSchristos break;
537ccd87bacSchristos while ((c = getc(fp)) != EOF)
538ccd87bacSchristos if (!isspace(c))
539ccd87bacSchristos break;
5409f4a9600Sandvar ungetc(c, fp); /*%< return the character to the input field */
541ccd87bacSchristos /* Handle hex!! FIXME. */
542ccd87bacSchristos
543ccd87bacSchristos if (fscanf(fp, "%d %d %d", &flags, &proto, &alg) != 3) {
544ccd87bacSchristos EREPORT(("%s: Can not read flag/proto/alg field from %s\n",
545ccd87bacSchristos __func__, name));
546ccd87bacSchristos return (NULL);
547ccd87bacSchristos }
548ccd87bacSchristos /* read in the key string */
549ccd87bacSchristos fgets(enckey, (int)sizeof(enckey), fp);
550ccd87bacSchristos
551ccd87bacSchristos /* If we aren't at end-of-file, something is wrong. */
552ccd87bacSchristos while ((c = getc(fp)) != EOF)
553ccd87bacSchristos if (!isspace(c))
554ccd87bacSchristos break;
555ccd87bacSchristos if (!feof(fp)) {
556ccd87bacSchristos EREPORT(("%s: Key too long in file: %s", __func__, name));
557ccd87bacSchristos return NULL;
558ccd87bacSchristos }
559ccd87bacSchristos fclose(fp);
560ccd87bacSchristos
561ccd87bacSchristos if ((len = strlen(enckey)) == 0)
562ccd87bacSchristos return (NULL);
563ccd87bacSchristos
564ccd87bacSchristos /* discard \n */
565ccd87bacSchristos enckey[--len] = '\0';
566ccd87bacSchristos
567ccd87bacSchristos /* remove leading spaces */
568ccd87bacSchristos for (notspace = (char *) enckey; isspace((*notspace)&0xff); len--)
569ccd87bacSchristos notspace++;
570ccd87bacSchristos
571ccd87bacSchristos dlen = b64_pton(notspace, deckey, sizeof(deckey));
572ccd87bacSchristos if (dlen < 0) {
573ccd87bacSchristos EREPORT(("%s: bad return from b64_pton = %d", __func__, dlen));
574ccd87bacSchristos return (NULL);
575ccd87bacSchristos }
576ccd87bacSchristos /* store key and info in a key structure that is returned */
577ccd87bacSchristos /* return dst_store_public_key(in_name, alg, proto, 666, flags, deckey,
578ccd87bacSchristos dlen);*/
579ccd87bacSchristos return dst_buffer_to_key(in_name, alg, flags, proto, deckey, dlen);
580ccd87bacSchristos }
581ccd87bacSchristos
582ccd87bacSchristos /*%
583ccd87bacSchristos * dst_write_public_key
584ccd87bacSchristos * Write a key to disk in DNS format.
585ccd87bacSchristos * Parameters
586ccd87bacSchristos * key Pointer to a DST key structure.
587ccd87bacSchristos * Returns
588ccd87bacSchristos * 0 Failure
589ccd87bacSchristos * 1 Success
590ccd87bacSchristos */
591ccd87bacSchristos
592ccd87bacSchristos static int
dst_s_write_public_key(const DST_KEY * key)593ccd87bacSchristos dst_s_write_public_key(const DST_KEY *key)
594ccd87bacSchristos {
595ccd87bacSchristos FILE *fp;
596ccd87bacSchristos char filename[PATH_MAX];
597ccd87bacSchristos u_char out_key[RAW_KEY_SIZE];
598ccd87bacSchristos char enc_key[RAW_KEY_SIZE];
599ccd87bacSchristos int len = 0;
600ccd87bacSchristos int mode;
601ccd87bacSchristos
602ccd87bacSchristos memset(out_key, 0, sizeof(out_key));
603ccd87bacSchristos if (key == NULL) {
604ccd87bacSchristos EREPORT(("%s: No key specified \n", __func__));
605ccd87bacSchristos return (0);
606ccd87bacSchristos } else if ((len = dst_key_to_dnskey(key, out_key,
607ccd87bacSchristos (int)sizeof(out_key)))< 0)
608ccd87bacSchristos return (0);
609ccd87bacSchristos
610ccd87bacSchristos /* Make the filename */
611ccd87bacSchristos if (dst_s_build_filename(filename, key->dk_key_name, key->dk_id,
612ccd87bacSchristos key->dk_alg, PUBLIC_KEY, PATH_MAX) == -1) {
613ccd87bacSchristos EREPORT(("%s: Cannot make filename from %s, %d, and %s\n",
614ccd87bacSchristos __func__, key->dk_key_name, key->dk_id, PUBLIC_KEY));
615ccd87bacSchristos return (0);
616ccd87bacSchristos }
617ccd87bacSchristos /* XXX in general this should be a check for symmetric keys */
618ccd87bacSchristos mode = (key->dk_alg == KEY_HMAC_MD5) ? 0600 : 0644;
619ccd87bacSchristos /* create public key file */
620ccd87bacSchristos if ((fp = dst_s_fopen(filename, "w+", mode)) == NULL) {
621ccd87bacSchristos EREPORT(("%s: open of file:%s failed (errno=%d)\n",
622ccd87bacSchristos __func__, filename, errno));
623ccd87bacSchristos return (0);
624ccd87bacSchristos }
625ccd87bacSchristos /*write out key first base64 the key data */
626ccd87bacSchristos if (key->dk_flags & DST_EXTEND_FLAG)
627ccd87bacSchristos b64_ntop(&out_key[6], len - 6, enc_key, sizeof(enc_key));
628ccd87bacSchristos else
629ccd87bacSchristos b64_ntop(&out_key[4], len - 4, enc_key, sizeof(enc_key));
630ccd87bacSchristos fprintf(fp, "%s IN KEY %d %d %d %s\n",
631ccd87bacSchristos key->dk_key_name,
632ccd87bacSchristos key->dk_flags, key->dk_proto, key->dk_alg, enc_key);
633ccd87bacSchristos fclose(fp);
634ccd87bacSchristos return (1);
635ccd87bacSchristos }
636ccd87bacSchristos
637ccd87bacSchristos /*%
638ccd87bacSchristos * dst_dnskey_to_public_key
639ccd87bacSchristos * This function converts the contents of a DNS KEY RR into a DST
640ccd87bacSchristos * key structure.
64137469493Sandvar * Parameters
642ccd87bacSchristos * len Length of the RDATA of the KEY RR RDATA
64350d90726Sandvar * rdata A pointer to the KEY RR RDATA.
644ccd87bacSchristos * in_name Key name to be stored in key structure.
645ccd87bacSchristos * Returns
646ccd87bacSchristos * NULL Failure
647ccd87bacSchristos * NON-NULL Success. Pointer to key structure.
648ccd87bacSchristos * Caller's responsibility to free() it.
649ccd87bacSchristos */
650ccd87bacSchristos
651ccd87bacSchristos DST_KEY *
dst_dnskey_to_key(const char * in_name,const u_char * rdata,const int len)652ccd87bacSchristos dst_dnskey_to_key(const char *in_name, const u_char *rdata, const int len)
653ccd87bacSchristos {
654ccd87bacSchristos DST_KEY *key_st;
655ccd87bacSchristos int alg ;
656ccd87bacSchristos int start = DST_KEY_START;
657ccd87bacSchristos
658ccd87bacSchristos if (rdata == NULL || len <= DST_KEY_ALG) /*%< no data */
659ccd87bacSchristos return (NULL);
660ccd87bacSchristos alg = (u_int8_t) rdata[DST_KEY_ALG];
661ccd87bacSchristos if (!dst_check_algorithm(alg)) { /*%< make sure alg is available */
662daa7d68eSandvar EREPORT(("%s: Algorithm %d not supported\n", __func__,
663ccd87bacSchristos alg));
664ccd87bacSchristos return (NULL);
665ccd87bacSchristos }
666ccd87bacSchristos
667ccd87bacSchristos if (in_name == NULL)
668ccd87bacSchristos return (NULL);
669ccd87bacSchristos
670ccd87bacSchristos if ((key_st = dst_s_get_key_struct(in_name, alg, 0, 0, 0)) == NULL)
671ccd87bacSchristos return (NULL);
672ccd87bacSchristos
673ccd87bacSchristos key_st->dk_id = dst_s_dns_key_id(rdata, len);
674ccd87bacSchristos key_st->dk_flags = dst_s_get_int16(rdata);
675ccd87bacSchristos key_st->dk_proto = (u_int16_t) rdata[DST_KEY_PROT];
676ccd87bacSchristos if (key_st->dk_flags & DST_EXTEND_FLAG) {
677ccd87bacSchristos u_int32_t ext_flags;
678ccd87bacSchristos ext_flags = (u_int32_t) dst_s_get_int16(&rdata[DST_EXT_FLAG]);
679ccd87bacSchristos key_st->dk_flags = key_st->dk_flags | (ext_flags << 16);
680ccd87bacSchristos start += 2;
681ccd87bacSchristos }
682ccd87bacSchristos /*
683492c086fSandvar * now point to the beginning of the data representing the encoding
684ccd87bacSchristos * of the key
685ccd87bacSchristos */
686ccd87bacSchristos if (key_st->dk_func && key_st->dk_func->from_dns_key) {
687ccd87bacSchristos if (key_st->dk_func->from_dns_key(key_st, &rdata[start],
688ccd87bacSchristos len - start) > 0)
689ccd87bacSchristos return (key_st);
690ccd87bacSchristos } else
691daa7d68eSandvar EREPORT(("%s: unsupported alg %d\n", __func__,
692ccd87bacSchristos alg));
693ccd87bacSchristos
694ccd87bacSchristos SAFE_FREE(key_st);
695a6fdb8b8Sjoerg return (NULL);
696ccd87bacSchristos }
697ccd87bacSchristos
698ccd87bacSchristos /*%
699ccd87bacSchristos * dst_public_key_to_dnskey
700ccd87bacSchristos * Function to encode a public key into DNS KEY wire format
701ccd87bacSchristos * Parameters
702ccd87bacSchristos * key Key structure to encode.
703ccd87bacSchristos * out_storage Location to write the encoded key to.
704ccd87bacSchristos * out_len Size of the output array.
705ccd87bacSchristos * Returns
706ccd87bacSchristos * <0 Failure
707ccd87bacSchristos * >=0 Number of bytes written to out_storage
708ccd87bacSchristos */
709ccd87bacSchristos
710ccd87bacSchristos int
dst_key_to_dnskey(const DST_KEY * key,u_char * out_storage,const int out_len)711ccd87bacSchristos dst_key_to_dnskey(const DST_KEY *key, u_char *out_storage,
712ccd87bacSchristos const int out_len)
713ccd87bacSchristos {
714ccd87bacSchristos u_int16_t val;
715ccd87bacSchristos int loc = 0;
716ccd87bacSchristos int enc_len = 0;
717ccd87bacSchristos if (key == NULL)
718ccd87bacSchristos return (-1);
719ccd87bacSchristos
720ccd87bacSchristos if (!dst_check_algorithm(key->dk_alg)) { /*%< make sure alg is available */
721daa7d68eSandvar EREPORT(("%s: Algorithm %d not supported\n", __func__,
722ccd87bacSchristos key->dk_alg));
723ccd87bacSchristos return (UNSUPPORTED_KEYALG);
724ccd87bacSchristos }
725ccd87bacSchristos memset(out_storage, 0, out_len);
726ccd87bacSchristos val = (u_int16_t)(key->dk_flags & 0xffff);
727ccd87bacSchristos dst_s_put_int16(out_storage, val);
728ccd87bacSchristos loc += 2;
729ccd87bacSchristos
730ccd87bacSchristos out_storage[loc++] = (u_char) key->dk_proto;
731ccd87bacSchristos out_storage[loc++] = (u_char) key->dk_alg;
732ccd87bacSchristos
733ccd87bacSchristos if (key->dk_flags > 0xffff) { /*%< Extended flags */
734ccd87bacSchristos val = (u_int16_t)((key->dk_flags >> 16) & 0xffff);
735ccd87bacSchristos dst_s_put_int16(&out_storage[loc], val);
736ccd87bacSchristos loc += 2;
737ccd87bacSchristos }
738ccd87bacSchristos if (key->dk_KEY_struct == NULL)
739ccd87bacSchristos return (loc);
740ccd87bacSchristos if (key->dk_func && key->dk_func->to_dns_key) {
741ccd87bacSchristos enc_len = key->dk_func->to_dns_key(key,
742ccd87bacSchristos (u_char *) &out_storage[loc],
743ccd87bacSchristos out_len - loc);
744ccd87bacSchristos if (enc_len > 0)
745ccd87bacSchristos return (enc_len + loc);
746ccd87bacSchristos else
747ccd87bacSchristos return (-1);
748ccd87bacSchristos } else
749ccd87bacSchristos EREPORT(("%s: Unsupported ALG %d\n", __func__, key->dk_alg));
750ccd87bacSchristos return (-1);
751ccd87bacSchristos }
752ccd87bacSchristos
753ccd87bacSchristos /*%
754ccd87bacSchristos * dst_buffer_to_key
755ccd87bacSchristos * Function to encode a string of raw data into a DST key
756ccd87bacSchristos * Parameters
757ccd87bacSchristos * alg The algorithm (HMAC only)
758ccd87bacSchristos * key A pointer to the data
759ccd87bacSchristos * keylen The length of the data
760ccd87bacSchristos * Returns
761ccd87bacSchristos * NULL an error occurred
762ccd87bacSchristos * NON-NULL the DST key
763ccd87bacSchristos */
764ccd87bacSchristos DST_KEY *
dst_buffer_to_key(const char * key_name,const int alg,const int flags,const int protocol,const u_char * key_buf,const int key_len)765ccd87bacSchristos dst_buffer_to_key(const char *key_name, /*!< name of the key */
766ccd87bacSchristos const int alg, /*!< algorithm */
767ccd87bacSchristos const int flags, /*!< dns flags */
768ccd87bacSchristos const int protocol, /*!< dns protocol */
769ccd87bacSchristos const u_char *key_buf, /*!< key in dns wire fmt */
770ccd87bacSchristos const int key_len) /*!< size of key */
771ccd87bacSchristos {
772ccd87bacSchristos
773ccd87bacSchristos DST_KEY *dkey = NULL;
774ccd87bacSchristos int dnslen;
775ccd87bacSchristos u_char dns[2048];
776ccd87bacSchristos
777ccd87bacSchristos if (!dst_check_algorithm(alg)) { /*%< make sure alg is available */
778daa7d68eSandvar EREPORT(("%s: Algorithm %d not supported\n", __func__, alg));
779ccd87bacSchristos return (NULL);
780ccd87bacSchristos }
781ccd87bacSchristos
782ccd87bacSchristos dkey = dst_s_get_key_struct(key_name, alg, flags, protocol, -1);
783ccd87bacSchristos
784ccd87bacSchristos if (dkey == NULL || dkey->dk_func == NULL ||
785ccd87bacSchristos dkey->dk_func->from_dns_key == NULL)
786ccd87bacSchristos return (dst_free_key(dkey));
787ccd87bacSchristos
788ccd87bacSchristos if (dkey->dk_func->from_dns_key(dkey, key_buf, key_len) < 0) {
789ccd87bacSchristos EREPORT(("%s: dst_buffer_to_hmac failed\n", __func__));
790ccd87bacSchristos return (dst_free_key(dkey));
791ccd87bacSchristos }
792ccd87bacSchristos
793ccd87bacSchristos dnslen = dst_key_to_dnskey(dkey, dns, (int)sizeof(dns));
794ccd87bacSchristos dkey->dk_id = dst_s_dns_key_id(dns, dnslen);
795ccd87bacSchristos return (dkey);
796ccd87bacSchristos }
797ccd87bacSchristos
798ccd87bacSchristos int
dst_key_to_buffer(DST_KEY * key,u_char * out_buff,int buf_len)799ccd87bacSchristos dst_key_to_buffer(DST_KEY *key, u_char *out_buff, int buf_len)
800ccd87bacSchristos {
801ccd87bacSchristos int len;
802ccd87bacSchristos /* this function will extrac the secret of HMAC into a buffer */
803ccd87bacSchristos if (key == NULL)
804ccd87bacSchristos return (0);
805ccd87bacSchristos if (key->dk_func != NULL && key->dk_func->to_dns_key != NULL) {
806ccd87bacSchristos len = key->dk_func->to_dns_key(key, out_buff, buf_len);
807ccd87bacSchristos if (len < 0)
808ccd87bacSchristos return (0);
809ccd87bacSchristos return (len);
810ccd87bacSchristos }
811ccd87bacSchristos return (0);
812ccd87bacSchristos }
813ccd87bacSchristos
814ccd87bacSchristos /*%
815ccd87bacSchristos * dst_s_read_private_key_file
816ccd87bacSchristos * Function reads in private key from a file.
817ccd87bacSchristos * Fills out the KEY structure.
818ccd87bacSchristos * Parameters
819ccd87bacSchristos * name Name of the key to be read.
820ccd87bacSchristos * pk_key Structure that the key is returned in.
821ccd87bacSchristos * in_id Key identifier (tag)
822ccd87bacSchristos * Return
823*82bba4e9Sandvar * 1 if everything works
824ccd87bacSchristos * 0 if there is any problem
825ccd87bacSchristos */
826ccd87bacSchristos
827ccd87bacSchristos static int
dst_s_read_private_key_file(char * name,DST_KEY * pk_key,u_int16_t in_id,int in_alg)828ccd87bacSchristos dst_s_read_private_key_file(char *name, DST_KEY *pk_key, u_int16_t in_id,
829ccd87bacSchristos int in_alg)
830ccd87bacSchristos {
831ccd87bacSchristos int alg, major, minor, file_major, file_minor;
832ccd87bacSchristos ssize_t cnt;
833ccd87bacSchristos size_t len;
834ccd87bacSchristos int ret, id;
835ccd87bacSchristos char filename[PATH_MAX];
836ccd87bacSchristos u_char in_buff[RAW_KEY_SIZE], *p;
837ccd87bacSchristos FILE *fp;
838ccd87bacSchristos int dnslen;
839ccd87bacSchristos u_char dns[2048];
840ccd87bacSchristos
841ccd87bacSchristos if (name == NULL || pk_key == NULL) {
842ccd87bacSchristos EREPORT(("%s: No key name given\n", __func__));
843ccd87bacSchristos return (0);
844ccd87bacSchristos }
845ccd87bacSchristos /* Make the filename */
846ccd87bacSchristos if (dst_s_build_filename(filename, name, in_id, in_alg, PRIVATE_KEY,
847ccd87bacSchristos PATH_MAX) == -1) {
848ccd87bacSchristos EREPORT(("%s: Cannot make filename from %s, %d, and %s\n",
849ccd87bacSchristos __func__, name, in_id, PRIVATE_KEY));
850ccd87bacSchristos return (0);
851ccd87bacSchristos }
852ccd87bacSchristos /* first check if we can find the key file */
853ccd87bacSchristos if ((fp = dst_s_fopen(filename, "r", 0)) == NULL) {
854ccd87bacSchristos EREPORT(("%s: Could not open file %s in directory %s\n",
855ccd87bacSchristos __func__, filename, dst_path[0] ? dst_path :
856ccd87bacSchristos getcwd(NULL, PATH_MAX - 1)));
857ccd87bacSchristos return (0);
858ccd87bacSchristos }
859ccd87bacSchristos /* now read the header info from the file */
860ccd87bacSchristos if ((cnt = fread(in_buff, 1, sizeof(in_buff), fp)) < 5) {
861ccd87bacSchristos fclose(fp);
862ccd87bacSchristos EREPORT(("%s: error reading file %s (empty file)\n",
863ccd87bacSchristos __func__, filename));
864ccd87bacSchristos return (0);
865ccd87bacSchristos }
866ccd87bacSchristos len = cnt;
867ccd87bacSchristos /* decrypt key */
868ccd87bacSchristos fclose(fp);
869ccd87bacSchristos if (memcmp(in_buff, "Private-key-format: v", 20) != 0)
870ccd87bacSchristos goto fail;
871ccd87bacSchristos p = in_buff;
872ccd87bacSchristos
873ccd87bacSchristos if (!dst_s_verify_str((const char **) (void *)&p,
874ccd87bacSchristos "Private-key-format: v")) {
875ccd87bacSchristos EREPORT(("%s: Not a Key file/Decrypt failed %s\n", __func__,
876ccd87bacSchristos name));
877ccd87bacSchristos goto fail;
878ccd87bacSchristos }
879ccd87bacSchristos /* read in file format */
880ccd87bacSchristos sscanf((char *)p, "%d.%d", &file_major, &file_minor);
881ccd87bacSchristos sscanf(KEY_FILE_FORMAT, "%d.%d", &major, &minor);
882ccd87bacSchristos if (file_major < 1) {
883ccd87bacSchristos EREPORT(("%s: Unknown keyfile %d.%d version for %s\n",
884ccd87bacSchristos __func__, file_major, file_minor, name));
885ccd87bacSchristos goto fail;
886ccd87bacSchristos } else if (file_major > major || file_minor > minor)
887ccd87bacSchristos EREPORT(("%s: Keyfile %s version higher than mine %d.%d MAY"
888ccd87bacSchristos " FAIL\n", __func__, name, file_major, file_minor));
889ccd87bacSchristos
890ccd87bacSchristos while (*p++ != '\n') ; /*%< skip to end of line */
891ccd87bacSchristos
892ccd87bacSchristos if (!dst_s_verify_str((const char **) (void *)&p, "Algorithm: "))
893ccd87bacSchristos goto fail;
894ccd87bacSchristos
895ccd87bacSchristos if (sscanf((char *)p, "%d", &alg) != 1)
896ccd87bacSchristos goto fail;
897ccd87bacSchristos while (*p++ != '\n') ; /*%< skip to end of line */
898ccd87bacSchristos
899ccd87bacSchristos if (pk_key->dk_key_name && !strcmp(pk_key->dk_key_name, name))
900ccd87bacSchristos SAFE_FREE2(pk_key->dk_key_name, strlen(pk_key->dk_key_name));
901ccd87bacSchristos pk_key->dk_key_name = strdup(name);
902ccd87bacSchristos
903ccd87bacSchristos /* allocate and fill in key structure */
904ccd87bacSchristos if (pk_key->dk_func == NULL || pk_key->dk_func->from_file_fmt == NULL)
905ccd87bacSchristos goto fail;
906ccd87bacSchristos
907ccd87bacSchristos ret = pk_key->dk_func->from_file_fmt(pk_key, (char *)p,
908ccd87bacSchristos (int)(&in_buff[len] - p));
909ccd87bacSchristos if (ret < 0)
910ccd87bacSchristos goto fail;
911ccd87bacSchristos
912ccd87bacSchristos dnslen = dst_key_to_dnskey(pk_key, dns, (int)sizeof(dns));
913ccd87bacSchristos id = dst_s_dns_key_id(dns, dnslen);
914ccd87bacSchristos
915ccd87bacSchristos /* Make sure the actual key tag matches the input tag used in the
916ccd87bacSchristos * filename */
917ccd87bacSchristos if (id != in_id) {
918ccd87bacSchristos EREPORT(("%s: actual tag of key read %d != input tag used to"
919ccd87bacSchristos "build filename %d.\n", __func__, id, in_id));
920ccd87bacSchristos goto fail;
921ccd87bacSchristos }
922ccd87bacSchristos pk_key->dk_id = (u_int16_t) id;
923ccd87bacSchristos pk_key->dk_alg = alg;
924ccd87bacSchristos memset(in_buff, 0, len);
925ccd87bacSchristos return (1);
926ccd87bacSchristos
927ccd87bacSchristos fail:
928ccd87bacSchristos memset(in_buff, 0, len);
929ccd87bacSchristos return (0);
930ccd87bacSchristos }
931ccd87bacSchristos
932ccd87bacSchristos /*%
933ccd87bacSchristos * Generate and store a public/private keypair.
934ccd87bacSchristos * Keys will be stored in formatted files.
935ccd87bacSchristos *
936ccd87bacSchristos * Parameters
937ccd87bacSchristos &
938ccd87bacSchristos *\par name Name of the new key. Used to create key files
939ccd87bacSchristos *\li K<name>+<alg>+<id>.public and K<name>+<alg>+<id>.private.
940ccd87bacSchristos *\par bits Size of the new key in bits.
941ccd87bacSchristos *\par exp What exponent to use:
942ccd87bacSchristos *\li 0 use exponent 3
943ccd87bacSchristos *\li non-zero use Fermant4
944ccd87bacSchristos *\par flags The default value of the DNS Key flags.
945ccd87bacSchristos *\li The DNS Key RR Flag field is defined in RFC2065,
946ccd87bacSchristos * section 3.3. The field has 16 bits.
947ccd87bacSchristos *\par protocol
948ccd87bacSchristos *\li Default value of the DNS Key protocol field.
949ccd87bacSchristos *\li The DNS Key protocol field is defined in RFC2065,
950ccd87bacSchristos * section 3.4. The field has 8 bits.
951ccd87bacSchristos *\par alg What algorithm to use. Currently defined:
952ccd87bacSchristos *\li KEY_RSA 1
953ccd87bacSchristos *\li KEY_DSA 3
954ccd87bacSchristos *\li KEY_HMAC 157
955ccd87bacSchristos *\par out_id The key tag is returned.
956ccd87bacSchristos *
957ccd87bacSchristos * Return
958ccd87bacSchristos *\li NULL Failure
959ccd87bacSchristos *\li non-NULL the generated key pair
960ccd87bacSchristos * Caller frees the result, and its dk_name pointer.
961ccd87bacSchristos */
962ccd87bacSchristos DST_KEY *
dst_generate_key(const char * name,const int bits,const int exp,const int flags,const int protocol,const int alg)963ccd87bacSchristos dst_generate_key(const char *name, const int bits, const int exp,
964ccd87bacSchristos const int flags, const int protocol, const int alg)
965ccd87bacSchristos {
966ccd87bacSchristos DST_KEY *new_key = NULL;
967ccd87bacSchristos int dnslen;
968ccd87bacSchristos u_char dns[2048];
969ccd87bacSchristos
970ccd87bacSchristos if (name == NULL)
971ccd87bacSchristos return (NULL);
972ccd87bacSchristos
973ccd87bacSchristos if (!dst_check_algorithm(alg)) { /*%< make sure alg is available */
974daa7d68eSandvar EREPORT(("%s: Algorithm %d not supported\n", __func__, alg));
975ccd87bacSchristos return (NULL);
976ccd87bacSchristos }
977ccd87bacSchristos
978ccd87bacSchristos new_key = dst_s_get_key_struct(name, alg, flags, protocol, bits);
979ccd87bacSchristos if (new_key == NULL)
980ccd87bacSchristos return (NULL);
981ccd87bacSchristos if (bits == 0) /*%< null key we are done */
982ccd87bacSchristos return (new_key);
983ccd87bacSchristos if (new_key->dk_func == NULL || new_key->dk_func->generate == NULL) {
984ccd87bacSchristos EREPORT(("%s: Unsupported algorithm %d\n", __func__, alg));
985ccd87bacSchristos return (dst_free_key(new_key));
986ccd87bacSchristos }
987ccd87bacSchristos if (new_key->dk_func->generate(new_key, exp) <= 0) {
988ccd87bacSchristos EREPORT(("%s: Key generation failure %s %d %d %d\n", __func__,
989ccd87bacSchristos new_key->dk_key_name, new_key->dk_alg,
990ccd87bacSchristos new_key->dk_key_size, exp));
991ccd87bacSchristos return (dst_free_key(new_key));
992ccd87bacSchristos }
993ccd87bacSchristos
994ccd87bacSchristos dnslen = dst_key_to_dnskey(new_key, dns, (int)sizeof(dns));
995ccd87bacSchristos if (dnslen != UNSUPPORTED_KEYALG)
996ccd87bacSchristos new_key->dk_id = dst_s_dns_key_id(dns, dnslen);
997ccd87bacSchristos else
998ccd87bacSchristos new_key->dk_id = 0;
999ccd87bacSchristos
1000ccd87bacSchristos return (new_key);
1001ccd87bacSchristos }
1002ccd87bacSchristos
1003ccd87bacSchristos /*%
1004ccd87bacSchristos * Release all data structures pointed to by a key structure.
1005ccd87bacSchristos *
1006ccd87bacSchristos * Parameters
1007ccd87bacSchristos *\li f_key Key structure to be freed.
1008ccd87bacSchristos */
1009ccd87bacSchristos
1010ccd87bacSchristos DST_KEY *
dst_free_key(DST_KEY * f_key)1011ccd87bacSchristos dst_free_key(DST_KEY *f_key)
1012ccd87bacSchristos {
1013ccd87bacSchristos
1014ccd87bacSchristos if (f_key == NULL)
1015ccd87bacSchristos return (f_key);
1016ccd87bacSchristos if (f_key->dk_func && f_key->dk_func->destroy)
1017ccd87bacSchristos f_key->dk_KEY_struct =
1018ccd87bacSchristos f_key->dk_func->destroy(f_key->dk_KEY_struct);
1019ccd87bacSchristos else {
1020ccd87bacSchristos EREPORT(("%s: Unknown key alg %d\n", __func__, f_key->dk_alg));
1021ccd87bacSchristos }
1022ccd87bacSchristos if (f_key->dk_KEY_struct) {
1023ccd87bacSchristos free(f_key->dk_KEY_struct);
1024ccd87bacSchristos f_key->dk_KEY_struct = NULL;
1025ccd87bacSchristos }
1026ccd87bacSchristos if (f_key->dk_key_name)
1027ccd87bacSchristos SAFE_FREE(f_key->dk_key_name);
1028ccd87bacSchristos SAFE_FREE(f_key);
1029ccd87bacSchristos return (NULL);
1030ccd87bacSchristos }
1031ccd87bacSchristos
1032ccd87bacSchristos /*%
10339a662625Sandvar * Return the maximum size of signature from the key specified in bytes
1034ccd87bacSchristos *
1035ccd87bacSchristos * Parameters
1036ccd87bacSchristos *\li key
1037ccd87bacSchristos *
1038ccd87bacSchristos * Returns
1039ccd87bacSchristos * \li bytes
1040ccd87bacSchristos */
1041ccd87bacSchristos int
dst_sig_size(DST_KEY * key)1042ccd87bacSchristos dst_sig_size(DST_KEY *key) {
1043ccd87bacSchristos switch (key->dk_alg) {
1044ccd87bacSchristos case KEY_HMAC_MD5:
1045ccd87bacSchristos return (16);
1046ccd87bacSchristos case KEY_HMAC_SHA1:
1047ccd87bacSchristos return (20);
1048ccd87bacSchristos case KEY_RSA:
1049ccd87bacSchristos return (key->dk_key_size + 7) / 8;
1050ccd87bacSchristos case KEY_DSA:
1051ccd87bacSchristos return (40);
1052ccd87bacSchristos default:
1053ccd87bacSchristos EREPORT(("%s: Unknown key alg %d\n", __func__, key->dk_alg));
1054ccd87bacSchristos return -1;
1055ccd87bacSchristos }
1056ccd87bacSchristos }
1057ccd87bacSchristos
1058ccd87bacSchristos /*! \file */
1059