/* Okay, here is a method of doing checksumming on ourselves. We calculate a checksum over the text segment from the address of main() to the end of the text segment (etext), and then encrypt it and ascii armour it (base64) for transport. Note, this is NOT foolproof, so don't rely on it for critical software! It is pretty slick though. :) The md5 checksum is xor'd with a random value, which is stored in the authentication packet. This gives relatively random input to the RSA encryption engine which encrypts the packet with the server's public key. The result is ascii armoured and sent to the server where the md5 checksum is extracted. Each time the packet is sent, it is a different ascii text, but decodes to a known checksum. This method should be resistant to cracking by snooping, spoofing, and code tampering, as long as the private key remains private. */ /* These checksum routines are activated by -DUSE_CHECKSUM */ #if defined(WIN32) || defined(__BEOS__) /* How do we get the end of the text segment with this OS? */ #undef USE_CHECKSUM #endif #ifdef USE_CHECKSUM #include #include #include #include #include #ifdef WIN32 #include #endif #include "checksum.h" #include "myerror.h" /* RSA MD5 checksum, public key routines */ #include "global.h" #include "rsaref.h" extern "C" { extern int RSAPublicEncrypt ( unsigned char *output, /* output block */ unsigned int *outputLen,/* length of output block */ unsigned char *input, /* input block */ unsigned int inputLen, /* length of input block */ R_RSA_PUBLIC_KEY *publicKey, /* RSA public key */ R_RANDOM_STRUCT *randomStruct /* random structure */ ); }; #include "public_key.h" /* Encrypt and ascii armour a message */ static char *armour_encrypt(unsigned char *buf, unsigned int len); /* Here is where we save the checksum and encrypted checksum */ #define MD5LEN 16 static unsigned char our_checksum[MD5LEN]; static unsigned char weak_encoder; /* How many times do you see this? :) */ extern "C" int main(int argc, char *argv[]); /* Call this to calculate the checksum -- first thing in main()! */ void checksum(void) { struct timeval now; /* These are the end of the text and data segments. */ extern int etext, edata; #ifdef PRINT_CHECKSUM error("Main = 0x%x, etext = 0x%x, edata = 0x%x\n",main,&etext,&edata); #endif /* Local variables */ void *mem_end=NULL; int i; MD5_CTX *ctx; /* Find the end of our code segment */ mem_end = &etext; if ( (caddr_t)mem_end < (caddr_t)main ) { // Uh oh... error("Warning: unexpected environment -- no checksum!!\n"); return; } /* Allocate and calculate our checksum */ ctx = new MD5_CTX; MD5Init(ctx); MD5Update(ctx, (unsigned char *)main, (caddr_t)mem_end-(caddr_t)main); MD5Final(our_checksum, ctx); /* ERASE THIS!! */ #ifdef PRINT_CHECKSUM error("Real checksum: "); for ( i=0; i 0; --bytesleft ) { randbyte = (rand()%256); R_RandomUpdate(&weewee, &randbyte, 1); } /* Get down to business! */ if (RSAPublicEncrypt(cbuf, &clen, tmp, len, pkey, &weewee)) { /* Uh oh... what do we do? */ error("Warning! RSA encryption failed!\n"); clen = 0; } } /* Clear out the original buffer, just in case */ for ( i=0; i