Index: isakmpd/Makefile
diff -c isakmpd/Makefile:1.1.1.2 isakmpd/Makefile:1.5
*** isakmpd/Makefile:1.1.1.2	Mon Mar 27 23:40:07 2000
--- isakmpd/Makefile	Tue Mar 28 00:17:22 2000
***************
*** 42,54 ****
  # openbsd means 2.5 or newer, openbsd-encap is for older kernels with PF_ENCAP
  # linux is the name for Linux with FreeS/WAN integrated, freebsd means FreeBSD
  # with KAME IPsec.
! OS=		openbsd
  #OS=		openbsd-encap
  #OS=		freebsd
  #OS=		linux
  
  # Compile-time configuration of otherwise optional features
! FEATURES=	tripledes blowfish cast policy x509 ec aggressive
  
  .PATH:		${.CURDIR}/sysdep/${OS}
  
--- 42,60 ----
  # openbsd means 2.5 or newer, openbsd-encap is for older kernels with PF_ENCAP
  # linux is the name for Linux with FreeS/WAN integrated, freebsd means FreeBSD
  # with KAME IPsec.
! #OS=		openbsd
  #OS=		openbsd-encap
  #OS=		freebsd
  #OS=		linux
+ OS=		netbsd
  
  # Compile-time configuration of otherwise optional features
! FEATURES=	tripledes blowfish cast ec aggressive
! .if ${OS} != openbsd
! FEATURES_U!=	echo ${FEATURES} | tr "[:lower:]" "[:upper:]"
! .else
! FEATURES_U=	${FEATURES:U}
! .endif
  
  .PATH:		${.CURDIR}/sysdep/${OS}
  
***************
*** 117,123 ****
  SUBDIR=		apps
  
  .if ${FEATURES} != ""
! CFLAGS+=	${FEATURES:U:S/^/-DUSE_/}
  .endif
  
  .include "sysdep/${OS}/Makefile.sysdep"
--- 123,129 ----
  SUBDIR=		apps
  
  .if ${FEATURES} != ""
! CFLAGS+=	${FEATURES_U:S/^/-DUSE_/}
  .endif
  
  .include "sysdep/${OS}/Makefile.sysdep"
Index: isakmpd/pf_key_v2.c
diff -c isakmpd/pf_key_v2.c:1.1.1.2 isakmpd/pf_key_v2.c:1.2
*** isakmpd/pf_key_v2.c:1.1.1.2	Mon Mar 27 23:41:25 2000
--- isakmpd/pf_key_v2.c	Tue Mar 28 00:14:07 2000
***************
*** 84,90 ****
  #define PF_KEY_V2_NODE_MALLOCED 1
  #define PF_KEY_V2_NODE_MARK 2
  
! #ifdef __FreeBSD__
  /*
   * KAME requires the sadb_msg_seq of an UPDATE be the same of that of the
   * GETSPI creating the larval SA.
--- 84,90 ----
  #define PF_KEY_V2_NODE_MALLOCED 1
  #define PF_KEY_V2_NODE_MARK 2
  
! #if defined(__FreeBSD__) || defined(__NetBSD__)
  /*
   * KAME requires the sadb_msg_seq of an UPDATE be the same of that of the
   * GETSPI creating the larval SA.
***************
*** 113,119 ****
  /* The socket to use for PF_KEY interactions.  */
  static int pf_key_v2_socket;
  
! #ifdef __FreeBSD__
  static int
  pf_key_v2_register_sa_seq (u_int8_t *spi, size_t sz, u_int8_t proto,
  			   struct sockaddr *dst, int dstlen, u_int32_t seq)
--- 113,119 ----
  /* The socket to use for PF_KEY interactions.  */
  static int pf_key_v2_socket;
  
! #if defined(__FreeBSD__) || defined(__NetBSD__)
  static int
  pf_key_v2_register_sa_seq (u_int8_t *spi, size_t sz, u_int8_t proto,
  			   struct sockaddr *dst, int dstlen, u_int32_t seq)
***************
*** 402,408 ****
  
    msg->sadb_msg_version = PF_KEY_V2;
    msg->sadb_msg_errno = 0;
! #ifdef __FreeBSD__
    if (!msg->sadb_msg_mode)
      msg->sadb_msg_mode = IPSEC_MODE_ANY;
    msg->sadb_msg_reqid = 0;	/* XXX */
--- 402,408 ----
  
    msg->sadb_msg_version = PF_KEY_V2;
    msg->sadb_msg_errno = 0;
! #if defined(__FreeBSD__) || defined(__NetBSD__)
    if (!msg->sadb_msg_mode)
      msg->sadb_msg_mode = IPSEC_MODE_ANY;
    msg->sadb_msg_reqid = 0;	/* XXX */
***************
*** 513,519 ****
  
    /* Register it to get ESP and AH acquires from the kernel.  */
    msg.sadb_msg_seq = 0;
! #ifdef __FreeBSD__
    msg.sadb_msg_mode = 0;
  #endif
    msg.sadb_msg_type = SADB_REGISTER;
--- 513,519 ----
  
    /* Register it to get ESP and AH acquires from the kernel.  */
    msg.sadb_msg_seq = 0;
! #if defined(__FreeBSD__) || defined(__NetBSD__)
    msg.sadb_msg_mode = 0;
  #endif
    msg.sadb_msg_type = SADB_REGISTER;
***************
*** 538,544 ****
    ret = 0;
  
    msg.sadb_msg_seq = 0;
! #ifdef __FreeBSD__
    msg.sadb_msg_mode = 0;
  #endif
    msg.sadb_msg_type = SADB_REGISTER;
--- 538,544 ----
    ret = 0;
  
    msg.sadb_msg_seq = 0;
! #if defined(__FreeBSD__) || defined(__NetBSD__)
    msg.sadb_msg_mode = 0;
  #endif
    msg.sadb_msg_type = SADB_REGISTER;
***************
*** 559,565 ****
  
    /* XXX Register the accepted transforms.  */
  
! #ifdef __FreeBSD__
    TAILQ_INIT (&pf_key_v2_sa_seq_map);
  #endif
  
--- 559,565 ----
  
    /* XXX Register the accepted transforms.  */
  
! #if defined(__FreeBSD__) || defined(__NetBSD__)
    TAILQ_INIT (&pf_key_v2_sa_seq_map);
  #endif
  
***************
*** 611,617 ****
     * from the acquire message.
     */
    msg.sadb_msg_seq = 0;
! #ifdef __FreeBSD__
    msg.sadb_msg_mode = IPSEC_MODE_TUNNEL;	/* XXX */
  #endif
    getspi = pf_key_v2_msg_new (&msg, 0);
--- 611,617 ----
     * from the acquire message.
     */
    msg.sadb_msg_seq = 0;
! #if defined(__FreeBSD__) || defined(__NetBSD__)
    msg.sadb_msg_mode = IPSEC_MODE_TUNNEL;	/* XXX */
  #endif
    getspi = pf_key_v2_msg_new (&msg, 0);
***************
*** 692,698 ****
    if (!spi)
      goto cleanup;
    memcpy (spi, &sa->sadb_sa_spi, *sz);
! #ifdef __FreeBSD__
    if (!pf_key_v2_register_sa_seq (spi, *sz, proto, dst, dstlen,
  				  ((struct sadb_msg *)(TAILQ_FIRST (ret)->seg))
  				  ->sadb_msg_seq))
--- 692,698 ----
    if (!spi)
      goto cleanup;
    memcpy (spi, &sa->sadb_sa_spi, *sz);
! #if defined(__FreeBSD__) || defined(__NetBSD__)
    if (!pf_key_v2_register_sa_seq (spi, *sz, proto, dst, dstlen,
  				  ((struct sadb_msg *)(TAILQ_FIRST (ret)->seg))
  				  ->sadb_msg_seq))
***************
*** 775,781 ****
        switch (iproto->auth)
  	{
  	case IPSEC_AUTH_HMAC_MD5:
! #ifdef __FreeBSD__
  	  ssa.sadb_sa_auth = SADB_AALG_MD5HMAC;
  #else
  	  ssa.sadb_sa_auth = SADB_AALG_MD5HMAC96;
--- 775,781 ----
        switch (iproto->auth)
  	{
  	case IPSEC_AUTH_HMAC_MD5:
! #if defined(__FreeBSD__) || defined(__NetBSD__)
  	  ssa.sadb_sa_auth = SADB_AALG_MD5HMAC;
  #else
  	  ssa.sadb_sa_auth = SADB_AALG_MD5HMAC96;
***************
*** 783,789 ****
  	  break;
  
  	case IPSEC_AUTH_HMAC_SHA:
! #ifdef __FreeBSD__
  	  ssa.sadb_sa_auth = SADB_AALG_SHA1HMAC;
  #else
  	  ssa.sadb_sa_auth = SADB_AALG_SHA1HMAC96;
--- 783,789 ----
  	  break;
  
  	case IPSEC_AUTH_HMAC_SHA:
! #if defined(__FreeBSD__) || defined(__NetBSD__)
  	  ssa.sadb_sa_auth = SADB_AALG_SHA1HMAC;
  #else
  	  ssa.sadb_sa_auth = SADB_AALG_SHA1HMAC96;
***************
*** 809,815 ****
        switch (proto->id)
  	{
  	case IPSEC_AH_MD5:
! #ifdef __FreeBSD__
  	  ssa.sadb_sa_auth = SADB_AALG_MD5HMAC;
  #else
  	  ssa.sadb_sa_auth = SADB_AALG_MD5HMAC96;
--- 809,815 ----
        switch (proto->id)
  	{
  	case IPSEC_AH_MD5:
! #if defined(__FreeBSD__) || defined(__NetBSD__)
  	  ssa.sadb_sa_auth = SADB_AALG_MD5HMAC;
  #else
  	  ssa.sadb_sa_auth = SADB_AALG_MD5HMAC96;
***************
*** 817,823 ****
  	  break;
  
  	case IPSEC_AH_SHA:
! #ifdef __FreeBSD__
  	  ssa.sadb_sa_auth = SADB_AALG_SHA1HMAC;
  #else
  	  ssa.sadb_sa_auth = SADB_AALG_SHA1HMAC96;
--- 817,823 ----
  	  break;
  
  	case IPSEC_AH_SHA:
! #if defined(__FreeBSD__) || defined(__NetBSD__)
  	  ssa.sadb_sa_auth = SADB_AALG_SHA1HMAC;
  #else
  	  ssa.sadb_sa_auth = SADB_AALG_SHA1HMAC96;
***************
*** 838,844 ****
      sa->transport->vtbl->get_src (sa->transport, &dst, &dstlen);
    else
      sa->transport->vtbl->get_dst (sa->transport, &dst, &dstlen);
! #ifdef __FreeBSD__
    msg.sadb_msg_seq
      = (incoming ? pf_key_v2_seq_by_sa (proto->spi[incoming],
  				       sizeof ssa.sadb_sa_spi, proto->proto,
--- 838,844 ----
      sa->transport->vtbl->get_src (sa->transport, &dst, &dstlen);
    else
      sa->transport->vtbl->get_dst (sa->transport, &dst, &dstlen);
! #if defined(__FreeBSD__) || defined(__NetBSD__)
    msg.sadb_msg_seq
      = (incoming ? pf_key_v2_seq_by_sa (proto->spi[incoming],
  				       sizeof ssa.sadb_sa_spi, proto->proto,
***************
*** 1131,1137 ****
        goto cleanup;
      }
    msg.sadb_msg_seq = 0;
! #ifdef __FreeBSD__
    msg.sadb_msg_mode = 0;
  #endif
    flow = pf_key_v2_msg_new (&msg, 0);
--- 1131,1137 ----
        goto cleanup;
      }
    msg.sadb_msg_seq = 0;
! #if defined(__FreeBSD__) || defined(__NetBSD__)
    msg.sadb_msg_mode = 0;
  #endif
    flow = pf_key_v2_msg_new (&msg, 0);
***************
*** 1317,1323 ****
    msg.sadb_msg_type = delete ? SADB_X_SPDDELETE : SADB_X_SPDADD;
    msg.sadb_msg_satype = SADB_SATYPE_UNSPEC;
    msg.sadb_msg_seq = 0;
! #ifdef __FreeBSD__
    msg.sadb_msg_mode = 0;
  #endif
    flow = pf_key_v2_msg_new (&msg, 0);
--- 1317,1323 ----
    msg.sadb_msg_type = delete ? SADB_X_SPDDELETE : SADB_X_SPDADD;
    msg.sadb_msg_satype = SADB_SATYPE_UNSPEC;
    msg.sadb_msg_seq = 0;
! #if defined(__FreeBSD__) || defined(__NetBSD__)
    msg.sadb_msg_mode = 0;
  #endif
    flow = pf_key_v2_msg_new (&msg, 0);
***************
*** 1615,1621 ****
        goto cleanup;
      }
    msg.sadb_msg_seq = 0;
! #ifdef __FreeBSD__
    msg.sadb_msg_mode = 0;
  #endif
    delete = pf_key_v2_msg_new (&msg, 0);
--- 1615,1621 ----
        goto cleanup;
      }
    msg.sadb_msg_seq = 0;
! #if defined(__FreeBSD__) || defined(__NetBSD__)
    msg.sadb_msg_mode = 0;
  #endif
    delete = pf_key_v2_msg_new (&msg, 0);
***************
*** 1889,1895 ****
        goto cleanup;
      }
    msg.sadb_msg_seq = 0;
! #ifdef __FreeBSD__
    msg.sadb_msg_mode = 0;
  #endif
    grpspis = pf_key_v2_msg_new (&msg, 0);
--- 1889,1895 ----
        goto cleanup;
      }
    msg.sadb_msg_seq = 0;
! #if defined(__FreeBSD__) || defined(__NetBSD__)
    msg.sadb_msg_mode = 0;
  #endif
    grpspis = pf_key_v2_msg_new (&msg, 0);
Index: isakmpd/apps/certpatch/Makefile
diff -c isakmpd/apps/certpatch/Makefile:1.1.1.1 isakmpd/apps/certpatch/Makefile:1.2
*** isakmpd/apps/certpatch/Makefile:1.1.1.1	Mon Mar 27 23:41:45 2000
--- isakmpd/apps/certpatch/Makefile	Tue Mar 28 00:14:08 2000
***************
*** 33,38 ****
--- 33,40 ----
  # This code was written under funding by Ericsson Radio Systems.
  #
  
+ OPSYS!=		uname -s
+ 
  PROG=		certpatch
  SRCS=		certpatch.c
  BINDIR=		/usr/sbin
***************
*** 40,48 ****
--- 42,58 ----
  TOPOBJ!=	cd ${TOPSRC}; printf "all:\n\t@pwd\n" |${MAKE} -f-
  OS!=		awk '/^OS=/ { print $$2 }' ${.CURDIR}/../../Makefile
  .PATH:		${TOPSRC} ${TOPSRC}/sysdep/${OS} ${TOPOBJ}
+ .if ${OPSYS} == "NetBSD"
+ CPPFLAGS+=	-I${TOPSRC} -I${TOPSRC}/sysdep/${OS} -I${TOPOBJ}
+ CFLAGS+=	-Wall
+ CPPFLAGS+=	-I/usr/pkg/include
+ LDADD+=		-L/usr/pkg/lib
+ .else
  CFLAGS+=	-I${TOPSRC} -I${TOPSRC}/sysdep/${OS} -I${TOPOBJ} -Wall
+ .endif
  LDADD+=		-lcrypto
  DPADD+=		${LIBCRYPTO}
  MAN=		certpatch.8
+ 
  
  .include <bsd.prog.mk>
Index: isakmpd/sysdep/common/libsysdep/Makefile
diff -c isakmpd/sysdep/common/libsysdep/Makefile:1.1.1.1 isakmpd/sysdep/common/libsysdep/Makefile:1.2
*** isakmpd/sysdep/common/libsysdep/Makefile:1.1.1.1	Mon Mar 27 23:42:03 2000
--- isakmpd/sysdep/common/libsysdep/Makefile	Tue Mar 28 00:14:09 2000
***************
*** 33,41 ****
--- 33,47 ----
  # This code was written under funding by Ericsson Radio Systems.
  #
  
+ OPSYS!=	uname -s
+ 
  LIB=		sysdep
  SRCS=		arc4random.c blowfish.c cast.c md5.c sha1.c
  NOMAN=
+ .if ${OPSYS} == "NetBSD"
+ CPPFLAGS+=	-I${.CURDIR}/.. -I/usr/include/machine
+ .else
  CFLAGS+=	-I${.CURDIR}/.. -I/usr/include/machine
+ .endif
  
  .include <bsd.lib.mk>
Index: isakmpd/sysdep/common/libsysdep/arc4random.c
diff -c isakmpd/sysdep/common/libsysdep/arc4random.c:1.1.1.1 isakmpd/sysdep/common/libsysdep/arc4random.c:1.2
*** isakmpd/sysdep/common/libsysdep/arc4random.c:1.1.1.1	Mon Mar 27 23:42:03 2000
--- isakmpd/sysdep/common/libsysdep/arc4random.c	Tue Mar 28 00:14:09 2000
***************
*** 1,176 ****
! /*	$OpenBSD: arc4random.c,v 1.3 1998/03/22 19:01:16 niklas Exp $	*/
! 
! /*
!  * Arc4 random number generator for OpenBSD.
!  * Copyright 1996 David Mazieres <dm@lcs.mit.edu>.
!  *
!  * Modification and redistribution in source and binary forms is
!  * permitted provided that due credit is given to the author and the
!  * OpenBSD project (for instance by leaving this copyright notice
!  * intact).
!  */
! 
! /*
!  * This code is derived from section 17.1 of Applied Cryptography,
!  * second edition, which describes a stream cipher allegedly
!  * compatible with RSA Labs "RC4" cipher (the actual description of
!  * which is a trade secret).  The same algorithm is used as a stream
!  * cipher called "arcfour" in Tatu Ylonen's ssh package.
!  *
!  * Here the stream cipher has been modified always to include the time
!  * when initializing the state.  That makes it impossible to
!  * regenerate the same random sequence twice, so this can't be used
!  * for encryption, but will generate good random numbers.
!  *
!  * RC4 is a registered trademark of RSA Laboratories.
!  */
! 
! #include <fcntl.h>
! #include <stdlib.h>
! #include <unistd.h>
! #include <sys/types.h>
! #include <sys/time.h>
! 
! #ifdef __GNUC__
! #define inline __inline
! #else				/* !__GNUC__ */
! #define inline
! #endif				/* !__GNUC__ */
! 
! struct arc4_stream {
! 	u_int8_t i;
! 	u_int8_t j;
! 	u_int8_t s[256];
! };
! 
! int     rs_initialized;
! static struct arc4_stream rs;
! 
! static inline void
! arc4_init(as)
! 	struct arc4_stream *as;
! {
! 	int     n;
! 
! 	for (n = 0; n < 256; n++)
! 		as->s[n] = n;
! 	as->i = 0;
! 	as->j = 0;
! }
! 
! static inline void
! arc4_addrandom(as, dat, datlen)
! 	struct arc4_stream *as;
! 	u_char *dat;
! 	int     datlen;
! {
! 	int     n;
! 	u_int8_t si;
! 
! 	as->i--;
! 	for (n = 0; n < 256; n++) {
! 		as->i = (as->i + 1);
! 		si = as->s[as->i];
! 		as->j = (as->j + si + dat[n % datlen]);
! 		as->s[as->i] = as->s[as->j];
! 		as->s[as->j] = si;
! 	}
! }
! 
! static void
! arc4_stir(as)
! 	struct arc4_stream *as;
! {
! 	int     fd;
! 	struct {
! 		struct timeval tv;
! 		u_int8_t rnd[128 - sizeof(struct timeval)];
! 	}       rdat;
! 
! 	gettimeofday(&rdat.tv, NULL);
! 	fd = open("/dev/arandom", O_RDONLY);
! 	if (fd >= 0) {
! 		read(fd, rdat.rnd, sizeof(rdat.rnd));
! 		close(fd);
! 	}
! 	/* fd < 0?  Ah, what the heck. We'll just take whatever was on the
! 	 * stack... */
! 
! 	arc4_addrandom(as, (void *) &rdat, sizeof(rdat));
! }
! 
! static inline u_int8_t
! arc4_getbyte(as)
! 	struct arc4_stream *as;
! {
! 	u_int8_t si, sj;
! 
! 	as->i = (as->i + 1);
! 	si = as->s[as->i];
! 	as->j = (as->j + si);
! 	sj = as->s[as->j];
! 	as->s[as->i] = sj;
! 	as->s[as->j] = si;
! 	return (as->s[(si + sj) & 0xff]);
! }
! 
! static inline u_int32_t
! arc4_getword(as)
! 	struct arc4_stream *as;
! {
! 	u_int32_t val;
! 	val = arc4_getbyte(as) << 24;
! 	val |= arc4_getbyte(as) << 16;
! 	val |= arc4_getbyte(as) << 8;
! 	val |= arc4_getbyte(as);
! 	return val;
! }
! 
! void
! arc4random_stir()
! {
! 	if (!rs_initialized) {
! 		arc4_init(&rs);
! 		rs_initialized = 1;
! 	}
! 	arc4_stir(&rs);
! }
! 
! void
! arc4random_addrandom(dat, datlen)
! 	u_char *dat;
! 	int     datlen;
! {
! 	if (!rs_initialized)
! 		arc4random_stir();
! 	arc4_addrandom(&rs, dat, datlen);
! }
! 
! u_int32_t
! arc4random()
! {
! 	if (!rs_initialized)
! 		arc4random_stir();
! 	return arc4_getword(&rs);
! }
! 
! #if 0
! /*-------- Test code for i386 --------*/
! #include <stdio.h>
! #include <machine/pctr.h>
! int
! main(int argc, char **argv)
! {
! 	const int iter = 1000000;
! 	int     i;
! 	pctrval v;
! 
! 	v = rdtsc();
! 	for (i = 0; i < iter; i++)
! 		arc4random();
! 	v = rdtsc() - v;
! 	v /= iter;
! 
! 	printf("%qd cycles\n", v);
! }
! #endif
--- 1,176 ----
! /*	$OpenBSD: arc4random.c,v 1.3 1998/03/22 19:01:16 niklas Exp $	*/
! 
! /*
!  * Arc4 random number generator for OpenBSD.
!  * Copyright 1996 David Mazieres <dm@lcs.mit.edu>.
!  *
!  * Modification and redistribution in source and binary forms is
!  * permitted provided that due credit is given to the author and the
!  * OpenBSD project (for instance by leaving this copyright notice
!  * intact).
!  */
! 
! /*
!  * This code is derived from section 17.1 of Applied Cryptography,
!  * second edition, which describes a stream cipher allegedly
!  * compatible with RSA Labs "RC4" cipher (the actual description of
!  * which is a trade secret).  The same algorithm is used as a stream
!  * cipher called "arcfour" in Tatu Ylonen's ssh package.
!  *
!  * Here the stream cipher has been modified always to include the time
!  * when initializing the state.  That makes it impossible to
!  * regenerate the same random sequence twice, so this can't be used
!  * for encryption, but will generate good random numbers.
!  *
!  * RC4 is a registered trademark of RSA Laboratories.
!  */
! 
! #include <fcntl.h>
! #include <stdlib.h>
! #include <unistd.h>
! #include <sys/types.h>
! #include <sys/time.h>
! 
! #ifdef __GNUC__
! #define inline __inline
! #else				/* !__GNUC__ */
! #define inline
! #endif				/* !__GNUC__ */
! 
! struct arc4_stream {
! 	u_int8_t i;
! 	u_int8_t j;
! 	u_int8_t s[256];
! };
! 
! int     rs_initialized;
! static struct arc4_stream rs;
! 
! static inline void
! arc4_init(as)
! 	struct arc4_stream *as;
! {
! 	int     n;
! 
! 	for (n = 0; n < 256; n++)
! 		as->s[n] = n;
! 	as->i = 0;
! 	as->j = 0;
! }
! 
! static inline void
! arc4_addrandom(as, dat, datlen)
! 	struct arc4_stream *as;
! 	u_char *dat;
! 	int     datlen;
! {
! 	int     n;
! 	u_int8_t si;
! 
! 	as->i--;
! 	for (n = 0; n < 256; n++) {
! 		as->i = (as->i + 1);
! 		si = as->s[as->i];
! 		as->j = (as->j + si + dat[n % datlen]);
! 		as->s[as->i] = as->s[as->j];
! 		as->s[as->j] = si;
! 	}
! }
! 
! static void
! arc4_stir(as)
! 	struct arc4_stream *as;
! {
! 	int     fd;
! 	struct {
! 		struct timeval tv;
! 		u_int8_t rnd[128 - sizeof(struct timeval)];
! 	}       rdat;
! 
! 	gettimeofday(&rdat.tv, NULL);
! 	fd = open("/dev/arandom", O_RDONLY);
! 	if (fd >= 0) {
! 		read(fd, rdat.rnd, sizeof(rdat.rnd));
! 		close(fd);
! 	}
! 	/* fd < 0?  Ah, what the heck. We'll just take whatever was on the
! 	 * stack... */
! 
! 	arc4_addrandom(as, (void *) &rdat, sizeof(rdat));
! }
! 
! static inline u_int8_t
! arc4_getbyte(as)
! 	struct arc4_stream *as;
! {
! 	u_int8_t si, sj;
! 
! 	as->i = (as->i + 1);
! 	si = as->s[as->i];
! 	as->j = (as->j + si);
! 	sj = as->s[as->j];
! 	as->s[as->i] = sj;
! 	as->s[as->j] = si;
! 	return (as->s[(si + sj) & 0xff]);
! }
! 
! static inline u_int32_t
! arc4_getword(as)
! 	struct arc4_stream *as;
! {
! 	u_int32_t val;
! 	val = arc4_getbyte(as) << 24;
! 	val |= arc4_getbyte(as) << 16;
! 	val |= arc4_getbyte(as) << 8;
! 	val |= arc4_getbyte(as);
! 	return val;
! }
! 
! void
! arc4random_stir()
! {
! 	if (!rs_initialized) {
! 		arc4_init(&rs);
! 		rs_initialized = 1;
! 	}
! 	arc4_stir(&rs);
! }
! 
! void
! arc4random_addrandom(dat, datlen)
! 	u_char *dat;
! 	int     datlen;
! {
! 	if (!rs_initialized)
! 		arc4random_stir();
! 	arc4_addrandom(&rs, dat, datlen);
! }
! 
! u_int32_t
! arc4random()
! {
! 	if (!rs_initialized)
! 		arc4random_stir();
! 	return arc4_getword(&rs);
! }
! 
! #if 0
! /*-------- Test code for i386 --------*/
! #include <stdio.h>
! #include <machine/pctr.h>
! int
! main(int argc, char **argv)
! {
! 	const int iter = 1000000;
! 	int     i;
! 	pctrval v;
! 
! 	v = rdtsc();
! 	for (i = 0; i < iter; i++)
! 		arc4random();
! 	v = rdtsc() - v;
! 	v /= iter;
! 
! 	printf("%qd cycles\n", v);
! }
! #endif
Index: isakmpd/sysdep/common/libsysdep/md5.c
diff -c isakmpd/sysdep/common/libsysdep/md5.c:1.1.1.1 isakmpd/sysdep/common/libsysdep/md5.c:1.2
*** isakmpd/sysdep/common/libsysdep/md5.c:1.1.1.1	Mon Mar 27 23:42:06 2000
--- isakmpd/sysdep/common/libsysdep/md5.c	Tue Mar 28 00:14:10 2000
***************
*** 42,51 ****
  #include <string.h>
  
  #include "md5.h"
! 
! #ifndef WIN32
  #include "endian.h"   /* sets BYTE_ORDER, LITTLE_ENDIAN, and BIG_ENDIAN */
! #endif
  
  #define HAVEMEMCOPY 1	/* use ISO C's memcpy and memset */
  
--- 42,54 ----
  #include <string.h>
  
  #include "md5.h"
! 
! #ifndef WIN32
! #ifdef __NetBSD__
! #include <sys/types.h>
! #endif
  #include "endian.h"   /* sets BYTE_ORDER, LITTLE_ENDIAN, and BIG_ENDIAN */
! #endif
  
  #define HAVEMEMCOPY 1	/* use ISO C's memcpy and memset */
  
Index: isakmpd/sysdep/common/libsysdep/sha1.c
diff -c isakmpd/sysdep/common/libsysdep/sha1.c:1.1.1.1 isakmpd/sysdep/common/libsysdep/sha1.c:1.2
*** isakmpd/sysdep/common/libsysdep/sha1.c:1.1.1.1	Mon Mar 27 23:42:06 2000
--- isakmpd/sysdep/common/libsysdep/sha1.c	Tue Mar 28 00:14:10 2000
***************
*** 19,26 ****
  
  #include <string.h>
  
! #include "sha1.h"
  #ifndef WIN32
    #include "endian.h"   /* sets BYTE_ORDER, LITTLE_ENDIAN, and BIG_ENDIAN */
  #endif
  
--- 19,29 ----
  
  #include <string.h>
  
! #include "sha1.h"
  #ifndef WIN32
+   #ifdef __NetBSD__
+     #include <sys/types.h>
+   #endif
    #include "endian.h"   /* sets BYTE_ORDER, LITTLE_ENDIAN, and BIG_ENDIAN */
  #endif
  
Index: isakmpd/sysdep/netbsd/GNUmakefile.sysdep
diff -c /dev/null isakmpd/sysdep/netbsd/GNUmakefile.sysdep:1.1
*** /dev/null	Tue Mar 28 00:28:58 2000
--- isakmpd/sysdep/netbsd/GNUmakefile.sysdep	Tue Mar 28 00:14:11 2000
***************
*** 0 ****
--- 1,46 ----
+ #	$Id: GNUmakefile.sysdep,v 1.1 2000/03/27 15:14:11 itojun Exp $
+ 
+ #
+ # Copyright (c) 1999 Håkan Olsson.  All rights reserved.
+ # Copyright (c) 1999 Niklas Hallqvist.  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. All advertising materials mentioning features or use of this software
+ #    must display the following acknowledgement:
+ #	This product includes software developed by Ericsson Radio Systems.
+ # 4. The name of the author may not 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 code was written under funding by Ericsson Radio Systems.
+ #
+ 
+ LIBGMP:=	/usr/lib/libgmp.a
+ LIBCRYPTO:=	/usr/lib/libcrypto.a
+ 
+ LDADD+=		-lgmp
+ DPADD+=		${LIBGMP}
+ 
+ IPSEC_SRCS=	pf_key_v2.c
+ IPSEC_CFLAGS=	-DUSE_PF_KEY_V2
+ 
+ USE_LIBCRYPTO=	defined
Index: isakmpd/sysdep/netbsd/Makefile.sysdep
diff -c /dev/null isakmpd/sysdep/netbsd/Makefile.sysdep:1.1
*** /dev/null	Tue Mar 28 00:28:58 2000
--- isakmpd/sysdep/netbsd/Makefile.sysdep	Tue Mar 28 00:14:11 2000
***************
*** 0 ****
--- 1,68 ----
+ #	$Id: Makefile.sysdep,v 1.1 2000/03/27 15:14:11 itojun Exp $
+ 
+ #
+ # Copyright (c) 1999 Niklas Hallqvist.  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. All advertising materials mentioning features or use of this software
+ #    must display the following acknowledgement:
+ #	This product includes software developed by Ericsson Radio Systems.
+ # 4. The name of the author may not 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 INN 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 code was written under funding by Ericsson Radio Systems.
+ #
+ 
+ LDADD+=		-lgmp ${.CURDIR}/sysdep/common/libsysdep/libsysdep.a \
+ 		-L/usr/pkg/lib 
+ DPADD+=		${LIBGMP} ${.CURDIR}/sysdep/common/libsysdep/libsysdep.a
+ CFLAGS+=	-I${.CURDIR}/sysdep/common -I/usr/include \
+ 		-I/usr/pkg/include -I/usr/pkg/include/openssl
+ 
+ IPSEC_SRCS=	pf_key_v2.c
+ IPSEC_CFLAGS=	-DUSE_PF_KEY_V2
+ 
+ USE_LIBCRYPTO=	defined
+ 
+ LDSTATIC=
+ 
+ # This is a hack in order to make sure libsysdep is built before the
+ # linkstage of isakmpd.  As a side effect the link is always done even if
+ # not necessary.  Well, I just don't care.
+ GENERATED+=	sysdep-target
+ sysdep-target:
+ 		cd ${.CURDIR}/sysdep/common/libsysdep; ${MAKE} ${.MAKEFLAGS}
+ 
+ .if make(clean)
+ SUBDIR+=	sysdep/common/libsysdep
+ .endif
+ 
+ # Kludge around bug in /usr/share/mk/bsd.subdir.mk
+ NO_REGRESS=	defined
+ 
+ obj:
+ 	mkdir obj
+ 
+ beforedepend:
+ 	rm -f ssl
+ 	ln -sf /usr/pkg/include/openssl ssl
Index: isakmpd/sysdep/netbsd/sysdep-os.h
diff -c /dev/null isakmpd/sysdep/netbsd/sysdep-os.h:1.1
*** /dev/null	Tue Mar 28 00:28:59 2000
--- isakmpd/sysdep/netbsd/sysdep-os.h	Tue Mar 28 00:14:12 2000
***************
*** 0 ****
--- 1,75 ----
+ /*	$Id: sysdep-os.h,v 1.1 2000/03/27 15:14:12 itojun Exp $	*/
+ 
+ /*
+  * Copyright (c) 1998, 1999 Niklas Hallqvist.  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. All advertising materials mentioning features or use of this software
+  *    must display the following acknowledgement:
+  *	This product includes software developed by Ericsson Radio Systems.
+  * 4. The name of the author may not 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 code was written under funding by Ericsson Radio Systems.
+  */
+ 
+ #ifndef _SYSDEP_OS_H_
+ 
+ #define _SYSDEP_OS_H_
+ 
+ #include <netinet6/ipsec.h>
+ 
+ #include <sys/types.h>
+ 
+ #if 0
+ /* We need in_addr_t, in_port_t & socklen_t.  */
+ typedef u_int32_t in_addr_t;
+ typedef u_int16_t in_port_t;
+ typedef u_int32_t socklen_t;
+ #endif
+ 
+ #if 0
+ /* OpenSSL differs from OpenBSD very slightly... */
+ 
+ #define SHA1Init SHA1_Init
+ #define SHA1Update SHA1_Update
+ #define SHA1Final SHA1_Final
+ 
+ #define cast_key CAST_KEY
+ #define cast_setkey(k, d, l) CAST_set_key ((k), (l), (d))
+ #define cast_encrypt(k, i, o) do { \
+   memcpy ((o), (i), BLOCKSIZE); \
+   CAST_encrypt ((CAST_LONG *)(o), (k)); \
+ }
+ #define cast_decrypt(k, i, o) do { \
+   memcpy ((o), (i), BLOCKSIZE); \
+   CAST_decrypt ((CAST_LONG *)(o), (k)); \
+ }
+ #endif
+ 
+ #ifndef TAILQ_LAST
+ #define TAILQ_LAST(head, headname) \
+ 	(*(((struct headname *)((head)->tqh_last))->tqh_last))
+ #endif
+ 
+ #endif /* _SYSDEP_OS_H_ */
Index: isakmpd/sysdep/netbsd/sysdep.c
diff -c /dev/null isakmpd/sysdep/netbsd/sysdep.c:1.1
*** /dev/null	Tue Mar 28 00:28:59 2000
--- isakmpd/sysdep/netbsd/sysdep.c	Tue Mar 28 00:14:12 2000
***************
*** 0 ****
--- 1,205 ----
+ /*	$Id: sysdep.c,v 1.1 2000/03/27 15:14:12 itojun Exp $	*/
+ 
+ /*
+  * Copyright (c) 1998, 1999 Niklas Hallqvist.  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. All advertising materials mentioning features or use of this software
+  *    must display the following acknowledgement:
+  *	This product includes software developed by Ericsson Radio Systems.
+  * 4. The name of the author may not 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 code was written under funding by Ericsson Radio Systems.
+  */
+ 
+ #include <sys/types.h>
+ #include <sys/socket.h>
+ #include <netinet/in.h>
+ #include <arpa/inet.h>
+ #include <stdlib.h>
+ #include <string.h>
+ 
+ #include "sysdep.h"
+ 
+ #include "util.h"
+ 
+ #ifdef NEED_SYSDEP_APP
+ #include "app.h"
+ #include "conf.h"
+ #include "ipsec.h"
+ 
+ #ifdef USE_PF_KEY_V2
+ #include "pf_key_v2.h"
+ #define KEY_API(x) pf_key_v2_##x
+ #else
+ #include <net/encap.h>
+ #include "pf_encap.h"
+ #define KEY_API(x) pf_encap_##x
+ #endif
+ 
+ #endif NEED_SYSDEP_APP
+ #include "log.h"
+ 
+ extern char *__progname;
+ 
+ /*
+  * An as strong as possible random number generator, reverting to a
+  * deterministic pseudo-random one if regrand is set.
+  */
+ u_int32_t
+ sysdep_random ()
+ {
+   return random();
+ }
+ 
+ /* Return the basename of the command used to invoke us.  */
+ char *
+ sysdep_progname ()
+ {
+   return __progname;
+ }
+ 
+ /* As regress/ use this file I protect the sysdep_app_* stuff like this.  */
+ #ifdef NEED_SYSDEP_APP
+ /*
+  * Prepare the application we negotiate SAs for (i.e. the IPsec stack)
+  * for communication.  We return a file descriptor useable to select(2) on.
+  */
+ int
+ sysdep_app_open ()
+ {
+   return KEY_API(open) ();
+ }
+ 
+ /*
+  * When select(2) has noticed our application needs attendance, this is what
+  * gets called.  FD is the file descriptor causing the alarm.
+  */
+ void
+ sysdep_app_handler (int fd)
+ {
+   KEY_API (handler) (fd);
+ }
+ 
+ /* Check that the connection named NAME is active, or else make it active.  */
+ void
+ sysdep_connection_check (char *name)
+ {
+   KEY_API (connection_check) (name);
+ }
+ 
+ /*
+  * Generate a SPI for protocol PROTO and the source/destination pair given by
+  * SRC, SRCLEN, DST & DSTLEN.  Stash the SPI size in SZ.
+  */
+ u_int8_t *
+ sysdep_ipsec_get_spi (size_t *sz, u_int8_t proto, struct sockaddr *src,
+ 		      int srclen, struct sockaddr *dst, int dstlen)
+ {
+   if (app_none)
+     {
+       *sz = IPSEC_SPI_SIZE;
+       /* XXX should be random instead I think.  */
+       return strdup ("\x12\x34\x56\x78");
+     }
+   return KEY_API (get_spi) (sz, proto, src, srclen, dst, dstlen);
+ }
+ 
+ /* Force communication on socket FD to go in the clear.  */
+ int
+ sysdep_cleartext (int fd)
+ {
+ #if 0
+   int level;
+ #endif
+ 
+   if (app_none)
+     return 0;
+ 
+ #if 0
+   /*
+    * Need to bypass system security policy, so I can send and
+    * receive key management datagrams in the clear.
+    */
+   level = IPSEC_LEVEL_BYPASS;
+   if (setsockopt (fd, IPPROTO_IP, IP_AUTH_LEVEL, (char *)&level, sizeof level)
+       == -1)
+     {
+       log_error ("sysdep_cleartext: "
+ 		 "setsockopt (%d, IPPROTO_IP, IP_AUTH_LEVEL, ...) failed", fd);
+       return -1;
+     }
+   if (setsockopt (fd, IPPROTO_IP, IP_ESP_TRANS_LEVEL, (char *)&level,
+ 		  sizeof level) == -1)
+     {
+       log_error ("sysdep_cleartext: "
+ 		 "setsockopt (%d, IPPROTO_IP, IP_ESP_TRANS_LEVEL, ...) "
+ 		 "failed", fd);
+       return -1;
+     }
+   if (setsockopt (fd, IPPROTO_IP, IP_ESP_NETWORK_LEVEL, (char *)&level,
+ 		  sizeof level) == -1)
+     {
+       log_error("sysdep_cleartext: "
+ 		"setsockopt (%d, IPPROTO_IP, IP_ESP_NETWORK_LEVEL, ...) "
+ 		 "failed", fd);
+       return -1;
+     }
+ #endif
+   return 0;
+ }
+ 
+ int
+ sysdep_ipsec_delete_spi (struct sa *sa, struct proto *proto, int incoming)
+ {
+   if (app_none)
+     return 0;
+   return KEY_API (delete_spi) (sa, proto, incoming);
+ }
+ 
+ int
+ sysdep_ipsec_enable_sa (struct sa *sa)
+ {
+   if (app_none)
+     return 0;
+   return KEY_API (enable_sa) (sa);
+ }
+ 
+ int
+ sysdep_ipsec_group_spis (struct sa *sa, struct proto *proto1,
+ 			 struct proto *proto2, int incoming)
+ {
+   if (app_none)
+     return 0;
+   return KEY_API (group_spis) (sa, proto1, proto2, incoming);
+ }
+ 
+ int
+ sysdep_ipsec_set_spi (struct sa *sa, struct proto *proto, int incoming)
+ {
+   if (app_none)
+     return 0;
+   return KEY_API (set_spi) (sa, proto, incoming);
+ }
+ #endif
