authkeys.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/*
* Copyright (c) 1996 by Sun Microsystems, Inc.
* All Rights Reserved.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* authkeys.c - routines to manage the storage of authentication keys
*/
#include <stdio.h>
#include "ntp_types.h"
#include "ntp_string.h"
#include "ntp_malloc.h"
#include "ntp_stdlib.h"
/*
* Structure to store keys in in the hash table.
*/
struct savekey {
union {
long bogon; /* Make sure union isn't empty... */
#ifdef DES
#endif
#ifdef MD5
char MD5_key[32];
#endif
} k;
#ifdef MD5
int keylen;
#endif
};
#ifdef DES
#endif
#ifdef MD5
#endif
/*
* The hash table. This is indexed by the low order bits of the
* keyid. We make this fairly big for potentially busy servers.
*/
#define HASHSIZE 64
/*
* Storage for free key structures. We malloc() such things but
* never free them.
*/
struct savekey *authfreekeys;
int authnumfreekeys;
#ifdef DES
/*
* Size of the key schedule (in u_int32s for fast DES)
*/
/*
* The zero key, which we always have. Store the permutted key
* zero in here.
*/
/*
* fast DES code expects base address aligned to u_long
*/
#endif
/*
* The key cache. We cache the last key we looked at here.
*/
#ifdef MD5
int cache_keylen;
char *cache_key;
#endif
/*
* init_auth - initialize internal data
*/
void
{
#ifdef DES
#endif
/*
* Initialize hash table and free list
*/
cache_keyid = 0;
cache_flags = 0;
authencryptions = authdecryptions = 0;
#ifdef DES
/*
* Initialize the zero key
*/
/* could just zero all */
#endif
}
/*
* auth_findkey - find a key in the hash table
*/
struct savekey *
{
while (sk != 0) {
return sk;
}
return 0;
}
/*
* auth_havekey - return whether a key is known
*/
int
{
return 1;
while (sk != 0) {
return 1;
else {
return 0;
}
}
}
return 0;
}
/*
* authhavekey - return whether a key is known. Permute and cache
* the key as a side effect.
*/
int
{
return 1;
while (sk != 0) {
break;
}
return 0;
}
#ifdef MD5
return 1;
}
#endif
#ifdef DES
return 1;
}
#endif
return 0;
}
/*
* auth_moremem - get some more free key structures
*/
int
{
register int i;
if (sk == 0)
return 0;
for (i = MEMINC; i > 0; i--) {
authfreekeys = sk++;
}
return authnumfreekeys;
}
/*
*/
void
int trust;
{
while (sk != 0) {
break;
}
return;
if (sk != 0) {
if (cache_keyid == keyno) {
cache_flags = 0;
cache_keyid = 0;
}
if (trust) {
return;
}
} else {
}
authnumkeys--;
authfreekeys = sk;
}
return;
}
if (authnumfreekeys == 0)
if (auth_moremem() == 0)
return;
sk = authfreekeys;
authnumkeys++;
return;
}
/*
* authistrusted - determine whether a key is trusted
*/
int
{
if (keyno == cache_keyid)
return ((cache_flags & KEY_TRUSTED) != 0);
while (sk != 0) {
break;
}
return 0;
return 1;
}
#ifdef DES
/*
* DESauth_setkey - set a key into the key array
*/
void
{
/*
* See if we already have the key. If so just stick in the
* new value.
*/
while (sk != 0) {
if (cache_keyid == keyno)
cache_flags = 0;
cache_keyid = 0;
return;
}
}
/*
* Need to allocate new structure. Do it.
*/
if (authnumfreekeys == 0) {
if (auth_moremem() == 0)
return;
}
sk = authfreekeys;
authnumkeys++;
return;
}
#endif
#ifdef MD5
void
{
/*
* See if we already have the key. If so just stick in the
* new value.
*/
while (sk != 0) {
if (cache_keyid == keyno) {
cache_flags = 0;
cache_keyid = 0;
}
return;
}
}
/*
* Need to allocate new structure. Do it.
*/
if (authnumfreekeys == 0) {
if (auth_moremem() == 0)
return;
}
sk = authfreekeys;
authnumkeys++;
return;
}
#endif
/*
* auth_delkeys - delete all known keys, in preparation for rereading
* the keys file (presumably)
*/
void
{
register int i;
for (i = 0; i < HASHSIZE; i++) {
while (sk != 0) {
#ifdef MD5
| KEY_MD5
#endif
#ifdef DES
| KEY_DES
#endif
);
authnumkeys--;
authfreekeys = sk;
} else {
}
}
}
}
/*
* auth1crypt - support for two stage encryption, part 1.
*/
void
int length; /* length of all encrypted data */
{
if (!authhavekey(keyno)) {
authnokey++;
return;
}
}
#ifdef DES
return;
}
#endif
#ifdef MD5
if (cache_flags & KEY_MD5) {
return;
}
#endif
}
/*
* auth2crypt - support for two stage encryption, part 2.
*/
int
int length; /* total length of encrypted area */
{
if (!authhavekey(keyno)) {
authnokey++;
return 0;
}
}
#ifdef DES
#endif
#ifdef MD5
if (cache_flags & KEY_MD5)
#endif
return 0;
}
int
int length; /* length of encrypted portion of packet */
{
if (!authhavekey(keyno)) {
authnokey++;
return 0;
}
}
#ifdef DES
#endif
#ifdef MD5
if (cache_flags & KEY_MD5)
#endif
return 0;
}
int
int length; /* length of variable data in octets */
{
if (!authhavekey(keyno)) {
authnokey++;
return 0;
}
}
#ifdef DES
#endif
#ifdef MD5
if (cache_flags & KEY_MD5)
#endif
return 0;
}