+++ /dev/null
-/* -*-c-*-
- *
- * $Id: crypt.c,v 1.1 1997/07/21 13:47:51 mdw Exp $
- *
- * Cryptographic transfer of `become' requests
- *
- * (c) 1997 EBI
- */
-
-/*----- Licencing notice --------------------------------------------------*
- *
- * This file is part of `become'
- *
- * `Become' is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * `Become' is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with `become'; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- */
-
-/*----- Revision history --------------------------------------------------*
- *
- * $Log: crypt.c,v $
- * Revision 1.1 1997/07/21 13:47:51 mdw
- * Initial revision
- *
- */
-
-/*----- Header files ------------------------------------------------------*/
-
-/* --- ANSI headers --- */
-
-#include <ctype.h>
-#include <errno.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <time.h>
-
-/* --- Unix headers --- */
-
-#include <sys/types.h>
-#include <unistd.h>
-#include <syslog.h>
-#include <fcntl.h>
-
-/* --- Local headers --- */
-
-#include "config.h"
-#include "crypt.h"
-#include "icrypt.h"
-#include "idea.h"
-#include "md5.h"
-#include "tx.h"
-#include "utils.h"
-
-/*----- Magic numbers -----------------------------------------------------*/
-
-#define crypt__timeError 15 /* Seconds error to permit */
-#define crypt__seedBits 512 /* Number of random seed bits */
-
-/*----- Dump a block of data ----------------------------------------------*/
-
-/* --- @crypt__dump@ --- *
- *
- * Arguments: @char *p@ = a string to display at the top of the message
- * @unsigned char *buf@ = pointer to a buffer to display
- * @size_t sz@ = size of the buffer
- * @FILE *fp@ = file to dump on
- *
- * Returns: --
- *
- * Use: Dumps a block of data to the terminal. This allows a
- * programmer to examine the traffic between client and server,
- * and possibly locate bugs.
- */
-
-#ifndef NDEBUG
-
-static void crypt__dump(const char *p, const unsigned char *buf,
- int sz, FILE *fp)
-{
- int i;
- fprintf(stderr,"+++ %s\n",p);
- while (sz>0)
- {
- fprintf(stderr,"+++ ");
- for (i=0;i<8;i++)
- {
- if (i<sz)
- fprintf(stderr,"%02x ",buf[i] & 0xff);
- else
- fputs("** ",stderr);
- }
- fputs(": ",stderr);
- for (i=0;i<8;i++)
- putc(i<sz ? (isprint(buf[i]) ? buf[i] : '.') : '*',stderr);
- putc('\n',stderr);
- buf+=8;
- sz-=8;
- }
- putc('\n',stderr);
-}
-
-#else
-
-#define crypt__dump(p, buf, sz, fp) ((void)0)
-
-#endif
-
-/*----- Main code ---------------------------------------------------------*/
-
-/* --- @crypt__sessionKey@ --- *
- *
- * Arguments: @const char *seedfile@ = pointer to name of seed file
- * @unsigned char *k@ = our secret key
- * @time_t t@ = the current time
- * @pid_t pid@ = our process id
- * @unsigned *sk@ = where to store the session key
- * @unsigned char *iv@ = where to store the IV
- *
- * Returns: ---
- *
- * Use: Decides on a random session key and initialisation vector.
- */
-
-static void crypt__sessionKey(const char *seedfile, unsigned char *k,
- time_t t, pid_t pid,
- unsigned char *sk, unsigned char *iv)
-{
- FILE *fp; /* File handle for reading */
- unsigned char s[crypt__seedBits / 8]; /* Random seed block */
- unsigned char b[4 + 4];
- /* For building the hash block */
-
- /* --- Read the old seed in --- *
- *
- * Interlock with other processes locking this file.
- */
-
- {
-#ifndef TEST_RIG
- struct flock l;
-#endif
-
- if ((fp = fopen(seedfile, "r+")) == 0)
- die("couldn't open random seed file `%s': %s",
- seedfile, strerror(errno));
-
-#ifndef TEST_RIG
- l.l_type = F_WRLCK;
- l.l_whence = SEEK_SET;
- l.l_start = 0;
- l.l_len = 0;
- if (fcntl(fileno(fp), F_SETLKW, &l) < 0)
- die("couldn't lock random number file: %s", strerror(errno));
-#endif
-
- tx_getBits(s, crypt__seedBits, fp);
- }
-
- crypt__dump("Initial seed", s, crypt__seedBits / 8, stdout);
-
- /* --- Build a block of data to hash --- */
-
- store32(b + 0, t);
- store32(b + 4, pid);
-
- /* --- Work out the session key --- */
-
- {
- md5 md;
-
- md5_init(&md);
- md5_buffer(&md, s, sizeof(s));
- md5_buffer(&md, b, sizeof(b));
- md5_buffer(&md, k, IDEA_KEYSIZE);
- md5_final(&md, sk);
-
- crypt__dump("Session key", sk, 128 / 8, stdout);
- burn(md);
- }
-
- /* --- Build the new seed and write it back again --- */
-
- if (crypt__seedBits > 128) {
- unsigned char tb[MD5_HASHSIZE];
- int i;
-
- memcpy(tb, s + crypt__seedBits / 8 - sizeof(tb), sizeof(tb));
- memmove(s + sizeof(tb), s, crypt__seedBits / 8 - sizeof(tb));
- memcpy(s, tb, sizeof(tb));
- for (i = 0; i < sizeof(sk); i++)
- s[i] ^= sk[i];
- burn(tb);
- }
-
- /* --- Take the seed we have and hash it again to get an IV --- */
-
- {
- unsigned char mdv[MD5_HASHSIZE];
- md5 md;
-
- md5_init(&md);
- md5_buffer(&md, b, sizeof(b));
- md5_buffer(&md, k, IDEA_KEYSIZE);
- md5_buffer(&md, s, sizeof(s));
- md5_final(&md, mdv);
- memcpy(iv, mdv, IDEA_BLKSIZE);
- crypt__dump("IV", iv, IDEA_BLKSIZE, stdout);
- burn(md); burn(mdv);
- }
-
-
- /* --- Lock the file again --- *
- *
- * We're closing the file after we've finished, so we don't need to
- * unlock it afterwards.
- */
-
- crypt__dump("Final seed", s, crypt__seedBits / 8, stdout);
-
- rewind(fp);
- tx_putBits(s, crypt__seedBits, fp);
- fclose(fp);
-
- /* --- Destroy sensitive data --- */
-
- burn(b); burn(s);
-}
-
-/* --- @crypt_packRequest@ --- *
- *
- * Arguments: @request *rq@ = pointer to request block
- * @unsigned char *buff@ = pointer to a buffer
- * @time_t t@ = the current time
- * @pid_t pid@ = my process ID
- * @unsigned char *k@ = pointer to 128-bit key
- * @unsigned char *sk@ = where to put the session key
- *
- * Returns: ---
- *
- * Use: Packs a request block into a buffer. The buffer should have
- * space for at least @crq_size@ bytes. The buffer comes back
- * encrypted and ready to send.
- */
-
-void crypt_packRequest(request *rq, unsigned char *buff,
- time_t t, pid_t pid,
- unsigned char *k, unsigned char *sk)
-{
- /* --- First, build the easy stuff in the block --- */
-
- buff[crq_cryptType] = cryptType_idea;
- store32(buff + crq_time, t);
- store32(buff + crq_pid, pid);
- store32(buff + crq_from, rq->from);
- store32(buff + crq_to, rq->to);
-
- /* --- The string causes a few problems --- *
- *
- * There's a good chance that the string will be a good deal shorter than
- * the space allowed for it. This will probably mean lots of zeroes, and a
- * very easy known-plaintext job for a potential attacker. (An early
- * version of this code used @strncpy@ which is even worse!)
- *
- * I'll fill the block with random (from @rand@(3) -- nothing too
- * elaborate) and then encrypt it using IDEA in CFB mode, using the first
- * few bytes as the key. This should provide a sufficiently unpredictable
- * background for the block.
- */
-
- {
- icrypt_job j;
- unsigned char *p;
- unsigned u;
-
- /* --- Initialise the buffer with junk --- */
-
- srand((unsigned int)(t ^ pid)); /* Seed the (bad) RNG */
- for (p = buff + crq_cmd; p < buff + crq_cmd + CMDLEN_MAX; p++) {
- u = rand();
- *p = u ^ (u >> 8);
- }
-
- /* --- Now make the junk a whole lot harder to predict --- */
-
- p = buff + crq_cmd;
- icrypt_init(&j, p, 0);
- icrypt_encrypt(&j, p, p, CMDLEN_MAX);
- burn(j);
-
- /* --- Copy the string into here --- */
-
- strcpy((char *)buff + crq_cmd, rq->cmd);
- }
-
- /* --- Generate a session key --- */
-
- {
- crypt__sessionKey(file_RANDSEED, k, t, pid, sk, buff + crq_iv);
- memcpy(buff + crq_session, sk, IDEA_KEYSIZE);
- }
-
- /* --- Checksum the finished data --- */
-
- {
- md5 md;
- unsigned char mdv[MD5_HASHSIZE];
-
- md5_init(&md);
- md5_buffer(&md, buff + crq_cipher, crq_check - crq_cipher);
- md5_final(&md, mdv);
- memcpy(buff + crq_check, mdv, 4);
- burn(md); burn(mdv);
- }
-
- /* --- Encrypt the block --- *
- *
- * First, encrypt the session key using the master key. Since the session
- * key is effectively random, this makes cracking the master key much
- * harder. The rest of the block is then encrypted with the session key,
- * using the IV left over from encrypting the session key.
- */
-
- {
- icrypt_job j;
-
- crypt__dump("request, before encryption", buff, crq_size, stdout);
-
- icrypt_init(&j, k, buff + crq_iv);
- icrypt_encrypt(&j, buff + crq_session, buff + crq_session, IDEA_KEYSIZE);
- icrypt_reset(&j, sk, 0);
- icrypt_encrypt(&j, buff + crq_cipher,
- buff + crq_cipher, crq_size - crq_cipher);
- burn(j);
-
- crypt__dump("request, after encryption", buff, crq_size, stdout);
- }
-}
-
-/* --- @crypt_unpackRequest@ --- *
- *
- * Arguments: @reqest *rq@ = pointer to destination request block
- * @unsigned char *buff@ = pointer to source buffer
- * @unsigned char *k@ = pointer to encryption key
- * @unsigned char *sk@ = pointer to where to store session key
- * @unsigned char *rpl@ = where to start building reply
- *
- * Returns: Nonzero if it was decrypted OK
- *
- * Use: Decrypts and unpacks a request buffer.
- */
-
-int crypt_unpackRequest(request *rq, unsigned char *buff,
- unsigned char *k, unsigned char *sk,
- unsigned char *rpl)
-{
- {
- /* --- Check the encryption format --- */
-
- if (buff[crq_cryptType] != cryptType_idea)
- return (0);
- }
-
- {
- /* --- First things first: decrypt the block --- */
-
- icrypt_job j;
-
- crypt__dump("request, before decryption", buff, crq_size, stdout);
-
- icrypt_init(&j, k, buff + crq_iv);
- icrypt_decrypt(&j, buff + crq_session, buff + crq_session, IDEA_KEYSIZE);
- memcpy(sk, buff + crq_session, IDEA_KEYSIZE);
- icrypt_reset(&j, sk, 0);
- icrypt_decrypt(&j, buff + crq_cipher,
- buff + crq_cipher, crq_size - crq_cipher);
- icrypt_saveIV(&j, rpl + crp_iv);
-
- memset(buff + crq_session, 0, IDEA_KEYSIZE); /* Burn, baby, burn */
- burn(j);
-
- crypt__dump("request, after decryption", buff, crq_size, stdout);
- }
-
- {
- /* --- Check the validity of the data therein --- */
-
- md5 md;
- unsigned char mdv[MD5_HASHSIZE];
-
- md5_init(&md);
- md5_buffer(&md, buff + crq_cipher, crq_check - crq_cipher);
- md5_final(&md, mdv);
- if (memcmp(mdv, buff + crq_check, 4) != 0) {
- syslog(LOG_INFO, "packet rejected: bad checksum");
- return (0);
- }
- burn(md); burn(mdv);
- }
-
- {
- /* --- Extract fields from the block --- */
-
- rq->from = load32(buff + crq_from);
- rq->to = load32(buff + crq_to);
- memcpy(rq->cmd, buff + crq_cmd, CMDLEN_MAX);
- }
-
- {
- /* --- Fill in bits of the reply block --- */
-
- long t = (long)time(0);
- long u = (long)load32(buff + crq_time);
-
- if (t - u > crypt__timeError || u - t > crypt__timeError) {
- syslog(LOG_INFO, "packet rejected: bad time");
- return (0);
- }
- memcpy(rpl + crp_time, buff + crq_time, 8);
- }
-
- /* --- Done --- */
-
- return (1);
-}
-
-/* --- @crypt_packReply@ --- *
- *
- * Arguments: @char *buff@ = pointer to reply block
- * @unsigned char *sk@ = pointer to session key
- * @int answer@ = yes or no
- *
- * Returns: ---
- *
- * Use: Packs and encrypts a reply block.
- */
-
-void crypt_packReply(unsigned char *buff, unsigned char *sk, int answer)
-{
- {
- /* --- Store the answer --- */
-
- buff[crp_answer] = (answer != 0);
- }
-
- {
- /* --- Build the checksum --- */
-
- md5 md;
- unsigned char mdv[MD5_HASHSIZE];
-
- md5_init(&md);
- md5_buffer(&md, buff + crp_cipher, crp_check - crp_cipher);
- md5_final(&md, mdv);
- memcpy(buff + crp_check, mdv, 4);
- burn(md); burn(mdv);
- }
-
- {
- /* --- Encrypt the buffer --- */
-
- icrypt_job j;
- icrypt_init(&j, sk, buff + crp_iv);
- crypt__dump("reply, before encryption", buff, crp_size, stdout);
- icrypt_encrypt(&j, buff + crp_cipher,
- buff + crp_cipher, crp_size - crp_cipher);
- crypt__dump("reply, after encryption", buff, crp_size, stdout);
- burn(j);
- }
-}
-
-/* --- @crypt_unpackReply@ --- *
- *
- * Arguments: @unsigned char *buff@ = pointer to reply buffer
- * @unsigned char *sk@ = pointer to session key
- * @time_t t@ = time at which request was sent
- * @pid_t pid@ = my process ID
- *
- * Returns: >0 if request granted, zero if denied, <0 if reply rejected
- *
- * Use: Unpacks a reply block, and informs the caller of the outcome.
- */
-
-int crypt_unpackReply(unsigned char *buff, unsigned char *sk,
- time_t t, pid_t pid)
-{
- {
- /* --- Decrypt my reply block --- */
-
- icrypt_job j;
- icrypt_init(&j, sk, buff + crp_iv);
- crypt__dump("reply, before decryption", buff, crp_size, stdout);
- icrypt_decrypt(&j, buff + crp_cipher,
- buff + crp_cipher, crp_size - crp_cipher);
- crypt__dump("reply, after decryption", buff, crp_size, stdout);
- burn(j);
- }
-
- {
- /* --- Check validity --- */
-
- md5 md;
- unsigned char mdv[MD5_HASHSIZE];
- char b[8];
-
- /* --- Check the checksum --- */
-
- md5_init(&md);
- md5_buffer(&md, buff + crp_cipher, crp_check - crp_cipher);
- md5_final(&md, mdv);
- if (memcmp(buff + crp_check, mdv, 4) != 0) {
- syslog(LOG_INFO, "reply rejected: bad checksum");
- return (-1);
- }
-
- /* --- Check the identifier --- */
-
- store32(b + 0, t); store32(b + 4, pid);
- if (memcmp(b, buff + crp_time, sizeof(b)) != 0) {
- syslog(LOG_INFO, "reply rejected: bad identification marker");
- return (-1);
- }
- }
-
- /* --- Return the value --- */
-
- return (buff[crp_answer]);
-}
-
-/*----- Test rig ----------------------------------------------------------*/
-
-#ifdef TEST_RIG
-
-int main(int argc, char *argv[])
-{
- time_t t = time(0);
- pid_t pid = getpid();
- unsigned char buff[8];
- unsigned char sk[IDEA_KEYSIZE], k[IDEA_KEYSIZE];
- FILE *fp;
-
- ego(argv[0]);
- if (argc < 3)
- die("bad args");
- fp = fopen(argv[1], "r");
- if (!fp)
- die("fopen: %s", strerror(errno));
- tx_getBits(k, 128, fp);
- fclose(fp);
- crypt__sessionKey(argv[2], k, t, pid, sk, buff);
- return (0);
-}
-
-#endif
-
-/*----- That's all, folks -------------------------------------------------*/