Key.c revision 6fc0178d
144dda7b2Smrg/* 244dda7b2SmrgCopyright 1989, 1998 The Open Group 344dda7b2Smrg 444dda7b2SmrgPermission to use, copy, modify, distribute, and sell this software and its 544dda7b2Smrgdocumentation for any purpose is hereby granted without fee, provided that 644dda7b2Smrgthe above copyright notice appear in all copies and that both that 744dda7b2Smrgcopyright notice and this permission notice appear in supporting 844dda7b2Smrgdocumentation. 944dda7b2Smrg 1044dda7b2SmrgThe above copyright notice and this permission notice shall be included in 1144dda7b2Smrgall copies or substantial portions of the Software. 1244dda7b2Smrg 1344dda7b2SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1444dda7b2SmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1544dda7b2SmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 1644dda7b2SmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 1744dda7b2SmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 1844dda7b2SmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 1944dda7b2Smrg 2044dda7b2SmrgExcept as contained in this notice, the name of The Open Group shall not be 2144dda7b2Smrgused in advertising or otherwise to promote the sale, use or other dealings 2244dda7b2Smrgin this Software without prior written authorization from The Open Group. 2344dda7b2Smrg * 2444dda7b2Smrg * Author: Keith Packard, MIT X Consortium 2544dda7b2Smrg */ 2644dda7b2Smrg 2744dda7b2Smrg#ifdef HAVE_CONFIG_H 2844dda7b2Smrg#include <config.h> 2944dda7b2Smrg#endif 3044dda7b2Smrg#include <X11/Xos.h> 3144dda7b2Smrg#include <X11/X.h> 3244dda7b2Smrg#include <X11/Xmd.h> 3344dda7b2Smrg#include <X11/Xdmcp.h> 3444dda7b2Smrg 35fc544a13Smrg#ifdef HAVE_LIBBSD 36fc544a13Smrg#include <bsd/stdlib.h> /* for arc4random_buf() */ 37fc544a13Smrg#endif 38fc544a13Smrg 39fc544a13Smrg#ifndef HAVE_ARC4RANDOM_BUF 4044dda7b2Smrgstatic void 4144dda7b2Smrggetbits (long data, unsigned char *dst) 4244dda7b2Smrg{ 4344dda7b2Smrg dst[0] = (data ) & 0xff; 4444dda7b2Smrg dst[1] = (data >> 8) & 0xff; 4544dda7b2Smrg dst[2] = (data >> 16) & 0xff; 4644dda7b2Smrg dst[3] = (data >> 24) & 0xff; 4744dda7b2Smrg} 48fc544a13Smrg#endif 4944dda7b2Smrg 5044dda7b2Smrg#define Time_t time_t 5144dda7b2Smrg 5244dda7b2Smrg#include <stdlib.h> 5344dda7b2Smrg 5444dda7b2Smrg#if defined(HAVE_LRAND48) && defined(HAVE_SRAND48) 5544dda7b2Smrg#define srandom srand48 5644dda7b2Smrg#define random lrand48 5744dda7b2Smrg#endif 5844dda7b2Smrg#ifdef WIN32 5944dda7b2Smrg#include <process.h> 6044dda7b2Smrg#define srandom srand 6144dda7b2Smrg#define random rand 6244dda7b2Smrg#define getpid(x) _getpid(x) 6344dda7b2Smrg#endif 6444dda7b2Smrg 65fc544a13Smrg#ifndef HAVE_ARC4RANDOM_BUF 666fc0178dSmrg 676fc0178dSmrgstatic void 686fc0178dSmrginsecure_getrandom_buf (unsigned char *auth, int len) 696fc0178dSmrg{ 7044dda7b2Smrg long lowbits, highbits; 7144dda7b2Smrg 7244dda7b2Smrg srandom ((int)getpid() ^ time((Time_t *)0)); 7344dda7b2Smrg lowbits = random (); 7444dda7b2Smrg highbits = random (); 756fc0178dSmrg getbits (lowbits, auth); 766fc0178dSmrg getbits (highbits, auth + 4); 776fc0178dSmrg} 786fc0178dSmrg 796fc0178dSmrgstatic void 806fc0178dSmrgarc4random_buf (void *auth, int len) 816fc0178dSmrg{ 826fc0178dSmrg int ret; 836fc0178dSmrg 846fc0178dSmrg#if HAVE_GETENTROPY 856fc0178dSmrg /* weak emulation of arc4random through the getentropy libc call */ 866fc0178dSmrg ret = getentropy (auth, len); 876fc0178dSmrg if (ret == 0) 886fc0178dSmrg return; 896fc0178dSmrg#endif /* HAVE_GETENTROPY */ 906fc0178dSmrg 916fc0178dSmrg insecure_getrandom_buf (auth, len); 926fc0178dSmrg} 936fc0178dSmrg 946fc0178dSmrg#endif /* !defined(HAVE_ARC4RANDOM_BUF) */ 956fc0178dSmrg 966fc0178dSmrgvoid 976fc0178dSmrgXdmcpGenerateKey (XdmAuthKeyPtr key) 986fc0178dSmrg{ 99fc544a13Smrg arc4random_buf(key->data, 8); 10044dda7b2Smrg} 10144dda7b2Smrg 10244dda7b2Smrgint 10344dda7b2SmrgXdmcpCompareKeys (const XdmAuthKeyPtr a, const XdmAuthKeyPtr b) 10444dda7b2Smrg{ 10544dda7b2Smrg int i; 10644dda7b2Smrg 10744dda7b2Smrg for (i = 0; i < 8; i++) 10844dda7b2Smrg if (a->data[i] != b->data[i]) 10944dda7b2Smrg return FALSE; 11044dda7b2Smrg return TRUE; 11144dda7b2Smrg} 11244dda7b2Smrg 11344dda7b2Smrgvoid 11444dda7b2SmrgXdmcpIncrementKey (XdmAuthKeyPtr key) 11544dda7b2Smrg{ 11644dda7b2Smrg int i; 11744dda7b2Smrg 11844dda7b2Smrg i = 7; 11944dda7b2Smrg while (++key->data[i] == 0) 12044dda7b2Smrg if (--i < 0) 12144dda7b2Smrg break; 12244dda7b2Smrg} 12344dda7b2Smrg 12444dda7b2Smrgvoid 12544dda7b2SmrgXdmcpDecrementKey (XdmAuthKeyPtr key) 12644dda7b2Smrg{ 12744dda7b2Smrg int i; 12844dda7b2Smrg 12944dda7b2Smrg i = 7; 13044dda7b2Smrg while (key->data[i]-- == 0) 13144dda7b2Smrg if (--i < 0) 13244dda7b2Smrg break; 13344dda7b2Smrg} 134