/*	$CoreSDI: srpp.c,v 1.7 2001/12/31 19:45:43 claudio Exp $	*/

/*
 *   Copyright (c) 2000, 2001, Core SDI S.A., Argentina
 *   All rights reserved
 *  
 *   Redistribution and use in source and binary forms, with or without
 *   modification, are permitted provided that the following conditions
 *   are met:
 *  
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *   3. Neither name of the Core SDI S.A. nor the names of its contributors
 *      may be used to endorse or promote products derived from this software
 *      without specific prior written permission.
 *  
 *   THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 *   IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 *   OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 *   IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 *   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 *   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 *   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * This program prints on standard output the SRPPass
 * resource data for a given plain text password.
 *
 * Author: Claudio Castiglia <claudio@core-sdi.com>
 *
 * Compiles with:
 *   cc srpp.c -o srpp -I../../../include -L../../../lib -lcommon -lcrypto
 */

#include <sys/param.h>
#include <sys/uio.h>

#include <ctype.h>
#include <err.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <syslog.h>
#include <unistd.h>
#include <openssl/bn.h>
#include <openssl/err.h>
#include <openssl/evp.h>

#ifdef __OpenBSD__
#include <sha1.h>
#else
#include <openssl/sha.h>
#define SHA1_DIGESTSIZE		SHA_DIGEST_LENGTH
#define SHA1_CTX		SHA_CTX
#define SHA1Init		SHA1_Init
#define SHA1Update		SHA1_Update
#define SHA1Final		SHA1_Final
#endif

#include "sysdep.h"
#include "packet.h"
#include "resource.h"
#include "modtypes.h"
#include "commands.h"
#include "authargs.h"
#include "log.h"

#ifndef SRP_PASS_RNAME
#define SRP_PASS_RNAME		"SRPPass"	/* Verifier n salt resource */
#endif

#define NBITS			2048
#define NBYTES			(NBITS >> 3)
#define ERR_STRLEN		121		/* See ERR_error_string(3) */

static const unsigned char prime[] = {
	0xED, 0x27, 0x7F, 0x57, 0xA3, 0x1A, 0x3F, 0x7C,
	0x63, 0xBC, 0xC6, 0x72, 0xE8, 0xE3, 0xF7, 0xA5,
	0xD3, 0x03, 0x2E, 0xC2, 0xA6, 0x9E, 0x00, 0x95,
	0xFD, 0xA8, 0xA2, 0x32, 0xCD, 0x4C, 0x93, 0x28,
	0xC5, 0x96, 0xAE, 0x23, 0xA9, 0xA2, 0x79, 0x81,
	0x75, 0x58, 0x5E, 0x88, 0x84, 0x85, 0xAB, 0x2A,
	0x6B, 0x39, 0x8A, 0x06, 0x4A, 0x15, 0x13, 0x5D,
	0xE2, 0x14, 0xB7, 0x8B, 0xF1, 0x94, 0xF6, 0xC0,
	0x01, 0xB5, 0xDF, 0xFE, 0xC1, 0x5A, 0xB7, 0xA1,
	0x21, 0x82, 0x5D, 0xF2, 0xCC, 0x11, 0x5A, 0x65,
	0x90, 0xE5, 0x19, 0xBD, 0x96, 0x3B, 0xC7, 0x9B,
	0xB0, 0x8C, 0x90, 0x90, 0xE5, 0x21, 0x1F, 0xCE,
	0x1B, 0x8B, 0x78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A, 0x92, 0x9F, 0x99,
	0xE2, 0x5E, 0x78, 0x17, 0x1C, 0xF5, 0x4E, 0x30,
	0x1C, 0x99, 0xA5, 0x69, 0x9C, 0x2C, 0x5F, 0xC0,
	0x0E, 0x4B, 0xB8, 0xA0, 0x70, 0x49, 0x84, 0xB4,
	0x90, 0x55, 0xA9, 0x0A, 0xAA, 0x05, 0xC7, 0xA8,
	0x79, 0x18, 0xC4, 0xD3, 0x77, 0x8E, 0xB5, 0xAB,
	0xA8, 0x41, 0xC8, 0x0B, 0xE3, 0x41, 0xF6, 0xF5,
	0xFF, 0x1B, 0x7E, 0x69, 0x56, 0x79, 0x83, 0xB7
};

static BIGNUM	p;				/* Prime module	*/
static BIGNUM	pm1;				/* p - 1	*/
static BIGNUM 	g;				/* Generator	*/


/*
 * _fatal_bn():
 *	Log fatal bn (big number) library error and exit(3).
 */
static void
_fatal_bn(const char *cname)
{
	char errb[ERR_STRLEN];

	ERR_error_string_n(ERR_get_error(), errb, sizeof(errb));
	fatal(EX_SOFTWARE, "%s: %s.", cname, errb);
}


/*
 * _rand_bn():
 *	Generate a random big number greater than 1 and less than n - 1.
 */
static void
_rand_bn(BIGNUM *bn, BIGNUM *tmp, BN_CTX *bnctx)
{
	do {
		if (!BN_rand(tmp, NBITS, 0, 0) ||
		    !BN_mod(bn, tmp, &pm1, bnctx))
			_fatal_bn("Can't create random big number.");
	} while (BN_is_zero(bn) || BN_is_one(bn));
}


/*
 * init()
 *	Initialization function, called once by dlopen(3).
 */
void
init()
{
	BN_init(&g);
	BN_init(&p);
	BN_init(&pm1);

	if (BN_bin2bn(prime, sizeof(prime), &p) == NULL ||
	    BN_bin2bn(prime, sizeof(prime), &pm1) == NULL ||
	    BN_bin2bn(generator, sizeof(generator), &g) == NULL ||
	    !BN_sub_word(&pm1, (BN_ULONG)1))
		_fatal_bn("_init()");
}


/*
 * finit()
 *	Finalization function, called once by last dlclose(3).
 */
void
finit()
{
	BN_free(&p);
	BN_free(&g);
	BN_free(&pm1);
}


/*
 * filter_resource()
 *	Detect a SRP_PASS_RNAME resource and code its correct value;
 *	return 0 on success and -1 on error and set errno.
 *	On fatal errors program is terminated.
 */
static int
_filter_resource(AUTHCON *ct, struct autharg_filter *args)
{
	SHA1_CTX	sha1_ctx;
	BN_CTX		bnctx;
	BIGNUM		salt, verifier, x, tmp;
	unsigned char	bsalt[NBYTES], xdigest[SHA1_DIGESTSIZE];

	if (args->rname == NULL || args->value == NULL || !args->vsize) {
		errno = EINVAL;
		return (-1);
	}

	/* Just filter srp passwords */
	if (!strcmp(args->rname, SRP_PASS_RNAME)) {

		BN_CTX_init(&bnctx);
		BN_init(&salt);
		BN_init(&x);
		BN_init(&verifier);
		BN_init(&tmp);

		/*
		 * Calculate:
		 *	salt = rand() % p
		 *	x = HASH(salt, password) % (p - 1)
		 *	verifier = g exp x
		 */
		_rand_bn(&salt, &tmp, &bnctx);
		BN_bn2bin(&salt, bsalt);
		SHA1Init(&sha1_ctx);
		SHA1Update(&sha1_ctx, bsalt, sizeof(bsalt));
		SHA1Update(&sha1_ctx, args->value, args->vsize);
		SHA1Final(xdigest, &sha1_ctx);
		if (BN_bin2bn(xdigest, sizeof(xdigest), &tmp) == NULL ||
		    !BN_mod(&x, &tmp, &pm1, &bnctx) ||
		    !BN_mod_exp(&verifier, &g, &x, &p, &bnctx))
			_fatal_bn("_filter_resource()");

		/* Get memory for the filtered value */
		args->fvsize = 2 * NBYTES;
		args->fvalue = malloc(args->fvsize);
		if (args->fvalue != NULL) {
			/*
			 * The resource value is the binary form of
			 * both salt and verifier data concatenated.
			 */
			memcpy(args->fvalue, bsalt, sizeof(bsalt));
			BN_bn2bin(&verifier, args->fvalue + sizeof(bsalt));
		}

		BN_clear_free(&tmp);
		BN_clear_free(&verifier);
		BN_clear_free(&x);
		BN_clear_free(&salt);
		BN_CTX_free(&bnctx);

		if (args->fvalue == NULL)
			return (-1);
	}

	return (0);
}


/*
 * main()
 */
int
main(int argc, char **argv)
{
	extern char		*__progname;
	struct autharg_filter	 f;
	int			 i, j;

	if (argc < 2) {
		fprintf(stderr, "Usage: %s <password>\n", __progname);
		exit(-1);
	}

	init();
	f.rname = SRP_PASS_RNAME;
	f.value = argv[1];
	f.vsize = strlen(argv[1]);
	if (_filter_resource(NULL, &f) < 0)
		err(-1, "Can't filter resource");
	finit();

	printf("[ " SRP_PASS_RNAME " ]");
	for (i = 0; i < f.fvsize; i += 16) {
		printf("\n\t");
		for (j = 0; j < 16; j++)
			printf("\\%02X", *((unsigned char *) f.fvalue + j + i));
	}
	
	printf("\n");
	free(f.fvalue);
	return (0);
}
