dst_api.c revision 3ddd814a97de1d152ba0913c592d6e6dc83d38a6
/*
* Portions Copyright (c) 1995-1999 by Network Associates, Inc.
*
* Permission to use, copy modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND NETWORK ASSOCIATES
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
* NETWORK ASSOCIATES 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 THE SOFTWARE.
*/
/*
* Principal Author: Brian Wellington
* $Id: dst_api.c,v 1.20 1999/12/23 00:09:04 explorer Exp $
*/
#include <config.h>
#include <ctype.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <isc/assertions.h>
#include <dns/keyvalues.h>
#include "dst_internal.h"
static isc_mutex_t random_lock;
/* Static functions */
static void initialize(void);
/*
* dst_supported_algorithm
* This function determines if the crypto system for the specified
* algorithm is present.
* Parameters
* alg The algorithm to test
* Returns
* ISC_TRUE The algorithm is available.
* ISC_FALSE The algorithm is not available.
*/
dst_supported_algorithm(const int alg) {
return (ISC_FALSE);
return (ISC_TRUE);
}
/*
* dst_sign
* An incremental signing function. Data is signed in steps.
* First the context must be initialized (DST_SIGMODE_INIT).
* Then data is hashed (DST_SIGMODE_UPDATE). Finally the signature
* itself is created (DST_SIGMODE_FINAL). This function can be called
* once with DST_SIGMODE_ALL set, or it can be called separately
* for each step. The UPDATE step may be repeated.
* Parameters
* mode A bit mask specifying operation(s) to be performed.
* DST_SIGMODE_INIT Initialize digest
* DST_SIGMODE_UPDATE Add data to digest
* DST_SIGMODE_FINAL Generate signature
* DST_SIGMODE_ALL Perform all operations
* key The private key used to sign the data
* context The state of the operation
* data The data to be signed.
* sig The buffer to which the signature will be written.
* Return
* ISC_R_SUCCESS Success
* !ISC_R_SUCCESS Failure
*/
{
if ((mode & DST_SIGMODE_UPDATE) != 0)
if ((mode & DST_SIGMODE_FINAL) != 0)
return (DST_R_UNSUPPORTEDALG);
return (DST_R_NULLKEY);
return (DST_R_NOTPRIVATEKEY);
}
/*
* dst_verify
* An incremental verify function. Data is verified in steps.
* First the context must be initialized (DST_SIGMODE_INIT).
* Then data is hashed (DST_SIGMODE_UPDATE). Finally the signature
* is verified (DST_SIGMODE_FINAL). This function can be called
* once with DST_SIGMODE_ALL set, or it can be called separately
* for each step. The UPDATE step may be repeated.
* Parameters
* mode A bit mask specifying operation(s) to be performed.
* DST_SIGMODE_INIT Initialize digest
* DST_SIGMODE_UPDATE Add data to digest
* DST_SIGMODE_FINAL Verify signature
* DST_SIGMODE_ALL Perform all operations
* key The public key used to verify the signature.
* context The state of the operation
* data The data to be digested.
* sig The signature.
* Returns
* ISC_R_SUCCESS Success
* !ISC_R_SUCCESS Failure
*/
{
if ((mode & DST_SIGMODE_UPDATE) != 0)
if ((mode & DST_SIGMODE_FINAL) != 0)
return (DST_R_UNSUPPORTEDALG);
return (DST_R_NULLKEY);
return (DST_R_NOTPUBLICKEY);
}
/*
* dst_digest
* An incremental digest function. Data is digested in steps.
* First the context must be initialized (DST_SIGMODE_INIT).
* Then data is hashed (DST_SIGMODE_UPDATE). Finally the digest
* is generated (DST_SIGMODE_FINAL). This function can be called
* once with DST_SIGMODE_ALL set, or it can be called separately
* for each step. The UPDATE step may be repeated.
* Parameters
* mode A bit mask specifying operation(s) to be performed.
* DST_SIGMODE_INIT Initialize digest
* DST_SIGMODE_UPDATE Add data to digest
* DST_SIGMODE_FINAL Complete digest
* DST_SIGMODE_ALL Perform all operations
* alg The digest algorithm to use
* context The state of the operation
* data The data to be digested.
* sig The sdigest.
* Returns
* ISC_R_SUCCESS Success
* !ISC_R_SUCCESS Failure
*/
{
if ((mode & DST_SIGMODE_UPDATE) != 0)
if ((mode & DST_SIGMODE_FINAL) != 0)
if (alg != DST_DIGEST_MD5)
return (DST_R_UNSUPPORTEDALG);
}
/*
* dst_computesecret
* A function to compute a shared secret from two (Diffie-Hellman) keys.
* Parameters
* pub The public key
* priv The private key
* secret A buffer into which the secret is written
* Returns
* ISC_R_SUCCESS Success
* !ISC_R_SUCCESS Failure
*/
{
return (DST_R_UNSUPPORTEDALG);
return (DST_R_NULLKEY);
return (DST_R_KEYCANNOTCOMPUTESECRET);
return (DST_R_NOTPRIVATEKEY);
}
/*
* dst_key_tofile
* Writes a key to disk. The key can either be a public or private key.
* The public key is written in DNS format and the private key is
* written as a set of base64 encoded values.
* Parameters
* key The key to be written.
* type Either DST_PUBLIC or DST_PRIVATE, or both
* Returns
* ISC_R_SUCCESS Success
* !ISC_R_SUCCESS Failure
*/
int ret = ISC_R_SUCCESS;
return (DST_R_UNSUPPORTEDALG);
return (DST_R_UNSUPPORTEDTYPE);
if (type & DST_TYPE_PUBLIC)
return (ret);
if ((type & DST_TYPE_PRIVATE) &&
{
if (ret != ISC_R_SUCCESS)
return (ret);
}
return (ret);
}
/*
* dst_key_fromfile
* Reads a key from disk. The key can either be a public or private
* key, and is specified by name, algorithm, and id.
* Parameters
* name The key name.
* id The id of the key.
* alg The algorithm of the key.
* type Either DST_PUBLIC or DST_PRIVATE
* mctx Memory context used to allocate key structure
* keyp Returns the new key
* Returns
* ISC_R_SUCCESS Success
* !ISC_R_SUCCESS Failure
*/
{
return (DST_R_UNSUPPORTEDALG);
return (DST_R_UNSUPPORTEDTYPE);
else if (ret != ISC_R_SUCCESS)
return (ret);
else {
if (type == DST_TYPE_PUBLIC ||
{
return (ISC_R_SUCCESS);
}
}
return (ISC_R_NOMEMORY);
/* Fill in private key and some fields in the general key structure */
if (ret != ISC_R_SUCCESS) {
return (ret);
}
return (ISC_R_SUCCESS);
}
/*
* dst_key_todns
* Function to encode a public key into DNS KEY format
* Parameters
* key Key structure to encode.
* target Buffer to write the encoded key into.
* Returns
* ISC_R_SUCCESS Success
* !ISC_R_SUCCESS Failure
*/
isc_region_t r;
return (DST_R_UNSUPPORTEDALG);
isc_buffer_available(target, &r);
if (r.length < 4)
return (ISC_R_NOSPACE);
isc_buffer_available(target, &r);
if (r.length < 2)
return (ISC_R_NOSPACE);
& 0xffff));
}
return (ISC_R_SUCCESS);
}
/*
* dst_key_fromdns
* This function converts the contents of a DNS KEY RR into a key
* Paramters
* name Name of the new key
* source A buffer containing the KEY RR
* mctx The memory context used to allocate the key
* keyp Returns the new key
* Returns
* ISC_R_SUCCESS Success
* !ISC_R_SUCCESS Failure
*/
{
isc_region_t r;
isc_buffer_remaining(source, &r);
return (DST_R_INVALIDPUBLICKEY);
if (!dst_supported_algorithm(alg))
return (DST_R_UNSUPPORTEDALG);
if (flags & DNS_KEYFLAG_EXTENDED) {
isc_buffer_remaining(source, &r);
if (r.length < 2)
return (DST_R_INVALIDPUBLICKEY);
}
return (ISC_R_NOMEMORY);
if (ret != ISC_R_SUCCESS)
dst_key_free((*keyp));
return (ret);
}
/*
* dst_key_frombuffer
* Function to convert raw data into a public key. The raw data format
* is basically DNS KEY rdata format.
* Parameters
* name The key name
* alg The algorithm
* flags The key's flags
* protocol The key's protocol
* source A buffer containing the key
* mctx The memory context used to allocate the key
* keyp Returns the new key
* Returns
* ISC_R_SUCCESS Success
* !ISC_R_SUCCESS Failure
*/
{
return (DST_R_UNSUPPORTEDALG);
return (ISC_R_NOMEMORY);
if (ret != ISC_R_SUCCESS) {
dst_key_free((*keyp));
return (ret);
}
return (ISC_R_SUCCESS);
}
/*
* dst_key_tobuffer
* Function to convert a public key into raw data. The raw data format
* is basically DNS KEY rdata format.
* Parameters
* key The key
* target The buffer to be written into.
* Returns
* ISC_R_SUCCESS Success
* !ISC_R_SUCCESS Failure
*/
return (DST_R_UNSUPPORTEDALG);
}
/*
* dst_key_generate
* Parameters
* name Name of the new key. Used to create key files
* K<name>+<alg>+<id>.public
* K<name>+<alg>+<id>.private
* alg The algorithm to use
* bits Size of the new key in bits
* param Algorithm specific
* RSA: exponent
* 0 use exponent 3
* !0 use Fermat4 (2^16 + 1)
* DH: generator
* 0 default - use well-known prime if bits == 768
* or 1024, otherwise use generator 2
* !0 use this value as the generator
* flags The default value of the DNS Key flags.
* protocol Default value of the DNS Key protocol field.
* mctx The memory context used to allocate the key
* keyp Returns the new key
*
* Return
* ISC_R_SUCCESS Success
* !ISC_R_SUCCESS Failure
*/
{
return (DST_R_UNSUPPORTEDALG);
return (ISC_R_NOMEMORY);
if (bits == 0) { /* NULL KEY */
return (ISC_R_SUCCESS);
}
if (ret != ISC_R_SUCCESS) {
dst_key_free(*keyp);
return (ret);
}
return (ISC_R_SUCCESS);
}
/*
* dst_key_compare
* Compares two keys for equality.
* Parameters
* key1, key2 Two keys to be compared.
* Returns
* ISC_TRUE The keys are equal.
* ISC_FALSE The keys are not equal.
*/
return (ISC_TRUE);
return (ISC_FALSE);
return (ISC_TRUE);
else
return (ISC_FALSE);
}
/*
* dst_key_paramcompare
* Compares two keys' parameters for equality. This is designed to
* determine if two (Diffie-Hellman) keys can be used to derive a shared
* secret.
* Parameters
* key1, key2 Two keys whose parameters are to be compared.
* Returns
* ISC_TRUE The keys' parameters are equal.
* ISC_FALSE The keys' parameters are not equal.
*/
return (ISC_TRUE);
return (ISC_FALSE);
return (ISC_TRUE);
else
return (ISC_FALSE);
}
/*
* dst_key_free
* Release all data structures pointed to by a key structure.
* Parameters
* key Key structure to be freed.
*/
void
}
char *
}
int
}
int
}
int
}
}
}
}
/*
* dst_sig_size
* Computes the maximum size of a signature generated by the given key
* Parameters
* key The DST key
* n Stores the number of bytes necessary to hold a signature
* with the key.
* Returns
* ISC_R_SUCCESS
* DST_R_UNSUPPORTEDALG
*/
case DST_ALG_RSA:
break;
case DST_ALG_DSA:
*n = DNS_SIG_DSASIGSIZE;
break;
case DST_ALG_HMACMD5:
*n = 16;
break;
case DST_ALG_HMACSHA1:
*n = 20;
break;
case DST_ALG_DH:
default:
return (DST_R_UNSUPPORTEDALG);
}
return (ISC_R_SUCCESS);
}
/*
* dst_secret_size
* Computes the maximum size of a shared secret generated by the given key
* Parameters
* key The DST key
* n Stores the number of bytes necessary to hold a shared secret
* generated by the key.
* Returns
* ISC_R_SUCCESS
* DST_R_UNSUPPORTEDALG
*/
case DST_ALG_DH:
break;
case DST_ALG_RSA:
case DST_ALG_DSA:
case DST_ALG_HMACMD5:
case DST_ALG_HMACSHA1:
default:
return (DST_R_UNSUPPORTEDALG);
}
return (ISC_R_SUCCESS);
}
/*
* dst_random_get
* a random number generator that can generate different levels of
* randomness
* Parameters
* mode selects the random number generator
* wanted the number of random bytes requested
* target the buffer to store the random data
* Returns
* ISC_R_SUCCESS Success
* !ISC_R_SUCCESS Failure
*/
isc_region_t r;
isc_buffer_available(target, &r);
return (ISC_R_NOSPACE);
return (ISC_R_SUCCESS);
}
/***
*** Static methods
***/
/*
* initialize
* This function initializes the Digital Signature Toolkit.
* Parameters
* none
* Returns
* none
*/
static void
initialize() {
#endif
#ifdef OPENSSL
#endif
}
/*
* get_key_struct
* This function allocates key structure and fills in some of the
* fields of the structure.
* Parameters:
* name the name of the key
* alg the algorithm number
* flags the dns flags of the key
* protocol the dns protocol of the key
* bits the size of the key
* mctx the memory context to allocate from
* Returns:
* NULL error
* valid pointer otherwise
*/
static dst_key_t *
{
return (NULL);
return (NULL);
}
}
else {
return (NULL);
}
}
return (key);
}
/*
* dst_read_public_key
* Read a public key from disk
* Parameters
* name The name
* id The id
* alg The algorithm
* mctx The memory context used to allocate the key
* keyp Returns the new key
* Returns
* ISC_R_SUCCESS Success
* !ISC_R_SUCCESS Failure
*/
static dst_result_t
{
char filename[ISC_DIR_NAMEMAX];
isc_buffer_t b;
unsigned int opt = ISC_LEXOPT_DNSMULTILINE;
sizeof(filename)) != ISC_R_SUCCESS)
return (DST_R_NAMETOOLONG);
/*
* Open the file and read its formatted contents
* File format:
* domain.name [ttl] [IN] KEY <flags> <protocol> <algorithm> <key>
*/
/* 1500 should be large enough for any key */
if (ret != ISC_R_SUCCESS)
return (ISC_R_NOMEMORY);
if (ret != ISC_R_SUCCESS) {
if (ret == ISC_R_FAILURE)
goto cleanup;
}
if (ret != ISC_R_SUCCESS) \
goto cleanup; \
}
/* Read the domain name */
/* Read the next word: either TTL, 'IN', or 'KEY' */
/* If it's a TTL, read the next one */
goto cleanup;
goto cleanup;
goto cleanup;
if (ret != ISC_R_SUCCESS)
goto cleanup;
goto cleanup;
return (ISC_R_SUCCESS);
}
return (ret);
}
/*
* write_public_key
* Write a key to disk in DNS format.
* Parameters
* key A DST key
* Returns
* ISC_R_SUCCESS Success
* !ISC_R_SUCCESS Failure
*/
static dst_result_t
isc_region_t r;
char filename[ISC_DIR_NAMEMAX];
unsigned char key_array[DST_KEY_MAXSIZE];
char text_array[DST_KEY_MAXSIZE];
if (ret != ISC_R_SUCCESS)
return (ret);
isc_buffer_used(&keyb, &r);
if (dnsret != ISC_R_SUCCESS)
return (DST_R_INVALIDPUBLICKEY);
isc_buffer_used(&textb, &r);
/*
* Make the filename.
*/
PUBLIC_KEY, sizeof(filename)) < 0)
return (DST_R_NAMETOOLONG);
/*
* Create public key file.
*/
return (DST_R_WRITEERROR);
return (ISC_R_SUCCESS);
}
void *
}
void
dst_mem_free(void *ptr) {
}
void *
void *p;
p = NULL;
if (size > 0) {
p = dst_mem_alloc(size);
}
return (p);
}