1999-07-12 20:08:42 +00:00
|
|
|
/*
|
2004-03-05 05:48:29 +00:00
|
|
|
* Portions Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
|
2002-02-20 03:35:59 +00:00
|
|
|
* Portions Copyright (C) 1999-2002 Internet Software Consortium.
|
2000-06-09 20:58:39 +00:00
|
|
|
* Portions Copyright (C) 1995-2000 by Network Associates, Inc.
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
2000-05-08 14:38:29 +00:00
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
1999-07-12 20:08:42 +00:00
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
2004-03-05 05:14:21 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NETWORK ASSOCIATES DISCLAIMS
|
|
|
|
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE
|
|
|
|
* FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
|
|
|
|
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
1999-07-12 20:08:42 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Principal Author: Brian Wellington
|
2004-03-05 05:48:29 +00:00
|
|
|
* $Id: dst_parse.c,v 1.40 2004/03/05 05:48:24 marka Exp $
|
1999-07-12 20:08:42 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <isc/base64.h>
|
1999-10-08 22:25:14 +00:00
|
|
|
#include <isc/dir.h>
|
2000-06-20 04:13:40 +00:00
|
|
|
#include <isc/fsaccess.h>
|
1999-07-12 20:08:42 +00:00
|
|
|
#include <isc/lex.h>
|
|
|
|
#include <isc/mem.h>
|
2000-05-08 14:38:29 +00:00
|
|
|
#include <isc/string.h>
|
2000-04-28 01:12:23 +00:00
|
|
|
#include <isc/util.h>
|
|
|
|
|
1999-07-12 20:08:42 +00:00
|
|
|
#include "dst_internal.h"
|
|
|
|
#include "dst_parse.h"
|
|
|
|
#include "dst/result.h"
|
|
|
|
|
2002-01-21 01:07:32 +00:00
|
|
|
#define DST_AS_STR(t) ((t).value.as_textregion.base)
|
1999-07-12 20:08:42 +00:00
|
|
|
|
|
|
|
#define PRIVATE_KEY_STR "Private-key-format:"
|
|
|
|
#define ALGORITHM_STR "Algorithm:"
|
|
|
|
|
|
|
|
struct parse_map {
|
2000-06-02 18:57:51 +00:00
|
|
|
const int value;
|
2000-06-01 18:26:56 +00:00
|
|
|
const char *tag;
|
1999-07-12 20:08:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct parse_map map[] = {
|
|
|
|
{TAG_RSA_MODULUS, "Modulus:"},
|
|
|
|
{TAG_RSA_PUBLICEXPONENT, "PublicExponent:"},
|
|
|
|
{TAG_RSA_PRIVATEEXPONENT, "PrivateExponent:"},
|
|
|
|
{TAG_RSA_PRIME1, "Prime1:"},
|
|
|
|
{TAG_RSA_PRIME2, "Prime2:"},
|
|
|
|
{TAG_RSA_EXPONENT1, "Exponent1:"},
|
|
|
|
{TAG_RSA_EXPONENT2, "Exponent2:"},
|
|
|
|
{TAG_RSA_COEFFICIENT, "Coefficient:"},
|
|
|
|
|
1999-09-27 16:55:45 +00:00
|
|
|
{TAG_DH_PRIME, "Prime(p):"},
|
|
|
|
{TAG_DH_GENERATOR, "Generator(g):"},
|
|
|
|
{TAG_DH_PRIVATE, "Private_value(x):"},
|
|
|
|
{TAG_DH_PUBLIC, "Public_value(y):"},
|
|
|
|
|
1999-07-12 20:08:42 +00:00
|
|
|
{TAG_DSA_PRIME, "Prime(p):"},
|
|
|
|
{TAG_DSA_SUBPRIME, "Subprime(q):"},
|
|
|
|
{TAG_DSA_BASE, "Base(g):"},
|
|
|
|
{TAG_DSA_PRIVATE, "Private_value(x):"},
|
|
|
|
{TAG_DSA_PUBLIC, "Public_value(y):"},
|
|
|
|
|
|
|
|
{TAG_HMACMD5_KEY, "Key:"},
|
|
|
|
{0, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
2000-06-02 18:57:51 +00:00
|
|
|
find_value(const char *s, const unsigned int alg) {
|
1999-07-12 20:08:42 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; ; i++) {
|
|
|
|
if (map[i].tag == NULL)
|
|
|
|
return (-1);
|
1999-09-30 17:41:36 +00:00
|
|
|
else if (strcasecmp(s, map[i].tag) == 0 &&
|
|
|
|
TAG_ALG(map[i].value) == alg)
|
1999-07-12 20:08:42 +00:00
|
|
|
return (map[i].value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-06-01 18:26:56 +00:00
|
|
|
static const char *
|
1999-07-12 20:08:42 +00:00
|
|
|
find_tag(const int value) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; ; i++) {
|
|
|
|
if (map[i].tag == NULL)
|
|
|
|
return (NULL);
|
|
|
|
else if (value == map[i].value)
|
|
|
|
return (map[i].tag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
check_rsa(const dst_private_t *priv) {
|
|
|
|
int i, j;
|
|
|
|
if (priv->nelements != RSA_NTAGS)
|
|
|
|
return (-1);
|
|
|
|
for (i = 0; i < RSA_NTAGS; i++) {
|
|
|
|
for (j = 0; j < priv->nelements; j++)
|
2000-09-02 01:17:20 +00:00
|
|
|
if (priv->elements[j].tag == TAG(DST_ALG_RSAMD5, i))
|
1999-07-12 20:08:42 +00:00
|
|
|
break;
|
|
|
|
if (j == priv->nelements)
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
1999-09-27 16:55:45 +00:00
|
|
|
static int
|
|
|
|
check_dh(const dst_private_t *priv) {
|
|
|
|
int i, j;
|
|
|
|
if (priv->nelements != DH_NTAGS)
|
|
|
|
return (-1);
|
|
|
|
for (i = 0; i < DH_NTAGS; i++) {
|
|
|
|
for (j = 0; j < priv->nelements; j++)
|
|
|
|
if (priv->elements[j].tag == TAG(DST_ALG_DH, i))
|
|
|
|
break;
|
|
|
|
if (j == priv->nelements)
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
1999-07-12 20:08:42 +00:00
|
|
|
static int
|
|
|
|
check_dsa(const dst_private_t *priv) {
|
|
|
|
int i, j;
|
|
|
|
if (priv->nelements != DSA_NTAGS)
|
|
|
|
return (-1);
|
|
|
|
for (i = 0; i < DSA_NTAGS; i++) {
|
|
|
|
for (j = 0; j < priv->nelements; j++)
|
|
|
|
if (priv->elements[j].tag == TAG(DST_ALG_DSA, i))
|
|
|
|
break;
|
|
|
|
if (j == priv->nelements)
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
check_hmac_md5(const dst_private_t *priv) {
|
|
|
|
if (priv->nelements != HMACMD5_NTAGS)
|
|
|
|
return (-1);
|
|
|
|
if (priv->elements[0].tag != TAG_HMACMD5_KEY)
|
|
|
|
return (-1);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2000-06-02 18:57:51 +00:00
|
|
|
check_data(const dst_private_t *priv, const unsigned int alg) {
|
2004-02-01 23:56:19 +00:00
|
|
|
/* XXXVIX this switch statement is too sparse to gen a jump table. */
|
1999-07-12 20:08:42 +00:00
|
|
|
switch (alg) {
|
2004-02-01 23:56:19 +00:00
|
|
|
case DST_ALG_RSAMD5:
|
|
|
|
case DST_ALG_RSASHA1:
|
|
|
|
return (check_rsa(priv));
|
|
|
|
case DST_ALG_DH:
|
|
|
|
return (check_dh(priv));
|
|
|
|
case DST_ALG_DSA:
|
|
|
|
return (check_dsa(priv));
|
|
|
|
case DST_ALG_HMACMD5:
|
|
|
|
return (check_hmac_md5(priv));
|
|
|
|
default:
|
|
|
|
return (DST_R_UNSUPPORTEDALG);
|
1999-07-12 20:08:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-06-02 23:36:14 +00:00
|
|
|
dst__privstruct_free(dst_private_t *priv, isc_mem_t *mctx) {
|
1999-07-12 20:08:42 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if (priv == NULL)
|
|
|
|
return;
|
|
|
|
for (i = 0; i < priv->nelements; i++) {
|
|
|
|
if (priv->elements[i].data == NULL)
|
|
|
|
continue;
|
|
|
|
memset(priv->elements[i].data, 0, MAXFIELDSIZE);
|
|
|
|
isc_mem_put(mctx, priv->elements[i].data, MAXFIELDSIZE);
|
|
|
|
}
|
|
|
|
priv->nelements = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2002-02-27 22:12:06 +00:00
|
|
|
dst__privstruct_parse(dst_key_t *key, unsigned int alg, isc_lex_t *lex,
|
|
|
|
isc_mem_t *mctx, dst_private_t *priv)
|
1999-07-12 20:08:42 +00:00
|
|
|
{
|
2000-09-08 14:25:40 +00:00
|
|
|
int n = 0, major, minor;
|
1999-07-12 20:08:42 +00:00
|
|
|
isc_buffer_t b;
|
|
|
|
isc_token_t token;
|
2002-02-27 22:12:06 +00:00
|
|
|
unsigned char *data = NULL;
|
1999-07-12 20:08:42 +00:00
|
|
|
unsigned int opt = ISC_LEXOPT_EOL;
|
2000-09-08 14:25:40 +00:00
|
|
|
isc_result_t ret;
|
1999-07-12 20:08:42 +00:00
|
|
|
|
|
|
|
REQUIRE(priv != NULL);
|
|
|
|
|
|
|
|
priv->nelements = 0;
|
|
|
|
|
2002-02-27 22:12:06 +00:00
|
|
|
#define NEXTTOKEN(lex, opt, token) \
|
|
|
|
do { \
|
|
|
|
ret = isc_lex_gettoken(lex, opt, token); \
|
|
|
|
if (ret != ISC_R_SUCCESS) \
|
|
|
|
goto fail; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define READLINE(lex, opt, token) \
|
|
|
|
do { \
|
|
|
|
ret = isc_lex_gettoken(lex, opt, token); \
|
|
|
|
if (ret == ISC_R_EOF) \
|
|
|
|
break; \
|
|
|
|
else if (ret != ISC_R_SUCCESS) \
|
|
|
|
goto fail; \
|
|
|
|
} while ((*token).type != isc_tokentype_eol)
|
1999-07-12 20:08:42 +00:00
|
|
|
|
2000-05-13 19:30:19 +00:00
|
|
|
/*
|
|
|
|
* Read the description line.
|
|
|
|
*/
|
1999-07-12 20:08:42 +00:00
|
|
|
NEXTTOKEN(lex, opt, &token);
|
|
|
|
if (token.type != isc_tokentype_string ||
|
2002-01-21 01:07:32 +00:00
|
|
|
strcmp(DST_AS_STR(token), PRIVATE_KEY_STR) != 0)
|
2000-09-08 14:25:40 +00:00
|
|
|
{
|
|
|
|
ret = DST_R_INVALIDPRIVATEKEY;
|
1999-07-12 20:08:42 +00:00
|
|
|
goto fail;
|
2000-09-08 14:25:40 +00:00
|
|
|
}
|
2000-08-01 01:33:37 +00:00
|
|
|
|
1999-07-12 20:08:42 +00:00
|
|
|
NEXTTOKEN(lex, opt, &token);
|
|
|
|
if (token.type != isc_tokentype_string ||
|
2002-01-21 01:07:32 +00:00
|
|
|
(DST_AS_STR(token))[0] != 'v')
|
2000-09-08 14:25:40 +00:00
|
|
|
{
|
|
|
|
ret = DST_R_INVALIDPRIVATEKEY;
|
1999-07-12 20:08:42 +00:00
|
|
|
goto fail;
|
2000-09-08 14:25:40 +00:00
|
|
|
}
|
2002-01-21 01:07:32 +00:00
|
|
|
if (sscanf(DST_AS_STR(token), "v%d.%d", &major, &minor) != 2)
|
2000-09-08 14:25:40 +00:00
|
|
|
{
|
|
|
|
ret = DST_R_INVALIDPRIVATEKEY;
|
1999-07-12 20:08:42 +00:00
|
|
|
goto fail;
|
2000-09-08 14:25:40 +00:00
|
|
|
}
|
1999-07-12 20:08:42 +00:00
|
|
|
|
|
|
|
if (major > MAJOR_VERSION ||
|
|
|
|
(major == MAJOR_VERSION && minor > MINOR_VERSION))
|
2000-09-08 14:25:40 +00:00
|
|
|
{
|
|
|
|
ret = DST_R_INVALIDPRIVATEKEY;
|
1999-07-12 20:08:42 +00:00
|
|
|
goto fail;
|
2000-09-08 14:25:40 +00:00
|
|
|
}
|
1999-07-12 20:08:42 +00:00
|
|
|
|
|
|
|
READLINE(lex, opt, &token);
|
|
|
|
|
2000-05-13 19:30:19 +00:00
|
|
|
/*
|
|
|
|
* Read the algorithm line.
|
|
|
|
*/
|
1999-07-12 20:08:42 +00:00
|
|
|
NEXTTOKEN(lex, opt, &token);
|
|
|
|
if (token.type != isc_tokentype_string ||
|
2002-01-21 01:07:32 +00:00
|
|
|
strcmp(DST_AS_STR(token), ALGORITHM_STR) != 0)
|
2000-09-08 14:25:40 +00:00
|
|
|
{
|
|
|
|
ret = DST_R_INVALIDPRIVATEKEY;
|
1999-07-12 20:08:42 +00:00
|
|
|
goto fail;
|
2000-09-08 14:25:40 +00:00
|
|
|
}
|
1999-07-12 20:08:42 +00:00
|
|
|
|
|
|
|
NEXTTOKEN(lex, opt | ISC_LEXOPT_NUMBER, &token);
|
|
|
|
if (token.type != isc_tokentype_number ||
|
2000-05-15 21:02:39 +00:00
|
|
|
token.value.as_ulong != (unsigned long) dst_key_alg(key))
|
2000-09-08 14:25:40 +00:00
|
|
|
{
|
|
|
|
ret = DST_R_INVALIDPRIVATEKEY;
|
1999-07-12 20:08:42 +00:00
|
|
|
goto fail;
|
2000-09-08 14:25:40 +00:00
|
|
|
}
|
1999-07-12 20:08:42 +00:00
|
|
|
|
|
|
|
READLINE(lex, opt, &token);
|
|
|
|
|
2000-05-13 19:30:19 +00:00
|
|
|
/*
|
|
|
|
* Read the key data.
|
|
|
|
*/
|
1999-07-12 20:08:42 +00:00
|
|
|
for (n = 0; n < MAXFIELDS; n++) {
|
|
|
|
int tag;
|
|
|
|
isc_region_t r;
|
|
|
|
|
2001-05-31 00:38:07 +00:00
|
|
|
do {
|
|
|
|
ret = isc_lex_gettoken(lex, opt, &token);
|
|
|
|
if (ret == ISC_R_EOF)
|
|
|
|
goto done;
|
|
|
|
if (ret != ISC_R_SUCCESS)
|
|
|
|
goto fail;
|
|
|
|
} while (token.type == isc_tokentype_eol);
|
|
|
|
|
2000-09-08 14:25:40 +00:00
|
|
|
if (token.type != isc_tokentype_string) {
|
|
|
|
ret = DST_R_INVALIDPRIVATEKEY;
|
1999-07-12 20:08:42 +00:00
|
|
|
goto fail;
|
2000-09-08 14:25:40 +00:00
|
|
|
}
|
1999-07-12 20:08:42 +00:00
|
|
|
|
|
|
|
memset(&priv->elements[n], 0, sizeof(dst_private_element_t));
|
2002-01-21 01:07:32 +00:00
|
|
|
tag = find_value(DST_AS_STR(token), alg);
|
2001-09-15 00:01:58 +00:00
|
|
|
if (tag < 0 || TAG_ALG(tag) != alg) {
|
|
|
|
ret = DST_R_INVALIDPRIVATEKEY;
|
1999-07-12 20:08:42 +00:00
|
|
|
goto fail;
|
2001-09-15 00:01:58 +00:00
|
|
|
}
|
1999-07-12 20:08:42 +00:00
|
|
|
priv->elements[n].tag = tag;
|
|
|
|
|
|
|
|
data = (unsigned char *) isc_mem_get(mctx, MAXFIELDSIZE);
|
2000-05-13 19:30:19 +00:00
|
|
|
if (data == NULL)
|
1999-07-12 20:08:42 +00:00
|
|
|
goto fail;
|
2000-05-13 19:30:19 +00:00
|
|
|
|
103. [func] libisc buffer API changes for <isc/buffer.h>:
Added:
isc_buffer_base(b) (pointer)
isc_buffer_current(b) (pointer)
isc_buffer_active(b) (pointer)
isc_buffer_used(b) (pointer)
isc_buffer_length(b) (int)
isc_buffer_usedlength(b) (int)
isc_buffer_consumedlength(b) (int)
isc_buffer_remaininglength(b) (int)
isc_buffer_activelength(b) (int)
isc_buffer_availablelength(b) (int)
Removed:
ISC_BUFFER_USEDCOUNT(b)
ISC_BUFFER_AVAILABLECOUNT(b)
isc_buffer_type(b)
Changed names:
isc_buffer_used(b, r) ->
isc_buffer_usedregion(b, r)
isc_buffer_available(b, r) ->
isc_buffer_available_region(b, r)
isc_buffer_consumed(b, r) ->
isc_buffer_consumedregion(b, r)
isc_buffer_active(b, r) ->
isc_buffer_activeregion(b, r)
isc_buffer_remaining(b, r) ->
isc_buffer_remainingregion(b, r)
Buffer types were removed, so the ISC_BUFFERTYPE_*
macros are no more, and the type argument to
isc_buffer_init and isc_buffer_allocate were removed.
isc_buffer_putstr is now void (instead of isc_result_t)
and requires that the caller ensure that there
is enough available buffer space for the string.
2000-04-27 00:03:12 +00:00
|
|
|
isc_buffer_init(&b, data, MAXFIELDSIZE);
|
1999-07-12 20:08:42 +00:00
|
|
|
ret = isc_base64_tobuffer(lex, &b, -1);
|
|
|
|
if (ret != ISC_R_SUCCESS)
|
|
|
|
goto fail;
|
103. [func] libisc buffer API changes for <isc/buffer.h>:
Added:
isc_buffer_base(b) (pointer)
isc_buffer_current(b) (pointer)
isc_buffer_active(b) (pointer)
isc_buffer_used(b) (pointer)
isc_buffer_length(b) (int)
isc_buffer_usedlength(b) (int)
isc_buffer_consumedlength(b) (int)
isc_buffer_remaininglength(b) (int)
isc_buffer_activelength(b) (int)
isc_buffer_availablelength(b) (int)
Removed:
ISC_BUFFER_USEDCOUNT(b)
ISC_BUFFER_AVAILABLECOUNT(b)
isc_buffer_type(b)
Changed names:
isc_buffer_used(b, r) ->
isc_buffer_usedregion(b, r)
isc_buffer_available(b, r) ->
isc_buffer_available_region(b, r)
isc_buffer_consumed(b, r) ->
isc_buffer_consumedregion(b, r)
isc_buffer_active(b, r) ->
isc_buffer_activeregion(b, r)
isc_buffer_remaining(b, r) ->
isc_buffer_remainingregion(b, r)
Buffer types were removed, so the ISC_BUFFERTYPE_*
macros are no more, and the type argument to
isc_buffer_init and isc_buffer_allocate were removed.
isc_buffer_putstr is now void (instead of isc_result_t)
and requires that the caller ensure that there
is enough available buffer space for the string.
2000-04-27 00:03:12 +00:00
|
|
|
isc_buffer_usedregion(&b, &r);
|
1999-07-12 20:08:42 +00:00
|
|
|
priv->elements[n].length = r.length;
|
|
|
|
priv->elements[n].data = r.base;
|
|
|
|
|
|
|
|
READLINE(lex, opt, &token);
|
2002-02-27 22:12:06 +00:00
|
|
|
data = NULL;
|
1999-07-12 20:08:42 +00:00
|
|
|
}
|
2001-05-31 00:38:07 +00:00
|
|
|
done:
|
1999-07-12 20:08:42 +00:00
|
|
|
priv->nelements = n;
|
|
|
|
|
2001-09-15 00:01:58 +00:00
|
|
|
if (check_data(priv, alg) < 0)
|
1999-07-12 20:08:42 +00:00
|
|
|
goto fail;
|
|
|
|
|
1999-10-20 22:14:15 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
1999-07-12 20:08:42 +00:00
|
|
|
|
|
|
|
fail:
|
|
|
|
priv->nelements = n;
|
2000-06-02 23:36:14 +00:00
|
|
|
dst__privstruct_free(priv, mctx);
|
2002-02-27 22:12:06 +00:00
|
|
|
if (data != NULL)
|
|
|
|
isc_mem_put(mctx, data, MAXFIELDSIZE);
|
|
|
|
|
2000-09-08 14:25:40 +00:00
|
|
|
return (ret);
|
1999-07-12 20:08:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-06-06 21:58:16 +00:00
|
|
|
dst__privstruct_writefile(const dst_key_t *key, const dst_private_t *priv,
|
|
|
|
const char *directory)
|
|
|
|
{
|
1999-07-12 20:08:42 +00:00
|
|
|
FILE *fp;
|
|
|
|
int ret, i;
|
|
|
|
isc_result_t iret;
|
1999-10-08 22:25:14 +00:00
|
|
|
char filename[ISC_DIR_NAMEMAX];
|
1999-07-12 20:08:42 +00:00
|
|
|
char buffer[MAXFIELDSIZE * 2];
|
2000-05-15 21:02:39 +00:00
|
|
|
isc_buffer_t b;
|
2000-06-20 04:13:40 +00:00
|
|
|
isc_fsaccess_t access;
|
1999-07-12 20:08:42 +00:00
|
|
|
|
|
|
|
REQUIRE(priv != NULL);
|
|
|
|
|
2000-05-15 21:02:39 +00:00
|
|
|
if (check_data(priv, dst_key_alg(key)) < 0)
|
1999-09-01 18:56:19 +00:00
|
|
|
return (DST_R_INVALIDPRIVATEKEY);
|
1999-07-12 20:08:42 +00:00
|
|
|
|
2000-05-15 21:02:39 +00:00
|
|
|
isc_buffer_init(&b, filename, sizeof(filename));
|
2000-06-06 21:58:16 +00:00
|
|
|
ret = dst_key_buildfilename(key, DST_TYPE_PRIVATE, directory, &b);
|
2000-05-15 21:02:39 +00:00
|
|
|
if (ret != ISC_R_SUCCESS)
|
|
|
|
return (ret);
|
1999-07-12 20:08:42 +00:00
|
|
|
|
|
|
|
if ((fp = fopen(filename, "w")) == NULL)
|
1999-09-01 18:56:19 +00:00
|
|
|
return (DST_R_WRITEERROR);
|
1999-07-12 20:08:42 +00:00
|
|
|
|
2000-06-20 04:13:40 +00:00
|
|
|
access = 0;
|
|
|
|
isc_fsaccess_add(ISC_FSACCESS_OWNER,
|
|
|
|
ISC_FSACCESS_READ | ISC_FSACCESS_WRITE,
|
|
|
|
&access);
|
|
|
|
(void)isc_fsaccess_set(filename, access);
|
1999-07-12 20:08:42 +00:00
|
|
|
|
2000-06-20 04:13:40 +00:00
|
|
|
/* XXXDCL return value should be checked for full filesystem */
|
1999-07-12 20:08:42 +00:00
|
|
|
fprintf(fp, "%s v%d.%d\n", PRIVATE_KEY_STR, MAJOR_VERSION,
|
|
|
|
MINOR_VERSION);
|
|
|
|
|
2000-05-15 21:02:39 +00:00
|
|
|
fprintf(fp, "%s %d ", ALGORITHM_STR, dst_key_alg(key));
|
2004-02-01 23:56:19 +00:00
|
|
|
/* XXXVIX this switch statement is too sparse to gen a jump table. */
|
2000-05-15 21:02:39 +00:00
|
|
|
switch (dst_key_alg(key)) {
|
2004-02-01 23:56:19 +00:00
|
|
|
case DST_ALG_RSAMD5:
|
|
|
|
fprintf(fp, "(RSA)\n");
|
|
|
|
break;
|
|
|
|
case DST_ALG_DH:
|
|
|
|
fprintf(fp, "(DH)\n");
|
|
|
|
break;
|
|
|
|
case DST_ALG_DSA:
|
|
|
|
fprintf(fp, "(DSA)\n");
|
|
|
|
break;
|
|
|
|
case DST_ALG_RSASHA1:
|
|
|
|
fprintf(fp, "(RSASHA1)\n");
|
|
|
|
break;
|
|
|
|
case DST_ALG_HMACMD5:
|
|
|
|
fprintf(fp, "(HMAC_MD5)\n");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(fp, "(?)\n"); break;
|
|
|
|
break;
|
1999-07-12 20:08:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < priv->nelements; i++) {
|
|
|
|
isc_buffer_t b;
|
|
|
|
isc_region_t r;
|
2000-06-01 18:26:56 +00:00
|
|
|
const char *s;
|
1999-07-12 20:08:42 +00:00
|
|
|
|
|
|
|
s = find_tag(priv->elements[i].tag);
|
|
|
|
|
|
|
|
r.base = priv->elements[i].data;
|
|
|
|
r.length = priv->elements[i].length;
|
103. [func] libisc buffer API changes for <isc/buffer.h>:
Added:
isc_buffer_base(b) (pointer)
isc_buffer_current(b) (pointer)
isc_buffer_active(b) (pointer)
isc_buffer_used(b) (pointer)
isc_buffer_length(b) (int)
isc_buffer_usedlength(b) (int)
isc_buffer_consumedlength(b) (int)
isc_buffer_remaininglength(b) (int)
isc_buffer_activelength(b) (int)
isc_buffer_availablelength(b) (int)
Removed:
ISC_BUFFER_USEDCOUNT(b)
ISC_BUFFER_AVAILABLECOUNT(b)
isc_buffer_type(b)
Changed names:
isc_buffer_used(b, r) ->
isc_buffer_usedregion(b, r)
isc_buffer_available(b, r) ->
isc_buffer_available_region(b, r)
isc_buffer_consumed(b, r) ->
isc_buffer_consumedregion(b, r)
isc_buffer_active(b, r) ->
isc_buffer_activeregion(b, r)
isc_buffer_remaining(b, r) ->
isc_buffer_remainingregion(b, r)
Buffer types were removed, so the ISC_BUFFERTYPE_*
macros are no more, and the type argument to
isc_buffer_init and isc_buffer_allocate were removed.
isc_buffer_putstr is now void (instead of isc_result_t)
and requires that the caller ensure that there
is enough available buffer space for the string.
2000-04-27 00:03:12 +00:00
|
|
|
isc_buffer_init(&b, buffer, sizeof(buffer));
|
1999-07-12 20:08:42 +00:00
|
|
|
iret = isc_base64_totext(&r, sizeof(buffer), "", &b);
|
|
|
|
if (iret != ISC_R_SUCCESS) {
|
|
|
|
fclose(fp);
|
1999-10-20 22:14:15 +00:00
|
|
|
return (DST_R_INVALIDPRIVATEKEY);
|
1999-07-12 20:08:42 +00:00
|
|
|
}
|
103. [func] libisc buffer API changes for <isc/buffer.h>:
Added:
isc_buffer_base(b) (pointer)
isc_buffer_current(b) (pointer)
isc_buffer_active(b) (pointer)
isc_buffer_used(b) (pointer)
isc_buffer_length(b) (int)
isc_buffer_usedlength(b) (int)
isc_buffer_consumedlength(b) (int)
isc_buffer_remaininglength(b) (int)
isc_buffer_activelength(b) (int)
isc_buffer_availablelength(b) (int)
Removed:
ISC_BUFFER_USEDCOUNT(b)
ISC_BUFFER_AVAILABLECOUNT(b)
isc_buffer_type(b)
Changed names:
isc_buffer_used(b, r) ->
isc_buffer_usedregion(b, r)
isc_buffer_available(b, r) ->
isc_buffer_available_region(b, r)
isc_buffer_consumed(b, r) ->
isc_buffer_consumedregion(b, r)
isc_buffer_active(b, r) ->
isc_buffer_activeregion(b, r)
isc_buffer_remaining(b, r) ->
isc_buffer_remainingregion(b, r)
Buffer types were removed, so the ISC_BUFFERTYPE_*
macros are no more, and the type argument to
isc_buffer_init and isc_buffer_allocate were removed.
isc_buffer_putstr is now void (instead of isc_result_t)
and requires that the caller ensure that there
is enough available buffer space for the string.
2000-04-27 00:03:12 +00:00
|
|
|
isc_buffer_usedregion(&b, &r);
|
1999-07-12 20:08:42 +00:00
|
|
|
|
|
|
|
fprintf(fp, "%s ", s);
|
|
|
|
fwrite(r.base, 1, r.length, fp);
|
|
|
|
fprintf(fp, "\n");
|
|
|
|
}
|
2000-08-01 01:33:37 +00:00
|
|
|
|
1999-07-12 20:08:42 +00:00
|
|
|
fclose(fp);
|
1999-10-20 22:14:15 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
1999-07-12 20:08:42 +00:00
|
|
|
}
|